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);
556 g_variant_iter_free(sub_iter);
562 g_variant_unref(message);
565 g_variant_iter_free(iter);
568 g_variant_iter_free(service);
570 return preferred_address6;
573 static void __netconfig_adjust_tcp_buffer_size(void)
575 int fdr = 0, fdw = 0;
576 int fdrmax = 0, fdwmax = 0;
577 const char *rbuf_size = NULL;
578 const char *wbuf_size = NULL;
579 const char *rmax_size = NULL;
580 const char *wmax_size = NULL;
581 const char *profile = netconfig_get_default_profile();
583 if (profile == NULL) {
584 DBG("There is no default connection");
586 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
587 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
588 } else if (netconfig_is_wifi_profile(profile) == TRUE) {
589 DBG("Default connection: Wi-Fi");
591 rbuf_size = NET_TCP_BUFFERSIZE_WIFI_READ;
592 wbuf_size = NET_TCP_BUFFERSIZE_WIFI_WRITE;
593 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
594 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
595 } else if (netconfig_is_cellular_profile(profile) == TRUE) {
596 int telephony_svctype = 0, telephony_pstype = 0;
598 netconfig_get_telephony_network_type(&telephony_svctype, &telephony_pstype);
599 DBG("Default cellular %d, %d", telephony_svctype, telephony_pstype);
601 switch (telephony_pstype) {
602 case VCONFKEY_TELEPHONY_PSTYPE_HSPA:
603 rbuf_size = NET_TCP_BUFFERSIZE_HSPA_READ;
604 wbuf_size = NET_TCP_BUFFERSIZE_HSPA_WRITE;
606 case VCONFKEY_TELEPHONY_PSTYPE_HSUPA:
607 rbuf_size = NET_TCP_BUFFERSIZE_HSUPA_READ;
608 wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
610 case VCONFKEY_TELEPHONY_PSTYPE_HSDPA:
611 rbuf_size = NET_TCP_BUFFERSIZE_HSDPA_READ;
612 wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
614 #if !defined TIZEN_WEARABLE
615 case VCONFKEY_TELEPHONY_PSTYPE_HSPAP:
616 rbuf_size = NET_TCP_BUFFERSIZE_HSPAP_READ;
617 wbuf_size = NET_TCP_BUFFERSIZE_HSPAP_WRITE;
621 switch (telephony_svctype) {
622 case VCONFKEY_TELEPHONY_SVCTYPE_LTE:
623 rbuf_size = NET_TCP_BUFFERSIZE_LTE_READ;
624 wbuf_size = NET_TCP_BUFFERSIZE_LTE_WRITE;
625 rmax_size = NET_TCP_BUFFERSIZE_LTE_RMEM_MAX;
627 case VCONFKEY_TELEPHONY_SVCTYPE_3G:
628 rbuf_size = NET_TCP_BUFFERSIZE_UMTS_READ;
629 wbuf_size = NET_TCP_BUFFERSIZE_UMTS_WRITE;
631 case VCONFKEY_TELEPHONY_SVCTYPE_2_5G_EDGE:
632 rbuf_size = NET_TCP_BUFFERSIZE_EDGE_READ;
633 wbuf_size = NET_TCP_BUFFERSIZE_EDGE_WRITE;
635 case VCONFKEY_TELEPHONY_SVCTYPE_2_5G:
636 rbuf_size = NET_TCP_BUFFERSIZE_GPRS_READ;
637 wbuf_size = NET_TCP_BUFFERSIZE_GPRS_WRITE;
640 /* TODO: Check LTE support */
641 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
642 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
648 DBG("Default TCP buffer configured");
650 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
651 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
654 if (rbuf_size != NULL) {
655 fdr = open(NET_PROC_SYS_NET_IPV4_TCP_RMEM, O_RDWR | O_CLOEXEC);
657 if (fdr < 0 || write(fdr, rbuf_size, strlen(rbuf_size)) < 0)
658 ERR("Failed to set TCP read buffer size");
664 if (wbuf_size != NULL) {
665 fdw = open(NET_PROC_SYS_NET_IPv4_TCP_WMEM, O_RDWR | O_CLOEXEC);
667 if (fdw < 0 || write(fdw, wbuf_size, strlen(wbuf_size)) < 0)
668 ERR("Failed to set TCP write buffer size");
675 if (rmax_size == NULL)
676 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
677 if (wmax_size == NULL)
678 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
680 if (rmax_size != NULL) {
681 fdrmax = open(NET_PROC_SYS_NET_CORE_RMEM_MAX, O_RDWR | O_CLOEXEC);
683 if (fdrmax < 0 || write(fdrmax, rmax_size, strlen(rmax_size)) < 0)
684 ERR("Failed to set TCP rmem_max size");
690 if (wmax_size != NULL) {
691 fdwmax = open(NET_PROC_SYS_NET_CORE_WMEM_MAX, O_RDWR | O_CLOEXEC);
693 if (fdwmax < 0 || write(fdwmax, wmax_size, strlen(wmax_size)) < 0)
694 ERR("Failed to set TCP wmem_max size");
701 static void __netconfig_update_default_connection_info(void)
703 int old_network_status = 0;
704 const char *profile = netconfig_get_default_profile();
705 const char *ip_addr = netconfig_get_default_ipaddress();
706 const char *ip_addr6 = netconfig_get_default_ipaddress6();
707 const char *proxy_addr = netconfig_get_default_proxy();
708 unsigned int freq = netconfig_get_default_frequency();
710 if (emulator_is_emulated() == TRUE) {
712 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr);
714 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
716 if (ip_addr6 != NULL)
717 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, ip_addr6);
719 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, "");
725 DBG("Reset network state configuration");
727 DBG("profile[%s] ipv4(%s) ipv6(%s) proxy(%s)", profile, ip_addr, ip_addr6, proxy_addr);
729 netconfig_vconf_get_int(VCONFKEY_NETWORK_STATUS, &old_network_status);
731 if (profile == NULL && old_network_status != VCONFKEY_NETWORK_OFF) {
732 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
734 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
735 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, "");
736 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
738 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 0);
739 netconfig_set_vconf_int("memory/private/wifi/frequency", 0);
741 DBG("Successfully clear IP and PROXY up");
743 } else if (profile != NULL) {
744 char *old_ip = vconf_get_str(VCONFKEY_NETWORK_IP);
745 char *old_ip6 = vconf_get_str(VCONFKEY_NETWORK_IP6);
746 char *old_proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY);
748 if (netconfig_is_wifi_profile(profile) == TRUE) {
749 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_WIFI);
750 netconfig_set_vconf_int("memory/private/wifi/frequency", freq);
752 netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
753 EKEY_NETWORK_STATUS, EVAL_NETWORK_WIFI);
754 } else if (netconfig_is_cellular_profile(profile)) {
756 if (!netconfig_is_cellular_internet_profile(profile)) {
757 DBG("connection is not a internet profile - stop to update the cellular state");
767 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_CELLULAR);
769 netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
770 EKEY_NETWORK_STATUS, EVAL_NETWORK_CELLULAR);
771 } else if (netconfig_is_ethernet_profile(profile) == TRUE) {
772 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_ETHERNET);
773 netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
774 EKEY_NETWORK_STATUS, EVAL_NETWORK_ETHERNET);
775 } else if (netconfig_is_bluetooth_profile(profile) == TRUE) {
776 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_BLUETOOTH);
777 netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
778 EKEY_NETWORK_STATUS, EVAL_NETWORK_BT);
780 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
781 netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
782 EKEY_NETWORK_STATUS, EVAL_NETWORK_DISCONNECTED);
785 if (g_strcmp0(old_ip, ip_addr) != 0 || old_ip == NULL) {
787 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr);
788 else if (old_ip != NULL && strlen(old_ip) > 0)
789 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
794 if (g_strcmp0(old_ip6, ip_addr6) != 0 || old_ip6 == NULL) {
795 if (ip_addr6 != NULL)
796 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, ip_addr6);
797 else if (old_ip6 != NULL && strlen(old_ip6) > 0)
798 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, "");
803 if (g_strcmp0(old_proxy, proxy_addr) != 0) {
804 if (proxy_addr == NULL)
805 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
807 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, proxy_addr);
812 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 1);
814 DBG("Successfully update default network configuration");
817 __netconfig_adjust_tcp_buffer_size();
820 static gboolean __netconfig_is_tech_state_connected(void)
822 gboolean ret = FALSE;
823 GVariant *message = NULL, *variant;
824 GVariantIter *iter, *next;
828 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
829 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
830 "GetTechnologies", NULL);
832 if (message == NULL) {
833 DBG("Fail to get technology state");
837 g_variant_get(message, "(a(oa{sv}))", &iter);
838 while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
842 while (g_variant_iter_loop(next, "{sv}", &key, &variant)) {
844 if (g_strcmp0(key, "Connected") == 0) {
845 data = g_variant_get_boolean(variant);
846 DBG("%s [%s: %s]", path, key, data ? "True" : "False");
851 g_variant_unref(variant);
852 g_variant_iter_free(next);
860 g_variant_iter_free(iter);
861 g_variant_unref(message);
866 static void __netconfig_update_if_service_connected(void)
868 GVariant *message = NULL, *var;
869 GVariantIter *iter, *next;
873 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
874 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
875 "GetServices", NULL);
877 if (message == NULL) {
878 ERR("Failed to get services");
882 g_variant_get(message, "(a(oa{sv}))", &iter);
883 while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
887 if (g_str_has_prefix(path,
888 CONNMAN_WIFI_SERVICE_PROFILE_PREFIX) == TRUE) {
889 if (g_strrstr(path + strlen(CONNMAN_WIFI_SERVICE_PROFILE_PREFIX),
891 /* skip hidden profiles */
895 } else if (g_str_has_prefix(path,
896 CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX) == TRUE) {
902 while (g_variant_iter_loop(next, "{sv}", &key, &var)) {
903 if (g_strcmp0(key, "State") == 0) {
904 const gchar *sdata = NULL;
905 sdata = g_variant_get_string(var, NULL);
906 DBG("%s [%s: %s]", path, key, sdata);
908 if (g_strcmp0(sdata, "online") == 0 || g_strcmp0(sdata, "ready") == 0) {
910 /* Found a connected WiFi / 3G service.
911 * Lets update the default profile info.
913 netconfig_update_default_profile((const gchar*)path);
916 g_variant_unref(var);
917 g_variant_iter_free(next);
924 g_variant_iter_free(iter);
925 g_variant_unref(message);
930 static void __netconfig_network_notify_result(const char *sig_name, const char *key)
932 GVariantBuilder *builder;
935 INFO("[Signal] %s %s", sig_name, key);
937 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
938 g_variant_builder_add(builder, "{sv}", "key", g_variant_new_string(key));
940 params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
941 g_variant_builder_unref(builder);
943 netconfig_dbus_emit_signal(NULL,
944 NETCONFIG_NETWORK_PATH,
945 NETCONFIG_NETWORK_INTERFACE,
952 const char *netconfig_get_default_profile(void)
954 return netconfig_default_connection_info.profile;
957 const char *netconfig_get_default_ifname(void)
959 return netconfig_default_connection_info.ifname;
962 const char *netconfig_get_default_ipaddress(void)
964 return netconfig_default_connection_info.ipaddress;
967 const char *netconfig_get_default_ipaddress6(void)
969 return netconfig_default_connection_info.ipaddress6;
972 const char *netconfig_get_default_proxy(void)
974 return netconfig_default_connection_info.proxy;
977 unsigned int netconfig_get_default_frequency(void)
979 return netconfig_default_connection_info.freq;
982 const char *netconfig_wifi_get_connected_essid(const char *default_profile)
984 if (default_profile == NULL)
987 if (netconfig_is_wifi_profile(default_profile) != TRUE)
990 if (g_strcmp0(default_profile, netconfig_default_connection_info.profile) != 0)
993 return netconfig_default_connection_info.essid;
996 gboolean netconfig_get_default_is_metered(void)
998 return netconfig_default_connection_info.is_metered;
1001 static int __netconfig_reset_ipv4_socket(void)
1006 struct sockaddr_in sai;
1007 const char *ipaddr = netconfig_get_default_ipaddress();
1008 DBG("ipaddr-[%s]", ipaddr);
1013 fd = socket(AF_INET, SOCK_DGRAM, 0);
1017 memset(&sai, 0, sizeof(struct sockaddr_in));
1018 sai.sin_family = AF_INET;
1020 if (!inet_aton(ipaddr, &sai.sin_addr)) {
1021 DBG("fail to inet_aton()");
1026 memset(&ifr, 0, sizeof(struct ifreq));
1027 memcpy(&ifr.ifr_addr, &sai, sizeof(sai));
1028 g_strlcpy((char *)ifr.ifr_name, WIFI_IFNAME, IFNAMSIZ);
1030 #ifndef SIOCKILLADDR
1031 #define SIOCKILLADDR 0x8939
1034 ret = ioctl(fd, SIOCKILLADDR, &ifr);
1036 DBG("fail to ioctl[SIOCKILLADDR]");
1045 void netconfig_update_default_profile(const char *profile)
1047 static char *old_profile = NULL;
1049 /* It's automatically updated by signal-handler
1050 * DO NOT update manually
1052 * It is going to update default connection information
1055 if (netconfig_default_connection_info.profile != NULL) {
1057 if (netconfig_is_wifi_profile(netconfig_default_connection_info.profile))
1058 __netconfig_reset_ipv4_socket();
1060 g_free(old_profile);
1061 old_profile = strdup(netconfig_default_connection_info.profile);
1063 g_free(netconfig_default_connection_info.profile);
1064 netconfig_default_connection_info.profile = NULL;
1066 g_free(netconfig_default_connection_info.ifname);
1067 netconfig_default_connection_info.ifname = NULL;
1069 g_free(netconfig_default_connection_info.ipaddress);
1070 netconfig_default_connection_info.ipaddress = NULL;
1072 g_free(netconfig_default_connection_info.ipaddress6);
1073 netconfig_default_connection_info.ipaddress6 = NULL;
1075 g_free(netconfig_default_connection_info.proxy);
1076 netconfig_default_connection_info.proxy = NULL;
1078 netconfig_default_connection_info.freq = 0;
1079 netconfig_default_connection_info.is_metered = FALSE;
1081 g_free(netconfig_default_connection_info.essid);
1082 netconfig_default_connection_info.essid = NULL;
1085 /* default profile is NULL and new connected profile is NULL */
1087 char *tmp_profile = __netconfig_get_default_profile();
1089 if (tmp_profile && netconfig_is_cellular_profile(tmp_profile) &&
1090 !netconfig_is_cellular_internet_profile(tmp_profile)) {
1091 DBG("not a default cellular profile");
1092 g_free(tmp_profile);
1097 __netconfig_update_default_connection_info();
1101 netconfig_default_connection_info.profile = g_strdup(tmp_profile);
1102 __netconfig_get_default_connection_info(tmp_profile);
1103 __netconfig_update_default_connection_info();
1104 g_free(tmp_profile);
1108 netconfig_default_connection_info.profile = g_strdup(profile);
1109 __netconfig_get_default_connection_info(profile);
1110 __netconfig_update_default_connection_info();
1113 void netconfig_update_default(void)
1115 if (__netconfig_is_tech_state_connected() == TRUE)
1116 __netconfig_update_if_service_connected();
1118 __netconfig_adjust_tcp_buffer_size();
1121 char *netconfig_get_ifname(const char *profile)
1123 GVariant *message = NULL, *variant;
1124 GVariantIter *iter, *next, *service;
1128 const gchar *value = NULL;
1129 gchar *ifname = NULL;
1130 gboolean found_profile = 0;
1132 if (profile == NULL)
1135 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
1136 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
1137 "GetServices", NULL);
1138 if (message == NULL) {
1139 ERR("Failed to get services informations");
1143 g_variant_get(message, "(a(oa{sv}))", &service);
1144 while (g_variant_iter_loop(service, "(oa{sv})", &obj_path, &iter)) {
1145 if (g_strcmp0(obj_path, profile) == 0) {
1152 if (found_profile == 0) {
1153 ERR("Profile %s doesn't exist", profile);
1154 g_variant_iter_free(service);
1155 g_variant_unref(message);
1159 while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
1160 if (g_strcmp0(key, "Ethernet") == 0) {
1161 while (g_variant_iter_loop(next, "{sv}", &key1, &variant)) {
1162 if (g_strcmp0(key1, "Interface") == 0) {
1163 value = g_variant_get_string(variant, NULL);
1164 ifname = g_strdup(value);
1166 g_variant_unref(variant);
1173 g_variant_unref(message);
1175 g_variant_iter_free(service);
1176 g_variant_iter_free(iter);
1181 /* Check Ethernet Cable Plug-in /Plug-out Status */
1182 void netconfig_network_notify_ethernet_cable_state(const char *key)
1184 __netconfig_network_notify_result("EthernetCableState", key);
1187 static gboolean handle_add_route(
1189 GDBusMethodInvocation *context,
1192 gchar *interface, gchar *gateway, gint address_family)
1194 const gchar *path = ROUTE_EXEC_PATH;
1195 gchar *const args[] = { "/sbin/route", "add", "-net", ip_addr,
1196 "netmask", netmask, "dev", interface, NULL };
1197 gchar *const envs[] = { NULL };
1198 const gchar* buf = NULL;
1203 DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
1205 switch (address_family) {
1207 if (ip_addr == NULL || netmask == NULL || interface == NULL) {
1208 ERR("Invalid parameter");
1209 netconfig_error_invalid_parameter(context);
1213 if (netconfig_execute_file(path, args, envs) < 0) {
1214 DBG("Failed to add a new route");
1215 netconfig_error_permission_denied(context);
1221 if (ip_addr == NULL || interface == NULL || gateway == NULL) {
1222 ERR("Invalid parameter");
1223 netconfig_error_invalid_parameter(context);
1228 ch = strchr(buf, '/');
1231 prefix_len = atoi(ch + 1);
1232 ip_addr[pos-1] = '\0';
1237 if (netconfig_add_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
1238 DBG("Failed to add a new route");
1239 netconfig_error_permission_denied(context);
1244 DBG("Unknown Address Family");
1245 netconfig_error_invalid_parameter(context);
1249 DBG("Successfully added a new route");
1250 network_complete_add_route(object, context, TRUE);
1254 static gboolean handle_remove_route(
1256 GDBusMethodInvocation *context,
1259 gchar *interface, gchar *gateway, gint address_family)
1261 const char *path = ROUTE_EXEC_PATH;
1262 gchar *const args[] = { "/sbin/route", "del", "-net", ip_addr,
1263 "netmask", netmask, "dev", interface, NULL };
1264 char *const envs[] = { NULL };
1265 const char* buf = NULL;
1270 DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
1272 switch (address_family) {
1274 if (ip_addr == NULL || netmask == NULL || interface == NULL) {
1275 DBG("Invalid parameter!");
1276 netconfig_error_invalid_parameter(context);
1279 if (netconfig_execute_file(path, args, envs) < 0) {
1280 DBG("Failed to remove the route");
1281 netconfig_error_permission_denied(context);
1286 if (ip_addr == NULL || interface == NULL || gateway == NULL) {
1287 DBG("Invalid parameter!");
1288 netconfig_error_invalid_parameter(context);
1293 ch = strchr(buf, '/');
1296 prefix_len = atoi(ch + 1);
1297 ip_addr[pos-1] = '\0';
1302 if (netconfig_del_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
1303 DBG("Failed to remove the route");
1304 netconfig_error_permission_denied(context);
1309 DBG("Unknown Address Family");
1310 netconfig_error_invalid_parameter(context);
1314 DBG("Successfully removed the route");
1315 network_complete_remove_route(object, context, TRUE);
1319 static gboolean handle_check_get_privilege(Network *object,
1320 GDBusMethodInvocation *context)
1322 network_complete_check_get_privilege(object, context);
1327 static gboolean handle_check_profile_privilege(Network *object,
1328 GDBusMethodInvocation *context)
1330 network_complete_check_profile_privilege(object, context);
1334 static gboolean handle_check_internet_privilege(Network *object,
1335 GDBusMethodInvocation *context)
1337 network_complete_check_internet_privilege(object, context);
1341 gboolean handle_ethernet_cable_state(Network *object,
1342 GDBusMethodInvocation *context)
1347 ret = netconfig_get_ethernet_cable_state(&state);
1349 DBG("Failed to get ethernet cable state");
1350 netconfig_error_fail_ethernet_cable_state(context);
1354 DBG("Successfully get ethernet cable state[%d]", state);
1355 network_complete_ethernet_cable_state(object, context, state);
1359 gboolean handle_get_metered_info(Network *object,
1360 GDBusMethodInvocation *context)
1364 state = netconfig_get_default_is_metered();
1366 DBG("Default metered state [%s]", state ? "TRUE" : "FALSE");
1367 network_complete_get_metered_info(object, context, state);
1371 gboolean handle_preferred_ipv6_address(Network *object,
1372 GDBusMethodInvocation *context, gchar *profile)
1374 char *address = NULL;
1376 address = __netconfig_get_preferred_ipv6_address(profile);
1377 if (address == NULL) {
1378 DBG("Failed to get preferred IPv6 address");
1379 netconfig_error_fail_preferred_ipv6_address(context);
1383 DBG("Successfully get preferred IPv6 address[%s]", address);
1384 network_complete_preferred_ipv6_address(object, context, address);
1388 void state_object_create_and_init(void)
1390 DBG("Creating network state object");
1391 GDBusInterfaceSkeleton *interface_network = NULL;
1392 GDBusConnection *connection = NULL;
1393 GDBusObjectManagerServer *server = netdbus_get_state_manager();
1397 connection = netdbus_get_connection();
1398 g_dbus_object_manager_server_set_connection(server, connection);
1400 /*Interface netconfig.network*/
1401 netconfigstate = network_skeleton_new();
1403 interface_network = G_DBUS_INTERFACE_SKELETON(netconfigstate);
1404 g_signal_connect(netconfigstate, "handle-add-route",
1405 G_CALLBACK(handle_add_route), NULL);
1406 g_signal_connect(netconfigstate, "handle-check-get-privilege",
1407 G_CALLBACK(handle_check_get_privilege), NULL);
1408 g_signal_connect(netconfigstate, "handle-check-profile-privilege",
1409 G_CALLBACK(handle_check_profile_privilege), NULL);
1410 g_signal_connect(netconfigstate, "handle-check-internet-privilege",
1411 G_CALLBACK(handle_check_internet_privilege), NULL);
1412 g_signal_connect(netconfigstate, "handle-ethernet-cable-state",
1413 G_CALLBACK(handle_ethernet_cable_state), NULL);
1414 g_signal_connect(netconfigstate, "handle-preferred-ipv6-address",
1415 G_CALLBACK(handle_preferred_ipv6_address), NULL);
1416 g_signal_connect(netconfigstate, "handle-remove-route",
1417 G_CALLBACK(handle_remove_route), NULL);
1418 g_signal_connect(netconfigstate, "handle-launch-mdns",
1419 G_CALLBACK(handle_launch_mdns), NULL);
1420 g_signal_connect(netconfigstate, "handle-device-policy-set-wifi",
1421 G_CALLBACK(handle_device_policy_set_wifi), NULL);
1422 g_signal_connect(netconfigstate, "handle-device-policy-get-wifi",
1423 G_CALLBACK(handle_device_policy_get_wifi), NULL);
1424 g_signal_connect(netconfigstate, "handle-device-policy-set-wifi-profile",
1425 G_CALLBACK(handle_device_policy_set_wifi_profile), NULL);
1426 g_signal_connect(netconfigstate, "handle-device-policy-get-wifi-profile",
1427 G_CALLBACK(handle_device_policy_get_wifi_profile), NULL);
1428 g_signal_connect(netconfigstate, "handle-get-metered-info",
1429 G_CALLBACK(handle_get_metered_info), NULL);
1431 if (!g_dbus_interface_skeleton_export(interface_network, connection,
1432 NETCONFIG_NETWORK_STATE_PATH, NULL)) {
1433 ERR("Export with path failed");
1437 void state_object_deinit(void)
1439 g_object_unref(netconfigstate);