source: lib/libtrace_int.h @ ef5ba20

develop
Last change on this file since ef5ba20 was ef5ba20, checked in by Jacob Van Walraven <jcv9@…>, 2 years ago

add abilty to get custom option from meta packets, add abilty to get entire section from meta packet, meta api now returns libtrace_meta_t structure

  • Property mode set to 100644
File size: 43.3 KB
Line 
1/*
2 *
3 * Copyright (c) 2007-2016 The University of Waikato, Hamilton, New Zealand.
4 * All rights reserved.
5 *
6 * This file is part of libtrace.
7 *
8 * This code has been developed by the University of Waikato WAND
9 * research group. For further information please see http://www.wand.net.nz/
10 *
11 * libtrace is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * libtrace is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 *
24 *
25 */
26/** @file
27 *
28 * @brief Header file containing definitions for structures and functions that
29 * are internal
30 *
31 * @author Daniel Lawson
32 * @author Perry Lorier
33 * @author Shane Alcock
34 *
35 * @version $Id$
36 *
37 * All of the structures and functions defined in this header file are intended
38 * for internal use within Libtrace only. They should not be exported as part
39 * of the library API as we don't want users accessing things like the
40 * contents of the libtrace packet structure directly!
41 */
42#ifndef LIBTRACE_INT_H
43#define LIBTRACE_INT_H
44
45#ifdef __cplusplus
46extern "C" {
47#endif
48
49#include "config.h"
50#include "common.h"
51#include "libtrace_parallel.h"
52#include "wandio.h"
53#include "lt_bswap.h"
54
55#ifdef _MSC_VER
56// warning: deprecated function
57#pragma warning(disable:4996)
58// warning: benign redefinitions of types
59#pragma warning(disable:4142)
60#endif
61
62#ifdef HAVE_INTTYPES_H
63# include <inttypes.h>
64#else
65# include "lt_inttypes.h"
66#endif
67
68#ifdef HAVE_STDDEF_H
69# include <stddef.h>
70#else
71#ifndef WIN32
72# error "Can't find stddev.h -- do you define ptrdiff_t elsewhere?"
73#endif
74#endif
75
76
77#include "rt_protocol.h"
78       
79/* Prefer net/bpf.h over pcap-bpf.h for format_bpf.c on MacOS */
80#ifdef HAVE_NET_BPF_H
81#    include <net/bpf.h>
82#    define HAVE_BPF 1
83#else
84#ifdef HAVE_PCAP_BPF_H
85#  include <pcap-bpf.h>
86#  define HAVE_BPF 1
87#endif
88#endif
89
90#ifdef HAVE_PCAP_H
91#  include <pcap.h>
92#  ifdef HAVE_PCAP_INT_H
93#    include <pcap-int.h>
94#  endif
95#endif
96
97#ifdef HAVE_ZLIB_H
98#  include <zlib.h>
99#endif
100
101#if !HAVE_DECL_STRNDUP
102char *strndup(const char *s, size_t size);
103#endif
104
105#if !HAVE_DECL_STRNCASECMP
106# ifndef HAVE__STRNICMP
107/** A local implementation of strncasecmp (as some systems do not have it) */
108int strncasecmp(const char *str1, const char *str2, size_t n);
109# else
110# define strncasecmp _strnicmp
111# endif
112#endif
113
114#if !HAVE_DECL_SNPRINTF
115# ifndef HAVE_SPRINTF_S
116/** A local implementation of snprintf (as some systems do not have it) */
117int snprintf(char *str, size_t size, const char *format, ...);
118# else
119# define snprintf sprintf_s
120# endif
121#endif
122
123#include "daglegacy.h"
124       
125#ifdef HAVE_DAG_API
126#  include "dagnew.h"
127#  include "dagapi.h"
128#       if DAG_VERSION == 24
129#               include <erftypes.h>
130#       else
131#               include <daginf.h>
132#       endif
133#  include "erftypes.h"
134#else
135#  include "dagformat.h"
136#endif
137
138#ifdef HAVE_LLVM
139#include "bpf-jit/bpf-jit.h"
140#endif
141
142#include "data-struct/ring_buffer.h"
143#include "data-struct/object_cache.h"
144#include "data-struct/vector.h"
145#include "data-struct/message_queue.h"
146#include "data-struct/deque.h"
147#include "data-struct/linked_list.h"
148#include "data-struct/sliding_window.h"
149#include "data-struct/buckets.h"
150#include "pthread_spinlock.h"
151
152//#define RP_BUFSIZE 65536U
153
154#define LIBTRACE_MAX_REPLAY_SPEEDUP 1000
155
156/** Data about the most recent event from a trace file */
157struct libtrace_event_status_t {
158        /** A libtrace packet to store the packet when a PACKET event occurs */
159        libtrace_packet_t *packet;
160
161        /* The walltime when we processed the first packet from the trace */
162        double first_now;
163
164        /* The tracetime of the first packet in the trace */
165        double first_ts;
166
167        /** The size of the current PACKET event */
168        int psize;
169        /** Whether there is a packet stored in *packet above waiting for an
170         * event to occur */
171        bool waiting;
172};
173
174enum thread_types {
175        THREAD_EMPTY,
176        THREAD_HASHER,
177        THREAD_PERPKT,
178        THREAD_REPORTER,
179        THREAD_KEEPALIVE
180};
181
182enum thread_states {
183        THREAD_RUNNING,
184        THREAD_FINISHING,
185        THREAD_FINISHED,
186        THREAD_PAUSED,
187        THREAD_STATE_MAX
188};
189
190enum hash_owner {
191        HASH_OWNED_LIBTRACE,
192        HASH_OWNED_EXTERNAL,
193};
194
195/**
196 * Information of this thread
197 */
198struct libtrace_thread_t {
199        uint64_t accepted_packets; // The number of packets accepted only used if pread
200        uint64_t filtered_packets;
201        // is retreving packets
202        // Set to true once the first packet has been stored
203        bool recorded_first;
204        // For thread safety reason we actually must store this here
205        int64_t tracetime_offset_usec;
206        void* user_data; // TLS for the user to use
207        void* format_data; // TLS for the format to use
208        libtrace_message_queue_t messages; // Message handling
209        libtrace_ringbuffer_t rbuffer; // Input
210        libtrace_t * trace;
211        void* ret;
212        enum thread_types type;
213        enum thread_states state;
214        pthread_t tid;
215        int perpkt_num; // A number from 0-X that represents this perpkt threads number
216                                // in the table, intended to quickly identify this thread
217                                // -1 represents NA (such as the case this is not a perpkt thread)
218} ALIGN_STRUCT(CACHE_LINE_SIZE);
219
220/**
221 * Storage to note time value against each.
222 * Used both internally to do trace time playback
223 * and can be used externally to assist applications which need
224 * a trace starting time such as tracertstats.
225 */
226struct first_packets {
227        pthread_spinlock_t lock;
228        size_t count; // If == perpkt_thread_count threads we have all
229        size_t first; // Valid if count != 0
230        struct {
231                libtrace_packet_t * packet;
232                struct timeval tv;
233        } * packets;
234};
235
236#define TRACE_STATES \
237        X(STATE_NEW) \
238        X(STATE_RUNNING) \
239        X(STATE_PAUSING) \
240        X(STATE_PAUSED) \
241        X(STATE_FINISHED) \
242        X(STATE_FINISHING) \
243        X(STATE_DESTROYED) \
244        X(STATE_JOINED) \
245        X(STATE_ERROR)
246
247#define X(a) a,
248enum trace_state {
249        TRACE_STATES
250};
251#undef X
252
253#define X(a) case a: return #a;
254static inline char *get_trace_state_name(enum trace_state ts){
255        switch(ts) {
256                TRACE_STATES
257                default:
258                        return "UNKNOWN";
259        }
260}
261#undef X
262
263#define READ_EOF 0
264#define READ_ERROR -1
265#define READ_MESSAGE -2
266// Used for inband tick message
267#define READ_TICK -3
268
269/**
270 * Tuning the parallel sizes
271 * See the user documentation trace_set_x
272 */
273struct user_configuration {
274        size_t cache_size;
275        size_t thread_cache_size;
276        bool fixed_count;
277        size_t burst_size;
278        size_t tick_interval;
279        size_t tick_count;
280        size_t perpkt_threads;
281        size_t hasher_queue_size;
282        bool hasher_polling;
283        bool reporter_polling;
284        size_t reporter_thold;
285        bool debug_state;
286};
287#define ZERO_USER_CONFIG(config) memset(&config, 0, sizeof(struct user_configuration));
288
289struct callback_set {
290
291        fn_cb_starting message_starting;
292        fn_cb_dataless message_stopping;
293        fn_cb_dataless message_resuming;
294        fn_cb_dataless message_pausing;
295        fn_cb_packet message_packet;
296        fn_cb_packet message_meta_packet;
297        fn_cb_result message_result;
298        fn_cb_first_packet message_first_packet;
299        fn_cb_tick message_tick_count;
300        fn_cb_tick message_tick_interval;
301        fn_cb_usermessage message_user;
302};
303
304/** A libtrace input trace
305 * @internal
306 */
307struct libtrace_t {
308        /** The capture format for the input trace */
309        struct libtrace_format_t *format; 
310        /** Details of the most recent PACKET event reported by the trace */
311        struct libtrace_event_status_t event;
312        /** Pointer to the "global" data for the capture format module */       
313        void *format_data;             
314        /** A BPF filter to be applied to all packets read by the trace -
315         * used only if the capture format does not support filters natively */
316        struct libtrace_filter_t *filter; 
317        /** The snap length to be applied to all packets read by the trace -
318         * used only if the capture format does not support snapping natively */
319        size_t snaplen;                 
320        /** Speed up the packet rate when using trace_event() to process trace
321         * files by this factor. */
322        int replayspeedup;
323        /** Count of the number of packets returned to the libtrace user */
324        uint64_t accepted_packets;
325        /** Count of the number of packets filtered by libtrace */
326        uint64_t filtered_packets;
327        /** The sequence is like accepted_packets but we don't reset this after a pause. */
328        uint64_t sequence_number;
329        /** The packet read out by the trace, backwards compatibility to allow us to finalise
330         * a packet when the trace is destroyed */
331        libtrace_packet_t *last_packet;
332        /** The filename from the uri for the trace */
333        char *uridata;
334        /** The libtrace IO reader for this trace (if applicable) */
335        io_t *io;
336        /** Error information for the trace */
337        libtrace_err_t err;
338        /** Boolean flag indicating whether the trace has been started */
339        bool started;
340        /** Number of times this trace has been started */
341        int startcount;
342        /** Synchronise writes/reads across this format object and attached threads etc */
343        pthread_mutex_t libtrace_lock;
344        /** Packet read lock, seperate from libtrace_lock as to not block while reading a burst */
345        pthread_mutex_t read_packet_lock;
346        /** State */
347        enum trace_state state;
348        /** Use to control pausing threads and finishing threads etc always used with libtrace_lock */
349        pthread_cond_t perpkt_cond;
350        /** Keeps track of counts of threads in any given state */
351        int perpkt_thread_states[THREAD_STATE_MAX]; 
352
353        /** Set to indicate a perpkt's queue is full as such the writing perpkt cannot proceed */
354        bool perpkt_queue_full;
355        /** Global storage for this trace, shared among all the threads  */
356        void* global_blob;
357        /** The actual freelist */
358        libtrace_ocache_t packet_freelist;
359        /** The hasher function */
360        enum hasher_types hasher_type;
361        /** The hasher function - NULL implies they don't care or balance */
362        fn_hasher hasher;
363        void *hasher_data;
364        enum hash_owner hasher_owner;
365        /** The pread_packet choosen path for the configuration */
366        int (*pread)(libtrace_t *, libtrace_thread_t *, libtrace_packet_t **, size_t);
367
368        libtrace_thread_t hasher_thread;
369        libtrace_thread_t reporter_thread;
370        libtrace_thread_t keepalive_thread;
371        int perpkt_thread_count;
372        libtrace_thread_t * perpkt_threads; // All our perpkt threads
373        // Used to keep track of the first packet seen on each thread
374        struct first_packets first_packets;
375        int tracetime;
376
377        /*
378         * Caches statistic counters in the case that our trace is
379         * paused or stopped before this counter is taken
380         */
381        libtrace_stat_t *stats;
382        struct user_configuration config;
383        libtrace_combine_t combiner;
384       
385        /* Set of callbacks to be executed by per packet threads in response
386         * to various messages. */
387        struct callback_set *perpkt_cbs;
388        /* Set of callbacks to be executed by the reporter thread in response
389         * to various messages. */
390        struct callback_set *reporter_cbs;
391};
392
393#define LIBTRACE_STAT_MAGIC 0x41
394
395void trace_fin_packet(libtrace_packet_t *packet);
396void libtrace_zero_thread(libtrace_thread_t * t);
397void store_first_packet(libtrace_t *libtrace, libtrace_packet_t *packet, libtrace_thread_t *t);
398libtrace_thread_t * get_thread_table(libtrace_t *libtrace);
399
400
401void send_message(libtrace_t *trace, libtrace_thread_t *target,
402                const enum libtrace_messages type,
403                libtrace_generic_t data, libtrace_thread_t *sender);
404
405/** A libtrace output trace
406 * @internal
407 */
408struct libtrace_out_t {
409        /** The capture format for the output trace */
410        struct libtrace_format_t *format;
411        /** Pointer to the "global" data for the capture format module */
412        void *format_data;             
413        /** The filename for the uri for the output trace */
414        char *uridata;                 
415        /** Error information for the output trace */
416        libtrace_err_t err;
417        /** Boolean flag indicating whether the trace has been started */
418        bool started;
419};
420
421/** Sets the error status on an input trace
422 *
423 * @param trace         The input trace to set the error status for
424 * @param errcode       The code for the error - can be a libtrace error code or a regular errno value
425 * @param msg           A message to print when reporting the error
426 */
427void trace_set_err(libtrace_t *trace, int errcode,const char *msg,...) 
428
429                                                                PRINTF(3,4);
430/** Sets the error status on an output trace
431 *
432 * @param trace         The output trace to set the error status for
433 * @param errcode       The code for the error - can be a libtrace error code or a regular errno value
434 * @param msg           A message to print when reporting the error
435 */
436void trace_set_err_out(libtrace_out_t *trace, int errcode, const char *msg,...)
437                                                                PRINTF(3,4);
438
439/** Clears the cached values for a libtrace packet
440 *
441 * @param packet        The libtrace packet that requires a cache reset
442 */
443void trace_clear_cache(libtrace_packet_t *packet);
444
445
446#ifndef PF_RULESET_NAME_SIZE
447#define PF_RULESET_NAME_SIZE 16
448#endif
449
450#ifndef IFNAMSIZ
451#define IFNAMSIZ 16
452#endif
453
454
455/** A local definition of a PFLOG header */
456typedef struct libtrace_pflog_header_t {
457        uint8_t    length;     
458        sa_family_t   af;
459        uint8_t    action;
460        uint8_t    reason;
461        char       ifname[IFNAMSIZ];
462        char       ruleset[PF_RULESET_NAME_SIZE];
463        uint32_t   rulenr;
464        uint32_t   subrulenr;
465        uint8_t    dir;
466        uint8_t    pad[3];
467} PACKED libtrace_pflog_header_t;
468
469/** A libtrace capture format module */
470/* All functions should return -1, or NULL on failure */
471struct libtrace_format_t {
472        /** The name of this module, used in the libtrace URI to identify the
473         * capture format */
474        const char *name;
475        /** The version of this module */
476        const char *version;
477        /** The RT protocol type of this module */
478        enum base_format_t type;
479
480
481        /** Given a filename, return if this is the most likely capture format
482         * (used for devices). Used to "guess" the capture format when the
483         * URI is not fully specified.
484         *
485         * @param fname         The name of the device or file to examine
486         * @return 1 if the name matches the capture format, 0 otherwise
487         */
488        int (*probe_filename)(const char *fname);
489       
490        /** Given a file, looks at the start of the file to determine if this
491         * is the capture format. Used to "guess" the capture format when the
492         * URI is not fully specified.
493         *
494         * @param io            An open libtrace IO reader for the file to check
495         * @return 1 if the file matches the capture format, 0 otherwise
496         */
497        int (*probe_magic)(io_t *io);
498
499        /** Initialises an input trace using the capture format.
500         *
501         * @param libtrace      The input trace to be initialised
502         * @return 0 if successful, -1 in the event of error
503         */
504        int (*init_input)(libtrace_t *libtrace);
505       
506        /** Applies a configuration option to an input trace.
507         *
508         * @param libtrace      The input trace to apply the option to
509         * @param option        The option that is being configured
510         * @param value         A pointer to the value that the option is to be
511         *                      set to
512         * @return 0 if successful, -1 if the option is unsupported or an error
513         * occurs
514         */
515        int (*config_input)(libtrace_t *libtrace,trace_option_t option,void *value);
516        /** Starts or unpauses an input trace - note that this function is
517         * often the one that opens the file or device for reading.
518         *
519         * @param libtrace      The input trace to be started or unpaused
520         * @return 0 if successful, -1 in the event of error */
521        int (*start_input)(libtrace_t *libtrace);
522
523        /** Pauses an input trace - this function should close or detach the
524         * file or device that is being read from.
525         *
526         * @param libtrace      The input trace to be paused
527         * @return 0 if successful, -1 in the event of error
528         */
529        int (*pause_input)(libtrace_t *libtrace);
530
531        /** Initialises an output trace using the capture format.
532         *
533         * @param libtrace      The output trace to be initialised
534         * @return 0 if successful, -1 in the event of error
535         */
536        int (*init_output)(libtrace_out_t *libtrace);
537       
538        /** Applies a configuration option to an output trace.
539         *
540         * @param libtrace      The output trace to apply the option to
541         * @param option        The option that is being configured
542         * @param value         A pointer to the value that the option is to be
543         *                      set to
544         * @return 0 if successful, -1 if the option is unsupported or an error
545         * occurs
546         * */
547        int (*config_output)(libtrace_out_t *libtrace, trace_option_output_t option, void *value);
548
549        /** Starts an output trace - note that this function is often the one
550         * that opens the file or device for writing.
551         *
552         * @param libtrace      The output trace to be started
553         * @return 0 if successful, -1 if an error occurs
554         *
555         * There is no pause for output traces, as writing is not performed
556         * asynchronously.
557         */
558        int (*start_output)(libtrace_out_t *libtrace);
559
560        /** Concludes an input trace and cleans up the capture format data.
561         *
562         * @param libtrace      The input trace to be concluded
563         * @return 0 if successful, -1 if an error occurs
564         *
565         * Libtrace will call the pause_input function if the input trace is
566         * currently active prior to calling this function.
567         */
568        int (*fin_input)(libtrace_t *libtrace);
569
570        /** Concludes an output trace and cleans up the capture format data.
571         *
572         * @param libtrace      The output trace to be concluded
573         * @return 0 if successful, -1 if an error occurs
574         */
575        int (*fin_output)(libtrace_out_t *libtrace);
576
577        /** Reads the next packet from an input trace into the provided packet
578         * structure.
579         *
580         * @param libtrace      The input trace to read from
581         * @param packet        The libtrace packet to read into
582         * @return The size of the packet read (in bytes) including the capture
583         * framing header, or -1 if an error occurs. 0 is returned in the
584         * event of an EOF or -2 in the case of interrupting the parallel API.
585         *
586         * If no packets are available for reading, this function should block
587         * until one appears or return 0 if the end of a trace file has been
588         * reached.
589         */
590        int (*read_packet)(libtrace_t *libtrace, libtrace_packet_t *packet);
591       
592        /** Converts a buffer containing a packet record into a libtrace packet
593         *
594         * @param libtrace      An input trace in the capture format for the
595         *                      packet
596         * @param packet        A libtrace packet to put the prepared packet
597         *                      into
598         * @param buffer        The buffer containing the packet record
599         *                      (including the capture format header)
600         * @param rt_type       The RT type for the packet
601         * @param flags         Flags describing properties that should be
602         *                      applied to the new packet
603         * @return 0 if successful, -1 if an error occurs.
604         *
605         * Updates internal trace and packet details, such as payload pointers,
606         * loss counters and packet types to match the packet record provided
607         * in the buffer. This is a zero-copy function.
608         *
609         * Intended (at this stage) only for internal use, particularly by
610         * RT which needs to decapsulate RT packets */
611        int (*prepare_packet)(libtrace_t *libtrace, libtrace_packet_t *packet,
612                        void *buffer, libtrace_rt_types_t rt_type, 
613                        uint32_t flags);
614       
615        /** Frees any resources allocated by the capture format module for a
616         * libtrace packet.
617         *
618         * @param The packet to be finalised
619         *       */
620        void (*fin_packet)(libtrace_packet_t *packet);
621
622        /** Write a libtrace packet to an output trace.
623         *
624         * @param libtrace      The output trace to write the packet to
625         * @param packet        The packet to be written out
626         * @return The number of bytes written, or -1 if an error occurs
627         */
628        int (*write_packet)(libtrace_out_t *libtrace, libtrace_packet_t *packet);
629
630        /** Flush any buffered output for an output trace.
631         *
632         * @param libtrace      The output trace to be flushed
633         */
634        int (*flush_output)(libtrace_out_t *libtrace);
635
636        /** Returns the libtrace link type for a packet.
637         *
638         * @param packet        The packet to get the link type for
639         * @return The libtrace link type, or -1 if this link type is unknown
640         */ 
641        libtrace_linktype_t (*get_link_type)(const libtrace_packet_t *packet);
642
643        /** Returns the direction of a packet.
644         *
645         * @param packet        The packet to get the direction for
646         * @return The direction of the packet, or -1 if no direction tag is
647         * present or an error occurs
648         */ 
649        libtrace_direction_t (*get_direction)(const libtrace_packet_t *packet);
650       
651        /** Sets the direction of a packet.
652         *
653         * @param packet        The packet to set the direction for
654         * @param direction     The direction to assign to the packet
655         * @return The updated direction for the packet, or -1 if an error
656         * occurs
657         *
658         * @note Some capture formats do not feature direction tagging, so it
659         * will not make sense to implement a set_direction function for them.
660         */ 
661        libtrace_direction_t (*set_direction)(libtrace_packet_t *packet, libtrace_direction_t direction);
662       
663        /** Returns the timestamp for a packet in the ERF timestamp format.
664         *
665         * @param packet        The packet to get the timestamp from
666         * @return The 64-bit ERF timestamp
667         *
668         * @note Each format must implement at least one of the four "get
669         * timestamp" functions.
670         *
671         * If not implemented, libtrace will convert the result of one of the
672         * other timestamp functions into the appropriate format instead.
673         * This means each capture format only needs to implement the most
674         * sensible of the four and let libtrace handle any conversions.
675         *
676         */
677        uint64_t (*get_erf_timestamp)(const libtrace_packet_t *packet);
678
679        /** Returns the timestamp for a packet in the timeval format
680         *
681         * @param packet        The packet to get the timestamp from
682         * @return The timestamp from the packet as a timeval
683         *
684         * @note Each format must implement at least one of the four "get
685         * timestamp" functions.
686         *
687         * If not implemented, libtrace will convert the result of one of the
688         * other timestamp functions into the appropriate format instead.
689         * This means each capture format only needs to implement the most
690         * sensible of the four and let libtrace handle any conversions.
691         */
692        struct timeval (*get_timeval)(const libtrace_packet_t *packet);
693       
694        /** Returns the timestamp for a packet in the timespec format.
695         *
696         * @param packet        The packet to get the timestamp from
697         * @return The timestamp from the packet as a timespec
698         *
699         * @note Each format must implement at least one of the four "get
700         * timestamp" functions.
701         *
702         * If not implemented, libtrace will convert the result of one of the
703         * other timestamp functions into the appropriate format instead.
704         * This means each capture format only needs to implement the most
705         * sensible of the four and let libtrace handle any conversions.
706         */
707        struct timespec (*get_timespec)(const libtrace_packet_t *packet);
708       
709        /** Returns the timestamp for a packet in floating point seconds.
710         *
711         * @param packet        The packet to get the timestamp from
712         * @return The timestamp from the packet as a floating point number of
713         * seconds since 1970-01-01 00:00:00 UTC
714         *
715         * @note Each format must implement at least one of the four "get
716         * timestamp" functions.
717         *
718         * If not implemented, libtrace will convert the result of one of the
719         * other timestamp functions into the appropriate format instead.
720         * This means each capture format only needs to implement the most
721         * sensible of the four and let libtrace handle any conversions.
722         */
723        double (*get_seconds)(const libtrace_packet_t *packet);
724       
725        /**
726         */
727        void *(*get_meta_section)(libtrace_packet_t *packet, uint32_t section_type);
728        void *(*get_meta_section_item)(libtrace_packet_t *packet, uint32_t section_type,
729                uint16_t section);
730
731        /** Moves the read pointer to a certain ERF timestamp within an input
732         * trace file.
733         *
734         * @param trace         The input trace to seek within
735         * @param timestamp     The timestamp to seek to, as an ERF timestamp
736         *
737         * @return 0 on success, -1 on failure.
738         *
739         * The next packet read from this trace will now be the first packet
740         * to have a timestamp equal to or greater than the provided timestamp.
741         *
742         * @note Each format that supports seeking must implement at least one
743         * of the seek functions.
744         *
745         * If not implemented, libtrace will convert the timestamp into the
746         * appropriate format to use a seek function that has been implemented.
747         * This means each capture format only needs to implement the seek
748         * function that matches the native timestamp format for that capture.
749         *
750         */
751        int (*seek_erf)(libtrace_t *trace, uint64_t timestamp);
752        /** Moves the read pointer to a certain timestamp represented using a
753         * timeval within an input trace file.
754         *
755         * @param trace         The input trace to seek within
756         * @param timestamp     The timestamp to seek to, as a timeval
757         *
758         * @return 0 on success, -1 on failure.
759         *
760         * The next packet read from this trace will now be the first packet
761         * to have a timestamp equal to or greater than the provided timestamp.
762         *
763         * @note Each format that supports seeking must implement at least one
764         * of the seek functions.
765         *
766         * If not implemented, libtrace will convert the timestamp into the
767         * appropriate format to use a seek function that has been implemented.
768         * This means each capture format only needs to implement the seek
769         * function that matches the native timestamp format for that capture.
770         *
771         */
772        int (*seek_timeval)(libtrace_t *trace, struct timeval tv);
773       
774        /** Moves the read pointer to a certain timestamp represented using
775         * floating point seconds within an input trace file.
776         *
777         * @param trace         The input trace to seek within
778         * @param timestamp     The timestamp to seek to, as floating point
779         *                      seconds since 1970-01-01 00:00:00 UTC
780         *
781         * @return 0 on success, -1 on failure.
782         *
783         * The next packet read from this trace will now be the first packet
784         * to have a timestamp equal to or greater than the provided timestamp.
785         *
786         * @note Each format that supports seeking must implement at least one
787         * of the seek functions.
788         *
789         * If not implemented, libtrace will convert the timestamp into the
790         * appropriate format to use a seek function that has been implemented.
791         * This means each capture format only needs to implement the seek
792         * function that matches the native timestamp format for that capture.
793         *
794         */
795        int (*seek_seconds)(libtrace_t *trace, double seconds);
796       
797        /** Returns the payload length of the captured packet record.
798         *
799         * @param packet        The packet to get the capture length from
800         * @return The capture length for the packet, or -1 if an error occurs
801         *
802         * Capture length is the current size of the packet record itself,
803         * following any truncation that may have occurred during the capture
804         * process. This length does not include the capture format framing
805         * header.
806         */
807        int (*get_capture_length)(const libtrace_packet_t *packet);
808
809        /** Returns the original length of the packet as it was on the wire.
810         *
811         * @param packet        The packet to get the wire length from
812         * @return The length of the packet on the wire at the time of capture,
813         * or -1 if an error occurs
814         *
815         * Wire length is the original size of the packet prior to any
816         * truncation that may have occurred as part of the capture process.
817         * This length does not include the capture format framing header.
818         */
819        int (*get_wire_length)(const libtrace_packet_t *packet);
820       
821        /** Returns the length of the capture format framing header
822         *
823         * @param packet        The packet to get the framing length from
824         * @return The length of the framing header, or -1 if an error occurs
825         *
826         * The framing header is the extra metadata that the capture process
827         * records about a packet.  The framing length does not include any
828         * of the packet payload itself. The total size of the packet record
829         * can be calculated be adding this value with the capture length.
830         */
831        int (*get_framing_length)(const libtrace_packet_t *packet);
832
833        /** Sets the capture length for a packet.
834         *
835         * @param packet        The packet to adjust the capture length for.
836         * @param size          The new capture length
837         * @return The new capture length of the packet, or -1 if an error
838         * occurs
839         *
840         * @note This function should only reduce the capture length. If the
841         * provided length is larger than the current capture length, -1 should
842         * be returned.
843         */
844        size_t (*set_capture_length)(struct libtrace_packet_t *packet,size_t size);
845        /** Returns the number of packets observed by an input trace.
846         *
847         * @param trace         The input trace to get the packet count for
848         * @return The number of packets observed by an input trace, or
849         * UINT64_MAX if the number is unknown
850         *
851         * This count includes packets that have been filtered and dropped.
852         */
853        uint64_t (*get_received_packets)(libtrace_t *trace);
854
855        /** Returns the number of packets filtered by an input trace.
856         *
857         * @param trace         The input trace to get the filtered count for
858         * @return The number of packets filtered by the input trace, or
859         * UINT64_MAX if the number is unknown
860         *
861         */
862        uint64_t (*get_filtered_packets)(libtrace_t *trace);
863       
864        /** Returns the number of packets dropped by an input trace.
865         *
866         * @param trace         The input trace to get the dropped count for
867         * @return The number of packets dropped by the input trace, or
868         * UINT64_MAX if the number is unknown
869         *
870         */
871        uint64_t (*get_dropped_packets)(libtrace_t *trace);
872
873        /** Returns statistics about a trace.
874         *
875         * @param trace The libtrace object
876         * @param stat [in,out] A statistics structure ready to be filled
877         *
878         * The filtered and accepted statistics will be set to the values
879         * stored in the library. All other statistics are not set.
880         *
881         * @note If filtering of packets is performed by a trace and the number
882         * of filtered packets is unknown this should be marked as invalid by
883         * the format.
884         */
885        void (*get_statistics)(libtrace_t *trace, libtrace_stat_t *stat);
886       
887        /** Returns the file descriptor used by the input trace.
888         *
889         * @param trace         The input trace to get the file descriptor for
890         * @return The file descriptor used by the input trace to read packets
891         *
892         */
893        int (*get_fd)(const libtrace_t *trace);
894       
895        /** Returns the next libtrace event for the input trace.
896         *
897         * @param trace         The input trace to get the next event from
898         * @param packet        A libtrace packet to read a packet into
899         * @return A libtrace event describing the event that occured
900         *
901         * The event API allows for non-blocking reading of packets from an
902         * input trace. If a packet is available and ready to be read, a packet
903         * event should be returned. Otherwise a sleep or fd event should be
904         * returned to indicate that the caller needs to wait. If the input
905         * trace has an error or reaches EOF, a terminate event should be
906         * returned.
907         */
908        struct libtrace_eventobj_t (*trace_event)(libtrace_t *trace, libtrace_packet_t *packet);       
909
910        /** Prints some useful help information to standard output. */
911        void (*help)(void);
912       
913        /** Next pointer, should always be NULL - used by the format module
914         * manager. */
915        struct libtrace_format_t *next;
916
917        /** Holds information about the trace format */
918        struct libtrace_info_t info;
919
920        /**
921         * Starts or unpauses an input trace in parallel mode - note that
922         * this function is often the one that opens the file or device for
923         * reading.
924         *
925         * @param libtrace      The input trace to be started or unpaused
926         * @return 0 upon success.
927         *         Otherwise in event of an error -1 is returned.
928         *
929         */
930        int (*pstart_input)(libtrace_t *trace);
931       
932        /**
933         * Read a batch of packets from the input stream related to thread.
934         * At most read nb_packets, however should return with less if packets
935         * are not waiting. However still must return at least 1, 0 still indicates
936         * EOF.
937         *
938         * @param libtrace      The input trace
939         * @param t     The thread
940         * @param packets       An array of packets
941         * @param nb_packets    The number of packets in the array (the maximum to read)
942         * @return The number of packets read, or 0 in the case of EOF or -1 in error or -2 to represent
943         * interrupted due to message waiting before packets had been read.
944         */
945        int (*pread_packets)(libtrace_t *trace, libtrace_thread_t *t, libtrace_packet_t **packets, size_t nb_packets);
946       
947        /** Pause a parallel trace
948         *
949         * @param libtrace      The input trace to be paused
950         */
951        int (*ppause_input)(libtrace_t *trace);
952       
953        /** Called after all threads have been paused, Finish (close) a parallel trace
954         *
955         * @param libtrace      The input trace to be stopped
956         */
957        int (*pfin_input)(libtrace_t *trace);
958
959        /**
960         * Register a thread for use with the format or using the packets produced
961         * by it. This is NOT only used for threads reading packets in fact all
962         * threads use this.
963         *
964         * The libtrace lock is not held by this format but can be aquired
965         * by the format.
966         *
967         * Some use cases include setting up any thread local storage required for
968         * to read packets and free packets. For DPDK we require any thread that
969         * may release or read a packet to have have an internal number associated
970         * with it.
971         *
972         * The thread type can be used to see if this thread is going to be used
973         * to read packets or otherwise.
974         *
975         * @return 0 if successful, -1 if the option is unsupported or an error
976         * occurs (such as a maximum of threads being reached)
977         */
978        int (*pregister_thread)(libtrace_t *libtrace, libtrace_thread_t *t, bool reader);
979
980        /**
981         * If needed any memory allocated with pregister_thread can be released
982         * in this function. The thread will be destroyed directly after this
983         * function is called.
984         */
985        void (*punregister_thread)(libtrace_t *libtrace, libtrace_thread_t *t);
986
987        /** Returns statistics for a single thread.
988         *
989         * @param trace The libtrace object
990         * @param t The thread to return statistics for
991         * @param stat [in,out] A statistics structure ready to be filled
992         *
993         * The filtered and accepted statistics will be set to the values
994         * stored in the library. All other statistics are not set.
995         *
996         * @note If filtering of packets is performed by a trace and the number
997         * of filtered packets is unknown this should be marked as invalid by
998         * the format.
999         */
1000        void (*get_thread_statistics)(libtrace_t *libtrace,
1001                                      libtrace_thread_t *t,
1002                                      libtrace_stat_t *stat);
1003};
1004
1005/** Macro to zero out a single thread format */
1006#define NON_PARALLEL(live) \
1007        {live, 1},              /* trace info */ \
1008        NULL,                   /* pstart_input */ \
1009        NULL,                   /* pread_packet */ \
1010        NULL,                   /* ppause_input */ \
1011        NULL,                   /* pfin_input */ \
1012        NULL,                   /* pregister_thread */ \
1013        NULL,                   /* punregister_thread */ \
1014        NULL,                   /* get_thread_statistics */
1015
1016/** The list of registered capture formats */
1017//extern struct libtrace_format_t *form;
1018
1019/** Specifies whether any blocking packet readers should cease reading
1020 * immediately
1021 */
1022extern volatile int libtrace_halt;
1023
1024/**
1025 * Used by a format to check if trace_interrupt or if a trace_pause/stop has
1026 * been called. Provides backwards compatibility with traditional read
1027 * functions when trace_read_packet() is used by the parallel API.
1028 *
1029 * Returns -1 if not halting otherwise returns the code that the read
1030 * operation should pass on.
1031 */
1032static inline int is_halted(libtrace_t *trace) {
1033        if (!(libtrace_halt || trace->state == STATE_PAUSING)) {
1034                return -1;
1035        } else if (libtrace_halt) {
1036                return READ_EOF;
1037        } else {
1038                return READ_MESSAGE;
1039        }
1040}
1041
1042/** Registers a new capture format module.
1043 *
1044 * @param format        The format module to be registered
1045 */
1046void register_format(struct libtrace_format_t *format);
1047
1048/** Converts a timeval into a timestamp in microseconds since the epoch.
1049 *
1050 * @param tv    The timeval to be converted.
1051 * @return A 64 bit timestamp in microseconds since the epoch.
1052 */
1053uint64_t tv_to_usec(const struct timeval *tv);
1054
1055/** Converts a PCAP DLT into a libtrace link type.
1056 *
1057 * @param linktype      The PCAP DLT to be converted
1058 * @return The libtrace link type that is equivalent to the provided DLT, or
1059 * -1 if the DLT is unknown
1060 */
1061libtrace_linktype_t pcap_linktype_to_libtrace(libtrace_dlt_t linktype);
1062
1063/** Converts a PCAP DLT into an RT protocol type.
1064 *
1065 * @param linktype      The PCAP DLT to be converted
1066 * @return The RT type that is equivalent to the provided DLT
1067 */
1068libtrace_rt_types_t pcap_linktype_to_rt(libtrace_dlt_t linktype);
1069
1070/** Converts a PCAP-NG DLT into an RT protocol type.
1071 *
1072 * @param linktype      The PCAP DLT to be converted
1073 * @return The RT type that is equivalent to the provided DLT
1074 */
1075libtrace_rt_types_t pcapng_linktype_to_rt(libtrace_dlt_t linktype);
1076
1077/** Converts a libtrace link type into a PCAP linktype.
1078 *
1079 * @param type          The libtrace link type to be converted
1080 * @return The PCAP linktype that is equivalent to the provided libtrace link
1081 * type, or -1 if the link type is unknown
1082 */
1083libtrace_dlt_t libtrace_to_pcap_linktype(libtrace_linktype_t type);
1084
1085/** Converts a libtrace link type into a PCAP DLT.
1086 *
1087 * @param type          The libtrace link type to be converted
1088 * @return The PCAP DLT that is equivalent to the provided libtrace link
1089 * type, or -1 if the link type is unknown
1090 */
1091libtrace_dlt_t libtrace_to_pcap_dlt(libtrace_linktype_t type);
1092
1093/** Converts an RT protocol type into a PCAP DLT.
1094 *
1095 * @param rt_type       The RT type to be converted
1096 * @return The PCAP DLT that is equivalent to the provided RT protocol
1097 */
1098libtrace_dlt_t rt_to_pcap_linktype(libtrace_rt_types_t rt_type);
1099
1100/** Converts a PCAP DLT into an RT protocol type for the BPF format.
1101 *
1102 * @param linktype      The PCAP DLT to be converted
1103 * @return The RT type that is equivalent to the provided DLT for BPF
1104 */
1105libtrace_rt_types_t bpf_linktype_to_rt(libtrace_dlt_t linktype);
1106
1107/** Converts an ERF type into a libtrace link type.
1108 *
1109 * @param erf           The ERF type to be converted
1110 * @return The libtrace link type that is equivalent to the provided ERF type,
1111 * or -1 if the ERF type is unknown
1112 */
1113libtrace_linktype_t erf_type_to_libtrace(uint8_t erf);
1114
1115/** Converts a libtrace link type into an ERF type.
1116 *
1117 * @param linktype      The libtrace link type to be converted
1118 * @return The ERF type that is equivalent to the provided libtrace link type,
1119 * or -1 if the link type cannot be matched to an ERF type.
1120 */
1121uint8_t libtrace_to_erf_type(libtrace_linktype_t linktype);
1122
1123/** Converts an ARPHRD type into a libtrace link type.
1124 *
1125 * @param arphrd        The ARPHRD type to be converted
1126 * @return The libtrace link type that is equivalent to the provided ARPHRD
1127 * type, or -1 if the ARPHRD type is unknown
1128 */
1129libtrace_linktype_t arphrd_type_to_libtrace(unsigned int arphrd);
1130
1131/** Converts a libtrace link type into an ARPHRD type.
1132 *
1133 * @param type          The libtrace link type to be converted
1134 * @return The ARPHRD type that is equivalent to the provided libtrace link
1135 * type, or -1 if the link type cannot be matched to an ARPHRD type
1136 */
1137unsigned int libtrace_to_arphrd_type(libtrace_linktype_t type);
1138
1139/** Converts a libtrace packet to the Linux SLL type.
1140 *
1141 * @param packet        The packet to be promoted
1142 *
1143 * @note This will involve memcpy() so use sparingly.
1144 *
1145 * This function prepends a Linux SLL header to a packet so that we can store
1146 * direction tagging information.
1147 */
1148void promote_packet(libtrace_packet_t *packet);
1149
1150/** Attempts to demote a packet by removing the first header.
1151 *
1152 * @param packet        The packet to be demoted
1153 * @return True if the packet was demoted, false otherwise.
1154 *
1155 * Essentially the opposite of promote_packet, except that it will also remove
1156 * an ATM header as well as Linux SLL.
1157 *
1158 */
1159bool demote_packet(libtrace_packet_t *packet);
1160
1161/** Returns a pointer to the header following a Linux SLL header.
1162 *
1163 * @param link          A pointer to the Linux SLL header to be skipped
1164 * @param[out] arphrd_type      The arp hardware type of the packet
1165 * @param[out] next_header      The ethertype of the next header
1166 * @param[in,out] remaining     Updated with the number of captured bytes
1167 *                              remaining
1168 * @return A pointer to the header following the Linux SLL header, or NULL if
1169 * no subsequent header is present.
1170 *
1171 * Remaining must point to the number of bytes captured from the Linux SLL
1172 * header and beyond.  It will be decremented by the number of bytes skipped
1173 * to find the payload.
1174 *
1175 * If the Linux SLL header is complete but there are zero bytes of payload
1176 * after the end of the header, a pointer to where the payload would be is
1177 * returned and remaining will be set to zero. If the Linux SLL header is
1178 * incomplete (truncated), then NULL is returned and remaining will be set to
1179 * 0. Therefore, it is very important to check the value of remaining after
1180 * calling this function.
1181 */     
1182void *trace_get_payload_from_linux_sll(const void *link,
1183                uint16_t *arphrd_type, 
1184                uint16_t *next_header, 
1185                uint32_t *remaining);
1186
1187/** Returns a pointer to the header following an ATM header.
1188 *
1189 * @param link          A pointer to the ATM header to be skipped
1190 * @param[out] type     The ethertype of the next header
1191 * @param[in,out] remaining     Updated with the number of captured bytes
1192 *                              remaining
1193 * @return A pointer to the header following the ATM header, or NULL if
1194 * no subsequent header is present.
1195 *
1196 * Remaining must point to the number of bytes captured from the ATM header
1197 * and beyond.  It will be decremented by the number of bytes skipped to find
1198 * the payload.
1199 *
1200 * If the ATM header is complete but there are zero bytes of payload
1201 * after the end of the header, a pointer to where the payload would be is
1202 * returned and remaining will be set to zero. If the ATM header is
1203 * incomplete (truncated), then NULL is returned and remaining will be set to
1204 * 0. Therefore, it is very important to check the value of remaining after
1205 * calling this function.
1206 */     
1207DLLEXPORT void *trace_get_payload_from_atm(void *link, uint8_t *type, 
1208                uint32_t *remaining);
1209
1210#ifdef HAVE_BPF
1211/* A type encapsulating a bpf filter
1212 * This type covers the compiled bpf filter, as well as the original filter
1213 * string
1214 *
1215 */
1216
1217/** Internal representation of a BPF filter */
1218struct libtrace_filter_t {
1219        struct bpf_program filter;      /**< The BPF program itself */
1220        char * filterstring;            /**< The filter string */
1221        int flag;                       /**< Indicates if the filter is valid */
1222        struct bpf_jit_t *jitfilter;
1223};
1224#else
1225/** BPF not supported by this system, but we still need to define a structure
1226 * for the filter */
1227struct libtrace_filter_t {};
1228#endif
1229
1230/** Local definition of a PCAP header */
1231typedef struct libtrace_pcapfile_pkt_hdr_t {
1232        uint32_t ts_sec;        /* Seconds portion of the timestamp */
1233        uint32_t ts_usec;       /* Microseconds portion of the timestamp */
1234        uint32_t caplen;        /* Capture length of the packet */
1235        uint32_t wirelen;       /* The wire length of the packet */
1236} libtrace_pcapfile_pkt_hdr_t;
1237
1238#ifdef HAVE_DAG
1239/** Constructor for the DAG format module */
1240void dag_constructor(void);
1241#endif
1242/** Constructor for the ERF format module */
1243void erf_constructor(void);
1244/** Constructor for the TSH format module */
1245void tsh_constructor(void);
1246/** Constructor for the Legacy DAG format module */
1247void legacy_constructor(void);
1248/** Constructor for the Linux Native format module */
1249void linuxnative_constructor(void);
1250/** Constructor for the Linux Ring format module */
1251void linuxring_constructor(void);
1252/** Constructor for the PCAP format module */
1253void pcap_constructor(void);
1254/** Constructor for the PCAP File format module */
1255void pcapfile_constructor(void);
1256/** Constructor for the PCAP-NG File format module */
1257void pcapng_constructor(void);
1258/** Constructor for the RT format module */
1259void rt_constructor(void);
1260/** Constructor for the DUCK format module */
1261void duck_constructor(void);
1262/** Constructor for the ATM Header format module */
1263void atmhdr_constructor(void);
1264/** Constructor for the network DAG format module */
1265void ndag_constructor(void);
1266/** Constructor for the live ETSI over TCP format module */
1267void etsilive_constructor(void);
1268#ifdef HAVE_BPF
1269/** Constructor for the BPF format module */
1270void bpf_constructor(void);
1271#endif
1272#if HAVE_DPDK
1273/** Constructor for Intels DPDK format module */
1274void dpdk_constructor(void);
1275
1276/** Constructor for receiving network DAG via Intels DPDK format module */
1277void dpdkndag_constructor(void);
1278
1279#endif
1280
1281/** Extracts the RadioTap flags from a wireless link header
1282 *
1283 * @param link          A pointer to the wireless link header
1284 * @param linktype      The link type of the wireless header
1285 * @param[out] flags    Space to store the extracted flags
1286 * @return True if libtrace was able to extract flags from the link header,
1287 * false otherwise.
1288 *
1289 * This function has been left internal because it is not portable across
1290 * drivers.
1291 */
1292bool trace_get_wireless_flags(void *link, libtrace_linktype_t linktype, uint8_t *flags);
1293#define TRACE_RADIOTAP_F_FCS 0x10
1294       
1295#ifdef __cplusplus
1296}
1297#endif
1298
1299#endif /* LIBTRACE_INT_H */
Note: See TracBrowser for help on using the repository browser.