summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrock Williams <brock@cotcomsol.com>2014-02-03 11:11:13 -0700
committerBrock Williams <brock@cotcomsol.com>2014-02-03 11:11:13 -0700
commit51edd4405b99cf102f9f5e02fc4a51a22487a3dc (patch)
tree904e52547f095d2508412cb317db478b4ed3972d
parent888b912c0c917e27bfa6fabbb1c00e7e34d2d94d (diff)
downloadMAC-Telnet-51edd4405b99cf102f9f5e02fc4a51a22487a3dc.tar.gz
MAC-Telnet-51edd4405b99cf102f9f5e02fc4a51a22487a3dc.zip
port to freebsd
-rw-r--r--interfaces.c107
-rw-r--r--macping.c9
-rw-r--r--mactelnet.c8
-rw-r--r--mactelnetd.c52
-rw-r--r--mndp.c9
-rw-r--r--protocol.c56
-rw-r--r--protocol.h23
7 files changed, 222 insertions, 42 deletions
diff --git a/interfaces.c b/interfaces.c
index 0ed2d8e..0874f90 100644
--- a/interfaces.c
+++ b/interfaces.c
@@ -16,6 +16,10 @@
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
+#if defined(__FreeBSD__)
+#define __USE_BSD
+#define __FAVOR_BSD
+#endif
#include <libintl.h>
#include <locale.h>
#include <stdio.h>
@@ -25,9 +29,18 @@
#include <sys/socket.h>
#include <sys/types.h>
#include <ifaddrs.h>
+#if defined(__FreeBSD__)
+#include <netinet/in.h>
+#endif
#include <netinet/ip.h>
#include <netinet/udp.h>
+#if defined(__FreeBSD__)
+#include <net/ethernet.h>
+#define ETH_FRAME_LEN (ETHER_MAX_LEN - ETHER_CRC_LEN)
+#define ETH_ALEN ETHER_ADDR_LEN
+#else
#include <netinet/ether.h>
+#endif
#include <arpa/inet.h>
#include <net/if.h>
#include <sys/ioctl.h>
@@ -78,9 +91,9 @@ static void net_update_mac(struct net_interface *interfaces, int max_devices) {
perror("net_update_mac");
exit(1);
}
-
for (i = 0; i < max_devices; ++i) {
- if (interfaces[i].in_use) {
+ if (!interfaces[i].in_use)
+ continue;
/* Find interface hardware address from device_name */
strncpy(ifr.ifr_name, interfaces[i].name, 16);
if (ioctl(tmpsock, SIOCGIFHWADDR, &ifr) == 0) {
@@ -90,8 +103,6 @@ static void net_update_mac(struct net_interface *interfaces, int max_devices) {
interfaces[i].has_mac = 1;
}
}
-
- }
}
close(tmpsock);
}
@@ -115,7 +126,7 @@ static int get_device_index(char *device_name) {
int net_get_interfaces(struct net_interface *interfaces, int max_devices) {
static struct ifaddrs *int_addrs;
- static const struct ifaddrs *int_cursor;
+ static const struct ifaddrs *ifaddrsp;
const struct sockaddr_in *dl_addr;
int found = 0;
@@ -124,14 +135,16 @@ int net_get_interfaces(struct net_interface *interfaces, int max_devices) {
exit(1);
}
- for (int_cursor = int_addrs; int_cursor != NULL; int_cursor = int_cursor->ifa_next) {
- dl_addr = (const struct sockaddr_in *) int_cursor->ifa_addr;
+ for (ifaddrsp = int_addrs; ifaddrsp; ifaddrsp = ifaddrsp->ifa_next) {
+ dl_addr = (const struct sockaddr_in *) ifaddrsp->ifa_addr;
- if (int_cursor->ifa_addr == NULL)
+ if (ifaddrsp->ifa_addr == NULL)
continue;
- if (int_cursor->ifa_addr->sa_family == AF_INET) {
- struct net_interface *interface = net_get_interface_ptr(interfaces, max_devices, int_cursor->ifa_name, 1);
+ if (ifaddrsp->ifa_addr->sa_family == AF_INET) {
+ struct net_interface *interface =
+ net_get_interface_ptr(interfaces, max_devices,
+ ifaddrsp->ifa_name, 1);
if (interface != NULL) {
found++;
memcpy(interface->ipv4_addr, &dl_addr->sin_addr, IPV4_ALEN);
@@ -143,11 +156,15 @@ 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;
+ struct sockaddr_dl *sdl = (struct sockaddr_dl *)ifaddrsp->ifa_addr;
+
if (sdl->sdl_alen == ETH_ALEN) {
- struct net_interface *interface = net_get_interface_ptr(interfaces, max_devices, int_cursor->ifa_name, 1);
+ struct net_interface *interface =
+ net_get_interface_ptr(interfaces, max_devices,
+ ifaddrsp->ifa_name, 1);
memcpy(interface->mac_addr, LLADDR(sdl), ETH_ALEN);
- if (interface != NULL && memcmp(interface->mac_addr, &emptymac, ETH_ALEN) != 0) {
+ if (interface != NULL &&
+ memcmp(interface->mac_addr, &emptymac, ETH_ALEN) != 0) {
interface->has_mac = 1;
}
}
@@ -162,13 +179,16 @@ int net_get_interfaces(struct net_interface *interfaces, int max_devices) {
#if 0
{
- int i = 0;
+ int i;
for (i = 0; i < max_devices; ++i) {
if (interfaces[i].in_use) {
- struct in_addr *addr = (struct in_addr *)interfaces[i].ipv4_addr;
+ struct in_addr *addr =
+ (struct in_addr *)interfaces[i].ipv4_addr;
+
printf("Interface %s:\n", interfaces[i].name);
printf("\tIP: %s\n", inet_ntoa(*addr));
- printf("\tMAC: %s\n", ether_ntoa((struct ether_addr *)interfaces[i].mac_addr));
+ printf("\tMAC: %s\n",
+ ether_ntoa((struct ether_addr *)interfaces[i].mac_addr));
#ifdef __linux__
printf("\tIfIndex: %d\n", interfaces[i].ifindex);
#endif
@@ -278,10 +298,16 @@ int net_send_udp(const int fd, struct net_interface *interface, const unsigned c
* and align header pointers to the correct positions.
*/
void* buffer = (void*)malloc(ETH_FRAME_LEN);
+#if defined(__FreeBSD__)
+ struct ether_header *eh = (struct ether_header *)buffer;
+ struct ip *ip = (struct ip *)(buffer + 14);
+#else
struct ethhdr *eh = (struct ethhdr *)buffer;
struct iphdr *ip = (struct iphdr *)(buffer + 14);
+#endif
struct udphdr *udp = (struct udphdr *)(buffer + 14 + 20);
- unsigned char *rest = (unsigned char *)(buffer + 20 + 14 + sizeof(struct udphdr));
+ unsigned char *rest =
+ (unsigned char *)(buffer + 20 + 14 + sizeof(struct udphdr));
if (((void *)rest - (void*)buffer) + datalen > ETH_FRAME_LEN) {
fprintf(stderr, _("packet size too large\n"));
@@ -299,9 +325,15 @@ int net_send_udp(const int fd, struct net_interface *interface, const unsigned c
}
/* Init ethernet header */
+#if defined(__FreeBSD__)
+ memcpy(eh->ether_shost, sourcemac, ETH_ALEN);
+ memcpy(eh->ether_dhost, destmac, ETH_ALEN);
+ eh->ether_type = htons(ETHERTYPE_IP);
+#else
memcpy(eh->h_source, sourcemac, ETH_ALEN);
memcpy(eh->h_dest, destmac, ETH_ALEN);
eh->h_proto = htons(ETH_P_IP);
+#endif
#ifdef __linux__
/* Init SendTo struct */
@@ -318,6 +350,19 @@ int net_send_udp(const int fd, struct net_interface *interface, const unsigned c
#endif
/* Init IP Header */
+#if defined(__FreeBSD__)
+ ip->ip_v = 4;
+ ip->ip_hl = 5;
+ ip->ip_tos = 0x10;
+ ip->ip_len = htons(datalen + 8 + 20);
+ ip->ip_id = htons(id++);
+ ip->ip_off = htons(0x4000);
+ ip->ip_ttl = 64;
+ ip->ip_p = 17; /* UDP */
+ ip->ip_sum = 0;
+ ip->ip_src.s_addr = sourceip->s_addr;
+ ip->ip_dst.s_addr = destip->s_addr;
+#else
ip->version = 4;
ip->ihl = 5;
ip->tos = 0x10;
@@ -329,26 +374,50 @@ int net_send_udp(const int fd, struct net_interface *interface, const unsigned c
ip->check = 0x0000;
ip->saddr = sourceip->s_addr;
ip->daddr = destip->s_addr;
+#endif
/* Calculate checksum for IP header */
+#if defined(__FreeBSD__)
+ ip->ip_sum = in_cksum((unsigned short *)ip, sizeof(struct ip));
+#else
ip->check = in_cksum((unsigned short *)ip, sizeof(struct iphdr));
+#endif
/* Init UDP Header */
+#if defined(__FreeBSD__)
+ udp->uh_sport = htons(sourceport);
+ udp->uh_dport = htons(destport);
+ udp->uh_ulen = htons(sizeof(struct udphdr) + datalen);
+ udp->uh_sum = 0;
+#else
udp->source = htons(sourceport);
udp->dest = htons(destport);
udp->len = htons(sizeof(struct udphdr) + datalen);
udp->check = 0;
+#endif
/* Insert actual data */
memcpy(rest, data, datalen);
/* Add UDP checksum */
- udp->check = udp_sum_calc((unsigned char *)&(ip->saddr), (unsigned char *)&(ip->daddr), (unsigned char *)udp, sizeof(struct udphdr) + datalen);
+#if defined(__FreeBSD__)
+ udp->uh_sum = udp_sum_calc((unsigned char *)&(ip->ip_src.s_addr),
+ (unsigned char *)&(ip->ip_dst.s_addr),
+ (unsigned char *)udp,
+ sizeof(struct udphdr) + datalen);
+ udp->uh_sum = htons(udp->uh_sum);
+#else
+ udp->check = udp_sum_calc((unsigned char *)&(ip->saddr),
+ (unsigned char *)&(ip->daddr),
+ (unsigned char *)udp,
+ sizeof(struct udphdr) + datalen);
udp->check = htons(udp->check);
+#endif
#ifdef __linux__
/* Send the packet */
- send_result = sendto(fd, buffer, datalen + 8 + 14 + 20, 0, (struct sockaddr*)&socket_address, sizeof(socket_address));
+ send_result = sendto(fd, buffer, datalen + 8 + 14 + 20, 0,
+ (struct sockaddr*)&socket_address, sizeof(socket_address));
if (send_result == -1)
perror("sendto");
#else
diff --git a/macping.c b/macping.c
index 7958055..ed111a3 100644
--- a/macping.c
+++ b/macping.c
@@ -22,7 +22,16 @@
#include <signal.h>
#include <stdio.h>
#include <arpa/inet.h>
+#if defined(__FreeBSD__)
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <net/ethernet.h>
+#include <netinet/in.h>
+#define ETH_FRAME_LEN ETHER_MAX_LEN
+#define ETH_ALEN ETHER_ADDR_LEN
+#else
#include <netinet/ether.h>
+#endif
#include <string.h>
#include <time.h>
#include <sys/time.h>
diff --git a/mactelnet.c b/mactelnet.c
index 4eebc5f..2661475 100644
--- a/mactelnet.c
+++ b/mactelnet.c
@@ -26,10 +26,16 @@
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
+#if defined(__FreeBSD__)
+#include <sys/endian.h>
+#include <sys/types.h>
+#include <net/ethernet.h>
+#else
#include <endian.h>
+#include <netinet/ether.h>
+#endif
#include <arpa/inet.h>
#include <netinet/in.h>
-#include <netinet/ether.h>
#include <sys/time.h>
#include <time.h>
#include <sys/types.h>
diff --git a/mactelnetd.c b/mactelnetd.c
index 633fb64..5bb1e9d 100644
--- a/mactelnetd.c
+++ b/mactelnetd.c
@@ -16,7 +16,9 @@
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
+#if defined(__linux__)
#define _XOPEN_SOURCE 600
+#endif
#define _BSD_SOURCE
#include <libintl.h>
#include <locale.h>
@@ -26,15 +28,22 @@
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
+#if defined(__FreeBSD__)
+#include <paths.h>
+#include <sys/endian.h>
+#else
#include <endian.h>
+#endif
#include <time.h>
#include <arpa/inet.h>
+#include <sys/types.h>
#include <net/ethernet.h>
#include <netinet/in.h>
+#if !defined(__FreeBSD__)
#include <netinet/ether.h>
+#endif
#include <sys/time.h>
#include <time.h>
-#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#ifdef __linux__
@@ -45,9 +54,17 @@
#endif
#include <sys/ioctl.h>
#include <sys/stat.h>
+#if defined(__linux__)
#include <sys/sysinfo.h>
+#endif
#include <pwd.h>
+#if defined(__FreeBSD__)
+#include <sys/time.h>
+/* This is the really Posix interface the Linux code should have used !!*/
+#include <utmpx.h>
+#else
#include <utmp.h>
+#endif
#include <syslog.h>
#include <sys/utsname.h>
#include "md5.h"
@@ -309,7 +326,11 @@ static void display_nologin() {
}
static void uwtmp_login(struct mt_connection *conn) {
+#if defined(__FreeBSD__)
+ struct utmpx utent;
+#else
struct utmp utent;
+#endif
pid_t pid;
pid = getpid();
@@ -326,23 +347,45 @@ static void uwtmp_login(struct mt_connection *conn) {
strncpy(utent.ut_user, conn->username, sizeof(utent.ut_user));
strncpy(utent.ut_line, line, sizeof(utent.ut_line));
strncpy(utent.ut_id, utent.ut_line + 3, sizeof(utent.ut_id));
- strncpy(utent.ut_host, ether_ntoa((const struct ether_addr *)conn->srcmac), sizeof(utent.ut_host));
+ strncpy(utent.ut_host,
+ ether_ntoa((const struct ether_addr *)conn->srcmac),
+ sizeof(utent.ut_host));
+#if defined(__FreeBSD__)
+ gettimeofday(&utent.ut_tv, NULL);
+#else
time((time_t *)&(utent.ut_time));
+#endif
/* Update utmp and/or wtmp */
+#if defined(__FreeBSD__)
+ setutxent();
+ pututxline(&utent);
+ endutxent();
+#else
setutent();
pututline(&utent);
endutent();
updwtmp(_PATH_WTMP, &utent);
+#endif
}
static void uwtmp_logout(struct mt_connection *conn) {
if (conn->pid > 0) {
+#if defined(__FreeBSD__)
+ struct utmpx *utentp;
+ struct utmpx utent;
+ setutxent();
+#else
struct utmp *utentp;
struct utmp utent;
setutent();
+#endif
+#if defined(__FreeBSD__)
+ while ((utentp = getutxent()) != NULL) {
+#else
while ((utentp = getutent()) != NULL) {
+#endif
if (utentp->ut_pid == conn->pid && utentp->ut_id) {
break;
}
@@ -354,9 +397,14 @@ static void uwtmp_logout(struct mt_connection *conn) {
utent.ut_type = DEAD_PROCESS;
utent.ut_tv.tv_sec = time(NULL);
+#if defined(__FreeBSD__)
+ pututxline(&utent);
+ endutxent();
+#else
pututline(&utent);
endutent();
updwtmp(_PATH_WTMP, &utent);
+#endif
}
}
}
diff --git a/mndp.c b/mndp.c
index 4ef8f7d..d257087 100644
--- a/mndp.c
+++ b/mndp.c
@@ -22,8 +22,15 @@
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
-#include <arpa/inet.h>
+#include <errno.h>
+#if defined(__FreeBSD__)
+#include <net/ethernet.h>
+#include <netinet/in.h>
+#include <sys/socket.h>
+#else
#include <netinet/ether.h>
+#endif
+#include <arpa/inet.h>
#include <string.h>
#include "protocol.h"
#include "config.h"
diff --git a/protocol.c b/protocol.c
index a23920f..f122032 100644
--- a/protocol.c
+++ b/protocol.c
@@ -28,9 +28,19 @@
#endif
#include <arpa/inet.h>
#include <netinet/in.h>
+#if defined(__FreeBSD__)
+#include <net/ethernet.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#else
#include <netinet/ether.h>
+#endif
#include <time.h>
+#if defined(__FreeBSD__)
+#include <sys/endian.h>
+#else
#include <endian.h>
+#endif
#include "protocol.h"
#include "config.h"
@@ -265,9 +275,9 @@ int mndp_init_packet(struct mt_packet *packet, unsigned char version, unsigned c
header->ttl = ttl;
header->cksum = 0;
- packet->size = sizeof(header);
+ packet->size = sizeof(*header);
- return sizeof(header);
+ return sizeof(*header);
}
int mndp_add_attribute(struct mt_packet *packet, enum mt_mndp_attrtype attrtype, void *attrdata, unsigned short data_len) {
@@ -297,7 +307,7 @@ int mndp_add_attribute(struct mt_packet *packet, enum mt_mndp_attrtype attrtype,
struct mt_mndp_info *parse_mndp(const unsigned char *data, const int packet_len) {
const unsigned char *p;
- static struct mt_mndp_info packet;
+ static struct mt_mndp_info *packetp;
struct mt_mndp_hdr *mndp_hdr;
/* Check for valid packet length */
@@ -305,11 +315,16 @@ struct mt_mndp_info *parse_mndp(const unsigned char *data, const int packet_len)
return NULL;
}
- bzero(&packet, sizeof(packet));
+ packetp = malloc(sizeof(*packetp));
+ if (packetp == NULL) {
+ fprintf(stderr, "ERROR %s: malloc() failed\n", __func__);
+ return NULL;
+ }
+ bzero(packetp, sizeof(*packetp));
mndp_hdr = (struct mt_mndp_hdr*)data;
- memcpy(&(packet.header), mndp_hdr, sizeof(struct mt_mndp_hdr));
+ memcpy(&packetp->header, mndp_hdr, sizeof(struct mt_mndp_hdr));
p = data + sizeof(struct mt_mndp_hdr);
@@ -326,13 +341,16 @@ struct mt_mndp_info *parse_mndp(const unsigned char *data, const int packet_len)
/* Check if len is invalid */
if (p + len > data + packet_len) {
+ fprintf(stderr, "%s: invalid data: "
+ "%p + %u > %p + %d\n",
+ __func__, p, len, data, packet_len);
break;
}
switch (type) {
case MT_MNDPTYPE_ADDRESS:
if (len >= ETH_ALEN) {
- memcpy(packet.address, p, ETH_ALEN);
+ memcpy(packetp->address, p, ETH_ALEN);
}
break;
@@ -341,8 +359,8 @@ struct mt_mndp_info *parse_mndp(const unsigned char *data, const int packet_len)
len = MT_MNDP_MAX_STRING_LENGTH;
}
- memcpy(packet.identity, p, len);
- packet.identity[len] = '\0';
+ memcpy(packetp->identity, p, len);
+ packetp->identity[len] = '\0';
break;
case MT_MNDPTYPE_PLATFORM:
@@ -350,8 +368,8 @@ struct mt_mndp_info *parse_mndp(const unsigned char *data, const int packet_len)
len = MT_MNDP_MAX_STRING_LENGTH;
}
- memcpy(packet.platform, p, len);
- packet.platform[len] = '\0';
+ memcpy(packetp->platform, p, len);
+ packetp->platform[len] = '\0';
break;
case MT_MNDPTYPE_VERSION:
@@ -359,14 +377,14 @@ struct mt_mndp_info *parse_mndp(const unsigned char *data, const int packet_len)
len = MT_MNDP_MAX_STRING_LENGTH;
}
- memcpy(packet.version, p, len);
- packet.version[len] = '\0';
+ memcpy(packetp->version, p, len);
+ packetp->version[len] = '\0';
break;
case MT_MNDPTYPE_TIMESTAMP:
- memcpy(&(packet.uptime), p, 4);
+ memcpy(&packetp->uptime, p, 4);
/* Seems like ping uptime is transmitted as little endian? */
- packet.uptime = le32toh(packet.uptime);
+ packetp->uptime = le32toh(packetp->uptime);
break;
case MT_MNDPTYPE_HARDWARE:
@@ -374,8 +392,8 @@ struct mt_mndp_info *parse_mndp(const unsigned char *data, const int packet_len)
len = MT_MNDP_MAX_STRING_LENGTH;
}
- memcpy(packet.hardware, p, len);
- packet.hardware[len] = '\0';
+ memcpy(packetp->hardware, p, len);
+ packetp->hardware[len] = '\0';
break;
case MT_MNDPTYPE_SOFTID:
@@ -383,8 +401,8 @@ struct mt_mndp_info *parse_mndp(const unsigned char *data, const int packet_len)
len = MT_MNDP_MAX_STRING_LENGTH;
}
- memcpy(packet.softid, p, len);
- packet.softid[len] = '\0';
+ memcpy(packetp->softid, p, len);
+ packetp->softid[len] = '\0';
break;
/*default:
@@ -395,7 +413,7 @@ struct mt_mndp_info *parse_mndp(const unsigned char *data, const int packet_len)
p += len;
}
- return &packet;
+ return packetp;
}
int query_mndp(const char *identity, unsigned char *mac) {
diff --git a/protocol.h b/protocol.h
index 65ffd5b..3894d4c 100644
--- a/protocol.h
+++ b/protocol.h
@@ -151,5 +151,28 @@ static const unsigned char mt_mactelnet_clienttype[2] = { 0x00, 0x15 };
/* Must be initialized by application */
extern unsigned char mt_direction_fromserver;
+/* Debugging stuff */
+#if defined(DEBUG_PROTO)
+#ifndef hexdump_defined
+void hexdump(const char *title, const void *buf, int len)
+{
+ int i;
+ unsigned char *data = (unsigned char *)buf;
+
+ fprintf(stderr, "%s:\n", title);
+ for (i = 0; i < len; i++) {
+ if (!(i & 0xf))
+ fprintf(stderr, "%04x:", i);
+ fprintf(stderr, " %02x", data[i]);
+ if (!(~i & 0xf) || i == len - 1)
+ fprintf(stderr, "\n");
+ }
+}
+#define HEXDUMP(title, buf, len) hexdump(title, buf, len)
+#define hexdump_defined
+#else
+#define HEXDUMP(title, buf, len)
+#endif
+#endif
#endif