source: rtems/cpukit/libmisc/shell/main_route.c @ 8916bdc7

4.104.115
Last change on this file since 8916bdc7 was 54b2e4b9, checked in by Joel Sherrill <joel.sherrill@…>, on 02/19/08 at 19:44:21

2008-02-19 Joel Sherrill <joel.sherrill@…>

  • libmisc/Makefile.am, libmisc/shell/main_wkspaceinfo.c, libmisc/shell/shell.c, libmisc/shell/shellconfig.h: Add route and ifconfig commands. The code for these was previously in the networking guide. Disable NFS filesystem mount until that code is in cpukit.
  • libmisc/shell/main_ifconfig.c, libmisc/shell/main_route.c: New files.
  • Property mode set to 100644
File size: 3.4 KB
Line 
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.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
19#include <netinet/in.h>
20#include <sys/types.h>
21#include <sys/socket.h>
22#include <arpa/inet.h>
23#include <net/route.h>
24
25#include <rtems.h>
26#include <rtems/rtems_bsdnet.h>
27#include <rtems/shell.h>
28#include "internal.h"
29
30int rtems_shell_main_route(
31  int   argc,
32  char *argv[]
33)
34{
35  int                cmd;
36  struct sockaddr_in dst;
37  struct sockaddr_in gw;
38  struct sockaddr_in netmask;
39  int                f_host;
40  int                f_gw       = 0;
41  int                cur_idx;
42  int                flags;
43  int                rc;
44 
45  memset(&dst, 0, sizeof(dst));
46  memset(&gw, 0, sizeof(gw));
47  memset(&netmask, 0, sizeof(netmask));
48 
49  dst.sin_len = sizeof(dst);
50  dst.sin_family = AF_INET;
51  dst.sin_addr.s_addr = inet_addr("0.0.0.0"); 
52 
53  gw.sin_len = sizeof(gw);
54  gw.sin_family = AF_INET;
55  gw.sin_addr.s_addr = inet_addr("0.0.0.0");
56 
57  netmask.sin_len = sizeof(netmask);
58  netmask.sin_family = AF_INET;
59  netmask.sin_addr.s_addr = inet_addr("255.255.255.0");
60 
61  if (argc < 2) {
62    rtems_bsdnet_show_inet_routes();
63    return 0;
64  }
65 
66  if (strcmp(argv[1], "add") == 0) {
67    cmd = RTM_ADD;
68  } else if (strcmp(argv[1], "del") == 0) {
69    cmd = RTM_DELETE;
70  } else {
71    printf("invalid command: %s\n", argv[1]);
72    printf("\tit should be 'add' or 'del'\n");
73    return -1;
74  }
75 
76  if (argc < 3) {
77    printf("not enough arguments\n");
78    return -1;
79  }
80 
81  if (strcmp(argv[2], "-host") == 0) {
82    f_host = 1;
83  } else if (strcmp(argv[2], "-net") == 0) {
84    f_host = 0;
85  } else {
86    printf("Invalid type: %s\n", argv[1]);
87    printf("\tit should be '-host' or '-net'\n");
88    return -1;
89  }
90 
91  if (argc < 4) {
92    printf("not enough arguments\n");
93    return -1;
94  }
95 
96  inet_pton(AF_INET, argv[3], &dst.sin_addr);
97 
98  cur_idx = 4;
99  while(cur_idx < argc) {
100    if (strcmp(argv[cur_idx], "gw") == 0) {
101      if ((cur_idx +1) >= argc) {
102        printf("no gateway address\n");
103        return -1;
104      }
105      f_gw = 1;
106      inet_pton(AF_INET, argv[cur_idx + 1], &gw.sin_addr);
107      cur_idx += 1;
108    } else if(strcmp(argv[cur_idx], "netmask") == 0) {
109      if ((cur_idx +1) >= argc) {
110        printf("no netmask address\n");
111        return -1;
112      }
113      f_gw = 1;
114      inet_pton(AF_INET, argv[cur_idx + 1], &netmask.sin_addr);
115      cur_idx += 1;
116    } else {
117      printf("Unknown argument\n");
118      return -1;
119    }
120    cur_idx += 1;
121  }
122 
123  flags = RTF_STATIC;
124  if (f_gw != 0) {
125    flags |= RTF_GATEWAY;
126  }
127  if (f_host != 0) {
128    flags |= RTF_HOST;
129  }
130
131  rc = rtems_bsdnet_rtrequest(
132    cmd,
133    (struct sockaddr *)&dst,
134    (struct sockaddr *)&gw,
135    (struct sockaddr *)&netmask,
136    flags,
137    NULL
138  );
139  if (rc < 0) {
140    printf("Error adding route\n");
141  }
142
143  return 0;
144}
145
146rtems_shell_cmd_t rtems_shell_ROUTE_Command = {
147  "route",                                         /* name */
148  "TBD",                                           /* usage */
149  "network",                                       /* topic */
150  rtems_shell_main_route,                          /* command */
151  NULL,                                            /* alias */
152  NULL                                             /* next */
153};
Note: See TracBrowser for help on using the repository browser.