Fixed: Coding rule issues.
[platform/core/connectivity/wifi-direct-manager.git] / src / wifi-direct-util.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 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 /**
21  * This file implements wifi direct utility functions.
22  *
23  * @file                wifi-direct-util.c
24  * @author      Gibyoung Kim (lastkgb.kim@samsung.com)
25  * @version     0.7
26  */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
33 #include <sys/socket.h>
34 #include <sys/ioctl.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37 #include <net/if.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <time.h>
41 #include <errno.h>
42
43 #include <glib.h>
44
45 #include <vconf.h>
46 #include <tzplatform_config.h>
47 #if defined(TIZEN_FEATURE_DEFAULT_CONNECTION_AGENT)
48 #include <systemd/sd-login.h>
49 #include <aul.h>
50 #endif
51 #include <wifi-direct.h>
52
53 #include "wifi-direct-ipc.h"
54 #include "wifi-direct-manager.h"
55 #include "wifi-direct-state.h"
56 #include "wifi-direct-util.h"
57 #include "wifi-direct-oem.h"
58 #include "wifi-direct-group.h"
59 #include "wifi-direct-session.h"
60 #include "wifi-direct-error.h"
61 #include "wifi-direct-log.h"
62 #include "wifi-direct-dbus.h"
63 #if defined(TIZEN_FEATURE_ASP)
64 #include "wifi-direct-asp.h"
65 #endif
66
67 #include <linux/unistd.h>
68 #include <asm/types.h>
69 #include <linux/netlink.h>
70 #include <linux/rtnetlink.h>
71
72 #include <netlink/netlink.h>
73 #include <netlink/socket.h>
74 #include <netlink/route/neighbour.h>
75
76 #define TIZEN_P2P_GO_IPADDR "192.168.49.1"
77 #define MAX_SIZE_ERROR_BUFFER 256
78
79 #if defined TIZEN_PROFILE_MOBILE
80 #define DEFAULT_MAC_FILE_PATH tzplatform_mkpath(TZ_SYS_ETC, ".mac.info")
81 #endif /* TIZEN_PROFILE_MOBILE */
82
83 #if defined TIZEN_PROFILE_TV
84 #       if defined TIZEN_WIFI_MODULE_BUNDLE
85 #               define DEFAULT_MAC_FILE_PATH "/sys/class/net/wlan0/address"
86 #       else /* TIZEN_WIFI_MODULE_BUNDLE */
87 #               define DEFAULT_MAC_FILE_PATH "/sys/class/net/p2p0/address"
88 #       endif /* TIZEN_WIFI_MODULE_BUNDLE */
89 #endif /* TIZEN_PROFILE_TV */
90
91 #ifndef DEFAULT_MAC_FILE_PATH
92 #       define DEFAULT_MAC_FILE_PATH "/sys/class/net/p2p0/address"
93 #endif
94
95 #define COUNTRY_CODE_FILE tzplatform_mkpath(TZ_SYS_RO_ETC, "wifi-direct/ccode.conf")
96
97 static int _txt_to_mac(char *txt, unsigned char *mac)
98 {
99         int i = 0;
100
101         for (;;) {
102                 mac[i++] = (char) strtoul(txt, &txt, 16);
103                 if (i == MACADDR_LEN || !*txt++)
104                         break;
105         }
106
107         if (i != MACADDR_LEN)
108                 return -1;
109
110         WDS_LOGD("Converted MAC address [" MACSECSTR "]",
111                                         MAC2SECSTR(mac));
112         return 0;
113 }
114
115 static int _txt_to_ip(char *txt, unsigned char *ip)
116 {
117         int i = 0;
118
119         for (;;) {
120                 ip[i++] = (char) strtoul(txt, &txt, 10);
121                 if (i == IPADDR_LEN || !*txt++)
122                         break;
123         }
124
125         if (i != IPADDR_LEN)
126                 return -1;
127
128         WDS_LOGD("Converted IP address [" IPSECSTR "]", IP2SECSTR(ip));
129         return 0;
130 }
131
132 #if !(__GNUC__ <= 4 && __GNUC_MINOR__ < 8)
133 int wfd_util_get_current_time(unsigned long *cur_time)
134 {
135         struct timespec time;
136         int res;
137
138         errno = 0;
139         res = clock_gettime(CLOCK_REALTIME, &time);
140         if (!res) {
141                 WDS_LOGD("Succeeded to get current real time");
142                 *cur_time = time.tv_sec;
143                 return 0;
144         }
145         WDS_LOGE("Failed to get current real time(%s)", strerror(errno));
146
147         errno = 0;
148         res = clock_gettime(CLOCK_MONOTONIC, &time);
149         if (!res) {
150                 WDS_LOGD("Succeeded to get current system time");
151                 *cur_time = time.tv_sec;
152                 return 0;
153         }
154         WDS_LOGE("Failed to get current system time(%s)", strerror(errno));
155
156         return -1;
157 }
158 #endif
159
160 #if defined(TIZEN_FEATURE_DEFAULT_CONNECTION_AGENT)
161 static int __wfd_util_find_login_user(uid_t *uid)
162 {
163         uid_t *uids;
164         int ret, i;
165         char *state;
166
167         ret = sd_get_uids(&uids);
168         if (ret <= 0)
169                 return -1;
170
171         for (i = 0; i < ret ; i++) {
172                 if (sd_uid_get_state(uids[i], &state) < 0) {
173                         free(uids);
174                         return -1;
175                 } else {
176                         if (!strncmp(state, "online", 6)) {
177                                 *uid = uids[i];
178                                 free(uids);
179                                 free(state);
180                                 return 0;
181                         }
182                 }
183          }
184         free(uids);
185         free(state);
186         return -1;
187 }
188 #endif
189
190 gboolean wfd_util_execute_file(const char *file_path,
191         char *const args[], char *const envs[])
192 {
193         pid_t pid = 0;
194         int rv = 0;
195         errno = 0;
196         register unsigned int index = 0;
197
198         while (args[index] != NULL) {
199                 WDS_LOGD("[%s]", args[index]);
200                 index++;
201         }
202
203         if (!(pid = fork())) {
204                 WDS_LOGD("pid(%d), ppid(%d)", getpid(), getppid());
205                 WDS_LOGD("Inside child, exec (%s) command", file_path);
206
207                 errno = 0;
208                 if (execve(file_path, args, envs) == -1) {
209                         WDS_LOGE("Fail to execute command (%s)", strerror(errno));
210                         exit(1);
211                 }
212         } else if (pid > 0) {
213                 if (waitpid(pid, &rv, 0) == -1)
214                         WDS_LOGD("wait pid (%u) rv (%d)", pid, rv);
215                 if (WIFEXITED(rv))
216                         WDS_LOGD("exited, rv=%d", WEXITSTATUS(rv));
217                 else if (WIFSIGNALED(rv))
218                         WDS_LOGD("killed by signal %d", WTERMSIG(rv));
219                 else if (WIFSTOPPED(rv))
220                         WDS_LOGD("stopped by signal %d", WSTOPSIG(rv));
221                 else if (WIFCONTINUED(rv))
222                         WDS_LOGD("continued");
223                 return TRUE;
224         }
225
226         WDS_LOGE("failed to fork (%s)", strerror(errno));
227         return FALSE;
228 }
229
230 int wfd_util_channel_to_freq(int channel)
231 {
232         if (channel < 1 || channel > 161 ||
233                 (channel > 48 && channel < 149) ||
234                 (channel > 14 && channel < 36)) {
235                 WDS_LOGE("Unsupported channel[%d]", channel);
236                 return -1;
237         }
238
239         if (channel >= 36)
240                 return 5000 + 5*channel;
241         else if (channel == 14)
242                 return 2484;
243         else
244                 return 2407 + 5*channel;
245 }
246
247 int wfd_util_freq_to_channel(int freq)
248 {
249         if (freq < 2412 || freq > 5825 ||
250                 (freq > 2484 && freq < 5180)) {
251                 WDS_LOGE("Unsupported frequency[%d]", freq);
252                 return -1;
253         }
254
255         if (freq >= 5180)
256                 return 36 + (freq - 5180)/5;
257         else if (freq <= 2472)
258                 return 1 + (freq - 2412)/5;
259         else if (freq == 2484)
260                 return 14;
261         else
262                 return -1;
263 }
264
265 int wfd_util_get_phone_name(char *phone_name)
266 {
267         __WDS_LOG_FUNC_ENTER__;
268         char *name = NULL;
269
270         name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
271         if (!name) {
272                 WDS_LOGE("Failed to get vconf value for %s", VCONFKEY_SETAPPL_DEVICE_NAME_STR);
273                 return -1;
274         }
275         g_strlcpy(phone_name, name, DEV_NAME_LEN + 1);
276         WDS_LOGD("[%s: %s]", VCONFKEY_SETAPPL_DEVICE_NAME_STR, phone_name);
277         g_free(name);
278         __WDS_LOG_FUNC_EXIT__;
279         return 0;
280 }
281
282 void _wfd_util_dev_name_changed_cb(keynode_t *key, void *data)
283 {
284         __WDS_LOG_FUNC_ENTER__;
285         char dev_name[DEV_NAME_LEN+1] = {0, };
286         int res = 0;
287
288         res = wfd_util_get_phone_name(dev_name);
289         if (res < 0) {
290                 WDS_LOGE("Failed to get phone name(vconf)");
291                 return;
292         }
293         WDS_LOGD("Device name changed as [%s]", dev_name);
294
295         res = wfd_local_set_dev_name(dev_name);
296         if (res < 0)
297                 WDS_LOGE("Failed to set device name");
298         __WDS_LOG_FUNC_EXIT__;
299         return;
300 }
301
302 void wfd_util_set_dev_name_notification()
303 {
304         __WDS_LOG_FUNC_ENTER__;
305         int res = 0;
306
307         res = vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR, _wfd_util_dev_name_changed_cb, NULL);
308         if (res) {
309                 WDS_LOGE("Failed to set vconf notification callback(SETAPPL_DEVICE_NAME_STR)");
310                 return;
311         }
312
313         __WDS_LOG_FUNC_EXIT__;
314         return;
315 }
316
317 void wfd_util_unset_dev_name_notification()
318 {
319         __WDS_LOG_FUNC_ENTER__;
320         int res = 0;
321
322         res = vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR, _wfd_util_dev_name_changed_cb);
323         if (res) {
324                 WDS_LOGE("Failed to set vconf notification callback(SETAPPL_DEVICE_NAME_STR)");
325                 return;
326         }
327
328         __WDS_LOG_FUNC_EXIT__;
329         return;
330 }
331
332
333 void _wfd_util_check_country_cb(keynode_t *key, void *data)
334 {
335         __WDS_LOG_FUNC_ENTER__;
336         wfd_manager_s *manager = (wfd_manager_s*) data;
337         int res = 0;
338         int plmn = 0;
339         char mcc[4] = {0, };
340         char *ccode;
341         GKeyFile *keyfile = NULL;
342         const char *file_path = COUNTRY_CODE_FILE;
343         GError * err = NULL;
344
345         if (!manager) {
346                 WDS_LOGE("Invalid parameter");
347                 return;
348         }
349
350         res = vconf_get_int(VCONFKEY_TELEPHONY_PLMN, &plmn);
351         if (res) {
352                 WDS_LOGE("Failed to get vconf value for PLMN(%d)", res);
353                 return;
354         }
355
356         snprintf(mcc, 4, "%d", plmn);
357
358         keyfile = g_key_file_new();
359         res = g_key_file_load_from_file(keyfile, file_path, 0, &err);
360         if (!res) {
361                 WDS_LOGE("Failed to load key file(%s)", err->message);
362                 g_key_file_free(keyfile);
363                 return;
364         }
365
366         ccode = g_key_file_get_string(keyfile, "ccode_map", mcc, &err);
367         if (!ccode) {
368                 WDS_LOGE("Failed to get country code string(%s)", err->message);
369                 g_key_file_free(keyfile);
370                 return;
371         }
372
373         res = wfd_oem_set_country(manager->oem_ops, ccode);
374         if (res < 0)
375                 WDS_LOGE("Failed to set contry code");
376         else
377                 WDS_LOGD("Succeeded to set country code(%s)", ccode);
378
379         g_key_file_free(keyfile);
380         g_free(ccode);
381
382         __WDS_LOG_FUNC_EXIT__;
383         return;
384 }
385
386 int wfd_util_set_country()
387 {
388         __WDS_LOG_FUNC_ENTER__;
389         wfd_manager_s *manager = wfd_get_manager();
390         int res = 0;
391
392         _wfd_util_check_country_cb(NULL, manager);
393
394         res = vconf_notify_key_changed(VCONFKEY_TELEPHONY_PLMN, _wfd_util_check_country_cb, manager);
395         if (res) {
396                 WDS_LOGE("Failed to set vconf notification callback(TELEPHONY_PLMN)");
397                 return -1;
398         }
399
400         __WDS_LOG_FUNC_EXIT__;
401         return 0;
402 }
403
404 int wfd_util_unset_country()
405 {
406         __WDS_LOG_FUNC_ENTER__;
407         int res = 0;
408
409         res = vconf_ignore_key_changed(VCONFKEY_TELEPHONY_PLMN, _wfd_util_check_country_cb);
410         if (res) {
411                 WDS_LOGE("Failed to unset vconf notification callback(TELEPHONY_PLMN)");
412                 return -1;
413         }
414
415         __WDS_LOG_FUNC_EXIT__;
416         return 0;
417 }
418
419 int wfd_util_check_wifi_state()
420 {
421         __WDS_LOG_FUNC_ENTER__;
422         int wifi_state = 0;
423         int res = 0;
424
425 /* vconf key and value (vconf-keys.h)
426 #define VCONFKEY_WIFI_STATE     "memory/wifi/state"
427 enum {
428         VCONFKEY_WIFI_OFF = 0x00,
429         VCONFKEY_WIFI_UNCONNECTED,
430         VCONFKEY_WIFI_CONNECTED,
431         VCONFKEY_WIFI_TRANSFER,
432         VCONFKEY_WIFI_STATE_MAX
433 };
434 */
435         res = vconf_get_int(VCONFKEY_WIFI_STATE, &wifi_state);
436         if (res < 0) {
437                 WDS_LOGE("Failed to get vconf value [%s]", VCONFKEY_WIFI_STATE);
438                 __WDS_LOG_FUNC_EXIT__;
439                 return -1;
440         }
441         WDS_LOGD("[%s: %d]", VCONFKEY_WIFI_STATE, wifi_state);
442
443         if (wifi_state > VCONFKEY_WIFI_OFF) {
444                 WDS_LOGD("Wi-Fi is on");
445                 __WDS_LOG_FUNC_EXIT__;
446                 return 1;
447         }
448         WDS_LOGD("OK. Wi-Fi is off\n");
449
450         __WDS_LOG_FUNC_EXIT__;
451         return 0;
452 }
453
454 int wfd_util_check_mobile_ap_state()
455 {
456         __WDS_LOG_FUNC_ENTER__;
457         int mobile_ap_state = 0;
458         int res = 0;
459
460         res = vconf_get_int(VCONFKEY_MOBILE_HOTSPOT_MODE, &mobile_ap_state);
461         if (res < 0) {
462                 WDS_LOGE("Failed to get vconf value[%s]", VCONFKEY_MOBILE_HOTSPOT_MODE);
463                 __WDS_LOG_FUNC_EXIT__;
464                 return -1;
465         }
466         WDS_LOGD("[%s: %d]", VCONFKEY_MOBILE_HOTSPOT_MODE, mobile_ap_state);
467
468         if ((mobile_ap_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI)
469                 || (mobile_ap_state & VCONFKEY_MOBILE_HOTSPOT_MODE_WIFI_AP)) {
470                 WDS_LOGD("Mobile AP is on");
471                 __WDS_LOG_FUNC_EXIT__;
472                 return 1;
473         }
474         WDS_LOGD("OK. Mobile AP is off\n");
475
476         __WDS_LOG_FUNC_EXIT__;
477         return 0;
478 }
479
480 int wfd_util_wifi_direct_activatable()
481 {
482         __WDS_LOG_FUNC_ENTER__;
483
484 #ifndef TIZEN_WLAN_CONCURRENT_ENABLE
485         int res_wifi = 0;
486
487         res_wifi = wfd_util_check_wifi_state();
488         if (res_wifi < 0) {
489                 WDS_LOGE("Failed to check Wi-Fi state");
490                 __WDS_LOG_FUNC_EXIT__;
491                 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
492         } else if (res_wifi > 0) {
493                 WDS_LOGE("Wi-Fi is On");
494                 __WDS_LOG_FUNC_EXIT__;
495                 return WIFI_DIRECT_ERROR_WIFI_USED;
496         } else {
497                 WDS_LOGE("Wi-Fi is Off");
498                 __WDS_LOG_FUNC_EXIT__;
499                 return WIFI_DIRECT_ERROR_NONE;
500         }
501 #endif
502
503 #if defined TIZEN_TETHERING_ENABLE
504         int res_mobap = 0;
505
506         res_mobap = wfd_util_check_mobile_ap_state();
507         if (res_mobap < 0) {
508                 WDS_LOGE("Failed to check Mobile AP state");
509                 __WDS_LOG_FUNC_EXIT__;
510                 return WIFI_DIRECT_ERROR_OPERATION_FAILED;
511         } else if (res_mobap > 0) {
512                 WDS_LOGE("Mobile AP is On");
513                 __WDS_LOG_FUNC_EXIT__;
514                 return WIFI_DIRECT_ERROR_MOBILE_AP_USED;
515         } else {
516                 WDS_LOGE("Mobile AP is Off");
517                 __WDS_LOG_FUNC_EXIT__;
518                 return WIFI_DIRECT_ERROR_NONE;
519         }
520 #endif
521
522         __WDS_LOG_FUNC_EXIT__;
523         return WIFI_DIRECT_ERROR_NONE;
524 }
525
526 #if 0
527 int wfd_util_get_wifi_direct_state()
528 {
529         __WDS_LOG_FUNC_ENTER__;
530         int state = 0;
531         int res = 0;
532
533         res = vconf_get_int(VCONFKEY_WIFI_DIRECT_STATE, &state);
534         if (res < 0) {
535                 WDS_LOGE("Failed to get vconf value [%s]\n", VCONFKEY_WIFI_DIRECT_STATE);
536                 __WDS_LOG_FUNC_EXIT__;
537                 return -1;
538         }
539
540         __WDS_LOG_FUNC_EXIT__;
541         return state;
542 }
543 #endif
544
545 int wfd_util_set_wifi_direct_state(int state)
546 {
547         __WDS_LOG_FUNC_ENTER__;
548         static int last_state = WIFI_DIRECT_STATE_DEACTIVATED;
549         int vconf_state = VCONFKEY_WIFI_DIRECT_DEACTIVATED;
550         int res = 0;
551
552         if (state < WIFI_DIRECT_STATE_DEACTIVATED ||
553             state > WIFI_DIRECT_STATE_GROUP_OWNER) {
554                 WDS_LOGE("Invalid parameter");
555                 __WDS_LOG_FUNC_EXIT__;
556                 return -1;
557         }
558
559         if (last_state == state) {
560                 WDS_LOGD("No change in state, not updating vconf [%s]",
561                          VCONFKEY_WIFI_DIRECT_STATE);
562                 __WDS_LOG_FUNC_EXIT__;
563                 return 0;
564         }
565
566         if (state == WIFI_DIRECT_STATE_ACTIVATED)
567                 vconf_state = VCONFKEY_WIFI_DIRECT_ACTIVATED;
568         else if (state == WIFI_DIRECT_STATE_DEACTIVATED)
569                 vconf_state = VCONFKEY_WIFI_DIRECT_DEACTIVATED;
570         else if (state == WIFI_DIRECT_STATE_CONNECTED)
571                 vconf_state = VCONFKEY_WIFI_DIRECT_CONNECTED;
572         else if (state == WIFI_DIRECT_STATE_GROUP_OWNER)
573                 vconf_state = VCONFKEY_WIFI_DIRECT_GROUP_OWNER;
574         else if (state == WIFI_DIRECT_STATE_DISCOVERING)
575                 vconf_state = VCONFKEY_WIFI_DIRECT_DISCOVERING;
576         else {
577                 WDS_LOGE("This state cannot be set as wifi_direct vconf state[%d]", state);
578                 return 0;
579         }
580
581         WDS_LOGD("Vconf key set [%s: %d]", VCONFKEY_WIFI_DIRECT_STATE, vconf_state);
582
583         res = vconf_set_int(VCONFKEY_WIFI_DIRECT_STATE, vconf_state);
584         if (res < 0) {
585                 WDS_LOGE("Failed to set vconf [%s]", VCONFKEY_WIFI_DIRECT_STATE);
586                 __WDS_LOG_FUNC_EXIT__;
587                 return -1;
588         }
589
590         last_state = state;
591
592         __WDS_LOG_FUNC_EXIT__;
593         return 0;
594 }
595
596 int wfd_util_get_local_dev_mac(unsigned char *dev_mac)
597 {
598         __WDS_LOG_FUNC_ENTER__;
599         const char *file_path = DEFAULT_MAC_FILE_PATH;
600         FILE *fd = NULL;
601         char local_mac[MACSTR_LEN] = {0, };
602         char *ptr = NULL;
603         int res = 0;
604
605         errno = 0;
606         fd = fopen(file_path, "r");
607         if (!fd) {
608                 WDS_LOGE("Failed to open MAC info file [%s] (%s)", file_path , strerror(errno));
609                 __WDS_LOG_FUNC_EXIT__;
610                 return -1;
611         }
612
613         errno = 0;
614         ptr = fgets(local_mac, MACSTR_LEN, fd);
615         if (!ptr) {
616                 WDS_LOGE("Failed to read file or no data read(%s)", strerror(errno));
617                 fclose(fd);
618                 __WDS_LOG_FUNC_EXIT__;
619                 return -1;
620         }
621         WDS_SECLOGD("Local MAC address [%s]", ptr);
622
623         res = _txt_to_mac(local_mac, dev_mac);
624         if (res < 0) {
625                 WDS_LOGE("Failed to convert text to MAC address");
626                 fclose(fd);
627                 __WDS_LOG_FUNC_EXIT__;
628                 return -1;
629         }
630
631         WDS_LOGD("Local Device MAC address [" MACSECSTR "]", MAC2SECSTR(dev_mac));
632
633         fclose(fd);
634         __WDS_LOG_FUNC_EXIT__;
635         return 0;
636 }
637
638 #ifdef TIZEN_FEATURE_DEFAULT_CONNECTION_AGENT
639 int wfd_util_start_wifi_direct_popup()
640 {
641         __WDS_LOG_FUNC_ENTER__;
642
643         uid_t uid = 0;
644         int ret = 0;
645         ret = __wfd_util_find_login_user(&uid);
646         if (ret < 0) {
647                 WDS_LOGE("__wfd_util_find_login_user Failed !");
648                 return -1;
649         }
650
651         if (AUL_R_OK != aul_launch_app_for_uid(
652                 "org.tizen.wifi-direct-popup", NULL, uid)) {
653                 WDS_LOGE("aul_launch_for_uid Failed !");
654                 return -1;
655         }
656
657         WDS_LOGD("Succeeded to launch wifi-direct-popup");
658         __WDS_LOG_FUNC_EXIT__;
659         return 0;
660 }
661
662 int wfd_util_stop_wifi_direct_popup()
663 {
664         __WDS_LOG_FUNC_ENTER__;
665
666         uid_t uid = 0;
667         int ret = 0;
668         ret = __wfd_util_find_login_user(&uid);
669         if (ret < 0) {
670                 WDS_LOGE("__wfd_util_find_login_user Failed !");
671                 return -1;
672         }
673
674         int pid = aul_app_get_pid_for_uid("org.tizen.wifi-direct-popup", uid);
675         if (pid > 0) {
676                 if (aul_terminate_pid_for_uid(pid, uid) != AUL_R_OK) {
677                         WDS_LOGD("Failed to destroy wifi-direct-popup pid[%d]", pid);
678                         return -1;
679                 } else {
680                         WDS_LOGD("Succeeded to destroy wifi-direct-popup");
681                 }
682         } else {
683                 WDS_LOGD("Wifi-direct-popup not running");
684         }
685
686         __WDS_LOG_FUNC_EXIT__;
687         return 0;
688 }
689 #endif /* TIZEN_FEATURE_DEFAULT_CONNECTION_AGENT */
690
691 int _connect_remote_device(char *ip_str)
692 {
693         int sock;
694         int flags;
695         int res = 0;
696         struct sockaddr_in remo_addr;
697
698         errno = 0;
699         sock = socket(PF_INET, SOCK_STREAM, 0);
700         if (sock == -1) {
701                 WDS_LOGE("Failed to create socket to remote device(%s)", strerror(errno));
702                 return -1;
703         }
704
705         flags = fcntl(sock, F_GETFL, 0);
706         res = fcntl(sock, F_SETFL, flags | O_NONBLOCK);
707         if (res < 0) {
708                 WDS_LOGE("File descriptor create failed");
709                 close(sock);
710                 __WDS_LOG_FUNC_EXIT__;
711                 return -1;
712         }
713
714         memset(&remo_addr, 0x0, sizeof(remo_addr));
715         remo_addr.sin_family = AF_INET;
716         remo_addr.sin_addr.s_addr = inet_addr(ip_str);
717         remo_addr.sin_port = htons(9999);
718
719         errno = 0;
720         res = connect(sock, (struct sockaddr*) &remo_addr, sizeof(remo_addr));
721         if (res < 0) {
722                 WDS_LOGE("Failed to connect to server socket [%s]", strerror(errno));
723                 close(sock);
724                 __WDS_LOG_FUNC_EXIT__;
725                 return -1;
726         }
727
728         WDS_SECLOGD("Status of connection to remote device[%s] - (%s)", ip_str, strerror(errno));
729
730         close(sock);
731
732         return 0;
733 }
734
735 static void _dhcps_ip_leased_cb(keynode_t *key, void* data)
736 {
737         __WDS_LOG_FUNC_ENTER__;
738         wfd_device_s *peer = (wfd_device_s*) data;
739         FILE *fp = NULL;
740         char buf[MAX_DHCP_DUMP_SIZE];
741         char ip_str[IPSTR_LEN] = {0, };
742         char intf_str[MACSTR_LEN];
743         unsigned char intf_addr[MACADDR_LEN];
744         char peer_mac_address[MACSTR_LEN+1] = {0,};
745         char assigned_ip_address[IPSTR_LEN+1] = {0,};
746         int n = 0;
747
748         if (!peer) {
749                 WDS_LOGD("Invalid parameter");
750                 return;
751         }
752         WDS_LOGD("DHCP server: IP leased");
753
754         errno = 0;
755         fp = fopen(DHCP_DUMP_FILE, "r");
756         if (NULL == fp) {
757                 WDS_LOGE("Could not read the file(%s). [%s]", DHCP_DUMP_FILE, strerror(errno));
758                 return;
759         }
760
761         while (fgets(buf, MAX_DHCP_DUMP_SIZE, fp) != NULL) {
762                 WDS_LOGD("Read line [%s]", buf);
763                 n = sscanf(buf, "%17s %15s", intf_str, ip_str);
764                 WDS_LOGD("ip=[%s], mac=[%s]", ip_str, intf_str);
765                 if (n != 2)
766                         continue;
767
768                 _txt_to_mac(intf_str, intf_addr);
769                 if (!memcmp(peer->intf_addr, intf_addr, MACADDR_LEN)) {
770                         WDS_LOGD("Peer intf mac found");
771                         _txt_to_ip(ip_str, peer->ip_addr);
772                         _connect_remote_device(ip_str);
773                         g_snprintf(peer_mac_address, MACSTR_LEN, MACSTR, MAC2STR(peer->dev_addr));
774                         g_snprintf(assigned_ip_address, IPSTR_LEN, IPSTR, IP2STR(peer->ip_addr));
775                         wfd_manager_dbus_emit_signal(WFD_MANAGER_MANAGE_INTERFACE,
776                                                      "PeerIPAssigned",
777                                                      g_variant_new("(ss)", peer_mac_address,
778                                                                            assigned_ip_address));
779                         break;
780                 } else {
781                         WDS_LOGD("Different interface address peer[" MACSECSTR "] vs dhcp[" MACSECSTR "]",
782                                                 MAC2SECSTR(peer->intf_addr), MAC2SECSTR(intf_addr));
783                 }
784         }
785         fclose(fp);
786
787         vconf_ignore_key_changed(VCONFKEY_WIFI_DIRECT_DHCP_IP_LEASE, _dhcps_ip_leased_cb);
788         vconf_set_int(VCONFKEY_WIFI_DIRECT_DHCP_IP_LEASE, 0);
789
790         __WDS_LOG_FUNC_EXIT__;
791         return;
792 }
793
794 static gboolean _polling_ip(gpointer user_data)
795 {
796         __WDS_LOG_FUNC_ENTER__;
797         wfd_manager_s *manager = wfd_get_manager();
798         wfd_device_s *local = (wfd_device_s*) manager->local;
799         wfd_device_s *peer = (wfd_device_s*) user_data;
800         char *ifname = NULL;
801         char ip_str[IPSTR_LEN] = {0, };
802         static int count = 0;
803         int res = 0;
804
805         if (!peer) {
806                 WDS_LOGE("peer data is not exists");
807                 return FALSE;
808         }
809
810         res = wfd_manager_get_goup_ifname(&ifname);
811         if (res < 0 || !ifname) {
812                 WDS_LOGE("Failed to get group interface name");
813                 return FALSE;
814         }
815
816         if (count > 28) {
817                 WDS_LOGE("Failed to get IP");
818                 count = 0;
819                 wfd_oem_destroy_group(manager->oem_ops, ifname);
820                 __WDS_LOG_FUNC_EXIT__;
821                 return FALSE;
822         }
823         res = wfd_util_local_get_ip(ifname, local->ip_addr, 0);
824         if (res < 0) {
825                 WDS_LOGE("Failed to get local IP for interface %s(count=%d)", ifname, count++);
826                 __WDS_LOG_FUNC_EXIT__;
827                 return TRUE;
828         }
829         WDS_LOGD("Succeeded to get local(client) IP [" IPSECSTR "] for iface[%s]",
830                                     IP2SECSTR(local->ip_addr), ifname);
831
832         res = wfd_util_dhcpc_get_server_ip(peer->ip_addr);
833         if (res < 0) {
834                 WDS_LOGE("Failed to get peer(server) IP(count=%d)", count++);
835                 __WDS_LOG_FUNC_EXIT__;
836                 return TRUE;
837         }
838         WDS_LOGD("Succeeded to get server IP [" IPSECSTR "]", IP2SECSTR(peer->ip_addr));
839         count = 0;
840
841         g_snprintf(ip_str, IPSTR_LEN, IPSTR, IP2STR(peer->ip_addr));
842         _connect_remote_device(ip_str);
843
844         char peer_mac_address[MACSTR_LEN+1] = {0, };
845
846         g_snprintf(peer_mac_address, MACSTR_LEN, MACSTR, MAC2STR(peer->dev_addr));
847         wfd_manager_dbus_emit_signal(WFD_MANAGER_MANAGE_INTERFACE,
848                                      "Connection",
849                                      g_variant_new("(iis)", WIFI_DIRECT_ERROR_NONE,
850                                                             WFD_EVENT_CONNECTION_RSP,
851                                                             peer_mac_address));
852
853 #if defined(TIZEN_FEATURE_ASP)
854         wfd_session_s *session = manager->session;
855         if (session && !ISZEROMACADDR(session->session_mac))
856                 wfd_asp_connect_status(session->session_mac,
857                                                         session->session_id,
858                                                         ASP_CONNECT_STATUS_GROUP_FORMATION_COMPLETED,
859                                                         NULL);
860 #endif
861
862         wfd_state_set(manager, WIFI_DIRECT_STATE_CONNECTED);
863         wfd_util_set_wifi_direct_state(WIFI_DIRECT_STATE_CONNECTED);
864         wfd_destroy_session(manager);
865
866         __WDS_LOG_FUNC_EXIT__;
867         return FALSE;
868 }
869
870 int wfd_util_dhcps_start(char *ifname)
871 {
872         __WDS_LOG_FUNC_ENTER__;
873         gboolean rv = FALSE;
874         char *const iface = ifname;
875         const char *path = "/usr/bin/wifi-direct-dhcp.sh";
876         char *const args[] = { "/usr/bin/wifi-direct-dhcp.sh", "server", iface, NULL };
877         char *const envs[] = { NULL };
878         wfd_manager_s *manager = wfd_get_manager();
879
880         vconf_set_int(VCONFKEY_WIFI_DIRECT_DHCP_IP_LEASE, 0);
881
882         rv = wfd_util_execute_file(path, args, envs);
883
884         if (rv != TRUE) {
885                 WDS_LOGE("Failed to start wifi-direct-dhcp.sh server");
886                 return -1;
887         }
888
889         /*
890          * As we are GO so IP should be updated
891          * before sending Group Created Event
892          */
893         vconf_set_str(VCONFKEY_WIFI_DIRECT_P2P_IFNAME, GROUP_IFNAME);
894         vconf_set_str(VCONFKEY_WIFI_DIRECT_P2P_LOCAL_IP, "192.168.49.1");
895         vconf_set_str(VCONFKEY_WIFI_DIRECT_P2P_SUBNET_MASK, "255.255.255.0");
896         vconf_set_str(VCONFKEY_WIFI_DIRECT_P2P_GATEWAY, "192.168.49.1");
897
898         WDS_LOGD("Successfully started wifi-direct-dhcp.sh server");
899
900         _txt_to_ip(TIZEN_P2P_GO_IPADDR, manager->local->ip_addr);
901
902         __WDS_LOG_FUNC_EXIT__;
903         return 0;
904 }
905
906 int wfd_util_dhcps_wait_ip_leased(wfd_device_s *peer)
907 {
908         __WDS_LOG_FUNC_ENTER__;
909
910         if (!peer) {
911                 WDS_LOGE("Invalid parameter");
912                 return -1;
913         }
914
915         vconf_set_int(VCONFKEY_WIFI_DIRECT_DHCP_IP_LEASE, 0);
916         vconf_notify_key_changed(VCONFKEY_WIFI_DIRECT_DHCP_IP_LEASE, _dhcps_ip_leased_cb, peer);
917
918         __WDS_LOG_FUNC_EXIT__;
919         return 0;
920 }
921
922 int wfd_util_dhcps_stop(char *ifname)
923 {
924         __WDS_LOG_FUNC_ENTER__;
925         gboolean rv = FALSE;
926         char *const iface = ifname;
927         const char *path = "/usr/bin/wifi-direct-dhcp.sh";
928         char *const args[] = { "/usr/bin/wifi-direct-dhcp.sh", "stop", iface, NULL };
929         char *const envs[] = { NULL };
930
931         vconf_ignore_key_changed(VCONFKEY_WIFI_DIRECT_DHCP_IP_LEASE, _dhcps_ip_leased_cb);
932         vconf_set_int(VCONFKEY_WIFI_DIRECT_DHCP_IP_LEASE, 0);
933
934         rv = wfd_util_execute_file(path, args, envs);
935
936         if (rv != TRUE) {
937                 WDS_LOGE("Failed to stop wifi-direct-dhcp.sh");
938                 return -1;
939         }
940         WDS_LOGD("Successfully stopped wifi-direct-dhcp.sh");
941
942         __WDS_LOG_FUNC_EXIT__;
943         return 0;
944 }
945
946 int wfd_util_dhcpc_start(char *ifname, wfd_device_s *peer)
947 {
948         __WDS_LOG_FUNC_ENTER__;
949         gboolean rv = FALSE;
950         char *const iface = ifname;
951         const char *path = "/usr/bin/wifi-direct-dhcp.sh";
952         char *const args[] = { "/usr/bin/wifi-direct-dhcp.sh", "client", iface, NULL };
953         char *const envs[] = { NULL };
954
955         if (!peer) {
956                 WDS_LOGE("Invalid parameter");
957                 return -1;
958         }
959
960         rv = wfd_util_execute_file(path, args, envs);
961         if (rv != TRUE) {
962                 WDS_LOGE("Failed to start wifi-direct-dhcp.sh client");
963                 return -1;
964         }
965         WDS_LOGD("Successfully started wifi-direct-dhcp.sh client");
966
967         g_timeout_add(250, (GSourceFunc) _polling_ip, peer);
968
969         __WDS_LOG_FUNC_EXIT__;
970         return 0;
971 }
972
973 int wfd_util_dhcpc_stop(char *ifname)
974 {
975         __WDS_LOG_FUNC_ENTER__;
976         gboolean rv = FALSE;
977         char *const iface = ifname;
978         const char *path = "/usr/bin/wifi-direct-dhcp.sh";
979         char *const args[] = { "/usr/bin/wifi-direct-dhcp.sh", "stop", iface, NULL };
980         char *const envs[] = { NULL };
981
982         rv = wfd_util_execute_file(path, args, envs);
983
984         if (rv != TRUE) {
985                 WDS_LOGE("Failed to stop wifi-direct-dhcp.sh");
986                 return -1;
987         }
988         WDS_LOGD("Successfully stopped wifi-direct-dhcp.sh");
989
990         __WDS_LOG_FUNC_EXIT__;
991         return 0;
992 }
993
994 int wfd_util_local_get_ip(char *ifname, unsigned char *ip_addr, int is_IPv6)
995 {
996         __WDS_LOG_FUNC_ENTER__;
997         struct ifreq ifr;
998         struct sockaddr_in *sin = NULL;
999         char *ip_str = NULL;
1000         int sock = -1;
1001         int res = -1;
1002
1003         if (!ifname || !ip_addr) {
1004                 WDS_LOGE("Invalid parameter");
1005                 __WDS_LOG_FUNC_EXIT__;
1006                 return -1;
1007         }
1008
1009         errno = 0;
1010         sock = socket(AF_INET, SOCK_DGRAM, 0);
1011         if (sock < SOCK_FD_MIN) {
1012                 WDS_LOGE("Failed to create socket. [%s]", strerror(errno));
1013                 if (sock >= 0)
1014                         close(sock);
1015                 __WDS_LOG_FUNC_EXIT__;
1016                 return -1;
1017         }
1018
1019         ifr.ifr_addr.sa_family = AF_INET;
1020         memset(ifr.ifr_name, 0x00, IFNAMSIZ);
1021         g_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
1022
1023         errno = 0;
1024         res = ioctl(sock, SIOCGIFADDR, &ifr);
1025         if (res < 0) {
1026                 WDS_LOGE("Failed to get IP from socket. [%s]", strerror(errno));
1027                 close(sock);
1028                 __WDS_LOG_FUNC_EXIT__;
1029                 return -1;
1030         }
1031         close(sock);
1032
1033         sin = (struct sockaddr_in*) &ifr.ifr_broadaddr;
1034         ip_str = inet_ntoa(sin->sin_addr);
1035         _txt_to_ip(ip_str, ip_addr);
1036         __WDS_LOG_FUNC_EXIT__;
1037         return 0;
1038 }
1039
1040 int wfd_util_dhcpc_get_server_ip(unsigned char* ip_addr)
1041 {
1042         __WDS_LOG_FUNC_ENTER__;
1043         char* get_str = NULL;
1044         int count = 0;
1045
1046         if (!ip_addr) {
1047                 WDS_LOGE("Invalid parameter");
1048                 __WDS_LOG_FUNC_EXIT__;
1049                 return -1;
1050         }
1051
1052         while (count < 10) {
1053                 get_str = vconf_get_str(VCONFKEY_WIFI_DIRECT_DHCPC_SERVER_IP);
1054                 if (!get_str) {
1055                         WDS_LOGE("Failed to get vconf value[%s]", VCONFKEY_WIFI_DIRECT_DHCPC_SERVER_IP);
1056                         __WDS_LOG_FUNC_EXIT__;
1057                         return -1;
1058                 }
1059
1060                 if (strcmp(get_str, ZEROIP) == 0) {
1061                         WDS_LOGE("Failed to get vconf value[%s]", VCONFKEY_WIFI_DIRECT_DHCPC_SERVER_IP);
1062                         g_free(get_str);
1063                         __WDS_LOG_FUNC_EXIT__;
1064                         return -1;
1065                 }
1066
1067                 WDS_LOGD("VCONFKEY_WIFI_DIRECT_DHCPC_SERVER_IP(%s) : %s\n", VCONFKEY_WIFI_DIRECT_DHCPC_SERVER_IP, get_str);
1068                 _txt_to_ip(get_str, ip_addr);
1069                 g_free(get_str);
1070                 if (*ip_addr)
1071                         break;
1072                 count++;
1073         }
1074
1075         __WDS_LOG_FUNC_EXIT__;
1076         return 0;
1077 }
1078
1079 #ifdef TIZEN_FEATURE_IP_OVER_EAPOL
1080 static int _wfd_util_set_vconf_for_static_ip(const char *ifname, char *static_ip)
1081 {
1082         __WDS_LOG_FUNC_ENTER__;
1083
1084         if (!ifname || !static_ip)
1085                 return -1;
1086
1087         vconf_set_str(VCONFKEY_WIFI_DIRECT_P2P_IFNAME, ifname);
1088         vconf_set_str(VCONFKEY_WIFI_DIRECT_P2P_LOCAL_IP, static_ip);
1089         vconf_set_str(VCONFKEY_WIFI_DIRECT_P2P_SUBNET_MASK, "255.255.255.0");
1090         vconf_set_str(VCONFKEY_WIFI_DIRECT_P2P_GATEWAY, "192.168.49.1");
1091
1092         __WDS_LOG_FUNC_EXIT__;
1093
1094         return 0;
1095 }
1096
1097
1098 static int _wfd_util_static_ip_set(const char *ifname, unsigned char *static_ip)
1099 {
1100         __WDS_LOG_FUNC_ENTER__;
1101         int res = 0;
1102         unsigned char ip_addr[IPADDR_LEN];
1103         char ip_str[IPSTR_LEN] = {0, };
1104
1105         int if_index;
1106         int nl_sock = -1;
1107         struct sockaddr_nl dst_addr;
1108
1109         struct {
1110                 struct nlmsghdr     nh;
1111                 struct ifaddrmsg    ifa;
1112                 char            attrbuf[1024];
1113         } req;
1114         struct rtattr *rta;
1115         struct iovec iov;
1116         struct msghdr nl_msg;
1117
1118         if (!ifname || !static_ip) {
1119                 WDS_LOGE("Invalid parameter");
1120                 __WDS_LOG_FUNC_EXIT__;
1121                 return -1;
1122         }
1123
1124         /* Get index of interface */
1125         if_index = if_nametoindex(ifname);
1126         if (if_index < 0) {
1127                 WDS_LOGE("Failed to get interface index. [%s]", strerror(errno));
1128                 __WDS_LOG_FUNC_EXIT__;
1129                 return -1;
1130         }
1131
1132         WDS_LOGD("Creating a Netlink Socket");
1133         nl_sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1134         if (nl_sock < 0) {
1135                 WDS_LOGE("Failed to create socket. [%s]", strerror(errno));
1136                 __WDS_LOG_FUNC_EXIT__;
1137                 return -1;
1138         }
1139
1140         memset(&dst_addr, 0, sizeof(dst_addr));
1141         dst_addr.nl_family =  AF_NETLINK;
1142         dst_addr.nl_pid = 0;
1143
1144         memset(&req, 0, sizeof(req));
1145         req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1146         req.nh.nlmsg_type = RTM_NEWADDR;
1147         req.nh.nlmsg_flags = NLM_F_CREATE | NLM_F_EXCL | NLM_F_REQUEST;
1148
1149         req.ifa.ifa_family = AF_INET;
1150         req.ifa.ifa_prefixlen = 24;
1151         req.ifa.ifa_flags = IFA_F_PERMANENT;
1152         req.ifa.ifa_scope = 0;
1153         req.ifa.ifa_index = if_index;
1154
1155         rta = (struct rtattr *)(req.attrbuf);
1156         rta->rta_type = IFA_LOCAL;
1157         rta->rta_len = RTA_LENGTH(IPADDR_LEN);
1158         memcpy(RTA_DATA(rta), static_ip, IPADDR_LEN);
1159         req.nh.nlmsg_len = NLMSG_ALIGN(req.nh.nlmsg_len) + rta->rta_len;
1160
1161         rta = (struct rtattr *)(req.attrbuf + rta->rta_len);
1162         rta->rta_type = IFA_BROADCAST;
1163         rta->rta_len = RTA_LENGTH(IPADDR_LEN);
1164         memcpy(ip_addr, static_ip, IPADDR_LEN);
1165         ip_addr[3] = 0xff;
1166         memcpy(RTA_DATA(rta), ip_addr, IPADDR_LEN);
1167         req.nh.nlmsg_len += rta->rta_len;
1168
1169         memset(&iov, 0, sizeof(iov));
1170         iov.iov_base = &req;
1171         iov.iov_len = req.nh.nlmsg_len;
1172
1173         memset(&nl_msg, 0, sizeof(nl_msg));
1174         nl_msg.msg_name = (void *)&dst_addr;
1175         nl_msg.msg_namelen = sizeof(dst_addr);
1176         nl_msg.msg_iov = &iov;
1177         nl_msg.msg_iovlen = 1;
1178
1179         res = sendmsg(nl_sock, &nl_msg, 0);
1180         if (res < 0)
1181                 WDS_LOGE("Failed to sendmsg. [%s]", strerror(errno));
1182         else
1183                 WDS_LOGD("Succed to sendmsg. [%d]", res);
1184
1185         close(nl_sock);
1186         WDS_LOGE("Succeeded to set local(client) IP [" IPSTR "] for iface[%s]",
1187                                 IP2STR(static_ip), ifname);
1188
1189         snprintf(ip_str, IPSTR_LEN, IPSTR, IP2STR(static_ip));
1190         _wfd_util_set_vconf_for_static_ip(ifname, ip_str);
1191
1192         __WDS_LOG_FUNC_EXIT__;
1193         return res;
1194 }
1195
1196 int wfd_util_ip_over_eap_assign(wfd_device_s *peer, const char *ifname)
1197 {
1198         __WDS_LOG_FUNC_ENTER__;
1199         wfd_manager_s *manager = wfd_get_manager();
1200         wfd_device_s *local = (wfd_device_s*) manager->local;
1201
1202         char ip_str[IPSTR_LEN] = {0, };
1203
1204         if (!peer) {
1205                 WDS_LOGE("Invalid paramater");
1206                 return -1;
1207         }
1208
1209         _wfd_util_static_ip_set(ifname, peer->client_ip_addr);
1210         memcpy(peer->ip_addr, peer->go_ip_addr, IPADDR_LEN);
1211         memcpy(local->ip_addr, peer->client_ip_addr, IPADDR_LEN);
1212
1213         g_snprintf(ip_str, IPSTR_LEN, IPSTR, IP2STR(peer->ip_addr));
1214         _connect_remote_device(ip_str);
1215
1216         __WDS_LOG_FUNC_EXIT__;
1217         return 0;
1218 }
1219 #endif /* TIZEN_FEATURE_IP_OVER_EAPOL */
1220
1221 int wfd_util_ip_unset(const char *ifname)
1222 {
1223         __WDS_LOG_FUNC_ENTER__;
1224         int res = 0;
1225         unsigned char ip_addr[IPADDR_LEN];
1226         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
1227
1228         int if_index;
1229         int nl_sock = -1;
1230         struct sockaddr_nl dst_addr;
1231
1232         struct {
1233                 struct nlmsghdr     nh;
1234                 struct ifaddrmsg    ifa;
1235                 char            attrbuf[1024];
1236         } req;
1237         struct rtattr *rta;
1238         struct iovec iov;
1239         struct msghdr nl_msg;
1240
1241         if (!ifname) {
1242                 WDS_LOGE("Invalid parameter");
1243                 __WDS_LOG_FUNC_EXIT__;
1244                 return -1;
1245         }
1246
1247         res = wfd_util_local_get_ip((char *)ifname, ip_addr, 0);
1248         if (res < 0) {
1249                 WDS_LOGE("Failed to get local IP for interface %s", ifname);
1250                 __WDS_LOG_FUNC_EXIT__;
1251                 return -1;
1252         }
1253         WDS_LOGE("Succeeded to get local(client) IP [" IPSTR "] for iface[%s]",
1254                         IP2STR(ip_addr), ifname);
1255
1256         if_index = if_nametoindex(ifname);
1257         if (if_index < 0) {
1258                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
1259                 WDS_LOGE("Failed to get interface index. [%s]", error_buf);
1260                 __WDS_LOG_FUNC_EXIT__;
1261                 return -1;
1262         }
1263
1264         WDS_LOGD("Creating a Netlink Socket");
1265         nl_sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1266         if (nl_sock < 0) {
1267                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
1268                 WDS_LOGE("Failed to create socket. [%s]", error_buf);
1269                 __WDS_LOG_FUNC_EXIT__;
1270                 return -1;
1271         }
1272
1273         WDS_LOGD("Set dst socket address to kernel");
1274         memset(&dst_addr, 0, sizeof(dst_addr));
1275         dst_addr.nl_family =  AF_NETLINK;
1276         dst_addr.nl_pid = 0;
1277
1278         memset(&req, 0, sizeof(req));
1279         req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
1280         req.nh.nlmsg_type = RTM_DELADDR;
1281         req.nh.nlmsg_flags = NLM_F_REQUEST;
1282
1283         req.ifa.ifa_family = AF_INET;
1284         req.ifa.ifa_prefixlen = 32;
1285         req.ifa.ifa_flags = IFA_F_PERMANENT;
1286         req.ifa.ifa_scope = 0;
1287         req.ifa.ifa_index = if_index;
1288
1289         rta = (struct rtattr *)(req.attrbuf);
1290         rta->rta_type = IFA_LOCAL;
1291         rta->rta_len = RTA_LENGTH(IPADDR_LEN);
1292         memcpy(RTA_DATA(rta), ip_addr, IPADDR_LEN);
1293         req.nh.nlmsg_len = NLMSG_ALIGN(req.nh.nlmsg_len) + rta->rta_len;
1294
1295         memset(&iov, 0, sizeof(iov));
1296         iov.iov_base = &req;
1297         iov.iov_len = req.nh.nlmsg_len;
1298
1299         memset(&nl_msg, 0, sizeof(nl_msg));
1300         nl_msg.msg_name = (void *)&dst_addr;
1301         nl_msg.msg_namelen = sizeof(dst_addr);
1302         nl_msg.msg_iov = &iov;
1303         nl_msg.msg_iovlen = 1;
1304
1305         res = sendmsg(nl_sock, &nl_msg, 0);
1306         if (res < 0) {
1307                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
1308                 WDS_LOGE("Failed to sendmsg. [%s]", error_buf);
1309         } else {
1310                 WDS_LOGD("Succeed to sendmsg. [%d]", res);
1311         }
1312
1313         close(nl_sock);
1314
1315         __WDS_LOG_FUNC_EXIT__;
1316         return res;
1317 }
1318
1319 gboolean wfd_util_is_remove_group_allowed(void)
1320 {
1321         wfd_manager_s *manager = wfd_get_manager();
1322
1323         if (!manager->auto_group_remove_enable)
1324                 return FALSE;
1325
1326         return TRUE;
1327 }