Skip to content

Commit

Permalink
fix(arch): On win32 a socket for multicast needs to be bound to INADD…
Browse files Browse the repository at this point in the history
…R_ANY
  • Loading branch information
jpfr committed Dec 25, 2023
1 parent 8517db9 commit 2d8239b
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions arch/eventloop_posix_udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -619,9 +619,6 @@ UDP_registerListenSocket(UA_POSIXConnectionManager *pcm, UA_UInt16 port,
}
}

/* Are we going to create a socket for multicast? */
MultiCastType mc = multiCastType(info);

/* Create the listen socket */
UA_FD listenSocket = socket(info->ai_family, info->ai_socktype, info->ai_protocol);
if(listenSocket == UA_INVALID_FD) {
Expand All @@ -646,8 +643,38 @@ UDP_registerListenSocket(UA_POSIXConnectionManager *pcm, UA_UInt16 port,
return UA_STATUSCODE_BADCONNECTIONREJECTED;
}

/* Are we going to prepare a socket for multicast? */
MultiCastType mc = multiCastType(info);

/* Bind socket to the address */
#ifdef _WIN32
/* On windows we need to bind the socket to INADDR_ANY before registering
* for the multicast group */
int ret = -1;
if(mc != MULTICASTTYPE_NONE) {
if(info->ai_family == AF_INET) {
struct sockaddr_in *orig = (struct sockaddr_in *)info->ai_addr;
struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = orig->sin_port;
ret = bind(listenSocket, (struct sockaddr*)&sin, sizeof(sin));
} else if(info->ai_family == AF_INET6) {
struct sockaddr_in6 *orig = (struct sockaddr_in6 *)info->ai_addr;
struct sockaddr_in6 sin6;
memset(&sin6, 0, sizeof(sin6));
sin6.sin6_family = AF_INET6;
sin6.sin6_addr = in6addr_any;
sin6.sin6_port = orig->sin6_port;
ret = bind(listenSocket, (struct sockaddr*)&sin6, sizeof(sin6));
}
} else {
ret = bind(listenSocket, info->ai_addr, (socklen_t)info->ai_addrlen);
}
#else
int ret = bind(listenSocket, info->ai_addr, (socklen_t)info->ai_addrlen);
#endif
if(ret < 0) {
UA_LOG_SOCKET_ERRNO_WRAP(
UA_LOG_WARNING(el->eventLoop.logger, UA_LOGCATEGORY_NETWORK,
Expand Down Expand Up @@ -896,9 +923,6 @@ registerSocketAndDestinationForSend(const UA_KeyValueMap *params,
const char *hostname, struct addrinfo *info,
int error, UDP_FD *ufd, UA_FD *sock,
const UA_Logger *logger) {
/* Are we going to create a socket for multicast? */
MultiCastType mc = multiCastType(info);

UA_FD newSock = socket(info->ai_family, info->ai_socktype, info->ai_protocol);
*sock = newSock;
if(newSock == UA_INVALID_FD) {
Expand All @@ -914,6 +938,8 @@ registerSocketAndDestinationForSend(const UA_KeyValueMap *params,
return res;
}

/* Prepare socket for multicast */
MultiCastType mc = multiCastType(info);
if(mc != MULTICASTTYPE_NONE) {
res = setupSendMultiCast(newSock, info, params, mc, logger);
if(res != UA_STATUSCODE_GOOD) {
Expand Down

0 comments on commit 2d8239b

Please sign in to comment.