diff options
author | Håkon Nessjøen <haakon.nessjoen@gmail.com> | 2010-09-28 00:07:49 +0200 |
---|---|---|
committer | Håkon Nessjøen <haakon.nessjoen@gmail.com> | 2010-09-28 00:07:49 +0200 |
commit | c90ce96f3f86404ee85b390a3ed4b575a257423d (patch) | |
tree | b779d3773e2a69a4b46a2c58e72a8e5971bed73f | |
parent | 86460e9be93df399286e63b8142853d6e5aee5a9 (diff) | |
download | MAC-Telnet-c90ce96f3f86404ee85b390a3ed4b575a257423d.tar.gz MAC-Telnet-c90ce96f3f86404ee85b390a3ed4b575a257423d.zip |
Comment cleanup
-rw-r--r-- | devices.c | 34 | ||||
-rw-r--r-- | mactelnet.c | 13 | ||||
-rw-r--r-- | mactelnet.h | 6 | ||||
-rw-r--r-- | main.c | 32 | ||||
-rw-r--r-- | udp.c | 12 |
5 files changed, 68 insertions, 29 deletions
@@ -1,3 +1,21 @@ +/* + Mac-Telnet - Connect to RouterOS clients via MAC address + Copyright (C) 2010, Håkon Nessjøen <haakon.nessjoen@gmail.com> + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ #include <stdlib.h> #include <string.h> #include <malloc.h> @@ -7,6 +25,8 @@ #include <sys/ioctl.h> #include <net/if.h> +/* Functions using NETDEVICE api */ + int getDeviceIndex(int sockfd, unsigned char *deviceName) { struct ifreq ifr; @@ -15,6 +35,7 @@ int getDeviceIndex(int sockfd, unsigned char *deviceName) { return -1; } + /* Return interface index */ return ifr.ifr_ifindex; } @@ -26,8 +47,8 @@ int getDeviceMAC(const int sockfd, const unsigned char *deviceName, unsigned cha return -1; } + /* Fetch mac address */ memcpy(mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN); - return 1; } @@ -36,26 +57,35 @@ int getDeviceIp(const int sockfd, const unsigned char *deviceName, struct sockad struct ifreq *ifr; int i,numDevices; + /* + * Do a initial query without allocating ifreq structs + * to count the number of ifreq structs to allocate memory for + */ memset(&ifc, 0, sizeof(ifc)); if (ioctl(sockfd, SIOCGIFCONF, &ifc) != 0) { return -1; } + /* + * Allocate memory for interfaces, multiply by two in case + * the number of interfaces has increased since last ioctl + */ if ((ifr = malloc(ifc.ifc_len * 2)) == NULL) { perror("malloc"); exit(1); } ifc.ifc_req = ifr; - if (ioctl(sockfd, SIOCGIFCONF, &ifc) != 0) { free(ifr); return -1; } + /* Iterate through all devices, searching for interface */ numDevices = ifc.ifc_len / sizeof(struct ifreq); for (i = 0; i < numDevices; ++i) { if (strcmp(ifr[i].ifr_name, deviceName) == 0) { + /* Fetch IP for found interface */ memcpy(ip, &(ifr[i].ifr_addr), sizeof(ip)); free(ifr); return 1; diff --git a/mactelnet.c b/mactelnet.c index 8cfab77..fd814a6 100644 --- a/mactelnet.c +++ b/mactelnet.c @@ -24,16 +24,16 @@ 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 + /* src ethernet address */ memcpy(data + 2, srcmac, ETH_ALEN); - // dst ethernet address + /* dst ethernet address */ memcpy(data + 8, dstmac, ETH_ALEN); data[14] = sessionkey >> 8; @@ -56,15 +56,16 @@ int addControlPacket(unsigned char *data, unsigned char cptype, void *cpdata, in data[2] = 0x12; data[3] = 0xff; - // Control packet type + /* Control packet type */ data[4] = cptype; - // Data length + /* Data length */ data[5] = (data_len >> 24) & 0xff; data[6] = (data_len >> 16) & 0xff; data[7] = (data_len >> 8) & 0xff; data[8] = data_len & 0xff; + /* Insert data */ if (data_len) { memcpy(data+9, cpdata, data_len); } diff --git a/mactelnet.h b/mactelnet.h index c9c5c02..d2af675 100644 --- a/mactelnet.h +++ b/mactelnet.h @@ -19,13 +19,13 @@ #ifndef _MACTELNET_H #define _MACTELNET_H 1 -// Packet type +/* Packet type */ #define MT_PTYPE_SESSIONSTART 0 #define MT_PTYPE_DATA 1 #define MT_PTYPE_ACK 2 #define MT_PTYPE_END 255 -// Control packet type +/* Control packet type */ #define MT_CPTYPE_BEGINAUTH 0 #define MT_CPTYPE_ENCRYPTIONKEY 1 #define MT_CPTYPE_PASSWORD 2 @@ -35,7 +35,7 @@ #define MT_CPTYPE_TERM_HEIGHT 6 #define MT_CPTYPE_PACKET_ERROR 7 #define MT_CPTYPE_END_AUTH 9 -// Internal CPTYPE, not part of protocol +/* Internal CPTYPE, not part of protocol */ #define MT_CPTYPE_PLAINDATA -1 struct mt_mactelnet_hdr { @@ -130,7 +130,7 @@ void handlePacket(unsigned char *data, int data_len) { } } else if (pkthdr.ptype == MT_PTYPE_ACK) { - // TODO: If we were resubmitting lost messages, stop resubmitting here if received counter is correct. + /* TODO: If we were resubmitting lost messages, stop resubmitting here if received counter is correct. */ } else if (pkthdr.ptype == MT_PTYPE_END) { char odata[200]; @@ -144,6 +144,9 @@ void handlePacket(unsigned char *data, int data_len) { } } +/* + * TODO: Rewrite main() when all sub-functionality is tested + */ int main (int argc, char **argv) { int insockfd; int result; @@ -163,10 +166,10 @@ int main (int argc, char **argv) { srand(time(NULL)); - // Transmit raw packets with this socket + /* Transmit raw packets with this socket */ sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); - // Receive regular udp packets with this socket + /* Receive regular udp packets with this socket */ insockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); deviceIndex = getDeviceIndex(sockfd, argv[1]); @@ -175,37 +178,40 @@ int main (int argc, char **argv) { return 1; } - // Even though we talk to the server without IP address, it makes it much - // easier to read packets when we use our real ip as the sender ip. - // This way we can listen to normal UDP traffic on port 20561 + /* + * Even though we talk to the server without IP address, it makes it much + * easier to read packets when we use our real ip as the sender ip. + * This way we can listen to normal UDP traffic on port 20561 + */ result = getDeviceIp(sockfd, argv[1], &si_me); if (result < 0) { fprintf(stderr, "Cannot determine IP of device %s\n", argv[1]); return 1; } + /* Determine source mac address */ result = getDeviceMAC(sockfd, argv[1], srcmac); if (result < 0) { fprintf(stderr, "Cannot determine MAC address of device %s\n", argv[1]); return 1; } - // Set up global info about the connection + /* Set up global info about the connection */ inet_pton(AF_INET, (char *)"255.255.255.255", &destip); memcpy(&sourceip, &(si_me.sin_addr), 4); - // Initialize receiving socket on the device chosen + /* Initialize receiving socket on the device chosen */ memset((char *) &si_me, 0, sizeof(si_me)); si_me.sin_family = AF_INET; si_me.sin_port = htons(20561); - // Bind to udp port + /* Bind to udp port */ if (bind(insockfd, (struct sockaddr *)&si_me, sizeof(si_me))==-1) { fprintf(stderr, "Error binding to %s:20561\n", inet_ntoa(si_me.sin_addr)); return 1; } - // Sessioon key + /* Sessioon key */ sessionkey = rand() % 65535; printf("Connecting to %s...\n", ether_ntoa((struct ether_addr *)dstmac)); @@ -220,8 +226,10 @@ int main (int argc, char **argv) { result = recvfrom(insockfd, buff, 1400, 0, 0, 0); handlePacket(buff, result); - // TODO: Should resubmit whenever a PTYPE_DATA packet is sent, and an ACK packet with correct datacounter is received - // or time out the connection, in all other cases. + /* + * TODO: Should resubmit whenever a PTYPE_DATA packet is sent, and an ACK packet with correct datacounter is received + * or time out the connection, in all other cases. + */ plen = initPacket(data, MT_PTYPE_DATA, srcmac, dstmac, sessionkey, 0); plen += addControlPacket(data + plen, MT_CPTYPE_BEGINAUTH, NULL, 0); outcounter += 9; @@ -49,12 +49,12 @@ int sendCustomUDP(const int socket, const int ifindex, const unsigned char *sour exit(1); } - // Ethernet header + /* Ethernet header */ memcpy(eh->h_source, sourcemac, ETH_ALEN); memcpy(eh->h_dest, destmac, ETH_ALEN); eh->h_proto = 8; - // SendTo struct + /* SendTo struct */ socket_address.sll_family = PF_PACKET; socket_address.sll_protocol = htons(ETH_P_IP); socket_address.sll_ifindex = ifindex; @@ -66,7 +66,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 + /* IP Header */ ip->version = 4; ip->ihl = 5; ip->tos = 0x10; @@ -74,13 +74,13 @@ int sendCustomUDP(const int socket, const int ifindex, const unsigned char *sour ip->id = htons(id++); ip->frag_off = 0x0040; ip->ttl = 64; - ip->protocol = 17; // UDP + ip->protocol = 17; /* UDP */ ip->check = 0x0000; ip->saddr = sourceip->s_addr; ip->daddr = destip->s_addr; ip->check = in_cksum((unsigned short *)ip, sizeof(struct iphdr)); - // UDP Header + /* UDP Header */ udp->source = htons(20561); udp->dest = htons(20561); udp->check = 0; @@ -88,7 +88,7 @@ int sendCustomUDP(const int socket, const int ifindex, const unsigned char *sour memcpy(rest, data, datalen); - /*send the packet*/ + /* Send the packet */ send_result = sendto(socket, buffer, datalen+8+14+20, 0, (struct sockaddr*)&socket_address, sizeof(socket_address)); free(buffer); |