Exemplo n.º 1
0
void Publisher::entryGroupCallback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *context) {
	Publisher *publisher = (Publisher*) context;
	const char* name = publisher->info.getServiceName().c_str();
	const int port = publisher->info.getPort();

	// callback called whenever our entry group state changes
	switch (state) {
	case AVAHI_ENTRY_GROUP_ESTABLISHED:
		std::cout << "Registration ok:" << name <<":" << port << " (" << publisher->serviceType << ")" << std::endl;
		break;
	case AVAHI_ENTRY_GROUP_COLLISION:
		// build new name
		publisher->nextName();
		// retry
		publisher->createServices(avahi_entry_group_get_client(g));
		break;
	case AVAHI_ENTRY_GROUP_FAILURE:
		std::cerr << "Registration failure ("
				<< avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g)))
				<< ")" << std::endl;
		publisher->quit();
		break;
	case AVAHI_ENTRY_GROUP_UNCOMMITED:
	case AVAHI_ENTRY_GROUP_REGISTERING:
		;
	}
}
Exemplo n.º 2
0
static void client_cnames_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) 
{
    struct runtime_data r;

   /* Called whenever the client or server state changes */
   switch (state) 
   {
      case AVAHI_CLIENT_S_RUNNING:
         create_cnames(c);
      break;

      case AVAHI_CLIENT_FAILURE:
         ap_log_error(APLOG_MARK, APLOG_ERR, 0, r.main_server, "Client failure: %s", avahi_strerror(avahi_client_errno(c)));
         avahi_simple_poll_quit(_poll);
      break;

      case AVAHI_CLIENT_S_COLLISION:
      case AVAHI_CLIENT_S_REGISTERING:
         if (_group) avahi_entry_group_reset(_group);
      break;

      case AVAHI_CLIENT_CONNECTING:
         /* do nothing */
      break;
   }
}
static void main_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
    struct userdata *u = userdata;
    pa_assert(u);

    switch (state) {

        case AVAHI_ENTRY_GROUP_ESTABLISHED:
            pa_log_info("Successfully established main service.");
            break;

        case AVAHI_ENTRY_GROUP_COLLISION: {
            char *t;

            t = avahi_alternative_service_name(u->service_name);
            pa_log_info("Name collision: renaming main service %s to %s.", u->service_name, t);
            pa_xfree(u->service_name);
            u->service_name = t;

            publish_main_service(u);
            break;
        }

        case AVAHI_ENTRY_GROUP_FAILURE: {
            pa_log("Failed to register main service: %s", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));

            avahi_entry_group_free(g);
            u->main_entry_group = NULL;
            break;
        }

        case AVAHI_ENTRY_GROUP_UNCOMMITED:
        case AVAHI_ENTRY_GROUP_REGISTERING:
            break;
    }
}
Exemplo n.º 4
0
int
mdns_browse(char *type, int flags, mdns_browse_cb cb)
{
  struct mdns_browser *mb;
  __block 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;

  dispatch_sync(mdns_sq,
		^{
		  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;
		    }
		});
Exemplo n.º 5
0
static void
browse_callback(
    AvahiServiceBrowser    *browser,	/* I - Browser */
    AvahiIfIndex           interface,	/* I - Interface index (unused) */
    AvahiProtocol          protocol,	/* I - Network protocol (unused) */
    AvahiBrowserEvent      event,	/* I - What happened */
    const char             *name,	/* I - Service name */
    const char             *type,	/* I - Registration type */
    const char             *domain,	/* I - Domain */
    AvahiLookupResultFlags flags,	/* I - Flags */
    void                   *context)	/* I - Devices array */
{
  AvahiClient *client = avahi_service_browser_get_client(browser);
					/* Client information */


  (void)interface;
  (void)protocol;
  (void)context;

  switch (event)
  {
    case AVAHI_BROWSER_FAILURE:
	fprintf(stderr, "DEBUG: browse_callback: %s\n",
		avahi_strerror(avahi_client_errno(client)));
	avahi_simple_poll_quit(simple_poll);
	break;

    case AVAHI_BROWSER_NEW:
       /*
	* This object is new on the network.
	*/

	if (flags & AVAHI_LOOKUP_RESULT_LOCAL)
	{
	 /*
	  * This comes from the local machine so ignore it.
	  */

	  fprintf(stderr, "DEBUG: Ignoring local service %s.\n", name);
	}
	else
	{
	 /*
	  * Create a device entry for it if it doesn't yet exist.
	  */

	  get_device((cups_array_t *)context, name, type, domain);
	}
	break;

    case AVAHI_BROWSER_REMOVE:
    case AVAHI_BROWSER_CACHE_EXHAUSTED:
        break;

    case AVAHI_BROWSER_ALL_FOR_NOW:
	browsers--;
	break;
  }
}
Exemplo n.º 6
0
Arquivo: shell.c Projeto: GNOME/libepc
static AvahiClient*
epc_shell_get_avahi_client (GError **error)
{
  g_return_val_if_fail (NULL != epc_shell_avahi_client ||
                        NULL != error, NULL);

  if (G_UNLIKELY (NULL == epc_shell_avahi_client))
    {
      gint error_code = AVAHI_OK;

      epc_shell_init ();

      epc_shell_avahi_client =
        avahi_client_new (avahi_glib_poll_get (epc_shell_avahi_poll),
                          AVAHI_CLIENT_NO_FAIL, epc_shell_avahi_client_cb,
                          NULL, &error_code);

      if (NULL == epc_shell_avahi_client)
        g_set_error (error, EPC_AVAHI_ERROR, error_code,
                     _("Cannot create Avahi client: %s"),
                     avahi_strerror (error_code));
    }

  return epc_shell_avahi_client;
}
Exemplo n.º 7
0
void avahi_register_game(Game * game)
{
#ifdef HAVE_AVAHI
	const AvahiPoll *poll_api;
	int error;
	glib_poll = avahi_glib_poll_new(NULL, G_PRIORITY_DEFAULT);
	poll_api = avahi_glib_poll_get(glib_poll);
	/* Allocate main loop object */
	if (!poll_api) {
		log_message(MSG_ERROR,
			    _("Avahi error: %s, %s\n"),
			    _("Unable to register Avahi server"),
			    "Failed to create glib poll object");
		avahi_unregister_game();
		return;
	}

	name = avahi_strdup(game->params->title);
	/* Allocate a new client */
	client =
	    avahi_client_new(poll_api, 0, client_callback, game, &error);
	/* Check whether creating the client object succeeded */
	if (!client) {
		log_message(MSG_ERROR,
			    _("Avahi error: %s, %s\n"),
			    _("Unable to register Avahi server"),
			    avahi_strerror(error));
		avahi_unregister_game();
	}
#endif				// HAVE_AVAHI
}
Exemplo n.º 8
0
static void client_callback(AvahiClient * c,
			    AvahiClientState state, AVAHI_GCC_UNUSED void
			    *userdata)
{
	Game *game = (Game *) userdata;
	g_assert(c != NULL);
	/* Called whenever the client or server state changes */
	switch (state) {
	case AVAHI_CLIENT_S_RUNNING:
		/* The server has startup successfully and registered its host
		 * name on the network, so it's time to create our services */
		if (!group)
			create_services(c, game);
		break;
	case AVAHI_CLIENT_S_COLLISION:
		/* Let's drop our registered services. When the server is back
		 * in AVAHI_SERVER_RUNNING state we will register them
		 * again with the new host name. */
		if (group)
			avahi_entry_group_reset(group);
		break;
	case AVAHI_CLIENT_FAILURE:
		log_message(MSG_ERROR,
			    _("Avahi error: %s, %s\n"), "Client failure",
			    avahi_strerror(avahi_client_errno(c)));
		avahi_glib_poll_free(glib_poll);
		break;
	case AVAHI_CLIENT_CONNECTING:
	case AVAHI_CLIENT_S_REGISTERING:
		;
	}
}
Exemplo n.º 9
0
const char *removeAvahiGroup(AvahiThreadedPoll *threaded_poll, AvahiEntryGroup *group) {
  int error = avahi_entry_group_free(group);
  if (AVAHI_OK != error) {
    return avahi_strerror(error);
  }
  return NULL;
}
Exemplo n.º 10
0
int dns_service_publisher_start( dns_service_desc_t *service_desc )
{
	int ret = 0;
	int error;

	if( ! service_desc ) return 1;

	if( ! (threaded_poll = avahi_threaded_poll_new() ) ) {
		logging_printf(LOGGING_ERROR, "Unable to create publisher thread\n");
		return 1;
	}

	sd_name_copy = avahi_strdup( service_desc->name );
	sd_service_copy = avahi_strdup( service_desc->service );
	sd_port = service_desc->port;

	use_ipv4 = service_desc->publish_ipv4;
	use_ipv6 = service_desc->publish_ipv6;

	client = avahi_client_new(avahi_threaded_poll_get(threaded_poll), 0, client_callback, NULL, &error);

	if (! client) {
		logging_printf(LOGGING_ERROR, "Failed to create client: %s\n", avahi_strerror(error));
		return 1;
	}

	avahi_threaded_poll_start( threaded_poll );

	return ret;
}
Exemplo n.º 11
0
AvahiClient *
rb_daap_mdns_avahi_get_client (void)
{
	if (g_once_init_enter (&client_init)) {
		AvahiClientFlags flags = 0;
		AvahiGLibPoll *apoll;
		int error = 0;

		avahi_set_allocator (avahi_glib_allocator ());

		apoll = avahi_glib_poll_new (NULL, G_PRIORITY_DEFAULT);
		if (apoll == NULL) {
			g_warning ("Unable to create AvahiGlibPoll object for mDNS");
		}

		client = avahi_client_new (avahi_glib_poll_get (apoll),
					   flags,
					   (AvahiClientCallback) client_cb,
					   NULL,
					   &error);
		if (error != 0) {
			g_warning ("Unable to initialize mDNS: %s", avahi_strerror (error));
		}

		g_once_init_leave (&client_init, 1);
	}

	return client;
}
Exemplo n.º 12
0
AvahiSession::AvahiSession()
	: mPoll(0),
	  mClient(0),
	  mGroup(0),
	  mEntries(0),
	  mServiceName(0)
{
	int err;

	mServiceName = avahi_strdup(kSCRendezvousServiceName);

	mPoll = avahi_threaded_poll_new();
	if (!mPoll) {
		scprintf("Zeroconf: failed to create poll API\n");
		return;
	}

	mClient = avahi_client_new(
		avahi_threaded_poll_get(mPoll),
		(AvahiClientFlags)0, client_cb, this, &err);
	if (!mClient) {
		scprintf("Zeroconf: failed to create client: %s\n", avahi_strerror(err));
		avahi_threaded_poll_free(mPoll);
		mPoll = 0;
		return;
	}

	avahi_threaded_poll_start(mPoll);
}
Exemplo n.º 13
0
void QxtMDNSPrivate::avahiClientCallback(AvahiClient *s, AvahiClientState state, void *userdata)
{
	QxtMDNSPrivate* self = static_cast<QxtMDNSPrivate*>(userdata);
	self->client = s;
	if (state == AVAHI_CLIENT_S_RUNNING)
	{
		self->recordbrowser = avahi_record_browser_new(
		                          s,
		                          AVAHI_IF_UNSPEC,
		                          AVAHI_PROTO_INET,
		                          self->name.toLocal8Bit().constData(),
		                          AVAHI_DNS_CLASS_IN,
		                          AVAHI_DNS_TYPE_A,
		                          (AvahiLookupFlags)0,
					   QxtMDNSPrivate::avahiRecordBrowserCallback,
		                          self);
		if (self->recordbrowser == NULL)
		{
			self->info.setError(QHostInfo::UnknownError);
			self->info.setErrorString(avahi_strerror(avahi_client_errno(self->client)));
			QMetaObject::invokeMethod(self->receiver, self->member, Q_ARG(QHostInfo, self->info));
			self->sent = true;
			self->qxt_p().cancelLookup();
		}
	}
}
Exemplo n.º 14
0
static void
client_callback (AvahiClient *c,
                 AvahiClientState state,
                 void *userdata)
{
	g_return_if_fail (c);

	switch (state) {
		case AVAHI_CLIENT_S_RUNNING:
			if (!group) {
				create_services (c);
			}
			break;
		case AVAHI_CLIENT_S_COLLISION:
			printf ("Client collision, terminating...");
			g_main_loop_quit (ml);
			break;
		case AVAHI_CLIENT_FAILURE:
			printf ("Client failure: %s\n", avahi_strerror (avahi_client_errno (c)));
			g_main_loop_quit (ml);
			break;
		case AVAHI_CLIENT_CONNECTING:
		case AVAHI_CLIENT_S_REGISTERING:
			;
	}
}
Exemplo n.º 15
0
static void add_static_host_to_server(StaticHost *h)
{

    if (!h->group)
        if (!(h->group = avahi_s_entry_group_new (avahi_server, entry_group_callback, h))) {
            avahi_log_error("avahi_s_entry_group_new() failed: %s", avahi_strerror(avahi_server_errno(avahi_server)));
            return;
        }

    if (avahi_s_entry_group_is_empty(h->group)) {
        AvahiProtocol p;
        int err;
        const AvahiServerConfig *config;
        config = avahi_server_get_config(avahi_server);
        
        p = (h->address.proto == AVAHI_PROTO_INET && config->publish_a_on_ipv6) ||
            (h->address.proto == AVAHI_PROTO_INET6 && config->publish_aaaa_on_ipv4) ? AVAHI_PROTO_UNSPEC : h->address.proto;
        
        if ((err = avahi_server_add_address(avahi_server, h->group, AVAHI_IF_UNSPEC, p, 0, h->host, &h->address)) < 0) {
            avahi_log_error ("Static host name %s: avahi_server_add_address failure: %s", h->host, avahi_strerror(err));
            return;
        }

        avahi_s_entry_group_commit (h->group);
    }
}
Exemplo n.º 16
0
static void
dnssd_callback(AvahiEntryGroup      *g,		/* I - Service */
	       AvahiEntryGroupState state,	/* I - Registration state */
	       void                 *context)	/* I - Printer */
{
  (void)context;

  if (g == NULL || (g_options.dnssd_data->ipp_ref != NULL &&
		    g_options.dnssd_data->ipp_ref != g))
    return;

  switch (state) {
  case AVAHI_ENTRY_GROUP_ESTABLISHED :
    /* The entry group has been established successfully */
    NOTE("Service entry for the printer successfully established.");
    break;
  case AVAHI_ENTRY_GROUP_COLLISION :
    ERR("DNS-SD service name for this printer already exists");
  case AVAHI_ENTRY_GROUP_FAILURE :
    ERR("Entry group failure: %s\n",
	avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
    g_options.terminate = 1;
    break;
  case AVAHI_ENTRY_GROUP_UNCOMMITED:
  case AVAHI_ENTRY_GROUP_REGISTERING:
  default:
    break;
  }
}
Exemplo n.º 17
0
static AvahiSEntryGroup* add_dns_servers(AvahiServer *s, AvahiSEntryGroup* g, char **l) {
    char **p;

    assert(s);
    assert(l);

    if (!g)
        g = avahi_s_entry_group_new(s, NULL, NULL);

    assert(avahi_s_entry_group_is_empty(g));

    for (p = l; *p; p++) {
        AvahiAddress a;

        if (!avahi_address_parse(*p, AVAHI_PROTO_UNSPEC, &a))
            avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
        else
            if (avahi_server_add_dns_server_address(s, g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, NULL, AVAHI_DNS_SERVER_RESOLVE, &a, 53) < 0) {
                avahi_s_entry_group_free(g);
                avahi_log_error("Failed to add DNS server address: %s", avahi_strerror(avahi_server_errno(s)));
                return NULL;
            }
    }

    avahi_s_entry_group_commit(g);

    return g;
}
Exemplo n.º 18
0
static int avahi_register(char *srvname, int srvport) {
  debug(1, "avahi: avahi_register.");
  name = strdup(srvname);
  port = srvport;

  int err;
  if (!(tpoll = avahi_threaded_poll_new())) {
    warn("couldn't create avahi threaded tpoll!");
    return -1;
  }
  if (!(client =
            avahi_client_new(avahi_threaded_poll_get(tpoll), 0, client_callback, NULL, &err))) {
    warn("couldn't create avahi client: %s!", avahi_strerror(err));
    return -1;
  }
  
 // we need this to detect the IPv6 number we're advertising...
 // if (!(sb = avahi_service_browser_new(client, AVAHI_IF_UNSPEC,  AVAHI_PROTO_UNSPEC, config.regtype, NULL, 0, browse_callback, client))) {
 //     warn("Failed to create service browser: %s\n", avahi_strerror(avahi_client_errno(client)));
 //     return -1;
 // }


  if (avahi_threaded_poll_start(tpoll) < 0) {
    warn("couldn't start avahi tpoll thread");
    return -1;
  }

  return 0;
}
Exemplo n.º 19
0
static void
search_for_server (SnraClient * client)
{
  const AvahiPoll *poll_api;
  int error;

  if (client->glib_poll == NULL) {
    client->glib_poll = avahi_glib_poll_new (NULL, G_PRIORITY_DEFAULT);
    if (client->glib_poll == NULL)
      return;
  }

  poll_api = avahi_glib_poll_get (client->glib_poll);

  if (client->avahi_client == NULL) {
    client->avahi_client =
        avahi_client_new (poll_api, AVAHI_CLIENT_NO_FAIL,
        (AvahiClientCallback) snra_avahi_client_callback, client, &error);
    if (client->avahi_client == NULL) {
      fprintf (stderr, "Failed to connect to Avahi: %s",
          avahi_strerror (error));
      return;
    }
  }

}
Exemplo n.º 20
0
void browse_callback(
		AvahiServiceBrowser *b,
		AvahiIfIndex interface,
		AvahiProtocol protocol,
		AvahiBrowserEvent event,
		const char *name,
		const char *type,
		const char *domain,
		AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
		void* userdata) {

	AvahiClient *c = (AvahiClient *)userdata;
	assert(b);

	/* Called whenever a new services becomes available on the LAN or is removed from the LAN */

	switch (event) {
	case AVAHI_BROWSER_FAILURE:

		std::cerr << "(Browser) " << avahi_strerror(avahi_client_errno(avahi_service_browser_get_client(b))) << std::endl;
		avahi_simple_poll_quit(simple_publish_poll);
		return;

	case AVAHI_BROWSER_NEW:
		std::cerr << "(Browser) NEW: service '" << name << "' of type '" << type << "' in domain '" << domain << "'\n" << std::endl;

		/* We ignore the returned resolver object. In the callback
               function we free it. If the server is terminated before
               the callback function is called the server will free
               the resolver for us. */

		if (!(avahi_service_resolver_new(c, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, (AvahiLookupFlags)0, resolve_callback, c)))
			std::cerr << "Failed to resolve service '" << name << "': " << avahi_strerror(avahi_client_errno(c)) <<std::endl;

		break;

	case AVAHI_BROWSER_REMOVE:
		mn->deleteHost((char *)name);
		std::cerr << "(Browser) REMOVE: service '" << name << "' of type '" << type << "' in domain '" << domain << std::endl;
		break;

	case AVAHI_BROWSER_ALL_FOR_NOW:
	case AVAHI_BROWSER_CACHE_EXHAUSTED:
		std::cerr << "(Browser) " << (event == AVAHI_BROWSER_CACHE_EXHAUSTED ? "CACHE_EXHAUSTED" : "ALL_FOR_NOW") << std::endl;
		break;
	}
}
Exemplo n.º 21
0
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;
}
Exemplo n.º 22
0
static void service_browser_callback(
        AvahiServiceBrowser *b,
        AvahiIfIndex interface,
        AvahiProtocol protocol,
        AvahiBrowserEvent event,
        const char *name,
        const char *type,
        const char *domain,
        AvahiLookupResultFlags flags,
        void *userdata) {

    Config *c = userdata;

    assert(b);
    assert(c);

    switch (event) {
        case AVAHI_BROWSER_NEW: {
            if (c->ignore_local && (flags & AVAHI_LOOKUP_RESULT_LOCAL))
                break;

            if (find_service(interface, protocol, name, type, domain))
                return;

            add_service(c, interface, protocol, name, type, domain);

            //print_service_line(c, '+', interface, protocol, name, type, domain, 1);
            break;

        }

        case AVAHI_BROWSER_REMOVE: {
            ServiceInfo *info;

            if (!(info = find_service(interface, protocol, name, type, domain)))
                return;

            remove_service(c, info);

            //print_service_line(c, '-', interface, protocol, name, type, domain, 1);
            break;
        }

        case AVAHI_BROWSER_FAILURE:
            fprintf(stderr, ("service_browser failed: %s\n"), avahi_strerror(avahi_client_errno(client)));
            avahi_simple_poll_quit(simple_poll);
            break;

        case AVAHI_BROWSER_CACHE_EXHAUSTED:
            n_cache_exhausted --;
            check_terminate(c);
            break;

        case AVAHI_BROWSER_ALL_FOR_NOW:
            n_all_for_now --;
            check_terminate(c);
            break;
    }
}
Exemplo n.º 23
0
/* Called when a resolve call completes */
static void resolve_reply(
        AvahiServiceResolver *UNUSED(r),
        AvahiIfIndex UNUSED(interface),
        AvahiProtocol UNUSED(protocol),
        AvahiResolverEvent event,
        const char *name,
        const char *UNUSED(type),
        const char *UNUSED(domain),
        const char *UNUSED(host_name),
        const AvahiAddress *a,
        uint16_t port,
        AvahiStringList *txt,
        AvahiLookupResultFlags UNUSED(flags),
        void *userdata) {

    struct host *h = userdata;

    switch (event) {

        case AVAHI_RESOLVER_FOUND: {
            AvahiStringList *i;

            /* Look for the number of CPUs in TXT RRs */
            for (i = txt; i; i = i->next) {
                char *key, *value;

                if (avahi_string_list_get_pair(i, &key, &value, NULL) < 0)
                    continue;

                if (!strcmp(key, "cpus"))
                    if ((h->n_cpus = atoi(value)) <= 0)
                        h->n_cpus = 1;

                avahi_free(key);
                avahi_free(value);
            }

            h->address = *a;
            h->port = port;

            avahi_service_resolver_free(h->resolver);
            h->resolver = NULL;

            /* Write modified hosts file */
            write_hosts(h->daemon_data);

            break;
        }

        case AVAHI_RESOLVER_FAILURE:

            rs_log_warning("Failed to resolve service '%s': %s\n", name,
                           avahi_strerror(avahi_client_errno(h->daemon_data->client)));

            free_host(h);
            break;
    }

}
Exemplo n.º 24
0
static void create_services(AvahiServer *s) {
    char r[128];
    int ret;
    assert(s);

    /* If this is the first time we're called, let's create a new entry group */
    if (!group)
        if (!(group = avahi_s_entry_group_new(s, entry_group_callback, NULL))) {
            fprintf(stderr, "avahi_entry_group_new() failed: %s\n", avahi_strerror(avahi_server_errno(s)));
            goto fail;
        }

    fprintf(stderr, "Adding service '%s'\n", name);

    /* Create some random TXT data */
    snprintf(r, sizeof(r), "random=%i", rand());

    /* Add the service for IPP */
    if ((ret = avahi_server_add_service(s, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_ipp._tcp", NULL, NULL, 651, "test=blah", r, NULL)) < 0) {
        fprintf(stderr, "Failed to add _ipp._tcp service: %s\n", avahi_strerror(ret));
        goto fail;
    }

    /* Add the same service for BSD LPR */
    if ((ret = avahi_server_add_service(s, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_printer._tcp", NULL, NULL, 515, NULL)) < 0) {
        fprintf(stderr, "Failed to add _printer._tcp service: %s\n", avahi_strerror(ret));
        goto fail;
    }

    /* Add an additional (hypothetic) subtype */
    if ((ret = avahi_server_add_service_subtype(s, group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, "_printer._tcp", NULL, "_magic._sub._printer._tcp") < 0)) {
        fprintf(stderr, "Failed to add subtype _magic._sub._printer._tcp: %s\n", avahi_strerror(ret));
        goto fail;
    }

    /* Tell the server to register the service */
    if ((ret = avahi_s_entry_group_commit(group)) < 0) {
        fprintf(stderr, "Failed to commit entry_group: %s\n", avahi_strerror(ret));
        goto fail;
    }

    return;

fail:
    avahi_simple_poll_quit(simple_poll);
}
Exemplo n.º 25
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 */
}
Exemplo n.º 26
0
static void browse_callback(
    AvahiServiceBrowser *b,
    AvahiIfIndex interface,
    AvahiProtocol protocol,
    AvahiBrowserEvent event,
    const char *name,
    const char *type,
    const char *domain,
    AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
    void* userdata) {
    
    AvahiClient *c = userdata;
    assert(b);

    /* Called whenever a new services becomes available on the LAN or is removed from the LAN */

    switch (event) {
        case AVAHI_BROWSER_FAILURE:
            
            fprintf(stderr, "(Browser) %s\n", avahi_strerror(avahi_client_errno(avahi_service_browser_get_client(b))));
            avahi_simple_poll_quit(simple_poll);
            return;

        case AVAHI_BROWSER_NEW:
            fprintf(stderr, "(Browser) NEW: service '%s' of type '%s' in domain '%s'\n", name, type, domain);

            /* We ignore the returned resolver object. In the callback
               function we free it. If the server is terminated before
               the callback function is called the server will free
               the resolver for us. */

            if (!(avahi_service_resolver_new(c, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, resolve_callback, c)))
                fprintf(stderr, "Failed to resolve service '%s': %s\n", name, avahi_strerror(avahi_client_errno(c)));
            
            break;

        case AVAHI_BROWSER_REMOVE:
            fprintf(stderr, "(Browser) REMOVE: service '%s' of type '%s' in domain '%s'\n", name, type, domain);
            break;

        case AVAHI_BROWSER_ALL_FOR_NOW:
        case AVAHI_BROWSER_CACHE_EXHAUSTED:
            fprintf(stderr, "(Browser) %s\n", event == AVAHI_BROWSER_CACHE_EXHAUSTED ? "CACHE_EXHAUSTED" : "ALL_FOR_NOW");
            break;
    }
}
Exemplo n.º 27
0
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;
}
Exemplo n.º 28
0
    void Browser::browse_callback(AvahiServiceBrowser *b,
				  AvahiIfIndex interface,
				  AvahiProtocol protocol,
				  AvahiBrowserEvent event,
				  const char *name,
				  const char *type,
				  const char *domain,
				  AvahiLookupResultFlags,
				  void* data)
    {
	assert(b);
	AvahiClient *client = avahi_service_browser_get_client(b);
	assert(data);
	Browser *browser = static_cast<Browser *>(data);

	switch (event) {
	case AVAHI_BROWSER_FAILURE:
	    cerr << "[Browser] " << avahi_strerror(avahi_client_errno(avahi_service_browser_get_client(b))) << endl;
	    throw BrowserError(avahi_client_errno(client));
	    //a->Stop();
	    return;

	case AVAHI_BROWSER_NEW:
	    //cerr << "[Browser] New service '" << name << "' of type '" << type << "' in domain '" << domain << "'" << endl;

	    if (!(avahi_service_resolver_new(client, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC,
					     (AvahiLookupFlags)0, resolve_callback, browser)))
		cerr << "Failed to resolve service '" << name << "': " << avahi_strerror(avahi_client_errno(client)) << endl;
	    break;

	case AVAHI_BROWSER_REMOVE:
	    //cerr << "[Browser] Removed service " << name << " of type " << type << " in domain " << domain << endl;
	    // XXX Remove cached service, broadcast
	    browser->remove_service(name);
	    break;

	case AVAHI_BROWSER_ALL_FOR_NOW:
	    //cerr << "[Browser] All for now." << endl;
	    break;

	case AVAHI_BROWSER_CACHE_EXHAUSTED:
	    //cerr << "[Browser] Cache exhausted." << endl;
	    break;
	}
    }
Exemplo n.º 29
0
static void
_browser_callback(AvahiServiceBrowser *b, AvahiIfIndex interface,
		  AvahiProtocol protocol, AvahiBrowserEvent event,
		  const char *name, const char *type, const char *domain,
		  AvahiLookupResultFlags flags, void *userdata) {

	PurpleAccount *account = userdata;
	PurpleBuddy *gb = NULL;

	switch (event) {
		case AVAHI_BROWSER_FAILURE:
			purple_debug_error("bonjour", "_browser_callback - Failure: %s\n",
				avahi_strerror(avahi_client_errno(avahi_service_browser_get_client(b))));
			/* TODO: This is an error that should be handled. */
			break;
		case AVAHI_BROWSER_NEW:
			/* A new peer has joined the network and uses iChat bonjour */
			purple_debug_info("bonjour", "_browser_callback - new service\n");
			/* Make sure it isn't us */
			if (g_ascii_strcasecmp(name, account->username) != 0) {
				if (!avahi_service_resolver_new(avahi_service_browser_get_client(b),
						interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC,
						0, _resolver_callback, account)) {
					purple_debug_warning("bonjour", "_browser_callback -- Error initiating resolver: %s\n",
						avahi_strerror(avahi_client_errno(avahi_service_browser_get_client(b))));
				}
			}
			break;
		case AVAHI_BROWSER_REMOVE:
			purple_debug_info("bonjour", "_browser_callback - Remove service\n");
			gb = purple_find_buddy(account, name);
			if (gb != NULL) {
				bonjour_buddy_delete(gb->proto_data);
				purple_blist_remove_buddy(gb);
			}
			break;
		case AVAHI_BROWSER_ALL_FOR_NOW:
		case AVAHI_BROWSER_CACHE_EXHAUSTED:
			purple_debug_warning("bonjour", "(Browser) %s\n",
				event == AVAHI_BROWSER_CACHE_EXHAUSTED ? "CACHE_EXHAUSTED" : "ALL_FOR_NOW");
			break;
		default:
			purple_debug_info("bonjour", "Unrecognized Service browser event: %d.\n", event);
	}
}
Exemplo n.º 30
0
/*
 * Tries to setup the Zeroconf thread and any
 * neccessary config setting.
 */
void av_zeroconf_register(const AFPObj *obj) {
    int error;

    /* initialize the struct that holds our config settings. */
    if (ctx) {
        LOG(log_debug, logtype_afpd, "Resetting zeroconf records");
        avahi_entry_group_reset(ctx->group);
    } else {
        ctx = calloc(1, sizeof(struct context));
        ctx->obj = obj;
        assert(ctx);
    }

    /* first of all we need to initialize our threading env */
    if (!(ctx->threaded_poll = avahi_threaded_poll_new())) {
        goto fail;
    }

    /* now we need to acquire a client */
    if (!(ctx->client = avahi_client_new(avahi_threaded_poll_get(ctx->threaded_poll),
                                         AVAHI_CLIENT_NO_FAIL,
                                         client_callback,
                                         NULL,
                                         &error))) {
        LOG(log_error, logtype_afpd, "Failed to create client object: %s",
            avahi_strerror(error));
        goto fail;
    }

    if (avahi_threaded_poll_start(ctx->threaded_poll) < 0) {
        LOG(log_error, logtype_afpd, "Failed to create thread: %s",
            avahi_strerror(avahi_client_errno(ctx->client)));
        goto fail;
    } else {
        LOG(log_info, logtype_afpd, "Successfully started avahi loop.");
    }

    ctx->thread_running = 1;
    return;

fail:
    av_zeroconf_unregister();

    return;
}