From 80177b983ce527b8a48d9f7d43a687e903e2a1ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kon=20Nessj=C3=B8en?= Date: Sat, 6 Apr 2024 21:43:06 +0200 Subject: [PATCH] Clean up udp code a bit, so we don't get random errors out of nowhere. And handle bpf device allocation better. --- src/interfaces.c | 22 +++++++++++++++++++--- src/mactelnetd.c | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/src/interfaces.c b/src/interfaces.c index a3ecfd9..84c30f0 100644 --- a/src/interfaces.c +++ b/src/interfaces.c @@ -283,6 +283,20 @@ unsigned short udp_sum_calc(unsigned char *src_addr,unsigned char *dst_addr, uns return (unsigned short) sum; } +#if !defined(__linux__) +int net_find_bpf_device() { + char dev[11]; + for (int i = 0; i < 99; ++i) { + sprintf(dev, "/dev/bpf%i", i); + int fd = open(dev, O_RDWR); + if (fd != -1) { + return fd; + } + } + return -1; +} +#endif + int net_init_raw_socket() { int fd; @@ -295,7 +309,7 @@ int net_init_raw_socket() { } #else /* Transmit raw packets with bpf */ - fd = open("/dev/bpf0", O_RDWR); + fd = net_find_bpf_device(); if (fd <= 0) { perror("open_bpf"); exit(1); @@ -450,8 +464,10 @@ int net_send_udp(const int fd, struct net_interface *interface, const unsigned c } send_result = write(fd, buffer, datalen + 8 + 14 + 20); - if (send_result == -1) - perror("bpf_write"); + if (send_result == -1) { + // perror("bpf_write"); + return 0; + } #endif /* Return amount of _data_ bytes sent */ diff --git a/src/mactelnetd.c b/src/mactelnetd.c index fef9bde..74cbe70 100644 --- a/src/mactelnetd.c +++ b/src/mactelnetd.c @@ -215,6 +215,7 @@ static struct net_interface *find_socket(unsigned char *mac) { return NULL; } +/* Setup sockets for sending on specific interfaces only */ static void setup_sockets() { struct net_interface *interface; @@ -227,6 +228,11 @@ static void setup_sockets() { continue; } + if (interface->ipv4_addr[0] == 0) { + // Ignore invalid ipv4 addresses + continue; + } + if (!use_raw_socket) { interface->socketfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (interface->socketfd < 0) { @@ -245,14 +251,16 @@ static void setup_sockets() { si_me.sin_port = htons(MT_MACTELNET_PORT); memcpy(&(si_me.sin_addr.s_addr), interface->ipv4_addr, IPV4_ALEN); - if (bind(interface->socketfd, (struct sockaddr *)&si_me, sizeof(si_me))==-1) { + if (bind(interface->socketfd, (struct sockaddr *)&si_me, sizeof(si_me)) == -1) { + close(interface->socketfd); + interface->socketfd = -1; fprintf(stderr, _("Error binding to %s:%d, %s\n"), inet_ntoa(si_me.sin_addr), sourceport, strerror(errno)); + syslog(LOG_NOTICE, _("Error binding to %s:%d on %s\n"), inet_ntoa(si_me.sin_addr), sourceport, interface->name); continue; } + syslog(LOG_NOTICE, _("Using %s to transmit packets from %s\n"), interface->name, ether_ntoa(mac)); } - syslog(LOG_NOTICE, _("Listening on %s for %s\n"), interface->name, ether_ntoa(mac)); - } } @@ -260,6 +268,11 @@ static int send_udp(const struct mt_connection *conn, const struct mt_packet *pa if (use_raw_socket) { return net_send_udp(sockfd, conn->interface, conn->dstmac, conn->srcmac, &sourceip, sourceport, &destip, conn->srcport, packet->data, packet->size); } else { + // We can't send on a socket that is not open + if (conn->interface->socketfd < 0) { + return 0; + } + /* Init SendTo struct */ struct sockaddr_in socket_address; socket_address.sin_family = AF_INET; @@ -277,6 +290,11 @@ static int send_special_udp(struct net_interface *interface, unsigned short port memset(dstmac, 0xff, ETH_ALEN); return net_send_udp(sockfd, interface, interface->mac_addr, dstmac, (const struct in_addr *)&interface->ipv4_addr, port, &destip, port, packet->data, packet->size); } else { + // We can't send on a socket that is not open + if (interface->socketfd < 0) { + return 0; + } + /* Init SendTo struct */ struct sockaddr_in socket_address; socket_address.sin_family = AF_INET; @@ -892,6 +910,8 @@ void mndp_broadcast() { return; } + int num_devices = 0; + int num_devices_sent = 0; DL_FOREACH(interfaces, interface) { struct mt_mndp_hdr *header = (struct mt_mndp_hdr *)&(pdata.data); @@ -899,6 +919,7 @@ void mndp_broadcast() { continue; } + num_devices++; mndp_init_packet(&pdata, 0, 1); mndp_add_attribute(&pdata, MT_MNDPTYPE_ADDRESS, interface->mac_addr, ETH_ALEN); mndp_add_attribute(&pdata, MT_MNDPTYPE_IDENTITY, s_uname.nodename, strlen(s_uname.nodename)); @@ -910,7 +931,13 @@ void mndp_broadcast() { mndp_add_attribute(&pdata, MT_MNDPTYPE_IFNAME, interface->name, strlen(interface->name)); header->cksum = in_cksum((unsigned short *)&(pdata.data), pdata.size); - send_special_udp(interface, MT_MNDP_PORT, &pdata); + if (send_special_udp(interface, MT_MNDP_PORT, &pdata) > 0) { + num_devices_sent++; + } + } + + if (num_devices > 0 && num_devices_sent == 0) { + syslog(LOG_WARNING, _("Was not able to send any MNDP packets")); } }