diff options
author | Boian Bonev <bbonev@ipacct.com> | 2011-11-14 09:19:56 +0530 |
---|---|---|
committer | Boian Bonev <bbonev@ipacct.com> | 2011-11-14 09:19:56 +0530 |
commit | 060569c084f072cd6dd0509e8c30d78d2d2d2b84 (patch) | |
tree | b649ed1012d8a9195c1349ab19252224764acf99 /macping.c | |
parent | 8e00efedad165bfed2856a41cbf492c91e0ddce0 (diff) | |
parent | a5b710a45671c1b9504130d83d3ebe7fe8a65c5a (diff) | |
download | MAC-Telnet-060569c084f072cd6dd0509e8c30d78d2d2d2b84.tar.gz MAC-Telnet-060569c084f072cd6dd0509e8c30d78d2d2d2b84.zip |
merge
Diffstat (limited to 'macping.c')
-rw-r--r-- | macping.c | 70 |
1 files changed, 24 insertions, 46 deletions
@@ -28,30 +28,23 @@ #include <stdio.h> #include <float.h> #include "protocol.h" -#include "udp.h" -#include "devices.h" +#include "interfaces.h" #include "config.h" #define MAX_DEVICES 128 #define MT_INTERFACE_LEN 128 #define PROGRAM_NAME "MAC-Ping" -#define PROGRAM_VERSION "0.3.2" 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 +59,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; @@ -193,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; @@ -204,12 +178,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 +208,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); @@ -264,12 +234,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 (!interface->has_mac) { + 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++; |