Fix warnings when compiling for Windows.
[tinc] / src / ifconfig.c
1 /*
2     ifconfig.c -- Generate platform specific interface configuration commands
3     Copyright (C) 2016-2018 Guus Sliepen <guus@tinc-vpn.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include "conf.h"
23 #include "ifconfig.h"
24 #include "subnet.h"
25
26 static long start;
27
28 #ifndef HAVE_MINGW
29 void ifconfig_header(FILE *out) {
30         fprintf(out, "#!/bin/sh\n");
31         start = ftell(out);
32 }
33
34 void ifconfig_dhcp(FILE *out) {
35         fprintf(out, "dhclient -nw \"$INTERFACE\"\n");
36 }
37
38 void ifconfig_dhcp6(FILE *out) {
39         fprintf(out, "dhclient -6 -nw \"$INTERFACE\"\n");
40 }
41
42 void ifconfig_slaac(FILE *out) {
43 #ifdef HAVE_LINUX
44         fprintf(out, "echo 1 >\"/proc/sys/net/ipv6/conf/$INTERFACE/accept_ra\"\n");
45         fprintf(out, "echo 1 >\"/proc/sys/net/ipv6/conf/$INTERFACE/autoconf\"\n");
46 #else
47         fprintf(out, "rtsol \"$INTERFACE\" &\n");
48 #endif
49 }
50
51 bool ifconfig_footer(FILE *out) {
52         if(ftell(out) == start) {
53                 fprintf(out, "echo 'Unconfigured tinc-up script, please edit '$0'!'\n\n#ifconfig $INTERFACE <your vpn IP address> netmask <netmask of whole VPN>\n");
54                 return false;
55         } else {
56 #ifdef HAVE_LINUX
57                 fprintf(out, "ip link set \"$INTERFACE\" up\n");
58 #else
59                 fprintf(out, "ifconfig \"$INTERFACE\" up\n");
60 #endif
61                 return true;
62         }
63 }
64 #else
65 void ifconfig_header(FILE *out) {
66         start = ftell(out);
67 }
68
69 void ifconfig_dhcp(FILE *out) {
70         fprintf(out, "netsh interface ipv4 set address \"%%INTERFACE%%\" dhcp\n");
71 }
72
73 void ifconfig_dhcp6(FILE *out) {
74         (void)out;
75         fprintf(stderr, "DHCPv6 requested, but not supported by tinc on this platform\n");
76 }
77
78 void ifconfig_slaac(FILE *out) {
79         (void)out;
80         // It's the default?
81 }
82
83 bool ifconfig_footer(FILE *out) {
84         return ftell(out) != start;
85 }
86 #endif
87
88 static subnet_t ipv4, ipv6;
89
90 void ifconfig_address(FILE *out, const char *value) {
91         subnet_t address = {0};
92         char address_str[MAXNETSTR];
93
94         if(!str2net(&address, value) || !net2str(address_str, sizeof(address_str), &address)) {
95                 fprintf(stderr, "Could not parse address in Ifconfig statement\n");
96                 return;
97         }
98
99         switch(address.type) {
100         case SUBNET_IPV4:
101                 ipv4 = address;
102                 break;
103
104         case SUBNET_IPV6:
105                 ipv6 = address;
106                 break;
107
108         default:
109                 return;
110         }
111
112 #if defined(HAVE_LINUX)
113
114         switch(address.type) {
115         case SUBNET_MAC:
116                 fprintf(out, "ip link set \"$INTERFACE\" address %s\n", address_str);
117                 break;
118
119         case SUBNET_IPV4:
120                 fprintf(out, "ip addr replace %s dev \"$INTERFACE\"\n", address_str);
121                 break;
122
123         case SUBNET_IPV6:
124                 fprintf(out, "ip addr replace %s dev \"$INTERFACE\"\n", address_str);
125                 break;
126
127         default:
128                 return;
129         }
130
131 #elif defined(HAVE_MINGW) || defined(HAVE_CYGWIN)
132
133         switch(address.type) {
134         case SUBNET_MAC:
135                 fprintf(out, "ip link set \"$INTERFACE\" address %s\n", address_str);
136                 break;
137
138         case SUBNET_IPV4:
139                 fprintf(out, "netsh inetface ipv4 set address \"%%INTERFACE%%\" static %s\n", address_str);
140                 break;
141
142         case SUBNET_IPV6:
143                 fprintf(out, "netsh inetface ipv6 set address \"%%INTERFACE%%\" %s\n", address_str);
144                 break;
145
146         default:
147                 return;
148         }
149
150 #else // assume BSD
151
152         switch(address.type) {
153         case SUBNET_MAC:
154                 fprintf(out, "ifconfig \"$INTERFACE\" link %s\n", address_str);
155                 break;
156
157         case SUBNET_IPV4:
158                 fprintf(out, "ifconfig \"$INTERFACE\" %s\n", address_str);
159                 break;
160
161         case SUBNET_IPV6:
162                 fprintf(out, "ifconfig \"$INTERFACE\" inet6 %s\n", address_str);
163                 break;
164
165         default:
166                 return;
167         }
168
169 #endif
170 }
171
172 void ifconfig_route(FILE *out, const char *value) {
173         subnet_t subnet = {0}, gateway = {0};
174         char subnet_str[MAXNETSTR] = "", gateway_str[MAXNETSTR] = "";
175         char *sep = strchr(value, ' ');
176
177         if(sep) {
178                 *sep++ = 0;
179         }
180
181         if(!str2net(&subnet, value) || !net2str(subnet_str, sizeof(subnet_str), &subnet) || subnet.type == SUBNET_MAC) {
182                 fprintf(stderr, "Could not parse subnet in Route statement\n");
183                 return;
184         }
185
186         if(sep) {
187                 if(!str2net(&gateway, sep) || !net2str(gateway_str, sizeof(gateway_str), &gateway) || gateway.type != subnet.type) {
188                         fprintf(stderr, "Could not parse gateway in Route statement\n");
189                         return;
190                 }
191
192                 char *slash = strchr(gateway_str, '/');
193
194                 if(slash) {
195                         *slash = 0;
196                 }
197         }
198
199 #if defined(HAVE_LINUX)
200
201         if(*gateway_str) {
202                 switch(subnet.type) {
203                 case SUBNET_IPV4:
204                         fprintf(out, "ip route add %s via %s dev \"$INTERFACE\" onlink\n", subnet_str, gateway_str);
205                         break;
206
207                 case SUBNET_IPV6:
208                         fprintf(out, "ip route add %s via %s dev \"$INTERFACE\" onlink\n", subnet_str, gateway_str);
209                         break;
210
211                 default:
212                         return;
213                 }
214         } else {
215                 switch(subnet.type) {
216                 case SUBNET_IPV4:
217                         fprintf(out, "ip route add %s dev \"$INTERFACE\"\n", subnet_str);
218                         break;
219
220                 case SUBNET_IPV6:
221                         fprintf(out, "ip route add %s dev \"$INTERFACE\"\n", subnet_str);
222                         break;
223
224                 default:
225                         return;
226                 }
227         }
228
229 #elif defined(HAVE_MINGW) || defined(HAVE_CYGWIN)
230
231         if(*gateway_str) {
232                 switch(subnet.type) {
233                 case SUBNET_IPV4:
234                         fprintf(out, "netsh interface ipv4 add route %s \"%%INTERFACE%%\" %s\n", subnet_str, gateway_str);
235                         break;
236
237                 case SUBNET_IPV6:
238                         fprintf(out, "netsh interface ipv6 add route %s \"%%INTERFACE%%\" %s\n", subnet_str, gateway_str);
239                         break;
240
241                 default:
242                         return;
243                 }
244         } else {
245                 switch(subnet.type) {
246                 case SUBNET_IPV4:
247                         fprintf(out, "netsh interface ipv4 add route %s \"%%INTERFACE%%\"\n", subnet_str);
248                         break;
249
250                 case SUBNET_IPV6:
251                         fprintf(out, "netsh interface ipv6 add route %s \"%%INTERFACE%%\"\n", subnet_str);
252                         break;
253
254                 default:
255                         return;
256                 }
257         }
258
259 #else // assume BSD
260
261         if(!*gateway_str) {
262                 switch(subnet.type) {
263                 case SUBNET_IPV4:
264                         if(!ipv4.type) {
265                                 fprintf(stderr, "Route requested but no Ifconfig\n");
266                                 return;
267                         }
268
269                         net2str(gateway_str, sizeof(gateway_str), &ipv4);
270                         break;
271
272                 case SUBNET_IPV6:
273                         if(!ipv6.type) {
274                                 fprintf(stderr, "Route requested but no Ifconfig\n");
275                                 return;
276                         }
277
278                         net2str(gateway_str, sizeof(gateway_str), &ipv6);
279                         break;
280
281                 default:
282                         return;
283                 }
284
285                 char *slash = strchr(gateway_str, '/');
286
287                 if(slash) {
288                         *slash = 0;
289                 }
290         }
291
292         switch(subnet.type) {
293         case SUBNET_IPV4:
294                 fprintf(out, "route add %s %s\n", subnet_str, gateway_str);
295                 break;
296
297         case SUBNET_IPV6:
298                 fprintf(out, "route add -inet6 %s %s\n", subnet_str, gateway_str);
299                 break;
300
301         default:
302                 return;
303         }
304
305 #endif
306 }