Skip to content

Commit

Permalink
Clean up udp code a bit, so we don't get random errors out of nowhere…
Browse files Browse the repository at this point in the history
…. And handle bpf device allocation better.
  • Loading branch information
haakonnessjoen committed Apr 6, 2024
1 parent 1536e49 commit 80177b9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
22 changes: 19 additions & 3 deletions src/interfaces.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -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 */
Expand Down
35 changes: 31 additions & 4 deletions src/mactelnetd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand All @@ -245,21 +251,28 @@ 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));

}
}

static int send_udp(const struct mt_connection *conn, const struct mt_packet *packet) {
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;
Expand All @@ -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;
Expand Down Expand Up @@ -892,13 +910,16 @@ 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);

if (interface->has_mac == 0) {
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));
Expand All @@ -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"));
}
}

Expand Down

0 comments on commit 80177b9

Please sign in to comment.