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"
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, *service = NULL;
168 GVariant *next = NULL;
173 gboolean found_profile = 0;
175 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
176 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
177 "GetServices", NULL);
178 if (message == NULL) {
179 ERR("Failed to get services informations");
183 g_variant_get(message, "(a(oa{sv}))", &service);
184 if (service == NULL) {
185 ERR("Failed to get services iter");
189 while (g_variant_iter_loop(service, "(oa{sv})", &obj_path, &iter)) {
190 if (g_strcmp0(obj_path, profile) == 0) {
197 if (iter == NULL || found_profile == 0) {
198 ERR("Profile %s doesn't exist", profile);
202 while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
203 const gchar *value = NULL;
205 if (g_strcmp0(key, "Name") == 0 &&
206 netconfig_is_wifi_profile(profile) == TRUE) {
207 if (g_variant_is_of_type(next, G_VARIANT_TYPE_STRING)) {
208 value = g_variant_get_string(next, NULL);
210 netconfig_default_connection_info.essid = g_strdup(value);
212 } else if (g_strcmp0(key, "Ethernet") == 0) {
213 g_variant_get(next, "a{sv}", &iter1);
216 while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
217 if (g_strcmp0(key1, "Interface") == 0) {
218 value = g_variant_get_string(variant, NULL);
219 netconfig_default_connection_info.ifname = g_strdup(value);
222 g_variant_iter_free(iter1);
223 } else if (g_strcmp0(key, "IPv4") == 0) {
224 g_variant_get(next, "a{sv}", &iter1);
227 while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
228 if (g_strcmp0(key1, "Address") == 0) {
229 value = g_variant_get_string(variant, NULL);
230 netconfig_default_connection_info.ipaddress = g_strdup(value);
233 g_variant_iter_free(iter1);
234 } else if (g_strcmp0(key, "IPv6") == 0) {
235 g_variant_get(next, "a{sv}", &iter1);
238 while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
239 if (g_strcmp0(key1, "Address") == 0) {
240 value = g_variant_get_string(variant, NULL);
241 netconfig_default_connection_info.ipaddress6 = g_strdup(value);
244 g_variant_iter_free(iter1);
246 } else if (g_strcmp0(key, "Proxy") == 0) {
247 g_variant_get(next, "a{sv}", &iter1);
250 while (g_variant_iter_loop(iter1, "{sv}", &key2, &variant2)) {
251 GVariantIter *iter_sub = NULL;
253 if (g_strcmp0(key2, "Servers") == 0) {
254 if (!g_variant_is_of_type(variant2, G_VARIANT_TYPE_STRING_ARRAY)) {
256 g_variant_unref(variant2);
260 g_variant_get(variant2, "as", &iter_sub);
261 g_variant_iter_loop(iter_sub, "s", &value);
262 g_variant_iter_free(iter_sub);
263 if (value != NULL && (strlen(value) > 0))
264 netconfig_default_connection_info.proxy = g_strdup(value);
265 } else if (g_strcmp0(key2, "Method") == 0) {
266 if (g_variant_is_of_type(variant2, G_VARIANT_TYPE_STRING)) {
268 g_variant_unref(variant2);
272 value = g_variant_get_string(variant2, NULL);
273 if (g_strcmp0(value, "direct") == 0) {
274 g_free(netconfig_default_connection_info.proxy);
275 netconfig_default_connection_info.proxy = NULL;
278 g_variant_unref(variant2);
283 g_variant_iter_free(iter1);
284 } else if (g_strcmp0(key, "Frequency") == 0) {
285 if (g_variant_is_of_type(next, G_VARIANT_TYPE_UINT16)) {
286 freq = g_variant_get_uint16(next);
287 netconfig_default_connection_info.freq = freq;
294 g_variant_unref(message);
297 g_variant_iter_free(iter);
300 g_variant_iter_free(service);
305 static void __netconfig_adjust_tcp_buffer_size(void)
307 int fdr = 0, fdw = 0;
308 int fdrmax = 0, fdwmax = 0;
309 const char *rbuf_size = NULL;
310 const char *wbuf_size = NULL;
311 const char *rmax_size = NULL;
312 const char *wmax_size = NULL;
313 const char *profile = netconfig_get_default_profile();
315 if (profile == NULL) {
316 DBG("There is no default connection");
318 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
319 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
320 } else if (netconfig_is_wifi_profile(profile) == TRUE) {
321 DBG("Default connection: Wi-Fi");
323 rbuf_size = NET_TCP_BUFFERSIZE_WIFI_READ;
324 wbuf_size = NET_TCP_BUFFERSIZE_WIFI_WRITE;
325 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
326 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
327 } else if (netconfig_is_cellular_profile(profile) == TRUE) {
328 TapiHandle *tapi_handle = NULL;
329 int telephony_svctype = 0, telephony_pstype = 0;
331 tapi_handle = (TapiHandle *)netconfig_tel_init();
332 if (NULL != tapi_handle) {
333 tel_get_property_int(tapi_handle,
334 TAPI_PROP_NETWORK_SERVICE_TYPE,
336 tel_get_property_int(tapi_handle, TAPI_PROP_NETWORK_PS_TYPE,
338 netconfig_tel_deinit();
341 DBG("Default cellular %d, %d", telephony_svctype, telephony_pstype);
343 switch (telephony_pstype) {
344 case VCONFKEY_TELEPHONY_PSTYPE_HSPA:
345 rbuf_size = NET_TCP_BUFFERSIZE_HSPA_READ;
346 wbuf_size = NET_TCP_BUFFERSIZE_HSPA_WRITE;
348 case VCONFKEY_TELEPHONY_PSTYPE_HSUPA:
349 rbuf_size = NET_TCP_BUFFERSIZE_HSUPA_READ;
350 wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
352 case VCONFKEY_TELEPHONY_PSTYPE_HSDPA:
353 rbuf_size = NET_TCP_BUFFERSIZE_HSDPA_READ;
354 wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
356 #if !defined TIZEN_WEARABLE
357 case VCONFKEY_TELEPHONY_PSTYPE_HSPAP:
358 rbuf_size = NET_TCP_BUFFERSIZE_HSPAP_READ;
359 wbuf_size = NET_TCP_BUFFERSIZE_HSPAP_WRITE;
363 switch (telephony_svctype) {
364 case VCONFKEY_TELEPHONY_SVCTYPE_LTE:
365 rbuf_size = NET_TCP_BUFFERSIZE_LTE_READ;
366 wbuf_size = NET_TCP_BUFFERSIZE_LTE_WRITE;
367 rmax_size = NET_TCP_BUFFERSIZE_LTE_RMEM_MAX;
369 case VCONFKEY_TELEPHONY_SVCTYPE_3G:
370 rbuf_size = NET_TCP_BUFFERSIZE_UMTS_READ;
371 wbuf_size = NET_TCP_BUFFERSIZE_UMTS_WRITE;
373 case VCONFKEY_TELEPHONY_SVCTYPE_2_5G_EDGE:
374 rbuf_size = NET_TCP_BUFFERSIZE_EDGE_READ;
375 wbuf_size = NET_TCP_BUFFERSIZE_EDGE_WRITE;
377 case VCONFKEY_TELEPHONY_SVCTYPE_2_5G:
378 rbuf_size = NET_TCP_BUFFERSIZE_GPRS_READ;
379 wbuf_size = NET_TCP_BUFFERSIZE_GPRS_WRITE;
382 /* TODO: Check LTE support */
383 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
384 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
390 DBG("Default TCP buffer configured");
392 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
393 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
396 if (rbuf_size != NULL) {
397 fdr = open(NET_PROC_SYS_NET_IPV4_TCP_RMEM, O_RDWR | O_CLOEXEC);
399 if (fdr < 0 || write(fdr, rbuf_size, strlen(rbuf_size)) < 0)
400 ERR("Failed to set TCP read buffer size");
406 if (wbuf_size != NULL) {
407 fdw = open(NET_PROC_SYS_NET_IPv4_TCP_WMEM, O_RDWR | O_CLOEXEC);
409 if (fdw < 0 || write(fdw, wbuf_size, strlen(wbuf_size)) < 0)
410 ERR("Failed to set TCP write buffer size");
417 if (rmax_size == NULL)
418 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
419 if (wmax_size == NULL)
420 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
422 if (rmax_size != NULL) {
423 fdrmax = open(NET_PROC_SYS_NET_CORE_RMEM_MAX, O_RDWR | O_CLOEXEC);
425 if (fdrmax < 0 || write(fdrmax, rmax_size, strlen(rmax_size)) < 0)
426 ERR("Failed to set TCP rmem_max size");
432 if (wmax_size != NULL) {
433 fdwmax = open(NET_PROC_SYS_NET_CORE_WMEM_MAX, O_RDWR | O_CLOEXEC);
435 if (fdwmax < 0 || write(fdwmax, wmax_size, strlen(wmax_size)) < 0)
436 ERR("Failed to set TCP wmem_max size");
443 static void __netconfig_update_default_connection_info(void)
445 int old_network_status = 0;
446 const char *profile = netconfig_get_default_profile();
447 const char *ip_addr = netconfig_get_default_ipaddress();
448 const char *ip_addr6 = netconfig_get_default_ipaddress6();
449 const char *proxy_addr = netconfig_get_default_proxy();
450 unsigned int freq = netconfig_get_default_frequency();
452 if (emulator_is_emulated() == TRUE)
456 DBG("Reset network state configuration");
458 DBG("%s: ip(%s) proxy(%s)", profile, ip_addr, proxy_addr);
460 netconfig_vconf_get_int(VCONFKEY_NETWORK_STATUS, &old_network_status);
462 if (profile == NULL && old_network_status != VCONFKEY_NETWORK_OFF) {
463 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
465 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
466 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
468 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 0);
469 netconfig_set_vconf_int("memory/private/wifi/frequency", 0);
471 DBG("Successfully clear IP and PROXY up");
473 } else if (profile != NULL) {
474 char *old_ip = vconf_get_str(VCONFKEY_NETWORK_IP);
475 char *old_proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY);
477 if (netconfig_is_wifi_profile(profile) == TRUE) {
478 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_WIFI);
479 netconfig_set_vconf_int("memory/private/wifi/frequency", freq);
481 netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
482 EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_WIFI);
483 } else if (netconfig_is_cellular_profile(profile)) {
485 if (!netconfig_is_cellular_internet_profile(profile)) {
486 DBG("connection is not a internet profile - stop to update the cellular state");
490 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_CELLULAR);
492 netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
493 EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_CELLULAR);
494 } else if (netconfig_is_ethernet_profile(profile) == TRUE) {
495 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_ETHERNET);
496 netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
497 EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_ETHERNET);
498 } else if (netconfig_is_bluetooth_profile(profile) == TRUE) {
499 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_BLUETOOTH);
500 netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
501 EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_BT);
503 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
504 netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
505 EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_DISCONNECTED);
508 if (g_strcmp0(old_ip, ip_addr) != 0 || old_ip == NULL) {
510 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr);
511 else if (ip_addr6 != NULL)
512 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr6);
514 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
518 if (g_strcmp0(old_proxy, proxy_addr) != 0) {
519 if (proxy_addr == NULL)
520 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
522 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, proxy_addr);
526 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 1);
528 DBG("Successfully update default network configuration");
531 __netconfig_adjust_tcp_buffer_size();
534 static gboolean __netconfig_is_tech_state_connected(void)
536 gboolean ret = FALSE;
537 GVariant *message = NULL, *variant;
538 GVariantIter *iter, *next;
542 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
543 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
544 "GetTechnologies", NULL);
546 if (message == NULL) {
547 DBG("Fail to get technology state");
551 g_variant_get(message, "(a(oa{sv}))", &iter);
552 while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
556 while (g_variant_iter_loop(next, "{sv}", &key, &variant)) {
558 if (g_strcmp0(key, "Connected") == 0) {
559 data = g_variant_get_boolean(variant);
560 DBG("%s [%s: %s]", path, key, data ? "True" : "False");
565 g_variant_unref(variant);
566 g_variant_iter_free(next);
574 g_variant_iter_free(iter);
575 g_variant_unref(message);
580 static void __netconfig_update_if_service_connected(void)
582 GVariant *message = NULL, *var;
583 GVariantIter *iter, *next;
587 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
588 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
589 "GetServices", NULL);
591 if (message == NULL) {
592 ERR("Failed to get services");
596 g_variant_get(message, "(a(oa{sv}))", &iter);
597 while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
601 if (g_str_has_prefix(path,
602 CONNMAN_WIFI_SERVICE_PROFILE_PREFIX) == TRUE) {
603 if (g_strrstr(path + strlen(CONNMAN_WIFI_SERVICE_PROFILE_PREFIX),
605 /* skip hidden profiles */
609 } else if (g_str_has_prefix(path,
610 CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX) == TRUE) {
616 while (g_variant_iter_loop(next, "{sv}", &key, &var)) {
617 if (g_strcmp0(key, "State") == 0) {
618 const gchar *sdata = NULL;
619 sdata = g_variant_get_string(var, NULL);
620 DBG("%s [%s: %s]", path, key, sdata);
622 if (g_strcmp0(sdata, "online") == 0 || g_strcmp0(sdata, "ready") == 0) {
624 /* Found a connected WiFi / 3G service.
625 * Lets update the default profile info.
627 netconfig_update_default_profile((const gchar*)path);
630 g_variant_unref(var);
631 g_variant_iter_free(next);
638 g_variant_iter_free(iter);
639 g_variant_unref(message);
644 static void __netconfig_network_notify_result(const char *sig_name, const char *key)
646 GVariantBuilder *builder;
649 INFO("[Signal] %s %s", sig_name, key);
651 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
652 g_variant_builder_add(builder, "{sv}", "key", g_variant_new_string(key));
654 params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
655 g_variant_builder_unref(builder);
657 netconfig_dbus_emit_signal(NULL,
658 NETCONFIG_NETWORK_PATH,
659 NETCONFIG_NETWORK_INTERFACE,
666 const char *netconfig_get_default_profile(void)
668 return netconfig_default_connection_info.profile;
671 const char *netconfig_get_default_ifname(void)
673 return netconfig_default_connection_info.ifname;
676 const char *netconfig_get_default_ipaddress(void)
678 return netconfig_default_connection_info.ipaddress;
681 const char *netconfig_get_default_ipaddress6(void)
683 return netconfig_default_connection_info.ipaddress6;
686 const char *netconfig_get_default_proxy(void)
688 return netconfig_default_connection_info.proxy;
691 unsigned int netconfig_get_default_frequency(void)
693 return netconfig_default_connection_info.freq;
696 const char *netconfig_wifi_get_connected_essid(const char *default_profile)
698 if (default_profile == NULL)
701 if (netconfig_is_wifi_profile(default_profile) != TRUE)
704 if (g_strcmp0(default_profile, netconfig_default_connection_info.profile) != 0)
707 return netconfig_default_connection_info.essid;
710 static int __netconfig_reset_ipv4_socket(void)
715 struct sockaddr_in sai;
716 const char *ipaddr = netconfig_get_default_ipaddress();
717 DBG("ipaddr-[%s]", ipaddr);
722 fd = socket(AF_INET, SOCK_DGRAM, 0);
726 memset(&sai, 0, sizeof(struct sockaddr_in));
727 sai.sin_family = AF_INET;
729 if (!inet_aton(ipaddr, &sai.sin_addr)) {
730 DBG("fail to inet_aton()");
735 memset(&ifr, 0, sizeof(struct ifreq));
736 memcpy(&ifr.ifr_addr, &sai, sizeof(sai));
737 g_strlcpy((char *)ifr.ifr_name, WIFI_IFNAME, IFNAMSIZ);
740 #define SIOCKILLADDR 0x8939
743 ret = ioctl(fd, SIOCKILLADDR, &ifr);
745 DBG("fail to ioctl[SIOCKILLADDR]");
754 void netconfig_update_default_profile(const char *profile)
756 static char *old_profile = NULL;
758 /* It's automatically updated by signal-handler
759 * DO NOT update manually
761 * It is going to update default connection information
764 if (netconfig_default_connection_info.profile != NULL) {
766 if (netconfig_is_wifi_profile(netconfig_default_connection_info.profile))
767 __netconfig_reset_ipv4_socket();
770 old_profile = strdup(netconfig_default_connection_info.profile);
772 g_free(netconfig_default_connection_info.profile);
773 netconfig_default_connection_info.profile = NULL;
775 g_free(netconfig_default_connection_info.ifname);
776 netconfig_default_connection_info.ifname = NULL;
778 g_free(netconfig_default_connection_info.ipaddress);
779 netconfig_default_connection_info.ipaddress = NULL;
781 g_free(netconfig_default_connection_info.ipaddress6);
782 netconfig_default_connection_info.ipaddress6 = NULL;
784 g_free(netconfig_default_connection_info.proxy);
785 netconfig_default_connection_info.proxy = NULL;
787 netconfig_default_connection_info.freq = 0;
789 if (wifi_state_get_service_state() != NETCONFIG_WIFI_CONNECTED) {
790 g_free(netconfig_default_connection_info.essid);
791 netconfig_default_connection_info.essid = NULL;
795 /* default profile is NULL and new connected profile is NULL */
797 profile = __netconfig_get_default_profile();
799 if (profile && netconfig_is_cellular_profile(profile) &&
800 !netconfig_is_cellular_internet_profile(profile)) {
801 DBG("not a default cellular profile");
806 __netconfig_update_default_connection_info();
811 netconfig_default_connection_info.profile = g_strdup(profile);
812 __netconfig_get_default_connection_info(profile);
813 __netconfig_update_default_connection_info();
817 void netconfig_update_default(void)
819 if (__netconfig_is_tech_state_connected() == TRUE)
820 __netconfig_update_if_service_connected();
822 __netconfig_adjust_tcp_buffer_size();
825 char *netconfig_get_ifname(const char *profile)
827 GVariant *message = NULL, *variant;
828 GVariantIter *iter, *next, *service;
832 const gchar *value = NULL;
833 gchar *ifname = NULL;
834 gboolean found_profile = 0;
839 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
840 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
841 "GetServices", NULL);
842 if (message == NULL) {
843 ERR("Failed to get services informations");
847 g_variant_get(message, "(a(oa{sv}))", &service);
848 while (g_variant_iter_loop(service, "(oa{sv})", &obj_path, &iter)) {
849 if (g_strcmp0(obj_path, profile) == 0) {
856 if (found_profile == 0) {
857 ERR("Profile %s doesn't exist", profile);
858 g_variant_iter_free(service);
859 g_variant_unref(message);
863 while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
864 if (g_strcmp0(key, "Ethernet") == 0) {
865 while (g_variant_iter_loop(next, "{sv}", &key1, &variant)) {
866 if (g_strcmp0(key1, "Interface") == 0) {
867 value = g_variant_get_string(variant, NULL);
868 ifname = g_strdup(value);
874 g_variant_unref(message);
876 g_variant_iter_free(service);
877 g_variant_iter_free(iter);
882 /* Check Ethernet Cable Plug-in /Plug-out Status */
883 void netconfig_network_notify_ethernet_cable_state(const char *key)
885 __netconfig_network_notify_result("EthernetCableState", key);
888 static gboolean handle_add_route(
890 GDBusMethodInvocation *context,
893 gchar *interface, gchar *gateway, gint address_family)
895 const gchar *path = ROUTE_EXEC_PATH;
896 gchar *const args[] = { "/sbin/route", "add", "-net", ip_addr,
897 "netmask", netmask, "dev", interface, NULL };
898 gchar *const envs[] = { NULL };
899 const gchar* buf = NULL;
904 DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
906 switch (address_family) {
908 if (ip_addr == NULL || netmask == NULL || interface == NULL) {
909 ERR("Invalid parameter");
910 netconfig_error_invalid_parameter(context);
914 if (netconfig_execute_file(path, args, envs) < 0) {
915 DBG("Failed to add a new route");
916 netconfig_error_permission_denied(context);
922 if (ip_addr == NULL || interface == NULL || gateway == NULL) {
923 ERR("Invalid parameter");
924 netconfig_error_invalid_parameter(context);
929 ch = strchr(buf, '/');
932 prefix_len = atoi(ch + 1);
933 ip_addr[pos-1] = '\0';
938 if (netconfig_add_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
939 DBG("Failed to add a new route");
940 netconfig_error_permission_denied(context);
945 DBG("Unknown Address Family");
946 netconfig_error_invalid_parameter(context);
950 DBG("Successfully added a new route");
951 network_complete_add_route(object, context, TRUE);
955 static gboolean handle_remove_route(
957 GDBusMethodInvocation *context,
960 gchar *interface, gchar *gateway, gint address_family)
962 const char *path = ROUTE_EXEC_PATH;
963 gchar *const args[] = { "/sbin/route", "del", "-net", ip_addr,
964 "netmask", netmask, "dev", interface, NULL };
965 char *const envs[] = { NULL };
966 const char* buf = NULL;
971 DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
973 switch (address_family) {
975 if (ip_addr == NULL || netmask == NULL || interface == NULL) {
976 DBG("Invalid parameter!");
977 netconfig_error_invalid_parameter(context);
980 if (netconfig_execute_file(path, args, envs) < 0) {
981 DBG("Failed to remove the route");
982 netconfig_error_permission_denied(context);
987 if (ip_addr == NULL || interface == NULL || gateway == NULL) {
988 DBG("Invalid parameter!");
989 netconfig_error_invalid_parameter(context);
994 ch = strchr(buf, '/');
997 prefix_len = atoi(ch + 1);
998 ip_addr[pos-1] = '\0';
1003 if (netconfig_del_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
1004 DBG("Failed to remove the route");
1005 netconfig_error_permission_denied(context);
1010 DBG("Unknown Address Family");
1011 netconfig_error_invalid_parameter(context);
1015 DBG("Successfully removed the route");
1016 network_complete_remove_route(object, context, TRUE);
1020 static gboolean handle_check_get_privilege(Network *object,
1021 GDBusMethodInvocation *context)
1023 network_complete_check_get_privilege(object, context);
1028 static gboolean handle_check_profile_privilege(Network *object,
1029 GDBusMethodInvocation *context)
1031 network_complete_check_profile_privilege(object, context);
1035 static gboolean handle_check_internet_privilege(Network *object,
1036 GDBusMethodInvocation *context)
1038 network_complete_check_internet_privilege(object, context);
1042 gboolean handle_ethernet_cable_state(Network *object,
1043 GDBusMethodInvocation *context)
1048 ret = netconfig_get_ethernet_cable_state(&state);
1050 DBG("Failed to get ethernet cable state");
1051 netconfig_error_fail_ethernet_cable_state(context);
1055 DBG("Successfully get ethernet cable state[%d]", state);
1056 network_complete_ethernet_cable_state(object, context, state);
1060 void state_object_create_and_init(void)
1062 DBG("Creating network state object");
1063 GDBusInterfaceSkeleton *interface_network = NULL;
1064 GDBusConnection *connection = NULL;
1065 GDBusObjectManagerServer *server = netdbus_get_state_manager();
1069 connection = netdbus_get_connection();
1070 g_dbus_object_manager_server_set_connection(server, connection);
1072 /*Interface netconfig.network*/
1073 netconfigstate = network_skeleton_new();
1075 interface_network = G_DBUS_INTERFACE_SKELETON(netconfigstate);
1076 g_signal_connect(netconfigstate, "handle-add-route",
1077 G_CALLBACK(handle_add_route), NULL);
1078 g_signal_connect(netconfigstate, "handle-check-get-privilege",
1079 G_CALLBACK(handle_check_get_privilege), NULL);
1080 g_signal_connect(netconfigstate, "handle-check-profile-privilege",
1081 G_CALLBACK(handle_check_profile_privilege), NULL);
1082 g_signal_connect(netconfigstate, "handle-check-internet-privilege",
1083 G_CALLBACK(handle_check_internet_privilege), NULL);
1084 g_signal_connect(netconfigstate, "handle-ethernet-cable-state",
1085 G_CALLBACK(handle_ethernet_cable_state), NULL);
1086 g_signal_connect(netconfigstate, "handle-remove-route",
1087 G_CALLBACK(handle_remove_route), NULL);
1088 g_signal_connect(netconfigstate, "handle-launch-mdns",
1089 G_CALLBACK(handle_launch_mdns), NULL);
1090 g_signal_connect(netconfigstate, "handle-device-policy-set-wifi",
1091 G_CALLBACK(handle_device_policy_set_wifi), NULL);
1092 g_signal_connect(netconfigstate, "handle-device-policy-get-wifi",
1093 G_CALLBACK(handle_device_policy_get_wifi), NULL);
1094 g_signal_connect(netconfigstate, "handle-device-policy-set-wifi-profile",
1095 G_CALLBACK(handle_device_policy_set_wifi_profile), NULL);
1096 g_signal_connect(netconfigstate, "handle-device-policy-get-wifi-profile",
1097 G_CALLBACK(handle_device_policy_get_wifi_profile), NULL);
1099 if (!g_dbus_interface_skeleton_export(interface_network, connection,
1100 NETCONFIG_NETWORK_STATE_PATH, NULL)) {
1101 ERR("Export with path failed");
1105 void state_object_deinit(void)
1107 g_object_unref(netconfigstate);