Changeset e4e33e9
- Timestamp:
- 01/16/19 13:44:21 (2 years ago)
- Branches:
- develop
- Children:
- 40c87be, 41f03b7, 8a6910b, 90709c5
- Parents:
- 9aa5d7d (diff), ab3ddbe (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - Files:
-
- 2 added
- 67 edited
Legend:
- Unmodified
- Added
- Removed
-
.gitlab-ci.yml
rdd48bb5 reeea305 2 2 - build 3 3 - deploy 4 - upload 4 5 5 6 build-debian-jessie: … … 112 113 only: 113 114 - tags 115 116 upload-packages: 117 stage: upload 118 image: ubuntu:bionic 119 script: 120 - ./bintray-upload.sh 121 only: 122 - tags 123 -
AUTHORS
r35de364 rc79c7fd 38 38 * Tim Dawson for fixing issues with building against musl libc 39 39 * Hendrik Leppelsack for reporting and fixing some errors in the tool manpages 40 * Jacob van Walraven for spending a summer doing various coding odd-jobs 40 41 * Jamie Curtis for fixing a couple of bugs many many years ago 41 42 * Brendon Jones for creating the original Windows DLLs and writing bits of -
README
rbc86030 r91600c7 1 libtrace 4.0. 51 libtrace 4.0.6 2 2 3 3 --------------------------------------------------------------------------- 4 Copyright (c) 2007-201 8The University of Waikato, Hamilton, New Zealand.4 Copyright (c) 2007-2019 The University of Waikato, Hamilton, New Zealand. 5 5 All rights reserved. 6 6 -
configure.in
rbc86030 r95ba467 4 4 # and in the README 5 5 6 AC_INIT([libtrace],[4.0. 5],[contact@wand.net.nz],[libtrace])6 AC_INIT([libtrace],[4.0.6],[contact@wand.net.nz],[libtrace]) 7 7 8 8 LIBTRACE_MAJOR=4 9 9 LIBTRACE_MID=0 10 LIBTRACE_MINOR= 510 LIBTRACE_MINOR=6 11 11 12 12 # OpenSolaris hides libraries like libncurses in /usr/gnu/lib, which is not … … 123 123 # Fail if any of these functions are missing 124 124 AC_CHECK_DECLS([strdup, strlcpy, strcasecmp, strncasecmp, snprintf, vsnprintf, strndup]) 125 AC_CHECK_DECLS([socket, recvmmsg], [], [], [[#include <sys/socket.h>]]) 125 AC_CHECK_DECLS([socket, recvmmsg], [], [], [[#define _GNU_SOURCE 1 126 #include <sys/socket.h>]]) 126 127 AC_CHECK_SIZEOF([long int]) 127 128 … … 403 404 # So instead simply check for existence 404 405 if test "$RTE_SDK" != ""; then 406 AC_CHECK_FILE("$RTE_SDK/$RTE_TARGET/lib/libdpdk.so", dpdk_found="dpdk", dpdk_found=0) 407 fi 408 if test "$dpdk_found" = 0 -a "$RTE_SDK" != ""; then 405 409 AC_CHECK_FILE("$RTE_SDK/$RTE_TARGET/lib/libintel_dpdk.a", dpdk_found=":libintel_dpdk.a", dpdk_found=0) 406 410 fi … … 408 412 if test "$dpdk_found" = 0 -a "$RTE_SDK" != ""; then 409 413 AC_CHECK_FILE("$RTE_SDK/$RTE_TARGET/lib/libdpdk.a", dpdk_found=":libdpdk.a", dpdk_found=0) 410 fi411 if test "$dpdk_found" = 0 -a "$RTE_SDK" != ""; then412 AC_CHECK_FILE("$RTE_SDK/$RTE_TARGET/lib/libdpdk.so", dpdk_found="dpdk", dpdk_found=0)413 414 fi 414 415 if test "$dpdk_found" = 0 -a "$RTE_SDK" != ""; then … … 511 512 if test "$have_pthread" = 1; then 512 513 AC_DEFINE(HAVE_LIBPTHREAD, 1, [Set to 1 if pthreads are supported]) 514 LIBTRACE_LIBS="$LIBTRACE_LIBS -lpthread" 513 515 fi 514 516 … … 602 604 else 603 605 LIBCXXFLAGS="`$LLVM_CONFIG --cxxflags` $CXXFLAGS" 604 LIBTRACE_LIBS="$LIB RACE_LIBS `$LLVM_CONFIG --libs all`";606 LIBTRACE_LIBS="$LIBTRACE_LIBS `$LLVM_CONFIG --libs all`"; 605 607 LDFLAGS="`$LLVM_CONFIG --ldflags` $LDFLAGS"; 606 608 JIT=yes -
debian/changelog
r78fe0d2 rf98e550 1 libtrace4 (4.0.6-1) unstable; urgency=low 2 3 * New upstream release 4 5 -- Shane Alcock <shane.alcock@waikato.ac.nz> Tue, 15 Jan 2019 11:50:06 +1300 6 1 7 libtrace4 (4.0.0-1) unstable; urgency=medium 2 8 -
debian/control
r9aa5d7d re4e33e9 13 13 Section: libdevel 14 14 Architecture: any 15 Depends: libtrace4 (= ${binary:Version}), ${ shlibs:Depends}, ${misc:Depends}15 Depends: libtrace4 (= ${binary:Version}), ${misc:Depends} 16 16 Provides: libtrace-dev 17 17 Conflicts: libtrace-dev, libpacketdump3-dev … … 46 46 Section: libdevel 47 47 Architecture: any 48 Depends: libpacketdump4 (= ${binary:Version}), ${misc:Depends}, 49 ${shlibs:Depends} 48 Depends: libpacketdump4 (= ${binary:Version}), ${misc:Depends} 50 49 Provides: libpacketdump-dev 51 50 Conflicts: libpacketdump-dev -
debian/rules
r4819d75 r729bed4 91 91 dh_gencontrol 92 92 dh_md5sums 93 dh_builddeb 93 dh_builddeb -- -Zgzip 94 94 95 95 binary: binary-indep binary-arch -
examples/parallel/network_capture.c
r10553bf r0f5d4de 21 21 #include <string.h> 22 22 23 static char *output = NULL;23 static char *outputfile = NULL; 24 24 25 25 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; 26 26 static int count = 0; 27 static libtrace_t * trace = NULL;27 static libtrace_t *inptrace = NULL; 28 28 29 29 static int compress_level=-1; … … 32 32 static void stop(int signal UNUSED) 33 33 { 34 if ( trace)35 trace_pstop( trace);34 if (inptrace) 35 trace_pstop(inptrace); 36 36 } 37 37 … … 57 57 const char * first_extension = NULL; 58 58 59 file_index = strrchr(output , '/');59 file_index = strrchr(outputfile, '/'); 60 60 if (file_index) 61 61 first_extension = strchr(file_index, '.'); … … 64 64 65 65 if (first_extension) { 66 snprintf(name, sizeof(name), "%.*s-%d%s", (int) (first_extension - output ), output, my_id, first_extension);66 snprintf(name, sizeof(name), "%.*s-%d%s", (int) (first_extension - outputfile), outputfile, my_id, first_extension); 67 67 } else { 68 snprintf(name, sizeof(name), "%s-%d", output , my_id);68 snprintf(name, sizeof(name), "%s-%d", outputfile, my_id); 69 69 } 70 70 … … 232 232 return 1; 233 233 } 234 trace = trace_create(argv[optind]);235 output = argv[optind+1];236 237 if (trace_is_err( trace)) {238 trace_perror( trace,"Opening trace file");234 inptrace = trace_create(argv[optind]); 235 outputfile = argv[optind+1]; 236 237 if (trace_is_err(inptrace)) { 238 trace_perror(inptrace,"Opening trace file"); 239 239 return 1; 240 240 } … … 247 247 248 248 if (snaplen != -1) 249 trace_set_snaplen( trace, snaplen);249 trace_set_snaplen(inptrace, snaplen); 250 250 if(nb_threads != -1) 251 trace_set_perpkt_threads( trace, nb_threads);251 trace_set_perpkt_threads(inptrace, nb_threads); 252 252 253 253 /* We use a new version of trace_start(), trace_pstart() 254 254 * The reporter function argument is optional and can be NULL */ 255 if (trace_pstart( trace, NULL, processing, NULL)) {256 trace_perror( trace,"Starting trace");257 trace_destroy( trace);255 if (trace_pstart(inptrace, NULL, processing, NULL)) { 256 trace_perror(inptrace,"Starting trace"); 257 trace_destroy(inptrace); 258 258 trace_destroy_callback_set(processing); 259 259 return 1; … … 261 261 262 262 /* Wait for the trace to finish */ 263 trace_join( trace);264 265 if (trace_is_err( trace)) {266 trace_perror( trace,"Reading packets");267 trace_destroy( trace);263 trace_join(inptrace); 264 265 if (trace_is_err(inptrace)) { 266 trace_perror(inptrace,"Reading packets"); 267 trace_destroy(inptrace); 268 268 trace_destroy_callback_set(processing); 269 269 return 1; … … 271 271 272 272 /* Print stats before we destroy the trace */ 273 stats = trace_get_statistics( trace, NULL);273 stats = trace_get_statistics(inptrace, NULL); 274 274 fprintf(stderr, "Overall statistics\n"); 275 275 trace_print_statistics(stats, stderr, "\t%s: %"PRIu64"\n"); 276 276 277 277 trace_destroy_callback_set(processing); 278 trace_destroy( trace);278 trace_destroy(inptrace); 279 279 return 0; 280 280 } -
examples/skeleton/parallel.c
r389dd77 r0f5d4de 53 53 54 54 volatile int done = 0; 55 libtrace_t * trace = NULL;55 libtrace_t *inptrace = NULL; 56 56 57 57 static void cleanup_signal(int sig) { 58 58 (void)sig; /* avoid warnings about unused parameter */ 59 59 done = 1; 60 if ( trace)61 trace_pstop( trace);60 if (inptrace) 61 trace_pstop(inptrace); 62 62 } 63 63 … … 288 288 for (i = optind; i < argc; i++) { 289 289 290 trace = trace_create(argv[i]);291 292 if (trace_is_err( trace)) {293 trace_perror( trace, "Opening trace file");290 inptrace = trace_create(argv[i]); 291 292 if (trace_is_err(inptrace)) { 293 trace_perror(inptrace, "Opening trace file"); 294 294 retcode = -1; 295 295 break; 296 296 } 297 297 298 if (filter && trace_config( trace, TRACE_OPTION_FILTER, filter) == -1) {299 trace_perror( trace, "trace_config(filter)");298 if (filter && trace_config(inptrace, TRACE_OPTION_FILTER, filter) == -1) { 299 trace_perror(inptrace, "trace_config(filter)"); 300 300 retcode = -1; 301 301 break; 302 302 } 303 303 304 trace_set_perpkt_threads( trace, threads);305 trace_set_combiner( trace, &combiner_ordered,304 trace_set_perpkt_threads(inptrace, threads); 305 trace_set_combiner(inptrace, &combiner_ordered, 306 306 (libtrace_generic_t) {0}); 307 trace_set_hasher( trace, HASHER_BIDIRECTIONAL, NULL, NULL);308 309 if (trace_pstart( trace, &global, processing, reporter)) {310 trace_perror( trace, "Starting trace");307 trace_set_hasher(inptrace, HASHER_BIDIRECTIONAL, NULL, NULL); 308 309 if (trace_pstart(inptrace, &global, processing, reporter)) { 310 trace_perror(inptrace, "Starting trace"); 311 311 break; 312 312 } 313 313 314 314 /* Wait for all threads to stop */ 315 trace_join( trace);316 317 if (trace_is_err( trace)) {318 trace_perror( trace, "Processing packets");315 trace_join(inptrace); 316 317 if (trace_is_err(inptrace)) { 318 trace_perror(inptrace, "Processing packets"); 319 319 retcode = -1; 320 320 break; … … 327 327 if (filter) 328 328 trace_destroy_filter(filter); 329 trace_destroy( trace);329 trace_destroy(inptrace); 330 330 trace_destroy_callback_set(processing); 331 331 trace_destroy_callback_set(reporter); -
examples/stats/stats.c
rb9009c2 redd0448 8 8 9 9 double lastts = 0.0; 10 uint64_t v4 =0;11 uint64_t v6 =0;12 uint64_t udp =0;13 uint64_t tcp =0;14 uint64_t icmp =0;15 uint64_t ok =0;10 uint64_t v4_packets=0; 11 uint64_t v6_packets=0; 12 uint64_t udp_packets=0; 13 uint64_t tcp_packets=0; 14 uint64_t icmp_packets=0; 15 uint64_t ok_packets=0; 16 16 17 17 static void per_packet(libtrace_packet_t *packet) … … 34 34 ++lastts; 35 35 printf("%.03f,",lastts); 36 printf("%"PRIu64",%"PRIu64",",v4 ,v6);37 printf("%"PRIu64",%"PRIu64",%"PRIu64,icmp ,tcp,udp);36 printf("%"PRIu64",%"PRIu64",",v4_packets,v6_packets); 37 printf("%"PRIu64",%"PRIu64",%"PRIu64,icmp_packets,tcp_packets,udp_packets); 38 38 printf("\n"); 39 v4 =v6=0;40 icmp =tcp=udp=0;39 v4_packets=v6_packets=0; 40 icmp_packets=tcp_packets=udp_packets=0; 41 41 } 42 42 … … 47 47 return; 48 48 49 /* Get the UDP/TCP/ICMP header from the IPv4 /IPv6packet */49 /* Get the UDP/TCP/ICMP header from the IPv4_packets/IPv6_packets packet */ 50 50 switch (ethertype) { 51 51 case 0x0800: … … 56 56 if (!transport) 57 57 return; 58 ++v4 ;58 ++v4_packets; 59 59 break; 60 60 case 0x86DD: … … 65 65 if (!transport) 66 66 return; 67 ++v6 ;67 ++v6_packets; 68 68 break; 69 69 default: … … 71 71 } 72 72 73 /* Parse the udp /tcp/icmppayload */73 /* Parse the udp_packets/tcp_packets/icmp_packets payload */ 74 74 switch(proto) { 75 75 case 1: 76 ++icmp ;76 ++icmp_packets; 77 77 return; 78 78 case 6: … … 83 83 return; 84 84 85 ++tcp ;85 ++tcp_packets; 86 86 break; 87 87 case 17: … … 92 92 if (!payload) 93 93 return; 94 ++udp ;94 ++udp_packets; 95 95 break; 96 96 default: 97 97 return; 98 98 } 99 ++ok ;99 ++ok_packets; 100 100 } 101 101 … … 201 201 trace_destroy(trace); 202 202 } 203 203 if (filter) { 204 trace_destroy_filter(filter); 205 } 204 206 return 0; 205 207 } -
lib/Makefile.am
rdab79f2 r1668208 10 10 data-struct/simple_circular_buffer.h 11 11 12 AM_CFLAGS=@LIBCFLAGS@ @CFLAG_VISIBILITY@ -pthread 13 AM_CXXFLAGS=@LIBCXXFLAGS@ @CFLAG_VISIBILITY@ -pthread 12 AM_CFLAGS=@LIBCFLAGS@ @CFLAG_VISIBILITY@ -pthread -std=gnu99 13 AM_CXXFLAGS=@LIBCXXFLAGS@ @CFLAG_VISIBILITY@ -pthread -std=gnu99 14 14 15 15 extra_DIST = format_template.c … … 43 43 export SAVED_CFLAGS:=$(CFLAGS) 44 44 export SAVED_CXXFLAGS:=$(CXXFLAGS) 45 export SAVED_CPPFLAGS:=$(CPPFLAGS) 46 export SAVED_LDFLAGS:=$(LDFLAGS) 45 47 include $(RTE_SDK)/mk/rte.vars.mk 46 48 # We need to add -Wl before the linker otherwise this breaks our build … … 50 52 export CFLAGS += $(SAVED_CFLAGS) 51 53 export CXXFLAGS += $(SAVED_CXXFLAGS) 54 export CPPFLAGS:=$(SAVED_CPPFLAGS) 55 export LDFLAGS += $(SAVED_LDFLAGS) 52 56 endif 53 57 -
lib/combiner_ordered.c
ree6e802 r2193905 36 36 static int init_combiner(libtrace_t *t, libtrace_combine_t *c) { 37 37 int i = 0; 38 assert(trace_get_perpkt_threads(t) > 0); 38 if (trace_get_perpkt_threads(t) <= 0) { 39 trace_set_err(t, TRACE_ERR_INIT_FAILED, "You must have atleast 1 processing thread"); 40 return -1; 41 } 39 42 libtrace_queue_t *queues; 40 43 c->queues = calloc(sizeof(libtrace_queue_t), trace_get_perpkt_threads(t)); … … 245 248 246 249 for (i = 0; i < trace_get_perpkt_threads(trace); i++) { 247 assert(libtrace_deque_get_size(&queues[i]) == 0); 250 if (libtrace_deque_get_size(&queues[i]) != 0) { 251 trace_set_err(trace, TRACE_ERR_COMBINER, 252 "Failed to destroy queues, A thread still has data in destroy()"); 253 return; 254 } 248 255 } 249 256 free(queues); -
lib/combiner_sorted.c
ree6e802 r2193905 34 34 static int init_combiner(libtrace_t *t, libtrace_combine_t *c) { 35 35 int i = 0; 36 assert(trace_get_perpkt_threads(t) > 0); 36 if (trace_get_perpkt_threads(t) <= 0) { 37 trace_set_err(t, TRACE_ERR_INIT_FAILED, "You must have atleast 1 processing thread"); 38 return -1; 39 } 37 40 libtrace_vector_t *queues; 38 41 c->queues = calloc(sizeof(libtrace_vector_t), trace_get_perpkt_threads(t)); … … 105 108 106 109 for (i = 0; i < trace_get_perpkt_threads(trace); i++) { 107 assert(libtrace_vector_get_size(&queues[i]) == 0); 110 if (libtrace_vector_get_size(&queues[i]) != 0) { 111 trace_set_err(trace, TRACE_ERR_COMBINER, 112 "Failed to destroy queues, A thread still has data in destroy()"); 113 return; 114 } 108 115 libtrace_vector_destroy(&queues[i]); 109 116 } -
lib/combiner_unordered.c
ree6e802 r2193905 34 34 static int init_combiner(libtrace_t *t, libtrace_combine_t *c) { 35 35 int i = 0; 36 assert(trace_get_perpkt_threads(t) > 0); 36 if (trace_get_perpkt_threads(t) <= 0) { 37 trace_set_err(t, TRACE_ERR_INIT_FAILED, "You must have atleast 1 processing thread"); 38 return -1; 39 } 37 40 libtrace_queue_t *queues; 38 41 c->queues = calloc(sizeof(libtrace_queue_t), trace_get_perpkt_threads(t)); … … 87 90 88 91 for (i = 0; i < trace_get_perpkt_threads(trace); i++) { 89 assert(libtrace_deque_get_size(&queues[i]) == 0); 92 if (libtrace_deque_get_size(&queues[i]) != 0) { 93 trace_set_err(trace, TRACE_ERR_COMBINER, 94 "Failed to destroy queues, A thread still has data in destroy()"); 95 return; 96 } 90 97 } 91 98 free(queues); -
lib/data-struct/buckets.c
ree6e802 r2193905 26 26 27 27 #include <stdlib.h> 28 #include <assert.h>29 28 #include <string.h> 30 29 #include "buckets.h" … … 183 182 libtrace_bucket_node_t tmp; 184 183 185 assert(id != 0); 184 if (id == 0) { 185 fprintf(stderr, "bucket ID cannot be 0 in libtrace_release_bucket_id()\n"); 186 return; 187 } 186 188 187 189 pthread_mutex_lock(&b->lock); 188 190 bnode = b->packets[id]; 189 assert(bnode != NULL); 191 if (!bnode) { 192 fprintf(stderr, "bucket ID %lu is NULL in libtrace_release_bucket_id()\n", id); 193 return; 194 } 190 195 191 196 … … 196 201 s = id - bnode->startindex; 197 202 } 198 assert(s < bnode->slots); 199 assert(bnode->released[s] != 0); 203 if (s >= bnode->slots) { 204 fprintf(stderr, "Error in libtrace_release_bucket_id()\n"); 205 return; 206 } 207 if (bnode->released[s] == 0) { 208 fprintf(stderr, "Error in libtrace_release_bucket_id()\n"); 209 return; 210 } 200 211 201 212 … … 228 239 break; 229 240 230 assert(lnode->next != NULL); 241 if (!lnode->next) { 242 fprintf(stderr, "Error in libtrace_release_bucket_id()\n"); 243 return; 244 } 231 245 for (i = 0; i < front->slots; i++) { 232 246 if (front->released[i] == 2) { -
lib/data-struct/deque.c
ree6e802 r2193905 63 63 ASSERT_RET(pthread_mutex_lock(&q->lock), == 0); 64 64 if (q->head == NULL) { 65 assert(q->tail == NULL && q->size == 0); 65 if (q->tail != NULL || q->size != 0) { 66 fprintf(stderr, "Error deque head cannot be NULL with a non NULL tail and size of more than 0 in libtrace_deque_push_back()\n"); 67 return; 68 } 66 69 new_node->prev = NULL; 67 70 q->head = q->tail = new_node; 68 71 } else { 69 assert (q->tail != NULL); 72 if (q->tail == NULL) { 73 fprintf(stderr, "Error deque tail cannot be NULL if it contains a head in libtrace_deque_push_back()\n"); 74 return; 75 } 70 76 q->tail->next = new_node; 71 77 new_node->prev = q->tail; // Done the double link … … 86 92 ASSERT_RET(pthread_mutex_lock(&q->lock), == 0); 87 93 if (q->head == NULL) { 88 assert(q->tail == NULL && q->size == 0); 94 if (q->tail != NULL || q->size != 0) { 95 fprintf(stderr, "Error deque head cannot be NULL with a non NULL tail and size of more than 0 in libtrace_deque_push_front()\n"); 96 return; 97 } 89 98 new_node->next = NULL; 90 99 q->head = q->tail = new_node; 91 100 } else { 92 assert (q->head != NULL);93 101 q->head->prev = new_node; 94 102 new_node->next = q->head; // Done the double link -
lib/data-struct/linked_list.c
ree6e802 r2193905 26 26 #include "linked_list.h" 27 27 28 #include <assert.h>29 28 #include <stddef.h> 30 29 #include <stdlib.h> … … 72 71 /* Create the new node */ 73 72 new = (libtrace_list_node_t *)malloc(sizeof(libtrace_list_node_t)); 74 assert(new != NULL); 73 if (!new) { 74 fprintf(stderr, "Unable to allocate memory for node in libtrace_list_push_front()\n"); 75 return; 76 } 75 77 new->data = malloc(l->element_size); 76 assert(new->data != NULL); 78 if (!new->data) { 79 fprintf(stderr, "Unable to allocate memory for node data in libtrace_list_push_front()\n"); 80 } 77 81 78 82 new->prev = NULL; … … 80 84 81 85 if (l->head == NULL) { 82 assert(l->tail == NULL && l->size == 0); 86 if (l->tail != NULL || l->size != 0) { 87 fprintf(stderr, "Error cannot have a NULL head with a non NULL tail and a size of non 0 in libtrace_list_push_front()\n"); 88 return; 89 } 83 90 new->next = NULL; 84 91 l->head = l->tail = new; … … 100 107 /* Create the new node */ 101 108 new = (libtrace_list_node_t *)malloc(sizeof(libtrace_list_node_t)); 102 assert(new != NULL); 109 if (!new) { 110 fprintf(stderr, "Unable to allocate memory for node in libtrace_list_push_back()\n"); 111 return; 112 } 103 113 new->data = malloc(l->element_size); 104 assert(new->data != NULL); 105 114 if (!new->data) { 115 fprintf(stderr, "Unable to allocate memory for node data in libtrace_list_push_back()\n"); 116 return; 117 } 106 118 new->next = NULL; 107 119 memcpy(new->data, item, l->element_size); 108 120 109 121 if (l->tail == NULL) { 110 assert(l->head == NULL && l->size == 0); 122 if (l->head != NULL || l->size != 0) { 123 fprintf(stderr, "Error cannot have a NULL tail with a non NULL head and a size of non 0 in libtrace_list_push_back()\n"); 124 return; 125 } 111 126 new->prev = NULL; 112 127 l->head = l->tail = new; … … 197 212 while (index--) { 198 213 ret = ret->next; 199 assert(ret != NULL); 214 if (!ret) { 215 fprintf(stderr, "Error encountered NULL index in libtrace_list_get_index()\n"); 216 return NULL; 217 } 200 218 } 201 219 -
lib/data-struct/message_queue.c
ree6e802 r2193905 43 43 void libtrace_message_queue_init(libtrace_message_queue_t *mq, size_t message_len) 44 44 { 45 assert(message_len); 45 if (!message_len) { 46 fprintf(stderr, "Message length cannot be 0 in libtrace_message_queue_init()\n"); 47 return; 48 } 46 49 ASSERT_RET(pipe(mq->pipefd), != -1); 47 50 mq->message_count = 0; … … 69 72 { 70 73 int ret; 71 assert(mq->message_len); 74 if (!mq->message_len) { 75 fprintf(stderr, "Message queue must be initialised with libtrace_message_queue_init()" 76 "before inserting messages in libtrace_message_queue_put()\n"); 77 return 0; 78 } 72 79 ASSERT_RET(write(mq->pipefd[1], message, mq->message_len), == (int) mq->message_len); 73 80 // Update after we've written -
lib/data-struct/object_cache.c
ree6e802 r2193905 143 143 */ 144 144 static void resize_memory_caches(struct local_caches *lcs) { 145 assert (lcs->t_mem_caches_total > 0); 145 if (lcs->t_mem_caches_total <= 0) { 146 fprintf(stderr, "Expected lcs->t_mem_caches_total to be greater or equal to 0 in resize_memory_caches()\n"); 147 return; 148 } 146 149 lcs->t_mem_caches += 0x10; 147 150 lcs->t_mem_caches = realloc(lcs->t_mem_caches, … … 166 169 /* This thread has not been used with a memory pool before */ 167 170 /* Allocate our TLS */ 168 assert(lcs == NULL); 171 if (lcs) { 172 fprintf(stderr, "Expected lcs to be NULL in get_local_caches()\n"); 173 return NULL; 174 } 169 175 lcs = calloc(1, sizeof (struct local_caches)); 170 assert(lcs); 176 if (!lcs) { 177 fprintf(stderr, "Unable to allocate memory for lcs in get_local_caches()\n"); 178 return NULL; 179 } 171 180 /* Hook into pthreads to destroy this when the thread ends */ 172 181 pthread_once(&memory_destructor_once, &once_memory_cache_key_init); … … 174 183 lcs->t_mem_caches_total = 0x10; 175 184 lcs->t_mem_caches = calloc(0x10, sizeof(struct local_cache)); 176 assert(lcs); 177 assert(lcs->t_mem_caches); 185 if (!lcs->t_mem_caches) { 186 fprintf(stderr, "Unable to allocate memory for lcs->t_mem_caches in get_local_caches()\n"); 187 return NULL; 188 } 178 189 return lcs; 179 190 } … … 210 221 } 211 222 212 assert(!lc->invalid); 223 if (lc->invalid) { 224 fprintf(stderr, "lc cache is invalid in find_cache()\n"); 225 return NULL; 226 } 213 227 return lc; 214 228 } … … 246 260 size_t buffer_size, bool limit_size) { 247 261 248 assert(buffer_size); 249 assert(alloc); 250 assert(free); 262 if (buffer_size <= 0) { 263 fprintf(stderr, "NULL buffer_size passed into libtrace_ocache_init()\n"); 264 return -1; 265 } 266 if (!alloc) { 267 fprintf(stderr, "NULL alloc passed into libtrace_ocache_init()\n"); 268 return -1; 269 } 270 if (!free) { 271 fprintf(stderr, "NULL free method passed into libtrace_ocache_init()\n"); 272 return -1; 273 } 251 274 if (libtrace_ringbuffer_init(&oc->rb, buffer_size, LIBTRACE_RINGBUFFER_BLOCKING) != 0) { 252 275 return -1; … … 366 389 mem_hits.read.miss += nb_buffers - i; 367 390 #endif 368 assert(i >= min_nb_buffers); 391 if (i < min_nb_buffers) { 392 fprintf(stderr, "Unable to fill remaining cache in libtrace_ocache_alloc_cache()\n"); 393 return ~0U; 394 } 369 395 return i; 370 396 } … … 376 402 bool try_alloc = !(oc->max_allocations && oc->max_allocations <= oc->current_allocations); 377 403 378 assert(oc->max_allocations ? nb_buffers < oc->max_allocations : 1); 404 if (oc->max_allocations) { 405 if(nb_buffers >= oc->max_allocations) { 406 fprintf(stderr, "Expected nb_buffers to be less than or equal to the object cache " 407 "max allocation in libtrace_ocache_alloc()\n"); 408 return ~0U; 409 } 410 } 379 411 min = try_alloc ? 0: min_nb_buffers; 380 412 if (lc) … … 399 431 for (;i < nb; ++i) { 400 432 values[i] = (*oc->alloc)(); 401 assert(values[i]); 402 } 403 assert (i == nb); 433 if (!values[i]) { 434 fprintf(stderr, "Unable to alloc memory for values[%zu] in libtrace_ocache_alloc()\n", i); 435 return ~0U; 436 } 437 } 438 439 if (i != nb) { 440 fprintf(stderr, "Expected i == nb in libtrace_ocache_alloc()\n"); 441 return ~0U; 442 } 404 443 // Still got to wait for more 405 444 if (nb < min_nb_buffers) { … … 410 449 } 411 450 } 412 assert(i >= min_nb_buffers); 451 if (i < min_nb_buffers) { 452 fprintf(stderr, "Failed to allocate minimum number of buffers for libtrace " 453 "object cache in libtrace_ocache_alloc()\n"); 454 return ~0U; 455 } 413 456 return i; 414 457 } … … 487 530 size_t min; 488 531 489 assert(oc->max_allocations ? nb_buffers < oc->max_allocations : 1); 532 if (oc->max_allocations) { 533 if(nb_buffers >= oc->max_allocations) { 534 fprintf(stderr, "Expected nb_buffers to be less than or equal to the object cache " 535 "max allocation in libtrace_ocache_alloc()\n"); 536 return ~0U; 537 } 538 } 490 539 min = oc->max_allocations ? min_nb_buffers : 0; 491 540 if (lc) -
lib/data-struct/ring_buffer.c
ree6e802 r2193905 268 268 size_t nb_ready; 269 269 size_t i = 0; 270 271 assert(min_nb_buffers <= nb_buffers); 270 271 if (min_nb_buffers > nb_buffers) { 272 fprintf(stderr, "min_nb_buffers must be greater than or equal to nb_buffers in libtrace_ringbuffer_write_bulk()\n"); 273 return ~0U; 274 } 272 275 if (!min_nb_buffers && libtrace_ringbuffer_is_full(rb)) 273 276 return 0; … … 346 349 size_t nb_ready; 347 350 size_t i = 0; 348 349 assert(min_nb_buffers <= nb_buffers); 351 352 if (min_nb_buffers > nb_buffers) { 353 fprintf(stderr, "min_nb_buffers must be greater than or equal to nb_buffers in libtrace_ringbuffer_write_bulk()\n"); 354 return ~0U; 355 } 350 356 351 357 if (!min_nb_buffers && libtrace_ringbuffer_is_empty(rb)) -
lib/data-struct/sliding_window.c
ree6e802 r2193905 27 27 28 28 #include <stdlib.h> 29 #include <assert.h>30 29 #include <string.h> 31 30 31 #include <stdio.h> 32 32 /** 33 33 * Implements a sliding window via a ring buffer, this is a fixed size. … … 43 43 sw->start = 0; 44 44 sw->elements = calloc(sw->size, sizeof(void*)); 45 assert(sw->elements); 45 if (!sw->elements) { 46 fprintf(stderr, "Unable to allocate memory for sw->elements in libtrace_slidingwindow_init()\n"); 47 return; 48 } 46 49 memset((void *) sw->elements, 0, sizeof(void*) * sw->size); 47 50 sw->start_number = start_number; -
lib/data-struct/vector.c
ree6e802 r2193905 54 54 v->max_size *= 2; 55 55 v->elements = realloc(v->elements, v->max_size * v->element_size); 56 assert(v->elements); 56 if (!v->elements) { 57 fprintf(stderr, "Unable to allocate memory for v->elements in libtrace_vector_push_back()\n"); 58 return; 59 } 57 60 } 58 61 memcpy(&v->elements[v->size*v->element_size], d, v->element_size); … … 104 107 DLLEXPORT void libtrace_vector_append(libtrace_vector_t *dest, libtrace_vector_t *src) 105 108 { 106 assert(dest->element_size == src->element_size); 109 if (dest->element_size != src->element_size) { 110 fprintf(stderr, "Elements must be the same size in libtrace_vector_append()\n"); 111 return; 112 } 107 113 if (src->size == 0) // Nothing to do if this is the case 108 114 return; -
lib/format_atmhdr.c
r32ee9b2 r2193905 34 34 35 35 #include <sys/stat.h> 36 #include <assert.h>37 36 #include <errno.h> 38 37 #include <fcntl.h> -
lib/format_bpf.c
ra0f031b r418c78d 123 123 124 124 /* Initialises a BPF input trace */ 125 static int bpf_init_input(libtrace_t *libtrace) 126 { 125 static int bpf_init_input(libtrace_t *libtrace) { 127 126 libtrace->format_data = (struct libtrace_format_data_t *) 128 127 malloc(sizeof(struct libtrace_format_data_t)); 129 128 129 if (!libtrace->format_data) { 130 trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, "Unable to allocate memory " 131 "for format data inside bpf_init_input()"); 132 return -1; 133 } 134 130 135 /* Throw some default values into the format data */ 131 136 FORMATIN(libtrace)->fd = -1; … … 372 377 break; 373 378 case TRACE_OPTION_REPLAY_SPEEDUP: 379 break; 380 case TRACE_OPTION_CONSTANT_ERF_FRAMING: 374 381 break; 375 382 /* Avoid default: so that future options will cause a warning -
lib/format_dag24.c
r32ee9b2 r418c78d 33 33 #include "format_erf.h" 34 34 35 #include <assert.h>36 35 #include <errno.h> 37 36 #include <fcntl.h> … … 179 178 return -1; 180 179 } 181 180 182 181 dag_init_format_data(libtrace); 183 182 if (S_ISCHR(buf.st_mode)) { … … 231 230 /* Live capture is always going to be realtime */ 232 231 return -1; 232 case TRACE_OPTION_CONSTANT_ERF_FRAMING: 233 return -1; 233 234 } 234 235 return -1; … … 326 327 return NULL; 327 328 size = ntohs(erfptr->rlen); 328 assert( size >= dag_record_size ); 329 if (size < dag_record_size) { 330 fprintf(stderr, "DAG2.4 rlen is invalid (rlen = %u, must be at least %u)\n", 331 size, dag_record_size); 332 return NULL; 333 } 329 334 FORMAT_DATA->offset += size; 330 335 FORMAT_DATA->diff -= size; … … 480 485 } while (1); 481 486 482 483 /* We only want to sleep for a very short time */484 assert(data == 0);485 487 event.type = TRACE_EVENT_SLEEP; 486 488 event.seconds = 0.0001; -
lib/format_dag25.c
r32ee9b2 rfce4572 33 33 #include "format_erf.h" 34 34 35 #include <assert.h>36 35 #include <errno.h> 37 36 #include <fcntl.h> … … 176 175 struct dag_dev_t *open_dags = NULL; 177 176 177 static bool dag_can_write(libtrace_packet_t *packet) { 178 /* Get the linktype */ 179 libtrace_linktype_t ltype = trace_get_link_type(packet); 180 181 if (ltype == TRACE_TYPE_CONTENT_INVALID) { 182 return false; 183 } 184 185 /* TODO erf meta should definitely be writable, pcapng meta 186 * could probably be converted into erf meta */ 187 if (ltype == TRACE_TYPE_ERF_META 188 || ltype == TRACE_TYPE_NONDATA 189 || ltype == TRACE_TYPE_PCAPNG_META) { 190 return false; 191 } 192 193 return true; 194 } 195 178 196 /* Returns the amount of padding between the ERF header and the start of the 179 197 * captured packet data */ … … 245 263 FORMAT_DATA->per_stream = 246 264 libtrace_list_init(sizeof(stream_data)); 247 assert(FORMAT_DATA->per_stream != NULL); 265 if (FORMAT_DATA->per_stream == NULL) { 266 trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, 267 "Unable to create list for stream in dag_init_format_data()"); 268 return; 269 } 248 270 249 271 /* We'll start with just one instance of stream_data, and we'll … … 284 306 { 285 307 /* Need to remove from the device list */ 286 assert(dev->ref_count == 0); 308 if (dev->ref_count != 0) { 309 fprintf(stderr, "Cannot close DAG device with non zero reference in dag_close_device()\n"); 310 return; 311 } 287 312 288 313 if (dev->prev == NULL) { … … 522 547 dag_dev_name = NULL; 523 548 pthread_mutex_unlock(&open_dag_mutex); 549 trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, "Unable to open DAG device %s", dag_dev_name); 524 550 return -1; 525 551 } … … 615 641 * the user to configure this correctly. */ 616 642 return 0; 643 case TRACE_OPTION_CONSTANT_ERF_FRAMING: 644 return -1; 617 645 } 618 646 return -1; … … 995 1023 996 1024 size = ntohs(erfptr->rlen); 997 assert( size >= dag_record_size ); 1025 if (size < dag_record_size) { 1026 fprintf(stderr, "DAG2.5 rlen is invalid (rlen %u, must be at least %u\n", 1027 size, dag_record_size); 1028 return NULL; 1029 } 998 1030 999 1031 /* Make certain we have the full packet available */ … … 1173 1205 static int dag_write_packet(libtrace_out_t *libtrace, libtrace_packet_t *packet) 1174 1206 { 1207 /* Check dag can write this type of packet */ 1208 if (!dag_can_write(packet)) { 1209 return 0; 1210 } 1211 1175 1212 /* This is heavily borrowed from erf_write_packet(). Yes, CnP 1176 1213 * coding sucks, sorry about that. … … 1188 1225 } 1189 1226 1190 if (trace_get_link_type(packet) == TRACE_TYPE_NONDATA)1191 return 0;1192 1193 1227 pad = dag_get_padding(packet); 1194 1228 … … 1225 1259 1226 1260 /* Packet length (rlen includes format overhead) */ 1227 assert(trace_get_capture_length(packet) > 0 1228 && trace_get_capture_length(packet) <= 65536); 1229 assert(erf_get_framing_length(packet) > 0 1230 && trace_get_framing_length(packet) <= 65536); 1231 assert(trace_get_capture_length(packet) + 1232 erf_get_framing_length(packet) > 0 1233 && trace_get_capture_length(packet) + 1234 erf_get_framing_length(packet) <= 65536); 1261 if (trace_get_capture_length(packet) <= 0 1262 || trace_get_capture_length(packet) > 65536) { 1263 trace_set_err_out(libtrace, TRACE_ERR_BAD_PACKET, 1264 "Capture length is out of range in dag_write_packet()"); 1265 return -1; 1266 } 1267 if (erf_get_framing_length(packet) <= 0 1268 || trace_get_framing_length(packet) > 65536) { 1269 trace_set_err_out(libtrace, TRACE_ERR_BAD_PACKET, 1270 "Framing length is out of range in dag_write_packet()"); 1271 return -1; 1272 } 1273 if (trace_get_capture_length(packet) + 1274 erf_get_framing_length(packet) <= 0 1275 || trace_get_capture_length(packet) + 1276 erf_get_framing_length(packet) > 65536) { 1277 trace_set_err_out(libtrace, TRACE_ERR_BAD_PACKET, 1278 "Capture + framing length is out of range in dag_write_packet()"); 1279 return -1; 1280 } 1235 1281 1236 1282 erfhdr.rlen = htons(trace_get_capture_length(packet) … … 1482 1528 { 1483 1529 libtrace_list_node_t *tmp; 1484 assert(stat && libtrace); 1530 if (!libtrace) { 1531 fprintf(stderr, "NULL trace passed into dag_get_statistics()\n"); 1532 return; 1533 } 1534 if (!stat) { 1535 trace_set_err(libtrace, TRACE_ERR_STAT, "NULL stat passed into dag_get_statistics()"); 1536 return; 1537 } 1485 1538 tmp = FORMAT_DATA_HEAD; 1486 1539 … … 1498 1551 libtrace_stat_t *stat) { 1499 1552 struct dag_per_stream_t *stream_data = t->format_data; 1500 assert(stat && libtrace); 1501 1553 if (!libtrace) { 1554 fprintf(stderr, "NULL trace passed into dag_get_thread_statistics()\n"); 1555 return; 1556 } 1557 if (!stat) { 1558 trace_set_err(libtrace, TRACE_ERR_STAT, "NULL stat passed into dag_get_thread_statistics()"); 1559 return; 1560 } 1502 1561 stat->dropped_valid = 1; 1503 1562 stat->dropped = stream_data->drops; -
lib/format_dpdk.c
r3b94ef2 r4d390c0 51 51 52 52 #include <stdlib.h> 53 #include <assert.h>54 53 #include <unistd.h> 55 54 #include <endian.h> … … 146 145 }; 147 146 147 static bool dpdk_can_write(libtrace_packet_t *packet) { 148 libtrace_linktype_t ltype = trace_get_link_type(packet); 149 150 if (ltype == TRACE_TYPE_CONTENT_INVALID) { 151 return false; 152 } 153 if (ltype == TRACE_TYPE_NONDATA || ltype == TRACE_TYPE_ERF_META || 154 ltype == TRACE_TYPE_PCAPNG_META) { 155 return false; 156 } 157 return true; 158 } 159 148 160 /** 149 161 * We want to blacklist all devices except those on the whitelist … … 213 225 static int parse_pciaddr(char * str, struct rte_pci_addr * addr, long * core) { 214 226 int matches; 215 assert(str); 227 228 if (!str) { 229 fprintf(stderr, "NULL str passed into parse_pciaddr()\n"); 230 return -1; 231 } 216 232 #if RTE_VERSION >= RTE_VERSION_NUM(17, 8, 0, 1) 217 233 matches = sscanf(str, "%8"SCNx32":%2"SCNx8":%2"SCNx8".%2"SCNx8"-%ld", … … 252 268 if((file = fopen(path, "r")) != NULL) { 253 269 int numa_node = -1; 254 fscanf(file, "%d", &numa_node); 270 if (fscanf(file, "%d", &numa_node) != 1) { 271 numa_node = -1; 272 } 255 273 fclose(file); 256 274 return numa_node; … … 325 343 int i; 326 344 327 assert (core < RTE_MAX_LCORE); 328 assert (rte_get_master_lcore() == rte_lcore_id()); 345 if (core >= RTE_MAX_LCORE) { 346 fprintf(stderr, "Core must be a value less than the number of cores " 347 "in dpdk_move_master_lcore()\n"); 348 return -1; 349 } 350 if (rte_get_master_lcore() != rte_lcore_id()) { 351 fprintf(stderr, "Master core not equal to core id in dpdk_move_master_lcore()\n"); 352 return -1; 353 } 329 354 330 355 if (core == rte_lcore_id()) … … 332 357 333 358 /* Make sure we are not overwriting someone else */ 334 assert(!rte_lcore_is_enabled(core)); 359 if (rte_lcore_is_enabled(core)) { 360 fprintf(stderr, "Cannot override another core in dpdk_move_master_lcore()\n"); 361 return -1; 362 } 335 363 336 364 /* Move the core */ … … 470 498 int max_node_cpu = -1; 471 499 struct bitmask *mask = numa_allocate_cpumask(); 472 assert(mask); 500 if (!mask) { 501 fprintf(stderr, "Unable to allocate cpu mask in dpdk_init_environment()\n"); 502 return -1; 503 } 473 504 numa_node_to_cpus(format_data->nic_numa_node, mask); 474 505 for (i = 0 ; i < nb_cpu; ++i) { … … 530 561 } 531 562 // Only the master should be running 532 assert(cfg->lcore_count == 1); 563 if (cfg->lcore_count != 1) { 564 fprintf(stderr, "Expected only the master core to be running in dpdk_init_environment()\n"); 565 return -1; 566 } 533 567 534 568 // TODO XXX TODO … … 587 621 libtrace->format_data = (struct dpdk_format_data_t *) 588 622 malloc(sizeof(struct dpdk_format_data_t)); 623 624 if (!libtrace->format_data) { 625 trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, "Unable to allocate memory for " 626 "format data inside dpdk_init_input()"); 627 return 1; 628 } 629 589 630 FORMAT(libtrace)->port = 0; /* Always assume 1 port loaded */ 590 631 FORMAT(libtrace)->nb_ports = 0; … … 628 669 libtrace->format_data = (struct dpdk_format_data_t *) 629 670 malloc(sizeof(struct dpdk_format_data_t)); 671 672 if (!libtrace->format_data) { 673 trace_set_err_out(libtrace, TRACE_ERR_INIT_FAILED, "Unable to allocate memory for " 674 "format data inside dpdk_init_output()"); 675 return -1; 676 } 630 677 FORMAT(libtrace)->port = 0; /* Always assume 1 port loaded */ 631 678 FORMAT(libtrace)->nb_ports = 0; … … 703 750 case TRACE_OPTION_EVENT_REALTIME: 704 751 case TRACE_OPTION_REPLAY_SPEEDUP: 752 case TRACE_OPTION_CONSTANT_ERF_FRAMING: 705 753 break; 706 754 /* Avoid default: so that future options will cause a warning … … 832 880 struct dpdk_format_data_t * format_data = cb_arg; 833 881 struct rte_eth_link link_info; 834 assert(event == RTE_ETH_EVENT_INTR_LSC); 835 assert(port == format_data->port); 882 if (event != RTE_ETH_EVENT_INTR_LSC) { 883 fprintf(stderr, "Received unexpected event in dpdk_lsc_callback()\n"); 884 #if RTE_VERSION >= RTE_VERSION_NUM(17, 8, 0, 1) 885 return -1; 886 #else 887 return; 888 #endif 889 } 890 if (port != format_data->port) { 891 fprintf(stderr, "Port does not match port in format data in dpdk_lsc_callback()\n"); 892 #if RTE_VERSION >= RTE_VERSION_NUM(17, 8, 0, 1) 893 return -1; 894 #else 895 return; 896 #endif 897 } 836 898 837 899 rte_eth_link_get_nowait(port, &link_info); … … 1445 1507 struct rte_config *cfg = rte_eal_get_configuration(); 1446 1508 1447 assert(rte_lcore_id() < RTE_MAX_LCORE); 1509 if (rte_lcore_id() >= RTE_MAX_LCORE) { 1510 fprintf(stderr, "Expected core id less than or equal to RTE_MAX_LCORE in " 1511 "dpdk_punregister_thread()\n"); 1512 return; 1513 } 1448 1514 pthread_mutex_lock(&dpdk_lock); 1449 1515 /* Skip if master */ … … 1458 1524 cfg->lcore_count--; 1459 1525 RTE_PER_LCORE(_lcore_id) = -1; // Might make the world burn if used again 1460 assert(cfg->lcore_count >= 1); // We cannot unregister the master LCORE!! 1526 if (cfg->lcore_count < 1) { 1527 fprintf(stderr, "You cannot unregister the master lcore in dpdk_punregister_thread()\n"); 1528 return; 1529 } 1461 1530 pthread_mutex_unlock(&dpdk_lock); 1462 1531 return; … … 1507 1576 static int dpdk_write_packet(libtrace_out_t *trace, 1508 1577 libtrace_packet_t *packet){ 1578 1579 /* Check dpdk can write this type of packet */ 1580 if (!dpdk_can_write(packet)) { 1581 return 0; 1582 } 1583 1509 1584 struct rte_mbuf* m_buff[1]; 1510 1585 … … 1584 1659 */ 1585 1660 static inline struct dpdk_addt_hdr * get_addt_hdr (const libtrace_packet_t *packet) { 1586 assert(packet); 1587 assert(packet->buffer); 1661 if (!packet) { 1662 fprintf(stderr, "NULL packet passed into dpdk_addt_hdr()\n"); 1663 return NULL; 1664 } 1665 if (!packet->buffer) { 1666 fprintf(stderr, "NULL packet buffer passed into dpdk_addt_hdr()\n"); 1667 return NULL; 1668 } 1588 1669 /* Our header sits straight after the mbuf header */ 1589 1670 return (struct dpdk_addt_hdr *) ((struct rte_mbuf*) packet->buffer + 1); … … 1603 1684 1604 1685 /* Reset the cached capture length first*/ 1605 packet->ca pture_length = -1;1686 packet->cached.capture_length = -1; 1606 1687 hdr->cap_len = (uint32_t) size; 1607 1688 return trace_get_capture_length(packet); … … 1637 1718 libtrace_packet_t *packet, void *buffer, 1638 1719 libtrace_rt_types_t rt_type, uint32_t flags) { 1639 assert(packet); 1720 if (!packet) { 1721 fprintf(stderr, "NULL packet passed into dpdk_prepare_packet()\n"); 1722 return TRACE_ERR_NULL_PACKET; 1723 } 1640 1724 if (packet->buffer != buffer && 1641 1725 packet->buf_control == TRACE_CTRL_PACKET) { … … 1939 2023 if (packets[i]->buffer != NULL) { 1940 2024 /* The packet should always be finished */ 1941 assert(packets[i]->buf_control == TRACE_CTRL_PACKET); 2025 if (packets[i]->buf_control != TRACE_CTRL_PACKET) { 2026 trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, "Expected packet buffer " 2027 "to be empty in dpdk_pread_packets()\n"); 2028 return -1; 2029 } 1942 2030 free(packets[i]->buffer); 1943 2031 } … … 1964 2052 if (packet->buffer != NULL) { 1965 2053 /* The packet should always be finished */ 1966 assert(packet->buf_control == TRACE_CTRL_PACKET); 2054 if (packet->buf_control != TRACE_CTRL_PACKET) { 2055 trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, "Expected packet buffer to be " 2056 "empty in dpdk_read_packet()\n"); 2057 return -1; 2058 } 1967 2059 free(packet->buffer); 1968 2060 packet->buffer = NULL; … … 2086 2178 if (packet->buffer != NULL) { 2087 2179 /* The packet should always be finished */ 2088 assert(packet->buf_control == TRACE_CTRL_PACKET); 2180 if (packet->buf_control != TRACE_CTRL_PACKET) { 2181 trace_set_err(trace, TRACE_ERR_BAD_PACKET, "Expected packet " 2182 "buffer to be empty in dpdk_trace_event()\n"); 2183 event.type = TRACE_EVENT_TERMINATE; 2184 return event; 2185 } 2089 2186 free(packet->buffer); 2090 2187 packet->buffer = NULL; -
lib/format_dpdkndag.c
r32ee9b2 rfdf23b8 7 7 #include "format_erf.h" 8 8 9 #include <assert.h>10 9 #include <errno.h> 11 10 #include <fcntl.h> … … 86 85 libtrace->format_data = (dpdkndag_format_data_t *)malloc( 87 86 sizeof(dpdkndag_format_data_t)); 87 88 if (!libtrace->format_data) { 89 trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, "Unable to allocate memory for " 90 "format data inside dpdkndag_init_input()"); 91 return -1; 92 } 88 93 89 94 FORMAT_DATA->localiface = NULL; -
lib/format_duck.c
r32ee9b2 r2193905 33 33 34 34 #include <errno.h> 35 #include <assert.h>36 35 #include <stdio.h> 37 36 #include <fcntl.h> … … 63 62 int fileflag; 64 63 iow_t *file; 65 int dag_version; 64 int dag_version; 66 65 }; 67 66 … … 69 68 libtrace->format_data = malloc(sizeof(struct duck_format_data_t)); 70 69 70 if (!libtrace->format_data) { 71 trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, "Unable to allocate memory for " 72 "format data inside duck_init_input()"); 73 return 1; 74 } 75 71 76 DATA(libtrace)->dag_version = 0; 72 77 return 0; … … 75 80 static int duck_init_output(libtrace_out_t *libtrace) { 76 81 libtrace->format_data = malloc(sizeof(struct duck_format_data_out_t)); 77 82 83 if (!libtrace->format_data) { 84 trace_set_err_out(libtrace, TRACE_ERR_INIT_FAILED, "Unable to allocate memory for " 85 "format data inside duck_init_output()"); 86 return -1; 87 } 88 78 89 OUTPUT->level = 0; 79 90 OUTPUT->compress_type = TRACE_OPTION_COMPRESSTYPE_NONE; … … 102 113 return -1; 103 114 } 104 assert(0); 115 trace_set_err_out(libtrace, TRACE_ERR_UNKNOWN_OPTION, 116 "Unknown option in duck_config_output()"); 117 return -1; 105 118 } 106 119 … … 233 246 234 247 if (duck_prepare_packet(libtrace, packet, packet->buffer, packet->type, 235 flags)) 236 return -1; 237 248 flags)) 249 return -1; 250 238 251 return numbytes; 239 252 } 240 253 241 static int duck_write_packet(libtrace_out_t *libtrace, 242 libtrace_packet_t *packet) 254 static int duck_write_packet(libtrace_out_t *libtrace, 255 libtrace_packet_t *packet) 243 256 { 244 257 … … 246 259 uint32_t duck_version; 247 260 248 if (packet->type != TRACE_RT_DUCK_2_4 261 if (packet->type != TRACE_RT_DUCK_2_4 249 262 && packet->type != TRACE_RT_DUCK_2_5 && 250 263 packet->type != TRACE_RT_DUCK_5_0) { … … 253 266 return -1; 254 267 } 255 256 assert(OUTPUT->file); 268 269 if (!OUTPUT->file) { 270 trace_set_err_out(libtrace, TRACE_ERR_BAD_IO, 271 "Attempted to write DUCK packets to a closed file, must call " 272 "trace_create_output() before calling trace_write_output()"); 273 return -1; 274 } 257 275 258 276 if (OUTPUT->dag_version == 0) { … … 261 279 if ((numbytes = wandio_wwrite(OUTPUT->file, &duck_version, 262 280 sizeof(duck_version))) != sizeof(uint32_t)){ 263 trace_set_err_out(libtrace, errno, 281 trace_set_err_out(libtrace, errno, 264 282 "Writing DUCK version failed"); 265 283 return -1; … … 267 285 OUTPUT->dag_version = packet->type; 268 286 } 269 270 if ((numbytes = wandio_wwrite(OUTPUT->file, packet->payload, 287 288 if ((numbytes = wandio_wwrite(OUTPUT->file, packet->payload, 271 289 trace_get_capture_length(packet))) != 272 290 (int)trace_get_capture_length(packet)) { -
lib/format_erf.c
rbccdffc r2d16fc7 35 35 #include "wandio.h" 36 36 37 #include <assert.h>38 37 #include <errno.h> 39 38 #include <fcntl.h> … … 120 119 } erf_index_t; 121 120 121 static bool erf_can_write(libtrace_packet_t *packet) { 122 /* Get the linktype */ 123 libtrace_linktype_t ltype = trace_get_link_type(packet); 124 125 if (ltype == TRACE_TYPE_CONTENT_INVALID) { 126 return false; 127 } 128 if (ltype == TRACE_TYPE_PCAPNG_META 129 || ltype == TRACE_TYPE_NONDATA) { 130 131 return false; 132 } 133 134 return true; 135 } 136 122 137 /* Ethernet packets have a 2 byte padding before the packet 123 138 * so that the IP header is aligned on a 32 bit boundary. … … 125 140 static inline int erf_get_padding(const libtrace_packet_t *packet) 126 141 { 127 if (packet->trace->format->type==TRACE_FORMAT_ERF || 128 packet->trace->format->type == TRACE_FORMAT_NDAG || 129 packet->trace->format->type == TRACE_FORMAT_RAWERF || 130 packet->trace->format->type == TRACE_FORMAT_DPDK_NDAG) { 131 dag_record_t *erfptr = (dag_record_t *)packet->header; 132 switch((erfptr->type & 0x7f)) { 133 case TYPE_ETH: 134 case TYPE_COLOR_ETH: 135 case TYPE_DSM_COLOR_ETH: 136 case TYPE_COLOR_HASH_ETH: 137 return 2; 138 default: return 0; 139 } 140 } 141 else { 142 switch(trace_get_link_type(packet)) { 143 case TRACE_TYPE_ETH: return 2; 144 default: return 0; 145 } 146 } 142 dag_record_t *erfptr = (dag_record_t *)packet->header; 143 144 switch(packet->trace->format->type) { 145 case TRACE_FORMAT_ERF: 146 case TRACE_FORMAT_NDAG: 147 case TRACE_FORMAT_RAWERF: 148 case TRACE_FORMAT_DPDK_NDAG: 149 switch((erfptr->type & 0x7f)) { 150 case TYPE_ETH: 151 case TYPE_COLOR_ETH: 152 case TYPE_DSM_COLOR_ETH: 153 case TYPE_COLOR_HASH_ETH: 154 return 2; 155 default: 156 return 0; 157 } 158 break; 159 default: 160 switch(trace_get_link_type(packet)) { 161 case TRACE_TYPE_ETH: return 2; 162 default: return 0; 163 } 164 break; 165 } 166 return 0; 147 167 } 148 168 … … 180 200 exthdr ++; 181 201 firstbyte = (uint8_t *)exthdr; 182 assert(extsize <= ntohs(erfptr->rlen)); 202 if (extsize > ntohs(erfptr->rlen)) { 203 trace_set_err(packet->trace, TRACE_ERR_BAD_PACKET, "Extension size is greater than dag record record length in erf_get_framing_length()"); 204 return -1; 205 } 183 206 } 184 207 } … … 225 248 } 226 249 227 static int erf_init_input(libtrace_t *libtrace) 228 { 250 static int erf_init_input(libtrace_t *libtrace) { 229 251 libtrace->format_data = malloc(sizeof(struct erf_format_data_t)); 230 252 253 if (!libtrace->format_data) { 254 trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, "Unable to allocate memory for " 255 "format data inside erf_init_input()"); 256 return -1; 257 } 258 231 259 IN_OPTIONS.real_time = 0; 232 260 DATA(libtrace)->drops = 0; 233 261 234 262 return 0; /* success */ 235 263 } … … 242 270 IN_OPTIONS.real_time = *(int *)value; 243 271 return 0; 272 case TRACE_OPTION_CONSTANT_ERF_FRAMING: 273 trace_set_err(libtrace, TRACE_ERR_OPTION_UNAVAIL, 274 "Setting constant framing length is not supported for %s:", libtrace->format->name); 275 return -1; 244 276 case TRACE_OPTION_SNAPLEN: 245 277 case TRACE_OPTION_PROMISC: … … 380 412 break; 381 413 case INDEX_UNKNOWN: 382 assert(0); 414 trace_set_err(libtrace, TRACE_ERR_SEEK_ERF, "Cannot seek to erf timestamp with unknown index in erf_seek_erf()"); 415 return -1; 383 416 break; 384 417 } … … 400 433 static int erf_init_output(libtrace_out_t *libtrace) { 401 434 libtrace->format_data = malloc(sizeof(struct erf_format_data_out_t)); 435 436 if (!libtrace->format_data) { 437 trace_set_err_out(libtrace, TRACE_ERR_INIT_FAILED, "Unable to allocate memory for " 438 "format data inside erf_init_output()"); 439 return -1; 440 } 402 441 403 442 OUT_OPTIONS.level = 0; … … 460 499 } else 461 500 packet->buf_control = TRACE_CTRL_EXTERNAL; 462 463 501 464 502 packet->type = rt_type; 465 503 packet->buffer = buffer; … … 472 510 } 473 511 474 assert(erfptr->rlen != 0); 475 512 if (erfptr->rlen == 0) { 513 trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, "ERF packet has an invalid record " 514 "length: zero, in erf_prepare_packet()\n"); 515 return -1; 516 } 517 476 518 if (libtrace->format_data == NULL) { 477 519 /* Allocates the format_data structure */ … … 644 686 libtrace_packet_t *packet) 645 687 { 688 689 /* Check erf can write this type of packet */ 690 if (!erf_can_write(packet)) { 691 return 0; 692 } 693 646 694 int numbytes = 0; 647 unsigned int pad = 0;648 695 dag_record_t *dag_hdr = (dag_record_t *)packet->header; 649 696 void *payload = packet->payload; 650 697 651 assert(OUTPUT->file); 652 653 if (trace_get_link_type(packet) == TRACE_TYPE_NONDATA) 654 return 0; 698 if (!OUTPUT->file) { 699 trace_set_err_out(libtrace, TRACE_ERR_BAD_IO, "Attempted to write ERF packets to a " 700 "closed file, must call trace_create_output() before calling trace_write_output()"); 701 return -1; 702 } 655 703 656 704 if (!packet->header) { … … 658 706 } 659 707 660 pad = erf_get_padding(packet);661 708 662 709 /* If we've had an rxerror, we have no payload to write - fix … … 666 713 * a way to write out the payload even if it is gibberish -- Perry */ 667 714 if (payload == NULL) { 715 unsigned int pad = 0; 716 pad = erf_get_padding(packet); 668 717 dag_hdr->rlen = htons(dag_record_size + pad); 669 718 … … 694 743 695 744 payload=packet->payload; 696 pad = erf_get_padding(packet);697 745 698 746 erfhdr.type = libtrace_to_erf_type(trace_get_link_type(packet)); 699 747 700 748 /* Packet length (rlen includes format overhead) */ 701 assert(trace_get_capture_length(packet)>0 702 && trace_get_capture_length(packet)<=65536); 703 assert(trace_get_framing_length(packet)<=65536); 704 749 if (trace_get_capture_length(packet) <= 0 750 || trace_get_capture_length(packet) > 65536) { 751 trace_set_err_out(libtrace, TRACE_ERR_BAD_PACKET, 752 "Capture length is out of range in erf_write_packet()"); 753 return -1; 754 } 755 if (trace_get_framing_length(packet) > 65536) { 756 trace_set_err_out(libtrace, TRACE_ERR_BAD_PACKET, 757 "Framing length is to large in erf_write_packet()"); 758 return -1; 759 } 760 705 761 if (erfhdr.type == TYPE_ETH) 706 762 framing = dag_record_size + 2; 707 763 else 708 764 framing = dag_record_size; 709 765 710 766 rlen = trace_get_capture_length(packet) + framing; 711 assert(rlen > 0 && rlen <= 65536); 767 if (rlen <= 0 || rlen > 65536) { 768 trace_set_err_out(libtrace, TRACE_ERR_BAD_PACKET, 769 "Capture + framing length is out of range in erf_write_packet()"); 770 return -1; 771 } 712 772 erfhdr.rlen = htons(rlen); 713 773 /* loss counter. Can't do this */ … … 729 789 dag_record_t *erfptr = 0; 730 790 erfptr = (dag_record_t *)packet->header; 731 uint8_t type = (erfptr->type & 0x7f); 791 uint8_t type; 792 793 if (packet->header == NULL) { 794 return ~0; 795 } 796 797 type = (erfptr->type & 0x7f); 732 798 if (type != TYPE_LEGACY) { 733 799 /* The top-most bit is now used to indicate the presence of … … 736 802 } 737 803 else { 804 if (trace_get_capture_length(packet) < 5 || 805 packet->payload == NULL) { 806 return ~0; 807 } 808 738 809 /* Sigh, lets start wildly guessing */ 739 810 if (((char*)packet->payload)[4]==0x45) … … 746 817 dag_record_t *erfptr = 0; 747 818 erfptr = (dag_record_t *)packet->header; 748 return erfptr->flags.iface; 819 if (packet->header) { 820 return erfptr->flags.iface; 821 } 822 return TRACE_DIR_UNKNOWN; 749 823 } 750 824 … … 752 826 dag_record_t *erfptr = 0; 753 827 erfptr = (dag_record_t *)packet->header; 828 829 if (packet->header == NULL) { 830 return TRACE_DIR_UNKNOWN; 831 } 754 832 erfptr->flags.iface = direction; 755 833 return erfptr->flags.iface; … … 759 837 dag_record_t *erfptr = 0; 760 838 erfptr = (dag_record_t *)packet->header; 839 840 if (erfptr == NULL) { 841 return 0; 842 } 761 843 return bswap_le_to_host64(erfptr->ts); 762 844 } … … 768 850 uint16_t wlen, rlen; 769 851 770 if (packet->payload == NULL )852 if (packet->payload == NULL || packet->header == NULL) 771 853 return 0; 772 854 … … 787 869 erfptr = (dag_record_t *)packet->header; 788 870 871 if (packet->header == NULL) { 872 return 0; 873 } 874 789 875 if ((erfptr->type & 0x7f) == TYPE_META) 790 876 return 0; … … 797 883 uint16_t wlen; 798 884 799 assert(packet); 885 if (!packet) { 886 fprintf(stderr, "NULL packet passed to erf_set_capture_length()\n"); 887 return ~0U; 888 } 800 889 erfptr = (dag_record_t *)packet->header; 890 891 if (packet->header == NULL) { 892 return ~0U; 893 } 801 894 802 895 if(size > trace_get_capture_length(packet) || (erfptr->type & 0x7f) == TYPE_META) { … … 808 901 * wrong value here and subsequent get_capture_length() calls will 809 902 * return the wrong value. */ 810 packet->ca pture_length = -1;903 packet->cached.capture_length = -1; 811 904 erfptr->rlen = htons(size + trace_get_framing_length(packet)); 812 905 wlen = ntohs(erfptr->wlen); -
lib/format_etsilive.c
r2044185 rb359a11 37 37 #include <libwandder_etsili.h> 38 38 39 #include <assert.h>40 39 #include <errno.h> 41 40 #include <fcntl.h> … … 72 71 etsisocket_t *sources; 73 72 uint16_t sourcecount; 73 uint16_t sourcealloc; 74 uint16_t activesources; 74 75 int threadindex; 75 76 wandder_etsispec_t *etsidec; … … 213 214 sizeof(etsilive_format_data_t)); 214 215 216 if (!libtrace->format) { 217 trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, "Unable to allocate memory for " 218 "format data inside etsilive_init_input()"); 219 return 1; 220 } 221 215 222 FORMAT_DATA->receivers = NULL; 216 223 FORMAT_DATA->nextthreadid = 0; … … 269 276 270 277 FORMAT_DATA->receivers[i].sources = NULL; 278 FORMAT_DATA->receivers[i].sourcealloc = 0; 271 279 FORMAT_DATA->receivers[i].sourcecount = 0; 280 FORMAT_DATA->receivers[i].activesources = 0; 272 281 FORMAT_DATA->receivers[i].threadindex = i; 273 282 FORMAT_DATA->receivers[i].etsidec = … … 326 335 != LIBTRACE_MQ_FAILED) { 327 336 etsisocket_t *esock = NULL; 337 int i; 328 338 329 339 if (et->sourcecount == 0) { 330 340 et->sources = (etsisocket_t *)malloc( 331 341 sizeof(etsisocket_t) * 10); 332 } else if ((et->sourcecount % 10) == 0) { 342 et->sourcealloc = 10; 343 344 for (i = 0; i < et->sourcealloc; i++) { 345 et->sources[i].sock = -1; 346 et->sources[i].srcaddr = NULL; 347 } 348 349 esock = &(et->sources[0]); 350 et->sourcecount = 1; 351 } else { 352 for (i = 0; i < et->sourcealloc; i++) { 353 if (et->sources[i].sock == -1) { 354 esock = &(et->sources[i]); 355 break; 356 } 357 } 358 } 359 360 if (esock == NULL) { 333 361 et->sources = (etsisocket_t *)realloc(et->sources, 334 sizeof(etsisocket_t) * (et->sourcecount + 10)); 335 } 336 337 esock = &(et->sources[et->sourcecount]); 362 sizeof(etsisocket_t) * (et->sourcealloc + 10)); 363 364 for (i = et->sourcealloc; i < et->sourcealloc + 10; 365 i++) { 366 et->sources[i].sock = -1; 367 et->sources[i].srcaddr = NULL; 368 } 369 esock = &(et->sources[et->sourcealloc]); 370 et->sourcealloc += 10; 371 et->sourcecount += 1; 372 373 } 374 338 375 esock->sock = msg.recvsock; 339 376 esock->srcaddr = msg.recvaddr; … … 343 380 esock->cached.length = 0; 344 381 345 et-> sourcecount+= 1;382 et->activesources += 1; 346 383 347 384 fprintf(stderr, "Thread %d is now handling %u sources.\n", 348 et->threadindex, et-> sourcecount);385 et->threadindex, et->activesources); 349 386 } 350 387 return 1; 351 388 } 352 389 353 static void receive_from_single_socket(etsisocket_t *esock ) {390 static void receive_from_single_socket(etsisocket_t *esock, etsithread_t *et) { 354 391 355 392 int ret = 0; … … 361 398 ret = libtrace_scb_recv_sock(&(esock->recvbuffer), esock->sock, 362 399 MSG_DONTWAIT); 363 if (ret == -1) {400 if (ret < 0) { 364 401 if (errno == EAGAIN || errno == EWOULDBLOCK) { 365 402 /* Would have blocked, nothing available */ … … 370 407 close(esock->sock); 371 408 esock->sock = -1; 409 et->activesources -= 1; 372 410 } 373 411 … … 376 414 close(esock->sock); 377 415 esock->sock = -1; 416 et->activesources -= 1; 378 417 } 379 418 … … 394 433 } 395 434 396 if (et-> sourcecount== 0) {435 if (et->activesources == 0) { 397 436 return 1; 398 437 } 399 438 400 439 for (i = 0; i < et->sourcecount; i++) { 401 receive_from_single_socket(&(et->sources[i]) );440 receive_from_single_socket(&(et->sources[i]), et); 402 441 } 403 442 return 1; … … 407 446 static inline void inspect_next_packet(etsisocket_t *sock, 408 447 etsisocket_t **earliestsock, uint64_t *earliesttime, 409 wandder_etsispec_t *dec ) {448 wandder_etsispec_t *dec, etsithread_t *et) { 410 449 411 450 … … 460 499 close(sock->sock); 461 500 sock->sock = -1; 501 et->activesources -= 1; 462 502 return; 463 503 } … … 467 507 close(sock->sock); 468 508 sock->sock = -1; 509 et->activesources -= 1; 469 510 return; 470 511 } … … 505 546 for (i = 0; i < et->sourcecount; i++) { 506 547 inspect_next_packet(&(et->sources[i]), &esock, &earliest, 507 et->etsidec );548 et->etsidec, et); 508 549 } 509 550 return esock; … … 526 567 packet->error = esock->cached.length; 527 568 528 packet-> wire_length = esock->cached.length;529 packet->ca pture_length = esock->cached.length;569 packet->cached.wire_length = esock->cached.length; 570 packet->cached.capture_length = esock->cached.length; 530 571 531 572 /* Advance the read pointer for this buffer … … 590 631 libtrace_t *libtrace = packet->trace; 591 632 592 assert(libtrace); 593 assert(FORMAT_DATA->shareddec); 633 if (!libtrace) { 634 fprintf(stderr, "Packet is not associated with a trace in etsilive_get_pdu_length()\n"); 635 return TRACE_ERR_NULL_TRACE; 636 } 637 if (!FORMAT_DATA->shareddec) { 638 trace_set_err(libtrace, TRACE_ERR_BAD_FORMAT, "Etsilive format data shareddec is NULL in etsilive_get_pdu_length()\n"); 639 return -1; 640 } 594 641 595 642 /* 0 should be ok here for quickly evaluating the first length -
lib/format_helper.c
rde060a8 rd7d3267 39 39 #include "format_helper.h" 40 40 41 #include <assert.h>42 41 #include <stdarg.h> 43 42 … … 69 68 struct timeval tv; 70 69 71 assert(trace != NULL); 72 assert(packet != NULL); 73 70 if (!trace) { 71 fprintf(stderr, "NULL trace passed into trace_event_device()\n"); 72 event.type = TRACE_EVENT_TERMINATE; 73 return event; 74 } 75 if (!packet) { 76 trace_set_err(trace, TRACE_ERR_NULL_PACKET, "NULL packet passed into trace_event_device()"); 77 event.type = TRACE_EVENT_TERMINATE; 78 return event; 79 } 80 74 81 FD_ZERO(&rfds); 75 82 FD_ZERO(&rfds_param); … … 220 227 packet->buf_control = trace->event.packet->buf_control; 221 228 229 packet->which_trace_start = trace->event.packet->which_trace_start; 230 222 231 event.type = TRACE_EVENT_PACKET; 223 232 … … 290 299 va_list va; 291 300 va_start(va,msg); 292 assert(errcode != 0 && "An error occurred, but it is unknown what it is"); 301 302 if (errcode == 0) { 303 fprintf(stderr, "An error occurred, but it is unknown what it is"); 304 return; 305 } 306 293 307 trace->err.err_num=errcode; 294 308 if (errcode>0) { … … 313 327 va_list va; 314 328 va_start(va,msg); 315 assert(errcode != 0 && "An error occurred, but it is unknown what it is"); 329 if (errcode == 0) { 330 fprintf(stderr, "An error occurred, but is is unknown what is is"); 331 return; 332 } 316 333 trace->err.err_num=errcode; 317 334 if (errcode>0) { -
lib/format_legacy.c
r32ee9b2 r2193905 34 34 35 35 #include <sys/stat.h> 36 #include <assert.h>37 36 #include <errno.h> 38 37 #include <fcntl.h> … … 258 257 break; 259 258 default: 260 assert(0); 259 trace_set_err(libtrace, TRACE_ERR_BAD_FORMAT, "Invalid trace format type in legacy_read_packet()"); 260 return -1; 261 261 } 262 262 … … 392 392 static int legacypos_get_wire_length(const libtrace_packet_t *packet) { 393 393 legacy_pos_t *lpos = (legacy_pos_t *)packet->header; 394 assert(ntohl(lpos->wlen)>0); 394 395 if (ntohl(lpos->wlen) <= 0) { 396 trace_set_err(packet->trace, TRACE_ERR_BAD_PACKET, "Packet wire length is invalid (%d) " 397 "in legacypos_get_wire_length()", ntohl(lpos->wlen)); 398 return -1; 399 } 395 400 return ntohl(lpos->wlen); 396 401 } -
lib/format_linux_common.c
rebed638 r5fe998b 40 40 #include <unistd.h> 41 41 #include <string.h> 42 #include <assert.h>43 42 44 43 #ifdef HAVE_INTTYPES_H … … 71 70 pcap_t *pcap; 72 71 73 /* Take a copy of the filter object as it was passed in */ 74 f = (libtrace_filter_t *) malloc(sizeof(libtrace_filter_t)); 75 memcpy(f, filter, sizeof(libtrace_filter_t)); 72 /* Take a copy of the filter structure to prevent against 73 * deletion causing the filter to no longer work */ 74 f = (libtrace_filter_t *) malloc(sizeof(libtrace_filter_t)); 75 memset(f, 0, sizeof(libtrace_filter_t)); 76 memcpy(f, filter, sizeof(libtrace_filter_t)); 77 f->filterstring = strdup(filter->filterstring); 76 78 77 79 /* If we are passed a filter with "flag" set to zero, then we must … … 121 123 122 124 if (FORMAT_DATA->filter != NULL) 123 free(FORMAT_DATA->filter);125 trace_destroy_filter(FORMAT_DATA->filter); 124 126 125 127 FORMAT_DATA->filter = f; … … 168 170 case TRACE_OPTION_REPLAY_SPEEDUP: 169 171 break; 172 case TRACE_OPTION_CONSTANT_ERF_FRAMING: 173 break; 170 174 /* Avoid default: so that future options will cause a warning 171 175 * here to remind us to implement it, or flag it as … … 185 189 libtrace->format_data = (struct linux_format_data_t *) 186 190 malloc(sizeof(struct linux_format_data_t)); 187 assert(libtrace->format_data != NULL); 191 192 if (!libtrace->format_data) { 193 trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, "Unable to allocate memory for " 194 "format data inside linuxcommon_init_input()"); 195 return -1; 196 } 188 197 189 198 FORMAT_DATA->per_stream = 190 199 libtrace_list_init(sizeof(stream_data)); 191 assert(FORMAT_DATA->per_stream != NULL); 200 201 if (!FORMAT_DATA->per_stream) { 202 trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, "Unable to create list for stream data linuxcommon_init_input()"); 203 return -1; 204 } 192 205 193 206 libtrace_list_push_back(FORMAT_DATA->per_stream, &stream_data); … … 211 224 libtrace->format_data = (struct linux_format_data_out_t*) 212 225 malloc(sizeof(struct linux_format_data_out_t)); 213 assert(libtrace->format_data != NULL); 226 227 if (!libtrace->format_data) { 228 trace_set_err_out(libtrace, TRACE_ERR_INIT_FAILED, "Unable to allocate memory for " 229 "format data inside linuxcommon_init_output()"); 230 return -1; 231 } 214 232 215 233 FORMAT_DATA_OUT->fd = -1; … … 483 501 if (libtrace->format_data) { 484 502 if (FORMAT_DATA->filter != NULL) 485 free(FORMAT_DATA->filter);503 trace_destroy_filter(FORMAT_DATA->filter); 486 504 487 505 if (FORMAT_DATA->per_stream) -
lib/format_linux_int.c
r32ee9b2 re9fe6ac 44 44 #include <unistd.h> 45 45 #include <string.h> 46 #include <assert.h>47 46 48 47 #ifdef HAVE_INTTYPES_H … … 54 53 #include "format_linux_common.h" 55 54 55 #define SLL_HEADER_LENGTH 6 56 56 57 57 #ifdef HAVE_NETPACKET_PACKET_H 58 58 59 static bool linuxnative_can_write(libtrace_packet_t *packet) { 60 /* Get the linktype */ 61 libtrace_linktype_t ltype = trace_get_link_type(packet); 62 63 if (ltype == TRACE_TYPE_NONDATA) { 64 return false; 65 } 66 if (ltype == TRACE_TYPE_CONTENT_INVALID) { 67 return false; 68 } 69 if (ltype == TRACE_TYPE_PCAPNG_META) { 70 return false; 71 } 72 if (ltype == TRACE_TYPE_ERF_META) { 73 return false; 74 } 75 76 return true; 77 } 59 78 60 79 static int linuxnative_start_input(libtrace_t *libtrace) … … 337 356 libtrace_packet_t *packet) 338 357 { 358 /* Check linuxnative can write this type of packet */ 359 if (!linuxnative_can_write(packet)) { 360 return 0; 361 } 362 339 363 struct sockaddr_ll hdr; 340 364 int ret = 0; 341 342 if (trace_get_link_type(packet) == TRACE_TYPE_NONDATA)343 return 0;344 365 345 366 hdr.sll_family = AF_PACKET; … … 348 369 hdr.sll_hatype = 0; 349 370 hdr.sll_pkttype = 0; 350 hdr.sll_halen = htons( 6); /* FIXME */351 memcpy(hdr.sll_addr,packet->payload,(size_t) ntohs(hdr.sll_halen));371 hdr.sll_halen = htons(SLL_HEADER_LENGTH); /* FIXME */ 372 memcpy(hdr.sll_addr,packet->payload,(size_t)SLL_HEADER_LENGTH); 352 373 353 374 /* This is pretty easy, just send the payload using sendto() (after … … 451 472 452 473 struct libtrace_linuxnative_header *linux_hdr = NULL; 453 assert(packet); 474 if (!packet) { 475 fprintf(stderr, "NULL packet passed into linuxnative_set_capture_length()\n"); 476 /* Return -1 on error? */ 477 return ~0U; 478 } 454 479 if (size > trace_get_capture_length(packet)) { 455 480 /* We should avoid making a packet larger */ … … 458 483 459 484 /* Reset the cached capture length */ 460 packet->ca pture_length = -1;485 packet->cached.capture_length = -1; 461 486 462 487 linux_hdr = (struct libtrace_linuxnative_header *)packet->header; -
lib/format_linux_ring.c
rebed638 rfce4572 45 45 #include <unistd.h> 46 46 #include <string.h> 47 #include <assert.h>48 47 49 48 #ifdef HAVE_INTTYPES_H … … 72 71 static int pagesize = 0; 73 72 73 static bool linuxring_can_write(libtrace_packet_t *packet) { 74 /* Get the linktype */ 75 libtrace_linktype_t ltype = trace_get_link_type(packet); 76 77 if (ltype == TRACE_TYPE_CONTENT_INVALID) { 78 return false; 79 } 80 if (ltype == TRACE_TYPE_NONDATA) { 81 return false; 82 } 83 if (ltype == TRACE_TYPE_PCAPNG_META) { 84 return false; 85 } 86 if (ltype == TRACE_TYPE_ERF_META) { 87 return false; 88 } 89 90 return true; 91 } 74 92 75 93 /* … … 149 167 150 168 /* In case we have some silly values*/ 151 assert(req->tp_block_size); 152 assert(req->tp_block_nr); 153 assert(req->tp_frame_size); 154 assert(req->tp_frame_nr); 155 assert(req->tp_block_size % req->tp_frame_size == 0); 169 if (!req->tp_block_size) { 170 fprintf(stderr, "Unexpected value of zero for req->tp_block_size in calculate_buffers()\n"); 171 } 172 if (!req->tp_block_nr) { 173 fprintf(stderr, "Unexpected value of zero for req->tp_block_nr in calculate_buffers()\n"); 174 } 175 if (!req->tp_frame_size) { 176 fprintf(stderr, "Unexpected value of zero for req->tp_frame_size in calculate_buffers()\n"); 177 } 178 if (!req->tp_frame_nr) { 179 fprintf(stderr, "Unexpected value of zero for req->tp_frame_nr in calculate_buffers()\n"); 180 } 181 if (req->tp_block_size % req->tp_frame_size != 0) { 182 fprintf(stderr, "Unexpected value of zero for req->tp_block_size %% req->tp_frame_size in calculate_buffers()\n"); 183 } 156 184 } 157 185 … … 299 327 } 300 328 301 302 free(FORMAT_DATA->filter);329 if (FORMAT_DATA->filter != NULL) 330 trace_destroy_filter(FORMAT_DATA->filter); 303 331 304 332 if (FORMAT_DATA->per_stream) … … 448 476 size_t size) 449 477 { 450 assert(packet); 478 if (!packet) { 479 fprintf(stderr, "NULL packet passed into linuxring_set_capture_length()\n"); 480 /* Return -1 on error? */ 481 return ~0U; 482 } 451 483 if (size > trace_get_capture_length(packet)) { 452 484 /* We should avoid making a packet larger */ … … 455 487 456 488 /* Reset the cached capture length */ 457 packet->ca pture_length = -1;489 packet->cached.capture_length = -1; 458 490 459 491 TO_TP_HDR2(packet->buffer)->tp_snaplen = size; … … 497 529 libtrace_packet_t *packet, 498 530 struct linux_per_stream_t *stream, 499 libtrace_message_queue_t *queue) { 531 libtrace_message_queue_t *queue, 532 uint8_t block) { 500 533 501 534 struct tpacket2_hdr *header; … … 506 539 packet->buf_control = TRACE_CTRL_EXTERNAL; 507 540 packet->type = TRACE_RT_DATA_LINUX_RING; 508 541 509 542 /* Fetch the current frame */ 510 543 header = GET_CURRENT_BUFFER(stream); 511 assert((((unsigned long) header) & (pagesize - 1)) == 0); 544 if ((((unsigned long) header) & (pagesize - 1)) != 0) { 545 trace_set_err(libtrace, TRACE_ERR_BAD_IO, "Linux ring packet is not correctly " 546 "aligned to page size in linux_read_string()"); 547 return -1; 548 } 512 549 513 550 /* TP_STATUS_USER means that we can use the frame. … … 516 553 */ 517 554 while (!(header->tp_status & TP_STATUS_USER) || 518 header->tp_status == TP_STATUS_LIBTRACE) { 519 if ((ret=is_halted(libtrace)) != -1) 520 return ret; 555 header->tp_status == TP_STATUS_LIBTRACE) { 556 if ((ret=is_halted(libtrace)) != -1) 557 return ret; 558 if (!block) { 559 return 0; 560 } 561 521 562 pollset[0].fd = stream->fd; 522 563 pollset[0].events = POLLIN; … … 600 641 601 642 static int linuxring_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { 602 return linuxring_read_stream(libtrace, packet, FORMAT_DATA_FIRST, NULL );643 return linuxring_read_stream(libtrace, packet, FORMAT_DATA_FIRST, NULL, 1); 603 644 } 604 645 … … 607 648 libtrace_thread_t *t, 608 649 libtrace_packet_t *packets[], 609 UNUSED size_t nb_packets) { 610 /* For now just read one packet */ 611 packets[0]->error = linuxring_read_stream(libtrace, packets[0], 612 t->format_data, &t->messages); 613 if (packets[0]->error >= 1) 614 return 1; 615 else 616 return packets[0]->error; 650 size_t nb_packets) { 651 size_t i; 652 int ret; 653 654 for (i = 0; i < nb_packets; i++) { 655 ret = linuxring_read_stream(libtrace, packets[i], 656 t->format_data, &t->messages, i == 0 ? 1 : 0); 657 packets[i]->error = ret; 658 if (ret < 0) { 659 return ret; 660 } 661 662 if (ret == 0) { 663 if (is_halted(libtrace) == READ_EOF) { 664 return READ_EOF; 665 } 666 return i; 667 } 668 } 669 670 return nb_packets; 617 671 } 618 672 #endif … … 655 709 if (packet->buffer == NULL) 656 710 return; 657 assert(packet->trace); 711 if (!packet->trace) { 712 fprintf(stderr, "Linux ring packet is not attached to a valid " 713 "trace, Unable to release it, in linuxring_fin_packet()\n"); 714 return; 715 } 658 716 659 717 /* If we own the packet (i.e. it's not a copy), we need to free it */ … … 670 728 libtrace_packet_t *packet) 671 729 { 730 /* Check linuxring can write this type of packet */ 731 if (!linuxring_can_write(packet)) { 732 return 0; 733 } 734 672 735 struct tpacket2_hdr *header; 673 736 struct pollfd pollset; … … 676 739 unsigned max_size; 677 740 void * off; 678 679 if (trace_get_link_type(packet) == TRACE_TYPE_NONDATA)680 return 0;681 741 682 742 max_size = FORMAT_DATA_OUT->req.tp_frame_size - -
lib/format_ndag.c
r857729e r6514097 35 35 #include "format_erf.h" 36 36 37 #include <assert.h>38 37 #include <errno.h> 39 38 #include <fcntl.h> … … 126 125 pthread_t controlthread; 127 126 libtrace_message_queue_t controlqueue; 127 int consterfframing; 128 128 } ndag_format_data_t; 129 129 … … 286 286 sizeof(ndag_format_data_t)); 287 287 288 if (!libtrace->format_data) { 289 trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, "Unable to allocate memory for " 290 "format data inside ndag_init_input()"); 291 return -1; 292 } 293 288 294 FORMAT_DATA->multicastgroup = NULL; 289 295 FORMAT_DATA->portstr = NULL; … … 291 297 FORMAT_DATA->nextthreadid = 0; 292 298 FORMAT_DATA->receivers = NULL; 299 FORMAT_DATA->consterfframing = -1; 293 300 294 301 scan = strchr(libtrace->uridata, ','); … … 311 318 FORMAT_DATA->portstr = strdup(scan + 1); 312 319 } 320 return 0; 321 } 322 323 static int ndag_config_input(libtrace_t *libtrace, trace_option_t option, 324 void *value) { 325 326 switch(option) { 327 case TRACE_OPTION_CONSTANT_ERF_FRAMING: 328 FORMAT_DATA->consterfframing = *(int *)value; 329 break; 330 case TRACE_OPTION_EVENT_REALTIME: 331 case TRACE_OPTION_SNAPLEN: 332 case TRACE_OPTION_PROMISC: 333 case TRACE_OPTION_FILTER: 334 case TRACE_OPTION_META_FREQ: 335 default: 336 trace_set_err(libtrace, TRACE_ERR_OPTION_UNAVAIL, 337 "Unsupported option %d", 338 option); 339 return -1; 340 } 341 313 342 return 0; 314 343 } … … 592 621 } 593 622 594 static int ndag_prepare_packet_stream(libtrace_t *libtrace, 595 recvstream_t *rt, 596 streamsock_t *ssock, libtrace_packet_t *packet, 597 uint32_t flags) { 623 static int ndag_get_framing_length(const libtrace_packet_t *packet) { 624 625 libtrace_t *libtrace = packet->trace; 626 627 if (FORMAT_DATA->consterfframing >= 0) { 628 return FORMAT_DATA->consterfframing; 629 } 630 return erf_get_framing_length(packet); 631 } 632 633 static int ndag_prepare_packet_stream(libtrace_t *restrict libtrace, 634 recvstream_t *restrict rt, 635 streamsock_t *restrict ssock, 636 libtrace_packet_t *restrict packet, 637 uint32_t flags UNUSED) { 638 639 /* XXX flags is constant, so we can tell the compiler to not 640 * bother copying over the parameter 641 */ 598 642 599 643 dag_record_t *erfptr; … … 603 647 uint16_t rlen; 604 648 649 /* 605 650 if ((flags & TRACE_PREP_OWN_BUFFER) == TRACE_PREP_OWN_BUFFER) { 606 651 packet->buf_control = TRACE_CTRL_PACKET; … … 608 653 packet->buf_control = TRACE_CTRL_EXTERNAL; 609 654 } 655 */ 656 packet->buf_control = TRACE_CTRL_EXTERNAL; 610 657 611 658 packet->trace = libtrace; … … 618 665 if (erfptr->flags.rxerror == 1) { 619 666 packet->payload = NULL; 620 erfptr->rlen = htons(erf_get_framing_length(packet)); 667 if (FORMAT_DATA->consterfframing >= 0) { 668 erfptr->rlen = htons(FORMAT_DATA->consterfframing & 0xffff); 669 } else { 670 erfptr->rlen = htons(erf_get_framing_length(packet)); 671 } 621 672 } else { 622 packet->payload = (char *)packet->buffer + 673 if (FORMAT_DATA->consterfframing >= 0) { 674 packet->payload = (char *)packet->buffer + 675 FORMAT_DATA->consterfframing; 676 } else { 677 packet->payload = (char *)packet->buffer + 623 678 erf_get_framing_length(packet); 679 } 624 680 } 625 681 … … 653 709 ssock->nextts = 0; 654 710 655 assert(ssock->nextread - ssock->saved[nr] <= ssock->savedsize[nr]); 711 if (ssock->nextread - ssock->saved[nr] > ssock->savedsize[nr]) { 712 trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, "Walked past the end of the " 713 "nDAG receive buffer, probably due to a invalid rlen, in ndag_prepare_packet_stream()"); 714 return -1; 715 } 656 716 657 717 if (ssock->nextread - ssock->saved[nr] >= ssock->savedsize[nr]) { … … 680 740 uint32_t flags UNUSED) { 681 741 682 assert(0 && "Sending nDAG records over RT doesn't make sense! Please stop.");742 fprintf(stderr, "Sending nDAG records over RT doesn't make sense! Please stop\n"); 683 743 return 0; 684 744 … … 863 923 } 864 924 #else 865 assert(required > 0); 925 if (required <= 0) { 926 fprintf(stderr, "You are required to have atleast 1 receiver in init_receivers\n"); 927 return TRACE_ERR_INIT_FAILED; 928 } 866 929 ssock->singlemsg.msg_iov->iov_base = ssock->saved[wind]; 867 930 ssock->singlemsg.msg_iov->iov_len = ENCAP_BUFSIZE; … … 898 961 ssock->bufavail --; 899 962 900 assert(ssock->bufavail >= 0); 963 if (ssock->bufavail < 0) { 964 fprintf(stderr, "No space in buffer in check_ndag_received()\n"); 965 return -1; 966 } 901 967 if (ssock->nextwriteind >= ENCAP_BUFFERS) { 902 968 ssock->nextwriteind = 0; … … 1035 1101 gottime = 0; 1036 1102 1037 FD_ZERO(&fds);1038 1039 1103 if (rt->maxfd == -1) { 1040 1104 return 0; 1041 1105 } 1042 1043 zerotv.tv_sec = 0;1044 zerotv.tv_usec = 0;1045 1106 1046 1107 for (i = 0; i < rt->sourcecount; i++) { … … 1061 1122 } 1062 1123 #endif 1124 if (maxfd == 0) { 1125 FD_ZERO(&fds); 1126 } 1063 1127 FD_SET(rt->sources[i].sock, &fds); 1064 1128 if (maxfd < rt->sources[i].sock) { … … 1072 1136 } 1073 1137 1138 zerotv.tv_sec = 0; 1139 zerotv.tv_usec = 0; 1074 1140 if (select(maxfd + 1, &fds, NULL, NULL, &zerotv) == -1) { 1075 1141 /* log the error? XXX */ … … 1243 1309 rt = (recvstream_t *)t->format_data; 1244 1310 1245 1246 1311 do { 1247 1312 /* Only check for messages once per batch */ … … 1249 1314 rem = receive_encap_records_block(libtrace, rt, 1250 1315 packets[read_packets]); 1251 } else { 1252 rem = receive_encap_records_nonblock(libtrace, rt, 1253 packets[read_packets]); 1254 } 1255 1256 if (rem < 0) { 1257 return rem; 1258 } 1259 1260 if (rem == 0) { 1261 break; 1316 if (rem < 0) { 1317 return rem; 1318 } 1319 1320 if (rem == 0) { 1321 break; 1322 } 1262 1323 } 1263 1324 … … 1281 1342 src->bufavail += src->bufwaiting; 1282 1343 src->bufwaiting = 0; 1283 assert(src->bufavail >= 0 && src->bufavail <= ENCAP_BUFFERS); 1344 if (src->bufavail < 0 || src->bufavail > ENCAP_BUFFERS) { 1345 trace_set_err(libtrace, TRACE_ERR_BAD_IO, "Not enough buffer space in ndag_pread_packets()"); 1346 return -1; 1347 } 1284 1348 } 1285 1349 … … 1370 1434 src->bufavail += src->bufwaiting; 1371 1435 src->bufwaiting = 0; 1372 assert(src->bufavail >= 0 && src->bufavail <= ENCAP_BUFFERS); 1436 if (src->bufavail < 0 || src->bufavail > ENCAP_BUFFERS) { 1437 trace_set_err(libtrace, TRACE_ERR_BAD_IO, "Not enough buffer space in trace_event_ndag()"); 1438 break; 1439 } 1373 1440 } 1374 1441 … … 1437 1504 NULL, /* probe magic */ 1438 1505 ndag_init_input, /* init_input */ 1439 NULL,/* config_input */1506 ndag_config_input, /* config_input */ 1440 1507 ndag_start_input, /* start_input */ 1441 1508 ndag_pause_input, /* pause_input */ … … 1462 1529 erf_get_capture_length, /* get_capture_length */ 1463 1530 erf_get_wire_length, /* get_wire_length */ 1464 erf_get_framing_length, /* get_framing_length */1531 ndag_get_framing_length, /* get_framing_length */ 1465 1532 erf_set_capture_length, /* set_capture_length */ 1466 1533 NULL, /* get_received_packets */ -
lib/format_pcap.c
r37ee856 r9a6bdbc 31 31 32 32 #include <sys/stat.h> 33 #include <assert.h>34 33 #include <stdio.h> 35 34 #include <stdlib.h> … … 99 98 }; 100 99 100 static bool pcap_can_write(libtrace_packet_t *packet) { 101 /* Get the linktype */ 102 libtrace_linktype_t ltype = trace_get_link_type(packet); 103 104 if (ltype == TRACE_TYPE_PCAPNG_META 105 || ltype == TRACE_TYPE_CONTENT_INVALID 106 || ltype == TRACE_TYPE_UNKNOWN 107 || ltype == TRACE_TYPE_ERF_META 108 || ltype == TRACE_TYPE_NONDATA) { 109 110 return false; 111 } 112 113 return true; 114 } 115 101 116 static int pcap_init_input(libtrace_t *libtrace) { 102 117 libtrace->format_data = malloc(sizeof(struct pcap_format_data_t)); 118 119 if (!libtrace->format_data) { 120 trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, "Unable to allocate memory for " 121 "format data inside pcap_init_input()"); 122 return -1; 123 } 103 124 104 125 INPUT.pcap = NULL; … … 173 194 return -1; 174 195 } 175 assert(0);196 return -1; 176 197 } 177 198 178 199 static int pcap_init_output(libtrace_out_t *libtrace) { 179 200 libtrace->format_data = malloc(sizeof(struct pcap_format_data_out_t)); 201 if (!libtrace->format_data) { 202 trace_set_err_out(libtrace, TRACE_ERR_INIT_FAILED, "Unable to allocate memory for " 203 "format data inside pcap_init_output()"); 204 return -1; 205 } 206 180 207 OUTPUT.trace.pcap = NULL; 181 208 OUTPUT.trace.dump = NULL; … … 186 213 #ifdef HAVE_PCAP_INJECT 187 214 libtrace->format_data = malloc(sizeof(struct pcap_format_data_out_t)); 215 if (!libtrace->format_data) { 216 trace_set_err_out(libtrace, TRACE_ERR_INIT_FAILED, "Unable to allocate memory for " 217 "format data inside pcapint_init_output()"); 218 return -1; 219 } 220 188 221 OUTPUT.trace.pcap = NULL; 189 222 OUTPUT.trace.dump = NULL; … … 205 238 static int pcapint_init_input(libtrace_t *libtrace) { 206 239 libtrace->format_data = malloc(sizeof(struct pcap_format_data_t)); 240 if (!libtrace->format_data) { 241 trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, "Unable to allocate memory for " 242 "format data inside pcapint_init_input()"); 243 return -1; 244 } 245 207 246 DATA(libtrace)->filter = NULL; 208 247 DATA(libtrace)->snaplen = LIBTRACE_PACKET_BUFSIZE; … … 240 279 return -1; 241 280 } 242 assert(0);281 return -1; 243 282 } 244 283 … … 377 416 pcap_dump_close(OUTPUT.trace.dump); 378 417 } 379 pcap_close(OUTPUT.trace.pcap); 418 if (OUTPUT.trace.pcap) { 419 pcap_close(OUTPUT.trace.pcap); 420 } 380 421 free(libtrace->format_data); 381 422 return 0; … … 423 464 int linktype; 424 465 uint32_t flags = 0; 425 426 assert(libtrace->format_data); 466 467 if (!libtrace->format_data) { 468 trace_set_err(libtrace, TRACE_ERR_BAD_FORMAT, "Trace format data missing, " 469 "call trace_create() before calling pcap_read_packet()"); 470 return -1; 471 } 427 472 linktype = pcap_datalink(DATA(libtrace)->input.pcap); 428 473 packet->type = pcap_linktype_to_rt(linktype); … … 491 536 libtrace_packet_t *packet) 492 537 { 538 539 /* Check pcap can write this type of packet */ 540 if (!pcap_can_write(packet)) { 541 return 0; 542 } 543 544 if (!libtrace) { 545 fprintf(stderr, "NULL trace passed into pcap_write_packet()\n"); 546 return TRACE_ERR_NULL_TRACE; 547 } 548 if (!packet) { 549 trace_set_err_out(libtrace, TRACE_ERR_NULL_PACKET, "NULL packet passed into pcap_write_packet()\n"); 550 return -1; 551 } 552 493 553 struct pcap_pkthdr pcap_pkt_hdr; 494 554 void *link; … … 497 557 498 558 link = trace_get_packet_buffer(packet,&linktype,&remaining); 499 500 /* Silently discard RT metadata packets and packets with an501 * unknown linktype. */502 if (linktype == TRACE_TYPE_NONDATA || linktype == TRACE_TYPE_UNKNOWN || linktype == TRACE_TYPE_ERF_META || linktype == TRACE_TYPE_CONTENT_INVALID) {503 return 0;504 }505 559 506 560 /* We may have to convert this packet into a suitable PCAP packet */ … … 575 629 pcap_pkt_hdr.len = trace_get_wire_length(packet); 576 630 577 assert(pcap_pkt_hdr.caplen<65536); 578 assert(pcap_pkt_hdr.len<65536); 631 if (pcap_pkt_hdr.caplen >= 65536) { 632 trace_set_err_out(libtrace, TRACE_ERR_BAD_HEADER, "Header capture length is larger than it should be in pcap_write_packet()"); 633 return -1; 634 } 635 if (pcap_pkt_hdr.len >= 65536) { 636 trace_set_err_out(libtrace, TRACE_ERR_BAD_HEADER, "Header wire length is larger than it should be pcap_write_packet()"); 637 return -1; 638 } 579 639 580 640 pcap_dump((u_char*)OUTPUT.trace.dump, &pcap_pkt_hdr, packet->payload); … … 670 730 671 731 static int pcap_get_capture_length(const libtrace_packet_t *packet) { 732 if (!packet) { 733 fprintf(stderr, "NULL packet passed into pcapng_get_capture_length()\n"); 734 return TRACE_ERR_NULL_PACKET; 735 } 672 736 struct pcap_pkthdr *pcapptr = 0; 673 737 pcapptr = (struct pcap_pkthdr *)packet->header; 674 assert(pcapptr->caplen<=65536); 738 if (pcapptr->caplen > 65536) { 739 trace_set_err(packet->trace, TRACE_ERR_BAD_PACKET, "Capture length is to large, Packet may be corrupt in pcap_get_capture_length()"); 740 return -1; 741 } 675 742 676 743 return pcapptr->caplen; … … 704 771 static size_t pcap_set_capture_length(libtrace_packet_t *packet,size_t size) { 705 772 struct pcap_pkthdr *pcapptr = 0; 706 assert(packet); 773 if (!packet) { 774 fprintf(stderr, "NULL packet passed to pcap_set_capture_length()\n"); 775 return TRACE_ERR_NULL_PACKET; 776 } 707 777 if (size > trace_get_capture_length(packet)) { 708 778 /* Can't make a packet larger */ … … 710 780 } 711 781 /* Reset the cached capture length */ 712 packet->ca pture_length = -1;782 packet->cached.capture_length = -1; 713 783 pcapptr = (struct pcap_pkthdr *)packet->header; 714 784 pcapptr->caplen = size; … … 717 787 718 788 static int pcap_get_fd(const libtrace_t *trace) { 719 720 assert(trace->format_data); 789 if (!trace) { 790 fprintf(stderr, "NULL trace passed to pcap_get_fd()\n"); 791 return TRACE_ERR_NULL_TRACE; 792 } 793 if (!trace->format_data) { 794 /* cant do this because trace is a const? */ 795 /*trace_set_err(trace, TRACE_ERR_BAD_FORMAT, "Trace format data missing, call init_input() before calling pcap_get_fd()");*/ 796 fprintf(stderr, "Trace format data missing, call init_input() before calling pcap_get_fd()\n"); 797 return TRACE_ERR_BAD_FORMAT; 798 } 721 799 return pcap_fileno(DATA(trace)->input.pcap); 722 800 } -
lib/format_pcapfile.c
r37ee856 r5460603 31 31 32 32 #include <sys/stat.h> 33 #include <assert.h>34 33 #include <stdio.h> 35 34 #include <stdlib.h> … … 69 68 #define MAGIC2_REV 0x4d3cb2a1 70 69 70 static bool pcapfile_can_write(libtrace_packet_t *packet) { 71 /* Get the linktype */ 72 libtrace_linktype_t ltype = trace_get_link_type(packet); 73 74 if (ltype == TRACE_TYPE_PCAPNG_META 75 || ltype == TRACE_TYPE_CONTENT_INVALID 76 || ltype == TRACE_TYPE_UNKNOWN 77 || ltype == TRACE_TYPE_ERF_META 78 || ltype == TRACE_TYPE_NONDATA) { 79 80 return false; 81 } 82 83 return true; 84 } 85 71 86 static inline int header_is_backwards_magic(pcapfile_header_t *header) { 72 87 return (header->magic_number == MAGIC1_REV || header->magic_number == MAGIC2_REV); … … 125 140 static int pcapfile_init_input(libtrace_t *libtrace) { 126 141 libtrace->format_data = malloc(sizeof(struct pcapfile_format_data_t)); 127 128 if (libtrace->format_data == NULL) {129 trace_set_err(libtrace,ENOMEM,"Out of memory");142 if (!libtrace->format_data) { 143 trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, "Unable to allocate memory for " 144 "format data inside pcapfile_init_input()"); 130 145 return -1; 131 146 } … … 139 154 libtrace->format_data = 140 155 malloc(sizeof(struct pcapfile_format_data_out_t)); 156 if (!libtrace->format_data) { 157 trace_set_err_out(libtrace, TRACE_ERR_INIT_FAILED, "Unable to allocate memory for " 158 "format data inside pcapfile_init_output()"); 159 return -1; 160 } 141 161 142 162 DATAOUT(libtrace)->file=NULL; … … 171 191 if (!DATA(libtrace)) 172 192 return num; 173 193 174 194 /* We can use the PCAP magic number to determine the byte order */ 175 195 if (header_is_backwards_magic(&(DATA(libtrace)->header))) … … 191 211 if (!DATA(libtrace)->started) { 192 212 193 if (!libtrace->io) 213 if (!libtrace->io) { 214 trace_set_err(libtrace, TRACE_ERR_BAD_IO, "Trace cannot start IO in pcapfile_start_input()"); 194 215 return -1; 216 } 195 217 196 218 err=wandio_read(libtrace->io, … … 199 221 200 222 DATA(libtrace)->started = true; 201 assert(sizeof(DATA(libtrace)->header) > 0); 202 223 if (!(sizeof(DATA(libtrace)->header) > 0)) { 224 trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, "Trace is missing header in pcapfile_start_input()"); 225 return -1; 226 } 227 203 228 if (err<1) { 204 229 trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, … … 206 231 return -1; 207 232 } 208 233 209 234 if (err != (int)sizeof(DATA(libtrace)->header)) { 210 235 trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, … … 258 283 case TRACE_OPTION_HASHER: 259 284 case TRACE_OPTION_REPLAY_SPEEDUP: 285 case TRACE_OPTION_CONSTANT_ERF_FRAMING: 260 286 /* All these are either unsupported or handled 261 287 * by trace_config */ … … 343 369 size_t bytes_to_read = 0; 344 370 345 assert(libtrace->format_data); 371 if (!libtrace->format_data) { 372 trace_set_err(libtrace, TRACE_ERR_BAD_FORMAT, "Trace format data missing, " 373 "call trace_create() before calling trace_read_packet()"); 374 return -1; 375 } 346 376 347 377 packet->type = pcap_linktype_to_rt(swapl(libtrace, … … 353 383 354 384 flags |= TRACE_PREP_OWN_BUFFER; 355 385 356 386 err=wandio_read(libtrace->io, 357 387 packet->buffer, 358 388 sizeof(libtrace_pcapfile_pkt_hdr_t)); 389 359 390 if (err<0) { 360 391 trace_set_err(libtrace,TRACE_ERR_WANDIO_FAILED,"reading packet"); … … 373 404 bytes_to_read = swapl(libtrace,((libtrace_pcapfile_pkt_hdr_t*)packet->buffer)->caplen); 374 405 375 if (bytes_to_read >= LIBTRACE_PACKET_BUFSIZE) { 406 if (bytes_to_read >= (LIBTRACE_PACKET_BUFSIZE - 407 sizeof(libtrace_pcapfile_pkt_hdr_t))) { 376 408 trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, "Invalid caplen in pcap header (%u) - trace may be corrupt", (uint32_t)bytes_to_read); 377 409 return -1; 378 410 } 379 380 assert(bytes_to_read < LIBTRACE_PACKET_BUFSIZE);381 411 382 412 /* If there is no payload to read, do not ask wandio_read to try and … … 413 443 /* We may as well cache this value now, seeing as we already had to 414 444 * look it up */ 415 packet->ca pture_length = bytes_to_read;445 packet->cached.capture_length = bytes_to_read; 416 446 return sizeof(libtrace_pcapfile_pkt_hdr_t) + bytes_to_read; 417 447 } … … 420 450 libtrace_packet_t *packet) 421 451 { 452 453 /* Check pcapfile can write this type of packet */ 454 if (!pcapfile_can_write(packet)) { 455 return 0; 456 } 457 422 458 struct libtrace_pcapfile_pkt_hdr_t hdr; 423 459 struct timeval tv = trace_get_timeval(packet); … … 429 465 430 466 ptr = trace_get_packet_buffer(packet,&linktype,&remaining); 431 432 /* Silently discard RT metadata packets and packets with an433 * unknown linktype. */434 if (linktype == TRACE_TYPE_NONDATA || linktype == TRACE_TYPE_UNKNOWN || linktype == TRACE_TYPE_ERF_META || linktype == TRACE_TYPE_CONTENT_INVALID) {435 return 0;436 }437 467 438 468 /* If this packet cannot be converted to a pcap linktype then … … 444 474 TRACE_ERR_NO_CONVERSION, 445 475 "pcap does not support this format"); 446 assert(0);447 476 return -1; 448 477 } … … 485 514 hdr.ts_usec = (uint32_t)tv.tv_usec; 486 515 hdr.caplen = trace_get_capture_length(packet); 487 assert(hdr.caplen < LIBTRACE_PACKET_BUFSIZE); 516 if (hdr.caplen >= LIBTRACE_PACKET_BUFSIZE) { 517 trace_set_err_out(out, TRACE_ERR_BAD_PACKET, "Capture length is greater than buffer size in pcap_write_packet()"); 518 return -1; 519 } 488 520 /* PCAP doesn't include the FCS in its wire length value, but we do */ 489 521 if (linktype==TRACE_TYPE_ETH) { … … 557 589 libtrace_pcapfile_pkt_hdr_t *hdr; 558 590 struct timeval ts; 559 560 assert(packet->header); 561 591 592 memset(&ts, 0, sizeof(struct timeval)); 593 594 if (!packet) { 595 fprintf(stderr, "NULL packet passed to pcapfile_get_timeval()\n"); 596 /* Return default timeval on error? */ 597 return ts; 598 } 599 600 if (!packet->header) { 601 trace_set_err(packet->trace, TRACE_ERR_BAD_HEADER, "pcap packet with NULL header passed to " 602 "pcapfile_get_timeval()"); 603 /* Return default timeval on error? */ 604 return ts; 605 } 606 562 607 hdr = (libtrace_pcapfile_pkt_hdr_t*)packet->header; 563 608 ts.tv_sec = swapl(packet->trace,hdr->ts_sec); … … 575 620 libtrace_pcapfile_pkt_hdr_t *hdr; 576 621 struct timespec ts; 577 578 assert(packet->header); 579 622 623 memset(&ts, 0, sizeof(struct timespec)); 624 if (!packet) { 625 fprintf(stderr, "NULL packet passed to pcapfile_get_timespec()"); 626 /* Return default timespec on error? */ 627 return ts; 628 } 629 630 if (!packet->header) { 631 trace_set_err(packet->trace, TRACE_ERR_BAD_HEADER, "pcap packet with NULL header passed to " 632 "pcapfile_get_timespec()"); 633 /* Return fefault timespec on error? */ 634 return ts; 635 } 636 580 637 hdr = (libtrace_pcapfile_pkt_hdr_t*)packet->header; 581 638 ts.tv_sec = swapl(packet->trace,hdr->ts_sec); … … 592 649 libtrace_pcapfile_pkt_hdr_t *pcapptr; 593 650 594 assert(packet->header); 651 if (!packet) { 652 fprintf(stderr, "NULL packet passed to pcapfile_get_capture_length()\n"); 653 return TRACE_ERR_NULL_PACKET; 654 } 655 656 if (!packet->header) { 657 trace_set_err(packet->trace, TRACE_ERR_BAD_HEADER, "pcap packet with NULL header passed to " 658 "pcapfile_get_capture_length()"); 659 return -1; 660 } 595 661 pcapptr = (libtrace_pcapfile_pkt_hdr_t *)packet->header; 596 662 … … 601 667 libtrace_pcapfile_pkt_hdr_t *pcapptr; 602 668 603 assert(packet->header); 669 if (!packet) { 670 fprintf(stderr, "NULL packet passed to pcapfile_get_wire_length()\n"); 671 return TRACE_ERR_NULL_PACKET; 672 } 673 674 if (!packet->header) { 675 trace_set_err(packet->trace, TRACE_ERR_BAD_HEADER, "pcap packet with NULL header passed to " 676 "pcapfile_get_wire_length()"); 677 return -1; 678 } 679 680 if (packet->payload == NULL) { 681 return 0; 682 } 604 683 605 684 pcapptr = (libtrace_pcapfile_pkt_hdr_t *)packet->header; … … 640 719 static size_t pcapfile_set_capture_length(libtrace_packet_t *packet,size_t size) { 641 720 libtrace_pcapfile_pkt_hdr_t *pcapptr = 0; 642 assert(packet); 643 assert(packet->header); 721 722 if (!packet) { 723 fprintf(stderr, "NULL packet passed into pcapfile_set_capture_length\n"); 724 /* Return -1 on error? */ 725 return ~0U; 726 } 727 728 if (!packet->header) { 729 trace_set_err(packet->trace, TRACE_ERR_BAD_HEADER, "pcap packet with NULL header passed to " 730 "pcapfile_set_capture_length()"); 731 /* Return -1 on error? */ 732 return ~0U; 733 } 644 734 if (size > trace_get_capture_length(packet)) { 645 735 /* Can't make a packet larger */ … … 647 737 } 648 738 /* Reset the cached capture length */ 649 packet->ca pture_length = -1;739 packet->cached.capture_length = -1; 650 740 pcapptr = (libtrace_pcapfile_pkt_hdr_t *)packet->header; 651 741 pcapptr->caplen = swapl(packet->trace,(uint32_t)size); -
lib/format_pcapng.c
re9da777 r063d5dd 31 31 32 32 #include <sys/stat.h> 33 #include <assert.h>34 33 #include <stdio.h> 35 34 #include <stdlib.h> … … 49 48 #define PCAPNG_CUSTOM_TYPE 0x00000BAD 50 49 #define PCAPNG_CUSTOM_NONCOPY_TYPE 0x40000BAD 50 #define PCAPNG_DECRYPTION_SECRETS_TYPE 0x0000000A 51 52 #define PCAPNG_NRB_RECORD_END 0x0000 53 #define PCAPNG_NRB_RECORD_IP4 0x0001 54 #define PCAPNG_NRB_RECORD_IP6 0x0002 55 56 #define PCAPNG_CUSTOM_OPTION_UTF8 0xBAC 57 #define PCAPNG_CUSTOM_OPTION_BIN 0xBAD 58 #define PCAPNG_CUSTOM_OPTION_UTF8_NONCOPY 0x4BAC 59 #define PCAPNG_CUSTOM_OPTION_BIN_NONCOPY 0x4BAD 60 61 #define PCAPNG_OPTION_END 0x0000 51 62 52 63 #define PACKET_IS_ENHANCED (pcapng_get_record_type(packet) == PCAPNG_ENHANCED_PACKET_TYPE) … … 55 66 56 67 #define PACKET_IS_OLD (pcapng_get_record_type(packet) == PCAPNG_OLD_PACKET_TYPE) 57 58 68 59 69 #define PCAPNG_IFOPT_TSRESOL 9 … … 134 144 typedef struct pcapng_interface_t pcapng_interface_t; 135 145 146 struct pcapng_timestamp { 147 uint32_t timehigh; 148 uint32_t timelow; 149 }; 150 136 151 struct pcapng_interface_t { 137 152 … … 161 176 uint16_t allocatedinterfaces; 162 177 uint16_t nextintid; 178 179 }; 180 181 struct pcapng_format_data_out_t { 182 iow_t *file; 183 int compress_level; 184 int compress_type; 185 int flag; 186 187 /* Section data */ 188 uint16_t sechdr_count; 189 bool byteswapped; 190 191 /* Interface data */ 192 uint16_t nextintid; 193 libtrace_linktype_t lastdlt; 163 194 }; 164 195 … … 168 199 }; 169 200 201 struct pcapng_custom_optheader { 202 uint16_t optcode; 203 uint16_t optlen; 204 uint32_t pen; 205 }; 206 struct pcapng_nrb_record { 207 uint16_t recordtype; 208 uint16_t recordlen; 209 }; 170 210 struct pcapng_peeker { 171 211 uint32_t blocktype; … … 175 215 typedef struct pcapng_peeker pcapng_hdr_t; 176 216 177 178 217 #define DATA(x) ((struct pcapng_format_data_t *)((x)->format_data)) 218 #define DATAOUT(x) ((struct pcapng_format_data_out_t*)((x)->format_data)) 219 220 static char *pcapng_parse_next_option(libtrace_t *libtrace, char **pktbuf, 221 uint16_t *code, uint16_t *length, pcapng_hdr_t *blockhdr); 222 223 static bool pcapng_can_write(libtrace_packet_t *packet) { 224 /* Get the linktype */ 225 libtrace_linktype_t ltype = trace_get_link_type(packet); 226 227 /* TODO convert erf meta to pcapng meta? */ 228 if (ltype == TRACE_TYPE_CONTENT_INVALID 229 || ltype == TRACE_TYPE_UNKNOWN 230 || ltype == TRACE_TYPE_ERF_META 231 || ltype == TRACE_TYPE_NONDATA) { 232 233 return false; 234 } 235 236 return true; 237 } 179 238 180 239 static pcapng_interface_t *lookup_interface(libtrace_t *libtrace, 181 240 uint32_t intid) { 182 241 183 184 if (intid >= DATA(libtrace)->nextintid) { 185 return NULL; 186 } 187 188 return DATA(libtrace)->interfaces[intid]; 189 242 if (intid >= DATA(libtrace)->nextintid) { 243 return NULL; 244 } 245 246 return DATA(libtrace)->interfaces[intid]; 190 247 } 191 248 192 249 static inline uint32_t pcapng_get_record_type(const libtrace_packet_t *packet) { 193 194 250 uint32_t *btype = (uint32_t *)packet->header; 195 251 196 if (DATA(packet->trace)->byteswapped) 197 return byteswap32(*btype); 252 /* only check for byteswapped if input format is pcapng */ 253 if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) { 254 if (DATA(packet->trace)->byteswapped) 255 return byteswap32(*btype); 256 } 257 198 258 return *btype; 259 } 260 261 static inline uint32_t pcapng_swap32(libtrace_out_t *libtrace, uint32_t value) { 262 if (DATAOUT(libtrace)->byteswapped) { 263 return byteswap32(value); 264 } else { 265 return value; 266 } 267 } 268 static inline uint32_t pcapng_swap16(libtrace_out_t *libtrace, uint32_t value) { 269 if (DATAOUT(libtrace)->byteswapped) { 270 return byteswap16(value); 271 } else { 272 return value; 273 } 274 } 275 static inline uint32_t pcapng_get_blocklen(const libtrace_packet_t *packet) { 276 struct pcapng_peeker *hdr = (struct pcapng_peeker *)packet->buffer; 277 278 /* only check for byteswapped if input format is pcapng */ 279 if (packet->trace->format->type == TRACE_FORMAT_PCAPNG) { 280 if (DATA(packet->trace)->byteswapped) 281 return byteswap32(hdr->blocklen); 282 } 283 284 return hdr->blocklen; 285 286 } 287 288 #if 0 289 static inline uint16_t pcapng_get_customdata_len(libtrace_packet_t *packet, char *ptr) { 290 struct pcapng_custom_optheader *hdr = (struct pcapng_custom_optheader *)ptr; 291 292 if (DATA(packet->trace)->byteswapped) { 293 return byteswap16(hdr->optlen); 294 } else { 295 return hdr->optlen; 296 } 297 } 298 static inline uint16_t pcapng_get_customdata_optcode(libtrace_packet_t *packet, char *ptr) { 299 struct pcapng_custom_optheader *hdr = (struct pcapng_custom_optheader *)ptr; 300 301 if (DATA(packet->trace)->byteswapped) { 302 return byteswap16(hdr->optcode); 303 } else { 304 return hdr->optcode; 305 } 306 } 307 #endif 308 309 static inline uint16_t pcapng_get_nrb_record_type(libtrace_packet_t *packet, char *ptr) { 310 struct pcapng_nrb_record *hdr = (struct pcapng_nrb_record *)ptr; 311 if (DATA(packet->trace)->byteswapped) { 312 return byteswap16(hdr->recordtype); 313 } else { 314 return hdr->recordtype; 315 } 316 } 317 static inline uint16_t pcapng_get_nrb_record_len(libtrace_packet_t *packet, char *ptr) { 318 struct pcapng_nrb_record *hdr = (struct pcapng_nrb_record *)ptr; 319 if (DATA(packet->trace)->byteswapped) { 320 return byteswap16(hdr->recordlen); 321 } else { 322 return hdr->recordlen; 323 } 324 } 325 static uint32_t pcapng_output_options(libtrace_out_t *libtrace, libtrace_packet_t *packet, 326 char *ptr) { 327 328 struct pcapng_optheader opthdr; 329 uint16_t optcode, optlen; 330 char *optval = NULL; 331 char *bodyptr = NULL; 332 int padding; 333 void *padding_data; 334 uint32_t len = 0; 335 336 bodyptr = ptr; 337 338 while ((optval = pcapng_parse_next_option(packet->trace, &bodyptr, 339 &optcode, &optlen, (pcapng_hdr_t *) packet->buffer))) { 340 341 /* pcapng_parse_next_option byteswaps the opcode and len for us */ 342 opthdr.optcode = optcode; 343 opthdr.optlen = optlen; 344 345 /* output the header */ 346 wandio_wwrite(DATAOUT(libtrace)->file, &opthdr, sizeof(opthdr)); 347 348 /* If this is a custom option */ 349 if (optcode == PCAPNG_CUSTOM_OPTION_UTF8 || 350 optcode == PCAPNG_CUSTOM_OPTION_BIN || 351 optcode == PCAPNG_CUSTOM_OPTION_UTF8_NONCOPY || 352 optcode == PCAPNG_CUSTOM_OPTION_BIN_NONCOPY) { 353 /* flip the pen and output the option value */ 354 //uint32_t pen = byteswap32((uint32_t)*optval); 355 wandio_wwrite(DATAOUT(libtrace)->file, optval, sizeof(uint32_t)); 356 357 /* the len for custom options include pen */ 358 optval += sizeof(uint32_t); 359 optlen -= sizeof(uint32_t); 360 } 361 362 /* output the rest of the data */ 363 wandio_wwrite(DATAOUT(libtrace)->file, &optval, optlen); 364 365 /* calculate any required padding */ 366 padding = optlen % 4; 367 if (padding) { padding = 4 - padding; } 368 padding_data = calloc(1, padding); 369 /* output the padding */ 370 wandio_wwrite(DATAOUT(libtrace)->file, padding_data, padding); 371 free(padding_data); 372 373 len += sizeof(opthdr) + optlen; 374 } 375 376 return len; 377 } 378 static uint32_t pcapng_output_interface_packet(libtrace_out_t *libtrace, libtrace_packet_t *packet) { 379 pcapng_int_t *cur = (pcapng_int_t *)packet->header; 380 pcapng_int_t hdr; 381 char *bodyptr = NULL; 382 383 /* If the input trace is not pcapng we have no way of finding the byteordering 384 * this can occur if a packet is reconstructed with a deadtrace. Or if the packet 385 * is in the correct byte ordering just output it */ 386 if ((packet->trace->format->type != TRACE_FORMAT_PCAPNG) || 387 (DATA(packet->trace)->byteswapped == DATAOUT(libtrace)->byteswapped)) { 388 uint32_t len = pcapng_get_blocklen(packet); 389 wandio_wwrite(DATAOUT(libtrace)->file, packet->buffer, len); 390 return len; 391 } 392 393 /* Byteswap the headers */ 394 hdr.blocktype = byteswap32(cur->blocktype); 395 hdr.blocklen = byteswap32(cur->blocklen); 396 hdr.linktype = byteswap16(cur->linktype); 397 hdr.reserved = byteswap16(cur->reserved); 398 hdr.snaplen = byteswap32(cur->snaplen); 399 400 wandio_wwrite(DATAOUT(libtrace)->file, &hdr, sizeof(hdr)); 401 /* output any options */ 402 bodyptr = (char *)packet->buffer + sizeof(hdr); 403 pcapng_output_options(libtrace, packet, bodyptr); 404 wandio_wwrite(DATAOUT(libtrace)->file, &hdr.blocklen, sizeof(hdr.blocklen)); 405 406 return hdr.blocklen; 407 } 408 static uint32_t pcapng_output_simple_packet(libtrace_out_t *libtrace, libtrace_packet_t *packet) { 409 pcapng_spkt_t *cur = (pcapng_spkt_t *)packet->header; 410 pcapng_spkt_t hdr; 411 uint32_t len; 412 char *bodyptr = NULL; 413 414 /* If the input trace is not pcapng we have no way of finding the byteordering 415 * this can occur if a packet is reconstructed with a deadtrace. Or if the packet 416 * is in the correct byte ordering just output it */ 417 if ((packet->trace->format->type != TRACE_FORMAT_PCAPNG) || 418 (DATA(packet->trace)->byteswapped == DATAOUT(libtrace)->byteswapped)) { 419 len = pcapng_get_blocklen(packet); 420 wandio_wwrite(DATAOUT(libtrace)->file, packet->buffer, len); 421 return len; 422 } 423 424 hdr.blocktype = byteswap32(cur->blocktype); 425 hdr.blocklen = byteswap32(cur->blocklen); 426 hdr.wlen = byteswap32(cur->wlen); 427 428 wandio_wwrite(DATAOUT(libtrace)->file, &hdr, sizeof(hdr)); 429 430 /* output the packet payload */ 431 bodyptr = (char *)packet->buffer + sizeof(hdr); 432 len = pcapng_get_blocklen(packet) - sizeof(hdr) - sizeof(hdr.blocklen); 433 wandio_wwrite(DATAOUT(libtrace)->file, bodyptr, len); 434 435 wandio_wwrite(DATAOUT(libtrace)->file, &hdr.blocklen, sizeof(hdr.blocklen)); 436 437 return hdr.blocklen; 438 } 439 static uint32_t pcapng_output_old_packet(libtrace_out_t *libtrace, libtrace_packet_t *packet) { 440 pcapng_opkt_t *cur = (pcapng_opkt_t *)packet->header; 441 pcapng_opkt_t hdr; 442 uint32_t len; 443 char *bodyptr = NULL; 444 445 /* If the input trace is not pcapng we have no way of finding the byteordering 446 * this can occur if a packet is reconstructed with a deadtrace. Or if the packet 447 * is in the correct byte ordering just output it */ 448 if ((packet->trace->format->type != TRACE_FORMAT_PCAPNG) || 449 (DATA(packet->trace)->byteswapped == DATAOUT(libtrace)->byteswapped)) { 450 len = pcapng_get_blocklen(packet); 451 wandio_wwrite(DATAOUT(libtrace)->file, packet->buffer, len); 452 return len; 453 } 454 455 hdr.blocktype = byteswap32(cur->blocktype); 456 hdr.blocklen = byteswap32(cur->blocklen); 457 hdr.interfaceid = byteswap16(cur->interfaceid); 458 hdr.drops = byteswap16(cur->drops); 459 hdr.timestamp_high = byteswap32(cur->timestamp_high); 460 hdr.timestamp_low = byteswap32(cur->timestamp_low); 461 hdr.caplen = byteswap32(cur->caplen); 462 hdr.wlen = byteswap32(cur->wlen); 463 464 wandio_wwrite(DATAOUT(libtrace)->file, &hdr, sizeof(hdr)); 465 466 /* output the packet payload */ 467 bodyptr = (char *)packet->buffer + sizeof(hdr); 468 len = pcapng_get_blocklen(packet) - sizeof(hdr) - sizeof(hdr.blocklen); 469 wandio_wwrite(DATAOUT(libtrace)->file, bodyptr, len); 470 471 /* output any options if present */ 472 pcapng_output_options(libtrace, packet, bodyptr); 473 474 wandio_wwrite(DATAOUT(libtrace)->file, &hdr.blocklen, sizeof(hdr.blocklen)); 475 476 477 return hdr.blocklen; 478 } 479 static uint32_t pcapng_output_nameresolution_packet(libtrace_out_t *libtrace, libtrace_packet_t *packet) { 480 pcapng_nrb_t *cur = (pcapng_nrb_t *)packet->buffer; 481 pcapng_nrb_t hdr; 482 char *bodyptr = NULL; 483 int padding; 484 void *padding_data; 485 486 /* If the input trace is not pcapng we have no way of finding the byteordering 487 * this can occur if a packet is reconstructed with a deadtrace. Or if the packet 488 * is in the correct byte ordering just output it */ 489 if ((packet->trace->format->type != TRACE_FORMAT_PCAPNG) || 490 (DATA(packet->trace)->byteswapped == DATAOUT(libtrace)->byteswapped)) { 491 uint32_t len = pcapng_get_blocklen(packet); 492 wandio_wwrite(DATAOUT(libtrace)->file, packet->buffer, len); 493 return len; 494 } 495 496 hdr.blocktype = byteswap32(cur->blocktype); 497 hdr.blocklen = byteswap32(cur->blocklen); 498 499 /* output the header */ 500 wandio_wwrite(DATAOUT(libtrace)->file, &hdr, sizeof(hdr)); 501 bodyptr = (char *)packet->buffer + sizeof(hdr); 502 503 struct pcapng_nrb_record *nrbr = (struct pcapng_nrb_record *)bodyptr; 504 505 uint16_t record_type = pcapng_get_nrb_record_type(packet, bodyptr); 506 while (record_type != PCAPNG_NRB_RECORD_END) { 507 508 struct pcapng_nrb_record nrb; 509 510 /* recordlen contains only the length of the record value without 511 * any padding */ 512 uint16_t recordlen = pcapng_get_nrb_record_len(packet, bodyptr); 513 514 nrb.recordtype = byteswap16(nrbr->recordtype); 515 nrb.recordlen = byteswap16(nrbr->recordlen); 516 517 /* output the record header */ 518 wandio_wwrite(DATAOUT(libtrace)->file, &nrb, sizeof(nrb)); 519 bodyptr += sizeof(nrb); 520 521 /* output the record data */ 522 wandio_wwrite(DATAOUT(libtrace)->file, bodyptr, recordlen); 523 bodyptr += recordlen; 524 525 /* calculate any required padding. record also contains the 8 byte header 526 * but we dont need to subtract it because it will be removed with % 4 */ 527 padding = recordlen % 4; 528 if (padding) { padding = 4 - padding; } 529 padding_data = calloc(1, padding); 530 /* output the padding */ 531 wandio_wwrite(DATAOUT(libtrace)->file, padding_data, padding); 532 free(padding_data); 533 bodyptr += padding; 534 535 /* get the next record if it exists */ 536 nrbr = (struct pcapng_nrb_record *)bodyptr; 537 record_type = pcapng_get_nrb_record_type(packet, bodyptr); 538 } 539 540 /* output nrb record end block */ 541 struct pcapng_nrb_record nrbftr; 542 nrbftr.recordtype = PCAPNG_NRB_RECORD_END; 543 nrbftr.recordlen = 0; 544 wandio_wwrite(DATAOUT(libtrace)->file, &nrbftr, sizeof(nrbftr)); 545 bodyptr += sizeof(nrbftr); 546 547 /* output any options if present */ 548 pcapng_output_options(libtrace, packet, bodyptr); 549 550 /* and print out rest of the header */ 551 wandio_wwrite(DATAOUT(libtrace)->file, &hdr.blocklen, sizeof(hdr.blocklen)); 552 553 return hdr.blocklen; 554 } 555 static uint32_t pcapng_output_custom_packet(libtrace_out_t *libtrace, libtrace_packet_t *packet) { 556 pcapng_custom_t *cur = (pcapng_custom_t *)packet->buffer; 557 pcapng_custom_t hdr; 558 char *bodyptr = (char *)packet->buffer; 559 560 /* If the input trace is not pcapng we have no way of finding the byteordering 561 * this can occur if a packet is reconstructed with a deadtrace. Or if the packet 562 * is in the correct byte ordering just output it */ 563 if ((packet->trace->format->type != TRACE_FORMAT_PCAPNG) || 564 (DATA(packet->trace)->byteswapped == DATAOUT(libtrace)->byteswapped)) { 565 uint32_t len = pcapng_get_blocklen(packet); 566 wandio_wwrite(DATAOUT(libtrace)->file, packet->buffer, len); 567 return len; 568 } 569 570 hdr.blocktype = byteswap32(cur->blocktype); 571 hdr.blocklen = byteswap32(cur->blocklen); 572 hdr.pen = byteswap32(cur->blocklen); 573 574 /* output the header */ 575 wandio_wwrite(DATAOUT(libtrace)->file, &hdr, sizeof(hdr)); 576 bodyptr += sizeof(hdr); 577 578 /* now print out any options */ 579 pcapng_output_options(libtrace, packet, bodyptr); 580 581 /* and print out rest of the header */ 582 wandio_wwrite(DATAOUT(libtrace)->file, &hdr.blocklen, sizeof(hdr.blocklen)); 583 584 return hdr.blocklen; 585 } 586 static uint32_t pcapng_output_enhanced_packet(libtrace_out_t *libtrace, libtrace_packet_t *packet) { 587 pcapng_epkt_t *cur = (pcapng_epkt_t *)packet->buffer; 588 pcapng_epkt_t hdr; 589 char *bodyptr = NULL; 590 uint32_t len; 591 592 /* If the input trace is not pcapng we have no way of finding the byteordering 593 * this can occur if a packet is reconstructed with a deadtrace. Or if the packet 594 * is in the correct byte ordering just output it */ 595 if ((packet->trace->format->type != TRACE_FORMAT_PCAPNG) || 596 (DATA(packet->trace)->byteswapped == DATAOUT(libtrace)->byteswapped)) { 597 len = pcapng_get_blocklen(packet); 598 wandio_wwrite(DATAOUT(libtrace)->file, packet->buffer, len); 599 return len; 600 } 601 602 hdr.blocktype = byteswap32(cur->blocktype); 603 hdr.blocklen = byteswap32(cur->blocklen); 604 hdr.interfaceid = byteswap32(cur->interfaceid); 605 hdr.timestamp_high = byteswap32(cur->timestamp_high); 606 hdr.timestamp_low = byteswap32(cur->timestamp_low); 607 hdr.caplen = byteswap32(cur->caplen); 608 hdr.wlen = byteswap32(cur->wlen); 609 610 /* output beginning of header */ 611 wandio_wwrite(DATAOUT(libtrace)->file, &hdr, sizeof(hdr)); 612 613 /* output the packet payload */ 614 bodyptr = (char *)packet->buffer + sizeof(hdr); 615 len = pcapng_get_blocklen(packet) - sizeof(hdr) - sizeof(hdr.blocklen); 616 wandio_wwrite(DATAOUT(libtrace)->file, bodyptr, len); 617 618 /* output any options */ 619 pcapng_output_options(libtrace, packet, bodyptr); 620 621 /* output end of header */ 622 wandio_wwrite(DATAOUT(libtrace)->file, &hdr.blocklen, sizeof(hdr.blocklen)); 623 624 return hdr.blocklen; 625 } 626 static uint32_t pcapng_output_interfacestats_packet(libtrace_out_t *libtrace, libtrace_packet_t *packet) { 627 pcapng_stats_t *cur = (pcapng_stats_t *)packet->header; 628 pcapng_stats_t hdr; 629 char *bodyptr = NULL; 630 631 /* If the input trace is not pcapng we have no way of finding the byteordering 632 * this can occur if a packet is reconstructed with a deadtrace. Or if the packet 633 * is in the correct byte ordering just output it */ 634 if ((packet->trace->format->type != TRACE_FORMAT_PCAPNG) || 635 (DATA(packet->trace)->byteswapped == DATAOUT(libtrace)->byteswapped)) { 636 uint32_t len = pcapng_get_blocklen(packet); 637 wandio_wwrite(DATAOUT(libtrace)->file, packet->buffer, len); 638 return len; 639 } 640 641 hdr.blocktype = byteswap32(cur->blocktype); 642 hdr.blocklen = byteswap32(cur->blocklen); 643 hdr.interfaceid = byteswap32(cur->interfaceid); 644 hdr.timestamp_high = byteswap32(cur->timestamp_high); 645 hdr.timestamp_low = byteswap32(cur->timestamp_low); 646 647 /* output interface stats header */ 648 wandio_wwrite(DATAOUT(libtrace)->file, &hdr, sizeof(hdr)); 649 /* output any options if present */ 650 bodyptr = (char *)packet->buffer + sizeof(hdr); 651 pcapng_output_options(libtrace, packet, bodyptr); 652 /* output rest of interface stats header */ 653 wandio_wwrite(DATAOUT(libtrace)->file, &hdr.blocklen, sizeof(hdr.blocklen)); 654 655 return hdr.blocklen; 199 656 } 200 657 … … 215 672 } 216 673 674 static struct pcapng_timestamp pcapng_get_timestamp(libtrace_packet_t *packet) { 675 struct timeval tv = trace_get_timeval(packet); 676 uint64_t time = ((uint64_t)tv.tv_sec * (uint64_t)1000000) + tv.tv_usec; 677 678 struct pcapng_timestamp timestamp; 679 timestamp.timehigh = time >> 32; 680 timestamp.timelow = time & 0xFFFFFFFF; 681 682 return timestamp; 683 } 684 217 685 218 686 static int pcapng_init_input(libtrace_t *libtrace) { 219 687 libtrace->format_data = malloc(sizeof(struct pcapng_format_data_t)); 220 if (libtrace->format_data == NULL) { 221 trace_set_err(libtrace, ENOMEM, "Out of memory!"); 688 if (!libtrace->format_data) { 689 trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, "Unable to allocate memory for " 690 "format data inside pcapng_init_input()"); 222 691 return -1; 223 692 } … … 232 701 233 702 return 0; 703 } 704 705 static int pcapng_config_output(libtrace_out_t *libtrace, trace_option_output_t option, 706 void *value) { 707 708 switch (option) { 709 case TRACE_OPTION_OUTPUT_COMPRESS: 710 DATAOUT(libtrace)->compress_level = *(int *)value; 711 return 0; 712 case TRACE_OPTION_OUTPUT_COMPRESSTYPE: 713 DATAOUT(libtrace)->compress_type = *(int *)value; 714 return 0; 715 case TRACE_OPTION_OUTPUT_FILEFLAGS: 716 DATAOUT(libtrace)->flag = *(int *)value; 717 return 0; 718 default: 719 trace_set_err_out(libtrace, TRACE_ERR_UNKNOWN_OPTION, 720 "Unknown option"); 721 return -1; 722 } 234 723 } 235 724 … … 263 752 case TRACE_OPTION_HASHER: 264 753 case TRACE_OPTION_REPLAY_SPEEDUP: 754 case TRACE_OPTION_CONSTANT_ERF_FRAMING: 265 755 break; 266 756 } … … 271 761 } 272 762 763 static int pcapng_init_output(libtrace_out_t *libtrace) { 764 libtrace->format_data = malloc(sizeof(struct pcapng_format_data_out_t)); 765 766 DATAOUT(libtrace)->file = NULL; 767 DATAOUT(libtrace)->compress_level = 0; 768 DATAOUT(libtrace)->compress_type = TRACE_OPTION_COMPRESSTYPE_NONE; 769 DATAOUT(libtrace)->flag = O_CREAT|O_WRONLY; 770 771 DATAOUT(libtrace)->sechdr_count = 0; 772 DATAOUT(libtrace)->byteswapped = false; 773 774 DATAOUT(libtrace)->nextintid = 0; 775 DATAOUT(libtrace)->lastdlt = 0; 776 777 return 0; 778 } 779 273 780 static int pcapng_fin_input(libtrace_t *libtrace) { 274 781 … … 286 793 free(libtrace->format_data); 287 794 return 0; 795 } 796 797 static int pcapng_fin_output(libtrace_out_t *libtrace) { 798 if (DATAOUT(libtrace)->file) { 799 wandio_wdestroy(DATAOUT(libtrace)->file); 800 } 801 free(libtrace->format_data); 802 libtrace->format_data = NULL; 803 return 0; 288 804 } 289 805 … … 302 818 } 303 819 304 assert((char *)blockhdr < *pktbuf); 820 if ((char *)blockhdr >= *pktbuf) { 821 return NULL; 822 } 305 823 // Check if we have reached the end of the block, +4 for trailing block-size 306 824 // We cannot assume a endofopt, so we add one … … 429 947 } 430 948 949 static int pcapng_write_packet(libtrace_out_t *libtrace, libtrace_packet_t *packet) { 950 951 if (!libtrace) { 952 fprintf(stderr, "NULL trace passed into pcapng_write_packet()\n"); 953 return TRACE_ERR_NULL_TRACE; 954 } 955 if (!packet) { 956 trace_set_err_out(libtrace, TRACE_ERR_NULL_PACKET, "NULL packet passed " 957 "into pcapng_write_packet()\n"); 958 return -1; 959 } 960 961 /* Check pcapng can write this type of packet */ 962 if (!pcapng_can_write(packet)) { 963 return 0; 964 } 965 966 libtrace_linktype_t linktype = trace_get_link_type(packet); 967 968 /* If the file is not open, open it */ 969 if (!DATAOUT(libtrace)->file) { 970 DATAOUT(libtrace)->file = trace_open_file_out(libtrace, 971 DATAOUT(libtrace)->compress_type, 972 DATAOUT(libtrace)->compress_level, 973 DATAOUT(libtrace)->flag); 974 } 975 976 /* If the packet is already encapsulated in a pcapng frame just output it */ 977 switch (pcapng_get_record_type(packet)) { 978 case PCAPNG_SECTION_TYPE: { 979 /* If the section header passed in is byteswapped, everything we output 980 * till the next section header needs to be byteswapped. The next header 981 * will determine if we need to continue swapping bytes */ 982 if (DATA(packet->trace)->byteswapped) { 983 DATAOUT(libtrace)->byteswapped = true; 984 } else { 985 DATAOUT(libtrace)->byteswapped = false; 986 } 987 988 wandio_wwrite(DATAOUT(libtrace)->file, packet->buffer, 989 pcapng_get_blocklen(packet)); 990 991 DATAOUT(libtrace)->sechdr_count += 1; 992 993 return pcapng_get_blocklen(packet); 994 } 995 case PCAPNG_INTERFACE_TYPE: { 996 /* increment the interface id */ 997 DATAOUT(libtrace)->nextintid += 1; 998 999 return pcapng_output_interface_packet(libtrace, packet); 1000 } 1001 case PCAPNG_OLD_PACKET_TYPE: { 1002 return pcapng_output_old_packet(libtrace, packet); 1003 } 1004 case PCAPNG_SIMPLE_PACKET_TYPE: { 1005 if (DATAOUT(libtrace)->nextintid == 0) { 1006 trace_set_err_out(libtrace, TRACE_ERR_BAD_PACKET, 1007 "Cannot output simple packet before a interface " 1008 "block has been output in pcapng_write_packet()\n"); 1009 return -1; 1010 } 1011 return pcapng_output_simple_packet(libtrace, packet); 1012 } 1013 case PCAPNG_NAME_RESOLUTION_TYPE: { 1014 return pcapng_output_nameresolution_packet(libtrace, packet); 1015 } 1016 case PCAPNG_INTERFACE_STATS_TYPE: { 1017 if (DATAOUT(libtrace)->nextintid == 0) { 1018 trace_set_err_out(libtrace, TRACE_ERR_BAD_PACKET, 1019 "Cannot output a interface statistics block before a " 1020 "interface block has been output in pcapng_write_packet()\n"); 1021 return -1; 1022 } 1023 return pcapng_output_interfacestats_packet(libtrace, packet); 1024 } 1025 case PCAPNG_ENHANCED_PACKET_TYPE: { 1026 if (DATAOUT(libtrace)->nextintid == 0) { 1027 trace_set_err_out(libtrace, TRACE_ERR_BAD_PACKET, 1028 "Cannot output enhanced packet before a interface " 1029 "block has been output in pcapng_write_packet()\n"); 1030 return -1; 1031 } 1032 return pcapng_output_enhanced_packet(libtrace, packet); 1033 } 1034 case PCAPNG_CUSTOM_TYPE: { 1035 return pcapng_output_custom_packet(libtrace, packet); 1036 } 1037 case PCAPNG_DECRYPTION_SECRETS_TYPE: { 1038 return 0; 1039 } 1040 case PCAPNG_CUSTOM_NONCOPY_TYPE: { 1041 /* This custom block type is not ment to be copied */ 1042 return 0; 1043 } 1044 default: { 1045 1046 /* create section header if not already */ 1047 if (DATAOUT(libtrace)->sechdr_count == 0) { 1048 /* Create section block */ 1049 pcapng_sec_t sechdr; 1050 sechdr.blocktype = pcapng_swap32(libtrace, PCAPNG_SECTION_TYPE); 1051 sechdr.blocklen = pcapng_swap32(libtrace, 28); 1052 sechdr.ordering = pcapng_swap32(libtrace, 0x1A2B3C4D); 1053 sechdr.majorversion = pcapng_swap16(libtrace, 1); 1054 sechdr.minorversion = 0; 1055 sechdr.sectionlen = 0xFFFFFFFFFFFFFFFF; 1056 1057 wandio_wwrite(DATAOUT(libtrace)->file, &sechdr, sizeof(sechdr)); 1058 wandio_wwrite(DATAOUT(libtrace)->file, &sechdr.blocklen, sizeof(sechdr.blocklen)); 1059 1060 DATAOUT(libtrace)->sechdr_count += 1; 1061 } 1062 1063 /* create interface header if not already or if the linktype has changed */ 1064 if (DATAOUT(libtrace)->nextintid == 0 1065 || DATAOUT(libtrace)->lastdlt != linktype) { 1066 /* Create interface block*/ 1067 pcapng_int_t inthdr; 1068 inthdr.blocktype = pcapng_swap32(libtrace, PCAPNG_INTERFACE_TYPE); 1069 inthdr.blocklen = pcapng_swap32(libtrace, 20); 1070 inthdr.linktype = pcapng_swap16(libtrace, libtrace_to_pcap_dlt(linktype)); 1071 inthdr.reserved = 0; 1072 inthdr.snaplen = 0; 1073 1074 wandio_wwrite(DATAOUT(libtrace)->file, &inthdr, sizeof(inthdr)); 1075 wandio_wwrite(DATAOUT(libtrace)->file, &inthdr.blocklen, sizeof(inthdr.blocklen)); 1076 1077 /* increment the interface counter */ 1078 DATAOUT(libtrace)->nextintid += 1; 1079 /* update the last linktype */ 1080 DATAOUT(libtrace)->lastdlt = linktype; 1081 } 1082 1083 break; 1084 } 1085 } 1086 1087 /* If we get this far the packet is not a pcapng type so we need to encapsulate it 1088 * within a enhanced pcapng packet */ 1089 uint32_t remaining; 1090 void *link; 1091 uint32_t blocklen; 1092 uint32_t padding; 1093 uint32_t caplen; 1094 uint32_t wirelen; 1095 void *padding_data; 1096 pcapng_epkt_t epkthdr; 1097 1098 link = trace_get_packet_buffer(packet, &linktype, &remaining); 1099 1100 wirelen = trace_get_wire_length(packet); 1101 caplen = trace_get_capture_length(packet); 1102 1103 /* trace_get_wirelength includes FCS, while pcapng doesn't */ 1104 if (trace_get_link_type(packet)==TRACE_TYPE_ETH) { 1105 if (wirelen >= 4) { 1106 wirelen -= 4; 1107 } else { 1108 wirelen = 0; 1109 } 1110 } 1111 /* capture length should always be less than the wirelength */ 1112 if (caplen > wirelen) { 1113 caplen = wirelen; 1114 } 1115 1116 /* calculate padding to 32bits */ 1117 padding = caplen % 4; 1118 if (padding) { padding = 4 - padding; } 1119 padding_data = calloc(1, padding); 1120 1121 /* get pcapng_timestamp */ 1122 struct pcapng_timestamp ts = pcapng_get_timestamp(packet); 1123 1124 /* calculate the block length */ 1125 blocklen = sizeof(epkthdr) + sizeof(epkthdr.blocklen) + caplen + padding; 1126 1127 /* construct the packet */ 1128 epkthdr.blocktype = pcapng_swap32(libtrace, PCAPNG_ENHANCED_PACKET_TYPE); 1129 epkthdr.blocklen = pcapng_swap32(libtrace, blocklen); 1130 epkthdr.interfaceid = pcapng_swap32(libtrace, DATAOUT(libtrace)->nextintid-1); 1131 epkthdr.timestamp_high = pcapng_swap32(libtrace, ts.timehigh); 1132 epkthdr.timestamp_low = pcapng_swap32(libtrace, ts.timelow); 1133 epkthdr.wlen = pcapng_swap32(libtrace, wirelen); 1134 epkthdr.caplen = pcapng_swap32(libtrace, caplen); 1135 1136 /* output enhanced packet header */ 1137 wandio_wwrite(DATAOUT(libtrace)->file, &epkthdr, sizeof(epkthdr)); 1138 /* output the packet */ 1139 wandio_wwrite(DATAOUT(libtrace)->file, link, (size_t)caplen); 1140 /* output padding */ 1141 wandio_wwrite(DATAOUT(libtrace)->file, padding_data, (size_t)padding); 1142 /* output rest of the enhanced packet */ 1143 wandio_wwrite(DATAOUT(libtrace)->file, &epkthdr.blocklen, sizeof(epkthdr.blocklen)); 1144 1145 /* release padding memory */ 1146 free(padding_data); 1147 1148 return blocklen; 1149 } 1150 1151 static int pcapng_flush_output(libtrace_out_t *libtrace) { 1152 return wandio_wflush(DATAOUT(libtrace)->file); 1153 } 1154 431 1155 static int pcapng_read_section(libtrace_t *libtrace, 432 1156 libtrace_packet_t *packet, uint32_t flags) { … … 434 1158 pcapng_sec_t *sechdr; 435 1159 int err; 436 uint32_t to_read ;1160 uint32_t to_read, blocklen; 437 1161 char *bodyptr = NULL; 438 1162 … … 456 1180 } 457 1181 458 assert(sechdr->blocktype == PCAPNG_SECTION_TYPE); 1182 if (sechdr->blocktype != PCAPNG_SECTION_TYPE) { 1183 trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, "Invalid block type in pcapng section block"); 1184 return -1; 1185 } 459 1186 460 1187 if (sechdr->ordering == 0x1A2B3C4D) { … … 475 1202 return -1; 476 1203 } 477 to_read = byteswap32(sechdr->blocklen) - sizeof(pcapng_sec_t); 1204 blocklen = byteswap32(sechdr->blocklen); 1205 478 1206 } else { 479 1207 if (sechdr->majorversion != 1 && sechdr->minorversion != 0) { … … 482 1210 return -1; 483 1211 } 484 to_read = sechdr->blocklen - sizeof(pcapng_sec_t); 485 } 486 1212 blocklen = sechdr->blocklen; 1213 } 1214 1215 if (blocklen < sizeof(pcapng_sec_t)) { 1216 trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, 1217 "Block length in pcapng section header is invalid."); 1218 return -1; 1219 } 1220 to_read = blocklen - sizeof(pcapng_sec_t); 487 1221 /* Read all of the options etc. -- we don't need them for now, but 488 1222 * we have to skip forward to the next useful header. */ 489 1223 bodyptr = (char *) packet->buffer + sizeof(pcapng_sec_t); 1224 1225 if (to_read > LIBTRACE_PACKET_BUFSIZE) { 1226 trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, 1227 "Excessively large section header contents of %u bytes, likely a corrupt trace.", to_read); 1228 return -1; 1229 } 1230 490 1231 err = pcapng_read_body(libtrace, bodyptr, to_read); 491 1232 if (err <= 0) { … … 531 1272 532 1273 if (DATA(libtrace)->byteswapped) { 533 assert(byteswap32(inthdr->blocktype) == PCAPNG_INTERFACE_TYPE); 1274 if (byteswap32(inthdr->blocktype) != PCAPNG_INTERFACE_TYPE) { 1275 trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, "Invalid block type in pcapng interface"); 1276 return -1; 1277 } 534 1278 newint->snaplen = byteswap32(inthdr->snaplen); 535 1279 newint->linktype = byteswap16(inthdr->linktype); 536 1280 } else { 537 assert(inthdr->blocktype == PCAPNG_INTERFACE_TYPE); 1281 if (inthdr->blocktype != PCAPNG_INTERFACE_TYPE) { 1282 trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, "Invalid block type in pcapng interface"); 1283 return -1; 1284 } 538 1285 newint->snaplen = inthdr->snaplen; 539 1286 newint->linktype = inthdr->linktype; … … 606 1353 /* Read the rest of the packet into the buffer */ 607 1354 if (DATA(libtrace)->byteswapped) { 608 assert(byteswap32(hdr->blocktype) == PCAPNG_NAME_RESOLUTION_TYPE); 1355 if (byteswap32(hdr->blocktype) != PCAPNG_NAME_RESOLUTION_TYPE) { 1356 trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, "Invalid block type in pcapng name " 1357 "resolution block"); 1358 return -1; 1359 } 609 1360 } else { 610 assert(hdr->blocktype == PCAPNG_NAME_RESOLUTION_TYPE); 1361 if (hdr->blocktype != PCAPNG_NAME_RESOLUTION_TYPE) { 1362 trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, "Invalid block type in pcapng name " 1363 "resolution block"); 1364 return -1; 1365 } 611 1366 } 612 1367 … … 640 1395 /* Read the rest of the packet into the buffer */ 641 1396 if (DATA(libtrace)->byteswapped) { 642 assert(byteswap32(hdr->blocktype) == PCAPNG_CUSTOM_TYPE || 643 byteswap32(hdr->blocktype) == PCAPNG_CUSTOM_NONCOPY_TYPE); 1397 if (byteswap32(hdr->blocktype) != PCAPNG_CUSTOM_TYPE || 1398 byteswap32(hdr->blocktype) != PCAPNG_CUSTOM_NONCOPY_TYPE) { 1399 trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, "Invalid blocktype " 1400 "in pcapng custom block"); 1401 return -1; 1402 } 644 1403 } else { 645 assert(hdr->blocktype == PCAPNG_CUSTOM_TYPE || 646 hdr->blocktype == PCAPNG_CUSTOM_NONCOPY_TYPE); 1404 if (hdr->blocktype != PCAPNG_CUSTOM_TYPE || 1405 hdr->blocktype != PCAPNG_CUSTOM_NONCOPY_TYPE) { 1406 trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, "Invalid blocktype " 1407 "in pcapng custom block"); 1408 return -1; 1409 } 647 1410 } 648 1411 … … 677 1440 /* Read the rest of the packet into the buffer */ 678 1441 if (DATA(libtrace)->byteswapped) { 679 assert(byteswap32(hdr->blocktype) == PCAPNG_INTERFACE_STATS_TYPE); 1442 if (byteswap32(hdr->blocktype) != PCAPNG_INTERFACE_STATS_TYPE) { 1443 trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, "Invalid block type " 1444 "in pcapng statistics block"); 1445 return -1; 1446 } 680 1447 ifaceid = byteswap32(hdr->interfaceid); 681 1448 timestamp = ((uint64_t)(byteswap32(hdr->timestamp_high)) << 32) + byteswap32(hdr->timestamp_low); 682 1449 } else { 683 assert(hdr->blocktype == PCAPNG_INTERFACE_STATS_TYPE); 1450 if (hdr->blocktype != PCAPNG_INTERFACE_STATS_TYPE) { 1451 trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, "Invalid block type " 1452 "in pcapng statistics block"); 1453 return -1; 1454 } 684 1455 ifaceid = hdr->interfaceid; 685 1456 timestamp = ((uint64_t)(hdr->timestamp_high) << 32) + … … 776 1547 /* Read the rest of the packet into the buffer */ 777 1548 if (DATA(libtrace)->byteswapped) { 778 assert(byteswap32(hdr->blocktype) == PCAPNG_SIMPLE_PACKET_TYPE); 1549 if (byteswap32(hdr->blocktype) != PCAPNG_SIMPLE_PACKET_TYPE) { 1550 trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, "Invalid block type in " 1551 "pcapng simple packet block"); 1552 return -1; 1553 } 779 1554 caplen = byteswap32(hdr->blocklen) - sizeof(pcapng_spkt_t) - 4; 780 1555 /* account for trailing length field */ 781 1556 } else { 782 assert(hdr->blocktype == PCAPNG_SIMPLE_PACKET_TYPE); 1557 if (hdr->blocktype != PCAPNG_SIMPLE_PACKET_TYPE) { 1558 trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, "Invalid block type in " 1559 "pcapng simple packet block"); 1560 return -1; 1561 } 783 1562 caplen = hdr->blocklen - sizeof(pcapng_spkt_t) - 4; 784 1563 /* account for trailing length field */ … … 796 1575 /* May as well cache the capture length now, since we've 797 1576 * already got it in the right byte order */ 798 packet->ca pture_length = caplen;1577 packet->cached.capture_length = caplen; 799 1578 800 1579 if (pcapng_prepare_packet(libtrace, packet, packet->buffer, … … 826 1605 /* Read the rest of the packet into the buffer */ 827 1606 if (DATA(libtrace)->byteswapped) { 828 assert(byteswap32(hdr->blocktype) == PCAPNG_ENHANCED_PACKET_TYPE); 1607 if (byteswap32(hdr->blocktype) != PCAPNG_ENHANCED_PACKET_TYPE) { 1608 trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, "Invalid block type in " 1609 "pcapng enhanced packet block"); 1610 return -1; 1611 } 829 1612 caplen = byteswap32(hdr->caplen); 830 1613 ifaceid = byteswap32(hdr->interfaceid); 831 1614 } else { 832 assert(hdr->blocktype == PCAPNG_ENHANCED_PACKET_TYPE); 1615 if (hdr->blocktype != PCAPNG_ENHANCED_PACKET_TYPE) { 1616 trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, "Invalid block type in " 1617 "pcapng enhanced packet block"); 1618 return -1; 1619 } 833 1620 caplen = hdr->caplen; 834 1621 ifaceid = hdr->interfaceid; … … 847 1634 /* May as well cache the capture length now, since we've 848 1635 * already got it in the right byte order */ 849 packet->ca pture_length = caplen;1636 packet->cached.capture_length = caplen; 850 1637 851 1638 if (pcapng_prepare_packet(libtrace, packet, packet->buffer, … … 899 1686 int gotpacket = 0; 900 1687 1688 /* Ensure trace and packet are not NULL */ 1689 if (!libtrace) { 1690 fprintf(stderr, "NULL trace passed into pcapng_read_packet()\n"); 1691 return TRACE_ERR_NULL_TRACE; 1692 } 1693 if (!packet) { 1694 trace_set_err(libtrace, TRACE_ERR_NULL_PACKET, "NULL packet passed into " 1695 "pcapng_read_packet()\n"); 1696 return -1; 1697 } 1698 901 1699 /* Peek to get next block type */ 902 assert(libtrace->format_data); 903 assert(libtrace->io); 1700 if (!libtrace->format_data) { 1701 trace_set_err(libtrace, TRACE_ERR_BAD_FORMAT, "Trace has no format data in " 1702 "pcapng_read_packet()"); 1703 return -1; 1704 } 1705 if (!libtrace->io) { 1706 trace_set_err(libtrace, TRACE_ERR_BAD_IO, "Trace has no valid file handle " 1707 "attached to it in pcapng_read_packet()"); 1708 return -1; 1709 } 904 1710 905 1711 if (!packet->buffer || packet->buf_control == TRACE_CTRL_EXTERNAL) { … … 1021 1827 } 1022 1828 1023 static libtrace_linktype_t pcapng_get_link_type(const libtrace_packet_t *packet) 1024 { 1829 static libtrace_linktype_t pcapng_get_link_type(const libtrace_packet_t *packet) { 1830 1831 if (packet->type == TRACE_RT_PCAPNG_META) { 1832 return TRACE_TYPE_PCAPNG_META; 1833 } 1025 1834 1026 1835 return pcap_linktype_to_libtrace(rt_to_pcap_linktype(packet->type)); 1027 1028 1836 } 1029 1837 … … 1042 1850 pcapng_interface_t *interface; 1043 1851 1044 assert(packet->header); 1852 memset(&ts, 0, sizeof(struct timespec)); 1853 1854 if (!packet) { 1855 fprintf(stderr, "NULL packet passed into pcapng_get_timespec()"); 1856 /* Return default timespec on error? */ 1857 return ts; 1858 } 1859 if (!packet->header) { 1860 trace_set_err(packet->trace, TRACE_ERR_BAD_PACKET, "NULL header in packet in pcapng_get_timespec()"); 1861 /* Return default timespec on error? */ 1862 return ts; 1863 } 1045 1864 1046 1865 ts.tv_sec = 0; … … 1269 2088 } 1270 2089 } 1271 packet->ca pture_length = -1;2090 packet->cached.capture_length = -1; 1272 2091 return trace_get_capture_length(packet); 1273 2092 } … … 1356 2175 pcapng_start_input, /* start_input */ 1357 2176 NULL, /* pause_input */ 1358 NULL,/* init_output */1359 NULL,/* config_output */2177 pcapng_init_output, /* init_output */ 2178 pcapng_config_output, /* config_output */ 1360 2179 NULL, /* start_output */ 1361 2180 pcapng_fin_input, /* fin_input */ 1362 NULL,/* fin_output */2181 pcapng_fin_output, /* fin_output */ 1363 2182 pcapng_read_packet, /* read_packet */ 1364 2183 pcapng_prepare_packet, /* prepare_packet */ 1365 2184 NULL, /* fin_packet */ 1366 NULL,/* write_packet */1367 NULL,/* flush_output */2185 pcapng_write_packet, /* write_packet */ 2186 pcapng_flush_output, /* flush_output */ 1368 2187 pcapng_get_link_type, /* get_link_type */ 1369 2188 pcapng_get_direction, /* get_direction */ … … 1394 2213 register_format(&pcapng); 1395 2214 } 1396 -
lib/format_rt.c
r32ee9b2 r2193905 37 37 38 38 #include <sys/stat.h> 39 #include <assert.h>40 39 #include <errno.h> 41 40 #include <fcntl.h> … … 233 232 234 233 /* If the user specifies "rt:" then assume localhost and the default 235 * port */ 234 * port */ 236 235 if (strlen(uridata) == 0) { 237 236 RT_INFO->hostname = … … 611 610 packet->payload = RT_INFO->buf_read + sizeof(rt_header_t); 612 611 packet->internalid = libtrace_push_into_bucket(RT_INFO->bucket); 613 assert(packet->internalid != 0); 612 if (!packet->internalid) { 613 trace_set_err(libtrace, TRACE_ERR_RT_FAILURE, "packet->internalid is 0 in rt_get_next_packet()"); 614 return -1; 615 } 614 616 packet->srcbucket = RT_INFO->bucket; 615 617 packet->buf_control = TRACE_CTRL_EXTERNAL; … … 756 758 libtrace_err_t read_err; 757 759 758 assert(trace); 759 assert(packet); 760 760 if (!trace) { 761 fprintf(stderr, "NULL trace passed into trace_event_rt()\n"); 762 /* Return empty event on error? */ 763 return event; 764 } 765 if (!packet) { 766 trace_set_err(trace, TRACE_ERR_NULL_PACKET, "NULL packet passed into trace_event_rt()"); 767 /* Return empty event on error? */ 768 return event; 769 } 770 761 771 if (trace->format->get_fd) { 762 772 event.fd = trace->format->get_fd(trace); -
lib/format_tsh.c
r32ee9b2 r2193905 31 31 #include "format_helper.h" 32 32 33 #include <assert.h>34 33 #include <errno.h> 35 34 #include <fcntl.h> -
lib/libtrace.h.in
rfe4940e r49f8ceb 307 307 TRACE_ERR_UNSUPPORTED_COMPRESS = -11, 308 308 /** Wandio has returned an error */ 309 TRACE_ERR_WANDIO_FAILED = -12 309 TRACE_ERR_WANDIO_FAILED = -12, 310 /** Input URI (file) not found */ 311 TRACE_ERR_URI_NOT_FOUND = -13, 312 /** NULL passed to create trace **/ 313 TRACE_ERR_URI_NULL = -14, 314 /** NULL trace passed to trace_start **/ 315 TRACE_ERR_NULL_TRACE = -15, 316 /** Unable to finish last packet in trace_pause **/ 317 TRACE_ERR_PAUSE_FIN = -16, 318 /** Packet is NULL **/ 319 TRACE_ERR_NULL_PACKET = -17, 320 /** Filter is NULL **/ 321 TRACE_ERR_NULL_FILTER = -18, 322 /** Buffer is NULL **/ 323 TRACE_ERR_NULL_BUFFER = -19, 324 /** Trace stats err **/ 325 TRACE_ERR_STAT = -20, 326 /** Unable to create deadtrace **/ 327 TRACE_ERR_CREATE_DEADTRACE = -21, 328 /** Bad linktype **/ 329 TRACE_ERR_BAD_LINKTYPE = -22, 330 /** Bad IO for the trace **/ 331 TRACE_ERR_BAD_IO = -23, 332 /** Trace has bad header **/ 333 TRACE_ERR_BAD_HEADER = -24, 334 /** Trace err seeking by erf **/ 335 TRACE_ERR_SEEK_ERF = -25, 336 /** Trace err combiner **/ 337 TRACE_ERR_COMBINER = -26, 338 /** Error pausing processing thread **/ 339 TRACE_ERR_PAUSE_PTHREAD = -27, 340 /** Error with trace thread **/ 341 TRACE_ERR_THREAD = -28, 342 /** Thread in unexpecting state **/ 343 TRACE_ERR_THREAD_STATE = -29, 344 /** Trace configuration error **/ 345 TRACE_ERR_CONFIG = -30, 346 /** NULL passed misc **/ 347 TRACE_ERR_NULL = -31, 348 /** Err with trace output file **/ 349 TRACE_ERR_OUTPUT_FILE = -32, 310 350 }; 311 351 … … 372 412 TRACE_TYPE_OPENBSD_LOOP = 20, /**< OpenBSD loopback */ 373 413 TRACE_TYPE_ERF_META = 21, /**< ERF Provenance metadata record */ 374 TRACE_TYPE_ETSILI = 22 /**< ETSI Lawful Intercept */ 414 TRACE_TYPE_ETSILI = 22, /**< ETSI Lawful Intercept */ 415 TRACE_TYPE_PCAPNG_META = 23, /** PCAPNG meta packet */ 375 416 } libtrace_linktype_t; 376 417 … … 401 442 TRACE_FORMAT_DPDK_NDAG =20, /**< DAG multicast over a network, received via DPDK */ 402 443 TRACE_FORMAT_ETSILIVE =21, /**< ETSI LI over a network */ 444 TRACE_FORMAT_UNKNOWN =22, /** Unknown format */ 403 445 }; 404 446 … … 538 580 } libtrace_ethertype_t; 539 581 540 /** The libtrace packet structure. Applications shouldn't be 541 * meddling around in here 542 */ 543 typedef struct libtrace_packet_t { 544 struct libtrace_t *trace; /**< Pointer to the trace */ 545 void *header; /**< Pointer to the framing header */ 546 void *payload; /**< Pointer to the link layer */ 547 void *buffer; /**< Allocated buffer */ 548 libtrace_rt_types_t type; /**< RT protocol type for the packet */ 549 buf_control_t buf_control; /**< Describes memory ownership */ 582 typedef struct libtrace_packet_cache { 550 583 int capture_length; /**< Cached capture length */ 551 584 int wire_length; /**< Cached wire length */ … … 561 594 uint8_t transport_proto; /**< Cached transport protocol */ 562 595 uint32_t l4_remaining; /**< Cached transport remaining */ 596 } libtrace_packet_cache_t; 597 598 /** The libtrace packet structure. Applications shouldn't be 599 * meddling around in here 600 */ 601 typedef struct libtrace_packet_t { 602 struct libtrace_t *trace; /**< Pointer to the trace */ 603 void *header; /**< Pointer to the framing header */ 604 void *payload; /**< Pointer to the link layer */ 605 void *buffer; /**< Allocated buffer */ 606 libtrace_rt_types_t type; /**< RT protocol type for the packet */ 607 buf_control_t buf_control; /**< Describes memory ownership */ 608 libtrace_packet_cache_t cached; /**< Cached packet properties / headers */ 563 609 uint64_t order; /**< Notes the order of this packet in relation to the input */ 564 610 uint64_t hash; /**< A hash of the packet as supplied by the user */ … … 1327 1373 /** Speed up trace file replays (via trace_event()) by this factor */ 1328 1374 TRACE_OPTION_REPLAY_SPEEDUP, 1375 1376 /** Always assume ERF framing length is the given value, rather than 1377 * trying to calculate it from the packet type. */ 1378 TRACE_OPTION_CONSTANT_ERF_FRAMING, 1329 1379 } trace_option_t; 1330 1380 -
lib/linktypes.c
r91bd90e r49f8ceb 28 28 29 29 #include "rt_protocol.h" 30 #include <assert.h>31 30 #include "libtrace_int.h" 32 31 #include <stdlib.h> … … 111 110 case TRACE_TYPE_ETSILI: 112 111 break; 112 case TRACE_TYPE_PCAPNG_META: 113 113 case TRACE_TYPE_UNKNOWN: 114 114 case TRACE_TYPE_CONTENT_INVALID: … … 162 162 163 163 fprintf(stderr, "Error: RT type %u cannot be converted to a pcap DLT\n", rt_type); 164 assert(false);165 164 return 0; /* satisfy warnings */ 166 165 } … … 214 213 case TRACE_TYPE_OPENBSD_LOOP: 215 214 case TRACE_TYPE_ETSILI: 215 case TRACE_TYPE_PCAPNG_META: 216 216 case TRACE_TYPE_UNKNOWN: 217 217 case TRACE_TYPE_CONTENT_INVALID: -
lib/protocols.h
ree6e802 rbe1b03a 24 24 * 25 25 */ 26 27 #ifndef LIBTRACE_PROTOCOLS_H_ 28 #define LIBTRACE_PROTOCOLS_H_ 26 29 27 30 /** @file … … 81 84 }; 82 85 83 86 #endif -
lib/protocols_l2.c
rfe4940e r385678b 27 27 #include "libtrace.h" 28 28 #include "protocols.h" 29 #include <assert.h>30 29 #include <stdlib.h> 31 30 #include <string.h> … … 182 181 (dest - (char *)packet->payload)); 183 182 packet->payload = nextpayload - (dest - (char *)packet->payload); 184 packet-> l2_header = NULL;183 packet->cached.l2_header = NULL; 185 184 } 186 185 … … 194 193 */ 195 194 void *trace_get_payload_from_mpls(void *ethernet, uint16_t *type, 196 uint32_t *remaining) 197 { 198 199 assert(type); 195 uint32_t *remaining) { 196 /* Ensure supplied type is not NULL */ 197 if (!type) { 198 fprintf(stderr, "NULL type passed into trace_get_payload_from_mpls()\n"); 199 return NULL; 200 } 201 200 202 if ((((char*)ethernet)[2]&0x01)==0) { 201 203 /* The MPLS Stack bit is set */ … … 327 329 void *trace_get_payload_from_pppoe(void *link, uint16_t *type, 328 330 uint32_t *remaining) { 329 assert(type); 330 331 /* Ensure type supplied is not NULL */ 332 if (!type) { 333 fprintf(stderr, "NULL type passed into trace_get_payload_from_pppoe()\n"); 334 return NULL; 335 } 336 331 337 if (remaining) { 332 338 if (*remaining < sizeof(libtrace_pppoe_t)) { … … 437 443 uint32_t dummyrem; 438 444 void *meta = NULL; 439 440 assert(packet != NULL); 441 assert(linktype != NULL); 445 446 if (!packet) { 447 fprintf(stderr, "NULL packet passed into trace_get_layer2()\n"); 448 return NULL; 449 } 450 if (!linktype) { 451 fprintf(stderr, "NULL linktype passed into trace_get_layer2()\n"); 452 return NULL; 453 } 442 454 443 455 if (remaining == NULL) 444 456 remaining = &dummyrem; 445 457 446 if (packet-> l2_header) {458 if (packet->cached.l2_header) { 447 459 /* Use cached values */ 448 *linktype = packet-> link_type;449 *remaining = packet-> l2_remaining;450 return packet-> l2_header;460 *linktype = packet->cached.link_type; 461 *remaining = packet->cached.l2_remaining; 462 return packet->cached.l2_header; 451 463 } 452 464 … … 454 466 * calling trace_get_packet_buffer more than once like we used to. 455 467 */ 456 457 468 meta = trace_get_packet_buffer(packet, linktype, remaining); 469 if (meta == NULL) { 470 return NULL; 471 } 458 472 459 473 /* If there are no meta-data headers, we just return the start of the … … 475 489 case TRACE_TYPE_NONDATA: 476 490 case TRACE_TYPE_OPENBSD_LOOP: 477 ((libtrace_packet_t*)packet)-> l2_header = meta;478 ((libtrace_packet_t*)packet)-> l2_remaining = *remaining;491 ((libtrace_packet_t*)packet)->cached.l2_header = meta; 492 ((libtrace_packet_t*)packet)->cached.l2_remaining = *remaining; 479 493 return meta; 480 494 case TRACE_TYPE_LINUX_SLL: … … 483 497 case TRACE_TYPE_PFLOG: 484 498 case TRACE_TYPE_ERF_META: 499 case TRACE_TYPE_PCAPNG_META: 485 500 case TRACE_TYPE_ETSILI: 486 501 break; … … 513 528 case TRACE_TYPE_NONDATA: 514 529 case TRACE_TYPE_OPENBSD_LOOP: 515 ((libtrace_packet_t*)packet)-> l2_header = meta;516 ((libtrace_packet_t*)packet)-> l2_remaining = *remaining;530 ((libtrace_packet_t*)packet)->cached.l2_header = meta; 531 ((libtrace_packet_t*)packet)->cached.l2_remaining = *remaining; 517 532 return meta; 518 533 case TRACE_TYPE_LINUX_SLL: … … 521 536 case TRACE_TYPE_PFLOG: 522 537 case TRACE_TYPE_ERF_META: 538 case TRACE_TYPE_PCAPNG_META: 523 539 case TRACE_TYPE_ETSILI: 524 540 break; … … 572 588 return NULL; 573 589 } 574 590 591 if (link == NULL) { 592 return NULL; 593 } 594 575 595 switch(linktype) { 576 596 /* Packet Metadata headers, not layer2 headers */ … … 591 611 case TRACE_TYPE_METADATA: 592 612 case TRACE_TYPE_NONDATA: 613 case TRACE_TYPE_PCAPNG_META: 593 614 case TRACE_TYPE_ERF_META: 594 615 case TRACE_TYPE_CONTENT_INVALID: … … 601 622 return trace_get_payload_from_ethernet(link,ethertype,remaining); 602 623 case TRACE_TYPE_NONE: 624 if (*remaining == 0) { 625 return NULL; 626 } 627 603 628 if ((*(char*)link&0xF0) == 0x40) 604 629 *ethertype=TRACE_ETHERTYPE_IP; /* IPv4 */ 605 630 else if ((*(char*)link&0xF0) == 0x60) 606 631 *ethertype=TRACE_ETHERTYPE_IPV6; /* IPv6 */ 632 else 633 return NULL; /* No idea */ 607 634 return link; /* I love the simplicity */ 608 635 case TRACE_TYPE_PPP: … … 629 656 630 657 case TRACE_TYPE_OPENBSD_LOOP: 658 if (*remaining <= 4) { 659 return NULL; 660 } 631 661 link = link + 4; /* Loopback header is 4 bytes */ 632 662 if ((*(char*)link&0xF0) == 0x40) … … 634 664 else if ((*(char*)link&0xF0) == 0x60) 635 665 *ethertype=TRACE_ETHERTYPE_IPV6; /* IPv6 */ 666 else 667 return NULL; 636 668 return link; /* I love the simplicity */ 637 669 … … 668 700 669 701 DLLEXPORT uint8_t *trace_get_source_mac(libtrace_packet_t *packet) { 702 /* Ensure the supplied packet is not NULL */ 703 if (!packet) { 704 fprintf(stderr, "NULL packet passed into trace_get_source_mac()\n"); 705 return NULL; 706 } 707 670 708 void *link; 671 709 uint32_t remaining; 672 710 libtrace_linktype_t linktype; 673 assert(packet);674 711 link = trace_get_layer2(packet,&linktype,&remaining); 675 712 … … 696 733 case TRACE_TYPE_OPENBSD_LOOP: 697 734 case TRACE_TYPE_ERF_META: 735 case TRACE_TYPE_PCAPNG_META: 698 736 case TRACE_TYPE_UNKNOWN: 699 737 case TRACE_TYPE_CONTENT_INVALID: … … 705 743 case TRACE_TYPE_80211_RADIO: 706 744 case TRACE_TYPE_ETSILI: 707 assert(!"Metadata headers should already be skipped");708 break;745 fprintf(stderr, "Metadata headers should already be skipped in trace_get_source_mac()\n"); 746 return NULL; 709 747 } 710 748 fprintf(stderr,"%s not implemented for linktype %i\n", __func__, linktype); 711 assert(0);712 749 return NULL; 713 750 } 714 751 715 DLLEXPORT uint8_t *trace_get_destination_mac(libtrace_packet_t *packet) 716 { 752 DLLEXPORT uint8_t *trace_get_destination_mac(libtrace_packet_t *packet) { 753 /* Ensure the supplied packet is not NULL */ 754 if (!packet) { 755 fprintf(stderr, "NULL packet passed into trace_get_destination_mac()\n"); 756 return NULL; 757 } 758 717 759 void *link; 718 760 libtrace_linktype_t linktype; … … 748 790 case TRACE_TYPE_OPENBSD_LOOP: 749 791 case TRACE_TYPE_ERF_META: 792 case TRACE_TYPE_PCAPNG_META: 750 793 case TRACE_TYPE_UNKNOWN: 751 794 case TRACE_TYPE_CONTENT_INVALID: … … 757 800 case TRACE_TYPE_80211_RADIO: 758 801 case TRACE_TYPE_ETSILI: 759 assert(!"Metadata headers should already be skipped");760 break;