1 | /* |
---|
2 | * COPYRIGHT (c) 1989-2007. |
---|
3 | * On-Line Applications Research Corporation (OAR). |
---|
4 | * |
---|
5 | * The license and distribution terms for this file may be |
---|
6 | * found in the file LICENSE in this distribution or at |
---|
7 | * http://www.rtems.com/license/LICENSE. |
---|
8 | * |
---|
9 | * $Id$ |
---|
10 | */ |
---|
11 | |
---|
12 | #ifdef HAVE_CONFIG_H |
---|
13 | #include "config.h" |
---|
14 | #endif |
---|
15 | |
---|
16 | #include <rtems.h> |
---|
17 | #include <stdio.h> |
---|
18 | #include <ctype.h> |
---|
19 | #include <errno.h> |
---|
20 | |
---|
21 | #include <rtems/rtems_bsdnet.h> |
---|
22 | #include <sys/socket.h> |
---|
23 | #include <net/if.h> |
---|
24 | #include <arpa/inet.h> |
---|
25 | #include <netinet/in.h> |
---|
26 | #include <sys/sockio.h> |
---|
27 | #include <net/route.h> |
---|
28 | |
---|
29 | |
---|
30 | void mon_ifconfig(int argc, char *argv[], uint32_t command_arg, |
---|
31 | boolean verbose) |
---|
32 | { |
---|
33 | struct sockaddr_in ipaddr; |
---|
34 | struct sockaddr_in dstaddr; |
---|
35 | struct sockaddr_in netmask; |
---|
36 | struct sockaddr_in broadcast; |
---|
37 | char *iface; |
---|
38 | int f_ip = 0; |
---|
39 | int f_ptp = 0; |
---|
40 | int f_netmask = 0; |
---|
41 | int f_up = 0; |
---|
42 | int f_down = 0; |
---|
43 | int f_bcast = 0; |
---|
44 | int cur_idx; |
---|
45 | int rc; |
---|
46 | int flags; |
---|
47 | |
---|
48 | memset(&ipaddr, 0, sizeof(ipaddr)); |
---|
49 | memset(&dstaddr, 0, sizeof(dstaddr)); |
---|
50 | memset(&netmask, 0, sizeof(netmask)); |
---|
51 | memset(&broadcast, 0, sizeof(broadcast)); |
---|
52 | |
---|
53 | ipaddr.sin_len = sizeof(ipaddr); |
---|
54 | ipaddr.sin_family = AF_INET; |
---|
55 | |
---|
56 | dstaddr.sin_len = sizeof(dstaddr); |
---|
57 | dstaddr.sin_family = AF_INET; |
---|
58 | |
---|
59 | netmask.sin_len = sizeof(netmask); |
---|
60 | netmask.sin_family = AF_INET; |
---|
61 | |
---|
62 | broadcast.sin_len = sizeof(broadcast); |
---|
63 | broadcast.sin_family = AF_INET; |
---|
64 | |
---|
65 | cur_idx = 0; |
---|
66 | if (argc <= 1) { |
---|
67 | /* display all interfaces */ |
---|
68 | iface = NULL; |
---|
69 | cur_idx += 1; |
---|
70 | } else { |
---|
71 | iface = argv[1]; |
---|
72 | if (isdigit(*argv[2])) { |
---|
73 | if (inet_pton(AF_INET, argv[2], &ipaddr.sin_addr) < 0) { |
---|
74 | printf("bad ip address: %s\n", argv[2]); |
---|
75 | return; |
---|
76 | } |
---|
77 | f_ip = 1; |
---|
78 | cur_idx += 3; |
---|
79 | } else { |
---|
80 | cur_idx += 2; |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | if ((f_down !=0) && (f_ip != 0)) { |
---|
85 | f_up = 1; |
---|
86 | } |
---|
87 | |
---|
88 | while(argc > cur_idx) { |
---|
89 | if (strcmp(argv[cur_idx], "up") == 0) { |
---|
90 | f_up = 1; |
---|
91 | if (f_down != 0) { |
---|
92 | printf("Can't make interface up and down\n"); |
---|
93 | } |
---|
94 | } else if(strcmp(argv[cur_idx], "down") == 0) { |
---|
95 | f_down = 1; |
---|
96 | if (f_up != 0) { |
---|
97 | printf("Can't make interface up and down\n"); |
---|
98 | } |
---|
99 | } else if(strcmp(argv[cur_idx], "netmask") == 0) { |
---|
100 | if ((cur_idx + 1) >= argc) { |
---|
101 | printf("No netmask address\n"); |
---|
102 | return; |
---|
103 | } |
---|
104 | if (inet_pton(AF_INET, argv[cur_idx+1], &netmask.sin_addr) < 0) { |
---|
105 | printf("bad netmask: %s\n", argv[cur_idx]); |
---|
106 | return; |
---|
107 | } |
---|
108 | f_netmask = 1; |
---|
109 | cur_idx += 1; |
---|
110 | } else if(strcmp(argv[cur_idx], "broadcast") == 0) { |
---|
111 | if ((cur_idx + 1) >= argc) { |
---|
112 | printf("No broadcast address\n"); |
---|
113 | return; |
---|
114 | } |
---|
115 | if (inet_pton(AF_INET, argv[cur_idx+1], &broadcast.sin_addr) < 0) { |
---|
116 | printf("bad broadcast: %s\n", argv[cur_idx]); |
---|
117 | return; |
---|
118 | } |
---|
119 | f_bcast = 1; |
---|
120 | cur_idx += 1; |
---|
121 | } else if(strcmp(argv[cur_idx], "pointopoint") == 0) { |
---|
122 | if ((cur_idx + 1) >= argc) { |
---|
123 | printf("No pointopoint address\n"); |
---|
124 | return; |
---|
125 | } |
---|
126 | if (inet_pton(AF_INET, argv[cur_idx+1], &dstaddr.sin_addr) < 0) { |
---|
127 | printf("bad pointopoint: %s\n", argv[cur_idx]); |
---|
128 | return; |
---|
129 | } |
---|
130 | |
---|
131 | f_ptp = 1; |
---|
132 | cur_idx += 1; |
---|
133 | } else { |
---|
134 | printf("Bad parameter: %s\n", argv[cur_idx]); |
---|
135 | return; |
---|
136 | } |
---|
137 | |
---|
138 | cur_idx += 1; |
---|
139 | } |
---|
140 | |
---|
141 | printf("ifconfig "); |
---|
142 | if (iface != NULL) { |
---|
143 | printf("%s ", iface); |
---|
144 | if (f_ip != 0) { |
---|
145 | char str[256]; |
---|
146 | inet_ntop(AF_INET, &ipaddr.sin_addr, str, 256); |
---|
147 | printf("%s ", str); |
---|
148 | } |
---|
149 | |
---|
150 | if (f_netmask != 0) { |
---|
151 | char str[256]; |
---|
152 | inet_ntop(AF_INET, &netmask.sin_addr, str, 256); |
---|
153 | printf("netmask %s ", str); |
---|
154 | } |
---|
155 | |
---|
156 | if (f_bcast != 0) { |
---|
157 | char str[256]; |
---|
158 | inet_ntop(AF_INET, &broadcast.sin_addr, str, 256); |
---|
159 | printf("broadcast %s ", str); |
---|
160 | } |
---|
161 | |
---|
162 | if (f_ptp != 0) { |
---|
163 | char str[256]; |
---|
164 | inet_ntop(AF_INET, &dstaddr.sin_addr, str, 256); |
---|
165 | printf("pointopoint %s ", str); |
---|
166 | } |
---|
167 | |
---|
168 | if (f_up != 0) { |
---|
169 | printf("up\n"); |
---|
170 | } else if (f_down != 0) { |
---|
171 | printf("down\n"); |
---|
172 | } else { |
---|
173 | printf("\n"); |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | if ((iface == NULL) || ((f_ip == 0) && (f_down == 0) && (f_up == 0))) { |
---|
178 | rtems_bsdnet_show_if_stats(); |
---|
179 | return; |
---|
180 | } |
---|
181 | |
---|
182 | flags = 0; |
---|
183 | if (f_netmask) { |
---|
184 | rc = rtems_bsdnet_ifconfig(iface, SIOCSIFNETMASK, &netmask); |
---|
185 | if (rc < 0) { |
---|
186 | printf("Could not set netmask: %s\n", strerror(errno)); |
---|
187 | return; |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | if (f_bcast) { |
---|
192 | rc = rtems_bsdnet_ifconfig(iface, SIOCSIFBRDADDR, &broadcast); |
---|
193 | if (rc < 0) { |
---|
194 | printf("Could not set broadcast: %s\n", strerror(errno)); |
---|
195 | return; |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | if (f_ptp) { |
---|
200 | rc = rtems_bsdnet_ifconfig(iface, SIOCSIFDSTADDR, &dstaddr); |
---|
201 | if (rc < 0) { |
---|
202 | printf("Could not set destination address: %s\n", strerror(errno)); |
---|
203 | return; |
---|
204 | } |
---|
205 | flags |= IFF_POINTOPOINT; |
---|
206 | } |
---|
207 | |
---|
208 | /* This must come _after_ setting the netmask, broadcast addresses */ |
---|
209 | if (f_ip) { |
---|
210 | rc = rtems_bsdnet_ifconfig(iface, SIOCSIFADDR, &ipaddr); |
---|
211 | if (rc < 0) { |
---|
212 | printf("Could not set IP address: %s\n", strerror(errno)); |
---|
213 | return; |
---|
214 | } |
---|
215 | } |
---|
216 | |
---|
217 | if (f_up != 0) { |
---|
218 | flags |= IFF_UP; |
---|
219 | } |
---|
220 | |
---|
221 | if (f_down != 0) { |
---|
222 | printf("Warning: taking interfaces down is not supported\n"); |
---|
223 | } |
---|
224 | |
---|
225 | rc = rtems_bsdnet_ifconfig(iface, SIOCSIFFLAGS, &flags); |
---|
226 | if (rc < 0) { |
---|
227 | printf("Could not set interface flags: %s\n", strerror(errno)); |
---|
228 | return; |
---|
229 | } |
---|
230 | } |
---|
231 | |
---|
232 | |
---|
233 | |
---|
234 | void mon_route(int argc, char *argv[], uint32_t command_arg, |
---|
235 | boolean verbose) |
---|
236 | { |
---|
237 | int cmd; |
---|
238 | struct sockaddr_in dst; |
---|
239 | struct sockaddr_in gw; |
---|
240 | struct sockaddr_in netmask; |
---|
241 | int f_host; |
---|
242 | int f_gw = 0; |
---|
243 | int cur_idx; |
---|
244 | int flags; |
---|
245 | int rc; |
---|
246 | |
---|
247 | memset(&dst, 0, sizeof(dst)); |
---|
248 | memset(&gw, 0, sizeof(gw)); |
---|
249 | memset(&netmask, 0, sizeof(netmask)); |
---|
250 | |
---|
251 | dst.sin_len = sizeof(dst); |
---|
252 | dst.sin_family = AF_INET; |
---|
253 | dst.sin_addr.s_addr = inet_addr("0.0.0.0"); |
---|
254 | |
---|
255 | gw.sin_len = sizeof(gw); |
---|
256 | gw.sin_family = AF_INET; |
---|
257 | gw.sin_addr.s_addr = inet_addr("0.0.0.0"); |
---|
258 | |
---|
259 | netmask.sin_len = sizeof(netmask); |
---|
260 | netmask.sin_family = AF_INET; |
---|
261 | netmask.sin_addr.s_addr = inet_addr("255.255.255.0"); |
---|
262 | |
---|
263 | if (argc < 2) { |
---|
264 | rtems_bsdnet_show_inet_routes(); |
---|
265 | return; |
---|
266 | } |
---|
267 | |
---|
268 | if (strcmp(argv[1], "add") == 0) { |
---|
269 | cmd = RTM_ADD; |
---|
270 | } else if (strcmp(argv[1], "del") == 0) { |
---|
271 | cmd = RTM_DELETE; |
---|
272 | } else { |
---|
273 | printf("invalid command: %s\n", argv[1]); |
---|
274 | printf("\tit should be 'add' or 'del'\n"); |
---|
275 | return; |
---|
276 | } |
---|
277 | |
---|
278 | if (argc < 3) { |
---|
279 | printf("not enough arguments\n"); |
---|
280 | return; |
---|
281 | } |
---|
282 | |
---|
283 | if (strcmp(argv[2], "-host") == 0) { |
---|
284 | f_host = 1; |
---|
285 | } else if (strcmp(argv[2], "-net") == 0) { |
---|
286 | f_host = 0; |
---|
287 | } else { |
---|
288 | printf("Invalid type: %s\n", argv[1]); |
---|
289 | printf("\tit should be '-host' or '-net'\n"); |
---|
290 | return; |
---|
291 | } |
---|
292 | |
---|
293 | if (argc < 4) { |
---|
294 | printf("not enough arguments\n"); |
---|
295 | return; |
---|
296 | } |
---|
297 | |
---|
298 | inet_pton(AF_INET, argv[3], &dst.sin_addr); |
---|
299 | |
---|
300 | cur_idx = 4; |
---|
301 | while(cur_idx < argc) { |
---|
302 | if (strcmp(argv[cur_idx], "gw") == 0) { |
---|
303 | if ((cur_idx +1) >= argc) { |
---|
304 | printf("no gateway address\n"); |
---|
305 | return; |
---|
306 | } |
---|
307 | f_gw = 1; |
---|
308 | inet_pton(AF_INET, argv[cur_idx + 1], &gw.sin_addr); |
---|
309 | cur_idx += 1; |
---|
310 | } else if(strcmp(argv[cur_idx], "netmask") == 0) { |
---|
311 | if ((cur_idx +1) >= argc) { |
---|
312 | printf("no netmask address\n"); |
---|
313 | return; |
---|
314 | } |
---|
315 | f_gw = 1; |
---|
316 | inet_pton(AF_INET, argv[cur_idx + 1], &netmask.sin_addr); |
---|
317 | cur_idx += 1; |
---|
318 | } else { |
---|
319 | printf("Unknown argument: %s\n", argv[cur_idx]); |
---|
320 | return; |
---|
321 | } |
---|
322 | cur_idx += 1; |
---|
323 | } |
---|
324 | |
---|
325 | flags = RTF_STATIC; |
---|
326 | if (f_gw != 0) { |
---|
327 | flags |= RTF_GATEWAY; |
---|
328 | } |
---|
329 | if (f_host != 0) { |
---|
330 | flags |= RTF_HOST; |
---|
331 | } |
---|
332 | |
---|
333 | rc = rtems_bsdnet_rtrequest(cmd, |
---|
334 | (struct sockaddr*) &dst, |
---|
335 | (struct sockaddr*) &gw, |
---|
336 | (struct sockaddr*) &netmask, flags, NULL); |
---|
337 | if (rc < 0) { |
---|
338 | printf("Error adding route\n"); |
---|
339 | } |
---|
340 | } |
---|