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-monitor.h"
44 #include "netsupplicant.h"
45 #include "wifi-tel-intf.h"
46 #include "clatd-handler.h"
48 #include "generated-code.h"
49 /* Define TCP buffer sizes for various networks */
50 /* ReadMin, ReadInitial, ReadMax */ /* WriteMin, WriteInitial, WriteMax */
51 #define NET_TCP_BUFFERSIZE_DEFAULT_READ "4096 87380 704512"
52 #define NET_TCP_BUFFERSIZE_DEFAULT_WRITE "4096 16384 110208"
53 #define NET_TCP_BUFFERSIZE_WIFI_READ "524288 1048576 2560000"
54 #define NET_TCP_BUFFERSIZE_WIFI_WRITE "524288 1048576 2560000"
55 #define NET_TCP_BUFFERSIZE_LTE_READ "524288 1048576 2560000"
56 #define NET_TCP_BUFFERSIZE_LTE_WRITE "524288 1048576 2560000"
57 #define NET_TCP_BUFFERSIZE_UMTS_READ "4094 87380 704512"
58 #define NET_TCP_BUFFERSIZE_UMTS_WRITE "4096 16384 110208"
59 #define NET_TCP_BUFFERSIZE_HSPA_READ "4092 87380 704512"
60 #define NET_TCP_BUFFERSIZE_HSPA_WRITE "4096 16384 262144"
61 #define NET_TCP_BUFFERSIZE_HSDPA_READ "4092 87380 704512"
62 #define NET_TCP_BUFFERSIZE_HSDPA_WRITE "4096 16384 262144"
63 #define NET_TCP_BUFFERSIZE_HSUPA_READ "4092 87380 704512"
64 #define NET_TCP_BUFFERSIZE_HSUPA_WRITE "4096 16384 262144"
65 #define NET_TCP_BUFFERSIZE_HSPAP_READ "4092 87380 1220608"
66 #define NET_TCP_BUFFERSIZE_HSPAP_WRITE "4096 16384 1220608"
67 #define NET_TCP_BUFFERSIZE_EDGE_READ "4093 26280 35040"
68 #define NET_TCP_BUFFERSIZE_EDGE_WRITE "4096 16384 35040"
69 #define NET_TCP_BUFFERSIZE_GPRS_READ "4096 30000 30000"
70 #define NET_TCP_BUFFERSIZE_GPRS_WRITE "4096 8760 11680"
72 #define NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX "1048576"
73 #define NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX "2097152"
74 #define NET_TCP_BUFFERSIZE_LTE_RMEM_MAX "5242880"
76 #define NET_TCP_BUFFERSIZE_WIFID_WMEM_MAX "2097152"
78 #define NET_PROC_SYS_NET_IPV4_TCP_RMEM "/proc/sys/net/ipv4/tcp_rmem"
79 #define NET_PROC_SYS_NET_IPv4_TCP_WMEM "/proc/sys/net/ipv4/tcp_wmem"
80 #define NET_PROC_SYS_NET_CORE_RMEM_MAX "/proc/sys/net/core/rmem_max"
81 #define NET_PROC_SYS_NET_CORE_WMEM_MAX "/proc/sys/net/core/wmem_max"
83 #define ROUTE_EXEC_PATH "/sbin/route"
85 static Network *netconfigstate = NULL;
87 struct netconfig_default_connection {
97 static struct netconfig_default_connection
98 netconfig_default_connection_info = { NULL, };
100 gboolean netconfig_iface_network_state_ethernet_cable_state(gint32 *state);
102 static gboolean __netconfig_is_connected(GVariantIter *array)
104 gboolean is_connected = FALSE;
105 GVariant *variant = NULL;
107 const gchar *value = NULL;
109 while (g_variant_iter_loop(array, "{sv}", &key, &variant)) {
110 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) {
150 if(netconfig_is_cellular_profile(object_path) && !netconfig_is_cellular_internet_profile(object_path)){
154 if (__netconfig_is_connected(next) == TRUE) {
155 default_profile = g_strdup(object_path);
157 g_variant_iter_free(next);
161 g_variant_iter_free(iter);
162 g_variant_unref(message);
164 return default_profile;
167 static void __netconfig_get_default_connection_info(const char *profile)
169 GVariant *message = NULL, *variant = NULL, *variant2 = NULL;
170 GVariantIter *iter = NULL, *iter1 = NULL;
171 GVariant *next = NULL;
176 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE, profile,
177 CONNMAN_SERVICE_INTERFACE, "GetProperties", NULL);
178 if (message == NULL) {
179 ERR("Failed to get service properties");
183 g_variant_get(message, "(a{sv})", &iter);
184 while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
185 const gchar *value = NULL;
187 if (g_strcmp0(key, "Name") == 0 &&
188 netconfig_is_wifi_profile(profile) == TRUE) {
189 if (g_variant_is_of_type(next, G_VARIANT_TYPE_STRING)) {
190 value = g_variant_get_string(next, NULL);
192 netconfig_default_connection_info.essid = g_strdup(value);
194 } else if (g_strcmp0(key, "Ethernet") == 0) {
195 g_variant_get(next, "a{sv}", &iter1);
196 while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
197 if (g_strcmp0(key1, "Interface") == 0) {
198 value = g_variant_get_string(variant, NULL);
199 netconfig_default_connection_info.ifname = g_strdup(value);
202 } else if (g_strcmp0(key, "IPv4") == 0) {
203 g_variant_get(next, "a{sv}", &iter1);
204 while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
205 if (g_strcmp0(key1, "Address") == 0) {
206 value = g_variant_get_string(variant, NULL);
207 netconfig_default_connection_info.ipaddress = g_strdup(value);
210 } else if (g_strcmp0(key, "IPv6") == 0) {
211 g_variant_get(next, "a{sv}", &iter1);
212 while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
213 if (g_strcmp0(key1, "Address") == 0) {
214 value = g_variant_get_string(variant, NULL);
215 netconfig_default_connection_info.ipaddress6 = g_strdup(value);
218 } else if (g_strcmp0(key, "Proxy") == 0) {
219 g_variant_get(next, "a{sv}", &iter1);
220 while (g_variant_iter_loop(iter1, "{sv}", &key2, &variant2)) {
221 GVariantIter *iter_sub = NULL;
223 if (g_strcmp0(key2, "Servers") == 0) {
224 if (!g_variant_is_of_type(variant2, G_VARIANT_TYPE_STRING_ARRAY)) {
226 g_variant_unref(variant2);
230 g_variant_get(variant2, "as", &iter_sub);
231 g_variant_iter_loop(iter_sub, "s", &value);
232 g_variant_iter_free(iter_sub);
233 if (value != NULL && (strlen(value) > 0))
234 netconfig_default_connection_info.proxy = g_strdup(value);
235 } else if (g_strcmp0(key2, "Method") == 0) {
236 if (g_variant_is_of_type(variant2, G_VARIANT_TYPE_STRING)) {
238 g_variant_unref(variant2);
242 value = g_variant_get_string(variant2, NULL);
243 if (g_strcmp0(value, "direct") == 0) {
244 g_free(netconfig_default_connection_info.proxy);
245 netconfig_default_connection_info.proxy = NULL;
248 g_variant_unref(variant2);
253 } else if (g_strcmp0(key, "Frequency") == 0) {
254 if (g_variant_is_of_type(next, G_VARIANT_TYPE_UINT16)) {
255 freq = g_variant_get_uint16(next);
256 netconfig_default_connection_info.freq = freq;
263 g_variant_unref(message);
266 g_variant_iter_free (iter);
269 g_variant_iter_free (iter1);
274 static void __netconfig_adjust_tcp_buffer_size(void)
276 int fdr = 0, fdw = 0;
277 int fdrmax = 0, fdwmax = 0;
278 const char *rbuf_size = NULL;
279 const char *wbuf_size = NULL;
280 const char *rmax_size = NULL;
281 const char *wmax_size = NULL;
282 const char *profile = netconfig_get_default_profile();
284 if (profile == NULL) {
285 DBG("There is no default connection");
287 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
288 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
289 } else if (netconfig_is_wifi_profile(profile) == TRUE) {
290 DBG("Default connection: Wi-Fi");
292 rbuf_size = NET_TCP_BUFFERSIZE_WIFI_READ;
293 wbuf_size = NET_TCP_BUFFERSIZE_WIFI_WRITE;
294 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
295 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
296 } else if (netconfig_is_cellular_profile(profile) == TRUE) {
297 TapiHandle *tapi_handle = NULL;
298 int telephony_svctype = 0, telephony_pstype = 0;
300 tapi_handle = (TapiHandle *)netconfig_tel_init();
301 if (NULL != tapi_handle) {
302 tel_get_property_int(tapi_handle,
303 TAPI_PROP_NETWORK_SERVICE_TYPE,
305 tel_get_property_int(tapi_handle, TAPI_PROP_NETWORK_PS_TYPE,
307 netconfig_tel_deinit();
310 DBG("Default cellular %d, %d", telephony_svctype, telephony_pstype);
312 switch (telephony_pstype) {
313 case VCONFKEY_TELEPHONY_PSTYPE_HSPA:
314 rbuf_size = NET_TCP_BUFFERSIZE_HSPA_READ;
315 wbuf_size = NET_TCP_BUFFERSIZE_HSPA_WRITE;
317 case VCONFKEY_TELEPHONY_PSTYPE_HSUPA:
318 rbuf_size = NET_TCP_BUFFERSIZE_HSUPA_READ;
319 wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
321 case VCONFKEY_TELEPHONY_PSTYPE_HSDPA:
322 rbuf_size = NET_TCP_BUFFERSIZE_HSDPA_READ;
323 wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
325 #if !defined TIZEN_WEARABLE
326 case VCONFKEY_TELEPHONY_PSTYPE_HSPAP:
327 rbuf_size = NET_TCP_BUFFERSIZE_HSPAP_READ;
328 wbuf_size = NET_TCP_BUFFERSIZE_HSPAP_WRITE;
332 switch (telephony_svctype) {
333 case VCONFKEY_TELEPHONY_SVCTYPE_LTE:
334 rbuf_size = NET_TCP_BUFFERSIZE_LTE_READ;
335 wbuf_size = NET_TCP_BUFFERSIZE_LTE_WRITE;
336 rmax_size = NET_TCP_BUFFERSIZE_LTE_RMEM_MAX;
338 case VCONFKEY_TELEPHONY_SVCTYPE_3G:
339 rbuf_size = NET_TCP_BUFFERSIZE_UMTS_READ;
340 wbuf_size = NET_TCP_BUFFERSIZE_UMTS_WRITE;
342 case VCONFKEY_TELEPHONY_SVCTYPE_2_5G_EDGE:
343 rbuf_size = NET_TCP_BUFFERSIZE_EDGE_READ;
344 wbuf_size = NET_TCP_BUFFERSIZE_EDGE_WRITE;
346 case VCONFKEY_TELEPHONY_SVCTYPE_2_5G:
347 rbuf_size = NET_TCP_BUFFERSIZE_GPRS_READ;
348 wbuf_size = NET_TCP_BUFFERSIZE_GPRS_WRITE;
351 /* TODO: Check LTE support */
352 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
353 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
359 DBG("Default TCP buffer configured");
361 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
362 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
365 if (rbuf_size != NULL) {
366 fdr = open(NET_PROC_SYS_NET_IPV4_TCP_RMEM, O_RDWR | O_CLOEXEC);
368 if (fdr < 0 || write(fdr, rbuf_size, strlen(rbuf_size)) < 0)
369 ERR("Failed to set TCP read buffer size");
375 if (wbuf_size != NULL) {
376 fdw = open(NET_PROC_SYS_NET_IPv4_TCP_WMEM, O_RDWR | O_CLOEXEC);
378 if (fdw < 0 || write(fdw, wbuf_size, strlen(wbuf_size)) < 0)
379 ERR("Failed to set TCP write buffer size");
386 if (rmax_size == NULL)
387 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
388 if (wmax_size == NULL)
389 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
391 if (rmax_size != NULL) {
392 fdrmax = open(NET_PROC_SYS_NET_CORE_RMEM_MAX, O_RDWR | O_CLOEXEC);
394 if (fdrmax < 0 || write(fdrmax, rmax_size, strlen(rmax_size)) < 0)
395 ERR("Failed to set TCP rmem_max size");
401 if (wmax_size != NULL) {
402 fdwmax = open(NET_PROC_SYS_NET_CORE_WMEM_MAX, O_RDWR | O_CLOEXEC);
404 if (fdwmax < 0 || write(fdwmax, wmax_size, strlen(wmax_size)) < 0)
405 ERR("Failed to set TCP wmem_max size");
412 static void __netconfig_update_default_connection_info(void)
414 int old_network_status = 0;
415 const char *profile = netconfig_get_default_profile();
416 const char *ip_addr = netconfig_get_default_ipaddress();
417 const char *ip_addr6 = netconfig_get_default_ipaddress6();
418 const char *proxy_addr = netconfig_get_default_proxy();
419 unsigned int freq = netconfig_get_default_frequency();
421 if (emulator_is_emulated() == TRUE)
425 DBG("Reset network state configuration");
427 DBG("%s: ip(%s) proxy(%s)", profile, ip_addr, proxy_addr);
429 vconf_get_int(VCONFKEY_NETWORK_STATUS, &old_network_status);
431 if (profile == NULL && old_network_status != VCONFKEY_NETWORK_OFF) {
432 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
434 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
435 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
437 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 0);
438 netconfig_set_vconf_int("memory/private/wifi/frequency", 0);
440 DBG("Successfully clear IP and PROXY up");
442 /* Disable clatd if it is in running state */
443 netconfig_clatd_disable();
445 else if (profile != NULL) {
446 char *old_ip = vconf_get_str(VCONFKEY_NETWORK_IP);
447 char *old_proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY);
449 if (netconfig_is_wifi_profile(profile) == TRUE) {
450 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_WIFI);
451 netconfig_set_vconf_int("memory/private/wifi/frequency", freq);
453 netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
454 EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_WIFI);
456 else if (netconfig_is_cellular_profile(profile) ){
458 if( !netconfig_is_cellular_internet_profile(profile)){
459 DBG("connection is not a internet profile - stop to update the cellular state");
463 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_CELLULAR);
465 netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
466 EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_CELLULAR);
468 /* Enable clatd if IPv6 is set and no IPv4 address */
469 if (!ip_addr && ip_addr6 )
470 netconfig_clatd_enable();
472 else if (netconfig_is_ethernet_profile(profile) == TRUE){
473 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_ETHERNET);
474 netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
475 EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_ETHERNET);
477 else if (netconfig_is_bluetooth_profile(profile) == TRUE){
478 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_BLUETOOTH);
479 netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
480 EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_BT);
483 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
484 netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
485 EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_DISCONNECTED);
488 if (g_strcmp0(old_ip, ip_addr) != 0 || old_ip == NULL) {
490 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr);
491 else if (ip_addr6 != NULL)
492 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr6);
494 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
498 if (g_strcmp0(old_proxy, proxy_addr) != 0) {
499 if (proxy_addr == NULL)
500 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
502 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, proxy_addr);
506 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 1);
508 DBG("Successfully update default network configuration");
510 /* Disable clatd if it is in running state */
511 if (netconfig_is_cellular_profile(profile) != TRUE)
512 netconfig_clatd_disable();
515 __netconfig_adjust_tcp_buffer_size();
518 static gboolean __netconfig_is_tech_state_connected(void)
520 gboolean ret = FALSE;
521 GVariant *message = NULL, *variant;
522 GVariantIter *iter, *next;
526 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
527 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
528 "GetTechnologies", NULL);
530 if (message == NULL) {
531 DBG("Fail to get technology state");
535 g_variant_get(message, "(a(oa{sv}))", &iter);
536 while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
541 while (g_variant_iter_loop(next, "{sv}", &key, &variant)) {
543 if (g_strcmp0(key, "Connected") == 0) {
544 data = g_variant_get_boolean(variant);
545 DBG("%s [%s: %s]", path, key, data ? "True" : "False");
550 g_variant_unref(variant);
551 g_variant_iter_free(next);
559 g_variant_iter_free(iter);
560 g_variant_unref(message);
565 static void __netconfig_update_if_service_connected(void)
567 GVariant *message = NULL, *var;
568 GVariantIter *iter, *next;
572 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
573 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
574 "GetServices", NULL);
576 if (message == NULL) {
577 ERR("Failed to get services");
581 g_variant_get(message, "(a(oa{sv}))", &iter);
582 while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
587 if (g_str_has_prefix(path,
588 CONNMAN_WIFI_SERVICE_PROFILE_PREFIX) == TRUE) {
589 if (g_strrstr(path + strlen(CONNMAN_WIFI_SERVICE_PROFILE_PREFIX),
591 /* skip hidden profiles */
595 } else if (g_str_has_prefix(path,
596 CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX) == TRUE) {
602 while (g_variant_iter_loop(next, "{sv}", &key, &var)) {
603 if (g_strcmp0(key, "State") == 0) {
604 const gchar *sdata = NULL;
605 sdata = g_variant_get_string(var, NULL);
606 DBG("%s [%s: %s]", path, key, sdata);
608 if (g_strcmp0(sdata, "online") == 0 || g_strcmp0(sdata, "ready") == 0) {
610 /* Found a connected WiFi / 3G service.
611 * Lets update the default profile info.
613 netconfig_update_default_profile((const gchar*)path);
616 g_variant_unref(var);
617 g_variant_iter_free(next);
624 g_variant_iter_free(iter);
625 g_variant_unref(message);
630 static void __netconfig_network_notify_result(const char *sig_name, const char *key)
634 GVariantBuilder *builder = NULL;
635 GDBusConnection *connection = NULL;
636 GError *error = NULL;
637 const char *prop_key = "key";
639 INFO("[Signal] %s %s", sig_name, key);
641 connection = netdbus_get_connection();
642 if (connection == NULL) {
643 ERR("Failed to get GDBus Connection");
647 builder = g_variant_builder_new(G_VARIANT_TYPE ("a{sv}"));
648 g_variant_builder_add(builder, "{sv}", prop_key, g_variant_new_string(key));
649 params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
651 g_variant_builder_unref(builder);
653 reply = g_dbus_connection_emit_signal(connection,
655 NETCONFIG_NETWORK_PATH,
656 NETCONFIG_NETWORK_INTERFACE,
663 ERR("Failed to send signal [%s]", error->message);
669 INFO("Sent signal (%s), key (%s)", sig_name, key);
673 const char *netconfig_get_default_profile(void)
675 return netconfig_default_connection_info.profile;
678 const char *netconfig_get_default_ifname(void)
680 return netconfig_default_connection_info.ifname;
683 const char *netconfig_get_default_ipaddress(void)
685 return netconfig_default_connection_info.ipaddress;
688 const char *netconfig_get_default_ipaddress6(void)
690 return netconfig_default_connection_info.ipaddress6;
693 const char *netconfig_get_default_proxy(void)
695 return netconfig_default_connection_info.proxy;
698 unsigned int netconfig_get_default_frequency(void)
700 return netconfig_default_connection_info.freq;
703 const char *netconfig_wifi_get_connected_essid(const char *default_profile)
705 if (default_profile == NULL)
708 if (netconfig_is_wifi_profile(default_profile) != TRUE)
711 if (g_strcmp0(default_profile, netconfig_default_connection_info.profile) != 0)
714 return netconfig_default_connection_info.essid;
717 static int __netconfig_reset_ipv4_socket(void)
722 struct sockaddr_in sai;
723 const char *ipaddr = netconfig_get_default_ipaddress();
724 DBG("ipaddr-[%s]", ipaddr);
729 fd = socket(AF_INET, SOCK_DGRAM, 0);
733 memset(&sai, 0, sizeof(struct sockaddr_in));
734 sai.sin_family = AF_INET;
736 if (!inet_aton(ipaddr, &sai.sin_addr)) {
737 DBG("fail to inet_aton()");
742 memset(&ifr, 0, sizeof(struct ifreq));
743 memcpy(&ifr.ifr_addr, &sai, sizeof(sai));
744 g_strlcpy((char *)ifr.ifr_name, WIFI_IFNAME, IFNAMSIZ);
747 #define SIOCKILLADDR 0x8939
750 ret = ioctl(fd, SIOCKILLADDR, &ifr);
752 DBG("fail to ioctl[SIOCKILLADDR]");
761 void netconfig_update_default_profile(const char *profile)
763 static char *old_profile = NULL;
765 /* It's automatically updated by signal-handler
766 * DO NOT update manually
768 * It is going to update default connection information
771 if (netconfig_default_connection_info.profile != NULL) {
773 if (netconfig_is_wifi_profile(netconfig_default_connection_info.profile))
774 __netconfig_reset_ipv4_socket();
777 old_profile = strdup(netconfig_default_connection_info.profile);
779 g_free(netconfig_default_connection_info.profile);
780 netconfig_default_connection_info.profile = NULL;
782 g_free(netconfig_default_connection_info.ifname);
783 netconfig_default_connection_info.ifname = NULL;
785 g_free(netconfig_default_connection_info.ipaddress);
786 netconfig_default_connection_info.ipaddress = NULL;
788 g_free(netconfig_default_connection_info.ipaddress6);
789 netconfig_default_connection_info.ipaddress6 = NULL;
791 g_free(netconfig_default_connection_info.proxy);
792 netconfig_default_connection_info.proxy = NULL;
794 netconfig_default_connection_info.freq = 0;
796 if (wifi_state_get_service_state() != NETCONFIG_WIFI_CONNECTED) {
797 g_free(netconfig_default_connection_info.essid);
798 netconfig_default_connection_info.essid = NULL;
802 //default profile is NULL and new connected profile is NULL
804 profile = __netconfig_get_default_profile();
806 if (profile && netconfig_is_cellular_profile(profile) &&
807 !netconfig_is_cellular_internet_profile(profile)){
808 DBG("not a default cellular profile");
813 __netconfig_update_default_connection_info();
818 netconfig_default_connection_info.profile = g_strdup(profile);
819 __netconfig_get_default_connection_info(profile);
820 __netconfig_update_default_connection_info();
824 void netconfig_update_default(void)
826 if (__netconfig_is_tech_state_connected() == TRUE)
827 __netconfig_update_if_service_connected();
829 __netconfig_adjust_tcp_buffer_size();
832 char *netconfig_get_ifname(const char *profile)
834 GVariant *message = NULL, *variant;
835 GVariantIter *iter, *next;
838 const gchar *value = NULL;
839 gchar *ifname = NULL;
844 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE, profile,
845 CONNMAN_SERVICE_INTERFACE, "GetProperties", NULL);
846 if (message == NULL) {
847 ERR("Failed to get service properties");
851 g_variant_get(message, "(a{sv})", &iter);
852 while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
853 if (g_strcmp0(key, "Ethernet") == 0) {
854 while (g_variant_iter_loop(next, "{sv}", &key1, &variant)) {
855 if (g_strcmp0(key1, "Interface") == 0) {
856 value = g_variant_get_string(variant, NULL);
857 ifname = g_strdup(value);
863 g_variant_unref(message);
865 g_variant_iter_free(iter);
870 /* Check Ethernet Cable Plug-in /Plug-out Status */
871 void netconfig_network_notify_ethernet_cable_state(const char *key)
873 __netconfig_network_notify_result("EthernetCableState", key);
876 static gboolean handle_add_route(
878 GDBusMethodInvocation *context,
881 gchar *interface, gchar *gateway, gint address_family)
883 const gchar *path = ROUTE_EXEC_PATH;
884 gchar *const args[] = { "/sbin/route", "add", "-net", ip_addr,
885 "netmask", netmask, "dev", interface, NULL };
886 gchar *const envs[] = { NULL };
887 const gchar* buf = NULL;
892 DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
894 switch(address_family) {
896 if (ip_addr == NULL || netmask == NULL || interface == NULL) {
897 ERR("Invalid parameter");
898 netconfig_error_invalid_parameter(context);
902 if (netconfig_execute_file(path, args, envs) < 0) {
903 DBG("Failed to add a new route");
904 netconfig_error_permission_denied(context);
910 if (ip_addr == NULL || interface == NULL || gateway == NULL) {
911 ERR("Invalid parameter");
912 netconfig_error_invalid_parameter(context);
917 ch = strchr(buf, '/');
920 prefix_len = atoi(ch + 1);
921 ip_addr[pos-1] = '\0';
926 if (netconfig_add_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
927 DBG("Failed to add a new route");
928 netconfig_error_permission_denied(context);
933 DBG("Unknown Address Family");
934 netconfig_error_invalid_parameter(context);
938 DBG("Successfully added a new route");
939 network_complete_add_route(object, context, TRUE);
943 static gboolean handle_remove_route(
945 GDBusMethodInvocation *context,
948 gchar *interface, gchar *gateway, gint address_family)
950 const char *path = ROUTE_EXEC_PATH;
951 gchar *const args[] = { "/sbin/route", "del", "-net", ip_addr,
952 "netmask", netmask, "dev", interface, NULL };
953 char *const envs[] = { NULL };
954 const char* buf = NULL;
959 DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
961 switch(address_family) {
963 if (ip_addr == NULL || netmask == NULL || interface == NULL) {
964 DBG("Invalid parameter!");
965 netconfig_error_invalid_parameter(context);
968 if (netconfig_execute_file(path, args, envs) < 0) {
969 DBG("Failed to remove the route");
970 netconfig_error_permission_denied(context);
975 if (ip_addr == NULL || interface == NULL || gateway == NULL) {
976 DBG("Invalid parameter!");
977 netconfig_error_invalid_parameter(context);
982 ch = strchr(buf, '/');
985 prefix_len = atoi(ch + 1);
986 ip_addr[pos-1] = '\0';
991 if (netconfig_del_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
992 DBG("Failed to remove the route");
993 netconfig_error_permission_denied(context);
998 DBG("Unknown Address Family");
999 netconfig_error_invalid_parameter(context);
1003 DBG("Successfully removed the route");
1004 network_complete_remove_route(object, context, TRUE);
1008 static gboolean handle_check_get_privilege(Network *object,
1009 GDBusMethodInvocation *context)
1011 network_complete_check_get_privilege(object, context);
1016 static gboolean handle_check_profile_privilege(Network *object,
1017 GDBusMethodInvocation *context)
1019 network_complete_check_profile_privilege(object, context);
1023 gboolean handle_ethernet_cable_state(Network *object,
1024 GDBusMethodInvocation *context)
1029 ret = netconfig_get_ethernet_cable_state(&state);
1031 DBG("Failed to get ethernet cable state");
1032 netconfig_error_fail_ethernet_cable_state(context);
1036 DBG("Successfully get ethernet cable state[%d]", state);
1037 network_complete_ethernet_cable_state(object, context, state);
1041 void state_object_create_and_init(void)
1043 DBG("Creating network state object");
1044 GDBusInterfaceSkeleton *interface_network = NULL;
1045 GDBusConnection *connection = NULL;
1046 GDBusObjectManagerServer *server = netdbus_get_state_manager();
1050 connection = netdbus_get_connection();
1051 g_dbus_object_manager_server_set_connection(server, connection);
1053 /*Interface netconfig.network*/
1054 netconfigstate = network_skeleton_new();
1056 interface_network = G_DBUS_INTERFACE_SKELETON(netconfigstate);
1057 g_signal_connect(netconfigstate, "handle-add-route",
1058 G_CALLBACK(handle_add_route), NULL);
1059 g_signal_connect(netconfigstate, "handle-check-get-privilege",
1060 G_CALLBACK(handle_check_get_privilege), NULL);
1061 g_signal_connect(netconfigstate, "handle-check-profile-privilege",
1062 G_CALLBACK(handle_check_profile_privilege), NULL);
1063 g_signal_connect(netconfigstate, "handle-ethernet-cable-state",
1064 G_CALLBACK(handle_ethernet_cable_state), NULL);
1065 g_signal_connect(netconfigstate, "handle-remove-route",
1066 G_CALLBACK(handle_remove_route), NULL);
1068 if (!g_dbus_interface_skeleton_export(interface_network, connection,
1069 NETCONFIG_NETWORK_STATE_PATH, NULL)) {
1070 ERR("Export with path failed");
1074 void state_object_deinit(void)
1076 g_object_unref(netconfigstate);