1 | /* |
---|
2 | * IFCONFIG 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 | #include <errno.h> |
---|
17 | |
---|
18 | #include <netinet/in.h> |
---|
19 | #include <sys/types.h> |
---|
20 | #include <sys/socket.h> |
---|
21 | #include <arpa/inet.h> |
---|
22 | #include <net/if.h> |
---|
23 | |
---|
24 | |
---|
25 | #include <rtems.h> |
---|
26 | #include <rtems/rtems_bsdnet.h> |
---|
27 | #include <rtems/shell.h> |
---|
28 | #include "internal.h" |
---|
29 | |
---|
30 | |
---|
31 | static const char IFCONFIG_USAGE[] = |
---|
32 | "ifconfig [iface]\n" |
---|
33 | "ifconfig iface options | addr\n" |
---|
34 | " iface: name of the interface\n" |
---|
35 | " options: iface \n" |
---|
36 | " up: bring the interface up\n" |
---|
37 | " down: take the interface down\n" |
---|
38 | " netmask addr: network mask\n" |
---|
39 | " broadcast addr: boardcast address\n" |
---|
40 | " pointopoint addr: destination address for a PTP link\n" |
---|
41 | "\n" |
---|
42 | " addr: IP address"; |
---|
43 | |
---|
44 | |
---|
45 | static int rtems_shell_main_ifconfig( |
---|
46 | int argc, |
---|
47 | char *argv[] |
---|
48 | ) |
---|
49 | { |
---|
50 | struct sockaddr_in ipaddr; |
---|
51 | struct sockaddr_in dstaddr; |
---|
52 | struct sockaddr_in netmask; |
---|
53 | struct sockaddr_in broadcast; |
---|
54 | char *iface = NULL; |
---|
55 | int f_ip = 0; |
---|
56 | int f_ptp = 0; |
---|
57 | int f_netmask = 0; |
---|
58 | int f_up = 0; |
---|
59 | int f_down = 0; |
---|
60 | int f_bcast = 0; |
---|
61 | int f_usage = 0; |
---|
62 | int cur_idx; |
---|
63 | int rc; |
---|
64 | int flags; |
---|
65 | |
---|
66 | memset(&ipaddr, 0, sizeof(ipaddr)); |
---|
67 | memset(&dstaddr, 0, sizeof(dstaddr)); |
---|
68 | memset(&netmask, 0, sizeof(netmask)); |
---|
69 | memset(&broadcast, 0, sizeof(broadcast)); |
---|
70 | |
---|
71 | ipaddr.sin_len = sizeof(ipaddr); |
---|
72 | ipaddr.sin_family = AF_INET; |
---|
73 | |
---|
74 | dstaddr.sin_len = sizeof(dstaddr); |
---|
75 | dstaddr.sin_family = AF_INET; |
---|
76 | |
---|
77 | netmask.sin_len = sizeof(netmask); |
---|
78 | netmask.sin_family = AF_INET; |
---|
79 | |
---|
80 | broadcast.sin_len = sizeof(broadcast); |
---|
81 | broadcast.sin_family = AF_INET; |
---|
82 | |
---|
83 | cur_idx = 0; |
---|
84 | if (argc <= 1) { |
---|
85 | /* display all interfaces */ |
---|
86 | iface = NULL; |
---|
87 | cur_idx += 1; |
---|
88 | } else { |
---|
89 | if ( 0 == strcmp( "--help", argv[1] ) ) { |
---|
90 | f_usage = 1; |
---|
91 | cur_idx += 2; |
---|
92 | } else if ( 0 == strcmp( "-help", argv[1] ) ) { |
---|
93 | f_usage = 1; |
---|
94 | cur_idx += 2; |
---|
95 | } else { |
---|
96 | iface = argv[1]; |
---|
97 | if ( argc >= 3 ) { |
---|
98 | if (isdigit((unsigned char)*argv[2])) { |
---|
99 | if (inet_pton(AF_INET, argv[2], &ipaddr.sin_addr) < 0) { |
---|
100 | printf("bad ip address: %s\n", argv[2]); |
---|
101 | return 0; |
---|
102 | } |
---|
103 | f_ip = 1; |
---|
104 | cur_idx += 3; |
---|
105 | } else { |
---|
106 | cur_idx += 2; |
---|
107 | } |
---|
108 | } else { |
---|
109 | cur_idx += 2; |
---|
110 | } |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | if ((f_down !=0) && (f_ip != 0)) { |
---|
115 | f_up = 1; |
---|
116 | } |
---|
117 | |
---|
118 | while(argc > cur_idx) { |
---|
119 | if (strcmp(argv[cur_idx], "up") == 0) { |
---|
120 | f_up = 1; |
---|
121 | if (f_down != 0) { |
---|
122 | printf("Can't make interface up and down\n"); |
---|
123 | return -1; |
---|
124 | } |
---|
125 | } else if(strcmp(argv[cur_idx], "down") == 0) { |
---|
126 | f_down = 1; |
---|
127 | if (f_up != 0) { |
---|
128 | printf("Can't make interface up and down\n"); |
---|
129 | return -1; |
---|
130 | } |
---|
131 | } else if(strcmp(argv[cur_idx], "netmask") == 0) { |
---|
132 | if ((cur_idx + 1) >= argc) { |
---|
133 | printf("No netmask address\n"); |
---|
134 | return -1; |
---|
135 | } |
---|
136 | if (inet_pton(AF_INET, argv[cur_idx+1], &netmask.sin_addr) < 0) { |
---|
137 | printf("bad netmask: %s\n", argv[cur_idx]); |
---|
138 | return -1; |
---|
139 | } |
---|
140 | f_netmask = 1; |
---|
141 | cur_idx += 1; |
---|
142 | } else if(strcmp(argv[cur_idx], "broadcast") == 0) { |
---|
143 | if ((cur_idx + 1) >= argc) { |
---|
144 | printf("No broadcast address\n"); |
---|
145 | return -1; |
---|
146 | } |
---|
147 | if (inet_pton(AF_INET, argv[cur_idx+1], &broadcast.sin_addr) < 0) { |
---|
148 | printf("bad broadcast: %s\n", argv[cur_idx]); |
---|
149 | return -1; |
---|
150 | } |
---|
151 | f_bcast = 1; |
---|
152 | cur_idx += 1; |
---|
153 | } else if(strcmp(argv[cur_idx], "pointopoint") == 0) { |
---|
154 | if ((cur_idx + 1) >= argc) { |
---|
155 | printf("No pointopoint address\n"); |
---|
156 | return -1; |
---|
157 | } |
---|
158 | if (inet_pton(AF_INET, argv[cur_idx+1], &dstaddr.sin_addr) < 0) { |
---|
159 | printf("bad pointopoint: %s\n", argv[cur_idx]); |
---|
160 | return -1; |
---|
161 | } |
---|
162 | f_ptp = 1; |
---|
163 | cur_idx += 1; |
---|
164 | } else { |
---|
165 | printf("Bad parameter: %s\n", argv[cur_idx]); |
---|
166 | return -1; |
---|
167 | } |
---|
168 | cur_idx += 1; |
---|
169 | } |
---|
170 | |
---|
171 | printf("ifconfig "); |
---|
172 | if (iface != NULL) { |
---|
173 | printf("%s ", iface); |
---|
174 | if (f_ip != 0) { |
---|
175 | char str[256]; |
---|
176 | inet_ntop(AF_INET, &ipaddr.sin_addr, str, 256); |
---|
177 | printf("%s ", str); |
---|
178 | } |
---|
179 | |
---|
180 | if (f_netmask != 0) { |
---|
181 | char str[256]; |
---|
182 | inet_ntop(AF_INET, &netmask.sin_addr, str, 256); |
---|
183 | printf("netmask %s ", str); |
---|
184 | } |
---|
185 | |
---|
186 | if (f_bcast != 0) { |
---|
187 | char str[256]; |
---|
188 | inet_ntop(AF_INET, &broadcast.sin_addr, str, 256); |
---|
189 | printf("broadcast %s ", str); |
---|
190 | } |
---|
191 | |
---|
192 | if (f_ptp != 0) { |
---|
193 | char str[256]; |
---|
194 | inet_ntop(AF_INET, &dstaddr.sin_addr, str, 256); |
---|
195 | printf("pointopoint %s ", str); |
---|
196 | } |
---|
197 | |
---|
198 | if (f_up != 0) { |
---|
199 | printf("up\n"); |
---|
200 | } else if (f_down != 0) { |
---|
201 | printf("down\n"); |
---|
202 | } else { |
---|
203 | printf("\n"); |
---|
204 | } |
---|
205 | } else if (f_usage != 0) { |
---|
206 | printf ( "\n" ); |
---|
207 | printf ( IFCONFIG_USAGE ); |
---|
208 | } |
---|
209 | |
---|
210 | if ( ! f_usage ) { |
---|
211 | if ((iface == NULL) || ((f_ip == 0) && (f_down == 0) && (f_up == 0))) { |
---|
212 | rtems_bsdnet_show_if_stats(); |
---|
213 | return 0; |
---|
214 | } |
---|
215 | } |
---|
216 | |
---|
217 | flags = 0; |
---|
218 | if (f_netmask) { |
---|
219 | rc = rtems_bsdnet_ifconfig(iface, SIOCSIFNETMASK, &netmask); |
---|
220 | if (rc < 0) { |
---|
221 | printf("Could not set netmask: %s\n", strerror(errno)); |
---|
222 | return -1; |
---|
223 | } |
---|
224 | } |
---|
225 | |
---|
226 | if (f_bcast) { |
---|
227 | rc = rtems_bsdnet_ifconfig(iface, SIOCSIFBRDADDR, &broadcast); |
---|
228 | if (rc < 0) { |
---|
229 | printf("Could not set broadcast: %s\n", strerror(errno)); |
---|
230 | return -1; |
---|
231 | } |
---|
232 | } |
---|
233 | |
---|
234 | if (f_ptp) { |
---|
235 | rc = rtems_bsdnet_ifconfig(iface, SIOCSIFDSTADDR, &dstaddr); |
---|
236 | if (rc < 0) { |
---|
237 | printf("Could not set destination address: %s\n", strerror(errno)); |
---|
238 | return -1; |
---|
239 | } |
---|
240 | flags |= IFF_POINTOPOINT; |
---|
241 | } |
---|
242 | |
---|
243 | /* This must come _after_ setting the netmask, broadcast addresses */ |
---|
244 | if (f_ip) { |
---|
245 | rc = rtems_bsdnet_ifconfig(iface, SIOCSIFADDR, &ipaddr); |
---|
246 | if (rc < 0) { |
---|
247 | printf("Could not set IP address: %s\n", strerror(errno)); |
---|
248 | return -1; |
---|
249 | } |
---|
250 | } |
---|
251 | |
---|
252 | if (f_up != 0) { |
---|
253 | flags |= IFF_UP; |
---|
254 | } |
---|
255 | |
---|
256 | if ( ! f_usage ) { |
---|
257 | rc = rtems_bsdnet_ifconfig(iface, SIOCSIFFLAGS, &flags); |
---|
258 | if (rc < 0) { |
---|
259 | printf("Could not set interface flags: %s\n", strerror(errno)); |
---|
260 | return -1; |
---|
261 | } |
---|
262 | } |
---|
263 | |
---|
264 | return 0; |
---|
265 | } |
---|
266 | |
---|
267 | rtems_shell_cmd_t rtems_shell_IFCONFIG_Command = { |
---|
268 | "ifconfig", /* name */ |
---|
269 | IFCONFIG_USAGE, /* usage */ |
---|
270 | "network", /* topic */ |
---|
271 | rtems_shell_main_ifconfig, /* command */ |
---|
272 | NULL, /* alias */ |
---|
273 | NULL /* next */ |
---|
274 | }; |
---|