Exemplo n.º 1
0
static ServiceInfo *add_service(Config *c, AvahiIfIndex interface, AvahiProtocol protocol, const char *name, const char *type, const char *domain) {
    ServiceInfo *i;

    i = avahi_new(ServiceInfo, 1);

    if (c->resolve) {
        if (!(i->resolver = avahi_service_resolver_new(client, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, service_resolver_callback, i))) {
            avahi_free(i);
            fprintf(stderr, ("Failed to resolve service '%s' of type '%s' in domain '%s': %s\n"), name, type, domain, avahi_strerror(avahi_client_errno(client)));
            return NULL;
        }

        n_resolving++;
    } else
        i->resolver = NULL;

    i->interface = interface;
    i->protocol = protocol;
    i->name = avahi_strdup(name);
    i->type = avahi_strdup(type);
    i->domain = avahi_strdup(domain);
    i->config = c;

    AVAHI_LLIST_PREPEND(ServiceInfo, info, services, i);

    return i;
}
Exemplo n.º 2
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;
}
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) {
    AvahiServerConfig config;
    AvahiServer *server = NULL;
    int error;
    int ret = 1;

    /* Initialize the pseudo-RNG */
    srand(time(NULL));

    /* Allocate main loop object */
    if (!(simple_poll = avahi_simple_poll_new())) {
        fprintf(stderr, "Failed to create simple poll object.\n");
        goto fail;
    }

    name = avahi_strdup("MegaPrinter");

    /* Let's set the host name for this server. */
    avahi_server_config_init(&config);
    config.host_name = avahi_strdup("gurkiman");
    config.publish_workstation = 0;

    /* Allocate a new server */
    server = avahi_server_new(avahi_simple_poll_get(simple_poll), &config, server_callback, NULL, &error);

    /* Free the configuration data */
    avahi_server_config_free(&config);

    /* Check wether creating the server object succeeded */
    if (!server) {
        fprintf(stderr, "Failed to create server: %s\n", avahi_strerror(error));
        goto fail;
    }

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

    ret = 0;

fail:

    /* Cleanup things */

    if (server)
        avahi_server_free(server);

    if (simple_poll)
        avahi_simple_poll_free(simple_poll);

    avahi_free(name);

    return ret;
}
Exemplo n.º 4
0
static void
_create_services(void)
{
  struct mdns_group_entry *pentry;
  int ret;

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

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

      return;
    }

    if (mdns_group == NULL)
      {
        mdns_group = avahi_entry_group_new(mdns_client, entry_group_callback, NULL);
	if (!mdns_group)
	  {
            DPRINTF(E_WARN, L_MDNS, "Could not create Avahi EntryGroup: %s\n",
                    avahi_strerror(avahi_client_errno(mdns_client)));

            return;
	  }
      }

    pentry = group_entries;
    while (pentry)
      {
        DPRINTF(E_DBG, L_MDNS, "Re-registering %s/%s\n", pentry->name, pentry->type);

        ret = avahi_entry_group_add_service_strlst(mdns_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0,
						   avahi_strdup(pentry->name), avahi_strdup(pentry->type),
						   NULL, NULL, pentry->port, pentry->txt);
	if (ret < 0)
	  {
	    DPRINTF(E_WARN, L_MDNS, "Could not add mDNS services: %s\n", avahi_strerror(ret));

	    return;
	  }

	pentry = pentry->next;
      }

    ret = avahi_entry_group_commit(mdns_group);
    if (ret < 0)
      {
	DPRINTF(E_WARN, L_MDNS, "Could not commit mDNS services: %s\n",
		avahi_strerror(avahi_client_errno(mdns_client)));
      }
}
Exemplo n.º 5
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.º 6
0
void avahi_init_proc_title(int argc, char **argv) {

#if !defined(HAVE_SETPROCTITLE) && defined(__linux__)

    unsigned i;
    char **new_environ, *endptr;

    /* This code is really really ugly. We make some memory layout
     * assumptions and reuse the environment array as memory to store
     * our process title in */

    for (i = 0; environ[i]; i++);

    endptr = i ? environ[i-1] + strlen(environ[i-1]) : argv[argc-1] + strlen(argv[argc-1]);

    argv_buffer = argv;
    argv_size = endptr - argv_buffer[0];

    /* Make a copy of environ */
    
    new_environ = avahi_malloc(sizeof(char*) * (i + 1));
    for (i = 0; environ[i]; i++)
        new_environ[i] = avahi_strdup(environ[i]);
    new_environ[i] = NULL;

    environ = new_environ;

#endif
}	
Exemplo n.º 7
0
static char *replacestr(const char *pattern, const char *a, const char *b) {
    char *r = NULL, *e, *n;

    while ((e = strstr(pattern, a))) {
        char *k;

        k = avahi_strndup(pattern, e - pattern);
        if (r)
            n = avahi_strdup_printf("%s%s%s", r, k, b);
        else
            n = avahi_strdup_printf("%s%s", k, b);

        avahi_free(k);
        avahi_free(r);
        r = n;

        pattern = e + strlen(a);
    }

    if (!r)
        return avahi_strdup(pattern);

    n = avahi_strdup_printf("%s%s", r, pattern);
    avahi_free(r);

    return n;
}
Exemplo n.º 8
0
PublishAvahi::PublishAvahi(const std::string& serviceName) : PublishmDNS(serviceName),
	client_(NULL), active_(false)
{
	group = NULL;
	simple_poll = NULL;
	name = avahi_strdup(serviceName_.c_str());
}
Exemplo n.º 9
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.º 10
0
static char *concat_dns_servers(AvahiIfIndex interface) {
    DNSServerInfo *i;
    char *r = NULL;

    for (i = servers; i; i = i->servers_next)
        if (i->interface == interface || interface <= 0) {
            DNSServerInfo *j;
            char *t;

            /* Filter out double entries */
            for (j = servers; j != i; j = j->servers_next)
                if (j->interface == interface || interface <= 0)
                    if (strcmp(i->address, j->address) == 0)
                        break;

            if (j != i)
                continue;

            if (!r)
                t = avahi_strdup(i->address);
            else
                t = avahi_strdup_printf("%s %s", r, i->address);

            avahi_free(r);
            r = t;
        }

    return r;
}
Exemplo n.º 11
0
char *avahi_get_host_name_strdup(void) {
    char t[AVAHI_DOMAIN_NAME_MAX];

    if (!(avahi_get_host_name(t, sizeof(t))))
        return NULL;

    return avahi_strdup(t);
}
Exemplo n.º 12
0
char *avahi_normalize_name_strdup(const char *s) {
    char t[AVAHI_DOMAIN_NAME_MAX];
    assert(s);

    if (!(avahi_normalize_name(s, t, sizeof(t))))
        return NULL;

    return avahi_strdup(t);
}
ServiceInfo* NetworkServicesProviderAvahi::addServiceInfo(
    AvahiIfIndex interface, 
    AvahiProtocol protocol, 
    const char* name, 
    const char* type, 
    const char* domain) 
{
    ServiceInfo* i;

    m_currentService = i = avahi_new(ServiceInfo, 1);

    if (!(i->resolver = avahi_service_resolver_new(
        m_avahiClient,
        interface, 
        protocol, 
        name, 
        type, 
        domain, 
        AVAHI_PROTO_UNSPEC, 
        (AvahiLookupFlags)0, 
        &serviceResolverCallback, 
        static_cast<void*>(i)))) 
    {
        avahi_free(i);
        LOG_ERROR("Failed to resolve service '%s' of type '%s' in domain '%s': %s\n", 
            name, type, domain, avahi_strerror(avahi_client_errno(m_avahiClient)));
        return 0;
    }

    m_resolving++;

    i->interface = interface;
    i->protocol = protocol;
    i->name = avahi_strdup(name);
    i->type = avahi_strdup(type);
    i->domain = avahi_strdup(domain);
    i->provider = this;
    i->notify = false;

    AVAHI_LLIST_PREPEND(ServiceInfo, info, m_services, i);

    return i;
}
Exemplo n.º 14
0
Arquivo: client.c Projeto: EBone/Faust
static char* avahi_client_get_string_reply_and_block (AvahiClient *client, const char *method, const char *param) {
    DBusMessage *message = NULL, *reply = NULL;
    DBusError error;
    char *ret, *n;

    assert(client);
    assert(method);

    dbus_error_init (&error);

    if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, method))) {
        avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
        goto fail;
    }

    if (param) {
        if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &param, DBUS_TYPE_INVALID)) {
            avahi_client_set_errno (client, AVAHI_ERR_NO_MEMORY);
            goto fail;
        }
    }

    reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);

    if (!reply || dbus_error_is_set (&error))
        goto fail;

    if (!dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID) ||
        dbus_error_is_set (&error))
        goto fail;

    if (!(n = avahi_strdup(ret))) {
        avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
        goto fail;
    }

    dbus_message_unref(message);
    dbus_message_unref(reply);

    return n;

fail:

    if (message)
        dbus_message_unref(message);
    if (reply)
        dbus_message_unref(reply);

    if (dbus_error_is_set(&error)) {
        avahi_client_set_dbus_error(client, &error);
        dbus_error_free(&error);
    }

    return NULL;
}
Exemplo n.º 15
0
Avahi::PresencePublisher::PresencePublisher (Ekiga::ServiceCore& core_,
					     Ekiga::PersonalDetails& details_,
					     Ekiga::CallCore& call_core_):
  core(core_), details(details_), call_core(call_core_),
  client(NULL), group(NULL)
{
  display_name = details.get_display_name ();
  details.updated.connect (boost::bind (&Avahi::PresencePublisher::on_details_updated, this));
  name = avahi_strdup (display_name.c_str ());
  glib_poll = avahi_glib_poll_new (NULL, G_PRIORITY_DEFAULT);
  create_client ();
}
Exemplo n.º 16
0
static int register_once() {
    AvahiClient *client = NULL;
    int error;
    int ret = 1;
    struct timeval tv;

    group = NULL;
    simple_poll = NULL;
    name = NULL;

    /* Allocate main loop object */
    if (!(simple_poll = avahi_simple_poll_new())) {
        fprintf(stderr, "Failed to create simple poll object.\n");
        goto fail;
    }

    name = avahi_strdup("MegaPrinter");

    /* Allocate a new client */
    client = avahi_client_new(avahi_simple_poll_get(simple_poll), 0, client_callback, NULL, &error);

    /* Check wether creating the client object succeeded */
    if (!client) {
        fprintf(stderr, "Failed to create client: %s\n", avahi_strerror(error));
        goto fail;
    }

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

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

    ret = 0;

fail:

    /* Cleanup things */

    if (client)
        avahi_client_free(client);

    if (simple_poll)
        avahi_simple_poll_free(simple_poll);

    avahi_free(name);

    return ret;
}
Exemplo n.º 17
0
void
Avahi::PresencePublisher::on_details_updated ()
{
  if (display_name != details.get_display_name ()) {

    display_name = details.get_display_name ();
    remove_services ();
    avahi_free (name);
    name = avahi_strdup (display_name.c_str ());
    avahi_entry_group_new (client,
			   (AvahiEntryGroupCallback)entry_group_cb, this);
  }
}
Exemplo n.º 18
0
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char*argv[]) {
    AvahiClient *client = NULL;
    int error;
    int ret = 1;
    //struct timeval tv;		// Not needed due no modifications made by avahi service
								// Comes from sample code
	
    /* Allocate main loop object */
    if (!(simple_poll = avahi_simple_poll_new())) {
        fprintf(stderr, "Failed to create simple poll object.\n");
        goto fail;
    }
	
    name = avahi_strdup("gbMon2");
	
    /* Allocate a new client */
    client = avahi_client_new(avahi_simple_poll_get(simple_poll), 0, client_callback, NULL, &error);
	
    /* Check wether creating the client object succeeded */
    if (!client) {
        fprintf(stderr, "Failed to create client: %s\n", avahi_strerror(error));
        goto fail;
    }
	
    /* After 10s do some weird modification to the service */
    /*
	avahi_simple_poll_get(simple_poll)->timeout_new(
													avahi_simple_poll_get(simple_poll),
													avahi_elapse_time(&tv, 1000*10, 0),
													modify_callback,
													client);
	*/
    /* Run the main loop */
    avahi_simple_poll_loop(simple_poll);
	
    ret = 0;
	
fail:
	
    /* Cleanup things */
	
    if (client)
        avahi_client_free(client);
	
    if (simple_poll)
        avahi_simple_poll_free(simple_poll);
	
    avahi_free(name);
	
    return ret;
}
Exemplo n.º 19
0
static DNSServerInfo* new_server_info(AvahiIfIndex interface, AvahiProtocol protocol, const char *address) {
    DNSServerInfo *i;

    assert(address);

    i = avahi_new(DNSServerInfo, 1);
    i->interface = interface;
    i->protocol = protocol;
    i->address = avahi_strdup(address);

    AVAHI_LLIST_PREPEND(DNSServerInfo, servers, servers, i);

    return i;
}
Exemplo n.º 20
0
static void *
avahi_thread(void *aux)
{
  const AvahiPoll *ap = avahi_simple_poll_get(avahi_asp);

  name = avahi_strdup("Tvheadend");

  avahi_client_new(ap, AVAHI_CLIENT_NO_FAIL, client_callback, NULL, NULL);
 
  while(avahi_simple_poll_iterate(avahi_asp, -1) == 0);

  return NULL;
  

}
Exemplo n.º 21
0
static StaticServiceGroup *static_service_group_new(char *filename) {
    StaticServiceGroup *g;
    assert(filename);

    g = avahi_new(StaticServiceGroup, 1);
    g->filename = avahi_strdup(filename);
    g->mtime = 0;
    g->name = g->chosen_name = NULL;
    g->replace_wildcards = 0;
    g->entry_group = NULL;

    AVAHI_LLIST_HEAD_INIT(StaticService, g->services);
    AVAHI_LLIST_PREPEND(StaticServiceGroup, groups, groups, g);

    return g;
}
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.º 23
0
static void
register_service (void)
{
	int error;
	const AvahiPoll *poll_api;
	AvahiGLibPoll *glib_poll;
	AvahiClient *client = NULL;

	name = avahi_strdup ("XMMS2 mDNS Agent");

	glib_poll = avahi_glib_poll_new (NULL, G_PRIORITY_DEFAULT);
	poll_api = avahi_glib_poll_get (glib_poll);

	client = avahi_client_new (poll_api, 0, client_callback, NULL, &error);
	if (!client) {
		printf ("Failed to create Avahi client: %s\n", avahi_strerror (error));
		avahi_client_free (client);
		avahi_glib_poll_free (glib_poll);
	}

}
Exemplo n.º 24
0
	void CommunicationManager::run() {
		AvahiClient *client = NULL;
		int error;
		struct timeval tv;

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

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

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

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

		if (simplePoll) {
			avahi_simple_poll_free(simplePoll);
		}

		avahi_free(name);
	}
Exemplo n.º 25
0
int launchRegisterService(const char* nameService) {

	printf("Name of Service to add = %s\n", nameService);

    //AvahiClient *client = NULL;
    int error;
    int ret = 1;
    struct timeval tv;

    /* Allocate main loop object */
    if (!(simple_poll = avahi_simple_poll_new())) {
        fprintf(stderr, "Failed to create simple poll object.\n");
        return 0;
    }

//	char host_name[256];
//	gethostname(host_name, sizeof(host_name));

	name = avahi_strdup(nameService);

    /* Allocate a new client */
    client = avahi_client_new(avahi_simple_poll_get(simple_poll), (AvahiClientFlags)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));
        return 0;
    }


	//pthread_t myNewThread;
	if(!pthread_create(&myNewThread, NULL, registerService, simple_poll))
		printf("thread was created\n");
	else
		printf("New thread not created\n");

	return 1;
}
Exemplo n.º 26
0
Arquivo: avahi.c Projeto: cjd/lyricue
int publish_avahi(int port_passed, char *type_in_passed) {
    int error;
    AvahiGLibPoll *glib_poll;
    port = port_passed;
    type_in = strndup(type_in_passed,32);

    avahi_set_allocator (avahi_glib_allocator ());

    /* Create the GLIB Adaptor */
    glib_poll = avahi_glib_poll_new (NULL, G_PRIORITY_DEFAULT);

    name = avahi_strdup("Lyricue Display");

    /* Allocate a new client */
    client = avahi_client_new(avahi_glib_poll_get(glib_poll), 0, client_callback, NULL, &error);

    /* Check if creating the client object succeeded */
    if (!client) {
        l_debug("Failed to create client: %s", avahi_strerror(error));
        unpublish_avahi();
    }

    
    /* Create the service browser */
    if (miniviews==NULL) {
        miniviews = g_hash_table_new(g_str_hash, g_str_equal);
    }
    if (sb == NULL) {
        if (!(sb = avahi_service_browser_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_INET, "_lyricue._tcp", NULL, 0, browse_callback, client))) {
            l_debug("Failed to create service browser: %s\n", avahi_strerror(avahi_client_errno(client)));
        }
    }


    return 0;
}
Exemplo n.º 27
0
static int load_config_file(DaemonConfig *c) {
    int r = -1;
    AvahiIniFile *f;
    AvahiIniFileGroup *g;

    assert(c);

    if (!(f = avahi_ini_file_load(c->config_file ? c->config_file : AVAHI_CONFIG_FILE)))
        goto finish;

    for (g = f->groups; g; g = g->groups_next) {

        if (strcasecmp(g->name, "server") == 0) {
            AvahiIniFilePair *p;

            for (p = g->pairs; p; p = p->pairs_next) {

                if (strcasecmp(p->key, "host-name") == 0) {
                    avahi_free(c->server_config.host_name);
                    c->server_config.host_name = avahi_strdup(p->value);
                } else if (strcasecmp(p->key, "domain-name") == 0) {
                    avahi_free(c->server_config.domain_name);
                    c->server_config.domain_name = avahi_strdup(p->value);
                } else if (strcasecmp(p->key, "browse-domains") == 0) {
                    char **e, **t;

                    e = avahi_split_csv(p->value);

                    for (t = e; *t; t++) {
                        char cleaned[AVAHI_DOMAIN_NAME_MAX];

                        if (!avahi_normalize_name(*t, cleaned, sizeof(cleaned))) {
                            avahi_log_error("Invalid domain name \"%s\" for key \"%s\" in group \"%s\"\n", *t, p->key, g->name);
                            avahi_strfreev(e);
                            goto finish;
                        }

                        c->server_config.browse_domains = avahi_string_list_add(c->server_config.browse_domains, cleaned);
                    }

                    avahi_strfreev(e);

                    c->server_config.browse_domains = filter_duplicate_domains(c->server_config.browse_domains);
                } else if (strcasecmp(p->key, "use-ipv4") == 0)
                    c->server_config.use_ipv4 = is_yes(p->value);
                else if (strcasecmp(p->key, "use-ipv6") == 0)
                    c->server_config.use_ipv6 = is_yes(p->value);
                else if (strcasecmp(p->key, "check-response-ttl") == 0)
                    c->server_config.check_response_ttl = is_yes(p->value);
                else if (strcasecmp(p->key, "allow-point-to-point") == 0)
                    c->server_config.allow_point_to_point = is_yes(p->value);
                else if (strcasecmp(p->key, "use-iff-running") == 0)
                    c->server_config.use_iff_running = is_yes(p->value);
                else if (strcasecmp(p->key, "disallow-other-stacks") == 0)
                    c->server_config.disallow_other_stacks = is_yes(p->value);
                else if (strcasecmp(p->key, "host-name-from-machine-id") == 0) {
                    if (*(p->value) == 'y' || *(p->value) == 'Y') {
                        char *machine_id = get_machine_id();
                        if (machine_id != NULL) {
                            avahi_free(c->server_config.host_name);
                            c->server_config.host_name = machine_id;
                        }
                    }
                }
#ifdef HAVE_DBUS
                else if (strcasecmp(p->key, "enable-dbus") == 0) {

                    if (*(p->value) == 'w' || *(p->value) == 'W') {
                        c->fail_on_missing_dbus = 0;
                        c->enable_dbus = 1;
                    } else if (*(p->value) == 'y' || *(p->value) == 'Y') {
                        c->fail_on_missing_dbus = 1;
                        c->enable_dbus = 1;
                    } else {
                        c->enable_dbus = 0;
                    }
                }
#endif
                else if (strcasecmp(p->key, "allow-interfaces") == 0) {
                    char **e, **t;

                    avahi_string_list_free(c->server_config.allow_interfaces);
                    c->server_config.allow_interfaces = NULL;
                    e = avahi_split_csv(p->value);

                    for (t = e; *t; t++)
                        c->server_config.allow_interfaces = avahi_string_list_add(c->server_config.allow_interfaces, *t);

                    avahi_strfreev(e);
                } else if (strcasecmp(p->key, "deny-interfaces") == 0) {
                    char **e, **t;

                    avahi_string_list_free(c->server_config.deny_interfaces);
                    c->server_config.deny_interfaces = NULL;
                    e = avahi_split_csv(p->value);

                    for (t = e; *t; t++)
                        c->server_config.deny_interfaces = avahi_string_list_add(c->server_config.deny_interfaces, *t);

                    avahi_strfreev(e);
                } else if (strcasecmp(p->key, "ratelimit-interval-usec") == 0) {
                    AvahiUsec k;

                    if (parse_usec(p->value, &k) < 0) {
                        avahi_log_error("Invalid ratelimit-interval-usec setting %s", p->value);
                        goto finish;
                    }

                    c->server_config.ratelimit_interval = k;

                } else if (strcasecmp(p->key, "ratelimit-burst") == 0) {
                    unsigned k;

                    if (parse_unsigned(p->value, &k) < 0) {
                        avahi_log_error("Invalid ratelimit-burst setting %s", p->value);
                        goto finish;
                    }

                    c->server_config.ratelimit_burst = k;

                } else if (strcasecmp(p->key, "cache-entries-max") == 0) {
                    unsigned k;

                    if (parse_unsigned(p->value, &k) < 0) {
                        avahi_log_error("Invalid cache-entries-max setting %s", p->value);
                        goto finish;
                    }

                    c->server_config.n_cache_entries_max = k;
#ifdef HAVE_DBUS
                } else if (strcasecmp(p->key, "clients-max") == 0) {
                    unsigned k;

                    if (parse_unsigned(p->value, &k) < 0) {
                        avahi_log_error("Invalid clients-max setting %s", p->value);
                        goto finish;
                    }

                    c->n_clients_max = k;
                } else if (strcasecmp(p->key, "objects-per-client-max") == 0) {
                    unsigned k;

                    if (parse_unsigned(p->value, &k) < 0) {
                        avahi_log_error("Invalid objects-per-client-max setting %s", p->value);
                        goto finish;
                    }

                    c->n_objects_per_client_max = k;
                } else if (strcasecmp(p->key, "entries-per-entry-group-max") == 0) {
                    unsigned k;

                    if (parse_unsigned(p->value, &k) < 0) {
                        avahi_log_error("Invalid entries-per-entry-group-max setting %s", p->value);
                        goto finish;
                    }

                    c->n_entries_per_entry_group_max = k;
#endif
                } else {
                    avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
                    goto finish;
                }
            }

        } else if (strcasecmp(g->name, "publish") == 0) {
            AvahiIniFilePair *p;

            for (p = g->pairs; p; p = p->pairs_next) {

                if (strcasecmp(p->key, "publish-addresses") == 0)
                    c->server_config.publish_addresses = is_yes(p->value);
                else if (strcasecmp(p->key, "publish-hinfo") == 0)
                    c->server_config.publish_hinfo = is_yes(p->value);
                else if (strcasecmp(p->key, "publish-workstation") == 0)
                    c->server_config.publish_workstation = is_yes(p->value);
                else if (strcasecmp(p->key, "publish-domain") == 0)
                    c->server_config.publish_domain = is_yes(p->value);
                else if (strcasecmp(p->key, "publish-resolv-conf-dns-servers") == 0)
                    c->publish_resolv_conf = is_yes(p->value);
                else if (strcasecmp(p->key, "disable-publishing") == 0)
                    c->server_config.disable_publishing = is_yes(p->value);
                else if (strcasecmp(p->key, "disable-user-service-publishing") == 0)
                    c->disable_user_service_publishing = is_yes(p->value);
                else if (strcasecmp(p->key, "add-service-cookie") == 0)
                    c->server_config.add_service_cookie = is_yes(p->value);
                else if (strcasecmp(p->key, "publish-dns-servers") == 0) {
                    avahi_strfreev(c->publish_dns_servers);
                    c->publish_dns_servers = avahi_split_csv(p->value);
                } else if (strcasecmp(p->key, "publish-a-on-ipv6") == 0)
                    c->server_config.publish_a_on_ipv6 = is_yes(p->value);
                else if (strcasecmp(p->key, "publish-aaaa-on-ipv4") == 0)
                    c->server_config.publish_aaaa_on_ipv4 = is_yes(p->value);
                else {
                    avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
                    goto finish;
                }
            }

        } else if (strcasecmp(g->name, "wide-area") == 0) {
            AvahiIniFilePair *p;

            for (p = g->pairs; p; p = p->pairs_next) {

                if (strcasecmp(p->key, "enable-wide-area") == 0)
                    c->server_config.enable_wide_area = is_yes(p->value);
                else {
                    avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
                    goto finish;
                }
            }

        } else if (strcasecmp(g->name, "reflector") == 0) {
            AvahiIniFilePair *p;

            for (p = g->pairs; p; p = p->pairs_next) {

                if (strcasecmp(p->key, "enable-reflector") == 0)
                    c->server_config.enable_reflector = is_yes(p->value);
                else if (strcasecmp(p->key, "reflect-ipv") == 0)
                    c->server_config.reflect_ipv = is_yes(p->value);
                else {
                    avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
                    goto finish;
                }
            }

        } else if (strcasecmp(g->name, "rlimits") == 0) {
            AvahiIniFilePair *p;

            for (p = g->pairs; p; p = p->pairs_next) {

                if (strcasecmp(p->key, "rlimit-as") == 0) {
                    c->rlimit_as_set = 1;
                    c->rlimit_as = atoi(p->value);
                } else if (strcasecmp(p->key, "rlimit-core") == 0) {
                    c->rlimit_core_set = 1;
                    c->rlimit_core = atoi(p->value);
                } else if (strcasecmp(p->key, "rlimit-data") == 0) {
                    c->rlimit_data_set = 1;
                    c->rlimit_data = atoi(p->value);
                } else if (strcasecmp(p->key, "rlimit-fsize") == 0) {
                    c->rlimit_fsize_set = 1;
                    c->rlimit_fsize = atoi(p->value);
                } else if (strcasecmp(p->key, "rlimit-nofile") == 0) {
                    c->rlimit_nofile_set = 1;
                    c->rlimit_nofile = atoi(p->value);
                } else if (strcasecmp(p->key, "rlimit-stack") == 0) {
                    c->rlimit_stack_set = 1;
                    c->rlimit_stack = atoi(p->value);
                } else if (strcasecmp(p->key, "rlimit-nproc") == 0) {
#ifdef RLIMIT_NPROC
                    c->rlimit_nproc_set = 1;
                    c->rlimit_nproc = atoi(p->value);
#else
                    avahi_log_error("Ignoring configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
#endif
                } else {
                    avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
                    goto finish;
                }

            }

        } else {
            avahi_log_error("Invalid configuration file group \"%s\".\n", g->name);
            goto finish;
        }
    }

    r = 0;

finish:

    if (f)
        avahi_ini_file_free(f);

    return r;
}
Exemplo n.º 28
0
static int parse_command_line(DaemonConfig *c, int argc, char *argv[]) {
    int o;

    enum {
        OPTION_NO_RLIMITS = 256,
        OPTION_NO_DROP_ROOT,
#ifdef ENABLE_CHROOT
        OPTION_NO_CHROOT,
#endif
        OPTION_NO_PROC_TITLE,
        OPTION_DEBUG
    };

    static const struct option long_options[] = {
        { "help",           no_argument,       NULL, 'h' },
        { "daemonize",      no_argument,       NULL, 'D' },
        { "kill",           no_argument,       NULL, 'k' },
        { "version",        no_argument,       NULL, 'V' },
        { "file",           required_argument, NULL, 'f' },
        { "reload",         no_argument,       NULL, 'r' },
        { "check",          no_argument,       NULL, 'c' },
        { "syslog",         no_argument,       NULL, 's' },
        { "no-rlimits",     no_argument,       NULL, OPTION_NO_RLIMITS },
        { "no-drop-root",   no_argument,       NULL, OPTION_NO_DROP_ROOT },
#ifdef ENABLE_CHROOT
        { "no-chroot",      no_argument,       NULL, OPTION_NO_CHROOT },
#endif
        { "no-proc-title",  no_argument,       NULL, OPTION_NO_PROC_TITLE },
        { "debug",          no_argument,       NULL, OPTION_DEBUG },
        { NULL, 0, NULL, 0 }
    };

    assert(c);

    while ((o = getopt_long(argc, argv, "hDkVf:rcs", long_options, NULL)) >= 0) {

        switch(o) {
            case 's':
                c->use_syslog = 1;
                break;
            case 'h':
                c->command = DAEMON_HELP;
                break;
            case 'D':
                c->daemonize = 1;
                break;
            case 'k':
                c->command = DAEMON_KILL;
                break;
            case 'V':
                c->command = DAEMON_VERSION;
                break;
            case 'f':
                avahi_free(c->config_file);
                c->config_file = avahi_strdup(optarg);
                break;
            case 'r':
                c->command = DAEMON_RELOAD;
                break;
            case 'c':
                c->command = DAEMON_CHECK;
                break;
            case OPTION_NO_RLIMITS:
                c->set_rlimits = 0;
                break;
            case OPTION_NO_DROP_ROOT:
                c->drop_root = 0;
                break;
#ifdef ENABLE_CHROOT
            case OPTION_NO_CHROOT:
                c->use_chroot = 0;
                break;
#endif
            case OPTION_NO_PROC_TITLE:
                c->modify_proc_title = 0;
                break;
            case OPTION_DEBUG:
                c->debug = 1;
                break;
            default:
                return -1;
        }
    }

    if (optind < argc) {
        fprintf(stderr, "Too many arguments\n");
        return -1;
    }

    return 0;
}
Exemplo n.º 29
0
static int load_resolv_conf(void) {
    int ret = -1;
    FILE *f;
    int i = 0, j = 0;

    avahi_strfreev(resolv_conf_name_servers);
    resolv_conf_name_servers = NULL;

    avahi_strfreev(resolv_conf_search_domains);
    resolv_conf_search_domains = NULL;

#ifdef ENABLE_CHROOT
    f = avahi_chroot_helper_get_file(RESOLV_CONF);
#else
    f = fopen(RESOLV_CONF, "r");
#endif

    if (!f) {
        avahi_log_warn("Failed to open "RESOLV_CONF": %s", strerror(errno));
        goto finish;
    }

    resolv_conf_name_servers = avahi_new0(char*, AVAHI_WIDE_AREA_SERVERS_MAX+1);
    resolv_conf_search_domains = avahi_new0(char*, BROWSE_DOMAINS_MAX+1);

    while (!feof(f)) {
        char ln[128];
        char *p;

        if (!(fgets(ln, sizeof(ln), f)))
            break;

        ln[strcspn(ln, "\r\n#")] = 0;
        p = ln + strspn(ln, "\t ");

        if ((has_prefix(p, "nameserver ") || has_prefix(p, "nameserver\t")) && i < AVAHI_WIDE_AREA_SERVERS_MAX) {
            p += 10;
            p += strspn(p, "\t ");
            p[strcspn(p, "\t ")] = 0;
            resolv_conf_name_servers[i++] = avahi_strdup(p);
        }

        if ((has_prefix(p, "search ") || has_prefix(p, "search\t") ||
             has_prefix(p, "domain ") || has_prefix(p, "domain\t"))) {

            p += 6;

            while (j < BROWSE_DOMAINS_MAX) {
                size_t k;

                p += strspn(p, "\t ");
                k = strcspn(p, "\t ");

                if (k > 0) {
                    resolv_conf_search_domains[j++] = avahi_strndup(p, k);
                    p += k;
                }

                if (!*p)
                    break;
            }
        }
    }

    ret = 0;

finish:

    if (ret != 0) {
        avahi_strfreev(resolv_conf_name_servers);
        resolv_conf_name_servers = NULL;

        avahi_strfreev(resolv_conf_search_domains);
        resolv_conf_search_domains = NULL;
    }

    if (f)
        fclose(f);

    return ret;
}
Exemplo n.º 30
0
int main(int argc, char *argv[]) {
    int r = 255;
    int wrote_pid_file = 0;

    avahi_set_log_function(log_function);

    init_rand_seed();

    avahi_server_config_init(&config.server_config);
    config.command = DAEMON_RUN;
    config.daemonize = 0;
    config.config_file = NULL;
#ifdef HAVE_DBUS
    config.enable_dbus = 1;
    config.fail_on_missing_dbus = 1;
    config.n_clients_max = 0;
    config.n_objects_per_client_max = 0;
    config.n_entries_per_entry_group_max = 0;
#endif

    config.drop_root = 1;
    config.set_rlimits = 1;
#ifdef ENABLE_CHROOT
    config.use_chroot = 1;
#endif
    config.modify_proc_title = 1;

    config.disable_user_service_publishing = 0;
    config.publish_dns_servers = NULL;
    config.publish_resolv_conf = 0;
    config.use_syslog = 0;
    config.debug = 0;
    config.rlimit_as_set = 0;
    config.rlimit_core_set = 0;
    config.rlimit_data_set = 0;
    config.rlimit_fsize_set = 0;
    config.rlimit_nofile_set = 0;
    config.rlimit_stack_set = 0;
#ifdef RLIMIT_NPROC
    config.rlimit_nproc_set = 0;
#endif

    if ((argv0 = strrchr(argv[0], '/')))
        argv0 = avahi_strdup(argv0 + 1);
    else
        argv0 = avahi_strdup(argv[0]);

    daemon_pid_file_ident = (const char *) argv0;
    daemon_log_ident = (char*) argv0;
    daemon_pid_file_proc = pid_file_proc;

    if (parse_command_line(&config, argc, argv) < 0)
        goto finish;

    if (config.modify_proc_title)
        avahi_init_proc_title(argc, argv);

#ifdef ENABLE_CHROOT
    config.use_chroot = config.use_chroot && config.drop_root;
#endif

    if (config.command == DAEMON_HELP) {
        help(stdout);
        r = 0;
    } else if (config.command == DAEMON_VERSION) {
        printf("%s "PACKAGE_VERSION"\n", argv0);
        r = 0;
    } else if (config.command == DAEMON_KILL) {
        if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
            avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
            goto finish;
        }

        r = 0;

    } else if (config.command == DAEMON_RELOAD) {
        if (daemon_pid_file_kill(SIGHUP) < 0) {
            avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
            goto finish;
        }

        r = 0;

    } else if (config.command == DAEMON_CHECK)
        r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
    else if (config.command == DAEMON_RUN) {
        pid_t pid;

        if (getuid() != 0 && config.drop_root) {
            avahi_log_error("This program is intended to be run as root.");
            goto finish;
        }

        if ((pid = daemon_pid_file_is_running()) >= 0) {
            avahi_log_error("Daemon already running on PID %u", pid);
            goto finish;
        }

        if (load_config_file(&config) < 0)
            goto finish;

        if (config.daemonize) {
            daemon_retval_init();

            if ((pid = daemon_fork()) < 0)
                goto finish;
            else if (pid != 0) {
                int ret;
                /** Parent **/

                if ((ret = daemon_retval_wait(20)) < 0) {
                    avahi_log_error("Could not receive return value from daemon process.");
                    goto finish;
                }

                r = ret;
                goto finish;
            }

            /* Child */
        }

        if (config.use_syslog || config.daemonize)
            daemon_log_use = DAEMON_LOG_SYSLOG;

        if (sd_listen_fds(0) <= 0)
            if (daemon_close_all(-1) < 0)
                avahi_log_warn("Failed to close all remaining file descriptors: %s", strerror(errno));

        daemon_reset_sigs(-1);
        daemon_unblock_sigs(-1);

        if (make_runtime_dir() < 0)
            goto finish;

        if (config.drop_root) {
#ifdef ENABLE_CHROOT
            if (config.use_chroot)
                if (avahi_caps_reduce() < 0)
                    goto finish;
#endif

            if (drop_root() < 0)
                goto finish;

#ifdef ENABLE_CHROOT
            if (config.use_chroot)
                if (avahi_caps_reduce2() < 0)
                    goto finish;
#endif
        }

        if (daemon_pid_file_create() < 0) {
            avahi_log_error("Failed to create PID file: %s", strerror(errno));

            if (config.daemonize)
                daemon_retval_send(1);
            goto finish;
        } else
            wrote_pid_file = 1;

        if (config.set_rlimits)
            enforce_rlimits();

        chdir("/");

#ifdef ENABLE_CHROOT
        if (config.drop_root && config.use_chroot)
            if (avahi_chroot_helper_start(argv0) < 0) {
                avahi_log_error("failed to start chroot() helper daemon.");
                goto finish;
            }
#endif
        avahi_log_info("%s "PACKAGE_VERSION" starting up.", argv0);
        sd_notifyf(0, "STATUS=%s "PACKAGE_VERSION" starting up.", argv0);
        avahi_set_proc_title(argv0, "%s: starting up", argv0);

        if (run_server(&config) == 0)
            r = 0;

        avahi_log_info("%s "PACKAGE_VERSION" exiting.", argv0);
        sd_notifyf(0, "STATUS=%s "PACKAGE_VERSION" exiting.", argv0);
    }

finish:

    if (config.daemonize)
        daemon_retval_done();

    avahi_server_config_free(&config.server_config);
    avahi_free(config.config_file);
    avahi_strfreev(config.publish_dns_servers);
    avahi_strfreev(resolv_conf_name_servers);
    avahi_strfreev(resolv_conf_search_domains);

    if (wrote_pid_file) {
#ifdef ENABLE_CHROOT
        avahi_chroot_helper_unlink(pid_file_proc());
#else
        daemon_pid_file_remove();
#endif
    }

#ifdef ENABLE_CHROOT
    avahi_chroot_helper_shutdown();
#endif

    avahi_free(argv0);

    return r;
}