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 */ }
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) { 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_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; }
void Avahi::avahiPublish() { int error; /* Allocate main loop object */ if (!(simple_publish_poll = avahi_simple_poll_new())) { std::cerr << "Failed to create simple poll object." << std::endl; } /* Allocate a new client */ publishClient = avahi_client_new(avahi_simple_poll_get(simple_publish_poll), (AvahiClientFlags)0, client_publish_callback, NULL, &error); /* Check whether creating the client object succeeded */ if (!publishClient) { std::cerr << "Failed to create client: " << avahi_strerror(error) << std::endl; } /* Run the main loop */ avahi_simple_poll_loop(simple_publish_poll); }
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); }
/** cnames should be a NULL-terminated array of alias hostnames for this host. * Example invocation: const char * cnames = {"foo.local", "bar.local", NULL}; PublishAvahiCNames(cnames); * Note that this function normally does not ever return! */ void PublishAvahiCNames(const char ** cnames) { struct runtime_data r; _cnames = cnames; /* Allocate main loop object */ _poll = avahi_simple_poll_new(); if (_poll) { int error; AvahiClient * client = avahi_client_new(avahi_simple_poll_get(_poll), (AvahiClientFlags) 0, client_cnames_callback, NULL, &error); if (client) { avahi_simple_poll_loop(_poll); avahi_client_free(client); } else ap_log_error(APLOG_MARK, APLOG_ERR, 0, r.main_server, "Failed to create Avahi client: %s", avahi_strerror(error)); avahi_simple_poll_free(_poll); } else ap_log_error(APLOG_MARK, APLOG_ERR, 0, r.main_server, "Failed to create Avahi simple poll object."); }
int wxAvahiSimplePool::Loop(){ return avahi_simple_poll_loop(m_pool); }
int main(int argc, char *argv[]) { int ret = 1, error; Config config; const char *argv0; avahi_init_i18n(); setlocale(LC_ALL, ""); if ((argv0 = strrchr(argv[0], '/'))) argv0++; else argv0 = argv[0]; if (parse_command_line(&config, argv0, argc, argv) < 0) goto fail; switch (config.command) { case COMMAND_UNSPEC: ret = 1; fprintf(stderr, _("No command specified.\n")); break; case COMMAND_HELP: help(stdout, argv0); ret = 0; break; case COMMAND_VERSION: printf("%s "PACKAGE_VERSION"\n", argv0); ret = 0; break; case COMMAND_RESOLVE_HOST_NAME: case COMMAND_RESOLVE_ADDRESS: { int i; if (!(simple_poll = avahi_simple_poll_new())) { fprintf(stderr, _("Failed to create simple poll object.\n")); goto fail; } if (sigint_install(simple_poll) < 0) goto fail; if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), 0, client_callback, NULL, &error))) { fprintf(stderr, _("Failed to create client object: %s\n"), avahi_strerror(error)); goto fail; } if (config.verbose) { const char *version, *hn; if (!(version = avahi_client_get_version_string(client))) { fprintf(stderr, _("Failed to query version string: %s\n"), avahi_strerror(avahi_client_errno(client))); goto fail; } if (!(hn = avahi_client_get_host_name_fqdn(client))) { fprintf(stderr, _("Failed to query host name: %s\n"), avahi_strerror(avahi_client_errno(client))); goto fail; } fprintf(stderr, _("Server version: %s; Host name: %s\n"), version, hn); } n_resolving = 0; for (i = optind; i < argc; i++) { if (config.command == COMMAND_RESOLVE_HOST_NAME) { if (!(avahi_host_name_resolver_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, argv[i], config.proto, 0, host_name_resolver_callback, NULL))) { fprintf(stderr, _("Failed to create host name resolver: %s\n"), avahi_strerror(avahi_client_errno(client))); goto fail; } } else { AvahiAddress a; assert(config.command == COMMAND_RESOLVE_ADDRESS); if (!avahi_address_parse(argv[i], AVAHI_PROTO_UNSPEC, &a)) { fprintf(stderr, _("Failed to parse address '%s'\n"), argv[i]); goto fail; } if (!(avahi_address_resolver_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, &a, 0, address_resolver_callback, NULL))) { fprintf(stderr, _("Failed to create address resolver: %s\n"), avahi_strerror(avahi_client_errno(client))); goto fail; } } n_resolving++; } avahi_simple_poll_loop(simple_poll); ret = 0; break; } } fail: if (client) avahi_client_free(client); sigint_uninstall(); if (simple_poll) avahi_simple_poll_free(simple_poll); return ret; }
int main(int argc, char *argv[]) { int ret = 1, error; Config config; const char *argv0; if ((argv0 = strrchr(argv[0], '/'))) argv0++; else argv0 = argv[0]; if (parse_command_line(&config, argv0, argc, argv) < 0) goto fail; switch (config.command) { case COMMAND_UNSPEC: ret = 1; fprintf(stderr, "No command specified.\n"); break; case COMMAND_HELP: help(stdout, argv0); ret = 0; break; case COMMAND_VERSION: printf("%s "PACKAGE_VERSION"\n", argv0); ret = 0; break; case COMMAND_PUBLISH_SERVICE: case COMMAND_PUBLISH_ADDRESS: if (!(simple_poll = avahi_simple_poll_new())) { fprintf(stderr, "Failed to create simple poll object.\n"); goto fail; } if (sigint_install(simple_poll) < 0) goto fail; if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), config.no_fail ? AVAHI_CLIENT_NO_FAIL : 0, client_callback, &config, &error))) { fprintf(stderr, "Failed to create client object: %s\n", avahi_strerror(error)); goto fail; } if (avahi_client_get_state(client) != AVAHI_CLIENT_CONNECTING && config.verbose) { const char *version, *hn; if (!(version = avahi_client_get_version_string(client))) { fprintf(stderr, "Failed to query version string: %s\n", avahi_strerror(avahi_client_errno(client))); goto fail; } if (!(hn = avahi_client_get_host_name_fqdn(client))) { fprintf(stderr, "Failed to query host name: %s\n", avahi_strerror(avahi_client_errno(client))); goto fail; } fprintf(stderr, "Server version: %s; Host name: %s\n", version, hn); } avahi_simple_poll_loop(simple_poll); ret = 0; break; } fail: if (client) avahi_client_free(client); sigint_uninstall(); if (simple_poll) avahi_simple_poll_free(simple_poll); avahi_free(config.host); avahi_free(config.name); avahi_free(config.stype); avahi_free(config.domain); avahi_string_list_free(config.subtypes); avahi_string_list_free(config.txt); return ret; }
int main(int argc, char *argv[]) { int ret = 1, error; Config config; const char *argv0; char *ec; //avahi_init_i18n(); setlocale(LC_ALL, ""); if ((argv0 = strrchr(argv[0], '/'))) argv0++; else argv0 = argv[0]; if ((ec = getenv("COLUMNS"))) n_columns = atoi(ec); if (n_columns < 40) n_columns = 40; /*if (parse_command_line(&config, argv0, argc, argv) < 0) goto fail;*/ config.command = COMMAND_BROWSE_SERVICES; config.terminate_on_all_for_now = 1; config.verbose = 0; config.terminate_on_cache_exhausted = 0; config.ignore_local = 0; config.resolve = 1; config.no_fail = 1; config.parsable = 0; config.domain = NULL; config.stype = avahi_strdup("_rtp._tcp"); switch (config.command) { case COMMAND_HELP: help(stdout, argv0); ret = 0; break; case COMMAND_VERSION: printf("%s "PACKAGE_VERSION"\n", argv0); ret = 0; break; case COMMAND_BROWSE_SERVICES: case COMMAND_BROWSE_ALL_SERVICES: case COMMAND_BROWSE_DOMAINS: if (!(simple_poll = avahi_simple_poll_new())) { fprintf(stderr, ("Failed to create simple poll object.\n")); goto fail; } /* if (sigint_install(simple_poll) < 0) goto fail;*/ if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), config.no_fail ? AVAHI_CLIENT_NO_FAIL : 0, client_callback, &config, &error))) { fprintf(stderr, ("Failed to create client object: %s\n"), avahi_strerror(error)); goto fail; } avahi_simple_poll_loop(simple_poll); ret = 0; break; #if defined(HAVE_GDBM) || defined(HAVE_DBM) case COMMAND_DUMP_STDB: { char *t; stdb_setent(); while ((t = stdb_getent())) { if (config.no_db_lookup) printf("%s\n", t); else printf("%s\n", stdb_lookup(t)); } ret = 0; break; } #endif } fail: while (services) remove_service(&config, services); if (client) avahi_client_free(client); //sigint_uninstall(); if (simple_poll) avahi_simple_poll_free(simple_poll); avahi_free(config.domain); avahi_free(config.stype); avahi_string_list_free(browsed_types); #if defined(HAVE_GDBM) || defined(HAVE_DBM) stdb_shutdown(); #endif return ret; }
void* registerService(void* poll) { /* Run the main loop */ avahi_simple_poll_loop((AvahiSimplePoll *)poll); }
int main(int argc, char*argv[]) { int ret = 1; argp_program_version = "1.0"; static char doc[] = "Commotion Service Manager"; static struct argp_option options[] = { {"bind", 'b', "URI", 0, "commotiond management socket"}, {"nodaemon", 'n', 0, 0, "Do not fork into the background" }, {"out", 'o', "FILE", 0, "Output file to write services to when USR1 signal is received" }, {"pid", 'p', "FILE", 0, "Specify PID file"}, #ifdef USE_UCI {"uci", 'u', 0, 0, "Store service cache in UCI" }, #endif { 0 } }; /* Set defaults */ arguments.co_sock = DEFAULT_CO_SOCK; #ifdef USE_UCI arguments.uci = 0; #endif arguments.nodaemon = 0; arguments.output_file = DEFAULT_FILENAME; arguments.pid_file = PIDFILE; static struct argp argp = { options, parse_opt, NULL, doc }; argp_parse (&argp, argc, argv, 0, 0, &arguments); //fprintf(stdout,"uci: %d, out: %s\n",arguments.uci,arguments.output_file); if (!arguments.nodaemon) daemon_start(arguments.pid_file); CHECK(co_init(),"Failed to initialize Commotion client"); struct sigaction sa = {0}; sa.sa_handler = print_services; CHECK(sigaction(SIGUSR1,&sa,NULL) == 0, "Failed to set signal handler"); sa.sa_handler = shutdown; CHECK(sigaction(SIGINT,&sa,NULL) == 0, "Failed to set signal handler"); CHECK(sigaction(SIGTERM,&sa,NULL) == 0, "Failed to set signal handler"); /* Initialize the psuedo-RNG */ srand(time(NULL)); /* Allocate main loop object */ CHECK((simple_poll = avahi_simple_poll_new()),"Failed to create simple poll object."); /* Do not publish any local records */ avahi_server_config_init(&config); config.publish_hinfo = 0; config.publish_addresses = 0; config.publish_workstation = 0; config.publish_domain = 0; /* Set a unicast DNS server for wide area DNS-SD */ avahi_address_parse("192.168.50.1", AVAHI_PROTO_UNSPEC, &config.wide_area_servers[0]); config.n_wide_area_servers = 1; config.enable_wide_area = 1; // Start timer to create server struct timeval tv = {0}; avahi_elapse_time(&tv, 0, 0); avahi_simple_poll_get(simple_poll)->timeout_new(avahi_simple_poll_get(simple_poll), &tv, start_server, NULL); // create expiration event for service /* Run the main loop */ avahi_simple_poll_loop(simple_poll); ret = 0; error: /* Free the configuration data */ avahi_server_config_free(&config); co_shutdown(); /* Cleanup things */ if (stb) avahi_s_service_type_browser_free(stb); if (server) avahi_server_free(server); if (simple_poll) avahi_simple_poll_free(simple_poll); return ret; }
void ofxAvahiClientBrowser::threadedFunction(){ avahi_simple_poll_loop(poll); }
static void child_process(apr_pool_t *p, server_rec *server, struct global_config_data *d) { struct runtime_data r; int error; const AvahiPoll *api; AvahiWatch *w; ap_assert(d); unixd_setup_child(); if (pipe(sigterm_pipe_fds) < 0) { ap_log_error(APLOG_MARK, APLOG_ERR, 0, r.main_server, "pipe() failed: %s", strerror(errno)); goto quit; } set_nonblock(sigterm_pipe_fds[0]); set_nonblock(sigterm_pipe_fds[1]); apr_signal(SIGTERM, sigterm); apr_signal(SIGHUP, sigterm); apr_signal(AP_SIG_GRACEFUL, SIG_IGN); r.main_server = server; r.global_config_data = d; r.client = NULL; r.simple_poll = NULL; r.services = NULL; apr_pool_create(&r.pool, p); /* ap_log_error(APLOG_MARK, APLOG_ERR, 0, r.main_server, "Child process startup pid=%lu", (unsigned long) getpid()); */ assemble_services(&r); if (!r.services) { ap_log_error(APLOG_MARK, APLOG_WARNING, 0, r.main_server, __FILE__": No services found to register"); goto quit; } if (!(r.simple_poll = avahi_simple_poll_new())) { ap_log_error(APLOG_MARK, APLOG_ERR, 0, r.main_server, "avahi_simple_poll_new() failed: %s", strerror(errno)); goto quit; } api = avahi_simple_poll_get(r.simple_poll); w = api->watch_new(api, sigterm_pipe_fds[0], AVAHI_WATCH_IN, watch_callback, &r); ap_assert(w); if (!(r.client = avahi_client_new(avahi_simple_poll_get(r.simple_poll), AVAHI_CLIENT_NO_FAIL, client_callback, &r, &error))) { ap_log_error(APLOG_MARK, APLOG_ERR, 0, r.main_server, "avahi_client_new() failed: %s", avahi_strerror(error)); goto quit; } /* ap_log_error(APLOG_MARK, APLOG_ERR, 0, r.main_server, "Child process running"); */ avahi_simple_poll_loop(r.simple_poll); quit: if (r.client) avahi_client_free(r.client); if (r.simple_poll) avahi_simple_poll_free(r.simple_poll); if (r.pool) apr_pool_destroy(r.pool); if (sigterm_pipe_fds[0] >= 0) close(sigterm_pipe_fds[0]); if (sigterm_pipe_fds[1] >= 0) close(sigterm_pipe_fds[1]); sigterm_pipe_fds[0] = sigterm_pipe_fds[1] = -1; /* ap_log_error(APLOG_MARK, APLOG_ERR, 0, r.main_server, "Child process ending"); */ }