nethack.h

I made this header in hopes of it being useful to anyone including me. This header contains the includes for socket programming as well as structures for ethernet, ip, tcp, udp, and icmp echo headers. Not to mention a function for calculating the checksum for the headers.

#if defined(_WIN32)
#pragma message("Warning: Windows detected.")
#include <Windows.h>
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <IPHlpApi.h>
#elif defined(linux)
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#else 
#error gtfo macfag.
#endif

/* #include <pcap.h> */
#include <cstdlib>
#include <ctime>

typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned int uint;
typedef unsigned long ulong;

namespace nethack {

	/* Ethernet header */
#define ETHHDR_LEN 14
#define MACADDR_LEN 6;
	struct ethhdr {
		uchar macsrc[6];	/* Source mac address */
		uchar macdst[6];	/* Destination mac address */
		ushort type;		/* Packet type */
	};

	/* Internet Datagram Header */
#define IPHDR_LEN 20
	struct iphdr {
		uchar ipv:4;     /* Internet Protocol Version */
		uchar ihl:4;     /* Total length (in DWORDs) */
		uchar tos;       /* Type of Service */
		ushort len;      /* Total length */
		ushort id;       /* Identification number */
		ushort frag;     /* Fragment offset and flags */
		uchar ttl;       /* Time to live */
		uchar proto;     /* Protocol type */
		ushort check;    /* Checksum */
		uint src;        /* Source IP Address */
		uint dst;        /* Destination IP Address */
	};
	
	/*	TCP Header */	
#define TCPHDR_LEN 20
	struct tcphdr {
		ushort sport;      /* Source Port */
		ushort dport;      /* Destination Port */
		uint seq;          /* Sequence number */
		uint ack;          /* Acknowledgement number */
		uchar reserved:4;
		uchar offset:4;    /* Size of TCP Header in DWORDs */
		uchar flgs;        /* TCP Flags */
#define TCP_FIN 0x01
#define TCP_SYN 0x02
#define TCP_RST 0x04
#define TCP_PSH 0x08
#define TCP_ACK 0x10
#define TCP_URG 0x20
		ushort win;        /* Window. Size of data to accept */
		ushort check;      /* Checksum */
		ushort urgp;       /* idk */
	};

	/* TCP Psuedo-header */
#define TCPPH_LEN 12
	struct tcpph {
		uint src;			/* Source IP address */
		uint dst;			/* Destination IP address */
		uchar zero;			/* Reserved to zero */
		uchar proto;		/* Protocol (IPPROTO_TCP) */
		ushort tcp_len;	/* TCP header length */
	};

	/* UDP header */
#define UDPHDR_LEN 8
	struct udphdr {
		ushort sport;		/* Source port */
		ushort dport;		/* Destination port */
		ushort len;			/* Length of header and data */
		ushort check;		/* Checksum */
	};

/* ICMP Echo Header */
#define ICMPHDR_LEN 8
	struct icmphdr {
		uchar type;			/* Echo packet type */
#define ICMP_PING 8
#define ICMP_PONG 0
		uchar code;			/* Code number for type */
		ushort check;		/* Checksum */
		ushort id;			/* Id */
		ushort seq;			/* Sequence number */
	};

	ushort csum(short* data, int len) {
		int sum = 0;
		for (; len > 1; len -= 2) sum += *data++;
		if (len == 1) sum += *(uchar*)data;
		while (sum >> 16) sum = (sum & 0xffff) + (sum >> 16);
		return ~sum;
	}

	ushort rand16() {
		srand(time(0));
		srand(rand());
		return (rand() + time(0)) % 65535;
	}

	uint rand32() {
		srand(time(0));
		srand(rand());
		return (rand() & time(0));
	}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: