int connman_network_set_nameservers(struct connman_network *network,
				const char *nameservers)
{
	struct connman_service *service;
	char **nameservers_array;
	int i;

	DBG("network %p nameservers %s", network, nameservers);

	service = __connman_service_lookup_from_network(network);
	if (service == NULL)
		return -EINVAL;

	__connman_service_nameserver_clear(service);

	if (nameservers == NULL)
		return 0;

	nameservers_array = g_strsplit(nameservers, " ", 0);

	for (i = 0; nameservers_array[i] != NULL; i++) {
		__connman_service_nameserver_append(service,
						nameservers_array[i], FALSE);
	}

	g_strfreev(nameservers_array);

	return 0;
}
示例#2
0
文件: resolver.c 项目: intgr/connman
static int append_resolver(const char *interface, const char *domain,
				const char *server, unsigned int lifetime,
							unsigned int flags)
{
	struct entry_data *entry;
	unsigned int interval;

	DBG("interface %s domain %s server %s lifetime %d flags %d",
				interface, domain, server, lifetime, flags);

	if (server == NULL && domain == NULL)
		return -EINVAL;

	entry = g_try_new0(struct entry_data, 1);
	if (entry == NULL)
		return -ENOMEM;

	entry->interface = g_strdup(interface);
	entry->domain = g_strdup(domain);
	entry->server = g_strdup(server);
	entry->flags = flags;
	entry->lifetime = lifetime;

	if (server != NULL)
		entry->family = connman_inet_check_ipaddress(server);

	if (lifetime) {
		int index;
		interval = lifetime * RESOLVER_LIFETIME_REFRESH_THRESHOLD;

		DBG("RDNSS start interface %s domain %s "
				"server %s lifetime threshold %d",
				interface, domain, server, interval);

		entry->timeout = g_timeout_add_seconds(interval,
				resolver_refresh_cb, entry);

		/*
		 * We update the service only for those nameservers
		 * that are automagically added via netlink (lifetime > 0)
		 */
		index = connman_inet_ifindex(interface);
		if (server != NULL && index >= 0) {
			struct connman_service *service;
			service = __connman_service_lookup_from_index(index);
			if (service != NULL)
				__connman_service_nameserver_append(service,
								server, TRUE);
		}
	}
	entry_list = g_slist_append(entry_list, entry);

	if (dnsproxy_enabled == TRUE)
		__connman_dnsproxy_append(interface, domain, server);
	else
		__connman_resolvfile_append(interface, domain, server);

	return 0;
}
示例#3
0
static int append_resolver(const char *interface, const char *domain,
				const char *server, unsigned int lifetime,
							unsigned int flags)
{
	struct entry_data *entry;

	DBG("interface %s domain %s server %s lifetime %d flags %d",
				interface, domain, server, lifetime, flags);

	if (server == NULL && domain == NULL)
		return -EINVAL;

	entry = g_try_new0(struct entry_data, 1);
	if (entry == NULL)
		return -ENOMEM;

	entry->interface = g_strdup(interface);
	entry->domain = g_strdup(domain);
	entry->server = g_strdup(server);
	entry->flags = flags;
	if (lifetime) {
		int index;
		entry->timeout = g_timeout_add_seconds(lifetime,
						resolver_expire_cb, entry);

		/*
		 * We update the service only for those nameservers
		 * that are automagically added via netlink (lifetime > 0)
		 */
		index = connman_inet_ifindex(interface);
		if (index >= 0) {
			struct connman_service *service;
			service = __connman_service_lookup_from_index(index);
			if (service != NULL)
				__connman_service_nameserver_append(service,
								server, TRUE);
		}
	}
	entry_list = g_slist_append(entry_list, entry);

	if (dnsproxy_enabled == TRUE)
		__connman_dnsproxy_append(interface, domain, server);
	else
		__connman_resolvfile_append(interface, domain, server);

	return 0;
}
示例#4
0
文件: dhcp.c 项目: Pillar1989/connman
static bool apply_lease_available_on_network(GDHCPClient *dhcp_client,
						struct connman_dhcp *dhcp)
{
	char **nameservers, **timeservers, *pac = NULL;
	struct connman_service *service;
	GList *list, *option = NULL;
	int ns_entries;
	int i;

	if (!dhcp->network)
		return true;

	service = connman_service_lookup_from_network(dhcp->network);
	if (!service) {
		connman_error("Can not lookup service");
		return false;
	}

	option = g_dhcp_client_get_option(dhcp_client, G_DHCP_MTU);
	if (option && option->data) {
		int mtu, index, err;

		mtu = atoi(option->data);

		if (mtu >= IPV6_MIN_MTU && mtu <= ETH_DATA_LEN) {
			index = __connman_ipconfig_get_index(dhcp->ipconfig);
			err = connman_inet_set_mtu(index, mtu);

			DBG("MTU %d index %d err %d", mtu, index, err);
		}
	}

	option = g_dhcp_client_get_option(dhcp_client, 252);
	if (option)
		pac = g_strdup(option->data);

	option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
	ns_entries = g_list_length(option);
	nameservers = g_try_new0(char *, ns_entries + 1);
	if (nameservers) {
		for (i = 0, list = option;list; list = list->next, i++)
			nameservers[i] = g_strdup(list->data);
		nameservers[ns_entries] = NULL;
	}

	option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
	if (option)
		__connman_service_set_domainname(service, option->data);

	option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
	if (option)
		__connman_service_set_hostname(service, option->data);

	option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER);
	ns_entries = g_list_length(option);
	timeservers = g_try_new0(char *, ns_entries + 1);
	if (timeservers) {
		for (i = 0, list = option; list; list = list->next, i++)
			timeservers[i] = g_strdup(list->data);
		timeservers[ns_entries] = NULL;
	}

	if (!compare_string_arrays(nameservers, dhcp->nameservers)) {
		if (dhcp->nameservers) {
			for (i = 0; dhcp->nameservers[i]; i++) {
				__connman_service_nameserver_remove(service,
						dhcp->nameservers[i], false);
			}
			g_strfreev(dhcp->nameservers);
		}

		dhcp->nameservers = nameservers;

		for (i = 0; dhcp->nameservers && dhcp->nameservers[i]; i++) {
			__connman_service_nameserver_append(service,
						dhcp->nameservers[i], false);
		}
	} else {
		g_strfreev(nameservers);
	}

	if (!compare_string_arrays(timeservers, dhcp->timeservers)) {
		if (dhcp->timeservers) {
			for (i = 0; dhcp->timeservers[i]; i++) {
				__connman_service_timeserver_remove(service,
							dhcp->timeservers[i]);
			}
			g_strfreev(dhcp->timeservers);
		}

		dhcp->timeservers = timeservers;

		for (i = 0; dhcp->timeservers && dhcp->timeservers[i]; i++) {
			__connman_service_timeserver_append(service,
							dhcp->timeservers[i]);
		}
	} else {
		g_strfreev(timeservers);
	}

	if (g_strcmp0(pac, dhcp->pac) != 0) {
		g_free(dhcp->pac);
		dhcp->pac = pac;

		__connman_ipconfig_set_proxy_autoconfig(dhcp->ipconfig,
								dhcp->pac);
	}

	if (connman_setting_get_bool("Enable6to4"))
		__connman_6to4_probe(service);

	return true;
}
示例#5
0
static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
{
	struct connman_dhcp *dhcp = user_data;
	GList *list, *option = NULL;
	char *address, *netmask = NULL, *gateway = NULL;
	const char *c_address, *c_gateway;
	char *domainname = NULL, *hostname = NULL;
	char **nameservers, *timeserver = NULL, *pac = NULL;
	int ns_entries;
	struct connman_ipconfig *ipconfig;
	struct connman_service *service;
	unsigned char prefixlen, c_prefixlen;
	gboolean ip_change;
	int i;

	DBG("Lease available");

	service = __connman_service_lookup_from_network(dhcp->network);
	if (service == NULL) {
		connman_error("Can not lookup service");
		return;
	}

	ipconfig = __connman_service_get_ip4config(service);
	if (ipconfig == NULL) {
		connman_error("Could not lookup ipconfig");
		return;
	}

	c_address = __connman_ipconfig_get_local(ipconfig);
	c_gateway = __connman_ipconfig_get_gateway(ipconfig);
	c_prefixlen = __connman_ipconfig_get_prefixlen(ipconfig);

	address = g_dhcp_client_get_address(dhcp_client);

	__connman_ipconfig_set_dhcp_address(ipconfig, address);
	DBG("last address %s", address);

	option = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
	if (option != NULL)
		netmask = g_strdup(option->data);

	option = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
	if (option != NULL)
		gateway = g_strdup(option->data);

	prefixlen = __connman_ipconfig_netmask_prefix_len(netmask);

	DBG("c_address %s", c_address);

	if (address != NULL && c_address != NULL &&
					g_strcmp0(address, c_address) != 0)
		ip_change = TRUE;
	else if (gateway != NULL && c_gateway != NULL &&
					g_strcmp0(gateway, c_gateway) != 0)
		ip_change = TRUE;
	else if (prefixlen != c_prefixlen)
		ip_change = TRUE;
	else if (c_address == NULL || c_gateway == NULL)
		ip_change = TRUE;
	else
		ip_change = FALSE;

	option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
	for (ns_entries = 0, list = option; list; list = list->next)
		ns_entries += 1;
	nameservers = g_try_new0(char *, ns_entries + 1);
	if (nameservers != NULL) {
		for (i = 0, list = option; list; list = list->next, i++)
			nameservers[i] = g_strdup(list->data);
		nameservers[ns_entries] = NULL;
	}

	option = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
	if (option != NULL)
		domainname = g_strdup(option->data);

	option = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
	if (option != NULL)
		hostname = g_strdup(option->data);

	option = g_dhcp_client_get_option(dhcp_client, G_DHCP_NTP_SERVER);
	if (option != NULL)
		timeserver = g_strdup(option->data);

	option = g_dhcp_client_get_option(dhcp_client, 252);
	if (option != NULL)
		pac = g_strdup(option->data);

	connman_ipconfig_set_method(ipconfig, CONNMAN_IPCONFIG_METHOD_DHCP);

	if (ip_change == TRUE) {
		__connman_ipconfig_set_local(ipconfig, address);
		__connman_ipconfig_set_prefixlen(ipconfig, prefixlen);
		__connman_ipconfig_set_gateway(ipconfig, gateway);
	}

	if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
		if (dhcp->nameservers != NULL) {
			for (i = 0; dhcp->nameservers[i] != NULL; i++) {
				__connman_service_nameserver_remove(service,
							dhcp->nameservers[i]);
			}
			g_strfreev(dhcp->nameservers);
		}

		dhcp->nameservers = nameservers;

		for (i = 0; dhcp->nameservers[i] != NULL; i++) {
			__connman_service_nameserver_append(service,
							dhcp->nameservers[i]);
		}
	} else {
		g_strfreev(nameservers);
	}

	if (g_strcmp0(timeserver, dhcp->timeserver) != 0) {
		if (dhcp->timeserver != NULL) {
			__connman_service_timeserver_remove(service,
							dhcp->timeserver);
			g_free(dhcp->timeserver);
		}

		dhcp->timeserver = timeserver;

		if (dhcp->timeserver != NULL)
			__connman_service_timeserver_append(service,
							dhcp->timeserver);
	}

	if (g_strcmp0(pac, dhcp->pac) != 0) {
		g_free(dhcp->pac);
		dhcp->pac = pac;

		__connman_service_set_pac(service, dhcp->pac);
	}

	__connman_service_set_domainname(service, domainname);

	if (domainname != NULL)
		__connman_utsname_set_domainname(domainname);

	if (hostname != NULL)
		__connman_utsname_set_hostname(hostname);

	if (ip_change == TRUE)
		dhcp_valid(dhcp);

	__connman_6to4_probe(service);

	g_free(address);
	g_free(netmask);
	g_free(gateway);
	g_free(domainname);
	g_free(hostname);
}
示例#6
0
static int set_addresses(GDHCPClient *dhcp_client,
                         struct connman_dhcpv6 *dhcp)
{
    struct connman_service *service;
    struct connman_ipconfig *ipconfig;
    int entries, i;
    GList *option, *list;
    char **nameservers, **timeservers;
    const char *c_address;
    char *address = NULL;

    service = connman_service_lookup_from_network(dhcp->network);
    if (service == NULL) {
        connman_error("Can not lookup service");
        return -EINVAL;
    }

    ipconfig = __connman_service_get_ip6config(service);
    if (ipconfig == NULL) {
        connman_error("Could not lookup ip6config");
        return -EINVAL;
    }

    option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DNS_SERVERS);
    entries = g_list_length(option);

    nameservers = g_try_new0(char *, entries + 1);
    if (nameservers != NULL) {
        for (i = 0, list = option; list; list = list->next, i++)
            nameservers[i] = g_strdup(list->data);
    }

    if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
        if (dhcp->nameservers != NULL) {
            for (i = 0; dhcp->nameservers[i] != NULL; i++)
                __connman_service_nameserver_remove(service,
                                                    dhcp->nameservers[i],
                                                    FALSE);
            g_strfreev(dhcp->nameservers);
        }

        dhcp->nameservers = nameservers;

        for (i = 0; dhcp->nameservers != NULL &&
                dhcp->nameservers[i] != NULL; i++)
            __connman_service_nameserver_append(service,
                                                dhcp->nameservers[i],
                                                FALSE);
    } else
        g_strfreev(nameservers);


    option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_SNTP_SERVERS);
    entries = g_list_length(option);

    timeservers = g_try_new0(char *, entries + 1);
    if (timeservers != NULL) {
        for (i = 0, list = option; list; list = list->next, i++)
            timeservers[i] = g_strdup(list->data);
    }

    if (compare_string_arrays(timeservers, dhcp->timeservers) == FALSE) {
        if (dhcp->timeservers != NULL) {
            for (i = 0; dhcp->timeservers[i] != NULL; i++)
                __connman_service_timeserver_remove(service,
                                                    dhcp->timeservers[i]);
            g_strfreev(dhcp->timeservers);
        }

        dhcp->timeservers = timeservers;

        for (i = 0; dhcp->timeservers != NULL &&
                dhcp->timeservers[i] != NULL; i++)
            __connman_service_timeserver_append(service,
                                                dhcp->timeservers[i]);
    } else
        g_strfreev(timeservers);


    option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_NA);
    if (option != NULL)
        address = g_strdup(option->data);
    else {
        option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_IA_TA);
        if (option != NULL)
            address = g_strdup(option->data);
    }

    c_address = __connman_ipconfig_get_local(ipconfig);

    if (address != NULL &&
            ((c_address != NULL &&
              g_strcmp0(address, c_address) != 0) ||
             (c_address == NULL))) {
        int prefix_len;

        /* Is this prefix part of the subnet we are suppose to use? */
        prefix_len = check_ipv6_addr_prefix(dhcp->prefixes, address);

        __connman_ipconfig_set_local(ipconfig, address);
        __connman_ipconfig_set_prefixlen(ipconfig, prefix_len);

        DBG("new address %s/%d", address, prefix_len);
    }

    return 0;
}
示例#7
0
static void info_req_cb(GDHCPClient *dhcp_client, gpointer user_data)
{
    struct connman_dhcpv6 *dhcp = user_data;
    struct connman_service *service;
    int entries, i;
    GList *option, *list;
    char **nameservers, **timeservers;

    DBG("dhcpv6 information-request %p", dhcp);

    service = connman_service_lookup_from_network(dhcp->network);
    if (service == NULL) {
        connman_error("Can not lookup service");
        return;
    }

    option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_DNS_SERVERS);
    entries = g_list_length(option);

    nameservers = g_try_new0(char *, entries + 1);
    if (nameservers != NULL) {
        for (i = 0, list = option; list; list = list->next, i++)
            nameservers[i] = g_strdup(list->data);
    }

    if (compare_string_arrays(nameservers, dhcp->nameservers) == FALSE) {
        if (dhcp->nameservers != NULL) {
            for (i = 0; dhcp->nameservers[i] != NULL; i++)
                __connman_service_nameserver_remove(service,
                                                    dhcp->nameservers[i],
                                                    FALSE);
            g_strfreev(dhcp->nameservers);
        }

        dhcp->nameservers = nameservers;

        for (i = 0; dhcp->nameservers != NULL &&
                dhcp->nameservers[i] != NULL; i++)
            __connman_service_nameserver_append(service,
                                                dhcp->nameservers[i],
                                                FALSE);
    } else
        g_strfreev(nameservers);


    option = g_dhcp_client_get_option(dhcp_client, G_DHCPV6_SNTP_SERVERS);
    entries = g_list_length(option);

    timeservers = g_try_new0(char *, entries + 1);
    if (timeservers != NULL) {
        for (i = 0, list = option; list; list = list->next, i++)
            timeservers[i] = g_strdup(list->data);
    }

    if (compare_string_arrays(timeservers, dhcp->timeservers) == FALSE) {
        if (dhcp->timeservers != NULL) {
            for (i = 0; dhcp->timeservers[i] != NULL; i++)
                __connman_service_timeserver_remove(service,
                                                    dhcp->timeservers[i]);
            g_strfreev(dhcp->timeservers);
        }

        dhcp->timeservers = timeservers;

        for (i = 0; dhcp->timeservers != NULL &&
                dhcp->timeservers[i] != NULL; i++)
            __connman_service_timeserver_append(service,
                                                dhcp->timeservers[i]);
    } else
        g_strfreev(timeservers);


    if (dhcp->callback != NULL) {
        uint16_t status = g_dhcpv6_client_get_status(dhcp_client);
        dhcp->callback(dhcp->network, status == 0 ? TRUE : FALSE);
    }
}