Merge "Added cap_dac_override capability" into tizen_4.0
[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                                 }
555                         }
556                         g_variant_iter_free(sub_iter);
557                 }
558         }
559
560 done:
561         if (message)
562                 g_variant_unref(message);
563
564         if (iter)
565                 g_variant_iter_free(iter);
566
567         if (service)
568                 g_variant_iter_free(service);
569
570         return preferred_address6;
571 }
572
573 static void __netconfig_adjust_tcp_buffer_size(void)
574 {
575         int fdr = 0, fdw = 0;
576         int fdrmax = 0, fdwmax = 0;
577         const char *rbuf_size = NULL;
578         const char *wbuf_size = NULL;
579         const char *rmax_size = NULL;
580         const char *wmax_size = NULL;
581         const char *profile = netconfig_get_default_profile();
582
583         if (profile == NULL) {
584                 DBG("There is no default connection");
585
586                 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
587                 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
588         } else if (netconfig_is_wifi_profile(profile) == TRUE) {
589                 DBG("Default connection: Wi-Fi");
590
591                 rbuf_size = NET_TCP_BUFFERSIZE_WIFI_READ;
592                 wbuf_size = NET_TCP_BUFFERSIZE_WIFI_WRITE;
593                 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
594                 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
595         } else if (netconfig_is_cellular_profile(profile) == TRUE) {
596                 int telephony_svctype = 0, telephony_pstype = 0;
597
598                 netconfig_get_telephony_network_type(&telephony_svctype, &telephony_pstype);
599                 DBG("Default cellular %d, %d", telephony_svctype, telephony_pstype);
600
601                 switch (telephony_pstype) {
602                 case VCONFKEY_TELEPHONY_PSTYPE_HSPA:
603                         rbuf_size = NET_TCP_BUFFERSIZE_HSPA_READ;
604                         wbuf_size = NET_TCP_BUFFERSIZE_HSPA_WRITE;
605                         break;
606                 case VCONFKEY_TELEPHONY_PSTYPE_HSUPA:
607                         rbuf_size = NET_TCP_BUFFERSIZE_HSUPA_READ;
608                         wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
609                         break;
610                 case VCONFKEY_TELEPHONY_PSTYPE_HSDPA:
611                         rbuf_size = NET_TCP_BUFFERSIZE_HSDPA_READ;
612                         wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
613                         break;
614 #if !defined TIZEN_WEARABLE
615                 case VCONFKEY_TELEPHONY_PSTYPE_HSPAP:
616                         rbuf_size = NET_TCP_BUFFERSIZE_HSPAP_READ;
617                         wbuf_size = NET_TCP_BUFFERSIZE_HSPAP_WRITE;
618                         break;
619 #endif
620                 default:
621                         switch (telephony_svctype) {
622                         case VCONFKEY_TELEPHONY_SVCTYPE_LTE:
623                                 rbuf_size = NET_TCP_BUFFERSIZE_LTE_READ;
624                                 wbuf_size = NET_TCP_BUFFERSIZE_LTE_WRITE;
625                                 rmax_size = NET_TCP_BUFFERSIZE_LTE_RMEM_MAX;
626                                 break;
627                         case VCONFKEY_TELEPHONY_SVCTYPE_3G:
628                                 rbuf_size = NET_TCP_BUFFERSIZE_UMTS_READ;
629                                 wbuf_size = NET_TCP_BUFFERSIZE_UMTS_WRITE;
630                                 break;
631                         case VCONFKEY_TELEPHONY_SVCTYPE_2_5G_EDGE:
632                                 rbuf_size = NET_TCP_BUFFERSIZE_EDGE_READ;
633                                 wbuf_size = NET_TCP_BUFFERSIZE_EDGE_WRITE;
634                                 break;
635                         case VCONFKEY_TELEPHONY_SVCTYPE_2_5G:
636                                 rbuf_size = NET_TCP_BUFFERSIZE_GPRS_READ;
637                                 wbuf_size = NET_TCP_BUFFERSIZE_GPRS_WRITE;
638                                 break;
639                         default:
640                                 /* TODO: Check LTE support */
641                                 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
642                                 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
643                                 break;
644                         }
645                         break;
646                 }
647         } else {
648                 DBG("Default TCP buffer configured");
649
650                 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
651                 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
652         }
653
654         if (rbuf_size != NULL) {
655                 fdr = open(NET_PROC_SYS_NET_IPV4_TCP_RMEM, O_RDWR | O_CLOEXEC);
656
657                 if (fdr < 0 || write(fdr, rbuf_size, strlen(rbuf_size)) < 0)
658                         ERR("Failed to set TCP read buffer size");
659
660                 if (fdr >= 0)
661                         close(fdr);
662         }
663
664         if (wbuf_size != NULL) {
665                 fdw = open(NET_PROC_SYS_NET_IPv4_TCP_WMEM, O_RDWR | O_CLOEXEC);
666
667                 if (fdw < 0 || write(fdw, wbuf_size, strlen(wbuf_size)) < 0)
668                         ERR("Failed to set TCP write buffer size");
669
670                 if (fdw >= 0)
671                         close(fdw);
672         }
673
674         /* As default */
675         if (rmax_size == NULL)
676                 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
677         if (wmax_size == NULL)
678                 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
679
680         if (rmax_size != NULL) {
681                 fdrmax = open(NET_PROC_SYS_NET_CORE_RMEM_MAX, O_RDWR | O_CLOEXEC);
682
683                 if (fdrmax < 0 || write(fdrmax, rmax_size, strlen(rmax_size)) < 0)
684                         ERR("Failed to set TCP rmem_max size");
685
686                 if (fdrmax >= 0)
687                         close(fdrmax);
688         }
689
690         if (wmax_size != NULL) {
691                 fdwmax = open(NET_PROC_SYS_NET_CORE_WMEM_MAX, O_RDWR | O_CLOEXEC);
692
693                 if (fdwmax < 0 || write(fdwmax, wmax_size, strlen(wmax_size)) < 0)
694                         ERR("Failed to set TCP wmem_max size");
695
696                 if (fdwmax >= 0)
697                         close(fdwmax);
698         }
699 }
700
701 static void __netconfig_update_default_connection_info(void)
702 {
703         int old_network_status = 0;
704         const char *profile = netconfig_get_default_profile();
705         const char *ip_addr = netconfig_get_default_ipaddress();
706         const char *ip_addr6 = netconfig_get_default_ipaddress6();
707         const char *proxy_addr = netconfig_get_default_proxy();
708         unsigned int freq = netconfig_get_default_frequency();
709
710         if (emulator_is_emulated() == TRUE) {
711                 if (ip_addr != NULL)
712                         netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr);
713                 else
714                         netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
715
716                 if (ip_addr6 != NULL)
717                         netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, ip_addr6);
718                 else
719                         netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, "");
720
721                 return;
722         }
723
724         if (profile == NULL)
725                 DBG("Reset network state configuration");
726         else
727                 DBG("profile[%s] ipv4(%s) ipv6(%s) proxy(%s)", profile, ip_addr, ip_addr6, proxy_addr);
728
729         netconfig_vconf_get_int(VCONFKEY_NETWORK_STATUS, &old_network_status);
730
731         if (profile == NULL && old_network_status != VCONFKEY_NETWORK_OFF) {
732                 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
733
734                 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
735                 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, "");
736                 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
737
738                 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 0);
739                 netconfig_set_vconf_int("memory/private/wifi/frequency", 0);
740
741                 DBG("Successfully clear IP and PROXY up");
742
743         } else if (profile != NULL) {
744                 char *old_ip = vconf_get_str(VCONFKEY_NETWORK_IP);
745                 char *old_ip6 = vconf_get_str(VCONFKEY_NETWORK_IP6);
746                 char *old_proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY);
747
748                 if (netconfig_is_wifi_profile(profile) == TRUE) {
749                         netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_WIFI);
750                         netconfig_set_vconf_int("memory/private/wifi/frequency", freq);
751
752                         netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
753                                 EKEY_NETWORK_STATUS, EVAL_NETWORK_WIFI);
754                 } else if (netconfig_is_cellular_profile(profile)) {
755
756                         if (!netconfig_is_cellular_internet_profile(profile)) {
757                                 DBG("connection is not a internet profile - stop to update the cellular state");
758                                 if (old_ip)
759                                         free(old_ip);
760                                 if (old_ip6)
761                                         free(old_ip6);
762                                 if (old_proxy)
763                                         free(old_proxy);
764                                 return;
765                         }
766
767                         netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_CELLULAR);
768
769                         netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
770                                 EKEY_NETWORK_STATUS, EVAL_NETWORK_CELLULAR);
771                 } else if (netconfig_is_ethernet_profile(profile) == TRUE) {
772                         netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_ETHERNET);
773                         netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
774                                 EKEY_NETWORK_STATUS, EVAL_NETWORK_ETHERNET);
775                 } else if (netconfig_is_bluetooth_profile(profile) == TRUE) {
776                         netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_BLUETOOTH);
777                         netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
778                                 EKEY_NETWORK_STATUS, EVAL_NETWORK_BT);
779                 } else{
780                         netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
781                         netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
782                                 EKEY_NETWORK_STATUS, EVAL_NETWORK_DISCONNECTED);
783                 }
784
785                 if (g_strcmp0(old_ip, ip_addr) != 0 || old_ip == NULL) {
786                         if (ip_addr != NULL)
787                                 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr);
788                         else if (old_ip != NULL && strlen(old_ip) > 0)
789                                 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
790                 }
791                 if (old_ip)
792                         free(old_ip);
793
794                 if (g_strcmp0(old_ip6, ip_addr6) != 0 || old_ip6 == NULL) {
795                         if (ip_addr6 != NULL)
796                                 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, ip_addr6);
797                         else if (old_ip6 != NULL && strlen(old_ip6) > 0)
798                                 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, "");
799                 }
800                 if (old_ip6)
801                         free(old_ip6);
802
803                 if (g_strcmp0(old_proxy, proxy_addr) != 0) {
804                         if (proxy_addr == NULL)
805                                 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
806                         else
807                                 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, proxy_addr);
808                 }
809                 if (old_proxy)
810                         free(old_proxy);
811
812                 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 1);
813
814                 DBG("Successfully update default network configuration");
815         }
816
817         __netconfig_adjust_tcp_buffer_size();
818 }
819
820 static gboolean __netconfig_is_tech_state_connected(void)
821 {
822         gboolean ret = FALSE;
823         GVariant *message = NULL, *variant;
824         GVariantIter *iter, *next;
825         gchar *path;
826         gchar *key;
827
828         message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
829                         CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
830                         "GetTechnologies", NULL);
831
832         if (message == NULL) {
833                 DBG("Fail to get technology state");
834                 return FALSE;
835         }
836
837         g_variant_get(message, "(a(oa{sv}))", &iter);
838         while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
839                 if (path == NULL)
840                         continue;
841
842                 while (g_variant_iter_loop(next, "{sv}", &key, &variant)) {
843                         gboolean data;
844                         if (g_strcmp0(key, "Connected") == 0) {
845                                 data = g_variant_get_boolean(variant);
846                                 DBG("%s [%s: %s]", path, key, data ? "True" : "False");
847                                 if (TRUE == data) {
848                                         ret = TRUE;
849                                         g_free(path);
850                                         g_free(key);
851                                         g_variant_unref(variant);
852                                         g_variant_iter_free(next);
853                                         goto done;
854                                 }
855                         }
856                 }
857         }
858
859 done:
860         g_variant_iter_free(iter);
861         g_variant_unref(message);
862
863         return ret;
864 }
865
866 static void __netconfig_update_if_service_connected(void)
867 {
868         GVariant *message = NULL, *var;
869         GVariantIter *iter, *next;
870         gchar *path;
871         gchar *key;
872
873         message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
874                         CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
875                         "GetServices", NULL);
876
877         if (message == NULL) {
878                 ERR("Failed to get services");
879                 return;
880         }
881
882         g_variant_get(message, "(a(oa{sv}))", &iter);
883         while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
884                 if (path == NULL)
885                         continue;
886
887                 if (g_str_has_prefix(path,
888                                                 CONNMAN_WIFI_SERVICE_PROFILE_PREFIX) == TRUE) {
889                         if (g_strrstr(path + strlen(CONNMAN_WIFI_SERVICE_PROFILE_PREFIX),
890                                                         "hidden") != NULL) {
891                                 /* skip hidden profiles */
892                                 continue;
893                         }
894                         /* Process this */
895                 } else if (g_str_has_prefix(path,
896                                                 CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX) == TRUE) {
897                         /* Process this */
898                 } else {
899                         continue;
900                 }
901
902                 while (g_variant_iter_loop(next, "{sv}", &key, &var)) {
903                         if (g_strcmp0(key, "State") == 0) {
904                                 const gchar *sdata = NULL;
905                                 sdata = g_variant_get_string(var, NULL);
906                                 DBG("%s [%s: %s]", path, key, sdata);
907
908                                 if (g_strcmp0(sdata, "online") == 0 || g_strcmp0(sdata, "ready") == 0) {
909
910                                         /* Found a connected WiFi / 3G service.
911                                          * Lets update the default profile info.
912                                          */
913                                         netconfig_update_default_profile((const gchar*)path);
914                                         g_free(key);
915                                         g_free(path);
916                                         g_variant_unref(var);
917                                         g_variant_iter_free(next);
918                                         goto done;
919                                 }
920                         }
921                 }
922         }
923 done:
924         g_variant_iter_free(iter);
925         g_variant_unref(message);
926
927         return;
928 }
929
930 static void __netconfig_network_notify_result(const char *sig_name, const char *key)
931 {
932         GVariantBuilder *builder;
933         GVariant *params;
934
935         INFO("[Signal] %s %s", sig_name, key);
936
937         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
938         g_variant_builder_add(builder, "{sv}", "key", g_variant_new_string(key));
939
940         params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
941         g_variant_builder_unref(builder);
942
943         netconfig_dbus_emit_signal(NULL,
944                                 NETCONFIG_NETWORK_PATH,
945                                 NETCONFIG_NETWORK_INTERFACE,
946                                 sig_name,
947                                 params);
948
949         return;
950 }
951
952 const char *netconfig_get_default_profile(void)
953 {
954         return netconfig_default_connection_info.profile;
955 }
956
957 const char *netconfig_get_default_ifname(void)
958 {
959         return netconfig_default_connection_info.ifname;
960 }
961
962 const char *netconfig_get_default_ipaddress(void)
963 {
964         return netconfig_default_connection_info.ipaddress;
965 }
966
967 const char *netconfig_get_default_ipaddress6(void)
968 {
969         return netconfig_default_connection_info.ipaddress6;
970 }
971
972 const char *netconfig_get_default_proxy(void)
973 {
974         return netconfig_default_connection_info.proxy;
975 }
976
977 unsigned int netconfig_get_default_frequency(void)
978 {
979         return netconfig_default_connection_info.freq;
980 }
981
982 const char *netconfig_wifi_get_connected_essid(const char *default_profile)
983 {
984         if (default_profile == NULL)
985                 return NULL;
986
987         if (netconfig_is_wifi_profile(default_profile) != TRUE)
988                 return NULL;
989
990         if (g_strcmp0(default_profile, netconfig_default_connection_info.profile) != 0)
991                 return NULL;
992
993         return netconfig_default_connection_info.essid;
994 }
995
996 gboolean netconfig_get_default_is_metered(void)
997 {
998         return netconfig_default_connection_info.is_metered;
999 }
1000
1001 static int __netconfig_reset_ipv4_socket(void)
1002 {
1003         int ret;
1004         int fd;
1005         struct ifreq ifr;
1006         struct sockaddr_in sai;
1007         const char *ipaddr = netconfig_get_default_ipaddress();
1008         DBG("ipaddr-[%s]", ipaddr);
1009
1010         if (!ipaddr)
1011                 return -1;
1012
1013         fd = socket(AF_INET, SOCK_DGRAM, 0);
1014         if (fd < 0)
1015                 return -1;
1016
1017         memset(&sai, 0, sizeof(struct sockaddr_in));
1018         sai.sin_family = AF_INET;
1019         sai.sin_port = 0;
1020         if (!inet_aton(ipaddr, &sai.sin_addr)) {
1021                 DBG("fail to inet_aton()");
1022                 close(fd);
1023                 return -1;
1024         }
1025
1026         memset(&ifr, 0, sizeof(struct ifreq));
1027         memcpy(&ifr.ifr_addr, &sai, sizeof(sai));
1028         g_strlcpy((char *)ifr.ifr_name, WIFI_IFNAME, IFNAMSIZ);
1029
1030 #ifndef SIOCKILLADDR
1031 #define SIOCKILLADDR    0x8939
1032 #endif
1033
1034         ret = ioctl(fd, SIOCKILLADDR, &ifr);
1035         if (ret < 0) {
1036                 DBG("fail to ioctl[SIOCKILLADDR]");
1037                 close(fd);
1038                 return -1;
1039         }
1040
1041         close(fd);
1042         return 0;
1043 }
1044
1045 void netconfig_update_default_profile(const char *profile)
1046 {
1047         static char *old_profile = NULL;
1048
1049         /* It's automatically updated by signal-handler
1050          * DO NOT update manually
1051          *
1052          * It is going to update default connection information
1053          */
1054
1055         if (netconfig_default_connection_info.profile != NULL) {
1056
1057                 if (netconfig_is_wifi_profile(netconfig_default_connection_info.profile))
1058                         __netconfig_reset_ipv4_socket();
1059
1060                 g_free(old_profile);
1061                 old_profile = strdup(netconfig_default_connection_info.profile);
1062
1063                 g_free(netconfig_default_connection_info.profile);
1064                 netconfig_default_connection_info.profile = NULL;
1065
1066                 g_free(netconfig_default_connection_info.ifname);
1067                 netconfig_default_connection_info.ifname = NULL;
1068
1069                 g_free(netconfig_default_connection_info.ipaddress);
1070                 netconfig_default_connection_info.ipaddress = NULL;
1071
1072                 g_free(netconfig_default_connection_info.ipaddress6);
1073                 netconfig_default_connection_info.ipaddress6 = NULL;
1074
1075                 g_free(netconfig_default_connection_info.proxy);
1076                 netconfig_default_connection_info.proxy = NULL;
1077
1078                 netconfig_default_connection_info.freq = 0;
1079                 netconfig_default_connection_info.is_metered = FALSE;
1080
1081                 if (wifi_state_get_service_state() != NETCONFIG_WIFI_CONNECTED) {
1082                         g_free(netconfig_default_connection_info.essid);
1083                         netconfig_default_connection_info.essid = NULL;
1084                 }
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                                         ifname = g_strdup(value);
1167                                         g_free(key1);
1168                                         g_variant_unref(variant);
1169                                         break;
1170                                 }
1171                         }
1172                 }
1173         }
1174
1175         g_variant_unref(message);
1176
1177         g_variant_iter_free(service);
1178         g_variant_iter_free(iter);
1179
1180         return ifname;
1181 }
1182
1183 /* Check Ethernet Cable Plug-in /Plug-out Status */
1184 void netconfig_network_notify_ethernet_cable_state(const char *key)
1185 {
1186         __netconfig_network_notify_result("EthernetCableState", key);
1187 }
1188
1189 static gboolean handle_add_route(
1190                 Network *object,
1191                 GDBusMethodInvocation *context,
1192                 gchar *ip_addr,
1193                 gchar *netmask,
1194                 gchar *interface,  gchar *gateway, gint address_family)
1195 {
1196         const gchar *path = ROUTE_EXEC_PATH;
1197         gchar *const args[] = { "/sbin/route", "add", "-net", ip_addr,
1198                 "netmask", netmask, "dev", interface, NULL };
1199         gchar *const envs[] = { NULL };
1200         const gchar* buf = NULL;
1201         gchar* ch = NULL;
1202         int prefix_len = 0;
1203         int pos = 0;
1204
1205         DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
1206
1207         switch (address_family) {
1208         case AF_INET:
1209                 if (ip_addr == NULL || netmask == NULL || interface == NULL) {
1210                         ERR("Invalid parameter");
1211                         netconfig_error_invalid_parameter(context);
1212                         return FALSE;
1213                 }
1214
1215                 if (netconfig_execute_file(path, args, envs) < 0) {
1216                         DBG("Failed to add a new route");
1217                         netconfig_error_permission_denied(context);
1218                         return FALSE;
1219                 }
1220
1221                 break;
1222         case AF_INET6:
1223                 if (ip_addr == NULL || interface == NULL || gateway == NULL) {
1224                         ERR("Invalid parameter");
1225                         netconfig_error_invalid_parameter(context);
1226                         return FALSE;
1227                 }
1228
1229                 buf = ip_addr;
1230                 ch = strchr(buf, '/');
1231                 pos = ch - buf + 1;
1232                 if (ch) {
1233                         prefix_len = atoi(ch + 1);
1234                         ip_addr[pos-1] = '\0';
1235                 } else {
1236                         prefix_len = 128;
1237                 }
1238
1239                 if (netconfig_add_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
1240                         DBG("Failed to add a new route");
1241                         netconfig_error_permission_denied(context);
1242                         return FALSE;
1243                 }
1244                 break;
1245         default:
1246                 DBG("Unknown Address Family");
1247                 netconfig_error_invalid_parameter(context);
1248                 return FALSE;
1249         }
1250
1251         DBG("Successfully added a new route");
1252         network_complete_add_route(object, context, TRUE);
1253         return TRUE;
1254 }
1255
1256 static gboolean handle_remove_route(
1257                 Network *object,
1258                 GDBusMethodInvocation *context,
1259                 gchar *ip_addr,
1260                 gchar *netmask,
1261                 gchar *interface, gchar *gateway, gint address_family)
1262 {
1263         const char *path = ROUTE_EXEC_PATH;
1264         gchar *const args[] = { "/sbin/route", "del", "-net", ip_addr,
1265                 "netmask", netmask, "dev", interface, NULL };
1266         char *const envs[] = { NULL };
1267         const char* buf = NULL;
1268         char* ch = NULL;
1269         int prefix_len = 0;
1270         int pos = 0;
1271
1272         DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
1273
1274         switch (address_family) {
1275         case AF_INET:
1276                 if (ip_addr == NULL || netmask == NULL || interface == NULL) {
1277                         DBG("Invalid parameter!");
1278                         netconfig_error_invalid_parameter(context);
1279                         return FALSE;
1280                 }
1281                 if (netconfig_execute_file(path, args, envs) < 0) {
1282                         DBG("Failed to remove the route");
1283                         netconfig_error_permission_denied(context);
1284                         return FALSE;
1285                 }
1286                 break;
1287         case AF_INET6:
1288                 if (ip_addr == NULL || interface == NULL || gateway == NULL) {
1289                         DBG("Invalid parameter!");
1290                         netconfig_error_invalid_parameter(context);
1291                         return FALSE;
1292                 }
1293
1294                 buf = ip_addr;
1295                 ch = strchr(buf, '/');
1296                 pos = ch - buf + 1;
1297                 if (ch) {
1298                         prefix_len = atoi(ch + 1);
1299                         ip_addr[pos-1] = '\0';
1300                 } else {
1301                         prefix_len = 128;
1302                 }
1303
1304                 if (netconfig_del_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
1305                         DBG("Failed to remove the route");
1306                         netconfig_error_permission_denied(context);
1307                         return FALSE;
1308                 }
1309                 break;
1310         default:
1311                 DBG("Unknown Address Family");
1312                 netconfig_error_invalid_parameter(context);
1313                 return FALSE;
1314         }
1315
1316         DBG("Successfully removed the route");
1317         network_complete_remove_route(object, context, TRUE);
1318         return TRUE;
1319 }
1320
1321 static gboolean handle_check_get_privilege(Network *object,
1322                 GDBusMethodInvocation *context)
1323 {
1324         network_complete_check_get_privilege(object, context);
1325         return TRUE;
1326 }
1327
1328
1329 static gboolean handle_check_profile_privilege(Network *object,
1330                 GDBusMethodInvocation *context)
1331 {
1332         network_complete_check_profile_privilege(object, context);
1333         return TRUE;
1334 }
1335
1336 static gboolean handle_check_internet_privilege(Network *object,
1337                 GDBusMethodInvocation *context)
1338 {
1339         network_complete_check_internet_privilege(object, context);
1340         return TRUE;
1341 }
1342
1343 gboolean handle_ethernet_cable_state(Network *object,
1344         GDBusMethodInvocation *context)
1345 {
1346         int ret = 0;
1347         int state = 0;
1348
1349         ret = netconfig_get_ethernet_cable_state(&state);
1350         if (ret != 0) {
1351                 DBG("Failed to get ethernet cable state");
1352                 netconfig_error_fail_ethernet_cable_state(context);
1353                 return FALSE;
1354         }
1355
1356         DBG("Successfully get ethernet cable state[%d]", state);
1357         network_complete_ethernet_cable_state(object, context, state);
1358         return TRUE;
1359 }
1360
1361 gboolean handle_get_metered_info(Network *object,
1362         GDBusMethodInvocation *context)
1363 {
1364         gboolean state = 0;
1365
1366         state = netconfig_get_default_is_metered();
1367
1368         DBG("Default metered state [%s]", state ? "TRUE" : "FALSE");
1369         network_complete_get_metered_info(object, context, state);
1370         return TRUE;
1371 }
1372
1373 gboolean handle_preferred_ipv6_address(Network *object,
1374         GDBusMethodInvocation *context, gchar *profile)
1375 {
1376         char *address = NULL;
1377
1378         address = __netconfig_get_preferred_ipv6_address(profile);
1379         if (address == NULL) {
1380                 DBG("Failed to get preferred IPv6 address");
1381                 netconfig_error_fail_preferred_ipv6_address(context);
1382                 return FALSE;
1383         }
1384
1385         DBG("Successfully get preferred IPv6 address[%s]", address);
1386         network_complete_preferred_ipv6_address(object, context, address);
1387         return TRUE;
1388 }
1389
1390 void state_object_create_and_init(void)
1391 {
1392         DBG("Creating network state object");
1393         GDBusInterfaceSkeleton *interface_network = NULL;
1394         GDBusConnection *connection = NULL;
1395         GDBusObjectManagerServer *server = netdbus_get_state_manager();
1396         if (server == NULL)
1397                 return;
1398
1399         connection = netdbus_get_connection();
1400         g_dbus_object_manager_server_set_connection(server, connection);
1401
1402         /*Interface netconfig.network*/
1403         netconfigstate = network_skeleton_new();
1404
1405         interface_network = G_DBUS_INTERFACE_SKELETON(netconfigstate);
1406         g_signal_connect(netconfigstate, "handle-add-route",
1407                                 G_CALLBACK(handle_add_route), NULL);
1408         g_signal_connect(netconfigstate, "handle-check-get-privilege",
1409                                 G_CALLBACK(handle_check_get_privilege), NULL);
1410         g_signal_connect(netconfigstate, "handle-check-profile-privilege",
1411                                 G_CALLBACK(handle_check_profile_privilege), NULL);
1412         g_signal_connect(netconfigstate, "handle-check-internet-privilege",
1413                                 G_CALLBACK(handle_check_internet_privilege), NULL);
1414         g_signal_connect(netconfigstate, "handle-ethernet-cable-state",
1415                                 G_CALLBACK(handle_ethernet_cable_state), NULL);
1416         g_signal_connect(netconfigstate, "handle-preferred-ipv6-address",
1417                                 G_CALLBACK(handle_preferred_ipv6_address), NULL);
1418         g_signal_connect(netconfigstate, "handle-remove-route",
1419                                 G_CALLBACK(handle_remove_route), NULL);
1420         g_signal_connect(netconfigstate, "handle-launch-mdns",
1421                                 G_CALLBACK(handle_launch_mdns), NULL);
1422         g_signal_connect(netconfigstate, "handle-device-policy-set-wifi",
1423                                 G_CALLBACK(handle_device_policy_set_wifi), NULL);
1424         g_signal_connect(netconfigstate, "handle-device-policy-get-wifi",
1425                                 G_CALLBACK(handle_device_policy_get_wifi), NULL);
1426         g_signal_connect(netconfigstate, "handle-device-policy-set-wifi-profile",
1427                                 G_CALLBACK(handle_device_policy_set_wifi_profile), NULL);
1428         g_signal_connect(netconfigstate, "handle-device-policy-get-wifi-profile",
1429                                 G_CALLBACK(handle_device_policy_get_wifi_profile), NULL);
1430         g_signal_connect(netconfigstate, "handle-get-metered-info",
1431                                 G_CALLBACK(handle_get_metered_info), NULL);
1432
1433         if (!g_dbus_interface_skeleton_export(interface_network, connection,
1434                         NETCONFIG_NETWORK_STATE_PATH, NULL)) {
1435                 ERR("Export with path failed");
1436         }
1437 }
1438
1439 void state_object_deinit(void)
1440 {
1441         g_object_unref(netconfigstate);
1442 }