Merge "Refactored "NetlinkScan" DBus method." into tizen
[platform/core/connectivity/net-config.git] / src / utils / util.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 <dlfcn.h>
21 #include <errno.h>
22 #include <vconf.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <net/if.h>
28 #include <net/route.h>
29 #include <arpa/inet.h>
30 #include <sys/wait.h>
31 #include <sys/stat.h>
32 #include <sys/ioctl.h>
33 #include <ctype.h>
34 #include <vconf-keys.h>
35 #include <tzplatform_config.h>
36 #include <system_info.h>
37
38 #include "log.h"
39 #include "util.h"
40 #include "neterror.h"
41 #include "wifi-state.h"
42 #include "netdbus.h"
43
44 #define DBUS_SERVICE_DBUS               "org.freedesktop.DBus"
45 #define DBUS_INTERFACE_DBUS             "org.freedesktop.DBus"
46 #define MAC_INFO_FILEPATH               tzplatform_mkpath(TZ_SYS_ETC, "/.mac.info")
47 #define MAC_ADDRESS_FILEPATH    "/sys/class/net/wlan0/address"
48 #define MAC_ADDRESS_MAX_LEN             18
49 #define HEADED_PLUGIN_FILEPATH          "/usr/lib/net-config-plugin-headed.so"
50 #define TELEPHONY_PLUGIN_FILEPATH       "/usr/lib/net-config-plugin-telephony.so"
51
52 static gboolean netconfig_device_picker_test = FALSE;
53 static int mdnsd_ref_count = 0;
54 typedef struct {
55         char *conn_name;
56         int conn_id;
57 } dnssd_conn_destroy_data;
58
59 static gboolean netconfig_plugin_headed_enabled = FALSE;
60 static gboolean netconfig_plugin_telephony_enabled = FALSE;
61 static void *handle_headed;
62 static void *handle_telephony;
63 static struct netconfig_headed_plugin_t *headed_plugin;
64 static struct netconfig_telephony_plugin_t *telephony_plugin;
65
66 gboolean netconfig_check_passphrase(const gchar *service, const char *passphrase)
67 {
68         gsize length;
69
70         if (!passphrase)
71                 return FALSE;
72
73         length = strlen(passphrase);
74
75         if (g_str_has_suffix(service, "psk") == TRUE) {
76                 if (length == 64) {
77                         for (int i = 0; i < 64; i++)
78                                 if (!isxdigit((unsigned char)passphrase[i]))
79                                         return FALSE;
80                 } else if (length < 8 || length > 63)
81                         return FALSE;
82         } else if (g_str_has_suffix(service, "wep") == TRUE) {
83                 if (length == 10 || length == 26) {
84                         for (int i = 0; i < length; i++)
85                                 if (!isxdigit((unsigned char)passphrase[i]))
86                                         return FALSE;
87                 } else if (length != 5 && length != 13)
88                         return FALSE;
89         }
90
91         return TRUE;
92 }
93
94 GKeyFile *netconfig_keyfile_load(const char *pathname)
95 {
96         GKeyFile *keyfile = NULL;
97         GError *error = NULL;
98
99         keyfile = g_key_file_new();
100         if (g_key_file_load_from_file(keyfile, pathname, 0, &error) != TRUE) {
101                 DBG("Unable to open %s, error %s", pathname, error->message);
102                 g_error_free(error);
103
104                 g_key_file_free(keyfile);
105                 keyfile = NULL;
106         }
107
108         DBG("loaded keyfile %s", pathname);
109         return keyfile;
110 }
111
112 void netconfig_keyfile_save(GKeyFile *keyfile, const char *pathname)
113 {
114         gsize size = 0;
115         GError *error = NULL;
116         gchar *keydata = NULL;
117         gchar *needle = NULL, *directory = NULL;
118
119         directory = g_strdup(pathname);
120         needle = g_strrstr(directory, "/");
121
122         if (needle != NULL)
123                 *needle = '\0';
124
125         if (directory == NULL || (*directory) == '\0') {
126                 g_free(directory);
127                 ERR("directory is NULL");
128                 return;
129         }
130
131         if (g_file_test(directory, G_FILE_TEST_IS_DIR) != TRUE) {
132                 if (g_mkdir_with_parents(directory,
133                                 S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
134                         g_free(directory);
135                         ERR("failed to make directory");
136                         return;
137                 }
138         }
139         g_free(directory);
140
141         keydata = g_key_file_to_data(keyfile, &size, &error);
142         if (g_file_set_contents(pathname, keydata, size, &error) != TRUE) {
143                 ERR("Unable to save %s, error %s", pathname, error->message);
144                 g_error_free(error);
145         }
146
147         chmod(pathname, S_IRUSR | S_IWUSR);
148         DBG("Successfully saved keyfile %s", pathname);
149
150         g_free(keydata);
151 }
152
153 void netconfig_start_timer_seconds(guint secs,
154                 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
155 {
156         guint t_id = 0;
157
158         if (callback == NULL) {
159                 ERR("callback function is NULL");
160                 return;
161         }
162
163         if ((timer_id != NULL && *timer_id != 0)) {
164                 ERR("timer already is registered");
165                 return;
166         }
167
168         t_id = g_timeout_add_seconds(secs, callback, user_data);
169
170         if (t_id == 0) {
171                 ERR("Can't add timer");
172                 return;
173         }
174
175         if (timer_id != NULL)
176                 *timer_id = t_id;
177 }
178
179 void netconfig_start_timer(guint msecs,
180                 gboolean(*callback) (gpointer), void *user_data, guint *timer_id)
181 {
182         guint t_id = 0;
183
184         INFO("Register timer with callback pointer (%p)", callback);
185
186         if (callback == NULL) {
187                 ERR("callback function is NULL");
188                 return;
189         }
190
191         if ((timer_id != NULL && *timer_id != 0)) {
192                 ERR("timer already is registered");
193                 return;
194         }
195
196         t_id = g_timeout_add(msecs, callback, user_data);
197
198         if (t_id == 0) {
199                 ERR("Can't add timer");
200                 return;
201         }
202
203         if (timer_id != NULL)
204                 *timer_id = t_id;
205 }
206
207 void netconfig_stop_timer(guint *timer_id)
208 {
209         if (timer_id == NULL) {
210                 ERR("timer is NULL");
211                 return;
212         }
213
214         if (*timer_id != 0) {
215                 g_source_remove(*timer_id);
216                 *timer_id = 0;
217         }
218 }
219
220 static gboolean __netconfig_test_device_picker()
221 {
222         char *favorite_wifi_service = NULL;
223
224         favorite_wifi_service = wifi_get_favorite_service();
225         if (favorite_wifi_service != NULL) {
226                 ERR("favorite_wifi_service is existed[%s] : Donot launch device picker", favorite_wifi_service);
227                 g_free(favorite_wifi_service);
228                 return FALSE;
229         }
230
231         return TRUE;
232 }
233
234 static void __netconfig_pop_device_picker(void)
235 {
236         if (!netconfig_plugin_headed_enabled)
237                 return;
238
239         if (!headed_plugin)
240                 return;
241
242         headed_plugin->pop_device_picker();
243 }
244
245 static gboolean __netconfig_wifi_try_device_picker(gpointer data)
246 {
247         if (__netconfig_test_device_picker() == TRUE)
248                 __netconfig_pop_device_picker();
249
250         return FALSE;
251 }
252
253 static guint __netconfig_wifi_device_picker_timer_id(gboolean is_set_method, guint timer_id)
254 {
255         static guint netconfig_wifi_device_picker_service_timer = 0;
256
257         if (is_set_method != TRUE)
258                 return netconfig_wifi_device_picker_service_timer;
259
260         if (netconfig_wifi_device_picker_service_timer != timer_id)
261                 netconfig_wifi_device_picker_service_timer = timer_id;
262
263         return netconfig_wifi_device_picker_service_timer;
264 }
265
266 static void __netconfig_wifi_device_picker_set_timer_id(guint timer_id)
267 {
268         __netconfig_wifi_device_picker_timer_id(TRUE, timer_id);
269 }
270
271 static guint __netconfig_wifi_device_picker_get_timer_id(void)
272 {
273         return __netconfig_wifi_device_picker_timer_id(FALSE, -1);
274 }
275
276 void netconfig_wifi_enable_device_picker_test(void)
277 {
278         netconfig_device_picker_test = TRUE;
279 }
280
281 void netconfig_wifi_device_picker_service_start(void)
282 {
283         const int NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL = 700;
284         guint timer_id = 0;
285
286         if (netconfig_device_picker_test == TRUE)
287                 netconfig_device_picker_test = FALSE;
288         else
289                 return;
290
291         int wifi_ug_state;
292
293         netconfig_vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &wifi_ug_state);
294         if (wifi_ug_state == VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)
295                 return;
296
297         DBG("Register device picker timer with %d milliseconds", NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL);
298         netconfig_start_timer(NETCONFIG_WIFI_DEVICE_PICKER_INTERVAL, __netconfig_wifi_try_device_picker, NULL, &timer_id);
299
300         __netconfig_wifi_device_picker_set_timer_id(timer_id);
301 }
302
303 void netconfig_wifi_device_picker_service_stop(void)
304 {
305         guint timer_id = 0;
306
307         timer_id = __netconfig_wifi_device_picker_get_timer_id();
308         if (timer_id == 0)
309                 return;
310
311         DBG("Clear device picker timer with timer_id %d", timer_id);
312
313         netconfig_stop_timer(&timer_id);
314
315         __netconfig_wifi_device_picker_set_timer_id(timer_id);
316 }
317
318 gboolean netconfig_is_wifi_direct_on(void)
319 {
320         if (!netconfig_check_feature_supported(WIFI_DIRECT_FEATURE))
321                 return FALSE;
322
323         int wifi_direct_state = 0;
324
325         netconfig_vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &wifi_direct_state);
326
327         DBG("Wi-Fi direct mode %d", wifi_direct_state);
328         return (wifi_direct_state != 0) ? TRUE : FALSE;
329 }
330
331 gboolean netconfig_is_wifi_tethering_on(void)
332 {
333         if (netconfig_check_feature_supported(TETHERING_FEATURE)) {
334                 int wifi_tethering_state = 0;
335
336                 netconfig_vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &wifi_tethering_state);
337                 DBG("Wi-Ti tethering mode %d", wifi_tethering_state);
338                 if ((wifi_tethering_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI)
339                                 || (wifi_tethering_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI_AP)) {
340                         DBG("Mobile AP is on");
341                         return TRUE;
342                 }
343         }
344
345         DBG("Mobile AP is off");
346         return FALSE;
347 }
348
349 gboolean netconfig_interface_up(const char *ifname)
350 {
351         int fd;
352         struct ifreq ifr;
353
354         fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
355         if (fd < 0)
356                 return FALSE;
357
358         memset(&ifr, 0, sizeof(ifr));
359         g_strlcpy((char *)ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
360
361         if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
362                 close(fd);
363                 return FALSE;
364         }
365
366         ifr.ifr_flags |= (IFF_UP | IFF_DYNAMIC);
367         if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
368                 close(fd);
369                 return FALSE;
370         }
371
372         close(fd);
373
374         DBG("Successfully activated wireless interface %s", ifname);
375         return TRUE;
376 }
377
378 gboolean netconfig_interface_down(const char *ifname)
379 {
380         int fd;
381         struct ifreq ifr;
382
383         fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
384         if (fd < 0)
385                 return FALSE;
386
387         memset(&ifr, 0, sizeof(ifr));
388         g_strlcpy((char *)ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
389
390         if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
391                 close(fd);
392                 return FALSE;
393         }
394
395         ifr.ifr_flags = (ifr.ifr_flags & ~IFF_UP) | IFF_DYNAMIC;
396         if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
397                 close(fd);
398                 return FALSE;
399         }
400
401         close(fd);
402
403         DBG("Successfully de-activated wireless interface %s", ifname);
404         return TRUE;
405 }
406
407 int netconfig_execute_file(const char *file_path,
408                 char *const args[], char *const envs[])
409 {
410         pid_t pid = 0;
411         int status = 0;
412         int rv = 0;
413         errno = 0;
414         register unsigned int index = 0;
415         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
416
417         while (args[index] != NULL) {
418                 DBG("%s", args[index]);
419                 index++;
420         }
421
422         if (!(pid = fork())) {
423                 DBG("pid(%d), ppid (%d)", getpid(), getppid());
424                 DBG("Inside child, exec (%s) command", file_path);
425
426                 errno = 0;
427                 if (execve(file_path, args, envs) == -1) {
428                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
429                         DBG("Fail to execute command (%s)", error_buf);
430                         exit(1);
431                 }
432         } else if (pid > 0) {
433                 if (waitpid(pid, &status, 0) == -1)
434                         DBG("wait pid (%u) status (%d)", pid, status);
435
436                 if (WIFEXITED(status)) {
437                         rv = WEXITSTATUS(status);
438                         DBG("exited, status=%d", rv);
439                 } else if (WIFSIGNALED(status)) {
440                         DBG("killed by signal %d", WTERMSIG(status));
441                 } else if (WIFSTOPPED(status)) {
442                         DBG("stopped by signal %d", WSTOPSIG(status));
443                 } else if (WIFCONTINUED(status)) {
444                         DBG("continued");
445                 }
446
447                 return rv;
448         }
449
450         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
451         DBG("failed to fork(%s)", error_buf);
452         return -EIO;
453 }
454
455 int netconfig_execute_cmd(const char *cmd)
456 {
457         if (cmd == NULL)
458                 return -EIO;
459
460         pid_t pid = 0;
461         int status = 0;
462         int rv = 0;
463         errno = 0;
464         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
465         gchar **args = NULL;
466
467         DBG("command: %s", cmd);
468
469         args = g_strsplit_set(cmd, " ", -1);
470
471         if (!(pid = fork())) {
472                 DBG("pid(%d), ppid (%d)", getpid(), getppid());
473
474                 errno = 0;
475                 if (execv(args[0], args) == -1) {
476                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
477                         DBG("Fail to execute command (%s)", error_buf);
478                         g_strfreev(args);
479                         exit(1);
480                 }
481         } else if (pid > 0) {
482                 if (waitpid(pid, &status, 0) == -1)
483                         DBG("wait pid (%u) status (%d)", pid, status);
484
485                 if (WIFEXITED(status)) {
486                         rv = WEXITSTATUS(status);
487                         DBG("exited, status=%d", rv);
488                 } else if (WIFSIGNALED(status)) {
489                         DBG("killed by signal %d", WTERMSIG(status));
490                 } else if (WIFSTOPPED(status)) {
491                         DBG("stopped by signal %d", WSTOPSIG(status));
492                 } else if (WIFCONTINUED(status)) {
493                         DBG("continued");
494                 }
495
496                 g_strfreev(args);
497                 return rv;
498         }
499
500         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
501         DBG("failed to fork(%s)", error_buf);
502         g_strfreev(args);
503
504         return -EIO;
505 }
506
507 static void on_clat_handler()
508 {
509         pid_t clat_pid = 0;
510         int state = 0;
511
512         clat_pid = waitpid(-1, &state, WNOHANG);
513
514         DBG("clat(%d) state(%d)", clat_pid, WEXITSTATUS(state));
515 }
516
517 int netconfig_execute_clatd(const char *file_path, char *const args[])
518 {
519         pid_t pid = 0;
520         int rv = 0;
521         errno = 0;
522         register unsigned int index = 0;
523         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
524
525         struct sigaction act;
526         int state = 0;
527
528         act.sa_handler = on_clat_handler;
529         sigemptyset(&act.sa_mask);
530         act.sa_flags = 0;
531
532         state = sigaction(SIGCHLD, &act, 0);
533         if (state != 0) {
534                 DBG("sigaction() : %d");
535                 return -1;
536         }
537
538         while (args[index] != NULL) {
539                 DBG("%s", args[index]);
540                 index++;
541         }
542
543         if (!(pid = fork())) {
544                 DBG("pid(%d), ppid (%d)", getpid(), getppid());
545                 DBG("Inside child, exec (%s) command", file_path);
546
547                 errno = 0;
548                 if (execvp(file_path, args) == -1) {
549                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
550                         ERR("Fail to execute command (%s)", error_buf);
551                         return -1;
552                 }
553         } else if (pid > 0) {
554                 ERR("Success to launch clatd");
555                 return rv;
556         }
557
558         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
559         DBG("failed to fork(%s)", error_buf);
560         return -EIO;
561 }
562
563 static void no_wait_signal_handler()
564 {
565         pid_t child_pid = 0;
566         int state = 0;
567
568         child_pid = waitpid(-1, &state, WNOHANG);
569
570         DBG("child_id(%d) state(%d)", child_pid, WEXITSTATUS(state));
571 }
572
573 int netconfig_execute_file_no_wait(const char *file_path, char *const args[])
574 {
575         pid_t pid = 0;
576         int rv = 0;
577         errno = 0;
578         register unsigned int index = 0;
579
580         struct sigaction act;
581         int state = 0;
582
583         act.sa_handler = no_wait_signal_handler;
584         sigemptyset(&act.sa_mask);
585         act.sa_flags = 0;
586
587         state = sigaction(SIGCHLD, &act, 0);
588         if (state != 0) {
589                 DBG("sigaction() : %d");
590                 return -1;
591         }
592
593         while (args[index] != NULL) {
594                 DBG("%s", args[index]);
595                 index++;
596         }
597
598         if (!(pid = fork())) {
599                 DBG("pid(%d), ppid (%d)", getpid(), getppid());
600                 DBG("Inside child, exec (%s) command", file_path);
601
602                 errno = 0;
603                 if (execvp(file_path, args) == -1) {
604                         ERR("Fail to execute command (%s)", strerror(errno));
605                         return -1;
606                 }
607         } else if (pid > 0) {
608                 ERR("Successfully launched child process");
609                 return rv;
610         }
611
612         DBG("failed to fork(%s)", strerror(errno));
613         return -EIO;
614 }
615
616 int __netconfig_get_interface_index(const char *interface_name)
617 {
618         struct ifreq ifr;
619         int sock = 0;
620         int result = 0;
621         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
622
623         if (interface_name == NULL) {
624                 DBG("Inteface name is NULL");
625                 return -1;
626         }
627
628         errno = 0;
629         sock = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
630         if (sock < 0) {
631                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
632                 DBG("Failed to create socket : %s", error_buf);
633                 return -1;
634         }
635
636         memset(&ifr, 0, sizeof(ifr));
637         strncpy(ifr.ifr_name, interface_name, sizeof(ifr.ifr_name) - 1);
638         result = ioctl(sock, SIOCGIFINDEX, &ifr);
639         close(sock);
640
641         if (result < 0) {
642                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
643                 DBG("Failed to get ifr index: %s", error_buf);
644                 return -1;
645         }
646
647         return ifr.ifr_ifindex;
648 }
649
650 int netconfig_add_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gint address_family)
651 {
652         struct ifreq ifr;
653         struct rtentry rt;
654         struct sockaddr_in addr_in;
655         int sock;
656         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
657
658         memset(&ifr, 0, sizeof(ifr));
659
660         ifr.ifr_ifindex = __netconfig_get_interface_index(interface);
661
662         if (ifr.ifr_ifindex < 0)
663                 return -1;
664
665         strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
666
667         memset(&rt, 0, sizeof(rt));
668
669         rt.rt_flags = RTF_UP | RTF_HOST;
670         memset(&addr_in, 0, sizeof(struct sockaddr_in));
671         addr_in.sin_family = address_family;
672         addr_in.sin_addr.s_addr = inet_addr(ip_addr);
673         memcpy(&rt.rt_dst, &addr_in, sizeof(rt.rt_dst));
674
675         memset(&addr_in, 0, sizeof(struct sockaddr_in));
676         addr_in.sin_family = address_family;
677         addr_in.sin_addr.s_addr = INADDR_ANY;
678         memcpy(&rt.rt_gateway, &addr_in, sizeof(rt.rt_gateway));
679
680         memset(&addr_in, 0, sizeof(struct sockaddr_in));
681         addr_in.sin_family = AF_INET;
682         addr_in.sin_addr.s_addr = inet_addr(subnet);
683         memcpy(&rt.rt_genmask, &addr_in, sizeof(rt.rt_genmask));
684
685         rt.rt_dev = ifr.ifr_name;
686
687         errno = 0;
688         sock = socket(PF_INET, SOCK_DGRAM, 0);
689
690         if (sock < 0) {
691                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
692                 DBG("Failed to create socket : %s", error_buf);
693                 return -1;
694         }
695
696         if (ioctl(sock, SIOCADDRT, &rt) < 0) {
697                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
698                 DBG("Failed to set route address : %s", error_buf);
699                 close(sock);
700                 return -1;
701         }
702
703         close(sock);
704
705         return 1;
706 }
707
708 int netconfig_del_route_ipv4(gchar *ip_addr, gchar *subnet, gchar *interface, gint address_family)
709 {
710         struct ifreq ifr;
711         struct rtentry rt;
712         struct sockaddr_in addr_in;
713         int sock;
714         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
715
716         memset(&ifr, 0, sizeof(ifr));
717         ifr.ifr_ifindex = __netconfig_get_interface_index(interface);
718
719         if (ifr.ifr_ifindex < 0)
720                 return -1;
721
722         strncpy(ifr.ifr_name, interface, IFNAMSIZ-1);
723
724         memset(&rt, 0, sizeof(rt));
725
726         rt.rt_flags = RTF_UP;
727         memset(&addr_in, 0, sizeof(struct sockaddr_in));
728         addr_in.sin_family = address_family;
729         addr_in.sin_addr.s_addr = inet_addr(ip_addr);
730         memcpy(&rt.rt_dst, &addr_in, sizeof(rt.rt_dst));
731
732         memset(&addr_in, 0, sizeof(struct sockaddr_in));
733         addr_in.sin_family = address_family;
734         addr_in.sin_addr.s_addr = inet_addr(subnet);
735         memcpy(&rt.rt_genmask, &addr_in, sizeof(rt.rt_genmask));
736         rt.rt_dev = ifr.ifr_name;
737
738         errno = 0;
739         sock = socket(PF_INET, SOCK_DGRAM, 0);
740
741         if (sock < 0) {
742                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
743                 DBG("Failed to create socket : %s", error_buf);
744                 return -1;
745         }
746
747         if (ioctl(sock, SIOCDELRT, &rt) < 0) {
748                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
749                 DBG("Failed to set route address : %s", error_buf);
750                 close(sock);
751                 return -1;
752         }
753
754         close(sock);
755
756         return 1;
757 }
758
759 int netconfig_add_route_ipv6(gchar *ip_addr, gchar *interface, gchar *gateway, unsigned char prefix_len)
760 {
761         struct in6_rtmsg rt;
762         int fd = 0;
763         int err = 0;
764         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
765
766         memset(&rt, 0, sizeof(rt));
767
768         rt.rtmsg_dst_len = prefix_len;
769
770         rt.rtmsg_flags = RTF_UP | RTF_HOST;
771
772         errno = 0;
773         if (inet_pton(AF_INET6, ip_addr, &rt.rtmsg_dst) < 0) {
774                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
775                 DBG("inet_pton failed : %s", error_buf);
776                 return -1;
777         }
778
779         if (gateway != NULL) {
780                 rt.rtmsg_flags |= RTF_GATEWAY;
781                 if (inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway) < 0) {
782                         strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
783                         DBG("inet_pton failed : %s", error_buf);
784                         return -1;
785                 }
786         }
787
788         rt.rtmsg_metric = 1;
789
790         fd = socket(AF_INET6, SOCK_DGRAM, 0);
791         if (fd < 0) {
792                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
793                 DBG("Failed to create socket : %s", error_buf);
794                 return -1;
795         }
796
797         rt.rtmsg_ifindex = 0;
798
799         if (interface) {
800                 struct ifreq ifr;
801                 memset(&ifr, 0, sizeof(ifr));
802                 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)-1);
803                 ioctl(fd, SIOCGIFINDEX, &ifr);
804                 rt.rtmsg_ifindex = ifr.ifr_ifindex;
805         }
806
807         if ((err = ioctl(fd, SIOCADDRT, &rt)) < 0) {
808                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
809                 DBG("Failed to add route: %s", error_buf);
810                 close(fd);
811                 return -1;
812         }
813
814         close(fd);
815
816         return 1;
817 }
818
819 int netconfig_del_route_ipv6(gchar *ip_addr, gchar *interface, gchar *gateway, unsigned char prefix_len)
820 {
821         struct in6_rtmsg rt;
822         int fd = 0;
823         int err = 0;
824
825         memset(&rt, 0, sizeof(rt));
826
827         rt.rtmsg_dst_len = prefix_len;
828
829         rt.rtmsg_flags = RTF_UP | RTF_HOST;
830
831         if (inet_pton(AF_INET6, ip_addr, &rt.rtmsg_dst) < 0) {
832                 err = -errno;
833                 return err;
834         }
835
836         if (gateway != NULL) {
837                 rt.rtmsg_flags |= RTF_GATEWAY;
838                 if (inet_pton(AF_INET6, gateway, &rt.rtmsg_gateway) < 0) {
839                         err = -errno;
840                         return err;
841                 }
842         }
843
844         rt.rtmsg_metric = 1;
845
846         fd = socket(AF_INET6, SOCK_DGRAM, 0);
847         if (fd < 0)
848                 return -1;
849
850         rt.rtmsg_ifindex = 0;
851
852         if (interface) {
853                 struct ifreq ifr;
854                 memset(&ifr, 0, sizeof(ifr));
855                 strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)-1);
856                 ioctl(fd, SIOCGIFINDEX, &ifr);
857                 rt.rtmsg_ifindex = ifr.ifr_ifindex;
858         }
859
860         if ((err = ioctl(fd, SIOCDELRT, &rt)) < 0) {
861                 DBG("Failed to del route: %d\n", err);
862                 close(fd);
863                 return -1;
864         }
865
866         close(fd);
867
868         return 1;
869 }
870
871 gboolean handle_launch_direct(Wifi *wifi, GDBusMethodInvocation *context)
872 {
873         if (!netconfig_check_feature_supported(WIFI_DIRECT_FEATURE)) {
874                 wifi_complete_launch_direct(wifi, context);
875                 return FALSE;
876         }
877
878         int ret = 0;
879         DBG("Launch Wi-Fi direct daemon");
880
881         const char *path = "/usr/bin/wifi-direct-server.sh";
882         char *const args[] = { "wifi-direct-server.sh", "start", NULL };
883         char *const envs[] = { NULL };
884
885         ret = netconfig_execute_file(path, args, envs);
886         if (ret < 0) {
887                 ERR("Failed to launch Wi-Fi direct daemon");
888                 netconfig_error_wifi_direct_failed(context);
889                 return FALSE;
890         }
891
892         wifi_complete_launch_direct(wifi, context);
893         return TRUE;
894 }
895
896 int execute_mdnsd_script(char* op)
897 {
898         const char *path = "/usr/bin/mdnsresponder-server.sh";
899         char *const args[] = { "mdnsresponder-server.sh", op, NULL };
900         char *const envs[] = { NULL };
901
902         return netconfig_execute_file(path, args, envs);
903 }
904
905 static void __dnssd_conn_destroyed_cb(GDBusConnection *conn,
906                 const gchar *Name, const gchar *path, const gchar *interface,
907                 const gchar *sig, GVariant *param, gpointer user_data)
908 {
909         gchar *name = NULL;
910         gchar *old = NULL;
911         gchar *new = NULL;
912         dnssd_conn_destroy_data *data = user_data;
913         GDBusConnection *connection = NULL;
914         connection = netdbus_get_connection();
915
916         if (param == NULL)
917                 return;
918
919         g_variant_get(param, "(sss)", &name, &old, &new);
920
921         if (g_strcmp0(name, data->conn_name) == 0 && *new == '\0') {
922                 DBG("Connection %s Destroyed: name %s id %d", data->conn_name, name,
923                         data->conn_id);
924                 mdnsd_ref_count--;
925                 g_dbus_connection_signal_unsubscribe(connection, data->conn_id);
926                 if (mdnsd_ref_count == 0) {
927                         if (execute_mdnsd_script("stop") < 0)
928                                 ERR("Failed to stop mdnsresponder daemon");
929                 }
930         }
931         g_free(name);
932         g_free(old);
933         g_free(new);
934         g_free(data->conn_name);
935         g_free(data);
936         return;
937 }
938
939 static void register_dnssd_conn_destroy_signal(gchar *name)
940 {
941         dnssd_conn_destroy_data *data;
942         GDBusConnection *connection = NULL;
943         connection = netdbus_get_connection();
944
945         if (connection == NULL) {
946                 ERR("Failed to get GDbus Connection");
947                 return;
948         }
949
950         data = g_try_malloc0(sizeof(dnssd_conn_destroy_data));
951
952         if (data == NULL) {
953                 ERR("Out of Memory!");
954                 return;
955         }
956
957         data->conn_name = g_strdup(name);
958
959         data->conn_id = g_dbus_connection_signal_subscribe(connection,
960                                                         DBUS_SERVICE_DBUS, DBUS_INTERFACE_DBUS,
961                                                         "NameOwnerChanged", NULL, name,
962                                                         G_DBUS_SIGNAL_FLAGS_NONE, __dnssd_conn_destroyed_cb,
963                                                         data, NULL);
964         return;
965 }
966
967 gboolean handle_launch_mdns(Network *object, GDBusMethodInvocation *context,
968                                                         gchar *name)
969 {
970         DBG("Launch mdnsresponder daemon");
971
972         if (execute_mdnsd_script("start") < 0) {
973                 ERR("Failed to launch mdnsresponder daemon");
974                 netconfig_error_invalid_parameter(context);
975                 return FALSE;
976         }
977
978         mdnsd_ref_count++;
979         register_dnssd_conn_destroy_signal(name);
980         DBG("Ref mdnsresponder daemon. ref count: %d", mdnsd_ref_count);
981
982         network_complete_launch_mdns(object, context);
983         return TRUE;
984 }
985
986 gboolean netconfig_send_notification_to_net_popup(const char * noti, const char * ssid)
987 {
988         if (!netconfig_plugin_headed_enabled)
989                 return FALSE;
990
991         if (!headed_plugin)
992                 return FALSE;
993
994         return headed_plugin->send_notification_to_net_popup(noti, ssid);
995 }
996
997 int netconfig_send_message_to_net_popup(const char *title,
998                 const char *content, const char *type, const char *ssid)
999 {
1000         if (!netconfig_plugin_headed_enabled)
1001                 return 0;
1002
1003         if (!headed_plugin)
1004                 return 0;
1005
1006         return headed_plugin->send_message_to_net_popup(title, content, type, ssid);
1007 }
1008
1009 int netconfig_send_restriction_to_net_popup(const char *title,
1010                 const char *type, const char *restriction)
1011 {
1012         if (!netconfig_plugin_headed_enabled)
1013                 return 0;
1014
1015         if (!headed_plugin)
1016                 return 0;
1017
1018         return headed_plugin->send_restriction_to_net_popup(title, type, restriction);
1019 }
1020
1021 void netconfig_set_system_event(int sys_evt, int evt_key, int evt_val)
1022 {
1023         if (!netconfig_plugin_headed_enabled)
1024                 return;
1025
1026         if (!headed_plugin)
1027                 return;
1028
1029         headed_plugin->set_system_event(sys_evt, evt_key, evt_val);
1030 }
1031
1032 void __netconfig_pop_wifi_connected_poppup(const char *ssid)
1033 {
1034         if (!netconfig_plugin_headed_enabled)
1035                 return;
1036
1037         if (!headed_plugin)
1038                 return;
1039
1040         headed_plugin->pop_wifi_connected_poppup(ssid);
1041 }
1042
1043 void netconfig_get_telephony_network_type(int *svctype, int *pstype)
1044 {
1045         if (!netconfig_plugin_telephony_enabled)
1046                 return;
1047
1048         if (!telephony_plugin)
1049                 return;
1050
1051         telephony_plugin->get_telephony_network_type(svctype, pstype);
1052 }
1053
1054 gboolean __netconfig_wifi_get_sim_imsi(Wifi *wifi, GDBusMethodInvocation *context)
1055 {
1056         if (!netconfig_plugin_telephony_enabled)
1057                 return FALSE;
1058
1059         if (!telephony_plugin)
1060                 return FALSE;
1061
1062         return telephony_plugin->wifi_get_sim_imsi(wifi, context);
1063 }
1064
1065 netconfig_error_e __netconfig_wifi_req_aka_auth(GArray *rand_data, GArray *autn_data,
1066                 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1067 {
1068         if (!netconfig_plugin_telephony_enabled)
1069                 return NETCONFIG_ERROR_INTERNAL;
1070
1071         if (!telephony_plugin)
1072                 return NETCONFIG_ERROR_INTERNAL;
1073
1074         return telephony_plugin->wifi_req_aka_auth(rand_data, autn_data, context, data);
1075 }
1076
1077 gboolean __netconfig_wifi_req_sim_auth(GArray *rand_data,
1078                 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1079 {
1080         if (!netconfig_plugin_telephony_enabled)
1081                 return FALSE;
1082
1083         if (!telephony_plugin)
1084                 return FALSE;
1085
1086         return telephony_plugin->wifi_req_sim_auth(rand_data, context, data);
1087 }
1088
1089 gboolean netconfig_tapi_check_sim_state(void)
1090 {
1091         if (!netconfig_plugin_telephony_enabled)
1092                 return FALSE;
1093
1094         if (!telephony_plugin)
1095                 return FALSE;
1096
1097         return telephony_plugin->tapi_check_sim_state();
1098 }
1099
1100 gboolean __netconfig_wifi_get_aka_authdata(Wifi *wifi,
1101                 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1102 {
1103         if (!netconfig_plugin_telephony_enabled)
1104                 return FALSE;
1105
1106         if (!telephony_plugin)
1107                 return FALSE;
1108
1109         return telephony_plugin->wifi_get_aka_authdata(wifi, context, data);
1110 }
1111
1112 gboolean __netconfig_wifi_get_sim_authdata(Wifi *wifi,
1113                 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
1114 {
1115         if (!netconfig_plugin_telephony_enabled)
1116                 return FALSE;
1117
1118         if (!telephony_plugin)
1119                 return FALSE;
1120
1121         return telephony_plugin->wifi_get_sim_authdata(wifi, context, data);
1122 }
1123
1124 void netconfig_set_vconf_int(const char * key, int value)
1125 {
1126         int ret = 0;
1127
1128         DBG("[%s: %d]", key, value);
1129
1130         ret = vconf_set_int(key, value);
1131         if (ret != VCONF_OK)
1132                 ERR("Failed to set");
1133 }
1134
1135 void netconfig_set_vconf_str(const char * key, const char * value)
1136 {
1137         int ret = 0;
1138
1139         DBG("[%s: %s]", key, value);
1140
1141         ret = vconf_set_str(key, value);
1142         if (ret != VCONF_OK)
1143                 ERR("Failed to set");
1144 }
1145
1146 int netconfig_vconf_get_int(const char * key, int *value)
1147 {
1148         int ret = 0;
1149
1150         ret = vconf_get_int(key, value);
1151         if (ret != VCONF_OK) {
1152                 ERR("Failed to get vconfkey [%s] value", key);
1153                 return -1;
1154         }
1155
1156         return 0;
1157 }
1158
1159 int netconfig_vconf_get_bool(const char * key, int *value)
1160 {
1161         int ret = 0;
1162
1163         ret = vconf_get_bool(key, value);
1164         if (ret != VCONF_OK) {
1165                 ERR("Failed to get vconfkey [%s] value", key);
1166                 return -1;
1167         }
1168
1169         return 0;
1170 }
1171
1172 char* netconfig_get_env(const char *key)
1173 {
1174         FILE *fp;
1175         char buf[256], *entry = NULL, *value = NULL, *last;
1176         int len = 0;
1177
1178         if (!key)
1179                 return NULL;
1180
1181         fp = fopen(NETCONFIG_TIZEN_SYSTEM_ENV, "r");
1182         if (!fp)
1183                 return NULL;
1184
1185         while (fgets(buf, sizeof(buf), fp)) {
1186                 entry = buf;
1187                 entry = strtok_r(entry, "=", &last);
1188                 if (entry) {
1189                         if (strstr(entry, key)) {
1190                                 entry = strtok_r(NULL, "\n", &last);
1191                                 if (entry) {
1192                                         len = strlen(entry);
1193                                         value = (char*)malloc(len+1);
1194                                         g_strlcpy(value, entry, len+1);
1195                                 } else {
1196                                         value = (char*)malloc(sizeof(char));
1197                                         g_strlcpy(value, "\n", sizeof(char));
1198                                 }
1199                                 break;
1200                         }
1201                 }
1202         }
1203
1204         fclose(fp);
1205         return value;
1206 }
1207
1208 void netconfig_set_mac_address_from_file(void)
1209 {
1210         FILE *file = NULL;
1211         char mac_str[MAC_ADDRESS_MAX_LEN];
1212         gchar *mac_lower_str = NULL;
1213         int mac_len = 0;
1214
1215         file = fopen(MAC_INFO_FILEPATH, "r");
1216         if (file == NULL) {
1217                 ERR("Fail to open %s", MAC_INFO_FILEPATH);
1218                 file = fopen(MAC_ADDRESS_FILEPATH, "r");
1219                 if (file == NULL) {
1220                         ERR("Fail to open %s", MAC_ADDRESS_FILEPATH);
1221                         return;
1222                 }
1223         }
1224         if (fgets(mac_str, sizeof(mac_str), file) == NULL) {
1225                 ERR("Fail to read mac address");
1226                 fclose(file);
1227                 return;
1228         }
1229
1230         mac_len = strlen(mac_str);
1231         if (mac_len < 17) {
1232                 ERR("mac.info is empty");
1233                 fclose(file);
1234                 return;
1235         }
1236
1237         mac_lower_str = g_ascii_strup(mac_str, (gssize)mac_len);
1238         netconfig_set_vconf_str(VCONFKEY_WIFI_BSSID_ADDRESS, mac_lower_str);
1239
1240         g_free(mac_lower_str);
1241         fclose(file);
1242 }
1243
1244 tizen_profile_t _get_tizen_profile()
1245 {
1246         static tizen_profile_t profile = TIZEN_PROFILE_UNKNOWN;
1247         if (__builtin_expect(profile != TIZEN_PROFILE_UNKNOWN, 1))
1248                 return profile;
1249
1250         char *profileName;
1251         system_info_get_platform_string("http://tizen.org/feature/profile", &profileName);
1252         switch (*profileName) {
1253         case 'm':
1254         case 'M':
1255                 profile = TIZEN_PROFILE_MOBILE;
1256                 break;
1257         case 'w':
1258         case 'W':
1259                 profile = TIZEN_PROFILE_WEARABLE;
1260                 break;
1261         case 't':
1262         case 'T':
1263                 profile = TIZEN_PROFILE_TV;
1264                 break;
1265         case 'i':
1266         case 'I':
1267                 profile = TIZEN_PROFILE_IVI;
1268                 break;
1269         default: // common or unknown ==> ALL ARE COMMON.
1270                 profile = TIZEN_PROFILE_COMMON;
1271         }
1272         free(profileName);
1273
1274         return profile;
1275 }
1276
1277 bool netconfig_check_feature_supported(const char *feature)
1278 {
1279         bool is_supported = false;
1280
1281         if (!system_info_get_platform_bool(feature, &is_supported)) {
1282                 if (is_supported != TRUE)
1283                         DBG("%s is not supported", feature);
1284                 else
1285                         DBG("%s is supported", feature);
1286         } else {
1287                 ERR("Error - Feature getting from System Info");
1288         }
1289
1290         return is_supported;
1291 }
1292
1293 void netconfig_plugin_init()
1294 {
1295         handle_headed = dlopen(HEADED_PLUGIN_FILEPATH, RTLD_NOW);
1296         if (!handle_headed) {
1297                 ERR("Can't load %s: %s", HEADED_PLUGIN_FILEPATH, dlerror());
1298         } else {
1299                 headed_plugin = dlsym(handle_headed, "netconfig_headed_plugin");
1300                 if (!headed_plugin) {
1301                         ERR("Can't load symbol: %s", dlerror());
1302                         dlclose(handle_headed);
1303                 } else {
1304                         netconfig_plugin_headed_enabled = TRUE;
1305                 }
1306         }
1307
1308         handle_telephony = dlopen(TELEPHONY_PLUGIN_FILEPATH, RTLD_NOW);
1309         if (!handle_telephony) {
1310                 ERR("Can't load %s: %s", TELEPHONY_PLUGIN_FILEPATH, dlerror());
1311         } else {
1312                 telephony_plugin = dlsym(handle_telephony, "netconfig_telephony_plugin");
1313                 if (!telephony_plugin) {
1314                         ERR("Can't load symbol: %s", dlerror());
1315                         dlclose(handle_telephony);
1316                 } else {
1317                         netconfig_plugin_telephony_enabled = TRUE;
1318                 }
1319         }
1320 }
1321
1322 void netconfig_plugin_deinit()
1323 {
1324         if (netconfig_plugin_headed_enabled) {
1325                 netconfig_plugin_headed_enabled = FALSE;
1326                 dlclose(handle_headed);
1327         }
1328
1329         if (netconfig_plugin_telephony_enabled) {
1330                 netconfig_plugin_telephony_enabled = FALSE;
1331                 dlclose(handle_telephony);
1332         }
1333 }
1334
1335 gboolean netconfig_get_headed_plugin_flag()
1336 {
1337         return netconfig_plugin_headed_enabled;
1338 }
1339
1340 gboolean netconfig_get_telephony_plugin_flag()
1341 {
1342         return netconfig_plugin_telephony_enabled;
1343 }
1344