From 9bd3e1bd87e8ca90443f3faed17719bbd0298d36 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Tue, 29 Jan 2013 21:53:17 -0700 Subject: [PATCH] Reindent everything --- Makefile | 4 +- hd.c | 147 ++++++++++++++++++------------------- p4split.c | 187 +++++++++++++++++++++++++---------------------- pcap.c | 101 ++++++++++++++------------ pcap.h | 44 +++++------ pcat.c | 205 ++++++++++++++++++++++++---------------------------- pmerge.c | 193 ++++++++++++++++++++++++++----------------------- printfesc.c | 36 ++++----- puniq.c | 95 ++++++++++++------------ pyesc.c | 36 ++++----- stream.c | 105 ++++++++++++--------------- stream.h | 4 +- unhex.c | 75 ++++++++++--------- xor.c | 61 ++++++++-------- 14 files changed, 653 insertions(+), 640 deletions(-) diff --git a/Makefile b/Makefile index 382a264..d035b8c 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CFLAGS = -Wall -Werror -TARGETS = pmerge puniq p4split hd -TARGETS += pyesc printfesc pcat +TARGETS = pmerge puniq p4split pcat +TARGETS += hd pyesc printfesc xor unhex all: $(TARGETS) diff --git a/hd.c b/hd.c index 791490f..9a9e37e 100644 --- a/hd.c +++ b/hd.c @@ -2,96 +2,97 @@ #include #include -const char* charset[] = { - "·", "☺", "☻", "♥", "♦", "♣", "♠", "•", "◘", "○", "◙", "♂", "♀", "♪", "♫", "☼", - "►", "◄", "↕", "‼", "¶", "§", "▬", "↨", "↑", "↓", "→", "←", "∟", "↔", "▲", "▼", - " ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", - "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", - "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", - "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\\", "]", "^", "_", - "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", - "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", "⌂", +const char *charset[] = { + "·", "☺", "☻", "♥", "♦", "♣", "♠", "•", "◘", "○", "◙", "♂", "♀", "♪", "♫", "☼", + "►", "◄", "↕", "‼", "¶", "§", "▬", "↨", "↑", "↓", "→", "←", "∟", "↔", "▲", "▼", + " ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", + "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", + "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", + "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\\", "]", "^", "_", + "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", + "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", "⌂", - "Ç", "ü", "é", "â", "ä", "à", "å", "ç", "ê", "ë", "è", "ï", "î", "ì", "Ä", "Å", - "É", "æ", "Æ", "ô", "ö", "ò", "û", "ù", "ÿ", "Ö", "Ü", "¢", "£", "¥", "€", "ƒ", - "á", "í", "ó", "ú", "ñ", "Ñ", "ª", "º", "½", "⅓", "¼", "⅕", "⅙", "⅛", "«", "»", - "░", "▒", "▓", "│", "┤", "╡", "╢", "╖", "╕", "╣", "║", "╗", "╝", "╜", "╛", "┐", - "└", "┴", "┬", "├", "─", "┼", "╞", "╟", "╚", "╔", "╩", "╦", "╠", "═", "╬", "╧", - "╨", "╤", "╥", "╙", "╘", "╒", "╓", "╫", "╪", "┘", "┌", "█", "▄", "▌", "▐", "▀", - "α", "ß", "Γ", "π", "Σ", "σ", "µ", "τ", "Φ", "Θ", "Ω", "δ", "∞", "φ", "ε", "∩", - "⁰", "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹", "ⁱ", "ⁿ", "⁽", "⁼", "⁾", "¤", + "Ç", "ü", "é", "â", "ä", "à", "å", "ç", "ê", "ë", "è", "ï", "î", "ì", "Ä", "Å", + "É", "æ", "Æ", "ô", "ö", "ò", "û", "ù", "ÿ", "Ö", "Ü", "¢", "£", "¥", "€", "ƒ", + "á", "í", "ó", "ú", "ñ", "Ñ", "ª", "º", "½", "⅓", "¼", "⅕", "⅙", "⅛", "«", "»", + "░", "▒", "▓", "│", "┤", "╡", "╢", "╖", "╕", "╣", "║", "╗", "╝", "╜", "╛", "┐", + "└", "┴", "┬", "├", "─", "┼", "╞", "╟", "╚", "╔", "╩", "╦", "╠", "═", "╬", "╧", + "╨", "╤", "╥", "╙", "╘", "╒", "╓", "╫", "╪", "┘", "┌", "█", "▄", "▌", "▐", "▀", + "α", "ß", "Γ", "π", "Σ", "σ", "µ", "τ", "Φ", "Θ", "Ω", "δ", "∞", "φ", "ε", "∩", + "⁰", "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹", "ⁱ", "ⁿ", "⁽", "⁼", "⁾", "¤", }; int -dump(FILE *f) +dump(FILE * f) { - uint64_t p = 0; - uint8_t buf[32]; - int offset = 0; - int skipping = 0; + uint64_t p = 0; + uint8_t buf[32]; + int offset = 0; + int skipping = 0; - while (!feof(f)) { - uint8_t *bytes = buf + offset; - size_t len; - int i; + while (!feof(f)) { + uint8_t *bytes = buf + offset; + size_t len; + int i; - offset = 16 - offset; + offset = 16 - offset; - len = fread(bytes, 1, 16, f); - if (0 == len) break; + len = fread(bytes, 1, 16, f); + if (0 == len) + break; - if (p && (0 == memcmp(buf, buf + 16, 16))) { - if (! skipping) { - printf("*\n"); - skipping = 1; - } - p += 16; - continue; - } else { - skipping = 0; - } + if (p && (0 == memcmp(buf, buf + 16, 16))) { + if (!skipping) { + printf("*\n"); + skipping = 1; + } + p += 16; + continue; + } else { + skipping = 0; + } - printf("%08lx ", (long unsigned int)p); - for (i = 0; i < 16; i += 1) { - if (i < len) { - printf("%02x ", bytes[i]); - } else { - printf(" "); - } - if (7 == i) { - printf(" "); - } - } - printf(" ┆"); - for (i = 0; i < len; i += 1) { - printf("%s", charset[bytes[i]]); - } - if (-1 == printf("┆\n")) { - perror("printf"); - return 1; - } - p += len; - } - printf("%08lx\n", (long unsigned int)p); + printf("%08lx ", (long unsigned int) p); + for (i = 0; i < 16; i += 1) { + if (i < len) { + printf("%02x ", bytes[i]); + } else { + printf(" "); + } + if (7 == i) { + printf(" "); + } + } + printf(" ┆"); + for (i = 0; i < len; i += 1) { + printf("%s", charset[bytes[i]]); + } + if (-1 == printf("┆\n")) { + perror("printf"); + return 1; + } + p += len; + } + printf("%08lx\n", (long unsigned int) p); - return 0; + return 0; } int main(int argc, char *argv[]) { - if (1 == argc) { - dump(stdin); - } else { - FILE *f = fopen(argv[1], "rb"); + if (1 == argc) { + dump(stdin); + } else { + FILE *f = fopen(argv[1], "rb"); - if (! f) { - perror("open"); - return 1; - } + if (!f) { + perror("open"); + return 1; + } - dump(f); - } + dump(f); + } - return 0; + return 0; } diff --git a/p4split.c b/p4split.c index 4689688..35d0086 100644 --- a/p4split.c +++ b/p4split.c @@ -5,7 +5,9 @@ #include #include "pcap.h" -/* Gaah so much crap */ +/* + * Gaah so much crap + */ #include #include #include @@ -16,119 +18,132 @@ int usage(int ret) { - fprintf(stderr, "Usage: p4split CIDR\n"); - fprintf(stderr, "\n"); - fprintf(stderr, "Splits pcap on stdin into up to 256 files, based on CIDR.\n"); - return ret; + fprintf(stderr, "Usage: p4split CIDR\n"); + fprintf(stderr, "\n"); + fprintf(stderr, "Splits pcap on stdin into up to 256 files, based on CIDR.\n"); + return ret; } int -parse_cidr(char *s, uint32_t *addr, uint8_t *bits) +parse_cidr(char *s, uint32_t * addr, uint8_t * bits) { - char *slash = index(s, '/'); - struct in_addr inp; + char *slash = index(s, '/'); + struct in_addr inp; - if (slash) { - *slash = 0; - *bits = atoi(slash + 1); - } else { - *bits = 0; - } + if (slash) { + *slash = 0; + *bits = atoi(slash + 1); + } else { + *bits = 0; + } - if (0 == inet_aton(s, &inp)) return -1; - *addr = ntohl(inp.s_addr); + if (0 == inet_aton(s, &inp)) + return -1; + *addr = ntohl(inp.s_addr); - return 0; + return 0; } - + int main(int argc, char *argv[]) { - struct pcap_file p; - struct pcap_file out[256]; - int ok = 0; - uint32_t addr; - uint32_t mask; - uint8_t bits; - uint8_t shr; - int i; + struct pcap_file p; + struct pcap_file out[256]; + int ok = 0; + uint32_t addr; + uint32_t mask; + uint8_t bits; + uint8_t shr; + int i; - if (argc != 2) return usage(0); - if (-1 == parse_cidr(argv[1], &addr, &bits)) return usage(0); - if (bits > 24) return usage(0); - if (bits % 8) { - fprintf(stderr, "Warning: routing prefix is not a multiple of 8.\n"); - } + if (argc != 2) + return usage(0); + if (-1 == parse_cidr(argv[1], &addr, &bits)) + return usage(0); + if (bits > 24) + return usage(0); + if (bits % 8) { + fprintf(stderr, "Warning: routing prefix is not a multiple of 8.\n"); + } - mask = ~((1 << (32-bits)) - 1); - addr &= mask; - shr = (32-bits) - 8; + mask = ~((1 << (32 - bits)) - 1); + addr &= mask; + shr = (32 - bits) - 8; - for (i = 0; i < 256; i += 1) { - out[i].f = NULL; - } + for (i = 0; i < 256; i += 1) { + out[i].f = NULL; + } - if (-1 == pcap_open_in(&p, stdin)) return usage(0); + if (-1 == pcap_open_in(&p, stdin)) + return usage(0); - while (1) { - struct pcap_pkthdr hdr; - uint8_t octet; - char frame[MAXFRAME]; + while (1) { + struct pcap_pkthdr hdr; + uint8_t octet; + char frame[MAXFRAME]; - ok = 1; - if (-1 == pcap_read_pkthdr(&p, &hdr)) break; - if (1 != fread(frame, hdr.caplen, 1, p.f)) break; + ok = 1; + if (-1 == pcap_read_pkthdr(&p, &hdr)) + break; + if (1 != fread(frame, hdr.caplen, 1, p.f)) + break; - { - struct ether_header *eh = (struct ether_header *)frame; - struct iphdr *ih = (struct iphdr *)(frame + sizeof(struct ether_header)); - uint32_t a; + { + struct ether_header *eh = (struct ether_header *) frame; + struct iphdr *ih = (struct iphdr *) (frame + sizeof(struct ether_header)); + uint32_t a; - /* VLAN tag */ - if (ntohs(eh->ether_type) == 0x8100) { - ih = (struct iphdr *)((char *)ih + 4); - } + /* + * VLAN tag + */ + if (ntohs(eh->ether_type) == 0x8100) { + ih = (struct iphdr *) ((char *) ih + 4); + } - a = ntohl(ih->saddr); - if ((a & mask) != addr) { - a = ntohl(ih->daddr); - if ((a & mask) != addr) { - fprintf(stderr, "Warning: dropping unmatched packet %08x -> %08x\n", - ntohl(ih->saddr), ntohl(ih->daddr)); - continue; - } - } - octet = (a & ~mask) >> shr; - } + a = ntohl(ih->saddr); + if ((a & mask) != addr) { + a = ntohl(ih->daddr); + if ((a & mask) != addr) { + fprintf(stderr, "Warning: dropping unmatched packet %08x -> %08x\n", + ntohl(ih->saddr), ntohl(ih->daddr)); + continue; + } + } + octet = (a & ~mask) >> shr; + } - ok = 0; - if (! out[octet].f) { - char fn[9]; - FILE *f; + ok = 0; + if (!out[octet].f) { + char fn[9]; + FILE *f; - sprintf(fn, "%d.pcap", octet); + sprintf(fn, "%d.pcap", octet); - if (NULL == (f = fopen(fn, "wb"))) break; - if (-1 == pcap_open_out(&out[octet], f)) break; - } + if (NULL == (f = fopen(fn, "wb"))) + break; + if (-1 == pcap_open_out(&out[octet], f)) + break; + } - if (-1 == pcap_write_pkthdr(&out[octet], &hdr)) break; - if (1 != fwrite(frame, hdr.caplen, 1, out[octet].f)) break; - } + if (-1 == pcap_write_pkthdr(&out[octet], &hdr)) + break; + if (1 != fwrite(frame, hdr.caplen, 1, out[octet].f)) + break; + } - if (! ok) { - perror("Error"); - return 1; - } + if (!ok) { + perror("Error"); + return 1; + } - for (i = 0; i < 256; i += 1) { - if (out[i].f) { - pcap_close(&p); - } - } + for (i = 0; i < 256; i += 1) { + if (out[i].f) { + pcap_close(&p); + } + } - return 0; + return 0; } diff --git a/pcap.c b/pcap.c index 1eb7309..cf9bef3 100644 --- a/pcap.c +++ b/pcap.c @@ -4,83 +4,90 @@ #include "pcap.h" int -pcap_open_in(struct pcap_file *ctx, FILE *f) +pcap_open_in(struct pcap_file *ctx, FILE * f) { - struct pcap_file_header h; + struct pcap_file_header h; - if (1 != fread(&h, sizeof(h), 1, f)) { - h.magic = 0; - } + if (1 != fread(&h, sizeof(h), 1, f)) { + h.magic = 0; + } - if (MAGIC == h.magic) { - ctx->swap = 0; - } else if (bswap32(MAGIC) == h.magic) { - ctx->swap = 1; - } else { - return -1; - } - if ((h.version_major != 2) || (h.version_minor != 4)) return -1; + if (MAGIC == h.magic) { + ctx->swap = 0; + } else if (bswap32(MAGIC) == h.magic) { + ctx->swap = 1; + } else { + return -1; + } + if ((h.version_major != 2) || (h.version_minor != 4)) + return -1; - if (ctx->swap) h.snaplen = bswap32(h.snaplen); - if (h.snaplen > MAXFRAME) return -1; + if (ctx->swap) + h.snaplen = bswap32(h.snaplen); + if (h.snaplen > MAXFRAME) + return -1; - ctx->f = f; - return 0; + ctx->f = f; + return 0; } int -pcap_open_out(struct pcap_file *ctx, FILE *f) +pcap_open_out(struct pcap_file *ctx, FILE * f) { - struct pcap_file_header h = { MAGIC, 2, 4, 0, 0, MAXFRAME, 1 }; + struct pcap_file_header h = { MAGIC, 2, 4, 0, 0, MAXFRAME, 1 }; - if (1 != fwrite(&h, sizeof(h), 1, f)) return -1; - ctx->f = f; - ctx->swap = 0; + if (1 != fwrite(&h, sizeof(h), 1, f)) + return -1; + ctx->f = f; + ctx->swap = 0; - return 0; + return 0; } int pcap_read_pkthdr(struct pcap_file *ctx, struct pcap_pkthdr *hdr) { - if (1 != fread(hdr, sizeof(*hdr), 1, ctx->f)) { - return -1; - } + if (1 != fread(hdr, sizeof(*hdr), 1, ctx->f)) { + return -1; + } - if (ctx->swap) { - hdr->ts.tv_sec = bswap32(hdr->ts.tv_sec); - hdr->ts.tv_usec = bswap32(hdr->ts.tv_usec); - hdr->caplen = bswap32(hdr->caplen); - hdr->len = bswap32(hdr->len); - } + if (ctx->swap) { + hdr->ts.tv_sec = bswap32(hdr->ts.tv_sec); + hdr->ts.tv_usec = bswap32(hdr->ts.tv_usec); + hdr->caplen = bswap32(hdr->caplen); + hdr->len = bswap32(hdr->len); + } - if (hdr->caplen > MAXFRAME) return -1; + if (hdr->caplen > MAXFRAME) + return -1; - return 0; + return 0; } int pcap_write_pkthdr(struct pcap_file *ctx, struct pcap_pkthdr *hdr) { - if (ctx->swap) { - struct pcap_pkthdr ohdr; + if (ctx->swap) { + struct pcap_pkthdr ohdr; - memcpy(&ohdr, hdr, sizeof(ohdr)); - hdr->ts.tv_sec = bswap32(hdr->ts.tv_sec); - hdr->ts.tv_usec = bswap32(hdr->ts.tv_usec); - hdr->caplen = bswap32(hdr->caplen); - hdr->len = bswap32(hdr->len); + memcpy(&ohdr, hdr, sizeof(ohdr)); + hdr->ts.tv_sec = bswap32(hdr->ts.tv_sec); + hdr->ts.tv_usec = bswap32(hdr->ts.tv_usec); + hdr->caplen = bswap32(hdr->caplen); + hdr->len = bswap32(hdr->len); - if (1 != fwrite(&ohdr, sizeof(ohdr), 1, ctx->f)) return -1; - } else { - if (1 != fwrite(hdr, sizeof(*hdr), 1, ctx->f)) return -1; - } + if (1 != fwrite(&ohdr, sizeof(ohdr), 1, ctx->f)) + return -1; + } else { + if (1 != fwrite(hdr, sizeof(*hdr), 1, ctx->f)) + return -1; + } - return 0; + return 0; } void pcap_close(struct pcap_file *ctx) { - fclose(ctx->f); + fclose(ctx->f); } diff --git a/pcap.h b/pcap.h index 41ffe6f..c70b33f 100644 --- a/pcap.h +++ b/pcap.h @@ -8,35 +8,35 @@ #define MAXFRAME 9000 struct pcap_file { - FILE *f; - int swap; + FILE *f; + int swap; }; struct pcap_file_header { - uint32_t magic; - uint16_t version_major; - uint16_t version_minor; - int32_t thiszone; /* gmt to local correction */ - uint32_t sigfigs; /* accuracy of timestamps */ - int32_t snaplen; /* max length saved portion of each pkt */ - int32_t linktype; /* data link type (LINKTYPE_*) */ + uint32_t magic; + uint16_t version_major; + uint16_t version_minor; + int32_t thiszone; /* gmt to local correction */ + uint32_t sigfigs; /* accuracy of timestamps */ + int32_t snaplen; /* max length saved portion of each pkt */ + int32_t linktype; /* data link type (LINKTYPE_*) */ }; struct pcap_pkthdr { - struct pcap_timeval { - uint32_t tv_sec; - uint32_t tv_usec; - } ts; /* time stamp */ - uint32_t caplen; /* length of portion present */ - uint32_t len; /* length this packet (off wire) */ + struct pcap_timeval { + uint32_t tv_sec; + uint32_t tv_usec; + } ts; /* time stamp */ + uint32_t caplen; /* length of portion present */ + uint32_t len; /* length this packet (off wire) */ }; #ifndef max -# define max(a, b) ((a)>(b)?(a):(b)) +#define max(a, b) ((a)>(b)?(a):(b)) #endif #ifndef min -# define min(a, b) ((a)<(b)?(a):(b)) +#define min(a, b) ((a)<(b)?(a):(b)) #endif #define bswap32(i) (((i & 0xff000000) >> 030) | \ @@ -47,7 +47,9 @@ struct pcap_pkthdr { ((i & 0x00ff) << 010)) -/* Debugging help */ +/* + * Debugging help + */ #define DUMPf(fmt, args...) fprintf(stderr, "%s:%s:%d " fmt "\n", __FILE__, __FUNCTION__, __LINE__, ##args) #define DUMP() DUMPf("") #define DUMP_d(v) DUMPf("%s = %d", #v, v) @@ -57,10 +59,10 @@ struct pcap_pkthdr { #define DUMP_c(v) DUMPf("%s = %c", #v, v) #define DUMP_p(v) DUMPf("%s = %p", #v, v) -int pcap_open_in(struct pcap_file *ctx, FILE *f); -int pcap_open_out(struct pcap_file *ctx, FILE *f); +int pcap_open_in(struct pcap_file *ctx, FILE * f); +int pcap_open_out(struct pcap_file *ctx, FILE * f); int pcap_read_pkthdr(struct pcap_file *ctx, struct pcap_pkthdr *hdr); int pcap_write_pkthdr(struct pcap_file *ctx, struct pcap_pkthdr *hdr); void pcap_close(struct pcap_file *ctx); -#endif /* __PCAP_H__ */ +#endif /* __PCAP_H__ */ diff --git a/pcat.c b/pcat.c index 00d2db9..cbdb308 100644 --- a/pcat.c +++ b/pcat.c @@ -15,11 +15,7 @@ void ip4_addr(char *addr_s, uint32_t addr) { - snprintf(addr_s, 16, "%u.%u.%u.%u", - (addr >> 24) & 0xff, - (addr >> 16) & 0xff, - (addr >> 8) & 0xff, - (addr >> 0) & 0xff); + snprintf(addr_s, 16, "%u.%u.%u.%u", (addr >> 24) & 0xff, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr >> 0) & 0xff); } void @@ -27,164 +23,151 @@ print_payload(struct stream *s) { while (s->len) { printf("%02x", read_uint8(s)); - } + } } void process_tcp(struct stream *s, char *saddr_s, char *daddr_s) { - uint16_t sport = read_uint16be(s); - uint16_t dport = read_uint16be(s); - uint32_t seq = read_uint32be(s); - uint32_t ack = read_uint32be(s); - uint8_t off = read_uint8(s); - uint8_t hlen = (off >> 4) * 4; - uint8_t flags = read_uint8(s); - uint16_t window = read_uint16be(s); - uint16_t chksum = read_uint16be(s); - uint16_t urgent = read_uint16be(s); + uint16_t sport = read_uint16be(s); + uint16_t dport = read_uint16be(s); + uint32_t seq = read_uint32be(s); + uint32_t ack = read_uint32be(s); + uint8_t off = read_uint8(s); + uint8_t hlen = (off >> 4) * 4; + uint8_t flags = read_uint8(s); + uint16_t window = read_uint16be(s); + uint16_t chksum = read_uint16be(s); + uint16_t urgent = read_uint16be(s); - if (hlen < 20) { - printf("!"); - } + if (hlen < 20) { + printf("!"); + } - printf("TCP4 %s:%u %s:%u ", saddr_s, sport, daddr_s, dport); + printf("TCP4 %s:%u %s:%u ", saddr_s, sport, daddr_s, dport); - // shut the compiler up - if (false && - urgent && - chksum && - window && - flags && - ack && - seq && - false); + // shut the compiler up + if (false && urgent && chksum && window && flags && ack && seq && false); } void process_udp(struct stream *s, char *saddr_s, char *daddr_s) { - uint16_t sport = read_uint16be(s); - uint16_t dport = read_uint16be(s); - uint16_t len = read_uint16be(s); - uint16_t chksum = read_uint16be(s); + uint16_t sport = read_uint16be(s); + uint16_t dport = read_uint16be(s); + uint16_t len = read_uint16be(s); + uint16_t chksum = read_uint16be(s); - printf("UDP4 %s:%u %s:%u ", saddr_s, sport, daddr_s, dport); + printf("UDP4 %s:%u %s:%u ", saddr_s, sport, daddr_s, dport); - // Now, do some shit! - if (false && - len && - chksum && - false); + // Now, do some shit! + if (false && len && chksum && false); } void process_ip4(struct stream *s) { - uint8_t vhl = read_uint8(s); - uint8_t ihl = (vhl & 0x0f) * 4; - uint8_t tos = read_uint8(s); - uint16_t length = read_uint16be(s); - uint16_t id = read_uint16be(s); - uint16_t off = read_uint16be(s); - uint8_t ttl = read_uint8(s); - uint8_t proto = read_uint8(s); - uint16_t chksum = read_uint16be(s); - uint32_t saddr = read_uint32be(s); - uint32_t daddr = read_uint32be(s); + uint8_t vhl = read_uint8(s); + uint8_t ihl = (vhl & 0x0f) * 4; + uint8_t tos = read_uint8(s); + uint16_t length = read_uint16be(s); + uint16_t id = read_uint16be(s); + uint16_t off = read_uint16be(s); + uint8_t ttl = read_uint8(s); + uint8_t proto = read_uint8(s); + uint16_t chksum = read_uint16be(s); + uint32_t saddr = read_uint32be(s); + uint32_t daddr = read_uint32be(s); - char saddr_s[20]; - char daddr_s[20]; + char saddr_s[20]; + char daddr_s[20]; - ip4_addr(saddr_s, saddr); - ip4_addr(daddr_s, daddr); - - // Ignore options - sskip(s, 20 - ihl); - - // Force stream length to IP payload length - s->len = length - ihl; + ip4_addr(saddr_s, saddr); + ip4_addr(daddr_s, daddr); - switch (proto) { - case IPPROTO_TCP: - process_tcp(s, saddr_s, daddr_s); - break; - case IPPROTO_UDP: - process_udp(s, saddr_s, daddr_s); - break; - default: - printf("P%d %s %s ", proto, saddr_s, daddr_s); - break; - } + // Ignore options + sskip(s, 20 - ihl); - print_payload(s); + // Force stream length to IP payload length + s->len = length - ihl; - /* Placate compiler */ - if (false && - chksum && - id && - tos && - ttl && - off && - false); + switch (proto) { + case IPPROTO_TCP: + process_tcp(s, saddr_s, daddr_s); + break; + case IPPROTO_UDP: + process_udp(s, saddr_s, daddr_s); + break; + default: + printf("P%d %s %s ", proto, saddr_s, daddr_s); + break; + } + + print_payload(s); + + /* + * Placate compiler + */ + if (false && chksum && id && tos && ttl && off && false); } void print_ethernet(struct stream *s) { - uint8_t saddr[6]; - uint8_t daddr[6]; - uint16_t ethertype; + uint8_t saddr[6]; + uint8_t daddr[6]; + uint16_t ethertype; - sread(s, &saddr, sizeof(saddr)); - sread(s, &daddr, sizeof(daddr)); - ethertype = read_uint16be(s); + sread(s, &saddr, sizeof(saddr)); + sread(s, &daddr, sizeof(daddr)); + ethertype = read_uint16be(s); - if (ethertype == 0x8100) { - // VLAN - read_uint16be(s); - ethertype = read_uint16be(s); - } + if (ethertype == 0x8100) { + // VLAN + read_uint16be(s); + ethertype = read_uint16be(s); + } - switch (ethertype) { - case 0x0800: // IPv4 - process_ip4(s); - break; - } + switch (ethertype) { + case 0x0800: // IPv4 + process_ip4(s); + break; + } } void print_frame(struct pcap_pkthdr *hdr, char const *frame) { - struct stream streambuf; - struct stream *s = &streambuf; - - sinit(s, frame, hdr->caplen); - printf("%u,%u ", hdr->ts.tv_sec, hdr->ts.tv_usec); - print_ethernet(s); - printf("\n"); + struct stream streambuf; + struct stream *s = &streambuf; + + sinit(s, frame, hdr->caplen); + printf("%u,%u ", hdr->ts.tv_sec, hdr->ts.tv_usec); + print_ethernet(s); + printf("\n"); } void -pcat(FILE *f) +pcat(FILE * f) { struct pcap_file p; - if (-1 == pcap_open_in(&p, f)) return; - + if (-1 == pcap_open_in(&p, f)) + return; + for (;;) { struct pcap_pkthdr hdr; char frame[MAXFRAME]; - + if (-1 == pcap_read_pkthdr(&p, &hdr)) { break; } - + if (1 != fread(frame, hdr.caplen, 1, f)) { break; } - + print_frame(&hdr, frame); } } @@ -194,6 +177,6 @@ int main(int argc, char *argv[]) { pcat(stdin); - + return 0; -} \ No newline at end of file +} diff --git a/pmerge.c b/pmerge.c index 6c0d71d..d991774 100644 --- a/pmerge.c +++ b/pmerge.c @@ -4,124 +4,139 @@ #include "pcap.h" struct input_file { - int active; - struct pcap_file p; - struct pcap_pkthdr next; + int active; + struct pcap_file p; + struct pcap_pkthdr next; }; int usage(int ret) { - fprintf(stderr, "Usage: pmerge FILE ...\n"); - fprintf(stderr, "\n"); - fprintf(stderr, "Merges pcap files, outputting time-ordered pcap stream\n"); - return ret; + fprintf(stderr, "Usage: pmerge FILE ...\n"); + fprintf(stderr, "\n"); + fprintf(stderr, "Merges pcap files, outputting time-ordered pcap stream\n"); + return ret; } int read_next(struct input_file *file) { - if (! file->active) return -1; + if (!file->active) + return -1; - if (-1 == pcap_read_pkthdr(&file->p, &file->next)) { - pcap_close(&file->p); - file->active = 0; - file->next.ts.tv_sec = 0xffffffff; - file->next.ts.tv_usec = 0xffffffff; - return -1; - } + if (-1 == pcap_read_pkthdr(&file->p, &file->next)) { + pcap_close(&file->p); + file->active = 0; + file->next.ts.tv_sec = 0xffffffff; + file->next.ts.tv_usec = 0xffffffff; + return -1; + } - return 0; + return 0; } int main(int argc, char *argv[]) { - struct input_file files[argc-1]; - int nfiles = 0; - int nopen; - int i; - struct pcap_file out; + struct input_file files[argc - 1]; + int nfiles = 0; + int nopen; + int i; + struct pcap_file out; - if (1 == argc) return usage(0); + if (1 == argc) + return usage(0); - /* Open input files */ - for (i = 0; i < argc-1; i += 1) { - char *fn = argv[i+1]; - struct input_file *cur = &files[nfiles]; - FILE *f; + /* + * Open input files + */ + for (i = 0; i < argc - 1; i += 1) { + char *fn = argv[i + 1]; + struct input_file *cur = &files[nfiles]; + FILE *f; - if ('-' == fn[0]) return usage(0); + if ('-' == fn[0]) + return usage(0); - f = fopen(fn, "rb"); - if (NULL == f) { - perror(fn); - return EX_NOINPUT; - } + f = fopen(fn, "rb"); + if (NULL == f) { + perror(fn); + return EX_NOINPUT; + } - if (-1 == pcap_open_in(&cur->p, f)) { - fprintf(stderr, "%s: unable to process\n", fn); - return EX_IOERR; - } - cur->active = 1; + if (-1 == pcap_open_in(&cur->p, f)) { + fprintf(stderr, "%s: unable to process\n", fn); + return EX_IOERR; + } + cur->active = 1; - if (0 == read_next(cur)) { - nfiles += 1; - } - } + if (0 == read_next(cur)) { + nfiles += 1; + } + } - if (-1 == pcap_open_out(&out, stdout)) { - perror("writing header"); - return EX_IOERR; - } + if (-1 == pcap_open_out(&out, stdout)) { + perror("writing header"); + return EX_IOERR; + } - nopen = nfiles; - while (nopen) { - struct input_file *cur = &files[0]; - char frame[MAXFRAME]; - size_t len; + nopen = nfiles; + while (nopen) { + struct input_file *cur = &files[0]; + char frame[MAXFRAME]; + size_t len; - /* Find next oldest frame */ - for (i = 0; i < nfiles; i += 1) { - if (files[i].active && - ((files[i].next.ts.tv_sec < cur->next.ts.tv_sec) || - ((files[i].next.ts.tv_sec == cur->next.ts.tv_sec) && - (files[i].next.ts.tv_usec < cur->next.ts.tv_usec)))) { - cur = &files[i]; - } - } + /* + * Find next oldest frame + */ + for (i = 0; i < nfiles; i += 1) { + if (files[i].active && + ((files[i].next.ts.tv_sec < cur->next.ts.tv_sec) || + ((files[i].next.ts.tv_sec == cur->next.ts.tv_sec) && + (files[i].next.ts.tv_usec < cur->next.ts.tv_usec)))) { + cur = &files[i]; + } + } - /* Make sure it'll fit */ - if (cur->next.caplen > sizeof(frame)) { - fprintf(stderr, "error: huge frame (size %u)\n", (unsigned int)len); - return EX_SOFTWARE; - } + /* + * Make sure it'll fit + */ + if (cur->next.caplen > sizeof(frame)) { + fprintf(stderr, "error: huge frame (size %u)\n", (unsigned int) len); + return EX_SOFTWARE; + } - /* Read it */ - len = fread(frame, 1, cur->next.caplen, cur->p.f); - if (len < cur->next.caplen) { - /* Short read */ - cur->next.caplen = len; - pcap_close(&cur->p); - cur->active = 0; - } + /* + * Read it + */ + len = fread(frame, 1, cur->next.caplen, cur->p.f); + if (len < cur->next.caplen) { + /* + * Short read + */ + cur->next.caplen = len; + pcap_close(&cur->p); + cur->active = 0; + } - /* Write header + frame */ - if (len) { - if (1 != fwrite(&cur->next, sizeof(cur->next), 1, stdout)) { - perror("error"); - return EX_IOERR; - } - if (len != fwrite(frame, 1, len, stdout)) { - perror("error"); - return EX_IOERR; - } - } + /* + * Write header + frame + */ + if (len) { + if (1 != fwrite(&cur->next, sizeof(cur->next), 1, stdout)) { + perror("error"); + return EX_IOERR; + } + if (len != fwrite(frame, 1, len, stdout)) { + perror("error"); + return EX_IOERR; + } + } - if (-1 == read_next(cur)) { - nopen -= 1; - } - } + if (-1 == read_next(cur)) { + nopen -= 1; + } + } - return 0; + return 0; } diff --git a/printfesc.c b/printfesc.c index 28e5bee..e4f84bf 100644 --- a/printfesc.c +++ b/printfesc.c @@ -3,26 +3,26 @@ int main(int argc, char *argv[]) { - printf("printf "); + printf("printf "); - while (! feof(stdin)) { - int c = getchar(); + while (!feof(stdin)) { + int c = getchar(); - switch (c) { - case EOF: - break; - case '0' ... '9': - case 'A' ... 'Z': - case 'a' ... 'z': - putchar(c); - break; - default: - printf("\\\\%03o", c); - break; - } - } + switch (c) { + case EOF: + break; + case '0' ... '9': + case 'A' ... 'Z': + case 'a' ... 'z': + putchar(c); + break; + default: + printf("\\\\%03o", c); + break; + } + } - putchar('\n'); + putchar('\n'); - return 0; + return 0; } diff --git a/puniq.c b/puniq.c index 6e7ff21..b5ac07a 100644 --- a/puniq.c +++ b/puniq.c @@ -6,58 +6,63 @@ int main(int argc, char *argv[]) { - int i; - struct pcap_pkthdr hdr[2]; - char frame[2][MAXFRAME]; - int cur = 0; - struct pcap_file out; + int i; + struct pcap_pkthdr hdr[2]; + char frame[2][MAXFRAME]; + int cur = 0; + struct pcap_file out; - memset(&hdr, 0, sizeof(hdr)); - memset(frame, 0, 2 * MAXFRAME); + memset(&hdr, 0, sizeof(hdr)); + memset(frame, 0, 2 * MAXFRAME); - if (-1 == pcap_open_out(&out, stdout)) { - perror("writing header"); - return EX_IOERR; - } + if (-1 == pcap_open_out(&out, stdout)) { + perror("writing header"); + return EX_IOERR; + } - i = 1; - do { - char *fn = argv[i]; - FILE *f; - struct pcap_file p; + i = 1; + do { + char *fn = argv[i]; + FILE *f; + struct pcap_file p; - if ((! fn) || (0 == strcmp("-", fn))) { - f = stdin; - } else { - f = fopen(fn, "rb"); - if (NULL == f) { - perror(fn); - return EX_IOERR; - } - } + if ((!fn) || (0 == strcmp("-", fn))) { + f = stdin; + } else { + f = fopen(fn, "rb"); + if (NULL == f) { + perror(fn); + return EX_IOERR; + } + } - if (-1 == pcap_open_in(&p, f)) { - fprintf(stderr, "%s: unable to process\n", fn); - return EX_IOERR; - } + if (-1 == pcap_open_in(&p, f)) { + fprintf(stderr, "%s: unable to process\n", fn); + return EX_IOERR; + } - while (1) { - if (-1 == pcap_read_pkthdr(&p, &hdr[cur])) break; - if (1 != fread(frame[cur], hdr[cur].caplen, 1, p.f)) break; + while (1) { + if (-1 == pcap_read_pkthdr(&p, &hdr[cur])) + break; + if (1 != fread(frame[cur], hdr[cur].caplen, 1, p.f)) + break; - if ((0 == memcmp(&hdr[0], &hdr[1], sizeof(hdr[0]))) && - (0 == memcmp(frame[0], frame[1], hdr[cur].caplen))) { - /* Skip this duplicate */ - } else { - if (-1 == pcap_write_pkthdr(&out, &hdr[cur])) break; - if (1 != fwrite(frame[cur], hdr[cur].caplen, 1, out.f)) break; - } - cur = (1 - cur); - } - pcap_close(&p); + if ((0 == memcmp(&hdr[0], &hdr[1], sizeof(hdr[0]))) && (0 == memcmp(frame[0], frame[1], hdr[cur].caplen))) { + /* + * Skip this duplicate + */ + } else { + if (-1 == pcap_write_pkthdr(&out, &hdr[cur])) + break; + if (1 != fwrite(frame[cur], hdr[cur].caplen, 1, out.f)) + break; + } + cur = (1 - cur); + } + pcap_close(&p); - i += 1; - } while (i < argc); + i += 1; + } while (i < argc); - return 0; + return 0; } diff --git a/pyesc.c b/pyesc.c index 929951c..a67e709 100644 --- a/pyesc.c +++ b/pyesc.c @@ -3,24 +3,24 @@ int main(int argc, char *argv[]) { - while (1) { - int c = getchar(); + while (1) { + int c = getchar(); - switch (c) { - case EOF: - return 0; - case 134: - printf("\\\\"); - break; - case 32 ... 91: - case 93 ... 126: - putchar(c); - break; - default: - printf("\\x%02x", c); - break; - } - } + switch (c) { + case EOF: + return 0; + case 134: + printf("\\\\"); + break; + case 32 ... 91: + case 93 ... 126: + putchar(c); + break; + default: + printf("\\x%02x", c); + break; + } + } - return 0; + return 0; } diff --git a/stream.c b/stream.c index b1b2f57..293155d 100644 --- a/stream.c +++ b/stream.c @@ -7,100 +7,87 @@ void sinit(struct stream *s, char const *buf, size_t buflen) { - s->buf = buf; - s->len = buflen; + s->buf = buf; + s->len = buflen; } bool sskip(struct stream *s, size_t count) { - if (count > s->len) { - s->len = 0; - return false; - } + if (count > s->len) { + s->len = 0; + return false; + } - s->buf += count; - s->len -= count; - return true; + s->buf += count; + s->len -= count; + return true; } bool -sread(struct stream *s, void *buf, size_t count) +sread(struct stream * s, void *buf, size_t count) { - void const *d = s->buf; + void const *d = s->buf; - if (! sskip(s, count)) { - return false; - } - memcpy(buf, d, count); + if (!sskip(s, count)) { + return false; + } + memcpy(buf, d, count); - return true; + return true; } uint8_t -read_uint8(struct stream *s) +read_uint8(struct stream * s) { - uint8_t *d = (uint8_t *)s->buf; + uint8_t *d = (uint8_t *) s->buf; - if (! sskip(s, 1)) { - return 0; - } - return d[0]; + if (!sskip(s, 1)) { + return 0; + } + return d[0]; } uint16_t -read_uint16be(struct stream *s) +read_uint16be(struct stream * s) { - uint8_t *d = (uint8_t *)s->buf; + uint8_t *d = (uint8_t *) s->buf; - if (! sskip(s, 2)) { - return 0; - } - return ( - (d[0] << 8) | - (d[1] << 0)); + if (!sskip(s, 2)) { + return 0; + } + return ((d[0] << 8) | (d[1] << 0)); } uint16_t -read_uint16le(struct stream *s) +read_uint16le(struct stream * s) { - uint8_t *d = (uint8_t *)s->buf; + uint8_t *d = (uint8_t *) s->buf; - if (! sskip(s, 2)) { - return 0; - } - return ( - (d[0] << 0) | - (d[1] << 8)); + if (!sskip(s, 2)) { + return 0; + } + return ((d[0] << 0) | (d[1] << 8)); } uint32_t -read_uint32be(struct stream *s) +read_uint32be(struct stream * s) { - uint8_t *d = (uint8_t *)s->buf; + uint8_t *d = (uint8_t *) s->buf; - if (! sskip(s, 4)) { - return 0; - } - return ( - (d[0] << 24) | - (d[1] << 16) | - (d[2] << 8) | - (d[3] << 0)); + if (!sskip(s, 4)) { + return 0; + } + return ((d[0] << 24) | (d[1] << 16) | (d[2] << 8) | (d[3] << 0)); } uint32_t -read_uint32le(struct stream *s) +read_uint32le(struct stream * s) { - uint8_t *d = (uint8_t *)s->buf; + uint8_t *d = (uint8_t *) s->buf; - if (! sskip(s, 4)) { - return 0; - } - return ( - (d[0] << 0) | - (d[1] << 8) | - (d[2] << 16) | - (d[3] << 24)); + if (!sskip(s, 4)) { + return 0; + } + return ((d[0] << 0) | (d[1] << 8) | (d[2] << 16) | (d[3] << 24)); } - diff --git a/stream.h b/stream.h index c734c28..6115003 100644 --- a/stream.h +++ b/stream.h @@ -6,8 +6,8 @@ #include struct stream { - char const *buf; - size_t len; + char const *buf; + size_t len; }; void sinit(struct stream *s, char const *buf, size_t len); diff --git a/unhex.c b/unhex.c index 113c35a..72e4a44 100644 --- a/unhex.c +++ b/unhex.c @@ -1,7 +1,6 @@ -/* Hex Decoder -- 2012 Zephyr - * - * This file is in the public domain. I make no promises - * about the functionality of this program. +/* + * Hex Decoder -- 2012 Zephyr This file is in the public domain. I make no promises about the functionality + * of this program. */ #include @@ -9,43 +8,43 @@ int main(int argc, char *argv[]) { - unsigned char acc = 0; - unsigned char nybble = 0; - unsigned long int count = 0; + unsigned char acc = 0; + unsigned char nybble = 0; + unsigned long int count = 0; - while (1) { - int c = getchar(); + while (1) { + int c = getchar(); - count += 1; + count += 1; - switch (c) { - case EOF: - return 0; - case '0' ... '9': - acc = (acc << 4) + c - '0'; - nybble += 1; - break; - case 'a' ... 'f': - acc = (acc << 4) + c - 'a' + 10; - nybble += 1; - break; - case 'A' ... 'F': - acc = (acc << 4) + c - 'A' + 10; - nybble += 1; - break; - default: - if (nybble != 0) { - fprintf(stderr, "Warning: non-hex character mid-octet at offset %lu\n", count); - } - break; - } + switch (c) { + case EOF: + return 0; + case '0' ... '9': + acc = (acc << 4) + c - '0'; + nybble += 1; + break; + case 'a' ... 'f': + acc = (acc << 4) + c - 'a' + 10; + nybble += 1; + break; + case 'A' ... 'F': + acc = (acc << 4) + c - 'A' + 10; + nybble += 1; + break; + default: + if (nybble != 0) { + fprintf(stderr, "Warning: non-hex character mid-octet at offset %lu\n", count); + } + break; + } - if (nybble == 2) { - putchar(acc); - acc = 0; - nybble = 0; - } - } + if (nybble == 2) { + putchar(acc); + acc = 0; + nybble = 0; + } + } - return 0; + return 0; } diff --git a/xor.c b/xor.c index b3e5a78..9ac7fd3 100644 --- a/xor.c +++ b/xor.c @@ -1,7 +1,6 @@ -/* xor filter -- 2012 Zephyr - * - * This file is in the public domain. I make no promises - * about the functionality of this program. +/* + * xor filter -- 2012 Zephyr This file is in the public domain. I make no promises about the functionality + * of this program. */ #include @@ -11,38 +10,38 @@ int main(int argc, char *argv[]) { - int start = 1; - int base = 0; - int arg; + int start = 1; + int base = 0; + int arg; - if (argc < 2) { - fprintf(stderr, "Usage: %s [-x] m1 [m2 ...]\n", argv[0]); - return 1; - } + if (argc < 2) { + fprintf(stderr, "Usage: %s [-x] m1 [m2 ...]\n", argv[0]); + return 1; + } - if (0 == strcmp(argv[1], "-x")) { - base = 16; - start += 1; - } + if (0 == strcmp(argv[1], "-x")) { + base = 16; + start += 1; + } - arg = start; + arg = start; - while (1) { - int c = getchar(); - unsigned char mask; - - if (! argv[arg]) { - arg = start; - } - mask = strtol(argv[arg++], NULL, base); + while (1) { + int c = getchar(); + unsigned char mask; - if (EOF == c) { - break; - } + if (!argv[arg]) { + arg = start; + } + mask = strtol(argv[arg++], NULL, base); - c ^= mask; - putchar(c); - } + if (EOF == c) { + break; + } - return 0; + c ^= mask; + putchar(c); + } + + return 0; }