diff --git a/CMakeLists.txt b/CMakeLists.txt index a8ce3628..76642140 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -103,6 +103,7 @@ check_include_file(memory.h HAVE_MEMORY_H) check_include_file(strings.h HAVE_STRINGS_H) check_include_file(string.h HAVE_STRING_H) check_include_file(sys/sysctl.h HAVE_SYS_SYSCTL_H) +check_include_file(net/if.h HAVE_NET_IF_H) check_include_file(netinet/in.h HAVE_NETINET_IN_H) check_include_file(sys/epoll.h HAVE_EPOLL_H) check_include_file(sys/timerfd.h HAVE_TIMERFD_H) diff --git a/cmake_coap_config.h.in b/cmake_coap_config.h.in index 40b1330e..c0836090 100644 --- a/cmake_coap_config.h.in +++ b/cmake_coap_config.h.in @@ -58,6 +58,9 @@ /* Define to 1 if you have the header file. */ #cmakedefine HAVE_NETDB_H "@HAVE_NETDB_H@" + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_NET_IF_H "@HAVE_NET_IF_H@" /* Define to 1 if you have the header file. */ #cmakedefine HAVE_NETINET_IN_H "@HAVE_NETINET_IN_H@" diff --git a/examples/coap-rd.c b/examples/coap-rd.c index 11dfe01a..0184a9d7 100644 --- a/examples/coap-rd.c +++ b/examples/coap-rd.c @@ -611,12 +611,15 @@ usage( const char *program, const char *version) { break; } fprintf(stderr, "\n" - "Usage: %s [-g group] [-p port] [-v num] [-A address]\n" + "Usage: %s [-g group] [-G group_if] [-p port] [-v num] [-A address]\n" "\t [[-h hint] [-k key]]\n" "\t [[-c certfile] [-C cafile] [-n] [-R trust_casfile]]\n" "General Options\n" "\t-g group\tJoin the given multicast group.\n" "\t \t\tNote: DTLS over multicast is not currently supported\n" + "\t-G group_if\tUse this interface for listening for the multicast\n" + "\t \t\tgroup. This can be different from the implied interface\n" + "\t \t\tif the -A option is used\n" "\t-p port\t\tListen on specified port\n" "\t-v num \t\tVerbosity level (default 3, maximum is 9). Above 7,\n" "\t \t\tthere is increased verbosity in GnuTLS and OpenSSL logging\n" @@ -805,13 +808,14 @@ main(int argc, char **argv) { char addr_str[NI_MAXHOST] = "::"; char port_str[NI_MAXSERV] = "5683"; char *group = NULL; + char *group_if = NULL; int opt; coap_log_t log_level = LOG_WARNING; #ifndef _WIN32 struct sigaction sa; #endif - while ((opt = getopt(argc, argv, "A:c:C:g:h:k:n:R:p:v:")) != -1) { + while ((opt = getopt(argc, argv, "A:c:C:g:G:h:k:n:R:p:v:")) != -1) { switch (opt) { case 'A' : strncpy(addr_str, optarg, NI_MAXHOST-1); @@ -826,6 +830,9 @@ main(int argc, char **argv) { case 'g' : group = optarg; break; + case 'G' : + group_if = optarg; + break; case 'h' : if (!optarg[0]) { hint = NULL; @@ -869,7 +876,7 @@ main(int argc, char **argv) { return -1; if (group) - coap_join_mcast_group_intf(ctx, group, NULL); + coap_join_mcast_group_intf(ctx, group, group_if); init_resources(ctx); diff --git a/examples/coap-server.c b/examples/coap-server.c index a0c58def..aa66e6b5 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -2187,8 +2187,8 @@ usage( const char *program, const char *version) { break; } fprintf(stderr, "\n" - "Usage: %s [-d max] [-e] [-g group] [-l loss] [-p port] [-v num]\n" - "\t\t[-A address] [-L value] [-N]\n" + "Usage: %s [-d max] [-e] [-g group] [-G group_if] [-l loss] [-p port]\n" + "\t\t[-v num] [-A address] [-L value] [-N]\n" "\t\t[-P scheme://address[:port],name1[,name2..]]\n" "\t\t[[-h hint] [-i match_identity_file] [-k key]\n" "\t\t[-s match_psk_sni_file] [-u user]]\n" @@ -2201,6 +2201,10 @@ usage( const char *program, const char *version) { "\t \t\tuntil one of the dynamic resources has been deleted\n" "\t-e \t\tEcho back the data sent with a PUT\n" "\t-g group\tJoin the given multicast group\n" + "\t \t\tNote: DTLS over multicast is not currently supported\n" + "\t-G group_if\tUse this interface for listening for the multicast\n" + "\t \t\tgroup. This can be different from the implied interface\n" + "\t \t\tif the -A option is used\n" "\t-l list\t\tFail to send some datagrams specified by a comma\n" "\t \t\tseparated list of numbers or number ranges\n" "\t \t\t(for debugging only)\n" @@ -2630,6 +2634,7 @@ int main(int argc, char **argv) { coap_context_t *ctx; char *group = NULL; + char *group_if = NULL; coap_tick_t now; char addr_str[NI_MAXHOST] = "::"; char port_str[NI_MAXSERV] = "5683"; @@ -2653,7 +2658,7 @@ main(int argc, char **argv) { clock_offset = time(NULL); - while ((opt = getopt(argc, argv, "c:d:eg:h:i:j:J:k:l:mnp:s:u:v:A:C:L:M:NP:R:S:")) != -1) { + while ((opt = getopt(argc, argv, "c:d:eg:G:h:i:j:J:k:l:mnp:s:u:v:A:C:L:M:NP:R:S:")) != -1) { switch (opt) { case 'A' : strncpy(addr_str, optarg, NI_MAXHOST-1); @@ -2674,6 +2679,9 @@ main(int argc, char **argv) { case 'g' : group = optarg; break; + case 'G' : + group_if = optarg; + break; case 'h' : if (!optarg[0]) { hint = NULL; @@ -2784,7 +2792,7 @@ main(int argc, char **argv) { sizeof(cache_ignore_options)/sizeof(cache_ignore_options[0])); /* join multicast group if requested at command line */ if (group) - coap_join_mcast_group_intf(ctx, group, NULL); + coap_join_mcast_group_intf(ctx, group, group_if); coap_fd = coap_context_get_coap_fd(ctx); if (coap_fd != -1) { diff --git a/include/coap2/net.h b/include/coap2/net.h index 5280d59d..5c34e1b7 100644 --- a/include/coap2/net.h +++ b/include/coap2/net.h @@ -743,11 +743,13 @@ coap_pdu_t *coap_wellknown_response(coap_context_t *context, unsigned int coap_calc_timeout(coap_session_t *session, unsigned char r); /** - * Function interface for joining a multicast group for listening + * Function interface for joining a multicast group for listening for the + * currently defined endpoints that are UDP. * - * @param ctx The current context - * @param groupname The name of the group that is to be joined for listening - * @param ifname Network interface to join the group on + * @param ctx The current context. + * @param groupname The name of the group that is to be joined for listening. + * @param ifname Network interface to join the group on, or NULL if first + * appropriate interface is to be chosen by the O/S. * * @return 0 on success, -1 on error */ diff --git a/man/Makefile.am b/man/Makefile.am index 91b428a8..74333ca2 100644 --- a/man/Makefile.am +++ b/man/Makefile.am @@ -82,7 +82,7 @@ A2X_EXTRA_PAGES = @DOLLAR_SIGN@(shell for fil in $(TXT3) ; do sed -ne '/^NAME/,/ install-man: install-man3 install-man5 install-man7 @echo ".so man3/coap_cache.3" > coap_cache_get_app_data.3 @echo ".so man3/coap_cache.3" > coap_cache_set_app_data.3 - @echo ".so man3/coap_context.3" > coap_context_set_pki_root_cas.3 + @echo ".so man3/coap_context.3" > coap_join_mcast_group_intf.3 @echo ".so man3/coap_pdu_setup.3" > coap_encode_var_safe.3 @echo ".so man3/coap_pdu_setup.3" > coap_encode_var_safe8.3 @echo ".so man3/coap_pdu_setup.3" > coap_add_optlist_pdu.3 diff --git a/man/coap-rd.txt.in b/man/coap-rd.txt.in index 01bb0be4..042f4b19 100644 --- a/man/coap-rd.txt.in +++ b/man/coap-rd.txt.in @@ -14,7 +14,7 @@ coap-rd - A CoAP Resource Directory based on libcoap SYNOPSIS -------- -*coap-rd* [*-g* group] [*-p* port] [*-v* num] [*-A* address] +*coap-rd* [*-g* group] [*-G* group_if] [*-p* port] [*-v* num] [*-A* address] [[*-h* hint] [*-k* key]] [[*-c* certfile] [*-n*] [*-C* cafile] [*-R* trusted_casfile]] @@ -29,6 +29,10 @@ OPTIONS Join specified multicast 'group' on startup. *Note:* DTLS over multicast is not currently supported. +*-G* group_if:: + Use this interface for listening for the multicast group. This can be + different from the implied interface if the *-A* option is used. + *-p* port:: The 'port' on the given address will be listening for incoming connections. If (D)TLS is supported, then 'port' + 1 will also be listened on for diff --git a/man/coap-server.txt.in b/man/coap-server.txt.in index cb424d76..c20eed2b 100644 --- a/man/coap-server.txt.in +++ b/man/coap-server.txt.in @@ -14,8 +14,8 @@ coap-server - CoAP Server based on libcoap SYNOPSIS -------- -*coap-server* [*-d* max] [*-e*] [*-g* group] [*-l* loss] [*-p* port] [*-v* num] - [*-A* address] [*-L* value] [*-N*] +*coap-server* [*-d* max] [*-e*] [*-g* group] [*-G* group_if] [*-l* loss] + [*-p* port] [*-v* num] [*-A* address] [*-L* value] [*-N*] [*-P* scheme://addr[:port],name1[,name2..]] [[*-h* hint] [*-i* match_identity_file] [*-k* key] [*-s* match_psk_sni_file] [*-u* user]] @@ -40,6 +40,11 @@ OPTIONS - General *-g* group:: Join specified multicast 'group' on start up. + *Note:* DTLS over multicast is not currently supported. + +*-G* group_if:: + Use this interface for listening for the multicast group. This can be + different from the implied interface if the *-A* option is used. *-l* list:: Fail to send some datagrams specified by a comma separated list of diff --git a/man/coap_context.txt.in b/man/coap_context.txt.in index 43d7eda4..3f64bb9c 100644 --- a/man/coap_context.txt.in +++ b/man/coap_context.txt.in @@ -18,7 +18,8 @@ coap_context_set_pki_root_cas, coap_context_set_psk2, coap_new_endpoint, coap_free_endpoint, -coap_endpoint_set_default_mtu +coap_endpoint_set_default_mtu, +coap_join_mcast_group_intf - Work with CoAP contexts SYNOPSIS @@ -46,6 +47,9 @@ const coap_address_t *_listen_addr_, coap_proto_t _proto_);* *void coap_endpoint_set_default_mtu(coap_endpoint_t *_endpoint_, unsigned _mtu_);* +*int coap_join_mcast_group_intf(coap_context_t *_context_, +const char *_groupname_, const char *_ifname_);* + Link with *-lcoap-@LIBCOAP_API_VERSION@*, *-lcoap-@LIBCOAP_API_VERSION@-gnutls*, *-lcoap-@LIBCOAP_API_VERSION@-openssl*, *-lcoap-@LIBCOAP_API_VERSION@-mbedtls* or *-lcoap-@LIBCOAP_API_VERSION@-tinydtls* depending on your (D)TLS library @@ -158,6 +162,15 @@ The *coap_endpoint_set_default_mtu*() function is used to set the MTU size (the maximum message size) of the data in a packet, excluding any IP or TCP/UDP overhead to _mtu_ for the _endpoint_. A sensible default is 1280. +The *coap_join_mcast_group_intf*() function is used to join the currently +defined endpoints that are UDP, associated with _context_, to the defined +multicast group _groupname_. If _ifname_ is not NULL, then the multicast group +is associated with this interface, otherwise the underlying O/S will choose the +first appropriate interface. When the endpoint is freed off, the associated +multicast group will be removed. The registered multicast addresses for CoAP +are 224.0.1.187, ff0x::fd (Variable-Scope) - i.e. ff02::fd (Link-Local) and +ff05::fd (Site-Local). + RETURN VALUES ------------- *coap_new_context*() function returns a newly created context or @@ -169,6 +182,8 @@ NULL if there is a creation failure. *coap_new_endpoint*() function returns a newly created endpoint or NULL if there is a creation failure. +*coap_join_mcast_group_intf*() returns 0 on success, -1 on failure. + EXAMPLES -------- *CoAP Server Non-Encrypted Setup* diff --git a/src/coap_debug.c b/src/coap_debug.c index 4fd5340a..c0d5a25d 100644 --- a/src/coap_debug.c +++ b/src/coap_debug.c @@ -163,6 +163,10 @@ print_readable( const uint8_t *data, size_t len, #define min(a,b) ((a) < (b) ? (a) : (b)) #endif +/* + * Returned buf is always NULL terminated. + * Returned size is number of characters, not including NULL terminator. + */ size_t coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, size_t len) { #if defined( HAVE_ARPA_INET_H ) || defined( HAVE_WS2TCPIP_H ) @@ -171,14 +175,20 @@ coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, size_t le unsigned char *p = buf; size_t need_buf; + assert(buf); + assert(len); + buf[0] = '\000'; + switch (addr->addr.sa.sa_family) { case AF_INET: + if (len < INET_ADDRSTRLEN + 1) /* Include : */ + return 0; addrptr = &addr->addr.sin.sin_addr; port = ntohs(addr->addr.sin.sin_port); need_buf = INET_ADDRSTRLEN; break; case AF_INET6: - if (len < 7) /* do not proceed if buffer is even too short for [::]:0 */ + if (len < INET6_ADDRSTRLEN + 3) /* Include [ ] : */ return 0; *p++ = '['; @@ -189,7 +199,9 @@ coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, size_t le break; default: - memcpy(buf, "(unknown address type)", min(22, len)); + /* Include trailing NULL if possible */ + memcpy(buf, "(unknown address type)", min(22+1, len)); + buf[len-1] = '\000'; return min(22, len); } @@ -197,21 +209,23 @@ coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, size_t le if (inet_ntop(addr->addr.sa.sa_family, addrptr, (char *)p, min(len, need_buf)) == 0) { perror("coap_print_addr"); + buf[0] = '\000'; return 0; } - p += strnlen((char *)p, len); + p += strlen((char *)p); if (addr->addr.sa.sa_family == AF_INET6) { - if (p < buf + len) { + if (p + 1 < buf + len) { *p++ = ']'; } else - return 0; + return p - buf; /* Already NULL terminated */ } - p += snprintf((char *)p, buf + len - p + 1, ":%d", port); + /* Cannot rely on snprintf() return value for short buffers */ + snprintf((char *)p, buf + len - p, ":%d", port); - return buf + len - p; + return strlen((char *)buf); #else /* HAVE_ARPA_INET_H */ # if WITH_CONTIKI unsigned char *p = buf; @@ -219,7 +233,10 @@ coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, size_t le # if NETSTACK_CONF_WITH_IPV6 const uint8_t hex[] = "0123456789ABCDEF"; - if (len < 41) + assert(buf); + assert(len); + buf[0] = '\000'; + if (len < 42) return 0; *p++ = '['; @@ -237,23 +254,30 @@ coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, size_t le # else /* WITH_UIP6 */ # warning "IPv4 network addresses will not be included in debug output" - if (len < 21) + if (len < 21) { + *p = '\000'; return 0; + } # endif /* WITH_UIP6 */ - if (buf + len - p < 6) - return 0; + if (buf + len - p < 6) { + *p = '\000'; + return p - buf; + } #ifdef HAVE_SNPRINTF - p += snprintf((char *)p, buf + len - p + 1, ":%d", uip_htons(addr->port)); + /* Cannot rely on snprintf() return value for short buffers */ + snprintf((char *)p, buf + len - p, ":%d", uip_htons(addr->port)); #else /* HAVE_SNPRINTF */ /* @todo manual conversion of port number */ + *p = '\000'; #endif /* HAVE_SNPRINTF */ - return p - buf; + return strlen((char *)p); # else /* WITH_CONTIKI */ /* TODO: output addresses manually */ # warning "inet_ntop() not available, network addresses will not be included in debug output" # endif /* WITH_CONTIKI */ + buf[0] = '\000'; return 0; #endif } diff --git a/src/net.c b/src/net.c index 6d75f650..aff88403 100644 --- a/src/net.c +++ b/src/net.c @@ -3140,67 +3140,22 @@ void coap_cleanup(void) { #if ! defined WITH_CONTIKI && ! defined WITH_LWIP && ! defined RIOT_VERSION int coap_join_mcast_group_intf(coap_context_t *ctx, const char *group_name, - const char *ifname) { + const char *ifname) { struct ip_mreq mreq4; struct ipv6_mreq mreq6; - struct addrinfo *reslocal = NULL, *resmulti = NULL, hints, *ainfo; - struct ifreq ifr; + struct addrinfo *resmulti = NULL, hints, *ainfo; int result = -1; coap_endpoint_t *endpoint; int mgroup_setup = 0; - mreq6.ipv6mr_interface = -1U; - mreq4.imr_interface.s_addr = INADDR_BROADCAST; + /* Need to have at least one endpoint! */ + assert(ctx->endpoint); + if (!ctx->endpoint) + return -1; - if (!ifname) { - /* no interface specified */ - /* we have to resolve the link-local interface to get the interface id */ - memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_INET6; - hints.ai_socktype = SOCK_DGRAM; - - result = getaddrinfo("::", NULL, &hints, &reslocal); - if (result != 0) { - coap_log(LOG_WARNING, "coap_join_mcast_group_intf: " - "cannot resolve link-local interface: %s\n", - gai_strerror(result)); - } - else { - /* get the first suitable interface identifier */ - for (ainfo = reslocal; ainfo != NULL; ainfo = ainfo->ai_next) { - if (ainfo->ai_family == AF_INET6) { - mreq6.ipv6mr_interface = - ((struct sockaddr_in6 *)ainfo->ai_addr)->sin6_scope_id; - break; - } - } - } - - mreq4.imr_interface.s_addr = INADDR_ANY; - } - else { - /* interface specified */ - /* get interface index for IPv6 and address for IPv4 */ - strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1); - - result = ioctl(ctx->endpoint->sock.fd, SIOCGIFINDEX, &ifr); - if (result != 0) { - coap_log(LOG_WARNING, "coap_join_mcast_group_intf: " - "cannot get interface index for '%s': %d\n", ifname, result); - } - else { - mreq6.ipv6mr_interface = ifr.ifr_ifindex; - } - - result = ioctl(ctx->endpoint->sock.fd, SIOCGIFADDR, &ifr); - if (result != 0) { - coap_log(LOG_WARNING, "coap_join_mcast_group_intf: " - "cannot get IPv4 address for '%s': %d\n", ifname, result); - } - else { - mreq4.imr_interface = ((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr; - } - } + /* Default is let the kernel choose */ + mreq6.ipv6mr_interface = 0; + mreq4.imr_interface.s_addr = INADDR_ANY; memset(&hints, 0, sizeof(hints)); hints.ai_socktype = SOCK_DGRAM; @@ -3210,27 +3165,127 @@ coap_join_mcast_group_intf(coap_context_t *ctx, const char *group_name, if (result != 0) { coap_log(LOG_ERR, - "coap_join_mcast_group: cannot resolve multicast address: %s\n", - gai_strerror(result)); + "coap_join_mcast_group_intf: %s: " + "Cannot resolve multicast address: %s\n", + group_name, gai_strerror(result)); goto finish; } +/* Need to do a windows equivalent at some point */ +#ifndef _WIN32 + if (ifname) { + /* interface specified - check if we have correct IPv4/IPv6 information */ + int done_ip4 = 0; + int done_ip6 = 0; + int ip4fd; + struct ifreq ifr; + + /* See which mcast address family types are being asked for */ + for (ainfo = resmulti; ainfo != NULL && !(done_ip4 == 1 && done_ip6 == 1); + ainfo = ainfo->ai_next) { + switch (ainfo->ai_family) { + case AF_INET6: + if (done_ip6) + break; + done_ip6 = 1; + memset (&ifr, 0, sizeof(ifr)); + strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1); + ifr.ifr_name[IFNAMSIZ - 1] = '\000'; + + result = ioctl(ctx->endpoint->sock.fd, SIOCGIFINDEX, &ifr); + if (result != 0) { + coap_log(LOG_WARNING, "coap_join_mcast_group_intf: " + "cannot get interface index for '%s': %s\n", + ifname, coap_socket_strerror()); + } + else { + /* Capture the IPv6 if_index for later */ + mreq6.ipv6mr_interface = ifr.ifr_ifindex; + } + break; + case AF_INET: + if (done_ip4) + break; + done_ip4 = 1; + /* + * Need an AF_INET socket to do this unfortunately to stop + * "Invalid argument" error if AF_INET6 socket is used for SIOCGIFADDR + */ + ip4fd = socket(AF_INET, SOCK_DGRAM, 0); + if (ip4fd == -1) { + coap_log(LOG_ERR, + "coap_join_mcast_group_intf: %s: socket: %s\n", + ifname, coap_socket_strerror()); + continue; + } + memset (&ifr, 0, sizeof(ifr)); + strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1); + ifr.ifr_name[IFNAMSIZ - 1] = '\000'; + result = ioctl(ip4fd, SIOCGIFADDR, &ifr); + if (result != 0) { + coap_log(LOG_ERR, + "coap_join_mcast_group_intf: %s: " + "Cannot get IPv4 address: %s\n", + ifname, coap_socket_strerror()); + } + else { + /* Capture the IPv4 address for later */ + mreq4.imr_interface = ((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr; + } + close(ip4fd); + break; + default: + break; + } + } + } +#endif /* ! _WIN32 */ + + /* Add in mcast address(es) to appropriate interface */ for (ainfo = resmulti; ainfo != NULL; ainfo = ainfo->ai_next) { LL_FOREACH(ctx->endpoint, endpoint) { - if (endpoint->proto == COAP_PROTO_UDP || - endpoint->proto == COAP_PROTO_DTLS) { + /* Only UDP currently supported */ + if (endpoint->proto == COAP_PROTO_UDP) { + coap_address_t gaddr; + + coap_address_init(&gaddr); if (ainfo->ai_family == AF_INET6) { - if (mreq6.ipv6mr_interface == -1U) - continue; - mreq6.ipv6mr_multiaddr = + if (!ifname) { + if(endpoint->bind_addr.addr.sa.sa_family == AF_INET6) { + /* + * Do it on the ifindex that the server is listening on + * (sin6_scope_id could still be 0) + */ + mreq6.ipv6mr_interface = + endpoint->bind_addr.addr.sin6.sin6_scope_id; + } + else { + mreq6.ipv6mr_interface = 0; + } + } + gaddr.addr.sin6.sin6_family = AF_INET6; + gaddr.addr.sin6.sin6_port = endpoint->bind_addr.addr.sin6.sin6_port; + gaddr.addr.sin6.sin6_addr = mreq6.ipv6mr_multiaddr = ((struct sockaddr_in6 *)ainfo->ai_addr)->sin6_addr; result = setsockopt(endpoint->sock.fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, (char *)&mreq6, sizeof(mreq6)); } else if (ainfo->ai_family == AF_INET) { - if (mreq4.imr_interface.s_addr == INADDR_BROADCAST) - continue; - mreq4.imr_multiaddr.s_addr = + if (!ifname) { + if(endpoint->bind_addr.addr.sa.sa_family == AF_INET) { + /* + * Do it on the interface that the server is listening on + * (sin_addr could still be INADDR_ANY) + */ + mreq4.imr_interface = endpoint->bind_addr.addr.sin.sin_addr; + } + else { + mreq4.imr_interface.s_addr = INADDR_ANY; + } + } + gaddr.addr.sin.sin_family = AF_INET; + gaddr.addr.sin.sin_port = endpoint->bind_addr.addr.sin.sin_port; + gaddr.addr.sin.sin_addr.s_addr = mreq4.imr_multiaddr.s_addr = ((struct sockaddr_in *)ainfo->ai_addr)->sin_addr.s_addr; result = setsockopt(endpoint->sock.fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq4, sizeof(mreq4)); @@ -3241,10 +3296,21 @@ coap_join_mcast_group_intf(coap_context_t *ctx, const char *group_name, if (result == COAP_SOCKET_ERROR) { coap_log(LOG_ERR, - "coap_join_mcast_group: setsockopt: %s: '%s'\n", - coap_socket_strerror(), group_name); + "coap_join_mcast_group_intf: %s: setsockopt: %s\n", + group_name, coap_socket_strerror()); } else { + char addr_str[INET6_ADDRSTRLEN + 8 + 1]; + + addr_str[sizeof(addr_str)-1] = '\000'; + if (coap_print_addr(&gaddr, (uint8_t*)addr_str, + sizeof(addr_str) - 1)) { + if (ifname) + coap_log(LOG_DEBUG, "added mcast group %s i/f %s\n", addr_str, + ifname); + else + coap_log(LOG_DEBUG, "added mcast group %s\n", addr_str); + } mgroup_setup = 1; } } @@ -3256,14 +3322,13 @@ coap_join_mcast_group_intf(coap_context_t *ctx, const char *group_name, finish: freeaddrinfo(resmulti); - freeaddrinfo(reslocal); return result; } #else /* defined WITH_CONTIKI || defined WITH_LWIP */ int coap_join_mcast_group_intf(coap_context_t *ctx, const char *group_name, - const char *ifname) { + const char *ifname) { (void)ctx; (void)group_name; (void)ifname;