source: rtems/cpukit/libmisc/monitor/mon-network.c @ 15a47934

4.104.114.84.95
Last change on this file since 15a47934 was 15a47934, checked in by Joel Sherrill <joel.sherrill@…>, on 09/14/07 at 22:52:53

2007-09-14 Joel Sherrill <joel.sherrill@…>

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