source: trunk/minix/commands/simple/ifconfig.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.2 KB
Line 
1/*
2ifconfig.c
3*/
4
5#define _POSIX_C_SOURCE 2
6
7#include <sys/types.h>
8#include <assert.h>
9#include <errno.h>
10#include <fcntl.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <unistd.h>
15#include <sys/ioctl.h>
16#include <net/netlib.h>
17#include <net/gen/in.h>
18#include <net/gen/ip_io.h>
19#include <net/gen/inet.h>
20
21#if __STDC__
22#define PROTO(x,y) x y
23#else
24#define PROTO(x,y) x()
25#endif
26
27static PROTO (void usage, (void) );
28static PROTO (void set_hostaddr, (int ip_fd, char *host_s, int ins) );
29static PROTO (void set_netmask, (int ip_fd, char *net_s, int ins) );
30static PROTO (void set_mtu, (int ip_fd, char *mtu_s) );
31static PROTO (int check_ipaddrset, (int ip_fd) );
32static PROTO (int check_netmaskset, (int ip_fd) );
33static PROTO (int get_ipconf, (int ip_fd,
34 struct nwio_ipconf *ref_ipconf) );
35PROTO (int main, (int argc, char *argv[]) );
36
37char *prog_name;
38
39main(argc, argv)
40int argc;
41char *argv[];
42{
43 char *device_s, *hostaddr_s, *mtu_s, *netmask_s, **arg_s;
44 int ins;
45 int c, ip_fd, ifno;
46 struct nwio_ipconf ipconf;
47 int i_flag, v_flag, a_flag, modify;
48 char *d_arg, *h_arg, *m_arg, *n_arg;
49
50 prog_name= argv[0];
51
52 d_arg= NULL;
53 h_arg= NULL;
54 m_arg= NULL;
55 n_arg= NULL;
56 i_flag= 0;
57 v_flag= 0;
58 a_flag= 0;
59 while ((c= getopt(argc, argv, "?I:h:m:n:iva")) != -1)
60 {
61 switch(c)
62 {
63 case '?':
64 usage();
65 case 'I':
66 if (d_arg)
67 usage();
68 d_arg= optarg;
69 break;
70 case 'h':
71 if (h_arg)
72 usage();
73 h_arg= optarg;
74 break;
75 case 'm':
76 if (m_arg)
77 usage();
78 m_arg= optarg;
79 break;
80 case 'n':
81 if (n_arg)
82 usage();
83 n_arg= optarg;
84 break;
85 case 'i':
86 if (i_flag)
87 usage();
88 i_flag= 1;
89 break;
90 case 'v':
91 if (v_flag)
92 usage();
93 v_flag= 1;
94 break;
95 case 'a':
96 if (a_flag)
97 usage();
98 a_flag= 1;
99 break;
100 default:
101 fprintf(stderr, "%s: getopt failed: '%c'\n",
102 prog_name, c);
103 exit(1);
104 }
105 }
106 modify = (h_arg != NULL || n_arg != NULL || m_arg != NULL);
107 if (a_flag && modify) usage();
108 if (!modify) v_flag= 1;
109
110 if (modify) setuid(getuid());
111
112 if (optind != argc)
113 usage();
114
115 hostaddr_s= h_arg;
116 mtu_s= m_arg;
117 netmask_s= n_arg;
118 ins= i_flag;
119
120 ifno= 0;
121 do
122 {
123 if (!a_flag) {
124 device_s= d_arg;
125 if (device_s == NULL)
126 device_s= getenv("IP_DEVICE");
127 if (device_s == NULL)
128 device_s= IP_DEVICE;
129 } else {
130 static char device[sizeof("/dev/ip99")];
131
132 sprintf(device, "/dev/ip%d", ifno);
133 device_s= device;
134 }
135
136 ip_fd= open (device_s, O_RDWR);
137 if (ip_fd<0)
138 {
139 if (a_flag && (errno == ENOENT || errno == ENXIO))
140 continue;
141
142 fprintf(stderr, "%s: Unable to open '%s': %s\n",
143 prog_name, device_s, strerror(errno));
144 exit(1);
145 }
146
147 if (hostaddr_s)
148 set_hostaddr(ip_fd, hostaddr_s, ins);
149
150 if (netmask_s)
151 set_netmask(ip_fd, netmask_s, ins);
152
153 if (mtu_s)
154 set_mtu(ip_fd, mtu_s);
155
156 if (v_flag) {
157 if (!get_ipconf(ip_fd, &ipconf))
158 {
159 if (!a_flag)
160 {
161 fprintf(stderr,
162 "%s: %s: Host address not set\n",
163 prog_name, device_s);
164 exit(1);
165 }
166 }
167 else
168 {
169 printf("%s: address %s", device_s,
170 inet_ntoa(ipconf.nwic_ipaddr));
171
172 if (ipconf.nwic_flags & NWIC_NETMASK_SET)
173 {
174 printf(" netmask %s",
175 inet_ntoa(ipconf.nwic_netmask));
176 }
177#ifdef NWIC_MTU_SET
178 if (ipconf.nwic_mtu)
179 printf(" mtu %u", ipconf.nwic_mtu);
180#endif
181 fputc('\n', stdout);
182 }
183 }
184 close(ip_fd);
185 } while (a_flag && ++ifno < 32);
186 exit(0);
187}
188
189static void set_hostaddr (ip_fd, hostaddr_s, ins)
190int ip_fd;
191char *hostaddr_s;
192int ins;
193{
194 ipaddr_t ipaddr;
195 struct nwio_ipconf ipconf;
196 int result;
197
198 ipaddr= inet_addr (hostaddr_s);
199 if (ipaddr == (ipaddr_t)(-1))
200 {
201 fprintf(stderr, "%s: Invalid host address (%s)\n",
202 prog_name, hostaddr_s);
203 exit(1);
204 }
205 if (ins && check_ipaddrset(ip_fd))
206 return;
207
208 ipconf.nwic_flags= NWIC_IPADDR_SET;
209 ipconf.nwic_ipaddr= ipaddr;
210
211 result= ioctl(ip_fd, NWIOSIPCONF, &ipconf);
212 if (result<0)
213 {
214 fprintf(stderr, "%s: Unable to set IP configuration: %s\n",
215 prog_name, strerror(errno));
216 exit(1);
217 }
218}
219
220static int check_ipaddrset (ip_fd)
221int ip_fd;
222{
223 struct nwio_ipconf ipconf;
224
225 if (!get_ipconf(ip_fd, &ipconf))
226 return 0;
227
228 assert (ipconf.nwic_flags & NWIC_IPADDR_SET);
229
230 return 1;
231}
232
233static int get_ipconf (ip_fd, ref_ipaddr)
234int ip_fd;
235struct nwio_ipconf *ref_ipaddr;
236{
237 int flags;
238 int error, result;
239 nwio_ipconf_t ipconf;
240
241 flags= fcntl(ip_fd, F_GETFL);
242 fcntl(ip_fd, F_SETFL, flags | O_NONBLOCK);
243
244 result= ioctl (ip_fd, NWIOGIPCONF, &ipconf);
245 error= errno;
246
247 fcntl(ip_fd, F_SETFL, flags);
248
249 if (result <0 && error != EAGAIN)
250 {
251 errno= error;
252 fprintf(stderr, "%s: Unable to get IP configuration: %s\n",
253 prog_name, strerror(errno));
254 exit(1);
255 }
256 if (result == 0)
257 {
258 *ref_ipaddr = ipconf;
259 }
260 return result>=0;
261}
262
263static void usage()
264{
265 fprintf(stderr,
266 "Usage: %s [-I ip-device] [-h ipaddr] [-n netmask] [-m mtu] [-iva]\n",
267 prog_name);
268 exit(1);
269}
270
271static void set_netmask (ip_fd, netmask_s, ins)
272int ip_fd;
273char *netmask_s;
274int ins;
275{
276 ipaddr_t netmask;
277 struct nwio_ipconf ipconf;
278 int result;
279
280 netmask= inet_addr (netmask_s);
281 if (netmask == (ipaddr_t)(-1))
282 {
283 fprintf(stderr, "%s: Invalid netmask (%s)\n",
284 prog_name, netmask_s);
285 exit(1);
286 }
287 if (ins && check_netmaskset(ip_fd))
288 return;
289
290 ipconf.nwic_flags= NWIC_NETMASK_SET;
291 ipconf.nwic_netmask= netmask;
292
293 result= ioctl(ip_fd, NWIOSIPCONF, &ipconf);
294 if (result<0)
295 {
296 fprintf(stderr, "%s: Unable to set IP configuration: %s\n",
297 prog_name, strerror(errno));
298 exit(1);
299 }
300}
301
302static void set_mtu (ip_fd, mtu_s)
303int ip_fd;
304char *mtu_s;
305{
306 ipaddr_t netmask;
307 int result;
308 long mtu;
309 char *check;
310 struct nwio_ipconf ipconf;
311
312 mtu= strtol (mtu_s, &check, 0);
313 if (check[0] != '\0')
314 {
315 fprintf(stderr, "%s: Invalid mtu (%s)\n",
316 prog_name, mtu_s);
317 exit(1);
318 }
319
320#ifdef NWIC_MTU_SET
321 ipconf.nwic_flags= NWIC_MTU_SET;
322 ipconf.nwic_mtu= mtu;
323
324 result= ioctl(ip_fd, NWIOSIPCONF, &ipconf);
325 if (result<0)
326 {
327 fprintf(stderr, "%s: Unable to set IP configuration: %s\n",
328 prog_name, strerror(errno));
329 exit(1);
330 }
331#endif
332}
333
334static int check_netmaskset (ip_fd)
335int ip_fd;
336{
337 struct nwio_ipconf ipconf;
338
339 if (!get_ipconf(ip_fd, &ipconf))
340 {
341 fprintf(stderr,
342"%s: Unable to determine if netmask is set, please set IP address first\n",
343 prog_name);
344 exit(1);
345 }
346
347 return (ipconf.nwic_flags & NWIC_NETMASK_SET);
348}
349
350/*
351 * $PchId: ifconfig.c,v 1.7 2001/02/21 09:19:52 philip Exp $
352 */
Note: See TracBrowser for help on using the repository browser.