]> The Tcpdump Group git mirrors - tcpdump/commitdiff
Add a malloc/free process with garbage collector
authorFrancois-Xavier Le Bail <[email protected]>
Wed, 14 Mar 2018 12:41:33 +0000 (13:41 +0100)
committerFrancois-Xavier Le Bail <[email protected]>
Wed, 14 Mar 2018 12:59:26 +0000 (13:59 +0100)
Use it in the PPP printer.

CMakeLists.txt
Makefile.in
netdissect-alloc.c [new file with mode: 0644]
netdissect-alloc.h [new file with mode: 0644]
netdissect.h
print-ppp.c
print.c

index 6a10295dd53430d16f3369ecfa4a65a8166f75a2..d015477489ec24bc00995f9b1cbd362934a6ccb9 100644 (file)
@@ -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
index 67ee328139154155621bbd6de7f6e4b014a046d9..f32a11d1a60681eb2ecd7c8ba8aa80f688833310 100644 (file)
@@ -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 (file)
index 0000000..1a40b09
--- /dev/null
@@ -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 <stdlib.h>
+#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 (file)
index 0000000..aa28a36
--- /dev/null
@@ -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 <stdarg.h>
+#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 */
index 73fec2e2505ababc009b84650b9c635be24ece16..5c33ed5e25cf77e38dd6d8b6b3604f29b40b46aa 100644 (file)
@@ -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 */
index 5230eef75fa4ca24d34d399545f6cdb1bd1be1b4..7631ca586276fc5ba45545cdd280e8171704096a 100644 (file)
@@ -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 fd95b5125b4c1d7abf26cd662cb603429eedd8a7..4417a4fa2523b07597051ff150376bbf8d77cb85 100644 (file)
--- 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);
 }
 
 /*