AvahiStringList	*AvahiServiceSubset::stringlist(const ServiceSubset& s) {
	AvahiStringList	*strlist = NULL;
	if (s.has(ServiceSubset::INSTRUMENTS)) {
		strlist = avahi_string_list_add(strlist, "instruments");
	}
	if (s.has(ServiceSubset::TASKS)) {
		strlist = avahi_string_list_add(strlist, "tasks");
	}
	if (s.has(ServiceSubset::GUIDING)) {
		strlist = avahi_string_list_add(strlist, "guiding");
	}
	if (s.has(ServiceSubset::IMAGES)) {
		strlist = avahi_string_list_add(strlist, "images");
	}
	return strlist;
}
Exemple #2
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++;
}
void NetworkServicesProviderAvahi::browseServiceType(const char* stype, const char* domain)
{
    AvahiServiceBrowser* b;
    AvahiStringList* i;

    ASSERT(stype);

    for (i = m_browsedTypes; i; i = i->next)
        if (avahi_domain_equal(stype, (char*) i->text))
            return;
    
    if (!(b = avahi_service_browser_new(
        m_avahiClient, 
        AVAHI_IF_UNSPEC, 
        AVAHI_PROTO_UNSPEC,
        stype, 
        domain, 
        (AvahiLookupFlags)0, 
        serviceBrowserCallback, 
        static_cast<void*>(this)))) 
    {
        LOG_ERROR("avahi_service_browser_new() failed: %s\n", avahi_strerror(avahi_client_errno(m_avahiClient)));
    }

    m_browsedTypes = avahi_string_list_add(m_browsedTypes, stype);

    m_allForNow++;
}
Exemple #4
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;

  dispatch_async_f(mdns_sq, ge, mdns_register_task);

  return 0;
}
JNIEXPORT jint JNICALL Java_avahi4j_EntryGroup_update_1service_1txt_1records
			(JNIEnv *e, jobject t, jlong ptr, jint interfaceNum, jint proto,
					jstring name, jstring type, jstring domain, jarray txtRecord,
					jint length){
	dprint("[LOG] Entering %s\n", __PRETTY_FUNCTION__);

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

	AvahiIfIndex avahi_if;
	AvahiProtocol avahi_proto;
	const char *avahi_name=NULL, *avahi_type=NULL, *avahi_domain=NULL;
	AvahiStringList *list = NULL;
	jstring current;
	const char* record;
	int result, i;

	// translate the Protocol Enum and interface index
	GET_AVAHI_PROTO(avahi_proto, proto);
	GET_AVAHI_IF_IDX(avahi_if, interfaceNum);

	// go through the list of TXT  record and add them to the AvahiStringList
	for(i=0; i<length; i++) {
		current = (jstring) (*e)->GetObjectArrayElement(e, txtRecord, i);
		if(current==NULL)
		{
			dprint("error getting txt record %d\n",i);
			THROW_EXCEPTION(e, JNI_EXCP, "error getting record %d",i);
			return 0;
		}

		GET_UTF_STR(record, current, e, 0);
		dprint("Adding '%s' to TXT record\n", record);
		list = avahi_string_list_add(list, record);
		PUT_UTF_STR(record, current, e);
	}

	// get UTF string from name, type, domain (can be NULL)
	GET_UTF_STR_JUMP(avahi_name, name, e, bail);
	GET_UTF_STR_JUMP(avahi_type, type,e ,bail);
	GET_UTF_STR_JUMP(avahi_domain, domain, e, bail);

	result = avahi_entry_group_update_service_txt_strlst(group->group, avahi_if,
			avahi_proto, 0, avahi_name, avahi_type, avahi_domain, list);

bail:
	// free UTF string
	PUT_UTF_STR(avahi_name, name, e);
	PUT_UTF_STR(avahi_type, type,e);
	PUT_UTF_STR(avahi_domain, domain, e);

	// free string list
	avahi_string_list_free(list);

	CHECK_N_RET(avahi_entry_group_update_service_txt_strlst, result);
}
static gboolean
create_service (struct DMAPMdnsPublisherService *service,
		DMAPMdnsPublisher * publisher, GError ** error)
{
	int ret;
	const char *password_record;
	AvahiStringList *txt_records;

	if (service->password_required) {
		password_record = "Password=true";
	} else {
		password_record = "Password=false";
	}

	txt_records = avahi_string_list_new (password_record, NULL);

	if (service->txt_records) {
		//g_debug("Number of txt records: %d", service->txt_records);
		gchar **txt_record = service->txt_records;

		while (*txt_record) {
			txt_records =
				avahi_string_list_add (txt_records,
						       *txt_record);
			txt_record++;
		}
	}

	ret = avahi_entry_group_add_service_strlst (publisher->
						    priv->entry_group,
						    AVAHI_IF_UNSPEC,
						    AVAHI_PROTO_UNSPEC, 0,
						    service->name,
						    service->type_of_service,
						    NULL, NULL, service->port,
						    txt_records);

	avahi_string_list_free (txt_records);

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

	return TRUE;
}
Exemple #7
0
static void parse_environment(AvahiDomainBrowser *b) {
    char buf[AVAHI_DOMAIN_NAME_MAX*3], *e, *t, *p;

    assert(b);

    if (!(e = getenv("AVAHI_BROWSE_DOMAINS")))
        return;

    snprintf(buf, sizeof(buf), "%s", e);

    for (t = strtok_r(buf, ":", &p); t; t = strtok_r(NULL, ":", &p)) {
        char domain[AVAHI_DOMAIN_NAME_MAX];
        if (avahi_normalize_name(t, domain, sizeof(domain)))
            b->static_browse_domains = avahi_string_list_add(b->static_browse_domains, domain);
    }
}
Exemple #8
0
static void parse_domain_file(AvahiDomainBrowser *b) {
    FILE *f;
    char buf[AVAHI_DOMAIN_NAME_MAX];

    assert(b);

    if (!(f = avahi_xdg_config_open("avahi/browse-domains")))
        return;


    while (fgets(buf, sizeof(buf)-1, f)) {
        char domain[AVAHI_DOMAIN_NAME_MAX];
        buf[strcspn(buf, "\n\r")] = 0;

        if (avahi_normalize_name(buf, domain, sizeof(domain)))
            b->static_browse_domains = avahi_string_list_add(b->static_browse_domains, domain);
    }
}
Exemple #9
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;
}
Exemple #10
0
int
mdns_register(char *name, char *type, int port, char **txt)
{
  struct mdns_group_entry *ge;
  AvahiStringList *txt_sl;
  int i;

  ge = calloc(1, sizeof(struct mdns_group_entry));
  if (!ge)
    {
      DPRINTF(E_LOG, L_MDNS, "Out of memory for mdns register\n");
      return -1;
    }

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

  txt_sl = NULL;
  if (txt)
    {
      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;

  create_all_group_entries(); // TODO why is this required?

  return 0;
}
Exemple #11
0
static void update_browse_domains(void) {
    AvahiStringList *l;
    int n;
    char **p;

    if (!resolv_conf_search_domains) {
        avahi_server_set_browse_domains(avahi_server, NULL);
        return;
    }

    l = avahi_string_list_copy(config.server_config.browse_domains);

    for (p = resolv_conf_search_domains, n = 0; *p && n < BROWSE_DOMAINS_MAX; p++, n++) {
        if (!avahi_is_valid_domain_name(*p))
            avahi_log_warn("'%s' is no valid domain name, ignoring.", *p);
        else
            l = avahi_string_list_add(l, *p);
    }

    l = filter_duplicate_domains(l);

    avahi_server_set_browse_domains(avahi_server, l);
}
Exemple #12
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;
}
static int parse_command_line(Config *c, const char *argv0, int argc, char *argv[]) {
    int o;

    enum {
        ARG_SUBTYPE = 256
    };
    
    static const struct option long_options[] = {
        { "help",           no_argument,       NULL, 'h' },
        { "version",        no_argument,       NULL, 'V' },
        { "service",        no_argument,       NULL, 's' },
        { "address",        no_argument,       NULL, 'a' },
        { "verbose",        no_argument,       NULL, 'v' },
        { "domain",         required_argument, NULL, 'd' },
        { "host",           required_argument, NULL, 'H' },
        { "subtype",        required_argument, NULL, ARG_SUBTYPE},
        { "no-fail",        no_argument,       NULL, 'f' },
        { NULL, 0, NULL, 0 }
    };

    assert(c);

    c->command = strstr(argv0, "address") ? COMMAND_PUBLISH_ADDRESS : (strstr(argv0, "service") ? COMMAND_PUBLISH_SERVICE : COMMAND_UNSPEC);
    c->verbose = c->no_fail = 0;
    c->host = c->name = c->domain = c->stype = NULL;
    c->port = 0;
    c->txt = c->subtypes = NULL;

    opterr = 0;
    while ((o = getopt_long(argc, argv, "hVsavd:H:f", long_options, NULL)) >= 0) {

        switch(o) {
            case 'h':
                c->command = COMMAND_HELP;
                break;
            case 'V':
                c->command = COMMAND_VERSION;
                break;
            case 's':
                c->command = COMMAND_PUBLISH_SERVICE;
                break;
            case 'a':
                c->command = COMMAND_PUBLISH_ADDRESS;
                break;
            case 'v':
                c->verbose = 1;
                break;
            case 'd':
                avahi_free(c->domain);
                c->domain = avahi_strdup(optarg);
                break;
            case 'H':
                avahi_free(c->host);
                c->host = avahi_strdup(optarg);
                break;
            case 'f':
                c->no_fail = 1;
                break;
            case ARG_SUBTYPE:
                c->subtypes = avahi_string_list_add(c->subtypes, optarg);
                break;
            default:
                fprintf(stderr, "Invalid command line argument: %c\n", o);
                return -1;
        }
    }

    if (c->command == COMMAND_PUBLISH_ADDRESS) {
        if (optind+2 !=  argc) {
            fprintf(stderr, "Bad number of arguments\n");
            return -1;
        }

        avahi_free(c->name);
        c->name = avahi_strdup(argv[optind]);
        avahi_address_parse(argv[optind+1], AVAHI_PROTO_UNSPEC, &c->address);
        
    } else if (c->command == COMMAND_PUBLISH_SERVICE) {

        char *e;
        long int p;
        int i;
        
        if (optind+3 > argc) {
            fprintf(stderr, "Bad number of arguments\n");
            return -1;
        }

        c->name = avahi_strdup(argv[optind]);
        c->stype = avahi_strdup(argv[optind+1]);

        errno = 0;
        p = strtol(argv[optind+2], &e, 0);

        if (errno != 0 || *e || p < 0 || p > 0xFFFF) {
            fprintf(stderr, "Failed to parse port number: %s\n", argv[optind+2]);
            return -1;
        }

        c->port = p;
            
        for (i = optind+3; i < argc; i++)
            c->txt = avahi_string_list_add(c->txt, argv[i]);
    }
        
    return 0;
}
Exemple #14
0
static void XMLCALL xml_end(void *data, AVAHI_GCC_UNUSED const char *el) {
    struct xml_userdata *u = data;
    assert(u);

    if (u->failed)
        return;

    switch (u->current_tag) {
        case XML_TAG_SERVICE_GROUP:

            if (!u->group->name || !u->group->services) {
                avahi_log_error("%s: parse failure: service group incomplete.", u->group->filename);
                u->failed = 1;
                return;
            }

            u->current_tag = XML_TAG_INVALID;
            break;

        case XML_TAG_SERVICE:

            if (!u->service->type) {
                avahi_log_error("%s: parse failure: service incomplete.", u->group->filename);
                u->failed = 1;
                return;
            }

            u->service = NULL;
            u->current_tag = XML_TAG_SERVICE_GROUP;
            break;

        case XML_TAG_NAME:
            u->current_tag = XML_TAG_SERVICE_GROUP;
            break;

        case XML_TAG_PORT: {
            int p;
            assert(u->service);

            p = u->buf ? atoi(u->buf) : 0;

            if (p < 0 || p > 0xFFFF) {
                avahi_log_error("%s: parse failure: invalid port specification \"%s\".", u->group->filename, u->buf);
                u->failed = 1;
                return;
            }

            u->service->port = (uint16_t) p;

            u->current_tag = XML_TAG_SERVICE;
            break;
        }

        case XML_TAG_INTERFACE: {
            int interface;
            assert(u->service);

            interface = (int) if_nametoindex(u->buf);

            if(interface < 1) {
                avahi_log_error("%s: parse failure: invalid interface specification \"%s\".", u->group->filename, u->buf);
                u->failed = 1;
                return;
            }

            u->service->interface = interface;

            u->current_tag = XML_TAG_SERVICE;
            break;
        }

        case XML_TAG_TXT_RECORD: {
            assert(u->service);

            u->service->txt_records = avahi_string_list_add(u->service->txt_records, u->buf ? u->buf : "");
            u->current_tag = XML_TAG_SERVICE;
            break;
        }

        case XML_TAG_SUBTYPE: {
            assert(u->service);

            u->service->subtypes = avahi_string_list_add(u->service->subtypes, u->buf ? u->buf : "");
            u->current_tag = XML_TAG_SERVICE;
            break;
        }

        case XML_TAG_TYPE:
        case XML_TAG_DOMAIN_NAME:
        case XML_TAG_HOST_NAME:
            u->current_tag = XML_TAG_SERVICE;
            break;

        case XML_TAG_INVALID:
            ;
    }

    avahi_free(u->buf);
    u->buf = NULL;
}
Exemple #15
0
int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) {
    char *t, *v;
    uint8_t data[1024];
    AvahiStringList *a = NULL, *b, *p;
    size_t size, n;
    int r;

    a = avahi_string_list_new("prefix", "a", "b", NULL);
    
    a = avahi_string_list_add(a, "start");
    a = avahi_string_list_add(a, "foo=99");
    a = avahi_string_list_add(a, "bar");
    a = avahi_string_list_add(a, "");
    a = avahi_string_list_add(a, "");
    a = avahi_string_list_add(a, "quux");
    a = avahi_string_list_add(a, "");
    a = avahi_string_list_add_arbitrary(a, (const uint8_t*) "null\0null", 9);
    a = avahi_string_list_add_printf(a, "seven=%i %c", 7, 'x');
    a = avahi_string_list_add_pair(a, "blubb", "blaa");
    a = avahi_string_list_add_pair(a, "uxknurz", NULL);
    a = avahi_string_list_add_pair_arbitrary(a, "uxknurz2", (const uint8_t*) "blafasel\0oerks", 14);
    
    a = avahi_string_list_add(a, "end");

    t = avahi_string_list_to_string(a);
    printf("--%s--\n", t);
    avahi_free(t);
    
    n = avahi_string_list_serialize(a, NULL, 0);
    size = avahi_string_list_serialize(a, data, sizeof(data));
    assert(size == n);

    printf("%u\n", size);

    for (t = (char*) data, n = 0; n < size; n++, t++) {
        if (*t <= 32)
            printf("(%u)", *t);
        else
            printf("%c", *t);
    }

    printf("\n");
    
    assert(avahi_string_list_parse(data, size, &b) == 0);

    printf("equal: %i\n", avahi_string_list_equal(a, b));
    
    t = avahi_string_list_to_string(b);
    printf("--%s--\n", t);
    avahi_free(t);

    avahi_string_list_free(b);

    b = avahi_string_list_copy(a);

    assert(avahi_string_list_equal(a, b));

    t = avahi_string_list_to_string(b);
    printf("--%s--\n", t);
    avahi_free(t);

    p = avahi_string_list_find(a, "seven");
    assert(p);
    
    r = avahi_string_list_get_pair(p, &t, &v, NULL);  
    assert(r >= 0);
    assert(t);
    assert(v);
    
    printf("<%s>=<%s>\n", t, v);
    avahi_free(t);
    avahi_free(v);

    p = avahi_string_list_find(a, "quux");
    assert(p);

    r = avahi_string_list_get_pair(p, &t, &v, NULL);
    assert(r >= 0);
    assert(t);
    assert(!v);

    printf("<%s>=<%s>\n", t, v);
    avahi_free(t);
    avahi_free(v);
    
    avahi_string_list_free(a);
    avahi_string_list_free(b);

    n = avahi_string_list_serialize(NULL, NULL, 0);
    size = avahi_string_list_serialize(NULL, data, sizeof(data));
    assert(size == 1);
    assert(size == n);

    assert(avahi_string_list_parse(data, size, &a) == 0);
    assert(!a);
    
    return 0;
}
Exemple #16
0
static void create_service(struct service_data *j) {
    apr_pool_t *t;
    const char *n;
    char *p;
    struct runtime_data *r = j->runtime;
    char **type, **txt_record;
    AvahiStringList *strlist = NULL;


    if (!j->group)
        if (!(j->group = avahi_entry_group_new(r->client, service_callback, j))) {
            ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->main_server, "avahi_entry_group_new() failed: %s", avahi_strerror(avahi_client_errno(r->client)));
            return;
        }

    ap_assert(j->group);
    ap_assert(avahi_entry_group_is_empty(j->group));

    apr_pool_create(&t, r->pool);

/*         ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->main_server, "Service <%s>, host <%s>, port <%u>, location <%s>", j->name, j->host_name, j->port, j->location); */

    if (j->chosen_name)
        n = j->chosen_name;
    else if (!j->name)
        n = avahi_client_get_host_name(r->client);
    else if (j->append_host_name)
        n = apr_pstrcat(t, j->name, avahi_client_get_host_name(r->client), NULL);
    else
        n = j->name;

    if (!j->pool)
        apr_pool_create(&j->pool, r->pool);

    if (n != j->chosen_name) {
        apr_pool_clear(j->pool);
        j->chosen_name = apr_pstrdup(j->pool, n);
    }

    p = j->location ? apr_pstrcat(t, "path=", j->location, NULL) : NULL;

/*         ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->main_server, "%s, %s", p, n); */

    txt_record = (char **) j->txt_record->elts;

    for ( ; *txt_record ; txt_record++)
       strlist =  avahi_string_list_add(strlist, *txt_record);

    if (p)
        strlist = avahi_string_list_add(strlist, p);

    if (apr_is_empty_array(j->types)) {

        if (avahi_entry_group_add_service_strlst(
                j->group,
                AVAHI_IF_UNSPEC,
                AVAHI_PROTO_UNSPEC,
                0,
                n,
                j->port == 443 ? "_https._tcp" : "_http._tcp",
                NULL,
                j->host_name,
                j->port,
                strlist) < 0) {

            ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->main_server, "avahi_entry_group_add_service_strlst(\"%s\") failed: %s", n, avahi_strerror(avahi_client_errno(r->client)));
        }

        const char * cnames = {j->host_name, NULL};
        PublishAvahiCNames(cnames);

    } else {

        for (type = (char**) j->types->elts; *type; type++) {

            if (avahi_entry_group_add_service_strlst(
                    j->group,
                    AVAHI_IF_UNSPEC,
                    AVAHI_PROTO_UNSPEC,
                    0,
                    n,
                    *type,
                    NULL,
                    j->host_name,
                    j->port,
                    strlist) < 0) {

                ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->main_server, "avahi_entry_group_add_service_strlst(\"%s\") failed: %s", n, avahi_strerror(avahi_client_errno(r->client)));
            }

            const char * cnames = {j->host_name, NULL};
            PublishAvahiCNames(cnames);
        }
    }

    avahi_string_list_free(strlist);

    if (avahi_entry_group_is_empty(j->group)) {
        avahi_entry_group_free(j->group);
        j->group = NULL;
    } else
        avahi_entry_group_commit(j->group);

    apr_pool_destroy(t);
}
Exemple #17
0
int old_main(int argc, char *argv[]) {
    struct zc_published_service service = {
            .client = NULL,
            .entry_group = NULL,
            .config = NULL
    };
    char *zcp_argv[] = {"avahi-publish", "-s", "myservice", "_rtp._tcp",  "12345", "Here it is"};
    zc_publish_main(&service, 6, zcp_argv);

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

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

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

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

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

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

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

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

    queue_delete_queue_pointer(queue);

    if (service.client){
        fprintf(stderr, "Clean up client \n");
        avahi_client_free(service.client);
    }
    if(service.config->simple_poll){
        fprintf(stderr, "Clean up poll \n");
        avahi_simple_poll_free(service.config->simple_poll);
    }
    if(service.config){
        fprintf(stderr, "Clean up config \n");
        free(service.config);
    }
    return 0;
}