Changeset 1332

Show
Ignore:
Timestamp:
26/03/08 10:45:36 (10 months ago)
Author:
spa1
Message:
  • Added prepare_packet functions to all formats, primarily to support translating RT packets into the appropriate format. These functions are all used internally as well, as most formats still need to "prepare" packets that have been read by setting pointers, updating loss counters etc.
  • Also added a trace_prepare_packet function, but this is not made available externally at this stage
  • Added init_format_data functions to some formats to initialise format data structures in cases where the init_trace function does more than just that
  • Refactored rt packet reading code to use the new trace_prepare_packet functionality - also did a lot of tidying of the code
  • Added missing RT type for BPF format
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/format_atmhdr.c

    r1303 r1332  
    5656} 
    5757 
     58static int atmhdr_prepare_packet(libtrace_t *libtrace,  
     59                libtrace_packet_t *packet, void *buffer,  
     60                libtrace_rt_types_t rt_type, uint32_t flags) { 
     61 
     62        if (packet->buffer != buffer && 
     63                        packet->buf_control == TRACE_CTRL_PACKET) { 
     64                free(packet->buffer); 
     65        } 
     66 
     67        if ((flags & TRACE_PREP_OWN_BUFFER) == TRACE_PREP_OWN_BUFFER) { 
     68                packet->buf_control = TRACE_CTRL_PACKET; 
     69        } else 
     70                packet->buf_control = TRACE_CTRL_EXTERNAL; 
     71 
     72        packet->buffer = buffer; 
     73        packet->header = buffer; 
     74        packet->payload = (void*)((char*)packet->buffer +  
     75                        libtrace->format->get_framing_length(packet)); 
     76        packet->type = rt_type; 
     77 
     78        if (libtrace->format_data == NULL) { 
     79                if (atmhdr_init_input(libtrace)) 
     80                        return -1; 
     81        } 
     82        return 0; 
     83} 
     84 
    5885static int atmhdr_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { 
    5986        int numbytes; 
    6087        void *buffer; 
    61  
     88        uint32_t flags = 0; 
     89         
    6290        if (!packet->buffer || packet->buf_control == TRACE_CTRL_EXTERNAL) { 
    63                 packet->buf_control = TRACE_CTRL_PACKET; 
    6491                packet->buffer=malloc((size_t)LIBTRACE_PACKET_BUFSIZE); 
    6592        } 
    6693        buffer = packet->buffer; 
    67  
     94        flags |= TRACE_PREP_OWN_BUFFER; 
     95         
    6896        packet->type = TRACE_RT_DATA_ATMHDR; 
    6997 
     
    76104        } 
    77105 
    78         packet->header = packet->buffer; 
    79         packet->payload = (void*)((char*)packet->buffer + 
    80                         libtrace->format->get_framing_length(packet)); 
    81  
     106        if (atmhdr_prepare_packet(libtrace, packet, buffer,  
     107                                TRACE_RT_DATA_ATMHDR, flags)) { 
     108                return -1; 
     109        } 
     110                                 
     111         
    82112        return 12; 
    83113} 
     
    117147        NULL,                           /* fin_output */ 
    118148        atmhdr_read_packet,             /* read_packet */ 
    119         NULL,                           /* fin_packet */ 
     149        atmhdr_prepare_packet,          /* prepare_packet */ 
     150        NULL,                           /* fin_packet */ 
    120151        NULL,                           /* write_packet */ 
    121152        atmhdr_get_link_type,           /* get_link_type */ 
  • trunk/lib/format_bpf.c

    r1321 r1332  
    206206static uint64_t bpf_get_received_packets(libtrace_t *trace) 
    207207{ 
     208        if (trace->format_data == NULL) 
     209                return (uint64_t)-1; 
     210 
     211        if (FORMATIN(trace)->fd == -1) { 
     212                /* Almost certainly a 'dead' trace so there is no socket 
     213                 * for us to query */ 
     214                return (uint64_t) -1; 
     215        } 
    208216        /* If we're called with stats_valid == 0, or we're called again 
    209217         * then refresh the stats.  Don't refresh the stats if we're called 
     
    221229static uint64_t bpf_get_dropped_packets(libtrace_t *trace) 
    222230{ 
     231        if (trace->format_data == NULL) 
     232                return (uint64_t)-1; 
     233 
     234        if (FORMATIN(trace)->fd == -1) { 
     235                /* Almost certainly a 'dead' trace so there is no socket 
     236                 * for us to query */ 
     237                return (uint64_t) -1; 
     238        } 
    223239        /* If we're called with stats_valid == 0, or we're called again 
    224240         * then refresh the stats.  Don't refresh the stats if we're called 
     
    282298} 
    283299 
     300static int bpf_prepare_packet(libtrace_t *libtrace, libtrace_packet_t *packet, 
     301                void *buffer, libtrace_rt_types_t rt_type, uint32_t flags) { 
     302        if (packet->buffer != buffer && 
     303                        packet->buf_control == TRACE_CTRL_PACKET) { 
     304                free(packet->buffer); 
     305        } 
     306 
     307        if ((flags & TRACE_PREP_OWN_BUFFER) == TRACE_PREP_OWN_BUFFER) { 
     308                packet->buf_control = TRACE_CTRL_PACKET; 
     309        } else 
     310                packet->buf_control = TRACE_CTRL_EXTERNAL; 
     311 
     312 
     313        packet->buffer = buffer; 
     314        packet->header = buffer; 
     315        packet->type = rt_type; 
     316 
     317        /* Find the payload */ 
     318        /* TODO: Pcap deals with a padded FDDI linktype here */ 
     319        packet->payload=(char *)buffer + BPFHDR(packet)->bh_hdrlen; 
     320 
     321        if (libtrace->format_data == NULL) { 
     322                if (bpf_init_input(libtrace)) 
     323                        return -1; 
     324        } 
     325 
     326        return 0; 
     327} 
     328         
    284329static int bpf_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet)  
    285330{ 
     331        uint32_t flags = 0; 
     332         
    286333        /* Fill the buffer */ 
    287334        if (FORMATIN(libtrace)->remaining<=0) { 
     
    311358        if (packet->buf_control == TRACE_CTRL_PACKET) 
    312359                free(packet->buffer); 
    313         packet->buf_control = TRACE_CTRL_EXTERNAL; 
    314  
    315         /* Find the bpf header */ 
    316         packet->header=FORMATIN(libtrace)->bufptr; 
    317  
    318         /* Find the payload */ 
    319         /* TODO: Pcap deals with a padded FDDI linktype here */ 
    320         packet->payload=FORMATIN(libtrace)->bufptr+BPFHDR(packet)->bh_hdrlen; 
     360 
     361        if (bpf_prepare_packet(libtrace, packet, FORMATIN(libtrace)->bufptr, 
     362                TRACE_RT_DATA_BPF, flags)) { 
     363                return -1; 
     364        } 
     365         
    321366 
    322367        /* Now deal with any padding */ 
     
    389434        NULL,                   /* fin_output */ 
    390435        bpf_read_packet,        /* read_packet */ 
     436        bpf_prepare_packet,     /* prepare_packet */ 
    391437        NULL,                   /* fin_packet */ 
    392438        NULL,                   /* write_packet */ 
  • trunk/lib/format_dag24.c

    r1328 r1332  
    8383}; 
    8484 
     85static void dag_init_format_data(libtrace_t *libtrace) { 
     86        libtrace->format_data = (struct dag_format_data_t *) 
     87                malloc(sizeof(struct dag_format_data_t)); 
     88 
     89        DUCK.last_duck = 0; 
     90        DUCK.duck_freq = 0; 
     91        DUCK.last_pkt = 0; 
     92        DUCK.dummy_duck = NULL; 
     93        FORMAT_DATA->drops = 0; 
     94        FORMAT_DATA->top = 0; 
     95        FORMAT_DATA->bottom = 0; 
     96        FORMAT_DATA->buf = NULL; 
     97        FORMAT_DATA->fd = -1; 
     98        FORMAT_DATA->offset = 0; 
     99        FORMAT_DATA->diff = 0; 
     100} 
     101 
    85102static int dag_available(libtrace_t *libtrace) { 
    86103 
     
    120137        } 
    121138         
    122         libtrace->format_data = (struct dag_format_data_t *) 
    123                 malloc(sizeof(struct dag_format_data_t)); 
    124         FORMAT_DATA->top = 0; 
    125         FORMAT_DATA->bottom = 0; 
     139        dag_init_format_data(libtrace); 
    126140        if (S_ISCHR(buf.st_mode)) { 
    127141                /* DEVICE */ 
     
    145159        } 
    146160 
    147         DUCK.last_duck = 0; 
    148         DUCK.duck_freq = 0; 
    149         DUCK.last_pkt = 0; 
    150         DUCK.dummy_duck = NULL; 
    151         FORMAT_DATA->drops = 0; 
    152161        free(dag_dev_name); 
    153162 
     
    259268} 
    260269 
    261 static void dag_form_packet(dag_record_t *erfptr, libtrace_packet_t *packet) { 
     270static int dag_prepare_packet(libtrace_t *libtrace, libtrace_packet_t *packet, 
     271                void *buffer, libtrace_rt_types_t rt_type, uint32_t flags) { 
     272 
     273        dag_record_t *erfptr; 
     274        if (packet->buffer != buffer && 
     275                        packet->buf_control == TRACE_CTRL_PACKET) { 
     276                free(packet->buffer); 
     277        } 
     278 
     279        if ((flags & TRACE_PREP_OWN_BUFFER) == TRACE_PREP_OWN_BUFFER) { 
     280                packet->buf_control = TRACE_CTRL_PACKET; 
     281        } else  
     282                packet->buf_control = TRACE_CTRL_EXTERNAL; 
     283         
     284        erfptr = (dag_record_t *)packet->buffer; 
    262285        packet->buffer = erfptr; 
    263286        packet->header = erfptr; 
    264         packet->type = TRACE_RT_DATA_ERF; 
     287        packet->type = rt_type; 
     288 
    265289        if (erfptr->flags.rxerror == 1) { 
    266290                /* rxerror means the payload is corrupt - drop it 
     
    273297        } 
    274298 
     299        if (libtrace->format_data == NULL) { 
     300                dag_init_format_data(libtrace); 
     301        } 
     302 
     303        DATA(libtrace)->drops += ntohs(erfptr->lctr); 
     304 
     305        return 0; 
     306 
    275307} 
    276308 
     
    278310        int numbytes; 
    279311        int size = 0; 
    280         struct timeval tv; 
     312        uint32_t flags = 0; 
     313        struct timeval tv; 
    281314        dag_record_t *erfptr = NULL; 
    282315 
     
    307340                erfptr = dag_get_record(libtrace); 
    308341        } while (erfptr == NULL); 
    309         dag_form_packet(erfptr, packet); 
    310         tv = trace_get_timeval(packet); 
     342         
     343         
     344        if (dag_prepare_packet(libtrace, packet, erfptr, TRACE_RT_DATA_ERF,  
     345                                flags)) 
     346                return -1; 
     347        tv = trace_get_timeval(packet); 
    311348        DUCK.last_pkt = tv.tv_sec; 
    312         DATA(libtrace)->drops += ntohs(erfptr->lctr); 
    313349        return packet->payload ? htons(erfptr->rlen) : erf_get_framing_length(packet); 
    314350} 
     
    348384static uint64_t dag_get_dropped_packets(libtrace_t *trace) 
    349385{ 
     386        if (!trace->format_data) 
     387                return (uint64_t)-1; 
    350388        return DATA(trace)->drops; 
    351389} 
     
    377415        NULL,                           /* fin_output */ 
    378416        dag_read_packet,                /* read_packet */ 
    379         NULL,                           /* fin_packet */ 
     417        dag_prepare_packet,             /* prepare_packet */ 
     418        NULL,                           /* fin_packet */ 
    380419        NULL,                           /* write_packet */ 
    381420        erf_get_link_type,              /* get_link_type */ 
  • trunk/lib/format_dag25.c

    r1330 r1332  
    9999struct dag_dev_t *open_dags = NULL; 
    100100 
     101static void dag_init_format_data(libtrace_t *libtrace) { 
     102        libtrace->format_data = (struct dag_format_data_t *) 
     103                malloc(sizeof(struct dag_format_data_t)); 
     104        DUCK.last_duck = 0; 
     105        DUCK.duck_freq = 0; 
     106        DUCK.last_pkt = 0; 
     107        DUCK.dummy_duck = NULL; 
     108        FORMAT_DATA->stream_attached = 0; 
     109        FORMAT_DATA->drops = 0; 
     110        FORMAT_DATA->device = NULL; 
     111        FORMAT_DATA->dagstream = 0; 
     112        FORMAT_DATA->processed = 0; 
     113        FORMAT_DATA->bottom = NULL; 
     114        FORMAT_DATA->top = NULL; 
     115} 
     116 
    101117/* NOTE: This function assumes the open_dag_mutex is held by the caller */ 
    102118static struct dag_dev_t *dag_find_open_device(char *dev_name) { 
     
    187203        struct dag_dev_t *dag_device = NULL; 
    188204         
     205        dag_init_format_data(libtrace); 
    189206        pthread_mutex_lock(&open_dag_mutex); 
    190207        if ((scan = strchr(libtrace->uridata,',')) == NULL) { 
     
    197214         
    198215         
    199         libtrace->format_data = (struct dag_format_data_t *) 
    200                 malloc(sizeof(struct dag_format_data_t)); 
    201216 
    202217        /* For now, we don't offer the ability to select the stream */ 
     
    219234        } 
    220235 
    221  
    222  
    223         DUCK.last_duck = 0; 
    224         DUCK.duck_freq = 0; 
    225         DUCK.last_pkt = 0; 
    226         DUCK.dummy_duck = NULL; 
    227236        FORMAT_DATA->device = dag_device; 
    228         FORMAT_DATA->stream_attached = 0; 
    229         FORMAT_DATA->drops = 0; 
    230237         
    231238        pthread_mutex_unlock(&open_dag_mutex); 
     
    402409} 
    403410 
    404 static void dag_form_packet(dag_record_t *erfptr, libtrace_packet_t *packet) { 
    405         packet->buffer = erfptr; 
     411static int dag_prepare_packet(libtrace_t *libtrace, libtrace_packet_t *packet, 
     412                void *buffer, libtrace_rt_types_t rt_type, uint32_t flags) { 
     413         
     414        dag_record_t *erfptr; 
     415         
     416        if (packet->buffer != buffer &&  
     417                        packet->buf_control == TRACE_CTRL_PACKET) { 
     418                free(packet->buffer); 
     419        } 
     420         
     421        if ((flags & TRACE_PREP_OWN_BUFFER) == TRACE_PREP_OWN_BUFFER) { 
     422                packet->buf_control = TRACE_CTRL_PACKET; 
     423        } else 
     424                packet->buf_control = TRACE_CTRL_EXTERNAL; 
     425         
     426        erfptr = (dag_record_t *)packet->buffer; 
     427        packet->buffer = erfptr; 
    406428        packet->header = erfptr; 
    407         packet->type = TRACE_RT_DATA_ERF; 
    408         if (erfptr->flags.rxerror == 1) { 
     429        packet->type = rt_type; 
     430         
     431        if (erfptr->flags.rxerror == 1) { 
    409432                /* rxerror means the payload is corrupt - drop it 
    410433                 * by tweaking rlen */ 
     
    415438                        + erf_get_framing_length(packet); 
    416439        } 
    417  
     440         
     441        if (libtrace->format_data == NULL) { 
     442                dag_init_format_data(libtrace);  
     443        } 
     444         
     445        /* No loss counter for DSM coloured records - have to use  
     446         * some other API */ 
     447        if (erfptr->type == TYPE_DSM_COLOR_ETH) { 
     448                 
     449        } else { 
     450                DATA(libtrace)->drops += ntohs(erfptr->lctr); 
     451        } 
     452 
     453        return 0; 
    418454} 
    419455 
     
    424460        dag_record_t *erfptr = NULL; 
    425461        int numbytes = 0; 
     462        uint32_t flags = 0; 
    426463         
    427464        if (DUCK.last_pkt - DUCK.last_duck > DUCK.duck_freq && 
     
    437474 
    438475        if (packet->buf_control == TRACE_CTRL_PACKET) { 
    439                 packet->buf_control = TRACE_CTRL_EXTERNAL; 
    440476                free(packet->buffer); 
    441477                packet->buffer = 0; 
     
    452488        } while (erfptr == NULL); 
    453489 
    454         dag_form_packet(erfptr, packet); 
     490        //dag_form_packet(erfptr, packet); 
     491        if (dag_prepare_packet(libtrace, packet, erfptr, TRACE_RT_DATA_ERF, 
     492                                flags)) 
     493                return -1; 
    455494        tv = trace_get_timeval(packet); 
    456495        DUCK.last_pkt = tv.tv_sec; 
    457496         
    458         /* No loss counter for DSM coloured records - have to use  
    459          * some other API */ 
    460         if (erfptr->type == TYPE_DSM_COLOR_ETH) { 
    461                  
    462         } else { 
    463                 DATA(libtrace)->drops += ntohs(erfptr->lctr); 
    464         } 
    465497        return packet->payload ? htons(erfptr->rlen) :  
    466498                                erf_get_framing_length(packet); 
     
    472504        dag_record_t *erfptr = NULL; 
    473505        int numbytes; 
     506        uint32_t flags = 0; 
    474507         
    475508        /* Need to call dag_available so that the top pointer will get 
     
    487520                return event; 
    488521        } 
    489         dag_form_packet(erfptr, packet); 
     522        //dag_form_packet(erfptr, packet); 
     523        if (dag_prepare_packet(trace, packet, erfptr, TRACE_RT_DATA_ERF,  
     524                                flags)) { 
     525                event.type = TRACE_EVENT_TERMINATE; 
     526                return event; 
     527        } 
     528                 
     529                 
    490530        event.size = trace_get_capture_length(packet) + trace_get_framing_length(packet); 
    491531        if (trace->filter) { 
     
    509549 
    510550static uint64_t dag_get_dropped_packets(libtrace_t *trace) { 
     551        if (trace->format_data == NULL) 
     552                return (uint64_t)-1; 
    511553        return DATA(trace)->drops; 
    512554} 
     
    538580        NULL,                           /* fin_output */ 
    539581        dag_read_packet,                /* read_packet */ 
    540         NULL,                           /* fin_packet */ 
     582        dag_prepare_packet,             /* prepare_packet */ 
     583        NULL,                           /* fin_packet */ 
    541584        NULL,                           /* write_packet */ 
    542585        erf_get_link_type,              /* get_link_type */ 
  • trunk/lib/format_duck.c

    r1319 r1332  
    133133} 
    134134 
     135static int duck_prepare_packet(libtrace_t *libtrace, libtrace_packet_t *packet, 
     136                void *buffer, libtrace_rt_types_t rt_type, uint32_t flags) { 
     137 
     138        if (packet->buffer != buffer && 
     139                        packet->buf_control == TRACE_CTRL_PACKET) { 
     140                free(packet->buffer); 
     141        } 
     142 
     143        if ((flags & TRACE_PREP_OWN_BUFFER) == TRACE_PREP_OWN_BUFFER) { 
     144                packet->buf_control = TRACE_CTRL_PACKET; 
     145        } else 
     146                packet->buf_control = TRACE_CTRL_EXTERNAL; 
     147 
     148 
     149        packet->buffer = buffer; 
     150        packet->header = NULL; 
     151        packet->payload = buffer; 
     152        packet->type = rt_type; 
     153 
     154        if (libtrace->format_data == NULL) { 
     155                if (duck_init_input(libtrace)) 
     156                        return -1; 
     157        } 
     158 
     159        return 0; 
     160} 
     161 
    135162static int duck_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { 
    136163 
     
    138165        uint32_t version = 0; 
    139166        unsigned int duck_size; 
     167        uint32_t flags = 0; 
    140168         
    141169        if (!packet->buffer || packet->buf_control == TRACE_CTRL_EXTERNAL) { 
    142170                packet->buffer = malloc((size_t)LIBTRACE_PACKET_BUFSIZE); 
    143                 packet->buf_control = TRACE_CTRL_PACKET; 
    144171                if (!packet->buffer) { 
    145172                        trace_set_err(libtrace, errno, 
     
    149176        } 
    150177 
     178        flags |= TRACE_PREP_OWN_BUFFER; 
     179         
    151180        if (INPUT->dag_version == 0) { 
    152181                /* Read in the duck version from the start of the trace */ 
     
    164193         
    165194 
    166         packet->header = 0; 
    167         packet->payload = packet->buffer; 
    168          
    169195        if (INPUT->dag_version == TRACE_RT_DUCK_2_4) { 
    170196                duck_size = sizeof(duck2_4_t); 
     
    180206        } 
    181207 
    182         if ((numbytes = libtrace_io_read(INPUT->file, packet->payload
     208        if ((numbytes = libtrace_io_read(INPUT->file, packet->buffer
    183209                                        (size_t)duck_size)) != (int)duck_size) { 
    184210                if (numbytes == -1) { 
     
    194220        } 
    195221 
     222        if (duck_prepare_packet(libtrace, packet, packet->buffer, packet->type, 
     223                                flags))  
     224                return -1; 
     225         
    196226        return numbytes; 
    197227} 
     
    284314        duck_fin_output,                /* fin_output */ 
    285315        duck_read_packet,               /* read_packet */ 
    286         NULL,                           /* fin_packet */ 
     316        duck_prepare_packet,            /* prepare_packet */ 
     317        NULL,                           /* fin_packet */ 
    287318        duck_write_packet,              /* write_packet */ 
    288319        duck_get_link_type,             /* get_link_type */ 
  • trunk/lib/format_erf.c

    r1331 r1332  
    342342} 
    343343  
     344static int erf_prepare_packet(libtrace_t *libtrace, libtrace_packet_t *packet, 
     345                void *buffer, libtrace_rt_types_t rt_type, uint32_t flags) { 
     346         
     347        dag_record_t *erfptr; 
     348         
     349        if (packet->buffer != buffer &&  
     350                packet->buf_control == TRACE_CTRL_PACKET) { 
     351                free(packet->buffer); 
     352        } 
     353 
     354        if ((flags & TRACE_PREP_OWN_BUFFER) == TRACE_PREP_OWN_BUFFER) { 
     355                packet->buf_control = TRACE_CTRL_PACKET; 
     356        } else 
     357                packet->buf_control = TRACE_CTRL_EXTERNAL; 
     358         
     359         
     360        packet->type = rt_type; 
     361        packet->buffer = buffer; 
     362        packet->header = buffer; 
     363        erfptr = (dag_record_t *)packet->buffer; 
     364        if (erfptr->flags.rxerror == 1) { 
     365                packet->payload = NULL; 
     366                /* XXX: Do we want to do this? We never used to do this 
     367                  
     368                erfptr->rlen = htons(erf_get_framing_length(packet)); 
     369                */ 
     370        } else { 
     371                packet->payload = (char*)packet->buffer + erf_get_framing_length(packet); 
     372        } 
     373         
     374        if (libtrace->format_data == NULL) { 
     375                /* Allocates the format_data structure */ 
     376                if (erf_init_input(libtrace))  
     377                        return -1; 
     378        } 
     379 
     380        /* Check for loss */ 
     381        if (erfptr->type == TYPE_DSM_COLOR_ETH) { 
     382                /* No idea how we get this yet */ 
     383 
     384        } else { 
     385                DATA(libtrace)->drops += ntohs(erfptr->lctr); 
     386        } 
     387 
     388        return 0; 
     389} 
    344390 
    345391static int erf_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { 
     
    348394        void *buffer2 = packet->buffer; 
    349395        unsigned int rlen; 
    350  
     396        uint32_t flags = 0; 
     397         
     398         
    351399        if (!packet->buffer || packet->buf_control == TRACE_CTRL_EXTERNAL) { 
    352400                packet->buffer = malloc((size_t)LIBTRACE_PACKET_BUFSIZE); 
    353                 packet->buf_control = TRACE_CTRL_PACKET; 
    354401                if (!packet->buffer) { 
    355402                        trace_set_err(libtrace, errno,  
     
    359406        } 
    360407 
    361          
    362          
    363         packet->header = packet->buffer; 
    364         packet->type = TRACE_RT_DATA_ERF; 
    365  
     408        flags |= TRACE_PREP_OWN_BUFFER;  
     409         
    366410        if ((numbytes=libtrace_io_read(INPUT.file, 
    367411                                        packet->buffer, 
     
    403447                return -1; 
    404448        } 
    405         if (((dag_record_t *)packet->buffer)->flags.rxerror == 1) { 
    406                 packet->payload = NULL; 
    407         } else { 
    408                 packet->payload = (char*)packet->buffer + erf_get_framing_length(packet); 
    409         } 
     449         
     450        if (erf_prepare_packet(libtrace, packet, packet->buffer, TRACE_RT_DATA_ERF, flags)) 
     451                return -1; 
     452         
    410453        return rlen; 
    411454} 
     
    637680static uint64_t erf_get_dropped_packets(libtrace_t *trace) 
    638681{ 
     682        if (trace->format_data == NULL) 
     683                return (uint64_t)-1; 
    639684        return DATA(trace)->drops; 
    640685} 
     
    678723        erf_fin_output,                 /* fin_output */ 
    679724        erf_read_packet,                /* read_packet */ 
     725        erf_prepare_packet,             /* prepare_packet */ 
    680726        NULL,                           /* fin_packet */ 
    681727        erf_write_packet,               /* write_packet */ 
  • trunk/lib/format_legacy.c

    r1319 r1332  
    6969}; 
    7070 
     71static void legacy_init_format_data(libtrace_t *libtrace) { 
     72        libtrace->format_data = malloc(sizeof(struct legacy_format_data_t)); 
     73         
     74        DATA(libtrace)->input.file = NULL; 
     75        DATA(libtrace)->ts_high = 0; 
     76        DATA(libtrace)->ts_old = 0; 
     77        DATA(libtrace)->starttime = 0; 
     78} 
     79 
    7180static int legacyeth_get_framing_length(const libtrace_packet_t *packet UNUSED)  
    7281{ 
     
    91100static int erf_init_input(libtrace_t *libtrace)  
    92101{ 
    93         libtrace->format_data = malloc(sizeof(struct legacy_format_data_t)); 
    94  
    95         DATA(libtrace)->input.file = NULL; 
     102        legacy_init_format_data(libtrace); 
    96103 
    97104        return 0; 
     
    136143        regmatch_t match; 
    137144 
    138         libtrace->format_data = malloc(sizeof(struct legacy_format_data_t)); 
    139  
    140         DATA(libtrace)->input.file = NULL; 
    141          
     145 
     146        legacy_init_format_data(libtrace);       
    142147        if((retval = regcomp(&reg, "[0-9]{8}-[0-9]{6}", REG_EXTENDED)) != 0) { 
    143148                trace_set_err(libtrace, errno, "Failed to compile regex"); 
     
    149154        } 
    150155        DATA(libtrace)->starttime = trtime(&filename[match.rm_so]); 
    151         DATA(libtrace)->ts_high = 0; 
    152         DATA(libtrace)->ts_old = 0; 
    153156        return 0; 
    154157} 
     
    173176} 
    174177 
     178static int legacy_prepare_packet(libtrace_t *libtrace,  
     179                libtrace_packet_t *packet, void *buffer,  
     180                libtrace_rt_types_t rt_type, uint32_t flags) { 
     181 
     182        if (packet->buffer != buffer && 
     183                        packet->buf_control == TRACE_CTRL_PACKET) { 
     184                free(packet->buffer); 
     185        } 
     186 
     187        if ((flags & TRACE_PREP_OWN_BUFFER) == TRACE_PREP_OWN_BUFFER) { 
     188                packet->buf_control = TRACE_CTRL_PACKET; 
     189        } else 
     190                packet->buf_control = TRACE_CTRL_EXTERNAL; 
     191 
     192 
     193        packet->buffer = buffer; 
     194        packet->header = buffer; 
     195        packet->type = rt_type; 
     196        packet->payload = (void*)((char*)packet->buffer +  
     197                libtrace->format->get_framing_length(packet)); 
     198 
     199 
     200        if (libtrace->format_data == NULL) { 
     201                legacy_init_format_data(libtrace); 
     202        } 
     203        return 0; 
     204} 
     205 
    175206static int legacy_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { 
    176207        int numbytes; 
    177208        void *buffer; 
    178  
     209        uint32_t flags = 0; 
     210         
    179211        if (!packet->buffer || packet->buf_control == TRACE_CTRL_EXTERNAL) { 
    180                 packet->buf_control = TRACE_CTRL_PACKET; 
    181212                packet->buffer=malloc((size_t)LIBTRACE_PACKET_BUFSIZE); 
    182213        } 
     214        flags |= TRACE_PREP_OWN_BUFFER; 
    183215        buffer = packet->buffer; 
    184216 
     
    215247        } 
    216248         
    217         packet->header = packet->buffer; 
    218         packet->payload = (void*)((char*)packet->buffer +  
    219                 libtrace->format->get_framing_length(packet)); 
     249        if (legacy_prepare_packet(libtrace, packet, packet->buffer,  
     250                                packet->type, flags)) { 
     251                return -1; 
     252        } 
    220253         
    221254        return 64; 
     
    231264        void *buffer; 
    232265        char *data_ptr; 
     266        uint32_t flags = 0; 
    233267         
    234268        if (!packet->buffer || packet->buf_control == TRACE_CTRL_EXTERNAL) { 
    235                 packet->buf_control = TRACE_CTRL_PACKET; 
    236269                packet->buffer=malloc((size_t)LIBTRACE_PACKET_BUFSIZE); 
    237270        } 
     271        flags |= TRACE_PREP_OWN_BUFFER; 
     272         
    238273        buffer = packet->buffer; 
    239  
    240274        packet->type = TRACE_RT_DATA_LEGACY_NZIX; 
    241275         
     
    263297        memmove(data_ptr + 2, data_ptr, 26); 
    264298 
    265         packet->header = packet->buffer; 
    266         packet->payload = (void*)((char*)packet->buffer + 
    267                         libtrace->format->get_framing_length(packet)); 
     299        if (legacy_prepare_packet(libtrace, packet, packet->buffer,  
     300                                packet->type, flags)) { 
     301                return -1; 
     302        } 
    268303        return 68; 
    269304} 
     
    432467        NULL,                           /* fin_output */ 
    433468        legacy_read_packet,             /* read_packet */ 
     469        legacy_prepare_packet,          /* prepare_packet */ 
    434470        NULL,                           /* fin_packet */ 
    435471        NULL,                           /* write_packet */ 
     
    471507        NULL,                           /* fin_output */ 
    472508        legacy_read_packet,             /* read_packet */ 
     509        legacy_prepare_packet,          /* prepare_packet */ 
    473510        NULL,                           /* fin_packet */ 
    474511        NULL,                           /* write_packet */ 
     
    510547        NULL,                           /* fin_output */ 
    511548        legacy_read_packet,             /* read_packet */ 
     549        legacy_prepare_packet,          /* prepare_packet */ 
    512550        NULL,                           /* fin_packet */ 
    513551        NULL,                           /* write_packet */ 
     
    549587        NULL,                           /* fin_output */ 
    550588        legacynzix_read_packet,         /* read_packet */ 
     589        legacy_prepare_packet,          /* prepare_packet */ 
    551590        NULL,                           /* fin_packet */ 
    552591        NULL,                           /* write_packet */ 
  • trunk/lib/format_linux.c

    r1321 r1332  
    331331} 
    332332 
     333static int linuxnative_prepare_packet(libtrace_t *libtrace,  
     334                libtrace_packet_t *packet, void *buffer,  
     335                libtrace_rt_types_t rt_type, uint32_t flags) { 
     336 
     337        if (packet->buffer != buffer && 
     338                        packet->buf_control == TRACE_CTRL_PACKET) { 
     339                free(packet->buffer); 
     340        } 
     341 
     342        if ((flags & TRACE_PREP_OWN_BUFFER) == TRACE_PREP_OWN_BUFFER) { 
     343                packet->buf_control = TRACE_CTRL_PACKET; 
     344        } else 
     345                packet->buf_control = TRACE_CTRL_EXTERNAL; 
     346 
     347 
     348        packet->buffer = buffer; 
     349        packet->header = buffer; 
     350        packet->payload = (char *)buffer +  
     351                sizeof(struct libtrace_linuxnative_header); 
     352        packet->type = rt_type; 
     353 
     354        if (libtrace->format_data == NULL) { 
     355                if (linuxnative_init_input(libtrace)) 
     356                        return -1; 
     357        } 
     358        return 0; 
     359         
     360} 
     361 
    333362#define LIBTRACE_MIN(a,b) ((a)<(b) ? (a) : (b)) 
    334363 
     
    344373        socklen_t socklen; 
    345374        int snaplen; 
    346  
     375        uint32_t flags = 0; 
     376         
    347377        if (!packet->buffer || packet->buf_control == TRACE_CTRL_EXTERNAL) { 
    348378                packet->buffer = malloc((size_t)LIBTRACE_PACKET_BUFSIZE); 
    349                packet->buf_control = TRACE_CTRL_PACKET; 
    350         } 
    351  
    352         packet->header = packet->buffer; 
     379        } 
     380 
     381        flags |= TRACE_PREP_OWN_BUFFER; 
     382         
    353383        packet->type = TRACE_RT_DATA_LINUX_NATIVE; 
    354         packet->payload = (char*)packet->buffer+sizeof(*hdr); 
    355384 
    356385        hdr=(struct libtrace_linuxnative_header*)packet->buffer; 
     
    398427                perror("ioctl(SIOCGSTAMP)"); 
    399428 
     429        if (linuxnative_prepare_packet(libtrace, packet, packet->buffer, 
     430                                packet->type, flags)) 
     431                return -1; 
     432         
    400433        return hdr->wirelen+sizeof(*hdr); 
    401434} 
     
    490523 
    491524static int linuxnative_get_fd(const libtrace_t *trace) { 
     525        if (trace->format_data == NULL) 
     526                return -1; 
    492527        return FORMAT(trace->format_data)->fd; 
    493528} 
     
    506541/* Number of packets that past filtering */ 
    507542static uint64_t linuxnative_get_captured_packets(libtrace_t *trace) { 
     543        if (trace->format_data == NULL) 
     544                return UINT64_MAX; 
     545        if (FORMAT(trace->format_data)->fd == -1) { 
     546                /* This is probably a 'dead' trace so obviously we can't query 
     547                 * the socket for capture counts, can we? */ 
     548                return UINT64_MAX; 
     549        } 
     550         
    508551        if ((FORMAT(trace->format_data)->stats_valid & 1)  
    509552                        || FORMAT(trace->format_data)->stats_valid == 0) { 
     
    524567 */ 
    525568static uint64_t linuxnative_get_dropped_packets(libtrace_t *trace) { 
     569        if (trace->format_data == NULL) 
     570                return UINT64_MAX; 
     571        if (FORMAT(trace->format_data)->fd == -1) { 
     572                /* This is probably