pcap_rw: simplify code by using raw
authorDaniel Borkmann <dborkman@redhat.com>
Tue, 29 Jan 2013 16:32:37 +0000 (29 17:32 +0100)
committerDaniel Borkmann <dborkman@redhat.com>
Tue, 29 Jan 2013 16:32:37 +0000 (29 17:32 +0100)
Signed-off-by: Daniel Borkmann <dborkman@redhat.com>
pcap.h
pcap_rw.c

diff --git a/pcap.h b/pcap.h
index e1cd5b3..8062aea 100644 (file)
--- a/pcap.h
+++ b/pcap.h
@@ -89,6 +89,7 @@ typedef union {
        struct pcap_pkthdr_ns   ppn;
        struct pcap_pkthdr_kuz  ppk;
        struct pcap_pkthdr_bkm  ppb;
+       uint8_t                 raw;
 } pcap_pkthdr_t;
 
 enum pcap_type {
index c8ff55e..0ef5321 100644 (file)
--- a/pcap_rw.c
+++ b/pcap_rw.c
 static ssize_t pcap_rw_write(int fd, pcap_pkthdr_t *phdr, enum pcap_type type,
                             const uint8_t *packet, size_t len)
 {
-       ssize_t ret, hdrlen = 0, hdrsize = 0;
-
-       switch (type) {
-#define PCAP_HDR_WRITE(__member__) do { \
-                       hdrlen = phdr->__member__.caplen; \
-                       hdrsize = sizeof(phdr->__member__); \
-                       ret = write_or_die(fd, &phdr->__member__, hdrsize); \
-               } while (0)
-#define CASE_HDR_WRITE(what, member) \
-        case (what): \
-               PCAP_HDR_WRITE(member); \
-                break
-       CASE_HDR_WRITE(DEFAULT, ppo);
-       CASE_HDR_WRITE(NSEC, ppn);
-       CASE_HDR_WRITE(KUZNETZOV, ppk);
-       CASE_HDR_WRITE(BORKMANN, ppb);
-       default:
-               bug();
-       }
+       ssize_t ret, hdrsize = pcap_get_hdr_length(phdr, type), hdrlen = 0;
 
+       ret = write_or_die(fd, &phdr->raw, hdrsize);
        if (unlikely(ret != hdrsize))
                panic("Failed to write pkt header!\n");
+
+       hdrlen = pcap_get_length(phdr, type);
        if (unlikely(hdrlen != len))
                return -EINVAL;
 
@@ -56,28 +41,13 @@ static ssize_t pcap_rw_write(int fd, pcap_pkthdr_t *phdr, enum pcap_type type,
 static ssize_t pcap_rw_read(int fd, pcap_pkthdr_t *phdr, enum pcap_type type,
                            uint8_t *packet, size_t len)
 {
-       ssize_t ret, hdrlen = 0, hdrsize = 0;
-
-       switch (type) {
-#define PCAP_HDR_READ(__member__) do { \
-                       hdrsize = sizeof(phdr->__member__); \
-                       ret = read_or_die(fd, &phdr->__member__, hdrsize); \
-                       if (unlikely(ret != hdrsize)) \
-                               return -EIO; \
-                       hdrlen = phdr->__member__.caplen; \
-               } while (0)
-#define CASE_HDR_READ(what, member) \
-        case (what): \
-               PCAP_HDR_READ(member); \
-                break
-       CASE_HDR_READ(DEFAULT, ppo);
-       CASE_HDR_READ(NSEC, ppn);
-       CASE_HDR_READ(KUZNETZOV, ppk);
-       CASE_HDR_READ(BORKMANN, ppb);
-       default:
-               bug();
-       }
+       ssize_t ret, hdrsize = pcap_get_hdr_length(phdr, type), hdrlen = 0;
+
+       ret = read_or_die(fd, &phdr->raw, hdrsize);
+       if (unlikely(ret != hdrsize))
+               return -EIO;
 
+       hdrlen = pcap_get_length(phdr, type);
        if (unlikely(hdrlen == 0 || hdrlen > len))
                 return -EINVAL;