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
TARGETS = pmerge puniq p4split hd
TARGETS += pyesc printfesc pcat
TARGETS = pmerge puniq p4split pcat
TARGETS += hd pyesc printfesc xor unhex
all: $(TARGETS)

147
hd.c
View File

@ -2,96 +2,97 @@
#include <stdint.h>
#include <string.h>
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;
}

187
p4split.c
View File

@ -5,7 +5,9 @@
#include <strings.h>
#include "pcap.h"
/* Gaah so much crap */
/*
* Gaah so much crap
*/
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/ether.h>
@ -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;
}

101
pcap.c
View File

@ -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);
}

44
pcap.h
View File

@ -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__ */

205
pcat.c
View File

@ -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;
}
}

193
pmerge.c
View File

@ -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;
}

View File

@ -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;
}

95
puniq.c
View File

@ -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;
}

36
pyesc.c
View File

@ -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;
}

105
stream.c
View File

@ -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));
}

View File

@ -6,8 +6,8 @@
#include <stddef.h>
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);

75
unhex.c
View File

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

61
xor.c
View File

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