diff options
author | Håkon Nessjøen <haakon.nessjoen@gmail.com> | 2010-09-30 11:42:19 +0200 |
---|---|---|
committer | Håkon Nessjøen <haakon.nessjoen@gmail.com> | 2010-09-30 11:42:19 +0200 |
commit | c56e309956da3d41420db1c5d41ccf245c594e45 (patch) | |
tree | bb28c944f9017024d4f83be74b3f82cd451b5d5e | |
parent | 87341fcc53b58d270528192607d3ce547abffa77 (diff) | |
download | MAC-Telnet-c56e309956da3d41420db1c5d41ccf245c594e45.tar.gz MAC-Telnet-c56e309956da3d41420db1c5d41ccf245c594e45.zip |
Add a lot of comments to the code
-rw-r--r-- | devices.c | 3 | ||||
-rw-r--r-- | mactelnet.c | 45 | ||||
-rw-r--r-- | mactelnet.h | 3 | ||||
-rw-r--r-- | mndp.c | 11 | ||||
-rw-r--r-- | udp.c | 18 |
5 files changed, 67 insertions, 13 deletions
@@ -30,6 +30,7 @@ int getDeviceIndex(int sockfd, unsigned char *deviceName) { struct ifreq ifr; + /* Find interface index from deviceName */ strncpy(ifr.ifr_name, deviceName, 16); if (ioctl(sockfd, SIOCGIFINDEX, &ifr) != 0) { return -1; @@ -42,6 +43,7 @@ int getDeviceIndex(int sockfd, unsigned char *deviceName) { int getDeviceMAC(const int sockfd, const unsigned char *deviceName, unsigned char *mac) { struct ifreq ifr; + /* Find interface hardware address from deviceName */ strncpy(ifr.ifr_name, deviceName, 16); if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) != 0) { return -1; @@ -75,6 +77,7 @@ int getDeviceIp(const int sockfd, const unsigned char *deviceName, struct sockad exit(1); } + /* Do the actual query for info about all interfaces */ ifc.ifc_req = ifr; if (ioctl(sockfd, SIOCGIFCONF, &ifc) != 0) { free(ifr); 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; } } diff --git a/mactelnet.h b/mactelnet.h index d2af675..8275169 100644 --- a/mactelnet.h +++ b/mactelnet.h @@ -59,4 +59,7 @@ extern int addControlPacket(unsigned char *data, unsigned char cptype, void *cpd extern void parsePacket(unsigned char *data, struct mt_mactelnet_hdr *pkthdr); extern int parseControlPacket(unsigned char *data, const int data_len, struct mt_mactelnet_control_hdr *cpkthdr); +/* Control packet magic header */ +extern unsigned char mt_mactelnet_cpmagic[4]; + #endif @@ -31,13 +31,16 @@ int main(int argc, char **argv) { unsigned char name[100]; unsigned char mac[ETH_ALEN]; + /* Open a UDP socket handle */ sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + /* Set initialize address/port */ memset((char *) &si_me, 0, sizeof(si_me)); si_me.sin_family = AF_INET; si_me.sin_port = htons(5678); si_me.sin_addr.s_addr = htonl(INADDR_ANY); + /* Bind to specified address/port */ if (bind(sock, (struct sockaddr *)&si_me, sizeof(si_me))==-1) { fprintf(stderr, "Error binding to %s:5678\n", inet_ntoa(si_me.sin_addr)); return 1; @@ -47,25 +50,33 @@ int main(int argc, char **argv) { fprintf(stderr, "Searching for MikroTik routers... Abort with CTRL+C.\n"); while(1) { + /* Wait for a UDP packet */ result = recvfrom(sock, buff, 1500, 0, 0, 0); if (result < 0) { fprintf(stderr, "Error occured. aborting\n"); exit(1); } + /* Fetch length of Identifier string */ memcpy(&nameLen, buff+16,2); nameLen = (nameLen >> 8) | ((nameLen&0xff)<<8); /* Max name length = 99 */ nameLen = nameLen < 100 ? nameLen : 99; + /* Read Identifier string */ memcpy(&name, buff+18, nameLen); + + /* Append zero */ name[nameLen] = 0; + /* Read source MAC address */ memcpy(&mac, buff+8, ETH_ALEN); + /* Print it */ printf("%17s %s\n", ether_ntoa((struct ether_addr *)mac), name); } + /* We'll never get here.. */ return 0; } @@ -36,25 +36,32 @@ unsigned short in_cksum(unsigned short *addr, int len) int sendCustomUDP(const int socket, const int ifindex, const unsigned char *sourcemac, const unsigned char *destmac, const struct in_addr *sourceip, const int sourceport, const struct in_addr *destip, const int destport, const char *data, const int datalen) { struct sockaddr_ll socket_address; + + /* + * Create a buffer for the full ethernet frame + * and align header pointers to the correct positions. + */ void* buffer = (void*)malloc(ETH_FRAME_LEN); struct ethhdr *eh = (struct ethhdr *)buffer; struct iphdr *ip = (struct iphdr *)(buffer+14); struct udphdr *udp = (struct udphdr *)(buffer+14+20); unsigned char *rest = (unsigned char *)(buffer+20+14+sizeof(struct udphdr)); + static unsigned int id = 1; int send_result = 0; + /* Abort if we couldn't allocate enough memory */ if (buffer == NULL) { perror("malloc"); exit(1); } - /* Ethernet header */ + /* Init ethernet header */ memcpy(eh->h_source, sourcemac, ETH_ALEN); memcpy(eh->h_dest, destmac, ETH_ALEN); eh->h_proto = 8; - /* SendTo struct */ + /* Init SendTo struct */ socket_address.sll_family = PF_PACKET; socket_address.sll_protocol = htons(ETH_P_IP); socket_address.sll_ifindex = ifindex; @@ -66,7 +73,7 @@ int sendCustomUDP(const int socket, const int ifindex, const unsigned char *sour socket_address.sll_addr[6] = 0x00;/*not used*/ socket_address.sll_addr[7] = 0x00;/*not used*/ - /* IP Header */ + /* Init IP Header */ ip->version = 4; ip->ihl = 5; ip->tos = 0x10; @@ -78,19 +85,22 @@ int sendCustomUDP(const int socket, const int ifindex, const unsigned char *sour ip->check = 0x0000; ip->saddr = sourceip->s_addr; ip->daddr = destip->s_addr; + /* Calculate checksum for IP header */ ip->check = in_cksum((unsigned short *)ip, sizeof(struct iphdr)); - /* UDP Header */ + /* Init UDP Header */ udp->source = htons(20561); udp->dest = htons(20561); udp->check = 0; udp->len = htons(sizeof(struct udphdr) + datalen); + /* Insert actual data */ memcpy(rest, data, datalen); /* Send the packet */ send_result = sendto(socket, buffer, datalen+8+14+20, 0, (struct sockaddr*)&socket_address, sizeof(socket_address)); free(buffer); + /* Return amount of _data_ bytes sent */ return send_result-8-14-20; } |