static AvahiSEntryGroup* add_dns_servers(AvahiServer *s, AvahiSEntryGroup* g, char **l) { char **p; assert(s); assert(l); if (!g) g = avahi_s_entry_group_new(s, NULL, NULL); assert(avahi_s_entry_group_is_empty(g)); for (p = l; *p; p++) { AvahiAddress a; if (!avahi_address_parse(*p, AVAHI_PROTO_UNSPEC, &a)) avahi_log_warn("Failed to parse address '%s', ignoring.", *p); else if (avahi_server_add_dns_server_address(s, g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, NULL, AVAHI_DNS_SERVER_RESOLVE, &a, 53) < 0) { avahi_s_entry_group_free(g); avahi_log_error("Failed to add DNS server address: %s", avahi_strerror(avahi_server_errno(s))); return NULL; } } avahi_s_entry_group_commit(g); return g; }
static void add_static_host_to_server(StaticHost *h) { if (!h->group) if (!(h->group = avahi_s_entry_group_new (avahi_server, entry_group_callback, h))) { avahi_log_error("avahi_s_entry_group_new() failed: %s", avahi_strerror(avahi_server_errno(avahi_server))); return; } if (avahi_s_entry_group_is_empty(h->group)) { AvahiProtocol p; int err; const AvahiServerConfig *config; config = avahi_server_get_config(avahi_server); p = (h->address.proto == AVAHI_PROTO_INET && config->publish_a_on_ipv6) || (h->address.proto == AVAHI_PROTO_INET6 && config->publish_aaaa_on_ipv4) ? AVAHI_PROTO_UNSPEC : h->address.proto; if ((err = avahi_server_add_address(avahi_server, h->group, AVAHI_IF_UNSPEC, p, 0, h->host, &h->address)) < 0) { avahi_log_error ("Static host name %s: avahi_server_add_address failure: %s", h->host, avahi_strerror(err)); return; } avahi_s_entry_group_commit (h->group); } }
static void create_services(AvahiServer *s) { char r[128]; int ret; assert(s); /* If this is the first time we're called, let's create a new entry group */ if (!group) if (!(group = avahi_s_entry_group_new(s, entry_group_callback, NULL))) { fprintf(stderr, "avahi_entry_group_new() failed: %s\n", avahi_strerror(avahi_server_errno(s))); goto fail; } fprintf(stderr, "Adding service '%s'\n", name); /* Create some random TXT data */ snprintf(r, sizeof(r), "random=%i", rand()); /* Add the service for IPP */ if ((ret = avahi_server_add_service(s, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_ipp._tcp", NULL, NULL, 651, "test=blah", r, NULL)) < 0) { fprintf(stderr, "Failed to add _ipp._tcp service: %s\n", avahi_strerror(ret)); goto fail; } /* Add the same service for BSD LPR */ if ((ret = avahi_server_add_service(s, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_printer._tcp", NULL, NULL, 515, NULL)) < 0) { fprintf(stderr, "Failed to add _printer._tcp service: %s\n", avahi_strerror(ret)); goto fail; } /* Add an additional (hypothetic) subtype */ if ((ret = avahi_server_add_service_subtype(s, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_printer._tcp", NULL, "_magic._sub._printer._tcp") < 0)) { fprintf(stderr, "Failed to add subtype _magic._sub._printer._tcp: %s\n", avahi_strerror(ret)); goto fail; } /* Tell the server to register the service */ if ((ret = avahi_s_entry_group_commit(group)) < 0) { fprintf(stderr, "Failed to commit entry_group: %s\n", avahi_strerror(ret)); goto fail; } return; fail: avahi_simple_poll_quit(simple_poll); }
static void add_static_host_to_server(StaticHost *h) { AvahiAddress a; int err; if (!h->group) h->group = avahi_s_entry_group_new (avahi_server, entry_group_callback, h); if (!avahi_address_parse (h->ip, AVAHI_PROTO_UNSPEC, &a)) { avahi_log_error("Static host name %s: avahi_address_parse failed", h->host); return; } if ((err = avahi_server_add_address(avahi_server, h->group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, h->host, &a))) { avahi_log_error ("Static host name %s: avahi_server_add_address failure: %s", h->host, avahi_strerror(err)); return; } avahi_s_entry_group_commit (h->group); }
void ControllerDiscoveryMDNS::create_service( AvahiServer * server ) { if ( !group ) { group = avahi_s_entry_group_new( server, avahi_entry_group_callback, this ); if ( !group ) { g_warning( "FAILED TO CREATE AVAHI ENTRY GROUP : %s", avahi_strerror( avahi_server_errno( server ) ) ); } } if ( group ) { if ( avahi_s_entry_group_is_empty( group ) ) { int ret = AVAHI_OK; while ( true ) { // TODO: this could loop forever...maybe we should bail at some stage ret = avahi_server_add_service( server, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AvahiPublishFlags( 0 ), name.c_str(), TP_REMOTE_MDNS_SERVICE, NULL, NULL, port, NULL ); if ( ret == AVAHI_ERR_COLLISION ) { rename(); } else { break; } } if ( ret != AVAHI_OK ) { g_warning( "FAILED TO ADD AVAHI SERVICE : %s", avahi_strerror( ret ) ); } else { while ( true ) { // TODO: this could loop forever...maybe we should bail at some stage ret = avahi_server_add_service( server, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AvahiPublishFlags( 0 ), name.c_str(), TP_HTTP_MDNS_SERVICE, NULL, NULL, http_port, NULL ); if ( ret == AVAHI_ERR_COLLISION ) { rename(); } else { break; } } if ( ret != AVAHI_OK ) { g_warning( "FAILED TO ADD AVAHI SERVICE : %s", avahi_strerror( ret ) ); } else { ret = avahi_s_entry_group_commit( group ); if ( ret != AVAHI_OK ) { g_warning( "FAILED TO COMMIT AVAHI SERVICE : %s", avahi_strerror( ret ) ); } } } } } }
static void add_static_service_group_to_server(StaticServiceGroup *g) { StaticService *s; 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->replace_wildcards && strstr(g->name, "%h"))) { avahi_free(g->chosen_name); if (g->replace_wildcards) { char label[AVAHI_LABEL_MAX]; const char *p; p = avahi_server_get_host_name(avahi_server); avahi_unescape_label(&p, label, sizeof(label)); g->chosen_name = replacestr(g->name, "%h", label); } 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) { AvahiStringList *i; if (avahi_server_add_service_strlst( avahi_server, g->entry_group, s->interface, s->protocol, 0, g->chosen_name, s->type, s->domain_name, s->host_name, s->port, 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; } for (i = s->subtypes; i; i = i->next) { if (avahi_server_add_service_subtype( avahi_server, g->entry_group, AVAHI_IF_UNSPEC, s->protocol, 0, g->chosen_name, s->type, s->domain_name, (char*) i->text) < 0) { avahi_log_error("Failed to add subtype '%s' for service '%s' of type '%s', ignoring subtype (%s): %s", i->text, g->chosen_name, s->type, g->filename, avahi_strerror(avahi_server_errno(avahi_server))); } } } avahi_s_entry_group_commit(g->entry_group); }