ref: 2333328cb5d4c442b9764513dcd57c75e261d564
dir: /ether.c/
#include <stdio.h>
#include <stdint.h>
#include <pcap/pcap.h>
#include "common.h"
char*
etherTypeToStr(uint16_t frame)
{
switch(frame)
{
case ETHER_IP4:
return "IPv4";
case ETHER_IP6:
return "IPv6";
default:
err(1, "etherTypeToStr(%d): unkown type", frame);
return "unkown";
}
}
int
parseEther(const u_char *pkt, Ether *e)
{
int pos = 0;
for(int i = 0 ; i < ETHER_ADDR_LEN ; i++)
e->dst[i] = pkt[i];
pos += ETHER_DST;
for(int i = 0 ; i < ETHER_ADDR_LEN ; i++)
e->src[i] = pkt[i+ETHER_ADDR_LEN];
pos += ETHER_SRC;
e->type = get2(pkt + pos);
return 1;
}
void
printEther(Ether e)
{
printf("ether info:\n\tdst:");
for(int i = 0 ; i < ETHER_ADDR_LEN ; i++)
printf("%x:", e.dst[i]);
printf("\tsrc: ");
for(int i = 0 ; i < ETHER_ADDR_LEN ; i++)
printf("%x:", e.src[i]);
printf("\ttype: 0x%x (%s)\n", e.type, etherTypeToStr(e.type));
}
Parser etherParser = {
.parse = parseEther,
.print = printEther,
};