source: lib/libtrace_int.h @ 29ba7c2

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivelibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 29ba7c2 was 29bbef0, checked in by Richard Sanger <rsangerarj@…>, 7 years ago

My work from over summer, with a few things tidied up and updated to include recent commits/patches to bring this up to date. Still very much work in progress.

  • Property mode set to 100644
File size: 39.5 KB
Line 
1/*
2 * This file is part of libtrace
3 *
4 * Copyright (c) 2007,2008,2009,2010 The University of Waikato, Hamilton,
5 * New Zealand.
6 *
7 * Authors: Daniel Lawson
8 *          Perry Lorier
9 *          Shane Alcock
10 *         
11 * All rights reserved.
12 *
13 * This code has been developed by the University of Waikato WAND
14 * research group. For further information please see http://www.wand.net.nz/
15 *
16 * libtrace is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * libtrace is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with libtrace; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29 *
30 * $Id$
31 *
32 */
33
34/** @file
35 *
36 * @brief Header file containing definitions for structures and functions that
37 * are internal
38 *
39 * @author Daniel Lawson
40 * @author Perry Lorier
41 * @author Shane Alcock
42 *
43 * @version $Id$
44 *
45 * All of the structures and functions defined in this header file are intended
46 * for internal use within Libtrace only. They should not be exported as part
47 * of the library API as we don't want users accessing things like the
48 * contents of the libtrace packet structure directly!
49 */
50#ifndef LIBTRACE_INT_H
51#define LIBTRACE_INT_H
52
53#ifdef __cplusplus
54extern "C" {
55#endif
56
57#include "config.h"
58#include "common.h"
59#include "libtrace.h"
60#include "wandio.h"
61#include "lt_bswap.h"
62
63#ifdef _MSC_VER
64// warning: deprecated function
65#pragma warning(disable:4996)
66// warning: benign redefinitions of types
67#pragma warning(disable:4142)
68#endif
69
70#ifdef HAVE_INTTYPES_H
71# include <inttypes.h>
72#else
73# include "lt_inttypes.h"
74#endif
75
76#ifdef HAVE_STDDEF_H
77# include <stddef.h>
78#else
79#ifndef WIN32
80# error "Can't find stddev.h -- do you define ptrdiff_t elsewhere?"
81#endif
82#endif
83
84
85#include "rt_protocol.h"
86       
87/* Prefer net/bpf.h over pcap-bpf.h for format_bpf.c on MacOS */
88#ifdef HAVE_NET_BPF_H
89#    include <net/bpf.h>
90#    define HAVE_BPF 1
91#else
92#ifdef HAVE_PCAP_BPF_H
93#  include <pcap-bpf.h>
94#  define HAVE_BPF 1
95#endif
96#endif
97
98#ifdef HAVE_PCAP_H
99#  include <pcap.h>
100#  ifdef HAVE_PCAP_INT_H
101#    include <pcap-int.h>
102#  endif
103#endif
104
105#ifdef HAVE_ZLIB_H
106#  include <zlib.h>
107#endif
108
109#ifndef HAVE_STRNDUP
110char *strndup(const char *s, size_t size);
111#endif
112
113#ifndef HAVE_STRNCASECMP
114# ifndef HAVE__STRNICMP
115/** A local implementation of strncasecmp (as some systems do not have it) */
116int strncasecmp(const char *str1, const char *str2, size_t n);
117# else
118# define strncasecmp _strnicmp
119# endif
120#endif
121
122#ifndef HAVE_SNPRINTF
123# ifndef HAVE_SPRINTF_S
124/** A local implementation of snprintf (as some systems do not have it) */
125int snprintf(char *str, size_t size, const char *format, ...);
126# else
127# define snprintf sprintf_s
128# endif
129#endif
130
131#include "daglegacy.h"
132       
133#ifdef HAVE_DAG_API
134#  include "dagnew.h"
135#  include "dagapi.h"
136#       if DAG_VERSION == 24
137#               include <erftypes.h>
138#       else
139#               include <daginf.h>
140#       endif
141#  include "erftypes.h"
142#else
143#  include "dagformat.h"
144#endif
145
146#ifdef HAVE_LLVM
147#include "bpf-jit/bpf-jit.h"
148#endif
149
150//#define RP_BUFSIZE 65536U
151
152/** Data about the most recent event from a trace file */
153struct libtrace_event_status_t {
154        /** A libtrace packet to store the packet when a PACKET event occurs */
155        libtrace_packet_t *packet;
156        /** Time between the timestamp for the current packet and the current
157         * walltime */
158        double tdelta;
159        /** The timestamp of the previous PACKET event */
160        double trace_last_ts;
161        /** The size of the current PACKET event */
162        int psize;
163        /** Whether there is a packet stored in *packet above waiting for an
164         * event to occur */
165        bool waiting;
166};
167
168enum thread_types {
169        THREAD_EMPTY,
170        THREAD_HASHER,
171        THREAD_MAPPER,
172        THREAD_REDUCER
173};
174
175enum thread_states {
176        THREAD_RUNNING,
177        THREAD_FINISHING,
178        THREAD_FINISHED,
179        THREAD_PAUSED
180};
181
182enum hasher_types {
183        HASHER_BALANCE, /** Balance load across CPUs best as possible this is basically to say don't care about hash, but this might still might be implemented using a hash or round robin etc.. */
184        HASHER_BIDIRECTIONAL, /** Use a hash which is uni-directional for TCP flows (IP src dest,TCP port src dest), non TCP
185                                                        Will be sent to the same place, with the exception of UDP which may or may not be sent to separate cores */
186        HASHER_UNIDIRECTIONAL, /** Use a hash which is uni-directional across TCP flow */
187        HASHER_CUSTOM, /** Always use the user supplied hasher */
188        HASHER_HARDWARE, /** Set by the format if the hashing is going to be done in hardware */
189};
190
191// Reduce expects sequential data
192#define REDUCE_SEQUENTIAL 0x1
193// Reduce is working on ordered data
194#define REDUCE_ORDERED 0x2
195// Reduce should sort the data
196#define REDUCE_SORT 0x4
197// Drop out of order valid with
198#define REDUCE_DROP_OOO 0x8
199// Reduce reads all queues with same key
200#define REDUCE_STEPPING 0x10
201
202#define MAPPER_USE_SLIDING_WINDOW 0x20
203
204
205#include "trace_ringbuffer.h"
206#include "trace_vector.h"
207#include "libtrace_message_queue.h"
208#include "deque.h"
209#include "trace_sliding_window.h"
210
211/**
212 * Information of this thread
213 */
214typedef struct libtrace_thread_t {
215        libtrace_t * trace;
216        void* ret;
217        enum thread_types type;
218        enum thread_states state;
219        void* user_data; // TLS for the user to use
220        pthread_t tid;
221        int map_num; // A number from 0-X that represents this mapper number
222                                // in the table, intended to quickly identify this thread
223                                // -1 represents NA (such as in the case this is not a mapper thread)
224        libtrace_ringbuffer_t rbuffer; // Input
225        libtrace_vector_t vector; // Output
226        libtrace_queue_t deque; // Real Output type makes more sense
227        libtrace_message_queue_t messages; // Message handling
228        // Temp storage for time sensitive results
229        uint64_t tmp_key;
230        void *tmp_data;
231        pthread_spinlock_t tmp_spinlock;
232        // Set to true once the first packet has been stored
233        bool recorded_first;
234        // For thread safety reason we actually must store this here
235        int64_t tracetime_offset_usec;
236} libtrace_thread_t;
237
238/**
239 * Storage to note time value against each.
240 * Used both internally to do trace time playback
241 * and can be used externally to assist applications which need
242 * a trace starting time such as tracertstats.
243 */
244struct first_packets {
245        pthread_spinlock_t lock;
246        size_t count; // If == mappers we have all
247        size_t first; // Valid if count != 0
248        struct __packet_storage_magic_type {
249                libtrace_packet_t * packet;
250                struct timeval tv;
251        } * packets;
252};
253
254
255/** A libtrace input trace
256 * @internal
257 */
258struct libtrace_t {
259        /** The capture format for the input trace */
260        struct libtrace_format_t *format; 
261        /** Details of the most recent PACKET event reported by the trace */
262        struct libtrace_event_status_t event;
263        /** Pointer to the "global" data for the capture format module */       
264        void *format_data;             
265        /** A BPF filter to be applied to all packets read by the trace -
266         * used only if the capture format does not support filters natively */
267        struct libtrace_filter_t *filter; 
268        /** The snap length to be applied to all packets read by the trace -
269         * used only if the capture format does not support snapping natively */
270        size_t snaplen;                 
271        /** Count of the number of packets returned to the libtrace user */
272        uint64_t accepted_packets;     
273        /** Count of the number of packets filtered by libtrace */
274        uint64_t filtered_packets;     
275        /** The filename from the uri for the trace */
276        char *uridata;
277        /** The libtrace IO reader for this trace (if applicable) */
278        io_t *io;
279        /** Error information for the trace */
280        libtrace_err_t err;
281        /** Boolean flag indicating whether the trace has been started */
282        bool started;
283        /** Synchronise writes/reads across this format object and attached threads etc */
284        pthread_mutex_t libtrace_lock;
285       
286        /** Use to control pausing threads and finishing threads etc always used with libtrace_lock */
287        pthread_cond_t perpkt_cond;
288        /** Set to the number of mapper threads that are finishing (or have finished), or to -1 once all have been joined, 0 implies all are running */
289        int mappers_finishing;
290        /** A count of mappers that are pausing */
291        int perpkt_pausing;
292       
293        /** For the sliding window hasher implementation */
294        pthread_rwlock_t window_lock;
295        /** Set once trace_join has been called */
296        bool joined;
297        /** Set to indicate a mappers queue is full and such the writing mapper cannot proceed */
298        bool mapper_queue_full;
299        /** Global storage for this trace, shared among all the threads  */
300        void* global_blob;
301        /** Requested size of the pkt buffer (currently only used if using dedicated hasher thread) */
302        int packet_freelist_size;
303        /** The actual freelist */
304        libtrace_ringbuffer_t packet_freelist;
305        /** The number of packets that can queue per mapper thread - XXX consider deadlocks with non malloc()'d packets that need to be released */
306        int mapper_buffer_size;
307        /** The reducer flags */
308        int reducer_flags;
309        /** Used to track the next expected key */
310        uint64_t expected_key;
311        /** User defined per_pkt function called when a pkt is ready */
312        fn_per_pkt per_pkt;
313        /** User defined reducer function entry point XXX not hooked up */
314        fn_reducer reducer;
315        /** The hasher function */
316        enum hasher_types hasher_type;
317        /** The hasher function - NULL implies they don't care or balance */
318        fn_hasher hasher; // If valid using a separate thread
319        void *hasher_data;
320       
321        libtrace_thread_t hasher_thread;
322        libtrace_thread_t reducer_thread;
323        int mapper_thread_count;
324        libtrace_thread_t * mapper_threads; // All our mapper threads
325        libtrace_slidingwindow_t sliding_window;
326        sem_t sem;
327        // Used to keep track of the first packet seen on each thread
328        struct first_packets first_packets;
329        int tracetime;
330};
331
332inline void libtrace_zero_thread(libtrace_thread_t * t);
333inline void store_first_packet(libtrace_t *libtrace, libtrace_packet_t *packet, libtrace_thread_t *t);
334
335/** A libtrace output trace
336 * @internal
337 */
338struct libtrace_out_t {
339        /** The capture format for the output trace */
340        struct libtrace_format_t *format;
341        /** Pointer to the "global" data for the capture format module */
342        void *format_data;             
343        /** The filename for the uri for the output trace */
344        char *uridata;                 
345        /** Error information for the output trace */
346        libtrace_err_t err;
347        /** Boolean flag indicating whether the trace has been started */
348        bool started;
349};
350
351/** Sets the error status on an input trace
352 *
353 * @param trace         The input trace to set the error status for
354 * @param errcode       The code for the error - can be a libtrace error code or a regular errno value
355 * @param msg           A message to print when reporting the error
356 */
357void trace_set_err(libtrace_t *trace, int errcode,const char *msg,...) 
358
359                                                                PRINTF(3,4);
360/** Sets the error status on an output trace
361 *
362 * @param trace         The output trace to set the error status for
363 * @param errcode       The code for the error - can be a libtrace error code or a regular errno value
364 * @param msg           A message to print when reporting the error
365 */
366void trace_set_err_out(libtrace_out_t *trace, int errcode, const char *msg,...)
367                                                                PRINTF(3,4);
368
369/** Clears the cached values for a libtrace packet
370 *
371 * @param packet        The libtrace packet that requires a cache reset
372 */
373void trace_clear_cache(libtrace_packet_t *packet);
374
375/** Converts the data provided in buffer into a valid libtrace packet
376 *
377 * @param trace         An input trace of the same format as the "packet"
378 *                      contained in the buffer
379 * @param packet        The libtrace packet to prepare
380 * @param buffer        A buffer containing the packet data, including the
381 *                      capture format header
382 * @param rt_type       The RT type for the packet that is being prepared
383 * @param flags         Used to specify options for the preparation function,
384 *                      e.g. who owns the packet buffer
385 *
386 * @return -1 if an error occurs, 0 otherwise
387 *
388 * Packet preparation is a tricky concept - the idea is to take the data
389 * pointed to by 'buffer' and treat it as a packet record of the same capture
390 * format as that used by the input trace. The provided libtrace packet then
391 * has its internal pointers and values set to describe the packet record in
392 * the buffer.
393 *
394 * The primary use of this function is to allow the RT packet reader to
395 * easily and safely convert packets from the RT format back into the format
396 * that they were originally captured with., essentially removing the RT
397 * encapsulation.
398 *
399 * We've decided not to make this function available via the exported API
400 * because there are several issues that can arise if it is not used very
401 * carefully and it is not very useful outside of internal contexts anyway.
402 */
403int trace_prepare_packet(libtrace_t *trace, libtrace_packet_t *packet,
404                void *buffer, libtrace_rt_types_t rt_type, uint32_t flags);
405
406/** Flags for prepare_packet functions */
407enum {
408        /** The buffer memory has been allocated by libtrace and should be
409         * freed when the packet is destroyed. */
410        TRACE_PREP_OWN_BUFFER           =1,
411       
412        /** The buffer memory is externally-owned and must not be freed by
413         * libtrace when the packet is destroyed. */
414        TRACE_PREP_DO_NOT_OWN_BUFFER    =0
415};
416
417
418#ifndef PF_RULESET_NAME_SIZE
419#define PF_RULESET_NAME_SIZE 16
420#endif
421
422#ifndef IFNAMSIZ
423#define IFNAMSIZ 16
424#endif
425
426
427/** A local definition of a PFLOG header */
428typedef struct libtrace_pflog_header_t {
429        uint8_t    length;     
430        sa_family_t   af;
431        uint8_t    action;
432        uint8_t    reason;
433        char       ifname[IFNAMSIZ];
434        char       ruleset[PF_RULESET_NAME_SIZE];
435        uint32_t   rulenr;
436        uint32_t   subrulenr;
437        uint8_t    dir;
438        uint8_t    pad[3];
439} PACKED libtrace_pflog_header_t;
440
441
442
443/** A libtrace capture format module */
444/* All functions should return -1, or NULL on failure */
445struct libtrace_format_t {
446        /** The name of this module, used in the libtrace URI to identify the
447         * capture format */
448        const char *name;
449        /** The version of this module */
450        const char *version;
451        /** The RT protocol type of this module */
452        enum base_format_t type;
453
454
455        /** Given a filename, return if this is the most likely capture format
456         * (used for devices). Used to "guess" the capture format when the
457         * URI is not fully specified.
458         *
459         * @param fname         The name of the device or file to examine
460         * @return 1 if the name matches the capture format, 0 otherwise
461         */
462        int (*probe_filename)(const char *fname);
463       
464        /** Given a file, looks at the start of the file to determine if this
465         * is the capture format. Used to "guess" the capture format when the
466         * URI is not fully specified.
467         *
468         * @param io            An open libtrace IO reader for the file to check
469         * @return 1 if the file matches the capture format, 0 otherwise
470         */
471        int (*probe_magic)(io_t *io);
472
473        /** Initialises an input trace using the capture format.
474         *
475         * @param libtrace      The input trace to be initialised
476         * @return 0 if successful, -1 in the event of error
477         */
478        int (*init_input)(libtrace_t *libtrace);
479       
480        /** Applies a configuration option to an input trace.
481         *
482         * @param libtrace      The input trace to apply the option to
483         * @param option        The option that is being configured
484         * @param value         A pointer to the value that the option is to be
485         *                      set to
486         * @return 0 if successful, -1 if the option is unsupported or an error
487         * occurs
488         */
489        int (*config_input)(libtrace_t *libtrace,trace_option_t option,void *value);
490        /** Starts or unpauses an input trace - note that this function is
491         * often the one that opens the file or device for reading.
492         *
493         * @param libtrace      The input trace to be started or unpaused
494         * @return 0 if successful, -1 in the event of error */
495        int (*start_input)(libtrace_t *libtrace);
496
497        /** Pauses an input trace - this function should close or detach the
498         * file or device that is being read from.
499         *
500         * @param libtrace      The input trace to be paused
501         * @return 0 if successful, -1 in the event of error
502         */
503        int (*pause_input)(libtrace_t *libtrace);
504
505        /** Initialises an output trace using the capture format.
506         *
507         * @param libtrace      The output trace to be initialised
508         * @return 0 if successful, -1 in the event of error
509         */
510        int (*init_output)(libtrace_out_t *libtrace);
511       
512        /** Applies a configuration option to an output trace.
513         *
514         * @param libtrace      The output trace to apply the option to
515         * @param option        The option that is being configured
516         * @param value         A pointer to the value that the option is to be
517         *                      set to
518         * @return 0 if successful, -1 if the option is unsupported or an error
519         * occurs
520         * */
521        int (*config_output)(libtrace_out_t *libtrace, trace_option_output_t option, void *value);
522
523        /** Starts an output trace - note that this function is often the one
524         * that opens the file or device for writing.
525         *
526         * @param libtrace      The output trace to be started
527         * @return 0 if successful, -1 if an error occurs
528         *
529         * There is no pause for output traces, as writing is not performed
530         * asynchronously.
531         */
532        int (*start_output)(libtrace_out_t *libtrace);
533
534        /** Concludes an input trace and cleans up the capture format data.
535         *
536         * @param libtrace      The input trace to be concluded
537         * @return 0 if successful, -1 if an error occurs
538         *
539         * Libtrace will call the pause_input function if the input trace is
540         * currently active prior to calling this function.
541         */
542        int (*fin_input)(libtrace_t *libtrace);
543
544        /** Concludes an output trace and cleans up the capture format data.
545         *
546         * @param libtrace      The output trace to be concluded
547         * @return 0 if successful, -1 if an error occurs
548         */
549        int (*fin_output)(libtrace_out_t *libtrace);
550
551        /** Reads the next packet from an input trace into the provided packet
552         * structure.
553         *
554         * @param libtrace      The input trace to read from
555         * @param packet        The libtrace packet to read into
556         * @return The size of the packet read (in bytes) including the capture
557         * framing header, or -1 if an error occurs. 0 is returned in the
558         * event of an EOF.
559         *
560         * If no packets are available for reading, this function should block
561         * until one appears or return 0 if the end of a trace file has been
562         * reached.
563         */
564        int (*read_packet)(libtrace_t *libtrace, libtrace_packet_t *packet);
565       
566        /** Converts a buffer containing a packet record into a libtrace packet
567         *
568         * @param libtrace      An input trace in the capture format for the
569         *                      packet
570         * @param packet        A libtrace packet to put the prepared packet
571         *                      into
572         * @param buffer        The buffer containing the packet record
573         *                      (including the capture format header)
574         * @param rt_type       The RT type for the packet
575         * @param flags         Flags describing properties that should be
576         *                      applied to the new packet
577         * @return 0 if successful, -1 if an error occurs.
578         *
579         * Updates internal trace and packet details, such as payload pointers,
580         * loss counters and packet types to match the packet record provided
581         * in the buffer. This is a zero-copy function.
582         *
583         * Intended (at this stage) only for internal use, particularly by
584         * RT which needs to decapsulate RT packets */
585        int (*prepare_packet)(libtrace_t *libtrace, libtrace_packet_t *packet,
586                        void *buffer, libtrace_rt_types_t rt_type, 
587                        uint32_t flags);
588       
589        /** Frees any resources allocated by the capture format module for a
590         * libtrace packet.
591         *
592         * @param The packet to be finalised
593         *       */
594        void (*fin_packet)(libtrace_packet_t *packet);
595
596        /** Write a libtrace packet to an output trace.
597         *
598         * @param libtrace      The output trace to write the packet to
599         * @param packet        The packet to be written out
600         * @return The number of bytes written, or -1 if an error occurs
601         */
602        int (*write_packet)(libtrace_out_t *libtrace, libtrace_packet_t *packet);
603        /** Returns the libtrace link type for a packet.
604         *
605         * @param packet        The packet to get the link type for
606         * @return The libtrace link type, or -1 if this link type is unknown
607         */ 
608        libtrace_linktype_t (*get_link_type)(const libtrace_packet_t *packet);
609
610        /** Returns the direction of a packet.
611         *
612         * @param packet        The packet to get the direction for
613         * @return The direction of the packet, or -1 if no direction tag is
614         * present or an error occurs
615         */ 
616        libtrace_direction_t (*get_direction)(const libtrace_packet_t *packet);
617       
618        /** Sets the direction of a packet.
619         *
620         * @param packet        The packet to set the direction for
621         * @param direction     The direction to assign to the packet
622         * @return The updated direction for the packet, or -1 if an error
623         * occurs
624         *
625         * @note Some capture formats do not feature direction tagging, so it
626         * will not make sense to implement a set_direction function for them.
627         */ 
628        libtrace_direction_t (*set_direction)(libtrace_packet_t *packet, libtrace_direction_t direction);
629       
630        /** Returns the timestamp for a packet in the ERF timestamp format.
631         *
632         * @param packet        The packet to get the timestamp from
633         * @return The 64-bit ERF timestamp
634         *
635         * @note Each format must implement at least one of the four "get
636         * timestamp" functions.
637         *
638         * If not implemented, libtrace will convert the result of one of the
639         * other timestamp functions into the appropriate format instead.
640         * This means each capture format only needs to implement the most
641         * sensible of the four and let libtrace handle any conversions.
642         *
643         */
644        uint64_t (*get_erf_timestamp)(const libtrace_packet_t *packet);
645
646        /** Returns the timestamp for a packet in the timeval format
647         *
648         * @param packet        The packet to get the timestamp from
649         * @return The timestamp from the packet as a timeval
650         *
651         * @note Each format must implement at least one of the four "get
652         * timestamp" functions.
653         *
654         * If not implemented, libtrace will convert the result of one of the
655         * other timestamp functions into the appropriate format instead.
656         * This means each capture format only needs to implement the most
657         * sensible of the four and let libtrace handle any conversions.
658         */
659        struct timeval (*get_timeval)(const libtrace_packet_t *packet);
660       
661        /** Returns the timestamp for a packet in the timespec format.
662         *
663         * @param packet        The packet to get the timestamp from
664         * @return The timestamp from the packet as a timespec
665         *
666         * @note Each format must implement at least one of the four "get
667         * timestamp" functions.
668         *
669         * If not implemented, libtrace will convert the result of one of the
670         * other timestamp functions into the appropriate format instead.
671         * This means each capture format only needs to implement the most
672         * sensible of the four and let libtrace handle any conversions.
673         */
674        struct timespec (*get_timespec)(const libtrace_packet_t *packet);
675       
676        /** Returns the timestamp for a packet in floating point seconds.
677         *
678         * @param packet        The packet to get the timestamp from
679         * @return The timestamp from the packet as a floating point number of
680         * seconds since 1970-01-01 00:00:00 UTC
681         *
682         * @note Each format must implement at least one of the four "get
683         * timestamp" functions.
684         *
685         * If not implemented, libtrace will convert the result of one of the
686         * other timestamp functions into the appropriate format instead.
687         * This means each capture format only needs to implement the most
688         * sensible of the four and let libtrace handle any conversions.
689         */
690        double (*get_seconds)(const libtrace_packet_t *packet);
691       
692        /** Moves the read pointer to a certain ERF timestamp within an input
693         * trace file.
694         *
695         * @param trace         The input trace to seek within
696         * @param timestamp     The timestamp to seek to, as an ERF timestamp
697         *
698         * @return 0 on success, -1 on failure.
699         *
700         * The next packet read from this trace will now be the first packet
701         * to have a timestamp equal to or greater than the provided timestamp.
702         *
703         * @note Each format that supports seeking must implement at least one
704         * of the seek functions.
705         *
706         * If not implemented, libtrace will convert the timestamp into the
707         * appropriate format to use a seek function that has been implemented.
708         * This means each capture format only needs to implement the seek
709         * function that matches the native timestamp format for that capture.
710         *
711         */
712        int (*seek_erf)(libtrace_t *trace, uint64_t timestamp);
713        /** Moves the read pointer to a certain timestamp represented using a
714         * timeval within an input trace file.
715         *
716         * @param trace         The input trace to seek within
717         * @param timestamp     The timestamp to seek to, as a timeval
718         *
719         * @return 0 on success, -1 on failure.
720         *
721         * The next packet read from this trace will now be the first packet
722         * to have a timestamp equal to or greater than the provided timestamp.
723         *
724         * @note Each format that supports seeking must implement at least one
725         * of the seek functions.
726         *
727         * If not implemented, libtrace will convert the timestamp into the
728         * appropriate format to use a seek function that has been implemented.
729         * This means each capture format only needs to implement the seek
730         * function that matches the native timestamp format for that capture.
731         *
732         */
733        int (*seek_timeval)(libtrace_t *trace, struct timeval tv);
734       
735        /** Moves the read pointer to a certain timestamp represented using
736         * floating point seconds within an input trace file.
737         *
738         * @param trace         The input trace to seek within
739         * @param timestamp     The timestamp to seek to, as floating point
740         *                      seconds since 1970-01-01 00:00:00 UTC
741         *
742         * @return 0 on success, -1 on failure.
743         *
744         * The next packet read from this trace will now be the first packet
745         * to have a timestamp equal to or greater than the provided timestamp.
746         *
747         * @note Each format that supports seeking must implement at least one
748         * of the seek functions.
749         *
750         * If not implemented, libtrace will convert the timestamp into the
751         * appropriate format to use a seek function that has been implemented.
752         * This means each capture format only needs to implement the seek
753         * function that matches the native timestamp format for that capture.
754         *
755         */
756        int (*seek_seconds)(libtrace_t *trace, double seconds);
757       
758        /** Returns the payload length of the captured packet record.
759         *
760         * @param packet        The packet to get the capture length from
761         * @return The capture length for the packet, or -1 if an error occurs
762         *
763         * Capture length is the current size of the packet record itself,
764         * following any truncation that may have occurred during the capture
765         * process. This length does not include the capture format framing
766         * header.
767         */
768        int (*get_capture_length)(const libtrace_packet_t *packet);
769
770        /** Returns the original length of the packet as it was on the wire.
771         *
772         * @param packet        The packet to get the wire length from
773         * @return The length of the packet on the wire at the time of capture,
774         * or -1 if an error occurs
775         *
776         * Wire length is the original size of the packet prior to any
777         * truncation that may have occurred as part of the capture process.
778         * This length does not include the capture format framing header.
779         */
780        int (*get_wire_length)(const libtrace_packet_t *packet);
781       
782        /** Returns the length of the capture format framing header
783         *
784         * @param packet        The packet to get the framing length from
785         * @return The length of the framing header, or -1 if an error occurs
786         *
787         * The framing header is the extra metadata that the capture process
788         * records about a packet.  The framing length does not include any
789         * of the packet payload itself. The total size of the packet record
790         * can be calculated be adding this value with the capture length.
791         */
792        int (*get_framing_length)(const libtrace_packet_t *packet);
793
794        /** Sets the capture length for a packet.
795         *
796         * @param packet        The packet to adjust the capture length for.
797         * @param size          The new capture length
798         * @return The new capture length of the packet, or -1 if an error
799         * occurs
800         *
801         * @note This function should only reduce the capture length. If the
802         * provided length is larger than the current capture length, -1 should
803         * be returned.
804         */
805        size_t (*set_capture_length)(struct libtrace_packet_t *packet,size_t size);
806        /** Returns the number of packets observed by an input trace.
807         *
808         * @param trace         The input trace to get the packet count for
809         * @return The number of packets observed by an input trace, or
810         * UINT64_MAX if the number is unknown
811         *
812         * This count includes packets that have been filtered and dropped.
813         */
814        uint64_t (*get_received_packets)(libtrace_t *trace);
815
816        /** Returns the number of packets filtered by an input trace.
817         *
818         * @param trace         The input trace to get the filtered count for
819         * @return The number of packets filtered by the input trace, or
820         * UINT64_MAX if the number is unknown
821         *
822         */
823        uint64_t (*get_filtered_packets)(libtrace_t *trace);
824       
825        /** Returns the number of packets dropped by an input trace.
826         *
827         * @param trace         The input trace to get the dropped count for
828         * @return The number of packets dropped by the input trace, or
829         * UINT64_MAX if the number is unknown
830         *
831         */
832        uint64_t (*get_dropped_packets)(libtrace_t *trace);
833       
834        /** Returns the number of packets captured and returned by an input
835         * trace.
836         *
837         * @param trace         The input trace to get the capture count for
838         * @return The number of packets returned to the libtrace user, or
839         * UINT64_MAX if the number is unknown
840         *
841         * This is the number of packets that have been successfully returned
842         * to the libtrace user via the read_packet() function.
843         *
844         */
845        uint64_t (*get_captured_packets)(libtrace_t *trace);
846       
847        /** Returns the file descriptor used by the input trace.
848         *
849         * @param trace         The input trace to get the file descriptor for
850         * @return The file descriptor used by the input trace to read packets
851         *
852         */
853        int (*get_fd)(const libtrace_t *trace);
854       
855        /** Returns the next libtrace event for the input trace.
856         *
857         * @param trace         The input trace to get the next event from
858         * @param packet        A libtrace packet to read a packet into
859         * @return A libtrace event describing the event that occured
860         *
861         * The event API allows for non-blocking reading of packets from an
862         * input trace. If a packet is available and ready to be read, a packet
863         * event should be returned. Otherwise a sleep or fd event should be
864         * returned to indicate that the caller needs to wait. If the input
865         * trace has an error or reaches EOF, a terminate event should be
866         * returned.
867         */
868        struct libtrace_eventobj_t (*trace_event)(libtrace_t *trace, libtrace_packet_t *packet);       
869
870        /** Prints some useful help information to standard output. */
871        void (*help)(void);
872       
873        /** Starts or unpauses an input trace in parallel mode - note that
874         * this function is often the one that opens the file or device for
875         * reading.
876         *
877         * @param libtrace      The input trace to be started or unpaused
878         * @return If successful the number of threads started, 0 indicates
879         *                 no threads started and this should be done automatically.
880         *                 Otherwise in event of an error -1 is returned.
881         *
882         */
883        int (*pstart_input)(libtrace_t *trace);
884       
885        /** Read a packet in the new parallel mode
886         * @return same as read_packet, with the addition of return -2 to represent
887         * interrupted due to message waiting. */
888        int (*pread_packet)(libtrace_t *trace, libtrace_packet_t *packet);
889       
890        /** Pause a parallel trace */
891        int (*ppause_input)(libtrace_t *trace);
892       
893        /** Called after all threads have been paused, Finish (close) a parallel trace */
894        int (*pfin_input)(libtrace_t *trace);
895       
896        /** Applies a configuration option to an input trace.
897         *
898         * @param libtrace      The input trace to apply the option to
899         * @param option        The option that is being configured
900         * @param value         A pointer to the value that the option is to be
901         *                      set to
902         * @return 0 if successful, -1 if the option is unsupported or an error
903         * occurs
904         */
905        int (*pconfig_input)(libtrace_t *libtrace,trace_parallel_option_t option,void *value);
906       
907        /** Next pointer, should always be NULL - used by the format module
908         * manager. */
909        struct libtrace_format_t *next;
910};
911
912/** The list of registered capture formats */
913//extern struct libtrace_format_t *form;
914
915/** Specifies whether any blocking packet readers should cease reading
916 * immediately
917 */
918extern int libtrace_halt;
919
920/** Registers a new capture format module.
921 *
922 * @param format        The format module to be registered
923 */
924void register_format(struct libtrace_format_t *format);
925
926/** Converts a PCAP DLT into a libtrace link type.
927 *
928 * @param linktype      The PCAP DLT to be converted
929 * @return The libtrace link type that is equivalent to the provided DLT, or
930 * -1 if the DLT is unknown
931 */
932libtrace_linktype_t pcap_linktype_to_libtrace(libtrace_dlt_t linktype);
933
934/** Converts a PCAP DLT into an RT protocol type.
935 *
936 * @param linktype      The PCAP DLT to be converted
937 * @return The RT type that is equivalent to the provided DLT
938 */
939libtrace_rt_types_t pcap_linktype_to_rt(libtrace_dlt_t linktype);
940
941/** Converts a libtrace link type into a PCAP linktype.
942 *
943 * @param type          The libtrace link type to be converted
944 * @return The PCAP linktype that is equivalent to the provided libtrace link
945 * type, or -1 if the link type is unknown
946 */
947libtrace_dlt_t libtrace_to_pcap_linktype(libtrace_linktype_t type);
948
949/** Converts a libtrace link type into a PCAP DLT.
950 *
951 * @param type          The libtrace link type to be converted
952 * @return The PCAP DLT that is equivalent to the provided libtrace link
953 * type, or -1 if the link type is unknown
954 */
955libtrace_dlt_t libtrace_to_pcap_dlt(libtrace_linktype_t type);
956
957/** Converts an RT protocol type into a PCAP DLT.
958 *
959 * @param rt_type       The RT type to be converted
960 * @return The PCAP DLT that is equivalent to the provided RT protocol
961 */
962libtrace_dlt_t rt_to_pcap_linktype(libtrace_rt_types_t rt_type);
963
964/** Converts a PCAP DLT into an RT protocol type for the BPF format.
965 *
966 * @param linktype      The PCAP DLT to be converted
967 * @return The RT type that is equivalent to the provided DLT for BPF
968 */
969libtrace_rt_types_t bpf_linktype_to_rt(libtrace_dlt_t linktype);
970
971/** Converts an ERF type into a libtrace link type.
972 *
973 * @param erf           The ERF type to be converted
974 * @return The libtrace link type that is equivalent to the provided ERF type,
975 * or -1 if the ERF type is unknown
976 */
977libtrace_linktype_t erf_type_to_libtrace(uint8_t erf);
978
979/** Converts a libtrace link type into an ERF type.
980 *
981 * @param linktype      The libtrace link type to be converted
982 * @return The ERF type that is equivalent to the provided libtrace link type,
983 * or -1 if the link type cannot be matched to an ERF type.
984 */
985uint8_t libtrace_to_erf_type(libtrace_linktype_t linktype);
986
987/** Converts an ARPHRD type into a libtrace link type.
988 *
989 * @param arphrd        The ARPHRD type to be converted
990 * @return The libtrace link type that is equivalent to the provided ARPHRD
991 * type, or -1 if the ARPHRD type is unknown
992 */
993libtrace_linktype_t arphrd_type_to_libtrace(unsigned int arphrd);
994
995/** Converts a libtrace link type into an ARPHRD type.
996 *
997 * @param type          The libtrace link type to be converted
998 * @return The ARPHRD type that is equivalent to the provided libtrace link
999 * type, or -1 if the link type cannot be matched to an ARPHRD type
1000 */
1001unsigned int libtrace_to_arphrd_type(libtrace_linktype_t type);
1002
1003/** Converts a libtrace packet to the Linux SLL type.
1004 *
1005 * @param packet        The packet to be promoted
1006 *
1007 * @note This will involve memcpy() so use sparingly.
1008 *
1009 * This function prepends a Linux SLL header to a packet so that we can store
1010 * direction tagging information.
1011 */
1012void promote_packet(libtrace_packet_t *packet);
1013
1014/** Attempts to demote a packet by removing the first header.
1015 *
1016 * @param packet        The packet to be demoted
1017 * @return True if the packet was demoted, false otherwise.
1018 *
1019 * Essentially the opposite of promote_packet, except that it will also remove
1020 * an ATM header as well as Linux SLL.
1021 *
1022 */
1023bool demote_packet(libtrace_packet_t *packet);
1024
1025/** Returns a pointer to the header following a Linux SLL header.
1026 *
1027 * @param link          A pointer to the Linux SLL header to be skipped
1028 * @param[out] arphrd_type      The arp hardware type of the packet
1029 * @param[out] next_header      The ethertype of the next header
1030 * @param[in,out] remaining     Updated with the number of captured bytes
1031 *                              remaining
1032 * @return A pointer to the header following the Linux SLL header, or NULL if
1033 * no subsequent header is present.
1034 *
1035 * Remaining must point to the number of bytes captured from the Linux SLL
1036 * header and beyond.  It will be decremented by the number of bytes skipped
1037 * to find the payload.
1038 *
1039 * If the Linux SLL header is complete but there are zero bytes of payload
1040 * after the end of the header, a pointer to where the payload would be is
1041 * returned and remaining will be set to zero. If the Linux SLL header is
1042 * incomplete (truncated), then NULL is returned and remaining will be set to
1043 * 0. Therefore, it is very important to check the value of remaining after
1044 * calling this function.
1045 */     
1046void *trace_get_payload_from_linux_sll(const void *link,
1047                uint16_t *arphrd_type, 
1048                uint16_t *next_header, 
1049                uint32_t *remaining);
1050
1051/** Returns a pointer to the header following an ATM header.
1052 *
1053 * @param link          A pointer to the ATM header to be skipped
1054 * @param[out] type     The ethertype of the next header
1055 * @param[in,out] remaining     Updated with the number of captured bytes
1056 *                              remaining
1057 * @return A pointer to the header following the ATM header, or NULL if
1058 * no subsequent header is present.
1059 *
1060 * Remaining must point to the number of bytes captured from the ATM header
1061 * and beyond.  It will be decremented by the number of bytes skipped to find
1062 * the payload.
1063 *
1064 * If the ATM header is complete but there are zero bytes of payload
1065 * after the end of the header, a pointer to where the payload would be is
1066 * returned and remaining will be set to zero. If the ATM header is
1067 * incomplete (truncated), then NULL is returned and remaining will be set to
1068 * 0. Therefore, it is very important to check the value of remaining after
1069 * calling this function.
1070 */     
1071DLLEXPORT void *trace_get_payload_from_atm(void *link, uint8_t *type, 
1072                uint32_t *remaining);
1073
1074
1075#ifdef HAVE_BPF
1076/* A type encapsulating a bpf filter
1077 * This type covers the compiled bpf filter, as well as the original filter
1078 * string
1079 *
1080 */
1081
1082/** Internal representation of a BPF filter */
1083struct libtrace_filter_t {
1084        struct bpf_program filter;      /**< The BPF program itself */
1085        char * filterstring;            /**< The filter string */
1086        int flag;                       /**< Indicates if the filter is valid */
1087        struct bpf_jit_t *jitfilter;
1088};
1089#else
1090/** BPF not supported by this system, but we still need to define a structure
1091 * for the filter */
1092struct libtrace_filter_t {};
1093#endif
1094
1095/** Local definition of a PCAP header */
1096typedef struct libtrace_pcapfile_pkt_hdr_t {
1097        uint32_t ts_sec;        /* Seconds portion of the timestamp */
1098        uint32_t ts_usec;       /* Microseconds portion of the timestamp */
1099        uint32_t caplen;        /* Capture length of the packet */
1100        uint32_t wirelen;       /* The wire length of the packet */
1101} libtrace_pcapfile_pkt_hdr_t;
1102
1103#ifdef HAVE_DAG
1104/** Constructor for the DAG format module */
1105void dag_constructor(void);
1106#endif
1107/** Constructor for the ERF format module */
1108void erf_constructor(void);
1109/** Constructor for the TSH format module */
1110void tsh_constructor(void);
1111/** Constructor for the Legacy DAG format module */
1112void legacy_constructor(void);
1113/** Constructor for the Linux Native format module */
1114void linuxnative_constructor(void);
1115/** Constructor for the PCAP format module */
1116void pcap_constructor(void);
1117/** Constructor for the PCAP File format module */
1118void pcapfile_constructor(void);
1119/** Constructor for the RT format module */
1120void rt_constructor(void);
1121/** Constructor for the DUCK format module */
1122void duck_constructor(void);
1123/** Constructor for the ATM Header format module */
1124void atmhdr_constructor(void);
1125#ifdef HAVE_BPF
1126/** Constructor for the BPF format module */
1127void bpf_constructor(void);
1128#endif
1129#if HAVE_DPDK
1130/** Constructor for Intels DPDK format module */
1131void dpdk_constructor(void);
1132#endif
1133
1134/** Extracts the RadioTap flags from a wireless link header
1135 *
1136 * @param link          A pointer to the wireless link header
1137 * @param linktype      The link type of the wireless header
1138 * @param[out] flags    Space to store the extracted flags
1139 * @return True if libtrace was able to extract flags from the link header,
1140 * false otherwise.
1141 *
1142 * This function has been left internal because it is not portable across
1143 * drivers.
1144 */
1145bool trace_get_wireless_flags(void *link, libtrace_linktype_t linktype, uint8_t *flags);
1146#define TRACE_RADIOTAP_F_FCS 0x10
1147       
1148#ifdef __cplusplus
1149}
1150#endif
1151
1152#endif /* LIBTRACE_INT_H */
Note: See TracBrowser for help on using the repository browser.