void NetworkServicesProviderAvahi::browseServiceType(const char* stype, const char* domain) { AvahiServiceBrowser* b; AvahiStringList* i; ASSERT(stype); for (i = m_browsedTypes; i; i = i->next) if (avahi_domain_equal(stype, (char*) i->text)) return; if (!(b = avahi_service_browser_new( m_avahiClient, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, stype, domain, (AvahiLookupFlags)0, serviceBrowserCallback, static_cast<void*>(this)))) { LOG_ERROR("avahi_service_browser_new() failed: %s\n", avahi_strerror(avahi_client_errno(m_avahiClient))); } m_browsedTypes = avahi_string_list_add(m_browsedTypes, stype); m_allForNow++; }
static void browse_service_type(Config *c, const char *stype, const char *domain) { AvahiServiceBrowser *b; AvahiStringList *i; assert(c); assert(client); assert(stype); for (i = browsed_types; i; i = i->next) if (avahi_domain_equal(stype, (char*) i->text)) return; if (!(b = avahi_service_browser_new( client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, stype, domain, 0, service_browser_callback, c))) { fprintf(stderr, ("avahi_service_browser_new() failed: %s\n"), avahi_strerror(avahi_client_errno(client))); avahi_simple_poll_quit(simple_poll); } browsed_types = avahi_string_list_add(browsed_types, stype); n_all_for_now++; n_cache_exhausted++; }
/** * ssd: The BastileServiceDiscovery to init * * When compiled WITH_SHARING avahi will also be initialised and it will browse * for avahi services. **/ static void bastile_service_discovery_init (BastileServiceDiscovery *ssd) { ssd->priv = g_new0 (BastileServiceDiscoveryPriv, 1); ssd->services = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); #ifdef WITH_SHARING { int aerr; ssd->priv->client = avahi_client_new (bastile_util_dns_sd_get_poll (), 0, client_callback, ssd, &aerr); if (!ssd->priv->client) { g_warning ("DNS-SD initialization failed: %s", avahi_strerror (aerr)); return; } ssd->priv->browser = avahi_service_browser_new (ssd->priv->client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, HKP_SERVICE_TYPE, NULL, 0, browse_callback, ssd); if (!ssd->priv->browser) { g_warning ("Browsing for DNS-SD services failed: %s", avahi_strerror (avahi_client_errno (ssd->priv->client))); return; } } #endif /* WITH_SHARING */ }
static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) { struct mdns_browser *mb; AvahiServiceBrowser *b; int error; switch (state) { case AVAHI_CLIENT_S_RUNNING: DPRINTF(E_LOG, L_MDNS, "Avahi state change: Client running\n"); if (!mdns_group) create_all_group_entries(); for (mb = browser_list; mb; mb = mb->next) { b = avahi_service_browser_new(mdns_client, AVAHI_IF_UNSPEC, mb->protocol, mb->type, NULL, 0, browse_callback, mb); if (!b) DPRINTF(E_LOG, L_MDNS, "Failed to recreate service browser (service type %s): %s\n", mb->type, MDNSERR); } break; case AVAHI_CLIENT_S_COLLISION: DPRINTF(E_LOG, L_MDNS, "Avahi state change: Client collision\n"); if(mdns_group) avahi_entry_group_reset(mdns_group); break; case AVAHI_CLIENT_FAILURE: DPRINTF(E_LOG, L_MDNS, "Avahi state change: Client failure\n"); error = avahi_client_errno(c); if (error == AVAHI_ERR_DISCONNECTED) { DPRINTF(E_LOG, L_MDNS, "Avahi Server disconnected, reconnecting\n"); avahi_client_free(mdns_client); mdns_group = NULL; mdns_client = avahi_client_new(&ev_poll_api, AVAHI_CLIENT_NO_FAIL, client_callback, NULL, &error); if (!mdns_client) DPRINTF(E_LOG, L_MDNS, "Failed to create new Avahi client: %s\n", avahi_strerror(error)); } else { DPRINTF(E_LOG, L_MDNS, "Avahi client failure: %s\n", avahi_strerror(error)); } break; case AVAHI_CLIENT_S_REGISTERING: DPRINTF(E_LOG, L_MDNS, "Avahi state change: Client registering\n"); if (mdns_group) avahi_entry_group_reset(mdns_group); break; case AVAHI_CLIENT_CONNECTING: DPRINTF(E_LOG, L_MDNS, "Avahi state change: Client connecting\n"); break; } }
int mdns_browse(char *type, int flags, mdns_browse_cb cb) { struct mdns_browser *mb; AvahiServiceBrowser *b; DPRINTF(E_DBG, L_MDNS, "Adding service browser for type %s\n", type); mb = (struct mdns_browser *)malloc(sizeof(struct mdns_browser)); if (!mb) return -1; mb->type = strdup(type); mb->cb = cb; mb->flags = (flags) ? flags : MDNS_WANT_DEFAULT; mb->next = browser_list; browser_list = mb; b = avahi_service_browser_new(mdns_client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, type, NULL, 0, browse_callback, mb); if (!b) { DPRINTF(E_LOG, L_MDNS, "Failed to create service browser: %s\n", avahi_strerror(avahi_client_errno(mdns_client))); browser_list = mb->next; free(mb->type); free(mb); return -1; } return 0; }
/** * epc_shell_create_service_browser: * @interface: the index of the network interface to watch * @protocol: the protocol of the services to watch * @type: the DNS-SD service type to watch * @domain: the DNS domain to watch, or %NULL * @flags: flags for creating the service browser * @callback: a callback function * @user_data: data to pass to @callback * @error: return location for a #GError, or %NULL * * Creates a new #AvahiServiceBrowser for the library's shared #AvahiClient. * On success the newly created #AvahiEntryGroup is returned. On failure * %NULL is returned and @error is set. The error domain is #EPC_AVAHI_ERROR. * Possible error codes are those of the <citetitle>Avahi</citetitle> library. * * Returns: The newly created #AvahiServiceBrowser, or %NULL on error. */ AvahiServiceBrowser* epc_shell_create_service_browser (AvahiIfIndex interface, AvahiProtocol protocol, const gchar *type, const gchar *domain, AvahiLookupFlags flags, AvahiServiceBrowserCallback callback, gpointer user_data, GError **error) { AvahiClient *client = epc_shell_get_avahi_client (error); AvahiServiceBrowser *browser = NULL; if (error && *error) return NULL; if (NULL != client) browser = avahi_service_browser_new (client, interface, protocol, type, domain, flags, callback, user_data); if (G_UNLIKELY (!browser)) g_set_error (error, EPC_AVAHI_ERROR, AVAHI_ERR_FAILURE, _("Cannot create Avahi service browser.")); return browser; }
/***************************************************************************** * Open: initialize and create stuff *****************************************************************************/ static int Open( vlc_object_t *p_this ) { services_discovery_t *p_sd = ( services_discovery_t* )p_this; services_discovery_sys_t *p_sys; int err; p_sd->p_sys = p_sys = calloc( 1, sizeof( services_discovery_sys_t ) ); if( !p_sys ) return VLC_ENOMEM; p_sd->description = _("Zeroconf network services"); vlc_dictionary_init( &p_sys->services_name_to_input_item, 1 ); p_sys->poll = avahi_threaded_poll_new(); if( p_sys->poll == NULL ) { msg_Err( p_sd, "failed to create Avahi threaded poll" ); goto error; } p_sys->client = avahi_client_new( avahi_threaded_poll_get(p_sys->poll), 0, client_callback, p_sd, &err ); if( p_sys->client == NULL ) { msg_Err( p_sd, "failed to create avahi client: %s", avahi_strerror( err ) ); goto error; } for( unsigned i = 0; i < NB_PROTOCOLS; i++ ) { AvahiServiceBrowser *sb; sb = avahi_service_browser_new( p_sys->client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, protocols[i].psz_service_name, NULL, 0, browse_callback, p_sd ); if( sb == NULL ) { msg_Err( p_sd, "failed to create avahi service browser %s", avahi_strerror( avahi_client_errno(p_sys->client) ) ); goto error; } } avahi_threaded_poll_start( p_sys->poll ); return VLC_SUCCESS; error: if( p_sys->client != NULL ) avahi_client_free( p_sys->client ); if( p_sys->poll != NULL ) avahi_threaded_poll_free( p_sys->poll ); vlc_dictionary_clear( &p_sys->services_name_to_input_item, NULL, NULL ); free( p_sys ); return VLC_EGENERIC; }
static void client_callback(AvahiClient *c, AvahiClientState state, void *userdata) { struct userdata *u = userdata; pa_assert(c); pa_assert(u); u->client = c; switch (state) { case AVAHI_CLIENT_S_REGISTERING: case AVAHI_CLIENT_S_RUNNING: case AVAHI_CLIENT_S_COLLISION: if (!u->sink_browser) { if (!(u->sink_browser = avahi_service_browser_new( c, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, SERVICE_TYPE_SINK, NULL, 0, browser_cb, u))) { pa_log("avahi_service_browser_new() failed: %s", avahi_strerror(avahi_client_errno(c))); pa_module_unload_request(u->module, true); } } break; case AVAHI_CLIENT_FAILURE: if (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) { int error; pa_log_debug("Avahi daemon disconnected."); if (!(u->client = avahi_client_new(u->avahi_poll, AVAHI_CLIENT_NO_FAIL, client_callback, u, &error))) { pa_log("avahi_client_new() failed: %s", avahi_strerror(error)); pa_module_unload_request(u->module, true); } } /* Fall through */ case AVAHI_CLIENT_CONNECTING: if (u->sink_browser) { avahi_service_browser_free(u->sink_browser); u->sink_browser = NULL; } break; default: ; } }
void AvahiBrowseQuery::startBrowsing() { std::cout << "Start browsing" << std::endl; assert(!browser); avahi_threaded_poll_lock(querier->getThreadedPoll()); browser = avahi_service_browser_new(querier->getClient(), AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_presence._tcp", NULL, static_cast<AvahiLookupFlags>(0), &handleServiceDiscoveredStatic, this); if (!browser) { std::cout << "Error" << std::endl; eventLoop->postEvent(boost::bind(boost::ref(onError)), shared_from_this()); } avahi_threaded_poll_unlock(querier->getThreadedPoll()); }
AvahiServiceBrowser* CZeroconfBrowserAvahi::createServiceBrowser ( const std::string& fcr_service_type, AvahiClient* fp_client, void* fp_userdata) { assert(fp_client); AvahiServiceBrowser* ret = avahi_service_browser_new ( fp_client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, fcr_service_type.c_str(), NULL, ( AvahiLookupFlags ) 0, browseCallback, fp_userdata ); if ( !ret ) { CLog::Log ( LOGERROR, "CZeroconfBrowserAvahi::createServiceBrowser Failed to create service (%s) browser: %s", avahi_strerror ( avahi_client_errno ( fp_client ) ), fcr_service_type.c_str() ); } return ret; }
/***************************************************************************** * Open: initialize and create stuff *****************************************************************************/ static int Open( vlc_object_t *p_this ) { services_discovery_t *p_sd = ( services_discovery_t* )p_this; services_discovery_sys_t *p_sys; int err; p_sd->p_sys = p_sys = calloc( 1, sizeof( services_discovery_sys_t ) ); if( !p_sys ) return VLC_ENOMEM; vlc_dictionary_init( &p_sys->services_name_to_input_item, 1 ); p_sys->poll = avahi_threaded_poll_new(); if( p_sys->poll == NULL ) { msg_Err( p_sd, "failed to create Avahi threaded poll" ); goto error; } p_sys->client = avahi_client_new( avahi_threaded_poll_get(p_sys->poll), 0, client_callback, p_sd, &err ); if( p_sys->client == NULL ) { msg_Err( p_sd, "failed to create avahi client: %s", avahi_strerror( err ) ); goto error; } p_sys->sb = avahi_service_browser_new( p_sys->client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_vlc-http._tcp", NULL, 0, browse_callback, p_sd ); if( p_sys->sb == NULL ) { msg_Err( p_sd, "failed to create avahi service browser" ); goto error; } return VLC_SUCCESS; error: if( p_sys->sb != NULL ) avahi_service_browser_free( p_sys->sb ); if( p_sys->client != NULL ) avahi_client_free( p_sys->client ); if( p_sys->poll != NULL ) avahi_threaded_poll_free( p_sys->poll ); vlc_dictionary_clear( &p_sys->services_name_to_input_item, NULL, NULL ); free( p_sys ); return VLC_EGENERIC; }
int avahi_discover_tvh(struct htsp_t* htsp) { AvahiClient *client = NULL; AvahiServiceBrowser *sb = NULL; int error; int ret = 1; /* Allocate main loop object */ if (!(simple_poll = avahi_simple_poll_new())) { fprintf(stderr, "Failed to create simple poll object.\n"); goto fail; } /* Allocate a new client */ client = avahi_client_new(avahi_simple_poll_get(simple_poll), 0, client_callback, NULL, &error); /* Check wether creating the client object succeeded */ if (!client) { fprintf(stderr, "Failed to create client: %s\n", avahi_strerror(error)); goto fail; } /* Create the service browser */ if (!(sb = avahi_service_browser_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_INET, "_htsp._tcp", NULL, 0, browse_callback, client))) { fprintf(stderr, "Failed to create service browser: %s\n", avahi_strerror(avahi_client_errno(client))); goto fail; } /* Run the main loop */ avahi_simple_poll_loop(simple_poll); ret = 0; fail: /* Cleanup things */ if (sb) avahi_service_browser_free(sb); if (client) avahi_client_free(client); if (simple_poll) avahi_simple_poll_free(simple_poll); if (tvh_hostname) { htsp->host = tvh_hostname; htsp->ip = tvh_ip; htsp->port = tvh_port; } return ret; }
void Avahi::Heap::ClientCallback (AvahiClient *_client, AvahiClientState state) { /* this is the good client pointer */ client = _client; switch (state) { case AVAHI_CLIENT_FAILURE: /* bad, bad: free the client and try to get another one... but * won't I tax the box? */ #if DEBUG std::cout << __PRETTY_FUNCTION__ << " AVAHI_CLIENT_FAILURE" << std::endl; #endif if (client != NULL) avahi_client_free (client); client = NULL; break; case AVAHI_CLIENT_S_RUNNING: /* ignore what we get from the new, as it may not be the final * valid browser pointer... we'll take what our callback gets */ avahi_service_browser_new (client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_sip._udp", NULL, (AvahiLookupFlags)0, avahi_browser_callback, this); #if DEBUG std::cout << __PRETTY_FUNCTION__ << " AVAHI_CLIENT_S_RUNNING" << std::endl; if (browser == NULL) std::cout << "but NULL browser!" << std::endl; #endif break; case AVAHI_CLIENT_CONNECTING: case AVAHI_CLIENT_S_REGISTERING: case AVAHI_CLIENT_S_COLLISION: /* do nothing */ #if DEBUG std::cout << __PRETTY_FUNCTION__ << " OTHER" << std::endl; #endif break; default: #if DEBUG std::cout << __PRETTY_FUNCTION__ << " SHOULDN'T HAPPEN" << std::endl; #endif /* shouldn't happen */ break; } }
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) { AvahiClient *client = NULL; AvahiRecordBrowser *sb = NULL; int error; int ret = 1; /* Allocate main loop object */ if (!(simple_poll = avahi_simple_poll_new())) { fprintf(stderr, "Failed to create simple poll object.\n"); goto fail; } /* Allocate a new client */ client = avahi_client_new(avahi_simple_poll_get(simple_poll), 0, client_callback, NULL, &error); /* Check wether creating the client object succeeded */ if (!client) { fprintf(stderr, "Failed to create client: %s\n", avahi_strerror(error)); goto fail; } /* Create the service browser */ if (!(sb = avahi_service_browser_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_http._tcp", NULL, 0, browse_callback, client))) { fprintf(stderr, "Failed to create service browser: %s\n", avahi_strerror(avahi_client_errno(client))); goto fail; } /* Run the main loop */ avahi_simple_poll_loop(simple_poll); ret = 0; fail: /* Cleanup things */ if (sb) avahi_service_browser_free(sb); if (client) avahi_client_free(client); if (simple_poll) avahi_simple_poll_free(simple_poll); return ret; }
gboolean _mdns_browse(BonjourDnsSd *data) { AvahiSessionImplData *idata = data->mdns_impl_data; g_return_val_if_fail(idata != NULL, FALSE); idata->sb = avahi_service_browser_new(idata->client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, ICHAT_SERVICE, NULL, 0, _browser_callback, data->account); if (!idata->sb) { purple_debug_error("bonjour", "Unable to initialize service browser. Error: %s\n.", avahi_strerror(avahi_client_errno(idata->client))); return FALSE; } return TRUE; }
static int discover_host(AvahiAddress *addr, uint16_t *port) { struct avahi_discovery_data ddata; int ret = 0; AvahiClient *client; AvahiServiceBrowser *browser; AvahiSimplePoll *poll = avahi_simple_poll_new(); if (!poll) return -ENOMEM; client = avahi_client_new(avahi_simple_poll_get(poll), 0, NULL, NULL, &ret); if (!client) { ERROR("Unable to start ZeroConf client :%s\n", avahi_strerror(ret)); goto err_free_poll; } memset(&ddata, 0, sizeof(ddata)); ddata.poll = poll; ddata.address = addr; ddata.port = port; browser = avahi_service_browser_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_iio._tcp", NULL, 0, __avahi_browser_cb, &ddata); if (!browser) { ret = avahi_client_errno(client); ERROR("Unable to create ZeroConf browser: %s\n", avahi_strerror(ret)); goto err_free_client; } DEBUG("Trying to discover host\n"); avahi_simple_poll_loop(poll); if (!ddata.found) ret = ENXIO; avahi_service_browser_free(browser); err_free_client: avahi_client_free(client); err_free_poll: avahi_simple_poll_free(poll); return -ret; /* we want a negative error code */ }
static void avahi_browser_try_register(AvahiBrowserEntry *entry) { if (entry->browser) return; /* Already registered */ if ((!avahi_client) || (avahi_client_get_state(avahi_client) != AVAHI_CLIENT_S_RUNNING)) { eDebug("[Avahi] Not running yet, cannot browse for type %s.", entry->service_type); return; } entry->browser = avahi_service_browser_new(avahi_client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, entry->service_type, NULL, (AvahiLookupFlags)0, avahi_browser_callback, entry); if (!entry->browser) { eDebug("[Avahi] avahi_service_browser_new failed: %s", avahi_strerror(avahi_client_errno(avahi_client))); } }
Implementation(Browser *master, Context *ctx) : master_(master) , browser_(NULL) { AvahiClient *client = (AvahiClient*)ctx->context(); // Create the service browser. browser_ = avahi_service_browser_new( client, // AVAHI_IF_UNSPEC, // any interface AVAHI_PROTO_UNSPEC, // any protocol master_->serviceType(), // service type ("_http._tcp") NULL, // default domain (".local") (AvahiLookupFlags)0, sBrowseCallback, this); if (!browser_) { throw dub::Exception("Failed to create service browser (%s).\n", avahi_strerror(avahi_client_errno(client))); } }
bool BrowseAvahi::browse(const std::string& serviceName, int proto, AvahiResult& result, int timeout) { int error; /* Allocate main loop object */ if (!(simple_poll = avahi_simple_poll_new())) { logE << "Failed to create simple poll object.\n"; goto fail; } /* Allocate a new client */ client_ = avahi_client_new(avahi_simple_poll_get(simple_poll), (AvahiClientFlags)0, client_callback, this, &error); /* Check wether creating the client object succeeded */ if (!client_) { logE << "Failed to create client: " << avahi_strerror(error) << "\n"; goto fail; } /* Create the service browser */ if (!(sb_ = avahi_service_browser_new(client_, AVAHI_IF_UNSPEC, proto, serviceName.c_str(), NULL, (AvahiLookupFlags)0, browse_callback, this))) { logE << "Failed to create service browser: " << avahi_strerror(avahi_client_errno(client_)) << "\n"; goto fail; } result_.valid_ = false; while (timeout > 0) { avahi_simple_poll_iterate(simple_poll, 100); timeout -= 100; if (result_.valid_) { result = result_; return true; } } fail: return false; }
static void snra_avahi_client_callback (AvahiClient * s, AvahiClientState state, SnraClient * client) { switch (state) { case AVAHI_CLIENT_S_RUNNING:{ if (client->avahi_sb == NULL) { g_print ("Looking for new broadcast servers\n"); client->avahi_sb = avahi_service_browser_new (s, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_aurena._tcp", NULL, 0, browse_callback, client); if (client->avahi_sb == NULL) { fprintf (stderr, "Failed to create service browser: %s\n", avahi_strerror (avahi_client_errno (client->avahi_client))); } } break; } default: break; } }
gboolean dmap_mdns_browser_start (DMAPMdnsBrowser * browser, GError ** error) { if (browser->priv->client == NULL) { g_set_error (error, DMAP_MDNS_BROWSER_ERROR, DMAP_MDNS_BROWSER_ERROR_NOT_RUNNING, "%s", _("MDNS service is not running")); return FALSE; } if (browser->priv->service_browser != NULL) { g_debug ("Browser already active"); return TRUE; } browser->priv->service_browser = avahi_service_browser_new (browser->priv->client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, service_type_name[browser-> priv->service_type], NULL, #ifdef HAVE_AVAHI_0_6 0, #endif (AvahiServiceBrowserCallback) browse_cb, browser); if (browser->priv->service_browser == NULL) { g_debug ("Error starting mDNS discovery using AvahiServiceBrowser"); g_set_error (error, DMAP_MDNS_BROWSER_ERROR, DMAP_MDNS_BROWSER_ERROR_FAILED, "%s", _("Unable to activate browser")); return FALSE; } return TRUE; }
bool BrowseAvahi::browse(const std::string& serviceName, mDNSResult& result, int timeout) { try { /* Allocate main loop object */ if (!(simple_poll = avahi_simple_poll_new())) throw SnapException("BrowseAvahi - Failed to create simple poll object"); /* Allocate a new client */ int error; if (!(client_ = avahi_client_new(avahi_simple_poll_get(simple_poll), (AvahiClientFlags)0, client_callback, this, &error))) throw SnapException("BrowseAvahi - Failed to create client: " + std::string(avahi_strerror(error))); /* Create the service browser */ if (!(sb_ = avahi_service_browser_new(client_, AVAHI_IF_UNSPEC, AVAHI_PROTO_INET, serviceName.c_str(), NULL, (AvahiLookupFlags)0, browse_callback, this))) throw SnapException("BrowseAvahi - Failed to create service browser: " + std::string(avahi_strerror(avahi_client_errno(client_)))); result_.valid_ = false; while (timeout > 0) { avahi_simple_poll_iterate(simple_poll, 100); timeout -= 100; if (result_.valid_) { result = result_; cleanUp(); return true; } } cleanUp(); return false; } catch (...) { cleanUp(); throw; } }
bool ofxAvahiClientBrowser::lookup(const string& type){ int err; //struct timeval tv; if (!(poll = avahi_simple_poll_new())) { ofLogError(LOG_NAME) << "Failed to create simple poll object"; return false; } client = avahi_client_new(avahi_simple_poll_get(poll),(AvahiClientFlags)0,(AvahiClientCallback)&client_cb,this,&err); if(!client){ ofLogError(LOG_NAME) << "Failed to create avahi client" << avahi_strerror(err); close(); return false; } avahi_service_browser_new(client,AVAHI_IF_UNSPEC,AVAHI_PROTO_INET,type.c_str(),NULL,(AvahiLookupFlags)0,(AvahiServiceBrowserCallback)service_browser_cb,this); startThread(true,false); return true; }
void Avahi::avahiDiscovery() { int error; /* Allocate main loop object */ if (!(simple_discovery_poll = avahi_simple_poll_new())) { std::cerr << "Failed to create simple poll object." << std::endl; } /* Allocate a new client */ discoveryClient = avahi_client_new(avahi_simple_poll_get(simple_discovery_poll), (AvahiClientFlags)0, client_discovery_callback, NULL, &error); /* Check whether creating the client object succeeded */ if (!discoveryClient) { std::cerr << "Failed to create client: " << avahi_strerror(error) << std::endl; } /* Create the service browser */ if (!(sb = avahi_service_browser_new(discoveryClient, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_heatSync._tcp", NULL, (AvahiLookupFlags)0, browse_callback, discoveryClient))) { std::cerr << "Failed to create service browser: " << avahi_strerror(avahi_client_errno(discoveryClient)) << std::endl; } /* Run the main loop */ avahi_simple_poll_loop(simple_discovery_poll); }
int publish_avahi(int port_passed, char *type_in_passed) { int error; AvahiGLibPoll *glib_poll; port = port_passed; type_in = strndup(type_in_passed,32); avahi_set_allocator (avahi_glib_allocator ()); /* Create the GLIB Adaptor */ glib_poll = avahi_glib_poll_new (NULL, G_PRIORITY_DEFAULT); name = avahi_strdup("Lyricue Display"); /* Allocate a new client */ client = avahi_client_new(avahi_glib_poll_get(glib_poll), 0, client_callback, NULL, &error); /* Check if creating the client object succeeded */ if (!client) { l_debug("Failed to create client: %s", avahi_strerror(error)); unpublish_avahi(); } /* Create the service browser */ if (miniviews==NULL) { miniviews = g_hash_table_new(g_str_hash, g_str_equal); } if (sb == NULL) { if (!(sb = avahi_service_browser_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_INET, "_lyricue._tcp", NULL, 0, browse_callback, client))) { l_debug("Failed to create service browser: %s\n", avahi_strerror(avahi_client_errno(client))); } } return 0; }
int mdns_browse(char *type, int family, mdns_browse_cb cb) { struct mdns_browser *mb; AvahiServiceBrowser *b; DPRINTF(E_DBG, L_MDNS, "Adding service browser for type %s\n", type); mb = calloc(1, sizeof(struct mdns_browser)); if (!mb) { DPRINTF(E_LOG, L_MDNS, "Out of memory for new mdns browser\n"); return -1; } mb->protocol = avahi_af_to_proto(family); mb->type = strdup(type); mb->cb = cb; mb->next = browser_list; browser_list = mb; b = avahi_service_browser_new(mdns_client, AVAHI_IF_UNSPEC, mb->protocol, mb->type, NULL, 0, browse_callback, mb); if (!b) { DPRINTF(E_LOG, L_MDNS, "Failed to create service browser: %s\n", MDNSERR); browser_list = mb->next; free(mb->type); free(mb); return -1; } return 0; }
static void browse_callback(AvahiServiceBrowser *b, AvahiIfIndex intf, AvahiProtocol proto, AvahiBrowserEvent event, const char *name, const char *type, const char *domain, AvahiLookupResultFlags flags, void *userdata) { struct mdns_browser *mb; AvahiServiceResolver *res; int family; mb = (struct mdns_browser *)userdata; switch (event) { case AVAHI_BROWSER_FAILURE: DPRINTF(E_LOG, L_MDNS, "Avahi Browser failure: %s\n", avahi_strerror(avahi_client_errno(mdns_client))); avahi_service_browser_free(b); b = avahi_service_browser_new(mdns_client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, mb->type, NULL, 0, browse_callback, mb); if (!b) { DPRINTF(E_LOG, L_MDNS, "Failed to recreate service browser (service type %s): %s\n", mb->type, avahi_strerror(avahi_client_errno(mdns_client))); } return; case AVAHI_BROWSER_NEW: DPRINTF(E_DBG, L_MDNS, "Avahi Browser: NEW service '%s' type '%s' proto %d\n", name, type, proto); res = avahi_service_resolver_new(mdns_client, intf, proto, name, type, domain, proto, 0, browse_resolve_callback, mb); if (!res) DPRINTF(E_LOG, L_MDNS, "Failed to create service resolver: %s\n", avahi_strerror(avahi_client_errno(mdns_client))); break; case AVAHI_BROWSER_REMOVE: DPRINTF(E_DBG, L_MDNS, "Avahi Browser: REMOVE service '%s' type '%s' proto %d\n", name, type, proto); switch (proto) { case AVAHI_PROTO_INET: family = AF_INET; break; case AVAHI_PROTO_INET6: family = AF_INET6; break; default: DPRINTF(E_INFO, L_MDNS, "Avahi Browser: unknown protocol %d\n", proto); family = AF_UNSPEC; break; } if (family != AF_UNSPEC) mb->cb(name, type, domain, NULL, family, NULL, -1, NULL); break; case AVAHI_BROWSER_ALL_FOR_NOW: case AVAHI_BROWSER_CACHE_EXHAUSTED: DPRINTF(E_DBG, L_MDNS, "Avahi Browser (%s): no more results (%s)\n", mb->type, (event == AVAHI_BROWSER_CACHE_EXHAUSTED) ? "CACHE_EXHAUSTED" : "ALL_FOR_NOW"); break; } }
int /* O - Exit status */ main(int argc, /* I - Number of command-line args */ char *argv[]) /* I - Command-line arguments */ { const char *name; /* Backend name */ cups_array_t *devices; /* Device array */ cups_device_t *device; /* Current device */ char uriName[1024]; /* Unquoted fullName for URI */ #ifdef HAVE_DNSSD int fd; /* Main file descriptor */ fd_set input; /* Input set for select() */ struct timeval timeout; /* Timeout for select() */ DNSServiceRef main_ref, /* Main service reference */ fax_ipp_ref, /* IPP fax service reference */ ipp_ref, /* IPP service reference */ ipp_tls_ref, /* IPP w/TLS service reference */ ipps_ref, /* IPP service reference */ local_fax_ipp_ref, /* Local IPP fax service reference */ local_ipp_ref, /* Local IPP service reference */ local_ipp_tls_ref, /* Local IPP w/TLS service reference */ local_ipps_ref, /* Local IPP service reference */ local_printer_ref, /* Local LPD service reference */ pdl_datastream_ref, /* AppSocket service reference */ printer_ref, /* LPD service reference */ riousbprint_ref; /* Remote IO service reference */ #endif /* HAVE_DNSSD */ #ifdef HAVE_AVAHI AvahiClient *client; /* Client information */ int error; /* Error code, if any */ #endif /* HAVE_AVAHI */ #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET) struct sigaction action; /* Actions for POSIX signals */ #endif /* HAVE_SIGACTION && !HAVE_SIGSET */ /* * Don't buffer stderr, and catch SIGTERM... */ setbuf(stderr, NULL); #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */ sigset(SIGTERM, sigterm_handler); #elif defined(HAVE_SIGACTION) memset(&action, 0, sizeof(action)); sigemptyset(&action.sa_mask); action.sa_handler = sigterm_handler; sigaction(SIGTERM, &action, NULL); #else signal(SIGTERM, sigterm_handler); #endif /* HAVE_SIGSET */ /* * Check command-line... */ if (argc >= 6) exec_backend(argv); else if (argc != 1) { _cupsLangPrintf(stderr, _("Usage: %s job-id user title copies options [file]"), argv[0]); return (1); } /* * Only do discovery when run as "dnssd"... */ if ((name = strrchr(argv[0], '/')) != NULL) name ++; else name = argv[0]; if (strcmp(name, "dnssd")) return (0); /* * Create an array to track devices... */ devices = cupsArrayNew((cups_array_func_t)compare_devices, NULL); /* * Browse for different kinds of printers... */ #ifdef HAVE_DNSSD if (DNSServiceCreateConnection(&main_ref) != kDNSServiceErr_NoError) { perror("ERROR: Unable to create service connection"); return (1); } fd = DNSServiceRefSockFD(main_ref); fax_ipp_ref = main_ref; DNSServiceBrowse(&fax_ipp_ref, kDNSServiceFlagsShareConnection, 0, "_fax-ipp._tcp", NULL, browse_callback, devices); ipp_ref = main_ref; DNSServiceBrowse(&ipp_ref, kDNSServiceFlagsShareConnection, 0, "_ipp._tcp", NULL, browse_callback, devices); ipp_tls_ref = main_ref; DNSServiceBrowse(&ipp_tls_ref, kDNSServiceFlagsShareConnection, 0, "_ipp-tls._tcp", NULL, browse_callback, devices); ipps_ref = main_ref; DNSServiceBrowse(&ipps_ref, kDNSServiceFlagsShareConnection, 0, "_ipps._tcp", NULL, browse_callback, devices); local_fax_ipp_ref = main_ref; DNSServiceBrowse(&local_fax_ipp_ref, kDNSServiceFlagsShareConnection, kDNSServiceInterfaceIndexLocalOnly, "_fax-ipp._tcp", NULL, browse_local_callback, devices); local_ipp_ref = main_ref; DNSServiceBrowse(&local_ipp_ref, kDNSServiceFlagsShareConnection, kDNSServiceInterfaceIndexLocalOnly, "_ipp._tcp", NULL, browse_local_callback, devices); local_ipp_tls_ref = main_ref; DNSServiceBrowse(&local_ipp_tls_ref, kDNSServiceFlagsShareConnection, kDNSServiceInterfaceIndexLocalOnly, "_ipp-tls._tcp", NULL, browse_local_callback, devices); local_ipps_ref = main_ref; DNSServiceBrowse(&local_ipps_ref, kDNSServiceFlagsShareConnection, kDNSServiceInterfaceIndexLocalOnly, "_ipps._tcp", NULL, browse_local_callback, devices); local_printer_ref = main_ref; DNSServiceBrowse(&local_printer_ref, kDNSServiceFlagsShareConnection, kDNSServiceInterfaceIndexLocalOnly, "_printer._tcp", NULL, browse_local_callback, devices); pdl_datastream_ref = main_ref; DNSServiceBrowse(&pdl_datastream_ref, kDNSServiceFlagsShareConnection, 0, "_pdl-datastream._tcp", NULL, browse_callback, devices); printer_ref = main_ref; DNSServiceBrowse(&printer_ref, kDNSServiceFlagsShareConnection, 0, "_printer._tcp", NULL, browse_callback, devices); riousbprint_ref = main_ref; DNSServiceBrowse(&riousbprint_ref, kDNSServiceFlagsShareConnection, 0, "_riousbprint._tcp", NULL, browse_callback, devices); #endif /* HAVE_DNSSD */ #ifdef HAVE_AVAHI if ((simple_poll = avahi_simple_poll_new()) == NULL) { fputs("DEBUG: Unable to create Avahi simple poll object.\n", stderr); return (0); } avahi_simple_poll_set_func(simple_poll, poll_callback, NULL); client = avahi_client_new(avahi_simple_poll_get(simple_poll), 0, client_callback, simple_poll, &error); if (!client) { fputs("DEBUG: Unable to create Avahi client.\n", stderr); return (0); } browsers = 6; avahi_service_browser_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_fax-ipp._tcp", NULL, 0, browse_callback, devices); avahi_service_browser_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_ipp._tcp", NULL, 0, browse_callback, devices); avahi_service_browser_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_ipp-tls._tcp", NULL, 0, browse_callback, devices); avahi_service_browser_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_ipps._tcp", NULL, 0, browse_callback, devices); avahi_service_browser_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_pdl-datastream._tcp", NULL, 0, browse_callback, devices); avahi_service_browser_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_printer._tcp", NULL, 0, browse_callback, devices); #endif /* HAVE_AVAHI */ /* * Loop until we are killed... */ while (!job_canceled) { int announce = 0; /* Announce printers? */ #ifdef HAVE_DNSSD FD_ZERO(&input); FD_SET(fd, &input); timeout.tv_sec = 0; timeout.tv_usec = 500000; if (select(fd + 1, &input, NULL, NULL, &timeout) < 0) continue; if (FD_ISSET(fd, &input)) { /* * Process results of our browsing... */ DNSServiceProcessResult(main_ref); } else announce = 1; #elif defined(HAVE_AVAHI) got_data = 0; if ((error = avahi_simple_poll_iterate(simple_poll, 500)) > 0) { /* * We've been told to exit the loop. Perhaps the connection to * Avahi failed. */ break; } if (!got_data) announce = 1; #endif /* HAVE_DNSSD */ /* fprintf(stderr, "DEBUG: announce=%d\n", announce);*/ if (announce) { /* * Announce any devices we've found... */ #ifdef HAVE_DNSSD DNSServiceErrorType status; /* DNS query status */ #endif /* HAVE_DNSSD */ cups_device_t *best; /* Best matching device */ char device_uri[1024]; /* Device URI */ int count; /* Number of queries */ int sent; /* Number of sent */ for (device = (cups_device_t *)cupsArrayFirst(devices), best = NULL, count = 0, sent = 0; device; device = (cups_device_t *)cupsArrayNext(devices)) { if (device->sent) sent ++; if (device->ref) count ++; if (!device->ref && !device->sent) { /* * Found the device, now get the TXT record(s) for it... */ if (count < 50) { fprintf(stderr, "DEBUG: Querying \"%s\"...\n", device->fullName); #ifdef HAVE_DNSSD device->ref = main_ref; status = DNSServiceQueryRecord(&(device->ref), kDNSServiceFlagsShareConnection, 0, device->fullName, kDNSServiceType_TXT, kDNSServiceClass_IN, query_callback, device); if (status != kDNSServiceErr_NoError) fprintf(stderr, "ERROR: Unable to query \"%s\" for TXT records: %d\n", device->fullName, status); /* Users never see this */ else count ++; #else if ((device->ref = avahi_record_browser_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, device->fullName, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT, 0, query_callback, device)) == NULL) fprintf(stderr, "ERROR: Unable to query \"%s\" for TXT records: %s\n", device->fullName, avahi_strerror(avahi_client_errno(client))); /* Users never see this */ else count ++; #endif /* HAVE_AVAHI */ } } else if (!device->sent) { #ifdef HAVE_DNSSD /* * Got the TXT records, now report the device... */ DNSServiceRefDeallocate(device->ref); #else avahi_record_browser_free(device->ref); #endif /* HAVE_DNSSD */ device->ref = NULL; if (!best) best = device; else if (_cups_strcasecmp(best->name, device->name) || _cups_strcasecmp(best->domain, device->domain)) { unquote(uriName, best->fullName, sizeof(uriName)); if (best->uuid) httpAssembleURIf(HTTP_URI_CODING_ALL, device_uri, sizeof(device_uri), "dnssd", NULL, uriName, 0, best->cups_shared ? "/cups?uuid=%s" : "/?uuid=%s", best->uuid); else httpAssembleURI(HTTP_URI_CODING_ALL, device_uri, sizeof(device_uri), "dnssd", NULL, uriName, 0, best->cups_shared ? "/cups" : "/"); cupsBackendReport("network", device_uri, best->make_and_model, best->name, best->device_id, NULL); best->sent = 1; best = device; sent ++; } else if (best->priority > device->priority || (best->priority == device->priority && best->type < device->type)) { best->sent = 1; best = device; sent ++; } else { device->sent = 1; sent ++; } } } if (best) { unquote(uriName, best->fullName, sizeof(uriName)); if (best->uuid) httpAssembleURIf(HTTP_URI_CODING_ALL, device_uri, sizeof(device_uri), "dnssd", NULL, uriName, 0, best->cups_shared ? "/cups?uuid=%s" : "/?uuid=%s", best->uuid); else httpAssembleURI(HTTP_URI_CODING_ALL, device_uri, sizeof(device_uri), "dnssd", NULL, uriName, 0, best->cups_shared ? "/cups" : "/"); cupsBackendReport("network", device_uri, best->make_and_model, best->name, best->device_id, NULL); best->sent = 1; sent ++; } fprintf(stderr, "DEBUG: sent=%d, count=%d\n", sent, count); #ifdef HAVE_AVAHI if (sent == cupsArrayCount(devices) && browsers == 0) #else if (sent == cupsArrayCount(devices)) #endif /* HAVE_AVAHI */ break; } } return (CUPS_BACKEND_OK); }
gboolean daap_mdns_setup () { const AvahiPoll *av_poll; GMainLoop *ml = NULL; gint errval; struct timeval tv; browse_callback_userdata_t *browse_userdata; if (gl_poll) { goto fail; } browse_userdata = g_new0 (browse_callback_userdata_t, 1); avahi_set_allocator (avahi_glib_allocator ()); ml = g_main_loop_new (NULL, FALSE); gl_poll = avahi_glib_poll_new (NULL, G_PRIORITY_DEFAULT); av_poll = avahi_glib_poll_get (gl_poll); avahi_elapse_time (&tv, 2000, 0); av_poll->timeout_new (av_poll, &tv, daap_mdns_timeout, NULL); client = avahi_client_new (av_poll, 0, daap_mdns_client_cb, ml, &errval); if (!client) { goto fail; } browse_userdata->client = client; browse_userdata->mainloop = ml; browser = avahi_service_browser_new (client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_daap._tcp", NULL, 0, daap_mdns_browse_cb, browse_userdata); if (!browser) { goto fail; } return TRUE; fail: if (ml) g_main_loop_unref (ml); if (client) avahi_client_free (client); client = NULL; browser = NULL; g_free (browse_userdata); if (gl_poll) avahi_glib_poll_free (gl_poll); gl_poll = NULL; return FALSE; }
int main( AVAHI_GCC_UNUSED int argc, char *argv[] ) { AvahiClient *client = NULL; AvahiServiceBrowser *serviceBrowser = NULL; char *myName; int error; int result = 1; TiVoUnit *tivo; myName = strrchr(argv[0], '/') + 1; if (myName == (NULL+1)) myName = argv[0]; openlog( myName, LOG_PID, LOG_DAEMON ); loginfo("started"); /* must be done before any threads start */ curl_global_init( CURL_GLOBAL_ALL ); /* Allocate main loop object */ bonjourThread = avahi_threaded_poll_new(); if ( !bonjourThread ) { logerror( "Failed to create threaded poll object." ); } else { /* Allocate a new client */ client = avahi_client_new( avahi_threaded_poll_get( bonjourThread ), 0, client_callback, NULL, &error ); /* Check wether creating the client object succeeded */ if ( !client ) { logerror( "Unable to create client: %s", avahi_strerror( error ) ); } else { /* Create the service browser */ serviceBrowser = avahi_service_browser_new( client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_tivo-device._tcp", NULL, 0, browseCallback, client ); if ( !serviceBrowser ) { logerror( "Unable to create service browser: %s", avahi_strerror( avahi_client_errno( client ) ) ); } else { /* start the background network scan for TiVos */ avahi_threaded_poll_start( bonjourThread ); /* hack: wait for some results to arrive */ sleep(5); /* what did we find? */ result = dumpTiVos(); avahi_threaded_poll_stop( bonjourThread ); avahi_service_browser_free( serviceBrowser ); } avahi_client_free( client ); } avahi_threaded_poll_free( bonjourThread ); } /* docs say no other threads may be running */ curl_global_cleanup(); loginfo( "stopped" ); closelog(); return result; }