Exemplo n.º 1
0
static void
client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata)
{
  struct mdns_browser *mb;
  AvahiServiceBrowser *b;
  int error;

  switch (state)
    {
      case AVAHI_CLIENT_S_RUNNING:
        DPRINTF(E_LOG, L_MDNS, "Avahi state change: Client running\n");
        if (!mdns_group)
	  create_all_group_entries();

	for (mb = browser_list; mb; mb = mb->next)
	  {
	    b = avahi_service_browser_new(mdns_client, AVAHI_IF_UNSPEC, mb->protocol, mb->type, NULL, 0, browse_callback, mb);
	    if (!b)
	      DPRINTF(E_LOG, L_MDNS, "Failed to recreate service browser (service type %s): %s\n", mb->type, MDNSERR);
	  }
        break;

      case AVAHI_CLIENT_S_COLLISION:
        DPRINTF(E_LOG, L_MDNS, "Avahi state change: Client collision\n");
        if(mdns_group)
	  avahi_entry_group_reset(mdns_group);
        break;

      case AVAHI_CLIENT_FAILURE:
        DPRINTF(E_LOG, L_MDNS, "Avahi state change: Client failure\n");

	error = avahi_client_errno(c);
	if (error == AVAHI_ERR_DISCONNECTED)
	  {
	    DPRINTF(E_LOG, L_MDNS, "Avahi Server disconnected, reconnecting\n");

	    avahi_client_free(mdns_client);
	    mdns_group = NULL;

	    mdns_client = avahi_client_new(&ev_poll_api, AVAHI_CLIENT_NO_FAIL, client_callback, NULL, &error);
	    if (!mdns_client)
	      DPRINTF(E_LOG, L_MDNS, "Failed to create new Avahi client: %s\n", avahi_strerror(error));
	  }
	else
	  {
	    DPRINTF(E_LOG, L_MDNS, "Avahi client failure: %s\n", avahi_strerror(error));
	  }
        break;

      case AVAHI_CLIENT_S_REGISTERING:
        DPRINTF(E_LOG, L_MDNS, "Avahi state change: Client registering\n");
        if (mdns_group)
	  avahi_entry_group_reset(mdns_group);
        break;

      case AVAHI_CLIENT_CONNECTING:
        DPRINTF(E_LOG, L_MDNS, "Avahi state change: Client connecting\n");
        break;
    }
}
static void unpublish_all_services(struct userdata *u, pa_bool_t rem) {
    void *state = NULL;
    struct service *s;

    pa_assert(u);

    pa_log_debug("Unpublishing services in Zeroconf");

    while ((s = pa_hashmap_iterate(u->services, &state, NULL))) {
        if (s->entry_group) {
            if (rem) {
                pa_log_debug("Removing entry group for %s.", s->service_name);
                avahi_entry_group_free(s->entry_group);
                s->entry_group = NULL;
            } else {
                avahi_entry_group_reset(s->entry_group);
                pa_log_debug("Resetting entry group for %s.", s->service_name);
            }
        }
    }

    if (u->main_entry_group) {
        if (rem) {
            pa_log_debug("Removing main entry group.");
            avahi_entry_group_free(u->main_entry_group);
            u->main_entry_group = NULL;
        } else {
            avahi_entry_group_reset(u->main_entry_group);
            pa_log_debug("Resetting main entry group.");
        }
    }
}
Exemplo n.º 3
0
static void client_callback(AvahiClient *c, AvahiClientState state,
                            AVAHI_GCC_UNUSED void *userdata) {
  switch (state) {
     case AVAHI_CLIENT_S_REGISTERING:
       if (group)
         avahi_entry_group_reset(group);
       break;

     case AVAHI_CLIENT_S_RUNNING:
       register_service(c);
       break;

     case AVAHI_CLIENT_FAILURE:
       debug(1,"avahi: client failure");
       break;

     case AVAHI_CLIENT_S_COLLISION:
       debug(2, "avahi: state is AVAHI_CLIENT_S_COLLISION...needs a rename: %s", name );
       break;

     case AVAHI_CLIENT_CONNECTING:
       debug(2, "avahi: received AVAHI_CLIENT_CONNECTING" );
       break;

     default:
       debug(1,"avahi: unexpected and unhandled avahi client state: %d", state );
       break;
  }
}
Exemplo n.º 4
0
static void
avahi_client_callback (AVAHI_GCC_UNUSED AvahiClient * client,
    AvahiClientState state, void *userdata)
{
  AurAvahi *avahi = (AurAvahi *) (userdata);

  switch (state) {
    case AVAHI_CLIENT_S_RUNNING:
      /* Create services now */
      avahi->priv->client = client;
      create_service (avahi);
      break;
    case AVAHI_CLIENT_FAILURE:
      g_error ("Client failure: %s\n",
          avahi_strerror (avahi_client_errno (client)));
      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
       * to records to register until the host name is
       * properly established - so clear out any pending
       * registration group. */
      if (avahi->priv->group)
        avahi_entry_group_reset (avahi->priv->group);
      break;
    default:
      break;
  }
}
Exemplo n.º 5
0
static void
create_all_group_entries(void)
{
  struct mdns_group_entry *ge;
  int ret;

  if (!group_entries)
    {
      DPRINTF(E_DBG, L_MDNS, "No entries yet... skipping service create\n");
      return;
    }

  if (mdns_group)
    avahi_entry_group_reset(mdns_group);

  DPRINTF(E_INFO, L_MDNS, "Re-registering mDNS groups (services and records)\n");

  for (ge = group_entries; ge; ge = ge->next)
    {
      create_group_entry(ge, 0);
      if (!mdns_group)
	return;
    }

  ret = avahi_entry_group_commit(mdns_group);
  if (ret < 0)
    DPRINTF(E_WARN, L_MDNS, "Could not commit mDNS services: %s\n", MDNSERR);
}
Exemplo 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;
   }
}
Exemplo n.º 7
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.º 8
0
JNIEXPORT jint JNICALL Java_avahi4j_EntryGroup_reset_1group(JNIEnv *e, jobject t, jlong ptr){
	dprint("[LOG] Entering %s\n", __PRETTY_FUNCTION__);

	struct avahi4j_entry_group *group = (struct avahi4j_entry_group *) (uintptr_t) ptr;

	int result = avahi_entry_group_reset(group->group);

	CHECK_N_RET(avahi_entry_group_reset, result);
}
Exemplo n.º 9
0
void Publisher::createServices(AvahiClient *client) {
	int error;
	const char* name = info.getServiceName().c_str();
	int port = info.getPort();

	if (group == NULL) {
		group = avahi_entry_group_new(client, &Publisher::entryGroupCallback, this);

		if (group == NULL) {
			std::cerr << "Could not create avahi group ("
					<< 	avahi_strerror(avahi_client_errno(client))
					<< ")." << std::endl;
			return;
		}
	}

	if (avahi_entry_group_is_empty(group)) {
		// new group or after reset
		error = avahi_entry_group_add_service(group, // group
				AVAHI_IF_UNSPEC, // interface
				AVAHI_PROTO_UNSPEC, // protocol to announce service with
				AVAHI_PUBLISH_USE_MULTICAST, // flags
				name, // service name
				serviceType, // service type
				NULL, // domain
				NULL, // host
				port, // port
				txtDataDeviceName.c_str(),
				txtDataProviderClassName.c_str(),
				txtDataSerialNumber.c_str(),
				NULL); // list of txt records

		if (error < 0) {
			// error
			if (error == AVAHI_ERR_COLLISION) {
				// collision with local service name
				nextName();
				avahi_entry_group_reset(group);
				// retry
				createServices(client);
			} else {
				std::cerr << "Could not add service '"
						<< name << "' (" << serviceType << ") to avahi group ("
						<< avahi_strerror(error) << ")" << std::endl;
				return;
			}
		}
		// start registering the service
		error = avahi_entry_group_commit(group);
		if (error < 0) {
			std::cerr << "Could not commit avahi group "
					<< "'" << name << "'"
					<< " (" << avahi_strerror(error) << ")" << std::endl;
		}
	}
}
Exemplo n.º 10
0
static void
dnssd_client_cb(AvahiClient      *c,		/* I - Client */
		AvahiClientState state,		/* I - Current state */
		void             *userdata)	/* I - User data (unused) */
{
  (void)userdata;
  int error;			/* Error code, if any */

  if (!c)
    return;

  switch (state) {
  default :
    NOTE("Ignore Avahi state %d.", state);
    break;

  case AVAHI_CLIENT_CONNECTING:
    NOTE("Waiting for Avahi server.");
    break;

  case AVAHI_CLIENT_S_RUNNING:
    NOTE("Avahi server connection got available, registering printer.");
    dnssd_register(c);
    break;

  case AVAHI_CLIENT_S_REGISTERING:
  case AVAHI_CLIENT_S_COLLISION:
    NOTE("Dropping printer registration because of possible host name change.");
    if (g_options.dnssd_data->ipp_ref)
      avahi_entry_group_reset(g_options.dnssd_data->ipp_ref);
    break;

  case AVAHI_CLIENT_FAILURE:
    if (avahi_client_errno(c) == AVAHI_ERR_DISCONNECTED) {
      NOTE("Avahi server disappeared, unregistering printer");
      dnssd_unregister();
      /* Renewing client */
      if (g_options.dnssd_data->DNSSDClient)
	avahi_client_free(g_options.dnssd_data->DNSSDClient);
      if ((g_options.dnssd_data->DNSSDClient =
	   avahi_client_new(avahi_threaded_poll_get
			    (g_options.dnssd_data->DNSSDMaster),
			    AVAHI_CLIENT_NO_FAIL,
			    dnssd_client_cb, NULL, &error)) == NULL) {
	ERR("Error: Unable to initialize DNS-SD client.");
	g_options.terminate = 1;
      }
    } else {
      ERR("Avahi server connection failure: %s",
	  avahi_strerror(avahi_client_errno(c)));
      g_options.terminate = 1;
    }
    break;

  }
}
Exemplo n.º 11
0
static void client_callback(AvahiClient *client,
                            AvahiClientState state,
                            void *userdata)
{
    ctx->client = client;

    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 (!ctx->group)
            register_stuff();
        break;

    case AVAHI_CLIENT_S_COLLISION:
        if (ctx->group)
            avahi_entry_group_reset(ctx->group);
        break;

    case AVAHI_CLIENT_FAILURE: {
        if (avahi_client_errno(client) == AVAHI_ERR_DISCONNECTED) {
            int error;

            avahi_client_free(ctx->client);
            ctx->client = NULL;
            ctx->group = NULL;

            /* Reconnect to the server */
            if (!(ctx->client = avahi_client_new(avahi_threaded_poll_get(ctx->threaded_poll),
                                                 AVAHI_CLIENT_NO_FAIL,
                                                 client_callback,
                                                 ctx,
                                                 &error))) {

                LOG(log_error, logtype_afpd, "Failed to contact server: %s",
                    avahi_strerror(error));

                avahi_threaded_poll_quit(ctx->threaded_poll);
            }

        } else {
            LOG(log_error, logtype_afpd, "Client failure: %s",
                avahi_strerror(avahi_client_errno(client)));
            avahi_threaded_poll_quit(ctx->threaded_poll);
        }
        break;
    }

    case AVAHI_CLIENT_S_REGISTERING:
        break;
    case AVAHI_CLIENT_CONNECTING:
        break;
    }
}
Exemplo n.º 12
0
static void client_callback(AvahiClient *client, AvahiClientState state, void *userdata) {
    struct context *ctx = userdata;

    ctx->client = client;

    switch (state) {

        case AVAHI_CLIENT_S_RUNNING:

            register_stuff(ctx);
            break;

        case AVAHI_CLIENT_S_COLLISION:
        case AVAHI_CLIENT_S_REGISTERING:

            if (ctx->group)
                avahi_entry_group_reset(ctx->group);

            break;

        case AVAHI_CLIENT_FAILURE:

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

                avahi_client_free(ctx->client);
                ctx->client = NULL;
                ctx->group = NULL;

                /* Reconnect to the server */

                if (!(ctx->client = avahi_client_new(
                              avahi_threaded_poll_get(ctx->threaded_poll),
                              AVAHI_CLIENT_NO_FAIL,
                              client_callback,
                              ctx,
                              &error))) {

                    rs_log_crit("Failed to contact server: %s", avahi_strerror(error));
                    avahi_threaded_poll_quit(ctx->threaded_poll);
                }

            } else {
                rs_log_crit("Client failure: %s", avahi_strerror(avahi_client_errno(client)));
                avahi_threaded_poll_quit(ctx->threaded_poll);
            }

            break;

        case AVAHI_CLIENT_CONNECTING:
            ;
    }
}
Exemplo n.º 13
0
static void client_callback(AvahiClient *client,
			    AvahiClientState state,
			    void *userdata)
{
	ctx.client = client;

	switch (state) {
	case AVAHI_CLIENT_S_RUNNING:
		/* The server has started up successfully and registered its host
		* name on the network, so it's time to create our services */
		register_stuff();
		break;
	case AVAHI_CLIENT_S_COLLISION:
	case AVAHI_CLIENT_S_REGISTERING:
		if (ctx.group)
			avahi_entry_group_reset(ctx.group);
		break;
	case AVAHI_CLIENT_FAILURE:
		if (avahi_client_errno(client) == AVAHI_ERR_DISCONNECTED)
		{
			int error;

			avahi_client_free(ctx.client);
			ctx.client = NULL;
			ctx.group = NULL;

			/* Reconnect to the server */
			ctx.client = avahi_client_new(
					avahi_threaded_poll_get(ctx.threaded_poll),
					AVAHI_CLIENT_NO_FAIL, client_callback,
					&ctx, &error);
			if (!ctx.client)
			{
				DPRINTF(E_ERROR, L_SSDP,
					"Failed to contact server: %s\n",
					avahi_strerror(error));
				avahi_threaded_poll_quit(ctx.threaded_poll);
			}
		}
		else
		{
			DPRINTF(E_ERROR, L_SSDP, "Client failure: %s\n",
				avahi_strerror(avahi_client_errno(client)));
			avahi_threaded_poll_quit(ctx.threaded_poll);
		}
		break;
	case AVAHI_CLIENT_CONNECTING:
	default:
		break;
	}
}
Exemplo n.º 14
0
void AvahiSession::CreateServices(AvahiClient* client)
{
	int err;

	if (mGroup) {
		avahi_entry_group_reset(mGroup);
	} else {
		mGroup = avahi_entry_group_new(client, group_cb, this);
		if (!mGroup) {
			scprintf("Zeroconf: failed to create entry group: %s\n",
					 avahi_strerror(avahi_client_errno(client)));
			return;
		}
	}

	mMutex.lock();
	AvahiEntry* entry = mEntries;
	while (entry) {
		const char* type = SCRendezvousProtocolString(entry->mProto);
		err = avahi_entry_group_add_service(
			mGroup, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
			(AvahiPublishFlags)0, mServiceName, type,
			NULL, NULL, entry->mPort,
			NULL);
		if (err == AVAHI_ERR_COLLISION) {
			// BUG: shouldn't this actually be triggered in the entry
			//      group callback?
			RenameService();
			err = avahi_entry_group_add_service(
				mGroup, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
				(AvahiPublishFlags)0, mServiceName, type,
				NULL, NULL, entry->mPort,
				NULL);
		}
		if (err < 0) {
			scprintf("Zeroconf: failed to register service '%s': %s\n",
					 mServiceName, avahi_strerror(err));
		}
		entry = entry->mNext;
	}
	mMutex.unlock();

	if (!avahi_entry_group_is_empty(mGroup)) {
		err = avahi_entry_group_commit(mGroup);
		if (err < 0) {
			scprintf("Zeroconf: failed to commit entry group: %s\n",
					 avahi_strerror(err));
			return;
		}
	}
}
Exemplo n.º 15
0
static void
epc_service_reset (EpcService *self)
{
  if (self->group)
    {
      if (EPC_DEBUG_LEVEL (1))
        g_debug ("%s: Resetting `%s' for `%s'...",
                 G_STRLOC, self->type, self->dispatcher->priv->name);

      avahi_entry_group_reset (self->group);
    }
  else
    epc_service_run (self);
}
Exemplo n.º 16
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);
}
Exemplo n.º 17
0
static void reset_services(struct runtime_data *r) {
    struct service_data *j;

    ap_assert(r);

    for (j = r->services; j; j = j->next) {
        if (j->group)
            avahi_entry_group_reset(j->group);

        if (j->pool)
            apr_pool_clear(j->pool);

        j->chosen_name = NULL;
    }
}
Exemplo n.º 18
0
void CZeroconfAvahi::clientCallback(AvahiClient* fp_client, AvahiClientState f_state, void* fp_data)
{
  CZeroconfAvahi* p_instance = static_cast<CZeroconfAvahi*>(fp_data);

  //store our thread ID and check for shutdown -> check details in destructor
  p_instance->m_thread_id = pthread_self();

  if (p_instance->m_shutdown)
  {
    avahi_threaded_poll_quit(p_instance->mp_poll);
    return;
  }
  switch(f_state)
  {
  case AVAHI_CLIENT_S_RUNNING:
    CLog::Log(LOGDEBUG, "CZeroconfAvahi::clientCallback: client is up and running");
    p_instance->updateServices(fp_client);
    break;

  case AVAHI_CLIENT_FAILURE:
    CLog::Log(LOGINFO, "CZeroconfAvahi::clientCallback: client failure. avahi-daemon stopped? Recreating client...");
    //We were forced to disconnect from server. now free and recreate the client object
    avahi_client_free(fp_client);
    p_instance->mp_client = 0;
    //freeing the client also frees all groups and browsers, pointers are undefined afterwards, so fix that now
    for(tServiceMap::const_iterator it = p_instance->m_services.begin(); it != p_instance->m_services.end(); ++it)
    {
      it->second->mp_group = 0;
    }
    p_instance->createClient();
    break;

  case AVAHI_CLIENT_S_COLLISION:
  case AVAHI_CLIENT_S_REGISTERING:
    //HERE WE SHOULD REMOVE ALL OF OUR SERVICES AND "RESCHEDULE" them for later addition
    CLog::Log(LOGDEBUG, "CZeroconfAvahi::clientCallback: uiuui; coll or reg, anyways, resetting groups");
    for(tServiceMap::const_iterator it = p_instance->m_services.begin(); it != p_instance->m_services.end(); ++it)
    {
      if (it->second->mp_group)
        avahi_entry_group_reset(it->second->mp_group);
    }
    break;

  case AVAHI_CLIENT_CONNECTING:
    CLog::Log(LOGINFO, "CZeroconfAvahi::clientCallback: avahi server not available. But may become later...");
    break;
  }
}
Exemplo n.º 19
0
static void client_callback(AvahiClient *c, AvahiClientState state,
                            AVAHI_GCC_UNUSED void *userdata) {
  int err;

  switch (state) {
  case AVAHI_CLIENT_S_REGISTERING:
    if (group)
      avahi_entry_group_reset(group);
    break;

  case AVAHI_CLIENT_S_RUNNING:
    register_service(c);
    break;

  case AVAHI_CLIENT_FAILURE:
    err = avahi_client_errno(c);
    debug(1, "avahi: client failure: %s", avahi_strerror(err));

    if (err == AVAHI_ERR_DISCONNECTED) {
      /* We have been disconnected, so lets reconnect */
      avahi_client_free(c);
      c = NULL;
      group = NULL;

      if (!(client = avahi_client_new(avahi_threaded_poll_get(tpoll), AVAHI_CLIENT_NO_FAIL,
                                      client_callback, userdata, &err))) {
        warn("avahi: failed to create client object: %s", avahi_strerror(err));
        avahi_threaded_poll_quit(tpoll);
      }
    } else {
      warn("avahi: client failure: %s", avahi_strerror(err));
      avahi_threaded_poll_quit(tpoll);
    }
    break;

  case AVAHI_CLIENT_S_COLLISION:
    debug(2, "avahi: state is AVAHI_CLIENT_S_COLLISION...needs a rename: %s", service_name);
    break;

  case AVAHI_CLIENT_CONNECTING:
    debug(2, "avahi: received AVAHI_CLIENT_CONNECTING");
    break;

  default:
    debug(1, "avahi: unexpected and unhandled avahi client state: %d", state);
    break;
  }
}
static gboolean
create_services (DMAPMdnsPublisher * publisher, GError ** error)
{
	GSList *ptr;
	int ret;

	if (publisher->priv->entry_group == NULL) {
		publisher->priv->entry_group =
			avahi_entry_group_new (publisher->priv->client,
					       (AvahiEntryGroupCallback)
					       entry_group_cb, publisher);
		dmap_mdns_avahi_set_entry_group (publisher->
						 priv->entry_group);
	} else {
		avahi_entry_group_reset (publisher->priv->entry_group);
	}

	if (publisher->priv->entry_group == NULL) {
		g_warning ("Could not create AvahiEntryGroup for publishing");
		g_set_error (error,
			     DMAP_MDNS_PUBLISHER_ERROR,
			     DMAP_MDNS_PUBLISHER_ERROR_FAILED,
			     "%s",
			     _
			     ("Could not create AvahiEntryGroup for publishing"));
		return FALSE;
	}

	for (ptr = publisher->priv->service; ptr; ptr = g_slist_next (ptr)) {
		if (!create_service (ptr->data, publisher, error)) {
			return FALSE;
		}
	}

	ret = avahi_entry_group_commit (publisher->priv->entry_group);

	if (ret < 0) {
		g_set_error (error,
			     DMAP_MDNS_PUBLISHER_ERROR,
			     DMAP_MDNS_PUBLISHER_ERROR_FAILED,
			     "%s: %s",
			     _("Could not commit service"),
			     avahi_strerror (ret));
		return FALSE;
	}

	return TRUE;
}
Exemplo n.º 21
0
/*
 * Tries to shutdown this loop impl.
 * Call this function from inside this thread.
 */
void tivo_bonjour_unregister(void)
{
	DPRINTF(E_DEBUG, L_SSDP, "tivo_bonjour_unregister\n");

	if (ctx.group)
	{
		avahi_entry_group_reset(ctx.group);
		avahi_entry_group_free(ctx.group);
	}
	if (ctx.threaded_poll)
		avahi_threaded_poll_stop(ctx.threaded_poll);
	if (ctx.client)
		avahi_client_free(ctx.client);
	if (ctx.threaded_poll)
		avahi_threaded_poll_free(ctx.threaded_poll);
}
Exemplo n.º 22
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;
}
Exemplo n.º 23
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:
			;
	}
}
static int publish_main_service(struct userdata *u) {
    AvahiStringList *txt = NULL;
    int r = -1;

    pa_assert(u);

    if (!u->main_entry_group) {
        if (!(u->main_entry_group = avahi_entry_group_new(u->client, main_entry_group_callback, u))) {
            pa_log("avahi_entry_group_new() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
            goto fail;
        }
    } else
        avahi_entry_group_reset(u->main_entry_group);

    txt = txt_record_server_data(u->core, txt);

    if (avahi_entry_group_add_service_strlst(
                u->main_entry_group,
                AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
                0,
                u->service_name,
                SERVICE_TYPE_SERVER,
                NULL,
                NULL,
                compute_port(u),
                txt) < 0) {

        pa_log("avahi_entry_group_add_service_strlst() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
        goto fail;
    }

    if (avahi_entry_group_commit(u->main_entry_group) < 0) {
        pa_log("avahi_entry_group_commit() failed: %s", avahi_strerror(avahi_client_errno(u->client)));
        goto fail;
    }

    r = 0;

fail:
    avahi_string_list_free(txt);

    return r;
}
static void modify_callback(AVAHI_GCC_UNUSED AvahiTimeout *e, void *userdata) {
    AvahiClient *client = userdata;

    fprintf(stderr, "Doing some weird modification\n");

    avahi_free(name);
    name = avahi_strdup("Modified MegaPrinter");

    /* If the server is currently running, we need to remove our
     * service and create it anew */
    if (avahi_client_get_state(client) == AVAHI_CLIENT_S_RUNNING) {

        /* Remove the old services */
        if (group)
            avahi_entry_group_reset(group);

        /* And create them again with the new name */
        create_services(client);
    }
}
Exemplo n.º 26
0
static void
client_cb (AvahiClient         *client,
	   AvahiClientState     state,
	   gpointer             data)
{
	/* FIXME
	 * check to make sure we're in the _RUNNING state before we publish
	 * check for COLLISION state and remove published information
	 */

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

		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 (entry_group) {
			 avahi_entry_group_reset (entry_group);
		 }
		 break;

	case AVAHI_CLIENT_FAILURE:
		 g_warning ("Client failure: %s\n", avahi_strerror (avahi_client_errno (client)));
		 break;

	case AVAHI_CLIENT_CONNECTING:
	case AVAHI_CLIENT_S_REGISTERING:
	default:
		break;
	}
}
Exemplo n.º 27
0
int
mdns_register(char *name, char *type, int port, char **txt)
{
  struct mdns_group_entry *ge;
  AvahiStringList *txt_sl;
  int i;

  DPRINTF(E_DBG, L_MDNS, "Adding mDNS service %s/%s\n", name, type);

  ge = (struct mdns_group_entry *)malloc(sizeof(struct mdns_group_entry));
  if (!ge)
    return -1;

  ge->name = strdup(name);
  ge->type = strdup(type);
  ge->port = port;

  txt_sl = NULL;
  for (i = 0; txt[i]; i++)
    {
      txt_sl = avahi_string_list_add(txt_sl, txt[i]);

      DPRINTF(E_DBG, L_MDNS, "Added key %s\n", txt[i]);
    }

  ge->txt = txt_sl;

  ge->next = group_entries;
  group_entries = ge;

  if (mdns_group)
    {
      DPRINTF(E_DBG, L_MDNS, "Resetting mDNS group\n");
      avahi_entry_group_reset(mdns_group);
    }

  DPRINTF(E_DBG, L_MDNS, "Creating service group\n");
  _create_services();

  return 0;
}
Exemplo n.º 28
0
static void
mdns_register_task(void *arg)
{
  struct mdns_group_entry *ge;

  ge = (struct mdns_group_entry *)arg;

  DPRINTF(E_DBG, L_MDNS, "[ASYNC] Adding mDNS service %s/%s\n", ge->name, ge->type);

  ge->next = group_entries;
  group_entries = ge;

  if (mdns_group)
    {
      DPRINTF(E_DBG, L_MDNS, "Resetting mDNS group\n");
      avahi_entry_group_reset(mdns_group);
    }

  DPRINTF(E_DBG, L_MDNS, "Creating service group\n");
  _create_services();
}
Exemplo n.º 29
0
void avahi_reset(void) {
	int i;
if (db) fprintf(stderr, "in  avahi_reset\n");
	for (i=0; i<NREG; i++) {
		if (registered[i].name) {
			free(registered[i].name);
			registered[i].name = NULL;
		}
		if (registered[i].host) {
			free(registered[i].host);
			registered[i].host = NULL;
		}
	}
	if (!_client || !_group) {
if (db) fprintf(stderr, "    avahi_reset client/group null\n");
		return;
	}
	avahi_entry_group_reset(_group);
	rfbLog("Avahi resetting group.\n");
if (db) fprintf(stderr, "out avahi_reset\n");
}
gboolean
dmap_mdns_publisher_withdraw (DMAPMdnsPublisher * publisher,
			      guint port, GError ** error)
{
	struct DMAPMdnsPublisherService *ptr;

	if (publisher->priv->client == NULL) {
		g_set_error (error,
			     DMAP_MDNS_PUBLISHER_ERROR,
			     DMAP_MDNS_PUBLISHER_ERROR_NOT_RUNNING,
			     "%s",
			     _("The avahi MDNS service is not running"));
		return FALSE;
	}

	if (publisher->priv->entry_group == NULL
	    || !(ptr =
		 find_service_by_port (publisher->priv->service, port))) {
		g_set_error (error, DMAP_MDNS_PUBLISHER_ERROR,
			     DMAP_MDNS_PUBLISHER_ERROR_FAILED, "%s",
			     _("The MDNS service is not published"));
		return FALSE;
	}

	free_service (ptr, NULL);
	publisher->priv->service =
		g_slist_remove (publisher->priv->service, ptr);

	if (publisher->priv->service == NULL) {
		avahi_entry_group_reset (publisher->priv->entry_group);
		avahi_entry_group_free (publisher->priv->entry_group);
		publisher->priv->entry_group = NULL;
	} else {
		create_services (publisher, error);
		if (error != NULL)
			return FALSE;
	}

	return TRUE;
}