diff --git a/examples/client.c b/examples/client.c index f100143b..ed059854 100644 --- a/examples/client.c +++ b/examples/client.c @@ -24,8 +24,11 @@ #include #include "coap.h" +#include "coap_dtls.h" #include "coap_list.h" +void dtls_set_log_level(int); + int flags = 0; static unsigned char _token_data[8]; @@ -660,6 +663,11 @@ cmdline_content_type(char *arg, unsigned short key) { } } +static unsigned short +get_default_port(const coap_uri_t *u) { + return coap_uri_scheme_is_secure(u) ? COAPS_DEFAULT_PORT : COAP_DEFAULT_PORT; +} + static void cmdline_uri(char *arg) { unsigned char portbuf[2]; @@ -687,9 +695,9 @@ cmdline_uri(char *arg) { (unsigned char *)arg)); } else { /* split arg into Uri-* options */ - coap_split_uri((unsigned char *)arg, strlen(arg), &uri ); + coap_split_uri((unsigned char *)arg, strlen(arg), &uri ); - if (uri.port != COAP_DEFAULT_PORT) { + if (uri.port != get_default_port(&uri)) { coap_insert(&optlist, new_option_node(COAP_OPTION_URI_PORT, coap_encode_var_bytes(portbuf, uri.port), @@ -980,11 +988,19 @@ cmdline_method(char *arg) { } static coap_context_t * -get_context(const char *node, const char *port) { +get_context(const char *node, const char *port, int secure) { coap_context_t *ctx = NULL; int s; struct addrinfo hints; struct addrinfo *result, *rp; + int ep_type; + + ctx = coap_new_context(NULL); + if (!ctx) { + return NULL; + } + + ep_type = secure ? COAP_ENDPOINT_DTLS : COAP_ENDPOINT_NOSEC; memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ @@ -1000,23 +1016,29 @@ get_context(const char *node, const char *port) { /* iterate through results until success */ for (rp = result; rp != NULL; rp = rp->ai_next) { coap_address_t addr; + coap_endpoint_t *endpoint; if (rp->ai_addrlen <= sizeof(addr.addr)) { coap_address_init(&addr); addr.size = rp->ai_addrlen; memcpy(&addr.addr, rp->ai_addr, rp->ai_addrlen); - ctx = coap_new_context(&addr); - if (ctx) { - /* TODO: output address:port for successful binding */ + endpoint = coap_new_endpoint(&addr, ep_type); + if (endpoint) { + coap_attach_endpoint(ctx, endpoint); goto finish; + } else { + coap_log(LOG_CRIT, "cannot create endpoint\n"); + continue; } } } fprintf(stderr, "no context available for interface '%s'\n", node); + coap_free_context(ctx); + ctx = NULL; - finish: + finish: freeaddrinfo(result); return ctx; } @@ -1109,11 +1131,16 @@ main(int argc, char **argv) { } } + dtls_set_log_level(log_level); coap_set_log_level(log_level); - if ( optind < argc ) + if (optind < argc) { cmdline_uri( argv[optind] ); - else { + if (coap_uri_scheme_is_secure(&uri) && !coap_dtls_is_supported()) { + coap_log(LOG_EMERG, "coaps URI scheme not supported in this version of libcoap\n"); + exit(1); + } + } else { usage( argv[0], PACKAGE_VERSION ); exit( 1 ); } @@ -1144,13 +1171,15 @@ main(int argc, char **argv) { addrptr = &dst.addr.sin.sin_addr; /* create context for IPv4 */ - ctx = get_context(node_str[0] == 0 ? "0.0.0.0" : node_str, port_str); + ctx = get_context(node_str[0] == 0 ? "0.0.0.0" : node_str, port_str, + coap_uri_scheme_is_secure(&uri)); break; case AF_INET6: addrptr = &dst.addr.sin6.sin6_addr; /* create context for IPv6 */ - ctx = get_context(node_str[0] == 0 ? "::" : node_str, port_str); + ctx = get_context(node_str[0] == 0 ? "::" : node_str, port_str, + coap_uri_scheme_is_secure(&uri)); break; default: ; diff --git a/include/coap/mem.h b/include/coap/mem.h index fd3c69aa..e8a6d9b0 100644 --- a/include/coap/mem.h +++ b/include/coap/mem.h @@ -37,7 +37,8 @@ typedef enum { COAP_PDU, COAP_PDU_BUF, COAP_RESOURCE, - COAP_RESOURCEATTR + COAP_RESOURCEATTR, + COAP_DTLS_SESSION } coap_memory_tag_t; #ifndef WITH_LWIP diff --git a/include/coap/net.h b/include/coap/net.h index 6170620d..9bf18d82 100644 --- a/include/coap/net.h +++ b/include/coap/net.h @@ -21,6 +21,7 @@ #endif #include "coap_io.h" +#include "coap_dtls.h" #include "coap_time.h" #include "option.h" #include "pdu.h" @@ -124,6 +125,7 @@ typedef struct coap_context_t { ssize_t (*network_read)(coap_endpoint_t *ep, coap_packet_t **packet); + struct coap_dtls_context_t *dtls_context; } coap_context_t; /** diff --git a/src/net.c b/src/net.c index b4ae16c1..7fee81e3 100644 --- a/src/net.c +++ b/src/net.c @@ -30,6 +30,8 @@ #include #endif +#include "coap_dtls.h" + #ifdef WITH_LWIP #include #include @@ -358,11 +360,6 @@ coap_new_context( return NULL; #endif /* WITH_CONTIKI */ - if (!listen_addr) { - coap_log(LOG_EMERG, "no listen address specified\n"); - return NULL; - } - coap_clock_init(); #ifdef WITH_LWIP coap_prng_init(LWIP_RAND()); @@ -389,12 +386,19 @@ coap_new_context( memset(c, 0, sizeof( coap_context_t ) ); + c->dtls_context = coap_dtls_new_context(c); + if (c->dtls_context == NULL) { + coap_log(LOG_WARNING, "coap_init: no DTLS context available\n"); + } + /* initialize message id */ coap_prng((unsigned char *)&c->message_id, sizeof(unsigned short)); - coap_attach_endpoint(c, coap_new_endpoint(listen_addr, COAP_ENDPOINT_NOSEC)); - if (c->endpoint == NULL) { - goto onerror; + if (listen_addr) { + coap_attach_endpoint(c, coap_new_endpoint(listen_addr, COAP_ENDPOINT_NOSEC)); + if (c->endpoint == NULL) { + goto onerror; + } } #if defined(WITH_POSIX) || defined(WITH_CONTIKI) @@ -423,6 +427,7 @@ coap_new_context( void coap_free_context(coap_context_t *context) { + coap_endpoint_t *ep; if (!context) return; @@ -436,7 +441,12 @@ coap_free_context(coap_context_t *context) { coap_delete_all_resources(context); - coap_free_endpoint(context->endpoint); + LL_FOREACH(context->endpoint, ep) { + coap_free_endpoint(ep); + } + + coap_dtls_free_context(context->dtls_context); + #ifndef WITH_CONTIKI coap_free_type(COAP_CONTEXT, context); #endif/* not WITH_CONTIKI */ @@ -618,11 +628,43 @@ coap_send_impl(coap_context_t *context, } #endif /* WITH_LWIP */ +static int +is_dtls(const coap_endpoint_t *endpoint) { + return (endpoint->flags & COAP_ENDPOINT_DTLS) != 0; +} + coap_tid_t coap_send(coap_context_t *context, const coap_endpoint_t *local_interface, const coap_address_t *dst, coap_pdu_t *pdu) { + + /* FIXME: the following DTLS-specific case should probably go into + * coap_send_impl() with proper treatment of CON messages. */ + if (is_dtls(local_interface)) { + struct coap_dtls_session_t *session; + coap_tid_t id = COAP_INVALID_TID; + + session = coap_dtls_get_session(context, local_interface, dst); + if (session == NULL) { + coap_log(LOG_WARNING, "coap_send: no DTLS session available\n"); + return COAP_INVALID_TID; + } else { + coap_log(LOG_WARNING, "coap_send: trying DTLS\n"); + if (coap_dtls_send(context, session, + (unsigned char *)pdu->hdr, pdu->length) < 0) { + goto finish; + } else { + coap_transaction_id(dst, pdu, &id); + /* goto finish */ + } + } + finish: + coap_dtls_free_session(session); + return id; + } + + coap_log(LOG_WARNING, "coap_send: huh, no DTLS\n"); return coap_send_impl(context, local_interface, dst, pdu); } @@ -825,25 +867,44 @@ coap_retransmit(coap_context_t *context, coap_queue_t *node) { void coap_dispatch(coap_context_t *context, coap_queue_t *rcvd); +#ifdef WITH_LWIP int -coap_read( coap_context_t *ctx ) { +coap_read(coap_context_t *ctx) { + return -1; +} +#else /* WITH_LWIP */ +static int +coap_read_endpoint(coap_context_t *ctx, coap_endpoint_t *endpoint) { ssize_t bytes_read = -1; coap_packet_t *packet; - coap_address_t src; int result = -1; /* the value to be returned */ - coap_address_init(&src); + bytes_read = ctx->network_read(endpoint, &packet); -#if defined(WITH_POSIX) || defined(WITH_CONTIKI) - bytes_read = ctx->network_read(ctx->endpoint, &packet); -#endif /* WITH_POSIX or WITH_CONTIKI */ - - if ( bytes_read < 0 ) { + if (bytes_read < 0) { warn("coap_read: recvfrom"); } else { -#if defined(WITH_POSIX) || defined(WITH_CONTIKI) - result = coap_handle_message(ctx, packet); -#endif /* WITH_POSIX or WITH_CONTIKI */ + if (bytes_read > 0) { + if (is_dtls(endpoint)) { + unsigned char *data; + size_t data_len; + coap_address_t remote; + + coap_packet_get_memmapped(packet, &data, &data_len); + coap_packet_copy_source(packet, &remote); + + { + unsigned char buf[200]; + size_t s = coap_print_addr(&remote, buf, sizeof(buf)); + coap_log(LOG_DEBUG, "#### %.*s\n", s, buf); + } + result = coap_dtls_handle_message(ctx, endpoint, &remote, data, data_len); + } else { + result = coap_handle_message(ctx, packet); + } + } else { + debug("##### no data ######\n"); + } } coap_free_packet(packet); @@ -851,6 +912,22 @@ coap_read( coap_context_t *ctx ) { return result; } +int +coap_read(coap_context_t *ctx) { + coap_endpoint_t *ep, *tmp; + int result = -1; + + /* If we have more than one endpoint, the result does not really + * help with anything, so we set it simply to the last return value + * retrieved from coap_read_endpoint(). */ + LL_FOREACH_SAFE(ctx->endpoint, ep, tmp) { + result = coap_read_endpoint(ctx, ep); + } + + return result; +} +#endif /* WITH_LWIP */ + int coap_handle_message(coap_context_t *ctx, coap_packet_t *packet) { diff --git a/src/platform/posix/coap_io.c b/src/platform/posix/coap_io.c index b00644d5..28889439 100644 --- a/src/platform/posix/coap_io.c +++ b/src/platform/posix/coap_io.c @@ -32,6 +32,7 @@ #include "debug.h" #include "mem.h" +#include "coap_dtls.h" #include "coap_io.h" struct coap_packet_t { @@ -65,6 +66,11 @@ coap_new_endpoint(const coap_address_t *addr, int flags) { int on = 1; struct coap_endpoint_t *ep = NULL; + if (((flags & COAP_ENDPOINT_DTLS) != 0) && !coap_dtls_is_supported()) { + coap_log(LOG_CRIT, "coap_new_endpoint: DTLS not supported\n"); + return NULL; + } + sockfd = socket(addr->addr.sa.sa_family, SOCK_DGRAM, 0); if (sockfd < 0) { coap_log(LOG_WARNING, "coap_new_endpoint: socket"); diff --git a/src/platform/posix/run.c b/src/platform/posix/run.c index f86d155e..5c4d050b 100644 --- a/src/platform/posix/run.c +++ b/src/platform/posix/run.c @@ -69,9 +69,10 @@ coap_run_once(coap_context_t *ctx, unsigned int timeout_ms) { coap_tick_t past = now; LL_FOREACH(ctx->endpoint, ep) { if (FD_ISSET(ep->handle.fd, &readfds)) { - coap_read(ctx); /* read received data */ + ep->flags |= 0x1000; /* COAP_ENDPOINT_HAS_DATA */ } } + coap_read(ctx); /* read received data */ coap_ticks(&now); return ((now - past) * 1000) / COAP_TICKS_PER_SECOND; } else { /* timeout */