Changeset 62987bb


Ignore:
Timestamp:
05/02/06 10:55:03 (15 years ago)
Author:
Perry Lorier <perry@…>
Branches:
4.0.1-hotfixes, cachetimestamps, develop, dpdk-ndag, etsilive, getfragoff, help, libtrace4, master, ndag_format, pfring, rc-4.0.1, rc-4.0.2, rc-4.0.3, rc-4.0.4, ringdecrementfix, ringperformance, ringtimestampfixes
Children:
aeee9db
Parents:
9a5b234
Message:

Rewrite the pcap handling to deal with windows issues

File:
1 edited

Legend:

Unmodified
Added
Removed
  • lib/format_pcap.c

    r998bee5 r62987bb  
    259259}
    260260
    261 static void trace_pcap_handler(u_char *user, const struct pcap_pkthdr *pcaphdr, const u_char *pcappkt) {
    262         libtrace_packet_t *packet = (libtrace_packet_t *)user; 
    263         /*
    264         // pcap provides us with the right bits, in it's own buffers.
    265         // We hijack them.
    266         */
    267 
    268         if (!packet->buffer || packet->buf_control==TRACE_CTRL_EXTERNAL) {
    269                 /* We only need struct pcap_pkthdr, but we have no way
    270                  * to say how much we malloc'd so that formats can determine
    271                  * if they need to malloc more, so at the moment we just
    272                  * malloc 64k
    273                  */
    274                 packet->buf_control = TRACE_CTRL_PACKET;
    275                 packet->buffer=malloc(65536);
    276         }
    277         memcpy(packet->buffer,pcaphdr,sizeof(struct pcap_pkthdr));
    278         packet->header = packet->buffer;
    279         packet->payload = (void *)pcappkt;
    280 
    281         assert(pcaphdr->caplen<=65536);
    282 }
    283 
    284 /* TODO: use pcap_next_ex() if available */
     261
    285262static int pcap_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) {
    286         int pcapbytes = 0;
     263        int ret = 0;
    287264        int linktype;
    288265
     
    290267        linktype = pcap_datalink(DATA(libtrace)->input.pcap);
    291268        packet->type = pcap_dlt_to_rt(linktype);
    292        
    293         pcapbytes = pcap_dispatch(INPUT.pcap,
    294                                         1, /* number of packets */
    295                                         &trace_pcap_handler,
    296                                         (u_char *)packet);
    297 
    298         if (pcapbytes <= 0) {
    299                 return pcapbytes;
    300         }
    301         return ((struct pcap_pkthdr*)packet->header)->len+sizeof(struct pcap_pkthdr);
     269
     270        if (packet->buf_control==TRACE_CTRL_EXTERNAL)
     271                free(packet->buffer);
     272
     273        packet->buf_control = TRACE_CTRL_PACKET;
     274
     275        for(;;) {
     276
     277                ret=pcap_next_ex(INPUT.pcap,
     278                                (struct pcap_pkthdr **)&packet->header,
     279                                (const u_char **)&packet->payload);
     280
     281                switch(ret) {
     282                        case 1: break; /* no error */
     283                        case 0: continue; /* timeout expired */
     284                        case -1:
     285                                trace_set_err(libtrace,TRACE_ERR_BAD_PACKET,
     286                                                "%s",pcap_geterr(INPUT.pcap));
     287                                return -1; /* Error */
     288                        case -2:
     289                                return 0; /* EOF */
     290                }
     291
     292                return ((struct pcap_pkthdr*)packet->header)->len
     293                        +sizeof(struct pcap_pkthdr);
     294        }
    302295}
    303296
Note: See TracChangeset for help on using the changeset viewer.