ADD method for launching mdnsresponder
[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 #include <ITapiSim.h>
30 #include <TapiUtility.h>
31 #include <bundle.h>
32 #include <bundle_internal.h>
33 #include <eventsystem.h>
34
35 #include "log.h"
36 #include "util.h"
37 #include "netdbus.h"
38 #include "neterror.h"
39 #include "emulator.h"
40 #include "wifi-state.h"
41 #include "wifi-power.h"
42 #include "network-state.h"
43 #include "network-monitor.h"
44 #include "netsupplicant.h"
45 #include "wifi-tel-intf.h"
46 #include "clatd-handler.h"
47
48 #include "generated-code.h"
49 /* Define TCP buffer sizes for various networks */
50 /* ReadMin, ReadInitial, ReadMax */ /* WriteMin, WriteInitial, WriteMax */
51 #define NET_TCP_BUFFERSIZE_DEFAULT_READ         "4096 87380 704512"
52 #define NET_TCP_BUFFERSIZE_DEFAULT_WRITE        "4096 16384 110208"
53 #define NET_TCP_BUFFERSIZE_WIFI_READ            "524288 1048576 2560000"
54 #define NET_TCP_BUFFERSIZE_WIFI_WRITE           "524288 1048576 2560000"
55 #define NET_TCP_BUFFERSIZE_LTE_READ             "524288 1048576 2560000"
56 #define NET_TCP_BUFFERSIZE_LTE_WRITE            "524288 1048576 2560000"
57 #define NET_TCP_BUFFERSIZE_UMTS_READ            "4094 87380 704512"
58 #define NET_TCP_BUFFERSIZE_UMTS_WRITE           "4096 16384 110208"
59 #define NET_TCP_BUFFERSIZE_HSPA_READ            "4092 87380 704512"
60 #define NET_TCP_BUFFERSIZE_HSPA_WRITE           "4096 16384 262144"
61 #define NET_TCP_BUFFERSIZE_HSDPA_READ           "4092 87380 704512"
62 #define NET_TCP_BUFFERSIZE_HSDPA_WRITE          "4096 16384 262144"
63 #define NET_TCP_BUFFERSIZE_HSUPA_READ           "4092 87380 704512"
64 #define NET_TCP_BUFFERSIZE_HSUPA_WRITE          "4096 16384 262144"
65 #define NET_TCP_BUFFERSIZE_HSPAP_READ           "4092 87380 1220608"
66 #define NET_TCP_BUFFERSIZE_HSPAP_WRITE          "4096 16384 1220608"
67 #define NET_TCP_BUFFERSIZE_EDGE_READ            "4093 26280 35040"
68 #define NET_TCP_BUFFERSIZE_EDGE_WRITE           "4096 16384 35040"
69 #define NET_TCP_BUFFERSIZE_GPRS_READ            "4096 30000 30000"
70 #define NET_TCP_BUFFERSIZE_GPRS_WRITE           "4096 8760 11680"
71
72 #define NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX        "1048576"
73 #define NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX        "2097152"
74 #define NET_TCP_BUFFERSIZE_LTE_RMEM_MAX         "5242880"
75
76 #define NET_TCP_BUFFERSIZE_WIFID_WMEM_MAX       "2097152"
77
78 #define NET_PROC_SYS_NET_IPV4_TCP_RMEM          "/proc/sys/net/ipv4/tcp_rmem"
79 #define NET_PROC_SYS_NET_IPv4_TCP_WMEM          "/proc/sys/net/ipv4/tcp_wmem"
80 #define NET_PROC_SYS_NET_CORE_RMEM_MAX          "/proc/sys/net/core/rmem_max"
81 #define NET_PROC_SYS_NET_CORE_WMEM_MAX          "/proc/sys/net/core/wmem_max"
82
83 #define ROUTE_EXEC_PATH                                         "/sbin/route"
84
85 static Network *netconfigstate = NULL;
86
87 struct netconfig_default_connection {
88         char *profile;
89         char *ifname;
90         char *ipaddress;
91         char *ipaddress6;
92         char *proxy;
93         char *essid;
94         unsigned int freq;
95 };
96
97 static struct netconfig_default_connection
98                                 netconfig_default_connection_info = { NULL, };
99
100 gboolean netconfig_iface_network_state_ethernet_cable_state(gint32 *state);
101
102 static gboolean __netconfig_is_connected(GVariantIter *array)
103 {
104         gboolean is_connected = FALSE;
105         GVariant *variant = NULL;
106         gchar *key = NULL;
107         const gchar *value = NULL;
108
109         while (g_variant_iter_loop(array, "{sv}", &key, &variant)) {
110                 if (g_strcmp0(key, "State") != 0)
111                         continue;
112
113                 if (g_variant_is_of_type(variant, G_VARIANT_TYPE_STRING)) {
114                         value = g_variant_get_string(variant, NULL);
115                         if (g_strcmp0(value, "ready") == 0 || g_strcmp0(value, "online") == 0)
116                                 is_connected = TRUE;
117                 }
118
119                 g_free(key);
120                 g_variant_unref(variant);
121                 break;
122         }
123
124         return is_connected;
125 }
126
127 static char *__netconfig_get_default_profile(void)
128 {
129         GVariant *message = NULL;
130         GVariantIter *iter;
131         GVariantIter *next;
132         gchar *default_profile = NULL;
133         gchar *object_path;
134
135         message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
136                         CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
137                         "GetServices", NULL);
138         if (message == NULL) {
139                 ERR("Failed to get profiles");
140                 return NULL;
141         }
142
143         g_variant_get(message, "(a(oa{sv}))", &iter);
144         while (g_variant_iter_loop(iter, "(oa{sv})", &object_path, &next)) {
145                 if (object_path == NULL)
146                         continue;
147
148                 if (netconfig_is_cellular_profile(object_path) && !netconfig_is_cellular_internet_profile(object_path))
149                         continue;
150
151                 if (__netconfig_is_connected(next) == TRUE) {
152                         default_profile = g_strdup(object_path);
153                         g_free(object_path);
154                         g_variant_iter_free(next);
155                         break;
156                 }
157         }
158         g_variant_iter_free(iter);
159         g_variant_unref(message);
160
161         return default_profile;
162 }
163
164 static void __netconfig_get_default_connection_info(const char *profile)
165 {
166         GVariant *message = NULL, *variant = NULL, *variant2 = NULL;
167         GVariantIter *iter = NULL, *iter1 = NULL;
168         GVariant *next = NULL;
169         gchar *key = NULL;
170         gchar *key1 = NULL;
171         gchar *key2 = NULL;
172
173         message = netconfig_invoke_dbus_method(CONNMAN_SERVICE, profile,
174                         CONNMAN_SERVICE_INTERFACE, "GetProperties", NULL);
175         if (message == NULL) {
176                 ERR("Failed to get service properties");
177                 goto done;
178         }
179
180         g_variant_get(message, "(a{sv})", &iter);
181         while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
182                 const gchar *value = NULL;
183                 guint16 freq = 0;
184                 if (g_strcmp0(key, "Name") == 0 &&
185                                 netconfig_is_wifi_profile(profile) == TRUE) {
186                         if (g_variant_is_of_type(next, G_VARIANT_TYPE_STRING)) {
187                                 value = g_variant_get_string(next, NULL);
188
189                                 netconfig_default_connection_info.essid = g_strdup(value);
190                         }
191                 } else if (g_strcmp0(key, "Ethernet") == 0) {
192                         g_variant_get(next, "a{sv}", &iter1);
193                         while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
194                                 if (g_strcmp0(key1, "Interface") == 0) {
195                                         value = g_variant_get_string(variant, NULL);
196                                         netconfig_default_connection_info.ifname = g_strdup(value);
197                                 }
198                         }
199                 } else if (g_strcmp0(key, "IPv4") == 0) {
200                         g_variant_get(next, "a{sv}", &iter1);
201                         while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
202                                 if (g_strcmp0(key1, "Address") == 0) {
203                                         value = g_variant_get_string(variant, NULL);
204                                         netconfig_default_connection_info.ipaddress = g_strdup(value);
205                                 }
206                         }
207                 } else if (g_strcmp0(key, "IPv6") == 0) {
208                         g_variant_get(next, "a{sv}", &iter1);
209                         while (g_variant_iter_loop(iter1, "{sv}", &key1, &variant)) {
210                                 if (g_strcmp0(key1, "Address") == 0) {
211                                         value = g_variant_get_string(variant, NULL);
212                                         netconfig_default_connection_info.ipaddress6 = g_strdup(value);
213                                 }
214                         }
215                 } else if (g_strcmp0(key, "Proxy") == 0) {
216                         g_variant_get(next, "a{sv}", &iter1);
217                         while (g_variant_iter_loop(iter1, "{sv}", &key2, &variant2)) {
218                                 GVariantIter *iter_sub = NULL;
219
220                                 if (g_strcmp0(key2, "Servers") == 0) {
221                                         if (!g_variant_is_of_type(variant2, G_VARIANT_TYPE_STRING_ARRAY)) {
222                                                 g_free(key2);
223                                                 g_variant_unref(variant2);
224                                                 break;
225                                         }
226
227                                         g_variant_get(variant2, "as", &iter_sub);
228                                         g_variant_iter_loop(iter_sub, "s", &value);
229                                         g_variant_iter_free(iter_sub);
230                                         if (value != NULL && (strlen(value) > 0))
231                                                 netconfig_default_connection_info.proxy = g_strdup(value);
232                                 } else if (g_strcmp0(key2, "Method") == 0) {
233                                         if (g_variant_is_of_type(variant2, G_VARIANT_TYPE_STRING)) {
234                                                 g_free(key2);
235                                                 g_variant_unref(variant2);
236                                                 break;
237                                         }
238
239                                         value = g_variant_get_string(variant2, NULL);
240                                         if (g_strcmp0(value, "direct") == 0) {
241                                                 g_free(netconfig_default_connection_info.proxy);
242                                                 netconfig_default_connection_info.proxy = NULL;
243
244                                                 g_free(key2);
245                                                 g_variant_unref(variant2);
246                                                 break;
247                                         }
248                                 }
249                         }
250                 } else if (g_strcmp0(key, "Frequency") == 0) {
251                         if (g_variant_is_of_type(next, G_VARIANT_TYPE_UINT16)) {
252                                 freq = g_variant_get_uint16(next);
253                                 netconfig_default_connection_info.freq = freq;
254                         }
255                 }
256         }
257
258 done:
259         if (message)
260                 g_variant_unref(message);
261
262         if (iter)
263                 g_variant_iter_free(iter);
264
265         if (iter1)
266                 g_variant_iter_free(iter1);
267
268         return;
269 }
270
271 static void __netconfig_adjust_tcp_buffer_size(void)
272 {
273         int fdr = 0, fdw = 0;
274         int fdrmax = 0, fdwmax = 0;
275         const char *rbuf_size = NULL;
276         const char *wbuf_size = NULL;
277         const char *rmax_size = NULL;
278         const char *wmax_size = NULL;
279         const char *profile = netconfig_get_default_profile();
280
281         if (profile == NULL) {
282                 DBG("There is no default connection");
283
284                 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
285                 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
286         } else if (netconfig_is_wifi_profile(profile) == TRUE) {
287                 DBG("Default connection: Wi-Fi");
288
289                 rbuf_size = NET_TCP_BUFFERSIZE_WIFI_READ;
290                 wbuf_size = NET_TCP_BUFFERSIZE_WIFI_WRITE;
291                 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
292                 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
293         } else if (netconfig_is_cellular_profile(profile) == TRUE) {
294                 TapiHandle *tapi_handle = NULL;
295                 int telephony_svctype = 0, telephony_pstype = 0;
296
297                 tapi_handle = (TapiHandle *)netconfig_tel_init();
298                 if (NULL != tapi_handle) {
299                         tel_get_property_int(tapi_handle,
300                                         TAPI_PROP_NETWORK_SERVICE_TYPE,
301                                         &telephony_svctype);
302                         tel_get_property_int(tapi_handle, TAPI_PROP_NETWORK_PS_TYPE,
303                                         &telephony_pstype);
304                         netconfig_tel_deinit();
305                 }
306
307                 DBG("Default cellular %d, %d", telephony_svctype, telephony_pstype);
308
309                 switch (telephony_pstype) {
310                 case VCONFKEY_TELEPHONY_PSTYPE_HSPA:
311                         rbuf_size = NET_TCP_BUFFERSIZE_HSPA_READ;
312                         wbuf_size = NET_TCP_BUFFERSIZE_HSPA_WRITE;
313                         break;
314                 case VCONFKEY_TELEPHONY_PSTYPE_HSUPA:
315                         rbuf_size = NET_TCP_BUFFERSIZE_HSUPA_READ;
316                         wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
317                         break;
318                 case VCONFKEY_TELEPHONY_PSTYPE_HSDPA:
319                         rbuf_size = NET_TCP_BUFFERSIZE_HSDPA_READ;
320                         wbuf_size = NET_TCP_BUFFERSIZE_HSDPA_WRITE;
321                         break;
322 #if !defined TIZEN_WEARABLE
323                 case VCONFKEY_TELEPHONY_PSTYPE_HSPAP:
324                         rbuf_size = NET_TCP_BUFFERSIZE_HSPAP_READ;
325                         wbuf_size = NET_TCP_BUFFERSIZE_HSPAP_WRITE;
326                         break;
327 #endif
328                 default:
329                         switch (telephony_svctype) {
330                         case VCONFKEY_TELEPHONY_SVCTYPE_LTE:
331                                 rbuf_size = NET_TCP_BUFFERSIZE_LTE_READ;
332                                 wbuf_size = NET_TCP_BUFFERSIZE_LTE_WRITE;
333                                 rmax_size = NET_TCP_BUFFERSIZE_LTE_RMEM_MAX;
334                                 break;
335                         case VCONFKEY_TELEPHONY_SVCTYPE_3G:
336                                 rbuf_size = NET_TCP_BUFFERSIZE_UMTS_READ;
337                                 wbuf_size = NET_TCP_BUFFERSIZE_UMTS_WRITE;
338                                 break;
339                         case VCONFKEY_TELEPHONY_SVCTYPE_2_5G_EDGE:
340                                 rbuf_size = NET_TCP_BUFFERSIZE_EDGE_READ;
341                                 wbuf_size = NET_TCP_BUFFERSIZE_EDGE_WRITE;
342                                 break;
343                         case VCONFKEY_TELEPHONY_SVCTYPE_2_5G:
344                                 rbuf_size = NET_TCP_BUFFERSIZE_GPRS_READ;
345                                 wbuf_size = NET_TCP_BUFFERSIZE_GPRS_WRITE;
346                                 break;
347                         default:
348                                 /* TODO: Check LTE support */
349                                 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
350                                 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
351                                 break;
352                         }
353                         break;
354                 }
355         } else {
356                 DBG("Default TCP buffer configured");
357
358                 rbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_READ;
359                 wbuf_size = NET_TCP_BUFFERSIZE_DEFAULT_WRITE;
360         }
361
362         if (rbuf_size != NULL) {
363                 fdr = open(NET_PROC_SYS_NET_IPV4_TCP_RMEM, O_RDWR | O_CLOEXEC);
364
365                 if (fdr < 0 || write(fdr, rbuf_size, strlen(rbuf_size)) < 0)
366                         ERR("Failed to set TCP read buffer size");
367
368                 if (fdr >= 0)
369                         close(fdr);
370         }
371
372         if (wbuf_size != NULL) {
373                 fdw = open(NET_PROC_SYS_NET_IPv4_TCP_WMEM, O_RDWR | O_CLOEXEC);
374
375                 if (fdw < 0 || write(fdw, wbuf_size, strlen(wbuf_size)) < 0)
376                         ERR("Failed to set TCP write buffer size");
377
378                 if (fdw >= 0)
379                         close(fdw);
380         }
381
382         /* As default */
383         if (rmax_size == NULL)
384                 rmax_size = NET_TCP_BUFFERSIZE_WIFI_RMEM_MAX;
385         if (wmax_size == NULL)
386                 wmax_size = NET_TCP_BUFFERSIZE_WIFI_WMEM_MAX;
387
388         if (rmax_size != NULL) {
389                 fdrmax = open(NET_PROC_SYS_NET_CORE_RMEM_MAX, O_RDWR | O_CLOEXEC);
390
391                 if (fdrmax < 0 || write(fdrmax, rmax_size, strlen(rmax_size)) < 0)
392                         ERR("Failed to set TCP rmem_max size");
393
394                 if (fdrmax >= 0)
395                         close(fdrmax);
396         }
397
398         if (wmax_size != NULL) {
399                 fdwmax = open(NET_PROC_SYS_NET_CORE_WMEM_MAX, O_RDWR | O_CLOEXEC);
400
401                 if (fdwmax < 0 || write(fdwmax, wmax_size, strlen(wmax_size)) < 0)
402                         ERR("Failed to set TCP wmem_max size");
403
404                 if (fdwmax >= 0)
405                         close(fdwmax);
406         }
407 }
408
409 static void __netconfig_update_default_connection_info(void)
410 {
411         int old_network_status = 0;
412         const char *profile = netconfig_get_default_profile();
413         const char *ip_addr = netconfig_get_default_ipaddress();
414         const char *ip_addr6 = netconfig_get_default_ipaddress6();
415         const char *proxy_addr = netconfig_get_default_proxy();
416         unsigned int freq = netconfig_get_default_frequency();
417
418         if (emulator_is_emulated() == TRUE)
419                 return;
420
421         if (profile == NULL)
422                 DBG("Reset network state configuration");
423         else
424                 DBG("%s: ip(%s) proxy(%s)", profile, ip_addr, proxy_addr);
425
426         vconf_get_int(VCONFKEY_NETWORK_STATUS, &old_network_status);
427
428         if (profile == NULL && old_network_status != VCONFKEY_NETWORK_OFF) {
429                 netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
430
431                 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
432                 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
433
434                 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 0);
435                 netconfig_set_vconf_int("memory/private/wifi/frequency", 0);
436
437                 DBG("Successfully clear IP and PROXY up");
438
439                 /* Disable clatd if it is in running state */
440                 netconfig_clatd_disable();
441         } else if (profile != NULL) {
442                 char *old_ip = vconf_get_str(VCONFKEY_NETWORK_IP);
443                 char *old_proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY);
444
445                 if (netconfig_is_wifi_profile(profile) == TRUE) {
446                         netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_WIFI);
447                         netconfig_set_vconf_int("memory/private/wifi/frequency", freq);
448
449                         netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
450                                 EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_WIFI);
451                 } else if (netconfig_is_cellular_profile(profile)) {
452
453                         if (!netconfig_is_cellular_internet_profile(profile)) {
454                                 DBG("connection is not a internet profile - stop to update the cellular state");
455                                 return;
456                         }
457
458                         netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_CELLULAR);
459
460                         netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
461                                 EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_CELLULAR);
462
463                         /* Enable clatd if IPv6 is set and no IPv4 address */
464                         if (!ip_addr && ip_addr6)
465                                 netconfig_clatd_enable();
466                 } else if (netconfig_is_ethernet_profile(profile) == TRUE) {
467                         netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_ETHERNET);
468                         netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
469                                 EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_ETHERNET);
470                 } else if (netconfig_is_bluetooth_profile(profile) == TRUE) {
471                         netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_BLUETOOTH);
472                         netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
473                                 EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_BT);
474                 } else{
475                         netconfig_set_vconf_int(VCONFKEY_NETWORK_STATUS, VCONFKEY_NETWORK_OFF);
476                         netconfig_set_system_event(SYS_EVENT_NETWORK_STATUS,
477                                 EVT_KEY_NETWORK_STATUS, EVT_VAL_NETWORK_DISCONNECTED);
478                 }
479
480                 if (g_strcmp0(old_ip, ip_addr) != 0 || old_ip == NULL) {
481                         if (ip_addr != NULL)
482                                 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr);
483                         else if (ip_addr6 != NULL)
484                                 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, ip_addr6);
485                         else
486                                 netconfig_set_vconf_str(VCONFKEY_NETWORK_IP, "");
487                 }
488                 g_free(old_ip);
489
490                 if (g_strcmp0(old_proxy, proxy_addr) != 0) {
491                         if (proxy_addr == NULL)
492                                 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, "");
493                         else
494                                 netconfig_set_vconf_str(VCONFKEY_NETWORK_PROXY, proxy_addr);
495                 }
496                 g_free(old_proxy);
497
498                 netconfig_set_vconf_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, 1);
499
500                 DBG("Successfully update default network configuration");
501
502                 /* Disable clatd if it is in running state */
503                 if (netconfig_is_cellular_profile(profile) != TRUE)
504                         netconfig_clatd_disable();
505         }
506
507         __netconfig_adjust_tcp_buffer_size();
508 }
509
510 static gboolean __netconfig_is_tech_state_connected(void)
511 {
512         gboolean ret = FALSE;
513         GVariant *message = NULL, *variant;
514         GVariantIter *iter, *next;
515         gchar *path;
516         gchar *key;
517
518         message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
519                         CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
520                         "GetTechnologies", NULL);
521
522         if (message == NULL) {
523                 DBG("Fail to get technology state");
524                 return FALSE;
525         }
526
527         g_variant_get(message, "(a(oa{sv}))", &iter);
528         while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
529                 if (path == NULL)
530                         continue;
531
532                 while (g_variant_iter_loop(next, "{sv}", &key, &variant)) {
533                         gboolean data;
534                         if (g_strcmp0(key, "Connected") == 0) {
535                                 data = g_variant_get_boolean(variant);
536                                 DBG("%s [%s: %s]", path, key, data ? "True" : "False");
537                                 if (TRUE == data) {
538                                         ret = TRUE;
539                                         g_free(path);
540                                         g_free(key);
541                                         g_variant_unref(variant);
542                                         g_variant_iter_free(next);
543                                         goto done;
544                                 }
545                         }
546                 }
547         }
548
549 done:
550         g_variant_iter_free(iter);
551         g_variant_unref(message);
552
553         return ret;
554 }
555
556 static void __netconfig_update_if_service_connected(void)
557 {
558         GVariant *message = NULL, *var;
559         GVariantIter *iter, *next;
560         gchar *path;
561         gchar *key;
562
563         message = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
564                         CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE,
565                         "GetServices", NULL);
566
567         if (message == NULL) {
568                 ERR("Failed to get services");
569                 return;
570         }
571
572         g_variant_get(message, "(a(oa{sv}))", &iter);
573         while (g_variant_iter_loop(iter, "(oa{sv})", &path, &next)) {
574                 if (path == NULL)
575                         continue;
576
577                 if (g_str_has_prefix(path,
578                                                 CONNMAN_WIFI_SERVICE_PROFILE_PREFIX) == TRUE) {
579                         if (g_strrstr(path + strlen(CONNMAN_WIFI_SERVICE_PROFILE_PREFIX),
580                                                         "hidden") != NULL) {
581                                 /* skip hidden profiles */
582                                 continue;
583                         }
584                         /* Process this */
585                 } else if (g_str_has_prefix(path,
586                                                 CONNMAN_CELLULAR_SERVICE_PROFILE_PREFIX) == TRUE) {
587                         /* Process this */
588                 } else {
589                         continue;
590                 }
591
592                 while (g_variant_iter_loop(next, "{sv}", &key, &var)) {
593                         if (g_strcmp0(key, "State") == 0) {
594                                 const gchar *sdata = NULL;
595                                 sdata = g_variant_get_string(var, NULL);
596                                 DBG("%s [%s: %s]", path, key, sdata);
597
598                                 if (g_strcmp0(sdata, "online") == 0 || g_strcmp0(sdata, "ready") == 0) {
599
600                                         /* Found a connected WiFi / 3G service.
601                                          * Lets update the default profile info.
602                                          */
603                                         netconfig_update_default_profile((const gchar*)path);
604                                         g_free(key);
605                                         g_free(path);
606                                         g_variant_unref(var);
607                                         g_variant_iter_free(next);
608                                         goto done;
609                                 }
610                         }
611                 }
612         }
613 done:
614         g_variant_iter_free(iter);
615         g_variant_unref(message);
616
617         return;
618 }
619
620 static void __netconfig_network_notify_result(const char *sig_name, const char *key)
621 {
622         gboolean reply;
623         GVariant *params;
624         GVariantBuilder *builder = NULL;
625         GDBusConnection *connection = NULL;
626         GError *error = NULL;
627         const char *prop_key = "key";
628
629         INFO("[Signal] %s %s", sig_name, key);
630
631         connection = netdbus_get_connection();
632         if (connection == NULL) {
633                 ERR("Failed to get GDBus Connection");
634                 return;
635         }
636
637         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
638         g_variant_builder_add(builder, "{sv}", prop_key, g_variant_new_string(key));
639         params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
640
641         g_variant_builder_unref(builder);
642
643         reply = g_dbus_connection_emit_signal(connection,
644                         NULL,
645                         NETCONFIG_NETWORK_PATH,
646                         NETCONFIG_NETWORK_INTERFACE,
647                         sig_name,
648                         params,
649                         &error);
650
651         if (reply != TRUE) {
652                 if (error != NULL) {
653                         ERR("Failed to send signal [%s]", error->message);
654                         g_error_free(error);
655                 }
656                 return;
657         }
658
659         INFO("Sent signal (%s), key (%s)", sig_name, key);
660         return;
661 }
662
663 const char *netconfig_get_default_profile(void)
664 {
665         return netconfig_default_connection_info.profile;
666 }
667
668 const char *netconfig_get_default_ifname(void)
669 {
670         return netconfig_default_connection_info.ifname;
671 }
672
673 const char *netconfig_get_default_ipaddress(void)
674 {
675         return netconfig_default_connection_info.ipaddress;
676 }
677
678 const char *netconfig_get_default_ipaddress6(void)
679 {
680         return netconfig_default_connection_info.ipaddress6;
681 }
682
683 const char *netconfig_get_default_proxy(void)
684 {
685         return netconfig_default_connection_info.proxy;
686 }
687
688 unsigned int netconfig_get_default_frequency(void)
689 {
690         return netconfig_default_connection_info.freq;
691 }
692
693 const char *netconfig_wifi_get_connected_essid(const char *default_profile)
694 {
695         if (default_profile == NULL)
696                 return NULL;
697
698         if (netconfig_is_wifi_profile(default_profile) != TRUE)
699                 return NULL;
700
701         if (g_strcmp0(default_profile, netconfig_default_connection_info.profile) != 0)
702                 return NULL;
703
704         return netconfig_default_connection_info.essid;
705 }
706
707 static int __netconfig_reset_ipv4_socket(void)
708 {
709         int ret;
710         int fd;
711         struct ifreq ifr;
712         struct sockaddr_in sai;
713         const char *ipaddr = netconfig_get_default_ipaddress();
714         DBG("ipaddr-[%s]", ipaddr);
715
716         if (!ipaddr)
717                 return -1;
718
719         fd = socket(AF_INET, SOCK_DGRAM, 0);
720         if (fd < 0)
721                 return -1;
722
723         memset(&sai, 0, sizeof(struct sockaddr_in));
724         sai.sin_family = AF_INET;
725         sai.sin_port = 0;
726         if (!inet_aton(ipaddr, &sai.sin_addr)) {
727                 DBG("fail to inet_aton()");
728                 close(fd);
729                 return -1;
730         }
731
732         memset(&ifr, 0, sizeof(struct ifreq));
733         memcpy(&ifr.ifr_addr, &sai, sizeof(sai));
734         g_strlcpy((char *)ifr.ifr_name, WIFI_IFNAME, IFNAMSIZ);
735
736 #ifndef SIOCKILLADDR
737 #define SIOCKILLADDR    0x8939
738 #endif
739
740         ret = ioctl(fd, SIOCKILLADDR, &ifr);
741         if (ret < 0) {
742                 DBG("fail to ioctl[SIOCKILLADDR]");
743                 close(fd);
744                 return -1;
745         }
746
747         close(fd);
748         return 0;
749 }
750
751 void netconfig_update_default_profile(const char *profile)
752 {
753         static char *old_profile = NULL;
754
755         /* It's automatically updated by signal-handler
756          * DO NOT update manually
757          *
758          * It is going to update default connection information
759          */
760
761         if (netconfig_default_connection_info.profile != NULL) {
762
763                 if (netconfig_is_wifi_profile(netconfig_default_connection_info.profile))
764                         __netconfig_reset_ipv4_socket();
765
766                 g_free(old_profile);
767                 old_profile = strdup(netconfig_default_connection_info.profile);
768
769                 g_free(netconfig_default_connection_info.profile);
770                 netconfig_default_connection_info.profile = NULL;
771
772                 g_free(netconfig_default_connection_info.ifname);
773                 netconfig_default_connection_info.ifname = NULL;
774
775                 g_free(netconfig_default_connection_info.ipaddress);
776                 netconfig_default_connection_info.ipaddress = NULL;
777
778                 g_free(netconfig_default_connection_info.ipaddress6);
779                 netconfig_default_connection_info.ipaddress6 = NULL;
780
781                 g_free(netconfig_default_connection_info.proxy);
782                 netconfig_default_connection_info.proxy = NULL;
783
784                 netconfig_default_connection_info.freq = 0;
785
786                 if (wifi_state_get_service_state() != NETCONFIG_WIFI_CONNECTED) {
787                         g_free(netconfig_default_connection_info.essid);
788                         netconfig_default_connection_info.essid = NULL;
789                 }
790         }
791
792         /* default profile is NULL and new connected profile is NULL */
793         if (!profile) {
794                 profile = __netconfig_get_default_profile();
795
796                 if (profile && netconfig_is_cellular_profile(profile) &&
797                         !netconfig_is_cellular_internet_profile(profile)) {
798                         DBG("not a default cellular profile");
799                         profile = NULL;
800                 }
801
802                 if (!profile) {
803                         __netconfig_update_default_connection_info();
804                         return;
805                 }
806         }
807
808         netconfig_default_connection_info.profile = g_strdup(profile);
809         __netconfig_get_default_connection_info(profile);
810         __netconfig_update_default_connection_info();
811
812 }
813
814 void netconfig_update_default(void)
815 {
816         if (__netconfig_is_tech_state_connected() == TRUE)
817                 __netconfig_update_if_service_connected();
818         else
819                 __netconfig_adjust_tcp_buffer_size();
820 }
821
822 char *netconfig_get_ifname(const char *profile)
823 {
824         GVariant *message = NULL, *variant;
825         GVariantIter *iter, *next;
826         gchar *key;
827         gchar *key1;
828         const gchar *value = NULL;
829         gchar *ifname = NULL;
830
831         if (profile == NULL)
832                 return NULL;
833
834         message = netconfig_invoke_dbus_method(CONNMAN_SERVICE, profile,
835                         CONNMAN_SERVICE_INTERFACE, "GetProperties", NULL);
836         if (message == NULL) {
837                 ERR("Failed to get service properties");
838                 return NULL;
839         }
840
841         g_variant_get(message, "(a{sv})", &iter);
842         while (g_variant_iter_loop(iter, "{sv}", &key, &next)) {
843                 if (g_strcmp0(key, "Ethernet") == 0) {
844                         while (g_variant_iter_loop(next, "{sv}", &key1, &variant)) {
845                                 if (g_strcmp0(key1, "Interface") == 0) {
846                                         value = g_variant_get_string(variant, NULL);
847                                         ifname = g_strdup(value);
848                                 }
849                         }
850                 }
851         }
852
853         g_variant_unref(message);
854
855         g_variant_iter_free(iter);
856
857         return ifname;
858 }
859
860 /* Check Ethernet Cable Plug-in /Plug-out Status */
861 void netconfig_network_notify_ethernet_cable_state(const char *key)
862 {
863         __netconfig_network_notify_result("EthernetCableState", key);
864 }
865
866 static gboolean handle_add_route(
867                 Network *object,
868                 GDBusMethodInvocation *context,
869                 gchar *ip_addr,
870                 gchar *netmask,
871                 gchar *interface,  gchar *gateway, gint address_family)
872 {
873         const gchar *path = ROUTE_EXEC_PATH;
874         gchar *const args[] = { "/sbin/route", "add", "-net", ip_addr,
875                 "netmask", netmask, "dev", interface, NULL };
876         gchar *const envs[] = { NULL };
877         const gchar* buf = NULL;
878         gchar* ch = NULL;
879         int prefix_len = 0;
880         int pos = 0;
881
882         DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
883
884         switch (address_family) {
885         case AF_INET:
886                 if (ip_addr == NULL || netmask == NULL || interface == NULL) {
887                         ERR("Invalid parameter");
888                         netconfig_error_invalid_parameter(context);
889                         return FALSE;
890                 }
891
892                 if (netconfig_execute_file(path, args, envs) < 0) {
893                         DBG("Failed to add a new route");
894                         netconfig_error_permission_denied(context);
895                         return FALSE;
896                 }
897
898                 break;
899         case AF_INET6:
900                 if (ip_addr == NULL || interface == NULL || gateway == NULL) {
901                         ERR("Invalid parameter");
902                         netconfig_error_invalid_parameter(context);
903                         return FALSE;
904                 }
905
906                 buf = ip_addr;
907                 ch = strchr(buf, '/');
908                 pos = ch - buf + 1;
909                 if (ch) {
910                         prefix_len = atoi(ch + 1);
911                         ip_addr[pos-1] = '\0';
912                 } else {
913                         prefix_len = 128;
914                 }
915
916                 if (netconfig_add_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
917                         DBG("Failed to add a new route");
918                         netconfig_error_permission_denied(context);
919                         return FALSE;
920                 }
921                 break;
922         default:
923                 DBG("Unknown Address Family");
924                 netconfig_error_invalid_parameter(context);
925                 return FALSE;
926         }
927
928         DBG("Successfully added a new route");
929         network_complete_add_route(object, context, TRUE);
930         return TRUE;
931 }
932
933 static gboolean handle_remove_route(
934                 Network *object,
935                 GDBusMethodInvocation *context,
936                 gchar *ip_addr,
937                 gchar *netmask,
938                 gchar *interface, gchar *gateway, gint address_family)
939 {
940         const char *path = ROUTE_EXEC_PATH;
941         gchar *const args[] = { "/sbin/route", "del", "-net", ip_addr,
942                 "netmask", netmask, "dev", interface, NULL };
943         char *const envs[] = { NULL };
944         const char* buf = NULL;
945         char* ch = NULL;
946         int prefix_len = 0;
947         int pos = 0;
948
949         DBG("ip_addr(%s), netmask(%s), interface(%s), gateway(%s)", ip_addr, netmask, interface, gateway);
950
951         switch (address_family) {
952         case AF_INET:
953                 if (ip_addr == NULL || netmask == NULL || interface == NULL) {
954                         DBG("Invalid parameter!");
955                         netconfig_error_invalid_parameter(context);
956                         return FALSE;
957                 }
958                 if (netconfig_execute_file(path, args, envs) < 0) {
959                         DBG("Failed to remove the route");
960                         netconfig_error_permission_denied(context);
961                         return FALSE;
962                 }
963                 break;
964         case AF_INET6:
965                 if (ip_addr == NULL || interface == NULL || gateway == NULL) {
966                         DBG("Invalid parameter!");
967                         netconfig_error_invalid_parameter(context);
968                         return FALSE;
969                 }
970
971                 buf = ip_addr;
972                 ch = strchr(buf, '/');
973                 pos = ch - buf + 1;
974                 if (ch) {
975                         prefix_len = atoi(ch + 1);
976                         ip_addr[pos-1] = '\0';
977                 } else {
978                         prefix_len = 128;
979                 }
980
981                 if (netconfig_del_route_ipv6(ip_addr, interface, gateway, prefix_len) < 0) {
982                         DBG("Failed to remove the route");
983                         netconfig_error_permission_denied(context);
984                         return FALSE;
985                 }
986                 break;
987         default:
988                 DBG("Unknown Address Family");
989                 netconfig_error_invalid_parameter(context);
990                 return FALSE;
991         }
992
993         DBG("Successfully removed the route");
994         network_complete_remove_route(object, context, TRUE);
995         return TRUE;
996 }
997
998 static gboolean handle_check_get_privilege(Network *object,
999                 GDBusMethodInvocation *context)
1000 {
1001         network_complete_check_get_privilege(object, context);
1002         return TRUE;
1003 }
1004
1005
1006 static gboolean handle_check_profile_privilege(Network *object,
1007                 GDBusMethodInvocation *context)
1008 {
1009         network_complete_check_profile_privilege(object, context);
1010         return TRUE;
1011 }
1012
1013 gboolean handle_ethernet_cable_state(Network *object,
1014         GDBusMethodInvocation *context)
1015 {
1016         int ret = 0;
1017         int state = 0;
1018
1019         ret = netconfig_get_ethernet_cable_state(&state);
1020         if (ret != 0) {
1021                 DBG("Failed to get ethernet cable state");
1022                 netconfig_error_fail_ethernet_cable_state(context);
1023                 return FALSE;
1024         }
1025
1026         DBG("Successfully get ethernet cable state[%d]", state);
1027         network_complete_ethernet_cable_state(object, context, state);
1028         return TRUE;
1029 }
1030
1031 void state_object_create_and_init(void)
1032 {
1033         DBG("Creating network state object");
1034         GDBusInterfaceSkeleton *interface_network = NULL;
1035         GDBusConnection *connection = NULL;
1036         GDBusObjectManagerServer *server = netdbus_get_state_manager();
1037         if (server == NULL)
1038                 return;
1039
1040         connection = netdbus_get_connection();
1041         g_dbus_object_manager_server_set_connection(server, connection);
1042
1043         /*Interface netconfig.network*/
1044         netconfigstate = network_skeleton_new();
1045
1046         interface_network = G_DBUS_INTERFACE_SKELETON(netconfigstate);
1047         g_signal_connect(netconfigstate, "handle-add-route",
1048                                 G_CALLBACK(handle_add_route), NULL);
1049         g_signal_connect(netconfigstate, "handle-check-get-privilege",
1050                                 G_CALLBACK(handle_check_get_privilege), NULL);
1051         g_signal_connect(netconfigstate, "handle-check-profile-privilege",
1052                                 G_CALLBACK(handle_check_profile_privilege), NULL);
1053         g_signal_connect(netconfigstate, "handle-ethernet-cable-state",
1054                                 G_CALLBACK(handle_ethernet_cable_state), NULL);
1055         g_signal_connect(netconfigstate, "handle-remove-route",
1056                                 G_CALLBACK(handle_remove_route), NULL);
1057         g_signal_connect(netconfigstate, "handle-launch-mdns",
1058                                 G_CALLBACK(handle_launch_mdns), NULL);
1059         g_signal_connect(netconfigstate, "handle-ref-mdns",
1060                                 G_CALLBACK(handle_ref_mdns), NULL);
1061         g_signal_connect(netconfigstate, "handle-unref-mdns",
1062                                 G_CALLBACK(handle_unref_mdns), NULL);
1063
1064         if (!g_dbus_interface_skeleton_export(interface_network, connection,
1065                         NETCONFIG_NETWORK_STATE_PATH, NULL)) {
1066                 ERR("Export with path failed");
1067         }
1068 }
1069
1070 void state_object_deinit(void)
1071 {
1072         g_object_unref(netconfigstate);
1073 }