ref: 13738d2aa200eeb5859525225ec1d7bec7e8134e
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)
{
p->start = pos;
/* they'r both half a byte */
p->version = IP4_V(pkt[pos]);
p->headerlen = IP4_HL(pkt[pos]) * 4;
pos += PKT_VHL;
get(pkt, PKT_DSFIELD, &p->dsfield);
get(pkt, PKT_LEN, &p->len);
get(pkt, PKT_ID, &p->id);
/* flags are just 3 bits, smaller than a byte */
p->flags = pkt[pos];
/* this one is 12 bits */
p->fragoffset = get2(pkt + pos);
p->fragoffset &= 0x1FFF;
pos += PKT_FRAGOFFSET;
get(pkt, PKT_TTL, &p->ttl);
get(pkt, PKT_PROTO, &p->proto);
get(pkt, PKT_SUM, &p->sum);
get(pkt, PKT_SRC, &p->srcip);
get(pkt, PKT_SRC, &p->dstip);
return 1;
}
void
printPkt(Pkt p)
{
printf("ip packet info:\n"
"\tversion: %#x\theaderlen: %#x\tdsfield: %b\n",
p.version, p.headerlen, p.dsfield);
printf("\tlen: %d (%#x)\tid: %#x (%d)\n",
p.len, p.len, p.id, p.id);
printf("\tflags: %b\tfragment offset: %b\n",
p.flags, p.fragoffset);
printf("\tttl: %#x (%d)\tproto %#x (%s)\tsum: %#x\n",
p.ttl, p.ttl, p.proto, pktTypeToStr(p.proto), p.sum);
printf("\tsrcip: %#x\tdstip: %#x\n", p.srcip, p.dstip);
}
Parser pktParser = {
.name = "ipv4",
.parse = parsePkt,
.print = printPkt,
};