Module Name: src Committed By: pooka Date: Mon Mar 14 11:08:29 UTC 2011
Modified Files: src/usr.bin/shmif_dumpbus: Makefile shmif_dumpbus.c Log Message: * attempt to make this endian-independent (tested only on el) * write via libpcap (since i thought there would be some fancy api to deal with endianness. after doing 80% of the work i noticed there wasn't, but went ahead anyway) To generate a diff of this commit: cvs rdiff -u -r1.1 -r1.2 src/usr.bin/shmif_dumpbus/Makefile cvs rdiff -u -r1.4 -r1.5 src/usr.bin/shmif_dumpbus/shmif_dumpbus.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.bin/shmif_dumpbus/Makefile diff -u src/usr.bin/shmif_dumpbus/Makefile:1.1 src/usr.bin/shmif_dumpbus/Makefile:1.2 --- src/usr.bin/shmif_dumpbus/Makefile:1.1 Wed Jan 12 16:14:24 2011 +++ src/usr.bin/shmif_dumpbus/Makefile Mon Mar 14 11:08:28 2011 @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.1 2011/01/12 16:14:24 pooka Exp $ +# $NetBSD: Makefile,v 1.2 2011/03/14 11:08:28 pooka Exp $ # .include <bsd.own.mk> @@ -10,4 +10,6 @@ SRCS+= shmif_dumpbus.c shmif_busops.c CPPFLAGS+= -I${SHMIFD} +LDADD+= -lpcap + .include <bsd.prog.mk> Index: src/usr.bin/shmif_dumpbus/shmif_dumpbus.c diff -u src/usr.bin/shmif_dumpbus/shmif_dumpbus.c:1.4 src/usr.bin/shmif_dumpbus/shmif_dumpbus.c:1.5 --- src/usr.bin/shmif_dumpbus/shmif_dumpbus.c:1.4 Sat Mar 12 18:27:42 2011 +++ src/usr.bin/shmif_dumpbus/shmif_dumpbus.c Mon Mar 14 11:08:28 2011 @@ -1,4 +1,4 @@ -/* $NetBSD: shmif_dumpbus.c,v 1.4 2011/03/12 18:27:42 pooka Exp $ */ +/* $NetBSD: shmif_dumpbus.c,v 1.5 2011/03/14 11:08:28 pooka Exp $ */ /*- * Copyright (c) 2010 Antti Kantee. All Rights Reserved. @@ -34,6 +34,8 @@ #include <sys/mman.h> #include <sys/stat.h> +#include <machine/bswap.h> + #include <assert.h> #include <err.h> #include <fcntl.h> @@ -55,20 +57,9 @@ exit(1); } -/* - * Apparently pcap uses a non-exported structure as the on-disk - * packet header. Since that format isn't very likely to change - * soon, just define a local version - */ -struct ondisk_pcaphdr { - uint32_t ts_sec; - uint32_t ts_usec; - uint32_t caplen; - uint32_t len; - -}; - #define BUFSIZE 64*1024 +#define SWAPME(a) (doswap ? bswap32(a) : (a)) +#define SWAPME64(a) (doswap ? bswap64(a) : (a)) int main(int argc, char *argv[]) { @@ -77,10 +68,11 @@ const char *pcapfile = NULL; uint32_t curbus, buslast; struct shmif_mem *bmem; - int fd, pfd, i, ch; + int fd, i, ch; int bonus; char *buf; - bool hflag = false; + bool hflag = false, doswap = false, isstdout; + pcap_dumper_t *pdump; setprogname(argv[0]); while ((ch = getopt(argc, argv, "hp:")) != -1) { @@ -118,45 +110,37 @@ err(1, "mmap"); bmem = busmem; - if (bmem->shm_magic != SHMIF_MAGIC) - errx(1, "%s not a shmif bus", argv[0]); - if (bmem->shm_version != SHMIF_VERSION) + if (bmem->shm_magic != SHMIF_MAGIC) { + if (bmem->shm_magic != bswap32(SHMIF_MAGIC)) + errx(1, "%s not a shmif bus", argv[0]); + doswap = 1; + } + if (SWAPME(bmem->shm_version) != SHMIF_VERSION) errx(1, "bus vesrsion %d, program %d", - bmem->shm_version, SHMIF_VERSION); + SWAPME(bmem->shm_version), SHMIF_VERSION); printf("bus version %d, lock: %d, generation: %" PRIu64 ", firstoff: 0x%04x, lastoff: 0x%04x\n", - bmem->shm_version, bmem->shm_lock, bmem->shm_gen, - bmem->shm_first, bmem->shm_last); + SWAPME(bmem->shm_version), SWAPME(bmem->shm_lock), + SWAPME64(bmem->shm_gen), + SWAPME(bmem->shm_first), SWAPME(bmem->shm_last)); if (hflag) exit(0); if (pcapfile) { - struct pcap_file_header phdr; - - if (strcmp(pcapfile, "-") == 0) { - pfd = STDOUT_FILENO; - } else { - pfd = open(pcapfile, O_RDWR | O_CREAT | O_TRUNC, 0777); - if (pfd == -1) - err(1, "create pcap dump"); - } - - memset(&phdr, 0, sizeof(phdr)); - phdr.magic = 0xa1b2c3d4; /* tcpdump magic */ - phdr.version_major = PCAP_VERSION_MAJOR; - phdr.version_minor = PCAP_VERSION_MINOR; - phdr.snaplen = 1518; - phdr.linktype = DLT_EN10MB; - - if (write(pfd, &phdr, sizeof(phdr)) != sizeof(phdr)) - err(1, "phdr write"); + isstdout = strcmp(pcapfile, "-") == 0; + pcap_t *pcap = pcap_open_dead(DLT_EN10MB, 1518); + pdump = pcap_dump_open(pcap, pcapfile); + if (pdump == NULL) + err(1, "cannot open pcap dump file"); } else { - pfd = -1; /* XXXgcc */ + /* XXXgcc */ + isstdout = false; + pdump = NULL; } - curbus = bmem->shm_first; - buslast = bmem->shm_last; + curbus = SWAPME(bmem->shm_first); + buslast = SWAPME(bmem->shm_last); if (curbus == BUSMEM_DATASIZE) curbus = 0; @@ -166,9 +150,10 @@ i = 0; while (curbus <= buslast || bonus) { - struct ondisk_pcaphdr packhdr; + struct pcap_pkthdr packhdr; struct shmif_pkthdr sp; uint32_t oldoff; + uint32_t curlen; bool wrap; assert(curbus < sb.st_size); @@ -180,38 +165,39 @@ bonus = 0; assert(curbus < sb.st_size); + curlen = SWAPME(sp.sp_len); - if (sp.sp_len == 0) { + if (curlen == 0) { continue; } - if (pfd != STDOUT_FILENO) + if (!isstdout) printf("packet %d, offset 0x%04x, length 0x%04x, " "ts %d/%06d\n", i++, curbus, - sp.sp_len, sp.sp_sec, sp.sp_usec); + curlen, SWAPME(sp.sp_sec), SWAPME(sp.sp_usec)); if (!pcapfile) { curbus = shmif_busread(bmem, - buf, curbus, sp.sp_len, &wrap); + buf, curbus, curlen, &wrap); if (wrap) bonus = 0; continue; } memset(&packhdr, 0, sizeof(packhdr)); - packhdr.caplen = packhdr.len = sp.sp_len; - packhdr.ts_sec = sp.sp_sec; - packhdr.ts_usec = sp.sp_usec; - assert(sp.sp_len <= BUFSIZE); - - if (write(pfd, &packhdr, sizeof(packhdr)) != sizeof(packhdr)) - err(1, "error writing packethdr"); - curbus = shmif_busread(bmem, buf, curbus, sp.sp_len, &wrap); - if (write(pfd, buf, sp.sp_len) != (ssize_t)sp.sp_len) - err(1, "write packet"); + packhdr.caplen = packhdr.len = curlen; + packhdr.ts.tv_sec = SWAPME(sp.sp_sec); + packhdr.ts.tv_usec = SWAPME(sp.sp_usec); + assert(curlen <= BUFSIZE); + + curbus = shmif_busread(bmem, buf, curbus, curlen, &wrap); + pcap_dump((u_char *)pdump, &packhdr, (u_char *)buf); if (wrap) bonus = 0; } + if (pcapfile) + pcap_dump_close(pdump); + return 0; }