Skip to content

Commit

Permalink
Add multicast join for mdns
Browse files Browse the repository at this point in the history
  • Loading branch information
sepfy committed Jul 28, 2024
1 parent 0e40619 commit d320271
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/mdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,18 @@ int mdns_resolve_addr(const char *hostname, Address *addr) {
fd_set rfds;
int maxfd, send_retry, recv_retry, size, ret;

addr_from_string(MDNS_GROUP, &mcast_addr);
addr_set_port(&mcast_addr, MDNS_PORT);

if (udp_socket_open(&udp_socket, AF_INET, MDNS_PORT) < 0) {
LOGE("Failed to create socket");
return -1;
}

addr_from_string(MDNS_GROUP, &mcast_addr);
addr_set_port(&mcast_addr, MDNS_PORT);
if (udp_socket_add_multicast_group(&udp_socket, &mcast_addr) < 0) {
LOGE("Failed to add multicast group");
return -1;
}

maxfd = udp_socket.fd;
FD_ZERO(&rfds);

Expand All @@ -145,13 +149,15 @@ int mdns_resolve_addr(const char *hostname, Address *addr) {
LOGE("select error");
break;
} else if (ret > 0 && FD_ISSET(udp_socket.fd, &rfds)) {
ret = udp_socket_recvfrom(&udp_socket, &mcast_addr, buf, sizeof(buf));
ret = udp_socket_recvfrom(&udp_socket, NULL, buf, sizeof(buf));
if (!mdns_parse_answer(buf, ret, addr)) {
addr_to_string(addr, addr_string, sizeof(addr_string));
addr_set_family(addr, AF_INET);
LOGI("Resolved %s -> %s", hostname, addr_string);
udp_socket_close(&udp_socket);
return 1;
} else {
LOGD("timeout");
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,29 @@
#include "utils.h"
#include "socket.h"

int udp_socket_add_multicast_group(UdpSocket *udp_socket, Address *mcast_addr) {

int ret = 0;
struct ip_mreq imreq = {0};
struct in_addr iaddr = {0};

imreq.imr_interface.s_addr = INADDR_ANY;
// IPV4 only
imreq.imr_multiaddr.s_addr = mcast_addr->sin.sin_addr.s_addr;

if ((ret = setsockopt(udp_socket->fd, IPPROTO_IP, IP_MULTICAST_IF, &iaddr, sizeof(struct in_addr))) < 0) {
LOGE("Failed to set IP_MULTICAST_IF: %d", ret);
return ret;
}

if ((ret = setsockopt(udp_socket->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &imreq, sizeof(struct ip_mreq))) < 0) {
LOGE("Failed to set IP_ADD_MEMBERSHIP: %d", ret);
return ret;
}

return 0;
}

int udp_socket_open(UdpSocket *udp_socket, int family, int port) {

int ret;
Expand Down
2 changes: 2 additions & 0 deletions src/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ int udp_socket_sendto(UdpSocket *udp_socket, Address *bind_addr, const uint8_t *

int udp_socket_recvfrom(UdpSocket *udp_sock, Address *bind_addr, uint8_t *buf, int len);

int udp_socket_add_multicast_group(UdpSocket *udp_socket, Address *mcast_addr);

int tcp_socket_open(TcpSocket *tcp_socket, int family);

int tcp_socket_connect(TcpSocket *tcp_socket, Address *addr);
Expand Down

0 comments on commit d320271

Please sign in to comment.