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 -N coap://[ff02::fd%interface]
  • Loading branch information
mrdeep1 committed Jan 14, 2025
1 parent 4d84859 commit 21c7029
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
5 changes: 3 additions & 2 deletions examples/coap-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,7 @@ usage(const char *program, const char *version) {
"\tcoap-client -m get coap+tcp://%%2Funix%%2Fdomain%%2Fpath%%2Fstream/.well-known/core\n"
"\tcoap-client -m get coaps://[::1]/.well-known/core\n"
"\tcoap-client -m get coaps+tcp://[::1]/.well-known/core\n"
"\tcoap-client -m get -N coap://[ff02::fd%%ens32]/.well-known/core\n"
"\tcoap-client -m get coaps://%%2Funix%%2Fdomain%%2Fpath%%2Fdtls/.well-known/core\n"
"\tcoap-client -m get coaps+tcp://%%2Funix%%2Fdomain%%2Fpath%%2Ftls/.well-known/core\n"
"\tcoap-client -m get -T cafe coap://[::1]/time\n"
Expand Down Expand Up @@ -1613,11 +1614,11 @@ get_session(coap_context_t *ctx,

local.s = (const uint8_t *)local_addr;
local.length = strlen(local_addr);
/* resolve local address where data should be sent from */
/* resolve local address where data should be sent from (don't update port number */
info_list = coap_resolve_address_info(&local, port, port, port, port,
AI_PASSIVE | AI_NUMERICHOST | AI_NUMERICSERV | AI_ALL,
1 << scheme,
COAP_RESOLVE_TYPE_LOCAL);
COAP_RESOLVE_TYPE_REMOTE);
if (!info_list) {
fprintf(stderr, "coap_resolve_address_info: %s: failed\n", local_addr);
return NULL;
Expand Down
7 changes: 7 additions & 0 deletions man/coap-client.txt.in
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,13 @@ coap-client -m get coap://[::1]/.well-known/core
Query on the resource '.well-known/core' on localhost to get back a list of
the known resources along with their attribute definitions.

* Example
----
coap-client -m get -N coap://[ff02::fd%ens32]/.well-known/core
----
Discover the available resources along with their attribute definitions using
a multicast IP sent out over the ethernet interface ens32.

* Example
----
echo -n "mode=on" | coap-client -m put \
Expand Down
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
6 changes: 5 additions & 1 deletion src/coap_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,11 @@ coap_socket_send(coap_socket_t *sock, coap_session_t *session,

pktinfo = (struct in6_pktinfo *)CMSG_DATA(cmsg);

pktinfo->ipi6_ifindex = session->ifindex;
if (coap_is_mcast(&session->addr_info.remote)) {
pktinfo->ipi6_ifindex = session->addr_info.remote.addr.sin6.sin6_scope_id;
} else {
pktinfo->ipi6_ifindex = session->ifindex;
}
memcpy(&pktinfo->ipi6_addr,
&session->addr_info.local.addr.sin6.sin6_addr,
sizeof(pktinfo->ipi6_addr));
Expand Down

0 comments on commit 21c7029

Please sign in to comment.