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