source: rtems/cpukit/libmisc/shell/main_ifconfig.c @ 787f51f

5
Last change on this file since 787f51f was 787f51f, checked in by Sebastian Huber <sebastian.huber@…>, on 06/06/17 at 09:08:16

Do not include <sys/ioctl.h> in kernel-space

Update #2833.

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