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>
27 #include <arpa/inet.h>
28 #include <netinet/in.h>
29 #include <sys/ioctl.h>
31 #include <TapiUtility.h>
38 #include "wifi-state.h"
39 #include "wifi-power.h"
40 #include "network-state.h"
41 #include "network-monitor.h"
42 #include "netsupplicant.h"
43 #include "wifi-tel-intf.h"
44 #include "clatd-handler.h"
46 #include "generated-code.h"
47 /* Define TCP buffer sizes for various networks */
48 /* ReadMin, ReadInitial, ReadMax */ /* WriteMin, WriteInitial, WriteMax */
49 #define NET_TCP_BUFFERSIZE_DEFAULT_READ "4096 87380 704512"
50 #define NET_TCP_BUFFERSIZE_DEFAULT_WRITE "4096 16384 110208"
51 #define NET_TCP_BUFFERSIZE_WIFI_READ "524288 1048576 2560000"
52 #define NET_TCP_BUFFERSIZE_WIFI_WRITE "524288 1048576 2560000"
53 #define NET_TCP_BUFFERSIZE_LTE_READ "524288 1048576 2560000"
54 #define NET_TCP_BUFFERSIZE_LTE_WRITE "524288 1048576 2560000"
55 #define NET_TCP_BUFFERSIZE_UMTS_READ "4094 87380 704512"
56 #define NET_TCP_BUFFERSIZE_UMTS_WRITE "4096 16384 110208"
57 #define NET_TCP_BUFFERSIZE_HSPA_READ "4092 87380 704512"
58 #define NET_TCP_BUFFERSIZE_HSPA_WRITE "4096 16384 262144"
59 #define NET_TCP_BUFFERSIZE_HSDPA_READ "4092 87380 704512"
60 #define NET_TCP_BUFFERSIZE_HSDPA_WRITE "4096 16384 262144"
61 #define NET_TCP_BUFFERSIZE_HSUPA_READ "4092 87380 704512"
62 #define NET_TCP_BUFFERSIZE_HSUPA_WRITE "4096 16384 262144"
63 #define NET_TCP_BUFFERSIZE_HSPAP_READ "4092 87380 1220608"
64 #define NET_TCP_BUFFERSIZE_HSPAP_WRITE "4096 16384 1220608"
65 #define NET_TCP_BUFFERSIZE_EDGE_READ "4093 26280 35040"
66 #define NET_TCP_BUFFERSIZE_EDGE_WRITE "4096 16384 35040"
67 #define NET_TCP_BUFFERSIZE_GPRS_READ "4096 30000 30000"
68 #define NET_TCP_BUFFERSIZE_GPRS_WRITE "4096 8760 11680"
70 #define NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX "1048576"
71 #define NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX "2097152"
72 #define NET_TCP_BUFFERSIZE_LTE_RMEM_MAX "5242880"
74 #define NET_TCP_BUFFERSIZE_WIFID_WMEM_MAX "2097152"
76 #define NET_PROC_SYS_NET_IPV4_TCP_RMEM "/proc/sys/net/ipv4/tcp_rmem"
77 #define NET_PROC_SYS_NET_IPv4_TCP_WMEM "/proc/sys/net/ipv4/tcp_wmem"
78 #define NET_PROC_SYS_NET_CORE_RMEM_MAX "/proc/sys/net/core/rmem_max"
79 #define NET_PROC_SYS_NET_CORE_WMEM_MAX "/proc/sys/net/core/wmem_max"
81 #define ROUTE_EXEC_PATH "/sbin/route"
83 static Network *netconfigstate = NULL;
85 struct netconfig_default_connection {
95 static struct netconfig_default_connection
96 netconfig_default_connection_info = { NULL, };
98 gboolean netconfig_iface_network_state_ethernet_cable_state(gint32 *state);
100 static gboolean __netconfig_is_connected(GVariantIter *array)
102 gboolean is_connected = FALSE;
103 GVariant *variant = NULL;
105 const gchar *value = NULL;
107 while (g_variant_iter_loop(array, "{sv}", &key, &variant)) {
108 if (g_strcmp0(key, "State") != 0) {
112 if (g_variant_is_of_type(variant, G_VARIANT_TYPE_STRING)) {
113 value = g_variant_get_string(variant, NULL);
114 if (g_strcmp0(value, "ready") == 0 || g_strcmp0(value, "online") == 0)
119 g_variant_unref(variant);
126 static char *__netconfig_get_default_profile(void)
128 GVariant *message = NULL;
131 gchar *default_profile = NULL;
134 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
135 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
136 "GetServices", NULL);
137 if (message == NULL) {
138 ERR("Failed to get profiles");
142 g_variant_get(message, "(a(oa{sv}))", &iter);
143 while (g_variant_iter_loop(iter, "(oa{sv})", &object_path, &next)) {
144 if (object_path == NULL) {
148 if(netconfig_is_cellular_profile(object_path) && !netconfig_is_cellular_internet_profile(object_path)){
152 if (__netconfig_is_connected(next) == TRUE) {
153 default_profile = g_strdup(object_path);
155 g_variant_iter_free(next);
159 g_variant_iter_free(iter);
160 g_variant_unref(message);
162 return default_profile;
165 static void __netconfig_get_default_connection_info(const char *profile)
167 GVariant *message = NULL, *variant = NULL, *variant2 = NULL;
168 GVariantIter *iter = NULL, *iter1 = NULL;
169 GVariant *next = NULL;
174 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE, profile,
175 CONNMAN_SERVICE_INTERFACE, "GetProperties", NULL);
176 if (message == NULL) {
177 ERR("Failed to get service properties");
181 g_variant_get(message, "(a{sv})", &iter);
182 while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
183 const gchar *value = NULL;
185 if (g_strcmp0(key, "Name") == 0 &&
186 netconfig_is_wifi_profile(profile) == TRUE) {
187 if (g_variant_is_of_type(next, G_VARIANT_TYPE_STRING)) {
188 value = g_variant_get_string(next, NULL);
190 netconfig_default_connection_info.essid = g_strdup(value);
192 } else if (g_strcmp0(key, "Ethernet") == 0) {
193 g_variant_get(next, "a{sv}", &iter1);
194 while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
195 if (g_strcmp0(key1, "Interface") == 0) {
196 value = g_variant_get_string(variant, NULL);
197 netconfig_default_connection_info.ifname = g_strdup(value);
200 } else if (g_strcmp0(key, "IPv4") == 0) {
201 g_variant_get(next, "a{sv}", &iter1);
202 while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
203 if (g_strcmp0(key1, "Address") == 0) {
204 value = g_variant_get_string(variant, NULL);
205 netconfig_default_connection_info.ipaddress = g_strdup(value);
208 } else if (g_strcmp0(key, "IPv6") == 0) {
209 g_variant_get(next, "a{sv}", &iter1);
210 while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
211 if (g_strcmp0(key1, "Address") == 0) {
212 value = g_variant_get_string(variant, NULL);
213 netconfig_default_connection_info.ipaddress6 = g_strdup(value);
216 } else if (g_strcmp0(key, "Proxy") == 0) {
217 g_variant_get(next, "a{sv}", &iter1);
218 while (g_variant_iter_loop(iter1, "{sv}", &key2, &variant2)) {
219 GVariantIter *iter_sub = NULL;
221 if (g_strcmp0(key2, "Servers") == 0) {
222 if (!g_variant_is_of_type(variant2, G_VARIANT_TYPE_STRING_ARRAY)) {
224 g_variant_unref(variant2);
228 g_variant_get(variant2, "as", &iter_sub);
229 g_variant_iter_loop(iter_sub, "s", &value);
230 g_variant_iter_free(iter_sub);
231 if (value != NULL && (strlen(value) > 0))
232 netconfig_default_connection_info.proxy = g_strdup(value);
233 } else if (g_strcmp0(key2, "Method") == 0) {
234 if (g_variant_is_of_type(variant2, G_VARIANT_TYPE_STRING)) {
236 g_variant_unref(variant2);
240 value = g_variant_get_string(variant2, NULL);
241 if (g_strcmp0(value, "direct") == 0) {
242 g_free(netconfig_default_connection_info.proxy);
243 netconfig_default_connection_info.proxy = NULL;
246 g_variant_unref(variant2);
251 } else if (g_strcmp0(key, "Frequency") == 0) {
252 if (g_variant_is_of_type(next, G_VARIANT_TYPE_UINT16)) {
253 freq = g_variant_get_uint16(next);
254 netconfig_default_connection_info.freq = freq;
261 g_variant_unref(message);
264 g_variant_iter_free (iter);
267 g_variant_iter_free (iter1);
272 static void __netconfig_adjust_tcp_buffer_size(void)
274 int fdr = 0, fdw = 0;
275 int fdrmax = 0, fdwmax = 0;
276 const char *rbuf_size = NULL;
277 const char *wbuf_size = NULL;
278 const char *rmax_size = NULL;
279 const char *wmax_size = NULL;
280 const char *profile = netconfig_get_default_profile();
282 if (profile == NULL) {
283 DBG("There is no default connection");
285 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
286 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
287 } else if (netconfig_is_wifi_profile(profile) == TRUE) {
288 DBG("Default connection: Wi-Fi");
290 rbuf_size = NET_TCP_BUFFERSIZE_WIFI_READ;
291 wbuf_size = NET_TCP_BUFFERSIZE_WIFI_WRITE;
292 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
293 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
294 } else if (netconfig_is_cellular_profile(profile) == TRUE) {
295 TapiHandle *tapi_handle = NULL;
296 int telephony_svctype = 0, telephony_pstype = 0;
298 tapi_handle = (TapiHandle *)netconfig_tel_init();
299 if (NULL != tapi_handle) {
300 tel_get_property_int(tapi_handle,
301 TAPI_PROP_NETWORK_SERVICE_TYPE,
303 tel_get_property_int(tapi_handle, TAPI_PROP_NETWORK_PS_TYPE,
305 netconfig_tel_deinit();
308 DBG("Default cellular %d, %d", telephony_svctype, telephony_pstype);
310 switch (telephony_pstype) {
311 case VCONFKEY_TELEPHONY_PSTYPE_HSPA:
312 rbuf_size = NET_TCP_BUFFERSIZE_HSPA_READ;
313 wbuf_size = NET_TCP_BUFFERSIZE_HSPA_WRITE;
315 case VCONFKEY_TELEPHONY_PSTYPE_HSUPA:
316 rbuf_size = NET_TCP_BUFFERSIZE_HSUPA_READ;
317 wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
319 case VCONFKEY_TELEPHONY_PSTYPE_HSDPA:
320 rbuf_size = NET_TCP_BUFFERSIZE_HSDPA_READ;
321 wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
323 #if !defined TIZEN_WEARABLE
324 case VCONFKEY_TELEPHONY_PSTYPE_HSPAP:
325 rbuf_size = NET_TCP_BUFFERSIZE_HSPAP_READ;
326 wbuf_size = NET_TCP_BUFFERSIZE_HSPAP_WRITE;
330 switch (telephony_svctype) {
331 case VCONFKEY_TELEPHONY_SVCTYPE_LTE:
332 rbuf_size = NET_TCP_BUFFERSIZE_LTE_READ;
333 wbuf_size = NET_TCP_BUFFERSIZE_LTE_WRITE;
334 rmax_size = NET_TCP_BUFFERSIZE_LTE_RMEM_MAX;
336 case VCONFKEY_TELEPHONY_SVCTYPE_3G:
337 rbuf_size = NET_TCP_BUFFERSIZE_UMTS_READ;
338 wbuf_size = NET_TCP_BUFFERSIZE_UMTS_WRITE;
340 case VCONFKEY_TELEPHONY_SVCTYPE_2_5G_EDGE:
341 rbuf_size = NET_TCP_BUFFERSIZE_EDGE_READ;
342 wbuf_size = NET_TCP_BUFFERSIZE_EDGE_WRITE;
344 case VCONFKEY_TELEPHONY_SVCTYPE_2_5G:
345 rbuf_size = NET_TCP_BUFFERSIZE_GPRS_READ;
346 wbuf_size = NET_TCP_BUFFERSIZE_GPRS_WRITE;
349 /* TODO: Check LTE support */
350 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
351 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
357 DBG("Default TCP buffer configured");
359 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
360 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
363 if (rbuf_size != NULL) {
364 fdr = open(NET_PROC_SYS_NET_IPV4_TCP_RMEM, O_RDWR | O_CLOEXEC);
366 if (fdr < 0 || write(fdr, rbuf_size, strlen(rbuf_size)) < 0)
367 ERR("Failed to set TCP read buffer size");
373 if (wbuf_size != NULL) {
374 fdw = open(NET_PROC_SYS_NET_IPv4_TCP_WMEM, O_RDWR | O_CLOEXEC);
376 if (fdw < 0 || write(fdw, wbuf_size, strlen(wbuf_size)) < 0)
377 ERR("Failed to set TCP write buffer size");
384 if (rmax_size == NULL)
385 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
386 if (wmax_size == NULL)
387 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
389 if (rmax_size != NULL) {
390 fdrmax = open(NET_PROC_SYS_NET_CORE_RMEM_MAX, O_RDWR | O_CLOEXEC);
392 if (fdrmax < 0 || write(fdrmax, rmax_size, strlen(rmax_size)) < 0)
393 ERR("Failed to set TCP rmem_max size");
399 if (wmax_size != NULL) {
400 fdwmax = open(NET_PROC_SYS_NET_CORE_WMEM_MAX, O_RDWR | O_CLOEXEC);
402 if (fdwmax < 0 || write(fdwmax, wmax_size, strlen(wmax_size)) < 0)
403 ERR("Failed to set TCP wmem_max size");
410 static void __netconfig_update_default_connection_info(void)
412 int old_network_status = 0;
413 const char *profile = netconfig_get_default_profile();
414 const char *ip_addr = netconfig_get_default_ipaddress();
415 const char *ip_addr6 = netconfig_get_default_ipaddress6();
416 const char *proxy_addr = netconfig_get_default_proxy();
417 unsigned int freq = netconfig_get_default_frequency();
419 if (emulator_is_emulated() == TRUE)
423 DBG("Reset network state configuration");
425 DBG("%s: ip(%s) proxy(%s)", profile, ip_addr, proxy_addr);
427 vconf_get_int(VCONFKEY_NETWORK_STATUS, &old_network_status);
429 if (profile == NULL && old_network_status != VCONFKEY_NETWORK_OFF) {
430 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
432 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
433 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
435 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 0);
436 netconfig_set_vconf_int("memory/private/wifi/frequency", 0);
438 DBG("Successfully clear IP and PROXY up");
440 /* Disable clatd if it is in running state */
441 netconfig_clatd_disable();
443 else if (profile != NULL) {
444 char *old_ip = vconf_get_str(VCONFKEY_NETWORK_IP);
445 char *old_proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY);
447 if (netconfig_is_wifi_profile(profile) == TRUE) {
448 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_WIFI);
449 netconfig_set_vconf_int("memory/private/wifi/frequency", freq);
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 /* Enable clatd if IPv6 is set and no IPv4 address */
461 if (!ip_addr && ip_addr6 )
462 netconfig_clatd_enable();
464 else if (netconfig_is_ethernet_profile(profile) == TRUE){
465 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_ETHERNET);
467 else if (netconfig_is_bluetooth_profile(profile) == TRUE){
468 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_BLUETOOTH);
471 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
474 if (g_strcmp0(old_ip, ip_addr) != 0 || old_ip == NULL) {
476 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr);
477 else if (ip_addr6 != NULL)
478 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr6);
480 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
484 if (g_strcmp0(old_proxy, proxy_addr) != 0) {
485 if (proxy_addr == NULL)
486 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
488 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, proxy_addr);
492 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 1);
494 DBG("Successfully update default network configuration");
496 /* Disable clatd if it is in running state */
497 if (netconfig_is_cellular_profile(profile) != TRUE)
498 netconfig_clatd_disable();
501 __netconfig_adjust_tcp_buffer_size();
504 static gboolean __netconfig_is_tech_state_connected(void)
506 gboolean ret = FALSE;
507 GVariant *message = NULL, *variant;
508 GVariantIter *iter, *next;
512 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
513 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
514 "GetTechnologies", NULL);
516 if (message == NULL) {
517 DBG("Fail to get technology state");
521 g_variant_get(message, "(a(oa{sv}))", &iter);
522 while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
527 while (g_variant_iter_loop(next, "{sv}", &key, &variant)) {
529 if (g_strcmp0(key, "Connected") == 0) {
530 data = g_variant_get_boolean(variant);
531 DBG("%s [%s: %s]", path, key, data ? "True" : "False");
536 g_variant_unref(variant);
537 g_variant_iter_free(next);
545 g_variant_iter_free(iter);
546 g_variant_unref(message);
551 static void __netconfig_update_if_service_connected(void)
553 GVariant *message = NULL, *var;
554 GVariantIter *iter, *next;
558 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
559 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
560 "GetServices", NULL);
562 if (message == NULL) {
563 ERR("Failed to get services");
567 g_variant_get(message, "(a(oa{sv}))", &iter);
568 while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
573 if (g_str_has_prefix(path,
574 CONNMAN_WIFI_SERVICE_PROFILE_PREFIX) == TRUE) {
575 if (g_strrstr(path + strlen(CONNMAN_WIFI_SERVICE_PROFILE_PREFIX),
577 /* skip hidden profiles */
581 } else if (g_str_has_prefix(path,
582 CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX) == TRUE) {
588 while (g_variant_iter_loop(next, "{sv}", &key, &var)) {
589 if (g_strcmp0(key, "State") == 0) {
590 const gchar *sdata = NULL;
591 sdata = g_variant_get_string(var, NULL);
592 DBG("%s [%s: %s]", path, key, sdata);
594 if (g_strcmp0(sdata, "online") == 0 || g_strcmp0(sdata, "ready") == 0) {
596 /* Found a connected WiFi / 3G service.
597 * Lets update the default profile info.
599 netconfig_update_default_profile((const gchar*)path);
602 g_variant_unref(var);
603 g_variant_iter_free(next);
610 g_variant_iter_free(iter);
611 g_variant_unref(message);
616 static void __netconfig_network_notify_result(const char *sig_name, const char *key)
619 GVariantBuilder *builder = NULL;
620 GDBusConnection *connection = NULL;
621 GError *error = NULL;
622 const char *prop_key = "key";
624 INFO("[Signal] %s %s", sig_name, key);
626 connection = netdbus_get_connection();
627 if (connection == NULL) {
628 ERR("Failed to get GDBus Connection");
632 builder = g_variant_builder_new(G_VARIANT_TYPE ("a{sv}"));
633 g_variant_builder_add(builder, "{sv}", prop_key, g_variant_new("(s)", key));
635 reply = g_dbus_connection_emit_signal(connection,
637 NETCONFIG_NETWORK_PATH,
638 NETCONFIG_NETWORK_INTERFACE,
640 g_variant_builder_end(builder),
644 g_variant_builder_unref(builder);
648 ERR("Failed to send signal [%s]", error->message);
654 INFO("Sent signal (%s), key (%s)", sig_name, key);
658 const char *netconfig_get_default_profile(void)
660 return netconfig_default_connection_info.profile;
663 const char *netconfig_get_default_ifname(void)
665 return netconfig_default_connection_info.ifname;
668 const char *netconfig_get_default_ipaddress(void)
670 return netconfig_default_connection_info.ipaddress;
673 const char *netconfig_get_default_ipaddress6(void)
675 return netconfig_default_connection_info.ipaddress6;
678 const char *netconfig_get_default_proxy(void)
680 return netconfig_default_connection_info.proxy;
683 unsigned int netconfig_get_default_frequency(void)
685 return netconfig_default_connection_info.freq;
688 const char *netconfig_wifi_get_connected_essid(const char *default_profile)
690 if (default_profile == NULL)
693 if (netconfig_is_wifi_profile(default_profile) != TRUE)
696 if (g_strcmp0(default_profile, netconfig_default_connection_info.profile) != 0)
699 return netconfig_default_connection_info.essid;
702 static int __netconfig_reset_ipv4_socket(void)
707 struct sockaddr_in sai;
708 const char *ipaddr = netconfig_get_default_ipaddress();
709 DBG("ipaddr-[%s]", ipaddr);
714 fd = socket(AF_INET, SOCK_DGRAM, 0);
718 memset(&sai, 0, sizeof(struct sockaddr_in));
719 sai.sin_family = AF_INET;
721 if (!inet_aton(ipaddr, &sai.sin_addr)) {
722 DBG("fail to inet_aton()");
727 memset(&ifr, 0, sizeof(struct ifreq));
728 memcpy(&ifr.ifr_addr, &sai, sizeof(sai));
729 g_strlcpy((char *)ifr.ifr_name, WIFI_IFNAME, IFNAMSIZ);
732 #define SIOCKILLADDR 0x8939
735 ret = ioctl(fd, SIOCKILLADDR, &ifr);
737 DBG("fail to ioctl[SIOCKILLADDR]");
746 void netconfig_update_default_profile(const char *profile)
748 static char *old_profile = NULL;
750 /* It's automatically updated by signal-handler
751 * DO NOT update manually
753 * It is going to update default connection information
756 if (netconfig_default_connection_info.profile != NULL) {
758 if (netconfig_is_wifi_profile(netconfig_default_connection_info.profile))
759 __netconfig_reset_ipv4_socket();
762 old_profile = strdup(netconfig_default_connection_info.profile);
764 g_free(netconfig_default_connection_info.profile);
765 netconfig_default_connection_info.profile = NULL;
767 g_free(netconfig_default_connection_info.ifname);
768 netconfig_default_connection_info.ifname = NULL;
770 g_free(netconfig_default_connection_info.ipaddress);
771 netconfig_default_connection_info.ipaddress = NULL;
773 g_free(netconfig_default_connection_info.ipaddress6);
774 netconfig_default_connection_info.ipaddress6 = NULL;
776 g_free(netconfig_default_connection_info.proxy);
777 netconfig_default_connection_info.proxy = NULL;
779 netconfig_default_connection_info.freq = 0;
781 if (wifi_state_get_service_state() != NETCONFIG_WIFI_CONNECTED) {
782 g_free(netconfig_default_connection_info.essid);
783 netconfig_default_connection_info.essid = NULL;
787 //default profile is NULL and new connected profile is NULL
789 profile = __netconfig_get_default_profile();
791 if (profile && netconfig_is_cellular_profile(profile) &&
792 !netconfig_is_cellular_internet_profile(profile)){
793 DBG("not a default cellular profile");
798 __netconfig_update_default_connection_info();
803 netconfig_default_connection_info.profile = g_strdup(profile);
804 __netconfig_get_default_connection_info(profile);
805 __netconfig_update_default_connection_info();
809 void netconfig_update_default(void)
811 if (__netconfig_is_tech_state_connected() == TRUE)
812 __netconfig_update_if_service_connected();
814 __netconfig_adjust_tcp_buffer_size();
817 char *netconfig_get_ifname(const char *profile)
819 GVariant *message = NULL, *variant;
820 GVariantIter *iter, *next;
823 const gchar *value = NULL;
824 gchar *ifname = NULL;
829 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE, profile,
830 CONNMAN_SERVICE_INTERFACE, "GetProperties", NULL);
831 if (message == NULL) {
832 ERR("Failed to get service properties");
836 g_variant_get(message, "(a{sv})", &iter);
837 while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
838 if (g_strcmp0(key, "Ethernet") == 0) {
839 while (g_variant_iter_loop(next, "{sv}", &key1, &variant)) {
840 if (g_strcmp0(key1, "Interface") == 0) {
841 value = g_variant_get_string(variant, NULL);
842 ifname = g_strdup(value);
848 g_variant_unref(message);
850 g_variant_iter_free(iter);
855 /* Check Ethernet Cable Plug-in /Plug-out Status */
856 void netconfig_network_notify_ethernet_cable_state(const char *key)
858 __netconfig_network_notify_result("EthernetCableState", key);
861 static gboolean handle_add_route(
863 GDBusMethodInvocation *context,
866 gchar *interface, gchar *gateway, gint address_family)
868 const gchar *path = ROUTE_EXEC_PATH;
869 gchar *const args[] = { "/sbin/route", "add", "-net", ip_addr,
870 "netmask", netmask, "dev", interface, NULL };
871 gchar *const envs[] = { NULL };
872 const gchar* buf = NULL;
877 DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
879 switch(address_family) {
881 if (ip_addr == NULL || netmask == NULL || interface == NULL) {
882 ERR("Invalid parameter");
883 netconfig_error_invalid_parameter(context);
887 if (netconfig_execute_file(path, args, envs) < 0) {
888 DBG("Failed to add a new route");
889 netconfig_error_permission_denied(context);
895 if (ip_addr == NULL || interface == NULL || gateway == NULL) {
896 ERR("Invalid parameter");
897 netconfig_error_invalid_parameter(context);
902 ch = strchr(buf, '/');
905 prefix_len = atoi(ch + 1);
906 ip_addr[pos-1] = '\0';
911 if (netconfig_add_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
912 DBG("Failed to add a new route");
913 netconfig_error_permission_denied(context);
918 DBG("Unknown Address Family");
919 netconfig_error_invalid_parameter(context);
923 DBG("Successfully added a new route");
924 network_complete_add_route(object, context, TRUE);
928 static gboolean handle_remove_route(
930 GDBusMethodInvocation *context,
933 gchar *interface, gchar *gateway, gint address_family)
935 const char *path = ROUTE_EXEC_PATH;
936 gchar *const args[] = { "/sbin/route", "del", "-net", ip_addr,
937 "netmask", netmask, "dev", interface, NULL };
938 char *const envs[] = { NULL };
939 const char* buf = NULL;
944 DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
946 switch(address_family) {
948 if (ip_addr == NULL || netmask == NULL || interface == NULL) {
949 DBG("Invalid parameter!");
950 netconfig_error_invalid_parameter(context);
953 if (netconfig_execute_file(path, args, envs) < 0) {
954 DBG("Failed to remove the route");
955 netconfig_error_permission_denied(context);
960 if (ip_addr == NULL || interface == NULL || gateway == NULL) {
961 DBG("Invalid parameter!");
962 netconfig_error_invalid_parameter(context);
967 ch = strchr(buf, '/');
970 prefix_len = atoi(ch + 1);
971 ip_addr[pos-1] = '\0';
976 if (netconfig_del_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
977 DBG("Failed to remove the route");
978 netconfig_error_permission_denied(context);
983 DBG("Unknown Address Family");
984 netconfig_error_invalid_parameter(context);
988 DBG("Successfully removed the route");
989 network_complete_remove_route(object, context, TRUE);
993 static gboolean handle_check_get_privilege(Network *object,
994 GDBusMethodInvocation *context)
996 network_complete_check_get_privilege(object, context);
1001 static gboolean handle_check_profile_privilege(Network *object,
1002 GDBusMethodInvocation *context)
1004 network_complete_check_profile_privilege(object, context);
1008 gboolean handle_ethernet_cable_state(Network *object,
1009 GDBusMethodInvocation *context)
1014 ret = netconfig_get_ethernet_cable_state(&state);
1016 DBG("Failed to get ethernet cable state");
1017 netconfig_error_fail_ethernet_cable_state(context);
1021 DBG("Successfully get ethernet cable state[%d]", state);
1022 network_complete_ethernet_cable_state(object, context, state);
1026 void state_object_create_and_init(void)
1028 DBG("Creating network state object");
1029 GDBusInterfaceSkeleton *interface_network = NULL;
1030 GDBusConnection *connection = NULL;
1031 GDBusObjectManagerServer *server = netdbus_get_state_manager();
1035 connection = netdbus_get_connection();
1036 g_dbus_object_manager_server_set_connection(server, connection);
1038 /*Interface netconfig.network*/
1039 netconfigstate = network_skeleton_new();
1041 interface_network = G_DBUS_INTERFACE_SKELETON(netconfigstate);
1042 g_signal_connect(netconfigstate, "handle-add-route",
1043 G_CALLBACK(handle_add_route), NULL);
1044 g_signal_connect(netconfigstate, "handle-check-get-privilege",
1045 G_CALLBACK(handle_check_get_privilege), NULL);
1046 g_signal_connect(netconfigstate, "handle-check-profile-privilege",
1047 G_CALLBACK(handle_check_profile_privilege), NULL);
1048 g_signal_connect(netconfigstate, "handle-remove-route",
1049 G_CALLBACK(handle_remove_route), NULL);
1051 if (!g_dbus_interface_skeleton_export(interface_network, connection,
1052 NETCONFIG_NETWORK_STATE_PATH, NULL)) {
1053 ERR("Export with path failed");
1057 void state_object_deinit(void)
1059 g_object_unref(netconfigstate);