Пример #1
0
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) {

    AvahiSimplePoll *simple_poll;
    const AvahiPoll *poll_api;
    AvahiClient *client;
    AvahiRecordBrowser *r;
    
    simple_poll = avahi_simple_poll_new();
    assert(simple_poll);
    
    poll_api = avahi_simple_poll_get(simple_poll);
    assert(poll_api);
    
    client = avahi_client_new(poll_api, 0, NULL, NULL, NULL);
    assert(client);

    r = avahi_record_browser_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "ecstasy.local", AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_HINFO, 0, callback, simple_poll);
    assert(r);

    avahi_simple_poll_loop(simple_poll);

    avahi_client_free(client);
    avahi_simple_poll_free(simple_poll);

    return 0;
}
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;
}
Пример #3
0
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) {

    AvahiSimplePoll *simple_poll;
    const AvahiPoll *poll_api;
    AvahiClient *client;
    AvahiServiceResolver *r;

    simple_poll = avahi_simple_poll_new();
    assert(simple_poll);

    poll_api = avahi_simple_poll_get(simple_poll);
    assert(poll_api);

    client = avahi_client_new(poll_api, 0, NULL, NULL, NULL);
    assert(client);

    r = avahi_service_resolver_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, NULL, "_domain._udp", "0pointer.de", AVAHI_PROTO_UNSPEC, AVAHI_LOOKUP_NO_TXT, callback, simple_poll);
    assert(r);

    avahi_simple_poll_loop(simple_poll);

    avahi_client_free(client);
    avahi_simple_poll_free(simple_poll);

    return 0;
}
void ofxAvahiClientBrowser::close(){
    if (client) {
        avahi_client_free(client);
    }

    if (poll) {
        avahi_simple_poll_free(poll);
    }
}
Пример #5
0
BrowseAvahi::~BrowseAvahi()
{
	if (sb_)
        avahi_service_browser_free(sb_);

    if (client_)
        avahi_client_free(client_);

    if (simple_poll)
        avahi_simple_poll_free(simple_poll);
}
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;
}
Пример #7
0
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;
}
Пример #8
0
static int register_once() {
    AvahiClient *client = NULL;
    int error;
    int ret = 1;
    struct timeval tv;

    group = NULL;
    simple_poll = NULL;
    name = 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");

    /* 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;
    }

    /* After 10s quit. */
    avahi_simple_poll_get(simple_poll)->timeout_new(
        avahi_simple_poll_get(simple_poll),
        avahi_elapse_time(&tv, 300*10, 0),
        quit_callback,
        client);

    /* Run the main loop */
    avahi_simple_poll_loop(simple_poll);

    ret = 0;

fail:

    /* Cleanup things */

    if (client)
        avahi_client_free(client);

    if (simple_poll)
        avahi_simple_poll_free(simple_poll);

    avahi_free(name);

    return ret;
}
Пример #9
0
void stopRegisterService(const char* nameService){

	pthread_cancel(myNewThread);
    /* Cleanup things */

    if (client)
        avahi_client_free(client);

    if (simple_poll)
        avahi_simple_poll_free(simple_poll);

    avahi_free((void*)nameService);
}
Пример #10
0
PublishAvahi::~PublishAvahi()
{
	active_ = false;
	pollThread_.join();

	if (client_)
		avahi_client_free(client_);

	if (simple_poll)
		avahi_simple_poll_free(simple_poll);

	avahi_free(name);
}
Пример #11
0
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) {
    AvahiClient *client = NULL;
    int error;
    int ret = 1;
    //struct timeval tv;		// Not needed due no modifications made by avahi service
								// Comes from sample code
	
    /* 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("gbMon2");
	
    /* 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;
    }
	
    /* After 10s do some weird modification to the service */
    /*
	avahi_simple_poll_get(simple_poll)->timeout_new(
													avahi_simple_poll_get(simple_poll),
													avahi_elapse_time(&tv, 1000*10, 0),
													modify_callback,
													client);
	*/
    /* Run the main loop */
    avahi_simple_poll_loop(simple_poll);
	
    ret = 0;
	
fail:
	
    /* Cleanup things */
	
    if (client)
        avahi_client_free(client);
	
    if (simple_poll)
        avahi_simple_poll_free(simple_poll);
	
    avahi_free(name);
	
    return ret;
}
Пример #12
0
void BrowseAvahi::cleanUp()
{
	if (sb_)
		avahi_service_browser_free(sb_);
	sb_ = NULL;

	if (client_)
		avahi_client_free(client_);
	client_ = NULL;

	if (simple_poll)
		avahi_simple_poll_free(simple_poll);
	simple_poll = NULL;
}
Пример #13
0
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;
}
Пример #14
0
Avahi::~Avahi() {
	if (discoveryClient) {
		avahi_client_free(discoveryClient);
	}

	if (publishClient) {
		avahi_client_free(publishClient);
	}

	if (simple_discovery_poll) {
		avahi_simple_poll_free(simple_discovery_poll);
	}

	if (sb) {
		avahi_service_browser_free(sb);
	}
	if (simple_publish_poll) {
		avahi_simple_poll_free(simple_publish_poll);
	}
	distrd->join();
	pubtrd->join();

	// TODO Auto-generated destructor stub
}
Пример #15
0
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 */
}
void ofxAvahiCoreBrowser::close(){
	ofLogVerbose(LOG_NAME) << "closing";

    if (poll){
        avahi_simple_poll_quit(poll);
        waitForThread(false);
    }

	if (sb)
		avahi_s_service_browser_free(sb);

	if (server)
		avahi_server_free(server);

    if (poll)
        avahi_simple_poll_free(poll);
}
	void CommunicationManager::run() {
		AvahiClient *client = NULL;
		int error;
		struct timeval tv;

		/* Allocate main loop object */
		if (!(simplePoll = avahi_simple_poll_new())) {
			LoggerUtil_error(logger, "Failed to create simple poll object.\n");
		} else {
			name = avahi_strdup("Ginga Multimodal Event");

			/* Allocate a new client */
			client = avahi_client_new(avahi_simple_poll_get(simplePoll),
					(AvahiClientFlags)0, clientCallback, NULL, &error);

			/* Check whether creating the client object succeeded */
			if (!client) {
				LoggerUtil_error(logger, "Failed to create client: " <<
						avahi_strerror(error));
			} else {
				// Entra num loop para ficar aceitando conexões e recebendo
				// um xml em cada conexão estabelecida.
				startSocket();
				/* Run the main loop */
//				avahi_simple_poll_loop(simplePoll);
			}
		}

		/* Cleanup things */
		if (client) {
			avahi_client_free(client);
		}

		if (simplePoll) {
			avahi_simple_poll_free(simplePoll);
		}

		avahi_free(name);
	}
Пример #18
0
/** 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.");
}
Пример #19
0
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;
}
Пример #20
0
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"); */
}
Пример #21
0
wxAvahiSimplePool::~wxAvahiSimplePool(){
    if(m_pool!=NULL)
        avahi_simple_poll_free(m_pool);
}
Пример #22
0
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;
}
Пример #23
0
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;
}
Пример #24
0
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;
}
Пример #25
0
int old_main(int argc, char *argv[]) {
    struct zc_published_service service = {
            .client = NULL,
            .entry_group = NULL,
            .config = NULL
    };
    char *zcp_argv[] = {"avahi-publish", "-s", "myservice", "_rtp._tcp",  "12345", "Here it is"};
    zc_publish_main(&service, 6, zcp_argv);

    struct zc_queue *queue = create_queue_pointer();
    char *zcb_argv[] = {"avahi-browse", "-a", "-p", "-r", "-t", "-v"};
    zc_browse_main(queue, 6, zcb_argv);

    struct zc_queue_elem *cursor = NULL;
    struct zc_element *zc_elem = NULL;
    char *t= NULL;
    cursor = queue_pop_front(queue);
    while (cursor != NULL){
        zc_elem = (struct zc_element *)cursor;
        t = avahi_string_list_to_string(zc_elem->txt_options);
        fprintf(stdout, "Elem : %s:%d %s %s %s %s \n", zc_elem->address, zc_elem->port, zc_elem->ifname, zc_elem->hostname, zc_elem->type, t);
        cursor = queue_pop_front(queue);
    }

    if (!service.entry_group) {
        if (!(service.entry_group = avahi_entry_group_new(*(service.config->client), zcp_entry_group_callback, service.config))) {
            fprintf(stdout, ("Failed to create entry group: %s\n"), avahi_strerror(avahi_client_errno(*(service.config->client))));
            return -1;
        }else{
            fprintf(stdout, ("OK to create entry group: \n"));
        }
    }

    //assert(avahi_entry_group_is_empty(service.entry_group));

    fprintf(stderr, ("Name %s: \n"), service.config->name);
//
    service.config->txt = avahi_string_list_add(NULL, "plop=ok");
    avahi_entry_group_update_service_txt_strlst(service.entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, service.config->name, service.config->stype, service.config->domain, service.config->txt);

    //sleep(1);
    char *zcb_argv2[] = {"avahi-browse", "-a", "-p", "-r", "-t", "-v"};
    zc_browse_main(queue, 6, zcb_argv2);

    cursor = queue_pop_front(queue);
    while (cursor != NULL){
        zc_elem = (struct zc_element *)cursor;
        t = avahi_string_list_to_string(zc_elem->txt_options);
        fprintf(stdout, "Elem : %s:%d %s %s %s \n", zc_elem->address, zc_elem->port, zc_elem->hostname, zc_elem->type, t);
        cursor = queue_pop_front(queue);
    }

//    sleep(2);
//    char *zcp_argv3[] = {"avahi-publish", "-s", "myservice", "_http._tcp",  "12345", "Here it is 2 le retour"};
//    zc_publish_main(&client, &simple_poll, &service2, 6, zcp_argv3);
//
//    char *zcb_argv2[] = {"avahi-browse", "-a", "-p", "-r", "-t", "-v"};
//    sleep(5);
//    zc_browse_main(queue, 6, zcb_argv2);

    queue_delete_queue_pointer(queue);

    if (service.client){
        fprintf(stderr, "Clean up client \n");
        avahi_client_free(service.client);
    }
    if(service.config->simple_poll){
        fprintf(stderr, "Clean up poll \n");
        avahi_simple_poll_free(service.config->simple_poll);
    }
    if(service.config){
        fprintf(stderr, "Clean up config \n");
        free(service.config);
    }
    return 0;
}
Пример #26
0
/* The main function of the background daemon */
static int daemon_proc(const char *host_file, const char *lock_file, int n_slots) {
    int ret = 1;
    int lock_fd = -1;
    struct daemon_data d;
    time_t clip_time;
    int error;
    char machine[64], version[64], stype[128];

    rs_add_logger(rs_logger_syslog, RS_LOG_DEBUG, NULL, 0);

    /* Prepare daemon data structure */
    d.fd = -1;
    d.hosts = NULL;
    d.n_slots = n_slots;
    d.simple_poll = NULL;
    d.browser = NULL;
    d.client = NULL;
    clip_time = time(NULL);

    rs_log_info("Zeroconf daemon running.\n");

    /* Open daemon lock file and lock it */
    if ((lock_fd = open(lock_file, O_RDWR|O_CREAT, 0666)) < 0) {
        rs_log_crit("open('%s') failed: %s\n", lock_file, strerror(errno));
        goto finish;
    }

    if (generic_lock(lock_fd, 1, 1, 0) < 0) {
        /* lock failed, there's probably already another daemon running */
        goto finish;
    }

    /* Open host file */
    if ((d.fd = open(host_file, O_RDWR|O_CREAT, 0666)) < 0) {
        rs_log_crit("open('%s') failed: %s\n", host_file, strerror(errno));
        goto finish;
    }

    /* Clear host file */
    write_hosts(&d);

    if (!(d.simple_poll = avahi_simple_poll_new())) {
        rs_log_crit("Failed to create simple poll object.\n");
        goto finish;
    }

    if (!(d.client = avahi_client_new(
                  avahi_simple_poll_get(d.simple_poll),
                  0,
                  client_callback,
                  &d,
                  &error))) {
        rs_log_crit("Failed to create Avahi client object: %s\n", avahi_strerror(error));
        goto finish;
    }

    if (dcc_get_gcc_version(version, sizeof(version)) &&
        dcc_get_gcc_machine(machine, sizeof(machine))) {

        dcc_make_dnssd_subtype(stype, sizeof(stype), version, machine);
    } else {
        rs_log_warning("Warning, failed to get CC version and machine type.\n");

        strncpy(stype, DCC_DNS_SERVICE_TYPE, sizeof(stype));
        stype[sizeof(stype)-1] = 0;
    }

    rs_log_info("Browsing for '%s'.\n", stype);

    if (!(d.browser = avahi_service_browser_new(
                  d.client,
                  AVAHI_IF_UNSPEC,
                  AVAHI_PROTO_UNSPEC,
                  stype,
                  NULL,
                  0,
                  browse_reply,
                  &d))) {
        rs_log_crit("Failed to create service browser object: %s\n", avahi_strerror(avahi_client_errno(d.client)));
        goto finish;
    }

    /* Check whether the host file has been used recently */
    while (fd_last_used(d.fd, clip_time) <= MAX_IDLE_TIME) {

        /* Iterate the main loop for 5s */
        if (avahi_simple_poll_iterate(d.simple_poll, 5000) != 0) {
            rs_log_crit("Event loop exited abnormaly.\n");
            goto finish;
        }
    }

    /* Wer are idle */
    rs_log_info("Zeroconf daemon unused.\n");

    ret = 0;

finish:

    /* Cleanup */
    if (lock_fd >= 0) {
        generic_lock(lock_fd, 1, 0, 0);
        close(lock_fd);
    }

    if (d.fd >= 0)
        close(d.fd);

    while (d.hosts) {
        struct host *h = d.hosts;
        d.hosts = d.hosts->next;
        free_host(h);
    }

    if (d.client)
        avahi_client_free(d.client);

    if (d.simple_poll)
        avahi_simple_poll_free(d.simple_poll);

    rs_log_info("zeroconf daemon ended.\n");

    return ret;
}
static void
init_local (void)
{
	int i;
	MateVFSResult res;
	
	if (!started_local) {
		AvahiSimplePoll *simple_poll;
		const AvahiPoll *poll;
		AvahiClient *client = NULL;
		AvahiServiceBrowser **sb;
		struct timeval tv;
		int error;
		
		started_local = TRUE;
		
		for (i = 0; i < G_N_ELEMENTS (dns_sd_types); i++) {
			MateVFSDNSSDBrowseHandle *handle;
			res = mate_vfs_dns_sd_browse (&handle,
						       "local",
						       dns_sd_types[i].type,
						       local_browse_callback,
						       NULL, NULL);
			if (res == MATE_VFS_OK) {
				dns_sd_types[i].handle = handle;
			}
		}

		simple_poll = avahi_simple_poll_new ();
		if (simple_poll == NULL) {
			g_warning ("Failed to create simple poll object");
			return;
		}

		poll = avahi_simple_poll_get (simple_poll);
		client = avahi_client_new (poll, 0, 
					   avahi_client_callback, simple_poll, &error);
		
		/* Check wether creating the client object succeeded */
		if (client == NULL) {
			g_warning ("Failed to create client: %s\n", avahi_strerror (error));
			avahi_simple_poll_free (simple_poll);
			return;
		}

		sb = g_new0 (AvahiServiceBrowser *, G_N_ELEMENTS (dns_sd_types));

		for (i = 0; i < G_N_ELEMENTS (dns_sd_types); i++) {
			sb[i] = avahi_service_browser_new (client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 
							   dns_sd_types[i].type, "local",
							   AVAHI_LOOKUP_USE_MULTICAST,
							   local_browse_callback_sync, simple_poll);
		}
		

		avahi_elapse_time (&tv, LOCAL_SYNC_BROWSE_DELAY_MSEC, 0);
		poll->timeout_new (poll, &tv, stop_poll_timeout,
				   (void *)simple_poll);

		/* Run the main loop util reply or timeout */
		for (;;)
			if (avahi_simple_poll_iterate (simple_poll, -1) != 0)
				break;

		for (i = 0; i < G_N_ELEMENTS (dns_sd_types); i++) {
			if (sb[i] != NULL) {
				avahi_service_browser_free (sb[i]);
			}
		}
		avahi_client_free (client);
		avahi_simple_poll_free (simple_poll);

					  
	}
Пример #28
0
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;
}