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