- Timestamp:
- 09/13/05 11:06:34 (17 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:
- 1e66c64
- Parents:
- d3e4697
- Location:
- lib
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/libtrace.h
r5bbe424 rfba4ca0 36 36 37 37 /** API version as 3 byte hex digits, eg 0xXXYYZZ */ 38 #define LIBTRACE_API_VERSION 0x10001 438 #define LIBTRACE_API_VERSION 0x100015 39 39 40 40 #ifdef __cplusplus … … 55 55 * 56 56 * @par Usage 57 * <ol> 58 * <li> include "libtrace.h" 59 * <li> call create_trace with the uri of the trace you're interested in.<br> 60 * This is usually passed in as argv[1] to your program. 61 * <li> call libtrace_read_packet(), passing in the libtrace_t returned from 62 * create trace and a buffer (and the buffer length) 63 * <li> call getIP() on the buffer, and do whatever you need 64 * <li> loop back to step 3, until libtrace_read_packet() returns -1 65 * </ol> 57 * See the example/ directory in the source distribution for some simple examples 66 58 * @par Linking 67 59 * To use this library you need to link against libtrace by passing -ltrace … … 86 78 87 79 /** Opaque structure holding information about a packet */ 88 #define LIBTRACE_PACKET_BUFSIZE 65536 89 struct libtrace_packet_t { 90 struct libtrace_t *trace; 91 //void *buffer; 92 char buffer[LIBTRACE_PACKET_BUFSIZE]; 93 size_t size; 94 uint8_t status; 95 }; 80 struct libtrace_packet_t; 96 81 97 82 /** Enumeration of error codes */ … … 100 85 /** Structure for dealing with IP packets */ 101 86 struct libtrace_ip 102 87 { 103 88 #if BYTE_ORDER == LITTLE_ENDIAN 104 89 unsigned int ip_hl:4; /**< header length */ … … 123 108 struct in_addr ip_src; /**< source address */ 124 109 struct in_addr ip_dst; /**< dest address */ 125 };110 } __attribute__ ((__packed__)); 126 111 127 112 /** Structure for dealing with TCP packets */ … … 158 143 u_int16_t check; /**< Checksum */ 159 144 u_int16_t urg_ptr; /**< Urgent Pointer */ 160 } ;145 } __attribute__ ((__packed__)); 161 146 162 147 /** UDP Header for dealing with UDP packets */ … … 166 151 u_int16_t len; /**< Length */ 167 152 u_int16_t check; /**< Checksum */ 168 } ;153 } __attribute__ ((__packed__)); 169 154 170 155 /** ICMP Header for dealing with icmp packets */ … … 188 173 } frag; /**< path mtu discovery */ 189 174 } un; 190 }; 175 } __attribute__ ((__packed__)); 176 177 /** 802.3 frame */ 178 struct libtrace_ether 179 { 180 u_int8_t ether_dhost[6]; /* destination ether addr */ 181 u_int8_t ether_shost[6]; /* source ether addr */ 182 u_int16_t ether_type; /* packet type ID field (next-header) */ 183 } __attribute__ ((__packed__)); 191 184 192 185 /** 802.1Q frame */ … … 195 188 u_int8_t ether_dhost[6]; /* destination eth addr */ 196 189 u_int8_t ether_shost[6]; /* source ether addr */ 197 u_int16_t ether_type; 198 u_int16_t vlan_pri:3; 199 u_int16_t vlan_cfi:1; 200 u_int16_t vlan_id:12; 201 u_int16_t vlan_ether_type; 190 u_int16_t ether_type; /* packet type ID field , 0x8100 for VLAN */ 191 u_int16_t vlan_pri:3; /* vlan user priority */ 192 u_int16_t vlan_cfi:1; /* vlan format indicator, 0 for ethernet, 1 for token ring */ 193 u_int16_t vlan_id:12; /* vlan id */ 194 u_int16_t vlan_ether_type; /* vlan sub-packet type ID field (next-header)*/ 202 195 } __attribute__ ((__packed__)); 203 196 … … 211 204 /** Gets the output format for a given output trace 212 205 * 206 * @params libtrace the output trace to get the name of the format fo 207 * @returns callee-owned null-terminated char* containing the output format 208 * 213 209 */ 214 210 char *trace_get_output_format(struct libtrace_out_t *libtrace); … … 222 218 /** Create a trace file from a URI 223 219 * 220 * @params char * containing a valid libtrace URI 224 221 * @returns opaque pointer to a libtrace_t 225 222 * … … 229 226 * - erf:/path/to/rtclient/socket 230 227 * - erf:- (stdin) 231 * - dag:/dev/dagcard (implementd?)228 * - dag:/dev/dagcard 232 229 * - pcapint:pcapinterface (eg: pcap:eth0) 233 230 * - pcap:/path/to/pcap/file 234 * - pcap:/path/to/pcap/file.gz235 * - pcap:/path/to/pcap/socket (implemented?)236 231 * - pcap:- 237 232 * - rtclient:hostname 238 233 * - rtclient:hostname:port 234 * - wag:- 239 235 * - wag:/path/to/wag/file 240 236 * - wag:/path/to/wag/file.gz 241 237 * - wag:/path/to/wag/socket 242 * - wag:/dev/device243 238 * 244 239 * If an error occured when attempting to open the trace file, NULL is returned 245 * and an error is output to stdout.240 * and trace_errno is set. Use trace_perror() to get more information 246 241 */ 247 242 struct libtrace_t *trace_create(char *uri); … … 259 254 /** Creates a trace output file from a URI. 260 255 * 256 * @param uri the uri string describing the output format and destination 261 257 * @returns opaque pointer to a libtrace_output_t 258 * @author Shane Alcock 262 259 * 263 260 * Valid URI's are: … … 267 264 * - rtserver:hostname:port 268 265 * 269 * If an error occured when attempting to open the output trace, NULL is returned and270 * an error is output to stdout.266 * If an error occured when attempting to open the output trace, NULL is returned 267 * and trace_errno is set. Use trace_perror() to get more information 271 268 */ 272 269 struct libtrace_out_t *trace_output_create(char *uri); 273 270 274 /** Configures a trace output file as specified by an option string in getopt() format 275 * 276 * @param libtrace the output trace file to be configured 277 * @param options the string containing the configuration options 278 * @returns -1 if configuration fails, 0 if successful 271 /* Parses an output options string and calls the appropriate function to deal with output options. 272 * 273 * @param libtrace the output trace object to apply the options to 274 * @param options the options string 275 * @returns -1 if option configuration failed, 0 otherwise 276 * 277 * @author Shane Alcock 279 278 */ 280 279 int trace_output_config(struct libtrace_out_t *libtrace, char *options); … … 289 288 /** Close a trace output file, freeing up any resources it may have been using 290 289 * 290 * @param libtrace the output trace file to be destroyed 291 * 292 * @author Shane Alcock 291 293 */ 292 294 void trace_output_destroy(struct libtrace_out_t *trace); … … 296 298 * @param trace the libtrace opaque pointer 297 299 * @param packet the packet opaque pointer 298 * @returns false if it failed to read a packet300 * @returns 0 on EOF, negative value on error 299 301 * 300 302 */ … … 424 426 * 425 427 * @returns time that this packet was seen in 64bit floating point seconds 428 * @author Daniel Lawson 426 429 * @author Perry Lorier 427 430 */ … … 537 540 * @param trace the libtrace opaque pointer 538 541 * @param packet the libtrace_packet opaque pointer 539 * @param fd a pointer to a file descriptor to listen on540 * @param seconds a pointer the time in seconds since to the next event541 542 * @returns libtrace_event struct containing the type, and potential 542 543 * fd or seconds to sleep on … … 555 556 * @returns opaque pointer pointer to a libtrace_filter_t object 556 557 * @author Daniel Lawson 558 * @note The filter is not actually compiled at this point, so no correctness 559 * tests are performed here. trace_bpf_setfilter will always return ok, but 560 * if the filter is poorly constructed an error will be generated when the 561 * filter is actually used 557 562 */ 558 563 struct libtrace_filter_t *trace_bpf_setfilter(const char *filterstring); … … 563 568 * @returns 0 if the filter fails, 1 if it succeeds 564 569 * @author Daniel Lawson 570 * @note Due to the way BPF filters are built, the filter is not actually compiled 571 * until the first time trace_bpf_filter is called. If your filter is incorrect, it will generate an error message and assert, exiting the program. This behaviour may change to more graceful handling of this error in the future. 565 572 */ 566 573 int trace_bpf_filter(struct libtrace_filter_t *filter, … … 591 598 * @param dest the destination port from the packet 592 599 * @returns one of USE_SOURCE or USE_DEST depending on which one you should use 593 * @note ports must be in \em hostbyte order!600 * @note ports must be in \em HOST byte order! 594 601 * @author Daniel Lawson 595 602 */ -
lib/libtrace_int.h
rda17570 rfba4ca0 79 79 } trace_err; 80 80 81 /** Opaque structure holding information about a packet */ 82 #define LIBTRACE_PACKET_BUFSIZE 65536 83 struct libtrace_packet_t { 84 struct libtrace_t *trace; 85 //void *buffer; 86 char buffer[LIBTRACE_PACKET_BUFSIZE]; 87 size_t size; 88 uint8_t status; 89 }; 81 90 82 91 #define RP_BUFSIZE 65536 -
lib/trace.c
r5bbe424 rfba4ca0 30 30 31 31 32 /* *@file32 /* @file 33 33 * 34 34 * @brief Trace file processing library … … 120 120 121 121 #if HAVE_BPF 122 /* *A type encapsulating a bpf filter122 /* A type encapsulating a bpf filter 123 123 * This type covers the compiled bpf filter, as well as the original filter 124 124 * string … … 151 151 } 152 152 153 /* *Prints help information for libtrace153 /* Prints help information for libtrace 154 154 * 155 155 * Function prints out some basic help information regarding libtrace, … … 166 166 } 167 167 168 /* Prints error information 169 * 170 * Prints out a descriptive error message for the currently set trace_err value 171 */ 168 172 void trace_perror(char *caller) { 169 173 switch (trace_err.err_num) { … … 196 200 #define URI_PROTO_LINE 16 197 201 198 /* *Gets the name of the output format for a given output trace.202 /* Gets the name of the output format for a given output trace. 199 203 * 200 204 * @params libtrace the output trace to get the name of the format for 201 * @returns the output format205 * @returns callee-owned null-terminated char* containing the output format 202 206 * 203 207 */ … … 208 212 } 209 213 210 /** Create a trace file from a URI 211 * 214 /* Create a trace file from a URI 215 * 216 * @params char * containing a valid libtrace URI 212 217 * @returns opaque pointer to a libtrace_t 213 218 * … … 217 222 * erf:/path/to/rtclient/socket 218 223 * erf:- (stdin) 224 * dag:/dev/dagcard 219 225 * pcapint:pcapinterface (eg: pcapint:eth0) 220 226 * pcap:/path/to/pcap/file … … 226 232 * wag:/path/to/wag/file.gz 227 233 * wag:/path/to/wag/socket 228 * wagint:/dev/device229 *230 * URIs which have yet to be implemented are:231 * dag:/dev/dagcard232 * pcap:/path/to/pcap/socket233 234 * 234 235 * If an error occured when attempting to open a trace, NULL is returned … … 293 294 } 294 295 296 /* Creates a "dummy" trace file that has only the format type set. 297 * 298 * @returns opaque pointer to a (sparsely initialised) libtrace_t 299 * 300 * IMPORTANT: Do not attempt to call trace_read_packet or other such functions with 301 * the dummy trace. Its intended purpose is to act as a packet->trace for libtrace_packet_t's 302 * that are not associated with a libtrace_t structure. 303 */ 295 304 struct libtrace_t * trace_create_dead (char *uri) { 296 305 struct libtrace_t *libtrace = malloc(sizeof(struct libtrace_t)); … … 327 336 328 337 } 329 330 /** Creates a libtrace_out_t structure and the socket / file through which output will be directed. 331 * 332 * @param uri the uri string describing the output format and the destination 333 * @returns the newly created libtrace_out_t structure 334 * 338 339 /** Creates a trace output file from a URI. 340 * 341 * @param uri the uri string describing the output format and destination 342 * @returns opaque pointer to a libtrace_output_t 335 343 * @author Shane Alcock 336 * */ 344 * 345 * Valid URI's are: 346 * - gzerf:/path/to/erf/file.gz 347 * - gzerf:/path/to/erf/file 348 * - rtserver:hostname 349 * - rtserver:hostname:port 350 * 351 * If an error occured when attempting to open the output trace, NULL is returned 352 * and trace_errno is set. Use trace_perror() to get more information 353 */ 354 337 355 struct libtrace_out_t *trace_output_create(char *uri) { 338 356 struct libtrace_out_t *libtrace = malloc(sizeof(struct libtrace_out_t)); … … 388 406 } 389 407 390 /* *Parses an output options string and calls the appropriate function to deal with output options.408 /* Parses an output options string and calls the appropriate function to deal with output options. 391 409 * 392 410 * @param libtrace the output trace object to apply the options to … … 415 433 } 416 434 417 /* *Close a trace file, freeing up any resources it may have been using435 /* Close a trace file, freeing up any resources it may have been using 418 436 * 419 437 */ … … 432 450 free(libtrace); 433 451 } 434 /* *Close an output trace file, freeing up any resources it may have been using452 /* Close an output trace file, freeing up any resources it may have been using 435 453 * 436 454 * @param libtrace the output trace file to be destroyed … … 446 464 } 447 465 448 /* *Read one packet from the trace into buffer466 /* Read one packet from the trace into buffer 449 467 * 450 468 * @param libtrace the libtrace opaque pointer 451 469 * @param packet the packet opaque pointer 452 * @returns false if it failed to read a packet470 * @returns 0 on EOF, negative value on error 453 471 * 454 472 */ … … 470 488 } 471 489 472 /* *Writes a packet to the specified output490 /* Writes a packet to the specified output 473 491 * 474 492 * @param libtrace describes the output format, destination, etc. … … 488 506 } 489 507 490 /* *get a pointer to the link layer508 /* get a pointer to the link layer 491 509 * @param packet a pointer to a libtrace_packet structure 492 510 * 493 511 * @returns a pointer to the link layer, or NULL if there is no link layer 494 * you should call trace_get_link_type() to find out what type of link layer this is 512 * 513 * @note you should call trace_get_link_type() to find out what type of link layer this is 495 514 */ 496 515 void *trace_get_link(const struct libtrace_packet_t *packet) { … … 503 522 } 504 523 505 /* *get a pointer to the IP header (if any)524 /* get a pointer to the IP header (if any) 506 525 * @param packet a pointer to a libtrace_packet structure 507 526 * … … 542 561 case TRACE_TYPE_ETH: 543 562 { 544 struct ether_header *eth =563 struct libtrace_ether *eth = 545 564 trace_get_link(packet); 546 565 if (!eth) { … … 622 641 #define SW_IP_OFFMASK 0xff1f 623 642 624 /* *get a pointer to the TCP header (if any)643 /* get a pointer to the TCP header (if any) 625 644 * @param packet a pointer to a libtrace_packet structure 626 645 * … … 640 659 } 641 660 642 /* *get a pointer to the TCP header (if any) given a pointer to the IP header661 /* get a pointer to the TCP header (if any) given a pointer to the IP header 643 662 * @param ip The IP header 644 663 * @param[out] skipped An output variable of the number of bytes skipped … … 663 682 } 664 683 665 /* *get a pointer to the UDP header (if any)684 /* get a pointer to the UDP header (if any) 666 685 * @param packet a pointer to a libtrace_packet structure 667 686 * … … 682 701 } 683 702 684 /* *get a pointer to the UDP header (if any) given a pointer to the IP header703 /* get a pointer to the UDP header (if any) given a pointer to the IP header 685 704 * @param ip The IP header 686 705 * @param[out] skipped An output variable of the number of bytes skipped … … 705 724 706 725 707 /* *get a pointer to the ICMP header (if any)726 /* get a pointer to the ICMP header (if any) 708 727 * @param packet a pointer to a libtrace_packet structure 709 728 * … … 723 742 } 724 743 725 /* *get a pointer to the ICMP header (if any) given a pointer to the IP header744 /* get a pointer to the ICMP header (if any) given a pointer to the IP header 726 745 * @param ip The IP header 727 746 * @param[out] skipped An output variable of the number of bytes skipped … … 744 763 return icmpptr; 745 764 } 746 /* *parse an ip or tcp option765 /* parse an ip or tcp option 747 766 * @param[in,out] ptr the pointer to the current option 748 767 * @param[in,out] len the length of the remaining buffer … … 793 812 794 813 795 /* *Get the current time in DAG time format814 /* Get the current time in DAG time format 796 815 * @param packet a pointer to a libtrace_packet structure 797 816 * @returns a 64 bit timestamp in DAG ERF format (upper 32 bits are the seconds … … 821 840 } 822 841 823 /* *Get the current time in struct timeval842 /* Get the current time in struct timeval 824 843 * @param packet a pointer to a libtrace_packet structure 825 844 * … … 862 881 } 863 882 864 /* *Get the current time in floating point seconds883 /* Get the current time in floating point seconds 865 884 * @param packet a pointer to a libtrace_packet structure 866 885 * @returns time that this packet was seen in 64bit floating point seconds … … 888 907 } 889 908 890 /* *Get the size of the packet in the trace909 /* Get the size of the packet in the trace 891 910 * @param packet the packet opaque pointer 892 911 * @returns the size of the packet in the trace … … 907 926 } 908 927 909 /* *Get the size of the packet as it was seen on the wire.928 /* Get the size of the packet as it was seen on the wire. 910 929 * @param packet a pointer to a libtrace_packet structure 911 930 * … … 924 943 } 925 944 926 /* *Get the type of the link layer945 /* Get the type of the link layer 927 946 * @param packet a pointer to a libtrace_packet structure 928 947 * @returns libtrace_linktype_t … … 937 956 } 938 957 939 /* *Get the source MAC addres958 /* Get the source MAC addres 940 959 * @param packet a pointer to a libtrace_packet structure 941 960 * @returns a pointer to the source mac, (or NULL if there is no source MAC) … … 945 964 void *link = trace_get_link(packet); 946 965 struct ieee_802_11_header *wifi = link; 947 struct ether_header *ethptr = link;966 struct libtrace_ether *ethptr = link; 948 967 if (!link) 949 968 return NULL; … … 959 978 } 960 979 961 /* *Get the destination MAC addres980 /* Get the destination MAC addres 962 981 * @param packet a libtrace_packet pointer 963 982 * @returns a pointer to the destination mac, (or NULL if there is no … … 968 987 void *link = trace_get_link(packet); 969 988 struct ieee_802_11_header *wifi = link; 970 struct ether_header *ethptr = link;989 struct libtrace_ether *ethptr = link; 971 990 if (!link) 972 991 return NULL; … … 983 1002 984 1003 985 /* *process a libtrace event1004 /* process a libtrace event 986 1005 * @param trace the libtrace opaque pointer 987 1006 * @param packet the libtrace_packet opaque pointer … … 1017 1036 } 1018 1037 1019 /* *setup a BPF filter1038 /* setup a BPF filter 1020 1039 * @param filterstring a char * containing the bpf filter string 1021 1040 * @returns opaque pointer pointer to a libtrace_filter_t object … … 1034 1053 } 1035 1054 1036 /* *apply a BPF filter1055 /* apply a BPF filter 1037 1056 * @param filter the filter opaque pointer 1038 1057 * @param packet the packet opaque pointer … … 1096 1115 } 1097 1116 1098 /* *Set the direction flag, if it has one1117 /* Set the direction flag, if it has one 1099 1118 * @param packet the packet opaque pointer 1100 1119 * @param direction the new direction (0,1,2,3) … … 1110 1129 } 1111 1130 1112 /* *Get the direction flag, if it has one1131 /* Get the direction flag, if it has one 1113 1132 * @param packet a pointer to a libtrace_packet structure 1114 1133 * @returns a signed value containing the direction flag, or -1 if this is not supported … … 1146 1165 port = (struct ports_t *)((ptrdiff_t)ip + (ip->ip_hl * 4)); 1147 1166 1148 return htons(port->src);1167 return ntohs(port->src); 1149 1168 } 1150 1169 … … 1164 1183 port = (struct ports_t *)((ptrdiff_t)ip + (ip->ip_hl * 4)); 1165 1184 1166 return htons(port->dst);1185 return ntohs(port->dst); 1167 1186 } 1168 1187 … … 1282 1301 } 1283 1302 1284 /* *Truncate the packet at the suggested length1303 /* Truncate the packet at the suggested length 1285 1304 * @param packet the packet opaque pointer 1286 1305 * @param size the new length of the packet
Note: See TracChangeset
for help on using the changeset viewer.