No empty .Rs/.Re
[netbsd-mini2440.git] / usr.sbin / srtconfig / srtconfig.c
blob8ced16279a1861721f2de32a3789d61d08fcec77
1 /* $NetBSD: srtconfig.c,v 1.2 2006/12/29 10:39:59 wiz Exp $ */
2 /* This file is in the public domain. */
4 #include <stdio.h>
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <stdlib.h>
8 #include <strings.h>
9 #include <sys/ioctl.h>
10 #include <arpa/inet.h>
11 #include <net/if_srt.h>
13 extern const char *__progname;
15 #define ACT_ERROR 1 /* none of the below */
16 #define ACT_QUERYALL 2 /* srtX */
17 #define ACT_QUERYONE 3 /* srtX N */
18 #define ACT_DEL 4 /* srtX del N */
19 #define ACT_ADD 5 /* srtX add srcaddr mask dstif dstaddr */
20 #define ACT_SET 6 /* srtX set N srcaddr mask dstif dstaddr */
21 #define ACT_FLAGS 7 /* srtX flags */
22 #define ACT_SFLAG 8 /* srtX flags [+|-]flag */
23 #define ACT_DEBUG 9 /* srtX debug */
24 static int action = ACT_ERROR;
26 static char *txt_dev;
27 static char *txt_n;
28 static char *txt_flg;
29 static char *txt_addr;
30 static char *txt_mask;
31 static char *txt_dstif;
32 static char *txt_dstaddr;
34 static int devfd;
36 static struct {
37 const char *name;
38 unsigned int bit;
39 } flagbits[] = { { "mtulock", SSF_MTULOCK },
40 { 0, 0 } };
42 static void handleargs(int ac, char **av)
44 txt_dev = av[1];
45 if (ac == 2)
46 { action = ACT_QUERYALL;
48 else if ((ac == 3) && !strcmp(av[2],"debug"))
49 { action = ACT_DEBUG;
51 else if ((ac == 3) && !strcmp(av[2],"flags"))
52 { action = ACT_FLAGS;
54 else if (ac == 3)
55 { action = ACT_QUERYONE;
56 txt_n = av[2];
58 else if ((ac == 4) && !strcmp(av[2],"del"))
59 { action = ACT_DEL;
60 txt_n = av[3];
62 else if ((ac == 4) && !strcmp(av[2],"flags"))
63 { action = ACT_SFLAG;
64 txt_flg = av[3];
66 else if ((ac == 7) && !strcmp(av[2],"add"))
67 { action = ACT_ADD;
68 txt_addr = av[3];
69 txt_mask = av[4];
70 txt_dstif = av[5];
71 txt_dstaddr = av[6];
73 else if ((ac == 8) && !strcmp(av[2],"set"))
74 { action = ACT_SET;
75 txt_n = av[3];
76 txt_addr = av[4];
77 txt_mask = av[5];
78 txt_dstif = av[6];
79 txt_dstaddr = av[7];
81 if (action == ACT_ERROR)
82 { fprintf(stderr,"Usage: %s srtX\n",__progname);
83 fprintf(stderr," %s srtX N\n",__progname);
84 fprintf(stderr," %s srtX del N\n",__progname);
85 fprintf(stderr," %s srtX add addr mask dstif dstaddr\n",__progname);
86 fprintf(stderr," %s srtX set N addr mask dstif dstaddr\n",__progname);
87 fprintf(stderr," %s srtX flags {[+|-]flag}\n",__progname);
88 fprintf(stderr," %s srtX debug\n",__progname);
89 exit(1);
93 static void open_dev(int how)
95 if (! index(txt_dev,'/'))
96 { char *tmp;
97 asprintf(&tmp,"/dev/%s",txt_dev);
98 txt_dev = tmp;
100 devfd = open(txt_dev,how,0);
101 if (devfd < 0)
102 { fprintf(stderr,"%s; can't open %s: %s\n",__progname,txt_dev,strerror(errno));
103 exit(1);
107 static void query_n(int n)
109 struct srt_rt r;
110 char obuf[64];
112 r.inx = n;
113 if (ioctl(devfd,SRT_GETRT,&r) < 0)
114 { fprintf(stderr,"%s: can't get rt #%d: %s\n",__progname,n,strerror(errno));
115 return;
117 printf("%d:",n);
118 printf(" %s",inet_ntop(r.af,&r.srcmatch,&obuf[0],sizeof(obuf)));
119 printf(" /%d",r.srcmask);
120 printf(" %.*s",(int)sizeof(r.u.dstifn),&r.u.dstifn[0]);
121 switch (r.af)
122 { case AF_INET:
123 printf(" %s",inet_ntoa(r.dst.sin.sin_addr));
124 break;
125 case AF_INET6:
126 printf(" %s",inet_ntop(AF_INET6,&r.dst.sin6.sin6_addr,&obuf[0],sizeof(obuf)));
127 break;
128 default:
129 printf(" ?af%d",r.af);
130 break;
132 printf("\n");
135 static void do_query(int narg)
137 int i;
138 int n;
140 open_dev(O_RDONLY);
141 if (narg >= 0)
142 { query_n(narg);
144 else
145 { if (ioctl(devfd,SRT_GETNRT,&n) < 0)
146 { fprintf(stderr,"%s: can't get count: %s\n",__progname,strerror(errno));
147 exit(1);
149 for (i=0;i<n;i++) query_n(i);
153 static void do_del(unsigned int n)
155 open_dev(O_RDWR);
156 if (ioctl(devfd,SRT_DELRT,&n) < 0)
157 { fprintf(stderr,"%s: can't delete #%u: %s\n",__progname,n,strerror(errno));
158 exit(1);
162 static void do_set(int n)
164 struct srt_rt r;
165 int w;
166 int maxw;
167 void *dp;
169 open_dev(O_RDWR);
170 if (n < 0)
171 { unsigned int v;
172 if (ioctl(devfd,SRT_GETNRT,&v) < 0)
173 { fprintf(stderr,"%s: can't get count: %s\n",__progname,strerror(errno));
174 exit(1);
176 n = v;
178 bzero(&r.dst,sizeof(r.dst));
179 r.inx = n;
180 if (inet_pton(AF_INET,txt_addr,&r.srcmatch.v4) == 1)
181 { r.af = AF_INET;
182 r.dst.sin.sin_family = AF_INET;
183 r.dst.sin.sin_len = sizeof(r.dst.sin);
184 dp = &r.dst.sin.sin_addr;
185 maxw = 32;
187 else if (inet_pton(AF_INET6,txt_addr,&r.srcmatch.v6) == 1)
188 { r.af = AF_INET6;
189 r.dst.sin6.sin6_family = AF_INET6;
190 r.dst.sin6.sin6_len = sizeof(r.dst.sin6);
191 dp = &r.dst.sin6.sin6_addr;
192 maxw = 128;
194 else
195 { fprintf(stderr,"%s: %s: invalid match address\n",__progname,txt_addr);
196 exit(1);
198 if (txt_mask[0] == '/') txt_mask ++;
199 w = atoi(txt_mask);
200 if ((w < 0) || (w > maxw))
201 { fprintf(stderr,"%s: %s: out-of-range CIDR width\n",__progname,txt_mask);
202 exit(1);
204 r.srcmask = w;
205 if (strlen(txt_dstif) > sizeof(r.u.dstifn)-1)
206 { fprintf(stderr,"%s: %s: too long\n",__progname,txt_dstif);
207 exit(1);
209 strncpy(&r.u.dstifn[0],txt_dstif,sizeof(r.u.dstifn));
210 if (inet_pton(r.af,txt_dstaddr,dp) != 1)
211 { fprintf(stderr,"%s: %s: invalid destination address\n",__progname,txt_dstaddr);
212 exit(1);
214 if (ioctl(devfd,SRT_SETRT,&r) < 0)
215 { fprintf(stderr,"%s: can't set route: %s\n",__progname,strerror(errno));
216 exit(1);
220 static void do_flags(void)
222 unsigned int f;
223 int i;
225 open_dev(O_RDONLY);
226 if (ioctl(devfd,SRT_GFLAGS,&f) < 0)
227 { fprintf(stderr,"%s: can't get flags: %s\n",__progname,strerror(errno));
228 exit(1);
230 for (i=0;flagbits[i].name;i++)
231 { printf(" %c%s",(f&flagbits[i].bit)?'+':'-',flagbits[i].name);
232 f &= ~flagbits[i].bit;
234 if (f) printf(" +0x%x",f);
235 printf("\n");
238 static void do_sflag(void)
240 unsigned int f;
241 unsigned int b;
242 int i;
244 switch (txt_flg[0])
245 { case '+': case '-': break;
246 default:
247 fprintf(stderr,"%s: last argument must be +flag or -flag\n",__progname);
248 exit(1);
249 break;
251 for (i=0;flagbits[i].name;i++) if (!strcmp(flagbits[i].name,txt_flg+1)) break;
252 if (! flagbits[i].name)
253 { fprintf(stderr,"%s: unrecognized flag bit `%s'\n",__progname,txt_flg+1);
254 exit(1);
256 b = flagbits[i].bit;
257 open_dev(O_RDWR);
258 if (ioctl(devfd,SRT_GFLAGS,&f) < 0)
259 { fprintf(stderr,"%s: can't get flags: %s\n",__progname,strerror(errno));
260 exit(1);
262 if (txt_flg[0] == '+') f |= b; else f &= ~b;
263 if (ioctl(devfd,SRT_SFLAGS,&f) < 0)
264 { fprintf(stderr,"%s: can't set flags: %s\n",__progname,strerror(errno));
265 exit(1);
269 static void do_debug(void)
271 void *vp;
273 open_dev(O_RDWR);
274 vp = 0;
275 if (ioctl(devfd,SRT_DEBUG,&vp) < 0)
276 { fprintf(stderr,"%s: can't SRT_DEBUG: %s\n",__progname,strerror(errno));
277 exit(1);
281 int main(int, char **);
282 int main(int ac, char **av)
284 handleargs(ac,av);
285 switch (action)
286 { case ACT_QUERYALL:
287 do_query(-1);
288 break;
289 case ACT_QUERYONE:
290 do_query(atoi(txt_n));
291 break;
292 case ACT_DEL:
293 do_del(atoi(txt_n));
294 break;
295 case ACT_ADD:
296 do_set(-1);
297 break;
298 case ACT_SET:
299 do_set(atoi(txt_n));
300 break;
301 case ACT_FLAGS:
302 do_flags();
303 break;
304 case ACT_SFLAG:
305 do_sflag();
306 break;
307 case ACT_DEBUG:
308 do_debug();
309 break;
310 default:
311 abort();
312 break;
314 exit(0);