libpcap 사용 시 문제점.

twins99의 이미지

..
..
..
libpcap을 사용 중입니다.

궁금한 것이 pcap_dispatch()의 return값이 들쑥 날쑥하게 0으로 나왔다 1로 나왔다 합니다.

어떨때는 쭉~ 0으로 나오면서 system이 거의 hangup되는 수준까지 가네요.(control+C가 먹질 않을정도..)

무엇이 문제일까요?

제가 TEST중인 코드는 다음과 같습니다.

static void ping_recv(pcap_t *pcap,u_int32_t packetwait, pcap_handler func)
{
       struct timeval tv,tv2;
       char done = 0;
#ifndef WIN32
       fd_set fds;
#endif
 
 
       if(verbose>3) {
               printf("arping: receiving packets...\n");
       }
 
        tv.tv_sec = packetwait / 1000000;
        tv.tv_usec = packetwait % 1000000;
int count = 0;
 
        for (;!done;) {
                int sr;
                FD_ZERO(&fds);
                FD_SET(pcap_fileno(pcap), &fds);
 
                if (-1 == gettimeofday(&tv2,NULL)) {
                        fprintf(stderr, "arping: "
                                "gettimeofday(): %s\n",
                                strerror(errno));
                        sigint(0);
                }
//              printf("running select()\n");
 
                switch(sr = select(pcap_fileno(pcap)+1,
                                   &fds,
                                   NULL,NULL,&tv)) {
                case -1:
                        if (errno == EINTR) {
                                return;
                        }
                        fprintf(stderr, "arping: select(): "
                                "%s\n", strerror(errno));
                        sigint(0);
                case 0:
                        printf("select DONE:%d\n",__LINE__);
                        done = 1;
                        break;
                default: {
 
                        int ret;
                        if (1 != (ret = pcap_dispatch(pcap, -1,
                                                      func,
                                                      NULL))) {
                                // rest, so we don't take 100% CPU... mostly
                                // hmm... does usleep() exist everywhere?
                                printf("usleep pcap_dispatch:%d\n", ret);
                                usleep(10);
#ifndef HAVE_WEIRD_BSD
                                // weird is normal on bsd :)
                /*              if (verbose) {
                                        fprintf(stderr, "arping: select=%d "
                                                "pcap_dispatch=%d!\n",
                                                sr, ret);
                                }*/
#endif
                        }
                        if(count > 100){
                                done = 1;
                                count =0;
                        }
                        else
                                count++;
                        break; }
                }
 
                if (-1 == gettimeofday(&tv,NULL)) {
                        fprintf(stderr, "arping: "
                                "gettimeofday(): %s\n",
                                strerror(errno));
                        sigint(0);
                }
                /*
                 * setup next timeval, not very exact
                 */
                tv.tv_sec  = (packetwait / 1000000)
                        - (tv.tv_sec - tv2.tv_sec);
                tv.tv_usec = (packetwait % 1000000)
                        - (tv.tv_usec - tv2.tv_usec);
                while (tv.tv_usec < 0) {
                        tv.tv_sec--;
                        tv.tv_usec += 1000000;
                }
                if (tv.tv_sec < 0) {
                        tv.tv_sec = tv.tv_usec = 0;
                }
 
 
printf("FUNC:%s, LINE:%d\n", __FUNCTION__, __LINE__);
                usleep(10000);
 
        }
//      if (tv.tv_usec == 0) {
//              tv.tv_usec = 1;
//      }
        if (-1 == select(0, NULL,NULL,NULL, &tv)) {
                if (errno == EINTR) {
                        return;
                }
                fprintf(stderr, "arping: select(delay): %s\n",strerror(errno));
                sigint(0);
        }
}