From 51edd4405b99cf102f9f5e02fc4a51a22487a3dc Mon Sep 17 00:00:00 2001 From: Brock Williams Date: Mon, 3 Feb 2014 11:11:13 -0700 Subject: port to freebsd --- interfaces.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++++----------- macping.c | 9 +++++ mactelnet.c | 8 ++++- mactelnetd.c | 52 +++++++++++++++++++++++++++-- mndp.c | 9 ++++- protocol.c | 56 ++++++++++++++++++++----------- protocol.h | 23 +++++++++++++ 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 #include #include @@ -25,9 +29,18 @@ #include #include #include +#if defined(__FreeBSD__) +#include +#endif #include #include +#if defined(__FreeBSD__) +#include +#define ETH_FRAME_LEN (ETHER_MAX_LEN - ETHER_CRC_LEN) +#define ETH_ALEN ETHER_ADDR_LEN +#else #include +#endif #include #include #include @@ -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 #include #include +#if defined(__FreeBSD__) +#include +#include +#include +#include +#define ETH_FRAME_LEN ETHER_MAX_LEN +#define ETH_ALEN ETHER_ADDR_LEN +#else #include +#endif #include #include #include diff --git a/mactelnet.c b/mactelnet.c index 4eebc5f..2661475 100644 --- a/mactelnet.c +++ b/mactelnet.c @@ -26,10 +26,16 @@ #include #include #include +#if defined(__FreeBSD__) +#include +#include +#include +#else #include +#include +#endif #include #include -#include #include #include #include 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 #include @@ -26,15 +28,22 @@ #include #include #include +#if defined(__FreeBSD__) +#include +#include +#else #include +#endif #include #include +#include #include #include +#if !defined(__FreeBSD__) #include +#endif #include #include -#include #include #include #ifdef __linux__ @@ -45,9 +54,17 @@ #endif #include #include +#if defined(__linux__) #include +#endif #include +#if defined(__FreeBSD__) +#include +/* This is the really Posix interface the Linux code should have used !!*/ +#include +#else #include +#endif #include #include #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 #include #include -#include +#include +#if defined(__FreeBSD__) +#include +#include +#include +#else #include +#endif +#include #include #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 #include +#if defined(__FreeBSD__) +#include +#include +#include +#else #include +#endif #include +#if defined(__FreeBSD__) +#include +#else #include +#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 -- cgit v1.2.3