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