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