Fix ASAN build failure
[platform/core/connectivity/wifi-direct-manager.git] / files / wifi-direct-dhcp.sh.in
1 #!/bin/sh
2 PATH=/bin:/usr/bin:/sbin:/usr/sbin
3
4 if [ $2 ]; then
5 INTERFACE_NAME="$2"
6 else
7 INTERFACE_NAME="p2p0"
8 fi
9 INTERFACE_PREFIX="p2p"
10 TARGET="TM1"
11 DEFAULT_IP="192.168.49.1"
12 DEFAULT_NET="192.168.49.1/24"
13 DEFAULT_BRD="192.168.49.255"
14
15 if [ ! -f @SBIN_DIR@/ip ]; then
16         interface=`@SBIN_DIR@/ifconfig -a|/bin/grep ${INTERFACE_NAME}|@BIN_DIR@/cut -d":" -f1`
17 else
18         interface=`@SBIN_DIR@/ip link|/bin/grep ${INTERFACE_NAME}|@BIN_DIR@/cut -d":" -f2`
19 fi
20 echo "Target is ${TARGET} and interface ${INTERFACE_PREFIX}: ${interface}."
21
22 start_dhcp_server()
23 {
24         if [ "X${interface}" == "X" ]; then
25                 echo "interface(${INTERFACE_PREFIX}) is not up"
26                 return 0
27         fi
28
29         /bin/rm /@TZ_SYS_VAR@/lib/misc/dhcpd.leases
30         /bin/touch @TZ_SYS_VAR@/lib/misc/dhcpd.leases
31         if [ ! -f @SBIN_DIR@/ip ]; then
32                 @SBIN_DIR@/ifconfig ${interface} ${DEFAULT_IP} up
33         else
34                 @SBIN_DIR@/ip addr add ${DEFAULT_NET} brd ${DEFAULT_BRD} dev ${interface}
35         fi
36         @SBIN_DIR@/dhcpd -S -i ${interface} @TZ_SYS_RO_ETC@/wifi-direct/dhcpd.conf -f &
37
38         route=`/bin/cat @TZ_SYS_RO_ETC@/wifi-direct/dhcpd.conf | /bin/grep router | /bin/awk '{print $3}'`
39         if [ -z $route ]; then
40                 route="192.168.49.1"
41         fi
42         subnet=`/bin/cat @TZ_SYS_RO_ETC@/wifi-direct/dhcpd.conf | /bin/grep subnet | /bin/awk '{print $3}'`
43
44         if [ -z $subnet ]; then
45                 subnet="255.255.255.0"
46         fi
47 }
48
49 start_dhcp_client()
50 {
51         if [ "X${interface}" == "X" ]; then
52                 echo "interface(${INTERFACE_PREFIX}) is not up"
53                 return 0
54         fi
55
56         @BIN_DIR@/vconftool set -t string memory/private/wifi_direct_manager/dhcpc_server_ip "0.0.0.0" -f
57         @BIN_DIR@/dhcp -S -i $interface -s @TZ_SYS_RO_ETC@/wifi-direct/udhcp_script.non-autoip &
58 }
59
60
61 clean_vconfs()
62 {
63         @BIN_DIR@/vconftool set -t string memory/private/wifi_direct_manager/p2p_ifname "" -f
64         @BIN_DIR@/vconftool set -t string memory/private/wifi_direct_manager/p2p_subnet_mask "" -f
65         @BIN_DIR@/vconftool set -t string memory/private/wifi_direct_manager/p2p_gateway "" -f
66         @BIN_DIR@/vconftool set -t string memory/private/wifi_direct_manager/p2p_local_ip "" -f
67         @BIN_DIR@/vconftool set -t string memory/private/wifi_direct_manager/dhcpc_server_ip "0.0.0.0" -f
68 }
69
70 stop_dhcp()
71 {
72         @BIN_DIR@/pkill -x dhcp
73         @BIN_DIR@/pkill -x dhcpd
74         if [ "X${interface}" == "X" ]; then
75                 echo "interface(${INTERFACE_PREFIX}) is not up"
76                 return 0
77         fi
78
79         if [ ! -f @SBIN_DIR@/ip ]; then
80                 @SBIN_DIR@/ifconfig ${interface} "0.0.0.0"
81         else
82                 @SBIN_DIR@/ip addr del ${local_ip_net} dev ${interface}
83         fi
84 }
85
86 is_running()
87 {
88         program=$1
89         run=`/bin/ps -eo comm|/bin/grep ${program}`
90         if [ "X${run}" == "X" ]; then
91                 echo "${program} is not running"
92         else
93                 echo "${program} is already running"
94         fi
95 }
96
97 status_dhcp()
98 {
99         is_running @BIN_DIR@/dhcp
100         is_running @SBIN_DIR@/dhcpd
101 }
102
103
104 case $1 in
105 "server")
106 stop_dhcp
107 start_dhcp_server
108 ;;
109 "client")
110 clean_vconfs
111 stop_dhcp
112 start_dhcp_client
113 ;;
114 "stop")
115 clean_vconfs
116 stop_dhcp
117 ;;
118 "status")
119 status_dhcp
120 ;;
121 *)
122 /bin/echo wifi-direct-dhcp.sh [server] [client] [stop] [status]
123 exit 1
124 ;;
125 esac
126