2 ** Copyright 2003, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
8 #include <newos/errors.h>
10 #include <sys/syscalls.h>
13 static int usage(const char *argv
[])
16 printf("%s\n", argv
[0]);
17 printf("\tif create <interface>\n");
18 printf("\tif delete <interface>\n");
19 printf("\tif addaddr <interface> ipv4 addr <ip address> mask <netmask> broadcast <broadcast>\n");
20 printf("\tif rmaddr <interface> ipv4 addr <ip address>\n");
21 printf("\tif list\n");
23 printf("\troute add default ipv4 addr <ip address> if <interface> ipv4 addr <ip address>\n");
24 printf("\troute add net ipv4 addr <ip address> mask <netmask> if <interface> ipv4 addr <ip address>\n");
25 printf("\troute delete ipv4 addr <ip address>\n");
26 printf("\troute list\n");
31 static int parse_ipv4_addr_string(ipv4_addr
*ip_addr
, const char *_ip_addr_string
)
34 char ip_addr_string
[128];
36 strlcpy(ip_addr_string
, _ip_addr_string
, sizeof(ip_addr_string
));
40 // walk through the first number
43 for(; ip_addr_string
[b
] != 0 && ip_addr_string
[b
] != '.'; b
++)
45 if(ip_addr_string
[b
] == 0)
47 ip_addr_string
[b
] = 0;
48 *ip_addr
= atoi(&ip_addr_string
[a
]) << 24;
53 for(; ip_addr_string
[b
] != 0 && ip_addr_string
[b
] != '.'; b
++)
55 if(ip_addr_string
[b
] == 0)
57 ip_addr_string
[b
] = 0;
58 *ip_addr
|= atoi(&ip_addr_string
[a
]) << 16;
63 for(; ip_addr_string
[b
] != 0 && ip_addr_string
[b
] != '.'; b
++)
65 if(ip_addr_string
[b
] == 0)
67 ip_addr_string
[b
] = 0;
68 *ip_addr
|= atoi(&ip_addr_string
[a
]) << 8;
73 for(; ip_addr_string
[b
] != 0 && ip_addr_string
[b
] != '.'; b
++)
75 ip_addr_string
[b
] = 0;
76 *ip_addr
|= atoi(&ip_addr_string
[a
]);
81 static int do_if(int argc
, const char *argv
[], int curr_arg
)
84 const char *interface_name
= NULL
;
85 ipv4_addr if_addr
= 0;
86 ipv4_addr mask_addr
= 0;
87 ipv4_addr broadcast_addr
= 0;
88 struct _ioctl_net_if_control_struct control
;
93 /* parse the args for the various commands */
94 if(!strncasecmp(argv
[curr_arg
], "create", sizeof("create"))) {
95 if(curr_arg
+ 1 >= argc
)
97 op
= IOCTL_NET_CONTROL_IF_CREATE
;
98 interface_name
= argv
[curr_arg
+ 1];
99 } else if(!strncasecmp(argv
[curr_arg
], "delete", sizeof("delete"))) {
100 if(curr_arg
+ 1 >= argc
)
102 op
= IOCTL_NET_CONTROL_IF_DELETE
;
103 interface_name
= argv
[curr_arg
+ 1];
104 } else if(!strncasecmp(argv
[curr_arg
], "addaddr", sizeof("addaddr"))) {
105 if(curr_arg
+ 8 >= argc
)
107 op
= IOCTL_NET_CONTROL_IF_ADDADDR
;
108 interface_name
= argv
[curr_arg
+ 1];
109 if(strncasecmp(argv
[curr_arg
+ 2], "ipv4", sizeof("ipv4")) ||
110 strncasecmp(argv
[curr_arg
+ 3], "addr", sizeof("addr")) ||
111 strncasecmp(argv
[curr_arg
+ 5], "mask", sizeof("mask")) ||
112 strncasecmp(argv
[curr_arg
+ 7], "broadcast", sizeof("broadcast")))
114 parse_ipv4_addr_string(&if_addr
, argv
[curr_arg
+ 4]);
115 parse_ipv4_addr_string(&mask_addr
, argv
[curr_arg
+ 6]);
116 parse_ipv4_addr_string(&broadcast_addr
, argv
[curr_arg
+ 8]);
117 } else if(!strncasecmp(argv
[curr_arg
], "rmaddr", sizeof("rmaddr"))) {
118 if(curr_arg
+ 4 >= argc
)
120 op
= IOCTL_NET_CONTROL_IF_RMADDR
;
121 interface_name
= argv
[curr_arg
+ 1];
122 if(strncasecmp(argv
[curr_arg
+ 2], "ipv4", sizeof("ipv4")) ||
123 strncasecmp(argv
[curr_arg
+ 3], "addr", sizeof("addr")))
125 parse_ipv4_addr_string(&if_addr
, argv
[curr_arg
+ 4]);
126 } else if(!strncasecmp(argv
[curr_arg
], "list", sizeof("list"))) {
127 op
= IOCTL_NET_CONTROL_IF_LIST
;
132 // printf("do_if: op %d, interface_name '%s', if_addr 0x%x, mask_addr 0x%x, broadcast_addr 0x%x\n",
133 // op, interface_name, if_addr, mask_addr, broadcast_addr);
135 // fill out the control structure
136 memset(&control
, 0, sizeof(control
));
137 control
.if_name
[0] = 0;
139 strlcpy(control
.if_name
, interface_name
, sizeof(control
.if_name
));
140 control
.if_addr
.len
= 4;
141 control
.if_addr
.type
= ADDR_TYPE_IP
;
142 NETADDR_TO_IPV4(control
.if_addr
) = if_addr
;
143 control
.mask_addr
.len
= 4;
144 control
.mask_addr
.type
= ADDR_TYPE_IP
;
145 NETADDR_TO_IPV4(control
.mask_addr
) = mask_addr
;
146 control
.broadcast_addr
.len
= 4;
147 control
.broadcast_addr
.type
= ADDR_TYPE_IP
;
148 NETADDR_TO_IPV4(control
.broadcast_addr
) = broadcast_addr
;
155 fd
= open(NET_CONTROL_DEV
, 0);
157 printf("error opening network control device\n");
160 err
= ioctl(fd
, op
, &control
, sizeof(control
));
164 printf("error calling ioctl %d (%s)\n", err
, strerror(err
));
167 // printf("do_if: ioctl returned %d\n", err);
172 static int do_route(int argc
, const char *argv
[], int curr_arg
)
175 ipv4_addr net_addr
= 0;
176 ipv4_addr mask_addr
= 0;
177 ipv4_addr if_addr
= 0;
178 const char *interface_name
= NULL
;
179 struct _ioctl_net_route_struct control
;
184 /* parse the args for the various commands */
185 if(!strncasecmp(argv
[curr_arg
], "add", sizeof("add"))) {
189 if(!strncasecmp(argv
[curr_arg
], "default", sizeof("default"))) {
190 if(curr_arg
+ 8 >= argc
)
192 if(strncasecmp(argv
[curr_arg
+ 1], "ipv4", sizeof("ipv4")) ||
193 strncasecmp(argv
[curr_arg
+ 2], "addr", sizeof("addr")) ||
194 strncasecmp(argv
[curr_arg
+ 4], "if", sizeof("if")) ||
195 strncasecmp(argv
[curr_arg
+ 6], "ipv4", sizeof("ipv4")) ||
196 strncasecmp(argv
[curr_arg
+ 7], "addr", sizeof("addr")))
198 op
= IOCTL_NET_CONTROL_ROUTE_ADD
;
199 parse_ipv4_addr_string(&net_addr
, argv
[curr_arg
+ 3]);
200 interface_name
= argv
[curr_arg
+ 5];
201 parse_ipv4_addr_string(&if_addr
, argv
[curr_arg
+ 8]);
202 } else if(!strncasecmp(argv
[curr_arg
], "net", sizeof("net"))) {
203 if(curr_arg
+ 10 >= argc
)
205 if(strncasecmp(argv
[curr_arg
+ 1], "ipv4", sizeof("ipv4")) ||
206 strncasecmp(argv
[curr_arg
+ 2], "addr", sizeof("addr")) ||
207 strncasecmp(argv
[curr_arg
+ 4], "mask", sizeof("mask")) ||
208 strncasecmp(argv
[curr_arg
+ 6], "if", sizeof("if")) ||
209 strncasecmp(argv
[curr_arg
+ 8], "ipv4", sizeof("ipv4")) ||
210 strncasecmp(argv
[curr_arg
+ 9], "addr", sizeof("addr")))
212 op
= IOCTL_NET_CONTROL_ROUTE_ADD
;
213 parse_ipv4_addr_string(&net_addr
, argv
[curr_arg
+ 3]);
214 parse_ipv4_addr_string(&mask_addr
, argv
[curr_arg
+ 5]);
215 interface_name
= argv
[curr_arg
+ 7];
216 parse_ipv4_addr_string(&if_addr
, argv
[curr_arg
+ 10]);
220 } else if(!strncasecmp(argv
[curr_arg
], "delete", sizeof("delete"))) {
221 if(curr_arg
+ 3 >= argc
)
223 if(strncasecmp(argv
[curr_arg
+ 1], "ipv4", sizeof("ipv4")) ||
224 strncasecmp(argv
[curr_arg
+ 2], "addr", sizeof("addr")))
226 op
= IOCTL_NET_CONTROL_ROUTE_DELETE
;
227 parse_ipv4_addr_string(&net_addr
, argv
[curr_arg
+ 3]);
228 } else if(!strncasecmp(argv
[curr_arg
], "list", sizeof("list"))) {
229 op
= IOCTL_NET_CONTROL_ROUTE_LIST
;
234 // printf("do_route: op %d, net_addr 0x%x, mask_addr 0x%x, if_addr 0x%x, interface_name '%s'\n",
235 // op, net_addr, mask_addr, if_addr, interface_name);
237 // fill out the control structure
238 memset(&control
, 0, sizeof(control
));
239 control
.net_addr
.len
= 4;
240 control
.net_addr
.type
= ADDR_TYPE_IP
;
241 NETADDR_TO_IPV4(control
.net_addr
) = net_addr
;
242 control
.if_addr
.len
= 4;
243 control
.if_addr
.type
= ADDR_TYPE_IP
;
244 NETADDR_TO_IPV4(control
.if_addr
) = if_addr
;
245 control
.mask_addr
.len
= 4;
246 control
.mask_addr
.type
= ADDR_TYPE_IP
;
247 NETADDR_TO_IPV4(control
.mask_addr
) = mask_addr
;
248 control
.if_name
[0] = 0;
250 strlcpy(control
.if_name
, interface_name
, sizeof(control
.if_name
));
257 fd
= open(NET_CONTROL_DEV
, 0);
259 printf("error opening network control device\n");
262 err
= ioctl(fd
, op
, &control
, sizeof(control
));
266 printf("error calling ioctl %d (%s)\n", err
, strerror(err
));
269 // printf("do_if: ioctl returned %d\n", err);
276 int main(int argc
, const char *argv
[])
280 /* start processing args */
285 if(!strncasecmp(argv
[curr_arg
], "if", sizeof("if"))) {
286 return do_if(argc
, argv
, curr_arg
+ 1);
287 } else if(!strncasecmp(argv
[curr_arg
], "route", sizeof("route"))) {
288 return do_route(argc
, argv
, curr_arg
+ 1);