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 {
103 static struct netconfig_default_connection
104 netconfig_default_connection_info = { NULL, };
106 gboolean netconfig_iface_network_state_ethernet_cable_state(gint32 *state);
108 static gboolean __netconfig_is_connected(GVariantIter *array)
110 gboolean is_connected = FALSE;
111 GVariant *variant = NULL;
113 const gchar *value = NULL;
115 while (g_variant_iter_loop(array, "{sv}", &key, &variant)) {
116 if (g_strcmp0(key, "State") != 0)
119 if (g_variant_is_of_type(variant, G_VARIANT_TYPE_STRING)) {
120 value = g_variant_get_string(variant, NULL);
121 if (g_strcmp0(value, "ready") == 0 || g_strcmp0(value, "online") == 0)
126 g_variant_unref(variant);
133 static char *__netconfig_get_default_profile(void)
135 GVariant *message = NULL;
138 gchar *default_profile = NULL;
141 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
142 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
143 "GetServices", NULL);
144 if (message == NULL) {
145 ERR("Failed to get profiles");
149 g_variant_get(message, "(a(oa{sv}))", &iter);
150 while (g_variant_iter_loop(iter, "(oa{sv})", &object_path, &next)) {
151 if (object_path == NULL)
154 if (netconfig_is_cellular_profile(object_path) && !netconfig_is_cellular_internet_profile(object_path))
157 if (__netconfig_is_connected(next) == TRUE) {
158 default_profile = g_strdup(object_path);
160 g_variant_iter_free(next);
164 g_variant_iter_free(iter);
165 g_variant_unref(message);
167 return default_profile;
170 static int __netconfig_telephony_get_modem_object_path(GSList **modem_path_list)
173 GVariantIter *iter_modem = NULL;
174 GVariantIter *modem_properties = NULL;
175 const char *modem_path;
177 result = netconfig_invoke_dbus_method(TELEPHONY_SERVICE, TELEPHONY_MASTER_PATH,
178 TELEPHONY_MASTER_INTERFACE, "GetModems", NULL);
179 if (result == NULL) {
180 ERR("Failed to get modem path list");
184 g_variant_get(result, "(a{sa{ss}})", &iter_modem);
185 while (g_variant_iter_loop(iter_modem, "{sa{ss}}", &modem_path, &modem_properties)) {
186 *modem_path_list = g_slist_append(*modem_path_list, g_strdup(modem_path));
187 DBG("modem object path: %s", modem_path);
190 g_variant_iter_free(iter_modem);
191 g_variant_unref(result);
196 static int __netconfig_telephony_get_profile_list(net_profile_name_t **profile_list,
200 int count = 0, i = 0;
201 const char *str = NULL;
203 GVariantIter *iter = NULL;
204 GSList *profiles = NULL, *list = NULL;
205 net_profile_name_t *plist = NULL;
207 GSList *modem_path_list = NULL;
208 const char *path = NULL;
210 ret = __netconfig_telephony_get_modem_object_path(&modem_path_list);
212 ERR("Failed to get modems path list");
214 g_slist_free_full(modem_path_list, g_free);
218 for (list = modem_path_list; list != NULL; list = list->next) {
219 path = (const char *)list->data;
221 DBG("path: %s", path);
222 result = netconfig_invoke_dbus_method(TELEPHONY_SERVICE, path,
223 TELEPHONY_MODEM_INTERFACE, "GetProfileList", NULL);
224 if (result == NULL) {
225 DBG("Failed to get profiles: %s", path);
229 g_variant_get(result, "(as)", &iter);
230 while (g_variant_iter_loop(iter, "s", &str))
231 profiles = g_slist_append(profiles, g_strdup(str));
233 g_variant_iter_free(iter);
234 g_variant_unref(result);
237 g_slist_free_full(modem_path_list, g_free);
239 count = g_slist_length(profiles);
241 plist = (net_profile_name_t*)malloc(sizeof(net_profile_name_t) * count);
248 ERR("Failed to allocate memory");
254 for (list = profiles, i = 0; list != NULL; list = list->next, i++)
255 g_strlcpy(plist[i].profile_name,
256 (const char *)list->data, NET_PROFILE_NAME_LEN_MAX);
258 *profile_list = plist;
259 *profile_count = count;
262 g_slist_free_full(profiles, g_free);
267 static int __netconfig_telephony_search_pdp_profile(const char* profile_name, net_profile_name_t* pdp_name)
270 net_profile_name_t* profile_list = NULL;
271 char* prof_name = NULL;
272 char* tel_prof_name = NULL;
273 char* found_ptr = NULL;
274 int profile_count = 0;
277 /* Get pdp profile list from telephony service */
278 ret = __netconfig_telephony_get_profile_list(&profile_list, &profile_count);
280 ERR("Failed to get profile list from telephony service");
281 g_free(profile_list);
285 if (profile_list == NULL || profile_count <= 0) {
286 ERR("There is no PDP profiles");
287 g_free(profile_list);
291 /* Find matching profile */
292 prof_name = strrchr(profile_name, '/') + 1;
293 for (i = 0; i < profile_count; i++) {
294 tel_prof_name = strrchr(profile_list[i].profile_name, '/') + 1;
295 found_ptr = strstr(prof_name, tel_prof_name);
297 if (found_ptr != NULL && g_strcmp0(found_ptr, tel_prof_name) == 0) {
298 g_strlcpy(pdp_name->profile_name,
299 profile_list[i].profile_name, NET_PROFILE_NAME_LEN_MAX);
301 DBG("PDP profile name found in cellular profile: %s", pdp_name->profile_name);
306 if (i >= profile_count) {
307 ERR("There is no matching PDP profiles");
308 g_free(profile_list);
312 g_free(profile_list);
317 static gboolean __netconfig_telephony_get_metered_info(net_profile_name_t* pdp_name)
321 const gchar *key = NULL;
322 const gchar *value = NULL;
323 gboolean ret = FALSE;
325 if (pdp_name == NULL) {
326 ERR("Invalid parameter!");
330 result = netconfig_invoke_dbus_method(TELEPHONY_SERVICE, pdp_name->profile_name,
331 TELEPHONY_PROFILE_INTERFACE, "GetProfile", NULL);
332 if (result == NULL) {
333 ERR("_net_invoke_dbus_method failed");
337 g_variant_get(result, "(a{ss})", &iter);
338 while (g_variant_iter_next(iter, "{ss}", &key, &value)) {
339 if (g_strcmp0(key, "is_metered") == 0) {
343 if (g_strcmp0(value, "TRUE") == 0)
348 g_variant_iter_free(iter);
349 g_variant_unref(result);
351 DBG("is_metered = %s", ret ? "TRUE" : "FALSE");
356 static void __netconfig_get_default_connection_info(const char *profile)
358 GVariant *message = NULL, *variant = NULL, *variant2 = NULL;
359 GVariantIter *iter = NULL, *iter1 = NULL, *service = NULL;
360 GVariant *next = NULL;
365 gboolean found_profile = 0;
367 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
368 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
369 "GetServices", NULL);
370 if (message == NULL) {
371 ERR("Failed to get services informations");
375 g_variant_get(message, "(a(oa{sv}))", &service);
376 if (service == NULL) {
377 ERR("Failed to get services iter");
381 while (g_variant_iter_loop(service, "(oa{sv})", &obj_path, &iter)) {
382 if (g_strcmp0(obj_path, profile) == 0) {
389 if (iter == NULL || found_profile == 0) {
390 ERR("Profile %s doesn't exist", profile);
394 while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
395 const gchar *value = NULL;
397 if (g_strcmp0(key, "Name") == 0 &&
398 netconfig_is_wifi_profile(profile) == TRUE) {
399 if (g_variant_is_of_type(next, G_VARIANT_TYPE_STRING)) {
400 value = g_variant_get_string(next, NULL);
402 netconfig_default_connection_info.essid = g_strdup(value);
404 } else if (g_strcmp0(key, "Ethernet") == 0) {
405 g_variant_get(next, "a{sv}", &iter1);
408 while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
409 if (g_strcmp0(key1, "Interface") == 0) {
410 value = g_variant_get_string(variant, NULL);
411 netconfig_default_connection_info.ifname = g_strdup(value);
414 g_variant_iter_free(iter1);
415 } else if (g_strcmp0(key, "IPv4") == 0) {
416 g_variant_get(next, "a{sv}", &iter1);
419 while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
420 if (g_strcmp0(key1, "Address") == 0) {
421 value = g_variant_get_string(variant, NULL);
422 netconfig_default_connection_info.ipaddress = g_strdup(value);
425 g_variant_iter_free(iter1);
426 } else if (g_strcmp0(key, "IPv6") == 0) {
427 g_variant_get(next, "a{sv}", &iter1);
430 while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
431 if (g_strcmp0(key1, "Address") == 0) {
432 value = g_variant_get_string(variant, NULL);
433 netconfig_default_connection_info.ipaddress6 = g_strdup(value);
436 g_variant_iter_free(iter1);
438 } else if (g_strcmp0(key, "Proxy") == 0) {
439 g_variant_get(next, "a{sv}", &iter1);
442 while (g_variant_iter_loop(iter1, "{sv}", &key2, &variant2)) {
443 GVariantIter *iter_sub = NULL;
445 if (g_strcmp0(key2, "Servers") == 0) {
446 if (!g_variant_is_of_type(variant2, G_VARIANT_TYPE_STRING_ARRAY)) {
448 g_variant_unref(variant2);
452 g_variant_get(variant2, "as", &iter_sub);
453 g_variant_iter_loop(iter_sub, "s", &value);
454 g_variant_iter_free(iter_sub);
455 if (value != NULL && (strlen(value) > 0))
456 netconfig_default_connection_info.proxy = g_strdup(value);
457 } else if (g_strcmp0(key2, "Method") == 0) {
458 if (g_variant_is_of_type(variant2, G_VARIANT_TYPE_STRING)) {
460 g_variant_unref(variant2);
464 value = g_variant_get_string(variant2, NULL);
465 if (g_strcmp0(value, "direct") == 0) {
466 g_free(netconfig_default_connection_info.proxy);
467 netconfig_default_connection_info.proxy = NULL;
470 g_variant_unref(variant2);
475 g_variant_iter_free(iter1);
476 } else if (g_strcmp0(key, "Frequency") == 0) {
477 if (g_variant_is_of_type(next, G_VARIANT_TYPE_UINT16)) {
478 freq = g_variant_get_uint16(next);
479 netconfig_default_connection_info.freq = freq;
484 if (netconfig_is_cellular_profile(profile) == TRUE) {
485 net_profile_name_t pdp_name;
488 ret = __netconfig_telephony_search_pdp_profile(profile, &pdp_name);
489 if (ret >= 0 && strlen(pdp_name.profile_name) > 0)
490 if (__netconfig_telephony_get_metered_info(&pdp_name))
491 netconfig_default_connection_info.is_metered = TRUE;
496 g_variant_unref(message);
499 g_variant_iter_free(iter);
502 g_variant_iter_free(service);
507 static char *__netconfig_get_preferred_ipv6_address(char *profile)
509 GVariant *message = NULL, *variant = NULL, *next = NULL;
510 GVariantIter *iter = NULL, *sub_iter = NULL, *service = NULL;
513 gchar *sub_key = NULL;
514 gchar *preferred_address6 = NULL;
515 gboolean found_profile = 0;
517 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
518 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
519 "GetServices", NULL);
520 if (message == NULL) {
521 ERR("Failed to get service informations");
525 g_variant_get(message, "(a(oa{sv}))", &service);
526 if (service == NULL) {
527 ERR("Failed to get services iter");
531 while (g_variant_iter_loop(service, "(oa{sv})", &obj_path, &iter)) {
532 if (g_strcmp0(obj_path, profile) == 0) {
539 if (iter == NULL || found_profile == 0) {
540 ERR("Profile %s doesn't exist", profile);
544 while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
545 const gchar *value = NULL;
546 if (g_strcmp0(key, "IPv6") == 0) {
547 g_variant_get(next, "a{sv}", &sub_iter);
548 if (sub_iter == NULL)
550 while (g_variant_iter_loop(sub_iter, "{sv}", &sub_key, &variant)) {
551 if (g_strcmp0(sub_key, "Address") == 0) {
552 value = g_variant_get_string(variant, NULL);
553 preferred_address6 = g_strdup(value);
557 g_variant_iter_free(sub_iter);
563 g_variant_unref(message);
566 g_variant_iter_free(iter);
569 g_variant_iter_free(service);
571 return preferred_address6;
574 static void __netconfig_adjust_tcp_buffer_size(void)
576 int fdr = 0, fdw = 0;
577 int fdrmax = 0, fdwmax = 0;
578 const char *rbuf_size = NULL;
579 const char *wbuf_size = NULL;
580 const char *rmax_size = NULL;
581 const char *wmax_size = NULL;
582 const char *profile = netconfig_get_default_profile();
584 if (profile == NULL) {
585 DBG("There is no default connection");
587 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
588 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
589 } else if (netconfig_is_wifi_profile(profile) == TRUE) {
590 DBG("Default connection: Wi-Fi");
592 rbuf_size = NET_TCP_BUFFERSIZE_WIFI_READ;
593 wbuf_size = NET_TCP_BUFFERSIZE_WIFI_WRITE;
594 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
595 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
596 } else if (netconfig_is_cellular_profile(profile) == TRUE) {
597 int telephony_svctype = 0, telephony_pstype = 0;
599 netconfig_get_telephony_network_type(&telephony_svctype, &telephony_pstype);
600 DBG("Default cellular %d, %d", telephony_svctype, telephony_pstype);
602 switch (telephony_pstype) {
603 case VCONFKEY_TELEPHONY_PSTYPE_HSPA:
604 rbuf_size = NET_TCP_BUFFERSIZE_HSPA_READ;
605 wbuf_size = NET_TCP_BUFFERSIZE_HSPA_WRITE;
607 case VCONFKEY_TELEPHONY_PSTYPE_HSUPA:
608 rbuf_size = NET_TCP_BUFFERSIZE_HSUPA_READ;
609 wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
611 case VCONFKEY_TELEPHONY_PSTYPE_HSDPA:
612 rbuf_size = NET_TCP_BUFFERSIZE_HSDPA_READ;
613 wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
615 #if !defined TIZEN_WEARABLE
616 case VCONFKEY_TELEPHONY_PSTYPE_HSPAP:
617 rbuf_size = NET_TCP_BUFFERSIZE_HSPAP_READ;
618 wbuf_size = NET_TCP_BUFFERSIZE_HSPAP_WRITE;
622 switch (telephony_svctype) {
623 case VCONFKEY_TELEPHONY_SVCTYPE_LTE:
624 rbuf_size = NET_TCP_BUFFERSIZE_LTE_READ;
625 wbuf_size = NET_TCP_BUFFERSIZE_LTE_WRITE;
626 rmax_size = NET_TCP_BUFFERSIZE_LTE_RMEM_MAX;
628 case VCONFKEY_TELEPHONY_SVCTYPE_3G:
629 rbuf_size = NET_TCP_BUFFERSIZE_UMTS_READ;
630 wbuf_size = NET_TCP_BUFFERSIZE_UMTS_WRITE;
632 case VCONFKEY_TELEPHONY_SVCTYPE_2_5G_EDGE:
633 rbuf_size = NET_TCP_BUFFERSIZE_EDGE_READ;
634 wbuf_size = NET_TCP_BUFFERSIZE_EDGE_WRITE;
636 case VCONFKEY_TELEPHONY_SVCTYPE_2_5G:
637 rbuf_size = NET_TCP_BUFFERSIZE_GPRS_READ;
638 wbuf_size = NET_TCP_BUFFERSIZE_GPRS_WRITE;
641 /* TODO: Check LTE support */
642 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
643 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
649 DBG("Default TCP buffer configured");
651 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
652 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
655 if (rbuf_size != NULL) {
656 fdr = open(NET_PROC_SYS_NET_IPV4_TCP_RMEM, O_RDWR | O_CLOEXEC);
658 if (fdr < 0 || write(fdr, rbuf_size, strlen(rbuf_size)) < 0)
659 ERR("Failed to set TCP read buffer size");
665 if (wbuf_size != NULL) {
666 fdw = open(NET_PROC_SYS_NET_IPv4_TCP_WMEM, O_RDWR | O_CLOEXEC);
668 if (fdw < 0 || write(fdw, wbuf_size, strlen(wbuf_size)) < 0)
669 ERR("Failed to set TCP write buffer size");
676 if (rmax_size == NULL)
677 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
678 if (wmax_size == NULL)
679 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
681 if (rmax_size != NULL) {
682 fdrmax = open(NET_PROC_SYS_NET_CORE_RMEM_MAX, O_RDWR | O_CLOEXEC);
684 if (fdrmax < 0 || write(fdrmax, rmax_size, strlen(rmax_size)) < 0)
685 ERR("Failed to set TCP rmem_max size");
691 if (wmax_size != NULL) {
692 fdwmax = open(NET_PROC_SYS_NET_CORE_WMEM_MAX, O_RDWR | O_CLOEXEC);
694 if (fdwmax < 0 || write(fdwmax, wmax_size, strlen(wmax_size)) < 0)
695 ERR("Failed to set TCP wmem_max size");
702 static void __netconfig_update_default_connection_info(void)
704 int old_network_status = 0;
705 const char *profile = netconfig_get_default_profile();
706 const char *ip_addr = netconfig_get_default_ipaddress();
707 const char *ip_addr6 = netconfig_get_default_ipaddress6();
708 const char *proxy_addr = netconfig_get_default_proxy();
709 unsigned int freq = netconfig_get_default_frequency();
711 if (emulator_is_emulated() == TRUE) {
713 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr);
715 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
717 if (ip_addr6 != NULL)
718 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, ip_addr6);
720 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, "");
726 DBG("Reset network state configuration");
728 DBG("profile[%s] ipv4(%s) ipv6(%s) proxy(%s)", profile, ip_addr, ip_addr6, proxy_addr);
730 netconfig_vconf_get_int(VCONFKEY_NETWORK_STATUS, &old_network_status);
732 if (profile == NULL && old_network_status != VCONFKEY_NETWORK_OFF) {
733 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
735 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
736 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, "");
737 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
739 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 0);
740 netconfig_set_vconf_int("memory/private/wifi/frequency", 0);
742 DBG("Successfully clear IP and PROXY up");
744 } else if (profile != NULL) {
745 char *old_ip = vconf_get_str(VCONFKEY_NETWORK_IP);
746 char *old_ip6 = vconf_get_str(VCONFKEY_NETWORK_IP6);
747 char *old_proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY);
749 if (netconfig_is_wifi_profile(profile) == TRUE) {
750 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_WIFI);
751 netconfig_set_vconf_int("memory/private/wifi/frequency", freq);
753 netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
754 EKEY_NETWORK_STATUS, EVAL_NETWORK_WIFI);
755 } else if (netconfig_is_cellular_profile(profile)) {
757 if (!netconfig_is_cellular_internet_profile(profile)) {
758 DBG("connection is not a internet profile - stop to update the cellular state");
768 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_CELLULAR);
770 netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
771 EKEY_NETWORK_STATUS, EVAL_NETWORK_CELLULAR);
772 } else if (netconfig_is_ethernet_profile(profile) == TRUE) {
773 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_ETHERNET);
774 netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
775 EKEY_NETWORK_STATUS, EVAL_NETWORK_ETHERNET);
776 } else if (netconfig_is_bluetooth_profile(profile) == TRUE) {
777 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_BLUETOOTH);
778 netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
779 EKEY_NETWORK_STATUS, EVAL_NETWORK_BT);
781 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
782 netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
783 EKEY_NETWORK_STATUS, EVAL_NETWORK_DISCONNECTED);
786 if (g_strcmp0(old_ip, ip_addr) != 0 || old_ip == NULL) {
788 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr);
789 else if (old_ip != NULL && strlen(old_ip) > 0)
790 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
795 if (g_strcmp0(old_ip6, ip_addr6) != 0 || old_ip6 == NULL) {
796 if (ip_addr6 != NULL)
797 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, ip_addr6);
798 else if (old_ip6 != NULL && strlen(old_ip6) > 0)
799 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, "");
804 if (g_strcmp0(old_proxy, proxy_addr) != 0) {
805 if (proxy_addr == NULL)
806 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
808 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, proxy_addr);
813 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 1);
815 DBG("Successfully update default network configuration");
818 __netconfig_adjust_tcp_buffer_size();
821 static gboolean __netconfig_is_tech_state_connected(void)
823 gboolean ret = FALSE;
824 GVariant *message = NULL, *variant;
825 GVariantIter *iter, *next;
829 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
830 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
831 "GetTechnologies", NULL);
833 if (message == NULL) {
834 DBG("Fail to get technology state");
838 g_variant_get(message, "(a(oa{sv}))", &iter);
839 while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
843 while (g_variant_iter_loop(next, "{sv}", &key, &variant)) {
845 if (g_strcmp0(key, "Connected") == 0) {
846 data = g_variant_get_boolean(variant);
847 DBG("%s [%s: %s]", path, key, data ? "True" : "False");
852 g_variant_unref(variant);
853 g_variant_iter_free(next);
861 g_variant_iter_free(iter);
862 g_variant_unref(message);
867 static void __netconfig_update_if_service_connected(void)
869 GVariant *message = NULL, *var;
870 GVariantIter *iter, *next;
874 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
875 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
876 "GetServices", NULL);
878 if (message == NULL) {
879 ERR("Failed to get services");
883 g_variant_get(message, "(a(oa{sv}))", &iter);
884 while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
888 if (g_str_has_prefix(path,
889 CONNMAN_WIFI_SERVICE_PROFILE_PREFIX) == TRUE) {
890 if (g_strrstr(path + strlen(CONNMAN_WIFI_SERVICE_PROFILE_PREFIX),
892 /* skip hidden profiles */
896 } else if (g_str_has_prefix(path,
897 CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX) == TRUE) {
903 while (g_variant_iter_loop(next, "{sv}", &key, &var)) {
904 if (g_strcmp0(key, "State") == 0) {
905 const gchar *sdata = NULL;
906 sdata = g_variant_get_string(var, NULL);
907 DBG("%s [%s: %s]", path, key, sdata);
909 if (g_strcmp0(sdata, "online") == 0 || g_strcmp0(sdata, "ready") == 0) {
911 /* Found a connected WiFi / 3G service.
912 * Lets update the default profile info.
914 netconfig_update_default_profile((const gchar*)path);
917 g_variant_unref(var);
918 g_variant_iter_free(next);
925 g_variant_iter_free(iter);
926 g_variant_unref(message);
931 static void __netconfig_network_notify_result(const char *sig_name, const char *key)
933 GVariantBuilder *builder;
936 INFO("[Signal] %s %s", sig_name, key);
938 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
939 g_variant_builder_add(builder, "{sv}", "key", g_variant_new_string(key));
941 params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
942 g_variant_builder_unref(builder);
944 netconfig_dbus_emit_signal(NULL,
945 NETCONFIG_NETWORK_PATH,
946 NETCONFIG_NETWORK_INTERFACE,
953 const char *netconfig_get_default_profile(void)
955 return netconfig_default_connection_info.profile;
958 const char *netconfig_get_default_ifname(void)
960 return netconfig_default_connection_info.ifname;
963 const char *netconfig_get_default_ipaddress(void)
965 return netconfig_default_connection_info.ipaddress;
968 const char *netconfig_get_default_ipaddress6(void)
970 return netconfig_default_connection_info.ipaddress6;
973 const char *netconfig_get_default_proxy(void)
975 return netconfig_default_connection_info.proxy;
978 unsigned int netconfig_get_default_frequency(void)
980 return netconfig_default_connection_info.freq;
983 const char *netconfig_wifi_get_connected_essid(const char *default_profile)
985 if (default_profile == NULL)
988 if (netconfig_is_wifi_profile(default_profile) != TRUE)
991 if (g_strcmp0(default_profile, netconfig_default_connection_info.profile) != 0)
994 return netconfig_default_connection_info.essid;
997 gboolean netconfig_get_default_is_metered(void)
999 return netconfig_default_connection_info.is_metered;
1002 static int __netconfig_reset_ipv4_socket(void)
1007 struct sockaddr_in sai;
1008 const char *ipaddr = netconfig_get_default_ipaddress();
1009 DBG("ipaddr-[%s]", ipaddr);
1014 fd = socket(AF_INET, SOCK_DGRAM, 0);
1018 memset(&sai, 0, sizeof(struct sockaddr_in));
1019 sai.sin_family = AF_INET;
1021 if (!inet_aton(ipaddr, &sai.sin_addr)) {
1022 DBG("fail to inet_aton()");
1027 memset(&ifr, 0, sizeof(struct ifreq));
1028 memcpy(&ifr.ifr_addr, &sai, sizeof(sai));
1029 g_strlcpy((char *)ifr.ifr_name, WIFI_IFNAME, IFNAMSIZ);
1031 #ifndef SIOCKILLADDR
1032 #define SIOCKILLADDR 0x8939
1035 ret = ioctl(fd, SIOCKILLADDR, &ifr);
1037 DBG("fail to ioctl[SIOCKILLADDR]");
1046 void netconfig_update_default_profile(const char *profile)
1048 static char *old_profile = NULL;
1050 /* It's automatically updated by signal-handler
1051 * DO NOT update manually
1053 * It is going to update default connection information
1056 if (netconfig_default_connection_info.profile != NULL) {
1058 if (netconfig_is_wifi_profile(netconfig_default_connection_info.profile))
1059 __netconfig_reset_ipv4_socket();
1061 g_free(old_profile);
1062 old_profile = strdup(netconfig_default_connection_info.profile);
1064 g_free(netconfig_default_connection_info.profile);
1065 netconfig_default_connection_info.profile = NULL;
1067 g_free(netconfig_default_connection_info.ifname);
1068 netconfig_default_connection_info.ifname = NULL;
1070 g_free(netconfig_default_connection_info.ipaddress);
1071 netconfig_default_connection_info.ipaddress = NULL;
1073 g_free(netconfig_default_connection_info.ipaddress6);
1074 netconfig_default_connection_info.ipaddress6 = NULL;
1076 g_free(netconfig_default_connection_info.proxy);
1077 netconfig_default_connection_info.proxy = NULL;
1079 netconfig_default_connection_info.freq = 0;
1080 netconfig_default_connection_info.is_metered = FALSE;
1082 g_free(netconfig_default_connection_info.essid);
1083 netconfig_default_connection_info.essid = NULL;
1086 /* default profile is NULL and new connected profile is NULL */
1088 char *tmp_profile = __netconfig_get_default_profile();
1090 if (tmp_profile && netconfig_is_cellular_profile(tmp_profile) &&
1091 !netconfig_is_cellular_internet_profile(tmp_profile)) {
1092 DBG("not a default cellular profile");
1093 g_free(tmp_profile);
1098 __netconfig_update_default_connection_info();
1102 netconfig_default_connection_info.profile = g_strdup(tmp_profile);
1103 __netconfig_get_default_connection_info(tmp_profile);
1104 __netconfig_update_default_connection_info();
1105 g_free(tmp_profile);
1109 netconfig_default_connection_info.profile = g_strdup(profile);
1110 __netconfig_get_default_connection_info(profile);
1111 __netconfig_update_default_connection_info();
1114 void netconfig_update_default(void)
1116 if (__netconfig_is_tech_state_connected() == TRUE)
1117 __netconfig_update_if_service_connected();
1119 __netconfig_adjust_tcp_buffer_size();
1122 char *netconfig_get_ifname(const char *profile)
1124 GVariant *message = NULL, *variant;
1125 GVariantIter *iter, *next, *service;
1129 const gchar *value = NULL;
1130 gchar *ifname = NULL;
1131 gboolean found_profile = 0;
1133 if (profile == NULL)
1136 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
1137 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
1138 "GetServices", NULL);
1139 if (message == NULL) {
1140 ERR("Failed to get services informations");
1144 g_variant_get(message, "(a(oa{sv}))", &service);
1145 while (g_variant_iter_loop(service, "(oa{sv})", &obj_path, &iter)) {
1146 if (g_strcmp0(obj_path, profile) == 0) {
1153 if (found_profile == 0) {
1154 ERR("Profile %s doesn't exist", profile);
1155 g_variant_iter_free(service);
1156 g_variant_unref(message);
1160 while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
1161 if (g_strcmp0(key, "Ethernet") == 0) {
1162 while (g_variant_iter_loop(next, "{sv}", &key1, &variant)) {
1163 if (g_strcmp0(key1, "Interface") == 0) {
1164 value = g_variant_get_string(variant, NULL);
1166 ifname = g_strdup(value);
1168 g_variant_unref(variant);
1175 g_variant_unref(message);
1177 g_variant_iter_free(service);
1178 g_variant_iter_free(iter);
1183 /* Check Ethernet Cable Plug-in /Plug-out Status */
1184 void netconfig_network_notify_ethernet_cable_state(const char *key)
1186 __netconfig_network_notify_result("EthernetCableState", key);
1189 static gboolean handle_add_route(
1191 GDBusMethodInvocation *context,
1194 gchar *interface, gchar *gateway, gint address_family)
1196 const gchar *path = ROUTE_EXEC_PATH;
1197 gchar *const args[] = { "/sbin/route", "add", "-net", ip_addr,
1198 "netmask", netmask, "dev", interface, NULL };
1199 gchar *const envs[] = { NULL };
1200 const gchar* buf = NULL;
1205 DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
1207 switch (address_family) {
1209 if (ip_addr == NULL || netmask == NULL || interface == NULL) {
1210 ERR("Invalid parameter");
1211 netconfig_error_invalid_parameter(context);
1215 if (netconfig_execute_file(path, args, envs) < 0) {
1216 DBG("Failed to add a new route");
1217 netconfig_error_permission_denied(context);
1223 if (ip_addr == NULL || interface == NULL || gateway == NULL) {
1224 ERR("Invalid parameter");
1225 netconfig_error_invalid_parameter(context);
1230 ch = strchr(buf, '/');
1233 prefix_len = atoi(ch + 1);
1234 ip_addr[pos-1] = '\0';
1239 if (netconfig_add_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
1240 DBG("Failed to add a new route");
1241 netconfig_error_permission_denied(context);
1246 DBG("Unknown Address Family");
1247 netconfig_error_invalid_parameter(context);
1251 DBG("Successfully added a new route");
1252 network_complete_add_route(object, context, TRUE);
1256 static gboolean handle_remove_route(
1258 GDBusMethodInvocation *context,
1261 gchar *interface, gchar *gateway, gint address_family)
1263 const char *path = ROUTE_EXEC_PATH;
1264 gchar *const args[] = { "/sbin/route", "del", "-net", ip_addr,
1265 "netmask", netmask, "dev", interface, NULL };
1266 char *const envs[] = { NULL };
1267 const char* buf = NULL;
1272 DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
1274 switch (address_family) {
1276 if (ip_addr == NULL || netmask == NULL || interface == NULL) {
1277 DBG("Invalid parameter!");
1278 netconfig_error_invalid_parameter(context);
1281 if (netconfig_execute_file(path, args, envs) < 0) {
1282 DBG("Failed to remove the route");
1283 netconfig_error_permission_denied(context);
1288 if (ip_addr == NULL || interface == NULL || gateway == NULL) {
1289 DBG("Invalid parameter!");
1290 netconfig_error_invalid_parameter(context);
1295 ch = strchr(buf, '/');
1298 prefix_len = atoi(ch + 1);
1299 ip_addr[pos-1] = '\0';
1304 if (netconfig_del_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
1305 DBG("Failed to remove the route");
1306 netconfig_error_permission_denied(context);
1311 DBG("Unknown Address Family");
1312 netconfig_error_invalid_parameter(context);
1316 DBG("Successfully removed the route");
1317 network_complete_remove_route(object, context, TRUE);
1321 static gboolean handle_check_get_privilege(Network *object,
1322 GDBusMethodInvocation *context)
1324 network_complete_check_get_privilege(object, context);
1329 static gboolean handle_check_profile_privilege(Network *object,
1330 GDBusMethodInvocation *context)
1332 network_complete_check_profile_privilege(object, context);
1336 static gboolean handle_check_internet_privilege(Network *object,
1337 GDBusMethodInvocation *context)
1339 network_complete_check_internet_privilege(object, context);
1343 gboolean handle_ethernet_cable_state(Network *object,
1344 GDBusMethodInvocation *context)
1349 ret = netconfig_get_ethernet_cable_state(&state);
1351 DBG("Failed to get ethernet cable state");
1352 netconfig_error_fail_ethernet_cable_state(context);
1356 DBG("Successfully get ethernet cable state[%d]", state);
1357 network_complete_ethernet_cable_state(object, context, state);
1361 gboolean handle_get_metered_info(Network *object,
1362 GDBusMethodInvocation *context)
1366 state = netconfig_get_default_is_metered();
1368 DBG("Default metered state [%s]", state ? "TRUE" : "FALSE");
1369 network_complete_get_metered_info(object, context, state);
1373 gboolean handle_preferred_ipv6_address(Network *object,
1374 GDBusMethodInvocation *context, gchar *profile)
1376 char *address = NULL;
1378 address = __netconfig_get_preferred_ipv6_address(profile);
1379 if (address == NULL) {
1380 DBG("Failed to get preferred IPv6 address");
1381 netconfig_error_fail_preferred_ipv6_address(context);
1385 DBG("Successfully get preferred IPv6 address[%s]", address);
1386 network_complete_preferred_ipv6_address(object, context, address);
1390 void state_object_create_and_init(void)
1392 DBG("Creating network state object");
1393 GDBusInterfaceSkeleton *interface_network = NULL;
1394 GDBusConnection *connection = NULL;
1395 GDBusObjectManagerServer *server = netdbus_get_state_manager();
1399 connection = netdbus_get_connection();
1400 g_dbus_object_manager_server_set_connection(server, connection);
1402 /*Interface netconfig.network*/
1403 netconfigstate = network_skeleton_new();
1405 interface_network = G_DBUS_INTERFACE_SKELETON(netconfigstate);
1406 g_signal_connect(netconfigstate, "handle-add-route",
1407 G_CALLBACK(handle_add_route), NULL);
1408 g_signal_connect(netconfigstate, "handle-check-get-privilege",
1409 G_CALLBACK(handle_check_get_privilege), NULL);
1410 g_signal_connect(netconfigstate, "handle-check-profile-privilege",
1411 G_CALLBACK(handle_check_profile_privilege), NULL);
1412 g_signal_connect(netconfigstate, "handle-check-internet-privilege",
1413 G_CALLBACK(handle_check_internet_privilege), NULL);
1414 g_signal_connect(netconfigstate, "handle-ethernet-cable-state",
1415 G_CALLBACK(handle_ethernet_cable_state), NULL);
1416 g_signal_connect(netconfigstate, "handle-preferred-ipv6-address",
1417 G_CALLBACK(handle_preferred_ipv6_address), NULL);
1418 g_signal_connect(netconfigstate, "handle-remove-route",
1419 G_CALLBACK(handle_remove_route), NULL);
1420 g_signal_connect(netconfigstate, "handle-launch-mdns",
1421 G_CALLBACK(handle_launch_mdns), NULL);
1422 g_signal_connect(netconfigstate, "handle-device-policy-set-wifi",
1423 G_CALLBACK(handle_device_policy_set_wifi), NULL);
1424 g_signal_connect(netconfigstate, "handle-device-policy-get-wifi",
1425 G_CALLBACK(handle_device_policy_get_wifi), NULL);
1426 g_signal_connect(netconfigstate, "handle-device-policy-set-wifi-profile",
1427 G_CALLBACK(handle_device_policy_set_wifi_profile), NULL);
1428 g_signal_connect(netconfigstate, "handle-device-policy-get-wifi-profile",
1429 G_CALLBACK(handle_device_policy_get_wifi_profile), NULL);
1430 g_signal_connect(netconfigstate, "handle-get-metered-info",
1431 G_CALLBACK(handle_get_metered_info), NULL);
1433 if (!g_dbus_interface_skeleton_export(interface_network, connection,
1434 NETCONFIG_NETWORK_STATE_PATH, NULL)) {
1435 ERR("Export with path failed");
1439 void state_object_deinit(void)
1441 g_object_unref(netconfigstate);