source: rtems/cpukit/libmisc/shell/main_ifconfig.c @ ea4f072

4.115
Last change on this file since ea4f072 was ea4f072, checked in by Sebastian Huber <sebastian.huber@…>, on 12/14/12 at 09:23:30

shell: Remove interface down warning in ifconfig

Whether an interface down is supported or not depends on the interface
driver.

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