Changeset 725


Ignore:
Timestamp:
04/23/06 13:48:20 (7 years ago)
Author:
perry
Message:

Fixed the format module auditing code in trace.c to work with the new linked
list.
Did some more tidyups on format_linux, it now supports get_capture_length()
properly, does snapping and configurable promisc.

Location:
trunk/lib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/format_linux.c

    r719 r725  
    5454struct libtrace_format_data_t { 
    5555        int fd; 
     56        int snaplen; 
     57        int promisc; 
    5658}; 
    5759 
     
    5961        struct timeval ts; 
    6062        int wirelen; 
     63        int caplen; 
    6164        struct sockaddr_ll hdr; 
    6265}; 
     
    6467#define FORMAT(x) ((struct libtrace_format_data_t*)(x)) 
    6568 
    66 static int linuxnative_init_input(struct libtrace_t *libtrace) { 
    67         struct sockaddr_ll addr; 
     69static int linuxnative_init_input(libtrace_t *libtrace)  
     70{ 
    6871        libtrace->format_data = (struct libtrace_format_data_t *) 
    6972                malloc(sizeof(struct libtrace_format_data_t)); 
     73        FORMAT(libtrace->format_data)->fd = -1; 
     74        FORMAT(libtrace->format_data)->promisc = 0; 
     75        FORMAT(libtrace->format_data)->snaplen = 65536; 
     76 
     77        return 0; 
     78} 
     79 
     80static int linuxnative_start_input(libtrace_t *libtrace) 
     81{ 
     82        struct sockaddr_ll addr; 
    7083        FORMAT(libtrace->format_data)->fd =  
    7184                                socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); 
     
    109122} 
    110123 
    111 static int linuxnative_fin_input(struct libtrace_t *libtrace) { 
     124static int linuxnative_pause_input(libtrace_t *libtrace) 
     125{ 
    112126        close(FORMAT(libtrace->format_data)->fd); 
     127        FORMAT(libtrace->format_data)->fd=-1; 
     128 
     129        return 0; 
     130} 
     131 
     132static int linuxnative_fin_input(libtrace_t *libtrace)  
     133{ 
    113134        free(libtrace->format_data); 
    114135        return 0; 
    115136} 
    116137 
    117 static int linuxnative_read_packet(struct libtrace_t *libtrace, struct libtrace_packet_t *packet) { 
     138static int linuxnative_config_input(libtrace_t *libtrace, 
     139                trace_option_t option, 
     140                void *data) 
     141{ 
     142        switch(option) { 
     143                case TRACE_OPTION_SNAPLEN: 
     144                        FORMAT(libtrace->format_data)->snaplen=*(int*)data; 
     145                        return 0; 
     146                case TRACE_OPTION_PROMISC: 
     147                        FORMAT(libtrace->format_data)->promisc=*(int*)data; 
     148                        return 0; 
     149                case TRACE_OPTION_FILTER: 
     150                        /* We don't support bpf filters in any special way 
     151                         * so return an error and let libtrace deal with 
     152                         * emulating it 
     153                         */ 
     154                        break; 
     155                /* Avoid default: so that future options will cause a warning 
     156                 * here to remind us to implement it, or flag it as 
     157                 * unimplementable 
     158                 */ 
     159        } 
     160        trace_set_err(libtrace,TRACE_ERR_UNKNOWN_OPTION, 
     161                        "Unknown option %i", option); 
     162        return -1; 
     163} 
     164 
     165#define LIBTRACE_MIN(a,b) ((a)<(b) ? (a) : (b)) 
     166 
     167static int linuxnative_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { 
    118168        struct libtrace_linuxnative_header *hdr; 
    119169        socklen_t socklen; 
     170        int snaplen; 
    120171        if (!packet->buffer || packet->buf_control == TRACE_CTRL_EXTERNAL) { 
    121172                packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE); 
     
    129180        hdr=(void*)packet->buffer; 
    130181        socklen=sizeof(hdr->hdr); 
     182        snaplen=LIBTRACE_MIN( 
     183                        (int)LIBTRACE_PACKET_BUFSIZE-(int)sizeof(*hdr), 
     184                        (int)FORMAT(libtrace->format_data)->snaplen); 
    131185        hdr->wirelen = recvfrom(FORMAT(libtrace->format_data)->fd, 
    132186                        (void*)packet->payload, 
    133                         LIBTRACE_PACKET_BUFSIZE-sizeof(*hdr), 
     187                        snaplen, 
    134188                        MSG_TRUNC, 
    135189                        (void *)&hdr->hdr, 
     
    138192        if (hdr->wirelen==-1) 
    139193                return -1; 
     194 
     195        hdr->caplen=LIBTRACE_MIN(snaplen,hdr->wirelen); 
    140196 
    141197        if (ioctl(FORMAT(libtrace->format_data)->fd,SIOCGSTAMP,&hdr->ts)==-1) 
     
    166222} 
    167223 
    168 static struct timeval linuxnative_get_timeval(const struct libtrace_packet_t *packet) {  
     224static struct timeval linuxnative_get_timeval(const libtrace_packet_t *packet)  
     225{ 
    169226        return ((struct libtrace_linuxnative_header*)(packet->buffer))->ts; 
    170227} 
    171228 
    172 static int linuxnative_get_wire_length(const struct libtrace_packet_t *packet) { 
     229static int linuxnative_get_capture_length(const libtrace_packet_t *packet) 
     230{ 
     231        return ((struct libtrace_linuxnative_header*)(packet->buffer))->caplen; 
     232} 
     233 
     234static int linuxnative_get_wire_length(const libtrace_packet_t *packet)  
     235{ 
    173236        return ((struct libtrace_linuxnative_header*)(packet->buffer))->wirelen; 
    174237} 
    175238 
    176 static int linuxnative_get_framing_length(const struct libtrace_packet_t *packet) { 
     239static int linuxnative_get_framing_length(const libtrace_packet_t *packet)  
     240{ 
    177241        return sizeof(struct libtrace_linuxnative_header); 
    178242} 
     
    197261        TRACE_FORMAT_LINUX_NATIVE, 
    198262        linuxnative_init_input,         /* init_input */ 
    199         NULL,                           /* config_input */ 
    200         NULL,                           /* start_input */ 
    201         NULL,                           /* pause_input */ 
     263        linuxnative_config_input,       /* config_input */ 
     264        linuxnative_start_input,        /* start_input */ 
     265        linuxnative_pause_input,        /* pause_input */ 
    202266        NULL,                           /* init_output */ 
    203267        NULL,                           /* config_output */ 
     
    217281        NULL,                           /* seek_timeval */ 
    218282        NULL,                           /* seek_seconds */ 
    219         NULL,                           /* get_capture_length */ 
     283        linuxnative_get_capture_length, /* get_capture_length */ 
    220284        linuxnative_get_wire_length,    /* get_wire_length */ 
    221285        linuxnative_get_framing_length, /* get_framing_length */ 
    222286        NULL,                           /* set_capture_length */ 
    223287        linuxnative_get_fd,             /* get_fd */ 
    224         trace_event_trace,              /* trace_event */ 
     288        trace_event_device,             /* trace_event */ 
    225289        linuxnative_help,               /* help */ 
    226290        NULL 
  • trunk/lib/trace.c

    r724 r725  
    124124        f->next=formats_list; 
    125125        formats_list=f; 
    126         /* Now, verify things */ 
     126        /* Now, verify things  
     127         * This #if can be changed to a 1 to output warnings about inconsistant 
     128         * functions being provided by format modules.  This generally is very 
     129         * noisy, as almost all modules don't implement one or more functions 
     130         * for various reasons.  This is very useful when checking a new  
     131         * format module is sane. 
     132         */  
    127133#if 0 
    128         if (format_list[nformats]->init_input) { 
     134        if (f->init_input) { 
    129135#define REQUIRE(x) \ 
    130                 if (!format_list[nformats]->x) \ 
    131                         fprintf(stderr,"%s: Input format should provide " #x "\n",format_list[nformats]->name) 
     136                if (!f->x) \ 
     137                        fprintf(stderr,"%s: Input format should provide " #x "\n",f->name) 
    132138                REQUIRE(read_packet); 
    133139                REQUIRE(start_input); 
    134                 REQUIRE(config_input); 
    135                 REQUIRE(pause_input); 
    136140                REQUIRE(fin_input); 
    137141                REQUIRE(get_link_type); 
     
    140144                REQUIRE(get_framing_length); 
    141145                REQUIRE(trace_event); 
    142                 if (!format_list[nformats]->get_erf_timestamp  
    143                         && !format_list[nformats]->get_seconds 
    144                         && !format_list[nformats]->get_timeval) { 
     146                if (!f->get_erf_timestamp  
     147                        && !f->get_seconds 
     148                        && !f->get_timeval) { 
    145149                        fprintf(stderr,"%s: A trace format capable of input, should provide at least one of\n" 
    146 "get_erf_timestamp, get_seconds or trace_timeval\n",format_list[nformats]->name); 
    147                 } 
    148                 if (format_list[nformats]->trace_event==trace_event_device) { 
     150"get_erf_timestamp, get_seconds or trace_timeval\n",f->name); 
     151                } 
     152                if (f->trace_event!=trace_event_trace) { 
     153                        /* Theres nothing that a trace file could optimise with 
     154                         * config_input 
     155                         */ 
     156                        REQUIRE(pause_input); 
     157                        REQUIRE(config_input); 
    149158                        REQUIRE(get_fd); 
    150159                } 
    151160                else { 
    152                         if (format_list[nformats]->get_fd) { 
     161                        if (f->get_fd) { 
    153162                                fprintf(stderr,"%s: Unnecessary get_fd\n", 
    154                                                 format_list[nformats]->name); 
     163                                                f->name); 
    155164                        } 
    156165                } 
     
    159168        else { 
    160169#define REQUIRE(x) \ 
    161                 if (format_list[nformats]->x) \ 
    162                         fprintf(stderr,"%s: Non Input format shouldn't need " #x "\n",format_list[nformats]->name) 
     170                if (f->x) \ 
     171                        fprintf(stderr,"%s: Non Input format shouldn't need " #x "\n",f->name) 
    163172                REQUIRE(read_packet); 
    164173                REQUIRE(start_input); 
     
    175184#undef REQUIRE 
    176185        } 
    177         if (format_list[nformats]->init_output) { 
     186        if (f->init_output) { 
    178187#define REQUIRE(x) \ 
    179                 if (!format_list[nformats]->x) \ 
    180                         fprintf(stderr,"%s: Output format should provide " #x "\n",format_list[nformats]->name) 
     188                if (!f->x) \ 
     189                        fprintf(stderr,"%s: Output format should provide " #x "\n",f->name) 
    181190                REQUIRE(write_packet); 
    182191                REQUIRE(start_output); 
     
    187196        else { 
    188197#define REQUIRE(x) \ 
    189                 if (format_list[nformats]->x) \ 
    190                         fprintf(stderr,"%s: Non Output format shouldn't need " #x "\n",format_list[nformats]->name) 
     198                if (f->x) \ 
     199                        fprintf(stderr,"%s: Non Output format shouldn't need " #x "\n",f->name) 
    191200                REQUIRE(write_packet); 
    192201                REQUIRE(start_output); 
     
    13501359{ 
    13511360        char *buf2 = buf; 
    1352         static char staticbuf[18]={0,}; 
     1361        char staticbuf[18]={0,}; 
    13531362        if (!buf2) 
    13541363                buf2=staticbuf; 
Note: See TracChangeset for help on using the changeset viewer.