2 * Network Configuration Module
4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 #include <vconf-keys.h>
26 #include <arpa/inet.h>
27 #include <netinet/in.h>
28 #include <sys/ioctl.h>
35 #include "wifi-state.h"
36 #include "wifi-power.h"
37 #include "network-state.h"
38 #include "network-dpm.h"
39 #include "network-monitor.h"
40 #include "netsupplicant.h"
42 #include "generated-code.h"
43 /* Define TCP buffer sizes for various networks */
44 /* ReadMin, ReadInitial, ReadMax */ /* WriteMin, WriteInitial, WriteMax */
45 #define NET_TCP_BUFFERSIZE_DEFAULT_READ "4096 87380 704512"
46 #define NET_TCP_BUFFERSIZE_DEFAULT_WRITE "4096 16384 110208"
47 #define NET_TCP_BUFFERSIZE_WIFI_READ "524288 1048576 2560000"
48 #define NET_TCP_BUFFERSIZE_WIFI_WRITE "524288 1048576 2560000"
49 #define NET_TCP_BUFFERSIZE_LTE_READ "524288 1048576 2560000"
50 #define NET_TCP_BUFFERSIZE_LTE_WRITE "524288 1048576 2560000"
51 #define NET_TCP_BUFFERSIZE_UMTS_READ "4094 87380 704512"
52 #define NET_TCP_BUFFERSIZE_UMTS_WRITE "4096 16384 110208"
53 #define NET_TCP_BUFFERSIZE_HSPA_READ "4092 87380 704512"
54 #define NET_TCP_BUFFERSIZE_HSPA_WRITE "4096 16384 262144"
55 #define NET_TCP_BUFFERSIZE_HSDPA_READ "4092 87380 704512"
56 #define NET_TCP_BUFFERSIZE_HSDPA_WRITE "4096 16384 262144"
57 #define NET_TCP_BUFFERSIZE_HSUPA_READ "4092 87380 704512"
58 #define NET_TCP_BUFFERSIZE_HSUPA_WRITE "4096 16384 262144"
59 #define NET_TCP_BUFFERSIZE_HSPAP_READ "4092 87380 1220608"
60 #define NET_TCP_BUFFERSIZE_HSPAP_WRITE "4096 16384 1220608"
61 #define NET_TCP_BUFFERSIZE_EDGE_READ "4093 26280 35040"
62 #define NET_TCP_BUFFERSIZE_EDGE_WRITE "4096 16384 35040"
63 #define NET_TCP_BUFFERSIZE_GPRS_READ "4096 30000 30000"
64 #define NET_TCP_BUFFERSIZE_GPRS_WRITE "4096 8760 11680"
66 #define NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX "1048576"
67 #define NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX "2097152"
68 #define NET_TCP_BUFFERSIZE_LTE_RMEM_MAX "5242880"
70 #define NET_TCP_BUFFERSIZE_WIFID_WMEM_MAX "2097152"
72 #define NET_PROC_SYS_NET_IPV4_TCP_RMEM "/proc/sys/net/ipv4/tcp_rmem"
73 #define NET_PROC_SYS_NET_IPv4_TCP_WMEM "/proc/sys/net/ipv4/tcp_wmem"
74 #define NET_PROC_SYS_NET_CORE_RMEM_MAX "/proc/sys/net/core/rmem_max"
75 #define NET_PROC_SYS_NET_CORE_WMEM_MAX "/proc/sys/net/core/wmem_max"
77 #define ROUTE_EXEC_PATH "/sbin/route"
79 static Network *netconfigstate = NULL;
81 struct netconfig_default_connection {
91 static struct netconfig_default_connection
92 netconfig_default_connection_info = { NULL, };
94 gboolean netconfig_iface_network_state_ethernet_cable_state(gint32 *state);
96 static gboolean __netconfig_is_connected(GVariantIter *array)
98 gboolean is_connected = FALSE;
99 GVariant *variant = NULL;
101 const gchar *value = NULL;
103 while (g_variant_iter_loop(array, "{sv}", &key, &variant)) {
104 if (g_strcmp0(key, "State") != 0)
107 if (g_variant_is_of_type(variant, G_VARIANT_TYPE_STRING)) {
108 value = g_variant_get_string(variant, NULL);
109 if (g_strcmp0(value, "ready") == 0 || g_strcmp0(value, "online") == 0)
114 g_variant_unref(variant);
121 static char *__netconfig_get_default_profile(void)
123 GVariant *message = NULL;
126 gchar *default_profile = NULL;
129 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
130 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
131 "GetServices", NULL);
132 if (message == NULL) {
133 ERR("Failed to get profiles");
137 g_variant_get(message, "(a(oa{sv}))", &iter);
138 while (g_variant_iter_loop(iter, "(oa{sv})", &object_path, &next)) {
139 if (object_path == NULL)
142 if (netconfig_is_cellular_profile(object_path) && !netconfig_is_cellular_internet_profile(object_path))
145 if (__netconfig_is_connected(next) == TRUE) {
146 default_profile = g_strdup(object_path);
148 g_variant_iter_free(next);
152 g_variant_iter_free(iter);
153 g_variant_unref(message);
155 return default_profile;
158 static void __netconfig_get_default_connection_info(const char *profile)
160 GVariant *message = NULL, *variant = NULL, *variant2 = NULL;
161 GVariantIter *iter = NULL, *iter1 = NULL, *service = NULL;
162 GVariant *next = NULL;
167 gboolean found_profile = 0;
169 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
170 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
171 "GetServices", NULL);
172 if (message == NULL) {
173 ERR("Failed to get services informations");
177 g_variant_get(message, "(a(oa{sv}))", &service);
178 if (service == NULL) {
179 ERR("Failed to get services iter");
183 while (g_variant_iter_loop(service, "(oa{sv})", &obj_path, &iter)) {
184 if (g_strcmp0(obj_path, profile) == 0) {
191 if (iter == NULL || found_profile == 0) {
192 ERR("Profile %s doesn't exist", profile);
196 while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
197 const gchar *value = NULL;
199 if (g_strcmp0(key, "Name") == 0 &&
200 netconfig_is_wifi_profile(profile) == TRUE) {
201 if (g_variant_is_of_type(next, G_VARIANT_TYPE_STRING)) {
202 value = g_variant_get_string(next, NULL);
204 netconfig_default_connection_info.essid = g_strdup(value);
206 } else if (g_strcmp0(key, "Ethernet") == 0) {
207 g_variant_get(next, "a{sv}", &iter1);
210 while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
211 if (g_strcmp0(key1, "Interface") == 0) {
212 value = g_variant_get_string(variant, NULL);
213 netconfig_default_connection_info.ifname = g_strdup(value);
216 g_variant_iter_free(iter1);
217 } else if (g_strcmp0(key, "IPv4") == 0) {
218 g_variant_get(next, "a{sv}", &iter1);
221 while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
222 if (g_strcmp0(key1, "Address") == 0) {
223 value = g_variant_get_string(variant, NULL);
224 netconfig_default_connection_info.ipaddress = g_strdup(value);
227 g_variant_iter_free(iter1);
228 } else if (g_strcmp0(key, "IPv6") == 0) {
229 g_variant_get(next, "a{sv}", &iter1);
232 while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
233 if (g_strcmp0(key1, "Address") == 0) {
234 value = g_variant_get_string(variant, NULL);
235 netconfig_default_connection_info.ipaddress6 = g_strdup(value);
238 g_variant_iter_free(iter1);
240 } else if (g_strcmp0(key, "Proxy") == 0) {
241 g_variant_get(next, "a{sv}", &iter1);
244 while (g_variant_iter_loop(iter1, "{sv}", &key2, &variant2)) {
245 GVariantIter *iter_sub = NULL;
247 if (g_strcmp0(key2, "Servers") == 0) {
248 if (!g_variant_is_of_type(variant2, G_VARIANT_TYPE_STRING_ARRAY)) {
250 g_variant_unref(variant2);
254 g_variant_get(variant2, "as", &iter_sub);
255 g_variant_iter_loop(iter_sub, "s", &value);
256 g_variant_iter_free(iter_sub);
257 if (value != NULL && (strlen(value) > 0))
258 netconfig_default_connection_info.proxy = g_strdup(value);
259 } else if (g_strcmp0(key2, "Method") == 0) {
260 if (g_variant_is_of_type(variant2, G_VARIANT_TYPE_STRING)) {
262 g_variant_unref(variant2);
266 value = g_variant_get_string(variant2, NULL);
267 if (g_strcmp0(value, "direct") == 0) {
268 g_free(netconfig_default_connection_info.proxy);
269 netconfig_default_connection_info.proxy = NULL;
272 g_variant_unref(variant2);
277 g_variant_iter_free(iter1);
278 } else if (g_strcmp0(key, "Frequency") == 0) {
279 if (g_variant_is_of_type(next, G_VARIANT_TYPE_UINT16)) {
280 freq = g_variant_get_uint16(next);
281 netconfig_default_connection_info.freq = freq;
288 g_variant_unref(message);
291 g_variant_iter_free(iter);
294 g_variant_iter_free(service);
299 static void __netconfig_adjust_tcp_buffer_size(void)
301 int fdr = 0, fdw = 0;
302 int fdrmax = 0, fdwmax = 0;
303 const char *rbuf_size = NULL;
304 const char *wbuf_size = NULL;
305 const char *rmax_size = NULL;
306 const char *wmax_size = NULL;
307 const char *profile = netconfig_get_default_profile();
309 if (profile == NULL) {
310 DBG("There is no default connection");
312 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
313 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
314 } else if (netconfig_is_wifi_profile(profile) == TRUE) {
315 DBG("Default connection: Wi-Fi");
317 rbuf_size = NET_TCP_BUFFERSIZE_WIFI_READ;
318 wbuf_size = NET_TCP_BUFFERSIZE_WIFI_WRITE;
319 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
320 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
321 } else if (netconfig_is_cellular_profile(profile) == TRUE) {
322 int telephony_svctype = 0, telephony_pstype = 0;
324 netconfig_get_telephony_network_type(&telephony_svctype, &telephony_pstype);
325 DBG("Default cellular %d, %d", telephony_svctype, telephony_pstype);
327 switch (telephony_pstype) {
328 case VCONFKEY_TELEPHONY_PSTYPE_HSPA:
329 rbuf_size = NET_TCP_BUFFERSIZE_HSPA_READ;
330 wbuf_size = NET_TCP_BUFFERSIZE_HSPA_WRITE;
332 case VCONFKEY_TELEPHONY_PSTYPE_HSUPA:
333 rbuf_size = NET_TCP_BUFFERSIZE_HSUPA_READ;
334 wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
336 case VCONFKEY_TELEPHONY_PSTYPE_HSDPA:
337 rbuf_size = NET_TCP_BUFFERSIZE_HSDPA_READ;
338 wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
340 #if !defined TIZEN_WEARABLE
341 case VCONFKEY_TELEPHONY_PSTYPE_HSPAP:
342 rbuf_size = NET_TCP_BUFFERSIZE_HSPAP_READ;
343 wbuf_size = NET_TCP_BUFFERSIZE_HSPAP_WRITE;
347 switch (telephony_svctype) {
348 case VCONFKEY_TELEPHONY_SVCTYPE_LTE:
349 rbuf_size = NET_TCP_BUFFERSIZE_LTE_READ;
350 wbuf_size = NET_TCP_BUFFERSIZE_LTE_WRITE;
351 rmax_size = NET_TCP_BUFFERSIZE_LTE_RMEM_MAX;
353 case VCONFKEY_TELEPHONY_SVCTYPE_3G:
354 rbuf_size = NET_TCP_BUFFERSIZE_UMTS_READ;
355 wbuf_size = NET_TCP_BUFFERSIZE_UMTS_WRITE;
357 case VCONFKEY_TELEPHONY_SVCTYPE_2_5G_EDGE:
358 rbuf_size = NET_TCP_BUFFERSIZE_EDGE_READ;
359 wbuf_size = NET_TCP_BUFFERSIZE_EDGE_WRITE;
361 case VCONFKEY_TELEPHONY_SVCTYPE_2_5G:
362 rbuf_size = NET_TCP_BUFFERSIZE_GPRS_READ;
363 wbuf_size = NET_TCP_BUFFERSIZE_GPRS_WRITE;
366 /* TODO: Check LTE support */
367 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
368 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
374 DBG("Default TCP buffer configured");
376 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
377 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
380 if (rbuf_size != NULL) {
381 fdr = open(NET_PROC_SYS_NET_IPV4_TCP_RMEM, O_RDWR | O_CLOEXEC);
383 if (fdr < 0 || write(fdr, rbuf_size, strlen(rbuf_size)) < 0)
384 ERR("Failed to set TCP read buffer size");
390 if (wbuf_size != NULL) {
391 fdw = open(NET_PROC_SYS_NET_IPv4_TCP_WMEM, O_RDWR | O_CLOEXEC);
393 if (fdw < 0 || write(fdw, wbuf_size, strlen(wbuf_size)) < 0)
394 ERR("Failed to set TCP write buffer size");
401 if (rmax_size == NULL)
402 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
403 if (wmax_size == NULL)
404 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
406 if (rmax_size != NULL) {
407 fdrmax = open(NET_PROC_SYS_NET_CORE_RMEM_MAX, O_RDWR | O_CLOEXEC);
409 if (fdrmax < 0 || write(fdrmax, rmax_size, strlen(rmax_size)) < 0)
410 ERR("Failed to set TCP rmem_max size");
416 if (wmax_size != NULL) {
417 fdwmax = open(NET_PROC_SYS_NET_CORE_WMEM_MAX, O_RDWR | O_CLOEXEC);
419 if (fdwmax < 0 || write(fdwmax, wmax_size, strlen(wmax_size)) < 0)
420 ERR("Failed to set TCP wmem_max size");
427 static void __netconfig_update_default_connection_info(void)
429 int old_network_status = 0;
430 const char *profile = netconfig_get_default_profile();
431 const char *ip_addr = netconfig_get_default_ipaddress();
432 const char *ip_addr6 = netconfig_get_default_ipaddress6();
433 const char *proxy_addr = netconfig_get_default_proxy();
434 unsigned int freq = netconfig_get_default_frequency();
436 if (emulator_is_emulated() == TRUE) {
438 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr);
440 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
442 if (ip_addr6 != NULL)
443 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, ip_addr6);
445 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, "");
451 DBG("Reset network state configuration");
453 DBG("%s: ip(%s) ip6(%s) proxy(%s)", profile, ip_addr, ip_addr6, proxy_addr);
455 netconfig_vconf_get_int(VCONFKEY_NETWORK_STATUS, &old_network_status);
457 if (profile == NULL && old_network_status != VCONFKEY_NETWORK_OFF) {
458 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
460 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
461 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, "");
462 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
464 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 0);
465 netconfig_set_vconf_int("memory/private/wifi/frequency", 0);
467 DBG("Successfully clear IP and PROXY up");
469 } else if (profile != NULL) {
470 char *old_ip = vconf_get_str(VCONFKEY_NETWORK_IP);
471 char *old_ip6 = vconf_get_str(VCONFKEY_NETWORK_IP6);
472 char *old_proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY);
474 if (netconfig_is_wifi_profile(profile) == TRUE) {
475 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_WIFI);
476 netconfig_set_vconf_int("memory/private/wifi/frequency", freq);
478 netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
479 EKEY_NETWORK_STATUS, EVAL_NETWORK_WIFI);
480 } else if (netconfig_is_cellular_profile(profile)) {
482 if (!netconfig_is_cellular_internet_profile(profile)) {
483 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_EVT_NETWORK_STATUS,
496 EKEY_NETWORK_STATUS, EVAL_NETWORK_CELLULAR);
497 } else if (netconfig_is_ethernet_profile(profile) == TRUE) {
498 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_ETHERNET);
499 netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
500 EKEY_NETWORK_STATUS, EVAL_NETWORK_ETHERNET);
501 } else if (netconfig_is_bluetooth_profile(profile) == TRUE) {
502 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_BLUETOOTH);
503 netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
504 EKEY_NETWORK_STATUS, EVAL_NETWORK_BT);
506 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
507 netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
508 EKEY_NETWORK_STATUS, EVAL_NETWORK_DISCONNECTED);
511 if (g_strcmp0(old_ip, ip_addr) != 0 || old_ip == NULL) {
513 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr);
514 else if (old_ip != NULL && strlen(old_ip) > 0)
515 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
520 if (g_strcmp0(old_ip6, ip_addr6) != 0 || old_ip6 == NULL) {
521 if (ip_addr6 != NULL)
522 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, ip_addr6);
523 else if (old_ip6 != NULL && strlen(old_ip6) > 0)
524 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, "");
529 if (g_strcmp0(old_proxy, proxy_addr) != 0) {
530 if (proxy_addr == NULL)
531 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
533 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, proxy_addr);
538 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 1);
540 DBG("Successfully update default network configuration");
543 __netconfig_adjust_tcp_buffer_size();
546 static gboolean __netconfig_is_tech_state_connected(void)
548 gboolean ret = FALSE;
549 GVariant *message = NULL, *variant;
550 GVariantIter *iter, *next;
554 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
555 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
556 "GetTechnologies", NULL);
558 if (message == NULL) {
559 DBG("Fail to get technology state");
563 g_variant_get(message, "(a(oa{sv}))", &iter);
564 while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
568 while (g_variant_iter_loop(next, "{sv}", &key, &variant)) {
570 if (g_strcmp0(key, "Connected") == 0) {
571 data = g_variant_get_boolean(variant);
572 DBG("%s [%s: %s]", path, key, data ? "True" : "False");
577 g_variant_unref(variant);
578 g_variant_iter_free(next);
586 g_variant_iter_free(iter);
587 g_variant_unref(message);
592 static void __netconfig_update_if_service_connected(void)
594 GVariant *message = NULL, *var;
595 GVariantIter *iter, *next;
599 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
600 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
601 "GetServices", NULL);
603 if (message == NULL) {
604 ERR("Failed to get services");
608 g_variant_get(message, "(a(oa{sv}))", &iter);
609 while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
613 if (g_str_has_prefix(path,
614 CONNMAN_WIFI_SERVICE_PROFILE_PREFIX) == TRUE) {
615 if (g_strrstr(path + strlen(CONNMAN_WIFI_SERVICE_PROFILE_PREFIX),
617 /* skip hidden profiles */
621 } else if (g_str_has_prefix(path,
622 CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX) == TRUE) {
628 while (g_variant_iter_loop(next, "{sv}", &key, &var)) {
629 if (g_strcmp0(key, "State") == 0) {
630 const gchar *sdata = NULL;
631 sdata = g_variant_get_string(var, NULL);
632 DBG("%s [%s: %s]", path, key, sdata);
634 if (g_strcmp0(sdata, "online") == 0 || g_strcmp0(sdata, "ready") == 0) {
636 /* Found a connected WiFi / 3G service.
637 * Lets update the default profile info.
639 netconfig_update_default_profile((const gchar*)path);
642 g_variant_unref(var);
643 g_variant_iter_free(next);
650 g_variant_iter_free(iter);
651 g_variant_unref(message);
656 static void __netconfig_network_notify_result(const char *sig_name, const char *key)
658 GVariantBuilder *builder;
661 INFO("[Signal] %s %s", sig_name, key);
663 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
664 g_variant_builder_add(builder, "{sv}", "key", g_variant_new_string(key));
666 params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
667 g_variant_builder_unref(builder);
669 netconfig_dbus_emit_signal(NULL,
670 NETCONFIG_NETWORK_PATH,
671 NETCONFIG_NETWORK_INTERFACE,
678 const char *netconfig_get_default_profile(void)
680 return netconfig_default_connection_info.profile;
683 const char *netconfig_get_default_ifname(void)
685 return netconfig_default_connection_info.ifname;
688 const char *netconfig_get_default_ipaddress(void)
690 return netconfig_default_connection_info.ipaddress;
693 const char *netconfig_get_default_ipaddress6(void)
695 return netconfig_default_connection_info.ipaddress6;
698 const char *netconfig_get_default_proxy(void)
700 return netconfig_default_connection_info.proxy;
703 unsigned int netconfig_get_default_frequency(void)
705 return netconfig_default_connection_info.freq;
708 const char *netconfig_wifi_get_connected_essid(const char *default_profile)
710 if (default_profile == NULL)
713 if (netconfig_is_wifi_profile(default_profile) != TRUE)
716 if (g_strcmp0(default_profile, netconfig_default_connection_info.profile) != 0)
719 return netconfig_default_connection_info.essid;
722 static int __netconfig_reset_ipv4_socket(void)
727 struct sockaddr_in sai;
728 const char *ipaddr = netconfig_get_default_ipaddress();
729 DBG("ipaddr-[%s]", ipaddr);
734 fd = socket(AF_INET, SOCK_DGRAM, 0);
738 memset(&sai, 0, sizeof(struct sockaddr_in));
739 sai.sin_family = AF_INET;
741 if (!inet_aton(ipaddr, &sai.sin_addr)) {
742 DBG("fail to inet_aton()");
747 memset(&ifr, 0, sizeof(struct ifreq));
748 memcpy(&ifr.ifr_addr, &sai, sizeof(sai));
749 g_strlcpy((char *)ifr.ifr_name, WIFI_IFNAME, IFNAMSIZ);
752 #define SIOCKILLADDR 0x8939
755 ret = ioctl(fd, SIOCKILLADDR, &ifr);
757 DBG("fail to ioctl[SIOCKILLADDR]");
766 void netconfig_update_default_profile(const char *profile)
768 static char *old_profile = NULL;
770 /* It's automatically updated by signal-handler
771 * DO NOT update manually
773 * It is going to update default connection information
776 if (netconfig_default_connection_info.profile != NULL) {
778 if (netconfig_is_wifi_profile(netconfig_default_connection_info.profile))
779 __netconfig_reset_ipv4_socket();
782 old_profile = strdup(netconfig_default_connection_info.profile);
784 g_free(netconfig_default_connection_info.profile);
785 netconfig_default_connection_info.profile = NULL;
787 g_free(netconfig_default_connection_info.ifname);
788 netconfig_default_connection_info.ifname = NULL;
790 g_free(netconfig_default_connection_info.ipaddress);
791 netconfig_default_connection_info.ipaddress = NULL;
793 g_free(netconfig_default_connection_info.ipaddress6);
794 netconfig_default_connection_info.ipaddress6 = NULL;
796 g_free(netconfig_default_connection_info.proxy);
797 netconfig_default_connection_info.proxy = NULL;
799 netconfig_default_connection_info.freq = 0;
801 if (wifi_state_get_service_state() != NETCONFIG_WIFI_CONNECTED) {
802 g_free(netconfig_default_connection_info.essid);
803 netconfig_default_connection_info.essid = NULL;
807 /* default profile is NULL and new connected profile is NULL */
809 char *tmp_profile = __netconfig_get_default_profile();
811 if (tmp_profile && netconfig_is_cellular_profile(tmp_profile) &&
812 !netconfig_is_cellular_internet_profile(tmp_profile)) {
813 DBG("not a default cellular profile");
819 __netconfig_update_default_connection_info();
823 netconfig_default_connection_info.profile = g_strdup(tmp_profile);
824 __netconfig_get_default_connection_info(tmp_profile);
825 __netconfig_update_default_connection_info();
830 netconfig_default_connection_info.profile = g_strdup(profile);
831 __netconfig_get_default_connection_info(profile);
832 __netconfig_update_default_connection_info();
835 void netconfig_update_default(void)
837 if (__netconfig_is_tech_state_connected() == TRUE)
838 __netconfig_update_if_service_connected();
840 __netconfig_adjust_tcp_buffer_size();
843 char *netconfig_get_ifname(const char *profile)
845 GVariant *message = NULL, *variant;
846 GVariantIter *iter, *next, *service;
850 const gchar *value = NULL;
851 gchar *ifname = NULL;
852 gboolean found_profile = 0;
857 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
858 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
859 "GetServices", NULL);
860 if (message == NULL) {
861 ERR("Failed to get services informations");
865 g_variant_get(message, "(a(oa{sv}))", &service);
866 while (g_variant_iter_loop(service, "(oa{sv})", &obj_path, &iter)) {
867 if (g_strcmp0(obj_path, profile) == 0) {
874 if (found_profile == 0) {
875 ERR("Profile %s doesn't exist", profile);
876 g_variant_iter_free(service);
877 g_variant_unref(message);
881 while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
882 if (g_strcmp0(key, "Ethernet") == 0) {
883 while (g_variant_iter_loop(next, "{sv}", &key1, &variant)) {
884 if (g_strcmp0(key1, "Interface") == 0) {
885 value = g_variant_get_string(variant, NULL);
886 ifname = g_strdup(value);
888 g_variant_unref(variant);
895 g_variant_unref(message);
897 g_variant_iter_free(service);
898 g_variant_iter_free(iter);
903 /* Check Ethernet Cable Plug-in /Plug-out Status */
904 void netconfig_network_notify_ethernet_cable_state(const char *key)
906 __netconfig_network_notify_result("EthernetCableState", key);
909 static gboolean handle_add_route(
911 GDBusMethodInvocation *context,
914 gchar *interface, gchar *gateway, gint address_family)
916 const gchar *path = ROUTE_EXEC_PATH;
917 gchar *const args[] = { "/sbin/route", "add", "-net", ip_addr,
918 "netmask", netmask, "dev", interface, NULL };
919 gchar *const envs[] = { NULL };
920 const gchar* buf = NULL;
925 DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
927 switch (address_family) {
929 if (ip_addr == NULL || netmask == NULL || interface == NULL) {
930 ERR("Invalid parameter");
931 netconfig_error_invalid_parameter(context);
935 if (netconfig_execute_file(path, args, envs) < 0) {
936 DBG("Failed to add a new route");
937 netconfig_error_permission_denied(context);
943 if (ip_addr == NULL || interface == NULL || gateway == NULL) {
944 ERR("Invalid parameter");
945 netconfig_error_invalid_parameter(context);
950 ch = strchr(buf, '/');
953 prefix_len = atoi(ch + 1);
954 ip_addr[pos-1] = '\0';
959 if (netconfig_add_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
960 DBG("Failed to add a new route");
961 netconfig_error_permission_denied(context);
966 DBG("Unknown Address Family");
967 netconfig_error_invalid_parameter(context);
971 DBG("Successfully added a new route");
972 network_complete_add_route(object, context, TRUE);
976 static gboolean handle_remove_route(
978 GDBusMethodInvocation *context,
981 gchar *interface, gchar *gateway, gint address_family)
983 const char *path = ROUTE_EXEC_PATH;
984 gchar *const args[] = { "/sbin/route", "del", "-net", ip_addr,
985 "netmask", netmask, "dev", interface, NULL };
986 char *const envs[] = { NULL };
987 const char* buf = NULL;
992 DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
994 switch (address_family) {
996 if (ip_addr == NULL || netmask == NULL || interface == NULL) {
997 DBG("Invalid parameter!");
998 netconfig_error_invalid_parameter(context);
1001 if (netconfig_execute_file(path, args, envs) < 0) {
1002 DBG("Failed to remove the route");
1003 netconfig_error_permission_denied(context);
1008 if (ip_addr == NULL || interface == NULL || gateway == NULL) {
1009 DBG("Invalid parameter!");
1010 netconfig_error_invalid_parameter(context);
1015 ch = strchr(buf, '/');
1018 prefix_len = atoi(ch + 1);
1019 ip_addr[pos-1] = '\0';
1024 if (netconfig_del_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
1025 DBG("Failed to remove the route");
1026 netconfig_error_permission_denied(context);
1031 DBG("Unknown Address Family");
1032 netconfig_error_invalid_parameter(context);
1036 DBG("Successfully removed the route");
1037 network_complete_remove_route(object, context, TRUE);
1041 static gboolean handle_check_get_privilege(Network *object,
1042 GDBusMethodInvocation *context)
1044 network_complete_check_get_privilege(object, context);
1049 static gboolean handle_check_profile_privilege(Network *object,
1050 GDBusMethodInvocation *context)
1052 network_complete_check_profile_privilege(object, context);
1056 static gboolean handle_check_internet_privilege(Network *object,
1057 GDBusMethodInvocation *context)
1059 network_complete_check_internet_privilege(object, context);
1063 gboolean handle_ethernet_cable_state(Network *object,
1064 GDBusMethodInvocation *context)
1069 ret = netconfig_get_ethernet_cable_state(&state);
1071 DBG("Failed to get ethernet cable state");
1072 netconfig_error_fail_ethernet_cable_state(context);
1076 DBG("Successfully get ethernet cable state[%d]", state);
1077 network_complete_ethernet_cable_state(object, context, state);
1081 void state_object_create_and_init(void)
1083 DBG("Creating network state object");
1084 GDBusInterfaceSkeleton *interface_network = NULL;
1085 GDBusConnection *connection = NULL;
1086 GDBusObjectManagerServer *server = netdbus_get_state_manager();
1090 connection = netdbus_get_connection();
1091 g_dbus_object_manager_server_set_connection(server, connection);
1093 /*Interface netconfig.network*/
1094 netconfigstate = network_skeleton_new();
1096 interface_network = G_DBUS_INTERFACE_SKELETON(netconfigstate);
1097 g_signal_connect(netconfigstate, "handle-add-route",
1098 G_CALLBACK(handle_add_route), NULL);
1099 g_signal_connect(netconfigstate, "handle-check-get-privilege",
1100 G_CALLBACK(handle_check_get_privilege), NULL);
1101 g_signal_connect(netconfigstate, "handle-check-profile-privilege",
1102 G_CALLBACK(handle_check_profile_privilege), NULL);
1103 g_signal_connect(netconfigstate, "handle-check-internet-privilege",
1104 G_CALLBACK(handle_check_internet_privilege), NULL);
1105 g_signal_connect(netconfigstate, "handle-ethernet-cable-state",
1106 G_CALLBACK(handle_ethernet_cable_state), NULL);
1107 g_signal_connect(netconfigstate, "handle-remove-route",
1108 G_CALLBACK(handle_remove_route), NULL);
1109 g_signal_connect(netconfigstate, "handle-launch-mdns",
1110 G_CALLBACK(handle_launch_mdns), NULL);
1111 g_signal_connect(netconfigstate, "handle-device-policy-set-wifi",
1112 G_CALLBACK(handle_device_policy_set_wifi), NULL);
1113 g_signal_connect(netconfigstate, "handle-device-policy-get-wifi",
1114 G_CALLBACK(handle_device_policy_get_wifi), NULL);
1115 g_signal_connect(netconfigstate, "handle-device-policy-set-wifi-profile",
1116 G_CALLBACK(handle_device_policy_set_wifi_profile), NULL);
1117 g_signal_connect(netconfigstate, "handle-device-policy-get-wifi-profile",
1118 G_CALLBACK(handle_device_policy_get_wifi_profile), NULL);
1120 if (!g_dbus_interface_skeleton_export(interface_network, connection,
1121 NETCONFIG_NETWORK_STATE_PATH, NULL)) {
1122 ERR("Export with path failed");
1126 void state_object_deinit(void)
1128 g_object_unref(netconfigstate);