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>
30 #include <TapiUtility.h>
37 #include "wifi-state.h"
38 #include "wifi-power.h"
39 #include "network-state.h"
40 #include "network-dpm.h"
41 #include "network-monitor.h"
42 #include "netsupplicant.h"
43 #include "wifi-tel-intf.h"
45 #include "generated-code.h"
46 /* Define TCP buffer sizes for various networks */
47 /* ReadMin, ReadInitial, ReadMax */ /* WriteMin, WriteInitial, WriteMax */
48 #define NET_TCP_BUFFERSIZE_DEFAULT_READ "4096 87380 704512"
49 #define NET_TCP_BUFFERSIZE_DEFAULT_WRITE "4096 16384 110208"
50 #define NET_TCP_BUFFERSIZE_WIFI_READ "524288 1048576 2560000"
51 #define NET_TCP_BUFFERSIZE_WIFI_WRITE "524288 1048576 2560000"
52 #define NET_TCP_BUFFERSIZE_LTE_READ "524288 1048576 2560000"
53 #define NET_TCP_BUFFERSIZE_LTE_WRITE "524288 1048576 2560000"
54 #define NET_TCP_BUFFERSIZE_UMTS_READ "4094 87380 704512"
55 #define NET_TCP_BUFFERSIZE_UMTS_WRITE "4096 16384 110208"
56 #define NET_TCP_BUFFERSIZE_HSPA_READ "4092 87380 704512"
57 #define NET_TCP_BUFFERSIZE_HSPA_WRITE "4096 16384 262144"
58 #define NET_TCP_BUFFERSIZE_HSDPA_READ "4092 87380 704512"
59 #define NET_TCP_BUFFERSIZE_HSDPA_WRITE "4096 16384 262144"
60 #define NET_TCP_BUFFERSIZE_HSUPA_READ "4092 87380 704512"
61 #define NET_TCP_BUFFERSIZE_HSUPA_WRITE "4096 16384 262144"
62 #define NET_TCP_BUFFERSIZE_HSPAP_READ "4092 87380 1220608"
63 #define NET_TCP_BUFFERSIZE_HSPAP_WRITE "4096 16384 1220608"
64 #define NET_TCP_BUFFERSIZE_EDGE_READ "4093 26280 35040"
65 #define NET_TCP_BUFFERSIZE_EDGE_WRITE "4096 16384 35040"
66 #define NET_TCP_BUFFERSIZE_GPRS_READ "4096 30000 30000"
67 #define NET_TCP_BUFFERSIZE_GPRS_WRITE "4096 8760 11680"
69 #define NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX "1048576"
70 #define NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX "2097152"
71 #define NET_TCP_BUFFERSIZE_LTE_RMEM_MAX "5242880"
73 #define NET_TCP_BUFFERSIZE_WIFID_WMEM_MAX "2097152"
75 #define NET_PROC_SYS_NET_IPV4_TCP_RMEM "/proc/sys/net/ipv4/tcp_rmem"
76 #define NET_PROC_SYS_NET_IPv4_TCP_WMEM "/proc/sys/net/ipv4/tcp_wmem"
77 #define NET_PROC_SYS_NET_CORE_RMEM_MAX "/proc/sys/net/core/rmem_max"
78 #define NET_PROC_SYS_NET_CORE_WMEM_MAX "/proc/sys/net/core/wmem_max"
80 #define ROUTE_EXEC_PATH "/sbin/route"
82 static Network *netconfigstate = NULL;
84 struct netconfig_default_connection {
94 static struct netconfig_default_connection
95 netconfig_default_connection_info = { NULL, };
97 gboolean netconfig_iface_network_state_ethernet_cable_state(gint32 *state);
99 static gboolean __netconfig_is_connected(GVariantIter *array)
101 gboolean is_connected = FALSE;
102 GVariant *variant = NULL;
104 const gchar *value = NULL;
106 while (g_variant_iter_loop(array, "{sv}", &key, &variant)) {
107 if (g_strcmp0(key, "State") != 0)
110 if (g_variant_is_of_type(variant, G_VARIANT_TYPE_STRING)) {
111 value = g_variant_get_string(variant, NULL);
112 if (g_strcmp0(value, "ready") == 0 || g_strcmp0(value, "online") == 0)
117 g_variant_unref(variant);
124 static char *__netconfig_get_default_profile(void)
126 GVariant *message = NULL;
129 gchar *default_profile = NULL;
132 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
133 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
134 "GetServices", NULL);
135 if (message == NULL) {
136 ERR("Failed to get profiles");
140 g_variant_get(message, "(a(oa{sv}))", &iter);
141 while (g_variant_iter_loop(iter, "(oa{sv})", &object_path, &next)) {
142 if (object_path == NULL)
145 if (netconfig_is_cellular_profile(object_path) && !netconfig_is_cellular_internet_profile(object_path))
148 if (__netconfig_is_connected(next) == TRUE) {
149 default_profile = g_strdup(object_path);
151 g_variant_iter_free(next);
155 g_variant_iter_free(iter);
156 g_variant_unref(message);
158 return default_profile;
161 static void __netconfig_get_default_connection_info(const char *profile)
163 GVariant *message = NULL, *variant = NULL, *variant2 = NULL;
164 GVariantIter *iter = NULL, *iter1 = NULL, *service = NULL;
165 GVariant *next = NULL;
170 gboolean found_profile = 0;
172 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
173 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
174 "GetServices", NULL);
175 if (message == NULL) {
176 ERR("Failed to get services informations");
180 g_variant_get(message, "(a(oa{sv}))", &service);
181 if (service == NULL) {
182 ERR("Failed to get services iter");
186 while (g_variant_iter_loop(service, "(oa{sv})", &obj_path, &iter)) {
187 if (g_strcmp0(obj_path, profile) == 0) {
194 if (iter == NULL || found_profile == 0) {
195 ERR("Profile %s doesn't exist", profile);
199 while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
200 const gchar *value = NULL;
202 if (g_strcmp0(key, "Name") == 0 &&
203 netconfig_is_wifi_profile(profile) == TRUE) {
204 if (g_variant_is_of_type(next, G_VARIANT_TYPE_STRING)) {
205 value = g_variant_get_string(next, NULL);
207 netconfig_default_connection_info.essid = g_strdup(value);
209 } else if (g_strcmp0(key, "Ethernet") == 0) {
210 g_variant_get(next, "a{sv}", &iter1);
213 while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
214 if (g_strcmp0(key1, "Interface") == 0) {
215 value = g_variant_get_string(variant, NULL);
216 netconfig_default_connection_info.ifname = g_strdup(value);
219 g_variant_iter_free(iter1);
220 } else if (g_strcmp0(key, "IPv4") == 0) {
221 g_variant_get(next, "a{sv}", &iter1);
224 while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
225 if (g_strcmp0(key1, "Address") == 0) {
226 value = g_variant_get_string(variant, NULL);
227 netconfig_default_connection_info.ipaddress = g_strdup(value);
230 g_variant_iter_free(iter1);
231 } else if (g_strcmp0(key, "IPv6") == 0) {
232 g_variant_get(next, "a{sv}", &iter1);
235 while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
236 if (g_strcmp0(key1, "Address") == 0) {
237 value = g_variant_get_string(variant, NULL);
238 netconfig_default_connection_info.ipaddress6 = g_strdup(value);
241 g_variant_iter_free(iter1);
243 } else if (g_strcmp0(key, "Proxy") == 0) {
244 g_variant_get(next, "a{sv}", &iter1);
247 while (g_variant_iter_loop(iter1, "{sv}", &key2, &variant2)) {
248 GVariantIter *iter_sub = NULL;
250 if (g_strcmp0(key2, "Servers") == 0) {
251 if (!g_variant_is_of_type(variant2, G_VARIANT_TYPE_STRING_ARRAY)) {
253 g_variant_unref(variant2);
257 g_variant_get(variant2, "as", &iter_sub);
258 g_variant_iter_loop(iter_sub, "s", &value);
259 g_variant_iter_free(iter_sub);
260 if (value != NULL && (strlen(value) > 0))
261 netconfig_default_connection_info.proxy = g_strdup(value);
262 } else if (g_strcmp0(key2, "Method") == 0) {
263 if (g_variant_is_of_type(variant2, G_VARIANT_TYPE_STRING)) {
265 g_variant_unref(variant2);
269 value = g_variant_get_string(variant2, NULL);
270 if (g_strcmp0(value, "direct") == 0) {
271 g_free(netconfig_default_connection_info.proxy);
272 netconfig_default_connection_info.proxy = NULL;
275 g_variant_unref(variant2);
280 g_variant_iter_free(iter1);
281 } else if (g_strcmp0(key, "Frequency") == 0) {
282 if (g_variant_is_of_type(next, G_VARIANT_TYPE_UINT16)) {
283 freq = g_variant_get_uint16(next);
284 netconfig_default_connection_info.freq = freq;
291 g_variant_unref(message);
294 g_variant_iter_free(iter);
297 g_variant_iter_free(service);
302 static void __netconfig_adjust_tcp_buffer_size(void)
304 int fdr = 0, fdw = 0;
305 int fdrmax = 0, fdwmax = 0;
306 const char *rbuf_size = NULL;
307 const char *wbuf_size = NULL;
308 const char *rmax_size = NULL;
309 const char *wmax_size = NULL;
310 const char *profile = netconfig_get_default_profile();
312 if (profile == NULL) {
313 DBG("There is no default connection");
315 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
316 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
317 } else if (netconfig_is_wifi_profile(profile) == TRUE) {
318 DBG("Default connection: Wi-Fi");
320 rbuf_size = NET_TCP_BUFFERSIZE_WIFI_READ;
321 wbuf_size = NET_TCP_BUFFERSIZE_WIFI_WRITE;
322 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
323 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
324 } else if (netconfig_is_cellular_profile(profile) == TRUE) {
325 TapiHandle *tapi_handle = NULL;
326 int telephony_svctype = 0, telephony_pstype = 0;
328 tapi_handle = (TapiHandle *)netconfig_tel_init();
329 if (NULL != tapi_handle) {
330 tel_get_property_int(tapi_handle,
331 TAPI_PROP_NETWORK_SERVICE_TYPE,
333 tel_get_property_int(tapi_handle, TAPI_PROP_NETWORK_PS_TYPE,
335 netconfig_tel_deinit();
338 DBG("Default cellular %d, %d", telephony_svctype, telephony_pstype);
340 switch (telephony_pstype) {
341 case VCONFKEY_TELEPHONY_PSTYPE_HSPA:
342 rbuf_size = NET_TCP_BUFFERSIZE_HSPA_READ;
343 wbuf_size = NET_TCP_BUFFERSIZE_HSPA_WRITE;
345 case VCONFKEY_TELEPHONY_PSTYPE_HSUPA:
346 rbuf_size = NET_TCP_BUFFERSIZE_HSUPA_READ;
347 wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
349 case VCONFKEY_TELEPHONY_PSTYPE_HSDPA:
350 rbuf_size = NET_TCP_BUFFERSIZE_HSDPA_READ;
351 wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
353 #if !defined TIZEN_WEARABLE
354 case VCONFKEY_TELEPHONY_PSTYPE_HSPAP:
355 rbuf_size = NET_TCP_BUFFERSIZE_HSPAP_READ;
356 wbuf_size = NET_TCP_BUFFERSIZE_HSPAP_WRITE;
360 switch (telephony_svctype) {
361 case VCONFKEY_TELEPHONY_SVCTYPE_LTE:
362 rbuf_size = NET_TCP_BUFFERSIZE_LTE_READ;
363 wbuf_size = NET_TCP_BUFFERSIZE_LTE_WRITE;
364 rmax_size = NET_TCP_BUFFERSIZE_LTE_RMEM_MAX;
366 case VCONFKEY_TELEPHONY_SVCTYPE_3G:
367 rbuf_size = NET_TCP_BUFFERSIZE_UMTS_READ;
368 wbuf_size = NET_TCP_BUFFERSIZE_UMTS_WRITE;
370 case VCONFKEY_TELEPHONY_SVCTYPE_2_5G_EDGE:
371 rbuf_size = NET_TCP_BUFFERSIZE_EDGE_READ;
372 wbuf_size = NET_TCP_BUFFERSIZE_EDGE_WRITE;
374 case VCONFKEY_TELEPHONY_SVCTYPE_2_5G:
375 rbuf_size = NET_TCP_BUFFERSIZE_GPRS_READ;
376 wbuf_size = NET_TCP_BUFFERSIZE_GPRS_WRITE;
379 /* TODO: Check LTE support */
380 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
381 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
387 DBG("Default TCP buffer configured");
389 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
390 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
393 if (rbuf_size != NULL) {
394 fdr = open(NET_PROC_SYS_NET_IPV4_TCP_RMEM, O_RDWR | O_CLOEXEC);
396 if (fdr < 0 || write(fdr, rbuf_size, strlen(rbuf_size)) < 0)
397 ERR("Failed to set TCP read buffer size");
403 if (wbuf_size != NULL) {
404 fdw = open(NET_PROC_SYS_NET_IPv4_TCP_WMEM, O_RDWR | O_CLOEXEC);
406 if (fdw < 0 || write(fdw, wbuf_size, strlen(wbuf_size)) < 0)
407 ERR("Failed to set TCP write buffer size");
414 if (rmax_size == NULL)
415 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
416 if (wmax_size == NULL)
417 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
419 if (rmax_size != NULL) {
420 fdrmax = open(NET_PROC_SYS_NET_CORE_RMEM_MAX, O_RDWR | O_CLOEXEC);
422 if (fdrmax < 0 || write(fdrmax, rmax_size, strlen(rmax_size)) < 0)
423 ERR("Failed to set TCP rmem_max size");
429 if (wmax_size != NULL) {
430 fdwmax = open(NET_PROC_SYS_NET_CORE_WMEM_MAX, O_RDWR | O_CLOEXEC);
432 if (fdwmax < 0 || write(fdwmax, wmax_size, strlen(wmax_size)) < 0)
433 ERR("Failed to set TCP wmem_max size");
440 static void __netconfig_update_default_connection_info(void)
442 int old_network_status = 0;
443 const char *profile = netconfig_get_default_profile();
444 const char *ip_addr = netconfig_get_default_ipaddress();
445 const char *ip_addr6 = netconfig_get_default_ipaddress6();
446 const char *proxy_addr = netconfig_get_default_proxy();
447 unsigned int freq = netconfig_get_default_frequency();
449 if (emulator_is_emulated() == TRUE) {
451 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr);
453 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
455 if (ip_addr6 != NULL)
456 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, ip_addr6);
458 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, "");
464 DBG("Reset network state configuration");
466 DBG("%s: ip(%s) ip6(%s) proxy(%s)", profile, ip_addr, ip_addr6, proxy_addr);
468 netconfig_vconf_get_int(VCONFKEY_NETWORK_STATUS, &old_network_status);
470 if (profile == NULL && old_network_status != VCONFKEY_NETWORK_OFF) {
471 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
473 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
474 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, "");
475 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
477 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 0);
478 netconfig_set_vconf_int("memory/private/wifi/frequency", 0);
480 DBG("Successfully clear IP and PROXY up");
482 } else if (profile != NULL) {
483 char *old_ip = vconf_get_str(VCONFKEY_NETWORK_IP);
484 char *old_ip6 = vconf_get_str(VCONFKEY_NETWORK_IP6);
485 char *old_proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY);
487 if (netconfig_is_wifi_profile(profile) == TRUE) {
488 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_WIFI);
489 netconfig_set_vconf_int("memory/private/wifi/frequency", freq);
491 netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
492 EKEY_NETWORK_STATUS, EVAL_NETWORK_WIFI);
493 } else if (netconfig_is_cellular_profile(profile)) {
495 if (!netconfig_is_cellular_internet_profile(profile)) {
496 DBG("connection is not a internet profile - stop to update the cellular state");
500 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_CELLULAR);
502 netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
503 EKEY_NETWORK_STATUS, EVAL_NETWORK_CELLULAR);
504 } else if (netconfig_is_ethernet_profile(profile) == TRUE) {
505 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_ETHERNET);
506 netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
507 EKEY_NETWORK_STATUS, EVAL_NETWORK_ETHERNET);
508 } else if (netconfig_is_bluetooth_profile(profile) == TRUE) {
509 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_BLUETOOTH);
510 netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
511 EKEY_NETWORK_STATUS, EVAL_NETWORK_BT);
513 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
514 netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
515 EKEY_NETWORK_STATUS, EVAL_NETWORK_DISCONNECTED);
518 if (g_strcmp0(old_ip, ip_addr) != 0 || old_ip == NULL) {
520 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr);
521 else if (old_ip != NULL && strlen(old_ip) > 0)
522 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
526 if (g_strcmp0(old_ip6, ip_addr6) != 0 || old_ip6 == NULL) {
527 if (ip_addr6 != NULL)
528 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, ip_addr6);
529 else if (old_ip6 != NULL && strlen(old_ip6) > 0)
530 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, "");
534 if (g_strcmp0(old_proxy, proxy_addr) != 0) {
535 if (proxy_addr == NULL)
536 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
538 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, proxy_addr);
542 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 1);
544 DBG("Successfully update default network configuration");
547 __netconfig_adjust_tcp_buffer_size();
550 static gboolean __netconfig_is_tech_state_connected(void)
552 gboolean ret = FALSE;
553 GVariant *message = NULL, *variant;
554 GVariantIter *iter, *next;
558 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
559 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
560 "GetTechnologies", NULL);
562 if (message == NULL) {
563 DBG("Fail to get technology state");
567 g_variant_get(message, "(a(oa{sv}))", &iter);
568 while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
572 while (g_variant_iter_loop(next, "{sv}", &key, &variant)) {
574 if (g_strcmp0(key, "Connected") == 0) {
575 data = g_variant_get_boolean(variant);
576 DBG("%s [%s: %s]", path, key, data ? "True" : "False");
581 g_variant_unref(variant);
582 g_variant_iter_free(next);
590 g_variant_iter_free(iter);
591 g_variant_unref(message);
596 static void __netconfig_update_if_service_connected(void)
598 GVariant *message = NULL, *var;
599 GVariantIter *iter, *next;
603 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
604 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
605 "GetServices", NULL);
607 if (message == NULL) {
608 ERR("Failed to get services");
612 g_variant_get(message, "(a(oa{sv}))", &iter);
613 while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
617 if (g_str_has_prefix(path,
618 CONNMAN_WIFI_SERVICE_PROFILE_PREFIX) == TRUE) {
619 if (g_strrstr(path + strlen(CONNMAN_WIFI_SERVICE_PROFILE_PREFIX),
621 /* skip hidden profiles */
625 } else if (g_str_has_prefix(path,
626 CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX) == TRUE) {
632 while (g_variant_iter_loop(next, "{sv}", &key, &var)) {
633 if (g_strcmp0(key, "State") == 0) {
634 const gchar *sdata = NULL;
635 sdata = g_variant_get_string(var, NULL);
636 DBG("%s [%s: %s]", path, key, sdata);
638 if (g_strcmp0(sdata, "online") == 0 || g_strcmp0(sdata, "ready") == 0) {
640 /* Found a connected WiFi / 3G service.
641 * Lets update the default profile info.
643 netconfig_update_default_profile((const gchar*)path);
646 g_variant_unref(var);
647 g_variant_iter_free(next);
654 g_variant_iter_free(iter);
655 g_variant_unref(message);
660 static void __netconfig_network_notify_result(const char *sig_name, const char *key)
662 GVariantBuilder *builder;
665 INFO("[Signal] %s %s", sig_name, key);
667 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
668 g_variant_builder_add(builder, "{sv}", "key", g_variant_new_string(key));
670 params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
671 g_variant_builder_unref(builder);
673 netconfig_dbus_emit_signal(NULL,
674 NETCONFIG_NETWORK_PATH,
675 NETCONFIG_NETWORK_INTERFACE,
682 const char *netconfig_get_default_profile(void)
684 return netconfig_default_connection_info.profile;
687 const char *netconfig_get_default_ifname(void)
689 return netconfig_default_connection_info.ifname;
692 const char *netconfig_get_default_ipaddress(void)
694 return netconfig_default_connection_info.ipaddress;
697 const char *netconfig_get_default_ipaddress6(void)
699 return netconfig_default_connection_info.ipaddress6;
702 const char *netconfig_get_default_proxy(void)
704 return netconfig_default_connection_info.proxy;
707 unsigned int netconfig_get_default_frequency(void)
709 return netconfig_default_connection_info.freq;
712 const char *netconfig_wifi_get_connected_essid(const char *default_profile)
714 if (default_profile == NULL)
717 if (netconfig_is_wifi_profile(default_profile) != TRUE)
720 if (g_strcmp0(default_profile, netconfig_default_connection_info.profile) != 0)
723 return netconfig_default_connection_info.essid;
726 static int __netconfig_reset_ipv4_socket(void)
731 struct sockaddr_in sai;
732 const char *ipaddr = netconfig_get_default_ipaddress();
733 DBG("ipaddr-[%s]", ipaddr);
738 fd = socket(AF_INET, SOCK_DGRAM, 0);
742 memset(&sai, 0, sizeof(struct sockaddr_in));
743 sai.sin_family = AF_INET;
745 if (!inet_aton(ipaddr, &sai.sin_addr)) {
746 DBG("fail to inet_aton()");
751 memset(&ifr, 0, sizeof(struct ifreq));
752 memcpy(&ifr.ifr_addr, &sai, sizeof(sai));
753 g_strlcpy((char *)ifr.ifr_name, WIFI_IFNAME, IFNAMSIZ);
756 #define SIOCKILLADDR 0x8939
759 ret = ioctl(fd, SIOCKILLADDR, &ifr);
761 DBG("fail to ioctl[SIOCKILLADDR]");
770 void netconfig_update_default_profile(const char *profile)
772 static char *old_profile = NULL;
774 /* It's automatically updated by signal-handler
775 * DO NOT update manually
777 * It is going to update default connection information
780 if (netconfig_default_connection_info.profile != NULL) {
782 if (netconfig_is_wifi_profile(netconfig_default_connection_info.profile))
783 __netconfig_reset_ipv4_socket();
786 old_profile = strdup(netconfig_default_connection_info.profile);
788 g_free(netconfig_default_connection_info.profile);
789 netconfig_default_connection_info.profile = NULL;
791 g_free(netconfig_default_connection_info.ifname);
792 netconfig_default_connection_info.ifname = NULL;
794 g_free(netconfig_default_connection_info.ipaddress);
795 netconfig_default_connection_info.ipaddress = NULL;
797 g_free(netconfig_default_connection_info.ipaddress6);
798 netconfig_default_connection_info.ipaddress6 = NULL;
800 g_free(netconfig_default_connection_info.proxy);
801 netconfig_default_connection_info.proxy = NULL;
803 netconfig_default_connection_info.freq = 0;
805 if (wifi_state_get_service_state() != NETCONFIG_WIFI_CONNECTED) {
806 g_free(netconfig_default_connection_info.essid);
807 netconfig_default_connection_info.essid = NULL;
811 /* default profile is NULL and new connected profile is NULL */
813 profile = __netconfig_get_default_profile();
815 if (profile && netconfig_is_cellular_profile(profile) &&
816 !netconfig_is_cellular_internet_profile(profile)) {
817 DBG("not a default cellular profile");
822 __netconfig_update_default_connection_info();
827 netconfig_default_connection_info.profile = g_strdup(profile);
828 __netconfig_get_default_connection_info(profile);
829 __netconfig_update_default_connection_info();
833 void netconfig_update_default(void)
835 if (__netconfig_is_tech_state_connected() == TRUE)
836 __netconfig_update_if_service_connected();
838 __netconfig_adjust_tcp_buffer_size();
841 char *netconfig_get_ifname(const char *profile)
843 GVariant *message = NULL, *variant;
844 GVariantIter *iter, *next, *service;
848 const gchar *value = NULL;
849 gchar *ifname = NULL;
850 gboolean found_profile = 0;
855 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
856 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
857 "GetServices", NULL);
858 if (message == NULL) {
859 ERR("Failed to get services informations");
863 g_variant_get(message, "(a(oa{sv}))", &service);
864 while (g_variant_iter_loop(service, "(oa{sv})", &obj_path, &iter)) {
865 if (g_strcmp0(obj_path, profile) == 0) {
872 if (found_profile == 0) {
873 ERR("Profile %s doesn't exist", profile);
874 g_variant_iter_free(service);
875 g_variant_unref(message);
879 while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
880 if (g_strcmp0(key, "Ethernet") == 0) {
881 while (g_variant_iter_loop(next, "{sv}", &key1, &variant)) {
882 if (g_strcmp0(key1, "Interface") == 0) {
883 value = g_variant_get_string(variant, NULL);
886 ifname = g_strdup(value);
892 g_variant_unref(message);
894 g_variant_iter_free(service);
895 g_variant_iter_free(iter);
900 /* Check Ethernet Cable Plug-in /Plug-out Status */
901 void netconfig_network_notify_ethernet_cable_state(const char *key)
903 __netconfig_network_notify_result("EthernetCableState", key);
906 static gboolean handle_add_route(
908 GDBusMethodInvocation *context,
911 gchar *interface, gchar *gateway, gint address_family)
913 const gchar *path = ROUTE_EXEC_PATH;
914 gchar *const args[] = { "/sbin/route", "add", "-net", ip_addr,
915 "netmask", netmask, "dev", interface, NULL };
916 gchar *const envs[] = { NULL };
917 const gchar* buf = NULL;
922 DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
924 switch (address_family) {
926 if (ip_addr == NULL || netmask == NULL || interface == NULL) {
927 ERR("Invalid parameter");
928 netconfig_error_invalid_parameter(context);
932 if (netconfig_execute_file(path, args, envs) < 0) {
933 DBG("Failed to add a new route");
934 netconfig_error_permission_denied(context);
940 if (ip_addr == NULL || interface == NULL || gateway == NULL) {
941 ERR("Invalid parameter");
942 netconfig_error_invalid_parameter(context);
947 ch = strchr(buf, '/');
950 prefix_len = atoi(ch + 1);
951 ip_addr[pos-1] = '\0';
956 if (netconfig_add_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
957 DBG("Failed to add a new route");
958 netconfig_error_permission_denied(context);
963 DBG("Unknown Address Family");
964 netconfig_error_invalid_parameter(context);
968 DBG("Successfully added a new route");
969 network_complete_add_route(object, context, TRUE);
973 static gboolean handle_remove_route(
975 GDBusMethodInvocation *context,
978 gchar *interface, gchar *gateway, gint address_family)
980 const char *path = ROUTE_EXEC_PATH;
981 gchar *const args[] = { "/sbin/route", "del", "-net", ip_addr,
982 "netmask", netmask, "dev", interface, NULL };
983 char *const envs[] = { NULL };
984 const char* buf = NULL;
989 DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
991 switch (address_family) {
993 if (ip_addr == NULL || netmask == NULL || interface == NULL) {
994 DBG("Invalid parameter!");
995 netconfig_error_invalid_parameter(context);
998 if (netconfig_execute_file(path, args, envs) < 0) {
999 DBG("Failed to remove the route");
1000 netconfig_error_permission_denied(context);
1005 if (ip_addr == NULL || interface == NULL || gateway == NULL) {
1006 DBG("Invalid parameter!");
1007 netconfig_error_invalid_parameter(context);
1012 ch = strchr(buf, '/');
1015 prefix_len = atoi(ch + 1);
1016 ip_addr[pos-1] = '\0';
1021 if (netconfig_del_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
1022 DBG("Failed to remove the route");
1023 netconfig_error_permission_denied(context);
1028 DBG("Unknown Address Family");
1029 netconfig_error_invalid_parameter(context);
1033 DBG("Successfully removed the route");
1034 network_complete_remove_route(object, context, TRUE);
1038 static gboolean handle_check_get_privilege(Network *object,
1039 GDBusMethodInvocation *context)
1041 network_complete_check_get_privilege(object, context);
1046 static gboolean handle_check_profile_privilege(Network *object,
1047 GDBusMethodInvocation *context)
1049 network_complete_check_profile_privilege(object, context);
1053 static gboolean handle_check_internet_privilege(Network *object,
1054 GDBusMethodInvocation *context)
1056 network_complete_check_internet_privilege(object, context);
1060 gboolean handle_ethernet_cable_state(Network *object,
1061 GDBusMethodInvocation *context)
1066 ret = netconfig_get_ethernet_cable_state(&state);
1068 DBG("Failed to get ethernet cable state");
1069 netconfig_error_fail_ethernet_cable_state(context);
1073 DBG("Successfully get ethernet cable state[%d]", state);
1074 network_complete_ethernet_cable_state(object, context, state);
1078 void state_object_create_and_init(void)
1080 DBG("Creating network state object");
1081 GDBusInterfaceSkeleton *interface_network = NULL;
1082 GDBusConnection *connection = NULL;
1083 GDBusObjectManagerServer *server = netdbus_get_state_manager();
1087 connection = netdbus_get_connection();
1088 g_dbus_object_manager_server_set_connection(server, connection);
1090 /*Interface netconfig.network*/
1091 netconfigstate = network_skeleton_new();
1093 interface_network = G_DBUS_INTERFACE_SKELETON(netconfigstate);
1094 g_signal_connect(netconfigstate, "handle-add-route",
1095 G_CALLBACK(handle_add_route), NULL);
1096 g_signal_connect(netconfigstate, "handle-check-get-privilege",
1097 G_CALLBACK(handle_check_get_privilege), NULL);
1098 g_signal_connect(netconfigstate, "handle-check-profile-privilege",
1099 G_CALLBACK(handle_check_profile_privilege), NULL);
1100 g_signal_connect(netconfigstate, "handle-check-internet-privilege",
1101 G_CALLBACK(handle_check_internet_privilege), NULL);
1102 g_signal_connect(netconfigstate, "handle-ethernet-cable-state",
1103 G_CALLBACK(handle_ethernet_cable_state), NULL);
1104 g_signal_connect(netconfigstate, "handle-remove-route",
1105 G_CALLBACK(handle_remove_route), NULL);
1106 g_signal_connect(netconfigstate, "handle-launch-mdns",
1107 G_CALLBACK(handle_launch_mdns), NULL);
1108 g_signal_connect(netconfigstate, "handle-device-policy-set-wifi",
1109 G_CALLBACK(handle_device_policy_set_wifi), NULL);
1110 g_signal_connect(netconfigstate, "handle-device-policy-get-wifi",
1111 G_CALLBACK(handle_device_policy_get_wifi), NULL);
1112 g_signal_connect(netconfigstate, "handle-device-policy-set-wifi-profile",
1113 G_CALLBACK(handle_device_policy_set_wifi_profile), NULL);
1114 g_signal_connect(netconfigstate, "handle-device-policy-get-wifi-profile",
1115 G_CALLBACK(handle_device_policy_get_wifi_profile), NULL);
1117 if (!g_dbus_interface_skeleton_export(interface_network, connection,
1118 NETCONFIG_NETWORK_STATE_PATH, NULL)) {
1119 ERR("Export with path failed");
1123 void state_object_deinit(void)
1125 g_object_unref(netconfigstate);