From 94ac14d2765051a8992c3a822547bc09260492df Mon Sep 17 00:00:00 2001 From: Håkon Nessjøen Date: Sat, 12 Nov 2011 04:53:12 +0100 Subject: Rewrote the interface handling. Added full support for kFreeBSD, have not tested with FreeBSD. --- mactelnet.c | 71 ++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 40 insertions(+), 31 deletions(-) (limited to 'mactelnet.c') diff --git a/mactelnet.c b/mactelnet.c index ce91d01..0f6334a 100644 --- a/mactelnet.c +++ b/mactelnet.c @@ -1,4 +1,4 @@ -/* +/*find Mac-Telnet - Connect to RouterOS or mactelnetd devices via MAC address Copyright (C) 2010, Håkon Nessjøen @@ -32,21 +32,22 @@ #include #include #include +#ifdef __LINUX__ #include +#endif #include "md5.h" #include "protocol.h" -#include "udp.h" #include "console.h" -#include "devices.h" +#include "interfaces.h" #include "config.h" #include "mactelnet.h" #define PROGRAM_NAME "MAC-Telnet" #define PROGRAM_VERSION "0.3.2" -static int sockfd; +static int sockfd = 0; static int insockfd; -static int device_index; + static unsigned int outcounter = 0; static unsigned int incounter = 0; static int sessionkey = 0; @@ -73,6 +74,9 @@ static unsigned char encryptionkey[128]; static char username[255]; static char password[255]; +struct net_interface interfaces[MAX_INTERFACES]; +struct net_interface *active_interface; + /* Protocol data direction */ unsigned char mt_direction_fromserver = 0; @@ -99,7 +103,7 @@ static int send_udp(struct mt_packet *packet, int retransmit) { sent_bytes = sendto(send_socket, packet->data, packet->size, 0, (struct sockaddr*)&socket_address, sizeof(socket_address)); } else { - sent_bytes = send_custom_udp(sockfd, device_index, srcmac, dstmac, &sourceip, sourceport, &destip, MT_MACTELNET_PORT, packet->data, packet->size); + sent_bytes = net_send_udp(sockfd, active_interface, srcmac, dstmac, &sourceip, sourceport, &destip, MT_MACTELNET_PORT, packet->data, packet->size); } /* @@ -126,7 +130,7 @@ static int send_udp(struct mt_packet *packet, int retransmit) { if (reads && FD_ISSET(insockfd, &read_fds)) { unsigned char buff[1500]; int result; - + bzero(buff, 1500); result = recvfrom(insockfd, buff, 1500, 0, 0, 0); @@ -307,28 +311,39 @@ static int handle_packet(unsigned char *data, int data_len) { } static int find_interface() { + fd_set read_fds; struct mt_packet data; struct sockaddr_in myip; - int success; - char devicename[128]; - int testsocket; - fd_set read_fds; + unsigned char emptymac[ETH_ALEN]; + int i, testsocket; struct timeval timeout; int optval = 1; - - while ((success = get_ips(devicename, 128, &myip))) { - char str[INET_ADDRSTRLEN]; + + /* TODO: reread interfaces on HUP */ + bzero(&interfaces, sizeof(struct net_interface) * MAX_INTERFACES); + + bzero(emptymac, ETH_ALEN); + + if (net_get_interfaces(interfaces, MAX_INTERFACES) <= 0) { + fprintf(stderr, "Error: No suitable devices found\n"); + exit(1); + } + + for (i = 0; i < MAX_INTERFACES; ++i) { + if (!interfaces[i].in_use) { + break; + } /* Skip loopback interfaces */ - if (memcmp("lo", devicename, 2) == 0) { + if (memcmp("lo", interfaces[i].name, 2) == 0) { continue; } - inet_ntop(AF_INET, &(myip.sin_addr), str, INET_ADDRSTRLEN); - /* Initialize receiving socket on the device chosen */ + myip.sin_family = AF_INET; + memcpy((void *)&myip.sin_addr, interfaces[i].ipv4_addr, IPV4_ALEN); myip.sin_port = htons(sourceport); - + /* Initialize socket and bind to udp port */ if ((testsocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { continue; @@ -342,15 +357,16 @@ static int find_interface() { continue; } - /* Find the mac address for the current device */ - if (get_device_mac(testsocket, devicename, srcmac) < 0) { + /* Ensure that we have mac-address for this interface */ + if (memcmp(interfaces[i].mac_addr, emptymac, ETH_ALEN) == 0) { close(testsocket); continue; } - /* Set the global socket handle for send_udp() */ + /* Set the global socket handle and source mac address for send_udp() */ send_socket = testsocket; - device_index = get_device_index(testsocket, devicename); + memcpy(srcmac, interfaces[i].mac_addr, ETH_ALEN); + active_interface = &interfaces[i]; /* Send a SESSIONSTART message with the current device */ init_packet(&data, MT_PTYPE_SESSIONSTART, srcmac, dstmac, sessionkey, 0); @@ -369,8 +385,6 @@ static int find_interface() { close(testsocket); } - - /* We didn't find anything */ return 0; } @@ -466,12 +480,7 @@ int main (int argc, char **argv) { return 1; } - /* Transmit raw packets with this socket */ - sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); - if (sockfd < 0) { - perror("sockfd"); - return 1; - } + sockfd = net_init_raw_socket(); } /* Receive regular udp packets with this socket */ @@ -522,7 +531,7 @@ int main (int argc, char **argv) { /* Set up global info about the connection */ inet_pton(AF_INET, (char *)"255.255.255.255", &destip); - memcpy(&sourceip, &(si_me.sin_addr), 4); + memcpy(&sourceip, &(si_me.sin_addr), IPV4_ALEN); /* Sessioon key */ sessionkey = rand() % 65535; -- cgit v1.2.3 From 632bc61784657ca10da047c38596bde9d336998f Mon Sep 17 00:00:00 2001 From: Håkon Nessjøen Date: Sat, 12 Nov 2011 05:15:36 +0100 Subject: New version. 0.3.3. Moved versioning into config.h --- config.h | 2 ++ macping.c | 1 - mactelnet.c | 1 - mactelnetd.c | 1 - 4 files changed, 2 insertions(+), 3 deletions(-) (limited to 'mactelnet.c') diff --git a/config.h b/config.h index 15fdf4c..c2c1fd4 100644 --- a/config.h +++ b/config.h @@ -21,6 +21,8 @@ #define DEBUG 0 +#define PROGRAM_VERSION "0.3.3" + #if defined(__APPLE__) && defined(__MACH__) #define PLATFORM_NAME "Mac OS X" diff --git a/macping.c b/macping.c index a078540..a276701 100644 --- a/macping.c +++ b/macping.c @@ -35,7 +35,6 @@ #define MT_INTERFACE_LEN 128 #define PROGRAM_NAME "MAC-Ping" -#define PROGRAM_VERSION "0.3.2" static int sockfd, insockfd; diff --git a/mactelnet.c b/mactelnet.c index 0f6334a..c3ed71d 100644 --- a/mactelnet.c +++ b/mactelnet.c @@ -43,7 +43,6 @@ #include "mactelnet.h" #define PROGRAM_NAME "MAC-Telnet" -#define PROGRAM_VERSION "0.3.2" static int sockfd = 0; static int insockfd; diff --git a/mactelnetd.c b/mactelnetd.c index 9e88679..3380115 100644 --- a/mactelnetd.c +++ b/mactelnetd.c @@ -55,7 +55,6 @@ #include "config.h" #define PROGRAM_NAME "MAC-Telnet Daemon" -#define PROGRAM_VERSION "0.3.2" #define MAX_INSOCKETS 100 -- cgit v1.2.3 From 3084b5ec02f72c7c420a5c6687a61df637005ceb Mon Sep 17 00:00:00 2001 From: Håkon Nessjøen Date: Sun, 13 Nov 2011 06:03:15 +0100 Subject: Simplified checking of mac address (code cleanup) --- interfaces.c | 5 ++++- interfaces.h | 1 + macping.c | 3 +-- mactelnet.c | 2 +- mactelnetd.c | 13 +++---------- 5 files changed, 10 insertions(+), 14 deletions(-) (limited to 'mactelnet.c') diff --git a/interfaces.c b/interfaces.c index b37a162..86b9aab 100644 --- a/interfaces.c +++ b/interfaces.c @@ -82,6 +82,7 @@ static void net_update_mac(struct net_interface *interfaces, int max_devices) { if (ioctl(tmpsock, SIOCGIFHWADDR, &ifr) == 0) { /* Fetch mac address */ memcpy(interfaces[i].mac_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN); + interfaces[i].has_mac = 1; } } @@ -136,11 +137,13 @@ int net_get_interfaces(struct net_interface *interfaces, int max_devices) { } #ifndef __linux__ { + unsigned char emptymac[] = {0, 0, 0, 0, 0, 0}; struct sockaddr_dl *sdl = (struct sockaddr_dl *)int_cursor->ifa_addr; if (sdl->sdl_alen == ETH_ALEN) { struct net_interface *interface = net_get_interface_ptr(interfaces, max_devices, int_cursor->ifa_name, 1); - if (interface != NULL) { + if (interface != NULL && memcmp(interfaces->mac_addr, emptymac, ETH_ALEN) != 0) { memcpy(interface->mac_addr, LLADDR(sdl), ETH_ALEN); + interface->has_mac = 1; } } } diff --git a/interfaces.h b/interfaces.h index 8d556f1..afb3e72 100644 --- a/interfaces.h +++ b/interfaces.h @@ -32,6 +32,7 @@ struct net_interface { #ifdef __linux__ int ifindex; #endif + int has_mac; int in_use; }; diff --git a/macping.c b/macping.c index a276701..63ca5eb 100644 --- a/macping.c +++ b/macping.c @@ -223,7 +223,6 @@ int main(int argc, char **argv) { int waitforpacket; struct timeval timestamp; unsigned char pingdata[1500]; - unsigned char emptymac[ETH_ALEN] = {0, 0, 0, 0, 0, 0}; gettimeofday(×tamp, NULL); memcpy(pingdata, ×tamp, sizeof(timestamp)); @@ -238,7 +237,7 @@ int main(int argc, char **argv) { break; } - if (memcmp(emptymac, interface->mac_addr, ETH_ALEN) == 0) { + if (!interface->has_mac) { continue; } diff --git a/mactelnet.c b/mactelnet.c index c3ed71d..ca27f15 100644 --- a/mactelnet.c +++ b/mactelnet.c @@ -357,7 +357,7 @@ static int find_interface() { } /* Ensure that we have mac-address for this interface */ - if (memcmp(interfaces[i].mac_addr, emptymac, ETH_ALEN) == 0) { + if (!interfaces[i].has_mac) { close(testsocket); continue; } diff --git a/mactelnetd.c b/mactelnetd.c index 3380115..3690d38 100644 --- a/mactelnetd.c +++ b/mactelnetd.c @@ -200,18 +200,15 @@ static int find_socket(unsigned char *mac) { } static void setup_sockets() { - unsigned char emptymac[ETH_ALEN]; int i; - bzero(emptymac, ETH_ALEN); - if (net_get_interfaces(interfaces, MAX_INTERFACES) <= 0) { fprintf(stderr, "Error: No suitable devices found\n"); exit(1); } for (i = 0; i < MAX_INTERFACES; ++i) { - if (interfaces[i].in_use == 0 || memcmp(interfaces[i].mac_addr, emptymac, ETH_ALEN) == 0) { + if (interfaces[i].in_use == 0 || !interfaces[i].has_mac) { continue; } @@ -746,7 +743,6 @@ static void print_version() { void mndp_broadcast() { struct mt_packet pdata; struct utsname s_uname; - unsigned char emptymac[ETH_ALEN] = {0, 0, 0, 0, 0, 0}; int i; unsigned int uptime; #ifdef __linux__ @@ -774,7 +770,7 @@ void mndp_broadcast() { struct net_interface *interface = &interfaces[i]; struct mt_mndp_hdr *header = (struct mt_mndp_hdr *)&(pdata.data); - if (interfaces[i].in_use == 0 || memcmp(emptymac, interfaces[i].mac_addr, ETH_ALEN) == 0) { + if (interfaces[i].in_use == 0 || interfaces[i].has_mac == 0) { continue; } @@ -932,10 +928,7 @@ int main (int argc, char **argv) { syslog(LOG_NOTICE, "Bound to %s:%d", inet_ntoa(si_me.sin_addr), sourceport); for (i = 0; i < MAX_INTERFACES; ++i) { - unsigned char emptymac[ETH_ALEN]; - bzero(emptymac, ETH_ALEN); - - if (interfaces[i].in_use && memcmp(interfaces[i].mac_addr, emptymac, ETH_ALEN) != 0) { + if (interfaces[i].in_use && interfaces[i].has_mac) { struct ether_addr *mac = (struct ether_addr *)&(interfaces[i].mac_addr); syslog(LOG_NOTICE, "Listening on %s for %16s\n", interfaces[i].name, ether_ntoa(mac)); interface_count++; -- cgit v1.2.3