wm: dnsparser

ref: ba2943ae190fdc67a3f9ce5e1668eda51e3f64b1
dir: /ether.c/

View raw version
#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,
};