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).
24 #include <sys/types.h>
26 #include <sys/socket.h>
34 static int alternate_stdin
;
35 static int alternate_stdout
;
36 //#define STDIN_ALTFILENO 3
37 //#define STDOUT_ALTFILENO 4
38 #define NPFD NPIPES+MAXCONN+1
39 struct pollfd pfd
[NPFD
];
46 double loss
[2],lossplus
[2];
47 double delay
[2],delayplus
[2];
48 double ddup
[2],ddupplus
[2];
49 double band
[2],bandplus
[2];
50 double speed
[2],speedplus
[2];
51 double capacity
[2],capacityplus
[2];
52 double noise
[2],noiseplus
[2];
53 double mtu
[2],mtuplus
[2];
54 struct timeval nextband
[2];
55 struct timeval nextspeed
[2];
62 #define MGMTMODEARG 129
67 static void readdualvalue(char *s
,double *val
,double *valplus
)
74 while ((s
[n
] == ' ' || s
[n
] == '\n' || s
[n
] == '\t') && n
>0)
96 if ((n
=sscanf(s
,"%lf+%lf",&v
,&vplus
)) > 0) {
97 val
[LR
]=val
[RL
]=v
*mult
;
98 valplus
[LR
]=valplus
[RL
]=vplus
*mult
;
99 } else if ((n
=sscanf(s
,"LR%lf+%lf",&v
,&vplus
)) > 0) {
101 valplus
[LR
]=vplus
*mult
;
102 } else if ((n
=sscanf(s
,"RL%lf+%lf",&v
,&vplus
)) > 0) {
104 valplus
[RL
]=vplus
*mult
;
109 unsigned long long when
;
116 struct packpq sentinel
={0,0,NULL
,0};
118 unsigned long long maxwhen
;
127 gettimeofday(&v
,NULL
);
128 deltat
=pqh
[1]->when
-(v
.tv_sec
*1000000+v
.tv_usec
);
129 return (deltat
>0)?(int)(deltat
/1000):0;
134 int writepacket(int dir
,const unsigned char *buf
,int size
)
137 if (noise
[dir
]+noiseplus
[dir
] > 0) {
138 double noiseval
=noise
[dir
];
140 if (noiseplus
) noiseval
+=((drand48()*2.0)-1.0)*noiseplus
[dir
];
141 while ((drand48()*8*MEGA
) < (size
-2)*8*noiseval
)
144 unsigned char noisedpacket
[BUFSIZE
];
145 memcpy(noisedpacket
,buf
,size
);
147 int flippedbit
=(drand48()*size
*8);
148 noisedpacket
[(flippedbit
>> 3) + 2] ^= 1<<(flippedbit
& 0x7);
151 return write(outfd
[dir
],noisedpacket
,size
);
153 return write(outfd
[dir
],buf
,size
);
155 return write(outfd
[dir
],buf
,size
);
158 /* packet queues are priority queues implemented on a heap.
159 * enqueue time = dequeue time = O(log n) max&mean
162 static void packet_dequeue()
165 gettimeofday(&v
,NULL
);
166 unsigned long long now
=v
.tv_sec
*1000000+v
.tv_usec
;
167 while (npq
>0 && pqh
[1]->when
<= now
) {
168 struct packpq
*old
=pqh
[npq
--];
170 bufsize
[pqh
[1]->dir
] -= pqh
[1]->size
;
171 writepacket(pqh
[1]->dir
,pqh
[1]->buf
,pqh
[1]->size
);
177 if (j
<npq
&& pqh
[j
]->when
> pqh
[j
+1]->when
) j
++;
178 if (old
->when
<= pqh
[j
]->when
) {
188 static void packet_enqueue(int dir
,const unsigned char *buf
,int size
,int delms
)
193 if (capacity
[dir
]+capacityplus
[dir
] > 0) {
194 double capval
=capacity
[dir
];
195 if (capacityplus
[dir
])
196 capval
+=((drand48()*2.0)-1.0)*capacityplus
[dir
];
197 if ((bufsize
[dir
]+size
) > capval
)
202 struct packpq
*new=malloc(sizeof(struct packpq
));
204 fprintf(stderr
,"%s: malloc elem %s\n",progname
,strerror(errno
));
207 gettimeofday(&v
,NULL
);
208 new->when
=v
.tv_sec
* 1000000 + v
.tv_usec
+ delms
* 1000;
209 if (new->when
> maxwhen
) maxwhen
=new->when
;
210 if (!nofifo
&& new->when
< maxwhen
) new->when
=maxwhen
;
212 new->buf
=malloc(size
);
213 if (new->buf
==NULL
) {
214 fprintf(stderr
,"%s: malloc elem buf %s\n",progname
,strerror(errno
));
217 memcpy(new->buf
,buf
,size
);
221 pqh
=malloc(PQCHUNK
*sizeof(struct packpq
*));
223 fprintf(stderr
,"%s: malloc %s\n",progname
,strerror(errno
));
226 pqh
[0]=&sentinel
; maxpq
=PQCHUNK
;
229 pqh
=realloc(pqh
,(maxpq
=maxpq
+PQCHUNK
) * sizeof(struct packpq
*));
231 fprintf(stderr
,"%s: malloc %s\n",progname
,strerror(errno
));
236 while (new->when
< pqh
[k
>>1]->when
) {
244 void handle_packet(int dir
,const unsigned char *buf
,int size
)
247 if (mtu
[dir
] > 0 && size
> mtu
[dir
])
251 if (loss
[dir
]-lossplus
[dir
] >= 100.0)
253 if (loss
[dir
]+lossplus
[dir
] > 0) {
254 double losval
=(loss
[dir
]+((drand48()*2.0)-1.0)*lossplus
[dir
])/100;
255 if (drand48() < losval
)
261 if (ddup
[dir
]+ddupplus
[dir
] > 0) {
262 double dupval
=(ddup
[dir
]+((drand48()*2.0)-1.0)*ddupplus
[dir
])/100;
263 while (drand48() < dupval
)
270 if (speed
[dir
]+speedplus
[dir
] > 0) {
271 double speedval
=speed
[dir
];
272 if (speedplus
[dir
]) {
273 speedval
+=((drand48()*2.0)-1.0)*speedplus
[dir
];
274 if (speedval
<=0) return;
277 unsigned int commtime
=((unsigned)size
)*1000000/((unsigned int)speedval
);
279 gettimeofday(&tv
,NULL
);
280 banddelay
=commtime
/1000;
281 if (timercmp(&tv
,&nextspeed
[dir
], > ))
283 nextspeed
[dir
].tv_usec
+= commtime
;
284 nextspeed
[dir
].tv_sec
+= nextspeed
[dir
].tv_usec
/ 1000000;
285 nextspeed
[dir
].tv_usec
%= 1000000;
290 if (band
[dir
]+bandplus
[dir
] > 0) {
291 double bandval
=band
[dir
];
293 bandval
+=((drand48()*2.0)-1.0)*bandplus
[dir
];
294 if (bandval
<=0) return;
297 unsigned int commtime
=((unsigned)size
)*1000000/((unsigned int)bandval
);
299 gettimeofday(&tv
,NULL
);
300 if (timercmp(&tv
,&nextband
[dir
], > )) {
302 banddelay
=commtime
/1000;
304 timersub(&nextband
[dir
],&tv
,&tv
);
305 banddelay
=tv
.tv_sec
*1000 + (tv
.tv_usec
+ commtime
)/1000;
307 nextband
[dir
].tv_usec
+= commtime
;
308 nextband
[dir
].tv_sec
+= nextband
[dir
].tv_usec
/ 1000000;
309 nextband
[dir
].tv_usec
%= 1000000;
315 if (banddelay
>= 0) {
316 if (banddelay
> 0 || delay
[dir
]+delayplus
[dir
] > 0) {
317 double delval
=(delay
[dir
]+((drand48()*2.0)-1.0)*delayplus
[dir
]);
318 delval
=(delval
>= 0)?delval
+banddelay
:banddelay
;
320 packet_enqueue(dir
,buf
,size
,(int) delval
);
322 writepacket(dir
,buf
,size
);
324 writepacket(dir
,buf
,size
);
330 #define MIN(X,Y) (((X)<(Y))?(X):(Y))
332 static void splitpacket(const unsigned char *buf
,int size
,int dir
)
334 static unsigned char fragment
[BUFSIZE
][2];
335 static unsigned char *fragp
[2];
336 static unsigned int rnx
[2],remaining
[2];
338 //fprintf(stderr,"%s: splitpacket rnx=%d remaining=%d size=%d\n",progname,rnx[dir],remaining[dir],size);
341 register int amount
=MIN(remaining
[dir
],size
);
342 //fprintf(stderr,"%s: fragment amount %d\n",progname,amount);
343 memcpy(fragp
[dir
],buf
,amount
);
344 remaining
[dir
]-=amount
;
348 if (remaining
[dir
]==0) {
349 //fprintf(stderr,"%s: delivered defrag %d\n",progname,rnx[dir]);
350 handle_packet(dir
,fragment
[dir
],rnx
[dir
]+2);
355 rnx
[dir
]=(buf
[0]<<8)+buf
[1];
356 //fprintf(stderr,"%s: packet %d size %d %x %x dir %d\n",progname,rnx[dir],size-2,buf[0],buf[1],dir);
358 fprintf(stderr
,"%s: Packet length error size %d rnx %d\n",progname
,size
,rnx
[dir
]);
362 if (rnx
[dir
]+2 > size
) {
363 //fprintf(stderr,"%s: begin defrag %d\n",progname,rnx[dir]);
364 fragp
[dir
]=fragment
[dir
];
365 memcpy(fragp
[dir
],buf
,size
);
366 remaining
[dir
]=rnx
[dir
]+2-size
;
370 handle_packet(dir
,buf
,rnx
[dir
]+2);
378 static void packet_in(int dir
)
380 unsigned char buf
[BUFSIZE
];
382 n
=read(pfd
[dir
].fd
,buf
,BUFSIZE
);
385 splitpacket(buf
,n
,dir
);
388 static void initrand()
391 gettimeofday(&v
,NULL
);
392 srand48(v
.tv_sec
^ v
.tv_usec
^ getpid());
395 static int check_open_fifos(struct pollfd
*pfd
,int *outfd
)
398 struct stat stfd
[NPIPES
];
401 env_in
=getenv("ALTERNATE_STDIN");
402 env_out
=getenv("ALTERNATE_STDOUT");
404 alternate_stdin
=atoi(env_in
);
406 alternate_stdout
=atoi(env_out
);
407 if (fstat(STDIN_FILENO
,&stfd
[STDIN_FILENO
]) < 0) {
408 fprintf(stderr
,"%s: Error on stdin: %s\n",progname
,strerror(errno
));
411 if (fstat(STDOUT_FILENO
,&stfd
[STDOUT_FILENO
]) < 0) {
412 fprintf(stderr
,"%s: Error on stdout: %s\n",progname
,strerror(errno
));
415 if (!S_ISFIFO(stfd
[STDIN_FILENO
].st_mode
)) {
416 fprintf(stderr
,"%s: Error on stdin: %s\n",progname
,"it is not a pipe");
419 if (!S_ISFIFO(stfd
[STDOUT_FILENO
].st_mode
)) {
420 fprintf(stderr
,"%s: Error on stdin: %s\n",progname
,"it is not a pipe");
423 if (env_in
== NULL
|| fstat(alternate_stdin
,&stfd
[0]) < 0) {
425 pfd
[0].fd
=STDIN_FILENO
;
426 pfd
[0].events
=POLLIN
| POLLHUP
;
428 outfd
[0]=STDOUT_FILENO
;
430 if (fstat(outfd
[1],&stfd
[1]) < 0) {
431 fprintf(stderr
,"%s: Error on secondary out: %s\n",progname
,strerror(errno
));
434 if (!S_ISFIFO(stfd
[0].st_mode
)) {
435 fprintf(stderr
,"%s: Error on secondary in: %s\n",progname
,"it is not a pipe");
438 if (!S_ISFIFO(stfd
[1].st_mode
)) {
439 fprintf(stderr
,"%s: Error on secondary out: %s\n",progname
,"it is not a pipe");
443 pfd
[LR
].fd
=STDIN_FILENO
;
444 pfd
[LR
].events
=POLLIN
| POLLHUP
;
446 outfd
[LR
]=alternate_stdout
;
447 pfd
[RL
].fd
=alternate_stdin
;
448 pfd
[RL
].events
=POLLIN
| POLLHUP
;
450 outfd
[RL
]=STDOUT_FILENO
;
455 static void cleanup(void)
461 static void sig_handler(int sig
)
463 /*fprintf(stderr,"Caught signal %d, cleaning up and exiting", sig);*/
465 signal(sig
, SIG_DFL
);
469 static void setsighandlers()
471 /* setting signal handlers.
472 * * * sets clean termination for SIGHUP, SIGINT and SIGTERM, and simply
473 * * * ignores all the others signals which could cause termination. */
474 struct { int sig
; const char *name
; int ignore
; } signals
[] = {
475 { SIGHUP
, "SIGHUP", 0 },
476 { SIGINT
, "SIGINT", 0 },
477 { SIGPIPE
, "SIGPIPE", 1 },
478 { SIGALRM
, "SIGALRM", 1 },
479 { SIGTERM
, "SIGTERM", 0 },
480 { SIGUSR1
, "SIGUSR1", 1 },
481 { SIGUSR2
, "SIGUSR2", 1 },
482 { SIGPROF
, "SIGPROF", 1 },
483 { SIGVTALRM
, "SIGVTALRM", 1 },
485 { SIGPOLL
, "SIGPOLL", 1 },
486 { SIGSTKFLT
, "SIGSTKFLT", 1 },
487 { SIGIO
, "SIGIO", 1 },
488 { SIGPWR
, "SIGPWR", 1 },
489 { SIGUNUSED
, "SIGUNUSED", 1 },
492 { SIGXCPU
, "SIGXCPU", 1 },
493 { SIGXFSZ
, "SIGXFSZ", 1 },
499 for(i
= 0; signals
[i
].sig
!= 0; i
++)
500 if(signal(signals
[i
].sig
,
501 signals
[i
].ignore
? SIG_IGN
: sig_handler
) < 0)
502 fprintf(stderr
,"%s: Setting handler for %s: %s", progname
, signals
[i
].name
,
506 static int openmgmt(char *mgmt
)
509 struct sockaddr_un sun
;
512 if((mgmtconnfd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0){
513 fprintf(stderr
,"%s: mgmt socket: %s",progname
,strerror(errno
));
516 if(setsockopt(mgmtconnfd
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &one
,
518 fprintf(stderr
,"%s: mgmt setsockopt: %s",progname
,strerror(errno
));
521 if(fcntl(mgmtconnfd
, F_SETFL
, O_NONBLOCK
) < 0){
522 fprintf(stderr
,"%s: Setting O_NONBLOCK on mgmt fd: %s",progname
,strerror(errno
));
525 sun
.sun_family
= PF_UNIX
;
526 snprintf(sun
.sun_path
,sizeof(sun
.sun_path
),"%s",mgmt
);
527 if(bind(mgmtconnfd
, (struct sockaddr
*) &sun
, sizeof(sun
)) < 0){
528 fprintf(stderr
,"%s: mgmt bind %s",progname
,strerror(errno
));
531 chmod(sun
.sun_path
,mgmtmode
);
532 if(listen(mgmtconnfd
, 15) < 0){
533 fprintf(stderr
,"%s: mgmt listen: %s",progname
,strerror(errno
));
539 static char header
[]="\nVDE wirefilter V.%s\n(C) R.Davoli 2005 - GPLv2\n";
540 static char prompt
[]="\nVDEwf:";
541 static int newmgmtconn(int fd
,struct pollfd
*pfd
,int nfds
)
546 struct sockaddr addr
;
547 new = accept(fd
, &addr
, &len
);
549 fprintf(stderr
,"%s: mgmt accept %s",progname
,strerror(errno
));
553 snprintf(buf
,MAXCMD
,header
,PACKAGE_VERSION
);
554 write(new,buf
,strlen(buf
));
555 write(new,prompt
,strlen(prompt
));
557 pfd
[nfds
].events
=POLLIN
| POLLHUP
;
561 fprintf(stderr
,"%s: too many mgmt connections",progname
);
567 static void printoutc(int fd
, const char *format
, ...)
570 char outbuf
[MAXCMD
+1];
572 va_start (arg
, format
);
573 vsnprintf(outbuf
,MAXCMD
,format
,arg
);
575 write(fd
,outbuf
,strlen(outbuf
));
578 static int setdelay(int fd
,char *s
)
580 readdualvalue(s
,delay
,delayplus
);
584 static int setloss(int fd
,char *s
)
586 readdualvalue(s
,loss
,lossplus
);
590 static int setddup(int fd
,char *s
)
592 readdualvalue(s
,ddup
,ddupplus
);
596 static int setband(int fd
,char *s
)
598 readdualvalue(s
,band
,bandplus
);
602 static int setnoise(int fd
,char *s
)
604 readdualvalue(s
,noise
,noiseplus
);
608 static int setmtu(int fd
,char *s
)
610 readdualvalue(s
,mtu
,mtuplus
);
614 static int setspeed(int fd
,char *s
)
616 readdualvalue(s
,speed
,speedplus
);
620 static int setcapacity(int fd
,char *s
)
622 readdualvalue(s
,capacity
,capacityplus
);
626 static int setfifo(int fd
,char *s
)
636 static int logout(int fd
,char *s
)
641 static int doshutdown(int fd
,char *s
)
647 static int help(int fd
,char *s
)
649 printoutc(fd
, "help: print a summary of mgmt commands");
650 printoutc(fd
, "showinfo: show status and parameter values");
651 printoutc(fd
, "loss: set loss percentage");
652 printoutc(fd
, "delay: set delay ms");
653 printoutc(fd
, "dup: set dup packet percentage");
654 printoutc(fd
, "bandwidth: set channel bandwidth bytes/sec");
655 printoutc(fd
, "speed: set interface speed bytes/sec");
656 printoutc(fd
, "noise: set noise factor bits/Mbyte");
657 printoutc(fd
, "mtu: set channel MTU (bytes)");
658 printoutc(fd
, "capacity: set channel capacity (bytes)");
659 printoutc(fd
, "fifo: set channel fifoness");
660 printoutc(fd
, "shutdown: shut the channel down");
661 printoutc(fd
, "logout: log out from this mgmt session");
665 static int showinfo(int fd
,char *s
)
667 printoutc(fd
, "WireFilter: %sdirectional",(ndirs
==2)?"bi":"mono");
669 printoutc(fd
, "Loss L->R %g+%g R->L %g+%g",loss
[LR
],lossplus
[LR
],loss
[RL
],lossplus
[RL
]);
670 printoutc(fd
, "Delay L->R %g+%g R->L %g+%g",delay
[LR
],delayplus
[LR
],delay
[RL
],delayplus
[RL
]);
671 printoutc(fd
, "Dup L->R %g+%g R->L %g+%g",ddup
[LR
],ddupplus
[LR
],ddup
[RL
],ddupplus
[RL
]);
672 printoutc(fd
, "Bandw L->R %g+%g R->L %g+%g",band
[LR
],bandplus
[LR
],band
[RL
],bandplus
[RL
]);
673 printoutc(fd
, "Speed L->R %g+%g R->L %g+%g",speed
[LR
],speedplus
[LR
],speed
[RL
],speedplus
[RL
]);
674 printoutc(fd
, "Noise L->R %g+%g R->L %g+%g",noise
[LR
],noiseplus
[LR
],noise
[RL
],noiseplus
[RL
]);
675 printoutc(fd
, "MTU L->R %g R->L %g ",mtu
[LR
],mtu
[RL
]);
676 printoutc(fd
, "Cap. L->R %g+%g R->L %g+%g",capacity
[LR
],capacityplus
[LR
],capacity
[RL
],capacityplus
[RL
]);
677 printoutc(fd
, "Current Delay Queue size: L->R %d R->L %d ",bufsize
[LR
],bufsize
[RL
]);
679 printoutc(fd
, "Loss %g+%g",loss
[0],lossplus
[0]);
680 printoutc(fd
, "Delay %g+%g",delay
[0],delayplus
[0]);
681 printoutc(fd
, "Dup %g+%g",ddup
[0],ddupplus
[0]);
682 printoutc(fd
, "Bandw %g+%g",band
[0],bandplus
[0]);
683 printoutc(fd
, "Speed %g+%g",speed
[0],speedplus
[0]);
684 printoutc(fd
, "Noise %g+%g",noise
[0],noiseplus
[0]);
685 printoutc(fd
, "MTU %g",mtu
[0]);
686 printoutc(fd
, "Cap. %g+%g",capacity
[0],capacityplus
[0]);
687 printoutc(fd
, "Current Delay Queue size: %d",bufsize
[0]);
689 printoutc(fd
,"Fifoness %s",(nofifo
== 0)?"TRUE":"FALSE");
690 printoutc(fd
,"Waiting packets in delay queues %d",npq
);
694 static struct comlist
{
696 int (*fun
)(int fd
,char *arg
);
699 {"showinfo",showinfo
},
703 {"bandwidth",setband
},
706 {"capacity",setcapacity
},
711 {"shutdown",doshutdown
}
714 #define NCL sizeof(commandlist)/sizeof(struct comlist)
716 static int handle_cmd(int fd
,char *inbuf
)
720 while (*inbuf
== ' ' || *inbuf
== '\t') inbuf
++;
721 if (*inbuf
!= '\0' && *inbuf
!= '#') {
723 && strncmp(commandlist
[i
].tag
,inbuf
,strlen(commandlist
[i
].tag
))!=0;
728 inbuf
+= strlen(commandlist
[i
].tag
);
729 while (*inbuf
== ' ' || *inbuf
== '\t') inbuf
++;
730 rv
=commandlist
[i
].fun(fd
,inbuf
);
732 printoutc(fd
,"1%03d %s",rv
,strerror(rv
));
738 static int mgmtcommand(int fd
)
742 n
= read(fd
, buf
, MAXCMD
);
744 fprintf(stderr
,"%s: read from mgmt %s",progname
,strerror(errno
));
751 rv
=handle_cmd(fd
,buf
);
753 write(fd
,prompt
,strlen(prompt
));
758 static int delmgmtconn(int i
,struct pollfd
*pfd
,int nfds
)
762 memmove(pfd
+i
,pfd
+i
+1,sizeof (struct pollfd
) * (nfds
-i
-1));
770 fprintf(stderr
,"Usage: %s OPTIONS\n"
772 "\t--loss|-l loss_percentage\n"
773 "\t--delay|-d delay_ms\n"
774 "\t--dup|-D dup_percentage\n"
775 "\t--band|-b bandwidth(bytes/s)\n"
776 "\t--speed|-s interface_speed(bytes/s)\n"
777 "\t--capacity|-c delay_channel_capacity\n"
778 "\t--noise|-n noise_bits/megabye\n"
779 "\t--mtu|-m mtu_size\n"
781 "\t--mgmt|-M management_socket\n"
782 "\t--mgmtmode management_permission(octal)\n"
787 int main(int argc
,char *argv
[])
793 static struct option long_options
[] = {
796 {"delay",1 , 0, 'd'},
799 {"speed",1 , 0, 's'},
800 {"capacity",1 , 0, 'c'},
801 {"noise",1 , 0, 'n'},
803 {"nofifo",0 , 0, 'N'},
805 {"mgmtmode", 1, 0, MGMTMODEARG
}
807 progname
=basename(argv
[0]);
812 ndirs
=check_open_fifos(pfd
,outfd
);
818 c
= GETOPT_LONG (argc
, argv
, "hnl:d:M:D:m:b:s:c:",
819 long_options
, &option_index
);
827 readdualvalue(optarg
,delay
,delayplus
);
830 readdualvalue(optarg
,loss
,lossplus
);
833 readdualvalue(optarg
,ddup
,ddupplus
);
836 readdualvalue(optarg
,band
,bandplus
);
839 readdualvalue(optarg
,mtu
,mtuplus
);
842 readdualvalue(optarg
,noise
,noiseplus
);
845 readdualvalue(optarg
,speed
,speedplus
);
848 readdualvalue(optarg
,capacity
,capacityplus
);
857 sscanf(optarg
,"%o",&mgmtmode
);
868 fprintf(stderr
,"%s: bidirectional filter starting...\n",progname
);
870 fprintf(stderr
,"%s: monodirectional filter starting...\n",progname
);
875 int mgmtfd
=openmgmt(mgmt
);
877 pfd
[mgmtindex
].fd
=mgmtfd
;
878 pfd
[mgmtindex
].events
=POLLIN
| POLLHUP
;
879 pfd
[mgmtindex
].revents
=0;
886 pfd
[0].events
|= POLLIN
;
890 gettimeofday(&tv
,NULL
);
891 if (timercmp(&tv
, &nextspeed
[LR
], <)) {
892 timersub(&nextspeed
[LR
],&tv
,&tv
);
893 speeddelay
=tv
.tv_sec
*1000 + tv
.tv_usec
/1000;
894 if (speeddelay
> 0) {
895 pfd
[0].events
&= ~POLLIN
;
896 if (speeddelay
< delay
|| delay
< 0) delay
=speeddelay
;
901 pfd
[1].events
|= POLLIN
;
905 if (timercmp(&tv
, &nextspeed
[RL
], <)) {
906 gettimeofday(&tv
,NULL
);
907 timersub(&nextspeed
[RL
],&tv
,&tv
);
908 speeddelay
=tv
.tv_sec
*1000 + tv
.tv_usec
/1000;
909 if (speeddelay
> 0) {
910 pfd
[1].events
&= ~POLLIN
;
911 if (speeddelay
< delay
|| delay
< 0) delay
=speeddelay
;
916 n
=poll(pfd
,npfd
,delay
);
917 if (pfd
[0].revents
& POLLHUP
|| (ndirs
>1 && pfd
[1].revents
& POLLHUP
))
919 if (pfd
[0].revents
& POLLIN
) {
922 if (ndirs
>1 && pfd
[1].revents
& POLLIN
) {
925 if (mgmtindex
>= 0 && pfd
[mgmtindex
].revents
!= 0)
926 npfd
=newmgmtconn(pfd
[mgmtindex
].fd
,pfd
,npfd
);
927 if (npfd
> mgmtindex
+1) {
929 for (i
=mgmtindex
+1;i
<npfd
;i
++) {
930 if (pfd
[i
].revents
& POLLHUP
||
931 (pfd
[i
].revents
& POLLIN
&& mgmtcommand(pfd
[i
].fd
) < 0))
932 npfd
=delmgmtconn(i
,pfd
,npfd
);