diff options
-rw-r--r-- | devices.c | 72 | ||||
-rw-r--r-- | mactelnet.c | 27 | ||||
-rw-r--r-- | mactelnetd.c | 16 | ||||
-rw-r--r-- | mndp.c | 5 | ||||
-rw-r--r-- | protocol.c | 7 | ||||
-rw-r--r-- | udp.c | 4 |
6 files changed, 106 insertions, 25 deletions
@@ -19,13 +19,64 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <malloc.h> #include <unistd.h> -#include <netinet/in.h> -#include <linux/if_ether.h> + +#include <sys/types.h> +#include <sys/socket.h> +#include <ifaddrs.h> + +#ifdef __APPLE_CC__ +#include <net/if_dl.h> +#else +#include <malloc.h> +#include <ifaddrs.h> #include <sys/ioctl.h> #include <net/if.h> +#include <netpacket/packet.h> +#include <netinet/in.h> +#endif + +#ifndef IFT_ETHER +#define IFT_ETHER 0x6 /* Ethernet CSMACD */ +#endif + +int getDeviceMAC(const int sockfd, const unsigned char *deviceName, unsigned char *mac) { + struct ifaddrs *addrs; + const struct ifaddrs *cursor; +#ifdef __APPLE_CC__ + const struct sockaddr_dl *dlAddr; +#else + const struct sockaddr_ll *dlAddr; +#endif + + if (getifaddrs(&addrs) == 0) { + cursor = addrs; + while (cursor != NULL) { +#ifdef __APPLE_CC__ + dlAddr = (const struct sockaddr_dl *) cursor->ifa_addr; + if ( (cursor->ifa_addr->sa_family == AF_LINK) && (dlAddr->sdl_type == IFT_ETHER) ) { + if (strcmp(cursor->ifa_name, deviceName) == 0) { + memcpy(mac, dlAddr->sdl_data + dlAddr->sdl_nlen, 6); + return 1; + } + } +#else + dlAddr = (const struct sockaddr_ll *) cursor->ifa_addr; + if (cursor->ifa_addr->sa_family == PF_PACKET) { + if (strcmp(cursor->ifa_name, deviceName) == 0) { + memcpy(mac, dlAddr->sll_addr, 6); + return 1; + } + } +#endif + cursor = cursor->ifa_next; + } + freeifaddrs(addrs); + } + return -1; +} +#ifndef __APPLE_CC__ /* Functions using NETDEVICE api */ int getDeviceIndex(int sockfd, unsigned char *deviceName) { @@ -41,20 +92,6 @@ int getDeviceIndex(int sockfd, unsigned char *deviceName) { return ifr.ifr_ifindex; } -int getDeviceMAC(const int sockfd, const unsigned char *deviceName, unsigned char *mac) { - struct ifreq ifr; - - /* Find interface hardware address from deviceName */ - strncpy(ifr.ifr_name, deviceName, 16); - if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) != 0) { - return -1; - } - - /* Fetch mac address */ - memcpy(mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN); - return 1; -} - int getDeviceIp(const int sockfd, const unsigned char *deviceName, struct sockaddr_in *ip) { struct ifconf ifc; struct ifreq *ifr; @@ -98,3 +135,4 @@ int getDeviceIp(const int sockfd, const unsigned char *deviceName, struct sockad free(ifr); return -1; } +#endif diff --git a/mactelnet.c b/mactelnet.c index 652023b..d61ea05 100644 --- a/mactelnet.c +++ b/mactelnet.c @@ -23,14 +23,19 @@ #include <fcntl.h> #include <signal.h> #include <arpa/inet.h> -#include <netinet/in.h> -#include <netinet/ether.h> #include <sys/time.h> #include <time.h> #include <sys/types.h> #include <sys/socket.h> #include <string.h> +#ifndef __APPLE_CC__ #include <linux/if_ether.h> +#include <netinet/in.h> +#include <netinet/ether.h> +#else +#define ETH_ALEN 6 +#include <net/ethernet.h> +#endif #include "md5.h" #include "protocol.h" #include "udp.h" @@ -74,7 +79,9 @@ int sendUDP(struct mt_packet *packet) { return sendto(insockfd, packet->data, packet->size, 0, (struct sockaddr*)&socket_address, sizeof(socket_address)); } else { +#ifndef __APPLE_CC__ return sendCustomUDP(sockfd, deviceIndex, srcmac, dstmac, &sourceip, sourceport, &destip, MT_MACTELNET_PORT, packet->data, packet->size); +#endif } } @@ -238,6 +245,7 @@ void handlePacket(unsigned char *data, int data_len) { */ int main (int argc, char **argv) { int result; + struct ether_addr *tmpaddr; struct mt_packet data; struct sockaddr_in si_me; unsigned char buff[1500]; @@ -290,7 +298,9 @@ int main (int argc, char **argv) { fprintf(stderr, " ifname Network interface that the RouterOS resides on. (example: eth0)\n"); fprintf(stderr, " MAC MAC-Address of the RouterOS device. Use mndp to discover them.\n"); fprintf(stderr, " identity The identity/name of your RouterOS device. Uses MNDP protocol to find it.\n"); +#ifndef __APPLE_CC__ fprintf(stderr, " -n Do not use broadcast packets. Less insecure but requires root privileges.\n"); +#endif fprintf(stderr, " -u Specify username on command line.\n"); fprintf(stderr, " -p Specify password on command line.\n"); fprintf(stderr, " -h This help.\n"); @@ -311,6 +321,7 @@ int main (int argc, char **argv) { return 1; } +#ifndef __APPLE_CC__ if (!broadcastMode) { /* Transmit raw packets with this socket */ sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); @@ -319,6 +330,7 @@ int main (int argc, char **argv) { return 1; } } +#endif /* Receive regular udp packets with this socket */ insockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); @@ -335,6 +347,7 @@ int main (int argc, char **argv) { } } +#ifndef __APPLE_CC__ /* Find device index number for specified interface */ deviceIndex = getDeviceIndex(insockfd, devicename); if (deviceIndex < 0) { @@ -351,6 +364,7 @@ int main (int argc, char **argv) { fprintf(stderr, "Cannot determine IP of device %s\n", devicename); return 1; } +#endif /* Determine source mac address */ result = getDeviceMAC(insockfd, devicename, srcmac); @@ -383,7 +397,12 @@ int main (int argc, char **argv) { } else { /* Convert mac address string to ether_addr struct */ - ether_aton_r(argv[optind], (struct ether_addr *)dstmac); + tmpaddr = ether_aton(argv[optind]); + if (tmpaddr == NULL) { + fprintf(stderr, "Invalid MAC address\n"); + exit(1); + } + memcpy(dstmac, tmpaddr, sizeof(struct ether_addr)); } } @@ -400,7 +419,7 @@ int main (int argc, char **argv) { password[sizeof(password) - 1] = '\0'; /* security */ memset(tmp, 0, strlen(tmp)); -#ifdef __GNUC__ +#ifdef __gnu_linux__ free(tmp); #endif } diff --git a/mactelnetd.c b/mactelnetd.c index 77ac000..1ed68ca 100644 --- a/mactelnetd.c +++ b/mactelnetd.c @@ -24,14 +24,19 @@ #include <fcntl.h> #include <signal.h> #include <arpa/inet.h> -#include <netinet/in.h> -#include <netinet/ether.h> #include <sys/time.h> #include <time.h> #include <sys/types.h> #include <sys/socket.h> #include <string.h> +#ifndef __APPLE_CC__ #include <linux/if_ether.h> +#include <netinet/in.h> +#include <netinet/ether.h> +#else +#define ETH_ALEN 6 +#endif +#include <pwd.h> #include <sys/ioctl.h> #include <pwd.h> #include "md5.h" @@ -149,7 +154,9 @@ struct mt_connection *findConnection(unsigned short seskey, unsigned char *srcma } int sendUDP(const struct mt_connection *conn, const struct mt_packet *data) { +#ifndef __APPLE_CC__ return sendCustomUDP(sockfd, 2, conn->dstmac, conn->srcmac, &sourceip, sourceport, &destip, conn->srcport, data->data, data->size); +#endif } void handlePacket(unsigned char *data, int data_len, const struct sockaddr_in *address) { @@ -429,6 +436,10 @@ int main (int argc, char **argv) { struct mt_packet pdata; fd_set read_fds; +#ifdef __APPLE_CC__ + fprintf(stderr, "No support for mactelnetd in OS X yet.\n"); + exit(1); +#else /* Try to read user file */ readUserfile(); @@ -561,6 +572,7 @@ int main (int argc, char **argv) { close(sockfd); close(insockfd); +#endif return 0; } @@ -19,7 +19,12 @@ #include <stdlib.h> #include <stdio.h> #include <arpa/inet.h> +#ifndef __APPLE_CC__ #include <netinet/ether.h> +#else +#define ETH_ALEN 6 +#include <net/ethernet.h> +#endif #include <string.h> #include "protocol.h" #include "config.h" @@ -19,10 +19,15 @@ #include <string.h> #include <stdio.h> #include <stdlib.h> +#ifndef __APPLE_CC__ #include <linux/if_ether.h> -#include <arpa/inet.h> #include <netinet/in.h> #include <netinet/ether.h> +#else +#define ETH_ALEN 6 +#include <net/ethernet.h> +#endif +#include <arpa/inet.h> #include "protocol.h" #include "config.h" @@ -1,7 +1,8 @@ -#include <malloc.h> +#ifndef __APPLE_CC__ #include <string.h> #include <math.h> #include <sys/socket.h> +#include <malloc.h> #include <linux/if_packet.h> #include <linux/if_ether.h> #include <linux/ip.h> @@ -104,3 +105,4 @@ int sendCustomUDP(const int socket, const int ifindex, const unsigned char *sour /* Return amount of _data_ bytes sent */ return send_result-8-14-20; } +#endif |