5 #define _POSIX_C_SOURCE 2
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>
22 #define PROTO(x,y) x y
24 #define PROTO(x,y) x()
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
[]) );
43 char *device_s
, *hostaddr_s
, *mtu_s
, *netmask_s
, **arg_s
;
46 struct nwio_ipconf ipconf
;
47 int i_flag
, v_flag
, a_flag
, modify
;
48 char *d_arg
, *h_arg
, *m_arg
, *n_arg
;
59 while ((c
= getopt(argc
, argv
, "?I:h:m:n:iva")) != -1)
101 fprintf(stderr
, "%s: getopt failed: '%c'\n",
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());
125 if (device_s
== NULL
)
126 device_s
= getenv("IP_DEVICE");
127 if (device_s
== NULL
)
130 static char device
[sizeof("/dev/ip99")];
132 sprintf(device
, "/dev/ip%d", ifno
);
136 ip_fd
= open (device_s
, O_RDWR
);
139 if (a_flag
&& (errno
== ENOENT
|| errno
== ENXIO
))
142 fprintf(stderr
, "%s: Unable to open '%s': %s\n",
143 prog_name
, device_s
, strerror(errno
));
148 set_hostaddr(ip_fd
, hostaddr_s
, ins
);
151 set_netmask(ip_fd
, netmask_s
, ins
);
154 set_mtu(ip_fd
, mtu_s
);
157 if (!get_ipconf(ip_fd
, &ipconf
))
162 "%s: %s: Host address not set\n",
163 prog_name
, device_s
);
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
));
179 printf(" mtu %u", ipconf
.nwic_mtu
);
185 } while (a_flag
&& ++ifno
< 32);
189 static void set_hostaddr (ip_fd
, hostaddr_s
, ins
)
195 struct nwio_ipconf ipconf
;
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
);
205 if (ins
&& check_ipaddrset(ip_fd
))
208 ipconf
.nwic_flags
= NWIC_IPADDR_SET
;
209 ipconf
.nwic_ipaddr
= ipaddr
;
211 result
= ioctl(ip_fd
, NWIOSIPCONF
, &ipconf
);
214 fprintf(stderr
, "%s: Unable to set IP configuration: %s\n",
215 prog_name
, strerror(errno
));
220 static int check_ipaddrset (ip_fd
)
223 struct nwio_ipconf ipconf
;
225 if (!get_ipconf(ip_fd
, &ipconf
))
228 assert (ipconf
.nwic_flags
& NWIC_IPADDR_SET
);
233 static int get_ipconf (ip_fd
, ref_ipaddr
)
235 struct nwio_ipconf
*ref_ipaddr
;
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
);
247 fcntl(ip_fd
, F_SETFL
, flags
);
249 if (result
<0 && error
!= EAGAIN
)
252 fprintf(stderr
, "%s: Unable to get IP configuration: %s\n",
253 prog_name
, strerror(errno
));
258 *ref_ipaddr
= ipconf
;
266 "Usage: %s [-I ip-device] [-h ipaddr] [-n netmask] [-m mtu] [-iva]\n",
271 static void set_netmask (ip_fd
, netmask_s
, ins
)
277 struct nwio_ipconf ipconf
;
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
);
287 if (ins
&& check_netmaskset(ip_fd
))
290 ipconf
.nwic_flags
= NWIC_NETMASK_SET
;
291 ipconf
.nwic_netmask
= netmask
;
293 result
= ioctl(ip_fd
, NWIOSIPCONF
, &ipconf
);
296 fprintf(stderr
, "%s: Unable to set IP configuration: %s\n",
297 prog_name
, strerror(errno
));
302 static void set_mtu (ip_fd
, mtu_s
)
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",
321 ipconf
.nwic_flags
= NWIC_MTU_SET
;
322 ipconf
.nwic_mtu
= mtu
;
324 result
= ioctl(ip_fd
, NWIOSIPCONF
, &ipconf
);
327 fprintf(stderr
, "%s: Unable to set IP configuration: %s\n",
328 prog_name
, strerror(errno
));
334 static int check_netmaskset (ip_fd
)
337 struct nwio_ipconf ipconf
;
339 if (!get_ipconf(ip_fd
, &ipconf
))
342 "%s: Unable to determine if netmask is set, please set IP address first\n",
347 return (ipconf
.nwic_flags
& NWIC_NETMASK_SET
);
351 * $PchId: ifconfig.c,v 1.7 2001/02/21 09:19:52 philip Exp $