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