From ac5b0eadef805d968bb922e0cf1b635da3e80972 Mon Sep 17 00:00:00 2001 From: Håkon Nessjøen Date: Wed, 16 Feb 2011 03:54:49 +0100 Subject: mactelnetd only listened on interfaces with an ip on it. Rewrote mactelnetd to use all IFs it finds MAC addresses on, not only the ones with IP. Testing version, to be cleaned up. --- Makefile | 2 +- devices.c | 38 +++++++++++++++++++++++++++++++++++++- devices.h | 1 + mactelnetd.c | 29 ++++++++++++++--------------- 4 files changed, 53 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index fe27d84..92849ad 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ install: all mactelnet: config.h udp.h udp.c mactelnet.c mactelnet.h protocol.c protocol.h console.c console.h devices.c devices.h md5.c md5.h gcc -Wall -g -DUSERSFILE='"/etc/mactelnetd.users"' -o mactelnet mactelnet.c udp.c protocol.c console.c devices.c md5.c -mactelnetd: config.h mactelnetd.c udp.h udp.c protocol.c protocol.h console.c console.h users.c users.h md5.c md5.h +mactelnetd: config.h mactelnetd.c udp.h udp.c protocol.c protocol.h devices.c devices.h console.c console.h users.c users.h md5.c md5.h gcc -Wall -g -DUSERSFILE='"/etc/mactelnetd.users"' -o mactelnetd mactelnetd.c udp.c protocol.c console.c devices.c users.c md5.c mndp: config.h mndp.c protocol.c protocol.h diff --git a/devices.c b/devices.c index 57d134a..6e1f7f7 100644 --- a/devices.c +++ b/devices.c @@ -97,7 +97,7 @@ int get_device_ip(const int sockfd, const char *device_name, struct sockaddr_in for (i = 0; i < device_count; ++i) { if (strcmp(ifr[i].ifr_name, device_name) == 0) { /* Fetch IP for found interface */ - memcpy(ip, &(ifr[i].ifr_addr), sizeof(ip)); + memcpy(ip, &(ifr[i].ifr_addr), sizeof(struct sockaddr_in)); free(ifr); return 1; } @@ -106,6 +106,42 @@ int get_device_ip(const int sockfd, const char *device_name, struct sockaddr_in return -1; } +int get_macs(int sockfd, char *name, int name_len, unsigned char *mac) { + static int first = 1; + static struct ifaddrs *int_addrs; + static const struct ifaddrs *int_cursor; + const struct sockaddr_in *dl_addr; + + if (first == 1) { + first = 0; + if (getifaddrs(&int_addrs) == 0) { + int_cursor = int_addrs; + } else { + first = 1; + return 0; + } + } + if (int_cursor != NULL) { + while (int_cursor != NULL) { + dl_addr = (const struct sockaddr_in *) int_cursor->ifa_addr; + if (dl_addr != NULL && dl_addr->sin_family == AF_PACKET) { + strncpy(name, int_cursor->ifa_name, name_len - 1); + name[name_len - 1] = '\0'; + int_cursor = int_cursor->ifa_next; + if (get_device_mac(sockfd, name, mac)) { + return 1; + } + } + int_cursor = int_cursor->ifa_next; + } + } + if (int_addrs != NULL) { + freeifaddrs(int_addrs); + int_addrs = NULL; + } + return 0; +} + int get_ips(char *name, int name_len, struct sockaddr_in *ip) { static int first = 1; static struct ifaddrs *int_addrs; diff --git a/devices.h b/devices.h index dd8abe5..821db3c 100644 --- a/devices.h +++ b/devices.h @@ -2,3 +2,4 @@ extern int get_device_index(int sockfd, char *deviceName); extern int get_device_mac(const int sockfd, const char *deviceName, unsigned char *mac); extern int get_device_ip(const int sockfd, const char *deviceName, struct sockaddr_in *ip); int get_ips(char *name, int nameLen, struct sockaddr_in *ip); +int get_macs(int sockfd, char *name, int name_len, unsigned char *mac); diff --git a/mactelnetd.c b/mactelnetd.c index e5409a8..cded857 100644 --- a/mactelnetd.c +++ b/mactelnetd.c @@ -199,17 +199,17 @@ static void setup_sockets() { memset(emptymac, 0, ETH_ALEN); - while ((success = get_ips(devicename, MT_INTERFACE_LEN, &myip))) { - if (get_device_mac(insockfd, devicename, mac)) { - if (memcmp(mac, emptymac, ETH_ALEN) != 0 && find_socket(mac) < 0) { - int optval = 1; - struct sockaddr_in si_me; - struct mt_socket *mysocket = &(sockets[sockets_count]); + while ((success = get_macs(insockfd, devicename, MT_INTERFACE_LEN, mac))) { + if (memcmp(mac, emptymac, ETH_ALEN) != 0 && find_socket(mac) < 0) { + int optval = 1; + struct sockaddr_in si_me; + struct mt_socket *mysocket = &(sockets[sockets_count]); - memcpy(mysocket->mac, mac, ETH_ALEN); - strncpy(mysocket->name, devicename, MT_INTERFACE_LEN - 1); - mysocket->name[MT_INTERFACE_LEN - 1] = '\0'; + memcpy(mysocket->mac, mac, ETH_ALEN); + strncpy(mysocket->name, devicename, MT_INTERFACE_LEN - 1); + mysocket->name[MT_INTERFACE_LEN - 1] = '\0'; + if (use_raw_socket == 0 && get_device_ip(insockfd, devicename, &myip) > 0) { mysocket->sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (mysocket->sockfd < 0) { @@ -234,12 +234,11 @@ static void setup_sockets() { continue; } memcpy(mysocket->ip, &(myip.sin_addr), 4); - memcpy(mysocket->mac, mac, ETH_ALEN); - - mysocket->device_index = get_device_index(mysocket->sockfd, devicename); - - sockets_count++; } + mysocket->device_index = get_device_index(insockfd, devicename); + printf("dif=%d = %s\n", mysocket->device_index, devicename); + + sockets_count++; } } } @@ -808,7 +807,7 @@ int main (int argc, char **argv) { for (i = 0; i < sockets_count; ++i) { struct mt_socket *socket = &(sockets[i]); - syslog(LOG_NOTICE, "Listening on %s: %16s port %d\n", socket->name, ether_ntoa((struct ether_addr *)socket->mac), MT_MACTELNET_PORT); + syslog(LOG_NOTICE, "Listening on %s for %16s\n", socket->name, ether_ntoa((struct ether_addr *)socket->mac)); } if (sockets_count == 0) { -- cgit v1.2.3