From c56e309956da3d41420db1c5d41ccf245c594e45 Mon Sep 17 00:00:00 2001 From: Håkon Nessjøen Date: Thu, 30 Sep 2010 11:42:19 +0200 Subject: Add a lot of comments to the code --- mactelnet.c | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) (limited to 'mactelnet.c') diff --git a/mactelnet.c b/mactelnet.c index fd814a6..0d8de3d 100644 --- a/mactelnet.c +++ b/mactelnet.c @@ -22,12 +22,14 @@ #include "mactelnet.h" #include "config.h" +unsigned char mt_mactelnet_cpmagic[4] = { 0x56, 0x34, 0x12, 0xff }; + int initPacket(unsigned char *data, unsigned char ptype, unsigned char *srcmac, unsigned char *dstmac, unsigned short sessionkey, unsigned short counter) { - /* PACKET VERSION */ + /* Packet version */ data[0] = 1; - /* PACKET TYPE */ + /* Packet type */ data[1] = ptype; /* src ethernet address */ @@ -36,25 +38,27 @@ int initPacket(unsigned char *data, unsigned char ptype, unsigned char *srcmac, /* dst ethernet address */ memcpy(data + 8, dstmac, ETH_ALEN); + /* Session key */ data[14] = sessionkey >> 8; data[15] = sessionkey & 0xff; + /* Magic number */ data[16] = 0x00; data[17] = 0x15; + /* Received/sent data counter */ data[18] = (counter >> 24) & 0xff; data[19] = (counter >> 16) & 0xff; data[20] = (counter >> 8) & 0xff; data[21] = counter & 0xff; + /* 22 bytes header */ return 22; } int addControlPacket(unsigned char *data, unsigned char cptype, void *cpdata, int data_len) { - data[0] = 0x56; - data[1] = 0x34; - data[2] = 0x12; - data[3] = 0xff; + /* Control Packet Magic id */ + memcpy(data, mt_mactelnet_cpmagic, sizeof(mt_mactelnet_cpmagic)); /* Control packet type */ data[4] = cptype; @@ -70,40 +74,63 @@ int addControlPacket(unsigned char *data, unsigned char cptype, void *cpdata, in memcpy(data+9, cpdata, data_len); } + /* Control packet header length + data length */ return 9+data_len; } void parsePacket(unsigned char *data, struct mt_mactelnet_hdr *pkthdr) { + /* Packet version */ pkthdr->ver = data[0]; + + /* Packet type */ pkthdr->ptype = data[1]; + + /* src ethernet addr */ memcpy(pkthdr->srcaddr, data+2,6); + + /* dst ethernet addr */ memcpy(pkthdr->dstaddr, data+8,6); + + /* Session key */ pkthdr->seskey = data[16] << 8 | data[17]; + + /* Received/sent data counter */ pkthdr->counter = data[18] << 24 | data[19] << 16 | data[20] << 8 | data[21]; + + /* Set pointer to actual data */ pkthdr->data = data + 22; } int parseControlPacket(unsigned char *data, const int data_len, struct mt_mactelnet_control_hdr *cpkthdr) { - unsigned char magic[] = { 0x56, 0x34, 0x12, 0xff }; - if (data_len <= 0) + if (data_len < 0) return 0; - if (memcmp(data, &magic, 4) == 0) { + /* Check for valid minimum packet length & magic header */ + if (data_len >= 9 && memcmp(data, &mt_mactelnet_cpmagic, 4) == 0) { if (DEBUG) printf("\t----Control packet:\n\t\tType: %d\n\t\tLength: %d\n", data[4], data[5]<<24|data[6]<<16|data[7]<<8|data[8]); + /* Control packet type */ cpkthdr->cptype = data[4]; + + /* Control packet data length */ cpkthdr->length = data[5]<<24|data[6]<<16|data[7]<<8|data[8]; + + /* Set pointer to actual data */ cpkthdr->data = data + 9; + /* Return number of bytes in packet */ return cpkthdr->length + 9; } else { + /* Mark data as raw terminal data */ cpkthdr->cptype = MT_CPTYPE_PLAINDATA; cpkthdr->length = data_len; cpkthdr->data = data; + + /* Consume the whole rest of the packet */ return data_len; } } -- cgit v1.2.3