Changeset bf1029a


Ignore:
Timestamp:
02/12/14 16:37:45 (7 years ago)
Author:
Richard Sanger <rjs51@…>
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:
bcb2402
Parents:
8b49230
Message:

Fix issue #395 - Added pcap nanosecond file support based upon Martin Bligh's patch

Changes to original patch as follows:

  • Changed the byteswaps of the magic constants to defines to ensure this is optimised by the compiler
  • Whitespace tidy up
  • Removed unnecessary fprintf already handled by trace_set_err
File:
1 edited

Legend:

Unmodified
Added
Removed
  • lib/format_pcapfile.c

    r11041eb rbf1029a  
    7070                uint32_t snaplen;        /* aka "wirelen" */
    7171                uint32_t network;        /* data link type */
    72 } pcapfile_header_t;
     72} pcapfile_header_t;
     73
     74#define MAGIC1      0xa1b2c3d4  /* Original */
     75#define MAGIC2      0xa1b23c4d  /* Newer nanosecond format */
     76#define MAGIC1_REV  0xd4c3b2a1  /* Reversed byteorder detection */
     77#define MAGIC2_REV  0x4d3cb2a1
     78
     79static inline int header_is_backwards_magic(pcapfile_header_t *header) {
     80        return (header->magic_number == MAGIC1_REV || header->magic_number == MAGIC2_REV);
     81}
     82
     83static inline int header_is_magic(pcapfile_header_t *header) {
     84        return (header->magic_number == MAGIC1 || header->magic_number == MAGIC2 ||
     85                header_is_backwards_magic(header));
     86}
     87
     88static inline int trace_in_nanoseconds(pcapfile_header_t *header) {
     89        return (header->magic_number == MAGIC2 || header->magic_number == MAGIC2_REV);
     90}
    7391
    7492struct pcapfile_format_data_t {
     
    99117        int len;
    100118        len = wandio_peek(io, &header, sizeof(header));
     119
    101120        /* Is this long enough? */
    102121        if (len < (int)sizeof(header)) {
     
    104123        }
    105124        /* Pcap magic? */
    106         if (header.magic_number == 0xa1b2c3d4 || header.magic_number == 0xd4c3b2a1) {
     125        if (header_is_magic(&header)) {
    107126                return 1;
    108127        }
     
    110129        return 0;
    111130}
     131
    112132
    113133static int pcapfile_init_input(libtrace_t *libtrace) {
     
    146166       
    147167        /* We can use the PCAP magic number to determine the byte order */
    148         if (DATA(libtrace)->header.magic_number == 0xd4c3b2a1)
     168        if (header_is_backwards_magic(&(DATA(libtrace)->header)))
    149169                return byteswap16(num);
    150170
     
    161181       
    162182        /* We can use the PCAP magic number to determine the byte order */
    163         if (DATA(libtrace)->header.magic_number == 0xd4c3b2a1)
     183        if (header_is_backwards_magic(&(DATA(libtrace)->header)))
    164184                return byteswap32(num);
    165185
     
    197217                }
    198218               
    199                 if (swapl(libtrace,DATA(libtrace)->header.magic_number) !=
    200                                         0xa1b2c3d4) {
     219                if (!header_is_magic(&(DATA(libtrace)->header))) {
    201220                        trace_set_err(libtrace,TRACE_ERR_INIT_FAILED,
    202221                                        "Not a pcap tracefile (magic=%08x)\n",swapl(libtrace,DATA(libtrace)->header.magic_number));
     
    303322                packet->buf_control = TRACE_CTRL_PACKET;
    304323        } else
    305                 packet->buf_control = TRACE_CTRL_EXTERNAL;
     324                packet->buf_control = TRACE_CTRL_EXTERNAL;
    306325       
    307326       
     
    595614        hdr = (libtrace_pcapfile_pkt_hdr_t*)packet->header;
    596615        ts.tv_sec = swapl(packet->trace,hdr->ts_sec);
    597         ts.tv_usec = swapl(packet->trace,hdr->ts_usec);
     616        if (trace_in_nanoseconds(packet->header))
     617                ts.tv_usec = swapl(packet->trace, hdr->ts_usec) / 1000;
     618        else
     619                ts.tv_usec = swapl(packet->trace,hdr->ts_usec);
     620        return ts;
     621}
     622
     623static struct timespec pcapfile_get_timespec(
     624                const libtrace_packet_t *packet)
     625{
     626        libtrace_pcapfile_pkt_hdr_t *hdr;
     627        pcapfile_header_t *header = &(DATA(packet->trace)->header);
     628        struct timespec ts;
     629       
     630        assert(packet->header);
     631       
     632        hdr = (libtrace_pcapfile_pkt_hdr_t*)packet->header;
     633        ts.tv_sec = swapl(packet->trace,hdr->ts_sec);
     634        if (trace_in_nanoseconds(header))
     635                ts.tv_nsec = swapl(packet->trace, hdr->ts_usec);
     636        else
     637                ts.tv_nsec = swapl(packet->trace, hdr->ts_usec) * 1000;
    598638        return ts;
    599639}
     
    611651static int pcapfile_get_wire_length(const libtrace_packet_t *packet) {
    612652        libtrace_pcapfile_pkt_hdr_t *pcapptr;
    613          
     653
    614654        assert(packet->header);
    615655
     
    717757        NULL,                           /* get_erf_timestamp */
    718758        pcapfile_get_timeval,           /* get_timeval */
    719         NULL,                           /* get_timespec */
     759        pcapfile_get_timespec,          /* get_timespec */
    720760        NULL,                           /* get_seconds */
    721761        NULL,                           /* seek_erf */
Note: See TracChangeset for help on using the changeset viewer.