Changeset 538


Ignore:
Timestamp:
12/09/05 11:03:51 (7 years ago)
Author:
spa1
Message:

Separated wag device and wag trace into two separate uris - wag and wtf
respectively. Fixed wag_read to work correctly with wag drivers.
Updated anything that reads from the wag header as it is now stored
in network byte order.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/libtrace-2_0_25/lib/format_wag.c

    r536 r538  
    7777 
    7878static struct libtrace_format_t wag; 
     79static struct libtrace_format_t wag_trace; 
    7980 
    8081#define CONNINFO libtrace->format_data->conn_info 
     
    126127static int wag_init_input(struct libtrace_t *libtrace) { 
    127128        struct stat buf; 
    128         struct sockaddr_un unix_sock; 
     129        //struct sockaddr_un unix_sock; 
    129130        libtrace->format_data = (struct libtrace_format_data_t *)  
    130131                calloc(1,sizeof(struct libtrace_format_data_t)); 
    131132        CONNINFO.path = libtrace->uridata; 
     133         
     134        if (stat(CONNINFO.path,&buf) == -1 ) { 
     135                perror("stat"); 
     136                return 0; 
     137        } 
     138        if (S_ISCHR(buf.st_mode)) { 
     139                libtrace->sourcetype = DEVICE; 
     140                                 
     141                INPUT.fd = open(CONNINFO.path, O_RDONLY); 
     142 
     143        } else { 
     144                fprintf(stderr, "%s is not a valid char device, exiting\n", 
     145                                CONNINFO.path); 
     146                return 0; 
     147                 
     148        } 
     149        return 1; 
     150} 
     151 
     152static int wtf_init_input(struct libtrace_t *libtrace) { 
     153        struct stat buf; 
     154        struct sockaddr_un unix_sock; 
     155 
     156        libtrace->format_data = (struct libtrace_format_data_t *) 
     157                calloc(1,sizeof(struct libtrace_format_data_t)); 
     158        CONNINFO.path = libtrace->uridata; 
     159 
    132160        if (!strncmp(CONNINFO.path,"-",1)) { 
    133161                // STDIN 
     
    136164 
    137165        } else { 
     166 
     167 
     168                // Do we need this socket stuff at all?? 
     169                // If we do, put it into wag_init_input as it uses 
     170                // INPUT.fd 
     171 
     172                /* 
    138173                if (stat(CONNINFO.path,&buf) == -1 ) { 
    139174                        perror("stat"); 
     
    161196                        } 
    162197                } else {  
     198                */ 
    163199                        // TRACE 
    164200                        libtrace->sourcetype = TRACE; 
     
    172208                                        O_LARGEFILE), "r"); 
    173209 
    174                 } 
     210                //} 
    175211        } 
    176212        return 1; 
    177213} 
    178214 
    179 static int wag_init_output(struct libtrace_out_t *libtrace) { 
     215 
     216static int wtf_init_output(struct libtrace_out_t *libtrace) { 
    180217        char *filemode = 0; 
    181218        libtrace->format_data = (struct libtrace_format_data_out_t *) 
     
    192229                                        libtrace->uridata, 
    193230                                        O_CREAT | O_LARGEFILE | O_WRONLY, 
    194                                         S_IRUSR | S_IWUSR), filemode); 
     231                                        S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP), filemode); 
    195232        } 
    196233 
     
    198235} 
    199236 
    200 static int wag_config_output(struct libtrace_out_t *libtrace, int argc, char *argv[]) { 
     237static int wtf_config_output(struct libtrace_out_t *libtrace, int argc, char *argv[]) { 
    201238#if HAVE_ZLIB 
    202239        int opt; 
     
    227264 
    228265static int wag_fin_input(struct libtrace_t *libtrace) { 
     266        close(INPUT.fd); 
     267        return 0; 
     268} 
     269 
     270static int wtf_fin_input(struct libtrace_t *libtrace) { 
    229271        LIBTRACE_CLOSE(INPUT.file); 
    230272        return 0; 
    231273} 
    232274 
    233 static int wag_fin_output(struct libtrace_out_t *libtrace) { 
     275static int wtf_fin_output(struct libtrace_out_t *libtrace) { 
    234276        LIBTRACE_CLOSE(OUTPUT.file); 
    235277        return 0; 
     
    238280static int wag_read(struct libtrace_t *libtrace, void *buffer, size_t len) { 
    239281        int numbytes; 
     282        int framesize; 
    240283        assert(libtrace); 
    241284 
     
    243286                buffer = malloc(len); 
    244287 
    245         while(1) { 
    246                 switch(libtrace->sourcetype) { 
    247                         case DEVICE: 
    248                                 if ((numbytes=read(INPUT.fd,  
    249                                                                 buffer,  
    250                                                                 len)) == -1) { 
    251                                         perror("read"); 
    252                                         return -1; 
    253                                 } 
    254                                 break; 
    255                         default: 
    256                                 if ((numbytes=LIBTRACE_READ(INPUT.file, 
    257                                                                 buffer, 
    258                                                                 len)) == -1) { 
    259                                         perror("libtrace_read"); 
    260                                         return -1; 
    261                                 } 
    262                 } 
    263                 break; 
    264         } 
    265         return numbytes; 
    266  
     288        // read in wag_frame_hdr 
     289        if ((numbytes = read(INPUT.fd,  
     290                        buffer, 
     291                        sizeof(struct wag_frame_hdr))) != sizeof(struct wag_frame_hdr)) { 
     292                return -1; 
     293        } 
     294          
     295        framesize = ntohs(((struct wag_frame_hdr *)buffer)->size); 
     296 
     297        if (framesize > len) { 
     298                return -1; 
     299        } 
     300 
     301        // read in remainder of packet 
     302        if((numbytes = read(INPUT.fd, 
     303                        buffer + sizeof(struct wag_frame_hdr), 
     304                        framesize - sizeof(struct wag_frame_hdr))) !=  
     305                        (framesize - sizeof(struct wag_frame_hdr))) { 
     306                 
     307                return -1; 
     308           
     309        } 
     310 
     311        return framesize; 
    267312} 
    268313 
     
    270315static int wag_read_packet(struct libtrace_t *libtrace, struct libtrace_packet_t *packet) { 
    271316        int numbytes; 
     317         
     318        char buf[RP_BUFSIZE]; 
     319         
     320         
     321        packet->trace = libtrace; 
     322         
     323        if ((numbytes = wag_read(libtrace, buf, RP_BUFSIZE)) <= 0) { 
     324             
     325                return numbytes; 
     326        } 
     327 
     328        memcpy(packet->buffer, buf, numbytes); 
     329         
     330        packet->status.type = RT_DATA; 
     331        packet->status.message = 0; 
     332        packet->size = numbytes; 
     333        return numbytes; 
     334} 
     335 
     336static int wtf_read_packet(struct libtrace_t *libtrace, struct libtrace_packet_t *packet) { 
     337        int numbytes; 
     338        void *buffer = packet->buffer; 
     339        void *buffer2 = packet->buffer; 
     340        int framesize; 
    272341        int size; 
    273         char buf[RP_BUFSIZE]; 
    274         int read_required = 0; 
    275  
    276         void *buffer = 0; 
    277  
    278         packet->trace = libtrace; 
    279         buffer = packet->buffer; 
    280          
    281  
    282         do { 
    283                 if (tracefifo_out_available(libtrace->fifo) == 0 || read_required) { 
    284                         if ((numbytes = wag_read(libtrace,buf,RP_BUFSIZE)) <= 0) { 
    285                                 return numbytes; 
    286                         } 
    287                         assert(libtrace->fifo); 
    288                         tracefifo_write(libtrace->fifo,buf,numbytes); 
    289                         read_required = 0; 
    290                 } 
    291                 // read in wag_frame_hdr 
    292                 if ((numbytes = tracefifo_out_read(libtrace->fifo,  
    293                                                 buffer, 
    294                                                 sizeof(struct wag_frame_hdr))) 
    295                                 == 0 ) { 
    296                         tracefifo_out_reset(libtrace->fifo); 
    297                         read_required = 1; 
    298                         continue; 
    299                 } 
    300                  
    301                 size = ntohs(((struct wag_frame_hdr *)buffer)->size); 
    302  
    303                 // wag isn't in network byte order yet 
    304                 //size = htons(size); 
    305                 //printf("%d %d\n",size,htons(size)); 
    306  
    307                 // read in full packet 
    308                 if((numbytes = tracefifo_out_read(libtrace->fifo,buffer,size)) == 0) { 
    309                         tracefifo_out_reset(libtrace->fifo); 
    310                         read_required = 1; 
    311                         continue; 
    312                 } 
    313  
    314                 // have the whole packet 
    315                 tracefifo_out_update(libtrace->fifo,size); 
    316                 tracefifo_ack_update(libtrace->fifo,size); 
    317  
    318                 packet->status.type = RT_DATA; 
    319                 packet->status.message = 0; 
    320                 packet->size = numbytes; 
    321                 return numbytes; 
    322         } while(1); 
    323 } 
    324  
    325 static int wag_write_packet(struct libtrace_out_t *libtrace, const struct libtrace_packet_t *packet) { 
     342         
     343        if ((numbytes = LIBTRACE_READ(INPUT.file, buffer, sizeof(struct wag_frame_hdr))) == -1) { 
     344                perror("libtrace_read"); 
     345                return -1; 
     346        } 
     347 
     348        if (numbytes == 0) { 
     349                return 0; 
     350        } 
     351 
     352        framesize = ntohs(((struct wag_frame_hdr *)buffer)->size); 
     353        buffer2 = buffer + sizeof(struct wag_frame_hdr); 
     354        size = framesize - sizeof(struct wag_frame_hdr); 
     355        assert(size < LIBTRACE_PACKET_BUFSIZE); 
     356 
     357         
     358        if ((numbytes=LIBTRACE_READ(INPUT.file, buffer2, size)) != size) { 
     359                perror("libtrace read"); 
     360                return -1; 
     361        } 
     362 
     363        packet->status.type = RT_DATA; 
     364        packet->status.message = 0; 
     365        packet->size = framesize; 
     366        return framesize; 
     367         
     368}                                
     369         
     370static int wtf_write_packet(struct libtrace_out_t *libtrace, const struct libtrace_packet_t *packet) { 
    326371        int numbytes =0 ; 
    327         if (packet->trace->format != &wag) { 
    328                 fprintf(stderr,"Cannot convert from wag to %s format yet\n", 
     372        if (packet->trace->format != &wag_trace) { 
     373                fprintf(stderr,"Cannot convert from wag trace format to %s format yet\n", 
    329374                                packet->trace->format->name); 
    330375                return -1; 
     
    358403        struct wag_data_frame *wagptr = (struct wag_data_frame *)packet->buffer; 
    359404        uint64_t timestamp = 0; 
    360         timestamp = (((uint64_t)wagptr->ts.secs) << 32) + wagptr->ts.subsecs; 
    361         //timestamp |= (uint64_t)wagptr->ts.secs<<32; 
    362         //timestamp = ((timestamp%44000000)*(UINT_MAX/44000000))  
    363         //      | ((timestamp/44000000)<<32); 
     405        timestamp = ((uint64_t)(ntohl(wagptr->ts.secs)) << 32) | (uint64_t)(ntohl(wagptr->ts.subsecs)); 
    364406        return timestamp; 
    365407} 
     
    397439        printf("Supported input URIs:\n"); 
    398440        printf("\twag:/dev/wagn\n"); 
    399         printf("\twag:/path/to/trace.wag\n"); 
    400         printf("\twag:/path/to/trace.wag.gz\n"); 
    401441        printf("\n"); 
    402442        printf("\te.g.: wag:/dev/wag0\n"); 
    403         printf("\te.g.: wag:/tmp/trace.wag.gz\n"); 
    404443        printf("\n"); 
    405444        printf("Supported output URIs:\n"); 
    406         printf("\tnone\n"); 
     445        printf("\tNone\n"); 
     446        printf("\n"); 
     447} 
     448 
     449static void wtf_help() { 
     450        printf("wag trace format module: $Revision$\n"); 
     451        printf("Supported input URIs:\n"); 
     452        printf("\twtf:/path/to/trace.wag\n"); 
     453        printf("\twtf:/path/to/trace.wag.gz\n"); 
     454        printf("\n"); 
     455        printf("\te.g.: wtf:/tmp/trace.wag.gz\n"); 
     456        printf("\n"); 
     457        printf("Supported output URIs:\n"); 
     458        printf("\twtf:/path/to/trace.wag\n"); 
     459        printf("\twtf:/path/to/trace.wag.gz\n"); 
     460        printf("\n"); 
     461        printf("\te.g.: wtf:/tmp/trace.wag.gz\n"); 
    407462        printf("\n"); 
    408463} 
     
    411466        "wag", 
    412467        "$Id$", 
    413         "wag", 
     468        "wtf", 
    414469        wag_init_input,                 /* init_input */         
    415         wag_init_output,                /* init_output */ 
    416         wag_config_output,              /* config_output */ 
     470        NULL,                           /* init_output */ 
     471        NULL,                           /* config_output */ 
    417472        wag_fin_input,                  /* fin_input */ 
    418         wag_fin_output,                 /* fin_output */ 
     473        NULL,                           /* fin_output */ 
    419474        wag_read_packet,                /* read_packet */ 
    420         wag_write_packet,               /* write_packet */ 
     475        NULL,                           /* write_packet */ 
    421476        wag_get_link,                   /* get_link */ 
    422477        wag_get_link_type,              /* get_link_type */ 
     
    435490}; 
    436491 
     492/* wtf stands for Wag Trace Format */ 
     493 
     494static struct libtrace_format_t wag_trace = { 
     495        "wtf", 
     496        "$Id$", 
     497        "wtf", 
     498        wtf_init_input,                 /* init_input */ 
     499        wtf_init_output,                /* init_output */ 
     500        wtf_config_output,              /* config_output */ 
     501        wtf_fin_input,                  /* fin_input */ 
     502        wtf_fin_output,                 /* fin_output */ 
     503        wtf_read_packet,                /* read_packet */ 
     504        wtf_write_packet,               /* write_packet */ 
     505        wag_get_link,                   /* get_link */ 
     506        wag_get_link_type,              /* get_link_type */ 
     507        wag_get_direction,              /* get_direction */ 
     508        NULL,                           /* set_direction */ 
     509        wag_get_erf_timestamp,          /* get_erf_timestamp */ 
     510        NULL,                           /* get_timeval */ 
     511        NULL,                           /* get_seconds */ 
     512        wag_get_capture_length,         /* get_capture_length */ 
     513        wag_get_wire_length,            /* get_wire_length */ 
     514        wag_get_framing_length,         /* get_framing_length */ 
     515        NULL,                           /* set_capture_length */ 
     516        wag_get_fd,                     /* get_fd */ 
     517        wag_event_trace,                /* trace_event */ 
     518        wtf_help                        /* help */ 
     519}; 
     520 
     521 
    437522void __attribute__((constructor)) wag_constructor() { 
    438523        register_format(&wag); 
    439 } 
     524        register_format(&wag_trace); 
     525} 
Note: See TracChangeset for help on using the changeset viewer.