forget difference between big and small commands - obsolete with vm.
[minix.git] / commands / simple / ifconfig.c
blob4568dbfecc09f1bc8b98c66d9b9d321593bffdb0
1 /*
2 ifconfig.c
3 */
5 #define _POSIX_C_SOURCE 2
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>
21 #if __STDC__
22 #define PROTO(x,y) x y
23 #else
24 #define PROTO(x,y) x()
25 #endif
27 static PROTO (void usage, (void) );
28 static PROTO (void set_hostaddr, (int ip_fd, char *host_s, int ins) );
29 static PROTO (void set_netmask, (int ip_fd, char *net_s, int ins) );
30 static PROTO (void set_mtu, (int ip_fd, char *mtu_s) );
31 static PROTO (int check_ipaddrset, (int ip_fd) );
32 static PROTO (int check_netmaskset, (int ip_fd) );
33 static PROTO (int get_ipconf, (int ip_fd,
34 struct nwio_ipconf *ref_ipconf) );
35 PROTO (int main, (int argc, char *argv[]) );
37 char *prog_name;
39 main(argc, argv)
40 int argc;
41 char *argv[];
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;
50 prog_name= argv[0];
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)
61 switch(c)
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);
106 modify = (h_arg != NULL || n_arg != NULL || m_arg != NULL);
107 if (a_flag && modify) usage();
108 if (!modify) v_flag= 1;
110 if (modify) setuid(getuid());
112 if (optind != argc)
113 usage();
115 hostaddr_s= h_arg;
116 mtu_s= m_arg;
117 netmask_s= n_arg;
118 ins= i_flag;
120 ifno= 0;
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")];
132 sprintf(device, "/dev/ip%d", ifno);
133 device_s= device;
136 ip_fd= open (device_s, O_RDWR);
137 if (ip_fd<0)
139 if (a_flag && (errno == ENOENT || errno == ENXIO))
140 continue;
142 fprintf(stderr, "%s: Unable to open '%s': %s\n",
143 prog_name, device_s, strerror(errno));
144 exit(1);
147 if (hostaddr_s)
148 set_hostaddr(ip_fd, hostaddr_s, ins);
150 if (netmask_s)
151 set_netmask(ip_fd, netmask_s, ins);
153 if (mtu_s)
154 set_mtu(ip_fd, mtu_s);
156 if (v_flag) {
157 if (!get_ipconf(ip_fd, &ipconf))
159 if (!a_flag)
161 fprintf(stderr,
162 "%s: %s: Host address not set\n",
163 prog_name, device_s);
164 exit(1);
167 else
169 printf("%s: address %s", device_s,
170 inet_ntoa(ipconf.nwic_ipaddr));
172 if (ipconf.nwic_flags & NWIC_NETMASK_SET)
174 printf(" netmask %s",
175 inet_ntoa(ipconf.nwic_netmask));
177 #ifdef NWIC_MTU_SET
178 if (ipconf.nwic_mtu)
179 printf(" mtu %u", ipconf.nwic_mtu);
180 #endif
181 fputc('\n', stdout);
184 close(ip_fd);
185 } while (a_flag && ++ifno < 32);
186 exit(0);
189 static void set_hostaddr (ip_fd, hostaddr_s, ins)
190 int ip_fd;
191 char *hostaddr_s;
192 int ins;
194 ipaddr_t ipaddr;
195 struct nwio_ipconf ipconf;
196 int result;
198 ipaddr= inet_addr (hostaddr_s);
199 if (ipaddr == (ipaddr_t)(-1))
201 fprintf(stderr, "%s: Invalid host address (%s)\n",
202 prog_name, hostaddr_s);
203 exit(1);
205 if (ins && check_ipaddrset(ip_fd))
206 return;
208 ipconf.nwic_flags= NWIC_IPADDR_SET;
209 ipconf.nwic_ipaddr= ipaddr;
211 result= ioctl(ip_fd, NWIOSIPCONF, &ipconf);
212 if (result<0)
214 fprintf(stderr, "%s: Unable to set IP configuration: %s\n",
215 prog_name, strerror(errno));
216 exit(1);
220 static int check_ipaddrset (ip_fd)
221 int ip_fd;
223 struct nwio_ipconf ipconf;
225 if (!get_ipconf(ip_fd, &ipconf))
226 return 0;
228 assert (ipconf.nwic_flags & NWIC_IPADDR_SET);
230 return 1;
233 static int get_ipconf (ip_fd, ref_ipaddr)
234 int ip_fd;
235 struct nwio_ipconf *ref_ipaddr;
237 int flags;
238 int error, result;
239 nwio_ipconf_t ipconf;
241 flags= fcntl(ip_fd, F_GETFL);
242 fcntl(ip_fd, F_SETFL, flags | O_NONBLOCK);
244 result= ioctl (ip_fd, NWIOGIPCONF, &ipconf);
245 error= errno;
247 fcntl(ip_fd, F_SETFL, flags);
249 if (result <0 && error != EAGAIN)
251 errno= error;
252 fprintf(stderr, "%s: Unable to get IP configuration: %s\n",
253 prog_name, strerror(errno));
254 exit(1);
256 if (result == 0)
258 *ref_ipaddr = ipconf;
260 return result>=0;
263 static void usage()
265 fprintf(stderr,
266 "Usage: %s [-I ip-device] [-h ipaddr] [-n netmask] [-m mtu] [-iva]\n",
267 prog_name);
268 exit(1);
271 static void set_netmask (ip_fd, netmask_s, ins)
272 int ip_fd;
273 char *netmask_s;
274 int ins;
276 ipaddr_t netmask;
277 struct nwio_ipconf ipconf;
278 int result;
280 netmask= inet_addr (netmask_s);
281 if (netmask == (ipaddr_t)(-1))
283 fprintf(stderr, "%s: Invalid netmask (%s)\n",
284 prog_name, netmask_s);
285 exit(1);
287 if (ins && check_netmaskset(ip_fd))
288 return;
290 ipconf.nwic_flags= NWIC_NETMASK_SET;
291 ipconf.nwic_netmask= netmask;
293 result= ioctl(ip_fd, NWIOSIPCONF, &ipconf);
294 if (result<0)
296 fprintf(stderr, "%s: Unable to set IP configuration: %s\n",
297 prog_name, strerror(errno));
298 exit(1);
302 static void set_mtu (ip_fd, mtu_s)
303 int ip_fd;
304 char *mtu_s;
306 ipaddr_t netmask;
307 int result;
308 long mtu;
309 char *check;
310 struct nwio_ipconf ipconf;
312 mtu= strtol (mtu_s, &check, 0);
313 if (check[0] != '\0')
315 fprintf(stderr, "%s: Invalid mtu (%s)\n",
316 prog_name, mtu_s);
317 exit(1);
320 #ifdef NWIC_MTU_SET
321 ipconf.nwic_flags= NWIC_MTU_SET;
322 ipconf.nwic_mtu= mtu;
324 result= ioctl(ip_fd, NWIOSIPCONF, &ipconf);
325 if (result<0)
327 fprintf(stderr, "%s: Unable to set IP configuration: %s\n",
328 prog_name, strerror(errno));
329 exit(1);
331 #endif
334 static int check_netmaskset (ip_fd)
335 int ip_fd;
337 struct nwio_ipconf ipconf;
339 if (!get_ipconf(ip_fd, &ipconf))
341 fprintf(stderr,
342 "%s: Unable to determine if netmask is set, please set IP address first\n",
343 prog_name);
344 exit(1);
347 return (ipconf.nwic_flags & NWIC_NETMASK_SET);
351 * $PchId: ifconfig.c,v 1.7 2001/02/21 09:19:52 philip Exp $