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.
This commit is contained in:
Olaf Bergmann committed 2018-02-28 15:44:32 +01:00
1 parent b6f6cb274c
commit 7872a80fb4
4 files changed
+18 -2

No files matched your search

+1
View File
@@ -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 );
+15
View File
@@ -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:
+1 -1
View File
@@ -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);
+1 -1
View File
@@ -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;