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)

147
hd.c
View File

@ -2,96 +2,97 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
const char* charset[] = { const char *charset[] = {
"·", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "·", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "§", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "§", "", "", "", "", "", "", "", "", "", "",
" ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", " ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", "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", "@", "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", "[", "\\", "]", "^", "_", "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", "`", "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", "{", "|", "}", "~", "", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", "",
"Ç", "ü", "é", "â", "ä", "à", "å", "ç", "ê", "ë", "è", "ï", "î", "ì", "Ä", "Å", "Ç", "ü", "é", "â", "ä", "à", "å", "ç", "ê", "ë", "è", "ï", "î", "ì", "Ä", "Å",
"É", "æ", "Æ", "ô", "ö", "ò", "û", "ù", "ÿ", "Ö", "Ü", "¢", "£", "¥", "", "ƒ", "É", "æ", "Æ", "ô", "ö", "ò", "û", "ù", "ÿ", "Ö", "Ü", "¢", "£", "¥", "", "ƒ",
"á", "í", "ó", "ú", "ñ", "Ñ", "ª", "º", "½", "", "¼", "", "", "", "«", "»", "á", "í", "ó", "ú", "ñ", "Ñ", "ª", "º", "½", "", "¼", "", "", "", "«", "»",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"α", "ß", "Γ", "π", "Σ", "σ", "µ", "τ", "Φ", "Θ", "Ω", "δ", "", "φ", "ε", "", "α", "ß", "Γ", "π", "Σ", "σ", "µ", "τ", "Φ", "Θ", "Ω", "δ", "", "φ", "ε", "",
"", "¹", "²", "³", "", "", "", "", "", "", "", "", "", "", "", "¤", "", "¹", "²", "³", "", "", "", "", "", "", "", "", "", "", "", "¤",
}; };
int int
dump(FILE *f) dump(FILE * f)
{ {
uint64_t p = 0; uint64_t p = 0;
uint8_t buf[32]; uint8_t buf[32];
int offset = 0; int offset = 0;
int skipping = 0; int skipping = 0;
while (!feof(f)) { while (!feof(f)) {
uint8_t *bytes = buf + offset; uint8_t *bytes = buf + offset;
size_t len; size_t len;
int i; int i;
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) {
printf("*\n"); printf("*\n");
skipping = 1; skipping = 1;
} }
p += 16; p += 16;
continue; continue;
} else { } else {
skipping = 0; skipping = 0;
} }
printf("%08lx ", (long unsigned int)p); printf("%08lx ", (long unsigned int) p);
for (i = 0; i < 16; i += 1) { for (i = 0; i < 16; i += 1) {
if (i < len) { if (i < len) {
printf("%02x ", bytes[i]); printf("%02x ", bytes[i]);
} else { } else {
printf(" "); printf(" ");
} }
if (7 == i) { if (7 == i) {
printf(" "); printf(" ");
} }
} }
printf(""); printf("");
for (i = 0; i < len; i += 1) { for (i = 0; i < len; i += 1) {
printf("%s", charset[bytes[i]]); printf("%s", charset[bytes[i]]);
} }
if (-1 == printf("\n")) { if (-1 == printf("\n")) {
perror("printf"); perror("printf");
return 1; return 1;
} }
p += len; p += len;
} }
printf("%08lx\n", (long unsigned int)p); printf("%08lx\n", (long unsigned int) p);
return 0; return 0;
} }
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
if (1 == argc) { if (1 == argc) {
dump(stdin); dump(stdin);
} else { } else {
FILE *f = fopen(argv[1], "rb"); FILE *f = fopen(argv[1], "rb");
if (! f) { if (!f) {
perror("open"); perror("open");
return 1; return 1;
} }
dump(f); dump(f);
} }
return 0; return 0;
} }

187
p4split.c
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>
@ -16,119 +18,132 @@
int int
usage(int ret) usage(int ret)
{ {
fprintf(stderr, "Usage: p4split CIDR\n"); fprintf(stderr, "Usage: p4split CIDR\n");
fprintf(stderr, "\n"); fprintf(stderr, "\n");
fprintf(stderr, "Splits pcap on stdin into up to 256 files, based on CIDR.\n"); fprintf(stderr, "Splits pcap on stdin into up to 256 files, based on CIDR.\n");
return ret; return ret;
} }
int 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, '/'); char *slash = index(s, '/');
struct in_addr inp; struct in_addr inp;
if (slash) { if (slash) {
*slash = 0; *slash = 0;
*bits = atoi(slash + 1); *bits = atoi(slash + 1);
} else { } else {
*bits = 0; *bits = 0;
} }
if (0 == inet_aton(s, &inp)) return -1; if (0 == inet_aton(s, &inp))
*addr = ntohl(inp.s_addr); return -1;
*addr = ntohl(inp.s_addr);
return 0; return 0;
} }
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct pcap_file p; struct pcap_file p;
struct pcap_file out[256]; struct pcap_file out[256];
int ok = 0; int ok = 0;
uint32_t addr; uint32_t addr;
uint32_t mask; uint32_t mask;
uint8_t bits; uint8_t bits;
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))
if (bits % 8) { return usage(0);
fprintf(stderr, "Warning: routing prefix is not a multiple of 8.\n"); 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); mask = ~((1 << (32 - bits)) - 1);
addr &= mask; addr &= mask;
shr = (32-bits) - 8; shr = (32 - bits) - 8;
for (i = 0; i < 256; i += 1) { for (i = 0; i < 256; i += 1) {
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;
uint8_t octet; uint8_t octet;
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;
{ {
struct ether_header *eh = (struct ether_header *)frame; struct ether_header *eh = (struct ether_header *) frame;
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 */ /*
if (ntohs(eh->ether_type) == 0x8100) { * VLAN tag
ih = (struct iphdr *)((char *)ih + 4); */
} if (ntohs(eh->ether_type) == 0x8100) {
ih = (struct iphdr *) ((char *) ih + 4);
}
a = ntohl(ih->saddr); a = ntohl(ih->saddr);
if ((a & mask) != addr) { if ((a & mask) != addr) {
a = ntohl(ih->daddr); a = ntohl(ih->daddr);
if ((a & mask) != addr) { if ((a & mask) != addr) {
fprintf(stderr, "Warning: dropping unmatched packet %08x -> %08x\n", fprintf(stderr, "Warning: dropping unmatched packet %08x -> %08x\n",
ntohl(ih->saddr), ntohl(ih->daddr)); ntohl(ih->saddr), ntohl(ih->daddr));
continue; continue;
} }
} }
octet = (a & ~mask) >> shr; octet = (a & ~mask) >> shr;
} }
ok = 0; ok = 0;
if (! out[octet].f) { if (!out[octet].f) {
char fn[9]; char fn[9];
FILE *f; FILE *f;
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) {
perror("Error"); perror("Error");
return 1; return 1;
} }
for (i = 0; i < 256; i += 1) { for (i = 0; i < 256; i += 1) {
if (out[i].f) { if (out[i].f) {
pcap_close(&p); pcap_close(&p);
} }
} }
return 0; return 0;
} }

101
pcap.c
View File

@ -4,83 +4,90 @@
#include "pcap.h" #include "pcap.h"
int 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)) { if (1 != fread(&h, sizeof(h), 1, f)) {
h.magic = 0; h.magic = 0;
} }
if (MAGIC == h.magic) { if (MAGIC == h.magic) {
ctx->swap = 0; ctx->swap = 0;
} else if (bswap32(MAGIC) == h.magic) { } else if (bswap32(MAGIC) == h.magic) {
ctx->swap = 1; ctx->swap = 1;
} 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;
} }
int 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; if (1 != fwrite(&h, sizeof(h), 1, f))
ctx->f = f; return -1;
ctx->swap = 0; ctx->f = f;
ctx->swap = 0;
return 0; return 0;
} }
int int
pcap_read_pkthdr(struct pcap_file *ctx, struct pcap_pkthdr *hdr) pcap_read_pkthdr(struct pcap_file *ctx, struct pcap_pkthdr *hdr)
{ {
if (1 != fread(hdr, sizeof(*hdr), 1, ctx->f)) { if (1 != fread(hdr, sizeof(*hdr), 1, ctx->f)) {
return -1; return -1;
} }
if (ctx->swap) { if (ctx->swap) {
hdr->ts.tv_sec = bswap32(hdr->ts.tv_sec); hdr->ts.tv_sec = bswap32(hdr->ts.tv_sec);
hdr->ts.tv_usec = bswap32(hdr->ts.tv_usec); hdr->ts.tv_usec = bswap32(hdr->ts.tv_usec);
hdr->caplen = bswap32(hdr->caplen); hdr->caplen = bswap32(hdr->caplen);
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;
} }
int int
pcap_write_pkthdr(struct pcap_file *ctx, struct pcap_pkthdr *hdr) pcap_write_pkthdr(struct pcap_file *ctx, struct pcap_pkthdr *hdr)
{ {
if (ctx->swap) { if (ctx->swap) {
struct pcap_pkthdr ohdr; struct pcap_pkthdr ohdr;
memcpy(&ohdr, hdr, sizeof(ohdr)); memcpy(&ohdr, hdr, sizeof(ohdr));
hdr->ts.tv_sec = bswap32(hdr->ts.tv_sec); hdr->ts.tv_sec = bswap32(hdr->ts.tv_sec);
hdr->ts.tv_usec = bswap32(hdr->ts.tv_usec); hdr->ts.tv_usec = bswap32(hdr->ts.tv_usec);
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))
} else { return -1;
if (1 != fwrite(hdr, sizeof(*hdr), 1, ctx->f)) return -1; } else {
} if (1 != fwrite(hdr, sizeof(*hdr), 1, ctx->f))
return -1;
}
return 0; return 0;
} }
void void
pcap_close(struct pcap_file *ctx) pcap_close(struct pcap_file *ctx)
{ {
fclose(ctx->f); fclose(ctx->f);
} }

44
pcap.h
View File

@ -8,35 +8,35 @@
#define MAXFRAME 9000 #define MAXFRAME 9000
struct pcap_file { struct pcap_file {
FILE *f; FILE *f;
int swap; int swap;
}; };
struct pcap_file_header { struct pcap_file_header {
uint32_t magic; uint32_t magic;
uint16_t version_major; uint16_t version_major;
uint16_t version_minor; uint16_t version_minor;
int32_t thiszone; /* gmt to local correction */ int32_t thiszone; /* gmt to local correction */
uint32_t sigfigs; /* accuracy of timestamps */ uint32_t sigfigs; /* accuracy of timestamps */
int32_t snaplen; /* max length saved portion of each pkt */ int32_t snaplen; /* max length saved portion of each pkt */
int32_t linktype; /* data link type (LINKTYPE_*) */ int32_t linktype; /* data link type (LINKTYPE_*) */
}; };
struct pcap_pkthdr { struct pcap_pkthdr {
struct pcap_timeval { struct pcap_timeval {
uint32_t tv_sec; uint32_t tv_sec;
uint32_t tv_usec; uint32_t tv_usec;
} ts; /* time stamp */ } ts; /* time stamp */
uint32_t caplen; /* length of portion present */ uint32_t caplen; /* length of portion present */
uint32_t len; /* length this packet (off wire) */ uint32_t len; /* length this packet (off wire) */
}; };
#ifndef max #ifndef max
# define max(a, b) ((a)>(b)?(a):(b)) #define max(a, b) ((a)>(b)?(a):(b))
#endif #endif
#ifndef min #ifndef min
# define min(a, b) ((a)<(b)?(a):(b)) #define min(a, b) ((a)<(b)?(a):(b))
#endif #endif
#define bswap32(i) (((i & 0xff000000) >> 030) | \ #define bswap32(i) (((i & 0xff000000) >> 030) | \
@ -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)
@ -57,10 +59,10 @@ struct pcap_pkthdr {
#define DUMP_c(v) DUMPf("%s = %c", #v, v) #define DUMP_c(v) DUMPf("%s = %c", #v, v)
#define DUMP_p(v) DUMPf("%s = %p", #v, v) #define DUMP_p(v) DUMPf("%s = %p", #v, v)
int pcap_open_in(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_open_out(struct pcap_file *ctx, FILE * f);
int pcap_read_pkthdr(struct pcap_file *ctx, struct pcap_pkthdr *hdr); int pcap_read_pkthdr(struct pcap_file *ctx, struct pcap_pkthdr *hdr);
int pcap_write_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); void pcap_close(struct pcap_file *ctx);
#endif /* __PCAP_H__ */ #endif /* __PCAP_H__ */

205
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
@ -27,164 +23,151 @@ print_payload(struct stream *s)
{ {
while (s->len) { while (s->len) {
printf("%02x", read_uint8(s)); printf("%02x", read_uint8(s));
} }
} }
void void
process_tcp(struct stream *s, char *saddr_s, char *daddr_s) process_tcp(struct stream *s, char *saddr_s, char *daddr_s)
{ {
uint16_t sport = read_uint16be(s); uint16_t sport = read_uint16be(s);
uint16_t dport = read_uint16be(s); uint16_t dport = read_uint16be(s);
uint32_t seq = read_uint32be(s); uint32_t seq = read_uint32be(s);
uint32_t ack = read_uint32be(s); uint32_t ack = read_uint32be(s);
uint8_t off = read_uint8(s); uint8_t off = read_uint8(s);
uint8_t hlen = (off >> 4) * 4; uint8_t hlen = (off >> 4) * 4;
uint8_t flags = read_uint8(s); uint8_t flags = read_uint8(s);
uint16_t window = read_uint16be(s); uint16_t window = read_uint16be(s);
uint16_t chksum = read_uint16be(s); uint16_t chksum = read_uint16be(s);
uint16_t urgent = read_uint16be(s); uint16_t urgent = read_uint16be(s);
if (hlen < 20) { if (hlen < 20) {
printf("!"); 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 // shut the compiler up
if (false && if (false && urgent && chksum && window && flags && ack && seq && false);
urgent &&
chksum &&
window &&
flags &&
ack &&
seq &&
false);
} }
void void
process_udp(struct stream *s, char *saddr_s, char *daddr_s) process_udp(struct stream *s, char *saddr_s, char *daddr_s)
{ {
uint16_t sport = read_uint16be(s); uint16_t sport = read_uint16be(s);
uint16_t dport = read_uint16be(s); uint16_t dport = read_uint16be(s);
uint16_t len = read_uint16be(s); uint16_t len = read_uint16be(s);
uint16_t chksum = 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! // Now, do some shit!
if (false && if (false && len && chksum && false);
len &&
chksum &&
false);
} }
void void
process_ip4(struct stream *s) process_ip4(struct stream *s)
{ {
uint8_t vhl = read_uint8(s); uint8_t vhl = read_uint8(s);
uint8_t ihl = (vhl & 0x0f) * 4; uint8_t ihl = (vhl & 0x0f) * 4;
uint8_t tos = read_uint8(s); uint8_t tos = read_uint8(s);
uint16_t length = read_uint16be(s); uint16_t length = read_uint16be(s);
uint16_t id = read_uint16be(s); uint16_t id = read_uint16be(s);
uint16_t off = read_uint16be(s); uint16_t off = read_uint16be(s);
uint8_t ttl = read_uint8(s); uint8_t ttl = read_uint8(s);
uint8_t proto = read_uint8(s); uint8_t proto = read_uint8(s);
uint16_t chksum = read_uint16be(s); uint16_t chksum = read_uint16be(s);
uint32_t saddr = read_uint32be(s); uint32_t saddr = read_uint32be(s);
uint32_t daddr = read_uint32be(s); uint32_t daddr = read_uint32be(s);
char saddr_s[20]; char saddr_s[20];
char daddr_s[20]; char daddr_s[20];
ip4_addr(saddr_s, saddr); ip4_addr(saddr_s, saddr);
ip4_addr(daddr_s, daddr); ip4_addr(daddr_s, daddr);
// Ignore options
sskip(s, 20 - ihl);
// Force stream length to IP payload length
s->len = length - ihl;
switch (proto) { // Ignore options
case IPPROTO_TCP: sskip(s, 20 - ihl);
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); // Force stream length to IP payload length
s->len = length - ihl;
/* Placate compiler */ switch (proto) {
if (false && case IPPROTO_TCP:
chksum && process_tcp(s, saddr_s, daddr_s);
id && break;
tos && case IPPROTO_UDP:
ttl && process_udp(s, saddr_s, daddr_s);
off && break;
false); 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 void
print_ethernet(struct stream *s) print_ethernet(struct stream *s)
{ {
uint8_t saddr[6]; uint8_t saddr[6];
uint8_t daddr[6]; uint8_t daddr[6];
uint16_t ethertype; uint16_t ethertype;
sread(s, &saddr, sizeof(saddr)); sread(s, &saddr, sizeof(saddr));
sread(s, &daddr, sizeof(daddr)); sread(s, &daddr, sizeof(daddr));
ethertype = read_uint16be(s); ethertype = read_uint16be(s);
if (ethertype == 0x8100) { if (ethertype == 0x8100) {
// VLAN // VLAN
read_uint16be(s); read_uint16be(s);
ethertype = read_uint16be(s); ethertype = read_uint16be(s);
} }
switch (ethertype) { switch (ethertype) {
case 0x0800: // IPv4 case 0x0800: // IPv4
process_ip4(s); process_ip4(s);
break; break;
} }
} }
void void
print_frame(struct pcap_pkthdr *hdr, char const *frame) print_frame(struct pcap_pkthdr *hdr, char const *frame)
{ {
struct stream streambuf; struct stream streambuf;
struct stream *s = &streambuf; struct stream *s = &streambuf;
sinit(s, frame, hdr->caplen); sinit(s, frame, hdr->caplen);
printf("%u,%u ", hdr->ts.tv_sec, hdr->ts.tv_usec); printf("%u,%u ", hdr->ts.tv_sec, hdr->ts.tv_usec);
print_ethernet(s); print_ethernet(s);
printf("\n"); printf("\n");
} }
void void
pcat(FILE *f) 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;
char frame[MAXFRAME]; char frame[MAXFRAME];
if (-1 == pcap_read_pkthdr(&p, &hdr)) { if (-1 == pcap_read_pkthdr(&p, &hdr)) {
break; break;
} }
if (1 != fread(frame, hdr.caplen, 1, f)) { if (1 != fread(frame, hdr.caplen, 1, f)) {
break; break;
} }
print_frame(&hdr, frame); print_frame(&hdr, frame);
} }
} }
@ -194,6 +177,6 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
pcat(stdin); pcat(stdin);
return 0; return 0;
} }

193
pmerge.c
View File

@ -4,124 +4,139 @@
#include "pcap.h" #include "pcap.h"
struct input_file { struct input_file {
int active; int active;
struct pcap_file p; struct pcap_file p;
struct pcap_pkthdr next; struct pcap_pkthdr next;
}; };
int int
usage(int ret) usage(int ret)
{ {
fprintf(stderr, "Usage: pmerge FILE ...\n"); fprintf(stderr, "Usage: pmerge FILE ...\n");
fprintf(stderr, "\n"); fprintf(stderr, "\n");
fprintf(stderr, "Merges pcap files, outputting time-ordered pcap stream\n"); fprintf(stderr, "Merges pcap files, outputting time-ordered pcap stream\n");
return ret; return 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);
file->active = 0; file->active = 0;
file->next.ts.tv_sec = 0xffffffff; file->next.ts.tv_sec = 0xffffffff;
file->next.ts.tv_usec = 0xffffffff; file->next.ts.tv_usec = 0xffffffff;
return -1; return -1;
} }
return 0; return 0;
} }
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct input_file files[argc-1]; struct input_file files[argc - 1];
int nfiles = 0; int nfiles = 0;
int nopen; int nopen;
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 */ /*
for (i = 0; i < argc-1; i += 1) { * Open input files
char *fn = argv[i+1]; */
struct input_file *cur = &files[nfiles]; for (i = 0; i < argc - 1; i += 1) {
FILE *f; 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"); f = fopen(fn, "rb");
if (NULL == f) { if (NULL == f) {
perror(fn); perror(fn);
return EX_NOINPUT; return EX_NOINPUT;
} }
if (-1 == pcap_open_in(&cur->p, f)) { if (-1 == pcap_open_in(&cur->p, f)) {
fprintf(stderr, "%s: unable to process\n", fn); fprintf(stderr, "%s: unable to process\n", fn);
return EX_IOERR; return EX_IOERR;
} }
cur->active = 1; cur->active = 1;
if (0 == read_next(cur)) { if (0 == read_next(cur)) {
nfiles += 1; nfiles += 1;
} }
} }
if (-1 == pcap_open_out(&out, stdout)) { if (-1 == pcap_open_out(&out, stdout)) {
perror("writing header"); perror("writing header");
return EX_IOERR; return EX_IOERR;
} }
nopen = nfiles; nopen = nfiles;
while (nopen) { while (nopen) {
struct input_file *cur = &files[0]; struct input_file *cur = &files[0];
char frame[MAXFRAME]; char frame[MAXFRAME];
size_t len; size_t len;
/* Find next oldest frame */ /*
for (i = 0; i < nfiles; i += 1) { * Find next oldest frame
if (files[i].active && */
((files[i].next.ts.tv_sec < cur->next.ts.tv_sec) || for (i = 0; i < nfiles; i += 1) {
((files[i].next.ts.tv_sec == cur->next.ts.tv_sec) && if (files[i].active &&
(files[i].next.ts.tv_usec < cur->next.ts.tv_usec)))) { ((files[i].next.ts.tv_sec < cur->next.ts.tv_sec) ||
cur = &files[i]; ((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)) { * Make sure it'll fit
fprintf(stderr, "error: huge frame (size %u)\n", (unsigned int)len); */
return EX_SOFTWARE; 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); * Read it
if (len < cur->next.caplen) { */
/* Short read */ len = fread(frame, 1, cur->next.caplen, cur->p.f);
cur->next.caplen = len; if (len < cur->next.caplen) {
pcap_close(&cur->p); /*
cur->active = 0; * Short read
} */
cur->next.caplen = len;
pcap_close(&cur->p);
cur->active = 0;
}
/* Write header + frame */ /*
if (len) { * Write header + frame
if (1 != fwrite(&cur->next, sizeof(cur->next), 1, stdout)) { */
perror("error"); if (len) {
return EX_IOERR; if (1 != fwrite(&cur->next, sizeof(cur->next), 1, stdout)) {
} perror("error");
if (len != fwrite(frame, 1, len, stdout)) { return EX_IOERR;
perror("error"); }
return EX_IOERR; if (len != fwrite(frame, 1, len, stdout)) {
} perror("error");
} return EX_IOERR;
}
}
if (-1 == read_next(cur)) { if (-1 == read_next(cur)) {
nopen -= 1; nopen -= 1;
} }
} }
return 0; return 0;
} }

View File

@ -3,26 +3,26 @@
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
printf("printf "); printf("printf ");
while (! feof(stdin)) { while (!feof(stdin)) {
int c = getchar(); int c = getchar();
switch (c) { switch (c) {
case EOF: case EOF:
break; break;
case '0' ... '9': case '0' ... '9':
case 'A' ... 'Z': case 'A' ... 'Z':
case 'a' ... 'z': case 'a' ... 'z':
putchar(c); putchar(c);
break; break;
default: default:
printf("\\\\%03o", c); printf("\\\\%03o", c);
break; break;
} }
} }
putchar('\n'); putchar('\n');
return 0; return 0;
} }

95
puniq.c
View File

@ -6,58 +6,63 @@
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int i; int i;
struct pcap_pkthdr hdr[2]; struct pcap_pkthdr hdr[2];
char frame[2][MAXFRAME]; char frame[2][MAXFRAME];
int cur = 0; int cur = 0;
struct pcap_file out; struct pcap_file out;
memset(&hdr, 0, sizeof(hdr)); memset(&hdr, 0, sizeof(hdr));
memset(frame, 0, 2 * MAXFRAME); memset(frame, 0, 2 * MAXFRAME);
if (-1 == pcap_open_out(&out, stdout)) { if (-1 == pcap_open_out(&out, stdout)) {
perror("writing header"); perror("writing header");
return EX_IOERR; return EX_IOERR;
} }
i = 1; i = 1;
do { do {
char *fn = argv[i]; char *fn = argv[i];
FILE *f; FILE *f;
struct pcap_file p; struct pcap_file p;
if ((! fn) || (0 == strcmp("-", fn))) { if ((!fn) || (0 == strcmp("-", fn))) {
f = stdin; f = stdin;
} else { } else {
f = fopen(fn, "rb"); f = fopen(fn, "rb");
if (NULL == f) { if (NULL == f) {
perror(fn); perror(fn);
return EX_IOERR; return EX_IOERR;
} }
} }
if (-1 == pcap_open_in(&p, f)) { if (-1 == pcap_open_in(&p, f)) {
fprintf(stderr, "%s: unable to process\n", fn); fprintf(stderr, "%s: unable to process\n", fn);
return EX_IOERR; return EX_IOERR;
} }
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 { */
if (-1 == pcap_write_pkthdr(&out, &hdr[cur])) break; } else {
if (1 != fwrite(frame[cur], hdr[cur].caplen, 1, out.f)) break; if (-1 == pcap_write_pkthdr(&out, &hdr[cur]))
} break;
cur = (1 - cur); if (1 != fwrite(frame[cur], hdr[cur].caplen, 1, out.f))
} break;
pcap_close(&p); }
cur = (1 - cur);
}
pcap_close(&p);
i += 1; i += 1;
} while (i < argc); } while (i < argc);
return 0; return 0;
} }

36
pyesc.c
View File

@ -3,24 +3,24 @@
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
while (1) { while (1) {
int c = getchar(); int c = getchar();
switch (c) { switch (c) {
case EOF: case EOF:
return 0; return 0;
case 134: case 134:
printf("\\\\"); printf("\\\\");
break; break;
case 32 ... 91: case 32 ... 91:
case 93 ... 126: case 93 ... 126:
putchar(c); putchar(c);
break; break;
default: default:
printf("\\x%02x", c); printf("\\x%02x", c);
break; break;
} }
} }
return 0; return 0;
} }

105
stream.c
View File

@ -7,100 +7,87 @@
void void
sinit(struct stream *s, char const *buf, size_t buflen) sinit(struct stream *s, char const *buf, size_t buflen)
{ {
s->buf = buf; s->buf = buf;
s->len = buflen; s->len = buflen;
} }
bool bool
sskip(struct stream *s, size_t count) sskip(struct stream *s, size_t count)
{ {
if (count > s->len) { if (count > s->len) {
s->len = 0; s->len = 0;
return false; return false;
} }
s->buf += count; s->buf += count;
s->len -= count; s->len -= count;
return true; return true;
} }
bool 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)) { if (!sskip(s, count)) {
return false; return false;
} }
memcpy(buf, d, count); memcpy(buf, d, count);
return true; return true;
} }
uint8_t 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)) { if (!sskip(s, 1)) {
return 0; return 0;
} }
return d[0]; return d[0];
} }
uint16_t 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)) { 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
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)) { 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
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)) { 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
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)) { 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

@ -6,8 +6,8 @@
#include <stddef.h> #include <stddef.h>
struct stream { struct stream {
char const *buf; char const *buf;
size_t len; size_t len;
}; };
void sinit(struct stream *s, char const *buf, size_t len); void sinit(struct stream *s, char const *buf, size_t len);

75
unhex.c
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>
@ -9,43 +8,43 @@
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
unsigned char acc = 0; unsigned char acc = 0;
unsigned char nybble = 0; unsigned char nybble = 0;
unsigned long int count = 0; unsigned long int count = 0;
while (1) { while (1) {
int c = getchar(); int c = getchar();
count += 1; count += 1;
switch (c) { switch (c) {
case EOF: case EOF:
return 0; return 0;
case '0' ... '9': case '0' ... '9':
acc = (acc << 4) + c - '0'; acc = (acc << 4) + c - '0';
nybble += 1; nybble += 1;
break; break;
case 'a' ... 'f': case 'a' ... 'f':
acc = (acc << 4) + c - 'a' + 10; acc = (acc << 4) + c - 'a' + 10;
nybble += 1; nybble += 1;
break; break;
case 'A' ... 'F': case 'A' ... 'F':
acc = (acc << 4) + c - 'A' + 10; acc = (acc << 4) + c - 'A' + 10;
nybble += 1; nybble += 1;
break; break;
default: default:
if (nybble != 0) { if (nybble != 0) {
fprintf(stderr, "Warning: non-hex character mid-octet at offset %lu\n", count); fprintf(stderr, "Warning: non-hex character mid-octet at offset %lu\n", count);
} }
break; break;
} }
if (nybble == 2) { if (nybble == 2) {
putchar(acc); putchar(acc);
acc = 0; acc = 0;
nybble = 0; nybble = 0;
} }
} }
return 0; return 0;
} }

61
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>
@ -11,38 +10,38 @@
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int start = 1; int start = 1;
int base = 0; int base = 0;
int arg; int arg;
if (argc < 2) { if (argc < 2) {
fprintf(stderr, "Usage: %s [-x] m1 [m2 ...]\n", argv[0]); fprintf(stderr, "Usage: %s [-x] m1 [m2 ...]\n", argv[0]);
return 1; return 1;
} }
if (0 == strcmp(argv[1], "-x")) { if (0 == strcmp(argv[1], "-x")) {
base = 16; base = 16;
start += 1; start += 1;
} }
arg = start; arg = start;
while (1) { while (1) {
int c = getchar(); int c = getchar();
unsigned char mask; unsigned char mask;
if (! argv[arg]) {
arg = start;
}
mask = strtol(argv[arg++], NULL, base);
if (EOF == c) { if (!argv[arg]) {
break; arg = start;
} }
mask = strtol(argv[arg++], NULL, base);
c ^= mask; if (EOF == c) {
putchar(c); break;
} }
return 0; c ^= mask;
putchar(c);
}
return 0;
} }