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. --- macping.c | 66 ++++++++++++++++++++------------------------------------------- 1 file changed, 21 insertions(+), 45 deletions(-) (limited to 'macping.c') diff --git a/macping.c b/macping.c index 6b169d3..a078540 100644 --- a/macping.c +++ b/macping.c @@ -28,8 +28,7 @@ #include #include #include "protocol.h" -#include "udp.h" -#include "devices.h" +#include "interfaces.h" #include "config.h" #define MAX_DEVICES 128 @@ -40,18 +39,13 @@ static int sockfd, insockfd; -struct mt_device { - unsigned char mac[ETH_ALEN]; - char name[MT_INTERFACE_LEN]; - int device_index; -}; - static unsigned short ping_size = 38; -static struct mt_device devices[MAX_DEVICES]; -static int devices_count = 0; + +struct net_interface interfaces[MAX_INTERFACES]; + static struct in_addr sourceip; static struct in_addr destip; -static unsigned char dstmac[6]; +static unsigned char dstmac[ETH_ALEN]; static int ping_sent = 0; static int pong_received = 0; @@ -66,29 +60,6 @@ static void print_version() { fprintf(stderr, PROGRAM_NAME " " PROGRAM_VERSION "\n"); } -static void setup_devices() { - char devicename[MT_INTERFACE_LEN]; - unsigned char mac[ETH_ALEN]; - unsigned char emptymac[ETH_ALEN]; - int success; - - memset(emptymac, 0, ETH_ALEN); - - while ((success = get_macs(insockfd, devicename, MT_INTERFACE_LEN, mac))) { - if (memcmp(mac, emptymac, ETH_ALEN) != 0) { - struct mt_device *device = &(devices[devices_count]); - - memcpy(device->mac, mac, ETH_ALEN); - strncpy(device->name, devicename, MT_INTERFACE_LEN - 1); - device->name[MT_INTERFACE_LEN - 1] = '\0'; - - device->device_index = get_device_index(insockfd, devicename); - - devices_count++; - } - } -} - static long long int toddiff(struct timeval *tod1, struct timeval *tod2) { long long t1, t2; @@ -204,12 +175,7 @@ int main(int argc, char **argv) { return 1; } - /* Open a UDP socket handle */ - sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); - if (sockfd < 0) { - perror("sockfd"); - return 1; - } + sockfd = net_init_raw_socket(); insockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (insockfd < 0) { @@ -239,7 +205,8 @@ int main(int argc, char **argv) { srand(time(NULL)); - setup_devices(); + /* Enumerate available interfaces */ + net_get_interfaces(interfaces, MAX_INTERFACES); if (ping_size < sizeof(struct timeval)) { ping_size = sizeof(struct timeval); @@ -257,6 +224,7 @@ 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)); @@ -264,12 +232,20 @@ int main(int argc, char **argv) { pingdata[ii] = rand() % 256; } - for (ii = 0; ii < devices_count; ++ii) { - struct mt_device *device = &devices[ii]; + for (ii = 0; ii < MAX_INTERFACES; ++ii) { + struct net_interface *interface = &interfaces[ii]; + + if (!interface->in_use) { + break; + } + + if (memcmp(emptymac, interface->mac_addr, ETH_ALEN) == 0) { + continue; + } - init_pingpacket(&packet, device->mac, dstmac); + init_pingpacket(&packet, interface->mac_addr, dstmac); add_packetdata(&packet, pingdata, ping_size); - result = send_custom_udp(sockfd, device->device_index, device->mac, dstmac, &sourceip, MT_MACTELNET_PORT, &destip, MT_MACTELNET_PORT, packet.data, packet.size); + result = net_send_udp(sockfd, interface, interface->mac_addr, dstmac, &sourceip, MT_MACTELNET_PORT, &destip, MT_MACTELNET_PORT, packet.data, packet.size); if (result > 0) { sent++; -- 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 'macping.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 'macping.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 From 62fbf67c2a27b10e6ff534c7795a96e38d259e55 Mon Sep 17 00:00:00 2001 From: Håkon Nessjøen Date: Sun, 13 Nov 2011 06:40:22 +0100 Subject: Added comment to remind myself not from writing "non-root" version of macping any more times --- macping.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'macping.c') diff --git a/macping.c b/macping.c index 63ca5eb..2e90bc7 100644 --- a/macping.c +++ b/macping.c @@ -163,6 +163,10 @@ int main(int argc, char **argv) { exit(1); } + /* Mikrotik RouterOS does not answer unless the packet has the correct recipient mac-address in + * the ethernet frame. Unlike real MacTelnet connections where the OS is ok with it being a + * broadcast mac address. + */ if (geteuid() != 0) { fprintf(stderr, "You need to have root privileges to use %s.\n", argv[0]); return 1; -- cgit v1.2.3