Skip to content

Commit

Permalink
Merge pull request #94 from xorrkaz/fix-freebsd-broadcast
Browse files Browse the repository at this point in the history
Fix use of undirected broadcast in FreeBSD.
  • Loading branch information
haakonnessjoen authored Aug 16, 2024
2 parents 5f541a8 + 3ed1b25 commit 6d1f834
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/interfaces.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ int net_get_interfaces(struct net_interface **interfaces) {
static const struct ifaddrs *ifaddrsp;
const struct sockaddr_in *dl_addr;
int found = 0;
#if !defined(__FreeBSD__)
uint32_t allones_bcast = htonl(INADDR_BROADCAST);
#endif

if (getifaddrs(&int_addrs) < 0) {
perror("getifaddrs");
Expand All @@ -173,8 +176,14 @@ int net_get_interfaces(struct net_interface **interfaces) {

if (ifaddrsp->ifa_addr->sa_family == AF_INET) {
memcpy(interface->ipv4_addr, &dl_addr->sin_addr, IPV4_ALEN);
#if defined(__FreeBSD__)
memcpy(interface->bcast_addr, &((const struct sockaddr_in *)ifaddrsp->ifa_broadaddr)->sin_addr, IPV4_ALEN);
#else
memcpy(interface->bcast_addr, &allones_bcast, IPV4_ALEN);
#endif
} else {
memset(interface->ipv4_addr, 0, IPV4_ALEN);
memset(interface->bcast_addr, 0, IPV4_ALEN);
}
}
#ifdef __linux__
Expand Down Expand Up @@ -208,9 +217,12 @@ int net_get_interfaces(struct net_interface **interfaces) {
DL_FOREACH(*interfaces, interface) {
struct in_addr *addr =
(struct in_addr *)interface->ipv4_addr;
struct in_addr *bcast =
(struct in_addr *)interface->bcast_addr;

printf("Interface %s:\n", interface->name);
printf("\tIP: %s\n", inet_ntoa(*addr));
printf("\tBCAST: %s\n", inet_ntoa(*bcast));
printf("\tMAC: %s\n",
ether_ntoa((struct ether_addr *)interface->mac_addr));
#ifdef __linux__
Expand Down
3 changes: 2 additions & 1 deletion src/interfaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct net_interface {
char name[256];
unsigned char ipv4_addr[IPV4_ALEN];
unsigned char mac_addr[ETH_ALEN];
unsigned char bcast_addr[IPV4_ALEN];

/* used by mactelnetd */
int socketfd;
Expand Down Expand Up @@ -57,4 +58,4 @@ extern int net_send_udp(const int socket, struct net_interface *interface, const
const unsigned char *destmac, const struct in_addr *sourceip, const int sourceport,
const struct in_addr *destip, const int destport, const unsigned char *data, const int datalen);
extern unsigned short in_cksum(unsigned short *addr, int len);
#endif
#endif
8 changes: 7 additions & 1 deletion src/mactelnet.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ unsigned char mt_direction_fromserver = 0;

static unsigned int send_socket;

static unsigned char *bcast_addr;

static int handle_packet(unsigned char *data, int data_len);

static void print_version() {
Expand Down Expand Up @@ -168,7 +170,7 @@ static int send_udp(struct mt_packet *packet, int retransmit) {
struct sockaddr_in socket_address;
socket_address.sin_family = AF_INET;
socket_address.sin_port = htons(MT_MACTELNET_PORT);
socket_address.sin_addr.s_addr = htonl(INADDR_BROADCAST);
memcpy(&(socket_address.sin_addr), bcast_addr, IPV4_ALEN);

sent_bytes = sendto(send_socket, packet->data, packet->size, 0, (struct sockaddr *)&socket_address,
sizeof(socket_address));
Expand Down Expand Up @@ -470,6 +472,9 @@ static int find_interface() {
continue;
}

#if defined(__FreeBSD__)
setsockopt(testsocket, IPPROTO_IP, IP_ONESBCAST, &optval, sizeof(optval));
#endif
setsockopt(testsocket, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval));
setsockopt(testsocket, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));

Expand All @@ -488,6 +493,7 @@ static int find_interface() {
send_socket = testsocket;
memcpy(srcmac, interface->mac_addr, ETH_ALEN);
active_interface = interface;
bcast_addr = interface->bcast_addr;

/* Send a SESSIONSTART message with the current device */
init_packet(&data, MT_PTYPE_SESSIONSTART, srcmac, dstmac, sessionkey, 0);
Expand Down
10 changes: 8 additions & 2 deletions src/mactelnetd.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ static void setup_sockets() {
perror("SO_BROADCAST");
continue;
}
#if defined(__FreeBSD__)
if (setsockopt(interface->socketfd, IPPROTO_IP, IP_ONESBCAST, &optval, sizeof(optval)) == -1) {
perror("IP_ONESBCAST");
continue;
}
#endif

setsockopt(interface->socketfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));

Expand Down Expand Up @@ -290,7 +296,7 @@ static int send_udp(const struct mt_connection *conn, const struct mt_packet *pa
struct sockaddr_in socket_address;
socket_address.sin_family = AF_INET;
socket_address.sin_port = htons(conn->srcport);
socket_address.sin_addr.s_addr = htonl(INADDR_BROADCAST);
memcpy(&(socket_address.sin_addr), &conn->interface->bcast_addr, IPV4_ALEN);

return sendto(conn->interface->socketfd, packet->data, packet->size, 0, (struct sockaddr *)&socket_address,
sizeof(socket_address));
Expand All @@ -315,7 +321,7 @@ static int send_special_udp(struct net_interface *interface, unsigned short port
struct sockaddr_in socket_address;
socket_address.sin_family = AF_INET;
socket_address.sin_port = htons(port);
socket_address.sin_addr.s_addr = htonl(INADDR_BROADCAST);
memcpy(&(socket_address.sin_addr), &interface->bcast_addr, IPV4_ALEN);

return sendto(interface->socketfd, packet->data, packet->size, 0, (struct sockaddr *)&socket_address,
sizeof(socket_address));
Expand Down

0 comments on commit 6d1f834

Please sign in to comment.