source: trunk/minix/commands/simple/add_route.c@ 15

Last change on this file since 15 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 6.7 KB
Line 
1/*
2add_route.c
3
4Created August 7, 1991 by Philip Homburg
5*/
6
7#define _POSIX_C_SOURCE 2
8
9#include <sys/types.h>
10#include <sys/ioctl.h>
11#include <errno.h>
12#include <fcntl.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <unistd.h>
17
18#include <net/hton.h>
19#include <net/netlib.h>
20#include <net/gen/netdb.h>
21#include <net/gen/in.h>
22#include <net/gen/inet.h>
23#include <net/gen/route.h>
24#include <net/gen/socket.h>
25#include <net/gen/ip_io.h>
26
27static char *prog_name;
28static enum { ADD, DEL } action;
29
30static void usage(void);
31static int name_to_ip(char *name, ipaddr_t *addr);
32static int parse_cidr(char *cidr, ipaddr_t *addr, ipaddr_t *mask);
33
34int main(int argc, char *argv[])
35{
36 struct netent *netent;
37 ipaddr_t gateway, destination, netmask, defaultmask=0;
38 u8_t high_byte;
39 nwio_route_t route;
40 int ip_fd, itab;
41 int r;
42 int metric;
43 char *check;
44 char *ip_device;
45 char *netmask_str, *metric_str, *destination_str, *gateway_str;
46 int c;
47 char *d_arg, *g_arg, *m_arg, *n_arg, *I_arg;
48 int i_flag, o_flag, D_flag, v_flag;
49 int cidr;
50
51 prog_name= strrchr(argv[0], '/');
52 if (prog_name == NULL) prog_name= argv[0]; else prog_name++;
53
54 if (strcmp(prog_name, "add_route") == 0)
55 action= ADD;
56 else if (strcmp(prog_name, "del_route") == 0)
57 action= DEL;
58 else
59 {
60 fprintf(stderr, "Don't know what to do when named '%s'\n",
61 prog_name);
62 exit(1);
63 }
64
65 i_flag= 0;
66 o_flag= 0;
67 D_flag= 0;
68 v_flag= 0;
69 g_arg= NULL;
70 d_arg= NULL;
71 m_arg= NULL;
72 n_arg= NULL;
73 I_arg= NULL;
74 while ((c= getopt(argc, argv, "iovDg:d:m:n:I:?")) != -1)
75 {
76 switch(c)
77 {
78 case 'i':
79 if (i_flag)
80 usage();
81 i_flag= 1;
82 break;
83 case 'o':
84 if (o_flag)
85 usage();
86 o_flag= 1;
87 break;
88 case 'v':
89 if (v_flag)
90 usage();
91 v_flag= 1;
92 break;
93 case 'D':
94 if (D_flag)
95 usage();
96 D_flag= 1;
97 break;
98 case 'g':
99 if (g_arg)
100 usage();
101 g_arg= optarg;
102 break;
103 case 'd':
104 if (d_arg)
105 usage();
106 d_arg= optarg;
107 break;
108 case 'm':
109 if (m_arg)
110 usage();
111 m_arg= optarg;
112 break;
113 case 'n':
114 if (n_arg)
115 usage();
116 n_arg= optarg;
117 break;
118 case 'I':
119 if (I_arg)
120 usage();
121 I_arg= optarg;
122 break;
123 case '?':
124 usage();
125 default:
126 fprintf(stderr, "%s: getopt failed\n", prog_name);
127 exit(1);
128 }
129 }
130 if (optind != argc)
131 usage();
132 if (i_flag && o_flag)
133 usage();
134 itab= i_flag;
135
136 if (i_flag)
137 {
138 if (g_arg == NULL || d_arg == NULL || m_arg == NULL)
139 usage();
140 }
141 else
142 {
143 if (g_arg == NULL || (d_arg == NULL && n_arg != NULL))
144 {
145 usage();
146 }
147 }
148
149 gateway_str= g_arg;
150 destination_str= d_arg;
151 metric_str= m_arg;
152 netmask_str= n_arg;
153 ip_device= I_arg;
154
155 if (!name_to_ip(gateway_str, &gateway))
156 {
157 fprintf(stderr, "%s: unknown host '%s'\n", prog_name,
158 gateway_str);
159 exit(1);
160 }
161
162 destination= 0;
163 netmask= 0;
164 cidr= 0;
165
166 if (destination_str)
167 {
168 if (parse_cidr(destination_str, &destination, &netmask))
169 cidr= 1;
170 else if (inet_aton(destination_str, &destination))
171 ;
172 else if ((netent= getnetbyname(destination_str)) != NULL)
173 destination= netent->n_net;
174 else if (!name_to_ip(destination_str, &destination))
175 {
176 fprintf(stderr, "%s: unknown network/host '%s'\n",
177 prog_name, destination_str);
178 exit(1);
179 }
180 high_byte= *(u8_t *)&destination;
181 if (!(high_byte & 0x80)) /* class A or 0 */
182 {
183 if (destination)
184 defaultmask= HTONL(0xff000000);
185 }
186 else if (!(high_byte & 0x40)) /* class B */
187 {
188 defaultmask= HTONL(0xffff0000);
189 }
190 else if (!(high_byte & 0x20)) /* class C */
191 {
192 defaultmask= HTONL(0xffffff00);
193 }
194 else /* class D is multicast ... */
195 {
196 fprintf(stderr, "%s: Warning: Martian address '%s'\n",
197 prog_name, inet_ntoa(destination));
198 defaultmask= HTONL(0xffffffff);
199 }
200 if (destination & ~defaultmask)
201 {
202 /* host route */
203 defaultmask= HTONL(0xffffffff);
204 }
205 if (!cidr)
206 netmask= defaultmask;
207 }
208
209 if (netmask_str)
210 {
211 if (cidr)
212 usage();
213 if (inet_aton(netmask_str, &netmask) == 0)
214 {
215 fprintf(stderr, "%s: illegal netmask'%s'\n", prog_name,
216 netmask_str);
217 exit(1);
218 }
219 }
220
221 if (metric_str)
222 {
223 metric= strtol(metric_str, &check, 0);
224 if (check[0] != '\0' || metric < 1)
225 {
226 fprintf(stderr, "%s: illegal metric %s\n",
227 prog_name, metric_str);
228 }
229 }
230 else
231 metric= 1;
232
233 if (!ip_device)
234 ip_device= getenv("IP_DEVICE");
235 if (!ip_device)
236 ip_device= IP_DEVICE;
237
238 ip_fd= open(ip_device, O_RDWR);
239 if (ip_fd == -1)
240 {
241 fprintf(stderr, "%s: unable to open('%s'): %s\n",
242 prog_name, ip_device, strerror(errno));
243 exit(1);
244 }
245
246 if (v_flag)
247 {
248 printf("%s %s route to %s ",
249 action == ADD ? "adding" : "deleting",
250 itab ? "input" : "output",
251 inet_ntoa(destination));
252 printf("with netmask %s ", inet_ntoa(netmask));
253 printf("using gateway %s", inet_ntoa(gateway));
254 if (itab && action == ADD)
255 printf(" at distance %d", metric);
256 printf("\n");
257 }
258
259 route.nwr_ent_no= 0;
260 route.nwr_dest= destination;
261 route.nwr_netmask= netmask;
262 route.nwr_gateway= gateway;
263 route.nwr_dist= action == ADD ? metric : 0;
264 route.nwr_flags= (action == DEL && D_flag) ? 0 : NWRF_STATIC;
265 route.nwr_pref= 0;
266 route.nwr_mtu= 0;
267
268 if (action == ADD)
269 r= ioctl(ip_fd, itab ? NWIOSIPIROUTE : NWIOSIPOROUTE, &route);
270 else
271 r= ioctl(ip_fd, itab ? NWIODIPIROUTE : NWIODIPOROUTE, &route);
272 if (r == -1)
273 {
274 fprintf(stderr, "%s: NWIO%cIP%cROUTE: %s\n",
275 prog_name,
276 action == ADD ? 'S' : 'D',
277 itab ? 'I' : 'O',
278 strerror(errno));
279 exit(1);
280 }
281 return(0);
282}
283
284static void usage(void)
285{
286 fprintf(stderr,
287 "Usage: %s\n"
288 "\t[-o] %s-g gw [-d dst [-n netmask]] %s[-I ipdev] [-v]\n"
289 "\t-i %s-g gw -d dst [-n netmask] %s[-I ipdev] [-v]\n"
290 "Note: <dst> may be in CIDR notation\n",
291 prog_name,
292 action == DEL ? "[-D] " : "",
293 action == ADD ? "[-m metric] " : "",
294 action == DEL ? "[-D] " : "",
295 action == ADD ? "-m metric " : ""
296 );
297 exit(1);
298}
299
300static int name_to_ip(char *name, ipaddr_t *addr)
301{
302 /* Translate a name to an IP address. Try first with inet_aton(), then
303 * with gethostbyname(). (The latter can also recognize an IP address,
304 * but only decimals with at least one dot).)
305 */
306 struct hostent *hostent;
307
308 if (!inet_aton(name, addr)) {
309 if ((hostent= gethostbyname(name)) == NULL) return 0;
310 if (hostent->h_addrtype != AF_INET) return 0;
311 if (hostent->h_length != sizeof(*addr)) return 0;
312 memcpy(addr, hostent->h_addr, sizeof(*addr));
313 }
314 return 1;
315}
316
317static int parse_cidr(char *cidr, ipaddr_t *addr, ipaddr_t *mask)
318{
319 char *slash, *check;
320 ipaddr_t a;
321 int ok;
322 unsigned long len;
323
324 if ((slash= strchr(cidr, '/')) == NULL)
325 return 0;
326
327 *slash++= 0;
328 ok= 1;
329
330 if (!inet_aton(cidr, &a))
331 ok= 0;
332
333 len= strtoul(slash, &check, 10);
334 if (check == slash || *check != 0 || len > 32)
335 ok= 0;
336
337 *--slash= '/';
338 if (!ok)
339 return 0;
340 *addr= a;
341 *mask= htonl(len == 0 ? 0 : (0xFFFFFFFF << (32-len)) & 0xFFFFFFFF);
342 return 1;
343}
344
345/*
346 * $PchId: add_route.c,v 1.6 2001/04/20 10:45:07 philip Exp $
347 */
Note: See TracBrowser for help on using the repository browser.