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>
37 #include "wifi-state.h"
38 #include "wifi-power.h"
39 #include "network-state.h"
40 #include "network-monitor.h"
41 #include "netsupplicant.h"
42 #include "wifi-tel-intf.h"
43 #include "clatd-handler.h"
45 #include "generated-code.h"
46 /* Define TCP buffer sizes for various networks */
47 /* ReadMin, ReadInitial, ReadMax */ /* WriteMin, WriteInitial, WriteMax */
48 #define NET_TCP_BUFFERSIZE_DEFAULT_READ "4096 87380 704512"
49 #define NET_TCP_BUFFERSIZE_DEFAULT_WRITE "4096 16384 110208"
50 #define NET_TCP_BUFFERSIZE_WIFI_READ "524288 1048576 2560000"
51 #define NET_TCP_BUFFERSIZE_WIFI_WRITE "524288 1048576 2560000"
52 #define NET_TCP_BUFFERSIZE_LTE_READ "524288 1048576 2560000"
53 #define NET_TCP_BUFFERSIZE_LTE_WRITE "524288 1048576 2560000"
54 #define NET_TCP_BUFFERSIZE_UMTS_READ "4094 87380 704512"
55 #define NET_TCP_BUFFERSIZE_UMTS_WRITE "4096 16384 110208"
56 #define NET_TCP_BUFFERSIZE_HSPA_READ "4092 87380 704512"
57 #define NET_TCP_BUFFERSIZE_HSPA_WRITE "4096 16384 262144"
58 #define NET_TCP_BUFFERSIZE_HSDPA_READ "4092 87380 704512"
59 #define NET_TCP_BUFFERSIZE_HSDPA_WRITE "4096 16384 262144"
60 #define NET_TCP_BUFFERSIZE_HSUPA_READ "4092 87380 704512"
61 #define NET_TCP_BUFFERSIZE_HSUPA_WRITE "4096 16384 262144"
62 #define NET_TCP_BUFFERSIZE_HSPAP_READ "4092 87380 1220608"
63 #define NET_TCP_BUFFERSIZE_HSPAP_WRITE "4096 16384 1220608"
64 #define NET_TCP_BUFFERSIZE_EDGE_READ "4093 26280 35040"
65 #define NET_TCP_BUFFERSIZE_EDGE_WRITE "4096 16384 35040"
66 #define NET_TCP_BUFFERSIZE_GPRS_READ "4096 30000 30000"
67 #define NET_TCP_BUFFERSIZE_GPRS_WRITE "4096 8760 11680"
69 #define NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX "1048576"
70 #define NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX "2097152"
71 #define NET_TCP_BUFFERSIZE_LTE_RMEM_MAX "5242880"
73 #define NET_TCP_BUFFERSIZE_WIFID_WMEM_MAX "2097152"
75 #define NET_PROC_SYS_NET_IPV4_TCP_RMEM "/proc/sys/net/ipv4/tcp_rmem"
76 #define NET_PROC_SYS_NET_IPv4_TCP_WMEM "/proc/sys/net/ipv4/tcp_wmem"
77 #define NET_PROC_SYS_NET_CORE_RMEM_MAX "/proc/sys/net/core/rmem_max"
78 #define NET_PROC_SYS_NET_CORE_WMEM_MAX "/proc/sys/net/core/wmem_max"
80 #define ROUTE_EXEC_PATH "/sbin/route"
82 static Network *netconfigstate = NULL;
84 struct netconfig_default_connection {
94 static struct netconfig_default_connection
95 netconfig_default_connection_info = { NULL, };
97 gboolean netconfig_iface_network_state_ethernet_cable_state(gint32 *state);
99 static gboolean __netconfig_is_connected(GVariantIter *array)
101 gboolean is_connected = FALSE;
102 GVariant *variant = NULL;
104 const gchar *value = NULL;
106 while (g_variant_iter_loop(array, "{sv}", &key, &variant)) {
107 if (g_strcmp0(key, "State") != 0) {
111 if (g_variant_is_of_type(variant, G_VARIANT_TYPE_STRING)) {
112 value = g_variant_get_string(variant, NULL);
113 if (g_strcmp0(value, "ready") == 0 || g_strcmp0(value, "online") == 0)
118 g_variant_unref(variant);
125 static char *__netconfig_get_default_profile(void)
127 GVariant *message = NULL;
130 gchar *default_profile = NULL;
133 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
134 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
135 "GetServices", NULL);
136 if (message == NULL) {
137 ERR("Failed to get profiles");
141 g_variant_get(message, "(a(oa{sv}))", &iter);
142 while (g_variant_iter_loop(iter, "(oa{sv})", &object_path, &next)) {
143 if (object_path == NULL) {
147 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();
442 else if (profile != NULL) {
443 char *old_ip = vconf_get_str(VCONFKEY_NETWORK_IP);
444 char *old_proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY);
446 if (netconfig_is_wifi_profile(profile) == TRUE) {
447 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_WIFI);
448 netconfig_set_vconf_int("memory/private/wifi/frequency", freq);
450 else if (netconfig_is_cellular_profile(profile) ){
452 if( !netconfig_is_cellular_internet_profile(profile)){
453 DBG("connection is not a internet profile - stop to update the cellular state");
457 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_CELLULAR);
459 /* Enable clatd if IPv6 is set and no IPv4 address */
460 if (!ip_addr && ip_addr6 )
461 netconfig_clatd_enable();
463 else if (netconfig_is_ethernet_profile(profile) == TRUE){
464 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_ETHERNET);
466 else if (netconfig_is_bluetooth_profile(profile) == TRUE){
467 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_BLUETOOTH);
470 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
473 if (g_strcmp0(old_ip, ip_addr) != 0 || old_ip == NULL) {
475 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr);
476 else if (ip_addr6 != NULL)
477 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr6);
479 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
483 if (g_strcmp0(old_proxy, proxy_addr) != 0) {
484 if (proxy_addr == NULL)
485 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
487 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, proxy_addr);
491 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 1);
493 DBG("Successfully update default network configuration");
495 /* Disable clatd if it is in running state */
496 if (netconfig_is_cellular_profile(profile) != TRUE)
497 netconfig_clatd_disable();
500 __netconfig_adjust_tcp_buffer_size();
503 static gboolean __netconfig_is_tech_state_connected(void)
505 gboolean ret = FALSE;
506 GVariant *message = NULL, *variant;
507 GVariantIter *iter, *next;
511 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
512 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
513 "GetTechnologies", NULL);
515 if (message == NULL) {
516 DBG("Fail to get technology state");
520 g_variant_get(message, "(a(oa{sv}))", &iter);
521 while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
526 while (g_variant_iter_loop(next, "{sv}", &key, &variant)) {
528 if (g_strcmp0(key, "Connected") == 0) {
529 data = g_variant_get_boolean(variant);
530 DBG("%s [%s: %s]", path, key, data ? "True" : "False");
535 g_variant_unref(variant);
536 g_variant_iter_free(next);
544 g_variant_iter_free(iter);
545 g_variant_unref(message);
550 static void __netconfig_update_if_service_connected(void)
552 GVariant *message = NULL, *var;
553 GVariantIter *iter, *next;
557 message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
558 CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
559 "GetServices", NULL);
561 if (message == NULL) {
562 ERR("Failed to get services");
566 g_variant_get(message, "(a(oa{sv}))", &iter);
567 while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
572 if (g_str_has_prefix(path,
573 CONNMAN_WIFI_SERVICE_PROFILE_PREFIX) == TRUE) {
574 if (g_strrstr(path + strlen(CONNMAN_WIFI_SERVICE_PROFILE_PREFIX),
576 /* skip hidden profiles */
580 } else if (g_str_has_prefix(path,
581 CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX) == TRUE) {
587 while (g_variant_iter_loop(next, "{sv}", &key, &var)) {
588 if (g_strcmp0(key, "State") == 0) {
589 const gchar *sdata = NULL;
590 sdata = g_variant_get_string(var, NULL);
591 DBG("%s [%s: %s]", path, key, sdata);
593 if (g_strcmp0(sdata, "online") == 0 || g_strcmp0(sdata, "ready") == 0) {
595 /* Found a connected WiFi / 3G service.
596 * Lets update the default profile info.
598 netconfig_update_default_profile((const gchar*)path);
601 g_variant_unref(var);
602 g_variant_iter_free(next);
609 g_variant_iter_free(iter);
610 g_variant_unref(message);
615 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_string(key));
634 params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
636 g_variant_builder_unref(builder);
638 reply = g_dbus_connection_emit_signal(connection,
640 NETCONFIG_NETWORK_PATH,
641 NETCONFIG_NETWORK_INTERFACE,
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);