ref: 468c25eef8cfd6d979d050a742e680216a83220a
dir: /pkt.c/
#include <stdio.h>
#include <stdint.h>
#include <pcap.h>
#include "common.h"
/*
* todo: replace IPROTO with our own
*/
char*
pktTypeToStr(const u_char pkt)
{
switch(pkt)
{
case IPPROTO_UDP:
return "UDP";
case IPPROTO_TCP:
return "TCP";
default:
err(0, "pktTypeToStr(%d): unkown type", pkt);
return NULL;
}
}
int
parsePkt(const u_char *pkt, Pkt *p)
{
int pos = 0;
p->version = IP4_V(pkt[pos]);
p->headerlen = IP4_HL(pkt[pos]) * 4;
pos += PKT_VHL;
p->dsfield = pkt[pos];
pos += PKT_DSFIELD;
p->len = get2(pkt + pos);
pos += PKT_LEN;
p->id = get2(pkt + pos);
pos += PKT_ID;
p->flags = pkt[pos];
/* flags are just 3 bits, smaller than a byte */
p->fragoffset = get2(pkt + pos);
p->fragoffset &= 0x1FFF;
pos += PKT_FRAGOFFSET;
p->ttl = pkt[pos];
pos += PKT_TTL;
p->proto = pkt[pos];
pos += PKT_PROTO;
p->sum = get2(pkt + pos);
pos += PKT_SUM;
p->srcip = get4(pkt + pos);
pos += PKT_SRC;
p->dstip = get4(pkt + pos);
pos += PKT_DST;
return 1;
}
void
printPkt(Pkt p)
{
printf("ip packet info:\n"
"\tversion: 0x%x\theaderlen: 0x%x\tdsfield: %b\n",
p.version, p.headerlen, p.dsfield);
printf("\tlen: %d (%x)\tid: 0x%x (%d)\n",
p.len, p.len, p.id, p.id);
printf("\tttl: 0%x (%d)\tproto 0%x (%s)\tsum: 0x%x\n",
p.ttl, p.ttl, p.proto, pktTypeToStr(p.proto), p.sum);
printf("\tsrcip: 0x%x\tdstip: 0x%x\n", p.srcip, p.dstip);
}
Parser pktParser = {
.parse = parsePkt,
.print = printPkt,
};