Wirefilter: Several new features (bandwidth, speed, mtu, dup, noise).
[vde.git] / vde-2 / wirefilter.c
blob3629e70c60a54a0ef4c865ffab4586ec66b0b4c5
1 /* WIREFILTER (C) 2005 Renzo Davoli
2 * Licensed under the GPLv2
3 * Modified by Ludovico Gardenghi 2005
5 * This filter can be used for testing network protcols.
6 * It is possible to loose, delay or reorder packets.
7 * Options can be set on command line or interactively with a remote interface
8 * on a unix socket (see unixterm).
9 */
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <getopt.h>
16 #include <errno.h>
17 #include <libgen.h>
18 #include <fcntl.h>
19 #include <time.h>
20 #include <signal.h>
21 #include <stdarg.h>
22 #include <sys/time.h>
23 #include <sys/poll.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <sys/socket.h>
27 #include <sys/un.h>
28 #include <config.h>
30 #include <vde.h>
32 #define NPIPES 2
33 #define MAXCONN 3
34 #define STDIN_ALTFILENO 3
35 #define STDOUT_ALTFILENO 4
36 #define NPFD NPIPES+MAXCONN+1
37 struct pollfd pfd[NPFD];
38 int outfd[NPIPES];
39 char *progname;
40 char *mgmt;
41 int mgmtmode=0700;
42 #define LR 0
43 #define RL 1
44 double loss[2],lossplus[2];
45 double delay[2],delayplus[2];
46 double ddup[2],ddupplus[2];
47 double band[2],bandplus[2];
48 double speed[2],speedplus[2];
49 double capacity[2],capacityplus[2];
50 double noise[2],noiseplus[2];
51 double mtu[2],mtuplus[2];
52 struct timeval nextband[2];
53 struct timeval nextspeed[2];
54 int nofifo;
55 int ndirs;
56 int bufsize[2];
58 #define BUFSIZE 2048
59 #define MAXCMD 128
60 #define MGMTMODEARG 129
61 #define KILO (1<<10)
62 #define MEGA (1<<20)
63 #define GIGA (1<<30)
65 static void readdualvalue(char *s,double *val,double *valplus)
67 double v=0.0;
68 double vplus=0.0;
69 int n;
70 int mult;
71 n=strlen(s)-1;
72 while ((s[n] == ' ' || s[n] == '\n' || s[n] == '\t') && n>0)
74 s[n]=0;
75 n--;
77 switch (s[n]) {
78 case 'k':
79 case 'K':
80 mult=KILO;
81 break;
82 case 'm':
83 case 'M':
84 mult=MEGA;
85 break;
86 case 'g':
87 case 'G':
88 mult=GIGA;
89 break;
90 default:
91 mult=1;
92 break;
94 if ((n=sscanf(s,"%lf+%lf",&v,&vplus)) > 0) {
95 val[LR]=val[RL]=v*mult;
96 valplus[LR]=valplus[RL]=vplus*mult;
97 } else if ((n=sscanf(s,"LR%lf+%lf",&v,&vplus)) > 0) {
98 val[LR]=v*mult;
99 valplus[LR]=vplus*mult;
100 } else if ((n=sscanf(s,"RL%lf+%lf",&v,&vplus)) > 0) {
101 val[RL]=v*mult;
102 valplus[RL]=vplus*mult;
106 struct packpq {
107 unsigned long long when;
108 int dir;
109 unsigned char *buf;
110 int size;
113 struct packpq **pqh;
114 struct packpq sentinel={0,0,NULL,0};
115 int npq,maxpq;
116 unsigned long long maxwhen;
118 #define PQCHUNK 100
120 static int nextms()
122 if (npq>0) {
123 long long deltat;
124 struct timeval v;
125 gettimeofday(&v,NULL);
126 deltat=pqh[1]->when-(v.tv_sec*1000000+v.tv_usec);
127 return (deltat>0)?(int)(deltat/1000):0;
129 return -1;
132 int writepacket(int dir,const unsigned char *buf,int size)
134 /* NOISE */
135 if (noise[dir]+noiseplus[dir] > 0) {
136 double noiseval=noise[dir];
137 int nobit=0;
138 if (noiseplus) noiseval+=((drand48()*2.0)-1.0)*noiseplus[dir];
139 while ((drand48()*8*MEGA) < (size-2)*8*noiseval)
140 nobit++;
141 if (nobit>0) {
142 unsigned char noisedpacket[BUFSIZE];
143 memcpy(noisedpacket,buf,size);
144 while(nobit>0) {
145 int flippedbit=(drand48()*size*8);
146 noisedpacket[(flippedbit >> 3) + 2] ^= 1<<(flippedbit & 0x7);
147 nobit--;
149 return write(outfd[dir],noisedpacket,size);
150 } else
151 return write(outfd[dir],buf,size);
152 } else
153 return write(outfd[dir],buf,size);
156 /* packet queues are priority queues implemented on a heap.
157 * enqueue time = dequeue time = O(log n) max&mean
160 static void packet_dequeue()
162 struct timeval v;
163 gettimeofday(&v,NULL);
164 unsigned long long now=v.tv_sec*1000000+v.tv_usec;
165 while (npq>0 && pqh[1]->when <= now) {
166 struct packpq *old=pqh[npq--];
167 int k=1;
168 bufsize[pqh[1]->dir] -= pqh[1]->size;
169 writepacket(pqh[1]->dir,pqh[1]->buf,pqh[1]->size);
170 free(pqh[1]->buf);
171 free(pqh[1]);
172 while (k<= npq>>1)
174 int j= k<<1;
175 if (j<npq && pqh[j]->when > pqh[j+1]->when) j++;
176 if (old->when <= pqh[j]->when) {
177 break;
178 } else {
179 pqh[k]=pqh[j];k=j;
182 pqh[k]=old;
186 static void packet_enqueue(int dir,const unsigned char *buf,int size,int delms)
188 struct timeval v;
190 /* CAPACITY */
191 if (capacity[dir]+capacityplus[dir] > 0) {
192 double capval=capacity[dir];
193 if (capacityplus[dir])
194 capval+=((drand48()*2.0)-1.0)*capacityplus[dir];
195 if ((bufsize[dir]+size) > capval)
196 return;
198 /* */
200 struct packpq *new=malloc(sizeof(struct packpq));
201 if (new==NULL) {
202 fprintf(stderr,"%s: malloc elem %s\n",progname,strerror(errno));
203 exit (1);
205 gettimeofday(&v,NULL);
206 new->when=v.tv_sec * 1000000 + v.tv_usec + delms * 1000;
207 if (new->when > maxwhen) maxwhen=new->when;
208 if (!nofifo && new->when < maxwhen) new->when=maxwhen;
209 new->dir=dir;
210 new->buf=malloc(size);
211 if (new->buf==NULL) {
212 fprintf(stderr,"%s: malloc elem buf %s\n",progname,strerror(errno));
213 exit (1);
215 memcpy(new->buf,buf,size);
216 new->size=size;
217 bufsize[dir]+=size;
218 if (pqh==NULL) {
219 pqh=malloc(PQCHUNK*sizeof(struct packpq *));
220 if (pqh==NULL) {
221 fprintf(stderr,"%s: malloc %s\n",progname,strerror(errno));
222 exit (1);
224 pqh[0]=&sentinel; maxpq=PQCHUNK;
226 if (npq >= maxpq) {
227 pqh=realloc(pqh,(maxpq=maxpq+PQCHUNK) * sizeof(struct packpq *));
228 if (pqh==NULL) {
229 fprintf(stderr,"%s: malloc %s\n",progname,strerror(errno));
230 exit (1);
233 {int k=++npq;
234 while (new->when < pqh[k>>1]->when) {
235 pqh[k]=pqh[k>>1];
236 k >>= 1;
238 pqh[k]=new;
242 void handle_packet(int dir,const unsigned char *buf,int size)
244 /* MTU */
245 if (mtu[dir] > 0 && size > mtu[dir])
246 return;
248 /* LOSS */
249 if (loss[dir]-lossplus[dir] >= 100.0)
250 return;
251 if (loss[dir]+lossplus[dir] > 0) {
252 double losval=(loss[dir]+((drand48()*2.0)-1.0)*lossplus[dir])/100;
253 if (drand48() < losval)
254 return;
257 /* DUP */
258 int times=1;
259 if (ddup[dir]+ddupplus[dir] > 0) {
260 double dupval=(ddup[dir]+((drand48()*2.0)-1.0)*ddupplus[dir])/100;
261 while (drand48() < dupval)
262 times++;
264 while (times>0) {
265 /* SPEED */
266 if (speed[dir]+speedplus[dir] > 0) {
267 double speedval=speed[dir];
268 if (speedplus[dir]) {
269 speedval+=((drand48()*2.0)-1.0)*speedplus[dir];
270 if (speedval<=0) return;
272 if (speed>0) {
273 unsigned int commtime=4*(((unsigned)size)*2000000/((unsigned int)speedval));
274 struct timeval tv;
275 gettimeofday(&tv,NULL);
276 if (timercmp(&tv,&nextspeed[dir], > ))
277 nextspeed[dir]=tv;
278 nextspeed[dir].tv_usec += commtime;
279 nextspeed[dir].tv_sec += nextspeed[dir].tv_usec / 1000000;
280 nextspeed[dir].tv_usec %= 1000000;
284 /* BANDWIDTH */
285 int banddelay=0;
286 if (band[dir]+bandplus[dir] > 0) {
287 double bandval=band[dir];
288 if (bandplus[dir]) {
289 bandval+=((drand48()*2.0)-1.0)*bandplus[dir];
290 if (bandval<=0) return;
292 if (band>0) {
293 unsigned int commtime=4*(((unsigned)size)*2000000/((unsigned int)bandval));
294 struct timeval tv;
295 gettimeofday(&tv,NULL);
296 if (timercmp(&tv,&nextband[dir], > )) {
297 nextband[dir]=tv;
298 banddelay=commtime/1000;
299 } else {
300 timersub(&nextband[dir],&tv,&tv);
301 banddelay=tv.tv_sec*1000 + (tv.tv_usec + commtime)/1000;
303 nextband[dir].tv_usec += commtime;
304 nextband[dir].tv_sec += nextband[dir].tv_usec / 1000000;
305 nextband[dir].tv_usec %= 1000000;
306 } else
307 banddelay=-1;
310 /* DELAY */
311 if (banddelay >= 0) {
312 if (banddelay > 0 || delay[dir]+delayplus[dir] > 0) {
313 double delval=(delay[dir]+((drand48()*2.0)-1.0)*delayplus[dir]);
314 delval=(delval >= 0)?delval+banddelay:banddelay;
315 if (delval > 0) {
316 packet_enqueue(dir,buf,size,(int) delval);
317 } else
318 writepacket(dir,buf,size);
319 } else
320 writepacket(dir,buf,size);
322 times--;
326 #define MIN(X,Y) (((X)<(Y))?(X):(Y))
328 static void splitpacket(const unsigned char *buf,int size,int dir)
330 static unsigned char fragment[BUFSIZE];
331 static unsigned char *fragp;
332 static unsigned int rnx,remaining;
334 //fprintf(stderr,"%s: splitpacket rnx=%d remaining=%d size=%d\n",myname,rnx,remaining,size);
335 if (size==0) return;
336 if (rnx>0) {
337 register int amount=MIN(remaining,size);
338 //fprintf(stderr,"%s: fragment amount %d\n",myname,amount);
339 memcpy(fragp,buf,amount);
340 remaining-=amount;
341 fragp+=amount;
342 buf+=amount;
343 size-=amount;
344 if (remaining==0) {
345 //fprintf(stderr,"%s: delivered defrag %d\n",myname,rnx);
346 handle_packet(dir,fragment,rnx+2);
347 rnx=0;
350 while (size > 0) {
351 rnx=(buf[0]<<8)+buf[1];
352 //fprintf(stderr,"%s: packet %d size %d %x %x dir %d\n",progname,rnx,size-2,buf[0],buf[1],dir);
353 if (rnx>1521) {
354 fprintf(stderr,"%s: Packet length error size %d rnx %d\n",progname,size,rnx);
355 rnx=0;
356 return;
358 if (rnx+2 > size) {
359 //fprintf(stderr,"%s: begin defrag %d\n",myname,rnx);
360 fragp=fragment;
361 memcpy(fragp,buf,size);
362 remaining=rnx+2-size;
363 fragp+=size;
364 size=0;
365 } else {
366 handle_packet(dir,buf,rnx+2);
367 buf+=rnx+2;
368 size-=rnx+2;
369 rnx=0;
374 static void packet_in(int dir)
376 unsigned char buf[BUFSIZE];
377 int n;
378 n=read(pfd[dir].fd,buf,BUFSIZE);
379 if (n == 0)
380 exit (0);
381 splitpacket(buf,n,dir);
384 static void initrand()
386 struct timeval v;
387 gettimeofday(&v,NULL);
388 srand48(v.tv_sec ^ v.tv_usec ^ getpid());
391 static int check_open_fifos(struct pollfd *pfd,int *outfd)
393 int ndirs;
394 struct stat stfd[NPIPES];
395 if (fstat(STDIN_FILENO,&stfd[STDIN_FILENO]) < 0) {
396 fprintf(stderr,"%s: Error on stdin: %s\n",progname,strerror(errno));
397 exit(1);
399 if (fstat(STDOUT_FILENO,&stfd[STDOUT_FILENO]) < 0) {
400 fprintf(stderr,"%s: Error on stdout: %s\n",progname,strerror(errno));
401 exit(1);
403 if (!S_ISFIFO(stfd[STDIN_FILENO].st_mode)) {
404 fprintf(stderr,"%s: Error on stdin: %s\n",progname,"it is not a pipe");
405 exit(1);
407 if (!S_ISFIFO(stfd[STDOUT_FILENO].st_mode)) {
408 fprintf(stderr,"%s: Error on stdin: %s\n",progname,"it is not a pipe");
409 exit(1);
411 if (fstat(STDIN_ALTFILENO,&stfd[0]) < 0) {
412 ndirs=1;
413 pfd[0].fd=STDIN_FILENO;
414 pfd[0].events=POLLIN | POLLHUP;
415 pfd[0].revents=0;
416 outfd[0]=STDOUT_FILENO;
417 } else {
418 if (fstat(outfd[1],&stfd[1]) < 0) {
419 fprintf(stderr,"%s: Error on secondary out: %s\n",progname,strerror(errno));
420 exit(1);
422 if (!S_ISFIFO(stfd[0].st_mode)) {
423 fprintf(stderr,"%s: Error on secondary in: %s\n",progname,"it is not a pipe");
424 exit(1);
426 if (!S_ISFIFO(stfd[1].st_mode)) {
427 fprintf(stderr,"%s: Error on secondary out: %s\n",progname,"it is not a pipe");
428 exit(1);
430 ndirs=2;
431 pfd[LR].fd=STDIN_FILENO;
432 pfd[LR].events=POLLIN | POLLHUP;
433 pfd[LR].revents=0;
434 outfd[LR]=STDOUT_ALTFILENO;
435 pfd[RL].fd=STDIN_ALTFILENO;
436 pfd[RL].events=POLLIN | POLLHUP;
437 pfd[RL].revents=0;
438 outfd[RL]=STDOUT_FILENO;
440 return ndirs;
443 static void cleanup(void)
445 if (mgmt)
446 unlink(mgmt);
449 static void sig_handler(int sig)
451 /*fprintf(stderr,"Caught signal %d, cleaning up and exiting", sig);*/
452 cleanup();
453 signal(sig, SIG_DFL);
454 kill(getpid(), sig);
457 static void setsighandlers()
459 /* setting signal handlers.
460 * * * sets clean termination for SIGHUP, SIGINT and SIGTERM, and simply
461 * * * ignores all the others signals which could cause termination. */
462 struct { int sig; const char *name; int ignore; } signals[] = {
463 { SIGHUP, "SIGHUP", 0 },
464 { SIGINT, "SIGINT", 0 },
465 { SIGPIPE, "SIGPIPE", 1 },
466 { SIGALRM, "SIGALRM", 1 },
467 { SIGTERM, "SIGTERM", 0 },
468 { SIGUSR1, "SIGUSR1", 1 },
469 { SIGUSR2, "SIGUSR2", 1 },
470 { SIGPROF, "SIGPROF", 1 },
471 { SIGVTALRM, "SIGVTALRM", 1 },
472 #ifdef VDE_LINUX
473 { SIGPOLL, "SIGPOLL", 1 },
474 { SIGSTKFLT, "SIGSTKFLT", 1 },
475 { SIGIO, "SIGIO", 1 },
476 { SIGPWR, "SIGPWR", 1 },
477 { SIGUNUSED, "SIGUNUSED", 1 },
478 #endif
479 #ifdef VDE_DARWIN
480 { SIGXCPU, "SIGXCPU", 1 },
481 { SIGXFSZ, "SIGXFSZ", 1 },
482 #endif
483 { 0, NULL, 0 }
486 int i;
487 for(i = 0; signals[i].sig != 0; i++)
488 if(signal(signals[i].sig,
489 signals[i].ignore ? SIG_IGN : sig_handler) < 0)
490 fprintf(stderr,"%s: Setting handler for %s: %s", progname, signals[i].name,
491 strerror(errno));
494 static int openmgmt(char *mgmt)
496 int mgmtconnfd;
497 struct sockaddr_un sun;
498 int one = 1;
500 if((mgmtconnfd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0){
501 fprintf(stderr,"%s: mgmt socket: %s",progname,strerror(errno));
502 exit(1);
504 if(setsockopt(mgmtconnfd, SOL_SOCKET, SO_REUSEADDR, (char *) &one,
505 sizeof(one)) < 0){
506 fprintf(stderr,"%s: mgmt setsockopt: %s",progname,strerror(errno));
507 exit(1);
509 if(fcntl(mgmtconnfd, F_SETFL, O_NONBLOCK) < 0){
510 fprintf(stderr,"%s: Setting O_NONBLOCK on mgmt fd: %s",progname,strerror(errno));
511 exit(1);
513 sun.sun_family = PF_UNIX;
514 snprintf(sun.sun_path,sizeof(sun.sun_path),"%s",mgmt);
515 if(bind(mgmtconnfd, (struct sockaddr *) &sun, sizeof(sun)) < 0){
516 fprintf(stderr,"%s: mgmt bind %s",progname,strerror(errno));
517 exit(1);
519 chmod(sun.sun_path,mgmtmode);
520 if(listen(mgmtconnfd, 15) < 0){
521 fprintf(stderr,"%s: mgmt listen: %s",progname,strerror(errno));
522 exit(1);
524 return mgmtconnfd;
527 static char header[]="\nVDE wirefilter V.%s\n(C) R.Davoli 2005 - GPLv2\n";
528 static char prompt[]="\nVDEwf:";
529 static int newmgmtconn(int fd,struct pollfd *pfd,int nfds)
531 int new;
532 unsigned int len;
533 char buf[MAXCMD];
534 struct sockaddr addr;
535 new = accept(fd, &addr, &len);
536 if(new < 0){
537 fprintf(stderr,"%s: mgmt accept %s",progname,strerror(errno));
538 return nfds;
540 if (nfds < NPFD) {
541 snprintf(buf,MAXCMD,header,PACKAGE_VERSION);
542 write(new,buf,strlen(buf));
543 write(new,prompt,strlen(prompt));
544 pfd[nfds].fd=new;
545 pfd[nfds].events=POLLIN | POLLHUP;
546 pfd[nfds].revents=0;
547 return ++nfds;
548 } else {
549 fprintf(stderr,"%s: too many mgmt connections",progname);
550 close (new);
551 return nfds;
555 static void printoutc(int fd, const char *format, ...)
557 va_list arg;
558 char outbuf[MAXCMD+1];
560 va_start (arg, format);
561 vsnprintf(outbuf,MAXCMD,format,arg);
562 strcat(outbuf,"\n");
563 write(fd,outbuf,strlen(outbuf));
566 static int setdelay(int fd,char *s)
568 readdualvalue(s,delay,delayplus);
569 return 0;
572 static int setloss(int fd,char *s)
574 readdualvalue(s,loss,lossplus);
575 return 0;
578 static int setddup(int fd,char *s)
580 readdualvalue(s,ddup,ddupplus);
581 return 0;
584 static int setband(int fd,char *s)
586 readdualvalue(s,band,bandplus);
587 return 0;
590 static int setnoise(int fd,char *s)
592 readdualvalue(s,noise,noiseplus);
593 return 0;
596 static int setmtu(int fd,char *s)
598 readdualvalue(s,mtu,mtuplus);
599 return 0;
602 static int setspeed(int fd,char *s)
604 readdualvalue(s,speed,speedplus);
605 return 0;
608 static int setcapacity(int fd,char *s)
610 readdualvalue(s,capacity,capacityplus);
611 return 0;
614 static int setfifo(int fd,char *s)
616 int n=atoi(s);
617 if (n==0)
618 nofifo=1;
619 else
620 nofifo=0;
621 return 0;
624 static int logout(int fd,char *s)
626 return -1;
629 static int doshutdown(int fd,char *s)
631 exit(0);
635 static int help(int fd,char *s)
637 printoutc(fd, "help: print a summary of mgmt commands");
638 printoutc(fd, "showinfo: show status and parameter values");
639 printoutc(fd, "loss: set loss percentage");
640 printoutc(fd, "delay: set delay ms");
641 printoutc(fd, "dup: set dup packet percentage");
642 printoutc(fd, "bandwidth: set channel bandwidth bytes/sec");
643 printoutc(fd, "speed: set interface speed bytes/sec");
644 printoutc(fd, "noise: set noise factor bits/Mbyte");
645 printoutc(fd, "mtu: set channel MTU (bytes)");
646 printoutc(fd, "capacity: set channel capacity (bytes)");
647 printoutc(fd, "fifo: set channel fifoness");
648 printoutc(fd, "shutdown: shut the channel down");
649 printoutc(fd, "logout: log out from this mgmt session");
650 return 0;
653 static int showinfo(int fd,char *s)
655 printoutc(fd, "WireFilter: %sdirectional",(ndirs==2)?"bi":"mono");
656 if (ndirs==2) {
657 printoutc(fd, "Loss L->R %g+%g R->L %g+%g",loss[LR],lossplus[LR],loss[RL],lossplus[RL]);
658 printoutc(fd, "Delay L->R %g+%g R->L %g+%g",delay[LR],delayplus[LR],delay[RL],delayplus[RL]);
659 printoutc(fd, "Dup L->R %g+%g R->L %g+%g",ddup[LR],ddupplus[LR],ddup[RL],ddupplus[RL]);
660 printoutc(fd, "Bandw L->R %g+%g R->L %g+%g",band[LR],bandplus[LR],band[RL],bandplus[RL]);
661 printoutc(fd, "Speed L->R %g+%g R->L %g+%g",speed[LR],speedplus[LR],speed[RL],speedplus[RL]);
662 printoutc(fd, "Noise L->R %g+%g R->L %g+%g",noise[LR],noiseplus[LR],noise[RL],noiseplus[RL]);
663 printoutc(fd, "MTU L->R %g R->L %g ",mtu[LR],mtu[RL]);
664 printoutc(fd, "Cap. L->R %g+%g R->L %g+%g",capacity[LR],capacityplus[LR],capacity[RL],capacityplus[RL]);
665 printoutc(fd, "Current Delay Queue size: L->R %d R->L %d ",bufsize[LR],bufsize[RL]);
666 } else {
667 printoutc(fd, "Loss %g+%g",loss[0],lossplus[0]);
668 printoutc(fd, "Delay %g+%g",delay[0],delayplus[0]);
669 printoutc(fd, "Dup %g+%g",ddup[0],ddupplus[0]);
670 printoutc(fd, "Bandw %g+%g",band[0],bandplus[0]);
671 printoutc(fd, "Speed %g+%g",speed[0],speedplus[0]);
672 printoutc(fd, "Noise %g+%g",noise[0],noiseplus[0]);
673 printoutc(fd, "MTU %g",mtu[0]);
674 printoutc(fd, "Cap. %g+%g",capacity[0],capacityplus[0]);
675 printoutc(fd, "Current Delay Queue size: %d",bufsize[0]);
677 printoutc(fd,"Fifoness %s",(nofifo == 0)?"TRUE":"FALSE");
678 printoutc(fd,"Waiting packets in delay queues %d",npq);
679 return 0;
682 static struct comlist {
683 char *tag;
684 int (*fun)(int fd,char *arg);
685 } commandlist [] = {
686 {"help", help},
687 {"showinfo",showinfo},
688 {"delay",setdelay},
689 {"loss",setloss},
690 {"dup",setddup},
691 {"bandwidth",setband},
692 {"band",setband},
693 {"speed",setspeed},
694 {"capacity",setcapacity},
695 {"noise",setnoise},
696 {"mtu",setmtu},
697 {"fifo",setfifo},
698 {"logout",logout},
699 {"shutdown",doshutdown}
702 #define NCL sizeof(commandlist)/sizeof(struct comlist)
704 static int handle_cmd(int fd,char *inbuf)
706 int rv=ENOSYS;
707 int i;
708 while (*inbuf == ' ' || *inbuf == '\t') inbuf++;
709 if (*inbuf != '\0' && *inbuf != '#') {
710 for (i=0; i<NCL
711 && strncmp(commandlist[i].tag,inbuf,strlen(commandlist[i].tag))!=0;
712 i++)
714 if (i<NCL)
716 inbuf += strlen(commandlist[i].tag);
717 while (*inbuf == ' ' || *inbuf == '\t') inbuf++;
718 rv=commandlist[i].fun(fd,inbuf);
720 printoutc(fd,"1%03d %s",rv,strerror(rv));
721 return rv;
723 return rv;
726 static int mgmtcommand(int fd)
728 char buf[MAXCMD+1];
729 int n,rv;
730 n = read(fd, buf, MAXCMD);
731 if (n<0) {
732 fprintf(stderr,"%s: read from mgmt %s",progname,strerror(errno));
733 return 0;
735 else if (n==0)
736 return -1;
737 else {
738 buf[n]=0;
739 rv=handle_cmd(fd,buf);
740 if (rv>=0)
741 write(fd,prompt,strlen(prompt));
742 return rv;
746 static int delmgmtconn(int i,struct pollfd *pfd,int nfds)
748 if (i<nfds) {
749 close(pfd[i].fd);
750 memmove(pfd+i,pfd+i+1,sizeof (struct pollfd) * (nfds-i-1));
751 nfds--;
753 return nfds;
756 void usage(void)
758 fprintf(stderr,"Usage: %s OPTIONS\n"
759 "\t--help|-h\n"
760 "\t--loss|-l loss_percentage\n"
761 "\t--delay|-d delay_ms\n"
762 "\t--dup|-D dup_percentage\n"
763 "\t--band|-b bandwidth\n"
764 "\t--speed|-s interface_speed\n"
765 "\t--capacity|-c delay_channel_capacity\n"
766 "\t--noise|-n noise_bits/megabye\n"
767 "\t--mtu|-m\n mtu_size"
768 "\t--nofifo|-N\n"
769 "\t--mgmt|-M management_socket\n"
770 "\t--mgmtmode management_permission(octal)\n"
771 ,progname);
772 exit (1);
775 int main(int argc,char *argv[])
777 int n;
778 int npfd;
779 int option_index;
780 int mgmtindex=-1;
781 static struct option long_options[] = {
782 {"help",0 , 0, 'h'},
783 {"loss", 1, 0, 'l'},
784 {"delay",1 , 0, 'd'},
785 {"dup",1 , 0, 'D'},
786 {"band",1 , 0, 'b'},
787 {"speed",1 , 0, 's'},
788 {"capacity",1 , 0, 'c'},
789 {"noise",1 , 0, 'n'},
790 {"mtu",1 , 0, 'm'},
791 {"nofifo",0 , 0, 'N'},
792 {"mgmt", 1, 0, 'M'},
793 {"mgmtmode", 1, 0, MGMTMODEARG}
795 progname=basename(argv[0]);
797 setsighandlers();
798 atexit(cleanup);
800 ndirs=check_open_fifos(pfd,outfd);
802 while(1) {
803 int c;
804 c = GETOPT_LONG (argc, argv, "hnl:d:M:D:m:b:s:c:",
805 long_options, &option_index);
806 if (c<0)
807 break;
808 switch (c) {
809 case 'h':
810 usage();
811 break;
812 case 'd':
813 readdualvalue(optarg,delay,delayplus);
814 break;
815 case 'l':
816 readdualvalue(optarg,loss,lossplus);
817 break;
818 case 'D':
819 readdualvalue(optarg,ddup,ddupplus);
820 break;
821 case 'b':
822 readdualvalue(optarg,band,bandplus);
823 break;
824 case 'm':
825 readdualvalue(optarg,mtu,mtuplus);
826 break;
827 case 'n':
828 readdualvalue(optarg,noise,noiseplus);
829 break;
830 case 's':
831 readdualvalue(optarg,speed,speedplus);
832 break;
833 case 'c':
834 readdualvalue(optarg,capacity,capacityplus);
835 break;
836 case 'M':
837 mgmt=strdup(optarg);
838 break;
839 case 'N':
840 nofifo=1;
841 break;
842 case MGMTMODEARG:
843 sscanf(optarg,"%o",&mgmtmode);
844 break;
845 default:
846 usage();
847 break;
850 if (optind < argc)
851 usage();
853 if (ndirs==2)
854 fprintf(stderr,"%s: bidirectional filter starting...\n",progname);
855 else
856 fprintf(stderr,"%s: monodirectional filter starting...\n",progname);
858 npfd=ndirs;
860 if(mgmt != NULL) {
861 int mgmtfd=openmgmt(mgmt);
862 mgmtindex=npfd;
863 pfd[mgmtindex].fd=mgmtfd;
864 pfd[mgmtindex].events=POLLIN | POLLHUP;
865 pfd[mgmtindex].revents=0;
866 npfd++;
869 initrand();
870 while(1) {
871 int delay=nextms();
872 pfd[0].events |= POLLIN;
873 if (speed[LR] > 0) {
874 struct timeval tv;
875 int speeddelay;
876 gettimeofday(&tv,NULL);
877 if (timercmp(&tv, &nextspeed[LR], <)) {
878 timersub(&nextspeed[LR],&tv,&tv);
879 speeddelay=tv.tv_sec*1000 + tv.tv_usec/1000;
880 if (speeddelay > 0) {
881 pfd[0].events &= ~POLLIN;
882 if (speeddelay < delay || delay < 0) delay=speeddelay;
886 if (ndirs > 1) {
887 pfd[1].events |= POLLIN;
888 if (speed[RL] > 0) {
889 struct timeval tv;
890 int speeddelay;
891 if (timercmp(&tv, &nextspeed[RL], <)) {
892 gettimeofday(&tv,NULL);
893 timersub(&nextspeed[RL],&tv,&tv);
894 speeddelay=tv.tv_sec*1000 + tv.tv_usec/1000;
895 if (speeddelay > 0) {
896 pfd[1].events &= ~POLLIN;
897 if (speeddelay < delay || delay < 0) delay=speeddelay;
902 n=poll(pfd,npfd,delay);
903 if (pfd[0].revents & POLLHUP || (ndirs>1 && pfd[1].revents & POLLHUP))
904 exit(0);
905 if (pfd[0].revents & POLLIN) {
906 packet_in(LR);
908 if (ndirs>1 && pfd[1].revents & POLLIN) {
909 packet_in(RL);
911 if (mgmtindex >= 0 && pfd[mgmtindex].revents != 0)
912 npfd=newmgmtconn(pfd[mgmtindex].fd,pfd,npfd);
913 if (npfd > mgmtindex+1) {
914 register int i;
915 for (i=mgmtindex+1;i<npfd;i++) {
916 if (pfd[i].revents & POLLHUP ||
917 (pfd[i].revents & POLLIN && mgmtcommand(pfd[i].fd) < 0))
918 npfd=delmgmtconn(i,pfd,npfd);
921 packet_dequeue();