2 * Network Configuration Module
4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 #include <vconf-keys.h>
26 #include <arpa/inet.h>
27 #include <netinet/in.h>
28 #include <sys/ioctl.h>
35 #include "wifi-state.h"
36 #include "wifi-power.h"
37 #include "network-state.h"
38 #include "network-dpm.h"
39 #include "network-monitor.h"
40 #include "netsupplicant.h"
42 #include "generated-code.h"
43 /* Define TCP buffer sizes for various networks */
44 /* ReadMin, ReadInitial, ReadMax */ /* WriteMin, WriteInitial, WriteMax */
45 #define NET_TCP_BUFFERSIZE_DEFAULT_READ "4096 87380 704512"
46 #define NET_TCP_BUFFERSIZE_DEFAULT_WRITE "4096 16384 110208"
47 #define NET_TCP_BUFFERSIZE_WIFI_READ "524288 1048576 2560000"
48 #define NET_TCP_BUFFERSIZE_WIFI_WRITE "524288 1048576 2560000"
49 #define NET_TCP_BUFFERSIZE_LTE_READ "524288 1048576 2560000"
50 #define NET_TCP_BUFFERSIZE_LTE_WRITE "524288 1048576 2560000"
51 #define NET_TCP_BUFFERSIZE_UMTS_READ "4094 87380 704512"
52 #define NET_TCP_BUFFERSIZE_UMTS_WRITE "4096 16384 110208"
53 #define NET_TCP_BUFFERSIZE_HSPA_READ "4092 87380 704512"
54 #define NET_TCP_BUFFERSIZE_HSPA_WRITE "4096 16384 262144"
55 #define NET_TCP_BUFFERSIZE_HSDPA_READ "4092 87380 704512"
56 #define NET_TCP_BUFFERSIZE_HSDPA_WRITE "4096 16384 262144"
57 #define NET_TCP_BUFFERSIZE_HSUPA_READ "4092 87380 704512"
58 #define NET_TCP_BUFFERSIZE_HSUPA_WRITE "4096 16384 262144"
59 #define NET_TCP_BUFFERSIZE_HSPAP_READ "4092 87380 1220608"
60 #define NET_TCP_BUFFERSIZE_HSPAP_WRITE "4096 16384 1220608"
61 #define NET_TCP_BUFFERSIZE_EDGE_READ "4093 26280 35040"
62 #define NET_TCP_BUFFERSIZE_EDGE_WRITE "4096 16384 35040"
63 #define NET_TCP_BUFFERSIZE_GPRS_READ "4096 30000 30000"
64 #define NET_TCP_BUFFERSIZE_GPRS_WRITE "4096 8760 11680"
66 #define NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX "1048576"
67 #define NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX "2097152"
68 #define NET_TCP_BUFFERSIZE_LTE_RMEM_MAX "5242880"
70 #define NET_TCP_BUFFERSIZE_WIFID_WMEM_MAX "2097152"
72 #define NET_PROC_SYS_NET_IPV4_TCP_RMEM "/proc/sys/net/ipv4/tcp_rmem"
73 #define NET_PROC_SYS_NET_IPv4_TCP_WMEM "/proc/sys/net/ipv4/tcp_wmem"
74 #define NET_PROC_SYS_NET_CORE_RMEM_MAX "/proc/sys/net/core/rmem_max"
75 #define NET_PROC_SYS_NET_CORE_WMEM_MAX "/proc/sys/net/core/wmem_max"
77 #define ROUTE_EXEC_PATH "/sbin/route"
79 #define TELEPHONY_SERVICE "com.tcore.ps"
80 #define TELEPHONY_MASTER_INTERFACE TELEPHONY_SERVICE ".master"
81 #define TELEPHONY_MODEM_INTERFACE TELEPHONY_SERVICE ".modem"
82 #define TELEPHONY_PROFILE_INTERFACE TELEPHONY_SERVICE ".context"
83 #define TELEPHONY_MASTER_PATH "/"
84 #define NET_PROFILE_NAME_LEN_MAX 512
87 char profile_name[NET_PROFILE_NAME_LEN_MAX];
90 static Network *netconfigstate = NULL;
92 struct netconfig_default_connection {
104 static struct netconfig_default_connection
105 netconfig_default_connection_info = { NULL, };
107 gboolean netconfig_iface_network_state_ethernet_cable_state(gint32 *state);
109 static gboolean __netconfig_is_connected(GVariantIter *array)
111 gboolean is_connected = FALSE;
112 GVariant *variant = NULL;
114 const gchar *value = NULL;
116 while (g_variant_iter_loop(array, "{sv}", &key, &variant)) {
117 if (g_strcmp0(key, "State") != 0)
120 if (g_variant_is_of_type(variant, G_VARIANT_TYPE_STRING)) {
121 value = g_variant_get_string(variant, NULL);
122 if (g_strcmp0(value, "ready") == 0 || g_strcmp0(value, "online") == 0)
127 g_variant_unref(variant);
134 static char *__netconfig_get_default_profile(void)
136 GVariant *message = NULL;
139 gchar *default_profile = NULL;
142 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
143 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
144 "GetServices", NULL);
145 if (message == NULL) {
146 ERR("Failed to get profiles");
150 g_variant_get(message, "(a(oa{sv}))", &iter);
151 while (g_variant_iter_loop(iter, "(oa{sv})", &object_path, &next)) {
152 if (object_path == NULL)
155 if (netconfig_is_cellular_profile(object_path) && !netconfig_is_cellular_internet_profile(object_path))
158 if (__netconfig_is_connected(next) == TRUE) {
159 default_profile = g_strdup(object_path);
161 g_variant_iter_free(next);
165 g_variant_iter_free(iter);
166 g_variant_unref(message);
168 return default_profile;
171 static int __netconfig_telephony_get_modem_object_path(GSList **modem_path_list)
174 GVariantIter *iter_modem = NULL;
175 GVariantIter *modem_properties = NULL;
176 const char *modem_path;
178 result = netconfig_invoke_dbus_method(TELEPHONY_SERVICE, TELEPHONY_MASTER_PATH,
179 TELEPHONY_MASTER_INTERFACE, "GetModems", NULL);
180 if (result == NULL) {
181 ERR("Failed to get modem path list");
185 g_variant_get(result, "(a{sa{ss}})", &iter_modem);
186 while (g_variant_iter_loop(iter_modem, "{sa{ss}}", &modem_path, &modem_properties)) {
187 *modem_path_list = g_slist_append(*modem_path_list, g_strdup(modem_path));
188 DBG("modem object path: %s", modem_path);
191 g_variant_iter_free(iter_modem);
192 g_variant_unref(result);
197 static int __netconfig_telephony_get_profile_list(net_profile_name_t **profile_list,
201 int count = 0, i = 0;
202 const char *str = NULL;
204 GVariantIter *iter = NULL;
205 GSList *profiles = NULL, *list = NULL;
206 net_profile_name_t *plist = NULL;
208 GSList *modem_path_list = NULL;
209 const char *path = NULL;
211 ret = __netconfig_telephony_get_modem_object_path(&modem_path_list);
213 ERR("Failed to get modems path list");
215 g_slist_free_full(modem_path_list, g_free);
219 for (list = modem_path_list; list != NULL; list = list->next) {
220 path = (const char *)list->data;
222 DBG("path: %s", path);
223 result = netconfig_invoke_dbus_method(TELEPHONY_SERVICE, path,
224 TELEPHONY_MODEM_INTERFACE, "GetProfileList", NULL);
225 if (result == NULL) {
226 DBG("Failed to get profiles: %s", path);
230 g_variant_get(result, "(as)", &iter);
231 while (g_variant_iter_loop(iter, "s", &str))
232 profiles = g_slist_append(profiles, g_strdup(str));
234 g_variant_iter_free(iter);
235 g_variant_unref(result);
238 g_slist_free_full(modem_path_list, g_free);
240 count = g_slist_length(profiles);
242 plist = (net_profile_name_t*)malloc(sizeof(net_profile_name_t) * count);
249 ERR("Failed to allocate memory");
255 for (list = profiles, i = 0; list != NULL; list = list->next, i++)
256 g_strlcpy(plist[i].profile_name,
257 (const char *)list->data, NET_PROFILE_NAME_LEN_MAX);
259 *profile_list = plist;
260 *profile_count = count;
263 g_slist_free_full(profiles, g_free);
268 static int __netconfig_telephony_search_pdp_profile(const char* profile_name, net_profile_name_t* pdp_name)
271 net_profile_name_t* profile_list = NULL;
272 char* prof_name = NULL;
273 char* tel_prof_name = NULL;
274 char* found_ptr = NULL;
275 int profile_count = 0;
278 /* Get pdp profile list from telephony service */
279 ret = __netconfig_telephony_get_profile_list(&profile_list, &profile_count);
281 ERR("Failed to get profile list from telephony service");
282 g_free(profile_list);
286 if (profile_list == NULL || profile_count <= 0) {
287 ERR("There is no PDP profiles");
288 g_free(profile_list);
292 /* Find matching profile */
293 prof_name = strrchr(profile_name, '/') + 1;
294 for (i = 0; i < profile_count; i++) {
295 tel_prof_name = strrchr(profile_list[i].profile_name, '/') + 1;
296 found_ptr = strstr(prof_name, tel_prof_name);
298 if (found_ptr != NULL && g_strcmp0(found_ptr, tel_prof_name) == 0) {
299 g_strlcpy(pdp_name->profile_name,
300 profile_list[i].profile_name, NET_PROFILE_NAME_LEN_MAX);
302 DBG("PDP profile name found in cellular profile: %s", pdp_name->profile_name);
307 if (i >= profile_count) {
308 ERR("There is no matching PDP profiles");
309 g_free(profile_list);
313 g_free(profile_list);
318 static gboolean __netconfig_telephony_get_metered_info(net_profile_name_t* pdp_name)
322 const gchar *key = NULL;
323 const gchar *value = NULL;
324 gboolean ret = FALSE;
326 if (pdp_name == NULL) {
327 ERR("Invalid parameter!");
331 result = netconfig_invoke_dbus_method(TELEPHONY_SERVICE, pdp_name->profile_name,
332 TELEPHONY_PROFILE_INTERFACE, "GetProfile", NULL);
333 if (result == NULL) {
334 ERR("_net_invoke_dbus_method failed");
338 g_variant_get(result, "(a{ss})", &iter);
339 while (g_variant_iter_next(iter, "{ss}", &key, &value)) {
340 if (g_strcmp0(key, "is_metered") == 0) {
344 if (g_strcmp0(value, "TRUE") == 0)
349 g_variant_iter_free(iter);
350 g_variant_unref(result);
352 DBG("is_metered = %s", ret ? "TRUE" : "FALSE");
357 static void __netconfig_get_default_connection_info(const char *profile)
359 GVariant *message = NULL, *variant = NULL, *variant2 = NULL;
360 GVariantIter *iter = NULL, *iter1 = NULL, *service = NULL;
361 GVariant *next = NULL;
366 gboolean found_profile = 0;
368 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
369 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
370 "GetServices", NULL);
371 if (message == NULL) {
372 ERR("Failed to get services informations");
376 g_variant_get(message, "(a(oa{sv}))", &service);
377 if (service == NULL) {
378 ERR("Failed to get services iter");
382 while (g_variant_iter_loop(service, "(oa{sv})", &obj_path, &iter)) {
383 if (g_strcmp0(obj_path, profile) == 0) {
390 if (iter == NULL || found_profile == 0) {
391 ERR("Profile %s doesn't exist", profile);
395 while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
396 const gchar *value = NULL;
398 if (g_strcmp0(key, "Name") == 0 &&
399 netconfig_is_wifi_profile(profile) == TRUE) {
400 if (g_variant_is_of_type(next, G_VARIANT_TYPE_STRING)) {
401 value = g_variant_get_string(next, NULL);
403 netconfig_default_connection_info.essid = g_strdup(value);
405 } else if (g_strcmp0(key, "Ethernet") == 0) {
406 g_variant_get(next, "a{sv}", &iter1);
409 while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
410 if (g_strcmp0(key1, "Interface") == 0) {
411 value = g_variant_get_string(variant, NULL);
412 netconfig_default_connection_info.ifname = g_strdup(value);
413 } else if (g_strcmp0(key1, "Address") == 0) {
414 value = g_variant_get_string(variant, NULL);
415 netconfig_default_connection_info.mac_address = g_strdup(value);
418 g_variant_iter_free(iter1);
419 } else if (g_strcmp0(key, "IPv4") == 0) {
420 g_variant_get(next, "a{sv}", &iter1);
423 while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
424 if (g_strcmp0(key1, "Address") == 0) {
425 value = g_variant_get_string(variant, NULL);
426 netconfig_default_connection_info.ipaddress = g_strdup(value);
429 g_variant_iter_free(iter1);
430 } else if (g_strcmp0(key, "IPv6") == 0) {
431 g_variant_get(next, "a{sv}", &iter1);
434 while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
435 if (g_strcmp0(key1, "Address") == 0) {
436 value = g_variant_get_string(variant, NULL);
437 netconfig_default_connection_info.ipaddress6 = g_strdup(value);
440 g_variant_iter_free(iter1);
442 } else if (g_strcmp0(key, "Proxy") == 0) {
443 g_variant_get(next, "a{sv}", &iter1);
446 while (g_variant_iter_loop(iter1, "{sv}", &key2, &variant2)) {
447 GVariantIter *iter_sub = NULL;
449 if (g_strcmp0(key2, "Servers") == 0) {
450 if (!g_variant_is_of_type(variant2, G_VARIANT_TYPE_STRING_ARRAY)) {
452 g_variant_unref(variant2);
456 g_variant_get(variant2, "as", &iter_sub);
457 g_variant_iter_loop(iter_sub, "s", &value);
458 g_variant_iter_free(iter_sub);
459 if (value != NULL && (strlen(value) > 0))
460 netconfig_default_connection_info.proxy = g_strdup(value);
461 } else if (g_strcmp0(key2, "Method") == 0) {
462 if (g_variant_is_of_type(variant2, G_VARIANT_TYPE_STRING)) {
464 g_variant_unref(variant2);
468 value = g_variant_get_string(variant2, NULL);
469 if (g_strcmp0(value, "direct") == 0) {
470 g_free(netconfig_default_connection_info.proxy);
471 netconfig_default_connection_info.proxy = NULL;
474 g_variant_unref(variant2);
479 g_variant_iter_free(iter1);
480 } else if (g_strcmp0(key, "Frequency") == 0) {
481 if (g_variant_is_of_type(next, G_VARIANT_TYPE_UINT16)) {
482 freq = g_variant_get_uint16(next);
483 netconfig_default_connection_info.freq = freq;
488 if (netconfig_is_cellular_profile(profile) == TRUE) {
489 net_profile_name_t pdp_name;
492 ret = __netconfig_telephony_search_pdp_profile(profile, &pdp_name);
493 if (ret >= 0 && strlen(pdp_name.profile_name) > 0)
494 if (__netconfig_telephony_get_metered_info(&pdp_name))
495 netconfig_default_connection_info.is_metered = TRUE;
500 g_variant_unref(message);
503 g_variant_iter_free(iter);
506 g_variant_iter_free(service);
511 static char *__netconfig_get_preferred_ipv6_address(char *profile)
513 GVariant *message = NULL, *variant = NULL, *next = NULL;
514 GVariantIter *iter = NULL, *sub_iter = NULL, *service = NULL;
517 gchar *sub_key = NULL;
518 gchar *preferred_address6 = NULL;
519 gboolean found_profile = 0;
521 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
522 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
523 "GetServices", NULL);
524 if (message == NULL) {
525 ERR("Failed to get service informations");
529 g_variant_get(message, "(a(oa{sv}))", &service);
530 if (service == NULL) {
531 ERR("Failed to get services iter");
535 while (g_variant_iter_loop(service, "(oa{sv})", &obj_path, &iter)) {
536 if (g_strcmp0(obj_path, profile) == 0) {
543 if (iter == NULL || found_profile == 0) {
544 ERR("Profile %s doesn't exist", profile);
548 while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
549 const gchar *value = NULL;
550 if (g_strcmp0(key, "IPv6") == 0) {
551 g_variant_get(next, "a{sv}", &sub_iter);
552 if (sub_iter == NULL)
554 while (g_variant_iter_loop(sub_iter, "{sv}", &sub_key, &variant)) {
555 if (g_strcmp0(sub_key, "Address") == 0) {
556 value = g_variant_get_string(variant, NULL);
557 preferred_address6 = g_strdup(value);
560 g_variant_iter_free(sub_iter);
566 g_variant_unref(message);
569 g_variant_iter_free(iter);
572 g_variant_iter_free(service);
574 return preferred_address6;
577 static void __netconfig_adjust_tcp_buffer_size(void)
579 int fdr = 0, fdw = 0;
580 int fdrmax = 0, fdwmax = 0;
581 const char *rbuf_size = NULL;
582 const char *wbuf_size = NULL;
583 const char *rmax_size = NULL;
584 const char *wmax_size = NULL;
585 const char *profile = netconfig_get_default_profile();
587 if (profile == NULL) {
588 DBG("There is no default connection");
590 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
591 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
592 } else if (netconfig_is_wifi_profile(profile) == TRUE) {
593 DBG("Default connection: Wi-Fi");
595 rbuf_size = NET_TCP_BUFFERSIZE_WIFI_READ;
596 wbuf_size = NET_TCP_BUFFERSIZE_WIFI_WRITE;
597 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
598 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
599 } else if (netconfig_is_cellular_profile(profile) == TRUE) {
600 int telephony_svctype = 0, telephony_pstype = 0;
602 netconfig_get_telephony_network_type(&telephony_svctype, &telephony_pstype);
603 DBG("Default cellular %d, %d", telephony_svctype, telephony_pstype);
605 switch (telephony_pstype) {
606 case VCONFKEY_TELEPHONY_PSTYPE_HSPA:
607 rbuf_size = NET_TCP_BUFFERSIZE_HSPA_READ;
608 wbuf_size = NET_TCP_BUFFERSIZE_HSPA_WRITE;
610 case VCONFKEY_TELEPHONY_PSTYPE_HSUPA:
611 rbuf_size = NET_TCP_BUFFERSIZE_HSUPA_READ;
612 wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
614 case VCONFKEY_TELEPHONY_PSTYPE_HSDPA:
615 rbuf_size = NET_TCP_BUFFERSIZE_HSDPA_READ;
616 wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
618 #if !defined TIZEN_WEARABLE
619 case VCONFKEY_TELEPHONY_PSTYPE_HSPAP:
620 rbuf_size = NET_TCP_BUFFERSIZE_HSPAP_READ;
621 wbuf_size = NET_TCP_BUFFERSIZE_HSPAP_WRITE;
625 switch (telephony_svctype) {
626 case VCONFKEY_TELEPHONY_SVCTYPE_LTE:
627 rbuf_size = NET_TCP_BUFFERSIZE_LTE_READ;
628 wbuf_size = NET_TCP_BUFFERSIZE_LTE_WRITE;
629 rmax_size = NET_TCP_BUFFERSIZE_LTE_RMEM_MAX;
631 case VCONFKEY_TELEPHONY_SVCTYPE_3G:
632 rbuf_size = NET_TCP_BUFFERSIZE_UMTS_READ;
633 wbuf_size = NET_TCP_BUFFERSIZE_UMTS_WRITE;
635 case VCONFKEY_TELEPHONY_SVCTYPE_2_5G_EDGE:
636 rbuf_size = NET_TCP_BUFFERSIZE_EDGE_READ;
637 wbuf_size = NET_TCP_BUFFERSIZE_EDGE_WRITE;
639 case VCONFKEY_TELEPHONY_SVCTYPE_2_5G:
640 rbuf_size = NET_TCP_BUFFERSIZE_GPRS_READ;
641 wbuf_size = NET_TCP_BUFFERSIZE_GPRS_WRITE;
644 /* TODO: Check LTE support */
645 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
646 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
652 DBG("Default TCP buffer configured");
654 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
655 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
658 if (rbuf_size != NULL) {
659 fdr = open(NET_PROC_SYS_NET_IPV4_TCP_RMEM, O_RDWR | O_CLOEXEC);
661 if (fdr < 0 || write(fdr, rbuf_size, strlen(rbuf_size)) < 0)
662 ERR("Failed to set TCP read buffer size");
668 if (wbuf_size != NULL) {
669 fdw = open(NET_PROC_SYS_NET_IPv4_TCP_WMEM, O_RDWR | O_CLOEXEC);
671 if (fdw < 0 || write(fdw, wbuf_size, strlen(wbuf_size)) < 0)
672 ERR("Failed to set TCP write buffer size");
679 if (rmax_size == NULL)
680 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
681 if (wmax_size == NULL)
682 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
684 if (rmax_size != NULL) {
685 fdrmax = open(NET_PROC_SYS_NET_CORE_RMEM_MAX, O_RDWR | O_CLOEXEC);
687 if (fdrmax < 0 || write(fdrmax, rmax_size, strlen(rmax_size)) < 0)
688 ERR("Failed to set TCP rmem_max size");
694 if (wmax_size != NULL) {
695 fdwmax = open(NET_PROC_SYS_NET_CORE_WMEM_MAX, O_RDWR | O_CLOEXEC);
697 if (fdwmax < 0 || write(fdwmax, wmax_size, strlen(wmax_size)) < 0)
698 ERR("Failed to set TCP wmem_max size");
705 static void __netconfig_update_default_connection_info(void)
707 int old_network_status = 0;
708 const char *profile = netconfig_get_default_profile();
709 const char *ip_addr = netconfig_get_default_ipaddress();
710 const char *ip_addr6 = netconfig_get_default_ipaddress6();
711 const char *proxy_addr = netconfig_get_default_proxy();
712 unsigned int freq = netconfig_get_default_frequency();
714 if (emulator_is_emulated() == TRUE) {
716 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr);
718 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
720 if (ip_addr6 != NULL)
721 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, ip_addr6);
723 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, "");
729 DBG("Reset network state configuration");
731 DBG("profile[%s] ipv4(%s) ipv6(%s) proxy(%s)", profile, ip_addr, ip_addr6, proxy_addr);
733 netconfig_vconf_get_int(VCONFKEY_NETWORK_STATUS, &old_network_status);
735 if (profile == NULL && old_network_status != VCONFKEY_NETWORK_OFF) {
736 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
738 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
739 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, "");
740 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
742 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 0);
743 netconfig_set_vconf_int("memory/private/wifi/frequency", 0);
745 DBG("Successfully clear IP and PROXY up");
747 } else if (profile != NULL) {
748 char *old_ip = vconf_get_str(VCONFKEY_NETWORK_IP);
749 char *old_ip6 = vconf_get_str(VCONFKEY_NETWORK_IP6);
750 char *old_proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY);
752 if (netconfig_is_wifi_profile(profile) == TRUE) {
753 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_WIFI);
754 netconfig_set_vconf_int("memory/private/wifi/frequency", freq);
756 netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
757 EKEY_NETWORK_STATUS, EVAL_NETWORK_WIFI);
758 } else if (netconfig_is_cellular_profile(profile)) {
760 if (!netconfig_is_cellular_internet_profile(profile)) {
761 DBG("connection is not a internet profile - stop to update the cellular state");
771 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_CELLULAR);
773 netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
774 EKEY_NETWORK_STATUS, EVAL_NETWORK_CELLULAR);
775 } else if (netconfig_is_ethernet_profile(profile) == TRUE) {
776 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_ETHERNET);
777 netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
778 EKEY_NETWORK_STATUS, EVAL_NETWORK_ETHERNET);
779 } else if (netconfig_is_bluetooth_profile(profile) == TRUE) {
780 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_BLUETOOTH);
781 netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
782 EKEY_NETWORK_STATUS, EVAL_NETWORK_BT);
784 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
785 netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
786 EKEY_NETWORK_STATUS, EVAL_NETWORK_DISCONNECTED);
789 if (g_strcmp0(old_ip, ip_addr) != 0 || old_ip == NULL) {
791 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr);
792 else if (old_ip != NULL && strlen(old_ip) > 0)
793 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
798 if (g_strcmp0(old_ip6, ip_addr6) != 0 || old_ip6 == NULL) {
799 if (ip_addr6 != NULL)
800 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, ip_addr6);
801 else if (old_ip6 != NULL && strlen(old_ip6) > 0)
802 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, "");
807 if (g_strcmp0(old_proxy, proxy_addr) != 0) {
808 if (proxy_addr == NULL)
809 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
811 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, proxy_addr);
816 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 1);
818 DBG("Successfully update default network configuration");
821 __netconfig_adjust_tcp_buffer_size();
824 static gboolean __netconfig_is_tech_state_connected(void)
826 gboolean ret = FALSE;
827 GVariant *message = NULL, *variant;
828 GVariantIter *iter, *next;
832 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
833 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
834 "GetTechnologies", NULL);
836 if (message == NULL) {
837 DBG("Fail to get technology state");
841 g_variant_get(message, "(a(oa{sv}))", &iter);
842 while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
846 while (g_variant_iter_loop(next, "{sv}", &key, &variant)) {
848 if (g_strcmp0(key, "Connected") == 0) {
849 data = g_variant_get_boolean(variant);
850 DBG("%s [%s: %s]", path, key, data ? "True" : "False");
855 g_variant_unref(variant);
856 g_variant_iter_free(next);
864 g_variant_iter_free(iter);
865 g_variant_unref(message);
870 static void __netconfig_update_if_service_connected(void)
872 GVariant *message = NULL, *var;
873 GVariantIter *iter, *next;
877 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
878 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
879 "GetServices", NULL);
881 if (message == NULL) {
882 ERR("Failed to get services");
886 g_variant_get(message, "(a(oa{sv}))", &iter);
887 while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
891 if (g_str_has_prefix(path,
892 CONNMAN_WIFI_SERVICE_PROFILE_PREFIX) == TRUE) {
893 if (g_strrstr(path + strlen(CONNMAN_WIFI_SERVICE_PROFILE_PREFIX),
895 /* skip hidden profiles */
899 } else if (g_str_has_prefix(path,
900 CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX) == TRUE) {
906 while (g_variant_iter_loop(next, "{sv}", &key, &var)) {
907 if (g_strcmp0(key, "State") == 0) {
908 const gchar *sdata = NULL;
909 sdata = g_variant_get_string(var, NULL);
910 DBG("%s [%s: %s]", path, key, sdata);
912 if (g_strcmp0(sdata, "online") == 0 || g_strcmp0(sdata, "ready") == 0) {
914 /* Found a connected WiFi / 3G service.
915 * Lets update the default profile info.
917 netconfig_update_default_profile((const gchar*)path);
920 g_variant_unref(var);
921 g_variant_iter_free(next);
928 g_variant_iter_free(iter);
929 g_variant_unref(message);
934 static void __netconfig_network_notify_result(const char *sig_name, const char *key)
936 GVariantBuilder *builder;
939 INFO("[Signal] %s %s", sig_name, key);
941 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
942 g_variant_builder_add(builder, "{sv}", "key", g_variant_new_string(key));
944 params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
945 g_variant_builder_unref(builder);
947 netconfig_dbus_emit_signal(NULL,
948 NETCONFIG_NETWORK_PATH,
949 NETCONFIG_NETWORK_INTERFACE,
956 const char *netconfig_get_default_profile(void)
958 return netconfig_default_connection_info.profile;
961 const char *netconfig_get_default_ifname(void)
963 return netconfig_default_connection_info.ifname;
966 const char *netconfig_get_default_ipaddress(void)
968 return netconfig_default_connection_info.ipaddress;
971 const char *netconfig_get_default_ipaddress6(void)
973 return netconfig_default_connection_info.ipaddress6;
976 const char *netconfig_get_default_proxy(void)
978 return netconfig_default_connection_info.proxy;
981 unsigned int netconfig_get_default_frequency(void)
983 return netconfig_default_connection_info.freq;
986 const char *netconfig_get_default_mac_address(void)
988 return netconfig_default_connection_info.mac_address;
991 const char *netconfig_wifi_get_connected_essid(const char *default_profile)
993 if (default_profile == NULL)
996 if (netconfig_is_wifi_profile(default_profile) != TRUE)
999 if (g_strcmp0(default_profile, netconfig_default_connection_info.profile) != 0)
1002 return netconfig_default_connection_info.essid;
1005 gboolean netconfig_get_default_is_metered(void)
1007 return netconfig_default_connection_info.is_metered;
1010 static int __netconfig_reset_ipv4_socket(void)
1015 struct sockaddr_in sai;
1016 const char *ipaddr = netconfig_get_default_ipaddress();
1017 DBG("ipaddr-[%s]", ipaddr);
1022 fd = socket(AF_INET, SOCK_DGRAM, 0);
1026 memset(&sai, 0, sizeof(struct sockaddr_in));
1027 sai.sin_family = AF_INET;
1029 if (!inet_aton(ipaddr, &sai.sin_addr)) {
1030 DBG("fail to inet_aton()");
1035 memset(&ifr, 0, sizeof(struct ifreq));
1036 memcpy(&ifr.ifr_addr, &sai, sizeof(sai));
1037 g_strlcpy((char *)ifr.ifr_name, WIFI_IFNAME, IFNAMSIZ);
1039 #ifndef SIOCKILLADDR
1040 #define SIOCKILLADDR 0x8939
1043 ret = ioctl(fd, SIOCKILLADDR, &ifr);
1045 DBG("fail to ioctl[SIOCKILLADDR]");
1054 void netconfig_update_default_profile(const char *profile)
1056 static char *old_profile = NULL;
1058 /* It's automatically updated by signal-handler
1059 * DO NOT update manually
1061 * It is going to update default connection information
1064 if (netconfig_default_connection_info.profile != NULL) {
1066 if (netconfig_is_wifi_profile(netconfig_default_connection_info.profile))
1067 __netconfig_reset_ipv4_socket();
1069 g_free(old_profile);
1070 old_profile = strdup(netconfig_default_connection_info.profile);
1072 g_free(netconfig_default_connection_info.profile);
1073 netconfig_default_connection_info.profile = NULL;
1075 g_free(netconfig_default_connection_info.ifname);
1076 netconfig_default_connection_info.ifname = NULL;
1078 g_free(netconfig_default_connection_info.ipaddress);
1079 netconfig_default_connection_info.ipaddress = NULL;
1081 g_free(netconfig_default_connection_info.ipaddress6);
1082 netconfig_default_connection_info.ipaddress6 = NULL;
1084 g_free(netconfig_default_connection_info.proxy);
1085 netconfig_default_connection_info.proxy = NULL;
1087 g_free(netconfig_default_connection_info.mac_address);
1088 netconfig_default_connection_info.mac_address = NULL;
1090 netconfig_default_connection_info.freq = 0;
1091 netconfig_default_connection_info.is_metered = FALSE;
1093 if (wifi_state_get_service_state() != NETCONFIG_WIFI_CONNECTED) {
1094 g_free(netconfig_default_connection_info.essid);
1095 netconfig_default_connection_info.essid = NULL;
1099 /* default profile is NULL and new connected profile is NULL */
1101 char *tmp_profile = __netconfig_get_default_profile();
1103 if (tmp_profile && netconfig_is_cellular_profile(tmp_profile) &&
1104 !netconfig_is_cellular_internet_profile(tmp_profile)) {
1105 DBG("not a default cellular profile");
1106 g_free(tmp_profile);
1111 __netconfig_update_default_connection_info();
1115 netconfig_default_connection_info.profile = g_strdup(tmp_profile);
1116 __netconfig_get_default_connection_info(tmp_profile);
1117 __netconfig_update_default_connection_info();
1118 g_free(tmp_profile);
1122 netconfig_default_connection_info.profile = g_strdup(profile);
1123 __netconfig_get_default_connection_info(profile);
1124 __netconfig_update_default_connection_info();
1127 void netconfig_update_default(void)
1129 if (__netconfig_is_tech_state_connected() == TRUE)
1130 __netconfig_update_if_service_connected();
1132 __netconfig_adjust_tcp_buffer_size();
1135 char *netconfig_get_ifname(const char *profile)
1137 GVariant *message = NULL, *variant;
1138 GVariantIter *iter, *next, *service;
1142 const gchar *value = NULL;
1143 gchar *ifname = NULL;
1144 gboolean found_profile = 0;
1146 if (profile == NULL)
1149 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
1150 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
1151 "GetServices", NULL);
1152 if (message == NULL) {
1153 ERR("Failed to get services informations");
1157 g_variant_get(message, "(a(oa{sv}))", &service);
1158 while (g_variant_iter_loop(service, "(oa{sv})", &obj_path, &iter)) {
1159 if (g_strcmp0(obj_path, profile) == 0) {
1166 if (found_profile == 0) {
1167 ERR("Profile %s doesn't exist", profile);
1168 g_variant_iter_free(service);
1169 g_variant_unref(message);
1173 while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
1174 if (g_strcmp0(key, "Ethernet") == 0) {
1175 while (g_variant_iter_loop(next, "{sv}", &key1, &variant)) {
1176 if (g_strcmp0(key1, "Interface") == 0) {
1177 value = g_variant_get_string(variant, NULL);
1178 ifname = g_strdup(value);
1180 g_variant_unref(variant);
1187 g_variant_unref(message);
1189 g_variant_iter_free(service);
1190 g_variant_iter_free(iter);
1195 /* Check Ethernet Cable Plug-in /Plug-out Status */
1196 void netconfig_network_notify_ethernet_cable_state(const char *key)
1198 __netconfig_network_notify_result("EthernetCableState", key);
1201 static gboolean handle_add_route(
1203 GDBusMethodInvocation *context,
1206 gchar *interface, gchar *gateway, gint address_family)
1208 const gchar *path = ROUTE_EXEC_PATH;
1210 if (gateway == NULL || strcmp(gateway, "") == 0)
1211 gchar *const args[] = { "/sbin/route", "add", "-net", ip_addr,
1212 "netmask", netmask, "dev", interface, NULL };
1214 gchar *const args[] = { "/sbin/route", "add", "-net", ip_addr, "gw", gateway,
1215 "netmask", netmask, "dev", interface, NULL };
1217 gchar *const envs[] = { NULL };
1218 const gchar* buf = NULL;
1223 DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
1225 switch (address_family) {
1227 if (ip_addr == NULL || netmask == NULL || interface == NULL) {
1228 ERR("Invalid parameter");
1229 netconfig_error_invalid_parameter(context);
1233 if (netconfig_execute_file(path, args, envs) < 0) {
1234 DBG("Failed to add a new route");
1235 netconfig_error_permission_denied(context);
1241 if (ip_addr == NULL || interface == NULL || gateway == NULL) {
1242 ERR("Invalid parameter");
1243 netconfig_error_invalid_parameter(context);
1248 ch = strchr(buf, '/');
1251 prefix_len = atoi(ch + 1);
1252 ip_addr[pos-1] = '\0';
1257 if (netconfig_add_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
1258 DBG("Failed to add a new route");
1259 netconfig_error_permission_denied(context);
1264 DBG("Unknown Address Family");
1265 netconfig_error_invalid_parameter(context);
1269 DBG("Successfully added a new route");
1270 network_complete_add_route(object, context, TRUE);
1274 static gboolean handle_remove_route(
1276 GDBusMethodInvocation *context,
1279 gchar *interface, gchar *gateway, gint address_family)
1281 const char *path = ROUTE_EXEC_PATH;
1283 if (gateway == NULL || strcmp(gateway, "") == 0)
1284 gchar *const args[] = { "/sbin/route", "del", "-net", ip_addr,
1285 "netmask", netmask, "dev", interface, NULL };
1287 gchar *const args[] = { "/sbin/route", "del", "-net", ip_addr, "gw", gateway,
1288 "netmask", netmask, "dev", interface, NULL };
1290 char *const envs[] = { NULL };
1291 const char* buf = NULL;
1296 DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
1298 switch (address_family) {
1300 if (ip_addr == NULL || netmask == NULL || interface == NULL) {
1301 DBG("Invalid parameter!");
1302 netconfig_error_invalid_parameter(context);
1305 if (netconfig_execute_file(path, args, envs) < 0) {
1306 DBG("Failed to remove the route");
1307 netconfig_error_permission_denied(context);
1312 if (ip_addr == NULL || interface == NULL || gateway == NULL) {
1313 DBG("Invalid parameter!");
1314 netconfig_error_invalid_parameter(context);
1319 ch = strchr(buf, '/');
1322 prefix_len = atoi(ch + 1);
1323 ip_addr[pos-1] = '\0';
1328 if (netconfig_del_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
1329 DBG("Failed to remove the route");
1330 netconfig_error_permission_denied(context);
1335 DBG("Unknown Address Family");
1336 netconfig_error_invalid_parameter(context);
1340 DBG("Successfully removed the route");
1341 network_complete_remove_route(object, context, TRUE);
1345 static gboolean handle_check_get_privilege(Network *object,
1346 GDBusMethodInvocation *context)
1348 network_complete_check_get_privilege(object, context);
1353 static gboolean handle_check_profile_privilege(Network *object,
1354 GDBusMethodInvocation *context)
1356 network_complete_check_profile_privilege(object, context);
1360 static gboolean handle_check_internet_privilege(Network *object,
1361 GDBusMethodInvocation *context)
1363 network_complete_check_internet_privilege(object, context);
1367 gboolean handle_ethernet_cable_state(Network *object,
1368 GDBusMethodInvocation *context)
1373 ret = netconfig_get_ethernet_cable_state(&state);
1375 DBG("Failed to get ethernet cable state");
1376 netconfig_error_fail_ethernet_cable_state(context);
1380 DBG("Successfully get ethernet cable state[%d]", state);
1381 network_complete_ethernet_cable_state(object, context, state);
1385 gboolean handle_get_metered_info(Network *object,
1386 GDBusMethodInvocation *context)
1390 state = netconfig_get_default_is_metered();
1392 DBG("Default metered state [%s]", state ? "TRUE" : "FALSE");
1393 network_complete_get_metered_info(object, context, state);
1397 gboolean handle_preferred_ipv6_address(Network *object,
1398 GDBusMethodInvocation *context, gchar *profile)
1400 char *address = NULL;
1402 address = __netconfig_get_preferred_ipv6_address(profile);
1403 if (address == NULL) {
1404 DBG("Failed to get preferred IPv6 address");
1405 netconfig_error_fail_preferred_ipv6_address(context);
1409 DBG("Successfully get preferred IPv6 address[%s]", address);
1410 network_complete_preferred_ipv6_address(object, context, address);
1414 void state_object_create_and_init(void)
1416 DBG("Creating network state object");
1417 GDBusInterfaceSkeleton *interface_network = NULL;
1418 GDBusConnection *connection = NULL;
1419 GDBusObjectManagerServer *server = netdbus_get_state_manager();
1423 connection = netdbus_get_connection();
1424 g_dbus_object_manager_server_set_connection(server, connection);
1426 /*Interface netconfig.network*/
1427 netconfigstate = network_skeleton_new();
1429 interface_network = G_DBUS_INTERFACE_SKELETON(netconfigstate);
1430 g_signal_connect(netconfigstate, "handle-add-route",
1431 G_CALLBACK(handle_add_route), NULL);
1432 g_signal_connect(netconfigstate, "handle-check-get-privilege",
1433 G_CALLBACK(handle_check_get_privilege), NULL);
1434 g_signal_connect(netconfigstate, "handle-check-profile-privilege",
1435 G_CALLBACK(handle_check_profile_privilege), NULL);
1436 g_signal_connect(netconfigstate, "handle-check-internet-privilege",
1437 G_CALLBACK(handle_check_internet_privilege), NULL);
1438 g_signal_connect(netconfigstate, "handle-ethernet-cable-state",
1439 G_CALLBACK(handle_ethernet_cable_state), NULL);
1440 g_signal_connect(netconfigstate, "handle-preferred-ipv6-address",
1441 G_CALLBACK(handle_preferred_ipv6_address), NULL);
1442 g_signal_connect(netconfigstate, "handle-remove-route",
1443 G_CALLBACK(handle_remove_route), NULL);
1444 g_signal_connect(netconfigstate, "handle-launch-mdns",
1445 G_CALLBACK(handle_launch_mdns), NULL);
1446 g_signal_connect(netconfigstate, "handle-device-policy-set-wifi",
1447 G_CALLBACK(handle_device_policy_set_wifi), NULL);
1448 g_signal_connect(netconfigstate, "handle-device-policy-get-wifi",
1449 G_CALLBACK(handle_device_policy_get_wifi), NULL);
1450 g_signal_connect(netconfigstate, "handle-device-policy-set-wifi-profile",
1451 G_CALLBACK(handle_device_policy_set_wifi_profile), NULL);
1452 g_signal_connect(netconfigstate, "handle-device-policy-get-wifi-profile",
1453 G_CALLBACK(handle_device_policy_get_wifi_profile), NULL);
1454 g_signal_connect(netconfigstate, "handle-get-metered-info",
1455 G_CALLBACK(handle_get_metered_info), NULL);
1457 if (!g_dbus_interface_skeleton_export(interface_network, connection,
1458 NETCONFIG_NETWORK_STATE_PATH, NULL)) {
1459 ERR("Export with path failed");
1463 void state_object_deinit(void)
1465 g_object_unref(netconfigstate);