From: Francois-Xavier Le Bail Date: Wed, 14 Mar 2018 12:41:33 +0000 (+0100) Subject: Add a malloc/free process with garbage collector X-Git-Tag: tcpdump-4.99-bp~1233 X-Git-Url: https://git.tcpdump.org/tcpdump/commitdiff_plain/a53605c61b1615362347cebf0db7bb54a2ee6ac6 Add a malloc/free process with garbage collector Use it in the PPP printer. --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 6a10295d..d0154774 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -771,6 +771,8 @@ set(NETDISSECT_SOURCE_LIST_C ipproto.c l2vpn.c machdep.c + netdissect.c + netdissect-alloc.c nlpid.c oui.c parsenfsfh.c @@ -919,7 +921,6 @@ set(NETDISSECT_SOURCE_LIST_C print-zephyr.c print-zeromq.c ${LOCALSRC} - netdissect.c signature.c strtoaddr.c util-print.c diff --git a/Makefile.in b/Makefile.in index 67ee3281..f32a11d1 100644 --- a/Makefile.in +++ b/Makefile.in @@ -85,6 +85,8 @@ LIBNETDISSECT_SRC=\ ipproto.c \ l2vpn.c \ machdep.c \ + netdissect.c \ + netdissect-alloc.c \ nlpid.c \ oui.c \ parsenfsfh.c \ @@ -232,7 +234,6 @@ LIBNETDISSECT_SRC=\ print-wb.c \ print-zephyr.c \ print-zeromq.c \ - netdissect.c \ signature.c \ strtoaddr.c \ util-print.c @@ -278,6 +279,7 @@ HDR = \ mpls.h \ nameser.h \ netdissect.h \ + netdissect-alloc.h \ netdissect-stdinc.h \ nfs.h \ nfsfh.h \ diff --git a/netdissect-alloc.c b/netdissect-alloc.c new file mode 100644 index 00000000..1a40b09c --- /dev/null +++ b/netdissect-alloc.c @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2018 The TCPDUMP project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that: (1) source code + * distributions retain the above copyright notice and this paragraph + * in its entirety, and (2) distributions including binary code include + * the above copyright notice and this paragraph in its entirety in + * the documentation or other materials provided with the distribution. + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND + * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT + * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE. + */ + +#include +#include "netdissect-alloc.h" + +/* + * nd_free_all() is intended to be used after a packet printing + */ + +/* Add a memory chunk in allocation linked list */ +void +nd_add_alloc_list(netdissect_options *ndo, nd_mem_chunk_t *chunkp) +{ + if (ndo->ndo_last_mem_p == NULL) /* first memory allocation */ + chunkp->prev_mem_p = NULL; + else /* previous memory allocation */ + chunkp->prev_mem_p = ndo->ndo_last_mem_p; + ndo->ndo_last_mem_p = chunkp; +} + +/* malloc replacement, with tracking in a linked list */ +void * +nd_malloc(netdissect_options *ndo, size_t size) +{ + nd_mem_chunk_t *chunkp = malloc(sizeof(nd_mem_chunk_t) + size); + if (chunkp == NULL) + return NULL; + nd_add_alloc_list(ndo, chunkp); + return chunkp + 1; +} + +/* Free chunks in allocation linked list from last to first */ +void +nd_free_all(netdissect_options *ndo) +{ + nd_mem_chunk_t *current, *previous; + current = ndo->ndo_last_mem_p; + while (current != NULL) { + previous = current->prev_mem_p; + free(current); + current = previous; + } + ndo->ndo_last_mem_p = NULL; +} diff --git a/netdissect-alloc.h b/netdissect-alloc.h new file mode 100644 index 00000000..aa28a368 --- /dev/null +++ b/netdissect-alloc.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2018 The TCPDUMP project + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that: (1) source code + * distributions retain the above copyright notice and this paragraph + * in its entirety, and (2) distributions including binary code include + * the above copyright notice and this paragraph in its entirety in + * the documentation or other materials provided with the distribution. + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND + * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT + * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE. + */ + +#ifndef netdissect_alloc_h +#define netdissect_alloc_h + +#include +#include "netdissect-stdinc.h" +#include "netdissect.h" + +typedef struct nd_mem_chunk { + void *prev_mem_p; + /* variable size data */ +} nd_mem_chunk_t; + +void nd_add_alloc_list(netdissect_options *, nd_mem_chunk_t *); +void * nd_malloc(netdissect_options *, size_t); +void nd_free_all(netdissect_options *); + +#endif /* netdissect_alloc_h */ diff --git a/netdissect.h b/netdissect.h index 73fec2e2..5c33ed5e 100644 --- a/netdissect.h +++ b/netdissect.h @@ -182,6 +182,7 @@ struct netdissect_options { * LF, CR and SPACE as graphical chars */ int ndo_Hflag; /* dissect 802.11s draft mesh standard */ + void *ndo_last_mem_p; /* pointer to the last allocated memory chunk */ int ndo_packet_number; /* print a packet number in the beginning of line */ int ndo_suppress_default_print; /* don't use default_print() for unknown packet types */ int ndo_tstamp_precision; /* requested time stamp precision */ diff --git a/print-ppp.c b/print-ppp.c index 5230eef7..7631ca58 100644 --- a/print-ppp.c +++ b/print-ppp.c @@ -51,6 +51,7 @@ #include "chdlc.h" #include "ethertype.h" #include "oui.h" +#include "netdissect-alloc.h" /* * The following constants are defined by IANA. Please refer to @@ -1401,7 +1402,7 @@ ppp_hdlc(netdissect_options *ndo, if (length == 0) return; - b = (u_char *)malloc(length); + b = (u_char *)nd_malloc(ndo, length); if (b == NULL) return; @@ -1464,12 +1465,10 @@ ppp_hdlc(netdissect_options *ndo, cleanup: ndo->ndo_snapend = se; - free(b); return; trunc: ndo->ndo_snapend = se; - free(b); ND_PRINT("[|ppp]"); } diff --git a/print.c b/print.c index fd95b512..4417a4fa 100644 --- a/print.c +++ b/print.c @@ -37,6 +37,7 @@ #include "netdissect.h" #include "addrtoname.h" #include "print.h" +#include "netdissect-alloc.h" struct printer { if_printer f; @@ -448,6 +449,7 @@ pretty_print_packet(netdissect_options *ndo, const struct pcap_pkthdr *h, } ND_PRINT("\n"); + nd_free_all(ndo); } /*