Fixed coverity issues
[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                                         break;
555                                 }
556                         }
557                         g_variant_iter_free(sub_iter);
558                 }
559         }
560
561 done:
562         if (message)
563                 g_variant_unref(message);
564
565         if (iter)
566                 g_variant_iter_free(iter);
567
568         if (service)
569                 g_variant_iter_free(service);
570
571         return preferred_address6;
572 }
573
574 static void __netconfig_adjust_tcp_buffer_size(void)
575 {
576         int fdr = 0, fdw = 0;
577         int fdrmax = 0, fdwmax = 0;
578         const char *rbuf_size = NULL;
579         const char *wbuf_size = NULL;
580         const char *rmax_size = NULL;
581         const char *wmax_size = NULL;
582         const char *profile = netconfig_get_default_profile();
583
584         if (profile == NULL) {
585                 DBG("There is no default connection");
586
587                 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
588                 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
589         } else if (netconfig_is_wifi_profile(profile) == TRUE) {
590                 DBG("Default connection: Wi-Fi");
591
592                 rbuf_size = NET_TCP_BUFFERSIZE_WIFI_READ;
593                 wbuf_size = NET_TCP_BUFFERSIZE_WIFI_WRITE;
594                 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
595                 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
596         } else if (netconfig_is_cellular_profile(profile) == TRUE) {
597                 int telephony_svctype = 0, telephony_pstype = 0;
598
599                 netconfig_get_telephony_network_type(&telephony_svctype, &telephony_pstype);
600                 DBG("Default cellular %d, %d", telephony_svctype, telephony_pstype);
601
602                 switch (telephony_pstype) {
603                 case VCONFKEY_TELEPHONY_PSTYPE_HSPA:
604                         rbuf_size = NET_TCP_BUFFERSIZE_HSPA_READ;
605                         wbuf_size = NET_TCP_BUFFERSIZE_HSPA_WRITE;
606                         break;
607                 case VCONFKEY_TELEPHONY_PSTYPE_HSUPA:
608                         rbuf_size = NET_TCP_BUFFERSIZE_HSUPA_READ;
609                         wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
610                         break;
611                 case VCONFKEY_TELEPHONY_PSTYPE_HSDPA:
612                         rbuf_size = NET_TCP_BUFFERSIZE_HSDPA_READ;
613                         wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
614                         break;
615 #if !defined TIZEN_WEARABLE
616                 case VCONFKEY_TELEPHONY_PSTYPE_HSPAP:
617                         rbuf_size = NET_TCP_BUFFERSIZE_HSPAP_READ;
618                         wbuf_size = NET_TCP_BUFFERSIZE_HSPAP_WRITE;
619                         break;
620 #endif
621                 default:
622                         switch (telephony_svctype) {
623                         case VCONFKEY_TELEPHONY_SVCTYPE_LTE:
624                                 rbuf_size = NET_TCP_BUFFERSIZE_LTE_READ;
625                                 wbuf_size = NET_TCP_BUFFERSIZE_LTE_WRITE;
626                                 rmax_size = NET_TCP_BUFFERSIZE_LTE_RMEM_MAX;
627                                 break;
628                         case VCONFKEY_TELEPHONY_SVCTYPE_3G:
629                                 rbuf_size = NET_TCP_BUFFERSIZE_UMTS_READ;
630                                 wbuf_size = NET_TCP_BUFFERSIZE_UMTS_WRITE;
631                                 break;
632                         case VCONFKEY_TELEPHONY_SVCTYPE_2_5G_EDGE:
633                                 rbuf_size = NET_TCP_BUFFERSIZE_EDGE_READ;
634                                 wbuf_size = NET_TCP_BUFFERSIZE_EDGE_WRITE;
635                                 break;
636                         case VCONFKEY_TELEPHONY_SVCTYPE_2_5G:
637                                 rbuf_size = NET_TCP_BUFFERSIZE_GPRS_READ;
638                                 wbuf_size = NET_TCP_BUFFERSIZE_GPRS_WRITE;
639                                 break;
640                         default:
641                                 /* TODO: Check LTE support */
642                                 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
643                                 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
644                                 break;
645                         }
646                         break;
647                 }
648         } else {
649                 DBG("Default TCP buffer configured");
650
651                 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
652                 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
653         }
654
655         if (rbuf_size != NULL) {
656                 fdr = open(NET_PROC_SYS_NET_IPV4_TCP_RMEM, O_RDWR | O_CLOEXEC);
657
658                 if (fdr < 0 || write(fdr, rbuf_size, strlen(rbuf_size)) < 0)
659                         ERR("Failed to set TCP read buffer size");
660
661                 if (fdr >= 0)
662                         close(fdr);
663         }
664
665         if (wbuf_size != NULL) {
666                 fdw = open(NET_PROC_SYS_NET_IPv4_TCP_WMEM, O_RDWR | O_CLOEXEC);
667
668                 if (fdw < 0 || write(fdw, wbuf_size, strlen(wbuf_size)) < 0)
669                         ERR("Failed to set TCP write buffer size");
670
671                 if (fdw >= 0)
672                         close(fdw);
673         }
674
675         /* As default */
676         if (rmax_size == NULL)
677                 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
678         if (wmax_size == NULL)
679                 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
680
681         if (rmax_size != NULL) {
682                 fdrmax = open(NET_PROC_SYS_NET_CORE_RMEM_MAX, O_RDWR | O_CLOEXEC);
683
684                 if (fdrmax < 0 || write(fdrmax, rmax_size, strlen(rmax_size)) < 0)
685                         ERR("Failed to set TCP rmem_max size");
686
687                 if (fdrmax >= 0)
688                         close(fdrmax);
689         }
690
691         if (wmax_size != NULL) {
692                 fdwmax = open(NET_PROC_SYS_NET_CORE_WMEM_MAX, O_RDWR | O_CLOEXEC);
693
694                 if (fdwmax < 0 || write(fdwmax, wmax_size, strlen(wmax_size)) < 0)
695                         ERR("Failed to set TCP wmem_max size");
696
697                 if (fdwmax >= 0)
698                         close(fdwmax);
699         }
700 }
701
702 static void __netconfig_update_default_connection_info(void)
703 {
704         int old_network_status = 0;
705         const char *profile = netconfig_get_default_profile();
706         const char *ip_addr = netconfig_get_default_ipaddress();
707         const char *ip_addr6 = netconfig_get_default_ipaddress6();
708         const char *proxy_addr = netconfig_get_default_proxy();
709         unsigned int freq = netconfig_get_default_frequency();
710
711         if (emulator_is_emulated() == TRUE) {
712                 if (ip_addr != NULL)
713                         netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr);
714                 else
715                         netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
716
717                 if (ip_addr6 != NULL)
718                         netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, ip_addr6);
719                 else
720                         netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, "");
721
722                 return;
723         }
724
725         if (profile == NULL)
726                 DBG("Reset network state configuration");
727         else
728                 DBG("profile[%s] ipv4(%s) ipv6(%s) proxy(%s)", profile, ip_addr, ip_addr6, proxy_addr);
729
730         netconfig_vconf_get_int(VCONFKEY_NETWORK_STATUS, &old_network_status);
731
732         if (profile == NULL && old_network_status != VCONFKEY_NETWORK_OFF) {
733                 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
734
735                 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
736                 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, "");
737                 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
738
739                 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 0);
740                 netconfig_set_vconf_int("memory/private/wifi/frequency", 0);
741
742                 DBG("Successfully clear IP and PROXY up");
743
744         } else if (profile != NULL) {
745                 char *old_ip = vconf_get_str(VCONFKEY_NETWORK_IP);
746                 char *old_ip6 = vconf_get_str(VCONFKEY_NETWORK_IP6);
747                 char *old_proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY);
748
749                 if (netconfig_is_wifi_profile(profile) == TRUE) {
750                         netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_WIFI);
751                         netconfig_set_vconf_int("memory/private/wifi/frequency", freq);
752
753                         netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
754                                 EKEY_NETWORK_STATUS, EVAL_NETWORK_WIFI);
755                 } else if (netconfig_is_cellular_profile(profile)) {
756
757                         if (!netconfig_is_cellular_internet_profile(profile)) {
758                                 DBG("connection is not a internet profile - stop to update the cellular state");
759                                 if (old_ip)
760                                         free(old_ip);
761                                 if (old_ip6)
762                                         free(old_ip6);
763                                 if (old_proxy)
764                                         free(old_proxy);
765                                 return;
766                         }
767
768                         netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_CELLULAR);
769
770                         netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
771                                 EKEY_NETWORK_STATUS, EVAL_NETWORK_CELLULAR);
772                 } else if (netconfig_is_ethernet_profile(profile) == TRUE) {
773                         netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_ETHERNET);
774                         netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
775                                 EKEY_NETWORK_STATUS, EVAL_NETWORK_ETHERNET);
776                 } else if (netconfig_is_bluetooth_profile(profile) == TRUE) {
777                         netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_BLUETOOTH);
778                         netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
779                                 EKEY_NETWORK_STATUS, EVAL_NETWORK_BT);
780                 } else{
781                         netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
782                         netconfig_set_system_event(SYS_EVT_NETWORK_STATUS,
783                                 EKEY_NETWORK_STATUS, EVAL_NETWORK_DISCONNECTED);
784                 }
785
786                 if (g_strcmp0(old_ip, ip_addr) != 0 || old_ip == NULL) {
787                         if (ip_addr != NULL)
788                                 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr);
789                         else if (old_ip != NULL && strlen(old_ip) > 0)
790                                 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
791                 }
792                 if (old_ip)
793                         free(old_ip);
794
795                 if (g_strcmp0(old_ip6, ip_addr6) != 0 || old_ip6 == NULL) {
796                         if (ip_addr6 != NULL)
797                                 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, ip_addr6);
798                         else if (old_ip6 != NULL && strlen(old_ip6) > 0)
799                                 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP6, "");
800                 }
801                 if (old_ip6)
802                         free(old_ip6);
803
804                 if (g_strcmp0(old_proxy, proxy_addr) != 0) {
805                         if (proxy_addr == NULL)
806                                 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
807                         else
808                                 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, proxy_addr);
809                 }
810                 if (old_proxy)
811                         free(old_proxy);
812
813                 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 1);
814
815                 DBG("Successfully update default network configuration");
816         }
817
818         __netconfig_adjust_tcp_buffer_size();
819 }
820
821 static gboolean __netconfig_is_tech_state_connected(void)
822 {
823         gboolean ret = FALSE;
824         GVariant *message = NULL, *variant;
825         GVariantIter *iter, *next;
826         gchar *path;
827         gchar *key;
828
829         message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
830                         CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
831                         "GetTechnologies", NULL);
832
833         if (message == NULL) {
834                 DBG("Fail to get technology state");
835                 return FALSE;
836         }
837
838         g_variant_get(message, "(a(oa{sv}))", &iter);
839         while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
840                 if (path == NULL)
841                         continue;
842
843                 while (g_variant_iter_loop(next, "{sv}", &key, &variant)) {
844                         gboolean data;
845                         if (g_strcmp0(key, "Connected") == 0) {
846                                 data = g_variant_get_boolean(variant);
847                                 DBG("%s [%s: %s]", path, key, data ? "True" : "False");
848                                 if (TRUE == data) {
849                                         ret = TRUE;
850                                         g_free(path);
851                                         g_free(key);
852                                         g_variant_unref(variant);
853                                         g_variant_iter_free(next);
854                                         goto done;
855                                 }
856                         }
857                 }
858         }
859
860 done:
861         g_variant_iter_free(iter);
862         g_variant_unref(message);
863
864         return ret;
865 }
866
867 static void __netconfig_update_if_service_connected(void)
868 {
869         GVariant *message = NULL, *var;
870         GVariantIter *iter, *next;
871         gchar *path;
872         gchar *key;
873
874         message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
875                         CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
876                         "GetServices", NULL);
877
878         if (message == NULL) {
879                 ERR("Failed to get services");
880                 return;
881         }
882
883         g_variant_get(message, "(a(oa{sv}))", &iter);
884         while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
885                 if (path == NULL)
886                         continue;
887
888                 if (g_str_has_prefix(path,
889                                                 CONNMAN_WIFI_SERVICE_PROFILE_PREFIX) == TRUE) {
890                         if (g_strrstr(path + strlen(CONNMAN_WIFI_SERVICE_PROFILE_PREFIX),
891                                                         "hidden") != NULL) {
892                                 /* skip hidden profiles */
893                                 continue;
894                         }
895                         /* Process this */
896                 } else if (g_str_has_prefix(path,
897                                                 CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX) == TRUE) {
898                         /* Process this */
899                 } else {
900                         continue;
901                 }
902
903                 while (g_variant_iter_loop(next, "{sv}", &key, &var)) {
904                         if (g_strcmp0(key, "State") == 0) {
905                                 const gchar *sdata = NULL;
906                                 sdata = g_variant_get_string(var, NULL);
907                                 DBG("%s [%s: %s]", path, key, sdata);
908
909                                 if (g_strcmp0(sdata, "online") == 0 || g_strcmp0(sdata, "ready") == 0) {
910
911                                         /* Found a connected WiFi / 3G service.
912                                          * Lets update the default profile info.
913                                          */
914                                         netconfig_update_default_profile((const gchar*)path);
915                                         g_free(key);
916                                         g_free(path);
917                                         g_variant_unref(var);
918                                         g_variant_iter_free(next);
919                                         goto done;
920                                 }
921                         }
922                 }
923         }
924 done:
925         g_variant_iter_free(iter);
926         g_variant_unref(message);
927
928         return;
929 }
930
931 static void __netconfig_network_notify_result(const char *sig_name, const char *key)
932 {
933         GVariantBuilder *builder;
934         GVariant *params;
935
936         INFO("[Signal] %s %s", sig_name, key);
937
938         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
939         g_variant_builder_add(builder, "{sv}", "key", g_variant_new_string(key));
940
941         params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
942         g_variant_builder_unref(builder);
943
944         netconfig_dbus_emit_signal(NULL,
945                                 NETCONFIG_NETWORK_PATH,
946                                 NETCONFIG_NETWORK_INTERFACE,
947                                 sig_name,
948                                 params);
949
950         return;
951 }
952
953 const char *netconfig_get_default_profile(void)
954 {
955         return netconfig_default_connection_info.profile;
956 }
957
958 const char *netconfig_get_default_ifname(void)
959 {
960         return netconfig_default_connection_info.ifname;
961 }
962
963 const char *netconfig_get_default_ipaddress(void)
964 {
965         return netconfig_default_connection_info.ipaddress;
966 }
967
968 const char *netconfig_get_default_ipaddress6(void)
969 {
970         return netconfig_default_connection_info.ipaddress6;
971 }
972
973 const char *netconfig_get_default_proxy(void)
974 {
975         return netconfig_default_connection_info.proxy;
976 }
977
978 unsigned int netconfig_get_default_frequency(void)
979 {
980         return netconfig_default_connection_info.freq;
981 }
982
983 const char *netconfig_wifi_get_connected_essid(const char *default_profile)
984 {
985         if (default_profile == NULL)
986                 return NULL;
987
988         if (netconfig_is_wifi_profile(default_profile) != TRUE)
989                 return NULL;
990
991         if (g_strcmp0(default_profile, netconfig_default_connection_info.profile) != 0)
992                 return NULL;
993
994         return netconfig_default_connection_info.essid;
995 }
996
997 gboolean netconfig_get_default_is_metered(void)
998 {
999         return netconfig_default_connection_info.is_metered;
1000 }
1001
1002 static int __netconfig_reset_ipv4_socket(void)
1003 {
1004         int ret;
1005         int fd;
1006         struct ifreq ifr;
1007         struct sockaddr_in sai;
1008         const char *ipaddr = netconfig_get_default_ipaddress();
1009         DBG("ipaddr-[%s]", ipaddr);
1010
1011         if (!ipaddr)
1012                 return -1;
1013
1014         fd = socket(AF_INET, SOCK_DGRAM, 0);
1015         if (fd < 0)
1016                 return -1;
1017
1018         memset(&sai, 0, sizeof(struct sockaddr_in));
1019         sai.sin_family = AF_INET;
1020         sai.sin_port = 0;
1021         if (!inet_aton(ipaddr, &sai.sin_addr)) {
1022                 DBG("fail to inet_aton()");
1023                 close(fd);
1024                 return -1;
1025         }
1026
1027         memset(&ifr, 0, sizeof(struct ifreq));
1028         memcpy(&ifr.ifr_addr, &sai, sizeof(sai));
1029         g_strlcpy((char *)ifr.ifr_name, WIFI_IFNAME, IFNAMSIZ);
1030
1031 #ifndef SIOCKILLADDR
1032 #define SIOCKILLADDR    0x8939
1033 #endif
1034
1035         ret = ioctl(fd, SIOCKILLADDR, &ifr);
1036         if (ret < 0) {
1037                 DBG("fail to ioctl[SIOCKILLADDR]");
1038                 close(fd);
1039                 return -1;
1040         }
1041
1042         close(fd);
1043         return 0;
1044 }
1045
1046 void netconfig_update_default_profile(const char *profile)
1047 {
1048         static char *old_profile = NULL;
1049
1050         /* It's automatically updated by signal-handler
1051          * DO NOT update manually
1052          *
1053          * It is going to update default connection information
1054          */
1055
1056         if (netconfig_default_connection_info.profile != NULL) {
1057
1058                 if (netconfig_is_wifi_profile(netconfig_default_connection_info.profile))
1059                         __netconfig_reset_ipv4_socket();
1060
1061                 g_free(old_profile);
1062                 old_profile = strdup(netconfig_default_connection_info.profile);
1063
1064                 g_free(netconfig_default_connection_info.profile);
1065                 netconfig_default_connection_info.profile = NULL;
1066
1067                 g_free(netconfig_default_connection_info.ifname);
1068                 netconfig_default_connection_info.ifname = NULL;
1069
1070                 g_free(netconfig_default_connection_info.ipaddress);
1071                 netconfig_default_connection_info.ipaddress = NULL;
1072
1073                 g_free(netconfig_default_connection_info.ipaddress6);
1074                 netconfig_default_connection_info.ipaddress6 = NULL;
1075
1076                 g_free(netconfig_default_connection_info.proxy);
1077                 netconfig_default_connection_info.proxy = NULL;
1078
1079                 netconfig_default_connection_info.freq = 0;
1080                 netconfig_default_connection_info.is_metered = FALSE;
1081
1082                 g_free(netconfig_default_connection_info.essid);
1083                 netconfig_default_connection_info.essid = NULL;
1084         }
1085
1086         /* default profile is NULL and new connected profile is NULL */
1087         if (!profile) {
1088                 char *tmp_profile = __netconfig_get_default_profile();
1089
1090                 if (tmp_profile && netconfig_is_cellular_profile(tmp_profile) &&
1091                         !netconfig_is_cellular_internet_profile(tmp_profile)) {
1092                         DBG("not a default cellular profile");
1093                         g_free(tmp_profile);
1094                         tmp_profile = NULL;
1095                 }
1096
1097                 if (!tmp_profile) {
1098                         __netconfig_update_default_connection_info();
1099                         return;
1100                 }
1101
1102                 netconfig_default_connection_info.profile = g_strdup(tmp_profile);
1103                 __netconfig_get_default_connection_info(tmp_profile);
1104                 __netconfig_update_default_connection_info();
1105                 g_free(tmp_profile);
1106                 return;
1107         }
1108
1109         netconfig_default_connection_info.profile = g_strdup(profile);
1110         __netconfig_get_default_connection_info(profile);
1111         __netconfig_update_default_connection_info();
1112 }
1113
1114 void netconfig_update_default(void)
1115 {
1116         if (__netconfig_is_tech_state_connected() == TRUE)
1117                 __netconfig_update_if_service_connected();
1118         else
1119                 __netconfig_adjust_tcp_buffer_size();
1120 }
1121
1122 char *netconfig_get_ifname(const char *profile)
1123 {
1124         GVariant *message = NULL, *variant;
1125         GVariantIter *iter, *next, *service;
1126         gchar *obj_path;
1127         gchar *key;
1128         gchar *key1;
1129         const gchar *value = NULL;
1130         gchar *ifname = NULL;
1131         gboolean found_profile = 0;
1132
1133         if (profile == NULL)
1134                 return NULL;
1135
1136         message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
1137                         CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
1138                         "GetServices", NULL);
1139         if (message == NULL) {
1140                 ERR("Failed to get services informations");
1141                 return NULL;
1142         }
1143
1144         g_variant_get(message, "(a(oa{sv}))", &service);
1145         while (g_variant_iter_loop(service, "(oa{sv})", &obj_path, &iter)) {
1146                 if (g_strcmp0(obj_path, profile) == 0) {
1147                         g_free(obj_path);
1148                         found_profile = 1;
1149                         break;
1150                 }
1151         }
1152
1153         if (found_profile == 0) {
1154                 ERR("Profile %s doesn't exist", profile);
1155                 g_variant_iter_free(service);
1156                 g_variant_unref(message);
1157                 return NULL;
1158         }
1159
1160         while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
1161                 if (g_strcmp0(key, "Ethernet") == 0) {
1162                         while (g_variant_iter_loop(next, "{sv}", &key1, &variant)) {
1163                                 if (g_strcmp0(key1, "Interface") == 0) {
1164                                         value = g_variant_get_string(variant, NULL);
1165                                         if (!ifname)
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 }