From f98eeb45cb535d2598e205f7d80a80c819c74c08 Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Tue, 6 Jul 2010 04:42:51 +0200 Subject: [PATCH] multicast example: main process joins group ff02:2001::1 --- examples/tiny.c | 17 +++-------------- main.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ net.c | 4 ++++ 3 files changed, 55 insertions(+), 14 deletions(-) diff --git a/examples/tiny.c b/examples/tiny.c index 9c321d0d..f3ba9e6e 100644 --- a/examples/tiny.c +++ b/examples/tiny.c @@ -54,8 +54,7 @@ main(int argc, char **argv) { struct timeval tv; coap_pdu_t *pdu; struct sockaddr_in6 dst; - int hops = 1, loop = 1; - struct ipv6_mreq mreq; + int hops = 4; if ( argc > 1 && strncmp(argv[1], "-h", 2) == 0 ) { usage( argv[0] ); @@ -73,21 +72,11 @@ main(int argc, char **argv) { if ( IN6_IS_ADDR_MULTICAST(&dst.sin6_addr) ) { /* set socket options for multicast */ - + if ( setsockopt( ctx->sockfd, SOL_SOCKET, IPV6_MULTICAST_HOPS, - &hops, sizeof(hops) ) < 0 ) + (char *)&hops, sizeof(hops) ) < 0 ) perror("setsockopt: IPV6_MULTICAST_HOPS"); - if ( setsockopt( ctx->sockfd, SOL_SOCKET, IPV6_MULTICAST_LOOP, - &loop, sizeof(loop) ) < 0 ) - perror("setsockopt: IPV6_MULTICAST_LOOP"); - - memcpy( &mreq.ipv6mr_multiaddr, &dst.sin6_addr, sizeof ( dst.sin6_addr ) ); - mreq.ipv6mr_interface = 0; - - if ( setsockopt( ctx->sockfd, SOL_SOCKET, IPV6_ADD_MEMBERSHIP, - &mreq, sizeof(mreq) ) < 0 ) - perror("setsockopt: IPV6_ADD_MEMBERSHIP"); } while ( 1 ) { diff --git a/main.c b/main.c index 1a291eaa..e98550e8 100644 --- a/main.c +++ b/main.c @@ -144,6 +144,52 @@ usage( const char *program, const char *version) { program, version, program ); } +int +join( coap_context_t *ctx ){ + struct ipv6_mreq mreq; + struct addrinfo *reslocal, *resmulti, hints, *ainfo; + + // Get the local wildcard address to bind to (i.e. "::") + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET6; + hints.ai_socktype = SOCK_DGRAM; + +// Resolve the link-local interface + getaddrinfo("::", NULL, &hints, &reslocal); + + for (ainfo = reslocal; ainfo != NULL; ainfo = ainfo->ai_next) { + if ( ainfo->ai_family == AF_INET6 ) { + mreq.ipv6mr_interface = + ((struct sockaddr_in6 *)ainfo->ai_addr)->sin6_scope_id; + break; + } + } + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET6; + hints.ai_socktype = SOCK_DGRAM; + + // Resolve the multicast address + getaddrinfo("ff02:2001::1", NULL, &hints, &resmulti); + + for (ainfo = resmulti; ainfo != NULL; ainfo = ainfo->ai_next) { + if ( ainfo->ai_family == AF_INET6 ) { + // Join the multicast group + mreq.ipv6mr_multiaddr = + ((struct sockaddr_in6 *)ainfo->ai_addr)->sin6_addr; + break; + } + } + + setsockopt( ctx->sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, + (char *)&mreq, sizeof(mreq)); + + freeaddrinfo(resmulti); + freeaddrinfo(reslocal); + + return 0; +} + int main(int argc, char **argv) { coap_context_t *ctx; @@ -169,6 +215,8 @@ main(int argc, char **argv) { exit( 1 ); } + join( ctx ); + if (! (pdu = coap_new_get( path ) ) ) return -1; diff --git a/net.c b/net.c index e859206d..d0b58325 100644 --- a/net.c +++ b/net.c @@ -176,6 +176,7 @@ coap_new_context() { coap_context_t *c = malloc( sizeof( coap_context_t ) ); struct sockaddr_in6 addr; time_t now; + int reuse = 1; srand( getpid() ^ time(&now) ); @@ -192,6 +193,9 @@ coap_new_context() { goto onerror; } + if ( setsockopt( c->sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse) ) < 0 ) + perror("setsockopt SO_REUSEADDR"); + memset(&addr, 0, sizeof addr ); addr.sin6_family = AF_INET6; addr.sin6_port = htons( COAP_DEFAULT_PORT );