Changeset 1243


Ignore:
Timestamp:
07/03/07 13:34:06 (6 years ago)
Author:
spa1
Message:
  • Added a new format for reading NZIX-I traces, called legacynzix
Location:
trunk/lib
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/daglegacy.h

    r1242 r1243  
    2222        uint32_t ts_sec; 
    2323} PACKED atmhdr_t; 
     24 
     25typedef struct legacy_nzix { 
     26        uint32_t ts; 
     27        uint32_t crc; 
     28        uint32_t len; 
     29        /* The padding has actually been placed in the middle of the IP 
     30         * header - when we read in the packet, we will move various bits 
     31         * of the packet around until the padding ends up here and the  
     32         * IP header is undivided */ 
     33        uint8_t pad[2];          
     34} PACKED legacy_nzix_t; 
    2435#endif 
  • trunk/lib/format_legacy.c

    r1242 r1243  
    4343#include <string.h> 
    4444#include <stdlib.h> 
     45#include <regex.h> 
    4546 
    4647#ifdef WIN32 
     
    5657 
    5758#define DATA(x) ((struct legacy_format_data_t *)x->format_data) 
    58  
    5959#define INPUT DATA(libtrace)->input 
    6060 
     
    6464                libtrace_io_t *file; 
    6565        } input; 
     66        time_t starttime;       /* Used for legacy_nzix */ 
     67        uint64_t ts_high;       /* Used for legacy_nzix */ 
     68        uint32_t ts_old;        /* Used for legacy_nzix */ 
    6669}; 
    6770 
     
    8184} 
    8285 
     86static int legacynzix_get_framing_length(const libtrace_packet_t *packet UNUSED) 
     87{ 
     88        return sizeof(legacy_nzix_t); 
     89} 
     90 
    8391static int erf_init_input(libtrace_t *libtrace)  
    8492{ 
     
    8795        DATA(libtrace)->input.file = NULL; 
    8896 
     97        return 0; 
     98} 
     99 
     100static time_t trtime(char *s) { 
     101        /* XXX: this function may not be particularly portable to 
     102         * other platforms, e.g. *BSDs, Windows */ 
     103        struct tm tm; 
     104        char *tz; 
     105        time_t ret; 
     106        static char envbuf[256]; 
     107 
     108        if(sscanf(s, "%4u%2u%2u-%2u%2u%2u", &tm.tm_year, &tm.tm_mon, 
     109                                &tm.tm_mday, &tm.tm_hour, &tm.tm_min, 
     110                                &tm.tm_sec) != 6) { 
     111                return (time_t)0; 
     112        } 
     113        tm.tm_year = tm.tm_year - 1900; 
     114        tm.tm_mon --; 
     115        tm.tm_wday = 0; /* ignored */ 
     116        tm.tm_yday = 0; /* ignored */ 
     117        tm.tm_isdst = -1; /* forces check for summer time */ 
     118         
     119        tz = getenv("TZ"); 
     120        if (putenv("TZ=Pacific/Auckland")) { 
     121                perror("putenv"); 
     122                return (time_t)0; 
     123        } 
     124        tzset(); 
     125        ret = mktime(&tm); 
     126 
     127        return ret; 
     128} 
     129         
     130 
     131static int legacynzix_init_input(libtrace_t *libtrace) { 
     132 
     133        int retval; 
     134        char *filename = libtrace->uridata; 
     135        regex_t reg; 
     136        regmatch_t match; 
     137 
     138        libtrace->format_data = malloc(sizeof(struct legacy_format_data_t)); 
     139 
     140        DATA(libtrace)->input.file = NULL; 
     141         
     142        if((retval = regcomp(&reg, "[0-9]{8}-[0-9]{6}", REG_EXTENDED)) != 0) { 
     143                trace_set_err(libtrace, errno, "Failed to compile regex"); 
     144                return -1; 
     145        } 
     146        if ((retval = regexec(&reg, filename, 1, &match, 0)) !=0) { 
     147                trace_set_err(libtrace, errno, "Failed to exec regex"); 
     148                return -1; 
     149        } 
     150        DATA(libtrace)->starttime = trtime(&filename[match.rm_so]); 
     151        DATA(libtrace)->ts_high = 0; 
     152        DATA(libtrace)->ts_old = 0; 
    89153        return 0; 
    90154} 
     
    159223} 
    160224 
     225static int legacynzix_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { 
     226        /* Firstly, I apologize for all the constants being thrown around in 
     227         * this function, but it does befit the hackish origins of the 
     228         * NZIX format that I use them. Anyone who wants to clean them up is 
     229         * welcome to do so */ 
     230        int numbytes; 
     231        void *buffer; 
     232        char *data_ptr; 
     233         
     234        if (!packet->buffer || packet->buf_control == TRACE_CTRL_EXTERNAL) { 
     235                packet->buf_control = TRACE_CTRL_PACKET; 
     236                packet->buffer=malloc((size_t)LIBTRACE_PACKET_BUFSIZE); 
     237        } 
     238        buffer = packet->buffer; 
     239 
     240        packet->type = TRACE_RT_DATA_LEGACY_NZIX; 
     241         
     242        while (1) { 
     243                if ((numbytes = libtrace_io_read(INPUT.file, buffer, 
     244                                                (size_t)68)) != 68) { 
     245                        if (numbytes < 0) { 
     246                                trace_set_err(libtrace,errno,"read(%s)",libtrace->uridata); 
     247                        } else if (numbytes > 0) 
     248                                continue; 
     249                        return numbytes; 
     250                }  
     251                /* Packets with a zero length are GPS timestamp packets 
     252                 * but they aren't inserted at the right time to be 
     253                 * useful - instead we'll ignore them unless we can think 
     254                 * of a compelling reason to do otherwise */ 
     255                if (((legacy_nzix_t *)buffer)->len == 0) 
     256                        continue; 
     257                 
     258                break; 
     259        } 
     260 
     261        /* lets move the padding so that it's in the framing header */ 
     262        data_ptr = ((char *)buffer) + 12; 
     263        memmove(data_ptr + 2, data_ptr, 26); 
     264 
     265        packet->header = packet->buffer; 
     266        packet->payload = (void*)((char*)packet->buffer + 
     267                        libtrace->format->get_framing_length(packet)); 
     268        return 68; 
     269} 
     270                 
     271 
    161272static libtrace_linktype_t legacypos_get_link_type(const libtrace_packet_t *packet UNUSED) { 
    162273        return TRACE_TYPE_PPP; 
     
    171282} 
    172283 
     284static libtrace_linktype_t legacynzix_get_link_type(const libtrace_packet_t *packet UNUSED) { 
     285        return TRACE_TYPE_ETH; 
     286} 
     287 
    173288static int legacy_get_capture_length(const libtrace_packet_t *packet UNUSED) { 
    174289        return 64; 
     290} 
     291 
     292static int legacynzix_get_capture_length(const libtrace_packet_t *packet UNUSED) 
     293{ 
     294        return 54; 
    175295} 
    176296 
     
    190310} 
    191311 
     312static int legacynzix_get_wire_length(const libtrace_packet_t *packet) { 
     313        legacy_nzix_t *lnzix = (legacy_nzix_t *)packet->header; 
     314        return lnzix->len; 
     315} 
     316 
    192317static uint64_t legacy_get_erf_timestamp(const libtrace_packet_t *packet) 
    193318{ 
     
    195320        return legacy->ts; 
    196321 
     322 
     323static uint32_t ts_cmp(uint32_t ts_a, uint32_t ts_b) { 
     324         
     325        /* each ts is actually a 30 bit value */ 
     326        ts_a <<= 2; 
     327        ts_b <<= 2; 
     328 
     329 
     330        if (ts_a > ts_b)  
     331                return (ts_a - ts_b); 
     332        else 
     333                return (ts_b - ts_a); 
     334         
     335} 
     336 
     337static struct timeval legacynzix_get_timeval(const libtrace_packet_t *packet) { 
     338        uint64_t new_ts = DATA(packet->trace)->ts_high; 
     339        uint32_t old_ts = DATA(packet->trace)->ts_old; 
     340        struct timeval tv; 
     341        uint32_t hdr_ts; 
     342 
     343        double dts; 
     344         
     345        legacy_nzix_t *legacy = (legacy_nzix_t *)packet->header; 
     346                 
     347        hdr_ts = legacy->ts; 
     348 
     349        /* seems we only need 30 bits to represent our timestamp */ 
     350        hdr_ts >>=2; 
     351        /* try a sequence number wrap-around comparison */ 
     352        if (ts_cmp(hdr_ts, old_ts) > (UINT32_MAX / 2) ) 
     353                new_ts += (1LL << 30); /* wraparound */ 
     354        new_ts &= ~((1LL << 30) -1);    /* mask lower 30 bits */ 
     355        new_ts += hdr_ts;               /* packet ts is the new 30 bits */ 
     356        DATA(packet->trace)->ts_old = hdr_ts; 
     357 
     358        tv.tv_sec = DATA(packet->trace)->starttime + (new_ts / (1000 * 1000)); 
     359        tv.tv_usec = new_ts % (1000 * 1000); 
     360        DATA(packet->trace)->ts_high = new_ts; 
     361 
     362 
     363        dts = tv.tv_sec + (double)tv.tv_usec / 1000 / 1000; 
     364        return tv; 
     365         
     366}        
    197367 
    198368static void legacypos_help(void) { 
     
    226396        printf("\n"); 
    227397        printf("\te.g.: legacyeth:/tmp/trace.gz\n"); 
     398        printf("\n"); 
     399} 
     400 
     401static void legacynzix_help(void) { 
     402        printf("legacynzix format module: $Revision$\n"); 
     403        printf("Supported input URIs:\n"); 
     404        printf("\tlegacynzix:/path/to/file\t(uncompressed)\n"); 
     405        printf("\tlegacynzix:/path/to/file.gz\t(gzip-compressed)\n"); 
     406        printf("\tlegacynzix:-\t(stdin, either compressed or not)\n"); 
     407        printf("\n"); 
     408        printf("\te.g.: legacynzix:/tmp/trace.gz\n"); 
    228409        printf("\n"); 
    229410} 
     
    334515}; 
    335516 
     517static struct libtrace_format_t legacynzix = { 
     518        "legacynzix", 
     519        "$Id$", 
     520        TRACE_FORMAT_LEGACY_NZIX, 
     521        legacynzix_init_input,          /* init_input */         
     522        NULL,                           /* config_input */ 
     523        erf_start_input,                /* start_input */ 
     524        NULL,                           /* pause_input */ 
     525        NULL,                           /* init_output */ 
     526        NULL,                           /* config_output */ 
     527        NULL,                           /* start_output */ 
     528        erf_fin_input,                  /* fin_input */ 
     529        NULL,                           /* fin_output */ 
     530        legacynzix_read_packet,         /* read_packet */ 
     531        NULL,                           /* fin_packet */ 
     532        NULL,                           /* write_packet */ 
     533        legacynzix_get_link_type,       /* get_link_type */ 
     534        NULL,                           /* get_direction */ 
     535        NULL,                           /* set_direction */ 
     536        NULL,                           /* get_erf_timestamp */ 
     537        legacynzix_get_timeval,         /* get_timeval */ 
     538        NULL,                           /* get_seconds */ 
     539        NULL,                           /* seek_erf */ 
     540        NULL,                           /* seek_timeval */ 
     541        NULL,                           /* seek_seconds */ 
     542        legacynzix_get_capture_length,  /* get_capture_length */ 
     543        legacynzix_get_wire_length,     /* get_wire_length */ 
     544        legacynzix_get_framing_length,  /* get_framing_length */ 
     545        NULL,                           /* set_capture_length */ 
     546        NULL,                           /* get_fd */ 
     547        trace_event_trace,              /* trace_event */ 
     548        legacynzix_help,                /* help */ 
     549        NULL,                           /* next pointer */ 
     550}; 
    336551         
    337552void legacy_constructor(void) { 
     
    339554        register_format(&legacyeth); 
    340555        register_format(&legacyatm); 
    341 } 
     556        register_format(&legacynzix); 
     557} 
  • trunk/lib/libtrace.h.in

    r1242 r1243  
    263263        TRACE_FORMAT_BPF          =11, 
    264264        TRACE_FORMAT_TSH          =12, 
    265         TRACE_FORMAT_ATMHDR       =13 
     265        TRACE_FORMAT_ATMHDR       =13, 
     266        TRACE_FORMAT_LEGACY_NZIX  =14 
    266267}; 
    267268 
     
    301302 
    302303        TRACE_RT_DATA_ATMHDR = TRACE_RT_DATA_SIMPLE + TRACE_FORMAT_ATMHDR, 
     304        TRACE_RT_DATA_LEGACY_NZIX=TRACE_RT_DATA_SIMPLE + TRACE_FORMAT_LEGACY_NZIX, 
    303305        TRACE_RT_DATA_DLT               = 2000, /**< Pcap doesn't store the 
    304306                                                  * linktype per packet, and 
  • trunk/lib/protocols.c

    r1234 r1243  
    336336                        break; 
    337337        } 
    338         fprintf(stderr,"Don't understand link layer type %i in trace_get_payload_from_link()\n", 
     338        fprintf(stderr, "Don't understand link layer type %i in trace_get_payload_from_link()\n", 
    339339                linktype); 
    340340        return NULL; 
Note: See TracChangeset for help on using the changeset viewer.