static gboolean create_service (struct DMAPMdnsPublisherService *service, DMAPMdnsPublisher * publisher, GError ** error) { int ret; const char *password_record; AvahiStringList *txt_records; if (service->password_required) { password_record = "Password=true"; } else { password_record = "Password=false"; } txt_records = avahi_string_list_new (password_record, NULL); if (service->txt_records) { //g_debug("Number of txt records: %d", service->txt_records); gchar **txt_record = service->txt_records; while (*txt_record) { txt_records = avahi_string_list_add (txt_records, *txt_record); txt_record++; } } ret = avahi_entry_group_add_service_strlst (publisher-> priv->entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, service->name, service->type_of_service, NULL, NULL, service->port, txt_records); avahi_string_list_free (txt_records); if (ret < 0) { g_set_error (error, DMAP_MDNS_PUBLISHER_ERROR, DMAP_MDNS_PUBLISHER_ERROR_FAILED, "%s: %s", _("Could not add service"), avahi_strerror (ret)); return FALSE; } return TRUE; }
static void create_services(AvahiClient * c, Game * game) { gchar *hostname; gchar *servicename; AvahiStringList *sl; int ret; g_assert(c != NULL); /* If this is the first time we're called, let's create a new entry group */ if (!group) { if (! (group = avahi_entry_group_new(c, entry_group_callback, NULL))) { log_message(MSG_ERROR, _("Avahi error: %s, %s\n"), "avahi_entry_group_new() failed", avahi_strerror(avahi_client_errno(c))); avahi_glib_poll_free(glib_poll); return; } } sl = avahi_string_list_new(NULL, NULL); sl = avahi_string_list_add_printf(sl, "version=%s", PROTOCOL_VERSION); sl = avahi_string_list_add_printf(sl, "title=%s", game->params->title); /* Add the service for IPP */ hostname = game->hostname ? g_strdup(game->hostname) : get_my_hostname(); servicename = g_strdup_printf("%s [%s]", hostname, game->server_port); g_free(hostname); ret = avahi_entry_group_add_service_strlst(group, AVAHI_IF_UNSPEC, AVAHI_NETWORK_PROTOCOL, 0, servicename, AVAHI_ANNOUNCE_NAME, NULL, NULL, atoi(game->server_port), sl); g_free(servicename); if (ret < 0) { gchar *msg = g_strdup_printf("Failed to add '%s' service", AVAHI_ANNOUNCE_NAME); log_message(MSG_ERROR, _("Avahi error: %s, %s\n"), msg, avahi_strerror(ret)); g_free(msg); avahi_string_list_free(sl); avahi_glib_poll_free(glib_poll); return; } /* Tell the server to register the service */ if ((ret = avahi_entry_group_commit(group)) < 0) { log_message(MSG_ERROR, _("Avahi error: %s, %s\n"), "Failed to commit entry_group", avahi_strerror(ret)); avahi_string_list_free(sl); avahi_glib_poll_free(glib_poll); return; } avahi_string_list_free(sl); return; }
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) { char *t, *v; uint8_t data[1024]; AvahiStringList *a = NULL, *b, *p; size_t size, n; int r; a = avahi_string_list_new("prefix", "a", "b", NULL); a = avahi_string_list_add(a, "start"); a = avahi_string_list_add(a, "foo=99"); a = avahi_string_list_add(a, "bar"); a = avahi_string_list_add(a, ""); a = avahi_string_list_add(a, ""); a = avahi_string_list_add(a, "quux"); a = avahi_string_list_add(a, ""); a = avahi_string_list_add_arbitrary(a, (const uint8_t*) "null\0null", 9); a = avahi_string_list_add_printf(a, "seven=%i %c", 7, 'x'); a = avahi_string_list_add_pair(a, "blubb", "blaa"); a = avahi_string_list_add_pair(a, "uxknurz", NULL); a = avahi_string_list_add_pair_arbitrary(a, "uxknurz2", (const uint8_t*) "blafasel\0oerks", 14); a = avahi_string_list_add(a, "end"); t = avahi_string_list_to_string(a); printf("--%s--\n", t); avahi_free(t); n = avahi_string_list_serialize(a, NULL, 0); size = avahi_string_list_serialize(a, data, sizeof(data)); assert(size == n); printf("%u\n", size); for (t = (char*) data, n = 0; n < size; n++, t++) { if (*t <= 32) printf("(%u)", *t); else printf("%c", *t); } printf("\n"); assert(avahi_string_list_parse(data, size, &b) == 0); printf("equal: %i\n", avahi_string_list_equal(a, b)); t = avahi_string_list_to_string(b); printf("--%s--\n", t); avahi_free(t); avahi_string_list_free(b); b = avahi_string_list_copy(a); assert(avahi_string_list_equal(a, b)); t = avahi_string_list_to_string(b); printf("--%s--\n", t); avahi_free(t); p = avahi_string_list_find(a, "seven"); assert(p); r = avahi_string_list_get_pair(p, &t, &v, NULL); assert(r >= 0); assert(t); assert(v); printf("<%s>=<%s>\n", t, v); avahi_free(t); avahi_free(v); p = avahi_string_list_find(a, "quux"); assert(p); r = avahi_string_list_get_pair(p, &t, &v, NULL); assert(r >= 0); assert(t); assert(!v); printf("<%s>=<%s>\n", t, v); avahi_free(t); avahi_free(v); avahi_string_list_free(a); avahi_string_list_free(b); n = avahi_string_list_serialize(NULL, NULL, 0); size = avahi_string_list_serialize(NULL, data, sizeof(data)); assert(size == 1); assert(size == n); assert(avahi_string_list_parse(data, size, &a) == 0); assert(!a); return 0; }