54e519297d0a3b61bb0afd3d8d1a6447d8e722f1
[platform/core/connectivity/net-config.git] / src / network-state.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #include <vconf.h>
21 #include <vconf-keys.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <net/if.h>
26 #include <arpa/inet.h>
27 #include <netinet/in.h>
28 #include <sys/ioctl.h>
29 #include <ITapiSim.h>
30 #include <TapiUtility.h>
31 #include <bundle.h>
32 #include <bundle_internal.h>
33 #include <eventsystem.h>
34
35 #include "log.h"
36 #include "util.h"
37 #include "netdbus.h"
38 #include "neterror.h"
39 #include "emulator.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"
47 #include "clatd-handler.h"
48
49 #include "generated-code.h"
50 /* Define TCP buffer sizes for various networks */
51 /* ReadMin, ReadInitial, ReadMax */ /* WriteMin, WriteInitial, WriteMax */
52 #define NET_TCP_BUFFERSIZE_DEFAULT_READ         "4096 87380 704512"
53 #define NET_TCP_BUFFERSIZE_DEFAULT_WRITE        "4096 16384 110208"
54 #define NET_TCP_BUFFERSIZE_WIFI_READ            "524288 1048576 2560000"
55 #define NET_TCP_BUFFERSIZE_WIFI_WRITE           "524288 1048576 2560000"
56 #define NET_TCP_BUFFERSIZE_LTE_READ             "524288 1048576 2560000"
57 #define NET_TCP_BUFFERSIZE_LTE_WRITE            "524288 1048576 2560000"
58 #define NET_TCP_BUFFERSIZE_UMTS_READ            "4094 87380 704512"
59 #define NET_TCP_BUFFERSIZE_UMTS_WRITE           "4096 16384 110208"
60 #define NET_TCP_BUFFERSIZE_HSPA_READ            "4092 87380 704512"
61 #define NET_TCP_BUFFERSIZE_HSPA_WRITE           "4096 16384 262144"
62 #define NET_TCP_BUFFERSIZE_HSDPA_READ           "4092 87380 704512"
63 #define NET_TCP_BUFFERSIZE_HSDPA_WRITE          "4096 16384 262144"
64 #define NET_TCP_BUFFERSIZE_HSUPA_READ           "4092 87380 704512"
65 #define NET_TCP_BUFFERSIZE_HSUPA_WRITE          "4096 16384 262144"
66 #define NET_TCP_BUFFERSIZE_HSPAP_READ           "4092 87380 1220608"
67 #define NET_TCP_BUFFERSIZE_HSPAP_WRITE          "4096 16384 1220608"
68 #define NET_TCP_BUFFERSIZE_EDGE_READ            "4093 26280 35040"
69 #define NET_TCP_BUFFERSIZE_EDGE_WRITE           "4096 16384 35040"
70 #define NET_TCP_BUFFERSIZE_GPRS_READ            "4096 30000 30000"
71 #define NET_TCP_BUFFERSIZE_GPRS_WRITE           "4096 8760 11680"
72
73 #define NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX        "1048576"
74 #define NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX        "2097152"
75 #define NET_TCP_BUFFERSIZE_LTE_RMEM_MAX         "5242880"
76
77 #define NET_TCP_BUFFERSIZE_WIFID_WMEM_MAX       "2097152"
78
79 #define NET_PROC_SYS_NET_IPV4_TCP_RMEM          "/proc/sys/net/ipv4/tcp_rmem"
80 #define NET_PROC_SYS_NET_IPv4_TCP_WMEM          "/proc/sys/net/ipv4/tcp_wmem"
81 #define NET_PROC_SYS_NET_CORE_RMEM_MAX          "/proc/sys/net/core/rmem_max"
82 #define NET_PROC_SYS_NET_CORE_WMEM_MAX          "/proc/sys/net/core/wmem_max"
83
84 #define ROUTE_EXEC_PATH                                         "/sbin/route"
85
86 static Network *netconfigstate = NULL;
87
88 struct netconfig_default_connection {
89         char *profile;
90         char *ifname;
91         char *ipaddress;
92         char *ipaddress6;
93         char *proxy;
94         char *essid;
95         unsigned int freq;
96 };
97
98 static struct netconfig_default_connection
99                                 netconfig_default_connection_info = { NULL, };
100
101 gboolean netconfig_iface_network_state_ethernet_cable_state(gint32 *state);
102
103 static gboolean __netconfig_is_connected(GVariantIter *array)
104 {
105         gboolean is_connected = FALSE;
106         GVariant *variant = NULL;
107         gchar *key = NULL;
108         const gchar *value = NULL;
109
110         while (g_variant_iter_loop(array, "{sv}", &key, &variant)) {
111                 if (g_strcmp0(key, "State") != 0)
112                         continue;
113
114                 if (g_variant_is_of_type(variant, G_VARIANT_TYPE_STRING)) {
115                         value = g_variant_get_string(variant, NULL);
116                         if (g_strcmp0(value, "ready") == 0 || g_strcmp0(value, "online") == 0)
117                                 is_connected = TRUE;
118                 }
119
120                 g_free(key);
121                 g_variant_unref(variant);
122                 break;
123         }
124
125         return is_connected;
126 }
127
128 static char *__netconfig_get_default_profile(void)
129 {
130         GVariant *message = NULL;
131         GVariantIter *iter;
132         GVariantIter *next;
133         gchar *default_profile = NULL;
134         gchar *object_path;
135
136         message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
137                         CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
138                         "GetServices", NULL);
139         if (message == NULL) {
140                 ERR("Failed to get profiles");
141                 return NULL;
142         }
143
144         g_variant_get(message, "(a(oa{sv}))", &iter);
145         while (g_variant_iter_loop(iter, "(oa{sv})", &object_path, &next)) {
146                 if (object_path == NULL)
147                         continue;
148
149                 if (netconfig_is_cellular_profile(object_path) && !netconfig_is_cellular_internet_profile(object_path))
150                         continue;
151
152                 if (__netconfig_is_connected(next) == TRUE) {
153                         default_profile = g_strdup(object_path);
154                         g_free(object_path);
155                         g_variant_iter_free(next);
156                         break;
157                 }
158         }
159         g_variant_iter_free(iter);
160         g_variant_unref(message);
161
162         return default_profile;
163 }
164
165 static void __netconfig_get_default_connection_info(const char *profile)
166 {
167         GVariant *message = NULL, *variant = NULL, *variant2 = NULL;
168         GVariantIter *iter = NULL, *iter1 = NULL,  *service = NULL;
169         GVariant *next = NULL;
170         gchar *obj_path;
171         gchar *key = NULL;
172         gchar *key1 = NULL;
173         gchar *key2 = NULL;
174         gboolean found_profile = 0;
175
176         message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
177                         CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
178                         "GetServices", NULL);
179         if (message == NULL) {
180                 ERR("Failed to get services informations");
181                 goto done;
182         }
183
184         g_variant_get(message, "(a(oa{sv}))", &service);
185         if (service == NULL) {
186                 ERR("Failed to get services iter");
187                 goto done;
188         }
189
190         while (g_variant_iter_loop(service, "(oa{sv})", &obj_path, &iter)) {
191                 if (g_strcmp0(obj_path, profile) == 0) {
192                         g_free(obj_path);
193                         found_profile = 1;
194                         break;
195                 }
196         }
197
198         if (iter == NULL || found_profile == 0) {
199                 ERR("Profile %s doesn't exist", profile);
200                 goto done;
201         }
202
203         while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
204                 const gchar *value = NULL;
205                 guint16 freq = 0;
206                 if (g_strcmp0(key, "Name") == 0 &&
207                                 netconfig_is_wifi_profile(profile) == TRUE) {
208                         if (g_variant_is_of_type(next, G_VARIANT_TYPE_STRING)) {
209                                 value = g_variant_get_string(next, NULL);
210
211                                 netconfig_default_connection_info.essid = g_strdup(value);
212                         }
213                 } else if (g_strcmp0(key, "Ethernet") == 0) {
214                         g_variant_get(next, "a{sv}", &iter1);
215                         if (iter1 == NULL)
216                                 continue;
217                         while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
218                                 if (g_strcmp0(key1, "Interface") == 0) {
219                                         value = g_variant_get_string(variant, NULL);
220                                         netconfig_default_connection_info.ifname = g_strdup(value);
221                                 }
222                         }
223                         g_variant_iter_free(iter1);
224                 } else if (g_strcmp0(key, "IPv4") == 0) {
225                         g_variant_get(next, "a{sv}", &iter1);
226                         if (iter1 == NULL)
227                                 continue;
228                         while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
229                                 if (g_strcmp0(key1, "Address") == 0) {
230                                         value = g_variant_get_string(variant, NULL);
231                                         netconfig_default_connection_info.ipaddress = g_strdup(value);
232                                 }
233                         }
234                         g_variant_iter_free(iter1);
235                 } else if (g_strcmp0(key, "IPv6") == 0) {
236                         g_variant_get(next, "a{sv}", &iter1);
237                         if (iter1 == NULL)
238                                 continue;
239                         while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
240                                 if (g_strcmp0(key1, "Address") == 0) {
241                                         value = g_variant_get_string(variant, NULL);
242                                         netconfig_default_connection_info.ipaddress6 = g_strdup(value);
243                                 }
244                         }
245                         g_variant_iter_free(iter1);
246
247                 } else if (g_strcmp0(key, "Proxy") == 0) {
248                         g_variant_get(next, "a{sv}", &iter1);
249                         if (iter1 == NULL)
250                                 continue;
251                         while (g_variant_iter_loop(iter1, "{sv}", &key2, &variant2)) {
252                                 GVariantIter *iter_sub = NULL;
253
254                                 if (g_strcmp0(key2, "Servers") == 0) {
255                                         if (!g_variant_is_of_type(variant2, G_VARIANT_TYPE_STRING_ARRAY)) {
256                                                 g_free(key2);
257                                                 g_variant_unref(variant2);
258                                                 break;
259                                         }
260
261                                         g_variant_get(variant2, "as", &iter_sub);
262                                         g_variant_iter_loop(iter_sub, "s", &value);
263                                         g_variant_iter_free(iter_sub);
264                                         if (value != NULL && (strlen(value) > 0))
265                                                 netconfig_default_connection_info.proxy = g_strdup(value);
266                                 } else if (g_strcmp0(key2, "Method") == 0) {
267                                         if (g_variant_is_of_type(variant2, G_VARIANT_TYPE_STRING)) {
268                                                 g_free(key2);
269                                                 g_variant_unref(variant2);
270                                                 break;
271                                         }
272
273                                         value = g_variant_get_string(variant2, NULL);
274                                         if (g_strcmp0(value, "direct") == 0) {
275                                                 g_free(netconfig_default_connection_info.proxy);
276                                                 netconfig_default_connection_info.proxy = NULL;
277
278                                                 g_free(key2);
279                                                 g_variant_unref(variant2);
280                                                 break;
281                                         }
282                                 }
283                         }
284                         g_variant_iter_free(iter1);
285                 } else if (g_strcmp0(key, "Frequency") == 0) {
286                         if (g_variant_is_of_type(next, G_VARIANT_TYPE_UINT16)) {
287                                 freq = g_variant_get_uint16(next);
288                                 netconfig_default_connection_info.freq = freq;
289                         }
290                 }
291         }
292
293 done:
294         if (message)
295                 g_variant_unref(message);
296
297         if (iter)
298                 g_variant_iter_free(iter);
299
300         if (service)
301                 g_variant_iter_free(service);
302
303         return;
304 }
305
306 static void __netconfig_adjust_tcp_buffer_size(void)
307 {
308         int fdr = 0, fdw = 0;
309         int fdrmax = 0, fdwmax = 0;
310         const char *rbuf_size = NULL;
311         const char *wbuf_size = NULL;
312         const char *rmax_size = NULL;
313         const char *wmax_size = NULL;
314         const char *profile = netconfig_get_default_profile();
315
316         if (profile == NULL) {
317                 DBG("There is no default connection");
318
319                 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
320                 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
321         } else if (netconfig_is_wifi_profile(profile) == TRUE) {
322                 DBG("Default connection: Wi-Fi");
323
324                 rbuf_size = NET_TCP_BUFFERSIZE_WIFI_READ;
325                 wbuf_size = NET_TCP_BUFFERSIZE_WIFI_WRITE;
326                 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
327                 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
328         } else if (netconfig_is_cellular_profile(profile) == TRUE) {
329                 TapiHandle *tapi_handle = NULL;
330                 int telephony_svctype = 0, telephony_pstype = 0;
331
332                 tapi_handle = (TapiHandle *)netconfig_tel_init();
333                 if (NULL != tapi_handle) {
334                         tel_get_property_int(tapi_handle,
335                                         TAPI_PROP_NETWORK_SERVICE_TYPE,
336                                         &telephony_svctype);
337                         tel_get_property_int(tapi_handle, TAPI_PROP_NETWORK_PS_TYPE,
338                                         &telephony_pstype);
339                         netconfig_tel_deinit();
340                 }
341
342                 DBG("Default cellular %d, %d", telephony_svctype, telephony_pstype);
343
344                 switch (telephony_pstype) {
345                 case VCONFKEY_TELEPHONY_PSTYPE_HSPA:
346                         rbuf_size = NET_TCP_BUFFERSIZE_HSPA_READ;
347                         wbuf_size = NET_TCP_BUFFERSIZE_HSPA_WRITE;
348                         break;
349                 case VCONFKEY_TELEPHONY_PSTYPE_HSUPA:
350                         rbuf_size = NET_TCP_BUFFERSIZE_HSUPA_READ;
351                         wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
352                         break;
353                 case VCONFKEY_TELEPHONY_PSTYPE_HSDPA:
354                         rbuf_size = NET_TCP_BUFFERSIZE_HSDPA_READ;
355                         wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
356                         break;
357 #if !defined TIZEN_WEARABLE
358                 case VCONFKEY_TELEPHONY_PSTYPE_HSPAP:
359                         rbuf_size = NET_TCP_BUFFERSIZE_HSPAP_READ;
360                         wbuf_size = NET_TCP_BUFFERSIZE_HSPAP_WRITE;
361                         break;
362 #endif
363                 default:
364                         switch (telephony_svctype) {
365                         case VCONFKEY_TELEPHONY_SVCTYPE_LTE:
366                                 rbuf_size = NET_TCP_BUFFERSIZE_LTE_READ;
367                                 wbuf_size = NET_TCP_BUFFERSIZE_LTE_WRITE;
368                                 rmax_size = NET_TCP_BUFFERSIZE_LTE_RMEM_MAX;
369                                 break;
370                         case VCONFKEY_TELEPHONY_SVCTYPE_3G:
371                                 rbuf_size = NET_TCP_BUFFERSIZE_UMTS_READ;
372                                 wbuf_size = NET_TCP_BUFFERSIZE_UMTS_WRITE;
373                                 break;
374                         case VCONFKEY_TELEPHONY_SVCTYPE_2_5G_EDGE:
375                                 rbuf_size = NET_TCP_BUFFERSIZE_EDGE_READ;
376                                 wbuf_size = NET_TCP_BUFFERSIZE_EDGE_WRITE;
377                                 break;
378                         case VCONFKEY_TELEPHONY_SVCTYPE_2_5G:
379                                 rbuf_size = NET_TCP_BUFFERSIZE_GPRS_READ;
380                                 wbuf_size = NET_TCP_BUFFERSIZE_GPRS_WRITE;
381                                 break;
382                         default:
383                                 /* TODO: Check LTE support */
384                                 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
385                                 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
386                                 break;
387                         }
388                         break;
389                 }
390         } else {
391                 DBG("Default TCP buffer configured");
392
393                 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
394                 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
395         }
396
397         if (rbuf_size != NULL) {
398                 fdr = open(NET_PROC_SYS_NET_IPV4_TCP_RMEM, O_RDWR | O_CLOEXEC);
399
400                 if (fdr < 0 || write(fdr, rbuf_size, strlen(rbuf_size)) < 0)
401                         ERR("Failed to set TCP read buffer size");
402
403                 if (fdr >= 0)
404                         close(fdr);
405         }
406
407         if (wbuf_size != NULL) {
408                 fdw = open(NET_PROC_SYS_NET_IPv4_TCP_WMEM, O_RDWR | O_CLOEXEC);
409
410                 if (fdw < 0 || write(fdw, wbuf_size, strlen(wbuf_size)) < 0)
411                         ERR("Failed to set TCP write buffer size");
412
413                 if (fdw >= 0)
414                         close(fdw);
415         }
416
417         /* As default */
418         if (rmax_size == NULL)
419                 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
420         if (wmax_size == NULL)
421                 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
422
423         if (rmax_size != NULL) {
424                 fdrmax = open(NET_PROC_SYS_NET_CORE_RMEM_MAX, O_RDWR | O_CLOEXEC);
425
426                 if (fdrmax < 0 || write(fdrmax, rmax_size, strlen(rmax_size)) < 0)
427                         ERR("Failed to set TCP rmem_max size");
428
429                 if (fdrmax >= 0)
430                         close(fdrmax);
431         }
432
433         if (wmax_size != NULL) {
434                 fdwmax = open(NET_PROC_SYS_NET_CORE_WMEM_MAX, O_RDWR | O_CLOEXEC);
435
436                 if (fdwmax < 0 || write(fdwmax, wmax_size, strlen(wmax_size)) < 0)
437                         ERR("Failed to set TCP wmem_max size");
438
439                 if (fdwmax >= 0)
440                         close(fdwmax);
441         }
442 }
443
444 static void __netconfig_update_default_connection_info(void)
445 {
446         int old_network_status = 0;
447         const char *profile = netconfig_get_default_profile();
448         const char *ip_addr = netconfig_get_default_ipaddress();
449         const char *ip_addr6 = netconfig_get_default_ipaddress6();
450         const char *proxy_addr = netconfig_get_default_proxy();
451         unsigned int freq = netconfig_get_default_frequency();
452
453         if (emulator_is_emulated() == TRUE)
454                 return;
455
456         if (profile == NULL)
457                 DBG("Reset network state configuration");
458         else
459                 DBG("%s: ip(%s) proxy(%s)", profile, ip_addr, proxy_addr);
460
461         netconfig_vconf_get_int(VCONFKEY_NETWORK_STATUS, &old_network_status);
462
463         if (profile == NULL && old_network_status != VCONFKEY_NETWORK_OFF) {
464                 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
465
466                 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
467                 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
468
469                 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 0);
470                 netconfig_set_vconf_int("memory/private/wifi/frequency", 0);
471
472                 DBG("Successfully clear IP and PROXY up");
473
474                 /* Disable clatd if it is in running state */
475                 netconfig_clatd_disable();
476         } else if (profile != NULL) {
477                 char *old_ip = vconf_get_str(VCONFKEY_NETWORK_IP);
478                 char *old_proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY);
479
480                 if (netconfig_is_wifi_profile(profile) == TRUE) {
481                         netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_WIFI);
482                         netconfig_set_vconf_int("memory/private/wifi/frequency", freq);
483
484                         netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
485                                 EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_WIFI);
486                 } else if (netconfig_is_cellular_profile(profile)) {
487
488                         if (!netconfig_is_cellular_internet_profile(profile)) {
489                                 DBG("connection is not a internet profile - stop to update the cellular state");
490                                 return;
491                         }
492
493                         netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_CELLULAR);
494
495                         netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
496                                 EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_CELLULAR);
497
498                         /* Enable clatd if IPv6 is set and no IPv4 address */
499                         if (!ip_addr && ip_addr6)
500                                 netconfig_clatd_enable();
501                 } else if (netconfig_is_ethernet_profile(profile) == TRUE) {
502                         netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_ETHERNET);
503                         netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
504                                 EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_ETHERNET);
505                 } else if (netconfig_is_bluetooth_profile(profile) == TRUE) {
506                         netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_BLUETOOTH);
507                         netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
508                                 EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_BT);
509                 } else{
510                         netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
511                         netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
512                                 EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_DISCONNECTED);
513                 }
514
515                 if (g_strcmp0(old_ip, ip_addr) != 0 || old_ip == NULL) {
516                         if (ip_addr != NULL)
517                                 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr);
518                         else if (ip_addr6 != NULL)
519                                 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr6);
520                         else
521                                 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
522                 }
523                 g_free(old_ip);
524
525                 if (g_strcmp0(old_proxy, proxy_addr) != 0) {
526                         if (proxy_addr == NULL)
527                                 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
528                         else
529                                 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, proxy_addr);
530                 }
531                 g_free(old_proxy);
532
533                 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 1);
534
535                 DBG("Successfully update default network configuration");
536
537                 /* Disable clatd if it is in running state */
538                 if (netconfig_is_cellular_profile(profile) != TRUE)
539                         netconfig_clatd_disable();
540         }
541
542         __netconfig_adjust_tcp_buffer_size();
543 }
544
545 static gboolean __netconfig_is_tech_state_connected(void)
546 {
547         gboolean ret = FALSE;
548         GVariant *message = NULL, *variant;
549         GVariantIter *iter, *next;
550         gchar *path;
551         gchar *key;
552
553         message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
554                         CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
555                         "GetTechnologies", NULL);
556
557         if (message == NULL) {
558                 DBG("Fail to get technology state");
559                 return FALSE;
560         }
561
562         g_variant_get(message, "(a(oa{sv}))", &iter);
563         while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
564                 if (path == NULL)
565                         continue;
566
567                 while (g_variant_iter_loop(next, "{sv}", &key, &variant)) {
568                         gboolean data;
569                         if (g_strcmp0(key, "Connected") == 0) {
570                                 data = g_variant_get_boolean(variant);
571                                 DBG("%s [%s: %s]", path, key, data ? "True" : "False");
572                                 if (TRUE == data) {
573                                         ret = TRUE;
574                                         g_free(path);
575                                         g_free(key);
576                                         g_variant_unref(variant);
577                                         g_variant_iter_free(next);
578                                         goto done;
579                                 }
580                         }
581                 }
582         }
583
584 done:
585         g_variant_iter_free(iter);
586         g_variant_unref(message);
587
588         return ret;
589 }
590
591 static void __netconfig_update_if_service_connected(void)
592 {
593         GVariant *message = NULL, *var;
594         GVariantIter *iter, *next;
595         gchar *path;
596         gchar *key;
597
598         message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
599                         CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
600                         "GetServices", NULL);
601
602         if (message == NULL) {
603                 ERR("Failed to get services");
604                 return;
605         }
606
607         g_variant_get(message, "(a(oa{sv}))", &iter);
608         while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
609                 if (path == NULL)
610                         continue;
611
612                 if (g_str_has_prefix(path,
613                                                 CONNMAN_WIFI_SERVICE_PROFILE_PREFIX) == TRUE) {
614                         if (g_strrstr(path + strlen(CONNMAN_WIFI_SERVICE_PROFILE_PREFIX),
615                                                         "hidden") != NULL) {
616                                 /* skip hidden profiles */
617                                 continue;
618                         }
619                         /* Process this */
620                 } else if (g_str_has_prefix(path,
621                                                 CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX) == TRUE) {
622                         /* Process this */
623                 } else {
624                         continue;
625                 }
626
627                 while (g_variant_iter_loop(next, "{sv}", &key, &var)) {
628                         if (g_strcmp0(key, "State") == 0) {
629                                 const gchar *sdata = NULL;
630                                 sdata = g_variant_get_string(var, NULL);
631                                 DBG("%s [%s: %s]", path, key, sdata);
632
633                                 if (g_strcmp0(sdata, "online") == 0 || g_strcmp0(sdata, "ready") == 0) {
634
635                                         /* Found a connected WiFi / 3G service.
636                                          * Lets update the default profile info.
637                                          */
638                                         netconfig_update_default_profile((const gchar*)path);
639                                         g_free(key);
640                                         g_free(path);
641                                         g_variant_unref(var);
642                                         g_variant_iter_free(next);
643                                         goto done;
644                                 }
645                         }
646                 }
647         }
648 done:
649         g_variant_iter_free(iter);
650         g_variant_unref(message);
651
652         return;
653 }
654
655 static void __netconfig_network_notify_result(const char *sig_name, const char *key)
656 {
657         GVariantBuilder *builder;
658         GVariant *params;
659
660         INFO("[Signal] %s %s", sig_name, key);
661
662         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
663         g_variant_builder_add(builder, "{sv}", "key", g_variant_new_string(key));
664
665         params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
666         g_variant_builder_unref(builder);
667
668         netconfig_dbus_emit_signal(NULL,
669                                 NETCONFIG_NETWORK_PATH,
670                                 NETCONFIG_NETWORK_INTERFACE,
671                                 sig_name,
672                                 params);
673
674         return;
675 }
676
677 const char *netconfig_get_default_profile(void)
678 {
679         return netconfig_default_connection_info.profile;
680 }
681
682 const char *netconfig_get_default_ifname(void)
683 {
684         return netconfig_default_connection_info.ifname;
685 }
686
687 const char *netconfig_get_default_ipaddress(void)
688 {
689         return netconfig_default_connection_info.ipaddress;
690 }
691
692 const char *netconfig_get_default_ipaddress6(void)
693 {
694         return netconfig_default_connection_info.ipaddress6;
695 }
696
697 const char *netconfig_get_default_proxy(void)
698 {
699         return netconfig_default_connection_info.proxy;
700 }
701
702 unsigned int netconfig_get_default_frequency(void)
703 {
704         return netconfig_default_connection_info.freq;
705 }
706
707 const char *netconfig_wifi_get_connected_essid(const char *default_profile)
708 {
709         if (default_profile == NULL)
710                 return NULL;
711
712         if (netconfig_is_wifi_profile(default_profile) != TRUE)
713                 return NULL;
714
715         if (g_strcmp0(default_profile, netconfig_default_connection_info.profile) != 0)
716                 return NULL;
717
718         return netconfig_default_connection_info.essid;
719 }
720
721 static int __netconfig_reset_ipv4_socket(void)
722 {
723         int ret;
724         int fd;
725         struct ifreq ifr;
726         struct sockaddr_in sai;
727         const char *ipaddr = netconfig_get_default_ipaddress();
728         DBG("ipaddr-[%s]", ipaddr);
729
730         if (!ipaddr)
731                 return -1;
732
733         fd = socket(AF_INET, SOCK_DGRAM, 0);
734         if (fd < 0)
735                 return -1;
736
737         memset(&sai, 0, sizeof(struct sockaddr_in));
738         sai.sin_family = AF_INET;
739         sai.sin_port = 0;
740         if (!inet_aton(ipaddr, &sai.sin_addr)) {
741                 DBG("fail to inet_aton()");
742                 close(fd);
743                 return -1;
744         }
745
746         memset(&ifr, 0, sizeof(struct ifreq));
747         memcpy(&ifr.ifr_addr, &sai, sizeof(sai));
748         g_strlcpy((char *)ifr.ifr_name, WIFI_IFNAME, IFNAMSIZ);
749
750 #ifndef SIOCKILLADDR
751 #define SIOCKILLADDR    0x8939
752 #endif
753
754         ret = ioctl(fd, SIOCKILLADDR, &ifr);
755         if (ret < 0) {
756                 DBG("fail to ioctl[SIOCKILLADDR]");
757                 close(fd);
758                 return -1;
759         }
760
761         close(fd);
762         return 0;
763 }
764
765 void netconfig_update_default_profile(const char *profile)
766 {
767         static char *old_profile = NULL;
768
769         /* It's automatically updated by signal-handler
770          * DO NOT update manually
771          *
772          * It is going to update default connection information
773          */
774
775         if (netconfig_default_connection_info.profile != NULL) {
776
777                 if (netconfig_is_wifi_profile(netconfig_default_connection_info.profile))
778                         __netconfig_reset_ipv4_socket();
779
780                 g_free(old_profile);
781                 old_profile = strdup(netconfig_default_connection_info.profile);
782
783                 g_free(netconfig_default_connection_info.profile);
784                 netconfig_default_connection_info.profile = NULL;
785
786                 g_free(netconfig_default_connection_info.ifname);
787                 netconfig_default_connection_info.ifname = NULL;
788
789                 g_free(netconfig_default_connection_info.ipaddress);
790                 netconfig_default_connection_info.ipaddress = NULL;
791
792                 g_free(netconfig_default_connection_info.ipaddress6);
793                 netconfig_default_connection_info.ipaddress6 = NULL;
794
795                 g_free(netconfig_default_connection_info.proxy);
796                 netconfig_default_connection_info.proxy = NULL;
797
798                 netconfig_default_connection_info.freq = 0;
799
800                 if (wifi_state_get_service_state() != NETCONFIG_WIFI_CONNECTED) {
801                         g_free(netconfig_default_connection_info.essid);
802                         netconfig_default_connection_info.essid = NULL;
803                 }
804         }
805
806         /* default profile is NULL and new connected profile is NULL */
807         if (!profile) {
808                 profile = __netconfig_get_default_profile();
809
810                 if (profile && netconfig_is_cellular_profile(profile) &&
811                         !netconfig_is_cellular_internet_profile(profile)) {
812                         DBG("not a default cellular profile");
813                         profile = NULL;
814                 }
815
816                 if (!profile) {
817                         __netconfig_update_default_connection_info();
818                         return;
819                 }
820         }
821
822         netconfig_default_connection_info.profile = g_strdup(profile);
823         __netconfig_get_default_connection_info(profile);
824         __netconfig_update_default_connection_info();
825
826 }
827
828 void netconfig_update_default(void)
829 {
830         if (__netconfig_is_tech_state_connected() == TRUE)
831                 __netconfig_update_if_service_connected();
832         else
833                 __netconfig_adjust_tcp_buffer_size();
834 }
835
836 char *netconfig_get_ifname(const char *profile)
837 {
838         GVariant *message = NULL, *variant;
839         GVariantIter *iter, *next, *service;
840         gchar *obj_path;
841         gchar *key;
842         gchar *key1;
843         const gchar *value = NULL;
844         gchar *ifname = NULL;
845         gboolean found_profile = 0;
846
847         if (profile == NULL)
848                 return NULL;
849
850         message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
851                         CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
852                         "GetServices", NULL);
853         if (message == NULL) {
854                 ERR("Failed to get services informations");
855                 return NULL;
856         }
857
858         g_variant_get(message, "(a(oa{sv}))", &service);
859         while (g_variant_iter_loop(service, "(oa{sv})", &obj_path, &iter)) {
860                 if (g_strcmp0(obj_path, profile) == 0) {
861                         g_free(obj_path);
862                         found_profile = 1;
863                         break;
864                 }
865         }
866
867         if (found_profile == 0) {
868                 ERR("Profile %s doesn't exist", profile);
869                 g_variant_iter_free(service);
870                 g_variant_unref(message);
871                 return NULL;
872         }
873
874         while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
875                 if (g_strcmp0(key, "Ethernet") == 0) {
876                         while (g_variant_iter_loop(next, "{sv}", &key1, &variant)) {
877                                 if (g_strcmp0(key1, "Interface") == 0) {
878                                         value = g_variant_get_string(variant, NULL);
879                                         ifname = g_strdup(value);
880                                 }
881                         }
882                 }
883         }
884
885         g_variant_unref(message);
886
887         g_variant_iter_free(service);
888         g_variant_iter_free(iter);
889
890         return ifname;
891 }
892
893 /* Check Ethernet Cable Plug-in /Plug-out Status */
894 void netconfig_network_notify_ethernet_cable_state(const char *key)
895 {
896         __netconfig_network_notify_result("EthernetCableState", key);
897 }
898
899 static gboolean handle_add_route(
900                 Network *object,
901                 GDBusMethodInvocation *context,
902                 gchar *ip_addr,
903                 gchar *netmask,
904                 gchar *interface,  gchar *gateway, gint address_family)
905 {
906         const gchar *path = ROUTE_EXEC_PATH;
907         gchar *const args[] = { "/sbin/route", "add", "-net", ip_addr,
908                 "netmask", netmask, "dev", interface, NULL };
909         gchar *const envs[] = { NULL };
910         const gchar* buf = NULL;
911         gchar* ch = NULL;
912         int prefix_len = 0;
913         int pos = 0;
914
915         DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
916
917         switch (address_family) {
918         case AF_INET:
919                 if (ip_addr == NULL || netmask == NULL || interface == NULL) {
920                         ERR("Invalid parameter");
921                         netconfig_error_invalid_parameter(context);
922                         return FALSE;
923                 }
924
925                 if (netconfig_execute_file(path, args, envs) < 0) {
926                         DBG("Failed to add a new route");
927                         netconfig_error_permission_denied(context);
928                         return FALSE;
929                 }
930
931                 break;
932         case AF_INET6:
933                 if (ip_addr == NULL || interface == NULL || gateway == NULL) {
934                         ERR("Invalid parameter");
935                         netconfig_error_invalid_parameter(context);
936                         return FALSE;
937                 }
938
939                 buf = ip_addr;
940                 ch = strchr(buf, '/');
941                 pos = ch - buf + 1;
942                 if (ch) {
943                         prefix_len = atoi(ch + 1);
944                         ip_addr[pos-1] = '\0';
945                 } else {
946                         prefix_len = 128;
947                 }
948
949                 if (netconfig_add_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
950                         DBG("Failed to add a new route");
951                         netconfig_error_permission_denied(context);
952                         return FALSE;
953                 }
954                 break;
955         default:
956                 DBG("Unknown Address Family");
957                 netconfig_error_invalid_parameter(context);
958                 return FALSE;
959         }
960
961         DBG("Successfully added a new route");
962         network_complete_add_route(object, context, TRUE);
963         return TRUE;
964 }
965
966 static gboolean handle_remove_route(
967                 Network *object,
968                 GDBusMethodInvocation *context,
969                 gchar *ip_addr,
970                 gchar *netmask,
971                 gchar *interface, gchar *gateway, gint address_family)
972 {
973         const char *path = ROUTE_EXEC_PATH;
974         gchar *const args[] = { "/sbin/route", "del", "-net", ip_addr,
975                 "netmask", netmask, "dev", interface, NULL };
976         char *const envs[] = { NULL };
977         const char* buf = NULL;
978         char* ch = NULL;
979         int prefix_len = 0;
980         int pos = 0;
981
982         DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
983
984         switch (address_family) {
985         case AF_INET:
986                 if (ip_addr == NULL || netmask == NULL || interface == NULL) {
987                         DBG("Invalid parameter!");
988                         netconfig_error_invalid_parameter(context);
989                         return FALSE;
990                 }
991                 if (netconfig_execute_file(path, args, envs) < 0) {
992                         DBG("Failed to remove the route");
993                         netconfig_error_permission_denied(context);
994                         return FALSE;
995                 }
996                 break;
997         case AF_INET6:
998                 if (ip_addr == NULL || interface == NULL || gateway == NULL) {
999                         DBG("Invalid parameter!");
1000                         netconfig_error_invalid_parameter(context);
1001                         return FALSE;
1002                 }
1003
1004                 buf = ip_addr;
1005                 ch = strchr(buf, '/');
1006                 pos = ch - buf + 1;
1007                 if (ch) {
1008                         prefix_len = atoi(ch + 1);
1009                         ip_addr[pos-1] = '\0';
1010                 } else {
1011                         prefix_len = 128;
1012                 }
1013
1014                 if (netconfig_del_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
1015                         DBG("Failed to remove the route");
1016                         netconfig_error_permission_denied(context);
1017                         return FALSE;
1018                 }
1019                 break;
1020         default:
1021                 DBG("Unknown Address Family");
1022                 netconfig_error_invalid_parameter(context);
1023                 return FALSE;
1024         }
1025
1026         DBG("Successfully removed the route");
1027         network_complete_remove_route(object, context, TRUE);
1028         return TRUE;
1029 }
1030
1031 static gboolean handle_check_get_privilege(Network *object,
1032                 GDBusMethodInvocation *context)
1033 {
1034         network_complete_check_get_privilege(object, context);
1035         return TRUE;
1036 }
1037
1038
1039 static gboolean handle_check_profile_privilege(Network *object,
1040                 GDBusMethodInvocation *context)
1041 {
1042         network_complete_check_profile_privilege(object, context);
1043         return TRUE;
1044 }
1045
1046 static gboolean handle_check_internet_privilege(Network *object,
1047                 GDBusMethodInvocation *context)
1048 {
1049         network_complete_check_internet_privilege(object, context);
1050         return TRUE;
1051 }
1052
1053 gboolean handle_ethernet_cable_state(Network *object,
1054         GDBusMethodInvocation *context)
1055 {
1056         int ret = 0;
1057         int state = 0;
1058
1059         ret = netconfig_get_ethernet_cable_state(&state);
1060         if (ret != 0) {
1061                 DBG("Failed to get ethernet cable state");
1062                 netconfig_error_fail_ethernet_cable_state(context);
1063                 return FALSE;
1064         }
1065
1066         DBG("Successfully get ethernet cable state[%d]", state);
1067         network_complete_ethernet_cable_state(object, context, state);
1068         return TRUE;
1069 }
1070
1071 void state_object_create_and_init(void)
1072 {
1073         DBG("Creating network state object");
1074         GDBusInterfaceSkeleton *interface_network = NULL;
1075         GDBusConnection *connection = NULL;
1076         GDBusObjectManagerServer *server = netdbus_get_state_manager();
1077         if (server == NULL)
1078                 return;
1079
1080         connection = netdbus_get_connection();
1081         g_dbus_object_manager_server_set_connection(server, connection);
1082
1083         /*Interface netconfig.network*/
1084         netconfigstate = network_skeleton_new();
1085
1086         interface_network = G_DBUS_INTERFACE_SKELETON(netconfigstate);
1087         g_signal_connect(netconfigstate, "handle-add-route",
1088                                 G_CALLBACK(handle_add_route), NULL);
1089         g_signal_connect(netconfigstate, "handle-check-get-privilege",
1090                                 G_CALLBACK(handle_check_get_privilege), NULL);
1091         g_signal_connect(netconfigstate, "handle-check-profile-privilege",
1092                                 G_CALLBACK(handle_check_profile_privilege), NULL);
1093         g_signal_connect(netconfigstate, "handle-check-internet-privilege",
1094                                 G_CALLBACK(handle_check_internet_privilege), NULL);
1095         g_signal_connect(netconfigstate, "handle-ethernet-cable-state",
1096                                 G_CALLBACK(handle_ethernet_cable_state), NULL);
1097         g_signal_connect(netconfigstate, "handle-remove-route",
1098                                 G_CALLBACK(handle_remove_route), NULL);
1099         g_signal_connect(netconfigstate, "handle-launch-mdns",
1100                                 G_CALLBACK(handle_launch_mdns), NULL);
1101         g_signal_connect(netconfigstate, "handle-unref-mdns",
1102                                 G_CALLBACK(handle_unref_mdns), NULL);
1103         g_signal_connect(netconfigstate, "handle-device-policy-set-wifi",
1104                                 G_CALLBACK(handle_device_policy_set_wifi), NULL);
1105         g_signal_connect(netconfigstate, "handle-device-policy-get-wifi",
1106                                 G_CALLBACK(handle_device_policy_get_wifi), NULL);
1107         g_signal_connect(netconfigstate, "handle-device-policy-set-wifi-profile",
1108                                 G_CALLBACK(handle_device_policy_set_wifi_profile), NULL);
1109         g_signal_connect(netconfigstate, "handle-device-policy-get-wifi-profile",
1110                                 G_CALLBACK(handle_device_policy_get_wifi_profile), NULL);
1111
1112         if (!g_dbus_interface_skeleton_export(interface_network, connection,
1113                         NETCONFIG_NETWORK_STATE_PATH, NULL)) {
1114                 ERR("Export with path failed");
1115         }
1116 }
1117
1118 void state_object_deinit(void)
1119 {
1120         g_object_unref(netconfigstate);
1121 }