From 8411aa48b7d618aba01ea1eb8af01bf599ddd637 Mon Sep 17 00:00:00 2001 From: Julien BLACHE Date: Sat, 18 Sep 2010 17:16:29 +0200 Subject: [PATCH] Keep Avahi code inside mdns_avahi.c --- src/mdns.h | 4 ++-- src/mdns_avahi.c | 46 +++++++++++++++++++++++++++++++++++++++++--- src/player.c | 42 ++++++++++++---------------------------- src/remote_pairing.c | 28 +++++++++------------------ 4 files changed, 66 insertions(+), 54 deletions(-) diff --git a/src/mdns.h b/src/mdns.h index fb22bdd8..4c76defc 100644 --- a/src/mdns.h +++ b/src/mdns.h @@ -2,9 +2,9 @@ #ifndef __MDNS_H__ #define __MDNS_H__ -#include +#include "misc.h" -typedef void (* mdns_browse_cb)(const char *name, const char *type, const char *domain, const char *hostname, int family, const char *address, int port, AvahiStringList *txt); +typedef void (* mdns_browse_cb)(const char *name, const char *type, const char *domain, const char *hostname, int family, const char *address, int port, struct keyval *txt); /* mDNS interface functions */ /* Call only from the main thread */ diff --git a/src/mdns_avahi.c b/src/mdns_avahi.c index 5dcb976f..83d94d1b 100644 --- a/src/mdns_avahi.c +++ b/src/mdns_avahi.c @@ -365,9 +365,12 @@ browse_resolve_callback(AvahiServiceResolver *r, AvahiIfIndex intf, AvahiProtoco const char *name, const char *type, const char *domain, const char *hostname, const AvahiAddress *addr, uint16_t port, AvahiStringList *txt, AvahiLookupResultFlags flags, void *userdata) { - struct mdns_browser *mb; char address[AVAHI_ADDRESS_STR_MAX + IF_NAMESIZE + 2]; char ifname[IF_NAMESIZE]; + struct keyval txt_kv; + struct mdns_browser *mb; + char *key; + char *value; size_t len; int family; int ll; @@ -440,9 +443,46 @@ browse_resolve_callback(AvahiServiceResolver *r, AvahiIfIndex intf, AvahiProtoco break; } + if (family == -1) + break; + + memset(&txt_kv, 0, sizeof(struct keyval)); + + while (txt) + { + len = avahi_string_list_get_size(txt); + key = (char *)avahi_string_list_get_text(txt); + + value = memchr(key, '=', len); + if (!value) + { + value = ""; + len = 0; + } + else + { + *value = '\0'; + value++; + + len -= strlen(key) + 1; + } + + ret = keyval_add_size(&txt_kv, key, value, len); + if (ret < 0) + { + DPRINTF(E_LOG, L_MDNS, "Could not build TXT record keyval\n"); + + goto out_clear_txt_kv; + } + + txt = avahi_string_list_get_next(txt); + } + /* Execute callback (mb->cb) with all the data */ - if (family != -1) - mb->cb(name, type, domain, hostname, family, address, port, txt); + mb->cb(name, type, domain, hostname, family, address, port, &txt_kv); + + out_clear_txt_kv: + keyval_clear(&txt_kv); break; } diff --git a/src/player.c b/src/player.c index 372325a7..81223464 100644 --- a/src/player.c +++ b/src/player.c @@ -43,8 +43,6 @@ # include #endif -#include - #include #include @@ -2883,19 +2881,16 @@ player_set_update_handler(player_status_handler handler) /* Thread: main (mdns) */ static void -raop_device_cb(const char *name, const char *type, const char *domain, const char *hostname, int family, const char *address, int port, AvahiStringList *txt) +raop_device_cb(const char *name, const char *type, const char *domain, const char *hostname, int family, const char *address, int port, struct keyval *txt) { struct player_status status; - AvahiStringList *p; struct raop_device *rd; struct raop_device *prev; cfg_t *apex; + const char *p; char *at_name; char *password; - char *key; - char *val; uint64_t id; - size_t valsz; int has_password; int encrypt; int last_active; @@ -2999,7 +2994,7 @@ raop_device_cb(const char *name, const char *type, const char *domain, const cha } else { - p = avahi_string_list_find(txt, "tp"); + p = keyval_get(txt, "tp"); if (!p) { DPRINTF(E_LOG, L_PLAYER, "AirTunes %s: no tp field in TXT record!\n", name); @@ -3007,27 +3002,22 @@ raop_device_cb(const char *name, const char *type, const char *domain, const cha return; } - avahi_string_list_get_pair(p, &key, &val, &valsz); - avahi_free(key); - if (!val) + if (*p == '\0') { DPRINTF(E_LOG, L_PLAYER, "AirTunes %s: tp has no value\n", name); return; } - if (!strstr(val, "UDP")) + if (!strstr(p, "UDP")) { - DPRINTF(E_LOG, L_PLAYER, "AirTunes %s: device does not support AirTunes v2 (tp=%s), discarding\n", name, val); + DPRINTF(E_LOG, L_PLAYER, "AirTunes %s: device does not support AirTunes v2 (tp=%s), discarding\n", name, p); - avahi_free(val); return; } - avahi_free(val); - password = NULL; - p = avahi_string_list_find(txt, "pw"); + p = keyval_get(txt, "pw"); if (!p) { DPRINTF(E_LOG, L_PLAYER, "AirTunes %s: no pw field in TXT record!\n", name); @@ -3035,18 +3025,14 @@ raop_device_cb(const char *name, const char *type, const char *domain, const cha return; } - avahi_string_list_get_pair(p, &key, &val, &valsz); - avahi_free(key); - if (!val) + if (*p == '\0') { DPRINTF(E_LOG, L_PLAYER, "AirTunes %s: pw has no value\n", name); return; } - has_password = (strcmp(val, "false") != 0); - - avahi_free(val); + has_password = (strcmp(p, "false") != 0); if (has_password) { @@ -3061,7 +3047,7 @@ raop_device_cb(const char *name, const char *type, const char *domain, const cha } encrypt = 1; - p = avahi_string_list_find(txt, "am"); + p = keyval_get(txt, "am"); if (!p) { DPRINTF(E_LOG, L_PLAYER, "AirTunes %s: no am field in TXT record!\n", name); @@ -3069,20 +3055,16 @@ raop_device_cb(const char *name, const char *type, const char *domain, const cha goto no_am; } - avahi_string_list_get_pair(p, &key, &val, &valsz); - avahi_free(key); - if (!val) + if (*p == '\0') { DPRINTF(E_LOG, L_PLAYER, "AirTunes %s: am has no value\n", name); goto no_am; } - if (strncmp(val, "AppleTV", strlen("AppleTV")) == 0) + if (strncmp(p, "AppleTV", strlen("AppleTV")) == 0) encrypt = 0; - avahi_free(val); - no_am: pthread_mutex_lock(&dev_lck); diff --git a/src/remote_pairing.c b/src/remote_pairing.c index bd384f7e..560def11 100644 --- a/src/remote_pairing.c +++ b/src/remote_pairing.c @@ -37,6 +37,7 @@ #include #include #include +#include #include #include @@ -45,8 +46,6 @@ # include #endif -#include - #include #include "evhttp/evhttp.h" @@ -671,14 +670,11 @@ pairing_cb(int fd, short event, void *arg) /* Thread: main (mdns) */ static void -touch_remote_cb(const char *name, const char *type, const char *domain, const char *hostname, int family, const char *address, int port, AvahiStringList *txt) +touch_remote_cb(const char *name, const char *type, const char *domain, const char *hostname, int family, const char *address, int port, struct keyval *txt) { - AvahiStringList *p; + const char *p; char *devname; char *paircode; - char *key; - char *val; - size_t valsz; int ret; if (port < 0) @@ -696,7 +692,7 @@ touch_remote_cb(const char *name, const char *type, const char *domain, const ch else { /* Get device name (DvNm field in TXT record) */ - p = avahi_string_list_find(txt, "DvNm"); + p = keyval_get(txt, "DvNm"); if (!p) { DPRINTF(E_LOG, L_REMOTE, "Remote %s: no DvNm in TXT record!\n", name); @@ -704,17 +700,14 @@ touch_remote_cb(const char *name, const char *type, const char *domain, const ch return; } - avahi_string_list_get_pair(p, &key, &val, &valsz); - avahi_free(key); - if (!val) + if (*p == '\0') { DPRINTF(E_LOG, L_REMOTE, "Remote %s: DvNm has no value\n", name); return; } - devname = strndup(val, valsz); - avahi_free(val); + devname = strdup(p); if (!devname) { DPRINTF(E_LOG, L_REMOTE, "Out of memory for device name\n"); @@ -723,7 +716,7 @@ touch_remote_cb(const char *name, const char *type, const char *domain, const ch } /* Get pairing code (Pair field in TXT record) */ - p = avahi_string_list_find(txt, "Pair"); + p = keyval_get(txt, "Pair"); if (!p) { DPRINTF(E_LOG, L_REMOTE, "Remote %s: no Pair in TXT record!\n", name); @@ -732,9 +725,7 @@ touch_remote_cb(const char *name, const char *type, const char *domain, const ch return; } - avahi_string_list_get_pair(p, &key, &val, &valsz); - avahi_free(key); - if (!val) + if (*p == '\0') { DPRINTF(E_LOG, L_REMOTE, "Remote %s: Pair has no value\n", name); @@ -742,8 +733,7 @@ touch_remote_cb(const char *name, const char *type, const char *domain, const ch return; } - paircode = strndup(val, valsz); - avahi_free(val); + paircode = strdup(p); if (!paircode) { DPRINTF(E_LOG, L_REMOTE, "Out of memory for paircode\n");