Ejemplo n.º 1
0
static void check_terminate(Config *c) {

    assert(n_all_for_now >= 0);
    assert(n_cache_exhausted >= 0);
    assert(n_resolving >= 0);

    if (n_all_for_now <= 0 && n_resolving <= 0) {

        if (c->verbose && !c->parsable) {
            printf((": All for now\n"));
            n_all_for_now++; /* Make sure that this event is not repeated */
        }

        if (c->terminate_on_all_for_now)
            avahi_simple_poll_quit(simple_poll);
    }

    if (n_cache_exhausted <= 0 && n_resolving <= 0) {

        if (c->verbose && !c->parsable) {
            printf((": Cache exhausted\n"));
            n_cache_exhausted++; /* Make sure that this event is not repeated */
        }

        if (c->terminate_on_cache_exhausted)
            avahi_simple_poll_quit(simple_poll);
    }
}
static void server_callback(AvahiServer *s, AvahiServerState state, AVAHI_GCC_UNUSED void * userdata) {
    assert(s);

    /* Called whenever the server state changes */

    switch (state) {

        case AVAHI_SERVER_RUNNING:
            /* The serve has startup successfully and registered its host
             * name on the network, so it's time to create our services */

            if (!group)
                create_services(s);

            break;

        case AVAHI_SERVER_COLLISION: {
            char *n;
            int r;

            /* A host name collision happened. Let's pick a new name for the server */
            n = avahi_alternative_host_name(avahi_server_get_host_name(s));
            fprintf(stderr, "Host name collision, retrying with '%s'\n", n);
            r = avahi_server_set_host_name(s, n);
            avahi_free(n);

            if (r < 0) {
                fprintf(stderr, "Failed to set new host name: %s\n", avahi_strerror(r));

                avahi_simple_poll_quit(simple_poll);
                return;
            }

        }

            /* Fall through */

        case AVAHI_SERVER_REGISTERING:

	    /* 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_s_entry_group_reset(group);

            break;

        case AVAHI_SERVER_FAILURE:

            /* Terminate on failure */

            fprintf(stderr, "Server failure: %s\n", avahi_strerror(avahi_server_errno(s)));
            avahi_simple_poll_quit(simple_poll);
            break;

        case AVAHI_SERVER_INVALID:
            ;
    }
}
Ejemplo n.º 3
0
static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
    Config *config = userdata;
    
    client = c;
    
    switch (state) {
        case AVAHI_CLIENT_FAILURE:

            if (config->no_fail && avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
                int error;

                /* We have been disconnected, so let reconnect */

                fprintf(stderr, "Disconnected, reconnecting ...\n");

                avahi_client_free(client);
                client = NULL;
                entry_group = NULL;

                if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), AVAHI_CLIENT_NO_FAIL, client_callback, config, &error))) {
                    fprintf(stderr, "Failed to create client object: %s\n", avahi_strerror(error));
                    avahi_simple_poll_quit(simple_poll);
                }

            } else {
                fprintf(stderr, "Client failure, exiting: %s\n", avahi_strerror(avahi_client_errno(c)));
                avahi_simple_poll_quit(simple_poll);
            }

            break;
            
        case AVAHI_CLIENT_S_RUNNING:

            if (register_stuff(config) < 0)
                avahi_simple_poll_quit(simple_poll);
            
            break;

        case AVAHI_CLIENT_S_COLLISION:

            if (config->verbose)
                fprintf(stderr, "Host name conflict\n");
            
            if (entry_group) {
                avahi_entry_group_free(entry_group);
                entry_group = NULL;
            }
            break;
            
        case AVAHI_CLIENT_CONNECTING:
            
            if (config->verbose)
                fprintf(stderr, "Waiting for daemon ...\n");
            break;
            
        case AVAHI_CLIENT_S_REGISTERING:
            ;
    }
}
Ejemplo n.º 4
0
static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
    Config *config = userdata;

    /* This function might be called when avahi_client_new() has not
     * returned yet.*/
    client = c;

    switch (state) {
        case AVAHI_CLIENT_FAILURE:

            if (config->no_fail && avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
                int error;

                /* We have been disconnected, so let reconnect */

                fprintf(stderr, ("Disconnected, reconnecting ...\n"));

                avahi_client_free(client);
                client = NULL;

                avahi_string_list_free(browsed_types);
                browsed_types = NULL;

                while (services)
                    remove_service(config, services);

                browsing = 0;

                if (!(client = avahi_client_new(avahi_simple_poll_get(simple_poll), AVAHI_CLIENT_NO_FAIL, client_callback, config, &error))) {
                    fprintf(stderr, ("Failed to create client object: %s\n"), avahi_strerror(error));
                    avahi_simple_poll_quit(simple_poll);
                }

            } else {
                fprintf(stderr, ("Client failure, exiting: %s\n"), avahi_strerror(avahi_client_errno(c)));
                avahi_simple_poll_quit(simple_poll);
            }

            break;

        case AVAHI_CLIENT_S_REGISTERING:
        case AVAHI_CLIENT_S_RUNNING:
        case AVAHI_CLIENT_S_COLLISION:

            if (!browsing)
            if (start(config) < 0)
                avahi_simple_poll_quit(simple_poll);

            break;

        case AVAHI_CLIENT_CONNECTING:

            if (config->verbose && !config->parsable)
                fprintf(stderr, ("Waiting for daemon ...\n"));

            break;
    }
}
Ejemplo n.º 5
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:
            fprintf(stderr, "(Browser) %s\n", event == AVAHI_BROWSER_CACHE_EXHAUSTED ? "CACHE_EXHAUSTED" : "ALL_FOR_NOW");
            avahi_simple_poll_quit(simple_poll);
            break;

        case AVAHI_BROWSER_CACHE_EXHAUSTED:
            fprintf(stderr, "(Browser) %s\n", event == AVAHI_BROWSER_CACHE_EXHAUSTED ? "CACHE_EXHAUSTED" : "ALL_FOR_NOW");
            break;
    }
}
Ejemplo n.º 6
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;
   }
}
Ejemplo n.º 7
0
void
avahi_done(void)
{
  avahi_simple_poll_quit(avahi_asp);
  pthread_kill(avahi_tid, SIGTERM);
  pthread_join(avahi_tid, NULL);
}
Ejemplo n.º 8
0
static void browse_service_type(Config *c, const char *stype, const char *domain) {
    AvahiServiceBrowser *b;
    AvahiStringList *i;

    assert(c);
    assert(client);
    assert(stype);

    for (i = browsed_types; i; i = i->next)
        if (avahi_domain_equal(stype, (char*) i->text))
            return;

    if (!(b = avahi_service_browser_new(
            client,
            AVAHI_IF_UNSPEC,
            AVAHI_PROTO_UNSPEC,
            stype,
            domain,
            0,
            service_browser_callback,
            c))) {

        fprintf(stderr, ("avahi_service_browser_new() failed: %s\n"), avahi_strerror(avahi_client_errno(client)));
        avahi_simple_poll_quit(simple_poll);
    }

    browsed_types = avahi_string_list_add(browsed_types, stype);

    n_all_for_now++;
    n_cache_exhausted++;
}
Ejemplo n.º 9
0
Archivo: dnssd.c Proyecto: zdohnal/cups
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;
  }
}
static void
stop_poll_timeout (AvahiTimeout *timeout, void *user_data)
{
	AvahiSimplePoll *poll = user_data;
	
	avahi_simple_poll_quit (poll);
}
Ejemplo n.º 11
0
static void __avahi_browser_cb(AvahiServiceBrowser *browser,
		AvahiIfIndex iface, AvahiProtocol proto,
		AvahiBrowserEvent event, const char *name,
		const char *type, const char *domain,
		__notused AvahiLookupResultFlags flags, void *d)
{
	struct avahi_discovery_data *ddata = (struct avahi_discovery_data *) d;
	struct AvahiClient *client = avahi_service_browser_get_client(browser);

	switch (event) {
	default:
	case AVAHI_BROWSER_NEW:
		ddata->found = !!avahi_service_resolver_new(client, iface,
				proto, name, type, domain,
				AVAHI_PROTO_UNSPEC, 0,
				__avahi_resolver_cb, d);
		break;
	case AVAHI_BROWSER_ALL_FOR_NOW:
		if (ddata->found) {
			while (!ddata->resolved) {
				struct timespec ts;
				ts.tv_sec = 0;
				ts.tv_nsec = 4000000;
				nanosleep(&ts, NULL);
			}
		}
	case AVAHI_BROWSER_FAILURE: /* fall-through */
		avahi_simple_poll_quit(ddata->poll);
	case AVAHI_BROWSER_CACHE_EXHAUSTED: /* fall-through */
		break;
	}
}
Ejemplo n.º 12
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;
    }
}
Ejemplo n.º 13
0
static void create_cnames(AvahiClient *c) 
{
    struct runtime_data r;

   /* If this is the first time we're called, let's create a new entry group if necessary */
   if ((!_group)&&(!(_group = avahi_entry_group_new(c, entry_group_callback, NULL))))
   {
      ap_log_error(APLOG_MARK, APLOG_ERR, 0, r.main_server, "avahi_entry_group_new() failed: %s", avahi_strerror(avahi_client_errno(c)));
      goto fail;
   }

   /* If the group is empty (either because it was just created, or because it was reset previously, add our entries. */
   if (avahi_entry_group_is_empty(_group)) 
   {
      char hostname[HOST_NAME_MAX+64] = ".";  /* this dot will be overwritten with a count-byte, below */
      if (gethostname(&hostname[1], sizeof(hostname)-1) < 0) perror("gethostname");
      strncat(hostname, ".local", sizeof(hostname));
      hostname[sizeof(hostname)-1] = '\0';  /* paranoia? */

      /* Convert the hostname string into DNS's labelled-strings format */
      int hostnameLen = strlen(hostname);
      char count = 0;
      int i;
      for (i=hostnameLen-1; i>=0; i--)
      {
         if (hostname[i] == '.')
         {
            hostname[i] = count;
            count = 0;
         }
         else count++;
      }

      for (i=0; (_cnames[i] != NULL); i++)
      {
         int ret = avahi_entry_group_add_record(_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, (AvahiPublishFlags)(AVAHI_PUBLISH_USE_MULTICAST|AVAHI_PUBLISH_ALLOW_MULTIPLE), _cnames[i], AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_CNAME, AVAHI_DEFAULT_TTL, hostname, hostnameLen+1); 
         if (ret >= 0) ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r.main_server, "Published DNS-SD hostname alias [%s]", _cnames[i]);
         else
         {
            ap_log_error(APLOG_MARK, APLOG_ERR, 0, r.main_server, "Failed to add CNAME record [%s]: %s", _cnames[i], avahi_strerror(ret));
            goto fail;
         }
      }

      int ret = avahi_entry_group_commit(_group);
      if (ret < 0)
      {
         ap_log_error(APLOG_MARK, APLOG_ERR, 0, r.main_server, "Failed to commit entry group: %s", avahi_strerror(ret));
         goto fail;
      }
   }
   return;

fail:
   avahi_simple_poll_quit(_poll);
}
static void
avahi_client_callback (AvahiClient *client, AvahiClientState state, void *user_data)
{
	AvahiSimplePoll *poll;

	poll = user_data;
	if (state == AVAHI_CLIENT_FAILURE) {
		avahi_simple_poll_quit (poll);
	}
}
Ejemplo n.º 15
0
static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
    assert(c);

    /* Called whenever the client or server state changes */

    if (state == AVAHI_CLIENT_FAILURE) {
        fprintf(stderr, "Server connection failure: %s\n", avahi_strerror(avahi_client_errno(c)));
        avahi_simple_poll_quit(simple_poll);
    }
}
Ejemplo n.º 16
0
static void timeout_callback (AvahiTimeout *t, void *userdata) {
    const AvahiPoll *api = avahi_simple_poll_get (simple_poll);

    if (n_pending_resolvers > 0) {
        api->timeout_update (t, (struct timeval*) userdata);
    } else {
        avahi_free (userdata);
        api->timeout_free (t);
        avahi_simple_poll_quit (simple_poll);
    }
}
Ejemplo n.º 17
0
void BrowseAvahi::client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
    assert(c);

    /* Called whenever the client or server state changes */
//    BrowseAvahi* browseAvahi = static_cast<BrowseAvahi*>(userdata);

    if (state == AVAHI_CLIENT_FAILURE) {
        logE << "Server connection failure: " << avahi_strerror(avahi_client_errno(c)) << "\n";
        avahi_simple_poll_quit(simple_poll);
    }
}
Ejemplo n.º 18
0
static void domain_browser_callback(
        AvahiDomainBrowser *b,
        AVAHI_GCC_UNUSED AvahiIfIndex interface,
        AVAHI_GCC_UNUSED AvahiProtocol protocol,
        AvahiBrowserEvent event,
        const char *domain,
        AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
        void *userdata) {

    Config *c = userdata;

    assert(b);
    assert(c);

    switch (event) {

        case AVAHI_BROWSER_NEW:
        case AVAHI_BROWSER_REMOVE: {
            char ifname[IF_NAMESIZE];

            if (c->parsable)
                printf("%c;%s;%s;%s\n",
                       event == AVAHI_BROWSER_NEW ? '+' : '-',
                       interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "",
                       protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "",
                       domain);
            else
                printf("%c %4s %4s %s\n",
                       event == AVAHI_BROWSER_NEW ? '+' : '-',
                       interface != AVAHI_IF_UNSPEC ? if_indextoname(interface, ifname) : "n/a",
                       protocol != AVAHI_PROTO_UNSPEC ? avahi_proto_to_string(protocol) : "n/a",
                       domain);
            break;
        }

        case AVAHI_BROWSER_FAILURE:
            fprintf(stderr, ("domain_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;
    }
}
Ejemplo n.º 19
0
void create_services(AvahiClient *c) {
	char r[128];
	int ret;
	assert(c);

	/* If this is the first time we're called, let's create a new
	 * entry group if necessary */

	if (!group)
		if (!(group = avahi_entry_group_new(c, entry_group_callback, NULL))) {
			std::cerr << "avahi_entry_group_new() failed: " << avahi_strerror(avahi_client_errno(c)) << std::endl;
		}

	/* If the group is empty (either because it was just created, or
	 * because it was reset previously, add our entries.  */

	if (avahi_entry_group_is_empty(group)) {
		std::cerr << "Adding service '" << name << "'" << std::endl;

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

		/* We will now add two services and one subtype to the entry
		 * group. The two services have the same name, but differ in
		 * the service type (IPP vs. BSD LPR). Only services with the
		 * same name should be put in the same entry group. */

		if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, (AvahiPublishFlags)0, name.c_str(), "_heatSync._tcp", NULL, NULL, PORTINT, "version=0.1", NULL, NULL )) < 0) { //todo: magically put in version no.
			if (ret == AVAHI_ERR_COLLISION) {
				name = avahi_alternative_service_name(name.c_str());

				std::cerr << "Service name collision, renaming service to '" << name << "'" << std::endl;

				avahi_entry_group_reset(group);

				create_services(c);
				return;
			}

			std::cerr << "Failed to add " << name << " service: " << avahi_strerror(ret) << std::endl;
		}

		/* Tell the server to register the service */
		if ((ret = avahi_entry_group_commit(group)) < 0) {
			std::cerr << "Failed to commit entry group: " << avahi_strerror(ret) << std::endl;
		}
	}

	avahi_simple_poll_quit(simple_publish_poll);
}
Ejemplo n.º 20
0
static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
    switch (state) {
        case AVAHI_CLIENT_FAILURE:
            fprintf(stderr, _("Client failure, exiting: %s\n"), avahi_strerror(avahi_client_errno(c)));
            avahi_simple_poll_quit(simple_poll);
            break;

        case AVAHI_CLIENT_S_REGISTERING:
        case AVAHI_CLIENT_S_RUNNING:
        case AVAHI_CLIENT_S_COLLISION:
        case AVAHI_CLIENT_CONNECTING:
            ;
    }
}
Ejemplo n.º 21
0
static void watch_callback(AvahiWatch *w, int fd, AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
    int s;
    ssize_t l;
    
    assert(w);
    assert(fd == pipe_fds[0]);
    assert(event == AVAHI_WATCH_IN);

    l = read(fd, &s, sizeof(s));
    assert(l == sizeof(s));

    fprintf(stderr, "Got %s, quitting.\n", s == SIGINT ? "SIGINT" : "SIGTERM");
    avahi_simple_poll_quit(simple_poll);
}
Ejemplo n.º 22
0
static void watch_callback(AvahiWatch *w, int fd, AvahiWatchEvent event, void *userdata) {
    char c;
    ssize_t l;
    struct runtime_data *r = userdata;

    ap_assert(w);
    ap_assert(fd == sigterm_pipe_fds[0]);
    ap_assert(event == AVAHI_WATCH_IN);
    ap_assert(r);

    l = read(fd, &c, sizeof(c));
    ap_assert(l == sizeof(c));

    avahi_simple_poll_quit(r->simple_poll);
}
Ejemplo n.º 23
0
void BrowseAvahi::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* client = (AvahiClient*)userdata;
	BrowseAvahi* browseAvahi = static_cast<BrowseAvahi*>(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:
			logE << "(Browser) " << avahi_strerror(avahi_client_errno(avahi_service_browser_get_client(b))) << "\n";
			avahi_simple_poll_quit(simple_poll);
			return;

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

			/* 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(browseAvahi->client_, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, (AvahiLookupFlags)0, resolve_callback, userdata)))
				logE << "Failed to resolve service '" << name << "': " << avahi_strerror(avahi_client_errno(browseAvahi->client_)) << "\n";

			break;

		case AVAHI_BROWSER_REMOVE:
			logO << "(Browser) REMOVE: service '" << name << "' of type '" << type << "' in domain '" << domain << "'\n";
			break;

		case AVAHI_BROWSER_ALL_FOR_NOW:
		case AVAHI_BROWSER_CACHE_EXHAUSTED:
			logO << "(Browser) " << (event == AVAHI_BROWSER_CACHE_EXHAUSTED ? "CACHE_EXHAUSTED" : "ALL_FOR_NOW") << "\n";
			break;
	}
}
Ejemplo n.º 24
0
	void CommunicationManager::entryGroupCallback(AvahiEntryGroup *g,
			AvahiEntryGroupState state, AVAHI_GCC_UNUSED void *userdata) {

		CommunicationManager* cm = CommunicationManager::getInstance();
		cm->group = g;

		/* Called whenever the entry group state changes */

		switch (state) {
			case AVAHI_ENTRY_GROUP_ESTABLISHED: {
				/* The entry group has been established successfully */
				cout << "Service '" << cm->name << "' successfully established."
						<< endl;
				break;
			}
			case AVAHI_ENTRY_GROUP_COLLISION: {
				char *n;

				/* A service name collision with a remote service
				 * happened. Let's pick a new name */
				n = avahi_alternative_service_name(cm->name);
				avahi_free(cm->name);
				cm->name = n;

				cout << "Service name collision, renaming service to '" <<
						cm->name << "'" << endl;

				/* And recreate the services */
				cm->createServices(avahi_entry_group_get_client(g));
				break;
			}
			case AVAHI_ENTRY_GROUP_FAILURE: {
				cout << "Entry group failure: " << avahi_strerror(
						avahi_client_errno(avahi_entry_group_get_client(g)))
						<< endl;

				/* Some kind of failure happened while we were registering our
				 * services */
				avahi_simple_poll_quit(cm->simplePoll);
				break;
			}
			case AVAHI_ENTRY_GROUP_UNCOMMITED:
			case AVAHI_ENTRY_GROUP_REGISTERING: {
				break;
			}
		}
	}
Ejemplo n.º 25
0
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);
}
Ejemplo n.º 26
0
static void client_callback(AvahiClient *client, AvahiClientState state, void *userdata) {
    struct daemon_data *d = userdata;

    switch (state) {

        case AVAHI_CLIENT_FAILURE:
            rs_log_crit("Client failure: %s\n", avahi_strerror(avahi_client_errno(client)));
            avahi_simple_poll_quit(d->simple_poll);
            break;

        case AVAHI_CLIENT_S_COLLISION:
        case AVAHI_CLIENT_S_REGISTERING:
        case AVAHI_CLIENT_S_RUNNING:
        case AVAHI_CLIENT_CONNECTING:
            ;
    }
}
Ejemplo n.º 27
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);
}
Ejemplo n.º 28
0
static void Browse_close(struct connection_in *in)
{
#if OW_ZERO
	if (in->master.browse.bonjour_browse && (libdnssd != NULL)) {
		DNSServiceRefDeallocate(in->master.browse.bonjour_browse);
		in->master.browse.bonjour_browse = 0 ;
	}
#if !OW_CYGWIN	
	if ( in->master.browse.avahi_poll != NULL ) {
		// Signal avahi loop to quit (in ow_avahi_browse.c)
		// and clean up for itself
		avahi_simple_poll_quit(in->master.browse.avahi_poll);
	}
#endif /* !OW_CYGWIN */
#else /* OW_ZERO */
	(void) in ;
#endif /* OW_ZERO */
}
Ejemplo n.º 29
0
void PublishAvahi::client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata)
{
	assert(c);

	/* 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 */
			static_cast<PublishAvahi*>(userdata)->create_services(c);
			break;

		case AVAHI_CLIENT_FAILURE:

			logE << "Client failure: " << avahi_strerror(avahi_client_errno(c)) << "\n";
			avahi_simple_poll_quit(simple_poll);

			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. */

		case AVAHI_CLIENT_S_REGISTERING:

			/* The server records are now being established. This
				* might be caused by a host name change. We need to wait
				* for our own records to register until the host name is
				* properly esatblished. */

			if (group)
				avahi_entry_group_reset(group);

			break;

		case AVAHI_CLIENT_CONNECTING:
			;
	}
}
Ejemplo n.º 30
0
/* Called whenever the client or server state changes */
static void client_callback(AvahiClient *c, AvahiClientState state, void *userdata) {
    struct runtime_data *r = userdata;

    ap_assert(r);

    r->client = c;

/*     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->main_server, "client_callback(%u)", state); */

    switch (state) {
        case AVAHI_CLIENT_S_RUNNING:
            create_all_services(r);
            break;

        case AVAHI_CLIENT_S_COLLISION:
            reset_services(r);
            break;

        case AVAHI_CLIENT_FAILURE:

            if (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
                int error;

                free_services(r);
                avahi_client_free(r->client);

                if ((r->client = avahi_client_new(avahi_simple_poll_get(r->simple_poll), AVAHI_CLIENT_NO_FAIL, client_callback, r, &error)))
                    break;

                ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->main_server, "avahi_client_new() failed: %s", avahi_strerror(error));
            } else
                ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->main_server, "Client failure: %s", avahi_strerror(avahi_client_errno(c)));

            avahi_simple_poll_quit(r->simple_poll);

            break;

        case AVAHI_CLIENT_S_REGISTERING:
        case AVAHI_CLIENT_CONNECTING:
            /* do nothing */
            break;
    }

}