Stop the TXT record encoding madness

Use a proper string array to pass the TXT record fields, get rid of the
encoding stuff.

Changes the plugin API.
This commit is contained in:
Julien BLACHE 2009-04-08 16:35:59 +02:00
parent 2e2dbc6964
commit 3ec2cc0f00
6 changed files with 43 additions and 83 deletions

View File

@ -69,7 +69,7 @@ typedef struct tag_plugin_transcode_fn {
/* info for rendezvous advertising */ /* info for rendezvous advertising */
typedef struct tag_plugin_rend_info { typedef struct tag_plugin_rend_info {
char *type; char *type;
char *txt; char **txt;
} PLUGIN_REND_INFO; } PLUGIN_REND_INFO;
/* main info struct that plugins must provide */ /* main info struct that plugins must provide */

View File

@ -121,37 +121,9 @@ struct event_base *evbase_main;
static void usage(char *program); static void usage(char *program);
static void main_handler(WS_CONNINFO *pwsc); static void main_handler(WS_CONNINFO *pwsc);
static int main_auth(WS_CONNINFO *pwsc, char *username, char *password); static int main_auth(WS_CONNINFO *pwsc, char *username, char *password);
static void txt_add(char *txtrecord, char *fmt, ...);
static void main_io_errhandler(int level, char *msg); static void main_io_errhandler(int level, char *msg);
static void main_ws_errhandler(int level, char *msg); static void main_ws_errhandler(int level, char *msg);
/**
* build a dns text string
*
* @param txtrecord buffer to append text record string to
* @param fmt sprintf-style format
*/
void txt_add(char *txtrecord, char *fmt, ...) {
va_list ap;
char buff[256];
int len;
char *end;
va_start(ap, fmt);
vsnprintf(buff, sizeof(buff), fmt, ap);
va_end(ap);
len = (int)strlen(buff);
if(len + strlen(txtrecord) > 255) {
DPRINTF(E_FATAL,L_MAIN,"dns-sd text string too long. Try a shorter "
"share name.\n");
}
end = txtrecord + strlen(txtrecord);
*end = len;
strcpy(end+1,buff);
}
void main_handler(WS_CONNINFO *pwsc) { void main_handler(WS_CONNINFO *pwsc) {
DPRINTF(E_DBG,L_MAIN,"in main_handler\n"); DPRINTF(E_DBG,L_MAIN,"in main_handler\n");
if(plugin_url_candispatch(pwsc)) { if(plugin_url_candispatch(pwsc)) {
@ -411,7 +383,7 @@ int main(int argc, char *argv[]) {
char *ffid = NULL; char *ffid = NULL;
int appdir = 0; int appdir = 0;
char *perr=NULL; char *perr=NULL;
char txtrecord[255]; char *txtrecord[10];
void *phandle; void *phandle;
char *plugindir; char *plugindir;
struct event *main_timer; struct event *main_timer;
@ -420,6 +392,7 @@ int main(int argc, char *argv[]) {
int sigfd; int sigfd;
struct event sig_event; struct event sig_event;
int ret; int ret;
int i;
int err; int err;
char *apppath; char *apppath;
@ -702,27 +675,41 @@ int main(int argc, char *argv[]) {
/* Register mDNS services */ /* Register mDNS services */
servername = conf_get_servername(); servername = conf_get_servername();
memset(txtrecord,0,sizeof(txtrecord)); for (i = 0; i < (sizeof(txtrecord) / sizeof(*txtrecord) - 1); i++)
txt_add(txtrecord,"txtvers=1"); {
txt_add(txtrecord,"Database ID=%0X",util_djb_hash_str(servername)); txtrecord[i] = (char *)malloc(128);
txt_add(txtrecord,"Machine ID=%0X",util_djb_hash_str(servername)); if (!txtrecord[i])
txt_add(txtrecord,"Machine Name=%s",servername); {
txt_add(txtrecord,"mtd-version=" VERSION); DPRINTF(E_FATAL, L_MAIN, "Out of memory for TXT record\n");
txt_add(txtrecord,"iTSh Version=131073"); /* iTunes 6.0.4 */
txt_add(txtrecord,"Version=196610"); /* iTunes 6.0.4 */ mdns_deinit();
exit(EXIT_FAILURE);
}
memset(txtrecord[i], 0, 128);
}
snprintf(txtrecord[0], 128, "txtvers=1");
snprintf(txtrecord[1], 128, "Database ID=%0X", util_djb_hash_str(servername));
snprintf(txtrecord[2], 128, "Machine ID=%0X", util_djb_hash_str(servername));
snprintf(txtrecord[3], 128, "Machine Name=%s", servername);
snprintf(txtrecord[4], 128, "mtd-version=%s", VERSION);
snprintf(txtrecord[5], 128, "iTSh Version=131073"); /* iTunes 6.0.4 */
snprintf(txtrecord[6], 128, "Version=196610"); /* iTunes 6.0.4 */
tmp = conf_alloc_string("general", "password", NULL); tmp = conf_alloc_string("general", "password", NULL);
txt_add(txtrecord, "Password=%s", (tmp && (strlen(tmp) > 0)) ? "true" : "false"); snprintf(txtrecord[7], 128, "Password=%s", (tmp && (strlen(tmp) > 0)) ? "true" : "false");
if (tmp) if (tmp)
free(tmp); free(tmp);
srand((unsigned int)time(NULL)); srand((unsigned int)time(NULL));
if(ffid) { if (ffid)
txt_add(txtrecord,"ffid=%s",ffid); snprintf(txtrecord[8], 128, "ffid=%s", ffid);
} else { else
txt_add(txtrecord,"ffid=%08x",rand()); snprintf(txtrecord[8], 128, "ffid=%08x", rand());
}
txtrecord[9] = NULL;
DPRINTF(E_LOG,L_MAIN|L_REND,"Registering rendezvous names\n"); DPRINTF(E_LOG,L_MAIN|L_REND,"Registering rendezvous names\n");
/* Register main service */ /* Register main service */
@ -730,6 +717,9 @@ int main(int argc, char *argv[]) {
/* Register plugin services */ /* Register plugin services */
plugin_rend_register(servername, ws_config.port, txtrecord); plugin_rend_register(servername, ws_config.port, txtrecord);
for (i = 0; i < (sizeof(txtrecord) / sizeof(*txtrecord) - 1); i++)
free(txtrecord[i]);
free(servername); free(servername);
end_time=(int) time(NULL); end_time=(int) time(NULL);

View File

@ -505,14 +505,11 @@ mdns_deinit(void)
} }
int int
mdns_register(char *name, char *type, int port, char *txt) mdns_register(char *name, char *type, int port, char **txt)
{ {
struct mdns_group_entry *ge; struct mdns_group_entry *ge;
AvahiStringList *txt_sl; AvahiStringList *txt_sl;
unsigned char count; int i;
unsigned char *key;
unsigned char *nextkey;
unsigned char *newtxt;
DPRINTF(E_DBG, L_REND, "Adding mDNS service %s/%s\n", name, type); DPRINTF(E_DBG, L_REND, "Adding mDNS service %s/%s\n", name, type);
@ -524,41 +521,14 @@ mdns_register(char *name, char *type, int port, char *txt)
ge->type = strdup(type); ge->type = strdup(type);
ge->port = port; ge->port = port;
/* Build a string list from the "encoded" txt record
* "<len1><record1><len2><record2>...<recordN>\0"
* Length is 1 byte
*/
count = 0;
txt_sl = NULL; txt_sl = NULL;
newtxt = (unsigned char *)strdup(txt); for (i = 0; txt[i]; i++)
if (!newtxt)
{ {
DPRINTF(E_FATAL, L_REND, "Out of memory\n"); txt_sl = avahi_string_list_add(txt_sl, txt[i]);
return -1; DPRINTF(E_DBG, L_REND, "Added key %s\n", txt[i]);
} }
key = nextkey = newtxt;
if (*nextkey)
count = *nextkey;
DPRINTF(E_DBG, L_REND, "Found key of size %d\n", count);
while ((*nextkey) && (nextkey < (newtxt + strlen(txt))))
{
key = nextkey + 1;
nextkey += (count + 1);
count = *nextkey;
*nextkey = '\0';
txt_sl = avahi_string_list_add(txt_sl, (char *)key);
DPRINTF(E_DBG, L_REND, "Added key %s\n", key);
*nextkey = count;
}
free(newtxt);
ge->txt = txt_sl; ge->txt = txt_sl;
ge->next = group_entries; ge->next = group_entries;

View File

@ -11,6 +11,6 @@ void
mdns_deinit(void); mdns_deinit(void);
int int
mdns_register(char *name, char *type, int port, char *txt); mdns_register(char *name, char *type, int port, char **txt);
#endif /* !__MDNS_AVAHI_H__ */ #endif /* !__MDNS_AVAHI_H__ */

View File

@ -339,10 +339,10 @@ void plugin_url_handle(WS_CONNINFO *pwsc) {
* walk through the plugins and register whatever rendezvous * walk through the plugins and register whatever rendezvous
* names the clients want * names the clients want
*/ */
int plugin_rend_register(char *name, int port, char *txt) { int plugin_rend_register(char *name, int port, char **txt) {
PLUGIN_ENTRY *ppi; PLUGIN_ENTRY *ppi;
PLUGIN_REND_INFO *pri; PLUGIN_REND_INFO *pri;
char *supplied_txt; char **supplied_txt;
ppi = _plugin_list.next; ppi = _plugin_list.next;

View File

@ -34,7 +34,7 @@ extern int plugin_deinit(void);
extern int plugin_url_candispatch(WS_CONNINFO *pwsc); extern int plugin_url_candispatch(WS_CONNINFO *pwsc);
extern void plugin_url_handle(WS_CONNINFO *pwsc); extern void plugin_url_handle(WS_CONNINFO *pwsc);
extern int plugin_auth_handle(WS_CONNINFO *pwsc, char *username, char *pw); extern int plugin_auth_handle(WS_CONNINFO *pwsc, char *username, char *pw);
extern int plugin_rend_register(char *name, int port, char *txt); extern int plugin_rend_register(char *name, int port, char **txt);
extern void plugin_event_dispatch(int event_id, int intval, void *vp, int len); extern void plugin_event_dispatch(int event_id, int intval, void *vp, int len);
extern void *plugin_enum(void *); extern void *plugin_enum(void *);