From ae13e5127bc4e34da8009c10bb13cf924adbbee7 Mon Sep 17 00:00:00 2001 From: jcmichelou Date: Mon, 19 Jun 2017 20:37:11 +0200 Subject: [PATCH] Compile with tinydtls and remove warning message with gcc. --- Makefile.am | 2 +- include/coap/coap_dtls.h | 2 +- include/coap/coap_io.h | 29 ++++++++------------- include/coap/net.h | 2 +- include/coap/session.h | 20 +++++++-------- src/coap_io.c | 6 ++--- src/coap_tinydtls.c | 55 ++++++++++++++++++++++++++++------------ src/session.c | 16 +++++++----- 8 files changed, 75 insertions(+), 57 deletions(-) diff --git a/Makefile.am b/Makefile.am index 2365f1b3..6adf9d4f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -52,7 +52,7 @@ libcoap_@LIBCOAP_API_VERSION@_la_SOURCES = \ src/async.c \ src/block.c \ src/coap_io.c \ - src/coap_dtls.c \ + src/coap_tinydtls.c \ src/coap_event.c \ src/coap_time.c \ src/debug.c \ diff --git a/include/coap/coap_dtls.h b/include/coap/coap_dtls.h index 6d388667..7e10749f 100644 --- a/include/coap/coap_dtls.h +++ b/include/coap/coap_dtls.h @@ -44,7 +44,7 @@ int coap_dtls_get_log_level(void); * @return A DTLS context object or NULL on error; */ void * -coap_dtls_new_context( coap_context_t *coap_context ); +coap_dtls_new_context( struct coap_context_t *coap_context ); /** Releases the storage allocated for @p dtls_context. */ void coap_dtls_free_context( void *dtls_context ); diff --git a/include/coap/coap_io.h b/include/coap/coap_io.h index 1199d48a..a4879aff 100644 --- a/include/coap/coap_io.h +++ b/include/coap/coap_io.h @@ -28,16 +28,8 @@ typedef int coap_fd_t; #endif struct coap_packet_t; -typedef struct coap_packet_t coap_packet_t; - struct coap_session_t; -typedef struct coap_session_t coap_session_t; - -struct coap_context_t; -typedef struct coap_context_t coap_context_t; - struct coap_pdu_t; -typedef struct coap_pdu_t coap_pdu_t; typedef uint16_t coap_socket_flags_t; @@ -83,17 +75,17 @@ coap_socket_bind_udp( coap_socket_t *sock, void coap_socket_close( coap_socket_t *sock ); ssize_t -coap_socket_send( coap_socket_t *sock, coap_session_t *session, +coap_socket_send( coap_socket_t *sock, struct coap_session_t *session, const uint8_t *data, size_t data_len ); #ifdef WITH_LWIP ssize_t -coap_socket_send_pdu( coap_socket_t *sock, coap_session_t *session, - coap_pdu_t *pdu ); +coap_socket_send_pdu( coap_socket_t *sock, struct coap_session_t *session, + struct coap_pdu_t *pdu ); #endif -const char *coap_socket_strerror(); +const char *coap_socket_strerror( void ); /** * Function interface for data transmission. This function returns the number of @@ -107,7 +99,7 @@ const char *coap_socket_strerror(); * @return The number of bytes written on success, or a value * less than zero on error. */ -ssize_t coap_network_send( coap_socket_t *sock, const coap_session_t *session, const uint8_t *data, size_t datalen ); +ssize_t coap_network_send( coap_socket_t *sock, const struct coap_session_t *session, const uint8_t *data, size_t datalen ); /** * Function interface for reading data. This function returns the number of @@ -122,7 +114,7 @@ ssize_t coap_network_send( coap_socket_t *sock, const coap_session_t *session, c * @return The number of bytes received on success, or a value less than * zero on error. */ -ssize_t coap_network_read( coap_socket_t *sock, coap_packet_t **packet ); +ssize_t coap_network_read( coap_socket_t *sock, struct coap_packet_t **packet ); #ifndef coap_mcast_interface # define coap_mcast_interface(Local) 0 @@ -131,17 +123,17 @@ ssize_t coap_network_read( coap_socket_t *sock, coap_packet_t **packet ); /** * Releases the storage allocated for @p packet. */ -void coap_free_packet(coap_packet_t *packet); +void coap_free_packet(struct coap_packet_t *packet); /** * Given a packet, set msg and msg_len to an address and length of the packet's * data in memory. * */ -void coap_packet_get_memmapped(coap_packet_t *packet, +void coap_packet_get_memmapped(struct coap_packet_t *packet, unsigned char **address, size_t *length); -void coap_packet_set_addr( coap_packet_t *packet, const coap_address_t *src, +void coap_packet_set_addr( struct coap_packet_t *packet, const coap_address_t *src, const coap_address_t *dst ); #ifdef WITH_LWIP @@ -149,7 +141,7 @@ void coap_packet_set_addr( coap_packet_t *packet, const coap_address_t *src, * Get the pbuf of a packet. The caller takes over responsibility for freeing * the pbuf. */ -struct pbuf *coap_packet_extract_pbuf(coap_packet_t *packet); +struct pbuf *coap_packet_extract_pbuf(struct coap_packet_t *packet); #endif #if defined(WITH_LWIP) @@ -175,5 +167,6 @@ struct coap_packet_t { unsigned char payload[]; /**< payload */ }; #endif +typedef struct coap_packet_t coap_packet_t; #endif /* _COAP_IO_H_ */ diff --git a/include/coap/net.h b/include/coap/net.h index e463d72c..7aa5fba4 100644 --- a/include/coap/net.h +++ b/include/coap/net.h @@ -129,7 +129,7 @@ typedef struct coap_context_t { ssize_t (*network_send)(coap_socket_t *sock, const coap_session_t *session, const uint8_t *data, size_t datalen); - ssize_t (*network_read)(coap_socket_t *sock, coap_packet_t **packet); + ssize_t (*network_read)(coap_socket_t *sock, struct coap_packet_t **packet); unsigned (*get_client_psk)( const coap_session_t *session, const char *hint, char *identity, unsigned max_identity_len, uint8_t *psk, unsigned max_psk_len ); unsigned (*get_server_psk)( const coap_session_t *session, const char *identity, uint8_t *psk, unsigned max_psk_len ); diff --git a/include/coap/session.h b/include/coap/session.h index b577dd1e..e06eee52 100644 --- a/include/coap/session.h +++ b/include/coap/session.h @@ -14,7 +14,7 @@ #include "pdu.h" struct coap_endpoint_t; -typedef struct coap_endpoint_t coap_endpoint_t; +struct coap_contex_t; typedef uint8_t coap_proto_t; /** @@ -57,8 +57,8 @@ typedef struct coap_session_t { coap_address_t remote_addr; /**< remote address and port */ int ifindex; /**< interface index */ coap_socket_t sock; /**< socket object for the session, if any */ - coap_endpoint_t *endpoint; /**< session's endpoint */ - coap_context_t *context; /**< session's context */ + struct coap_endpoint_t *endpoint; /**< session's endpoint */ + struct coap_context_t *context; /**< session's context */ void *tls; /**< security parameters */ coap_pdu_queue_t *sendqueue; /**< list of messages waiting to be sent */ char *psk_identity; @@ -107,7 +107,7 @@ void coap_session_connected( coap_session_t *session ); * @return A new CoAP session or NULL if failed. Call coap_session_release to free. */ coap_session_t *coap_new_client_session( - coap_context_t *ctx, + struct coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto @@ -127,7 +127,7 @@ coap_session_t *coap_new_client_session( * @return A new CoAP session or NULL if failed. Call coap_session_release to free. */ coap_session_t *coap_new_client_session_psk( - coap_context_t *ctx, + struct coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, @@ -167,7 +167,7 @@ coap_session_delay_pdu( coap_session_t *session, coap_pdu_t *pdu, int retransmit */ typedef struct coap_endpoint_t { struct coap_endpoint_t *next; - coap_context_t *context; /**< endpoint's context */ + struct coap_context_t *context; /**< endpoint's context */ coap_proto_t proto; /**< protocol used on this interface */ coap_socket_t sock; /**< socket object for the interface, if any */ coap_address_t bind_addr; /**< local interface address */ @@ -183,7 +183,7 @@ typedef struct coap_endpoint_t { * @param proto Protocol used on this endpoint */ -coap_endpoint_t *coap_new_endpoint( coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto ); +coap_endpoint_t *coap_new_endpoint( struct coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto ); void coap_free_endpoint( coap_endpoint_t *ep ); @@ -203,10 +203,10 @@ const char *coap_endpoint_str( const coap_endpoint_t *endpoint ); * @param packet Received packet. * @return The CoAP session. */ -coap_session_t *coap_endpoint_get_session( coap_endpoint_t *endpoint, const coap_packet_t *packet ); +coap_session_t *coap_endpoint_get_session( coap_endpoint_t *endpoint, const struct coap_packet_t *packet ); -coap_session_t *coap_endpoint_new_dtls_session( coap_endpoint_t *endpoint, const coap_packet_t *packet ); +coap_session_t *coap_endpoint_new_dtls_session( coap_endpoint_t *endpoint, const struct coap_packet_t *packet ); -coap_session_t *coap_session_get_by_peer( coap_context_t *ctx, const coap_address_t *remote_addr, int ifindex ); +coap_session_t *coap_session_get_by_peer( struct coap_context_t *ctx, const struct coap_address_t *remote_addr, int ifindex ); #endif /* _SESSION_H */ diff --git a/src/coap_io.c b/src/coap_io.c index 2c9d25d6..fcc58d88 100644 --- a/src/coap_io.c +++ b/src/coap_io.c @@ -553,7 +553,7 @@ coap_network_read(coap_socket_t *sock, coap_packet_t **packet) { if ( r == 0 ) len = (ssize_t)dwNumberOfBytesRecvd; #else - len = recvmsg( socket->fd, &mhdr, 0 ); + len = recvmsg( sock->fd, &mhdr, 0 ); #endif if ( len < 0 ) { @@ -782,14 +782,14 @@ int coap_run_once( coap_context_t *ctx, unsigned int timeout_ms ) { #endif #ifdef _WIN32 -const char *coap_socket_strerror() { +const char *coap_socket_strerror( void ) { static char szError[256]; if ( FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, (DWORD)WSAGetLastError(), MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), (LPSTR)szError, ( DWORD )sizeof( szError ), NULL ) == 0 ) strcpy( szError, "Unknown error" ); return szError; } #else -const char *coap_socket_strerrror() { +const char *coap_socket_strerror( void ) { return strerror( errno ); } #endif diff --git a/src/coap_tinydtls.c b/src/coap_tinydtls.c index 4b5745fc..768594e8 100644 --- a/src/coap_tinydtls.c +++ b/src/coap_tinydtls.c @@ -170,26 +170,26 @@ get_psk_info(struct dtls_context_t *dtls_context, fatal_error = DTLS_ALERT_CLOSE_NOTIFY; goto error; } - length = strlen( result ); + length = strlen( (const char *)result ); client = 1; return (int)length; case DTLS_PSK_KEY: if ( coap_context->get_server_psk ) { if ( id_len + 1 > sizeof( buf ) ) { - coap_log( LOG_WARNING, "cannot set psk -- identity too big\n" ); - goto error; + coap_log( LOG_WARNING, "cannot set psk -- identity too big\n" ); + goto error; } memcpy( buf, id, id_len ); buf[id_len] = 0; - return (int)coap_context->get_server_psk( coap_session, buf, result, (unsigned)result_length ) + return (int)coap_context->get_server_psk( coap_session, buf, result, (unsigned)result_length ); } case DTLS_PSK_HINT: client = 0; if ( coap_context->get_server_hint ) { result[0] = 0; - if ( coap_context->get_server_hint( coap_session, result, result_length ) ) - length = strlen( result ); + if ( coap_context->get_server_hint( coap_session, (char *)result, result_length ) ) + length = strlen( (const char *)result ); } else { length = 0; } @@ -214,8 +214,8 @@ static dtls_handler_t cb = { #endif }; -struct coap_dtls_context_t * -coap_dtls_new_context( coap_context_t *coap_context ) { +void * +coap_dtls_new_context( struct coap_context_t *coap_context ) { struct dtls_context_t *dtls_context = dtls_new_context( coap_context ); if (!dtls_context) goto error; @@ -241,9 +241,9 @@ coap_dtls_new_session( coap_session_t *session ) { if ( dtls_session ) { /* create tinydtls session object from remote address and local * endpoint handle */ - dtls_session_init( &dtls_session ); - coap_copy_address( (coap_address_t*)&dtls_session, &session->remote_addr ); - dtls_session.ifindex = session->ifindex; + dtls_session_init( dtls_session ); + coap_address_copy( (coap_address_t*)dtls_session, &session->remote_addr ); + dtls_session->ifindex = session->ifindex; debug( "*** new session %p\n", dtls_session ); } @@ -261,17 +261,17 @@ void *coap_dtls_new_client_session( coap_session_t *session ) { return NULL; peer = dtls_get_peer( (struct dtls_context_t *)session->context->dtls_context, - &dtls_session ); + dtls_session ); if ( !peer ) { /* The peer connection does not yet exist. */ /* dtls_connect() returns a value greater than zero if a new * connection attempt is made, 0 for session reuse. */ if ( dtls_connect( (struct dtls_context_t *)session->context->dtls_context, - &dtls_session ) >= 0 ) { + dtls_session ) >= 0 ) { peer = - dtls_get_peer( (struct dtls_context_t *)coap_context->dtls_context, - &dtls_session ); + dtls_get_peer( (struct dtls_context_t *)session->context->dtls_context, + dtls_session ); } } @@ -302,7 +302,7 @@ coap_dtls_send( coap_session_t *session, coap_log(LOG_DEBUG, "call dtls_write\n"); res = dtls_write((struct dtls_context_t *)session->context->dtls_context, - (session_t)session->tls, (uint8 *)data, data_len); + (session_t *)session->tls, (uint8 *)data, data_len); if (res < 0) coap_log(LOG_WARNING, "coap_dtls_send: cannot send PDU\n"); @@ -322,4 +322,27 @@ coap_dtls_receive( coap_session_t *session, return res; } +int +coap_dtls_hello( coap_session_t *session, + const uint8_t *data, + size_t data_len +) { + session_t dtls_session; + struct dtls_context_t *dtls_context = + (struct dtls_context_t *)session->context->dtls_context; + + dtls_session_init( &dtls_session ); + coap_address_copy( (coap_address_t*)&dtls_session, &session->remote_addr ); + dtls_session.ifindex = session->ifindex; + int res = dtls_handle_message( dtls_context, &dtls_session, + (uint8 *)data, (int)data_len); + if ( res >= 0 ) { + if ( dtls_get_peer( dtls_context, &dtls_session ) ) + res = 1; + else + res = 0; + } + return res; +} + #endif /* HAVE_LIBTINYDTLS */ diff --git a/src/session.c b/src/session.c index 65a5ca1b..363c30b7 100644 --- a/src/session.c +++ b/src/session.c @@ -72,8 +72,8 @@ coap_free_session( coap_session_t *session ) { return; if ( session->proto == COAP_PROTO_DTLS ) coap_dtls_free_session( session ); - if ( session->sock.flags != COAP_SOCKET_EMPTY && session->sock.fd != COAP_INVALID_SOCKET ) - coap_closesocket( session->sock.fd ); + if ( session->sock.flags != COAP_SOCKET_EMPTY ) + coap_socket_close( &session->sock ); if ( session->endpoint ) { if ( session->endpoint->sessions ) LL_DELETE( session->endpoint->sessions, session ); @@ -107,7 +107,7 @@ ssize_t coap_session_send( coap_session_t *session, const uint8_t *data, size_t } bytes_written = coap_socket_send( sock, session, data, datalen ); - if ( bytes_written == datalen ) + if ( bytes_written == (ssize_t)datalen ) debug( "* %s: sent %zd bytes\n", coap_session_str( session ), datalen ); else debug( "* %s: failed to send %zd bytes\n", coap_session_str( session ), datalen ); @@ -171,6 +171,9 @@ void coap_session_disconnected( coap_session_t *session ) { coap_session_t * coap_endpoint_get_session( coap_endpoint_t *endpoint, const coap_packet_t *packet ) { coap_session_t *session = NULL; + + endpoint->hello.ifindex = -1; + LL_FOREACH( endpoint->sessions, session ) { if ( session->ifindex == packet->ifindex && coap_address_equals( &session->local_addr, &packet->dst ) && coap_address_equals( &session->remote_addr, &packet->src ) ) return session; @@ -221,7 +224,6 @@ coap_session_create_client( coap_proto_t proto ) { coap_session_t *session = NULL; - int on = 1, off = 0; assert( server ); assert( proto != COAP_PROTO_NONE ); @@ -268,7 +270,7 @@ coap_session_connect( coap_session_t *session ) { } coap_session_t *coap_new_client_session( - coap_context_t *ctx, + struct coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto @@ -282,13 +284,13 @@ coap_session_t *coap_new_client_session( } coap_session_t *coap_new_client_session_psk( - coap_context_t *ctx, + struct coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, const char *identity, const uint8_t *key, - size_t key_len + unsigned key_len ) { coap_session_t *session = coap_session_create_client( ctx, local_if, server, proto );