Changeset df338b3
- Timestamp:
- 02/20/06 13:52:09 (15 years ago)
- 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:
- 8ee7caa
- Parents:
- 548a9c2
- Location:
- lib
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/format_pcap.c
r3d4d52d rdf338b3 69 69 static struct libtrace_format_t pcapint; 70 70 71 72 #define CONNINFO libtrace->format_data->conn_info 71 #define DATA(x) ((struct libtrace_format_data_t*)((x)->format_data)) 72 73 73 #define INPUT libtrace->format_data->input 74 74 #define OUTPUT libtrace->format_data->output … … 83 83 pcap_t *pcap; 84 84 } input; 85 int snaplen; 86 libtrace_filter_t *filter; 87 int promisc; 85 88 }; 86 89 … … 100 103 101 104 static int pcap_init_input(struct libtrace_t *libtrace) { 102 char errbuf[PCAP_ERRBUF_SIZE];103 struct stat buf;104 105 libtrace->format_data = (struct libtrace_format_data_t *) 105 106 malloc(sizeof(struct libtrace_format_data_t)); 106 CONNINFO.path = libtrace->uridata; 107 108 if (!strncmp(CONNINFO.path,"-",1)) { 109 if ((INPUT.pcap = 110 pcap_open_offline(CONNINFO.path, 111 errbuf)) == NULL) { 112 trace_set_err(TRACE_ERR_INIT_FAILED,"%s",errbuf); 107 108 INPUT.pcap = NULL; 109 DATA(libtrace)->filter = NULL; 110 DATA(libtrace)->snaplen = 0; 111 DATA(libtrace)->promisc = 0; 112 113 return 1; 114 } 115 116 static int pcap_start_input(struct libtrace_t *libtrace) { 117 char errbuf[PCAP_ERRBUF_SIZE]; 118 if ((INPUT.pcap = 119 pcap_open_offline(libtrace->uridata, 120 errbuf)) == NULL) { 121 trace_set_err(TRACE_ERR_INIT_FAILED,"%s", 122 errbuf); 123 return -1; 124 } 125 if (DATA(libtrace)->filter) { 126 trace_bpf_compile(DATA(libtrace)->filter); 127 if (pcap_setfilter(INPUT.pcap,&DATA(libtrace)->filter->filter) 128 == -1) { 129 trace_set_err(TRACE_ERR_INIT_FAILED,"%s", 130 pcap_geterr(INPUT.pcap)); 131 return -1; 132 } 133 } 134 return 0; 135 } 136 137 static int pcap_config_input(libtrace_t *libtrace, 138 trace_option_t option, 139 void *data) 140 { 141 switch(option) { 142 case TRACE_OPTION_FILTER: 143 DATA(libtrace)->filter=data; 113 144 return 0; 114 } 115 } else { 116 if (stat(CONNINFO.path,&buf) == -1) { 117 trace_set_err(errno,"stat(%s)",CONNINFO.path); 118 return 0; 119 } 120 if (S_ISCHR(buf.st_mode)) { 121 if ((INPUT.pcap = 122 pcap_open_live(CONNINFO.path, 123 4096, 124 1, 125 1, 126 errbuf)) == NULL) { 127 trace_set_err(TRACE_ERR_INIT_FAILED,"%s", 128 errbuf); 129 return 0; 130 } 131 } else { 132 if ((INPUT.pcap = 133 pcap_open_offline(CONNINFO.path, 134 errbuf)) == NULL) { 135 trace_set_err(TRACE_ERR_INIT_FAILED,"%s", 136 errbuf); 137 return 0; 138 } 139 } 140 } 141 return 1; 142 145 case TRACE_OPTION_SNAPLEN: 146 /* Snapping isn't supported directly, so fall thru 147 * and let libtrace deal with it 148 */ 149 case TRACE_OPTION_PROMISC: 150 /* can't do promisc on a trace! fall thru */ 151 default: 152 trace_set_err(TRACE_ERR_UNKNOWN_OPTION, 153 "Unknown option %i", option); 154 return -1; 155 } 156 assert(0); 157 } 158 159 static int pcap_pause_input(libtrace_t *libtrace) { 160 pcap_close(INPUT.pcap); 161 INPUT.pcap=NULL; 143 162 } 144 163 … … 146 165 libtrace->format_data = (struct libtrace_format_data_out_t *) 147 166 malloc(sizeof(struct libtrace_format_data_out_t)); 148 CONNINFO.path = libtrace->uridata;149 167 OUTPUT.trace.pcap = NULL; 150 168 OUTPUT.trace.dump = NULL; … … 153 171 154 172 static int pcapint_init_input(struct libtrace_t *libtrace) { 155 char errbuf[PCAP_ERRBUF_SIZE];156 173 libtrace->format_data = (struct libtrace_format_data_t *) 157 174 malloc(sizeof(struct libtrace_format_data_t)); 158 CONNINFO.path = libtrace->uridata; 175 DATA(libtrace)->filter = NULL; 176 DATA(libtrace)->snaplen = 0; 177 DATA(libtrace)->promisc = 0; 178 return 1; 179 } 180 181 static int pcapint_config_input(libtrace_t *libtrace, 182 trace_option_t option, 183 void *data) 184 { 185 switch(option) { 186 case TRACE_OPTION_FILTER: 187 DATA(libtrace)->filter=data; 188 return 0; 189 case TRACE_OPTION_SNAPLEN: 190 DATA(libtrace)->snaplen=*(int*)data; 191 return 0; 192 case TRACE_OPTION_PROMISC: 193 DATA(libtrace)->promisc=*(int*)data; 194 return 0; 195 default: 196 trace_set_err(TRACE_ERR_UNKNOWN_OPTION, 197 "Unknown option %i", option); 198 return -1; 199 } 200 assert(0); 201 } 202 203 static int pcapint_start_input(libtrace_t *libtrace) { 204 char errbuf[PCAP_ERRBUF_SIZE]; 159 205 if ((INPUT.pcap = 160 pcap_open_live( CONNINFO.path,161 4096,162 1,206 pcap_open_live(libtrace->uridata, 207 DATA(libtrace)->snaplen, 208 DATA(libtrace)->promisc, 163 209 1, 164 210 errbuf)) == NULL) { 165 211 trace_set_err(TRACE_ERR_INIT_FAILED,"%s",errbuf); 212 /* Set a filter if one is defined */ 213 if (DATA(libtrace)->filter) 214 if (pcap_setfilter(INPUT.pcap,&DATA(libtrace)->filter->filter) 215 == -1) { 216 trace_set_err(TRACE_ERR_INIT_FAILED,"%s", 217 pcap_geterr(INPUT.pcap)); 218 return -1; 219 } 166 220 return 0; 167 221 } … … 228 282 libtrace_to_pcap_dlt(trace_get_link_type(packet)), 229 283 65536); 230 OUTPUT.trace.dump = pcap_dump_open(OUTPUT.trace.pcap,CONNINFO.path); 284 OUTPUT.trace.dump = pcap_dump_open(OUTPUT.trace.pcap, 285 libtrace->uridata); 231 286 fflush((FILE *)OUTPUT.trace.dump); 232 287 } … … 394 449 pcap_init_input, /* init_input */ 395 450 NULL, /* config_input */ 396 NULL,/* start_input */397 NULL,/* pause_input */451 pcap_start_input, /* start_input */ 452 pcap_pause_input, /* pause_input */ 398 453 pcap_init_output, /* init_output */ 399 454 NULL, /* config_output */ … … 426 481 "pcap", 427 482 pcapint_init_input, /* init_input */ 428 NULL,/* config_input */429 NULL,/* start_input */430 NULL,/* pause_input */483 pcapint_config_input, /* config_input */ 484 pcapint_start_input, /* start_input */ 485 pcap_pause_input, /* pause_input */ 431 486 pcapint_init_output, /* init_output */ 432 487 NULL, /* config_output */ -
lib/libtrace_int.h
r3d4d52d rdf338b3 190 190 char libtrace_to_erf_type(libtrace_linktype_t linktype); 191 191 192 #if HAVE_BPF 193 /* A type encapsulating a bpf filter 194 * This type covers the compiled bpf filter, as well as the original filter 195 * string 196 * 197 */ 198 struct libtrace_filter_t { 199 struct bpf_program filter; 200 int flag; 201 char * filterstring; 202 }; 203 #endif 192 204 193 205 #ifdef __cplusplus -
lib/trace.c
r65a5900 rdf338b3 120 120 #define MAXOPTS 1024 121 121 122 #if HAVE_BPF123 /* A type encapsulating a bpf filter124 * This type covers the compiled bpf filter, as well as the original filter125 * string126 *127 */128 struct libtrace_filter_t {129 struct bpf_insn *filter;130 char * filterstring;131 };132 #endif133 122 134 123 struct trace_err_t trace_err; … … 405 394 int trace_start(struct libtrace_t *libtrace) 406 395 { 396 assert(libtrace); 407 397 if (libtrace->format->start_input) { 408 398 int ret=libtrace->format->start_input(libtrace); … … 543 533 do { 544 534 packet->size=libtrace->format->read_packet(libtrace,packet); 545 if (packet->size==-1 )535 if (packet->size==-1 || packet->size==0) 546 536 return packet->size; 547 537 if (libtrace->filter) { … … 1252 1242 struct libtrace_filter_t *filter = malloc(sizeof(struct libtrace_filter_t)); 1253 1243 filter->filterstring = strdup(filterstring); 1254 filter->f ilter= 0;1244 filter->flag = 0; 1255 1245 return filter; 1256 1246 #else 1257 1247 fprintf(stderr,"This version of libtrace does not have bpf filter support\n"); 1258 1248 return 0; 1249 #endif 1250 } 1251 1252 /* compile a bpf filter, now we know what trace it's on 1253 * @internal 1254 * 1255 * @returns -1 on error, 0 on success 1256 */ 1257 int trace_bpf_compile(libtrace_filter_t *filter, 1258 const libtrace_packet_t *packet ) { 1259 #if HAVE_BPF 1260 void *linkptr = 0; 1261 assert(filter); 1262 1263 /* If this isn't a real packet, then fail */ 1264 linkptr = trace_get_link(packet); 1265 if (!linkptr) { 1266 return -1; 1267 } 1268 1269 if (filter->filterstring && ! filter->flag) { 1270 pcap_t *pcap; 1271 pcap=(pcap_t *)pcap_open_dead( 1272 libtrace_to_pcap_dlt(trace_get_link_type(packet)), 1273 1500); 1274 /* build filter */ 1275 if (pcap_compile( pcap, &filter->filter, filter->filterstring, 1276 1, 0)) { 1277 pcap_close(pcap); 1278 return -1; 1279 } 1280 pcap_close(pcap); 1281 filter->flag=1; 1282 } 1283 return 0; 1284 #else 1285 assert(!"This should never be called when BPF not enabled"); 1259 1286 #endif 1260 1287 } … … 1277 1304 return 0; 1278 1305 } 1306 1307 /* We need to compile it now, because before we didn't know what the 1308 * link type was 1309 */ 1310 trace_bpf_compile(filter,packet); 1279 1311 1280 1312 clen = trace_get_capture_length(packet); 1281 1282 1283 if (filter->filterstring && ! filter->filter) { 1284 pcap_t *pcap; 1285 struct bpf_program bpfprog; 1286 pcap=(pcap_t *)pcap_open_dead( 1287 libtrace_to_pcap_dlt(trace_get_link_type(packet)), 1288 1500); 1289 /* build filter */ 1290 if (pcap_compile( pcap, &bpfprog, filter->filterstring, 1, 0)) { 1291 printf("bpf compilation error: %s: %s\n", 1292 pcap_geterr(pcap),filter->filterstring); 1293 assert(0); 1294 } 1295 pcap_close(pcap); 1296 filter->filter = bpfprog.bf_insns; 1297 } 1298 1299 assert(filter->filter); 1300 return bpf_filter(filter->filter, linkptr, clen, clen); 1313 1314 assert(filter->flag); 1315 return bpf_filter(filter->filter.bf_insns, linkptr, clen, clen); 1301 1316 #else 1302 1317 fprintf(stderr,"This version of libtrace does not have bpf filter support\n");
Note: See TracChangeset
for help on using the changeset viewer.