tcpdump mailing list archives

parent-child process, selectable file descriptor and pcap


From: wen lui <esolvepolito () gmail com>
Date: Thu, 18 Apr 2013 04:10:55 +0200

I have a program, part of the source codes are:

        handle = pcap_open_live(dev, BUFSIZ, 0, 0, errbuf);
        pcap_compile(handle, &fp, filter_exp, 0, mask) == -1
        pcap_setfilter(handle, &fp);
        struct pcap_pkthdr pcap_header;      // The header that pcap gives
us
        const u_char *pcap_packet;           // The actual packet

        while(1){

          n=fork();
          if(n==0) { // child process
                fd_set rdfds;
                int pcap_fd = pcap_get_selectable_fd(pcap_handler);
                for(;;){
                        FD_ZERO(&rdfds);
                        FD_SET(pcap_fd, &rdfds);
                        FD_SET(sd_proxy, &rdfds);   // here is another fd

                        select(pcap_fd>sd_proxy?pcap_fd+1:sd_proxy+1,
&rdfds, NULL, NULL, NULL)

                        if(FD_ISSET(pcap_fd, &rdfds)) {
                                pcap_packet = pcap_next(pcap_handler,
&pcap_header);
                                if(pcap_packet !=NULL)
                                        printf("capture one ACK packet with
length of %d\n", pcap_header.len);

                                printf("pcap_packet size is %d, length is
%d\n", sizeof(pcap_packet), pcap_header.len);

                                n = send(sd_proxy,
pcap_packet+ETHERNET_HDR_LEN, pcap_header.len-ETHERNET_HDR_LEN, 0);
                                if(n<0){
                                        printf("send error: %s\n",
strerror(errno));
                                        goto END;
                                } //if

                             ....//blabla
                        }//if
                }//for
             // the child process will always exit the for loop after some
time
             close(pcap_fd);
             }//if

          // parent process

    }//while


for the first fork(), I mean for the first child process, it works fine
but for the second and following fork(), I always get "send error: Bad
address"
then I notice that the pcap_header.len is 0
what is the reason for this?


I think the problem may be:

1)   there are parent-child process relationship

2)   in each child process I used

              int pcap_fd = pcap_get_selectable_fd(pcap_handler);
              ....
              close(pcap_fd);


can anyone give some suggestions on this? thanks!
_______________________________________________
tcpdump-workers mailing list
tcpdump-workers () lists tcpdump org
https://lists.sandelman.ca/mailman/listinfo/tcpdump-workers


Current thread: