| 1 | /* |
|---|
| 2 | * This file is part of libtrace |
|---|
| 3 | * |
|---|
| 4 | * Copyright (c) 2004 The University of Waikato, Hamilton, New Zealand. |
|---|
| 5 | * Authors: Daniel Lawson |
|---|
| 6 | * Perry Lorier |
|---|
| 7 | * |
|---|
| 8 | * All rights reserved. |
|---|
| 9 | * |
|---|
| 10 | * This code has been developed by the University of Waikato WAND |
|---|
| 11 | * research group. For further information please see http://www.wand.net.nz/ |
|---|
| 12 | * |
|---|
| 13 | * libtrace is free software; you can redistribute it and/or modify |
|---|
| 14 | * it under the terms of the GNU General Public License as published by |
|---|
| 15 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 16 | * (at your option) any later version. |
|---|
| 17 | * |
|---|
| 18 | * libtrace is distributed in the hope that it will be useful, |
|---|
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 21 | * GNU General Public License for more details. |
|---|
| 22 | * |
|---|
| 23 | * You should have received a copy of the GNU General Public License |
|---|
| 24 | * along with libtrace; if not, write to the Free Software |
|---|
| 25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 26 | * |
|---|
| 27 | * $Id$ |
|---|
| 28 | * |
|---|
| 29 | */ |
|---|
| 30 | |
|---|
| 31 | #ifndef LIBTRACE_H |
|---|
| 32 | #define LIBTRACE_H |
|---|
| 33 | |
|---|
| 34 | /** @file |
|---|
| 35 | * |
|---|
| 36 | * @brief Trace file processing library header |
|---|
| 37 | * |
|---|
| 38 | * @author Daniel Lawson |
|---|
| 39 | * @author Perry Lorier |
|---|
| 40 | * |
|---|
| 41 | * @version $Id$ |
|---|
| 42 | * |
|---|
| 43 | * This library provides a per packet interface into a trace file, or a live |
|---|
| 44 | * captures. It supports ERF, DAG cards, WAG cards, WAG's event format, |
|---|
| 45 | * pcap etc. |
|---|
| 46 | * |
|---|
| 47 | * @par Usage |
|---|
| 48 | * See the example/ directory in the source distribution for some simple examples |
|---|
| 49 | * @par Linking |
|---|
| 50 | * To use this library you need to link against libtrace by passing -ltrace |
|---|
| 51 | * to your linker. You may also need to link against a version of libpcap |
|---|
| 52 | * and of zlib which are compiled for largefile support (if you wish to access |
|---|
| 53 | * traces larger than 2 GB). This is left as an exercise for the reader. Debian |
|---|
| 54 | * Woody, at least, does not support large file offsets. |
|---|
| 55 | * |
|---|
| 56 | */ |
|---|
| 57 | |
|---|
| 58 | #include <sys/types.h> |
|---|
| 59 | #ifdef _MSC_VER |
|---|
| 60 | /* define the following from MSVC's internal types */ |
|---|
| 61 | typedef __int8 int8_t; |
|---|
| 62 | typedef __int16 int16_t; |
|---|
| 63 | typedef __int32 int32_t; |
|---|
| 64 | typedef __int64 int64_t; |
|---|
| 65 | typedef unsigned __int8 uint8_t; |
|---|
| 66 | typedef unsigned __int16 uint16_t; |
|---|
| 67 | typedef unsigned __int32 uint32_t; |
|---|
| 68 | typedef unsigned __int64 uint64_t; |
|---|
| 69 | #ifdef BUILDING_DLL |
|---|
| 70 | #define DLLEXPORT __declspec(dllexport) |
|---|
| 71 | #else |
|---|
| 72 | #define DLLEXPORT __declspec(dllimport) |
|---|
| 73 | #endif |
|---|
| 74 | #define DLLLOCAL |
|---|
| 75 | #else |
|---|
| 76 | #ifdef HAVE_GCCVISIBILITYPATCH |
|---|
| 77 | #define DLLEXPORT __attribute__ (visibility("default")) |
|---|
| 78 | #define DLLLOCAL __attribute__ (visibility("hidden")) |
|---|
| 79 | #else |
|---|
| 80 | #define DLLEXPORT |
|---|
| 81 | #define DLLLOCAL |
|---|
| 82 | #endif |
|---|
| 83 | #endif |
|---|
| 84 | |
|---|
| 85 | #ifdef WIN32 |
|---|
| 86 | # include <winsock2.h> |
|---|
| 87 | # include <ws2tcpip.h> |
|---|
| 88 | typedef short sa_family_t; |
|---|
| 89 | /* Make up for a lack of stdbool.h */ |
|---|
| 90 | # define bool signed char |
|---|
| 91 | # define false 0 |
|---|
| 92 | # define true 1 |
|---|
| 93 | # if !defined(ssize_t) |
|---|
| 94 | /* XXX: Not 64-bit safe! */ |
|---|
| 95 | # define ssize_t int |
|---|
| 96 | # endif |
|---|
| 97 | #else |
|---|
| 98 | # include <netinet/in.h> |
|---|
| 99 | # include <stdbool.h> |
|---|
| 100 | #endif |
|---|
| 101 | |
|---|
| 102 | /** API version as 2 byte hex digits, eg 0xXXYYZZ */ |
|---|
| 103 | #define LIBTRACE_API_VERSION 0x030000 /* 3.0.00 */ |
|---|
| 104 | |
|---|
| 105 | #ifdef __cplusplus |
|---|
| 106 | extern "C" { |
|---|
| 107 | #endif |
|---|
| 108 | |
|---|
| 109 | /* Function does not depend on anything but its |
|---|
| 110 | * parameters, used to hint gcc's optimisations |
|---|
| 111 | */ |
|---|
| 112 | #if __GNUC__ >= 3 |
|---|
| 113 | # define SIMPLE_FUNCTION __attribute__((pure)) |
|---|
| 114 | # define UNUSED __attribute__((unused)) |
|---|
| 115 | # define PACKED __attribute__((packed)) |
|---|
| 116 | # define CONSTRUCTOR __attribute__((constructor)) |
|---|
| 117 | #else |
|---|
| 118 | # define SIMPLE_FUNCTION |
|---|
| 119 | # define UNUSED |
|---|
| 120 | # define PACKED __declspec(align(1)) |
|---|
| 121 | # define CONSTRUCTOR |
|---|
| 122 | #endif |
|---|
| 123 | |
|---|
| 124 | /** Opaque structure holding information about an output trace */ |
|---|
| 125 | typedef struct libtrace_out_t libtrace_out_t; |
|---|
| 126 | |
|---|
| 127 | /** Opaque structure holding information about a trace */ |
|---|
| 128 | typedef struct libtrace_t libtrace_t; |
|---|
| 129 | |
|---|
| 130 | /** Opaque structure holding information about a bpf filter */ |
|---|
| 131 | typedef struct libtrace_filter_t libtrace_filter_t; |
|---|
| 132 | |
|---|
| 133 | /** if a packet has memory allocated |
|---|
| 134 | * If the packet has allocated it's own memory it's buffer_control should |
|---|
| 135 | * be TRACE_CTRL_PACKET, when the packet is destroyed it's memory will be |
|---|
| 136 | * free()'d. If it's doing zerocopy out of memory owned by something else |
|---|
| 137 | * it should be TRACE_CTRL_EXTERNAL. |
|---|
| 138 | * @note the letters p and e are magic numbers used to detect if the packet |
|---|
| 139 | * wasn't created properly |
|---|
| 140 | */ |
|---|
| 141 | typedef enum { |
|---|
| 142 | TRACE_CTRL_PACKET='p', |
|---|
| 143 | TRACE_CTRL_EXTERNAL='e' |
|---|
| 144 | } buf_control_t; |
|---|
| 145 | /** Structure holding information about a packet */ |
|---|
| 146 | #define LIBTRACE_PACKET_BUFSIZE 65536 |
|---|
| 147 | |
|---|
| 148 | /** The libtrace structure, applications shouldn't be meddling around in here |
|---|
| 149 | */ |
|---|
| 150 | typedef struct libtrace_packet_t { |
|---|
| 151 | struct libtrace_t *trace; /**< pointer to the trace */ |
|---|
| 152 | void *header; /**< pointer to the framing header */ |
|---|
| 153 | void *payload; /**< pointer to the link layer */ |
|---|
| 154 | buf_control_t buf_control; /**< who owns the memory */ |
|---|
| 155 | void *buffer; /**< allocated buffer */ |
|---|
| 156 | size_t size; /**< trace_get_framing_length() |
|---|
| 157 | * +trace_get_capture_length() */ |
|---|
| 158 | uint32_t type; /**< rt protocol type for the packet */ |
|---|
| 159 | } libtrace_packet_t; |
|---|
| 160 | |
|---|
| 161 | /** libtrace error information */ |
|---|
| 162 | typedef struct trace_err_t{ |
|---|
| 163 | int err_num; /**< error code */ |
|---|
| 164 | char problem[255]; /**< the format, uri etc that caused the error for reporting purposes */ |
|---|
| 165 | } libtrace_err_t; |
|---|
| 166 | |
|---|
| 167 | /** Enumeration of error codes */ |
|---|
| 168 | enum { |
|---|
| 169 | /** No Error has occured.... yet. */ |
|---|
| 170 | TRACE_ERR_NOERROR = 0, |
|---|
| 171 | /** The URI passed to trace_create() is unsupported, or badly formed */ |
|---|
| 172 | TRACE_ERR_BAD_FORMAT = -1, |
|---|
| 173 | /** The trace failed to initialise */ |
|---|
| 174 | TRACE_ERR_INIT_FAILED = -2, |
|---|
| 175 | /** Unknown config option */ |
|---|
| 176 | TRACE_ERR_UNKNOWN_OPTION= -3, |
|---|
| 177 | /** Option known, but unsupported by this format */ |
|---|
| 178 | TRACE_ERR_OPTION_UNAVAIL= -6, |
|---|
| 179 | /** This output uri cannot write packets of this type */ |
|---|
| 180 | TRACE_ERR_NO_CONVERSION = -4, |
|---|
| 181 | /** This packet is corrupt, or unusable for the action required */ |
|---|
| 182 | TRACE_ERR_BAD_PACKET = -5, |
|---|
| 183 | /** This feature is unsupported */ |
|---|
| 184 | TRACE_ERR_UNSUPPORTED = -7 |
|---|
| 185 | }; |
|---|
| 186 | |
|---|
| 187 | typedef enum { |
|---|
| 188 | TRACE_DLT_NULL = 0, |
|---|
| 189 | TRACE_DLT_EN10MB = 1, |
|---|
| 190 | TRACE_DLT_ATM_RFC1483 = 11, |
|---|
| 191 | TRACE_DLT_IEEE802_11 = 105, |
|---|
| 192 | TRACE_DLT_LINUX_SLL = 113, |
|---|
| 193 | TRACE_DLT_PFLOG = 117 |
|---|
| 194 | } libtrace_dlt_t ; |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | /** @name Protocol structures |
|---|
| 198 | * These convenience structures are here as they are portable ways of dealing |
|---|
| 199 | * with various protocols. |
|---|
| 200 | * @{ |
|---|
| 201 | */ |
|---|
| 202 | |
|---|
| 203 | #ifdef WIN32 |
|---|
| 204 | #pragma pack(push) |
|---|
| 205 | #pragma pack(1) |
|---|
| 206 | #endif |
|---|
| 207 | |
|---|
| 208 | /** Structure for dealing with IP packets */ |
|---|
| 209 | typedef struct libtrace_ip |
|---|
| 210 | { |
|---|
| 211 | #if BYTE_ORDER == LITTLE_ENDIAN |
|---|
| 212 | uint8_t ip_hl:4; /**< header length */ |
|---|
| 213 | uint8_t ip_v:4; /**< version */ |
|---|
| 214 | #elif BYTE_ORDER == BIG_ENDIAN |
|---|
| 215 | uint8_t ip_v:4; /**< version */ |
|---|
| 216 | uint8_t ip_hl:4; /**< header length */ |
|---|
| 217 | #else |
|---|
| 218 | # error "Adjust your <bits/endian.h> defines" |
|---|
| 219 | #endif |
|---|
| 220 | uint8_t ip_tos; /**< type of service */ |
|---|
| 221 | uint16_t ip_len; /**< total length */ |
|---|
| 222 | int16_t ip_id; /**< identification */ |
|---|
| 223 | #if BYTE_ORDER == LITTLE_ENDIAN |
|---|
| 224 | uint16_t ip_off:12; /**< fragment offset */ |
|---|
| 225 | uint16_t ip_mf:1; /**< more fragments flag */ |
|---|
| 226 | uint16_t ip_df:1; /**< dont fragment flag */ |
|---|
| 227 | uint16_t ip_rf:1; /**< reserved fragment flag */ |
|---|
| 228 | #elif BYTE_ORDER == BIG_ENDIAN |
|---|
| 229 | uint16_t ip_rf:1; |
|---|
| 230 | uint16_t ip_df:1; |
|---|
| 231 | uint16_t ip_mf:1; |
|---|
| 232 | uint16_t ip_off:12; |
|---|
| 233 | #else |
|---|
| 234 | # error "Adjust your <bits/endian.h> defines" |
|---|
| 235 | #endif |
|---|
| 236 | uint8_t ip_ttl; /**< time to live */ |
|---|
| 237 | uint8_t ip_p; /**< protocol */ |
|---|
| 238 | uint16_t ip_sum; /**< checksum */ |
|---|
| 239 | struct in_addr ip_src; /**< source address */ |
|---|
| 240 | struct in_addr ip_dst; /**< dest address */ |
|---|
| 241 | } PACKED libtrace_ip_t; |
|---|
| 242 | |
|---|
| 243 | typedef struct libtrace_ip6_ext |
|---|
| 244 | { |
|---|
| 245 | uint8_t nxt; |
|---|
| 246 | uint8_t len; |
|---|
| 247 | } PACKED libtrace_ip6_ext_t; |
|---|
| 248 | |
|---|
| 249 | /** IPv6 header structure */ |
|---|
| 250 | typedef struct libtrace_ip6 |
|---|
| 251 | { |
|---|
| 252 | uint32_t flow; |
|---|
| 253 | uint16_t plen; /**< Payload length */ |
|---|
| 254 | uint8_t nxt; /**< Next header */ |
|---|
| 255 | uint8_t hlim; /**< Hop limit */ |
|---|
| 256 | struct in6_addr ip_src; /**< source address */ |
|---|
| 257 | struct in6_addr ip_dst; /**< dest address */ |
|---|
| 258 | } PACKED libtrace_ip6_t; |
|---|
| 259 | |
|---|
| 260 | /** Structure for dealing with TCP packets */ |
|---|
| 261 | typedef struct libtrace_tcp |
|---|
| 262 | { |
|---|
| 263 | uint16_t source; /**< Source Port */ |
|---|
| 264 | uint16_t dest; /**< Destination port */ |
|---|
| 265 | uint32_t seq; /**< Sequence number */ |
|---|
| 266 | uint32_t ack_seq; /**< Acknowledgement Number */ |
|---|
| 267 | # if BYTE_ORDER == LITTLE_ENDIAN |
|---|
| 268 | uint8_t res1:4; /**< Reserved bits */ |
|---|
| 269 | uint8_t doff:4; /**< data offset */ |
|---|
| 270 | uint8_t fin:1; /**< FIN */ |
|---|
| 271 | uint8_t syn:1; /**< SYN flag */ |
|---|
| 272 | uint8_t rst:1; /**< RST flag */ |
|---|
| 273 | uint8_t psh:1; /**< PuSH flag */ |
|---|
| 274 | uint8_t ack:1; /**< ACK flag */ |
|---|
| 275 | uint8_t urg:1; /**< URG flag */ |
|---|
| 276 | uint8_t res2:2; /**< Reserved */ |
|---|
| 277 | # elif BYTE_ORDER == BIG_ENDIAN |
|---|
| 278 | uint8_t doff:4; /**< Data offset */ |
|---|
| 279 | uint8_t res1:4; /**< Reserved bits */ |
|---|
| 280 | uint8_t res2:2; /**< Reserved */ |
|---|
| 281 | uint8_t urg:1; /**< URG flag */ |
|---|
| 282 | uint8_t ack:1; /**< ACK flag */ |
|---|
| 283 | uint8_t psh:1; /**< PuSH flag */ |
|---|
| 284 | uint8_t rst:1; /**< RST flag */ |
|---|
| 285 | uint8_t syn:1; /**< SYN flag */ |
|---|
| 286 | uint8_t fin:1; /**< FIN flag */ |
|---|
| 287 | # else |
|---|
| 288 | # error "Adjust your <bits/endian.h> defines" |
|---|
| 289 | # endif |
|---|
| 290 | uint16_t window; /**< Window Size */ |
|---|
| 291 | uint16_t check; /**< Checksum */ |
|---|
| 292 | uint16_t urg_ptr; /**< Urgent Pointer */ |
|---|
| 293 | } PACKED libtrace_tcp_t; |
|---|
| 294 | |
|---|
| 295 | /** UDP Header for dealing with UDP packets */ |
|---|
| 296 | typedef struct libtrace_udp { |
|---|
| 297 | uint16_t source; /**< Source port */ |
|---|
| 298 | uint16_t dest; /**< Destination port */ |
|---|
| 299 | uint16_t len; /**< Length */ |
|---|
| 300 | uint16_t check; /**< Checksum */ |
|---|
| 301 | } PACKED libtrace_udp_t; |
|---|
| 302 | |
|---|
| 303 | /** ICMP Header for dealing with icmp packets */ |
|---|
| 304 | typedef struct libtrace_icmp |
|---|
| 305 | { |
|---|
| 306 | uint8_t type; /**< message type */ |
|---|
| 307 | uint8_t code; /**< type sub-code */ |
|---|
| 308 | uint16_t checksum; /**< checksum */ |
|---|
| 309 | union |
|---|
| 310 | { |
|---|
| 311 | struct |
|---|
| 312 | { |
|---|
| 313 | uint16_t id; |
|---|
| 314 | uint16_t sequence; |
|---|
| 315 | } echo; /**< echo datagram */ |
|---|
| 316 | uint32_t gateway; /**< gateway address */ |
|---|
| 317 | struct |
|---|
| 318 | { |
|---|
| 319 | uint16_t unused; |
|---|
| 320 | uint16_t mtu; |
|---|
| 321 | } frag; /**< path mtu discovery */ |
|---|
| 322 | } un; /**< Union for payloads of various icmp codes */ |
|---|
| 323 | } PACKED libtrace_icmp_t; |
|---|
| 324 | |
|---|
| 325 | /** LLC/SNAP header */ |
|---|
| 326 | typedef struct libtrace_llcsnap |
|---|
| 327 | { |
|---|
| 328 | /* LLC */ |
|---|
| 329 | uint8_t dsap; /**< Destination Service Access Point */ |
|---|
| 330 | uint8_t ssap; /**< Source Service Access Point */ |
|---|
| 331 | uint8_t control; |
|---|
| 332 | /* SNAP */ |
|---|
| 333 | uint32_t oui:24; /**< Organisationally Unique Identifier (scope)*/ |
|---|
| 334 | uint16_t type; /**< Protocol within OUI */ |
|---|
| 335 | } PACKED libtrace_llcsnap_t; |
|---|
| 336 | |
|---|
| 337 | /** 802.3 frame */ |
|---|
| 338 | typedef struct libtrace_ether |
|---|
| 339 | { |
|---|
| 340 | uint8_t ether_dhost[6]; /**< destination ether addr */ |
|---|
| 341 | uint8_t ether_shost[6]; /**< source ether addr */ |
|---|
| 342 | uint16_t ether_type; /**< packet type ID field (next-header) */ |
|---|
| 343 | } PACKED libtrace_ether_t; |
|---|
| 344 | |
|---|
| 345 | /** 802.1Q frame */ |
|---|
| 346 | typedef struct libtrace_8021q |
|---|
| 347 | { |
|---|
| 348 | uint16_t vlan_pri:3; /**< vlan user priority */ |
|---|
| 349 | uint16_t vlan_cfi:1; /**< vlan format indicator, |
|---|
| 350 | * 0 for ethernet, 1 for token ring */ |
|---|
| 351 | uint16_t vlan_id:12; /**< vlan id */ |
|---|
| 352 | uint16_t vlan_ether_type; /**< vlan sub-packet type ID field |
|---|
| 353 | * (next-header)*/ |
|---|
| 354 | } PACKED libtrace_8021q_t; |
|---|
| 355 | |
|---|
| 356 | /** ATM cell */ |
|---|
| 357 | typedef struct libtrace_atm_cell |
|---|
| 358 | { |
|---|
| 359 | uint32_t gfc:4; /**< Generic Flow Control */ |
|---|
| 360 | uint32_t vpi:8; /**< Virtual Path Identifier */ |
|---|
| 361 | uint32_t vci:8; /**< Virtual Channel Identifier */ |
|---|
| 362 | uint32_t pt:3; /**< Payload Type */ |
|---|
| 363 | uint32_t clp:1; /**< Cell Loss Priority */ |
|---|
| 364 | uint32_t hec:8; /**< Header Error Control */ |
|---|
| 365 | } PACKED libtrace_atm_cell_t; |
|---|
| 366 | |
|---|
| 367 | /** POS header */ |
|---|
| 368 | typedef struct libtrace_pos |
|---|
| 369 | { |
|---|
| 370 | uint16_t header; |
|---|
| 371 | uint16_t ether_type; /**< ether type */ |
|---|
| 372 | } PACKED libtrace_pos_t; |
|---|
| 373 | |
|---|
| 374 | /** 802.11 header */ |
|---|
| 375 | typedef struct libtrace_80211_t { |
|---|
| 376 | uint16_t protocol:2; |
|---|
| 377 | uint16_t type:2; |
|---|
| 378 | uint16_t subtype:4; |
|---|
| 379 | uint16_t to_ds:1; /**< Packet to Distribution Service */ |
|---|
| 380 | uint16_t from_ds:1; /**< Packet from Distribution Service */ |
|---|
| 381 | uint16_t more_frag:1; /**< Packet has more fragments */ |
|---|
| 382 | uint16_t retry:1; /**< Packet is a retry */ |
|---|
| 383 | uint16_t power:1; |
|---|
| 384 | uint16_t more_data:1; |
|---|
| 385 | uint16_t wep:1; |
|---|
| 386 | uint16_t order:1; |
|---|
| 387 | uint16_t duration; |
|---|
| 388 | uint8_t mac1[6]; |
|---|
| 389 | uint8_t mac2[6]; |
|---|
| 390 | uint8_t mac3[6]; |
|---|
| 391 | uint16_t SeqCtl; |
|---|
| 392 | uint8_t mac4[6]; |
|---|
| 393 | } PACKED libtrace_80211_t; |
|---|
| 394 | |
|---|
| 395 | #ifdef WIN32 |
|---|
| 396 | #pragma pack(pop) |
|---|
| 397 | #endif |
|---|
| 398 | |
|---|
| 399 | |
|---|
| 400 | /*@}*/ |
|---|
| 401 | |
|---|
| 402 | /** Prints help information for libtrace |
|---|
| 403 | * |
|---|
| 404 | * Function prints out some basic help information regarding libtrace, |
|---|
| 405 | * and then prints out the help() function registered with each input module |
|---|
| 406 | */ |
|---|
| 407 | DLLEXPORT void trace_help(); |
|---|
| 408 | |
|---|
| 409 | /** Gets the output format for a given output trace |
|---|
| 410 | * |
|---|
| 411 | * @param libtrace the output trace to get the name of the format fo |
|---|
| 412 | * @return callee-owned null-terminated char* containing the output format |
|---|
| 413 | * |
|---|
| 414 | */ |
|---|
| 415 | DLLEXPORT SIMPLE_FUNCTION |
|---|
| 416 | char *trace_get_output_format(const libtrace_out_t *libtrace); |
|---|
| 417 | |
|---|
| 418 | /** @name Trace management |
|---|
| 419 | * These members deal with creating, configuring, starting, pausing and |
|---|
| 420 | * cleaning up a trace object |
|---|
| 421 | *@{ |
|---|
| 422 | */ |
|---|
| 423 | |
|---|
| 424 | /** Create a trace file from a URI |
|---|
| 425 | * |
|---|
| 426 | * @param uri containing a valid libtrace URI |
|---|
| 427 | * @return opaque pointer to a libtrace_t |
|---|
| 428 | * |
|---|
| 429 | * Valid URI's are: |
|---|
| 430 | * - erf:/path/to/erf/file |
|---|
| 431 | * - erf:/path/to/erf/file.gz |
|---|
| 432 | * - erf:/path/to/rtclient/socket |
|---|
| 433 | * - erf:- (stdin) |
|---|
| 434 | * - dag:/dev/dagcard |
|---|
| 435 | * - pcapint:pcapinterface (eg: pcap:eth0) |
|---|
| 436 | * - pcap:/path/to/pcap/file |
|---|
| 437 | * - pcap:- |
|---|
| 438 | * - rtclient:hostname |
|---|
| 439 | * - rtclient:hostname:port |
|---|
| 440 | * - wag:- |
|---|
| 441 | * - wag:/path/to/wag/file |
|---|
| 442 | * - wag:/path/to/wag/file.gz |
|---|
| 443 | * - wag:/path/to/wag/socket |
|---|
| 444 | * |
|---|
| 445 | * If an error occured when attempting to open the trace file, an error |
|---|
| 446 | * trace is returned and trace_get_error should be called to find out |
|---|
| 447 | * if an error occured, and what that error was. The trace is created in the |
|---|
| 448 | * configuration state, you must call trace_start to start the capture. |
|---|
| 449 | */ |
|---|
| 450 | DLLEXPORT libtrace_t *trace_create(const char *uri); |
|---|
| 451 | |
|---|
| 452 | /** Creates a "dummy" trace file that has only the format type set. |
|---|
| 453 | * |
|---|
| 454 | * @return opaque pointer to a (sparsely initialised) libtrace_t |
|---|
| 455 | * |
|---|
| 456 | * IMPORTANT: Do not attempt to call trace_read_packet or other such functions |
|---|
| 457 | * with the dummy trace. Its intended purpose is to act as a packet->trace for |
|---|
| 458 | * libtrace_packet_t's that are not associated with a libtrace_t structure. |
|---|
| 459 | */ |
|---|
| 460 | DLLEXPORT libtrace_t *trace_create_dead(const char *uri); |
|---|
| 461 | |
|---|
| 462 | /** Creates a trace output file from a URI. |
|---|
| 463 | * |
|---|
| 464 | * @param uri the uri string describing the output format and destination |
|---|
| 465 | * @return opaque pointer to a libtrace_output_t |
|---|
| 466 | * @author Shane Alcock |
|---|
| 467 | * |
|---|
| 468 | * Valid URI's are: |
|---|
| 469 | * - gzerf:/path/to/erf/file.gz |
|---|
| 470 | * - gzerf:/path/to/erf/file |
|---|
| 471 | * - rtserver:hostname |
|---|
| 472 | * - rtserver:hostname:port |
|---|
| 473 | * |
|---|
| 474 | * If an error occured when attempting to open the output trace, NULL is returned |
|---|
| 475 | * and trace_errno is set. Use trace_perror() to get more information |
|---|
| 476 | */ |
|---|
| 477 | DLLEXPORT libtrace_out_t *trace_create_output(const char *uri); |
|---|
| 478 | |
|---|
| 479 | /** Start the capture |
|---|
| 480 | * @param libtrace The trace to start |
|---|
| 481 | * @return 0 on success |
|---|
| 482 | * |
|---|
| 483 | * This does the actual work with starting the trace capture, and applying |
|---|
| 484 | * all the config options. This may fail. |
|---|
| 485 | */ |
|---|
| 486 | DLLEXPORT int trace_start(libtrace_t *libtrace); |
|---|
| 487 | |
|---|
| 488 | /** Pause the capture |
|---|
| 489 | * @param libtrace The trace to pause |
|---|
| 490 | * @return 0 on success |
|---|
| 491 | * |
|---|
| 492 | * This stops a capture in progress and returns you to the configuration |
|---|
| 493 | * state. Any packets that arrive after trace_pause() has been called |
|---|
| 494 | * will be discarded. To resume capture, call trace_start(). |
|---|
| 495 | */ |
|---|
| 496 | DLLEXPORT int trace_pause(libtrace_t *libtrace); |
|---|
| 497 | |
|---|
| 498 | /** Start an output trace |
|---|
| 499 | * @param libtrace The trace to start |
|---|
| 500 | * @return 0 on success |
|---|
| 501 | * |
|---|
| 502 | * This does the actual work with starting a trace for write. This generally |
|---|
| 503 | * creates the file. |
|---|
| 504 | */ |
|---|
| 505 | DLLEXPORT int trace_start_output(libtrace_out_t *libtrace); |
|---|
| 506 | |
|---|
| 507 | /** Valid trace capture options */ |
|---|
| 508 | typedef enum { |
|---|
| 509 | TRACE_OPTION_SNAPLEN, /**< Number of bytes captured */ |
|---|
| 510 | TRACE_OPTION_PROMISC, /**< Capture packets to other hosts */ |
|---|
| 511 | TRACE_OPTION_FILTER /**< Apply this filter to all packets recieved */ |
|---|
| 512 | } trace_option_t; |
|---|
| 513 | |
|---|
| 514 | /** Sets an input config option |
|---|
| 515 | * @param libtrace the trace object to apply the option to |
|---|
| 516 | * @param option the option to set |
|---|
| 517 | * @param value the value to set the option to |
|---|
| 518 | * @return -1 if option configuration failed, 0 otherwise |
|---|
| 519 | * This should be called after trace_create, and before trace_start |
|---|
| 520 | */ |
|---|
| 521 | DLLEXPORT int trace_config(libtrace_t *libtrace, |
|---|
| 522 | trace_option_t option, |
|---|
| 523 | void *value); |
|---|
| 524 | |
|---|
| 525 | typedef enum { |
|---|
| 526 | TRACE_OPTION_OUTPUT_FILEFLAGS, /**< File flags to open the trace file |
|---|
| 527 | * with. eg O_APPEND |
|---|
| 528 | */ |
|---|
| 529 | TRACE_OPTION_OUTPUT_COMPRESS /**< Compression level, eg 6. */ |
|---|
| 530 | } trace_option_output_t; |
|---|
| 531 | |
|---|
| 532 | /** Sets an output config option |
|---|
| 533 | * |
|---|
| 534 | * @param libtrace the output trace object to apply the option to |
|---|
| 535 | * @param option the option to set |
|---|
| 536 | * @param value the value to set the option to |
|---|
| 537 | * @return -1 if option configuration failed, 0 otherwise |
|---|
| 538 | * This should be called after trace_create_output, and before |
|---|
| 539 | * trace_start_output |
|---|
| 540 | */ |
|---|
| 541 | DLLEXPORT int trace_config_output(libtrace_out_t *libtrace, |
|---|
| 542 | trace_option_output_t option, |
|---|
| 543 | void *value |
|---|
| 544 | ); |
|---|
| 545 | |
|---|
| 546 | /** Close a trace file, freeing up any resources it may have been using |
|---|
| 547 | * |
|---|
| 548 | */ |
|---|
| 549 | DLLEXPORT void trace_destroy(libtrace_t *trace); |
|---|
| 550 | |
|---|
| 551 | /** Close a trace file, freeing up any resources it may have been using |
|---|
| 552 | * @param trace trace file to be destroyed |
|---|
| 553 | */ |
|---|
| 554 | DLLEXPORT void trace_destroy_dead(libtrace_t *trace); |
|---|
| 555 | |
|---|
| 556 | /** Close a trace output file, freeing up any resources it may have been using |
|---|
| 557 | * @param trace the output trace file to be destroyed |
|---|
| 558 | * |
|---|
| 559 | * @author Shane Alcock |
|---|
| 560 | */ |
|---|
| 561 | DLLEXPORT void trace_destroy_output(libtrace_out_t *trace); |
|---|
| 562 | |
|---|
| 563 | /** Check (and clear) the current error state of an input trace |
|---|
| 564 | * @param trace the trace file to check the error state on |
|---|
| 565 | * @return Error report |
|---|
| 566 | * This reads and returns the current error state and sets the current error |
|---|
| 567 | * to "no error". |
|---|
| 568 | */ |
|---|
| 569 | DLLEXPORT libtrace_err_t trace_get_err(libtrace_t *trace); |
|---|
| 570 | |
|---|
| 571 | /** Return if there is an error |
|---|
| 572 | * @param trace the trace file to check the error state on |
|---|
| 573 | * This does not clear the error status, and only returns true or false. |
|---|
| 574 | */ |
|---|
| 575 | DLLEXPORT bool trace_is_err(libtrace_t *trace); |
|---|
| 576 | |
|---|
| 577 | /** Output an error message to stderr and clear the error status. |
|---|
| 578 | * @param trace the trace with the error to output |
|---|
| 579 | * @param msg the message to prefix to the error |
|---|
| 580 | * This function does clear the error status. |
|---|
| 581 | */ |
|---|
| 582 | DLLEXPORT void trace_perror(libtrace_t *trace, const char *msg,...); |
|---|
| 583 | |
|---|
| 584 | /** Check (and clear) the current error state of an output trace |
|---|
| 585 | * @param trace the output trace file to check the error state on |
|---|
| 586 | * @return Error report |
|---|
| 587 | * This reads and returns the current error state and sets the current error |
|---|
| 588 | * to "no error". |
|---|
| 589 | */ |
|---|
| 590 | DLLEXPORT libtrace_err_t trace_get_err_output(libtrace_out_t *trace); |
|---|
| 591 | |
|---|
| 592 | /** Return if there is an error |
|---|
| 593 | * @param trace the trace file to check the error state on |
|---|
| 594 | * This does not clear the error status, and only returns true or false. |
|---|
| 595 | */ |
|---|
| 596 | DLLEXPORT bool trace_is_err_output(libtrace_out_t *trace); |
|---|
| 597 | |
|---|
| 598 | /** Output an error message to stderr and clear the error status. |
|---|
| 599 | * @param trace the trace with the error to output |
|---|
| 600 | * @param msg the message to prefix to the error |
|---|
| 601 | * This function does clear the error status. |
|---|
| 602 | */ |
|---|
| 603 | DLLEXPORT void trace_perror_output(libtrace_out_t *trace, const char *msg,...); |
|---|
| 604 | |
|---|
| 605 | |
|---|
| 606 | /*@}*/ |
|---|
| 607 | |
|---|
| 608 | /** @name Reading/Writing packets |
|---|
| 609 | * These members deal with creating, reading and writing packets |
|---|
| 610 | * |
|---|
| 611 | * @{ |
|---|
| 612 | */ |
|---|
| 613 | |
|---|
| 614 | /** Create a new packet object |
|---|
| 615 | * |
|---|
| 616 | * @return a pointer to an initialised libtrace_packet_t object |
|---|
| 617 | */ |
|---|
| 618 | DLLEXPORT libtrace_packet_t *trace_create_packet(); |
|---|
| 619 | |
|---|
| 620 | /** Copy a packet |
|---|
| 621 | * @param packet the source packet to copy |
|---|
| 622 | * @return a new packet which has the same content as the source packet |
|---|
| 623 | * @note This always involves a copy, which can be slow. Use of this |
|---|
| 624 | * function should be avoided where possible. |
|---|
| 625 | * @par The reason you would want to use this function is that a zerocopied |
|---|
| 626 | * packet from a device is using the devices memory which may be a limited |
|---|
| 627 | * resource. Copying the packet will cause it to be copied into the systems |
|---|
| 628 | * memory. |
|---|
| 629 | */ |
|---|
| 630 | DLLEXPORT libtrace_packet_t *trace_copy_packet(const libtrace_packet_t *packet); |
|---|
| 631 | |
|---|
| 632 | /** Destroy a packet object |
|---|
| 633 | * |
|---|
| 634 | * sideeffect: sets packet to NULL |
|---|
| 635 | */ |
|---|
| 636 | DLLEXPORT void trace_destroy_packet(libtrace_packet_t **packet); |
|---|
| 637 | |
|---|
| 638 | |
|---|
| 639 | /** Read one packet from the trace into buffer |
|---|
| 640 | * |
|---|
| 641 | * @param trace the libtrace opaque pointer |
|---|
| 642 | * @param packet the packet opaque pointer |
|---|
| 643 | * @return 0 on EOF, negative value on error |
|---|
| 644 | * |
|---|
| 645 | * @note the trace must have been started with trace_start before calling |
|---|
| 646 | * this function |
|---|
| 647 | */ |
|---|
| 648 | DLLEXPORT int trace_read_packet(libtrace_t *trace, libtrace_packet_t *packet); |
|---|
| 649 | |
|---|
| 650 | /** Event types |
|---|
| 651 | * see \ref libtrace_eventobj_t and \ref trace_event |
|---|
| 652 | */ |
|---|
| 653 | typedef enum { |
|---|
| 654 | TRACE_EVENT_IOWAIT, /**< Need to block on fd */ |
|---|
| 655 | TRACE_EVENT_SLEEP, /**< Sleep for some time */ |
|---|
| 656 | TRACE_EVENT_PACKET, /**< packet has arrived */ |
|---|
| 657 | TRACE_EVENT_TERMINATE /**< End of trace */ |
|---|
| 658 | } libtrace_event_t; |
|---|
| 659 | |
|---|
| 660 | /** structure returned by libtrace_event explaining what the current event is */ |
|---|
| 661 | typedef struct libtrace_eventobj_t { |
|---|
| 662 | libtrace_event_t type; /**< event type (iowait,sleep,packet) */ |
|---|
| 663 | int fd; /**< if IOWAIT, the fd to sleep on */ |
|---|
| 664 | double seconds; /**< if SLEEP, the amount of time to sleep for |
|---|
| 665 | */ |
|---|
| 666 | int size; /**< if PACKET, the value returned from |
|---|
| 667 | * trace_read_packet |
|---|
| 668 | */ |
|---|
| 669 | } libtrace_eventobj_t; |
|---|
| 670 | |
|---|
| 671 | /** process a libtrace event |
|---|
| 672 | * @param trace the libtrace opaque pointer |
|---|
| 673 | * @param packet the libtrace_packet opaque pointer |
|---|
| 674 | * @return libtrace_event struct containing the type, and potential |
|---|
| 675 | * fd or seconds to sleep on |
|---|
| 676 | * |
|---|
| 677 | * Type can be: |
|---|
| 678 | * TRACE_EVENT_IOWAIT Waiting on I/O on fd |
|---|
| 679 | * TRACE_EVENT_SLEEP Next event in seconds |
|---|
| 680 | * TRACE_EVENT_PACKET Packet arrived in buffer with size size |
|---|
| 681 | * TRACE_EVENT_TERMINATE Trace terminated (perhaps with an error condition) |
|---|
| 682 | */ |
|---|
| 683 | DLLEXPORT libtrace_eventobj_t trace_event(libtrace_t *trace, |
|---|
| 684 | libtrace_packet_t *packet); |
|---|
| 685 | |
|---|
| 686 | |
|---|
| 687 | /** Write one packet out to the output trace |
|---|
| 688 | * |
|---|
| 689 | * @param trace the libtrace_out opaque pointer |
|---|
| 690 | * @param packet the packet opaque pointer |
|---|
| 691 | * @return the number of bytes written out, if zero or negative then an error has occured. |
|---|
| 692 | */ |
|---|
| 693 | DLLEXPORT int trace_write_packet(libtrace_out_t *trace, const libtrace_packet_t *packet); |
|---|
| 694 | /*@}*/ |
|---|
| 695 | |
|---|
| 696 | /** @name Protocol decodes |
|---|
| 697 | * These functions locate and return a pointer to various headers inside a |
|---|
| 698 | * packet |
|---|
| 699 | * @{ |
|---|
| 700 | */ |
|---|
| 701 | |
|---|
| 702 | /** get a pointer to the link layer |
|---|
| 703 | * @param packet the packet opaque pointer |
|---|
| 704 | * |
|---|
| 705 | * @return a pointer to the link layer, or NULL if there is no link layer |
|---|
| 706 | * |
|---|
| 707 | * @note you should call getLinkType() to find out what type of link layer |
|---|
| 708 | * this is |
|---|
| 709 | */ |
|---|
| 710 | DLLEXPORT SIMPLE_FUNCTION |
|---|
| 711 | void *trace_get_link(const libtrace_packet_t *packet); |
|---|
| 712 | |
|---|
| 713 | /** get a pointer to the IP header (if any) |
|---|
| 714 | * @param packet the packet opaque pointer |
|---|
| 715 | * |
|---|
| 716 | * @return a pointer to the IP header, or NULL if there is not an IP packet |
|---|
| 717 | */ |
|---|
| 718 | DLLEXPORT SIMPLE_FUNCTION |
|---|
| 719 | libtrace_ip_t *trace_get_ip(libtrace_packet_t *packet); |
|---|
| 720 | |
|---|
| 721 | /** Gets a pointer to the transport layer header (if any) |
|---|
| 722 | * @param packet a pointer to a libtrace_packet structure |
|---|
| 723 | * @param[out] proto transport layer protocol |
|---|
| 724 | * |
|---|
| 725 | * @return a pointer to the transport layer header, or NULL if there is no header |
|---|
| 726 | * |
|---|
| 727 | * @note proto may be NULL if proto is unneeded. |
|---|
| 728 | */ |
|---|
| 729 | DLLEXPORT void *trace_get_transport(libtrace_packet_t *packet, uint8_t *proto, |
|---|
| 730 | uint32_t *remaining); |
|---|
| 731 | |
|---|
| 732 | /** Gets a pointer to the payload given a pointer to the IP header |
|---|
| 733 | * @param ip The IP Header |
|---|
| 734 | * @param[out] proto An output variable of the IP protocol |
|---|
| 735 | * @param[in,out] remaining Updated with the number of bytes remaining |
|---|
| 736 | * |
|---|
| 737 | * @return a pointer to the transport layer header, or NULL if header isn't present. |
|---|
| 738 | * |
|---|
| 739 | * Remaining may be NULL. If Remaining is not NULL it must point to the number |
|---|
| 740 | * of bytes captured of the IP header and beyond. It will be updated after this |
|---|
| 741 | * function to the number of bytes remaining after the IP header (and any IP options) |
|---|
| 742 | * have been removed. |
|---|
| 743 | * |
|---|
| 744 | * proto may be NULL if not needed. |
|---|
| 745 | * |
|---|
| 746 | * @note This is similar to trace_get_transport_from_ip in libtrace2 |
|---|
| 747 | */ |
|---|
| 748 | DLLEXPORT void *trace_get_payload_from_ip(libtrace_ip_t *ip, uint8_t *proto, |
|---|
| 749 | uint32_t *remaining); |
|---|
| 750 | |
|---|
| 751 | /** Gets a pointer to the payload given a pointer to a tcp header |
|---|
| 752 | * @param tcp The tcp Header |
|---|
| 753 | * @param[in,out] remaining Updated with the number of bytes remaining |
|---|
| 754 | * |
|---|
| 755 | * @return a pointer to the tcp payload, or NULL if the payload isn't present. |
|---|
| 756 | * |
|---|
| 757 | * Remaining may be NULL. If Remaining is not NULL it must point to the number |
|---|
| 758 | * of bytes captured of the TCP header and beyond. It will be updated after this |
|---|
| 759 | * function to the number of bytes remaining after the TCP header (and any TCP options) |
|---|
| 760 | * have been removed. |
|---|
| 761 | * |
|---|
| 762 | * @note This is similar to trace_get_transport_from_ip in libtrace2 |
|---|
| 763 | */ |
|---|
| 764 | DLLEXPORT void *trace_get_payload_from_tcp(libtrace_tcp_t *tcp, uint32_t *remaining); |
|---|
| 765 | |
|---|
| 766 | /** Gets a pointer to the payload given a pointer to a udp header |
|---|
| 767 | * @param udp The udp Header |
|---|
| 768 | * @param[in,out] remaining Updated with the number of bytes remaining |
|---|
| 769 | * |
|---|
| 770 | * @return a pointer to the udp payload, or NULL if the payload isn't present. |
|---|
| 771 | * |
|---|
| 772 | * Remaining may be NULL. If Remaining is not NULL it must point to the number |
|---|
| 773 | * of bytes captured of the TCP header and beyond. It will be updated after this |
|---|
| 774 | * function to the number of bytes remaining after the TCP header (and any TCP options) |
|---|
| 775 | * have been removed. |
|---|
| 776 | * |
|---|
| 777 | * @note This is similar trace_get_transport_from_ip in libtrace2 |
|---|
| 778 | */ |
|---|
| 779 | DLLEXPORT void *trace_get_payload_from_udp(libtrace_udp_t *udp, uint32_t *remaining); |
|---|
| 780 | |
|---|
| 781 | /** Gets a pointer to the payload given a pointer to a icmp header |
|---|
| 782 | * @param icmp The icmp Header |
|---|
| 783 | * @param[in,out] remaining Updated with the number of bytes remaining |
|---|
| 784 | * |
|---|
| 785 | * @return a pointer to the icmp payload, or NULL if the payload isn't present. |
|---|
| 786 | * |
|---|
| 787 | * Remaining may be NULL. If Remaining is not NULL it must point to the number |
|---|
| 788 | * of bytes captured of the TCP header and beyond. It will be updated after this |
|---|
| 789 | * function to the number of bytes remaining after the TCP header (and any TCP options) |
|---|
| 790 | * have been removed. |
|---|
| 791 | * |
|---|
| 792 | * @note This is similar to trace_get_payload_from_icmp in libtrace2 |
|---|
| 793 | */ |
|---|
| 794 | DLLEXPORT void *trace_get_payload_from_icmp(libtrace_icmp_t *icmp, uint32_t *skipped); |
|---|
| 795 | |
|---|
| 796 | /** get a pointer to the TCP header (if any) |
|---|
| 797 | * @param packet the packet opaque pointer |
|---|
| 798 | * |
|---|
| 799 | * @return a pointer to the TCP header, or NULL if there is not a TCP packet |
|---|
| 800 | */ |
|---|
| 801 | DLLEXPORT SIMPLE_FUNCTION |
|---|
| 802 | libtrace_tcp_t *trace_get_tcp(libtrace_packet_t *packet); |
|---|
| 803 | |
|---|
| 804 | /** get a pointer to the TCP header (if any) given a pointer to the IP header |
|---|
| 805 | * @param ip The IP header |
|---|
| 806 | * @param[in,out] remaining Updated with the number of bytes remaining |
|---|
| 807 | * |
|---|
| 808 | * @return a pointer to the TCP header, or NULL if this is not a TCP packet |
|---|
| 809 | * |
|---|
| 810 | * Remaining may be NULL. If Remaining is not NULL it must point to the number |
|---|
| 811 | * of bytes captured of the TCP header and beyond. It will be updated after this |
|---|
| 812 | * function to the number of bytes remaining after the TCP header (and any TCP options) |
|---|
| 813 | * have been removed. |
|---|
| 814 | * |
|---|
| 815 | * @note The last parameter has changed from libtrace2 |
|---|
| 816 | */ |
|---|
| 817 | DLLEXPORT SIMPLE_FUNCTION |
|---|
| 818 | libtrace_tcp_t *trace_get_tcp_from_ip(libtrace_ip_t *ip, uint32_t *remaining); |
|---|
| 819 | |
|---|
| 820 | /** get a pointer to the UDP header (if any) |
|---|
| 821 | * @param packet the packet opaque pointer |
|---|
| 822 | * |
|---|
| 823 | * @return a pointer to the UDP header, or NULL if this is not a UDP packet |
|---|
| 824 | */ |
|---|
| 825 | DLLEXPORT SIMPLE_FUNCTION |
|---|
| 826 | libtrace_udp_t *trace_get_udp(libtrace_packet_t *packet); |
|---|
| 827 | |
|---|
| 828 | /** get a pointer to the UDP header (if any) given a pointer to the IP header |
|---|
| 829 | * @param ip The IP header |
|---|
| 830 | * @param[in,out] remaining Updated with the number of bytes remaining |
|---|
| 831 | * |
|---|
| 832 | * @return a pointer to the UDP header, or NULL if this is not an UDP packet |
|---|
| 833 | * |
|---|
| 834 | * Remaining may be NULL. If Remaining is not NULL it must point to the number |
|---|
| 835 | * of bytes captured of the TCP header and beyond. It will be updated after this |
|---|
| 836 | * function to the number of bytes remaining after the TCP header (and any TCP options) |
|---|
| 837 | * have been removed. |
|---|
| 838 | * |
|---|
| 839 | * @note Beware the change from libtrace2 from skipped to remaining |
|---|
| 840 | */ |
|---|
| 841 | DLLEXPORT SIMPLE_FUNCTION |
|---|
| 842 | libtrace_udp_t *trace_get_udp_from_ip(libtrace_ip_t *ip,uint32_t *remaining); |
|---|
| 843 | |
|---|
| 844 | /** get a pointer to the ICMP header (if any) |
|---|
| 845 | * @param packet the packet opaque pointer |
|---|
| 846 | * |
|---|
| 847 | * @return a pointer to the ICMP header, or NULL if this is not a ICMP packet |
|---|
| 848 | */ |
|---|
| 849 | DLLEXPORT SIMPLE_FUNCTION |
|---|
| 850 | libtrace_icmp_t *trace_get_icmp(libtrace_packet_t *packet); |
|---|
| 851 | |
|---|
| 852 | /** get a pointer to the ICMP header (if any) given a pointer to the IP header |
|---|
| 853 | * @param ip The IP header |
|---|
| 854 | * @param[in,out] remaining Updated with the number of bytes remaining |
|---|
| 855 | * |
|---|
| 856 | * @return a pointer to the ICMP header, or NULL if this is not an ICMP packet |
|---|
| 857 | * |
|---|
| 858 | * Remaining may be NULL. If Remaining is not NULL it must point to the number |
|---|
| 859 | * of bytes captured of the TCP header and beyond. It will be updated after this |
|---|
| 860 | * function to the number of bytes remaining after the TCP header (and any TCP options) |
|---|
| 861 | * have been removed. |
|---|
| 862 | * |
|---|
| 863 | * @note Beware the change from libtrace2 from skipped to remaining |
|---|
| 864 | */ |
|---|
| 865 | DLLEXPORT SIMPLE_FUNCTION |
|---|
| 866 | libtrace_icmp_t *trace_get_icmp_from_ip(libtrace_ip_t *ip,uint32_t *remaining); |
|---|
| 867 | |
|---|
| 868 | /** Get the destination MAC addres |
|---|
| 869 | * @param packet the packet opaque pointer |
|---|
| 870 | * @return a pointer to the destination mac, (or NULL if there is no |
|---|
| 871 | * destination MAC) |
|---|
| 872 | */ |
|---|
| 873 | DLLEXPORT SIMPLE_FUNCTION |
|---|
| 874 | uint8_t *trace_get_destination_mac(libtrace_packet_t *packet); |
|---|
| 875 | |
|---|
| 876 | /** Get the source MAC addres |
|---|
| 877 | * @param packet the packet opaque pointer |
|---|
| 878 | * @return a pointer to the source mac, (or NULL if there is no source MAC) |
|---|
| 879 | */ |
|---|
| 880 | SIMPLE_FUNCTION |
|---|
| 881 | uint8_t *trace_get_source_mac(libtrace_packet_t *packet); |
|---|
| 882 | |
|---|
| 883 | /** Get the source addres |
|---|
| 884 | * @param packet the packet opaque pointer |
|---|
| 885 | * @param addr a pointer to a sockaddr to store the address in, or NULL to use |
|---|
| 886 | * static storage. |
|---|
| 887 | * @return NULL if there is no source address, or a sockaddr holding a v4 or v6 address |
|---|
| 888 | */ |
|---|
| 889 | DLLEXPORT SIMPLE_FUNCTION |
|---|
| 890 | struct sockaddr *trace_get_source_address(const libtrace_packet_t *packet, |
|---|
| 891 | struct sockaddr *addr); |
|---|
| 892 | |
|---|
| 893 | /** Get the source addres |
|---|
| 894 | * @param packet the packet opaque pointer |
|---|
| 895 | * @param addr a pointer to a sockaddr to store the address in, or NULL to use |
|---|
| 896 | * static storage. |
|---|
| 897 | * @return NULL if there is no source address, or a sockaddr holding a v4 or v6 address |
|---|
| 898 | */ |
|---|
| 899 | DLLEXPORT SIMPLE_FUNCTION |
|---|
| 900 | struct sockaddr *trace_get_destination_address(const libtrace_packet_t *packet, |
|---|
| 901 | struct sockaddr *addr); |
|---|
| 902 | |
|---|
| 903 | /*@}*/ |
|---|
| 904 | |
|---|
| 905 | /** parse an ip or tcp option |
|---|
| 906 | * @param[in,out] ptr the pointer to the current option |
|---|
| 907 | * @param[in,out] len the length of the remaining buffer |
|---|
| 908 | * @param[out] type the type of the option |
|---|
| 909 | * @param[out] optlen the length of the option |
|---|
| 910 | * @param[out] data the data of the option |
|---|
| 911 | * |
|---|
| 912 | * @return bool true if there is another option (and the fields are filled in) |
|---|
| 913 | * or false if this was the last option. |
|---|
| 914 | * |
|---|
| 915 | * This updates ptr to point to the next option after this one, and updates |
|---|
| 916 | * len to be the number of bytes remaining in the options area. Type is updated |
|---|
| 917 | * to be the code of this option, and data points to the data of this option, |
|---|
| 918 | * with optlen saying how many bytes there are. |
|---|
| 919 | * |
|---|
| 920 | * @note Beware of fragmented packets. |
|---|
| 921 | */ |
|---|
| 922 | DLLEXPORT int trace_get_next_option(unsigned char **ptr,int *len, |
|---|
| 923 | unsigned char *type, |
|---|
| 924 | unsigned char *optlen, |
|---|
| 925 | unsigned char **data); |
|---|
| 926 | |
|---|
| 927 | |
|---|
| 928 | /** @name Time |
|---|
| 929 | * These functions deal with time that a packet arrived and return it |
|---|
| 930 | * in various formats |
|---|
| 931 | * @{ |
|---|
| 932 | */ |
|---|
| 933 | /** Get the current time in DAG time format |
|---|
| 934 | * @param packet the packet opaque pointer |
|---|
| 935 | * |
|---|
| 936 | * @return a 64 bit timestamp in DAG ERF format (upper 32 bits are the seconds |
|---|
| 937 | * past 1970-01-01, the lower 32bits are partial seconds) |
|---|
| 938 | * @author Daniel Lawson |
|---|
| 939 | */ |
|---|
| 940 | DLLEXPORT SIMPLE_FUNCTION |
|---|
| 941 | uint64_t trace_get_erf_timestamp(const libtrace_packet_t *packet); |
|---|
| 942 | |
|---|
| 943 | /** Get the current time in struct timeval |
|---|
| 944 | * @param packet the packet opaque pointer |
|---|
| 945 | * |
|---|
| 946 | * @return time that this packet was seen in a struct timeval |
|---|
| 947 | * @author Daniel Lawson |
|---|
| 948 | * @author Perry Lorier |
|---|
| 949 | */ |
|---|
| 950 | DLLEXPORT SIMPLE_FUNCTION |
|---|
| 951 | struct timeval trace_get_timeval(const libtrace_packet_t *packet); |
|---|
| 952 | |
|---|
| 953 | /** Get the current time in floating point seconds |
|---|
| 954 | * @param packet the packet opaque pointer |
|---|
| 955 | * |
|---|
| 956 | * @return time that this packet was seen in 64bit floating point seconds |
|---|
| 957 | * @author Daniel Lawson |
|---|
| 958 | * @author Perry Lorier |
|---|
| 959 | */ |
|---|
| 960 | DLLEXPORT SIMPLE_FUNCTION |
|---|
| 961 | double trace_get_seconds(const libtrace_packet_t *packet); |
|---|
| 962 | |
|---|
| 963 | /** Seek within a trace |
|---|
| 964 | * @param trace trace to seek |
|---|
| 965 | * @param seconds time to seek to |
|---|
| 966 | * @return 0 on success. |
|---|
| 967 | * Make the next packet read to be the first packet to occur at or after the |
|---|
| 968 | * time searched for. This must be called in the configuration state (ie, |
|---|
| 969 | * before trace_start() or after trace_pause(). |
|---|
| 970 | * @note This function may be extremely slow. |
|---|
| 971 | */ |
|---|
| 972 | DLLEXPORT int trace_seek_seconds(libtrace_t *trace, double seconds); |
|---|
| 973 | |
|---|
| 974 | /** Seek within a trace |
|---|
| 975 | * @param trace trace to seek |
|---|
| 976 | * @param tv time to seek to |
|---|
| 977 | * @return 0 on success. |
|---|
| 978 | * Make the next packet read to be the first packet to occur at or after the |
|---|
| 979 | * time searched for. This must be called in the configuration state (ie, |
|---|
| 980 | * before trace_start() or after trace_pause(). |
|---|
| 981 | * @note This function may be extremely slow. |
|---|
| 982 | */ |
|---|
| 983 | DLLEXPORT int trace_seek_timeval(libtrace_t *trace, struct timeval tv); |
|---|
| 984 | |
|---|
| 985 | /** Seek within a trace |
|---|
| 986 | * @param trace trace to seek |
|---|
| 987 | * @param ts erf timestamp |
|---|
| 988 | * @return 0 on success. |
|---|
| 989 | * Make the next packet read to be the first packet to occur at or after the |
|---|
| 990 | * time searched for. This must be called in the configuration state (ie, |
|---|
| 991 | * before trace_start() or after trace_pause(). |
|---|
| 992 | * @note This function may be extremely slow. |
|---|
| 993 | */ |
|---|
| 994 | DLLEXPORT int trace_seek_erf_timestamp(libtrace_t *trace, uint64_t ts); |
|---|
| 995 | |
|---|
| 996 | /*@}*/ |
|---|
| 997 | |
|---|
| 998 | /** @name Sizes |
|---|
| 999 | * This section deals with finding or setting the various different lengths |
|---|
| 1000 | * a packet can have |
|---|
| 1001 | * @{ |
|---|
| 1002 | */ |
|---|
| 1003 | /** Get the size of the packet in the trace |
|---|
| 1004 | * @param packet the packet opaque pointer |
|---|
| 1005 | * @return the size of the packet in the trace |
|---|
| 1006 | * @author Perry Lorier |
|---|
| 1007 | * @note Due to this being a header capture, or anonymisation, this may not |
|---|
| 1008 | * be the same size as the original packet. See get_wire_length() for the |
|---|
| 1009 | * original size of the packet. |
|---|
| 1010 | * @note This can (and often is) different for different packets in a trace! |
|---|
| 1011 | * @note This is sometimes called the "snaplen". |
|---|
| 1012 | * @note The return size refers to the network-level payload of the packet and |
|---|
| 1013 | * does not include any capture headers. For example, an Ethernet packet with |
|---|
| 1014 | * an empty TCP packet will return sizeof(ethernet_header) + sizeof(ip_header) |
|---|
| 1015 | * + sizeof(tcp_header). |
|---|
| 1016 | */ |
|---|
| 1017 | DLLEXPORT SIMPLE_FUNCTION |
|---|
| 1018 | size_t trace_get_capture_length(const libtrace_packet_t *packet); |
|---|
| 1019 | |
|---|
| 1020 | /** Get the size of the packet as it was seen on the wire. |
|---|
| 1021 | * @param packet the packet opaque pointer |
|---|
| 1022 | * @return the size of the packet as it was on the wire. |
|---|
| 1023 | * @note Due to the trace being a header capture, or anonymisation this may |
|---|
| 1024 | * not be the same as the Capture Len. |
|---|
| 1025 | * @note trace_getwire_length \em{includes} FCS. |
|---|
| 1026 | */ |
|---|
| 1027 | DLLEXPORT SIMPLE_FUNCTION |
|---|
| 1028 | size_t trace_get_wire_length(const libtrace_packet_t *packet); |
|---|
| 1029 | |
|---|
| 1030 | /** Get the length of the capture framing headers. |
|---|
| 1031 | * @param packet the packet opaque pointer |
|---|
| 1032 | * @return the size of the packet as it was on the wire. |
|---|
| 1033 | * @author Perry Lorier |
|---|
| 1034 | * @author Daniel Lawson |
|---|
| 1035 | * @note this length corresponds to the difference between the size of a |
|---|
| 1036 | * captured packet in memory, and the captured length of the packet |
|---|
| 1037 | */ |
|---|
| 1038 | DLLEXPORT SIMPLE_FUNCTION |
|---|
| 1039 | size_t trace_get_framing_length(const libtrace_packet_t *packet); |
|---|
| 1040 | |
|---|
| 1041 | /** Truncate ("snap") the packet at the suggested length |
|---|
| 1042 | * @param packet the packet opaque pointer |
|---|
| 1043 | * @param size the new length of the packet |
|---|
| 1044 | * @return the new capture length of the packet, or the original capture |
|---|
| 1045 | * length of the packet if unchanged |
|---|
| 1046 | */ |
|---|
| 1047 | DLLEXPORT size_t trace_set_capture_length(libtrace_packet_t *packet, size_t size); |
|---|
| 1048 | |
|---|
| 1049 | /*@}*/ |
|---|
| 1050 | |
|---|
| 1051 | |
|---|
| 1052 | /** Link layer types |
|---|
| 1053 | * This enumates the various different link types that libtrace understands |
|---|
| 1054 | */ |
|---|
| 1055 | typedef enum { |
|---|
| 1056 | TRACE_TYPE_HDLC_POS = 1, |
|---|
| 1057 | TRACE_TYPE_ETH, /**< 802.3 style Ethernet */ |
|---|
| 1058 | TRACE_TYPE_ATM, |
|---|
| 1059 | TRACE_TYPE_AAL5, |
|---|
| 1060 | TRACE_TYPE_80211, /**< 802.11 frames */ |
|---|
| 1061 | TRACE_TYPE_NONE, |
|---|
| 1062 | TRACE_TYPE_LINUX_SLL, /**< Linux "null" framing */ |
|---|
| 1063 | TRACE_TYPE_PFLOG, /**< FreeBSD's PFlug */ |
|---|
| 1064 | TRACE_TYPE_POS, |
|---|
| 1065 | TRACE_TYPE_80211_PRISM = 12 |
|---|
| 1066 | } libtrace_linktype_t; |
|---|
| 1067 | |
|---|
| 1068 | /** Get the type of the link layer |
|---|
| 1069 | * @param packet the packet opaque pointer |
|---|
| 1070 | * @return libtrace_linktype_t |
|---|
| 1071 | * @author Perry Lorier |
|---|
| 1072 | * @author Daniel Lawson |
|---|
| 1073 | */ |
|---|
| 1074 | DLLEXPORT SIMPLE_FUNCTION |
|---|
| 1075 | libtrace_linktype_t trace_get_link_type(const libtrace_packet_t *packet); |
|---|
| 1076 | |
|---|
| 1077 | /** Set the direction flag, if it has one |
|---|
| 1078 | * @param packet the packet opaque pointer |
|---|
| 1079 | * @param direction the new direction (0,1,2,3) |
|---|
| 1080 | * @return a signed value containing the direction flag, or -1 if this is not supported |
|---|
| 1081 | * @author Daniel Lawson |
|---|
| 1082 | */ |
|---|
| 1083 | DLLEXPORT int8_t trace_set_direction(libtrace_packet_t *packet, int8_t direction); |
|---|
| 1084 | |
|---|
| 1085 | /** Get the direction flag, if it has one |
|---|
| 1086 | * @param packet the packet opaque pointer |
|---|
| 1087 | * @return a signed value containing the direction flag, or -1 if this is not supported |
|---|
| 1088 | * The direction is defined as 0 for packets originating locally (ie, outbound) |
|---|
| 1089 | * and 1 for packets originating remotely (ie, inbound). |
|---|
| 1090 | * Other values are possible, which might be overloaded to mean special things |
|---|
| 1091 | * for a special trace. |
|---|
| 1092 | * @author Daniel Lawson |
|---|
| 1093 | */ |
|---|
| 1094 | DLLEXPORT SIMPLE_FUNCTION |
|---|
| 1095 | int8_t trace_get_direction(const libtrace_packet_t *packet); |
|---|
| 1096 | |
|---|
| 1097 | /** @name BPF |
|---|
| 1098 | * This section deals with using Berkley Packet Filters |
|---|
| 1099 | * @{ |
|---|
| 1100 | */ |
|---|
| 1101 | /** setup a BPF filter |
|---|
| 1102 | * @param filterstring a char * containing the bpf filter string |
|---|
| 1103 | * @return opaque pointer pointer to a libtrace_filter_t object |
|---|
| 1104 | * @author Daniel Lawson |
|---|
| 1105 | * @note The filter is not actually compiled at this point, so no correctness |
|---|
| 1106 | * tests are performed here. trace_bpf_setfilter will always return ok, but |
|---|
| 1107 | * if the filter is poorly constructed an error will be generated when the |
|---|
| 1108 | * filter is actually used |
|---|
| 1109 | */ |
|---|
| 1110 | DLLEXPORT SIMPLE_FUNCTION |
|---|
| 1111 | libtrace_filter_t *trace_bpf_setfilter(const char *filterstring); |
|---|
| 1112 | |
|---|
| 1113 | /** apply a BPF filter |
|---|
| 1114 | * @param filter the filter opaque pointer |
|---|
| 1115 | * @param packet the packet opaque pointer |
|---|
| 1116 | * @return 1 if the filter matches, 0 if it doesn't. |
|---|
| 1117 | * @note Due to the way BPF filters are built, the filter is not actually |
|---|
| 1118 | * compiled until the first time trace_bpf_filter is called. If your filter is |
|---|
| 1119 | * incorrect, it will generate an error message and assert, exiting the |
|---|
| 1120 | * program. This behaviour may change to more graceful handling of this error |
|---|
| 1121 | * in the future. |
|---|
| 1122 | */ |
|---|
| 1123 | DLLEXPORT int trace_bpf_filter(libtrace_filter_t *filter, |
|---|
| 1124 | const libtrace_packet_t *packet); |
|---|
| 1125 | |
|---|
| 1126 | /** destory of BPF filter |
|---|
| 1127 | * @param filter the filter opaque pointer |
|---|
| 1128 | * Deallocate all the resources associated with a BPF filter |
|---|
| 1129 | */ |
|---|
| 1130 | DLLEXPORT void trace_destroy_bpf(libtrace_filter_t *filter); |
|---|
| 1131 | /*@}*/ |
|---|
| 1132 | |
|---|
| 1133 | /** @name Portability |
|---|
| 1134 | * This section has functions that causes annoyances to portability for one |
|---|
| 1135 | * reason or another. |
|---|
| 1136 | * @{ |
|---|
| 1137 | */ |
|---|
| 1138 | |
|---|
| 1139 | /** Convert an ethernet address to a string |
|---|
| 1140 | * @param addr Ethernet address in network byte order |
|---|
| 1141 | * @param buf Buffer to store the ascii representation, or NULL |
|---|
| 1142 | * @return buf, or if buf is NULL then a statically allocated buffer. |
|---|
| 1143 | * |
|---|
| 1144 | * This function is similar to the GNU ether_ntoa_r function, with a few |
|---|
| 1145 | * minor differences. if NULL is passed as buf, then the function will |
|---|
| 1146 | * use an internal static buffer, if NULL isn't passed then the function |
|---|
| 1147 | * will use that buffer instead. |
|---|
| 1148 | * |
|---|
| 1149 | * @note the type of addr isn't struct ether_addr as it is with ether_ntoa_r, |
|---|
| 1150 | * however it is bit compatible so that a cast will work. |
|---|
| 1151 | */ |
|---|
| 1152 | DLLEXPORT char *trace_ether_ntoa(const uint8_t *addr, char *buf); |
|---|
| 1153 | |
|---|
| 1154 | /** Convert a string to an ethernet address |
|---|
| 1155 | * @param buf Ethernet address in hex format delimited with :'s. |
|---|
| 1156 | * @param addr buffer to store the binary representation, or NULL |
|---|
| 1157 | * @return addr, or if addr is NULL, then a statically allocated buffer. |
|---|
| 1158 | * |
|---|
| 1159 | * This function is similar to the GNU ether_aton_r function, with a few |
|---|
| 1160 | * minor differences. if NULL is passed as addr, then the function will |
|---|
| 1161 | * use an internal static buffer, if NULL isn't passed then the function will |
|---|
| 1162 | * use that buffer instead. |
|---|
| 1163 | * |
|---|
| 1164 | * @note the type of addr isn't struct ether_addr as it is with ether_aton_r, |
|---|
| 1165 | * however it is bit compatible so that a cast will work. |
|---|
| 1166 | */ |
|---|
| 1167 | DLLEXPORT uint8_t *trace_ether_aton(const char *buf, uint8_t *addr); |
|---|
| 1168 | |
|---|
| 1169 | /*@}*/ |
|---|
| 1170 | |
|---|
| 1171 | |
|---|
| 1172 | /** Which port is the server port */ |
|---|
| 1173 | typedef enum { |
|---|
| 1174 | USE_DEST, /**< Destination port is the server port */ |
|---|
| 1175 | USE_SOURCE /**< Source port is the server port */ |
|---|
| 1176 | } serverport_t; |
|---|
| 1177 | |
|---|
| 1178 | /** Get the source port |
|---|
| 1179 | * @param packet the packet to read from |
|---|
| 1180 | * @return a port in \em HOST byte order, or equivilent to ports for this |
|---|
| 1181 | * protocol, or 0 if this protocol has no ports. |
|---|
| 1182 | * @author Perry Lorier |
|---|
| 1183 | */ |
|---|
| 1184 | DLLEXPORT SIMPLE_FUNCTION |
|---|
| 1185 | uint16_t trace_get_source_port(const libtrace_packet_t *packet); |
|---|
| 1186 | |
|---|
| 1187 | /** Get the destination port |
|---|
| 1188 | * @param packet the packet to read from |
|---|
| 1189 | * @return a port in \em HOST byte order, or equivilent to ports for this |
|---|
| 1190 | * protocol, or 0 if this protocol has no ports. |
|---|
| 1191 | * @author Perry Lorier |
|---|
| 1192 | */ |
|---|
| 1193 | DLLEXPORT SIMPLE_FUNCTION |
|---|
| 1194 | uint16_t trace_get_destination_port(const libtrace_packet_t *packet); |
|---|
| 1195 | |
|---|
| 1196 | /** hint at the server port in specified protocol |
|---|
| 1197 | * @param protocol the IP layer protocol, eg 6 (tcp), 17 (udp) |
|---|
| 1198 | * @param source the source port from the packet |
|---|
| 1199 | * @param dest the destination port from the packet |
|---|
| 1200 | * @return one of USE_SOURCE or USE_DEST depending on which one you should use |
|---|
| 1201 | * @note ports must be in \em HOST byte order! |
|---|
| 1202 | * @author Daniel Lawson |
|---|
| 1203 | */ |
|---|
| 1204 | DLLEXPORT SIMPLE_FUNCTION |
|---|
| 1205 | int8_t trace_get_server_port(uint8_t protocol, uint16_t source, uint16_t dest); |
|---|
| 1206 | |
|---|
| 1207 | /** Takes a uri and splits it into a format and uridata component. |
|---|
| 1208 | * Primarily for internal use but made available for external use. |
|---|
| 1209 | * @param uri the uri to be parsed |
|---|
| 1210 | * @param format destination location for the format component of the uri |
|---|
| 1211 | * @return 0 if an error occured, otherwise return the uridata component |
|---|
| 1212 | * @author Shane Alcock |
|---|
| 1213 | */ |
|---|
| 1214 | DLLEXPORT const char *trace_parse_uri(const char *uri, char **format); |
|---|
| 1215 | |
|---|
| 1216 | /** RT protocol base format identifiers |
|---|
| 1217 | * This is used to say what kind of packet is being sent over the rt protocol |
|---|
| 1218 | */ |
|---|
| 1219 | enum base_format_t { |
|---|
| 1220 | TRACE_FORMAT_ERF =1, |
|---|
| 1221 | TRACE_FORMAT_PCAP =2, |
|---|
| 1222 | TRACE_FORMAT_PCAPFILE =3, |
|---|
| 1223 | TRACE_FORMAT_WAG =4, |
|---|
| 1224 | TRACE_FORMAT_RT =5, |
|---|
| 1225 | TRACE_FORMAT_LEGACY_ATM =6, |
|---|
| 1226 | TRACE_FORMAT_LEGACY_POS =7, |
|---|
| 1227 | TRACE_FORMAT_LEGACY_ETH =8, |
|---|
| 1228 | TRACE_FORMAT_LINUX_NATIVE =9 |
|---|
| 1229 | }; |
|---|
| 1230 | |
|---|
| 1231 | /** Gets the framing header type for a given packet. |
|---|
| 1232 | * @param packet the packet opaque pointer |
|---|
| 1233 | * @return the format of the packet |
|---|
| 1234 | */ |
|---|
| 1235 | enum base_format_t trace_get_format(struct libtrace_packet_t *packet); |
|---|
| 1236 | |
|---|
| 1237 | #ifdef __cplusplus |
|---|
| 1238 | } /* extern "C" */ |
|---|
| 1239 | #endif /* #ifdef __cplusplus */ |
|---|
| 1240 | #endif /* LIBTRACE_H_ */ |
|---|