source: lib/libtrace.h @ 088533a

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 088533a was 088533a, checked in by Perry Lorier <perry@…>, 15 years ago

Cleanup old legacy link types

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