ControllerDiscoveryMDNS::ControllerDiscoveryMDNS( TPContext * context, const String & n, int _port , int _http_port ) : poll( NULL ), server( NULL ), group( NULL ), name( n ), ready( false ), port( _port ), http_port( _http_port ) { avahi_set_allocator( avahi_glib_allocator() ); poll = avahi_glib_poll_new( NULL, TRICKPLAY_PRIORITY ); g_assert( poll ); AvahiServerConfig config; avahi_server_config_init( &config ); config.publish_workstation = 0; int error; server = avahi_server_new( avahi_glib_poll_get( poll ), &config, avahi_server_callback, this, &error ); if ( !server ) { g_warning( "FAILED TO CREATE AVAHI SERVER : %s", avahi_strerror( error ) ); } }
Browser::Browser(QObject *parent) : QAbstractListModel(parent) { poll_.reset(avahi_glib_poll_new(nullptr, G_PRIORITY_DEFAULT)); if (!poll_) { throw std::runtime_error("Could not create AvahiGLibPoll"); } AvahiServerConfig config_backing { nullptr, }; std::unique_ptr<AvahiServerConfig, decltype(&avahi_server_config_free)> config(avahi_server_config_init(&config_backing), avahi_server_config_free); config->publish_hinfo = false; config->publish_addresses = false; config->publish_workstation = false; config->publish_domain = false; config->disable_publishing = true; int error = 0; server_.reset(avahi_server_new( avahi_glib_poll_get(poll_.get()), config.get(), &Browser::serverCallback, this, &error)); if (!server_) { throw std::runtime_error(std::string("Could not create AvahiServer: ") + avahi_strerror(error)); } roles[RoleServiceName] = "serviceName"; roles[RoleHostName] = "hostName"; roles[RoleAddress] = "address"; roles[RolePort] = "port"; roles[RoleTxt] = "txt"; }
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) { AvahiServer *server; AvahiServerConfig config; int error; AvahiSimplePoll *simple_poll; simple_poll = avahi_simple_poll_new(); avahi_server_config_init(&config); config.publish_hinfo = 0; config.publish_addresses = 0; config.publish_workstation = 0; config.publish_domain = 0; config.use_ipv6 = 0; config.enable_reflector = 1; server = avahi_server_new(avahi_simple_poll_get(simple_poll), &config, NULL, NULL, &error); avahi_server_config_free(&config); for (;;) if (avahi_simple_poll_iterate(simple_poll, -1) != 0) break; avahi_server_free(server); avahi_simple_poll_free(simple_poll); return 0; }
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) { AvahiServerConfig config; AvahiServer *server = NULL; int error; int ret = 1; /* Initialize the pseudo-RNG */ srand(time(NULL)); /* Allocate main loop object */ if (!(simple_poll = avahi_simple_poll_new())) { fprintf(stderr, "Failed to create simple poll object.\n"); goto fail; } name = avahi_strdup("MegaPrinter"); /* Let's set the host name for this server. */ avahi_server_config_init(&config); config.host_name = avahi_strdup("gurkiman"); config.publish_workstation = 0; /* Allocate a new server */ server = avahi_server_new(avahi_simple_poll_get(simple_poll), &config, server_callback, NULL, &error); /* Free the configuration data */ avahi_server_config_free(&config); /* Check wether creating the server object succeeded */ if (!server) { fprintf(stderr, "Failed to create server: %s\n", avahi_strerror(error)); goto fail; } /* Run the main loop */ avahi_simple_poll_loop(simple_poll); ret = 0; fail: /* Cleanup things */ if (server) avahi_server_free(server); if (simple_poll) avahi_simple_poll_free(simple_poll); avahi_free(name); return ret; }
bool ofxAvahiCoreBrowser::lookup(string _type){ AvahiServerConfig config; int error; type = _type; if (!(poll = avahi_simple_poll_new())) { ofLogError(LOG_NAME) << "Failed to create simple poll object."; close(); return false; } /* 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; config.use_ipv6 = 0; config.disallow_other_stacks = 1; config.allow_point_to_point = 1; /* 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;*/ server = avahi_server_new(avahi_simple_poll_get(poll), &config, (AvahiServerCallback)server_cb, this, &error); avahi_server_config_free(&config); if (!server) { ofLogError(LOG_NAME) << "Failed to create server:" << avahi_strerror(error); close(); return false; } /* Create the service browser */ if (!(sb = avahi_s_service_browser_new(server, AVAHI_IF_UNSPEC, AVAHI_PROTO_INET, type.c_str(), NULL, (AvahiLookupFlags)0, (AvahiSServiceBrowserCallback)browse_cb, this))) { ofLogError(LOG_NAME) << "Failed to create service browser:" << avahi_strerror(avahi_server_errno(server)); close(); return false; } startThread(true,false); return true; }
static void start_server(AvahiTimeout *t, void *userdata) { int error; assert(t); if (stb) { DEBUG("Service type browser already exists"); avahi_s_service_type_browser_free(stb); stb = NULL; } if (server) { DEBUG("Server already exists"); avahi_server_free(server); server = NULL; } /* Allocate a new server */ server = avahi_server_new(avahi_simple_poll_get(simple_poll), &config, server_callback, NULL, &error); /* Check whether creating the server object succeeded */ if (!server) { ERROR("Failed to create server: %s", avahi_strerror(error)); avahi_simple_poll_quit(simple_poll); return; } /* every UPDATE_INTERVAL seconds, shut down and re-create server. This * has the benefit of causing CSM to send queries to other nodes, prompting * them to re-multicast their services. This is done because mDNS seems to * be very unreliable on mesh, and often nodes don't get service announcements * or can't resolve them. */ struct timeval tv = {0}; avahi_elapse_time(&tv, 1000*UPDATE_INTERVAL, 0); avahi_simple_poll_get(simple_poll)->timeout_update(t, &tv); }
static int run_server(DaemonConfig *c) { int r = -1; int error; const AvahiPoll *poll_api = NULL; AvahiWatch *sig_watch = NULL; int retval_is_sent = 0; #ifdef HAVE_INOTIFY AvahiWatch *inotify_watch = NULL; #endif #ifdef HAVE_KQUEUE int i; AvahiWatch *kqueue_watch = NULL; #endif assert(c); ignore_signal(SIGPIPE); if (!(nss_support = avahi_nss_support())) avahi_log_warn("WARNING: No NSS support for mDNS detected, consider installing nss-mdns!"); if (!(simple_poll_api = avahi_simple_poll_new())) { avahi_log_error("Failed to create main loop object."); goto finish; } poll_api = avahi_simple_poll_get(simple_poll_api); if (daemon_signal_init(SIGINT, SIGHUP, SIGTERM, SIGUSR1, 0) < 0) { avahi_log_error("Could not register signal handlers (%s).", strerror(errno)); goto finish; } if (!(sig_watch = poll_api->watch_new(poll_api, daemon_signal_fd(), AVAHI_WATCH_IN, signal_callback, simple_poll_api))) { avahi_log_error( "Failed to create signal watcher"); goto finish; } if (simple_protocol_setup(poll_api) < 0) goto finish; #ifdef HAVE_DBUS if (c->enable_dbus) { if (dbus_protocol_setup(poll_api, config.disable_user_service_publishing, config.n_clients_max, config.n_objects_per_client_max, config.n_entries_per_entry_group_max, !c->fail_on_missing_dbus #ifdef ENABLE_CHROOT && !config.use_chroot #endif ) < 0) { avahi_log_warn("WARNING: Failed to contact D-Bus daemon."); if (c->fail_on_missing_dbus) goto finish; } } #endif #ifdef ENABLE_CHROOT if (config.drop_root && config.use_chroot) { if (chroot(AVAHI_CONFIG_DIR) < 0) { avahi_log_error("Failed to chroot(): %s", strerror(errno)); goto finish; } avahi_log_info("Successfully called chroot()."); chdir("/"); if (avahi_caps_drop_all() < 0) { avahi_log_error("Failed to drop capabilities."); goto finish; } avahi_log_info("Successfully dropped remaining capabilities."); } #endif #ifdef HAVE_INOTIFY if ((inotify_fd = inotify_init()) < 0) avahi_log_warn( "Failed to initialize inotify: %s", strerror(errno)); else { add_inotify_watches(); if (!(inotify_watch = poll_api->watch_new(poll_api, inotify_fd, AVAHI_WATCH_IN, inotify_callback, NULL))) { avahi_log_error( "Failed to create inotify watcher"); goto finish; } } #endif #ifdef HAVE_KQUEUE if ((kq = kqueue()) < 0) avahi_log_warn( "Failed to initialize kqueue: %s", strerror(errno)); else { add_kqueue_watches(); if (!(kqueue_watch = poll_api->watch_new(poll_api, kq, AVAHI_WATCH_IN, kqueue_callback, NULL))) { avahi_log_error( "Failed to create kqueue watcher"); goto finish; } } #endif load_resolv_conf(); #ifdef ENABLE_CHROOT static_service_load(config.use_chroot); static_hosts_load(config.use_chroot); #else static_service_load(0); static_hosts_load(0); #endif if (!(avahi_server = avahi_server_new(poll_api, &c->server_config, server_callback, c, &error))) { avahi_log_error("Failed to create server: %s", avahi_strerror(error)); goto finish; } update_wide_area_servers(); update_browse_domains(); if (c->daemonize) { daemon_retval_send(0); retval_is_sent = 1; } for (;;) { if ((r = avahi_simple_poll_iterate(simple_poll_api, -1)) < 0) { /* We handle signals through an FD, so let's continue */ if (errno == EINTR) continue; avahi_log_error("poll(): %s", strerror(errno)); goto finish; } else if (r > 0) /* Quit */ break; } r = 0; finish: static_service_remove_from_server(); static_service_free_all(); static_hosts_remove_from_server(); static_hosts_free_all(); remove_dns_server_entry_groups(); simple_protocol_shutdown(); #ifdef HAVE_DBUS if (c->enable_dbus) dbus_protocol_shutdown(); #endif if (avahi_server) { avahi_server_free(avahi_server); avahi_server = NULL; } daemon_signal_done(); if (sig_watch) poll_api->watch_free(sig_watch); #ifdef HAVE_INOTIFY if (inotify_watch) poll_api->watch_free(inotify_watch); if (inotify_fd >= 0) close(inotify_fd); #endif #ifdef HAVE_KQUEUE if (kqueue_watch) poll_api->watch_free(kqueue_watch); if (kq >= 0) close(kq); for (i = 0; i < num_kfds; i++) { if (kfds[i] >= 0) close(kfds[i]); } #endif if (simple_poll_api) { avahi_simple_poll_free(simple_poll_api); simple_poll_api = NULL; } if (!retval_is_sent && c->daemonize) daemon_retval_send(1); return r; }