/* Try to register the TiVo DNS SRV service type. */ static void register_stuff(void) { assert(ctx.client); if (!ctx.group) { ctx.group = avahi_entry_group_new(ctx.client, publish_reply, NULL); if (!ctx.group) { DPRINTF(E_ERROR, L_SSDP, "Failed to create entry group: %s\n", avahi_strerror(avahi_client_errno(ctx.client))); return; } } if (avahi_entry_group_is_empty(ctx.group)) { if (_add_svc("Music", "_tivo-music._tcp", "1", NULL) < 0) return; if (_add_svc("Photos", "_tivo-photos._tcp", "3", NULL) < 0) return; if (_add_svc("Videos", "_tivo-videostream._tcp", "2", "platform=pc/"SERVER_NAME) < 0) return; if (avahi_entry_group_commit(ctx.group) < 0) { DPRINTF(E_ERROR, L_SSDP, "Failed to commit entry group: %s\n", avahi_strerror(avahi_client_errno(ctx.client))); return; } } }
const char *addAvahiGroup(AvahiThreadedPoll *threaded_poll, AvahiClient *client, AvahiEntryGroup **group, const char *service_name, unsigned short port, AvahiStringList *txt) { *group = avahi_entry_group_new(client, handleGroupStateChange, (void *)service_name); if (!*group) { return avahi_strerror(avahi_client_errno(client)); } int error = avahi_entry_group_add_service_strlst( *group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, service_name, SERVICE_TYPE, NULL, NULL, port, txt); if (AVAHI_OK != error) { avahi_entry_group_free(*group); return avahi_strerror(error); } error = avahi_entry_group_add_service_subtype(*group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, service_name, SERVICE_TYPE, NULL, SERVICE_SUBTYPE); if (AVAHI_OK != error) { avahi_entry_group_free(*group); return avahi_strerror(error); } error = avahi_entry_group_commit(*group); if (AVAHI_OK != error) { avahi_entry_group_free(*group); return avahi_strerror(error); } return NULL; }
static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) { switch (state) { case AVAHI_CLIENT_S_RUNNING: { static AvahiEntryGroup *group = NULL; if (!group) { avahi_entry_group_new(c, entry_group_callback, &group); } if (avahi_entry_group_is_empty(group)) { printf("Adding service '%s'\n", name); avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_http._tcp", NULL, NULL, 8080, NULL); avahi_entry_group_commit(group); } break; } case AVAHI_CLIENT_FAILURE: { break; } case AVAHI_CLIENT_S_COLLISION: { break; } case AVAHI_CLIENT_S_REGISTERING: { break; } case AVAHI_CLIENT_CONNECTING: { break; } } }
void Avahi::PresencePublisher::register_services () { remove_services (); avahi_entry_group_new (client, (AvahiEntryGroupCallback)entry_group_cb, this); }
static void create_cnames(AvahiClient *c) { struct runtime_data r; /* If this is the first time we're called, let's create a new entry group if necessary */ if ((!_group)&&(!(_group = avahi_entry_group_new(c, entry_group_callback, NULL)))) { ap_log_error(APLOG_MARK, APLOG_ERR, 0, r.main_server, "avahi_entry_group_new() failed: %s", avahi_strerror(avahi_client_errno(c))); goto fail; } /* If the group is empty (either because it was just created, or because it was reset previously, add our entries. */ if (avahi_entry_group_is_empty(_group)) { char hostname[HOST_NAME_MAX+64] = "."; /* this dot will be overwritten with a count-byte, below */ if (gethostname(&hostname[1], sizeof(hostname)-1) < 0) perror("gethostname"); strncat(hostname, ".local", sizeof(hostname)); hostname[sizeof(hostname)-1] = '\0'; /* paranoia? */ /* Convert the hostname string into DNS's labelled-strings format */ int hostnameLen = strlen(hostname); char count = 0; int i; for (i=hostnameLen-1; i>=0; i--) { if (hostname[i] == '.') { hostname[i] = count; count = 0; } else count++; } for (i=0; (_cnames[i] != NULL); i++) { int ret = avahi_entry_group_add_record(_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, (AvahiPublishFlags)(AVAHI_PUBLISH_USE_MULTICAST|AVAHI_PUBLISH_ALLOW_MULTIPLE), _cnames[i], AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_CNAME, AVAHI_DEFAULT_TTL, hostname, hostnameLen+1); if (ret >= 0) ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r.main_server, "Published DNS-SD hostname alias [%s]", _cnames[i]); else { ap_log_error(APLOG_MARK, APLOG_ERR, 0, r.main_server, "Failed to add CNAME record [%s]: %s", _cnames[i], avahi_strerror(ret)); goto fail; } } int ret = avahi_entry_group_commit(_group); if (ret < 0) { ap_log_error(APLOG_MARK, APLOG_ERR, 0, r.main_server, "Failed to commit entry group: %s", avahi_strerror(ret)); goto fail; } } return; fail: avahi_simple_poll_quit(_poll); }
void Publisher::createServices(AvahiClient *client) { int error; const char* name = info.getServiceName().c_str(); int port = info.getPort(); if (group == NULL) { group = avahi_entry_group_new(client, &Publisher::entryGroupCallback, this); if (group == NULL) { std::cerr << "Could not create avahi group (" << avahi_strerror(avahi_client_errno(client)) << ")." << std::endl; return; } } if (avahi_entry_group_is_empty(group)) { // new group or after reset error = avahi_entry_group_add_service(group, // group AVAHI_IF_UNSPEC, // interface AVAHI_PROTO_UNSPEC, // protocol to announce service with AVAHI_PUBLISH_USE_MULTICAST, // flags name, // service name serviceType, // service type NULL, // domain NULL, // host port, // port txtDataDeviceName.c_str(), txtDataProviderClassName.c_str(), txtDataSerialNumber.c_str(), NULL); // list of txt records if (error < 0) { // error if (error == AVAHI_ERR_COLLISION) { // collision with local service name nextName(); avahi_entry_group_reset(group); // retry createServices(client); } else { std::cerr << "Could not add service '" << name << "' (" << serviceType << ") to avahi group (" << avahi_strerror(error) << ")" << std::endl; return; } } // start registering the service error = avahi_entry_group_commit(group); if (error < 0) { std::cerr << "Could not commit avahi group " << "'" << name << "'" << " (" << avahi_strerror(error) << ")" << std::endl; } } }
announcer::announcer(const aware::avahi::detail::client& client) : ptr(0) { ptr = avahi_entry_group_new(client, avahi_entry_group_callback, this); if (ptr == 0) throw std::bad_alloc(); assert(avahi_entry_group_get_state(ptr) == AVAHI_ENTRY_GROUP_UNCOMMITED); }
/** * epc_shell_create_avahi_entry_group: * @callback: a callback function * @user_data: data to pass to @callback * * Creates a new #AvahiEntryGroup for the library's shared #AvahiClient. * On success the newly created #AvahiEntryGroup is returned, * and %NULL on failure. * * Returns: The newly created #AvahiEntryGroup, or %NULL on error. */ AvahiEntryGroup* epc_shell_create_avahi_entry_group (AvahiEntryGroupCallback callback, gpointer user_data) { AvahiClient *client = epc_shell_get_avahi_client (NULL); AvahiEntryGroup *group = NULL; if (NULL != client) group = avahi_entry_group_new (client, callback, user_data); return group; }
static void _create_services(void) { struct mdns_group_entry *pentry; int ret; DPRINTF(E_DBG, L_MDNS, "Creating service group\n"); if (!group_entries) { DPRINTF(E_DBG, L_MDNS, "No entries yet... skipping service create\n"); return; } if (mdns_group == NULL) { mdns_group = avahi_entry_group_new(mdns_client, entry_group_callback, NULL); if (!mdns_group) { DPRINTF(E_WARN, L_MDNS, "Could not create Avahi EntryGroup: %s\n", avahi_strerror(avahi_client_errno(mdns_client))); return; } } pentry = group_entries; while (pentry) { DPRINTF(E_DBG, L_MDNS, "Re-registering %s/%s\n", pentry->name, pentry->type); ret = avahi_entry_group_add_service_strlst(mdns_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, avahi_strdup(pentry->name), avahi_strdup(pentry->type), NULL, NULL, pentry->port, pentry->txt); if (ret < 0) { DPRINTF(E_WARN, L_MDNS, "Could not add mDNS services: %s\n", avahi_strerror(ret)); return; } pentry = pentry->next; } ret = avahi_entry_group_commit(mdns_group); if (ret < 0) { DPRINTF(E_WARN, L_MDNS, "Could not commit mDNS services: %s\n", avahi_strerror(avahi_client_errno(mdns_client))); } }
void Avahi::PresencePublisher::on_details_updated () { if (display_name != details.get_display_name ()) { display_name = details.get_display_name (); remove_services (); avahi_free (name); name = avahi_strdup (display_name.c_str ()); avahi_entry_group_new (client, (AvahiEntryGroupCallback)entry_group_cb, this); } }
void AvahiSession::CreateServices(AvahiClient* client) { int err; if (mGroup) { avahi_entry_group_reset(mGroup); } else { mGroup = avahi_entry_group_new(client, group_cb, this); if (!mGroup) { scprintf("Zeroconf: failed to create entry group: %s\n", avahi_strerror(avahi_client_errno(client))); return; } } mMutex.lock(); AvahiEntry* entry = mEntries; while (entry) { const char* type = SCRendezvousProtocolString(entry->mProto); err = avahi_entry_group_add_service( mGroup, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, (AvahiPublishFlags)0, mServiceName, type, NULL, NULL, entry->mPort, NULL); if (err == AVAHI_ERR_COLLISION) { // BUG: shouldn't this actually be triggered in the entry // group callback? RenameService(); err = avahi_entry_group_add_service( mGroup, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, (AvahiPublishFlags)0, mServiceName, type, NULL, NULL, entry->mPort, NULL); } if (err < 0) { scprintf("Zeroconf: failed to register service '%s': %s\n", mServiceName, avahi_strerror(err)); } entry = entry->mNext; } mMutex.unlock(); if (!avahi_entry_group_is_empty(mGroup)) { err = avahi_entry_group_commit(mGroup); if (err < 0) { scprintf("Zeroconf: failed to commit entry group: %s\n", avahi_strerror(err)); return; } } }
JNIEXPORT jlong JNICALL Java_avahi4j_EntryGroup_initGroup(JNIEnv *e, jobject t, jlong ptr){ dprint("[LOG] Entering %s\n", __PRETTY_FUNCTION__); struct avahi4j_client *client = (struct avahi4j_client *) (uintptr_t) ptr; struct avahi4j_entry_group *group = NULL; jclass ourClass; // create struct group entry XMALLOC(group, struct avahi4j_entry_group *, sizeof(struct avahi4j_entry_group)); if (group==NULL) { THROW_EXCEPTION(e, JNI_EXCP, "Not enough memory"); return 0; } // save JavaVM ref if ((*e)->GetJavaVM(e, &group->jvm)!=0){ XFREE(group); THROW_EXCEPTION(e, JNI_EXCP, "Cant get hold of a JavaVM pointer"); return 0; } // cache methodID to java group callback method ourClass = (*e)->GetObjectClass(e, t); group->groupCallbackDispatch = (*e)->GetMethodID(e, ourClass, "dispatchCallback", "(I)V"); if (group->groupCallbackDispatch == NULL) { XFREE(group); THROW_EXCEPTION(e, GENERIC_EXCP, "Unable to get callback dispatch method ID"); return 0; } // create global ref to our object group->groupObject = (*e)->NewGlobalRef(e, t); if (group->groupObject==NULL) { XFREE(group); THROW_EXCEPTION(e, GENERIC_EXCP, "Unable to create global ref to group object"); return 0; } // create avahi group AVAHI_LOCK(client); if (!(group->group=avahi_entry_group_new(client->client, group_callback, group))) { AVAHI_UNLOCK(client); (*e)->DeleteGlobalRef(e, group->groupObject); XFREE(group); THROW_EXCEPTION(e, JNI_EXCP, "Error creating avahi group"); return 0; } AVAHI_UNLOCK(client); return (uintptr_t) group; }
void create_services(AvahiClient *c) { char r[128]; int ret; assert(c); /* If this is the first time we're called, let's create a new * entry group if necessary */ if (!group) if (!(group = avahi_entry_group_new(c, entry_group_callback, NULL))) { std::cerr << "avahi_entry_group_new() failed: " << avahi_strerror(avahi_client_errno(c)) << std::endl; } /* If the group is empty (either because it was just created, or * because it was reset previously, add our entries. */ if (avahi_entry_group_is_empty(group)) { std::cerr << "Adding service '" << name << "'" << std::endl; /* Create some random TXT data */ snprintf(r, sizeof(r), "random=%i", rand()); /* We will now add two services and one subtype to the entry * group. The two services have the same name, but differ in * the service type (IPP vs. BSD LPR). Only services with the * same name should be put in the same entry group. */ if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, (AvahiPublishFlags)0, name.c_str(), "_heatSync._tcp", NULL, NULL, PORTINT, "version=0.1", NULL, NULL )) < 0) { //todo: magically put in version no. if (ret == AVAHI_ERR_COLLISION) { name = avahi_alternative_service_name(name.c_str()); std::cerr << "Service name collision, renaming service to '" << name << "'" << std::endl; avahi_entry_group_reset(group); create_services(c); return; } std::cerr << "Failed to add " << name << " service: " << avahi_strerror(ret) << std::endl; } /* Tell the server to register the service */ if ((ret = avahi_entry_group_commit(group)) < 0) { std::cerr << "Failed to commit entry group: " << avahi_strerror(ret) << std::endl; } } avahi_simple_poll_quit(simple_publish_poll); }
static gboolean create_services (DMAPMdnsPublisher * publisher, GError ** error) { GSList *ptr; int ret; if (publisher->priv->entry_group == NULL) { publisher->priv->entry_group = avahi_entry_group_new (publisher->priv->client, (AvahiEntryGroupCallback) entry_group_cb, publisher); dmap_mdns_avahi_set_entry_group (publisher-> priv->entry_group); } else { avahi_entry_group_reset (publisher->priv->entry_group); } if (publisher->priv->entry_group == NULL) { g_warning ("Could not create AvahiEntryGroup for publishing"); g_set_error (error, DMAP_MDNS_PUBLISHER_ERROR, DMAP_MDNS_PUBLISHER_ERROR_FAILED, "%s", _ ("Could not create AvahiEntryGroup for publishing")); return FALSE; } for (ptr = publisher->priv->service; ptr; ptr = g_slist_next (ptr)) { if (!create_service (ptr->data, publisher, error)) { return FALSE; } } ret = avahi_entry_group_commit (publisher->priv->entry_group); if (ret < 0) { g_set_error (error, DMAP_MDNS_PUBLISHER_ERROR, DMAP_MDNS_PUBLISHER_ERROR_FAILED, "%s: %s", _("Could not commit service"), avahi_strerror (ret)); return FALSE; } return TRUE; }
static void create_services (AvahiClient *c) { int ret; struct utsname uts; gchar *name; g_return_if_fail (c); if (uname (&uts) != 0) { printf ("failed to run uname()\n"); name = g_strdup ("xmms2"); } else { name = g_strdup (uts.nodename); } if (!group) { if (!(group = avahi_entry_group_new (c, group_callback, NULL))) { printf ("couldn't create new group!\n"); g_free (name); g_main_loop_quit (ml); return; } } if ((ret = avahi_entry_group_add_service (group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_xmms2._tcp", NULL, NULL, port, NULL)) < 0) { printf ("couldn't add entry to group: %s\n", avahi_strerror (ret)); g_free (name); g_main_loop_quit (ml); avahi_entry_group_free (group); return; } g_free (name); if ((ret = avahi_entry_group_commit (group)) < 0) { printf ("couldn't commit group: %s\n", avahi_strerror (ret)); g_main_loop_quit (ml); avahi_entry_group_free (group); return; } return; }
void CZeroconfAvahi::addService(tServiceMap::mapped_type fp_service_info, AvahiClient* fp_client) { assert(fp_client); CLog::Log(LOGDEBUG, "CZeroconfAvahi::addService() named: %s type: %s port:%i", fp_service_info->m_name.c_str(), fp_service_info->m_type.c_str(), fp_service_info->m_port); //create the group if it doesn't exist if (!fp_service_info->mp_group) { if (!(fp_service_info->mp_group = avahi_entry_group_new(fp_client, &CZeroconfAvahi::groupCallback, this))) { CLog::Log(LOGDEBUG, "CZeroconfAvahi::addService() avahi_entry_group_new() failed: %s", avahi_strerror(avahi_client_errno(fp_client))); fp_service_info->mp_group = 0; return; } } // add entries to the group if it's empty int ret; if (avahi_entry_group_is_empty(fp_service_info->mp_group)) { if ((ret = avahi_entry_group_add_service_strlst(fp_service_info->mp_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AvahiPublishFlags(0), fp_service_info->m_name.c_str(), fp_service_info->m_type.c_str(), NULL, NULL, fp_service_info->m_port, fp_service_info->mp_txt) < 0)) { if (ret == AVAHI_ERR_COLLISION) { char* alt_name = avahi_alternative_service_name(fp_service_info->m_name.c_str()); fp_service_info->m_name = alt_name; avahi_free(alt_name); CLog::Log(LOGNOTICE, "CZeroconfAvahi::addService: Service name collision. Renamed to: %s", fp_service_info->m_name.c_str()); addService(fp_service_info, fp_client); return; } CLog::Log(LOGERROR, "CZeroconfAvahi::addService(): failed to add service named:%s@$(HOSTNAME) type:%s port:%i. Error:%s :/ FIXME!", fp_service_info->m_name.c_str(), fp_service_info->m_type.c_str(), fp_service_info->m_port, avahi_strerror(ret)); return; } } // Tell the server to register the service if ((ret = avahi_entry_group_commit(fp_service_info->mp_group)) < 0) { CLog::Log(LOGERROR, "CZeroconfAvahi::addService(): Failed to commit entry group! Error:%s", avahi_strerror(ret)); // TODO what now? reset the group? free it? } }
static void register_service(AvahiClient *c) { debug(1, "avahi: register_service."); if (!group) group = avahi_entry_group_new(c, egroup_callback, NULL); if (!group) debug(2, "avahi: avahi_entry_group_new failed"); else { if (!avahi_entry_group_is_empty(group)) return; int ret; AvahiIfIndex selected_interface; if (config.interface != NULL) selected_interface = config.interface_index; else selected_interface = AVAHI_IF_UNSPEC; #ifdef CONFIG_METADATA if (config.metadata_enabled) { ret = avahi_entry_group_add_service(group, selected_interface, AVAHI_PROTO_UNSPEC, 0, service_name, config.regtype, NULL, NULL, port, MDNS_RECORD_WITH_METADATA, NULL); if (ret == 0) debug(1, "avahi: request to add \"%s\" service with metadata", config.regtype); } else { #endif ret = avahi_entry_group_add_service(group, selected_interface, AVAHI_PROTO_UNSPEC, 0, service_name, config.regtype, NULL, NULL, port, MDNS_RECORD_WITHOUT_METADATA, NULL); if (ret == 0) debug(1, "avahi: request to add \"%s\" service without metadata", config.regtype); #ifdef CONFIG_METADATA } #endif if (ret < 0) debug(1, "avahi: avahi_entry_group_add_service failed"); else { ret = avahi_entry_group_commit(group); if (ret < 0) debug(1, "avahi: avahi_entry_group_commit failed"); } } }
static int publish_main_service(struct userdata *u) { AvahiStringList *txt = NULL; int r = -1; pa_assert(u); if (!u->main_entry_group) { if (!(u->main_entry_group = avahi_entry_group_new(u->client, main_entry_group_callback, u))) { pa_log("avahi_entry_group_new() failed: %s", avahi_strerror(avahi_client_errno(u->client))); goto fail; } } else avahi_entry_group_reset(u->main_entry_group); txt = txt_record_server_data(u->core, txt); if (avahi_entry_group_add_service_strlst( u->main_entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, u->service_name, SERVICE_TYPE_SERVER, NULL, NULL, compute_port(u), txt) < 0) { pa_log("avahi_entry_group_add_service_strlst() failed: %s", avahi_strerror(avahi_client_errno(u->client))); goto fail; } if (avahi_entry_group_commit(u->main_entry_group) < 0) { pa_log("avahi_entry_group_commit() failed: %s", avahi_strerror(avahi_client_errno(u->client))); goto fail; } r = 0; fail: avahi_string_list_free(txt); return r; }
int Trick::Zeroconf::init() { #ifdef HAVE_ZEROCONF #if __linux int error; int ret ; char r[128] ; if (!(simple_poll = avahi_simple_poll_new())) { fprintf(stderr, "Failed to create simple poll object.\n"); return -1 ; } client = avahi_client_new(avahi_simple_poll_get(simple_poll), (AvahiClientFlags)0, NULL, NULL, &error); if ( client != NULL ) { group = avahi_entry_group_new(client, entry_group_callback, (void *)name.c_str()) ; if (avahi_entry_group_is_empty(group)) { ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, (AvahiPublishFlags)0, name.c_str(), type.c_str(), NULL, NULL, var_server_get_port(), NULL, r, NULL) ; ret = avahi_entry_group_commit(group) ; } } #endif #if __APPLE__ DNSServiceErrorType error ; error = DNSServiceRegister(&dns_service_ref, 0, 0, name.c_str(), type.c_str(), NULL, NULL, htons(var_server_get_port()), 0, NULL, // txt record stuff NULL, NULL); // callback stuff #endif #endif return 0 ; }
static int register_stuff(Config *config) { assert(config); if (!entry_group) { if (!(entry_group = avahi_entry_group_new(client, entry_group_callback, config))) { fprintf(stderr, "Failed to create entry group: %s\n", avahi_strerror(avahi_client_errno(client))); return -1; } } assert(avahi_entry_group_is_empty(entry_group)); if (config->command == COMMAND_PUBLISH_ADDRESS) { if (avahi_entry_group_add_address(entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, config->name, &config->address) < 0) { fprintf(stderr, "Failed to add address: %s\n", avahi_strerror(avahi_client_errno(client))); return -1; } } else { AvahiStringList *i; assert(config->command == COMMAND_PUBLISH_SERVICE); if (avahi_entry_group_add_service_strlst(entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, config->name, config->stype, config->domain, config->host, config->port, config->txt) < 0) { fprintf(stderr, "Failed to add service: %s\n", avahi_strerror(avahi_client_errno(client))); return -1; } for (i = config->subtypes; i; i = i->next) if (avahi_entry_group_add_service_subtype(entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, config->name, config->stype, config->domain, (char*) i->text) < 0) { fprintf(stderr, "Failed to add subtype '%s': %s\n", i->text, avahi_strerror(avahi_client_errno(client))); return -1; } } avahi_entry_group_commit(entry_group); return 0; }
static void _avahi_create_services(char *name, char *host, uint16_t port) { avahi_service_t *svc = (avahi_service_t *)malloc(sizeof(avahi_service_t)); int ret = 0; if (db) fprintf(stderr, "in _avahi_create_services '%s' '%s' %d\n", name, host, port); svc->name = name; svc->host = host; svc->port = port; if (!_group) { if (db) fprintf(stderr, " _avahi_create_services create group\n"); _group = avahi_entry_group_new(_client, _avahi_entry_group_callback, svc); } if (!_group) { rfbLog("avahi_entry_group_new() failed: %s\n", avahi_strerror(avahi_client_errno(_client))); return; } ret = avahi_entry_group_add_service(_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_rfb._tcp", NULL, NULL, port, NULL); if (ret < 0) { rfbLog("Failed to add _rfb._tcp service: %s\n", avahi_strerror(ret)); return; } ret = avahi_entry_group_commit(_group); if (ret < 0) { rfbLog("Failed to commit entry_group:: %s\n", avahi_strerror(ret)); return; } if (db) fprintf(stderr, "out _avahi_create_services\n"); }
static void avahi_service_try_register(AvahiServiceEntry *entry) { if (entry->group) return; /* Already registered */ if ((!avahi_client) || (avahi_client_get_state(avahi_client) != AVAHI_CLIENT_S_RUNNING)) { eDebug("[Avahi] Not running yet, cannot register type %s.", entry->service_type); return; } entry->group = avahi_entry_group_new(avahi_client, avahi_group_callback, NULL); if (!entry->group) { eDebug("[Avahi] avahi_entry_group_new failed, cannot register %s %s.", entry->service_type, entry->service_name); return; } const char *service_name = entry->service_name; /* Blank or NULL service name, use our host name as service name, * this appears to be what other services do. */ if ((!service_name) || (!*service_name)) service_name = avahi_client_get_host_name(avahi_client); if (!avahi_entry_group_add_service(entry->group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, (AvahiPublishFlags)0, service_name, entry->service_type, NULL, NULL, entry->port_num, NULL)) { avahi_entry_group_commit(entry->group); eDebug("[Avahi] Registered %s (%s) on %s:%u", service_name, entry->service_type, avahi_client_get_host_name(avahi_client), entry->port_num); } /* NOTE: group is freed by avahi_client_free */ }
void create_services(AvahiClient *c) { char *n; gchar *type_txt, *profile_txt, *data_txt; int ret; assert(c); /* If this is the first time we're called, let's create a new * entry group if necessary */ if (!group) if (!(group = avahi_entry_group_new(c, entry_group_callback, NULL))) { l_debug("avahi_entry_group_new() failed: %s", avahi_strerror(avahi_client_errno(c))); goto fail; } /* If the group is empty (either because it was just created, or * because it was reset previously, add our entries. */ if (avahi_entry_group_is_empty(group)) { l_debug("Adding service '%s' type '%s'", name, type_in); /* Set type of service */ profile_txt = g_strdup_printf("profile=%s", profile); type_txt = g_strdup_printf("type=%s", type_in); if (extra_data == NULL) { extra_data = g_strdup(""); } data_txt = g_strdup_printf("data=%s", extra_data); /* Add the service for Lyricue Display */ if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_lyricue._tcp", NULL, NULL, port, type_txt, profile_txt, data_txt, NULL)) < 0) { if (ret == AVAHI_ERR_COLLISION) goto collision; l_debug("Failed to add _lyricue._tcp service: %s", avahi_strerror(ret)); goto fail; } g_free(profile_txt); g_free(type_txt); g_free(data_txt); /* Tell the server to register the service */ if ((ret = avahi_entry_group_commit(group)) < 0) { l_debug("Failed to commit entry group: %s", avahi_strerror(ret)); goto fail; } } return; collision: /* A service name collision with a local service happened. Let's * pick a new name */ n = avahi_alternative_service_name(name); avahi_free(name); name = n; l_debug("Service name collision, renaming service to '%s'", name); avahi_entry_group_reset(group); create_services(c); return; fail: avahi_simple_poll_quit(simple_poll); }
static void create_services(AvahiClient *c) { char *n, r[128]; int ret; assert(c); /* If this is the first time we're called, let's create a new * entry group if necessary */ if (!group) if (!(group = avahi_entry_group_new(c, entry_group_callback, NULL))) { fprintf(stderr, "avahi_entry_group_new() failed: %s\n", avahi_strerror(avahi_client_errno(c))); goto fail; } /* If the group is empty (either because it was just created, or * because it was reset previously, add our entries. */ if (avahi_entry_group_is_empty(group)) { fprintf(stderr, "Adding service '%s'\n", name); /* Create some random TXT data */ snprintf(r, sizeof(r), "random=%i", rand()); /* We will now add two services and one subtype to the entry * group. The two services have the same name, but differ in * the service type (IPP vs. BSD LPR). Only services with the * same name should be put in the same entry group. */ /* Add the service for IPP */ if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_ipp._tcp", NULL, NULL, 651, "test=blah", r, NULL)) < 0) { if (ret == AVAHI_ERR_COLLISION) goto collision; 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_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_printer._tcp", NULL, NULL, 515, NULL)) < 0) { if (ret == AVAHI_ERR_COLLISION) goto collision; fprintf(stderr, "Failed to add _printer._tcp service: %s\n", avahi_strerror(ret)); goto fail; } /* Add an additional (hypothetic) subtype */ if ((ret = avahi_entry_group_add_service_subtype(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_entry_group_commit(group)) < 0) { fprintf(stderr, "Failed to commit entry group: %s\n", avahi_strerror(ret)); goto fail; } } return; collision: /* A service name collision with a local service happened. Let's * pick a new name */ n = avahi_alternative_service_name(name); avahi_free(name); name = n; fprintf(stderr, "Service name collision, renaming service to '%s'\n", name); avahi_entry_group_reset(group); create_services(c); return; fail: avahi_simple_poll_quit(simple_poll); }
gboolean _mdns_publish(BonjourDnsSd *data, PublishType type, GSList *records) { int publish_result = 0; AvahiSessionImplData *idata = data->mdns_impl_data; AvahiStringList *lst = NULL; g_return_val_if_fail(idata != NULL, FALSE); if (!idata->group) { idata->group = avahi_entry_group_new(idata->client, _entry_group_cb, idata); if (!idata->group) { purple_debug_error("bonjour", "Unable to initialize the data for the mDNS (%s).\n", avahi_strerror(avahi_client_errno(idata->client))); return FALSE; } } while (records) { PurpleKeyValuePair *kvp = records->data; lst = avahi_string_list_add_pair(lst, kvp->key, kvp->value); records = records->next; } /* Publish the service */ switch (type) { case PUBLISH_START: publish_result = avahi_entry_group_add_service_strlst( idata->group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, purple_account_get_username(data->account), ICHAT_SERVICE, NULL, NULL, data->port_p2pj, lst); break; case PUBLISH_UPDATE: publish_result = avahi_entry_group_update_service_txt_strlst( idata->group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, purple_account_get_username(data->account), ICHAT_SERVICE, NULL, lst); break; } /* Free the memory used by temp data */ avahi_string_list_free(lst); if (publish_result < 0) { purple_debug_error("bonjour", "Failed to add the " ICHAT_SERVICE " service. Error: %s\n", avahi_strerror(publish_result)); return FALSE; } if (type == PUBLISH_START && (publish_result = avahi_entry_group_commit(idata->group)) < 0) { purple_debug_error("bonjour", "Failed to commit " ICHAT_SERVICE " service. Error: %s\n", avahi_strerror(publish_result)); return FALSE; } return TRUE; }
static int /* O - 1 on success, 0 on failure */ dnssdRegisterInstance( cupsd_srv_t *srv, /* O - Service */ cupsd_printer_t *p, /* I - Printer */ char *name, /* I - DNS-SD service name */ const char *type, /* I - DNS-SD service type */ const char *subtypes, /* I - Subtypes to register or NULL */ int port, /* I - Port number or 0 */ cupsd_txt_t *txt, /* I - TXT record */ int commit, /* I - Commit registration? */ int from_callback) /* I - Called from callback? */ { char temp[256], /* Temporary string */ *ptr; /* Pointer into string */ int error; /* Any error */ # ifdef HAVE_DNSSD (void)from_callback; # endif /* HAVE_DNSSD */ cupsdLogMessage(CUPSD_LOG_DEBUG, "Registering \"%s\" with DNS-SD type \"%s\".", name, type); if (p && !srv) { /* * Assign the correct pointer for "srv"... */ # ifdef HAVE_DNSSD if (!strcmp(type, "_printer._tcp")) srv = &p->printer_srv; /* Target LPD service */ # ifdef HAVE_SSL else if (!strcmp(type, "_ipps._tcp")) srv = &p->ipps_srv; /* Target IPPS service */ # endif /* HAVE_SSL */ else srv = &p->ipp_srv; /* Target IPP service */ # else /* HAVE_AVAHI */ srv = &p->ipp_srv; /* Target service group */ # endif /* HAVE_DNSSD */ } # ifdef HAVE_DNSSD (void)commit; # else /* HAVE_AVAHI */ if (!from_callback) avahi_threaded_poll_lock(DNSSDMaster); if (!*srv) *srv = avahi_entry_group_new(DNSSDClient, dnssdRegisterCallback, NULL); if (!*srv) { if (!from_callback) avahi_threaded_poll_unlock(DNSSDMaster); cupsdLogMessage(CUPSD_LOG_WARN, "DNS-SD registration of \"%s\" failed: %s", name, dnssdErrorString(avahi_client_errno(DNSSDClient))); return (0); } # endif /* HAVE_DNSSD */ /* * Make sure the name is <= 63 octets, and when we truncate be sure to * properly truncate any UTF-8 characters... */ ptr = name + strlen(name); while ((ptr - name) > 63) { do { ptr --; } while (ptr > name && (*ptr & 0xc0) == 0x80); if (ptr > name) *ptr = '\0'; } /* * Register the service... */ # ifdef HAVE_DNSSD if (subtypes) snprintf(temp, sizeof(temp), "%s,%s", type, subtypes); else strlcpy(temp, type, sizeof(temp)); *srv = DNSSDMaster; error = DNSServiceRegister(srv, kDNSServiceFlagsShareConnection, 0, name, temp, NULL, NULL, htons(port), txt ? TXTRecordGetLength(txt) : 0, txt ? TXTRecordGetBytesPtr(txt) : NULL, dnssdRegisterCallback, p); # else /* HAVE_AVAHI */ if (txt) { AvahiStringList *temptxt; for (temptxt = *txt; temptxt; temptxt = temptxt->next) cupsdLogMessage(CUPSD_LOG_DEBUG, "DNS_SD \"%s\" %s", name, temptxt->text); } error = avahi_entry_group_add_service_strlst(*srv, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, type, NULL, NULL, port, txt ? *txt : NULL); if (error) cupsdLogMessage(CUPSD_LOG_DEBUG, "DNS-SD service add for \"%s\" failed.", name); if (!error && subtypes) { /* * Register all of the subtypes... */ char *start, /* Start of subtype */ subtype[256]; /* Subtype string */ strlcpy(temp, subtypes, sizeof(temp)); for (start = temp; *start; start = ptr) { /* * Skip leading whitespace... */ while (*start && isspace(*start & 255)) start ++; /* * Grab everything up to the next comma or the end of the string... */ for (ptr = start; *ptr && *ptr != ','; ptr ++); if (*ptr) *ptr++ = '\0'; if (!*start) break; /* * Register the subtype... */ snprintf(subtype, sizeof(subtype), "%s._sub.%s", start, type); error = avahi_entry_group_add_service_subtype(*srv, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, type, NULL, subtype); if (error) { cupsdLogMessage(CUPSD_LOG_DEBUG, "DNS-SD subtype %s registration for \"%s\" failed." , subtype, name); break; } } } if (!error && commit) { if ((error = avahi_entry_group_commit(*srv)) != 0) cupsdLogMessage(CUPSD_LOG_DEBUG, "DNS-SD commit of \"%s\" failed.", name); } if (!from_callback) avahi_threaded_poll_unlock(DNSSDMaster); # endif /* HAVE_DNSSD */ if (error) { cupsdLogMessage(CUPSD_LOG_WARN, "DNS-SD registration of \"%s\" failed: %s", name, dnssdErrorString(error)); cupsdLogMessage(CUPSD_LOG_DEBUG, "DNS-SD type: %s", type); if (subtypes) cupsdLogMessage(CUPSD_LOG_DEBUG, "DNS-SD sub-types: %s", subtypes); } return (!error); }
gboolean _mdns_set_buddy_icon_data(BonjourDnsSd *data, gconstpointer avatar_data, gsize avatar_len) { AvahiSessionImplData *idata = data->mdns_impl_data; if (idata == NULL || idata->client == NULL) return FALSE; if (avatar_data != NULL) { gboolean new_group = FALSE; gchar *svc_name; int ret; AvahiPublishFlags flags = 0; if (idata->buddy_icon_group == NULL) { purple_debug_info("bonjour", "Setting new buddy icon.\n"); new_group = TRUE; idata->buddy_icon_group = avahi_entry_group_new(idata->client, _buddy_icon_group_cb, data); } else { purple_debug_info("bonjour", "Updating existing buddy icon.\n"); flags |= AVAHI_PUBLISH_UPDATE; } if (idata->buddy_icon_group == NULL) { purple_debug_error("bonjour", "Unable to initialize the buddy icon group (%s).\n", avahi_strerror(avahi_client_errno(idata->client))); return FALSE; } svc_name = g_strdup_printf("%s." ICHAT_SERVICE "local", purple_account_get_username(data->account)); ret = avahi_entry_group_add_record(idata->buddy_icon_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, flags, svc_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_NULL, 120, avatar_data, avatar_len); g_free(svc_name); if (ret < 0) { purple_debug_error("bonjour", "Failed to register buddy icon. Error: %s\n", avahi_strerror(ret)); if (new_group) { avahi_entry_group_free(idata->buddy_icon_group); idata->buddy_icon_group = NULL; } return FALSE; } if (new_group && (ret = avahi_entry_group_commit(idata->buddy_icon_group)) < 0) { purple_debug_error("bonjour", "Failed to commit buddy icon group. Error: %s\n", avahi_strerror(ret)); if (new_group) { avahi_entry_group_free(idata->buddy_icon_group); idata->buddy_icon_group = NULL; } return FALSE; } } else if (idata->buddy_icon_group != NULL) { purple_debug_info("bonjour", "Removing existing buddy icon.\n"); avahi_entry_group_free(idata->buddy_icon_group); idata->buddy_icon_group = NULL; } return TRUE; }
void PublishAvahi::create_services(AvahiClient *c) { assert(c); char *n; /// If this is the first time we're called, let's create a new entry group if necessary if (!group) { if (!(group = avahi_entry_group_new(c, entry_group_callback, this))) { logE << "avahi_entry_group_new() failed: " << avahi_strerror(avahi_client_errno(c)) << "\n"; goto fail; } } /// If the group is empty (either because it was just created, or because it was reset previously, add our entries. int ret; if (avahi_entry_group_is_empty(group)) { logO << "Adding service '" << name << "'\n"; /// We will now add two services and one subtype to the entry group for (const auto& service: services_) { if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AvahiPublishFlags(0), name, service.name_.c_str(), NULL, NULL, service.port_, NULL)) < 0) { if (ret == AVAHI_ERR_COLLISION) goto collision; logE << "Failed to add " << service.name_ << " service: " << avahi_strerror(ret) << "\n"; goto fail; } } /// Add an additional (hypothetic) subtype /* if ((ret = avahi_entry_group_add_service_subtype(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AvahiPublishFlags(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_entry_group_commit(group)) < 0) { logE << "Failed to commit entry group: " << avahi_strerror(ret) << "\n"; goto fail; } } return; collision: /// A service name collision with a local service happened. Let's pick a new name n = avahi_alternative_service_name(name); avahi_free(name); name = n; logO << "Service name collision, renaming service to '" << name << "'\n"; avahi_entry_group_reset(group); create_services(c); return; fail: avahi_simple_poll_quit(simple_poll); }
static void register_stuff(struct context *ctx) { if (!ctx->group) { if (!(ctx->group = avahi_entry_group_new(ctx->client, publish_reply, ctx))) { rs_log_crit("Failed to create entry group: %s", avahi_strerror(avahi_client_errno(ctx->client))); goto fail; } } if (avahi_entry_group_is_empty(ctx->group)) { char cpus[32] = ""; char machine[64] = "cc_machine=", version[64] = "cc_version="; char *m = NULL, *v = NULL; if (ctx->advertise_capabilities) { snprintf(cpus, sizeof(cpus), "cpus=%i", ctx->n_cpus); v = dcc_get_gcc_version(version+11, sizeof(version)-11); m = dcc_get_gcc_machine(machine+11, sizeof(machine)-11); } /* Register our service */ if (avahi_entry_group_add_service( ctx->group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, ctx->name, ctx->service_type, NULL, NULL, ctx->port, !ctx->advertise_capabilities ? NULL : "txtvers=1", cpus, "distcc="PACKAGE_VERSION, "gnuhost="GNU_HOST, v ? version : NULL, m ? machine : NULL, NULL) < 0) { rs_log_crit("Failed to add service: %s", avahi_strerror(avahi_client_errno(ctx->client))); goto fail; } if (ctx->advertise_capabilities) { if (v && m) { char stype[128]; dcc_make_dnssd_subtype(stype, sizeof(stype), v, m); if (avahi_entry_group_add_service_subtype( ctx->group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, ctx->name, ctx->service_type, NULL, stype) < 0) { rs_log_crit("Failed to add service: %s", avahi_strerror(avahi_client_errno(ctx->client))); goto fail; } } else rs_log_warning("Failed to determine CC version, not registering DNS-SD service subtype!"); } if (avahi_entry_group_commit(ctx->group) < 0) { rs_log_crit("Failed to commit entry group: %s", avahi_strerror(avahi_client_errno(ctx->client))); goto fail; } } return; fail: avahi_threaded_poll_quit(ctx->threaded_poll); }
static void create_service (AurAvahi * avahi) { AurAvahiPrivate *priv = avahi->priv; int ret; do { if (priv->group == NULL) { priv->group = avahi_entry_group_new (priv->client, entry_group_callback, avahi); if (priv->group == NULL) { g_critical ("avahi_entry_group_new() failed: %s\n", avahi_strerror (avahi_client_errno (priv->client))); goto fail; } } /* If the group is empty (either because it was just created, or * because it was reset previously, add our entries. */ if (avahi_entry_group_is_empty (priv->group)) { g_message ("Adding service '%s' on port %d", priv->service_name, priv->port); ret = avahi_entry_group_add_service (priv->group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, priv->service_name, "_aurena._tcp", NULL, NULL, priv->port, NULL); if (ret < 0) { if (ret == AVAHI_ERR_COLLISION) { /* A service name collision with a local service happened. Let's * pick a new name */ char *n = avahi_alternative_service_name (priv->service_name); g_free (priv->service_name); priv->service_name = n; g_message ("Service name collision, renaming service to '%s'\n", priv->service_name); avahi_entry_group_reset (priv->group); continue; } else { g_critical ("Failed to add _aurena._tcp service: %s", avahi_strerror (ret)); goto fail; } } #if 0 /* Add an additional (hypothetic) subtype */ if ((ret = avahi_entry_group_add_service_subtype (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; } #endif if ((ret = avahi_entry_group_commit (priv->group)) < 0) { g_critical ("Failed to commit entry group: %s\n", avahi_strerror (ret)); } } return; } while (TRUE); fail: return; }