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)
113 if (g_variant_is_of_type(variant, G_VARIANT_TYPE_STRING)) {
114 value = g_variant_get_string(variant, NULL);
115 if (g_strcmp0(value, "ready") == 0 || g_strcmp0(value, "online") == 0)
120 g_variant_unref(variant);
127 static char *__netconfig_get_default_profile(void)
129 GVariant *message = NULL;
132 gchar *default_profile = NULL;
135 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
136 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
137 "GetServices", NULL);
138 if (message == NULL) {
139 ERR("Failed to get profiles");
143 g_variant_get(message, "(a(oa{sv}))", &iter);
144 while (g_variant_iter_loop(iter, "(oa{sv})", &object_path, &next)) {
145 if (object_path == NULL)
148 if (netconfig_is_cellular_profile(object_path) && !netconfig_is_cellular_internet_profile(object_path))
151 if (__netconfig_is_connected(next) == TRUE) {
152 default_profile = g_strdup(object_path);
154 g_variant_iter_free(next);
158 g_variant_iter_free(iter);
159 g_variant_unref(message);
161 return default_profile;
164 static void __netconfig_get_default_connection_info(const char *profile)
166 GVariant *message = NULL, *variant = NULL, *variant2 = NULL;
167 GVariantIter *iter = NULL, *iter1 = NULL;
168 GVariant *next = NULL;
173 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE, profile,
174 CONNMAN_SERVICE_INTERFACE, "GetProperties", NULL);
175 if (message == NULL) {
176 ERR("Failed to get service properties");
180 g_variant_get(message, "(a{sv})", &iter);
181 while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
182 const gchar *value = NULL;
184 if (g_strcmp0(key, "Name") == 0 &&
185 netconfig_is_wifi_profile(profile) == TRUE) {
186 if (g_variant_is_of_type(next, G_VARIANT_TYPE_STRING)) {
187 value = g_variant_get_string(next, NULL);
189 netconfig_default_connection_info.essid = g_strdup(value);
191 } else if (g_strcmp0(key, "Ethernet") == 0) {
192 g_variant_get(next, "a{sv}", &iter1);
193 while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
194 if (g_strcmp0(key1, "Interface") == 0) {
195 value = g_variant_get_string(variant, NULL);
196 netconfig_default_connection_info.ifname = g_strdup(value);
199 } else if (g_strcmp0(key, "IPv4") == 0) {
200 g_variant_get(next, "a{sv}", &iter1);
201 while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
202 if (g_strcmp0(key1, "Address") == 0) {
203 value = g_variant_get_string(variant, NULL);
204 netconfig_default_connection_info.ipaddress = g_strdup(value);
207 } else if (g_strcmp0(key, "IPv6") == 0) {
208 g_variant_get(next, "a{sv}", &iter1);
209 while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
210 if (g_strcmp0(key1, "Address") == 0) {
211 value = g_variant_get_string(variant, NULL);
212 netconfig_default_connection_info.ipaddress6 = g_strdup(value);
215 } else if (g_strcmp0(key, "Proxy") == 0) {
216 g_variant_get(next, "a{sv}", &iter1);
217 while (g_variant_iter_loop(iter1, "{sv}", &key2, &variant2)) {
218 GVariantIter *iter_sub = NULL;
220 if (g_strcmp0(key2, "Servers") == 0) {
221 if (!g_variant_is_of_type(variant2, G_VARIANT_TYPE_STRING_ARRAY)) {
223 g_variant_unref(variant2);
227 g_variant_get(variant2, "as", &iter_sub);
228 g_variant_iter_loop(iter_sub, "s", &value);
229 g_variant_iter_free(iter_sub);
230 if (value != NULL && (strlen(value) > 0))
231 netconfig_default_connection_info.proxy = g_strdup(value);
232 } else if (g_strcmp0(key2, "Method") == 0) {
233 if (g_variant_is_of_type(variant2, G_VARIANT_TYPE_STRING)) {
235 g_variant_unref(variant2);
239 value = g_variant_get_string(variant2, NULL);
240 if (g_strcmp0(value, "direct") == 0) {
241 g_free(netconfig_default_connection_info.proxy);
242 netconfig_default_connection_info.proxy = NULL;
245 g_variant_unref(variant2);
250 } else if (g_strcmp0(key, "Frequency") == 0) {
251 if (g_variant_is_of_type(next, G_VARIANT_TYPE_UINT16)) {
252 freq = g_variant_get_uint16(next);
253 netconfig_default_connection_info.freq = freq;
260 g_variant_unref(message);
263 g_variant_iter_free(iter);
266 g_variant_iter_free(iter1);
271 static void __netconfig_adjust_tcp_buffer_size(void)
273 int fdr = 0, fdw = 0;
274 int fdrmax = 0, fdwmax = 0;
275 const char *rbuf_size = NULL;
276 const char *wbuf_size = NULL;
277 const char *rmax_size = NULL;
278 const char *wmax_size = NULL;
279 const char *profile = netconfig_get_default_profile();
281 if (profile == NULL) {
282 DBG("There is no default connection");
284 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
285 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
286 } else if (netconfig_is_wifi_profile(profile) == TRUE) {
287 DBG("Default connection: Wi-Fi");
289 rbuf_size = NET_TCP_BUFFERSIZE_WIFI_READ;
290 wbuf_size = NET_TCP_BUFFERSIZE_WIFI_WRITE;
291 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
292 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
293 } else if (netconfig_is_cellular_profile(profile) == TRUE) {
294 TapiHandle *tapi_handle = NULL;
295 int telephony_svctype = 0, telephony_pstype = 0;
297 tapi_handle = (TapiHandle *)netconfig_tel_init();
298 if (NULL != tapi_handle) {
299 tel_get_property_int(tapi_handle,
300 TAPI_PROP_NETWORK_SERVICE_TYPE,
302 tel_get_property_int(tapi_handle, TAPI_PROP_NETWORK_PS_TYPE,
304 netconfig_tel_deinit();
307 DBG("Default cellular %d, %d", telephony_svctype, telephony_pstype);
309 switch (telephony_pstype) {
310 case VCONFKEY_TELEPHONY_PSTYPE_HSPA:
311 rbuf_size = NET_TCP_BUFFERSIZE_HSPA_READ;
312 wbuf_size = NET_TCP_BUFFERSIZE_HSPA_WRITE;
314 case VCONFKEY_TELEPHONY_PSTYPE_HSUPA:
315 rbuf_size = NET_TCP_BUFFERSIZE_HSUPA_READ;
316 wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
318 case VCONFKEY_TELEPHONY_PSTYPE_HSDPA:
319 rbuf_size = NET_TCP_BUFFERSIZE_HSDPA_READ;
320 wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
322 #if !defined TIZEN_WEARABLE
323 case VCONFKEY_TELEPHONY_PSTYPE_HSPAP:
324 rbuf_size = NET_TCP_BUFFERSIZE_HSPAP_READ;
325 wbuf_size = NET_TCP_BUFFERSIZE_HSPAP_WRITE;
329 switch (telephony_svctype) {
330 case VCONFKEY_TELEPHONY_SVCTYPE_LTE:
331 rbuf_size = NET_TCP_BUFFERSIZE_LTE_READ;
332 wbuf_size = NET_TCP_BUFFERSIZE_LTE_WRITE;
333 rmax_size = NET_TCP_BUFFERSIZE_LTE_RMEM_MAX;
335 case VCONFKEY_TELEPHONY_SVCTYPE_3G:
336 rbuf_size = NET_TCP_BUFFERSIZE_UMTS_READ;
337 wbuf_size = NET_TCP_BUFFERSIZE_UMTS_WRITE;
339 case VCONFKEY_TELEPHONY_SVCTYPE_2_5G_EDGE:
340 rbuf_size = NET_TCP_BUFFERSIZE_EDGE_READ;
341 wbuf_size = NET_TCP_BUFFERSIZE_EDGE_WRITE;
343 case VCONFKEY_TELEPHONY_SVCTYPE_2_5G:
344 rbuf_size = NET_TCP_BUFFERSIZE_GPRS_READ;
345 wbuf_size = NET_TCP_BUFFERSIZE_GPRS_WRITE;
348 /* TODO: Check LTE support */
349 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
350 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
356 DBG("Default TCP buffer configured");
358 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
359 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
362 if (rbuf_size != NULL) {
363 fdr = open(NET_PROC_SYS_NET_IPV4_TCP_RMEM, O_RDWR | O_CLOEXEC);
365 if (fdr < 0 || write(fdr, rbuf_size, strlen(rbuf_size)) < 0)
366 ERR("Failed to set TCP read buffer size");
372 if (wbuf_size != NULL) {
373 fdw = open(NET_PROC_SYS_NET_IPv4_TCP_WMEM, O_RDWR | O_CLOEXEC);
375 if (fdw < 0 || write(fdw, wbuf_size, strlen(wbuf_size)) < 0)
376 ERR("Failed to set TCP write buffer size");
383 if (rmax_size == NULL)
384 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
385 if (wmax_size == NULL)
386 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
388 if (rmax_size != NULL) {
389 fdrmax = open(NET_PROC_SYS_NET_CORE_RMEM_MAX, O_RDWR | O_CLOEXEC);
391 if (fdrmax < 0 || write(fdrmax, rmax_size, strlen(rmax_size)) < 0)
392 ERR("Failed to set TCP rmem_max size");
398 if (wmax_size != NULL) {
399 fdwmax = open(NET_PROC_SYS_NET_CORE_WMEM_MAX, O_RDWR | O_CLOEXEC);
401 if (fdwmax < 0 || write(fdwmax, wmax_size, strlen(wmax_size)) < 0)
402 ERR("Failed to set TCP wmem_max size");
409 static void __netconfig_update_default_connection_info(void)
411 int old_network_status = 0;
412 const char *profile = netconfig_get_default_profile();
413 const char *ip_addr = netconfig_get_default_ipaddress();
414 const char *ip_addr6 = netconfig_get_default_ipaddress6();
415 const char *proxy_addr = netconfig_get_default_proxy();
416 unsigned int freq = netconfig_get_default_frequency();
418 if (emulator_is_emulated() == TRUE)
422 DBG("Reset network state configuration");
424 DBG("%s: ip(%s) proxy(%s)", profile, ip_addr, proxy_addr);
426 vconf_get_int(VCONFKEY_NETWORK_STATUS, &old_network_status);
428 if (profile == NULL && old_network_status != VCONFKEY_NETWORK_OFF) {
429 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
431 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
432 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
434 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 0);
435 netconfig_set_vconf_int("memory/private/wifi/frequency", 0);
437 DBG("Successfully clear IP and PROXY up");
439 /* Disable clatd if it is in running state */
440 netconfig_clatd_disable();
441 } else if (profile != NULL) {
442 char *old_ip = vconf_get_str(VCONFKEY_NETWORK_IP);
443 char *old_proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY);
445 if (netconfig_is_wifi_profile(profile) == TRUE) {
446 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_WIFI);
447 netconfig_set_vconf_int("memory/private/wifi/frequency", freq);
449 netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
450 EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_WIFI);
451 } else if (netconfig_is_cellular_profile(profile)) {
453 if (!netconfig_is_cellular_internet_profile(profile)) {
454 DBG("connection is not a internet profile - stop to update the cellular state");
458 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_CELLULAR);
460 netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
461 EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_CELLULAR);
463 /* Enable clatd if IPv6 is set and no IPv4 address */
464 if (!ip_addr && ip_addr6)
465 netconfig_clatd_enable();
466 } else if (netconfig_is_ethernet_profile(profile) == TRUE) {
467 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_ETHERNET);
468 netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
469 EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_ETHERNET);
470 } else if (netconfig_is_bluetooth_profile(profile) == TRUE) {
471 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_BLUETOOTH);
472 netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
473 EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_BT);
475 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
476 netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
477 EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_DISCONNECTED);
480 if (g_strcmp0(old_ip, ip_addr) != 0 || old_ip == NULL) {
482 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr);
483 else if (ip_addr6 != NULL)
484 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr6);
486 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
490 if (g_strcmp0(old_proxy, proxy_addr) != 0) {
491 if (proxy_addr == NULL)
492 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
494 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, proxy_addr);
498 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 1);
500 DBG("Successfully update default network configuration");
502 /* Disable clatd if it is in running state */
503 if (netconfig_is_cellular_profile(profile) != TRUE)
504 netconfig_clatd_disable();
507 __netconfig_adjust_tcp_buffer_size();
510 static gboolean __netconfig_is_tech_state_connected(void)
512 gboolean ret = FALSE;
513 GVariant *message = NULL, *variant;
514 GVariantIter *iter, *next;
518 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
519 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
520 "GetTechnologies", NULL);
522 if (message == NULL) {
523 DBG("Fail to get technology state");
527 g_variant_get(message, "(a(oa{sv}))", &iter);
528 while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
532 while (g_variant_iter_loop(next, "{sv}", &key, &variant)) {
534 if (g_strcmp0(key, "Connected") == 0) {
535 data = g_variant_get_boolean(variant);
536 DBG("%s [%s: %s]", path, key, data ? "True" : "False");
541 g_variant_unref(variant);
542 g_variant_iter_free(next);
550 g_variant_iter_free(iter);
551 g_variant_unref(message);
556 static void __netconfig_update_if_service_connected(void)
558 GVariant *message = NULL, *var;
559 GVariantIter *iter, *next;
563 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
564 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
565 "GetServices", NULL);
567 if (message == NULL) {
568 ERR("Failed to get services");
572 g_variant_get(message, "(a(oa{sv}))", &iter);
573 while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
577 if (g_str_has_prefix(path,
578 CONNMAN_WIFI_SERVICE_PROFILE_PREFIX) == TRUE) {
579 if (g_strrstr(path + strlen(CONNMAN_WIFI_SERVICE_PROFILE_PREFIX),
581 /* skip hidden profiles */
585 } else if (g_str_has_prefix(path,
586 CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX) == TRUE) {
592 while (g_variant_iter_loop(next, "{sv}", &key, &var)) {
593 if (g_strcmp0(key, "State") == 0) {
594 const gchar *sdata = NULL;
595 sdata = g_variant_get_string(var, NULL);
596 DBG("%s [%s: %s]", path, key, sdata);
598 if (g_strcmp0(sdata, "online") == 0 || g_strcmp0(sdata, "ready") == 0) {
600 /* Found a connected WiFi / 3G service.
601 * Lets update the default profile info.
603 netconfig_update_default_profile((const gchar*)path);
606 g_variant_unref(var);
607 g_variant_iter_free(next);
614 g_variant_iter_free(iter);
615 g_variant_unref(message);
620 static void __netconfig_network_notify_result(const char *sig_name, const char *key)
624 GVariantBuilder *builder = NULL;
625 GDBusConnection *connection = NULL;
626 GError *error = NULL;
627 const char *prop_key = "key";
629 INFO("[Signal] %s %s", sig_name, key);
631 connection = netdbus_get_connection();
632 if (connection == NULL) {
633 ERR("Failed to get GDBus Connection");
637 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
638 g_variant_builder_add(builder, "{sv}", prop_key, g_variant_new_string(key));
639 params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
641 g_variant_builder_unref(builder);
643 reply = g_dbus_connection_emit_signal(connection,
645 NETCONFIG_NETWORK_PATH,
646 NETCONFIG_NETWORK_INTERFACE,
653 ERR("Failed to send signal [%s]", error->message);
659 INFO("Sent signal (%s), key (%s)", sig_name, key);
663 const char *netconfig_get_default_profile(void)
665 return netconfig_default_connection_info.profile;
668 const char *netconfig_get_default_ifname(void)
670 return netconfig_default_connection_info.ifname;
673 const char *netconfig_get_default_ipaddress(void)
675 return netconfig_default_connection_info.ipaddress;
678 const char *netconfig_get_default_ipaddress6(void)
680 return netconfig_default_connection_info.ipaddress6;
683 const char *netconfig_get_default_proxy(void)
685 return netconfig_default_connection_info.proxy;
688 unsigned int netconfig_get_default_frequency(void)
690 return netconfig_default_connection_info.freq;
693 const char *netconfig_wifi_get_connected_essid(const char *default_profile)
695 if (default_profile == NULL)
698 if (netconfig_is_wifi_profile(default_profile) != TRUE)
701 if (g_strcmp0(default_profile, netconfig_default_connection_info.profile) != 0)
704 return netconfig_default_connection_info.essid;
707 static int __netconfig_reset_ipv4_socket(void)
712 struct sockaddr_in sai;
713 const char *ipaddr = netconfig_get_default_ipaddress();
714 DBG("ipaddr-[%s]", ipaddr);
719 fd = socket(AF_INET, SOCK_DGRAM, 0);
723 memset(&sai, 0, sizeof(struct sockaddr_in));
724 sai.sin_family = AF_INET;
726 if (!inet_aton(ipaddr, &sai.sin_addr)) {
727 DBG("fail to inet_aton()");
732 memset(&ifr, 0, sizeof(struct ifreq));
733 memcpy(&ifr.ifr_addr, &sai, sizeof(sai));
734 g_strlcpy((char *)ifr.ifr_name, WIFI_IFNAME, IFNAMSIZ);
737 #define SIOCKILLADDR 0x8939
740 ret = ioctl(fd, SIOCKILLADDR, &ifr);
742 DBG("fail to ioctl[SIOCKILLADDR]");
751 void netconfig_update_default_profile(const char *profile)
753 static char *old_profile = NULL;
755 /* It's automatically updated by signal-handler
756 * DO NOT update manually
758 * It is going to update default connection information
761 if (netconfig_default_connection_info.profile != NULL) {
763 if (netconfig_is_wifi_profile(netconfig_default_connection_info.profile))
764 __netconfig_reset_ipv4_socket();
767 old_profile = strdup(netconfig_default_connection_info.profile);
769 g_free(netconfig_default_connection_info.profile);
770 netconfig_default_connection_info.profile = NULL;
772 g_free(netconfig_default_connection_info.ifname);
773 netconfig_default_connection_info.ifname = NULL;
775 g_free(netconfig_default_connection_info.ipaddress);
776 netconfig_default_connection_info.ipaddress = NULL;
778 g_free(netconfig_default_connection_info.ipaddress6);
779 netconfig_default_connection_info.ipaddress6 = NULL;
781 g_free(netconfig_default_connection_info.proxy);
782 netconfig_default_connection_info.proxy = NULL;
784 netconfig_default_connection_info.freq = 0;
786 if (wifi_state_get_service_state() != NETCONFIG_WIFI_CONNECTED) {
787 g_free(netconfig_default_connection_info.essid);
788 netconfig_default_connection_info.essid = NULL;
792 /* default profile is NULL and new connected profile is NULL */
794 profile = __netconfig_get_default_profile();
796 if (profile && netconfig_is_cellular_profile(profile) &&
797 !netconfig_is_cellular_internet_profile(profile)) {
798 DBG("not a default cellular profile");
803 __netconfig_update_default_connection_info();
808 netconfig_default_connection_info.profile = g_strdup(profile);
809 __netconfig_get_default_connection_info(profile);
810 __netconfig_update_default_connection_info();
814 void netconfig_update_default(void)
816 if (__netconfig_is_tech_state_connected() == TRUE)
817 __netconfig_update_if_service_connected();
819 __netconfig_adjust_tcp_buffer_size();
822 char *netconfig_get_ifname(const char *profile)
824 GVariant *message = NULL, *variant;
825 GVariantIter *iter, *next;
828 const gchar *value = NULL;
829 gchar *ifname = NULL;
834 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE, profile,
835 CONNMAN_SERVICE_INTERFACE, "GetProperties", NULL);
836 if (message == NULL) {
837 ERR("Failed to get service properties");
841 g_variant_get(message, "(a{sv})", &iter);
842 while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
843 if (g_strcmp0(key, "Ethernet") == 0) {
844 while (g_variant_iter_loop(next, "{sv}", &key1, &variant)) {
845 if (g_strcmp0(key1, "Interface") == 0) {
846 value = g_variant_get_string(variant, NULL);
847 ifname = g_strdup(value);
853 g_variant_unref(message);
855 g_variant_iter_free(iter);
860 /* Check Ethernet Cable Plug-in /Plug-out Status */
861 void netconfig_network_notify_ethernet_cable_state(const char *key)
863 __netconfig_network_notify_result("EthernetCableState", key);
866 static gboolean handle_add_route(
868 GDBusMethodInvocation *context,
871 gchar *interface, gchar *gateway, gint address_family)
873 const gchar *path = ROUTE_EXEC_PATH;
874 gchar *const args[] = { "/sbin/route", "add", "-net", ip_addr,
875 "netmask", netmask, "dev", interface, NULL };
876 gchar *const envs[] = { NULL };
877 const gchar* buf = NULL;
882 DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
884 switch (address_family) {
886 if (ip_addr == NULL || netmask == NULL || interface == NULL) {
887 ERR("Invalid parameter");
888 netconfig_error_invalid_parameter(context);
892 if (netconfig_execute_file(path, args, envs) < 0) {
893 DBG("Failed to add a new route");
894 netconfig_error_permission_denied(context);
900 if (ip_addr == NULL || interface == NULL || gateway == NULL) {
901 ERR("Invalid parameter");
902 netconfig_error_invalid_parameter(context);
907 ch = strchr(buf, '/');
910 prefix_len = atoi(ch + 1);
911 ip_addr[pos-1] = '\0';
916 if (netconfig_add_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
917 DBG("Failed to add a new route");
918 netconfig_error_permission_denied(context);
923 DBG("Unknown Address Family");
924 netconfig_error_invalid_parameter(context);
928 DBG("Successfully added a new route");
929 network_complete_add_route(object, context, TRUE);
933 static gboolean handle_remove_route(
935 GDBusMethodInvocation *context,
938 gchar *interface, gchar *gateway, gint address_family)
940 const char *path = ROUTE_EXEC_PATH;
941 gchar *const args[] = { "/sbin/route", "del", "-net", ip_addr,
942 "netmask", netmask, "dev", interface, NULL };
943 char *const envs[] = { NULL };
944 const char* buf = NULL;
949 DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
951 switch (address_family) {
953 if (ip_addr == NULL || netmask == NULL || interface == NULL) {
954 DBG("Invalid parameter!");
955 netconfig_error_invalid_parameter(context);
958 if (netconfig_execute_file(path, args, envs) < 0) {
959 DBG("Failed to remove the route");
960 netconfig_error_permission_denied(context);
965 if (ip_addr == NULL || interface == NULL || gateway == NULL) {
966 DBG("Invalid parameter!");
967 netconfig_error_invalid_parameter(context);
972 ch = strchr(buf, '/');
975 prefix_len = atoi(ch + 1);
976 ip_addr[pos-1] = '\0';
981 if (netconfig_del_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
982 DBG("Failed to remove the route");
983 netconfig_error_permission_denied(context);
988 DBG("Unknown Address Family");
989 netconfig_error_invalid_parameter(context);
993 DBG("Successfully removed the route");
994 network_complete_remove_route(object, context, TRUE);
998 static gboolean handle_check_get_privilege(Network *object,
999 GDBusMethodInvocation *context)
1001 network_complete_check_get_privilege(object, context);
1006 static gboolean handle_check_profile_privilege(Network *object,
1007 GDBusMethodInvocation *context)
1009 network_complete_check_profile_privilege(object, context);
1013 gboolean handle_ethernet_cable_state(Network *object,
1014 GDBusMethodInvocation *context)
1019 ret = netconfig_get_ethernet_cable_state(&state);
1021 DBG("Failed to get ethernet cable state");
1022 netconfig_error_fail_ethernet_cable_state(context);
1026 DBG("Successfully get ethernet cable state[%d]", state);
1027 network_complete_ethernet_cable_state(object, context, state);
1031 void state_object_create_and_init(void)
1033 DBG("Creating network state object");
1034 GDBusInterfaceSkeleton *interface_network = NULL;
1035 GDBusConnection *connection = NULL;
1036 GDBusObjectManagerServer *server = netdbus_get_state_manager();
1040 connection = netdbus_get_connection();
1041 g_dbus_object_manager_server_set_connection(server, connection);
1043 /*Interface netconfig.network*/
1044 netconfigstate = network_skeleton_new();
1046 interface_network = G_DBUS_INTERFACE_SKELETON(netconfigstate);
1047 g_signal_connect(netconfigstate, "handle-add-route",
1048 G_CALLBACK(handle_add_route), NULL);
1049 g_signal_connect(netconfigstate, "handle-check-get-privilege",
1050 G_CALLBACK(handle_check_get_privilege), NULL);
1051 g_signal_connect(netconfigstate, "handle-check-profile-privilege",
1052 G_CALLBACK(handle_check_profile_privilege), NULL);
1053 g_signal_connect(netconfigstate, "handle-ethernet-cable-state",
1054 G_CALLBACK(handle_ethernet_cable_state), NULL);
1055 g_signal_connect(netconfigstate, "handle-remove-route",
1056 G_CALLBACK(handle_remove_route), NULL);
1058 if (!g_dbus_interface_skeleton_export(interface_network, connection,
1059 NETCONFIG_NETWORK_STATE_PATH, NULL)) {
1060 ERR("Export with path failed");
1064 void state_object_deinit(void)
1066 g_object_unref(netconfigstate);