1 | /* |
---|
2 | * ROUTE Command Implmentation |
---|
3 | * |
---|
4 | * The license and distribution terms for this file may be |
---|
5 | * found in the file LICENSE in this distribution or at |
---|
6 | * http://www.rtems.org/license/LICENSE. |
---|
7 | */ |
---|
8 | |
---|
9 | #ifdef HAVE_CONFIG_H |
---|
10 | #include "config.h" |
---|
11 | #endif |
---|
12 | |
---|
13 | #include <stdio.h> |
---|
14 | #include <string.h> |
---|
15 | #include <ctype.h> |
---|
16 | |
---|
17 | #include <netinet/in.h> |
---|
18 | #include <sys/types.h> |
---|
19 | #include <sys/socket.h> |
---|
20 | #include <arpa/inet.h> |
---|
21 | #include <net/route.h> |
---|
22 | |
---|
23 | #include <rtems.h> |
---|
24 | #include <rtems/rtems_bsdnet.h> |
---|
25 | #include <rtems/shell.h> |
---|
26 | #include "internal.h" |
---|
27 | |
---|
28 | static int rtems_shell_main_route( |
---|
29 | int argc, |
---|
30 | char *argv[] |
---|
31 | ) |
---|
32 | { |
---|
33 | int cmd; |
---|
34 | struct sockaddr_in dst; |
---|
35 | struct sockaddr_in gw; |
---|
36 | struct sockaddr_in netmask; |
---|
37 | int f_host; |
---|
38 | int f_gw = 0; |
---|
39 | int cur_idx; |
---|
40 | int flags; |
---|
41 | int rc; |
---|
42 | |
---|
43 | memset(&dst, 0, sizeof(dst)); |
---|
44 | memset(&gw, 0, sizeof(gw)); |
---|
45 | memset(&netmask, 0, sizeof(netmask)); |
---|
46 | |
---|
47 | dst.sin_len = sizeof(dst); |
---|
48 | dst.sin_family = AF_INET; |
---|
49 | dst.sin_addr.s_addr = inet_addr("0.0.0.0"); |
---|
50 | |
---|
51 | gw.sin_len = sizeof(gw); |
---|
52 | gw.sin_family = AF_INET; |
---|
53 | gw.sin_addr.s_addr = inet_addr("0.0.0.0"); |
---|
54 | |
---|
55 | netmask.sin_len = sizeof(netmask); |
---|
56 | netmask.sin_family = AF_INET; |
---|
57 | netmask.sin_addr.s_addr = inet_addr("255.255.255.0"); |
---|
58 | |
---|
59 | if (argc < 2) { |
---|
60 | rtems_bsdnet_show_inet_routes(); |
---|
61 | return 0; |
---|
62 | } |
---|
63 | |
---|
64 | if (strcmp(argv[1], "add") == 0) { |
---|
65 | cmd = RTM_ADD; |
---|
66 | } else if (strcmp(argv[1], "del") == 0) { |
---|
67 | cmd = RTM_DELETE; |
---|
68 | } else { |
---|
69 | printf("invalid command: %s\n", argv[1]); |
---|
70 | printf("\tit should be 'add' or 'del'\n"); |
---|
71 | return -1; |
---|
72 | } |
---|
73 | |
---|
74 | if (argc < 3) { |
---|
75 | printf("not enough arguments\n"); |
---|
76 | return -1; |
---|
77 | } |
---|
78 | |
---|
79 | if (strcmp(argv[2], "-host") == 0) { |
---|
80 | f_host = 1; |
---|
81 | } else if (strcmp(argv[2], "-net") == 0) { |
---|
82 | f_host = 0; |
---|
83 | } else { |
---|
84 | printf("Invalid type: %s\n", argv[1]); |
---|
85 | printf("\tit should be '-host' or '-net'\n"); |
---|
86 | return -1; |
---|
87 | } |
---|
88 | |
---|
89 | if (argc < 4) { |
---|
90 | printf("not enough arguments\n"); |
---|
91 | return -1; |
---|
92 | } |
---|
93 | |
---|
94 | inet_pton(AF_INET, argv[3], &dst.sin_addr); |
---|
95 | |
---|
96 | cur_idx = 4; |
---|
97 | while(cur_idx < argc) { |
---|
98 | if (strcmp(argv[cur_idx], "gw") == 0) { |
---|
99 | if ((cur_idx +1) >= argc) { |
---|
100 | printf("no gateway address\n"); |
---|
101 | return -1; |
---|
102 | } |
---|
103 | f_gw = 1; |
---|
104 | inet_pton(AF_INET, argv[cur_idx + 1], &gw.sin_addr); |
---|
105 | cur_idx += 1; |
---|
106 | } else if(strcmp(argv[cur_idx], "netmask") == 0) { |
---|
107 | if ((cur_idx +1) >= argc) { |
---|
108 | printf("no netmask address\n"); |
---|
109 | return -1; |
---|
110 | } |
---|
111 | f_gw = 1; |
---|
112 | inet_pton(AF_INET, argv[cur_idx + 1], &netmask.sin_addr); |
---|
113 | cur_idx += 1; |
---|
114 | } else { |
---|
115 | printf("Unknown argument\n"); |
---|
116 | return -1; |
---|
117 | } |
---|
118 | cur_idx += 1; |
---|
119 | } |
---|
120 | |
---|
121 | flags = RTF_STATIC; |
---|
122 | if (f_gw != 0) { |
---|
123 | flags |= RTF_GATEWAY; |
---|
124 | } |
---|
125 | if (f_host != 0) { |
---|
126 | flags |= RTF_HOST; |
---|
127 | } |
---|
128 | |
---|
129 | rc = rtems_bsdnet_rtrequest( |
---|
130 | cmd, |
---|
131 | (struct sockaddr *)&dst, |
---|
132 | (struct sockaddr *)&gw, |
---|
133 | (struct sockaddr *)&netmask, |
---|
134 | flags, |
---|
135 | NULL |
---|
136 | ); |
---|
137 | if (rc < 0) { |
---|
138 | printf("Error adding route\n"); |
---|
139 | } |
---|
140 | |
---|
141 | return 0; |
---|
142 | } |
---|
143 | |
---|
144 | rtems_shell_cmd_t rtems_shell_ROUTE_Command = { |
---|
145 | "route", /* name */ |
---|
146 | "TBD", /* usage */ |
---|
147 | "network", /* topic */ |
---|
148 | rtems_shell_main_route, /* command */ |
---|
149 | NULL, /* alias */ |
---|
150 | NULL /* next */ |
---|
151 | }; |
---|