Changeset 591


Ignore:
Timestamp:
02/16/06 15:44:06 (7 years ago)
Author:
perry
Message:

Added libtrace_set_err() to everything (except format_rt, that's shane's job)

Location:
trunk/lib
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/fifo.c

    r577 r591  
    3333#include <errno.h> 
    3434#include <stdlib.h> /* free */ 
    35 #include <stdio.h> /* perror, sprintf, printf */ 
     35#include <stdio.h> /* sprintf, printf */ 
    3636#include <assert.h> /* assert */ 
    3737#include <string.h> /* bzero */ 
     
    8585 
    8686        if ((fifo->base = malloc(fifo->length)) == 0) { 
    87                 perror("malloc"); 
     87                libtrace_set_err(errno,"malloc failed"); 
    8888                return 0; 
    8989        } 
  • trunk/lib/format_erf.c

    r590 r591  
    163163        CONNINFO.path = libtrace->uridata; 
    164164        if (stat(CONNINFO.path,&buf) == -1) { 
    165                 perror("stat"); 
     165                trace_set_err(errno,"stat(%s)",CONNINFO.path); 
    166166                return 0; 
    167167        }  
     
    170170                libtrace->sourcetype = TRACE_SOURCE_DEVICE; 
    171171                if((INPUT.fd = dag_open(CONNINFO.path)) < 0) { 
    172                         fprintf(stderr,"Cannot open DAG %s: %m\n",  
    173                                         CONNINFO.path,errno); 
    174                         exit(0); 
     172                        trace_set_err(errno,"Cannot open DAG %s", 
     173                                        CONNINFO.path); 
     174                        return 0; 
    175175                } 
    176176                if((DAG.buf = (void *)dag_mmap(INPUT.fd)) == MAP_FAILED) { 
    177                         fprintf(stderr,"Cannot mmap DAG %s: %m\n",  
    178                                         CONNINFO.path,errno); 
    179                         exit(0); 
     177                        trace_set_err(errno,"Cannot mmap DAG %s", 
     178                                        CONNINFO.path); 
     179                        return 0; 
    180180                } 
    181181                if(dag_start(INPUT.fd) < 0) { 
    182                         fprintf(stderr,"Cannot start DAG %s: %m\n",  
    183                                         CONNINFO.path,errno); 
    184                         exit(0); 
     182                        trace_set_err(errno,"Cannot start DAG %s", 
     183                                        CONNINFO.path); 
     184                        return 0; 
    185185                } 
    186186        } else { 
    187                 fprintf(stderr,"%s isn't a valid char device, exiting\n", 
     187                trace_set_err(errno,"Not a valid dag device: %s", 
    188188                                CONNINFO.path); 
    189189                return 0; 
     
    221221                libtrace->sourcetype = TRACE_SOURCE_STDIN; 
    222222                INPUT.file = LIBTRACE_FDOPEN(fileno(stdin), "r"); 
    223                  
    224  
    225223        } else { 
    226224                if (stat(CONNINFO.path,&buf) == -1 ) { 
    227                         perror("stat"); 
     225                        trace_set_err(errno,"stat(%s)", 
     226                                        CONNINFO.path); 
    228227                        return 0; 
    229228                } 
     
    232231                        if ((INPUT.fd = socket( 
    233232                                        AF_UNIX, SOCK_STREAM, 0)) == -1) { 
    234                                 perror("socket"); 
     233                                trace_set_err(errno, 
     234                                                "socket(AF_UNIX,SOCK_STREAM)"); 
    235235                                return 0; 
    236236                        } 
     
    244244                                        (struct sockaddr *)&unix_sock, 
    245245                                        sizeof(struct sockaddr)) == -1) { 
    246                                 perror("connect (unix)"); 
     246                                trace_set_err(errno, 
     247                                                "connect(%s)", CONNINFO.path); 
    247248                                return 0; 
    248249                        } 
    249250                } else {  
     251                        int fd; 
    250252 
    251253                        libtrace->sourcetype = TRACE_SOURCE_TRACE; 
     
    256258                         * letting you do this... 
    257259                         */ 
     260                        fd=open(CONNINFO.path,O_LARGEFILE); 
     261                        if (fd==-1) { 
     262                                trace_set_err(errno,"Unable to open %s", 
     263                                                CONNINFO.path); 
     264                        } 
    258265                        INPUT.file = LIBTRACE_FDOPEN(open( 
    259266                                                CONNINFO.path, 
     
    295302         
    296303        if ((he=gethostbyname(CONNINFO.rt.hostname)) == NULL) {   
    297                 perror("gethostbyname"); 
     304                trace_set_err(errno,"failed to resolve %s", 
     305                                CONNINFO.rt.hostname); 
    298306                return 0; 
    299307        }  
    300308        if ((INPUT.fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { 
    301                 perror("socket"); 
     309                trace_set_err(errno,"socket(AF_INET,SOCK_STREAM)"); 
    302310                return 0; 
    303311        } 
     
    310318        if (connect(INPUT.fd, (struct sockaddr *)&remote, 
    311319                                sizeof(struct sockaddr)) == -1) { 
    312                 perror("connect (inet)"); 
     320                trace_set_err(errno,"connect(%s)", 
     321                                CONNINFO.rt.hostname); 
    313322                return 0; 
    314323        } 
     
    347356                                S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); 
    348357                if (fd <= 0) { 
     358                        trace_set_err(errno,"OPEN(%s,\"w\")", 
     359                                        libtrace->uridata); 
    349360                        return 0; 
    350361                } 
     
    367378                default: 
    368379                        /* Unknown option */ 
     380                        trace_set_err(TRACE_ERR_UNKNOWN_OPTION, 
     381                                        "Unknown option"); 
    369382                        return -1; 
    370383        } 
     
    485498                                        packet->buffer, 
    486499                                        dag_record_size)) == -1) { 
    487                 perror("libtrace_read"); 
     500                trace_set_err(errno,"read(%s)", 
     501                                CONNINFO.path); 
    488502                return -1; 
    489503        } 
     
    497511        assert(size < LIBTRACE_PACKET_BUFSIZE); 
    498512         
    499         /* If your trace is legacy, or corrupt, then this assert may fire. */ 
    500         /* turns out some older traces have fixed snaplens, which are padded 
    501          * with 00's if the packet is smaller, so this doesn't work.  Sigh. 
    502         assert(ntohs(((dag_record_t *)buffer)->rlen) <=  
    503                         ntohs(((dag_record_t*)buffer)->wlen)+erf_get_framing_length(packet)); 
    504         */ 
    505513        /* Unknown/corrupt */ 
    506514        assert(((dag_record_t *)packet->buffer)->type < 10); 
     
    510518                                        buffer2, 
    511519                                        size)) != size) { 
    512                 perror("libtrace_read"); 
     520                trace_set_err(errno, "read(%s)", CONNINFO.path); 
    513521                return -1; 
    514522        } 
     
    541549                                continue; 
    542550                        } 
    543                         perror("recv"); 
     551                        trace_set_err(errno,"recv(%s)", 
     552                                        libtrace->uridata); 
    544553                        return -1; 
    545554                } 
     
    594603                tracefifo_out_update(libtrace->fifo, sizeof(uint32_t)); 
    595604                 
    596                 /* 
    597                 // read in the ERF header 
    598                 if ((numbytes = tracefifo_out_read(libtrace->fifo, buffer, 
    599                                                 dag_record_size)) == 0) { 
    600                         tracefifo_out_reset(libtrace->fifo); 
    601                         read_required = 1; 
    602                         continue; 
    603                 } 
    604                 */ 
    605605                if (status.type == RT_MSG) { 
    606606                        /* Need to skip this packet as it is a message packet */ 
     
    636636} 
    637637 
    638 static int erf_dump_packet(struct libtrace_out_t *libtrace, dag_record_t *erfptr, int pad, void *buffer, size_t size) { 
     638static int erf_dump_packet(struct libtrace_out_t *libtrace, 
     639                dag_record_t *erfptr, int pad, void *buffer, size_t size) { 
    639640        int numbytes = 0; 
    640641        if ((numbytes = LIBTRACE_WRITE(OUTPUT.file, erfptr, dag_record_size + pad)) == 0) { 
    641                 perror("libtrace_write"); 
     642                trace_set_err(errno,"write(%s)",CONNINFO.path); 
    642643                return -1; 
    643644        } 
    644645 
    645646        if (buffer) { 
    646                 if ((numbytes = LIBTRACE_WRITE(OUTPUT.file, buffer, size)) == 0) { 
    647                         perror("libtrace_write"); 
     647                if ((numbytes=LIBTRACE_WRITE(OUTPUT.file, buffer, size)) == 0) { 
     648                        trace_set_err(errno,"write(%s)",CONNINFO.path); 
    648649                        return -1; 
    649650                } 
     
    665666        if (!OUTPUT.file) { 
    666667                if (erf_open_output(libtrace) <= 0) {  
    667                         perror("erf_open_output"); 
     668                        trace_set_err(errno,"open(%s)",libtrace->uridata); 
    668669                        return -1; 
    669670                } 
  • trunk/lib/format_helper.c

    r512 r591  
    5555        } 
    5656        if (ioctl(event.fd,FIONREAD,&data)==-1) { 
    57                 perror("ioctl(FIONREAD)"); 
     57                event.type = TRACE_EVENT_TERMINATE; 
     58                return event; 
    5859        } 
    5960        if (data>0) { 
     
    8283                                        trace->event.packet.size); 
    8384                } else { 
    84                         // return here, the test for 
    85                         // event.size will sort out the error 
     85                        /* return here, the test for 
     86                         * event.size will sort out the error 
     87                         */ 
    8688                        event.type = TRACE_EVENT_TERMINATE; 
    8789                        return event; 
     
    9193        ts=trace_get_seconds(packet); 
    9294        if (trace->event.tdelta!=0) { 
    93                 // Get the adjusted current time 
     95                /* Get the adjusted current time */ 
    9496                gettimeofday(&stv, NULL); 
    9597                now = stv.tv_sec +  
    9698                        ((double)stv.tv_usec / 1000000.0); 
    97                 // adjust for trace delta 
     99                /* adjust for trace delta */ 
    98100                now -= trace->event.tdelta;  
    99101 
    100                 //if the trace timestamp is still in the  
     102                /*if the trace timestamp is still in the  
    101103                //future, return a SLEEP event,  
    102104                //otherwise fire the packet 
     105                 */ 
    103106                if (ts > now) { 
    104107                        event.seconds = ts -  
     
    109112        } else { 
    110113                gettimeofday(&stv, NULL); 
    111                 // work out the difference between the  
     114                /* work out the difference between the  
    112115                // start of trace replay, and the first 
    113116                // packet in the trace 
     117                 */ 
    114118                trace->event.tdelta = stv.tv_sec +  
    115119                        ((double)stv.tv_usec / 1000000.0); 
     
    117121        } 
    118122 
    119         // This is the first packet, so just fire away. 
     123        /* This is the first packet, so just fire away. */ 
    120124        packet->size = trace->event.packet.size; 
    121125        memcpy(packet->buffer, 
  • trunk/lib/format_legacy.c

    r590 r591  
    9595                gzFile *file; 
    9696#else    
    97                 //FILE  *file; 
     97                /*FILE  *file; */ 
    9898                int file; 
    9999#endif 
     
    135135        CONNINFO.path = libtrace->uridata; 
    136136        if (!strncmp(CONNINFO.path,"-",1)) { 
    137                 // STDIN 
     137                /* STDIN */ 
    138138                libtrace->sourcetype = TRACE_SOURCE_STDIN; 
    139139                INPUT.file = LIBTRACE_FDOPEN(fileno(stdin), "r"); 
    140140        } else { 
    141141                if (stat(CONNINFO.path,&buf) == -1 ) { 
    142                         perror("stat"); 
     142                        trace_set_err(errno,"stat(%s)",CONNINFO.path); 
    143143                        return 0; 
    144144                } 
     
    147147                        if ((INPUT.fd = socket( 
    148148                                        AF_UNIX, SOCK_STREAM, 0)) == -1) { 
    149                                 perror("socket"); 
     149                                trace_set_err("socket(%s)",CONNINFO.path); 
    150150                                return 0; 
    151151                        } 
     
    159159                                        (struct sockaddr *)&unix_sock, 
    160160                                        sizeof(struct sockaddr)) == -1) { 
    161                                 perror("connect (unix)"); 
     161                                trace_set_err("socket(%s)",CONNINFO.path); 
    162162                                return 0; 
    163163                        } 
    164164                } else {  
     165                        int fd; 
    165166                        libtrace->sourcetype = TRACE_SOURCE_TRACE; 
    166167 
    167                         // we use an FDOPEN call to reopen an FD 
     168                        /* we use an FDOPEN call to reopen an FD 
    168169                        // returned from open(), so that we can set 
    169170                        // O_LARGEFILE. This gets around gzopen not 
    170171                        // letting you do this... 
    171                         INPUT.file = LIBTRACE_FDOPEN(open( 
    172                                                 CONNINFO.path, 
    173                                                 O_LARGEFILE),"r"); 
     172                        */ 
     173                        fd=open( CONNINFO.path, O_LARGEFILE); 
     174                        if (fd==-1) { 
     175                                trace_set_err(errno,"open(%s)",CONNINFO.path); 
     176                                return 0; 
     177                        } 
     178                        INPUT.file = LIBTRACE_FDOPEN(fd,"r"); 
    174179                } 
    175180        } 
     
    190195                                        buffer, 
    191196                                        64)) == -1) { 
    192                 perror("libtrace_read"); 
     197                trace_set_err(errno,"read(%s)",libtrace->uridata); 
    193198                return -1; 
    194199        } 
     
    196201         
    197202        packet->header = packet->buffer; 
    198         packet->payload = packet->buffer +  
    199                 packet->trace->format->get_framing_length(packet); 
     203        packet->payload = (void*)((char*)packet->buffer +  
     204                packet->trace->format->get_framing_length(packet)); 
    200205         
    201206        return 64; 
  • trunk/lib/format_pcap.c

    r590 r591  
    4848#include <stdlib.h> 
    4949#include <string.h> 
     50#include <errno.h> 
    5051 
    5152#if HAVE_PCAP_BPF_H 
     
    9899}; 
    99100 
    100 static int linktype_to_dlt(libtrace_linktype_t t) { 
    101         static int table[] = { 
    102                 -1, /* LEGACY */ 
    103                 -1, /* HDLC over POS */ 
    104                 DLT_EN10MB, /* Ethernet */ 
    105                 -1, /* ATM */ 
    106                 DLT_IEEE802_11, /* 802.11 */ 
    107                 -1 /* END OF TABLE */ 
    108         }; 
    109         if (t>sizeof(table)/sizeof(*table)) { 
    110                 return -1; 
    111         } 
    112         return table[t]; 
    113 } 
    114  
    115101static int pcap_init_input(struct libtrace_t *libtrace) { 
    116102        char errbuf[PCAP_ERRBUF_SIZE]; 
     
    125111                        pcap_open_offline(CONNINFO.path, 
    126112                                                errbuf)) == NULL) { 
    127                         fprintf(stderr,"%s\n",errbuf); 
     113                        trace_set_err(TRACE_ERR_INIT_FAILED,"%s",errbuf); 
    128114                        return 0; 
    129115                }                
    130116        } else { 
    131117                if (stat(CONNINFO.path,&buf) == -1) { 
    132                         perror("stat"); 
     118                        trace_set_err(errno,"stat(%s)",CONNINFO.path); 
    133119                        return 0; 
    134120                } 
     
    140126                                        1, 
    141127                                        errbuf)) == NULL) { 
    142                                 fprintf(stderr,"%s\n",errbuf); 
     128                                trace_set_err(TRACE_ERR_INIT_FAILED,"%s", 
     129                                                errbuf); 
    143130                                return 0; 
    144131                        } 
     
    147134                                pcap_open_offline(CONNINFO.path, 
    148135                                        errbuf)) == NULL) { 
    149                                 fprintf(stderr,"%s\n",errbuf); 
     136                                trace_set_err(TRACE_ERR_INIT_FAILED,"%s", 
     137                                                errbuf); 
    150138                                return 0; 
    151139                        } 
     
    177165                        1, 
    178166                        errbuf)) == NULL) { 
    179                 fprintf(stderr,"%s\n",errbuf); 
     167                trace_set_err(TRACE_ERR_INIT_FAILED,"%s",errbuf); 
    180168                return 0; 
    181169        } 
     
    184172 
    185173static int pcapint_init_output(struct libtrace_out_t *libtrace __attribute__((unused))) { 
     174        trace_set_err(TRACE_ERR_NO_INIT_OUT,"Writing to a pcap interface not implemented yet"); 
    186175        return -1; 
    187176} 
     
    265254static int pcapint_write_packet(struct libtrace_out_t *libtrace __attribute__((unused)), const struct libtrace_packet_t *packet __attribute__((unused))) { 
    266255 
     256        assert(0); 
    267257        return 0; 
    268258} 
     
    273263        pcapptr = (struct pcap_pkthdr *)packet->header; 
    274264        linktype = pcap_datalink(packet->trace->format_data->input.pcap); 
    275         switch(linktype) { 
    276                 case DLT_NULL: 
    277                         return TRACE_TYPE_NONE; 
    278                 case DLT_EN10MB: 
    279                         return TRACE_TYPE_ETH; 
    280                 case DLT_ATM_RFC1483: 
    281                         return TRACE_TYPE_ATM; 
    282                 case DLT_IEEE802_11: 
    283                         return TRACE_TYPE_80211; 
    284 #ifdef DLT_LINUX_SLL 
    285                 case DLT_LINUX_SLL: 
    286                         return TRACE_TYPE_LINUX_SLL; 
    287 #endif 
    288 #ifdef DLT_PFLOG 
    289                 case DLT_PFLOG: 
    290                         return TRACE_TYPE_PFLOG; 
    291 #endif 
    292         } 
    293         return -1; 
     265        return pcap_dlt_to_libtrace(linktype); 
    294266} 
    295267 
     
    302274                        sll = trace_get_link(packet); 
    303275                        if (!sll) { 
     276                                trace_set_err(TRACE_ERR_BAD_PACKET, 
     277                                                "Bad or missing packet"); 
    304278                                return -1; 
    305279                        } 
     
    328302                        pflog = trace_get_link(packet); 
    329303                        if (!pflog) { 
     304                                trace_set_err(TRACE_ERR_BAD_PACKET, 
     305                                                "Bad or missing packet"); 
    330306                                return -1; 
    331307                        } 
  • trunk/lib/format_wag.c

    r590 r591  
    111111                struct { 
    112112                        int level; 
     113                        int filemode; 
    113114                } zlib; 
    114115        } options; 
     
    130131         
    131132        if (stat(CONNINFO.path,&buf) == -1 ) { 
    132                 perror("stat"); 
     133                trace_set_err(errno,"stat(%s)",CONNINFO.path); 
    133134                return 0; 
    134135        } 
     
    139140 
    140141        } else { 
    141                 fprintf(stderr, "%s is not a valid char device, exiting\n", 
     142                trace_set_err(TRACE_ERR_INIT_FAILED, 
     143                                "%s is not a valid char device", 
    142144                                CONNINFO.path); 
    143145                return 0; 
     
    157159                libtrace->sourcetype = TRACE_SOURCE_STDIN; 
    158160                INPUT.file = LIBTRACE_FDOPEN(fileno(stdin),"r"); 
    159  
    160161        } else { 
    161                 /* Do we need this socket stuff at all?? 
    162                  * If we do, put it into wag_init_input as it uses 
    163                  * INPUT.fd 
     162                int fd; 
     163                /* TRACE */ 
     164                libtrace->sourcetype = TRACE_SOURCE_TRACE; 
     165 
     166                /* we use an FDOPEN call to reopen an FD 
     167                 * returned from open(), so that we can set 
     168                 * O_LARGEFILE. This gets around gzopen not 
     169                 * letting you do this... 
    164170                 */ 
    165  
    166                 /* 
    167                 if (stat(CONNINFO.path,&buf) == -1 ) { 
    168                         perror("stat"); 
     171                fd = open(CONNINFO.path, O_LARGEFILE); 
     172                if (fd==-1) { 
     173                        trace_set_err(errno,"open(%s)",CONNINFO.path); 
    169174                        return 0; 
    170175                } 
    171                 if (S_ISSOCK(buf.st_mode)) { 
    172                         libtrace->sourcetype = SOCKET; 
    173                         // SOCKET 
    174                         if ((INPUT.fd = socket( 
    175                                         AF_UNIX, SOCK_STREAM, 0)) == -1) { 
    176                                 perror("socket"); 
    177                                 return 0; 
    178                         } 
    179                         unix_sock.sun_family = AF_UNIX; 
    180                         bzero(unix_sock.sun_path,108); 
    181                         snprintf(unix_sock.sun_path, 
    182                                         108,"%s" 
    183                                         ,CONNINFO.path); 
    184  
    185                         if (connect(INPUT.fd,  
    186                                         (struct sockaddr *)&unix_sock, 
    187                                         sizeof(struct sockaddr)) == -1) { 
    188                                 perror("connect (unix)"); 
    189                                 return 0; 
    190                         } 
    191                 } else {  
    192                 */ 
    193                         /* TRACE */ 
    194                         libtrace->sourcetype = TRACE_SOURCE_TRACE; 
    195                          
    196                         /* we use an FDOPEN call to reopen an FD 
    197                          * returned from open(), so that we can set 
    198                          * O_LARGEFILE. This gets around gzopen not 
    199                          * letting you do this... 
    200                          */ 
    201                         INPUT.file = LIBTRACE_FDOPEN(open( 
    202                                         CONNINFO.path, 
    203                                         O_LARGEFILE), "r"); 
     176                INPUT.file = LIBTRACE_FDOPEN(fd, "r"); 
    204177 
    205178        } 
     
    219192                OUTPUT.file = LIBTRACE_FDOPEN(dup(1), filemode); 
    220193        } else { 
     194                int fd; 
    221195                /* TRACE */ 
    222                 OUTPUT.file = LIBTRACE_FDOPEN(open( 
    223                                         libtrace->uridata, 
    224                                         O_CREAT | O_LARGEFILE | O_WRONLY, 
    225                                         S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP), filemode); 
     196                fd=open(libtrace->uridata,OPTIONS.zlib.filemode, 
     197                                        S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); 
     198                if (fd==-1) { 
     199                        trace_set_err(errno,"open(%s)",libtrace->uridata); 
     200                        return 0; 
     201                } 
     202                OUTPUT.file = LIBTRACE_FDOPEN(fd, filemode); 
    226203        } 
    227204 
     
    242219                case TRACE_OPTION_OUTPUT_COMPRESS: 
    243220                        /* E feature unavailable */ 
     221                        trace_set_err(TRACE_ERR_OPTION_UNAVAIL, 
     222                                        "zlib not supported"); 
    244223                        return -1; 
    245224#endif 
    246225                default: 
    247226                        /* E unknown feature */ 
     227                        trace_set_err(TRACE_ERR_UNKNOWN_OPTION, 
     228                                        "Unknown option"); 
    248229                        return -1; 
    249230        } 
     
    283264            if (errno == EINTR || errno==EAGAIN) 
    284265              continue; 
    285             perror("read(frame)"); 
     266 
     267            trace_set_err(errno,"read(%s)",libtrace->uridata); 
    286268            return -1; 
    287269          } 
     
    297279 
    298280        if (magic != 0xdaa1) { 
    299           printf("Magic number is BAD!\n"); 
    300           return -1; 
     281          trace_set_err(TRACE_ERR_BAD_PACKET,"magic number bad or missing"); 
     282          return -1; 
    301283        } 
    302284 
    303         if (framesize > len) { 
    304           printf("Framesize > len\n"); 
    305                 return -1; 
    306         } 
     285        /* We should deal.  this is called "snapping", but we don't yet */ 
     286        assert(framesize>len); 
    307287 
    308288        buf_ptr = (void*)((char*)buffer + sizeof (struct frame_t)); 
     
    315295            if (errno == EINTR || errno==EAGAIN) 
    316296              continue; 
    317             perror("read(frame)"); 
     297            trace_set_err(errno,"read(%s)",libtrace->uridata); 
    318298            return -1; 
    319299          } 
     
    363343         
    364344        if ((numbytes = LIBTRACE_READ(INPUT.file, buffer, sizeof(struct frame_t))) == -1) { 
    365                 perror("libtrace_read"); 
     345                libtrace_set_err(errno,"read(%s)",packet->trace->uridata); 
    366346                return -1; 
    367347        } 
     
    378358         
    379359        if ((numbytes=LIBTRACE_READ(INPUT.file, buffer2, size)) != size) { 
    380                 perror("libtrace read"); 
     360                libtrace_set_err(errno,"read(%s)",packet->trace->uridata); 
    381361                return -1; 
    382362        } 
     
    392372        int numbytes =0 ; 
    393373        if (packet->trace->format != &wag_trace) { 
    394                 fprintf(stderr,"Cannot convert from wag trace format to %s format yet\n", 
     374                trace_set_err(TRACE_ERR_NO_CONVERSION, 
     375                                "Cannot convert from wag trace format to %s format yet", 
    395376                                packet->trace->format->name); 
    396377                return -1; 
     
    400381         * to read from the header and payload pointers 
    401382         */ 
    402         if ((numbytes = LIBTRACE_WRITE(OUTPUT.file, packet->header, trace_get_framing_length(packet))) == 0) { 
    403                 perror("libtrace_write"); 
     383        if ((numbytes = LIBTRACE_WRITE(OUTPUT.file, packet->header, trace_get_framing_length(packet))) == -1) { 
     384                libtrace_set_err(errno,"write(%s)",packet->trace->uridata); 
    404385                return -1; 
    405386        } 
    406387        if ((numbytes = LIBTRACE_WRITE(OUTPUT.file, packet->payload,  
    407388                        packet->size - trace_get_framing_length(packet))) == 0) { 
    408                 perror("libtrace_write"); 
     389                libtrace_set_err(errno,"write(%s)",packet->trace->uridata); 
    409390                return -1; 
    410391        } 
  • trunk/lib/libtrace.h

    r590 r591  
    101101 
    102102/** Enumeration of error codes */ 
    103 enum {E_NOERROR, E_BAD_FORMAT, E_NO_INIT, E_NO_INIT_OUT, E_URI_LONG, E_URI_NOCOLON, E_INIT_FAILED }; 
     103enum { 
     104        TRACE_ERR_NOERROR       = 0, 
     105        TRACE_ERR_BAD_FORMAT    = -1, 
     106        TRACE_ERR_NO_INIT       = -2, 
     107        TRACE_ERR_NO_INIT_OUT   = -3, 
     108        TRACE_ERR_URI_LONG      = -4, 
     109        TRACE_ERR_URI_NOCOLON   = -5, 
     110        TRACE_ERR_INIT_FAILED   = -6, 
     111        TRACE_ERR_UNKNOWN_OPTION= -7, 
     112        TRACE_ERR_NO_CONVERSION = -8, 
     113        TRACE_ERR_BAD_PACKET    = -9, 
     114        TRACE_ERR_OPTION_UNAVAIL= -10 
     115}; 
    104116 
    105117/** @name Packet structures 
     
    275287char *trace_get_output_format(const libtrace_out_t *libtrace); 
    276288 
    277 /** Prints error information 
    278  * 
    279  * Prints out a descriptive error message for the currently set trace_err value 
    280  */ 
    281 void trace_perror(const char *caller); 
    282  
    283289/** @name Creation and destruction of traces 
    284290 * These members deal with creating, configuring and cleaning up a trace object 
  • trunk/lib/libtrace_int.h

    r590 r591  
    8989} trace_err; 
    9090 
     91void trace_set_error(int errcode,const char *msg,...); 
    9192 
    9293#define RP_BUFSIZE 65536 
     
    115116        char *uridata; 
    116117        struct tracefifo_t *fifo;    
    117  
     118        struct libtrace_filter_t *filter; /**< used by libtrace if the module 
     119                                            * doesn't support filters natively 
     120                                            */ 
     121        int snaplen;                    /**< used by libtrace if the module 
     122                                          * doesn't support snapping natively 
     123                                          */ 
    118124}; 
    119125 
  • trunk/lib/trace.c

    r590 r591  
    183183                } 
    184184        } 
    185 } 
    186  
    187 /* Prints error information 
    188  * 
    189  * Prints out a descriptive error message for the currently set trace_err value 
    190  */ 
    191 void trace_perror(const char *caller) { 
    192         switch (trace_err.err_num) { 
    193                 case E_BAD_FORMAT: 
    194                         fprintf(stderr, "%s: No support for format (%s)\n", caller, trace_err.problem); 
    195                         break; 
    196                 case E_NO_INIT: 
    197                         fprintf(stderr, "%s: Format (%s) does not have an init_trace function defined\n", caller, trace_err.problem); 
    198                         break; 
    199                 case E_NO_INIT_OUT: 
    200                         fprintf(stderr, "%s: Format (%s) does not have an init_output function defined\n", caller, trace_err.problem); 
    201                         break; 
    202                 case E_URI_LONG: 
    203                         fprintf(stderr, "%s: uri is too long\n", caller); 
    204                         break; 
    205                 case E_URI_NOCOLON: 
    206                         fprintf(stderr, "%s: A uri must contain at least one colon e.g. format:destination\n", caller); 
    207                         break; 
    208                 case E_INIT_FAILED: 
    209                         fprintf(stderr, "%s: libtrace failed to initialise (%s)\n",caller,trace_err.problem); 
    210                          
    211                 default: 
    212                         fprintf(stderr, "Unknown errcode %d\n",trace_err.err_num); 
    213                         break;   
    214         } 
    215         trace_err.err_num = E_NOERROR; 
    216185} 
    217186 
     
    262231        int i = 0; 
    263232         
    264         trace_err.err_num = E_NOERROR; 
     233        trace_err.err_num = TRACE_ERR_NOERROR; 
    265234         
    266235        /* parse the URI to determine what sort of event we are dealing with */ 
     
    270239         
    271240        libtrace->event.tdelta = 0.0; 
     241        libtrace->filter = NULL; 
     242        libtrace->snaplen = 0; 
    272243 
    273244        libtrace->format = 0; 
     
    282253        } 
    283254        if (libtrace->format == 0) { 
    284                 trace_err.err_num = E_BAD_FORMAT; 
    285                 strcpy(trace_err.problem, scan); 
     255                trace_set_err(TRACE_ERR_BAD_FORMAT, 
     256                                "Unknown format (%s)",scan); 
    286257                return 0; 
    287258        } 
     
    294265        if (libtrace->format->init_input) { 
    295266                if (!libtrace->format->init_input( libtrace)) { 
    296                         trace_err.err_num = E_INIT_FAILED; 
    297                         strcpy(trace_err.problem, scan); 
     267                        /* init_input should call trace_set_err to set  
     268                         * the error message 
     269                         */ 
    298270                        return 0; 
    299271                } 
    300272        } else { 
    301                 trace_err.err_num = E_NO_INIT; 
    302                 strcpy(trace_err.problem, scan); 
     273                trace_set_err(TRACE_ERR_NO_INIT, 
     274                                "Format does not support input (%s)",scan); 
    303275                return 0; 
    304276        } 
     
    316288 * @returns opaque pointer to a (sparsely initialised) libtrace_t 
    317289 * 
    318  * IMPORTANT: Do not attempt to call trace_read_packet or other such functions with 
    319  * the dummy trace. Its intended purpose is to act as a packet->trace for libtrace_packet_t's 
    320  * that are not associated with a libtrace_t structure. 
     290 * IMPORTANT: Do not attempt to call trace_read_packet or other such functions 
     291 * with the dummy trace. Its intended purpose is to act as a packet->trace for 
     292 * libtrace_packet_t's that are not associated with a libtrace_t structure. 
    321293 */ 
    322294struct libtrace_t * trace_create_dead (const char *uri) { 
     
    326298        int i; 
    327299         
    328         trace_err.err_num = E_NOERROR; 
     300        trace_err.err_num = TRACE_ERR_NOERROR; 
    329301 
    330302        if((uridata = strchr(uri,':')) == NULL) { 
     
    346318        } 
    347319        if (libtrace->format == 0) { 
    348                 trace_err.err_num = E_BAD_FORMAT; 
    349                 strcpy(trace_err.problem, scan); 
     320                trace_set_err(TRACE_ERR_BAD_FORMAT, 
     321                                "Unknown format (%s)",scan); 
    350322                return 0; 
    351323        } 
     
    359331 * 
    360332 * @param uri   the uri string describing the output format and destination 
    361  * @returns opaque pointer to a libtrace_output_t 
     333 * @returns opaque pointer to a libtrace_output_t  
    362334 * @author Shane Alcock 
    363335 * 
    364  * Valid URI's are: 
    365  *  - gzerf:/path/to/erf/file.gz 
    366  *  - gzerf:/path/to/erf/file 
    367  *  - rtserver:hostname 
    368  *  - rtserver:hostname:port 
    369  * 
    370  *  If an error occured when attempting to open the output trace, NULL is returned  
    371  *  and trace_errno is set. Use trace_perror() to get more information 
     336 * Valid URI's are: - gzerf:/path/to/erf/file.gz - gzerf:/path/to/erf/file - 
     337 * rtserver:hostname - rtserver:hostname:port 
     338 * 
     339 *  If an error occured when attempting to open the output trace, NULL is 
     340 *  returned and trace_errno is set.  
    372341 */ 
    373342         
     
    379348        int i; 
    380349 
    381         trace_err.err_num = E_NOERROR; 
     350        trace_err.err_num = TRACE_ERR_NOERROR; 
    382351        /* parse the URI to determine what sort of event we are dealing with */ 
    383352 
     
    398367        } 
    399368        if (libtrace->format == 0) { 
    400                 trace_err.err_num = E_BAD_FORMAT; 
    401                 strcpy(trace_err.problem, scan); 
     369                trace_set_err(TRACE_ERR_BAD_FORMAT, 
     370                                "Unknown output format (%s)",scan); 
    402371                return 0; 
    403372        } 
     
    414383                } 
    415384        } else { 
    416                 trace_err.err_num = E_NO_INIT_OUT; 
    417                 strcpy(trace_err.problem, scan); 
     385                trace_set_err(TRACE_ERR_NO_INIT_OUT, 
     386                                "Format does not support writing (%s)",scan); 
    418387                return 0; 
    419388        } 
     
    542511 
    543512        if (libtrace->format->read_packet) { 
    544                 return (packet->size=libtrace->format->read_packet(libtrace,packet)); 
     513                do { 
     514                        packet->size=libtrace->format->read_packet(libtrace,packet); 
     515                        if (packet->size==-1) 
     516                                return packet->size; 
     517                        if (libtrace->filter) { 
     518                                /* If the filter doesn't match, read another 
     519                                 * packet 
     520                                 */ 
     521                                if (!trace_bpf_filter(libtrace->filter,packet)){ 
     522                                        continue; 
     523                                } 
     524                        } 
     525                        if (libtrace->snaplen>0) { 
     526                                /* Snap the packet */ 
     527                                trace_set_capture_length(packet, 
     528                                                libtrace->snaplen); 
     529                        } 
     530 
     531                        return packet->size; 
     532                } while(1); 
    545533        } 
    546534        packet->size=-1; 
     
    14991487        if((uridata = strchr(uri,':')) == NULL) { 
    15001488                /* badly formed URI - needs a : */ 
    1501                 trace_err.err_num = E_URI_NOCOLON; 
     1489                trace_set_err(TRACE_ERR_URI_NOCOLON,"Format missing"); 
    15021490                return 0; 
    15031491        } 
     
    15051493        if ((uridata - uri) > URI_PROTO_LINE) { 
    15061494                /* badly formed URI - uri type is too long */ 
    1507                 trace_err.err_num = E_URI_LONG; 
     1495                trace_set_err(TRACE_ERR_URI_LONG,"Format too long"); 
    15081496                return 0; 
    15091497        } 
     
    15171505} 
    15181506         
     1507/** Update the libtrace error 
     1508 * @param errcode either an Econstant from libc, or a LIBTRACE_ERROR 
     1509 * @param msg a plaintext error message 
     1510 */ 
     1511void trace_set_error(int errcode,const char *msg,...) 
     1512{ 
     1513        va_list va; 
     1514        va_start(msg,va); 
     1515        trace_err.err_num=errcode; 
     1516        vsnprintf(trace_err.problem,sizeof(trace_err.problem),msg,va); 
     1517        va_end(); 
     1518} 
Note: See TracChangeset for help on using the changeset viewer.