source: lib/trace.c @ d0f25d4

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

Add ERF and PCAPNG decoders to libpacketdump

  • Property mode set to 100644
File size: 77.5 KB
RevLine 
[a79ddbe]1/*
[9636216]2 *
[ee6e802]3 * Copyright (c) 2007-2016 The University of Waikato, Hamilton, New Zealand.
[a79ddbe]4 * All rights reserved.
5 *
[ee6e802]6 * This file is part of libtrace.
7 *
[9636216]8 * This code has been developed by the University of Waikato WAND
[a79ddbe]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
[ee6e802]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
[a79ddbe]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
[ee6e802]19 * GNU Lesser General Public License for more details.
[a79ddbe]20 *
[ee6e802]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/>.
[a79ddbe]23 *
24 *
25 */
26
[2c060e3]27#define _GNU_SOURCE
[6c05d2c]28#include "common.h"
[60fc8aa]29#include "config.h"
[2c060e3]30#include <assert.h>
31#include <errno.h>
32#include <fcntl.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <sys/stat.h>
37#include <sys/types.h>
[6ed6c4a]38#ifndef WIN32
[77285d9]39#include <sys/socket.h>
[6ed6c4a]40#endif
[3d4d52d]41#include <stdarg.h>
[11041eb]42#include <sys/param.h>
[e5d3718]43
44#ifdef HAVE_LIMITS_H
45#  include <limits.h>
46#endif
47
[1f3696b]48#ifdef HAVE_SYS_LIMITS_H
49#  include <sys/limits.h>
50#endif
[e5d3718]51
[808a478]52#ifdef HAVE_NET_IF_ARP_H
53#  include <net/if_arp.h>
54#endif
55
56#ifdef HAVE_NET_IF_H
57#  include <net/if.h>
58#endif
59
60#ifdef HAVE_NETINET_IN_H
61#  include <netinet/in.h>
62#endif
63
64#ifdef HAVE_NET_ETHERNET_H
65#  include <net/ethernet.h>
66#endif
67
68#ifdef HAVE_NETINET_IF_ETHER_H
69#  include <netinet/if_ether.h>
70#endif
71
[2c060e3]72#include <time.h>
[6ed6c4a]73#ifdef WIN32
74#include <sys/timeb.h>
75#endif
[dfef05d]76
[2c060e3]77#include "libtrace.h"
[9e2a109]78#include "libtrace_int.h"
[2c060e3]79
[e5c2bc4]80#ifdef HAVE_PCAP_BPF_H
[6c05d2c]81#  include <pcap-bpf.h>
82#else
83#  ifdef HAVE_NET_BPF_H
84#    include <net/bpf.h>
85#  endif
86#endif
[2137b49]87
[e5c2bc4]88
[9e2a109]89#include "libtrace_int.h"
[72bfe20]90#include "format_helper.h"
[6dbc47a]91#include "rt_protocol.h"
[2c060e3]92
[29bbef0]93#include <pthread.h>
94#include <signal.h>
95
[91ebc50]96#define MAXOPTS 1024
97
[eda2def]98/* This file contains much of the implementation of the libtrace API itself. */
[c5a22ec]99
[4dfa44e]100static struct libtrace_format_t *formats_list = NULL;
[a6badc2]101
[b73407f]102volatile int libtrace_halt = 0;
[60e8e86]103/* Set once pstart is called used for backwards compatibility reasons */
[17a3dff]104int libtrace_parallel = 0;
[60e8e86]105
[d439067]106static const libtrace_packet_cache_t clearcache = {
107        -1, -1, -1, -1, NULL, 0, 0, NULL, 0, 0, NULL, 0, 0};
108
[6f75b9d]109/* strncpy is not assured to copy the final \0, so we
110 * will use our own one that does
111 */
[47d4f8c]112static inline void xstrncpy(char *dest, const char *src, size_t n,
113                size_t destlen)
[6f75b9d]114{
[47d4f8c]115        size_t slen = destlen - 1;
116        if (n < slen) {
117                slen = n;
118        }
119        strncpy(dest,src,slen);
120        dest[slen]='\0';
[6f75b9d]121}
[9636216]122
[6f75b9d]123static char *xstrndup(const char *src,size_t n)
[9636216]124{
[e502f76]125        char *ret=(char*)malloc(n+1);
[d694056]126        if (ret==NULL) {
[25a3255]127                fprintf(stderr,"Out of memory\n");
[d694056]128                exit(EXIT_FAILURE);
129        }
[47d4f8c]130        xstrncpy(ret,src,n,n+1);
[6f75b9d]131        return ret;
132}
133
[8dd1830]134
[dd2eaee]135/* call all the constructors if they haven't yet all been called */
[0bb8d49]136__attribute__((constructor))
[33d83d4]137static void trace_init(void)
[dd2eaee]138{
[f0cb0d4]139        struct timeval tv;
140        gettimeofday(&tv, NULL);
141        srand(tv.tv_sec);
142
[dd2eaee]143        if (!formats_list) {
[cd7eec7]144                duck_constructor();
[dd2eaee]145                erf_constructor();
[e01a738]146                tsh_constructor();
[dd2eaee]147                legacy_constructor();
[e1fdc05]148                atmhdr_constructor();
[035f8a7]149                linuxring_constructor();
[dd2eaee]150                linuxnative_constructor();
[e5c2bc4]151#ifdef HAVE_LIBPCAP
[dd2eaee]152                pcap_constructor();
153#endif
[755e794]154                bpf_constructor();
[dd2eaee]155                pcapfile_constructor();
[ed5b2ce]156                pcapng_constructor();
157                rt_constructor();
[ea75ec2]158                ndag_constructor();
[b663d33]159#ifdef HAVE_WANDDER
[df87f00]160                etsilive_constructor();
[b663d33]161#endif
[5e85c23]162#ifdef HAVE_DAG
163                dag_constructor();
164#endif
[c04929c]165#ifdef HAVE_DPDK
[c7e547e]166                dpdk_constructor();
167                dpdkndag_constructor();
[c04929c]168#endif
[dd2eaee]169        }
170}
171
[9636216]172/* Prints help information for libtrace
[40ba6ad]173 *
174 * Function prints out some basic help information regarding libtrace,
175 * and then prints out the help() function registered with each input module
176 */
[e1868fb]177DLLEXPORT void trace_help(void) {
[eeab9832]178        struct libtrace_format_t *tmp;
[dd2eaee]179        trace_init();
[47de0aa]180        printf("libtrace %s\n\n",PACKAGE_VERSION);
181        printf("Following this are a list of the format modules supported in this build of libtrace\n\n");
[eeab9832]182        for(tmp=formats_list;tmp;tmp=tmp->next) {
183                if (tmp->help)
184                        tmp->help();
[40ba6ad]185        }
186}
187
[4bd8a5b]188#define URI_PROTO_LINE 16U
[2c060e3]189
[eda2def]190/* Try to guess which format module is appropriate for a given trace file or
191 * device */
[91b72d3]192static void guess_format(libtrace_t *libtrace, const char *filename)
193{
194        struct libtrace_format_t *tmp;
[9636216]195
[91b72d3]196        /* Try and guess based on filename */
197        for(tmp = formats_list; tmp; tmp=tmp->next) {
198                if (tmp->probe_filename && tmp->probe_filename(filename)) {
199                        libtrace->format = tmp;
[23e1258]200                        libtrace->uridata = strdup(filename);
[91b72d3]201                        return;
202                }
203        }
204
205        libtrace->io = wandio_create(filename);
[2193905]206        if (!libtrace->io) {
207                trace_set_err(libtrace,TRACE_ERR_URI_NOT_FOUND,"Unable to find URI (%s)", filename);
[91b72d3]208                return;
[2193905]209        }
[91b72d3]210
211        /* Try and guess based on file magic */
212        for(tmp = formats_list; tmp; tmp=tmp->next) {
213                if (tmp->probe_magic && tmp->probe_magic(libtrace->io)) {
214                        libtrace->format = tmp;
[23e1258]215                        libtrace->uridata = strdup(filename);
[91b72d3]216                        return;
217                }
218        }
[9636216]219
[584d907]220        /* No formats matched -- make sure we clean up the IO object we
221         * used to probe the file magic */
222        wandio_destroy(libtrace->io);
[2193905]223        trace_set_err(libtrace,TRACE_ERR_BAD_FORMAT,"Unable to guess format (%s)", filename);
[91b72d3]224        return;
225}
[dd2eaee]226
[eda2def]227/* Creates an input trace from a URI
[fba4ca0]228 *
229 * @params char * containing a valid libtrace URI
[2c060e3]230 * @returns opaque pointer to a libtrace_t
231 *
[eda2def]232 * Some valid URI's are:
[277442c]233 *  erf:/path/to/erf/file
234 *  erf:/path/to/erf/file.gz
[9636216]235 *  erf:-                       (stdin)
[fba4ca0]236 *  dag:/dev/dagcard
[9636216]237 *  pcapint:pcapinterface               (eg: pcapint:eth0)
[eda2def]238 *  pcapfile:/path/to/pcap/file
239 *  pcapfile:-
240 *  int:interface                       (eg: int:eth0) only on Linux
241 *  rt:hostname
242 *  rt:hostname:port
[277442c]243 *
244 * If an error occured when attempting to open a trace, NULL is returned
245 * and an error is output to stdout.
[2c060e3]246 */
[e6d963c]247DLLEXPORT libtrace_t *trace_create(const char *uri) {
[9636216]248        libtrace_t *libtrace =
[e6d963c]249                        (libtrace_t *)malloc(sizeof(libtrace_t));
[de8c420b]250        char *scan = 0;
[9636216]251        const char *uridata = 0;
[0c222cc]252
[dd2eaee]253        trace_init();
254
[d694056]255        if (!libtrace) {
[25a3255]256                fprintf(stderr, "Unable to allocate memory in trace_create()\n");
[2dc1d9b]257                return NULL;
258        }
259
260        if(!uri) {
[25a3255]261                trace_set_err(libtrace, TRACE_ERR_URI_NULL, "NULL uri passed to trace_create()");
[2dc1d9b]262                return libtrace;
[d694056]263        }
[9636216]264
[eea0f87]265        libtrace->err.err_num = TRACE_ERR_NOERROR;
[0ea3526]266        libtrace->format=NULL;
[9636216]267
[b9aa767]268        libtrace->event.packet = NULL;
269        libtrace->event.psize = 0;
[cc9c9de]270        libtrace->event.first_ts = 0.0;
271        libtrace->event.first_now = 0.0;
[9cc1266]272        libtrace->event.waiting = false;
[880aa58]273        libtrace->filter = NULL;
274        libtrace->snaplen = 0;
[cc9c9de]275        libtrace->replayspeedup = 1;
[d8f02df]276        libtrace->started=false;
[37ee856]277        libtrace->startcount=0;
[670bbe5]278        libtrace->uridata = NULL;
[91b72d3]279        libtrace->io = NULL;
[670bbe5]280        libtrace->filtered_packets = 0;
[5bd27a3]281        libtrace->accepted_packets = 0;
[2fa43fa]282        libtrace->last_packet = NULL;
[25a3255]283
[29bbef0]284        /* Parallel inits */
[04bf7c5]285        ASSERT_RET(pthread_mutex_init(&libtrace->libtrace_lock, NULL), == 0);
[5e3f16c]286        ASSERT_RET(pthread_mutex_init(&libtrace->read_packet_lock, NULL), == 0);
[04bf7c5]287        ASSERT_RET(pthread_cond_init(&libtrace->perpkt_cond, NULL), == 0);
[fac8c46]288        libtrace->state = STATE_NEW;
[17a3dff]289        libtrace->perpkt_queue_full = false;
[29bbef0]290        libtrace->global_blob = NULL;
291        libtrace->hasher = NULL;
[ff12906]292        libtrace->hasher_data = NULL;
[e375e0f]293        libtrace->hasher_owner = HASH_OWNED_EXTERNAL;
[a49a9eb]294        libtrace_zero_ocache(&libtrace->packet_freelist);
[29bbef0]295        libtrace_zero_thread(&libtrace->hasher_thread);
[f051c1b]296        libtrace_zero_thread(&libtrace->reporter_thread);
[82facc5]297        libtrace_zero_thread(&libtrace->keepalive_thread);
[f051c1b]298        libtrace->reporter_thread.type = THREAD_EMPTY;
[17a3dff]299        libtrace->perpkt_thread_count = 0;
300        libtrace->perpkt_threads = NULL;
[fac8c46]301        libtrace->tracetime = 0;
[c99b1e5]302        libtrace->first_packets.first = 0;
303        libtrace->first_packets.count = 0;
304        libtrace->first_packets.packets = NULL;
[5ab626a]305        libtrace->stats = NULL;
[858ce90]306        libtrace->pread = NULL;
[91e016c]307        libtrace->sequence_number = 0;
[f051c1b]308        ZERO_USER_CONFIG(libtrace->config);
[f50515e]309        memset(&libtrace->combiner, 0, sizeof(libtrace->combiner));
[f625817]310        libtrace->perpkt_cbs = NULL;
311        libtrace->reporter_cbs = NULL;
[9e2a109]312
[eda2def]313        /* Parse the URI to determine what sort of trace we are dealing with */
[984f7da]314        if ((uridata = trace_parse_uri(uri, &scan)) == 0) {
[eda2def]315                /* Could not parse the URI nicely */
[91b72d3]316                guess_format(libtrace,uri);
[2193905]317                if (trace_is_err(libtrace)) {
[91b72d3]318                        return libtrace;
[6f75b9d]319                }
[9e2a109]320        }
[91b72d3]321        else {
322                struct libtrace_format_t *tmp;
323
[eda2def]324                /* Find a format that matches the first part of the URI */
[91b72d3]325                for (tmp=formats_list;tmp;tmp=tmp->next) {
326                        if (strlen(scan) == strlen(tmp->name) &&
327                                        strncasecmp(scan, tmp->name, strlen(scan)) == 0
328                                        ) {
329                                libtrace->format=tmp;
330                                break;
331                        }
332                }
[eda2def]333
[91b72d3]334                if (libtrace->format == 0) {
335                        trace_set_err(libtrace, TRACE_ERR_BAD_FORMAT,
336                                        "Unknown format (%s)",scan);
337                        return libtrace;
338                }
[9e2a109]339
[91b72d3]340                libtrace->uridata = strdup(uridata);
341        }
[7068467]342        /* libtrace->format now contains the type of uri
343         * libtrace->uridata contains the appropriate data for this
344         */
[9636216]345
346        /* Call the init_input function for the matching capture format */
[9e2a109]347        if (libtrace->format->init_input) {
[d5879cc]348                int err=libtrace->format->init_input(libtrace);
349                if (err==-1) {
[9636216]350                        /* init_input should call trace_set_err to set the
351                         * error message
[880aa58]352                         */
[0ea3526]353                        return libtrace;
[ffc8c8d]354                }
[9e2a109]355        } else {
[91db67b]356                trace_set_err(libtrace,TRACE_ERR_UNSUPPORTED,
[880aa58]357                                "Format does not support input (%s)",scan);
[0ea3526]358                return libtrace;
[9e2a109]359        }
[9636216]360
[dfbdd796]361        if (scan)
362                free(scan);
[ae66bde]363        libtrace->err.err_num=TRACE_ERR_NOERROR;
364        libtrace->err.problem[0]='\0';
[2c060e3]365        return libtrace;
366}
367
[fba4ca0]368/* Creates a "dummy" trace file that has only the format type set.
369 *
370 * @returns opaque pointer to a (sparsely initialised) libtrace_t
371 *
[880aa58]372 * IMPORTANT: Do not attempt to call trace_read_packet or other such functions
373 * with the dummy trace. Its intended purpose is to act as a packet->trace for
374 * libtrace_packet_t's that are not associated with a libtrace_t structure.
[fba4ca0]375 */
[e6d963c]376DLLEXPORT libtrace_t * trace_create_dead (const char *uri) {
377        libtrace_t *libtrace = (libtrace_t *) malloc(sizeof(libtrace_t));
[67a14d4]378        char *scan = (char *)calloc(sizeof(char),URI_PROTO_LINE);
[084d95a]379        char *uridata;
[eeab9832]380        struct libtrace_format_t *tmp;
[0453ca8]381
382        trace_init();
[9636216]383
[eea0f87]384        libtrace->err.err_num = TRACE_ERR_NOERROR;
[084d95a]385
386        if((uridata = strchr(uri,':')) == NULL) {
[47d4f8c]387                xstrncpy(scan, uri, strlen(uri), URI_PROTO_LINE);
[084d95a]388        } else {
[47d4f8c]389                xstrncpy(scan,uri, (size_t)(uridata - uri), URI_PROTO_LINE);
[084d95a]390        }
[9636216]391
[91b72d3]392        libtrace->err.err_num = TRACE_ERR_NOERROR;
393        libtrace->format=NULL;
[9636216]394
[91b72d3]395        libtrace->event.packet = NULL;
396        libtrace->event.psize = 0;
[cc9c9de]397        libtrace->event.first_ts = 0;
398        libtrace->event.first_now = 0;
[91b72d3]399        libtrace->filter = NULL;
400        libtrace->snaplen = 0;
401        libtrace->started=false;
[9c86dee]402        libtrace->startcount = 0;
[91b72d3]403        libtrace->uridata = NULL;
404        libtrace->io = NULL;
405        libtrace->filtered_packets = 0;
[2fa43fa]406        libtrace->accepted_packets = 0;
407        libtrace->last_packet = NULL;
[2725318]408
[29bbef0]409        /* Parallel inits */
[04bf7c5]410        ASSERT_RET(pthread_mutex_init(&libtrace->libtrace_lock, NULL), == 0);
[5e3f16c]411        ASSERT_RET(pthread_mutex_init(&libtrace->read_packet_lock, NULL), == 0);
[04bf7c5]412        ASSERT_RET(pthread_cond_init(&libtrace->perpkt_cond, NULL), == 0);
[fac8c46]413        libtrace->state = STATE_NEW; // TODO MAYBE DEAD
[17a3dff]414        libtrace->perpkt_queue_full = false;
[29bbef0]415        libtrace->global_blob = NULL;
416        libtrace->hasher = NULL;
[a49a9eb]417        libtrace_zero_ocache(&libtrace->packet_freelist);
[29bbef0]418        libtrace_zero_thread(&libtrace->hasher_thread);
[f051c1b]419        libtrace_zero_thread(&libtrace->reporter_thread);
[82facc5]420        libtrace_zero_thread(&libtrace->keepalive_thread);
[f051c1b]421        libtrace->reporter_thread.type = THREAD_EMPTY;
[17a3dff]422        libtrace->perpkt_thread_count = 0;
423        libtrace->perpkt_threads = NULL;
[fac8c46]424        libtrace->tracetime = 0;
[5ab626a]425        libtrace->stats = NULL;
[858ce90]426        libtrace->pread = NULL;
[91e016c]427        libtrace->sequence_number = 0;
[f051c1b]428        ZERO_USER_CONFIG(libtrace->config);
[f50515e]429        memset(&libtrace->combiner, 0, sizeof(libtrace->combiner));
[f625817]430        libtrace->perpkt_cbs = NULL;
431        libtrace->reporter_cbs = NULL;
[eeab9832]432        for(tmp=formats_list;tmp;tmp=tmp->next) {
433                if (strlen(scan) == strlen(tmp->name) &&
[084d95a]434                                !strncasecmp(scan,
[eeab9832]435                                        tmp->name,
[084d95a]436                                        strlen(scan))) {
[eeab9832]437                                libtrace->format=tmp;
[084d95a]438                                break;
439                                }
440        }
441        if (libtrace->format == 0) {
[0ea3526]442                trace_set_err(libtrace,TRACE_ERR_BAD_FORMAT,
[880aa58]443                                "Unknown format (%s)",scan);
[084d95a]444        }
[7ac9705]445
446        libtrace->format_data = NULL;
[ae8196e]447        free(scan);
[084d95a]448        return libtrace;
449
450}
[fba4ca0]451
[9636216]452/* Creates an output trace from a URI.
[5eae97a]453 *
[fba4ca0]454 * @param uri   the uri string describing the output format and destination
[9636216]455 * @returns opaque pointer to a libtrace_output_t
[fba4ca0]456 *
[880aa58]457 *  If an error occured when attempting to open the output trace, NULL is
[9636216]458 *  returned and trace_errno is set.
[fba4ca0]459 */
[9636216]460
[39e141f]461DLLEXPORT libtrace_out_t *trace_create_output(const char *uri) {
[9636216]462        libtrace_out_t *libtrace =
[e6d963c]463                        (libtrace_out_t*)malloc(sizeof(libtrace_out_t));
[9636216]464
[de8c420b]465        char *scan = 0;
[6f75b9d]466        const char *uridata = 0;
[eeab9832]467        struct libtrace_format_t *tmp;
[df2dff9]468
[dd2eaee]469        trace_init();
470
[eea0f87]471        libtrace->err.err_num = TRACE_ERR_NOERROR;
[d97bbb2]472        strcpy(libtrace->err.problem,"Error message set\n");
[52cc59f]473        libtrace->format = NULL;
474        libtrace->uridata = NULL;
[9636216]475
[eda2def]476        /* Parse the URI to determine what capture format we want to write */
[df2dff9]477
[de8c420b]478        if ((uridata = trace_parse_uri(uri, &scan)) == 0) {
[eeab9832]479                trace_set_err_out(libtrace,TRACE_ERR_BAD_FORMAT,
480                                "Bad uri format (%s)",uri);
[0ea3526]481                return libtrace;
[de8c420b]482        }
[9636216]483
[eda2def]484        /* Attempt to find the format in the list of supported formats */
[eeab9832]485        for(tmp=formats_list;tmp;tmp=tmp->next) {
486                if (strlen(scan) == strlen(tmp->name) &&
[df2dff9]487                                !strncasecmp(scan,
[eeab9832]488                                        tmp->name,
[df2dff9]489                                        strlen(scan))) {
[eeab9832]490                                libtrace->format=tmp;
[df2dff9]491                                break;
492                                }
493        }
[52cc59f]494        free(scan);
495
[eeab9832]496        if (libtrace->format == NULL) {
[0ea3526]497                trace_set_err_out(libtrace,TRACE_ERR_BAD_FORMAT,
[880aa58]498                                "Unknown output format (%s)",scan);
[eeab9832]499                return libtrace;
[df2dff9]500        }
501        libtrace->uridata = strdup(uridata);
502
[7068467]503        /* libtrace->format now contains the type of uri
504         * libtrace->uridata contains the appropriate data for this
505         */
[df2dff9]506
507        if (libtrace->format->init_output) {
[25a3255]508                int err = libtrace->format->init_output(libtrace);
509                if (err == -1) {
510                        /* init_output should call trace_set_err to set the
511                         * error message
512                         */
513                        return libtrace;
[ffc8c8d]514                }
[df2dff9]515        } else {
[91db67b]516                trace_set_err_out(libtrace,TRACE_ERR_UNSUPPORTED,
[880aa58]517                                "Format does not support writing (%s)",scan);
[0ea3526]518                return libtrace;
[df2dff9]519        }
520
521
[0a6638f]522        libtrace->started=false;
[91ebc50]523        return libtrace;
524}
[5eae97a]525
[eda2def]526/* Start an input trace
[37195b4]527 * @param libtrace      the input trace to start
528 * @returns 0 on success
529 *
530 * This does the work associated with actually starting up
531 * the trace.  it may fail.
532 */
[e6d963c]533DLLEXPORT int trace_start(libtrace_t *libtrace)
[37195b4]534{
[2dc1d9b]535        if(!libtrace) {
[25a3255]536                fprintf(stderr, "NULL trace passed to trace_start()\n");
[2dc1d9b]537                return TRACE_ERR_NULL_TRACE;
538        }
539
[8f18776]540        if (trace_is_err(libtrace))
541                return -1;
[37195b4]542        if (libtrace->format->start_input) {
543                int ret=libtrace->format->start_input(libtrace);
[49babe0]544                if (ret < 0) {
[37195b4]545                        return ret;
546                }
547        }
[37ee856]548        libtrace->startcount ++;
[37195b4]549        libtrace->started=true;
550        return 0;
551}
552
[eda2def]553/* Start an output trace */
[9636216]554DLLEXPORT int trace_start_output(libtrace_out_t *libtrace)
[0a6638f]555{
[2dc1d9b]556        if(!libtrace) {
[25a3255]557                fprintf(stderr, "NULL trace passed to trace_start_output()\n");
[2dc1d9b]558                return TRACE_ERR_NULL_TRACE;
559        }
[0a6638f]560        if (libtrace->format->start_output) {
561                int ret=libtrace->format->start_output(libtrace);
562                if (ret < 0) {
563                        return ret;
564                }
565        }
566
[eeab9832]567        libtrace->started=true;
[0a6638f]568        return 0;
569}
570
[39e141f]571DLLEXPORT int trace_pause(libtrace_t *libtrace)
[1fbd938]572{
[2dc1d9b]573        if(!libtrace) {
[25a3255]574                fprintf(stderr, "NULL trace passed to trace_pause()\n");
[2dc1d9b]575                return TRACE_ERR_NULL_TRACE;
576        }
[8f18776]577        if (!libtrace->started) {
578                trace_set_err(libtrace,TRACE_ERR_BAD_STATE, "You must call trace_start() before calling trace_pause()");
579                return -1;
580        }
[2fa43fa]581
582        /* Finish the last packet we read - for backwards compatibility */
[adb2c4c]583        if (!libtrace_parallel && libtrace->last_packet)
[2fa43fa]584                trace_fin_packet(libtrace->last_packet);
[2dc1d9b]585        if(libtrace->last_packet != NULL) {
[2725318]586                trace_set_err(libtrace, TRACE_ERR_PAUSE_FIN, "Unable to remove all data stored against trace in trace_pause()");
[2dc1d9b]587                return -1;
588        }
[2fa43fa]589
[1fbd938]590        if (libtrace->format->pause_input)
591                libtrace->format->pause_input(libtrace);
[2fa43fa]592
[1fbd938]593        libtrace->started=false;
594        return 0;
595}
596
[39e141f]597DLLEXPORT int trace_config(libtrace_t *libtrace,
[65a5900]598                trace_option_t option,
599                void *value)
600{
601        int ret;
[8f18776]602
603        if (trace_is_err(libtrace)) {
604                return -1;
605        }
[6b98325]606
607        if (option == TRACE_OPTION_HASHER)
608                return trace_set_hasher(libtrace,
609                                        (enum hasher_types) *((int *) value),
610                                        NULL, NULL);
611
[eda2def]612        /* If the capture format supports configuration, try using their
613         * native configuration first */
[65a5900]614        if (libtrace->format->config_input) {
615                ret=libtrace->format->config_input(libtrace,option,value);
616                if (ret==0)
617                        return 0;
618        }
[eda2def]619
620        /* If we get here, either the native configuration failed or the
[9636216]621         * format did not support configuration. However, libtrace can
[eda2def]622         * deal with some options itself, so give that a go */
[65a5900]623        switch(option) {
[cc9c9de]624                case TRACE_OPTION_REPLAY_SPEEDUP:
625                        /* Clear the error if there was one */
626                        if (trace_is_err(libtrace)) {
627                                trace_get_err(libtrace);
628                        }
629                        if (*(int*)value<1
630                                || *(int*)value>LIBTRACE_MAX_REPLAY_SPEEDUP) {
631                                trace_set_err(libtrace,TRACE_ERR_BAD_STATE,
632                                        "Invalid replay speed");
633                        }
634                        libtrace->replayspeedup=*(int*)value;
635                        return 0;
636
[65a5900]637                case TRACE_OPTION_SNAPLEN:
[205809b]638                        /* Clear the error if there was one */
639                        if (trace_is_err(libtrace)) {
[a3041a4]640                                trace_get_err(libtrace);
[205809b]641                        }
[9636216]642                        if (*(int*)value<0
[cdcaef6]643                                || *(int*)value>LIBTRACE_PACKET_BUFSIZE) {
644                                trace_set_err(libtrace,TRACE_ERR_BAD_STATE,
645                                        "Invalid snap length");
646                        }
[65a5900]647                        libtrace->snaplen=*(int*)value;
[67a14d4]648                        return 0;
[65a5900]649                case TRACE_OPTION_FILTER:
[205809b]650                        /* Clear the error if there was one */
651                        if (trace_is_err(libtrace)) {
[a3041a4]652                                trace_get_err(libtrace);
[205809b]653                        }
[e6d963c]654                        libtrace->filter=(libtrace_filter_t *)value;
[67a14d4]655                        return 0;
[65a5900]656                case TRACE_OPTION_PROMISC:
[205809b]657                        if (!trace_is_err(libtrace)) {
658                                trace_set_err(libtrace,TRACE_ERR_OPTION_UNAVAIL,
659                                                "Promisc mode is not supported by this format module");
660                        }
[65a5900]661                        return -1;
[646aca1]662                case TRACE_OPTION_META_FREQ:
[205809b]663                        if (!trace_is_err(libtrace)) {
[9636216]664                                trace_set_err(libtrace,
[205809b]665                                                TRACE_ERR_OPTION_UNAVAIL,
666                                                "This format does not support meta-data gathering");
667                        }
[cd7eec7]668                        return -1;
[646aca1]669                case TRACE_OPTION_EVENT_REALTIME:
670                        if (!trace_is_err(libtrace)) {
[9636216]671                                trace_set_err(libtrace,
[646aca1]672                                                TRACE_ERR_OPTION_UNAVAIL,
[f6e8d46]673                                                "This format does not support realtime events");
[646aca1]674                        }
675                        return -1;
[6b98325]676                case TRACE_OPTION_HASHER:
677                        /* Dealt with earlier */
678                        return -1;
[509ee47]679                case TRACE_OPTION_CONSTANT_ERF_FRAMING:
680                        if (!trace_is_err(libtrace)) {
681                                trace_set_err(libtrace,
682                                                TRACE_ERR_OPTION_UNAVAIL,
683                                                "This format does not feature an ERF header or does not support bypassing the framing length calculation");
684                        }
685                        return -1;
686
[d4eed70]687                case TRACE_OPTION_DISCARD_META:
688                        if (!trace_is_err(libtrace)) {
689                                trace_set_err(libtrace, TRACE_ERR_OPTION_UNAVAIL,
690                                        "Libtrace does not support meta packets for this format");
691                        }
692                        return -1;
[65a5900]693        }
[205809b]694        if (!trace_is_err(libtrace)) {
695                trace_set_err(libtrace,TRACE_ERR_UNKNOWN_OPTION,
696                        "Unknown option %i", option);
697        }
[67a14d4]698        return -1;
[65a5900]699}
700
[6b98325]701DLLEXPORT int trace_set_snaplen(libtrace_t *trace, int snaplen) {
702        return trace_config(trace, TRACE_OPTION_SNAPLEN, &snaplen);
703}
704
705DLLEXPORT int trace_set_promisc(libtrace_t *trace, bool promisc) {
706        int tmp = promisc;
707        return trace_config(trace, TRACE_OPTION_PROMISC, &tmp);
708}
709
710DLLEXPORT int trace_set_filter(libtrace_t *trace, libtrace_filter_t *filter) {
711        return trace_config(trace, TRACE_OPTION_FILTER, filter);
712}
713
714DLLEXPORT int trace_set_meta_freq(libtrace_t *trace, int freq) {
715        return trace_config(trace, TRACE_OPTION_META_FREQ, &freq);
716}
717
718DLLEXPORT int trace_set_event_realtime(libtrace_t *trace, bool realtime) {
719        int tmp = realtime;
720        return trace_config(trace, TRACE_OPTION_EVENT_REALTIME, &tmp);
721}
722
[e6d963c]723DLLEXPORT int trace_config_output(libtrace_out_t *libtrace, 
[7068467]724                trace_option_output_t option,
725                void *value) {
[9636216]726
[eda2def]727        /* Unlike the input options, libtrace does not natively support any of
728         * the output options - the format module must be able to deal with
729         * them. */
[9c6aa95]730        if (libtrace->format->config_output) {
[7068467]731                return libtrace->format->config_output(libtrace, option, value);
[9c6aa95]732        }
733        return -1;
[5eae97a]734}
735
[eda2def]736/* Close an input trace file, freeing up any resources it may have been using
[2c060e3]737 *
738 */
[e6d963c]739DLLEXPORT void trace_destroy(libtrace_t *libtrace) {
[04bf7c5]740        int i;
[2dc1d9b]741
[89cc0cc]742        if(!libtrace) {
[2193905]743                fprintf(stderr, "NULL trace passed to trace_destroy()\n");
[25a3255]744                return;
[89cc0cc]745        }
[9857d1c]746
[04bf7c5]747        ASSERT_RET(pthread_mutex_destroy(&libtrace->libtrace_lock), == 0);
[5e3f16c]748        ASSERT_RET(pthread_mutex_destroy(&libtrace->read_packet_lock), == 0);
[04bf7c5]749        ASSERT_RET(pthread_cond_destroy(&libtrace->perpkt_cond), == 0);
750
751        /* destroy any packets that are still around */
[c99b1e5]752        if (libtrace->state != STATE_NEW && libtrace->first_packets.packets) {
[9857d1c]753                for (i = 0; i < libtrace->perpkt_thread_count; ++i) {
754                        if(libtrace->first_packets.packets[i].packet) {
755                                trace_destroy_packet(libtrace->first_packets.packets[i].packet);
756                        }
757                }
758                free(libtrace->first_packets.packets);
[a49a9eb]759                ASSERT_RET(pthread_spin_destroy(&libtrace->first_packets.lock), == 0);
[9857d1c]760        }
761
[2fa43fa]762        /* Finish any the last packet we read - for backwards compatibility */
[adb2c4c]763        if (!libtrace_parallel && libtrace->last_packet) {
[2fa43fa]764                trace_fin_packet(libtrace->last_packet);
[903f64d]765        }
[89cc0cc]766        if (libtrace->last_packet != NULL) {
767                trace_set_err(libtrace, TRACE_ERR_PAUSE_FIN, "Unable to remove all data stored against trace in trace_destroy()");
[25a3255]768                return;
[89cc0cc]769        }
[2fa43fa]770
[4e65f42]771        if (libtrace->format) {
772                if (libtrace->started && libtrace->format->pause_input)
773                        libtrace->format->pause_input(libtrace);
[528eb6e]774                if (libtrace->format->fin_input)
775                        libtrace->format->fin_input(libtrace);
[4e65f42]776        }
[29bbef0]777        /* Need to free things! */
778        if (libtrace->uridata)
[4e65f42]779                free(libtrace->uridata);
[5ab626a]780
781        if (libtrace->stats)
782                free(libtrace->stats);
[2725318]783
[29bbef0]784        /* Empty any packet memory */
[c99b1e5]785        if (libtrace->state != STATE_NEW) {
[a49a9eb]786                // This has all of our packets
787                libtrace_ocache_destroy(&libtrace->packet_freelist);
[92a2bf6]788                for (i = 0; i < libtrace->perpkt_thread_count; ++i) {
[dc27de3]789                        libtrace_message_queue_destroy(&libtrace->perpkt_threads[i].messages);
790                }
791                if (libtrace->hasher_thread.type == THREAD_HASHER)
792                        libtrace_message_queue_destroy(&libtrace->hasher_thread.messages);
793                if (libtrace->keepalive_thread.type == THREAD_KEEPALIVE)
794                        libtrace_message_queue_destroy(&libtrace->keepalive_thread.messages);
795                if (libtrace->reporter_thread.type == THREAD_REPORTER)
796                        libtrace_message_queue_destroy(&libtrace->reporter_thread.messages);
[92a2bf6]797
798
[f625817]799                if (libtrace->combiner.destroy && libtrace->reporter_cbs)
[2498008]800                        libtrace->combiner.destroy(libtrace, &libtrace->combiner);
[c99b1e5]801                free(libtrace->perpkt_threads);
802                libtrace->perpkt_threads = NULL;
803                libtrace->perpkt_thread_count = 0;
[f625817]804
[29bbef0]805        }
[f625817]806
[e375e0f]807        if (libtrace->hasher_owner == HASH_OWNED_LIBTRACE) {
808                if (libtrace->hasher_data) {
809                        free(libtrace->hasher_data);
810                }
811        }
812
813
[f625817]814        if (libtrace->perpkt_cbs)
815                trace_destroy_callback_set(libtrace->perpkt_cbs);
816        if (libtrace->reporter_cbs)
817                trace_destroy_callback_set(libtrace->reporter_cbs);
818
[1935565]819        if (libtrace->event.packet) {
820                /* Don't use trace_destroy_packet here - there is almost
821                 * certainly going to be another libtrace_packet_t that is
822                 * pointing to the buffer for this packet, so we don't want
823                 * to free it. Rather, it will get freed when the user calls
824                 * trace_destroy_packet on the libtrace_packet_t that they
825                 * own.
826                 *
827                 * All we need to do then is free our packet structure itself.
828                 */
829                 free(libtrace->event.packet);
830        }
[903f64d]831
[9857d1c]832        free(libtrace);
[2c060e3]833}
834
[084d95a]835
[e6d963c]836DLLEXPORT void trace_destroy_dead(libtrace_t *libtrace) {
[89cc0cc]837        if(!libtrace) {
[2193905]838                fprintf(stderr, "NULL trace passed to trace_destroy_dead()\n");
[25a3255]839                return;
[89cc0cc]840        }
[eda2def]841
[04bf7c5]842        ASSERT_RET(pthread_mutex_destroy(&libtrace->libtrace_lock), == 0);
[5e3f16c]843        ASSERT_RET(pthread_mutex_destroy(&libtrace->read_packet_lock), == 0);
[04bf7c5]844        ASSERT_RET(pthread_cond_destroy(&libtrace->perpkt_cond), == 0);
845
[eda2def]846        /* Don't call pause_input or fin_input, because we should never have
847         * used this trace to do any reading anyway. Do make sure we free
848         * any format_data that has been created, though. */
[f0fb38f]849        if (libtrace->format_data)
850                free(libtrace->format_data);
[084d95a]851        free(libtrace);
852}
[fba4ca0]853/* Close an output trace file, freeing up any resources it may have been using
[5eae97a]854 *
855 * @param libtrace      the output trace file to be destroyed
[eda2def]856 */
[25a3255]857DLLEXPORT void trace_destroy_output(libtrace_out_t *libtrace) {
[89cc0cc]858        if(!libtrace) {
[25a3255]859                fprintf(stderr, "NULL trace passed to trace_destroy_output()\n");
860                return;
[89cc0cc]861        }
[9e6b38b]862        if (libtrace->format && libtrace->format->fin_output)
[61b4543]863                libtrace->format->fin_output(libtrace);
[52cc59f]864        if (libtrace->uridata)
865                free(libtrace->uridata);
[5eae97a]866        free(libtrace);
867}
868
[32ee9b2]869DLLEXPORT int trace_flush_output(libtrace_out_t *libtrace) {
870        if (!libtrace) {
[25a3255]871                fprintf(stderr, "NULL trace passed to trace_flush_output()\n");
[2dc1d9b]872                return TRACE_ERR_NULL_TRACE;
[32ee9b2]873        }
874        if (libtrace->format && libtrace->format->flush_output) {
875                return libtrace->format->flush_output(libtrace);
876        }
[25a3255]877
878        return 0;
[32ee9b2]879}
880
[a49a9eb]881DLLEXPORT libtrace_packet_t *trace_create_packet(void)
[33d83d4]882{
[a49a9eb]883        libtrace_packet_t *packet =
[4bd8a5b]884                (libtrace_packet_t*)calloc((size_t)1,sizeof(libtrace_packet_t));
[7c72e4d]885
[fd4482d]886        if (packet == NULL)
887                return NULL;
888
[c1db742]889        packet->buf_control=TRACE_CTRL_PACKET;
[37ee856]890        packet->which_trace_start = 0;
[c1205bd]891        pthread_mutex_init(&(packet->ref_lock), NULL);
[9cc1266]892        trace_clear_cache(packet);
[14d8a63]893        return packet;
894}
895
[39e141f]896DLLEXPORT libtrace_packet_t *trace_copy_packet(const libtrace_packet_t *packet) {
[37ee856]897        libtrace_packet_t *dest;
898
899        if (packet->which_trace_start != packet->trace->startcount) {
900                return NULL;
901        }
902
903        dest = (libtrace_packet_t *)calloc((size_t)1, sizeof(libtrace_packet_t));
[ca1272c]904        if (!dest) {
[eda2def]905                printf("Out of memory constructing packet\n");
[ca1272c]906                abort();
907        }
[91c9552]908        dest->trace=packet->trace;
[9bc4689]909        dest->buffer=malloc(65536);
[ca1272c]910        if (!dest->buffer) {
[eda2def]911                printf("Out of memory allocating buffer memory\n");
[ca1272c]912                abort();
913        }
[91c9552]914        dest->header=dest->buffer;
915        dest->payload=(void*)
916                ((char*)dest->buffer+trace_get_framing_length(packet));
917        dest->type=packet->type;
918        dest->buf_control=TRACE_CTRL_PACKET;
[29bbef0]919        dest->order = packet->order;
[d994324]920        dest->hash = packet->hash;
921        dest->error = packet->error;
[37ee856]922        dest->which_trace_start = packet->which_trace_start;
[e1fdc05]923        /* Reset the cache - better to recalculate than try to convert
924         * the values over to the new packet */
[9636216]925        trace_clear_cache(dest);
[eda2def]926        /* Ooooh nasty memcpys! This is why we want to avoid copying packets
927         * as much as possible */
[91c9552]928        memcpy(dest->header,packet->header,trace_get_framing_length(packet));
929        memcpy(dest->payload,packet->payload,trace_get_capture_length(packet));
[d8f02df]930
931        return dest;
[91c9552]932}
933
[14d8a63]934/** Destroy a packet object
935 */
[e2aebe7]936DLLEXPORT void trace_destroy_packet(libtrace_packet_t *packet) {
[29bbef0]937        /* Free any resources possibly associated with the packet */
[60e8e86]938        if (libtrace_parallel && packet->trace && packet->trace->format->fin_packet) {
[29bbef0]939                packet->trace->format->fin_packet(packet);
940        }
[adb2c4c]941        if (!libtrace_parallel && packet->trace &&
942             packet->trace->last_packet == packet) {
[9a3a846]943                packet->trace->last_packet = NULL;
[adb2c4c]944        }
[2dc1d9b]945
[1935565]946        if (packet->buf_control == TRACE_CTRL_PACKET && packet->buffer) {
[e2aebe7]947                free(packet->buffer);
948        }
[c1205bd]949        pthread_mutex_destroy(&(packet->ref_lock));
[9636216]950        packet->buf_control=(buf_control_t)'\0';
[eda2def]951                                /* A "bad" value to force an assert
[85a79b0]952                                 * if this packet is ever reused
953                                 */
[e2aebe7]954        free(packet);
[85e87b5]955}
[14d8a63]956
[29bbef0]957/**
958 * Removes any possible data stored againt the trace and releases any data.
959 * This will not destroy a reusable good malloc'd buffer (TRACE_CTRL_PACKET)
960 * use trace_destroy_packet() for those diabolical purposes.
961 */
962void trace_fin_packet(libtrace_packet_t *packet) {
963        if (packet)
964        {
965                if (packet->trace && packet->trace->format->fin_packet) {
966                        packet->trace->format->fin_packet(packet);
967                }
[d391ce0]968
969                if (packet->srcbucket && packet->internalid != 0) {
970                        libtrace_bucket_t *b = (libtrace_bucket_t *)packet->srcbucket;
971                        libtrace_release_bucket_id(b, packet->internalid);
972                }
973
[348396b]974                if (packet->trace) {
[adb2c4c]975                        if (!libtrace_parallel && packet->trace->last_packet == packet)
976                                packet->trace->last_packet = NULL;
[348396b]977                }
[29bbef0]978
979                // No matter what we remove the header and link pointers
980                packet->trace = NULL;
981                packet->header = NULL;
982                packet->payload = NULL;
983
984                if (packet->buf_control != TRACE_CTRL_PACKET)
985                {
986                        packet->buffer = NULL;
987                }
988
[bdc8b36]989                trace_clear_cache(packet);
[29bbef0]990                packet->hash = 0;
991                packet->order = 0;
[348396b]992                packet->srcbucket = NULL;
[29bbef0]993        }
994}
995
[eda2def]996/* Read one packet from the trace into buffer. Note that this function will
997 * block until a packet is read (or EOF is reached).
[2c060e3]998 *
[9636216]999 * @param libtrace      the libtrace opaque pointer
1000 * @param packet        the packet opaque pointer
[fba4ca0]1001 * @returns 0 on EOF, negative value on error
[2c060e3]1002 *
1003 */
[39e141f]1004DLLEXPORT int trace_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) {
[2137b49]1005
[2dc1d9b]1006        if (!libtrace) {
[25a3255]1007                fprintf(stderr, "NULL trace passed to trace_read_packet()\n");
[2dc1d9b]1008                return TRACE_ERR_NULL_TRACE;
1009        }
[33b735c]1010
[8f18776]1011        if (trace_is_err(libtrace))
1012                return -1;
[33b735c]1013
[8f18776]1014        if (!libtrace->started) {
[25a3255]1015                trace_set_err(libtrace,TRACE_ERR_BAD_STATE,"You must call libtrace_start() before trace_read_packet()");
[8f18776]1016                return -1;
1017        }
[2725318]1018
1019        if (!packet) {
[25a3255]1020                trace_set_err(libtrace, TRACE_ERR_NULL_PACKET, "NULL packet passed into trace_read_packet()");
[33b735c]1021                return -1;
[2725318]1022        }
1023
[9636216]1024        if (!(packet->buf_control==TRACE_CTRL_PACKET
1025                    || packet->buf_control==TRACE_CTRL_EXTERNAL)) {
[25a3255]1026                trace_set_err(libtrace,TRACE_ERR_BAD_STATE,"Packet passed to trace_read_packet() is invalid");
[8f18776]1027                return -1;
1028        }
[7c72e4d]1029
[4dedc28]1030        if (libtrace->format->read_packet) {
[348396b]1031                /* Finalise the packet, freeing any resources the format module
1032                 * may have allocated it and zeroing all data associated with it.
1033                 */
[903f64d]1034                if (packet->trace == libtrace) {
[348396b]1035                        trace_fin_packet(packet);
[903f64d]1036                }
[880aa58]1037                do {
[a7d1914]1038                        size_t ret;
[9e429e8]1039                        int filtret;
[5e3f16c]1040                        if ((ret=is_halted(libtrace)) != (size_t)-1)
1041                                return ret;
[29bbef0]1042                        /* Store the trace we are reading from into the packet opaque
1043                         * structure */
1044                        packet->trace = libtrace;
[bccdffc]1045                        packet->which_trace_start = libtrace->startcount;
[a7d1914]1046                        ret=libtrace->format->read_packet(libtrace,packet);
[5e3f16c]1047                        if (ret==(size_t)READ_MESSAGE ||
1048                            ret==(size_t)-1 || ret==0) {
[903f64d]1049                                packet->trace = NULL;
[a7d1914]1050                                return ret;
[222d8f5]1051                        }
[348396b]1052                        if (libtrace->filter) {
[880aa58]1053                                /* If the filter doesn't match, read another
1054                                 * packet
1055                                 */
[b8d8186]1056                                filtret = trace_apply_filter(libtrace->filter, packet);
1057                                if (filtret == -1) {
1058                                        /* Error compiling filter, probably */
1059                                        return ~0U;
1060                                }
[348396b]1061
[b8d8186]1062                                if (filtret == 0) {
[9d3c721]1063                                        ++libtrace->filtered_packets;
[348396b]1064                                        trace_fin_packet(packet);
1065                                        continue;
[880aa58]1066                                }
1067                        }
1068                        if (libtrace->snaplen>0) {
1069                                /* Snap the packet */
1070                                trace_set_capture_length(packet,
1071                                                libtrace->snaplen);
1072                        }
[ed5b2ce]1073                        if (!IS_LIBTRACE_META_PACKET(packet)) {
1074                                ++libtrace->accepted_packets;
1075                        }
[c862ad1]1076                        if (packet->order == 0) {
1077                                trace_packet_set_order(packet, libtrace->sequence_number);
1078                        }
[ed5b2ce]1079                        ++libtrace->sequence_number;
[adb2c4c]1080                        if (!libtrace_parallel && packet->trace == libtrace)
[0a368ae]1081                                libtrace->last_packet = packet;
[348396b]1082
[a7d1914]1083                        return ret;
[880aa58]1084                } while(1);
[a22c979]1085        }
[91db67b]1086        trace_set_err(libtrace,TRACE_ERR_UNSUPPORTED,"This format does not support reading packets\n");
[85a79b0]1087        return ~0U;
[2c060e3]1088}
1089
[eda2def]1090/* Converts the provided buffer into a libtrace packet of the given type.
1091 *
1092 * Unlike trace_construct_packet, the buffer is expected to begin with the
1093 * appropriate capture format header for the format type that the packet is
1094 * being converted to. This also allows for a packet to be converted into
[9636216]1095 * just about capture format that is supported by libtrace, provided the
[eda2def]1096 * format header is present in the buffer.
1097 *
1098 * This function is primarily used to convert packets received via the RT
1099 * protocol back into their original capture format. The RT header encapsulates
[9636216]1100 * the original capture format header, so after removing it the packet must
[eda2def]1101 * have it's header and payload pointers updated and the packet format and type
1102 * changed, amongst other things.
1103 *
[9636216]1104 * Intended only for internal use at this point - this function is not
[eda2def]1105 * available through the external libtrace API.
1106 */
[f0fb38f]1107int trace_prepare_packet(libtrace_t *trace, libtrace_packet_t *packet,
1108                void *buffer, libtrace_rt_types_t rt_type, uint32_t flags) {
1109
[2dc1d9b]1110        if (!trace) {
[25a3255]1111                fprintf(stderr, "NULL trace passed into trace_prepare_packet()\n");
[2dc1d9b]1112                return TRACE_ERR_NULL_TRACE;
1113        }
[9636216]1114
[33b735c]1115        if (!packet) {
[25a3255]1116                trace_set_err(trace, TRACE_ERR_NULL_TRACE, "NULL packet passed into trace_prepare_packet()");
[f0fb38f]1117                return -1;
[33b735c]1118        }
[9636216]1119
[33b735c]1120        if (!buffer) {
[25a3255]1121                trace_set_err(trace, TRACE_ERR_NULL_BUFFER, "NULL buffer passed into trace_prepare_packet()");
[f0fb38f]1122                return -1;
[33b735c]1123        }
[f0fb38f]1124
1125        if (!(packet->buf_control==TRACE_CTRL_PACKET || packet->buf_control==TRACE_CTRL_EXTERNAL)) {
[25a3255]1126                trace_set_err(trace,TRACE_ERR_BAD_STATE,"Packet passed to trace_read_packet() is invalid");
[f0fb38f]1127                return -1;
1128        }
[9636216]1129
[f0fb38f]1130        packet->trace = trace;
[adb2c4c]1131        if (!libtrace_parallel)
1132                trace->last_packet = packet;
[f0fb38f]1133        /* Clear packet cache */
[9cc1266]1134        trace_clear_cache(packet);
[f0fb38f]1135
1136        if (trace->format->prepare_packet) {
1137                return trace->format->prepare_packet(trace, packet,
1138                                buffer, rt_type, flags);
1139        }
[9636216]1140        trace_set_err(trace, TRACE_ERR_UNSUPPORTED,
[25a3255]1141                        "This format does not support preparing packets");
[f0fb38f]1142        return -1;
1143
1144}
1145
[eda2def]1146/* Writes a packet to the specified output trace
[5eae97a]1147 *
1148 * @param libtrace      describes the output format, destination, etc.
1149 * @param packet        the packet to be written out
1150 * @returns the number of bytes written, -1 if write failed
[eda2def]1151 */
[3a169d7]1152DLLEXPORT int trace_write_packet(libtrace_out_t *libtrace, libtrace_packet_t *packet) {
[2193905]1153
[2dc1d9b]1154        if (!libtrace) {
[25a3255]1155                fprintf(stderr, "NULL trace passed into trace_write_packet()\n");
[2dc1d9b]1156                return TRACE_ERR_NULL_TRACE;
1157        }
1158        if (!packet) {
[25a3255]1159                trace_set_err_out(libtrace, TRACE_ERR_NULL_PACKET, "NULL trace passed into trace_write_packet()");
[33b735c]1160                return -1;
[2dc1d9b]1161        }
[a8a9355]1162        /* Verify the packet is valid */
[8f18776]1163        if (!libtrace->started) {
1164                trace_set_err_out(libtrace,TRACE_ERR_BAD_STATE,
[2193905]1165                        "You must call trace_start_output() before calling trace_write_packet()");
[8f18776]1166                return -1;
1167        }
[5eae97a]1168
[ed5b2ce]1169        /* Don't try to convert meta-packets across formats */
1170        if (strcmp(libtrace->format->name, packet->trace->format->name) != 0 &&
1171                        IS_LIBTRACE_META_PACKET(packet)) {
1172                return 0;
1173        }
1174
[1fc2f6a]1175        if (libtrace->format->write_packet) {
1176                return libtrace->format->write_packet(libtrace, packet);
[5eae97a]1177        }
[8a1fb3c]1178        trace_set_err_out(libtrace,TRACE_ERR_UNSUPPORTED,
1179                "This format does not support writing packets");
[9c6aa95]1180        return -1;
[5eae97a]1181}
[2c060e3]1182
[eda2def]1183/* Get a pointer to the first byte of the packet payload */
[180f095]1184DLLEXPORT void *trace_get_packet_buffer(const libtrace_packet_t *packet,
1185                libtrace_linktype_t *linktype, uint32_t *remaining) {
[b185327]1186        int cap_len;
1187        int wire_len;
[86087c53]1188        libtrace_linktype_t ltype;
[b185327]1189
[2dc1d9b]1190        if (!packet) {
[25a3255]1191                fprintf(stderr, "NULL packet passed into trace_get_packet_buffer()\n");
[2dc1d9b]1192                return NULL;
1193        }
[86087c53]1194        ltype = trace_get_link_type(packet);
1195
1196        if (linktype) {
1197                *linktype = ltype;
1198        }
1199
1200        if (ltype == TRACE_TYPE_CONTENT_INVALID) {
1201                if (remaining) {
1202                        *remaining = 0;
[37ee856]1203                }
[86087c53]1204                return NULL;
1205        }
[37ee856]1206
[86087c53]1207        if (remaining) {
[b185327]1208                /* I think we should choose the minimum of the capture and
1209                 * wire lengths to be the "remaining" value. If the packet has
1210                 * been padded to increase the capture length, we don't want
[9636216]1211                 * to allow subsequent protocol decoders to consider the
[b185327]1212                 * padding as part of the packet.
1213                 *
1214                 * For example, in Auck 4 there is a trace where the IP header
1215                 * length is incorrect (24 bytes) followed by a 20 byte TCP
1216                 * header. Total IP length is 40 bytes. As a result, the
1217                 * legacyatm padding gets treated as the "missing" bytes of
1218                 * the TCP header, which isn't the greatest. We're probably
1219                 * better off returning an incomplete TCP header in that case.
1220                 */
[9636216]1221
[b185327]1222                cap_len = trace_get_capture_length(packet);
1223                wire_len = trace_get_wire_length(packet);
1224
[2725318]1225                if (!(cap_len >= 0)) {
[25a3255]1226                        fprintf(stderr, "Was expecting capture length of atleast 0 in trace_get_packet_buffer()\n");
[2725318]1227                        return NULL;
1228                }
[d3320d1]1229
1230                /* There is the odd corrupt packet, e.g. in IPLS II, that have
1231                 * massively negative wire lens. We could assert fail here on
1232                 * them, but we could at least try the capture length instead.
[9636216]1233                 *
[d3320d1]1234                 * You may still run into problems if you try to write that
1235                 * packet, but at least reading should work OK.
1236                 */
1237                if (wire_len < 0)
1238                        *remaining = cap_len;
1239                else if (wire_len < cap_len)
[b185327]1240                        *remaining = wire_len;
1241                else
1242                        *remaining = cap_len;
1243                /* *remaining = trace_get_capture_length(packet); */
1244        }
[180f095]1245        return (void *) packet->payload;
1246}
1247
[eda2def]1248
[9636216]1249/* Get a pointer to the first byte of the packet payload
[eda2def]1250 *
1251 * DEPRECATED - use trace_get_packet_buffer() instead */
[e6d963c]1252DLLEXPORT void *trace_get_link(const libtrace_packet_t *packet) {
[14d8a63]1253        return (void *)packet->payload;
[2c060e3]1254}
1255
[9636216]1256/* Get the current time in DAG time format
1257 * @param packet        a pointer to a libtrace_packet structure
[2c060e3]1258 * @returns a 64 bit timestamp in DAG ERF format (upper 32 bits are the seconds
1259 * past 1970-01-01, the lower 32bits are partial seconds)
[9636216]1260 */
[39e141f]1261DLLEXPORT uint64_t trace_get_erf_timestamp(const libtrace_packet_t *packet) {
[37ee856]1262        if (packet->which_trace_start != packet->trace->startcount) {
1263                return (uint64_t)0;
1264        }
1265
[4dedc28]1266        if (packet->trace->format->get_erf_timestamp) {
[7068467]1267                /* timestamp -> timestamp */
[1aa4bf7]1268                return packet->trace->format->get_erf_timestamp(packet);
1269        } else if (packet->trace->format->get_timespec) {
1270                /* timespec -> timestamp */
1271                struct timespec ts;
1272                ts = packet->trace->format->get_timespec(packet);
1273                return ((((uint64_t)ts.tv_sec) << 32) +
1274                                (((uint64_t)ts.tv_nsec << 32)/1000000000));
[4dedc28]1275        } else if (packet->trace->format->get_timeval) {
[7068467]1276                /* timeval -> timestamp */
[1aa4bf7]1277                struct timeval tv;
1278                tv = packet->trace->format->get_timeval(packet);
1279                return ((((uint64_t)tv.tv_sec) << 32) +
1280                                (((uint64_t)tv.tv_usec << 32)/1000000));
[ca87bcd]1281        } else if (packet->trace->format->get_seconds) {
[7068467]1282                /* seconds -> timestamp */
[1aa4bf7]1283                double seconds = packet->trace->format->get_seconds(packet);
1284                return (((uint64_t)seconds)<<32)
[4569c93]1285                          + (uint64_t)((seconds-(uint64_t)seconds)*UINT_MAX);
[ca87bcd]1286        }
[1aa4bf7]1287        else {
1288                return (uint64_t)0;
1289        }
[2c060e3]1290}
1291
[fba4ca0]1292/* Get the current time in struct timeval
[850630f]1293 * @param packet        a pointer to a libtrace_packet structure
1294 *
[2c060e3]1295 * @returns time that this packet was seen in a struct timeval
1296 * @author Daniel Lawson
1297 * @author Perry Lorier
[9636216]1298 */
[39e141f]1299DLLEXPORT struct timeval trace_get_timeval(const libtrace_packet_t *packet) {
[2c060e3]1300        struct timeval tv;
[4dedc28]1301        uint64_t ts = 0;
[37ee856]1302
1303        if (packet->which_trace_start != packet->trace->startcount) {
1304                tv.tv_sec=-1;
1305                tv.tv_usec=-1;
1306        } else if (packet->trace->format->get_timeval) {
[7068467]1307                /* timeval -> timeval */
[4dedc28]1308                tv = packet->trace->format->get_timeval(packet);
1309        } else if (packet->trace->format->get_erf_timestamp) {
[7068467]1310                /* timestamp -> timeval */
[4dedc28]1311                ts = packet->trace->format->get_erf_timestamp(packet);
1312                tv.tv_sec = ts >> 32;
[11041eb]1313                tv.tv_usec = ((ts&0xFFFFFFFF)*1000000)>>32;
[9636216]1314                if (tv.tv_usec >= 1000000) {
1315                        tv.tv_usec -= 1000000;
1316                        tv.tv_sec += 1;
1317                }
[1aa4bf7]1318        } else if (packet->trace->format->get_timespec) {
1319                struct timespec ts = packet->trace->format->get_timespec(packet);
1320                tv.tv_sec = ts.tv_sec;
1321                tv.tv_usec = ts.tv_nsec/1000;
[ca87bcd]1322        } else if (packet->trace->format->get_seconds) {
[7068467]1323                /* seconds -> timeval */
[1aa4bf7]1324                double seconds = packet->trace->format->get_seconds(packet);
[ca87bcd]1325                tv.tv_sec = (uint32_t)seconds;
1326                tv.tv_usec = (uint32_t)(((seconds - tv.tv_sec) * 1000000)/UINT_MAX);
[4dedc28]1327        }
[302714f]1328        else {
1329                tv.tv_sec=-1;
1330                tv.tv_usec=-1;
1331        }
[4dedc28]1332
[29bbef0]1333    return tv;
[2c060e3]1334}
1335
[1aa4bf7]1336DLLEXPORT struct timespec trace_get_timespec(const libtrace_packet_t *packet) {
1337        struct timespec ts;
1338
[37ee856]1339        if (packet->which_trace_start != packet->trace->startcount) {
1340                ts.tv_sec=-1;
1341                ts.tv_nsec=-1;
1342        } else if (packet->trace->format->get_timespec) {
[1aa4bf7]1343                return packet->trace->format->get_timespec(packet);
1344        } else if (packet->trace->format->get_erf_timestamp) {
1345                /* timestamp -> timeval */
1346                uint64_t erfts = packet->trace->format->get_erf_timestamp(packet);
1347                ts.tv_sec = erfts >> 32;
[11041eb]1348                ts.tv_nsec = ((erfts&0xFFFFFFFF)*1000000000)>>32;
[9636216]1349                if (ts.tv_nsec >= 1000000000) {
1350                        ts.tv_nsec -= 1000000000;
1351                        ts.tv_sec += 1;
1352                }
[1aa4bf7]1353        } else if (packet->trace->format->get_timeval) {
1354                /* timeval -> timespec */
1355                struct timeval tv = packet->trace->format->get_timeval(packet);
1356                ts.tv_sec = tv.tv_sec;
1357                ts.tv_nsec = tv.tv_usec*1000;
1358        } else if (packet->trace->format->get_seconds) {
1359                /* seconds -> timespec */
1360                double seconds = packet->trace->format->get_seconds(packet);
1361                ts.tv_sec = (uint32_t)seconds;
1362                ts.tv_nsec = (long)(((seconds - ts.tv_sec) * 1000000000)/UINT_MAX);
1363        }
1364        else {
1365                ts.tv_sec=-1;
1366                ts.tv_nsec=-1;
1367        }
[37ee856]1368        return ts;
[1aa4bf7]1369}
1370
1371
[fba4ca0]1372/* Get the current time in floating point seconds
[9636216]1373 * @param packet        a pointer to a libtrace_packet structure
[2c060e3]1374 * @returns time that this packet was seen in 64bit floating point seconds
[9636216]1375 */
[e6d963c]1376DLLEXPORT double trace_get_seconds(const libtrace_packet_t *packet) {
[9c6aa95]1377        double seconds = 0.0;
[a8a9355]1378
[37ee856]1379        if (packet->which_trace_start != packet->trace->startcount) {
1380                return 0.0;
1381        }
1382
[4dedc28]1383        if (packet->trace->format->get_seconds) {
[7068467]1384                /* seconds->seconds */
[4dedc28]1385                seconds = packet->trace->format->get_seconds(packet);
1386        } else if (packet->trace->format->get_erf_timestamp) {
[7068467]1387                /* timestamp -> seconds */
[1aa4bf7]1388                uint64_t ts = 0;
[4dedc28]1389                ts = packet->trace->format->get_erf_timestamp(packet);
1390                seconds =  (ts>>32) + ((ts & UINT_MAX)*1.0 / UINT_MAX);
[1aa4bf7]1391        } else if (packet->trace->format->get_timespec) {
1392                /* timespec -> seconds */
1393                struct timespec ts;
1394                ts = packet->trace->format->get_timespec(packet);
1395                seconds = ts.tv_sec + ((ts.tv_nsec * 1.0) / 1000000000);
[ca87bcd]1396        } else if (packet->trace->format->get_timeval) {
[7068467]1397                /* timeval -> seconds */
[1aa4bf7]1398                struct timeval tv;
[ca87bcd]1399                tv = packet->trace->format->get_timeval(packet);
[b51edf5]1400                seconds = tv.tv_sec + ((tv.tv_usec * 1.0) / 1000000);
[ca87bcd]1401        }
1402
[4dedc28]1403        return seconds;
[2c060e3]1404}
1405
[9636216]1406DLLEXPORT size_t trace_get_capture_length(const libtrace_packet_t *packet)
[a7d1914]1407{
[7c72e4d]1408        /* Cache the capture length */
[37ee856]1409        if (packet->which_trace_start != packet->trace->startcount) {
1410                return ~0U;
1411        }
[d439067]1412        if (packet->cached.capture_length == -1) {
[7c72e4d]1413                if (!packet->trace->format->get_capture_length)
1414                        return ~0U;
[9bc4689]1415                /* Cast away constness because this is "just" a cache */
[d439067]1416                ((libtrace_packet_t*)packet)->cached.capture_length =
[7c72e4d]1417                        packet->trace->format->get_capture_length(packet);
[2c060e3]1418        }
[7c72e4d]1419
[d439067]1420        if (!(packet->cached.capture_length < LIBTRACE_PACKET_BUFSIZE)) {
[25a3255]1421                fprintf(stderr, "Capture length is greater than the buffer size in trace_get_capture_length()\n");
1422                return 0;
1423                /* should we be returning ~OU here? */
1424        }
[182d043]1425
[d439067]1426        return packet->cached.capture_length;
[2c060e3]1427}
[9636216]1428
[fba4ca0]1429/* Get the size of the packet as it was seen on the wire.
[850630f]1430 * @param packet        a pointer to a libtrace_packet structure
1431 *
[2c060e3]1432 * @returns the size of the packet as it was on the wire.
1433 * @note Due to the trace being a header capture, or anonymisation this may
1434 * not be the same as the Capture Len.
[9636216]1435 */
[39e141f]1436DLLEXPORT size_t trace_get_wire_length(const libtrace_packet_t *packet){
[9636216]1437
[37ee856]1438        if (packet->which_trace_start != packet->trace->startcount) {
1439                return ~0U;
1440        }
1441
[d439067]1442        if (packet->cached.wire_length == -1) {
[9636216]1443                if (!packet->trace->format->get_wire_length)
[979a84f4]1444                        return ~0U;
[d439067]1445                ((libtrace_packet_t *)packet)->cached.wire_length =
[979a84f4]1446                        packet->trace->format->get_wire_length(packet);
[2c060e3]1447        }
[979a84f4]1448
[d439067]1449        if (!(packet->cached.wire_length < LIBTRACE_PACKET_BUFSIZE)) {
[25a3255]1450                fprintf(stderr, "Wire length is greater than the buffer size in trace_get_wire_length()\n");
1451                return 0;
1452                /* should we be returning ~OU here? */
1453        }
[d439067]1454        return packet->cached.wire_length;
[2c060e3]1455
1456}
1457
[7c8eacf]1458/* Get the length of the capture framing headers.
[9636216]1459 * @param packet        the packet opaque pointer
[7c8eacf]1460 * @returns the size of the packet as it was on the wire.
[9636216]1461 * @note this length corresponds to the difference between the size of a
[7c8eacf]1462 * captured packet in memory, and the captured length of the packet
[9636216]1463 */
[39e141f]1464DLLEXPORT SIMPLE_FUNCTION
[d8f02df]1465size_t trace_get_framing_length(const libtrace_packet_t *packet) {
[37ee856]1466        if (packet->which_trace_start != packet->trace->startcount) {
1467                return ~0U;
1468        }
1469
[d439067]1470        if (packet->cached.framing_length >= 0) {
1471                return packet->cached.framing_length;
[f3e1f19]1472        }
1473
[7c8eacf]1474        if (packet->trace->format->get_framing_length) {
[d439067]1475                ((libtrace_packet_t *)packet)->cached.framing_length =
[f3e1f19]1476                       packet->trace->format->get_framing_length(packet);
[d439067]1477                return packet->cached.framing_length;
[7c8eacf]1478        }
[85a79b0]1479        return ~0U;
[7c8eacf]1480}
1481
1482
[fba4ca0]1483/* Get the type of the link layer
[9636216]1484 * @param packet        a pointer to a libtrace_packet structure
[2c060e3]1485 * @returns libtrace_linktype_t
1486 */
[39e141f]1487DLLEXPORT libtrace_linktype_t trace_get_link_type(const libtrace_packet_t *packet ) {
[979a84f4]1488
[37ee856]1489        if (packet->which_trace_start != packet->trace->startcount) {
1490                return TRACE_TYPE_CONTENT_INVALID;
1491        }
1492
[d439067]1493        if (packet->cached.link_type == 0) {
[979a84f4]1494                if (!packet->trace->format->get_link_type)
[f7bcbfb]1495                        return TRACE_TYPE_UNKNOWN;
[d439067]1496                ((libtrace_packet_t *)packet)->cached.link_type =
[979a84f4]1497                        packet->trace->format->get_link_type(packet);
[2c060e3]1498        }
[979a84f4]1499
[d439067]1500        return packet->cached.link_type;
[2c060e3]1501}
1502
[fba4ca0]1503/* process a libtrace event
[ef36351d]1504 * @param trace the libtrace opaque pointer
1505 * @param packet the libtrace_packet opaque pointer
[2c060e3]1506 * @returns
[68667ee]1507 *  TRACE_EVENT_IOWAIT  Waiting on I/O on fd
1508 *  TRACE_EVENT_SLEEP   Next event in seconds
1509 *  TRACE_EVENT_PACKET  Packet arrived in buffer with size size
[7bb7dda]1510 *  TRACE_EVENT_TERMINATE Trace terminated (perhaps with an error condition)
[2137b49]1511 * FIXME currently keeps a copy of the packet inside the trace pointer,
1512 * which in turn is stored inside the new packet object...
[2c060e3]1513 */
[9636216]1514DLLEXPORT libtrace_eventobj_t trace_event(libtrace_t *trace,
[e6d963c]1515                libtrace_packet_t *packet) {
1516        libtrace_eventobj_t event = {TRACE_EVENT_IOWAIT,0,0.0,0};
[4fd12ac]1517
1518        if (!trace) {
[25a3255]1519                fprintf(stderr, "NULL trace passed into trace_event()");
[f6f3ae5]1520                /* Return default event on error? */
1521                return event;
[4fd12ac]1522        }
[2725318]1523        if (!packet) {
[25a3255]1524                trace_set_err(trace, TRACE_ERR_NULL_PACKET, "NULL packet passed into trace_event()");
[f6f3ae5]1525                /* Return default event on error? */
1526                return event;
[4fd12ac]1527        }
1528
[7718e54]1529        /* Free the last packet */
1530        trace_fin_packet(packet);
[4fd12ac]1531        /* Store the trace we are reading from into the packet opaque
1532         * structure */
1533        packet->trace = trace;
1534
[72bfe20]1535        if (packet->trace->format->trace_event) {
[c70f59f]1536                /* Note: incrementing accepted, filtered etc. packet
[9636216]1537                 * counters is handled by the format-specific
[c70f59f]1538                 * function so don't increment them here.
1539                 */
[9d3c721]1540                event=packet->trace->format->trace_event(trace,packet);
1541                }
1542        return event;
[72bfe20]1543
[2c060e3]1544}
[c681e86]1545
[3a14f3b]1546/** Setup a BPF filter based on pre-compiled byte-code.
1547 * @param bf_insns      A pointer to the start of the byte-code
1548 * @param bf_len        The number of BPF instructions
1549 * @returns             an opaque pointer to a libtrace_filter_t object
1550 * @note                The supplied byte-code is not checked for correctness.
1551 * @author              Scott Raynel
1552 */
1553DLLEXPORT libtrace_filter_t *
1554trace_create_filter_from_bytecode(void *bf_insns, unsigned int bf_len)
1555{
[d47ca18]1556#ifndef HAVE_BPF
[3a14f3b]1557        fprintf(stderr, "This version of libtrace does not have BPF support\n");
1558        return NULL;
1559#else
1560        struct libtrace_filter_t *filter = (struct libtrace_filter_t *)
1561                malloc(sizeof(struct libtrace_filter_t));
1562        filter->filter.bf_insns = (struct bpf_insn *)
1563                malloc(sizeof(struct bpf_insn) * bf_len);
[9636216]1564
[3a14f3b]1565        memcpy(filter->filter.bf_insns, bf_insns,
1566                        bf_len * sizeof(struct bpf_insn));
[9636216]1567
[3a14f3b]1568        filter->filter.bf_len = bf_len;
1569        filter->filterstring = NULL;
[d48008d]1570        filter->jitfilter = NULL;
[3a14f3b]1571        /* "flag" indicates that the filter member is valid */
[9636216]1572        filter->flag = 1;
1573
[3a14f3b]1574        return filter;
1575#endif
1576}
1577
[eda2def]1578/* Create a BPF filter
[c681e86]1579 * @param filterstring a char * containing the bpf filter string
[c5a22ec]1580 * @returns opaque pointer pointer to a libtrace_filter_t object
[c681e86]1581 */
[e2aebe7]1582DLLEXPORT libtrace_filter_t *trace_create_filter(const char *filterstring) {
[d47ca18]1583#ifdef HAVE_BPF
[e6d963c]1584        libtrace_filter_t *filter = (libtrace_filter_t*)
1585                                malloc(sizeof(libtrace_filter_t));
[c5a22ec]1586        filter->filterstring = strdup(filterstring);
[d48008d]1587        filter->jitfilter = NULL;
[df338b3]1588        filter->flag = 0;
[c5a22ec]1589        return filter;
[9580a74]1590#else
1591        fprintf(stderr,"This version of libtrace does not have bpf filter support\n");
[516a90d]1592        return NULL;
[9580a74]1593#endif
[c681e86]1594}
1595
[e2aebe7]1596DLLEXPORT void trace_destroy_filter(libtrace_filter_t *filter)
[747c501]1597{
[d47ca18]1598#ifdef HAVE_BPF
[747c501]1599        free(filter->filterstring);
1600        if (filter->flag)
1601                pcap_freecode(&filter->filter);
[d48008d]1602#ifdef HAVE_LLVM
[9636216]1603        if (filter->jitfilter)
[d48008d]1604                destroy_program(filter->jitfilter);
1605#endif
[747c501]1606        free(filter);
[516a90d]1607#else
1608
1609#endif
[747c501]1610}
1611
[eda2def]1612/* Compile a bpf filter, now we know the link type for the trace that we're
1613 * applying it to.
1614 *
[df338b3]1615 * @internal
1616 *
1617 * @returns -1 on error, 0 on success
1618 */
[0bb3004]1619static int trace_bpf_compile(libtrace_filter_t *filter,
[c0506ea]1620                const libtrace_packet_t *packet,
[9636216]1621                void *linkptr,
[c0506ea]1622                libtrace_linktype_t linktype    ) {
[d47ca18]1623#ifdef HAVE_BPF
[29bbef0]1624        /* It just so happens that the underlying libs used by pthread arn't
1625         * thread safe, namely lex/flex thingys, so single threaded compile
1626         * multi threaded running should be safe.
1627         */
1628        static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
[33b735c]1629
1630        if (!packet) {
[25a3255]1631                fprintf(stderr, "NULL packet passed into trace_bpf_compile()");
[33b735c]1632                return TRACE_ERR_NULL_PACKET;
1633        }
1634
[2725318]1635        if (!filter) {
1636                trace_set_err(packet->trace,
1637                                TRACE_ERR_NULL_FILTER, "Filter is NULL trace_bpf_compile()");
1638                return -1;
1639        }
[df338b3]1640
1641        /* If this isn't a real packet, then fail */
1642        if (!linkptr) {
[0ea3526]1643                trace_set_err(packet->trace,
[e90d5a8]1644                                TRACE_ERR_BAD_FILTER,"Packet has no payload");
[df338b3]1645                return -1;
1646        }
[9636216]1647
[df338b3]1648        if (filter->filterstring && ! filter->flag) {
[d242ea8]1649                pcap_t *pcap = NULL;
[d5879cc]1650                if (linktype==(libtrace_linktype_t)-1) {
[302714f]1651                        trace_set_err(packet->trace,
[e90d5a8]1652                                        TRACE_ERR_BAD_FILTER,
[d5879cc]1653                                        "Packet has an unknown linktype");
1654                        return -1;
1655                }
[f7bcbfb]1656                if (libtrace_to_pcap_dlt(linktype) == TRACE_DLT_ERROR) {
[e90d5a8]1657                        trace_set_err(packet->trace,TRACE_ERR_BAD_FILTER,
[d242ea8]1658                                        "Unknown pcap equivalent linktype");
[d5879cc]1659                        return -1;
1660                }
[33b735c]1661                pthread_mutex_lock(&mutex);
[29bbef0]1662                /* Make sure not one bet us to this */
1663                if (filter->flag) {
[33b735c]1664                        pthread_mutex_unlock(&mutex);
[2725318]1665                        return -1;
[29bbef0]1666                }
[df338b3]1667                pcap=(pcap_t *)pcap_open_dead(
[4bd8a5b]1668                                (int)libtrace_to_pcap_dlt(linktype),
1669                                1500U);
[df338b3]1670                /* build filter */
[2725318]1671                if (!pcap) {
1672                        trace_set_err(packet->trace, TRACE_ERR_BAD_FILTER,
1673                                                "Unable to open pcap_t for compiling filters trace_bpf_compile()");
1674                        return -1;
1675                }
[9636216]1676                if (pcap_compile( pcap, &filter->filter, filter->filterstring,
[df338b3]1677                                        1, 0)) {
[e90d5a8]1678                        trace_set_err(packet->trace,TRACE_ERR_BAD_FILTER,
[9636216]1679                                        "Unable to compile the filter \"%s\": %s",
[d242ea8]1680                                        filter->filterstring,
1681                                        pcap_geterr(pcap));
1682                        pcap_close(pcap);
[33b735c]1683                        pthread_mutex_unlock(&mutex);
[df338b3]1684                        return -1;
1685                }
1686                pcap_close(pcap);
1687                filter->flag=1;
[33b735c]1688                pthread_mutex_unlock(&mutex);
[df338b3]1689        }
1690        return 0;
1691#else
[abc8e94]1692        trace_set_err(packet->trace,TRACE_ERR_OPTION_UNAVAIL,
1693                                "Feature unavailable");
1694        return -1;
[df338b3]1695#endif
1696}
1697
[e2aebe7]1698DLLEXPORT int trace_apply_filter(libtrace_filter_t *filter,
[e6d963c]1699                        const libtrace_packet_t *packet) {
[d47ca18]1700#ifdef HAVE_BPF
[2137b49]1701        void *linkptr = 0;
[9bc4689]1702        uint32_t clen = 0;
[76eeca0]1703        bool free_packet_needed = false;
1704        int ret;
[c0506ea]1705        libtrace_linktype_t linktype;
[778d459]1706        libtrace_packet_t *packet_copy = (libtrace_packet_t*)packet;
[29bbef0]1707#ifdef HAVE_LLVM
1708        static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1709#endif
[33d83d4]1710
[2725318]1711        if (!packet) {
[25a3255]1712                fprintf(stderr, "NULL packet passed into trace_apply_filter()\n");
[33b735c]1713                return TRACE_ERR_NULL_PACKET;
[2725318]1714        }
1715        if (!filter) {
1716                trace_set_err(packet->trace, TRACE_ERR_NULL_FILTER,
[25a3255]1717                        "NULL filter passed into trace_apply_filter()");
[2725318]1718                return -1;
1719        }
[1352066]1720
[b6664e8]1721        /* Match all non-data packets as we probably want them to pass
1722         * through to the caller */
[c0506ea]1723        linktype = trace_get_link_type(packet);
1724
[9a6bdbc]1725        if (linktype == TRACE_TYPE_NONDATA || linktype == TRACE_TYPE_ERF_META
1726                || linktype == TRACE_TYPE_PCAPNG_META)
[9636216]1727                return 1;
[b6664e8]1728
[f7bcbfb]1729        if (libtrace_to_pcap_dlt(linktype)==TRACE_DLT_ERROR) {
[9636216]1730
[eda2def]1731                /* If we cannot get a suitable DLT for the packet, it may
1732                 * be because the packet is encapsulated in a link type that
1733                 * does not correspond to a DLT. Therefore, we should try
1734                 * popping off headers until we either can find a suitable
1735                 * link type or we can't do any more sensible decapsulation. */
[9636216]1736
[1352066]1737                /* Copy the packet, as we don't want to trash the one we
[eda2def]1738                 * were passed in */
[7ca2c7d]1739                packet_copy=trace_copy_packet(packet);
[76eeca0]1740                free_packet_needed=true;
[eda2def]1741
[f7bcbfb]1742                while (libtrace_to_pcap_dlt(linktype) == TRACE_DLT_ERROR) {
[7ca2c7d]1743                        if (!demote_packet(packet_copy)) {
[9636216]1744                                trace_set_err(packet->trace,
[1352066]1745                                                TRACE_ERR_NO_CONVERSION,
[e732393]1746                                                "pcap does not support this linktype so cannot apply BPF filters");
[76eeca0]1747                                if (free_packet_needed) {
[7ca2c7d]1748                                        trace_destroy_packet(packet_copy);
[76eeca0]1749                                }
[1352066]1750                                return -1;
1751                        }
[c0506ea]1752                        linktype = trace_get_link_type(packet_copy);
[1352066]1753                }
[c0506ea]1754
[1352066]1755        }
[9636216]1756
[9bc4689]1757        linkptr = trace_get_packet_buffer(packet_copy,NULL,&clen);
[76eeca0]1758        if (!linkptr) {
1759                if (free_packet_needed) {
[7ca2c7d]1760                        trace_destroy_packet(packet_copy);
[76eeca0]1761                }
1762                return 0;
1763        }
1764
[9636216]1765        /* We need to compile the filter now, because before we didn't know
[eda2def]1766         * what the link type was
[76eeca0]1767         */
[29bbef0]1768        // Note internal mutex locking used here
[c0506ea]1769        if (trace_bpf_compile(filter,packet_copy,linkptr,linktype)==-1) {
[76eeca0]1770                if (free_packet_needed) {
[7ca2c7d]1771                        trace_destroy_packet(packet_copy);
[76eeca0]1772                }
1773                return -1;
1774        }
1775
[d48008d]1776        /* If we're jitting, we may need to JIT the BPF code now too */
1777#if HAVE_LLVM
1778        if (!filter->jitfilter) {
[a49a9eb]1779                ASSERT_RET(pthread_mutex_lock(&mutex), == 0);
[29bbef0]1780                /* Again double check here like the bpf filter */
[d3849c7]1781                if(!filter->jitfilter)
[29bbef0]1782                /* Looking at compile_program source this appears to be thread safe
1783                 * however if this gets called twice we will leak this memory :(
1784                 * as such lock here anyways */
1785                        filter->jitfilter = compile_program(filter->filter.bf_insns, filter->filter.bf_len);
[a49a9eb]1786                ASSERT_RET(pthread_mutex_unlock(&mutex), == 0);
[d48008d]1787        }
1788#endif
1789
[2725318]1790        if (!filter->flag) {
1791                trace_set_err(packet->trace, TRACE_ERR_BAD_FILTER,
[25a3255]1792                        "Bad filter passed into trace_apply_filter()");
[2725318]1793                return -1;
1794        }
[d48008d]1795        /* Now execute the filter */
1796#if HAVE_LLVM
1797        ret=filter->jitfilter->bpf_run((unsigned char *)linkptr, clen);
1798#else
[9bc4689]1799        ret=bpf_filter(filter->filter.bf_insns,(u_char*)linkptr,(unsigned int)clen,(unsigned int)clen);
[d48008d]1800#endif
[eda2def]1801
1802        /* If we copied the packet earlier, make sure that we free it */
[76eeca0]1803        if (free_packet_needed) {
[7ca2c7d]1804                trace_destroy_packet(packet_copy);
[76eeca0]1805        }
1806        return ret;
[9580a74]1807#else
1808        fprintf(stderr,"This version of libtrace does not have bpf filter support\n");
1809        return 0;
1810#endif
[c681e86]1811}
1812
[fba4ca0]1813/* Set the direction flag, if it has one
[80a2e99]1814 * @param packet the packet opaque pointer
1815 * @param direction the new direction (0,1,2,3)
1816 * @returns a signed value containing the direction flag, or -1 if this is not supported
1817 */
[9636216]1818DLLEXPORT libtrace_direction_t trace_set_direction(libtrace_packet_t *packet,
1819                libtrace_direction_t direction)
[c37ab9f]1820{
[2725318]1821        if (!packet) {
[25a3255]1822                fprintf(stderr, "NULL packet passed into trace_set_direction()\n");
1823                return (libtrace_direction_t)~0U;
[2725318]1824        }
[4dedc28]1825        if (packet->trace->format->set_direction) {
1826                return packet->trace->format->set_direction(packet,direction);
[80a2e99]1827        }
[85a79b0]1828        return (libtrace_direction_t)~0U;
[80a2e99]1829}
1830
[fba4ca0]1831/* Get the direction flag, if it has one
[850630f]1832 * @param packet a pointer to a libtrace_packet structure
[14a9d05]1833 * @returns a signed value containing the direction flag, or -1 if this is not supported
[9075d49]1834 * The direction is defined as 0 for packets originating locally (ie, outbound)
1835 * and 1 for packets originating remotely (ie, inbound).
1836 * Other values are possible, which might be overloaded to mean special things
1837 * for a special trace.
[14a9d05]1838 */
[9636216]1839DLLEXPORT libtrace_direction_t trace_get_direction(const libtrace_packet_t *packet)
[c37ab9f]1840{
[2725318]1841        if (!packet) {
[25a3255]1842                fprintf(stderr, "NULL packet passed into trace_set_direction()\n");
[2725318]1843                return (libtrace_direction_t)~0U;
1844        }
[37ee856]1845        if (packet->which_trace_start != packet->trace->startcount) {
1846                return (libtrace_direction_t)~0U;
1847        }
[4dedc28]1848        if (packet->trace->format->get_direction) {
1849                return packet->trace->format->get_direction(packet);
[14a9d05]1850        }
[85a79b0]1851        return (libtrace_direction_t)~0U;
[14a9d05]1852}
[c681e86]1853
[5c25d3a]1854#define ROOT_SERVER(x) ((x) < 512)
1855#define ROOT_CLIENT(x) ((512 <= (x)) && ((x) < 1024))
1856#define NONROOT_SERVER(x) ((x) >= 5000)
1857#define NONROOT_CLIENT(x) ((1024 <= (x)) && ((x) < 5000))
1858#define DYNAMIC(x) ((49152 < (x)) && ((x) < 65535))
[83e0a25]1859#define SERVER(x) ROOT_SERVER(x) || NONROOT_SERVER(x)
[9636216]1860#define CLIENT(x) ROOT_CLIENT(x) || NONROOT_CLIENT(x)
[83e0a25]1861
[d66fbf0]1862/* Attempt to deduce the 'server' port
[1cea750]1863 * @param protocol the IP protocol (eg, 6 or 17 for TCP or UDP)
1864 * @param source the TCP or UDP source port
1865 * @param dest the TCP or UDP destination port
1866 * @returns a hint as to which port is the server port
1867 */
[9636216]1868DLLEXPORT int8_t trace_get_server_port(UNUSED uint8_t protocol,
1869                uint16_t source, uint16_t dest)
[85a79b0]1870{
[83e0a25]1871        /*
1872         * * If the ports are equal, return DEST
1873         * * Check for well-known ports in the given protocol
1874         * * Root server ports: 0 - 511
1875         * * Root client ports: 512 - 1023
1876         * * non-root client ports: 1024 - 4999
1877         * * non-root server ports: 5000+
1878         * * Check for static ranges: 1024 - 49151
1879         * * Check for dynamic ranges: 49152 - 65535
1880         * * flip a coin.
1881         */
[9636216]1882
[b007afc]1883        /* equal */
[9c6aa95]1884        if (source == dest)
[b007afc]1885                return USE_DEST;
1886
1887        /* root server port, 0 - 511 */
[5c25d3a]1888        if (ROOT_SERVER(source) && ROOT_SERVER(dest)) {
1889                if (source < dest)
1890                        return USE_SOURCE;
[b007afc]1891                return USE_DEST;
[5c25d3a]1892        }
[b007afc]1893
1894        if (ROOT_SERVER(source) && !ROOT_SERVER(dest))
[83e0a25]1895                return USE_SOURCE;
[b007afc]1896        if (!ROOT_SERVER(source) && ROOT_SERVER(dest))
1897                return USE_DEST;
1898
1899        /* non-root server */
[5c25d3a]1900        if (NONROOT_SERVER(source) && NONROOT_SERVER(dest)) {
1901                if (source < dest)
1902                        return USE_SOURCE;
[83e0a25]1903                return USE_DEST;
[5c25d3a]1904        }
[b007afc]1905        if (NONROOT_SERVER(source) && !NONROOT_SERVER(dest))
[83e0a25]1906                return USE_SOURCE;
[b007afc]1907        if (!NONROOT_SERVER(source) && NONROOT_SERVER(dest))
[83e0a25]1908                return USE_DEST;
[b007afc]1909
1910        /* root client */
[5c25d3a]1911        if (ROOT_CLIENT(source) && ROOT_CLIENT(dest)) {
1912                if (source < dest)
1913                        return USE_SOURCE;
[b007afc]1914                return USE_DEST;
[5c25d3a]1915        }
[f0a2f98]1916        if (ROOT_CLIENT(source) && !ROOT_CLIENT(dest)) {
1917                /* prefer root-client over nonroot-client */
1918                if (NONROOT_CLIENT(dest))
1919                        return USE_SOURCE;
[b007afc]1920                return USE_DEST;
[f0a2f98]1921        }
1922        if (!ROOT_CLIENT(source) && ROOT_CLIENT(dest)) {
1923                /* prefer root-client over nonroot-client */
1924                if (NONROOT_CLIENT(source))
1925                        return USE_DEST;
[b007afc]1926                return USE_SOURCE;
[f0a2f98]1927        }
[9636216]1928
[b007afc]1929        /* nonroot client */
[5c25d3a]1930        if (NONROOT_CLIENT(source) && NONROOT_CLIENT(dest)) {
[9636216]1931                if (source < dest)
[5c25d3a]1932                        return USE_SOURCE;
[b007afc]1933                return USE_DEST;
[5c25d3a]1934        }
[b007afc]1935        if (NONROOT_CLIENT(source) && !NONROOT_CLIENT(dest))
1936                return USE_DEST;
1937        if (!NONROOT_CLIENT(source) && NONROOT_CLIENT(dest))
1938                return USE_SOURCE;
1939
[5c25d3a]1940        /* dynamic range */
[e1868fb]1941        if (DYNAMIC(source) && DYNAMIC(dest)) {
[f9bb6c5]1942                if (source < dest)
1943                        return USE_SOURCE;
[b007afc]1944                return USE_DEST;
[e1868fb]1945        }
[b007afc]1946        if (DYNAMIC(source) && !DYNAMIC(dest))
1947                return USE_DEST;
1948        if (!DYNAMIC(source) && DYNAMIC(dest))
1949                return USE_SOURCE;
1950        /*
[9636216]1951        if (SERVER(source) && CLIENT(dest))
[b007afc]1952                return USE_SOURCE;
[9636216]1953
1954        if (SERVER(dest) && CLIENT(source))
[b007afc]1955                return USE_DEST;
[9636216]1956        if (ROOT_SERVER(source) && !ROOT_SERVER(dest))
[b007afc]1957                return USE_SOURCE;
[9636216]1958        if (ROOT_SERVER(dest) && !ROOT_SERVER(source))
[b007afc]1959                return USE_DEST;
1960        */
[7068467]1961        /* failing that test... */
[83e0a25]1962        if (source < dest) {
1963                return USE_SOURCE;
[9636216]1964        }
[83e0a25]1965        return USE_DEST;
[9636216]1966
[83e0a25]1967}
[80a2e99]1968
[fba4ca0]1969/* Truncate the packet at the suggested length
[80a2e99]1970 * @param packet        the packet opaque pointer
[850630f]1971 * @param size          the new length of the packet
[fe7b292]1972 * @returns the new size of the packet
[c95b923]1973 * @note size and the return size refer to the network-level payload of the
1974 * packet, and do not include any capture headers. For example, to truncate a
1975 * packet after the IP header, set size to sizeof(ethernet_header) +
1976 * sizeof(ip_header)
1977 * @note If the original network-level payload is smaller than size, then the
1978 * original size is returned and the packet is left unchanged.
[80a2e99]1979 */
[e6d963c]1980DLLEXPORT size_t trace_set_capture_length(libtrace_packet_t *packet, size_t size) {
[33b735c]1981        if (!packet) {
[25a3255]1982                fprintf(stderr, "NULL packet passed into trace_set_capture_length()\n");
[33b735c]1983                return ~0U;
1984        }
[80a2e99]1985
[c95b923]1986        if (packet->trace->format->set_capture_length) {
[d439067]1987                packet->cached.capture_length = packet->trace->format->set_capture_length(packet,size);
1988                return packet->cached.capture_length;
[80a2e99]1989        }
[fe7b292]1990
[85a79b0]1991        return ~0U;
[80a2e99]1992}
1993
[eda2def]1994/* Splits a URI into two components - the format component which is seen before
1995 * the ':', and the uridata which follows the ':'.
1996 *
1997 * Returns a pointer to the URI data, but updates the format parameter to
[9636216]1998 * point to a copy of the format component.
[eda2def]1999 */
2000
[39e141f]2001DLLEXPORT const char * trace_parse_uri(const char *uri, char **format) {
[6f75b9d]2002        const char *uridata = 0;
[9636216]2003
[de8c420b]2004        if((uridata = strchr(uri,':')) == NULL) {
[eda2def]2005                /* Badly formed URI - needs a : */
[de8c420b]2006                return 0;
2007        }
2008
[36d51c8]2009        if ((unsigned)(uridata - uri) > URI_PROTO_LINE) {
[eda2def]2010                /* Badly formed URI - uri type is too long */
[de8c420b]2011                return 0;
2012        }
2013
[eda2def]2014        /* NOTE: this is allocated memory - it should be freed by the caller
2015         * once they are done with it */
[4bd8a5b]2016        *format=xstrndup(uri, (size_t)(uridata - uri));
[6f75b9d]2017
[eda2def]2018        /* Push uridata past the delimiter */
[de8c420b]2019        uridata++;
[9636216]2020
[de8c420b]2021        return uridata;
2022}
[6dbc47a]2023
[2725318]2024enum base_format_t trace_get_format(libtrace_packet_t *packet) {
[25a3255]2025        if (!packet) {
2026                fprintf(stderr, "NULL packet passed into trace_get_format()\n");
2027                return TRACE_FORMAT_UNKNOWN;
2028        }
[6dbc47a]2029
2030        return packet->trace->format->type;
2031}
[9636216]2032
[39e141f]2033DLLEXPORT libtrace_err_t trace_get_err(libtrace_t *trace)
[880aa58]2034{
[0ea3526]2035        libtrace_err_t err = trace->err;
2036        trace->err.err_num = 0; /* "OK" */
2037        trace->err.problem[0]='\0';
2038        return err;
2039}
2040
[39e141f]2041DLLEXPORT bool trace_is_err(libtrace_t *trace)
[eeab9832]2042{
2043        return trace->err.err_num != 0;
2044}
2045
[eda2def]2046/* Prints the input error status to standard error and clears the error state */
[39e141f]2047DLLEXPORT void trace_perror(libtrace_t *trace,const char *msg,...)
[eeab9832]2048{
2049        char buf[256];
2050        va_list va;
2051        va_start(va,msg);
2052        vsnprintf(buf,sizeof(buf),msg,va);
2053        va_end(va);
2054        if(trace->err.err_num) {
[670bbe5]2055                if (trace->uridata) {
2056                        fprintf(stderr,"%s(%s): %s\n",
2057                                        buf,trace->uridata,trace->err.problem);
2058                } else {
2059                        fprintf(stderr,"%s: %s\n", buf, trace->err.problem);
2060                }
[eeab9832]2061        } else {
[670bbe5]2062                if (trace->uridata) {
2063                        fprintf(stderr,"%s(%s): No error\n",buf,trace->uridata);
2064                } else {
[b4a17c7]2065                        fprintf(stderr,"%s: No error\n", buf);
[670bbe5]2066                }
[eeab9832]2067        }
[114b8d6]2068        trace->err.err_num = 0; /* "OK" */
2069        trace->err.problem[0]='\0';
[eeab9832]2070}
2071
[39e141f]2072DLLEXPORT libtrace_err_t trace_get_err_output(libtrace_out_t *trace)
[0ea3526]2073{
2074        libtrace_err_t err = trace->err;
[ae66bde]2075        trace->err.err_num = TRACE_ERR_NOERROR; /* "OK" */
[0ea3526]2076        trace->err.problem[0]='\0';
2077        return err;
[880aa58]2078}
[0d57541]2079
[39e141f]2080DLLEXPORT bool trace_is_err_output(libtrace_out_t *trace)
[eeab9832]2081{
2082        return trace->err.err_num != 0;
2083}
2084
[eda2def]2085/* Prints the output error status to standard error and clears the error state
2086 */
[39e141f]2087DLLEXPORT void trace_perror_output(libtrace_out_t *trace,const char *msg,...)
[eeab9832]2088{
2089        char buf[256];
2090        va_list va;
2091        va_start(va,msg);
2092        vsnprintf(buf,sizeof(buf),msg,va);
2093        va_end(va);
2094        if(trace->err.err_num) {
2095                fprintf(stderr,"%s(%s): %s\n",
[61b4543]2096                                buf,
2097                                trace->uridata?trace->uridata:"no uri",
2098                                trace->err.problem);
[eeab9832]2099        } else {
2100                fprintf(stderr,"%s(%s): No error\n",buf,trace->uridata);
2101        }
[e4976e1]2102        trace->err.err_num = TRACE_ERR_NOERROR; /* "OK" */
2103        trace->err.problem[0]='\0';
[eeab9832]2104}
2105
[39e141f]2106DLLEXPORT int trace_seek_erf_timestamp(libtrace_t *trace, uint64_t ts)
[0d57541]2107{
2108        if (trace->format->seek_erf) {
2109                return trace->format->seek_erf(trace,ts);
2110        }
2111        else {
[6c248a9]2112                if (trace->format->seek_timeval) {
2113                        struct timeval tv;
2114#if __BYTE_ORDER == __BIG_ENDIAN
2115                        tv.tv_sec = ts & 0xFFFFFFFF;
[11041eb]2116                        tv.tv_usec = ((ts >> 32) * 1000000) & 0xFFFFFFFF;
[6c248a9]2117#elif __BYTE_ORDER == __LITTLE_ENDIAN
2118                        tv.tv_sec = ts >> 32;
[11041eb]2119                        tv.tv_usec = ((ts&0xFFFFFFFF)*1000000)>>32;
[6c248a9]2120#else
2121#error "What on earth are you running this on?"
2122#endif
2123                        if (tv.tv_usec >= 1000000) {
2124                                tv.tv_usec -= 1000000;
2125                                tv.tv_sec += 1;
2126                        }
2127                        return trace->format->seek_timeval(trace,tv);
2128                }
2129                if (trace->format->seek_seconds) {
[9636216]2130                        double seconds =
[6c248a9]2131                                (ts>>32) + ((ts & UINT_MAX)*1.0 / UINT_MAX);
2132                        return trace->format->seek_seconds(trace,seconds);
2133                }
2134                trace_set_err(trace,
2135                                TRACE_ERR_OPTION_UNAVAIL,
2136                                "Feature unimplemented");
2137                return -1;
2138        }
2139}
2140
[39e141f]2141DLLEXPORT int trace_seek_seconds(libtrace_t *trace, double seconds)
[6c248a9]2142{
2143        if (trace->format->seek_seconds) {
2144                return trace->format->seek_seconds(trace,seconds);
2145        }
2146        else {
2147                if (trace->format->seek_timeval) {
2148                        struct timeval tv;
2149                        tv.tv_sec = (uint32_t)seconds;
2150                        tv.tv_usec = (uint32_t)(((seconds - tv.tv_sec) * 1000000)/UINT_MAX);
2151                        return trace->format->seek_timeval(trace,tv);
2152                }
2153                if (trace->format->seek_erf) {
[9636216]2154                        uint64_t timestamp =
[6c248a9]2155                                ((uint64_t)((uint32_t)seconds) << 32) + \
[67a14d4]2156                            (uint64_t)(( seconds - (uint32_t)seconds   ) * UINT_MAX);
[6c248a9]2157                        return trace->format->seek_erf(trace,timestamp);
2158                }
2159                trace_set_err(trace,
2160                                TRACE_ERR_OPTION_UNAVAIL,
2161                                "Feature unimplemented");
2162                return -1;
2163        }
2164}
2165
[39e141f]2166DLLEXPORT int trace_seek_timeval(libtrace_t *trace, struct timeval tv)
[6c248a9]2167{
2168        if (trace->format->seek_timeval) {
2169                return trace->format->seek_timeval(trace,tv);
2170        }
2171        else {
2172                if (trace->format->seek_erf) {
2173                        uint64_t timestamp = ((((uint64_t)tv.tv_sec) << 32) + \
2174                                (((uint64_t)tv.tv_usec * UINT_MAX)/1000000));
2175                        return trace->format->seek_erf(trace,timestamp);
2176                }
2177                if (trace->format->seek_seconds) {
[e4e1635]2178                        double seconds = tv.tv_sec + ((tv.tv_usec * 1.0)/1000000);
[6c248a9]2179                        return trace->format->seek_seconds(trace,seconds);
2180                }
[0d57541]2181                trace_set_err(trace,
2182                                TRACE_ERR_OPTION_UNAVAIL,
2183                                "Feature unimplemented");
2184                return -1;
2185        }
2186}
2187
[eda2def]2188/* Converts a binary ethernet MAC address into a printable string */
[39e141f]2189DLLEXPORT char *trace_ether_ntoa(const uint8_t *addr, char *buf)
[752709f]2190{
[21ab138]2191        static char staticbuf[18]={0,};
2192        if (!buf)
2193                buf=staticbuf;
2194        snprintf(buf,(size_t)18,"%02x:%02x:%02x:%02x:%02x:%02x",
[752709f]2195                        addr[0],addr[1],addr[2],
2196                        addr[3],addr[4],addr[5]);
[21ab138]2197        return buf;
[752709f]2198}
2199
[eda2def]2200/* Converts a printable ethernet MAC address into a binary format */
[39e141f]2201DLLEXPORT uint8_t *trace_ether_aton(const char *buf, uint8_t *addr)
[752709f]2202{
2203        uint8_t *buf2 = addr;
2204        unsigned int tmp[6];
2205        static uint8_t staticaddr[6];
2206        if (!buf2)
2207                buf2=staticaddr;
2208        sscanf(buf,"%x:%x:%x:%x:%x:%x",
2209                        &tmp[0],&tmp[1],&tmp[2],
2210                        &tmp[3],&tmp[4],&tmp[5]);
2211        buf2[0]=tmp[0]; buf2[1]=tmp[1]; buf2[2]=tmp[2];
2212        buf2[3]=tmp[3]; buf2[4]=tmp[4]; buf2[5]=tmp[5];
2213        return buf2;
2214}
[4af54d1]2215
[eda2def]2216
[9636216]2217/* Creates a libtrace packet from scratch using the contents of the provided
[eda2def]2218 * buffer as the packet payload.
2219 *
2220 * Unlike trace_prepare_packet(), the buffer should not contain any capture
[9636216]2221 * format headers; instead this function will add the PCAP header to the
[eda2def]2222 * packet record. This also means only PCAP packets can be constructed using
2223 * this function.
2224 *
2225 */
[c5ebbaa]2226DLLEXPORT
2227void trace_construct_packet(libtrace_packet_t *packet,
2228                libtrace_linktype_t linktype,
2229                const void *data,
[89cc0cc]2230                uint16_t len) {
2231
[1ed3f1e]2232        if (!packet) {
2233                fprintf(stderr, "NULL packet passed into trace_contruct_packet()\n");
2234                return;
2235        }
2236        /* Check a valid linktype was supplied */
2237        if (linktype == TRACE_TYPE_UNKNOWN || linktype == TRACE_TYPE_CONTENT_INVALID) {
2238                fprintf(stderr, "Unknown or invalid linktype passed into trace_construct_packet()\n");
2239                return;
2240        }
2241
[a7d1914]2242        size_t size;
[0a62bba]2243        static libtrace_t *deadtrace=NULL;
[c5ebbaa]2244        libtrace_pcapfile_pkt_hdr_t hdr;
[6ed6c4a]2245#ifdef WIN32
2246        struct _timeb tstruct;
2247#else
[c5ebbaa]2248        struct timeval tv;
[6ed6c4a]2249#endif
2250
[eda2def]2251        /* We need a trace to attach the constructed packet to (and it needs
2252         * to be PCAP) */
[9636216]2253        if (NULL == deadtrace)
[6ed6c4a]2254                deadtrace=trace_create_dead("pcapfile");
2255
[eda2def]2256        /* Fill in the new PCAP header */
[6ed6c4a]2257#ifdef WIN32
2258        _ftime(&tstruct);
2259        hdr.ts_sec=tstruct.time;
2260        hdr.ts_usec=tstruct.millitm * 1000;
2261#else
[c5ebbaa]2262        gettimeofday(&tv,NULL);
2263        hdr.ts_sec=tv.tv_sec;
2264        hdr.ts_usec=tv.tv_usec;
[6ed6c4a]2265#endif
2266
[c5ebbaa]2267        hdr.caplen=len;
2268        hdr.wirelen=len;
2269
[eda2def]2270        /* Now fill in the libtrace packet itself */
[89cc0cc]2271        if (!deadtrace) {
[2193905]2272                fprintf(stderr, "Unable to create dummy trace for use within trace_construct_packet()\n");
[f6f3ae5]2273                return;
[89cc0cc]2274        }
[c5ebbaa]2275        packet->trace=deadtrace;
[a7d1914]2276        size=len+sizeof(hdr);
[7d1ada1]2277        if (size < LIBTRACE_PACKET_BUFSIZE)
2278            size = LIBTRACE_PACKET_BUFSIZE;
[c5ebbaa]2279        if (packet->buf_control==TRACE_CTRL_PACKET) {
[7d1ada1]2280            packet->buffer = realloc(packet->buffer, size);
[c5ebbaa]2281        }
2282        else {
[7d1ada1]2283                packet->buffer = malloc(size);
[c5ebbaa]2284        }
2285        packet->buf_control=TRACE_CTRL_PACKET;
2286        packet->header=packet->buffer;
2287        packet->payload=(void*)((char*)packet->buffer+sizeof(hdr));
[7d1ada1]2288
2289        /* Ugh, memmove - sadly necessary, also beware that we might be
2290         * moving data around within this packet, so ordering is important.
2291         */
[4161a69]2292        if (data != NULL) {
2293                memmove(packet->payload, data, (size_t)len);
2294        } else {
2295                packet->payload = NULL;
2296        }
[7d1ada1]2297        memmove(packet->header, &hdr, sizeof(hdr));
[73dd29f]2298        packet->type=pcap_linktype_to_rt(libtrace_to_pcap_linktype(linktype));
[eff6d8c]2299
2300        trace_clear_cache(packet);
[c5ebbaa]2301}
[f2fae49]2302
[f0fb38f]2303
[50bbce8]2304uint64_t trace_get_received_packets(libtrace_t *trace)
[f2fae49]2305{
[be3f75b]2306        uint64_t ret;
2307
[25a3255]2308        if (!trace) {
2309                fprintf(stderr, "NULL trace passed to trace_get_received_packets()\n");
2310                /* When the number of received packets is not known we return UINT64_MAX */
2311                return UINT64_MAX;
2312        }
2313
[f2fae49]2314        if (trace->format->get_received_packets) {
[be3f75b]2315                if ((ret = trace->format->get_received_packets(trace)) != UINT64_MAX)
2316                        return ret;
[5ab626a]2317        } else if (trace->format->get_statistics) {
2318                struct libtrace_stat_t stat;
2319                stat.magic = LIBTRACE_STAT_MAGIC;
2320                trace_get_statistics(trace, &stat);
2321                if (stat.received_valid)
2322                        return stat.received;
2323        }
2324
2325        // Read the cached value taken before the trace was paused/closed
2326        if(trace->stats && trace->stats->received_valid)
2327                return trace->stats->received;
2328        else
2329                return UINT64_MAX;
[f2fae49]2330}
2331
[50bbce8]2332uint64_t trace_get_filtered_packets(libtrace_t *trace)
[f2fae49]2333{
[25a3255]2334        if (!trace) {
2335                fprintf(stderr, "NULL trace passed to trace_get_filtered_packets()\n");
2336                return UINT64_MAX;
2337        }
[368a1ae]2338        int i = 0;
[5ab626a]2339        uint64_t lib_filtered = trace->filtered_packets;
[368a1ae]2340        for (i = 0; i < trace->perpkt_thread_count; i++) {
[5ab626a]2341                lib_filtered += trace->perpkt_threads[i].filtered_packets;
[368a1ae]2342        }
[f2fae49]2343        if (trace->format->get_filtered_packets) {
[5ab626a]2344                uint64_t trace_filtered = trace->format->get_filtered_packets(trace);
2345                if (trace_filtered == UINT64_MAX)
2346                        return UINT64_MAX;
2347                else
2348                        return trace_filtered + lib_filtered;
2349        } else if (trace->format->get_statistics) {
2350                struct libtrace_stat_t stat;
2351                stat.magic = LIBTRACE_STAT_MAGIC;
2352                trace_get_statistics(trace, &stat);
2353                if (stat.filtered_valid)
2354                        return lib_filtered + stat.filtered;
2355                else
2356                        return UINT64_MAX;
[f2fae49]2357        }
[5ab626a]2358
2359        // Read the cached value taken before the trace was paused/closed
2360        if(trace->stats && trace->stats->filtered_valid)
2361                return trace->stats->filtered + lib_filtered;
2362        else
2363                return lib_filtered;
[f2fae49]2364}
2365
[50bbce8]2366uint64_t trace_get_dropped_packets(libtrace_t *trace)
[f2fae49]2367{
[25a3255]2368        if (!trace) {
2369                fprintf(stderr, "NULL trace passed into trace_get_dropped_packets()\n");
2370                return UINT64_MAX;
2371        }
[be3f75b]2372        uint64_t ret;
2373
[f2fae49]2374        if (trace->format->get_dropped_packets) {
[be3f75b]2375                if ((ret = trace->format->get_dropped_packets(trace)) != UINT64_MAX)
2376                        return ret;
[5ab626a]2377        } else if (trace->format->get_statistics) {
2378                struct libtrace_stat_t stat;
2379                stat.magic = LIBTRACE_STAT_MAGIC;
2380                trace_get_statistics(trace, &stat);
2381                if (stat.dropped_valid)
2382                        return stat.dropped;
2383        }
2384
2385        // Read the cached value taken before the trace was paused/closed
2386        if(trace->stats && trace->stats->dropped_valid)
2387                return trace->stats->dropped;
2388        else
2389                return UINT64_MAX;
[f2fae49]2390}
2391
[50bbce8]2392uint64_t trace_get_accepted_packets(libtrace_t *trace)
[f2fae49]2393{
[25a3255]2394        if (!trace) {
2395                fprintf(stderr, "NULL trace passed into trace_get_accepted_packets()\n");
2396                return UINT64_MAX;
2397        }
[be3f75b]2398        int i = 0;
[526d9d0]2399        uint64_t ret = 0;
2400        /* We always add to a thread's accepted count before dispatching the
2401         * packet to the user. However if the underlying trace is single
2402         * threaded it will also be increasing the global count. So if we
2403         * find perpkt ignore the global count.
2404         */
[be3f75b]2405        for (i = 0; i < trace->perpkt_thread_count; i++) {
2406                ret += trace->perpkt_threads[i].accepted_packets;
2407        }
[526d9d0]2408        return ret ? ret : trace->accepted_packets;
[f2fae49]2409}
2410
[5ab626a]2411libtrace_stat_t *trace_get_statistics(libtrace_t *trace, libtrace_stat_t *stat)
2412{
[5478d3d]2413        uint64_t ret = 0;
[5ab626a]2414        int i;
[25a3255]2415        if (!trace) {
2416                fprintf(stderr, "NULL trace passed into trace_get_statistics()\n");
2417                return NULL;
2418        }
[5ab626a]2419        if (stat == NULL) {
2420                if (trace->stats == NULL)
2421                        trace->stats = trace_create_statistics();
2422                stat = trace->stats;
2423        }
[2193905]2424        if (stat->magic != LIBTRACE_STAT_MAGIC) {
2425                trace_set_err(trace, TRACE_ERR_STAT, "Use trace_create_statistics() to allocate "
2426                        "statistics prior to calling trace_get_statistics()");
[25a3255]2427                return NULL;
2428        }
[694823f]2429
2430        /* If the trace has paused or finished get the cached results */
[5ab626a]2431        if (trace->state == STATE_PAUSED ||
[ac65c9f]2432            trace->state == STATE_FINISHED ||
2433            trace->state == STATE_FINISHING ||
[5ab626a]2434            trace->state == STATE_JOINED) {
2435                if (trace->stats && trace->stats != stat)
2436                        *stat = *trace->stats;
2437                return stat;
[694823f]2438        }
2439
2440        stat->reserved1 = 0;
2441        stat->reserved2 = 0;
2442#define X(x) stat->x ##_valid = 0;
2443        LIBTRACE_STAT_FIELDS;
2444#undef X
[526d9d0]2445        /* Both accepted and filtered are stored against in the library */
[5478d3d]2446
2447        /* We always add to a thread's accepted count before dispatching the
2448         * packet to the user. However if the underlying trace is single
2449         * threaded it will also be increasing the global count. So if we
2450         * find perpkt ignore the global count.
2451         */
2452        for (i = 0; i < trace->perpkt_thread_count; i++) {
2453                ret += trace->perpkt_threads[i].accepted_packets;
[5ab626a]2454        }
[526d9d0]2455
[5478d3d]2456        stat->accepted_valid = 1;
2457        stat->accepted = ret ? ret : trace->accepted_packets;
2458
[526d9d0]2459        stat->filtered_valid = 1;
2460        stat->filtered = trace->filtered_packets;
2461        for (i = 0; i < trace->perpkt_thread_count; i++) {
2462                stat->filtered += trace->perpkt_threads[i].filtered_packets;
[5ab626a]2463        }
[526d9d0]2464
2465        if (trace->format->get_statistics) {
2466                trace->format->get_statistics(trace, stat);
[5ab626a]2467        }
2468        return stat;
2469}
2470
2471void trace_get_thread_statistics(libtrace_t *trace, libtrace_thread_t *t,
2472                                 libtrace_stat_t *stat)
2473{
[89cc0cc]2474        if (!trace) {
[25a3255]2475                fprintf(stderr, "NULL trace passed into trace_get_thread_statistics()\n");
[f6f3ae5]2476                return;
[89cc0cc]2477        }
2478        if (!stat) {
[2193905]2479                trace_set_err(trace, TRACE_ERR_STAT, "NULL statistics structure passed into "
2480                        "trace_get_thread_statistics()");
[f6f3ae5]2481                return;
[89cc0cc]2482        }
[2193905]2483        if (stat->magic != LIBTRACE_STAT_MAGIC) {
2484                trace_set_err(trace, TRACE_ERR_STAT, "Use trace_create_statistics() to "
2485                        "allocate statistics prior to calling trace_get_thread_statistics()");
[f6f3ae5]2486                return;
[89cc0cc]2487        }
[5ab626a]2488        stat->reserved1 = 0;
2489        stat->reserved2 = 0;
2490#define X(x) stat->x ##_valid= 0;
2491        LIBTRACE_STAT_FIELDS;
2492#undef X
[526d9d0]2493        stat->accepted_valid = 1;
2494        stat->accepted = t->accepted_packets;
2495        stat->filtered_valid = 1;
2496        stat->filtered = t->filtered_packets;
2497        if (!trace_has_dedicated_hasher(trace) && trace->format->get_thread_statistics) {
[5ab626a]2498                trace->format->get_thread_statistics(trace, t, stat);
2499        }
2500}
2501
2502libtrace_stat_t *trace_create_statistics(void) {
2503        libtrace_stat_t *ret;
2504        ret = malloc(sizeof(libtrace_stat_t));
2505        if (ret) {
2506                memset(ret, 0, sizeof(libtrace_stat_t));
2507                ret->magic = LIBTRACE_STAT_MAGIC;
2508        }
2509        return ret;
2510}
2511
[0cdd231]2512void trace_clear_statistics(libtrace_stat_t *s) {
2513        memset(s, 0, sizeof(libtrace_stat_t));
2514        s->magic = LIBTRACE_STAT_MAGIC;
2515}
2516
[5ab626a]2517void trace_subtract_statistics(const libtrace_stat_t *a, const libtrace_stat_t *b,
2518                         libtrace_stat_t *c) {
[89cc0cc]2519
[2193905]2520        if (a->magic != LIBTRACE_STAT_MAGIC
2521                || b->magic != LIBTRACE_STAT_MAGIC
2522                || c->magic != LIBTRACE_STAT_MAGIC) {
2523                fprintf(stderr, "Use trace_create_statistics() to allocate statistics prior to "
2524                        "calling trace_subtract_statistics()\n");
[f6f3ae5]2525                return;
[89cc0cc]2526        }
[5ab626a]2527
2528#define X(x) \
2529        if (a->x ##_valid && b->x ##_valid) { \
2530                c->x ##_valid = 1; \
2531                c->x = a->x - b->x; \
2532        } else {\
2533                c->x ##_valid = 0;\
2534        }
2535        LIBTRACE_STAT_FIELDS
2536#undef X
2537}
2538
2539void trace_add_statistics(const libtrace_stat_t *a, const libtrace_stat_t *b,
2540                         libtrace_stat_t *c) {
[2193905]2541        if (a->magic != LIBTRACE_STAT_MAGIC
2542                || b->magic != LIBTRACE_STAT_MAGIC
2543                || c->magic != LIBTRACE_STAT_MAGIC) {
2544                fprintf(stderr, "Use trace_create_statistics() to allocate statistics prior to "
2545                        "calling trace_add_statistics()\n");
[f6f3ae5]2546                return;
[89cc0cc]2547        }
[5ab626a]2548
2549#define X(x) \
2550        if (a->x ##_valid&& b->x ##_valid) { \
2551                c->x ##_valid = 1; \
2552                c->x = a->x + b->x; \
2553        } else {\
2554                c->x ##_valid = 0;\
2555        }
2556        LIBTRACE_STAT_FIELDS
2557#undef X
2558}
2559
2560int trace_print_statistics(const libtrace_stat_t *s, FILE *f, const char *format) {
[2193905]2561        if (s->magic != LIBTRACE_STAT_MAGIC) {
2562                fprintf(stderr, "Use trace_create_statistics() to allocate statistics prior to "
2563                        "calling trace_print_statistics\n");
[33b735c]2564                return TRACE_ERR_STAT;
2565        }
[5ab626a]2566        if (format == NULL)
2567                format = "%s: %"PRIu64"\n";
2568#define xstr(s) str(s)
2569#define str(s) #s
2570#define X(x) \
2571        if (s->x ##_valid) { \
2572                if (fprintf(f, format, xstr(x), s->x) < 0) \
2573                        return -1; \
2574        }
2575        LIBTRACE_STAT_FIELDS
2576#undef X
2577        return 0;
2578}
2579
2580
[d439067]2581inline void trace_clear_cache(libtrace_packet_t *packet) {
[9cc1266]2582
[d439067]2583        packet->cached = clearcache;
[9cc1266]2584}
2585
[c0506ea]2586void trace_interrupt(void) {
2587        libtrace_halt = 1;
2588}
2589
[eda2def]2590void register_format(struct libtrace_format_t *f) {
[2193905]2591        if (f->next != NULL) {
[25a3255]2592                fprintf(stderr, "You cannot register a format twice in register_format()");
2593                return;
2594        }
[eda2def]2595        f->next=formats_list;
2596        formats_list=f;
2597
2598        /* Now, verify that the format has at least the minimum functionality.
[9636216]2599         *
[eda2def]2600         * This #if can be changed to a 1 to output warnings about inconsistent
2601         * functions being provided by format modules.  This generally is very
2602         * noisy, as almost all modules don't implement one or more functions
[9636216]2603         * for various reasons.  This is very useful when checking a new
[eda2def]2604         * format module is sane.
[9636216]2605         */
[eda2def]2606#if 0
2607        if (f->init_input) {
2608#define REQUIRE(x) \
2609                if (!f->x) \
2610                        fprintf(stderr,"%s: Input format should provide " #x "\n",f->name)
2611                REQUIRE(read_packet);
2612                REQUIRE(start_input);
2613                REQUIRE(fin_input);
2614                REQUIRE(get_link_type);
2615                REQUIRE(get_capture_length);
2616                REQUIRE(get_wire_length);
2617                REQUIRE(get_framing_length);
2618                REQUIRE(trace_event);
[9636216]2619                if (!f->get_erf_timestamp
[eda2def]2620                        && !f->get_seconds
2621                        && !f->get_timeval) {
2622                        fprintf(stderr,"%s: A trace format capable of input, should provide at least one of\n"
2623"get_erf_timestamp, get_seconds or trace_timeval\n",f->name);
2624                }
2625                if (f->trace_event!=trace_event_trace) {
2626                        /* Theres nothing that a trace file could optimise with
2627                         * config_input
2628                         */
2629                        REQUIRE(pause_input);
2630                        REQUIRE(config_input);
2631                        REQUIRE(get_fd);
2632                }
2633                else {
2634                        if (f->get_fd) {
2635                                fprintf(stderr,"%s: Unnecessary get_fd\n",
2636                                                f->name);
2637                        }
2638                }
2639#undef REQUIRE
2640        }
2641        else {
2642#define REQUIRE(x) \
2643                if (f->x) \
2644                        fprintf(stderr,"%s: Non Input format shouldn't need " #x "\n",f->name)
2645                REQUIRE(read_packet);
2646                REQUIRE(start_input);
2647                REQUIRE(pause_input);
2648                REQUIRE(fin_input);
2649                REQUIRE(get_link_type);
2650                REQUIRE(get_capture_length);
2651                REQUIRE(get_wire_length);
2652                REQUIRE(get_framing_length);
2653                REQUIRE(trace_event);
2654                REQUIRE(get_seconds);
2655                REQUIRE(get_timeval);
2656                REQUIRE(get_erf_timestamp);
2657#undef REQUIRE
2658        }
2659        if (f->init_output) {
2660#define REQUIRE(x) \
2661                if (!f->x) \
2662                        fprintf(stderr,"%s: Output format should provide " #x "\n",f->name)
2663                REQUIRE(write_packet);
2664                REQUIRE(start_output);
2665                REQUIRE(config_output);
2666                REQUIRE(fin_output);
2667#undef REQUIRE
2668        }
2669        else {
2670#define REQUIRE(x) \
2671                if (f->x) \
2672                        fprintf(stderr,"%s: Non Output format shouldn't need " #x "\n",f->name)
2673                REQUIRE(write_packet);
2674                REQUIRE(start_output);
2675                REQUIRE(config_output);
2676                REQUIRE(fin_output);
2677#undef REQUIRE
2678        }
2679#endif
2680}
2681
Note: See TracBrowser for help on using the repository browser.