Keep Avahi code inside mdns_avahi.c

This commit is contained in:
Julien BLACHE 2010-09-18 17:16:29 +02:00
parent 7ba3b23f63
commit 8411aa48b7
4 changed files with 66 additions and 54 deletions

@ -2,9 +2,9 @@
#ifndef __MDNS_H__ #ifndef __MDNS_H__
#define __MDNS_H__ #define __MDNS_H__
#include <avahi-common/strlst.h> #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 */ /* mDNS interface functions */
/* Call only from the main thread */ /* Call only from the main thread */

@ -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, const char *name, const char *type, const char *domain, const char *hostname, const AvahiAddress *addr,
uint16_t port, AvahiStringList *txt, AvahiLookupResultFlags flags, void *userdata) uint16_t port, AvahiStringList *txt, AvahiLookupResultFlags flags, void *userdata)
{ {
struct mdns_browser *mb;
char address[AVAHI_ADDRESS_STR_MAX + IF_NAMESIZE + 2]; char address[AVAHI_ADDRESS_STR_MAX + IF_NAMESIZE + 2];
char ifname[IF_NAMESIZE]; char ifname[IF_NAMESIZE];
struct keyval txt_kv;
struct mdns_browser *mb;
char *key;
char *value;
size_t len; size_t len;
int family; int family;
int ll; int ll;
@ -440,9 +443,46 @@ browse_resolve_callback(AvahiServiceResolver *r, AvahiIfIndex intf, AvahiProtoco
break; 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 */ /* Execute callback (mb->cb) with all the data */
if (family != -1) mb->cb(name, type, domain, hostname, family, address, port, &txt_kv);
mb->cb(name, type, domain, hostname, family, address, port, txt);
out_clear_txt_kv:
keyval_clear(&txt_kv);
break; break;
} }

@ -43,8 +43,6 @@
# include <sys/eventfd.h> # include <sys/eventfd.h>
#endif #endif
#include <avahi-common/malloc.h>
#include <event.h> #include <event.h>
#include <gcrypt.h> #include <gcrypt.h>
@ -2883,19 +2881,16 @@ player_set_update_handler(player_status_handler handler)
/* Thread: main (mdns) */ /* Thread: main (mdns) */
static void 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; struct player_status status;
AvahiStringList *p;
struct raop_device *rd; struct raop_device *rd;
struct raop_device *prev; struct raop_device *prev;
cfg_t *apex; cfg_t *apex;
const char *p;
char *at_name; char *at_name;
char *password; char *password;
char *key;
char *val;
uint64_t id; uint64_t id;
size_t valsz;
int has_password; int has_password;
int encrypt; int encrypt;
int last_active; int last_active;
@ -2999,7 +2994,7 @@ raop_device_cb(const char *name, const char *type, const char *domain, const cha
} }
else else
{ {
p = avahi_string_list_find(txt, "tp"); p = keyval_get(txt, "tp");
if (!p) if (!p)
{ {
DPRINTF(E_LOG, L_PLAYER, "AirTunes %s: no tp field in TXT record!\n", name); 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; return;
} }
avahi_string_list_get_pair(p, &key, &val, &valsz); if (*p == '\0')
avahi_free(key);
if (!val)
{ {
DPRINTF(E_LOG, L_PLAYER, "AirTunes %s: tp has no value\n", name); DPRINTF(E_LOG, L_PLAYER, "AirTunes %s: tp has no value\n", name);
return; 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; return;
} }
avahi_free(val);
password = NULL; password = NULL;
p = avahi_string_list_find(txt, "pw"); p = keyval_get(txt, "pw");
if (!p) if (!p)
{ {
DPRINTF(E_LOG, L_PLAYER, "AirTunes %s: no pw field in TXT record!\n", name); 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; return;
} }
avahi_string_list_get_pair(p, &key, &val, &valsz); if (*p == '\0')
avahi_free(key);
if (!val)
{ {
DPRINTF(E_LOG, L_PLAYER, "AirTunes %s: pw has no value\n", name); DPRINTF(E_LOG, L_PLAYER, "AirTunes %s: pw has no value\n", name);
return; return;
} }
has_password = (strcmp(val, "false") != 0); has_password = (strcmp(p, "false") != 0);
avahi_free(val);
if (has_password) if (has_password)
{ {
@ -3061,7 +3047,7 @@ raop_device_cb(const char *name, const char *type, const char *domain, const cha
} }
encrypt = 1; encrypt = 1;
p = avahi_string_list_find(txt, "am"); p = keyval_get(txt, "am");
if (!p) if (!p)
{ {
DPRINTF(E_LOG, L_PLAYER, "AirTunes %s: no am field in TXT record!\n", name); 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; goto no_am;
} }
avahi_string_list_get_pair(p, &key, &val, &valsz); if (*p == '\0')
avahi_free(key);
if (!val)
{ {
DPRINTF(E_LOG, L_PLAYER, "AirTunes %s: am has no value\n", name); DPRINTF(E_LOG, L_PLAYER, "AirTunes %s: am has no value\n", name);
goto no_am; goto no_am;
} }
if (strncmp(val, "AppleTV", strlen("AppleTV")) == 0) if (strncmp(p, "AppleTV", strlen("AppleTV")) == 0)
encrypt = 0; encrypt = 0;
avahi_free(val);
no_am: no_am:
pthread_mutex_lock(&dev_lck); pthread_mutex_lock(&dev_lck);

@ -37,6 +37,7 @@
#include <string.h> #include <string.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdint.h> #include <stdint.h>
#include <inttypes.h>
#include <errno.h> #include <errno.h>
#include <pthread.h> #include <pthread.h>
@ -45,8 +46,6 @@
# include <sys/eventfd.h> # include <sys/eventfd.h>
#endif #endif
#include <avahi-common/malloc.h>
#include <event.h> #include <event.h>
#include "evhttp/evhttp.h" #include "evhttp/evhttp.h"
@ -671,14 +670,11 @@ pairing_cb(int fd, short event, void *arg)
/* Thread: main (mdns) */ /* Thread: main (mdns) */
static void 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 *devname;
char *paircode; char *paircode;
char *key;
char *val;
size_t valsz;
int ret; int ret;
if (port < 0) if (port < 0)
@ -696,7 +692,7 @@ touch_remote_cb(const char *name, const char *type, const char *domain, const ch
else else
{ {
/* Get device name (DvNm field in TXT record) */ /* Get device name (DvNm field in TXT record) */
p = avahi_string_list_find(txt, "DvNm"); p = keyval_get(txt, "DvNm");
if (!p) if (!p)
{ {
DPRINTF(E_LOG, L_REMOTE, "Remote %s: no DvNm in TXT record!\n", name); 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; return;
} }
avahi_string_list_get_pair(p, &key, &val, &valsz); if (*p == '\0')
avahi_free(key);
if (!val)
{ {
DPRINTF(E_LOG, L_REMOTE, "Remote %s: DvNm has no value\n", name); DPRINTF(E_LOG, L_REMOTE, "Remote %s: DvNm has no value\n", name);
return; return;
} }
devname = strndup(val, valsz); devname = strdup(p);
avahi_free(val);
if (!devname) if (!devname)
{ {
DPRINTF(E_LOG, L_REMOTE, "Out of memory for device name\n"); 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) */ /* Get pairing code (Pair field in TXT record) */
p = avahi_string_list_find(txt, "Pair"); p = keyval_get(txt, "Pair");
if (!p) if (!p)
{ {
DPRINTF(E_LOG, L_REMOTE, "Remote %s: no Pair in TXT record!\n", name); 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; return;
} }
avahi_string_list_get_pair(p, &key, &val, &valsz); if (*p == '\0')
avahi_free(key);
if (!val)
{ {
DPRINTF(E_LOG, L_REMOTE, "Remote %s: Pair has no value\n", name); 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; return;
} }
paircode = strndup(val, valsz); paircode = strdup(p);
avahi_free(val);
if (!paircode) if (!paircode)
{ {
DPRINTF(E_LOG, L_REMOTE, "Out of memory for paircode\n"); DPRINTF(E_LOG, L_REMOTE, "Out of memory for paircode\n");