From 7872a80fb43234412f7231d4ceac90f688cb66ae Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Wed, 28 Feb 2018 15:44:32 +0100 Subject: [PATCH] network I/O: fixed socket creation for multicast communication This bugfix allows clients to send datagrams to IP multicast groups. After introducing the concept of client sessions, datagram sockets have been connected to their remote peer in coap_socket_connect_udp(). As this operation fails for multicast addresses, sending to multicast groups was not possible anymore. coap_socket_connect_udp() now checks whether the given server address is a multicast address. If this is the case, connect() is not called and the flag COAP_SOCKET_MULTICAST is set in the coap_socket_t structure while COAP_SOCKET_CONNECTED is cleared. The flag COAP_SOCKET_CONNECTED is now set by coap_socket_connect_udp() and coap_socket_connect_tcp1(). coap_read_session() now allows reading from sockets that have either the flag COAP_SOCKET_CONNECTED or COAP_SOCKET_MULTICAST set. --- include/coap/coap_io.h | 1 + src/coap_io.c | 15 +++++++++++++++ src/coap_session.c | 2 +- src/net.c | 2 +- 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/include/coap/coap_io.h b/include/coap/coap_io.h index 4edd80f3..7348c8fe 100644 --- a/include/coap/coap_io.h +++ b/include/coap/coap_io.h @@ -63,6 +63,7 @@ typedef struct coap_socket_t { #define COAP_SOCKET_CAN_WRITE 0x0200 /**< non blocking socket can now write without blocking */ #define COAP_SOCKET_CAN_ACCEPT 0x0400 /**< non blocking server socket can now accept without blocking */ #define COAP_SOCKET_CAN_CONNECT 0x0800 /**< non blocking client socket can now connect without blocking */ +#define COAP_SOCKET_MULTICAST 0x1000 /**< socket is used for multicast communication */ struct coap_endpoint_t *coap_malloc_endpoint( void ); void coap_mfree_endpoint( struct coap_endpoint_t *ep ); diff --git a/src/coap_io.c b/src/coap_io.c index 9ff02e35..97971d25 100644 --- a/src/coap_io.c +++ b/src/coap_io.c @@ -261,6 +261,7 @@ coap_socket_connect_tcp1(coap_socket_t *sock, coap_address_t connect_addr; coap_address_copy( &connect_addr, server ); + sock->flags &= ~COAP_SOCKET_CONNECTED; sock->fd = socket(server->addr.sa.sa_family, SOCK_STREAM, 0); if (sock->fd == COAP_INVALID_SOCKET) { @@ -326,6 +327,7 @@ coap_socket_connect_tcp1(coap_socket_t *sock, coap_log(LOG_WARNING, "coap_socket_connect_tcp1: getpeername: %s\n", coap_socket_strerror()); } + sock->flags |= COAP_SOCKET_CONNECTED; return 1; error: @@ -480,8 +482,10 @@ coap_socket_connect_udp(coap_socket_t *sock, u_long u_on = 1; #endif coap_address_t connect_addr; + int is_mcast = coap_is_mcast(server); coap_address_copy(&connect_addr, server); + sock->flags &= ~(COAP_SOCKET_CONNECTED | COAP_SOCKET_MULTICAST); sock->fd = socket(connect_addr.addr.sa.sa_family, SOCK_DGRAM, 0); if (sock->fd == COAP_INVALID_SOCKET) { @@ -523,6 +527,16 @@ coap_socket_connect_udp(coap_socket_t *sock, } } + /* special treatment for sockets that are used for multicast communication */ + if (is_mcast) { + if (getsockname(sock->fd, &local_addr->addr.sa, &local_addr->size) == COAP_SOCKET_ERROR) { + coap_log(LOG_WARNING, "coap_socket_connect_udp: getsockname for multicast socket: %s\n", coap_socket_strerror()); + } + coap_address_copy(remote_addr, &connect_addr); + sock->flags |= COAP_SOCKET_MULTICAST; + return 1; + } + if (connect(sock->fd, &connect_addr.addr.sa, connect_addr.size) == COAP_SOCKET_ERROR) { coap_log(LOG_WARNING, "coap_socket_connect_udp: connect: %s\n", coap_socket_strerror()); goto error; @@ -536,6 +550,7 @@ coap_socket_connect_udp(coap_socket_t *sock, coap_log(LOG_WARNING, "coap_socket_connect_udp: getpeername: %s\n", coap_socket_strerror()); } + sock->flags |= COAP_SOCKET_CONNECTED; return 1; error: diff --git a/src/coap_session.c b/src/coap_session.c index 9c5bd4a8..258649db 100644 --- a/src/coap_session.c +++ b/src/coap_session.c @@ -463,7 +463,7 @@ coap_session_create_client( } } - session->sock.flags |= COAP_SOCKET_NOT_EMPTY | COAP_SOCKET_CONNECTED | COAP_SOCKET_WANT_READ; + session->sock.flags |= COAP_SOCKET_NOT_EMPTY | COAP_SOCKET_WANT_READ; if (local_if) session->sock.flags |= COAP_SOCKET_BOUND; LL_PREPEND(ctx->sessions, session); diff --git a/src/net.c b/src/net.c index f191d5bd..bd2fc11f 100644 --- a/src/net.c +++ b/src/net.c @@ -1099,7 +1099,7 @@ coap_read_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now) coap_packet_t *packet = &s_packet; #endif /* WITH_CONTIKI */ - assert(session->sock.flags & COAP_SOCKET_CONNECTED); + assert(session->sock.flags & (COAP_SOCKET_CONNECTED | COAP_SOCKET_MULTICAST)); if (COAP_PROTO_NOT_RELIABLE(session->proto)) { ssize_t bytes_read;