Skip to content

Commit

Permalink
mcast: Support specifying output interface for IPv6 multicast packets
Browse files Browse the repository at this point in the history
Specify the interface or scope_id in the IP address as a %suffix.

For example:-

examples/coap-client coap://[ff02::fd%interface]
  • Loading branch information
mrdeep1 committed Jan 10, 2025
1 parent 4d84859 commit c84d27a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/coap_address.c
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ coap_resolve_address_info(const coap_str_const_t *address,
error = getaddrinfo(addrstr, NULL, &hints, &res);

if (error != 0) {
coap_log_warn("getaddrinfo: %s\n", gai_strerror(error));
coap_log_warn("getaddrinfo: %s: %s\n", addrstr, gai_strerror(error));
return NULL;
}

Expand Down
21 changes: 21 additions & 0 deletions src/coap_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,11 @@ coap_socket_connect_udp(coap_socket_t *sock,

coap_address_init(&bind_addr);
bind_addr.addr.sa.sa_family = connect_addr.addr.sa.sa_family;
#if COAP_IPV6_SUPPORT
if (connect_addr.addr.sa.sa_family == AF_INET6) {
bind_addr.addr.sin6.sin6_scope_id = connect_addr.addr.sin6.sin6_scope_id;
}
#endif /* COAP_IPV6_SUPPORT */
if (bind(sock->fd, &bind_addr.addr.sa,
#if COAP_IPV4_SUPPORT
bind_addr.addr.sa.sa_family == AF_INET ?
Expand All @@ -323,6 +328,16 @@ coap_socket_connect_udp(coap_socket_t *sock,
goto error;
}
}
#if COAP_IPV6_SUPPORT
if (connect_addr.addr.sa.sa_family == AF_INET6 && !coap_is_bcast(&connect_addr)) {
int value = connect_addr.addr.sin6.sin6_scope_id;

if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, OPTVAL_T(&value),
sizeof(value)) == COAP_SOCKET_ERROR)
coap_log_warn("coap_socket_connect_udp: getsockopt IPV6_MULTICAST_IF: %d: %s\n",
value, coap_socket_strerror());
}
#endif /* COAP_IPV6_SUPPORT */
if (getsockname(sock->fd, &local_addr->addr.sa, &local_addr->size) == COAP_SOCKET_ERROR) {
coap_log_warn("coap_socket_connect_udp: getsockname for multicast socket: %s\n",
coap_socket_strerror());
Expand Down Expand Up @@ -841,6 +856,12 @@ coap_socket_send(coap_socket_t *sock, coap_session_t *session,
#else
bytes_written = send(sock->fd, data, datalen, 0);
#endif
#if COAP_IPV6_SUPPORT
} else if (session->addr_info.remote.addr.sa.sa_family == AF_INET6 && coap_is_mcast(&session->addr_info.remote)) {
bytes_written = sendto(sock->fd, (const void *)data, datalen, 0,
&session->addr_info.remote.addr.sa,
session->addr_info.remote.size);
#endif /* COAP_IPV6_SUPPORT */
} else {
#if defined(_WIN32)
DWORD dwNumberOfBytesSent = 0;
Expand Down

0 comments on commit c84d27a

Please sign in to comment.