summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHaakon Nessjoen <haakon.nessjoen@gmail.com>2010-10-18 16:37:58 +0200
committerHaakon Nessjoen <haakon.nessjoen@gmail.com>2010-10-18 16:37:58 +0200
commitaccffc10a27d2dbe5c9c35ab4179221b612dbcff (patch)
tree75279efc09183c69914a0afd5e14b882e09d09dd
parent94113dbaec206b4c15b5b03871ae3642efaf0290 (diff)
downloadMAC-Telnet-accffc10a27d2dbe5c9c35ab4179221b612dbcff.tar.gz
MAC-Telnet-accffc10a27d2dbe5c9c35ab4179221b612dbcff.zip
Client support for OS X
-rw-r--r--devices.c40
-rw-r--r--mactelnet.c27
-rw-r--r--mactelnetd.c13
-rw-r--r--mndp.c5
-rw-r--r--protocol.c5
-rw-r--r--udp.c4
6 files changed, 87 insertions, 7 deletions
diff --git a/devices.c b/devices.c
index c380ead..899411c 100644
--- a/devices.c
+++ b/devices.c
@@ -19,8 +19,45 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <malloc.h>
#include <unistd.h>
+#ifdef __APPLE_CC__
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <ifaddrs.h>
+
+#include <net/if_dl.h>
+
+
+
+#if ! defined(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;
+ const struct sockaddr_dl * dlAddr;
+
+ if (getifaddrs(&addrs) == 0) {
+ cursor = addrs;
+ while (cursor != NULL) {
+ 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;
+ }
+ }
+ cursor = cursor->ifa_next;
+ }
+ freeifaddrs(addrs);
+ }
+ return -1;
+}
+
+#else
+#include <malloc.h>
#include <netinet/in.h>
#include <linux/if_ether.h>
#include <sys/ioctl.h>
@@ -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 935ca52..637b9f8 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 <openssl/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];
@@ -289,7 +297,9 @@ int main (int argc, char **argv) {
fprintf(stderr, "\nParameters:\n");
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");
+#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");
@@ -303,7 +313,12 @@ int main (int argc, char **argv) {
devicename[sizeof(devicename) - 1] = '\0';
/* 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));
/* Seed randomizer */
srand(time(NULL));
@@ -313,6 +328,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));
@@ -321,6 +337,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);
@@ -337,6 +354,7 @@ int main (int argc, char **argv) {
}
}
+#ifndef __APPLE_CC__
/* Find device index number for specified interface */
deviceIndex = getDeviceIndex(insockfd, devicename);
if (deviceIndex < 0) {
@@ -353,6 +371,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);
@@ -375,8 +394,10 @@ int main (int argc, char **argv) {
/* security */
memset(tmp, 0, strlen(tmp));
#ifdef __GNUC__
+#ifndef __APPLE_CC__
free(tmp);
#endif
+#endif
}
diff --git a/mactelnetd.c b/mactelnetd.c
index 1689342..afe5a94 100644
--- a/mactelnetd.c
+++ b/mactelnetd.c
@@ -24,14 +24,16 @@
#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>
+#endif
#include <openssl/md5.h>
#include <pwd.h>
#include <sys/ioctl.h>
@@ -147,7 +149,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) {
@@ -412,6 +416,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();
@@ -540,6 +548,7 @@ int main (int argc, char **argv) {
close(sockfd);
close(insockfd);
+#endif
return 0;
}
diff --git a/mndp.c b/mndp.c
index 006985c..4f2b88a 100644
--- a/mndp.c
+++ b/mndp.c
@@ -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"
diff --git a/protocol.c b/protocol.c
index a1cbf8e..5606365 100644
--- a/protocol.c
+++ b/protocol.c
@@ -19,7 +19,12 @@
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
+#ifndef __APPLE_CC__
#include <linux/if_ether.h>
+#else
+#define ETH_ALEN 6
+#include <net/ethernet.h>
+#endif
#include "protocol.h"
#include "config.h"
diff --git a/udp.c b/udp.c
index 4c1a383..e8b2a3d 100644
--- a/udp.c
+++ b/udp.c
@@ -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