X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=avahi-daemon%2Fstatic-services.c;h=09777cca847224808f06e21f021a7a8f6161a5d0;hb=6b391bb81f0dce0193a722254016b26c12a17643;hp=0ac63263e8610ec1d0eca21c7532d2265f712cd3;hpb=66142b071a2497d7e3cf58d7bf5159bb1c970d84;p=catta diff --git a/avahi-daemon/static-services.c b/avahi-daemon/static-services.c index 0ac6326..09777cc 100644 --- a/avahi-daemon/static-services.c +++ b/avahi-daemon/static-services.c @@ -31,11 +31,14 @@ #include #include -#include #include -#include +#include +#include +#include +#include #include +#include #include "main.h" #include "static-services.h" @@ -46,10 +49,11 @@ typedef struct StaticServiceGroup StaticServiceGroup; struct StaticService { StaticServiceGroup *group; - gchar *type; - gchar *domain_name; - gchar *host_name; - guint16 port; + char *type; + char *domain_name; + char *host_name; + uint16_t port; + int protocol; AvahiStringList *txt_records; @@ -57,43 +61,43 @@ struct StaticService { }; struct StaticServiceGroup { - gchar *filename; + char *filename; time_t mtime; - gchar *name, *chosen_name; - gboolean replace_wildcards; + char *name, *chosen_name; + int replace_wildcards; - AvahiEntryGroup *entry_group; + AvahiSEntryGroup *entry_group; AVAHI_LLIST_HEAD(StaticService, services); AVAHI_LLIST_FIELDS(StaticServiceGroup, groups); }; static AVAHI_LLIST_HEAD(StaticServiceGroup, groups) = NULL; -static gchar *replacestr(const gchar *pattern, const gchar *a, const gchar *b) { - gchar *r = NULL, *e, *n; +static char *replacestr(const char *pattern, const char *a, const char *b) { + char *r = NULL, *e, *n; while ((e = strstr(pattern, a))) { - gchar *k; + char *k; - k = g_strndup(pattern, e - pattern); + k = avahi_strndup(pattern, e - pattern); if (r) - n = g_strconcat(r, k, b, NULL); + n = avahi_strdup_printf("%s%s%s", r, k, b); else - n = g_strconcat(k, b, NULL); + n = avahi_strdup_printf("%s%s", k, b); - g_free(k); - g_free(r); + avahi_free(k); + avahi_free(r); r = n; pattern = e + strlen(a); } if (!r) - return g_strdup(pattern); + return avahi_strdup(pattern); - n = g_strconcat(r, pattern, NULL); - g_free(r); + n = avahi_strdup_printf("%s%s", r, pattern); + avahi_free(r); return n; } @@ -104,12 +108,13 @@ static void remove_static_service_group_from_server(StaticServiceGroup *g); static StaticService *static_service_new(StaticServiceGroup *group) { StaticService *s; - g_assert(group); - s = g_new(StaticService, 1); + assert(group); + s = avahi_new(StaticService, 1); s->group = group; s->type = s->host_name = s->domain_name = NULL; s->port = 0; + s->protocol = AVAHI_PROTO_UNSPEC; s->txt_records = NULL; @@ -118,15 +123,15 @@ static StaticService *static_service_new(StaticServiceGroup *group) { return s; } -static StaticServiceGroup *static_service_group_new(gchar *filename) { +static StaticServiceGroup *static_service_group_new(char *filename) { StaticServiceGroup *g; - g_assert(filename); + assert(filename); - g = g_new(StaticServiceGroup, 1); - g->filename = g_strdup(filename); + g = avahi_new(StaticServiceGroup, 1); + g->filename = avahi_strdup(filename); g->mtime = 0; g->name = g->chosen_name = NULL; - g->replace_wildcards = FALSE; + g->replace_wildcards = 0; g->entry_group = NULL; AVAHI_LLIST_HEAD_INIT(StaticService, g->services); @@ -136,48 +141,49 @@ static StaticServiceGroup *static_service_group_new(gchar *filename) { } static void static_service_free(StaticService *s) { - g_assert(s); + assert(s); AVAHI_LLIST_REMOVE(StaticService, services, s->group->services, s); - g_free(s->type); - g_free(s->host_name); - g_free(s->domain_name); + avahi_free(s->type); + avahi_free(s->host_name); + avahi_free(s->domain_name); avahi_string_list_free(s->txt_records); - g_free(s); + avahi_free(s); } static void static_service_group_free(StaticServiceGroup *g) { - g_assert(g); + assert(g); - remove_static_service_group_from_server(g); + if (g->entry_group) + avahi_s_entry_group_free(g->entry_group); while (g->services) static_service_free(g->services); AVAHI_LLIST_REMOVE(StaticServiceGroup, groups, groups, g); - g_free(g->filename); - g_free(g->name); - g_free(g->chosen_name); - g_free(g); + avahi_free(g->filename); + avahi_free(g->name); + avahi_free(g->chosen_name); + avahi_free(g); } -static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *eg, AvahiEntryGroupState state, gpointer userdata) { +static void entry_group_callback(AvahiServer *s, AvahiSEntryGroup *eg, AvahiEntryGroupState state, void* userdata) { StaticServiceGroup *g = userdata; - g_assert(s); - g_assert(g); + assert(s); + assert(g); if (state == AVAHI_ENTRY_GROUP_COLLISION) { - gchar *n; + char *n; remove_static_service_group_from_server(g); n = avahi_alternative_service_name(g->chosen_name); - g_free(g->chosen_name); + avahi_free(g->chosen_name); g->chosen_name = n; avahi_log_notice("Service name conflict for \"%s\" (%s), retrying with \"%s\".", g->name, g->filename, g->chosen_name); @@ -190,46 +196,52 @@ static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *eg, AvahiEntry static void add_static_service_group_to_server(StaticServiceGroup *g) { StaticService *s; - g_assert(g); - - if (g->entry_group) + assert(g); + + if (g->entry_group && !avahi_s_entry_group_is_empty(g->entry_group)) + /* This service group is already registered in the server */ return; - if (g->chosen_name) - g_free(g->chosen_name); - - if (g->replace_wildcards) - g->chosen_name = replacestr(g->name, "%h", avahi_server_get_host_name(avahi_server)); - else - g->chosen_name = g_strdup(g->name); - - g->entry_group = avahi_entry_group_new(avahi_server, entry_group_callback, g); + if (!g->chosen_name) { + + if (g->replace_wildcards) + g->chosen_name = replacestr(g->name, "%h", avahi_server_get_host_name(avahi_server)); + else + g->chosen_name = avahi_strdup(g->name); + + } + + if (!g->entry_group) + g->entry_group = avahi_s_entry_group_new(avahi_server, entry_group_callback, g); + + assert(avahi_s_entry_group_is_empty(g->entry_group)); for (s = g->services; s; s = s->services_next) { if (avahi_server_add_service_strlst( avahi_server, g->entry_group, - -1, AF_UNSPEC, + AVAHI_IF_UNSPEC, s->protocol, + 0, g->chosen_name, s->type, s->domain_name, s->host_name, s->port, - avahi_string_list_copy(s->txt_records)) < 0) { - avahi_log_error("Failed to add service '%s' of type '%s', ignoring service group (%s)", g->chosen_name, s->type, g->filename); + s->txt_records) < 0) { + avahi_log_error("Failed to add service '%s' of type '%s', ignoring service group (%s): %s", + g->chosen_name, s->type, g->filename, + avahi_strerror(avahi_server_errno(avahi_server))); remove_static_service_group_from_server(g); return; } } - avahi_entry_group_commit(g->entry_group); + avahi_s_entry_group_commit(g->entry_group); } static void remove_static_service_group_from_server(StaticServiceGroup *g) { - g_assert(g); + assert(g); - if (g->entry_group) { - avahi_entry_group_free(g->entry_group); - g->entry_group = NULL; - } + if (g->entry_group) + avahi_s_entry_group_reset(g->entry_group); } typedef enum { @@ -248,14 +260,14 @@ struct xml_userdata { StaticServiceGroup *group; StaticService *service; xml_tag_name current_tag; - gboolean failed; - gchar *buf; + int failed; + char *buf; }; static void XMLCALL xml_start(void *data, const char *el, const char *attr[]) { struct xml_userdata *u = data; - g_assert(u); + assert(u); if (u->failed) return; @@ -274,21 +286,41 @@ static void XMLCALL xml_start(void *data, const char *el, const char *attr[]) { u->group->replace_wildcards = strcmp(attr[1], "yes") == 0; else goto invalid_attr; - } - if (attr[2]) - goto invalid_attr; - - + if (attr[2]) + goto invalid_attr; + } } else if (u->current_tag == XML_TAG_SERVICE_GROUP && strcmp(el, "service") == 0) { - if (attr[0]) - goto invalid_attr; + u->current_tag = XML_TAG_SERVICE; - g_assert(!u->service); + assert(!u->service); u->service = static_service_new(u->group); - u->current_tag = XML_TAG_SERVICE; + if (attr[0]) { + if (strcmp(attr[0], "protocol") == 0) { + AvahiProtocol protocol; + + if (strcmp(attr[1], "ipv4") == 0) { + protocol = AVAHI_PROTO_INET; + } else if (strcmp(attr[1], "ipv6") == 0) { + protocol = AVAHI_PROTO_INET6; + } else if (strcmp(attr[1], "any") == 0) { + protocol = AVAHI_PROTO_UNSPEC; + } else { + avahi_log_error("%s: parse failure: invalid protocol specification \"%s\".", u->group->filename, attr[1]); + u->failed = 1; + return; + } + + u->service->protocol = protocol; + } else + goto invalid_attr; + + if (attr[2]) + goto invalid_attr; + } + } else if (u->current_tag == XML_TAG_SERVICE && strcmp(el, "type") == 0) { if (attr[0]) goto invalid_attr; @@ -316,20 +348,20 @@ static void XMLCALL xml_start(void *data, const char *el, const char *attr[]) { u->current_tag = XML_TAG_TXT_RECORD; } else { avahi_log_error("%s: parse failure: didn't expect element <%s>.", u->group->filename, el); - u->failed = TRUE; + u->failed = 1; } return; invalid_attr: avahi_log_error("%s: parse failure: invalid attribute for element <%s>.", u->group->filename, el); - u->failed = TRUE; + u->failed = 1; return; } static void XMLCALL xml_end(void *data, const char *el) { struct xml_userdata *u = data; - g_assert(u); + assert(u); if (u->failed) return; @@ -339,7 +371,7 @@ static void XMLCALL xml_end(void *data, const char *el) { if (!u->group->name || !u->group->services) { avahi_log_error("%s: parse failure: service group incomplete.", u->group->filename); - u->failed = TRUE; + u->failed = 1; return; } @@ -348,9 +380,9 @@ static void XMLCALL xml_end(void *data, const char *el) { case XML_TAG_SERVICE: - if (u->service->port == 0 || !u->service->type) { + if (!u->service->type) { avahi_log_error("%s: parse failure: service incomplete.", u->group->filename); - u->failed = TRUE; + u->failed = 1; return; } @@ -364,24 +396,24 @@ static void XMLCALL xml_end(void *data, const char *el) { case XML_TAG_PORT: { int p; - g_assert(u->service); + assert(u->service); p = u->buf ? atoi(u->buf) : 0; - if (p <= 0 || p > 0xFFFF) { + if (p < 0 || p > 0xFFFF) { avahi_log_error("%s: parse failure: invalid port specification \"%s\".", u->group->filename, u->buf); - u->failed = TRUE; + u->failed = 1; return; } - u->service->port = (guint16) p; + u->service->port = (uint16_t) p; u->current_tag = XML_TAG_SERVICE; break; } case XML_TAG_TXT_RECORD: { - g_assert(u->service); + assert(u->service); u->service->txt_records = avahi_string_list_add(u->service->txt_records, u->buf ? u->buf : ""); u->current_tag = XML_TAG_SERVICE; @@ -398,22 +430,23 @@ static void XMLCALL xml_end(void *data, const char *el) { ; } - g_free(u->buf); + avahi_free(u->buf); u->buf = NULL; } -static gchar *append_cdata(gchar *t, const gchar *n, int length) { - gchar *r, *k; +static char *append_cdata(char *t, const char *n, int length) { + char *r, *k; if (!length) return t; - k = g_strndup(n, length); + + k = avahi_strndup(n, length); if (t) { - r = g_strconcat(t, k, NULL); - g_free(k); - g_free(t); + r = avahi_strdup_printf("%s%s", t, k); + avahi_free(k); + avahi_free(t); } else r = k; @@ -422,7 +455,7 @@ static gchar *append_cdata(gchar *t, const gchar *n, int length) { static void XMLCALL xml_cdata(void *data, const XML_Char *s, int len) { struct xml_userdata *u = data; - g_assert(u); + assert(u); if (u->failed) return; @@ -433,22 +466,23 @@ static void XMLCALL xml_cdata(void *data, const XML_Char *s, int len) { break; case XML_TAG_TYPE: - g_assert(u->service); + assert(u->service); u->service->type = append_cdata(u->service->type, s, len); break; case XML_TAG_DOMAIN_NAME: - g_assert(u->service); + assert(u->service); u->service->domain_name = append_cdata(u->service->domain_name, s, len); break; case XML_TAG_HOST_NAME: - g_assert(u->service); + assert(u->service); u->service->host_name = append_cdata(u->service->host_name, s, len); break; case XML_TAG_PORT: case XML_TAG_TXT_RECORD: + assert(u->service); u->buf = append_cdata(u->buf, s, len); break; @@ -459,31 +493,31 @@ static void XMLCALL xml_cdata(void *data, const XML_Char *s, int len) { } } -static gint static_service_group_load(StaticServiceGroup *g) { +static int static_service_group_load(StaticServiceGroup *g) { XML_Parser parser = NULL; - gint fd = -1; + int fd = -1; struct xml_userdata u; - gint r = -1; + int r = -1; struct stat st; ssize_t n; - g_assert(g); + assert(g); u.buf = NULL; u.group = g; u.service = NULL; u.current_tag = XML_TAG_INVALID; - u.failed = FALSE; + u.failed = 0; /* Cleanup old data in this service group, if available */ remove_static_service_group_from_server(g); while (g->services) static_service_free(g->services); - g_free(g->name); - g_free(g->chosen_name); + avahi_free(g->name); + avahi_free(g->chosen_name); g->name = g->chosen_name = NULL; - g->replace_wildcards = FALSE; + g->replace_wildcards = 0; if (!(parser = XML_ParserCreate(NULL))) { avahi_log_error("XML_ParserCreate() failed."); @@ -540,20 +574,20 @@ finish: if (parser) XML_ParserFree(parser); - g_free(u.buf); + avahi_free(u.buf); return r; } -static void load_file(gchar *n) { +static void load_file(char *n) { StaticServiceGroup *g; - g_assert(n); + assert(n); for (g = groups; g; g = g->groups_next) if (strcmp(g->filename, n) == 0) return; - avahi_log_info("Loading service file %s", n); + avahi_log_info("Loading service file %s.", n); g = static_service_group_new(n); if (static_service_group_load(g) < 0) { @@ -565,7 +599,7 @@ static void load_file(gchar *n) { void static_service_load(void) { StaticServiceGroup *g, *n; glob_t globbuf; - gchar **p; + char **p; for (g = groups; g; g = n) { struct stat st;