Reindent everything

This commit is contained in:
Neale Pickett 2013-01-29 21:53:17 -07:00
parent 31e9beb71a
commit 9bd3e1bd87
14 changed files with 653 additions and 640 deletions

View File

@ -1,6 +1,6 @@
CFLAGS = -Wall -Werror CFLAGS = -Wall -Werror
TARGETS = pmerge puniq p4split hd TARGETS = pmerge puniq p4split pcat
TARGETS += pyesc printfesc pcat TARGETS += hd pyesc printfesc xor unhex
all: $(TARGETS) all: $(TARGETS)

3
hd.c
View File

@ -38,7 +38,8 @@ dump(FILE *f)
offset = 16 - offset; offset = 16 - offset;
len = fread(bytes, 1, 16, f); len = fread(bytes, 1, 16, f);
if (0 == len) break; if (0 == len)
break;
if (p && (0 == memcmp(buf, buf + 16, 16))) { if (p && (0 == memcmp(buf, buf + 16, 16))) {
if (!skipping) { if (!skipping) {

View File

@ -5,7 +5,9 @@
#include <strings.h> #include <strings.h>
#include "pcap.h" #include "pcap.h"
/* Gaah so much crap */ /*
* Gaah so much crap
*/
#include <sys/socket.h> #include <sys/socket.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netinet/ether.h> #include <netinet/ether.h>
@ -36,7 +38,8 @@ parse_cidr(char *s, uint32_t *addr, uint8_t *bits)
*bits = 0; *bits = 0;
} }
if (0 == inet_aton(s, &inp)) return -1; if (0 == inet_aton(s, &inp))
return -1;
*addr = ntohl(inp.s_addr); *addr = ntohl(inp.s_addr);
return 0; return 0;
@ -55,9 +58,12 @@ main(int argc, char *argv[])
uint8_t shr; uint8_t shr;
int i; int i;
if (argc != 2) return usage(0); if (argc != 2)
if (-1 == parse_cidr(argv[1], &addr, &bits)) return usage(0); return usage(0);
if (bits > 24) return usage(0); if (-1 == parse_cidr(argv[1], &addr, &bits))
return usage(0);
if (bits > 24)
return usage(0);
if (bits % 8) { if (bits % 8) {
fprintf(stderr, "Warning: routing prefix is not a multiple of 8.\n"); fprintf(stderr, "Warning: routing prefix is not a multiple of 8.\n");
} }
@ -70,7 +76,8 @@ main(int argc, char *argv[])
out[i].f = NULL; 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) { while (1) {
struct pcap_pkthdr hdr; struct pcap_pkthdr hdr;
@ -78,8 +85,10 @@ main(int argc, char *argv[])
char frame[MAXFRAME]; char frame[MAXFRAME];
ok = 1; ok = 1;
if (-1 == pcap_read_pkthdr(&p, &hdr)) break; if (-1 == pcap_read_pkthdr(&p, &hdr))
if (1 != fread(frame, hdr.caplen, 1, p.f)) break; break;
if (1 != fread(frame, hdr.caplen, 1, p.f))
break;
{ {
@ -87,7 +96,9 @@ main(int argc, char *argv[])
struct iphdr *ih = (struct iphdr *) (frame + sizeof(struct ether_header)); struct iphdr *ih = (struct iphdr *) (frame + sizeof(struct ether_header));
uint32_t a; uint32_t a;
/* VLAN tag */ /*
* VLAN tag
*/
if (ntohs(eh->ether_type) == 0x8100) { if (ntohs(eh->ether_type) == 0x8100) {
ih = (struct iphdr *) ((char *) ih + 4); ih = (struct iphdr *) ((char *) ih + 4);
} }
@ -111,12 +122,16 @@ main(int argc, char *argv[])
sprintf(fn, "%d.pcap", octet); sprintf(fn, "%d.pcap", octet);
if (NULL == (f = fopen(fn, "wb"))) break; if (NULL == (f = fopen(fn, "wb")))
if (-1 == pcap_open_out(&out[octet], f)) break; break;
if (-1 == pcap_open_out(&out[octet], f))
break;
} }
if (-1 == pcap_write_pkthdr(&out[octet], &hdr)) break; if (-1 == pcap_write_pkthdr(&out[octet], &hdr))
if (1 != fwrite(frame, hdr.caplen, 1, out[octet].f)) break; break;
if (1 != fwrite(frame, hdr.caplen, 1, out[octet].f))
break;
} }
if (!ok) { if (!ok) {

21
pcap.c
View File

@ -19,10 +19,13 @@ pcap_open_in(struct pcap_file *ctx, FILE *f)
} else { } else {
return -1; return -1;
} }
if ((h.version_major != 2) || (h.version_minor != 4)) return -1; if ((h.version_major != 2) || (h.version_minor != 4))
return -1;
if (ctx->swap) h.snaplen = bswap32(h.snaplen); if (ctx->swap)
if (h.snaplen > MAXFRAME) return -1; h.snaplen = bswap32(h.snaplen);
if (h.snaplen > MAXFRAME)
return -1;
ctx->f = f; ctx->f = f;
return 0; return 0;
@ -33,7 +36,8 @@ 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; if (1 != fwrite(&h, sizeof(h), 1, f))
return -1;
ctx->f = f; ctx->f = f;
ctx->swap = 0; ctx->swap = 0;
@ -54,7 +58,8 @@ pcap_read_pkthdr(struct pcap_file *ctx, struct pcap_pkthdr *hdr)
hdr->len = bswap32(hdr->len); hdr->len = bswap32(hdr->len);
} }
if (hdr->caplen > MAXFRAME) return -1; if (hdr->caplen > MAXFRAME)
return -1;
return 0; return 0;
} }
@ -71,9 +76,11 @@ pcap_write_pkthdr(struct pcap_file *ctx, struct pcap_pkthdr *hdr)
hdr->caplen = bswap32(hdr->caplen); hdr->caplen = bswap32(hdr->caplen);
hdr->len = bswap32(hdr->len); hdr->len = bswap32(hdr->len);
if (1 != fwrite(&ohdr, sizeof(ohdr), 1, ctx->f)) return -1; if (1 != fwrite(&ohdr, sizeof(ohdr), 1, ctx->f))
return -1;
} else { } else {
if (1 != fwrite(hdr, sizeof(*hdr), 1, ctx->f)) return -1; if (1 != fwrite(hdr, sizeof(*hdr), 1, ctx->f))
return -1;
} }
return 0; return 0;

4
pcap.h
View File

@ -47,7 +47,9 @@ struct pcap_pkthdr {
((i & 0x00ff) << 010)) ((i & 0x00ff) << 010))
/* Debugging help */ /*
* Debugging help
*/
#define DUMPf(fmt, args...) fprintf(stderr, "%s:%s:%d " fmt "\n", __FILE__, __FUNCTION__, __LINE__, ##args) #define DUMPf(fmt, args...) fprintf(stderr, "%s:%s:%d " fmt "\n", __FILE__, __FUNCTION__, __LINE__, ##args)
#define DUMP() DUMPf("") #define DUMP() DUMPf("")
#define DUMP_d(v) DUMPf("%s = %d", #v, v) #define DUMP_d(v) DUMPf("%s = %d", #v, v)

35
pcat.c
View File

@ -15,11 +15,7 @@
void void
ip4_addr(char *addr_s, uint32_t addr) ip4_addr(char *addr_s, uint32_t addr)
{ {
snprintf(addr_s, 16, "%u.%u.%u.%u", snprintf(addr_s, 16, "%u.%u.%u.%u", (addr >> 24) & 0xff, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr >> 0) & 0xff);
(addr >> 24) & 0xff,
(addr >> 16) & 0xff,
(addr >> 8) & 0xff,
(addr >> 0) & 0xff);
} }
void void
@ -51,14 +47,7 @@ process_tcp(struct stream *s, char *saddr_s, char *daddr_s)
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 // shut the compiler up
if (false && if (false && urgent && chksum && window && flags && ack && seq && false);
urgent &&
chksum &&
window &&
flags &&
ack &&
seq &&
false);
} }
void void
@ -72,10 +61,7 @@ process_udp(struct stream *s, char *saddr_s, char *daddr_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! // Now, do some shit!
if (false && if (false && len && chksum && false);
len &&
chksum &&
false);
} }
void void
@ -119,14 +105,10 @@ process_ip4(struct stream *s)
print_payload(s); print_payload(s);
/* Placate compiler */ /*
if (false && * Placate compiler
chksum && */
id && if (false && chksum && id && tos && ttl && off && false);
tos &&
ttl &&
off &&
false);
} }
@ -171,7 +153,8 @@ pcat(FILE *f)
{ {
struct pcap_file p; struct pcap_file p;
if (-1 == pcap_open_in(&p, f)) return; if (-1 == pcap_open_in(&p, f))
return;
for (;;) { for (;;) {
struct pcap_pkthdr hdr; struct pcap_pkthdr hdr;

View File

@ -21,7 +21,8 @@ usage(int ret)
int int
read_next(struct input_file *file) 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)) { if (-1 == pcap_read_pkthdr(&file->p, &file->next)) {
pcap_close(&file->p); pcap_close(&file->p);
@ -43,15 +44,19 @@ main(int argc, char *argv[])
int i; int i;
struct pcap_file out; struct pcap_file out;
if (1 == argc) return usage(0); if (1 == argc)
return usage(0);
/* Open input files */ /*
* Open input files
*/
for (i = 0; i < argc - 1; i += 1) { for (i = 0; i < argc - 1; i += 1) {
char *fn = argv[i + 1]; char *fn = argv[i + 1];
struct input_file *cur = &files[nfiles]; struct input_file *cur = &files[nfiles];
FILE *f; FILE *f;
if ('-' == fn[0]) return usage(0); if ('-' == fn[0])
return usage(0);
f = fopen(fn, "rb"); f = fopen(fn, "rb");
if (NULL == f) { if (NULL == f) {
@ -81,7 +86,9 @@ main(int argc, char *argv[])
char frame[MAXFRAME]; char frame[MAXFRAME];
size_t len; size_t len;
/* Find next oldest frame */ /*
* Find next oldest frame
*/
for (i = 0; i < nfiles; i += 1) { for (i = 0; i < nfiles; i += 1) {
if (files[i].active && 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) ||
@ -91,22 +98,30 @@ main(int argc, char *argv[])
} }
} }
/* Make sure it'll fit */ /*
* Make sure it'll fit
*/
if (cur->next.caplen > sizeof(frame)) { if (cur->next.caplen > sizeof(frame)) {
fprintf(stderr, "error: huge frame (size %u)\n", (unsigned int) len); fprintf(stderr, "error: huge frame (size %u)\n", (unsigned int) len);
return EX_SOFTWARE; return EX_SOFTWARE;
} }
/* Read it */ /*
* Read it
*/
len = fread(frame, 1, cur->next.caplen, cur->p.f); len = fread(frame, 1, cur->next.caplen, cur->p.f);
if (len < cur->next.caplen) { if (len < cur->next.caplen) {
/* Short read */ /*
* Short read
*/
cur->next.caplen = len; cur->next.caplen = len;
pcap_close(&cur->p); pcap_close(&cur->p);
cur->active = 0; cur->active = 0;
} }
/* Write header + frame */ /*
* Write header + frame
*/
if (len) { if (len) {
if (1 != fwrite(&cur->next, sizeof(cur->next), 1, stdout)) { if (1 != fwrite(&cur->next, sizeof(cur->next), 1, stdout)) {
perror("error"); perror("error");

19
puniq.c
View File

@ -42,15 +42,20 @@ main(int argc, char *argv[])
} }
while (1) { while (1) {
if (-1 == pcap_read_pkthdr(&p, &hdr[cur])) break; if (-1 == pcap_read_pkthdr(&p, &hdr[cur]))
if (1 != fread(frame[cur], hdr[cur].caplen, 1, p.f)) break; break;
if (1 != fread(frame[cur], hdr[cur].caplen, 1, p.f))
break;
if ((0 == memcmp(&hdr[0], &hdr[1], sizeof(hdr[0]))) && if ((0 == memcmp(&hdr[0], &hdr[1], sizeof(hdr[0]))) && (0 == memcmp(frame[0], frame[1], hdr[cur].caplen))) {
(0 == memcmp(frame[0], frame[1], hdr[cur].caplen))) { /*
/* Skip this duplicate */ * Skip this duplicate
*/
} else { } else {
if (-1 == pcap_write_pkthdr(&out, &hdr[cur])) break; if (-1 == pcap_write_pkthdr(&out, &hdr[cur]))
if (1 != fwrite(frame[cur], hdr[cur].caplen, 1, out.f)) break; break;
if (1 != fwrite(frame[cur], hdr[cur].caplen, 1, out.f))
break;
} }
cur = (1 - cur); cur = (1 - cur);
} }

View File

@ -56,9 +56,7 @@ read_uint16be(struct stream *s)
if (!sskip(s, 2)) { if (!sskip(s, 2)) {
return 0; return 0;
} }
return ( return ((d[0] << 8) | (d[1] << 0));
(d[0] << 8) |
(d[1] << 0));
} }
uint16_t uint16_t
@ -69,9 +67,7 @@ read_uint16le(struct stream *s)
if (!sskip(s, 2)) { if (!sskip(s, 2)) {
return 0; return 0;
} }
return ( return ((d[0] << 0) | (d[1] << 8));
(d[0] << 0) |
(d[1] << 8));
} }
uint32_t uint32_t
@ -82,11 +78,7 @@ read_uint32be(struct stream *s)
if (!sskip(s, 4)) { if (!sskip(s, 4)) {
return 0; return 0;
} }
return ( return ((d[0] << 24) | (d[1] << 16) | (d[2] << 8) | (d[3] << 0));
(d[0] << 24) |
(d[1] << 16) |
(d[2] << 8) |
(d[3] << 0));
} }
uint32_t uint32_t
@ -97,10 +89,5 @@ read_uint32le(struct stream *s)
if (!sskip(s, 4)) { if (!sskip(s, 4)) {
return 0; return 0;
} }
return ( return ((d[0] << 0) | (d[1] << 8) | (d[2] << 16) | (d[3] << 24));
(d[0] << 0) |
(d[1] << 8) |
(d[2] << 16) |
(d[3] << 24));
} }

View File

@ -1,7 +1,6 @@
/* Hex Decoder -- 2012 Zephyr <zephyr@dirtbags.net> /*
* * Hex Decoder -- 2012 Zephyr <zephyr@dirtbags.net> This file is in the public domain. I make no promises about the functionality
* This file is in the public domain. I make no promises * of this program.
* about the functionality of this program.
*/ */
#include <stdio.h> #include <stdio.h>

7
xor.c
View File

@ -1,7 +1,6 @@
/* xor filter -- 2012 Zephyr <zephyr@dirtbags.net> /*
* * xor filter -- 2012 Zephyr <zephyr@dirtbags.net> This file is in the public domain. I make no promises about the functionality
* This file is in the public domain. I make no promises * of this program.
* about the functionality of this program.
*/ */
#include <stdio.h> #include <stdio.h>