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