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>
32 #include <bundle_internal.h>
33 #include <eventsystem.h>
40 #include "wifi-state.h"
41 #include "wifi-power.h"
42 #include "network-state.h"
43 #include "network-dpm.h"
44 #include "network-monitor.h"
45 #include "netsupplicant.h"
46 #include "wifi-tel-intf.h"
47 #include "clatd-handler.h"
49 #include "generated-code.h"
50 /* Define TCP buffer sizes for various networks */
51 /* ReadMin, ReadInitial, ReadMax */ /* WriteMin, WriteInitial, WriteMax */
52 #define NET_TCP_BUFFERSIZE_DEFAULT_READ "4096 87380 704512"
53 #define NET_TCP_BUFFERSIZE_DEFAULT_WRITE "4096 16384 110208"
54 #define NET_TCP_BUFFERSIZE_WIFI_READ "524288 1048576 2560000"
55 #define NET_TCP_BUFFERSIZE_WIFI_WRITE "524288 1048576 2560000"
56 #define NET_TCP_BUFFERSIZE_LTE_READ "524288 1048576 2560000"
57 #define NET_TCP_BUFFERSIZE_LTE_WRITE "524288 1048576 2560000"
58 #define NET_TCP_BUFFERSIZE_UMTS_READ "4094 87380 704512"
59 #define NET_TCP_BUFFERSIZE_UMTS_WRITE "4096 16384 110208"
60 #define NET_TCP_BUFFERSIZE_HSPA_READ "4092 87380 704512"
61 #define NET_TCP_BUFFERSIZE_HSPA_WRITE "4096 16384 262144"
62 #define NET_TCP_BUFFERSIZE_HSDPA_READ "4092 87380 704512"
63 #define NET_TCP_BUFFERSIZE_HSDPA_WRITE "4096 16384 262144"
64 #define NET_TCP_BUFFERSIZE_HSUPA_READ "4092 87380 704512"
65 #define NET_TCP_BUFFERSIZE_HSUPA_WRITE "4096 16384 262144"
66 #define NET_TCP_BUFFERSIZE_HSPAP_READ "4092 87380 1220608"
67 #define NET_TCP_BUFFERSIZE_HSPAP_WRITE "4096 16384 1220608"
68 #define NET_TCP_BUFFERSIZE_EDGE_READ "4093 26280 35040"
69 #define NET_TCP_BUFFERSIZE_EDGE_WRITE "4096 16384 35040"
70 #define NET_TCP_BUFFERSIZE_GPRS_READ "4096 30000 30000"
71 #define NET_TCP_BUFFERSIZE_GPRS_WRITE "4096 8760 11680"
73 #define NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX "1048576"
74 #define NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX "2097152"
75 #define NET_TCP_BUFFERSIZE_LTE_RMEM_MAX "5242880"
77 #define NET_TCP_BUFFERSIZE_WIFID_WMEM_MAX "2097152"
79 #define NET_PROC_SYS_NET_IPV4_TCP_RMEM "/proc/sys/net/ipv4/tcp_rmem"
80 #define NET_PROC_SYS_NET_IPv4_TCP_WMEM "/proc/sys/net/ipv4/tcp_wmem"
81 #define NET_PROC_SYS_NET_CORE_RMEM_MAX "/proc/sys/net/core/rmem_max"
82 #define NET_PROC_SYS_NET_CORE_WMEM_MAX "/proc/sys/net/core/wmem_max"
84 #define ROUTE_EXEC_PATH "/sbin/route"
86 static Network *netconfigstate = NULL;
88 struct netconfig_default_connection {
98 static struct netconfig_default_connection
99 netconfig_default_connection_info = { NULL, };
101 gboolean netconfig_iface_network_state_ethernet_cable_state(gint32 *state);
103 static gboolean __netconfig_is_connected(GVariantIter *array)
105 gboolean is_connected = FALSE;
106 GVariant *variant = NULL;
108 const gchar *value = NULL;
110 while (g_variant_iter_loop(array, "{sv}", &key, &variant)) {
111 if (g_strcmp0(key, "State") != 0)
114 if (g_variant_is_of_type(variant, G_VARIANT_TYPE_STRING)) {
115 value = g_variant_get_string(variant, NULL);
116 if (g_strcmp0(value, "ready") == 0 || g_strcmp0(value, "online") == 0)
121 g_variant_unref(variant);
128 static char *__netconfig_get_default_profile(void)
130 GVariant *message = NULL;
133 gchar *default_profile = NULL;
136 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
137 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
138 "GetServices", NULL);
139 if (message == NULL) {
140 ERR("Failed to get profiles");
144 g_variant_get(message, "(a(oa{sv}))", &iter);
145 while (g_variant_iter_loop(iter, "(oa{sv})", &object_path, &next)) {
146 if (object_path == NULL)
149 if (netconfig_is_cellular_profile(object_path) && !netconfig_is_cellular_internet_profile(object_path))
152 if (__netconfig_is_connected(next) == TRUE) {
153 default_profile = g_strdup(object_path);
155 g_variant_iter_free(next);
159 g_variant_iter_free(iter);
160 g_variant_unref(message);
162 return default_profile;
165 static void __netconfig_get_default_connection_info(const char *profile)
167 GVariant *message = NULL, *variant = NULL, *variant2 = NULL;
168 GVariantIter *iter = NULL, *iter1 = NULL, *service = NULL;
169 GVariant *next = NULL;
174 gboolean found_profile = 0;
176 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
177 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
178 "GetServices", NULL);
179 if (message == NULL) {
180 ERR("Failed to get services informations");
184 g_variant_get(message, "(a(oa{sv}))", &service);
185 if (service == NULL) {
186 ERR("Failed to get services iter");
190 while (g_variant_iter_loop(service, "(oa{sv})", &obj_path, &iter)) {
191 if (g_strcmp0(obj_path, profile) == 0) {
198 if (iter == NULL || found_profile == 0) {
199 ERR("Profile %s doesn't exist", profile);
203 while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
204 const gchar *value = NULL;
206 if (g_strcmp0(key, "Name") == 0 &&
207 netconfig_is_wifi_profile(profile) == TRUE) {
208 if (g_variant_is_of_type(next, G_VARIANT_TYPE_STRING)) {
209 value = g_variant_get_string(next, NULL);
211 netconfig_default_connection_info.essid = g_strdup(value);
213 } else if (g_strcmp0(key, "Ethernet") == 0) {
214 g_variant_get(next, "a{sv}", &iter1);
217 while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
218 if (g_strcmp0(key1, "Interface") == 0) {
219 value = g_variant_get_string(variant, NULL);
220 netconfig_default_connection_info.ifname = g_strdup(value);
223 g_variant_iter_free(iter1);
224 } else if (g_strcmp0(key, "IPv4") == 0) {
225 g_variant_get(next, "a{sv}", &iter1);
228 while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
229 if (g_strcmp0(key1, "Address") == 0) {
230 value = g_variant_get_string(variant, NULL);
231 netconfig_default_connection_info.ipaddress = g_strdup(value);
234 g_variant_iter_free(iter1);
235 } else if (g_strcmp0(key, "IPv6") == 0) {
236 g_variant_get(next, "a{sv}", &iter1);
239 while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
240 if (g_strcmp0(key1, "Address") == 0) {
241 value = g_variant_get_string(variant, NULL);
242 netconfig_default_connection_info.ipaddress6 = g_strdup(value);
245 g_variant_iter_free(iter1);
247 } else if (g_strcmp0(key, "Proxy") == 0) {
248 g_variant_get(next, "a{sv}", &iter1);
251 while (g_variant_iter_loop(iter1, "{sv}", &key2, &variant2)) {
252 GVariantIter *iter_sub = NULL;
254 if (g_strcmp0(key2, "Servers") == 0) {
255 if (!g_variant_is_of_type(variant2, G_VARIANT_TYPE_STRING_ARRAY)) {
257 g_variant_unref(variant2);
261 g_variant_get(variant2, "as", &iter_sub);
262 g_variant_iter_loop(iter_sub, "s", &value);
263 g_variant_iter_free(iter_sub);
264 if (value != NULL && (strlen(value) > 0))
265 netconfig_default_connection_info.proxy = g_strdup(value);
266 } else if (g_strcmp0(key2, "Method") == 0) {
267 if (g_variant_is_of_type(variant2, G_VARIANT_TYPE_STRING)) {
269 g_variant_unref(variant2);
273 value = g_variant_get_string(variant2, NULL);
274 if (g_strcmp0(value, "direct") == 0) {
275 g_free(netconfig_default_connection_info.proxy);
276 netconfig_default_connection_info.proxy = NULL;
279 g_variant_unref(variant2);
284 g_variant_iter_free(iter1);
285 } else if (g_strcmp0(key, "Frequency") == 0) {
286 if (g_variant_is_of_type(next, G_VARIANT_TYPE_UINT16)) {
287 freq = g_variant_get_uint16(next);
288 netconfig_default_connection_info.freq = freq;
295 g_variant_unref(message);
298 g_variant_iter_free(iter);
301 g_variant_iter_free(service);
306 static void __netconfig_adjust_tcp_buffer_size(void)
308 int fdr = 0, fdw = 0;
309 int fdrmax = 0, fdwmax = 0;
310 const char *rbuf_size = NULL;
311 const char *wbuf_size = NULL;
312 const char *rmax_size = NULL;
313 const char *wmax_size = NULL;
314 const char *profile = netconfig_get_default_profile();
316 if (profile == NULL) {
317 DBG("There is no default connection");
319 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
320 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
321 } else if (netconfig_is_wifi_profile(profile) == TRUE) {
322 DBG("Default connection: Wi-Fi");
324 rbuf_size = NET_TCP_BUFFERSIZE_WIFI_READ;
325 wbuf_size = NET_TCP_BUFFERSIZE_WIFI_WRITE;
326 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
327 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
328 } else if (netconfig_is_cellular_profile(profile) == TRUE) {
329 TapiHandle *tapi_handle = NULL;
330 int telephony_svctype = 0, telephony_pstype = 0;
332 tapi_handle = (TapiHandle *)netconfig_tel_init();
333 if (NULL != tapi_handle) {
334 tel_get_property_int(tapi_handle,
335 TAPI_PROP_NETWORK_SERVICE_TYPE,
337 tel_get_property_int(tapi_handle, TAPI_PROP_NETWORK_PS_TYPE,
339 netconfig_tel_deinit();
342 DBG("Default cellular %d, %d", telephony_svctype, telephony_pstype);
344 switch (telephony_pstype) {
345 case VCONFKEY_TELEPHONY_PSTYPE_HSPA:
346 rbuf_size = NET_TCP_BUFFERSIZE_HSPA_READ;
347 wbuf_size = NET_TCP_BUFFERSIZE_HSPA_WRITE;
349 case VCONFKEY_TELEPHONY_PSTYPE_HSUPA:
350 rbuf_size = NET_TCP_BUFFERSIZE_HSUPA_READ;
351 wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
353 case VCONFKEY_TELEPHONY_PSTYPE_HSDPA:
354 rbuf_size = NET_TCP_BUFFERSIZE_HSDPA_READ;
355 wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
357 #if !defined TIZEN_WEARABLE
358 case VCONFKEY_TELEPHONY_PSTYPE_HSPAP:
359 rbuf_size = NET_TCP_BUFFERSIZE_HSPAP_READ;
360 wbuf_size = NET_TCP_BUFFERSIZE_HSPAP_WRITE;
364 switch (telephony_svctype) {
365 case VCONFKEY_TELEPHONY_SVCTYPE_LTE:
366 rbuf_size = NET_TCP_BUFFERSIZE_LTE_READ;
367 wbuf_size = NET_TCP_BUFFERSIZE_LTE_WRITE;
368 rmax_size = NET_TCP_BUFFERSIZE_LTE_RMEM_MAX;
370 case VCONFKEY_TELEPHONY_SVCTYPE_3G:
371 rbuf_size = NET_TCP_BUFFERSIZE_UMTS_READ;
372 wbuf_size = NET_TCP_BUFFERSIZE_UMTS_WRITE;
374 case VCONFKEY_TELEPHONY_SVCTYPE_2_5G_EDGE:
375 rbuf_size = NET_TCP_BUFFERSIZE_EDGE_READ;
376 wbuf_size = NET_TCP_BUFFERSIZE_EDGE_WRITE;
378 case VCONFKEY_TELEPHONY_SVCTYPE_2_5G:
379 rbuf_size = NET_TCP_BUFFERSIZE_GPRS_READ;
380 wbuf_size = NET_TCP_BUFFERSIZE_GPRS_WRITE;
383 /* TODO: Check LTE support */
384 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
385 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
391 DBG("Default TCP buffer configured");
393 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
394 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
397 if (rbuf_size != NULL) {
398 fdr = open(NET_PROC_SYS_NET_IPV4_TCP_RMEM, O_RDWR | O_CLOEXEC);
400 if (fdr < 0 || write(fdr, rbuf_size, strlen(rbuf_size)) < 0)
401 ERR("Failed to set TCP read buffer size");
407 if (wbuf_size != NULL) {
408 fdw = open(NET_PROC_SYS_NET_IPv4_TCP_WMEM, O_RDWR | O_CLOEXEC);
410 if (fdw < 0 || write(fdw, wbuf_size, strlen(wbuf_size)) < 0)
411 ERR("Failed to set TCP write buffer size");
418 if (rmax_size == NULL)
419 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
420 if (wmax_size == NULL)
421 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
423 if (rmax_size != NULL) {
424 fdrmax = open(NET_PROC_SYS_NET_CORE_RMEM_MAX, O_RDWR | O_CLOEXEC);
426 if (fdrmax < 0 || write(fdrmax, rmax_size, strlen(rmax_size)) < 0)
427 ERR("Failed to set TCP rmem_max size");
433 if (wmax_size != NULL) {
434 fdwmax = open(NET_PROC_SYS_NET_CORE_WMEM_MAX, O_RDWR | O_CLOEXEC);
436 if (fdwmax < 0 || write(fdwmax, wmax_size, strlen(wmax_size)) < 0)
437 ERR("Failed to set TCP wmem_max size");
444 static void __netconfig_update_default_connection_info(void)
446 int old_network_status = 0;
447 const char *profile = netconfig_get_default_profile();
448 const char *ip_addr = netconfig_get_default_ipaddress();
449 const char *ip_addr6 = netconfig_get_default_ipaddress6();
450 const char *proxy_addr = netconfig_get_default_proxy();
451 unsigned int freq = netconfig_get_default_frequency();
453 if (emulator_is_emulated() == TRUE)
457 DBG("Reset network state configuration");
459 DBG("%s: ip(%s) proxy(%s)", profile, ip_addr, proxy_addr);
461 netconfig_vconf_get_int(VCONFKEY_NETWORK_STATUS, &old_network_status);
463 if (profile == NULL && old_network_status != VCONFKEY_NETWORK_OFF) {
464 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
466 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
467 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
469 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 0);
470 netconfig_set_vconf_int("memory/private/wifi/frequency", 0);
472 DBG("Successfully clear IP and PROXY up");
474 /* Disable clatd if it is in running state */
475 netconfig_clatd_disable();
476 } else if (profile != NULL) {
477 char *old_ip = vconf_get_str(VCONFKEY_NETWORK_IP);
478 char *old_proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY);
480 if (netconfig_is_wifi_profile(profile) == TRUE) {
481 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_WIFI);
482 netconfig_set_vconf_int("memory/private/wifi/frequency", freq);
484 netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
485 EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_WIFI);
486 } else if (netconfig_is_cellular_profile(profile)) {
488 if (!netconfig_is_cellular_internet_profile(profile)) {
489 DBG("connection is not a internet profile - stop to update the cellular state");
493 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_CELLULAR);
495 netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
496 EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_CELLULAR);
498 /* Enable clatd if IPv6 is set and no IPv4 address */
499 if (!ip_addr && ip_addr6)
500 netconfig_clatd_enable();
501 } else if (netconfig_is_ethernet_profile(profile) == TRUE) {
502 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_ETHERNET);
503 netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
504 EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_ETHERNET);
505 } else if (netconfig_is_bluetooth_profile(profile) == TRUE) {
506 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_BLUETOOTH);
507 netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
508 EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_BT);
510 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
511 netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
512 EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_DISCONNECTED);
515 if (g_strcmp0(old_ip, ip_addr) != 0 || old_ip == NULL) {
517 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr);
518 else if (ip_addr6 != NULL)
519 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr6);
521 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
525 if (g_strcmp0(old_proxy, proxy_addr) != 0) {
526 if (proxy_addr == NULL)
527 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
529 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, proxy_addr);
533 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 1);
535 DBG("Successfully update default network configuration");
537 /* Disable clatd if it is in running state */
538 if (netconfig_is_cellular_profile(profile) != TRUE)
539 netconfig_clatd_disable();
542 __netconfig_adjust_tcp_buffer_size();
545 static gboolean __netconfig_is_tech_state_connected(void)
547 gboolean ret = FALSE;
548 GVariant *message = NULL, *variant;
549 GVariantIter *iter, *next;
553 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
554 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
555 "GetTechnologies", NULL);
557 if (message == NULL) {
558 DBG("Fail to get technology state");
562 g_variant_get(message, "(a(oa{sv}))", &iter);
563 while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
567 while (g_variant_iter_loop(next, "{sv}", &key, &variant)) {
569 if (g_strcmp0(key, "Connected") == 0) {
570 data = g_variant_get_boolean(variant);
571 DBG("%s [%s: %s]", path, key, data ? "True" : "False");
576 g_variant_unref(variant);
577 g_variant_iter_free(next);
585 g_variant_iter_free(iter);
586 g_variant_unref(message);
591 static void __netconfig_update_if_service_connected(void)
593 GVariant *message = NULL, *var;
594 GVariantIter *iter, *next;
598 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
599 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
600 "GetServices", NULL);
602 if (message == NULL) {
603 ERR("Failed to get services");
607 g_variant_get(message, "(a(oa{sv}))", &iter);
608 while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
612 if (g_str_has_prefix(path,
613 CONNMAN_WIFI_SERVICE_PROFILE_PREFIX) == TRUE) {
614 if (g_strrstr(path + strlen(CONNMAN_WIFI_SERVICE_PROFILE_PREFIX),
616 /* skip hidden profiles */
620 } else if (g_str_has_prefix(path,
621 CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX) == TRUE) {
627 while (g_variant_iter_loop(next, "{sv}", &key, &var)) {
628 if (g_strcmp0(key, "State") == 0) {
629 const gchar *sdata = NULL;
630 sdata = g_variant_get_string(var, NULL);
631 DBG("%s [%s: %s]", path, key, sdata);
633 if (g_strcmp0(sdata, "online") == 0 || g_strcmp0(sdata, "ready") == 0) {
635 /* Found a connected WiFi / 3G service.
636 * Lets update the default profile info.
638 netconfig_update_default_profile((const gchar*)path);
641 g_variant_unref(var);
642 g_variant_iter_free(next);
649 g_variant_iter_free(iter);
650 g_variant_unref(message);
655 static void __netconfig_network_notify_result(const char *sig_name, const char *key)
657 GVariantBuilder *builder;
660 INFO("[Signal] %s %s", sig_name, key);
662 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
663 g_variant_builder_add(builder, "{sv}", "key", g_variant_new_string(key));
665 params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
666 g_variant_builder_unref(builder);
668 netconfig_dbus_emit_signal(NULL,
669 NETCONFIG_NETWORK_PATH,
670 NETCONFIG_NETWORK_INTERFACE,
677 const char *netconfig_get_default_profile(void)
679 return netconfig_default_connection_info.profile;
682 const char *netconfig_get_default_ifname(void)
684 return netconfig_default_connection_info.ifname;
687 const char *netconfig_get_default_ipaddress(void)
689 return netconfig_default_connection_info.ipaddress;
692 const char *netconfig_get_default_ipaddress6(void)
694 return netconfig_default_connection_info.ipaddress6;
697 const char *netconfig_get_default_proxy(void)
699 return netconfig_default_connection_info.proxy;
702 unsigned int netconfig_get_default_frequency(void)
704 return netconfig_default_connection_info.freq;
707 const char *netconfig_wifi_get_connected_essid(const char *default_profile)
709 if (default_profile == NULL)
712 if (netconfig_is_wifi_profile(default_profile) != TRUE)
715 if (g_strcmp0(default_profile, netconfig_default_connection_info.profile) != 0)
718 return netconfig_default_connection_info.essid;
721 static int __netconfig_reset_ipv4_socket(void)
726 struct sockaddr_in sai;
727 const char *ipaddr = netconfig_get_default_ipaddress();
728 DBG("ipaddr-[%s]", ipaddr);
733 fd = socket(AF_INET, SOCK_DGRAM, 0);
737 memset(&sai, 0, sizeof(struct sockaddr_in));
738 sai.sin_family = AF_INET;
740 if (!inet_aton(ipaddr, &sai.sin_addr)) {
741 DBG("fail to inet_aton()");
746 memset(&ifr, 0, sizeof(struct ifreq));
747 memcpy(&ifr.ifr_addr, &sai, sizeof(sai));
748 g_strlcpy((char *)ifr.ifr_name, WIFI_IFNAME, IFNAMSIZ);
751 #define SIOCKILLADDR 0x8939
754 ret = ioctl(fd, SIOCKILLADDR, &ifr);
756 DBG("fail to ioctl[SIOCKILLADDR]");
765 void netconfig_update_default_profile(const char *profile)
767 static char *old_profile = NULL;
769 /* It's automatically updated by signal-handler
770 * DO NOT update manually
772 * It is going to update default connection information
775 if (netconfig_default_connection_info.profile != NULL) {
777 if (netconfig_is_wifi_profile(netconfig_default_connection_info.profile))
778 __netconfig_reset_ipv4_socket();
781 old_profile = strdup(netconfig_default_connection_info.profile);
783 g_free(netconfig_default_connection_info.profile);
784 netconfig_default_connection_info.profile = NULL;
786 g_free(netconfig_default_connection_info.ifname);
787 netconfig_default_connection_info.ifname = NULL;
789 g_free(netconfig_default_connection_info.ipaddress);
790 netconfig_default_connection_info.ipaddress = NULL;
792 g_free(netconfig_default_connection_info.ipaddress6);
793 netconfig_default_connection_info.ipaddress6 = NULL;
795 g_free(netconfig_default_connection_info.proxy);
796 netconfig_default_connection_info.proxy = NULL;
798 netconfig_default_connection_info.freq = 0;
800 if (wifi_state_get_service_state() != NETCONFIG_WIFI_CONNECTED) {
801 g_free(netconfig_default_connection_info.essid);
802 netconfig_default_connection_info.essid = NULL;
806 /* default profile is NULL and new connected profile is NULL */
808 profile = __netconfig_get_default_profile();
810 if (profile && netconfig_is_cellular_profile(profile) &&
811 !netconfig_is_cellular_internet_profile(profile)) {
812 DBG("not a default cellular profile");
817 __netconfig_update_default_connection_info();
822 netconfig_default_connection_info.profile = g_strdup(profile);
823 __netconfig_get_default_connection_info(profile);
824 __netconfig_update_default_connection_info();
828 void netconfig_update_default(void)
830 if (__netconfig_is_tech_state_connected() == TRUE)
831 __netconfig_update_if_service_connected();
833 __netconfig_adjust_tcp_buffer_size();
836 char *netconfig_get_ifname(const char *profile)
838 GVariant *message = NULL, *variant;
839 GVariantIter *iter, *next, *service;
843 const gchar *value = NULL;
844 gchar *ifname = NULL;
845 gboolean found_profile = 0;
850 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
851 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
852 "GetServices", NULL);
853 if (message == NULL) {
854 ERR("Failed to get services informations");
858 g_variant_get(message, "(a(oa{sv}))", &service);
859 while (g_variant_iter_loop(service, "(oa{sv})", &obj_path, &iter)) {
860 if (g_strcmp0(obj_path, profile) == 0) {
867 if (found_profile == 0) {
868 ERR("Profile %s doesn't exist", profile);
869 g_variant_iter_free(service);
870 g_variant_unref(message);
874 while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
875 if (g_strcmp0(key, "Ethernet") == 0) {
876 while (g_variant_iter_loop(next, "{sv}", &key1, &variant)) {
877 if (g_strcmp0(key1, "Interface") == 0) {
878 value = g_variant_get_string(variant, NULL);
879 ifname = g_strdup(value);
885 g_variant_unref(message);
887 g_variant_iter_free(service);
888 g_variant_iter_free(iter);
893 /* Check Ethernet Cable Plug-in /Plug-out Status */
894 void netconfig_network_notify_ethernet_cable_state(const char *key)
896 __netconfig_network_notify_result("EthernetCableState", key);
899 static gboolean handle_add_route(
901 GDBusMethodInvocation *context,
904 gchar *interface, gchar *gateway, gint address_family)
906 const gchar *path = ROUTE_EXEC_PATH;
907 gchar *const args[] = { "/sbin/route", "add", "-net", ip_addr,
908 "netmask", netmask, "dev", interface, NULL };
909 gchar *const envs[] = { NULL };
910 const gchar* buf = NULL;
915 DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
917 switch (address_family) {
919 if (ip_addr == NULL || netmask == NULL || interface == NULL) {
920 ERR("Invalid parameter");
921 netconfig_error_invalid_parameter(context);
925 if (netconfig_execute_file(path, args, envs) < 0) {
926 DBG("Failed to add a new route");
927 netconfig_error_permission_denied(context);
933 if (ip_addr == NULL || interface == NULL || gateway == NULL) {
934 ERR("Invalid parameter");
935 netconfig_error_invalid_parameter(context);
940 ch = strchr(buf, '/');
943 prefix_len = atoi(ch + 1);
944 ip_addr[pos-1] = '\0';
949 if (netconfig_add_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
950 DBG("Failed to add a new route");
951 netconfig_error_permission_denied(context);
956 DBG("Unknown Address Family");
957 netconfig_error_invalid_parameter(context);
961 DBG("Successfully added a new route");
962 network_complete_add_route(object, context, TRUE);
966 static gboolean handle_remove_route(
968 GDBusMethodInvocation *context,
971 gchar *interface, gchar *gateway, gint address_family)
973 const char *path = ROUTE_EXEC_PATH;
974 gchar *const args[] = { "/sbin/route", "del", "-net", ip_addr,
975 "netmask", netmask, "dev", interface, NULL };
976 char *const envs[] = { NULL };
977 const char* buf = NULL;
982 DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
984 switch (address_family) {
986 if (ip_addr == NULL || netmask == NULL || interface == NULL) {
987 DBG("Invalid parameter!");
988 netconfig_error_invalid_parameter(context);
991 if (netconfig_execute_file(path, args, envs) < 0) {
992 DBG("Failed to remove the route");
993 netconfig_error_permission_denied(context);
998 if (ip_addr == NULL || interface == NULL || gateway == NULL) {
999 DBG("Invalid parameter!");
1000 netconfig_error_invalid_parameter(context);
1005 ch = strchr(buf, '/');
1008 prefix_len = atoi(ch + 1);
1009 ip_addr[pos-1] = '\0';
1014 if (netconfig_del_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
1015 DBG("Failed to remove the route");
1016 netconfig_error_permission_denied(context);
1021 DBG("Unknown Address Family");
1022 netconfig_error_invalid_parameter(context);
1026 DBG("Successfully removed the route");
1027 network_complete_remove_route(object, context, TRUE);
1031 static gboolean handle_check_get_privilege(Network *object,
1032 GDBusMethodInvocation *context)
1034 network_complete_check_get_privilege(object, context);
1039 static gboolean handle_check_profile_privilege(Network *object,
1040 GDBusMethodInvocation *context)
1042 network_complete_check_profile_privilege(object, context);
1046 static gboolean handle_check_internet_privilege(Network *object,
1047 GDBusMethodInvocation *context)
1049 network_complete_check_internet_privilege(object, context);
1053 gboolean handle_ethernet_cable_state(Network *object,
1054 GDBusMethodInvocation *context)
1059 ret = netconfig_get_ethernet_cable_state(&state);
1061 DBG("Failed to get ethernet cable state");
1062 netconfig_error_fail_ethernet_cable_state(context);
1066 DBG("Successfully get ethernet cable state[%d]", state);
1067 network_complete_ethernet_cable_state(object, context, state);
1071 void state_object_create_and_init(void)
1073 DBG("Creating network state object");
1074 GDBusInterfaceSkeleton *interface_network = NULL;
1075 GDBusConnection *connection = NULL;
1076 GDBusObjectManagerServer *server = netdbus_get_state_manager();
1080 connection = netdbus_get_connection();
1081 g_dbus_object_manager_server_set_connection(server, connection);
1083 /*Interface netconfig.network*/
1084 netconfigstate = network_skeleton_new();
1086 interface_network = G_DBUS_INTERFACE_SKELETON(netconfigstate);
1087 g_signal_connect(netconfigstate, "handle-add-route",
1088 G_CALLBACK(handle_add_route), NULL);
1089 g_signal_connect(netconfigstate, "handle-check-get-privilege",
1090 G_CALLBACK(handle_check_get_privilege), NULL);
1091 g_signal_connect(netconfigstate, "handle-check-profile-privilege",
1092 G_CALLBACK(handle_check_profile_privilege), NULL);
1093 g_signal_connect(netconfigstate, "handle-check-internet-privilege",
1094 G_CALLBACK(handle_check_internet_privilege), NULL);
1095 g_signal_connect(netconfigstate, "handle-ethernet-cable-state",
1096 G_CALLBACK(handle_ethernet_cable_state), NULL);
1097 g_signal_connect(netconfigstate, "handle-remove-route",
1098 G_CALLBACK(handle_remove_route), NULL);
1099 g_signal_connect(netconfigstate, "handle-launch-mdns",
1100 G_CALLBACK(handle_launch_mdns), NULL);
1101 g_signal_connect(netconfigstate, "handle-unref-mdns",
1102 G_CALLBACK(handle_unref_mdns), NULL);
1103 g_signal_connect(netconfigstate, "handle-device-policy-set-wifi",
1104 G_CALLBACK(handle_device_policy_set_wifi), NULL);
1105 g_signal_connect(netconfigstate, "handle-device-policy-get-wifi",
1106 G_CALLBACK(handle_device_policy_get_wifi), NULL);
1107 g_signal_connect(netconfigstate, "handle-device-policy-set-wifi-profile",
1108 G_CALLBACK(handle_device_policy_set_wifi_profile), NULL);
1109 g_signal_connect(netconfigstate, "handle-device-policy-get-wifi-profile",
1110 G_CALLBACK(handle_device_policy_get_wifi_profile), NULL);
1112 if (!g_dbus_interface_skeleton_export(interface_network, connection,
1113 NETCONFIG_NETWORK_STATE_PATH, NULL)) {
1114 ERR("Export with path failed");
1118 void state_object_deinit(void)
1120 g_object_unref(netconfigstate);