diff --git a/examples/coap-client.c b/examples/coap-client.c index d87fac81..969233d9 100644 --- a/examples/coap-client.c +++ b/examples/coap-client.c @@ -1509,41 +1509,15 @@ get_session(coap_context_t *ctx, const char *local_addr, const char *local_port, coap_uri_scheme_t scheme, + coap_proto_t proto, coap_address_t *dst, const uint8_t *identity, size_t identity_len, const uint8_t *key, size_t key_len) { coap_session_t *session = NULL; - coap_proto_t proto; is_mcast = coap_is_mcast(dst); - switch(scheme) { - case COAP_URI_SCHEME_COAP: - proto = COAP_PROTO_UDP; - break; - case COAP_URI_SCHEME_COAPS: - proto = COAP_PROTO_DTLS; - break; - case COAP_URI_SCHEME_COAP_TCP: - proto = COAP_PROTO_TCP; - break; - case COAP_URI_SCHEME_COAPS_TCP: - proto = COAP_PROTO_TLS; - break; - case COAP_URI_SCHEME_COAP_WS: - proto = COAP_PROTO_WS; - break; - case COAP_URI_SCHEME_COAPS_WS: - proto = COAP_PROTO_WSS; - break; - case COAP_URI_SCHEME_LAST: - default: - case COAP_URI_SCHEME_HTTP: - case COAP_URI_SCHEME_HTTPS: - coap_log_err("http:// or https:// not supported\n"); - return NULL; - } if (local_addr || dst->addr.sa.sa_family == AF_UNIX) { if (dst->addr.sa.sa_family == AF_UNIX) { coap_address_t bind_addr; @@ -1634,6 +1608,7 @@ main(int argc, char **argv) { int create_uri_opts = 1; size_t i; coap_uri_scheme_t scheme; + coap_proto_t proto; uint32_t repeat_ms = REPEAT_DELAY_MS; uint8_t *data = NULL; size_t data_len = 0; @@ -1853,6 +1828,7 @@ main(int argc, char **argv) { coap_log_err("failed to resolve address\n"); goto failed; } + proto = info_list->proto; memcpy(&dst, &info_list->addr, sizeof(dst)); coap_free_address_info(info_list); @@ -1881,6 +1857,7 @@ main(int argc, char **argv) { node_str[0] ? node_str : NULL, port_str[0] ? port_str : NULL, scheme, + proto, &dst, user_length >= 0 ? user : NULL, user_length >= 0 ? user_length : 0, diff --git a/examples/coap-rd.c b/examples/coap-rd.c index 9c9644f1..4103ab9f 100644 --- a/examples/coap-rd.c +++ b/examples/coap-rd.c @@ -60,6 +60,9 @@ static ssize_t key_length = 0; static int key_defined = 0; static const char *hint = "CoAP"; static size_t extended_token_size = COAP_TOKEN_DEFAULT_MAX; +static int enable_ws = 0; +static int ws_port = 80; +static int wss_port = 443; #ifndef min #define min(a,b) ((a) < (b) ? (a) : (b)) @@ -84,6 +87,22 @@ cmdline_read_key(char *arg, unsigned char *buf, size_t maxlen) { return -1; } +static int +cmdline_ws(char *arg) { + char *cp = strchr(arg, ','); + + if (cp) { + if (cp != arg) + ws_port = atoi(arg); + cp++; + if (*cp != '\000') + wss_port = atoi(cp); + } else { + ws_port = atoi(arg); + } + return 1; +} + static inline rd_t * rd_new(void) { rd_t *rd; @@ -584,7 +603,7 @@ usage(const char *program, const char *version) { fprintf(stderr, "%s\n", coap_string_tls_support(buffer, sizeof(buffer))); fprintf(stderr, "\n" "Usage: %s [-g group] [-G group_if] [-p port] [-v num] [-A address]\n" - "\t [-T max_token_size] [-V num]\n" + "\t [-w [port][,secure_port]] [-T max_token_size] [-V num]\n" "\t [[-h hint] [-k key]]\n" "\t [[-c certfile] [-C cafile] [-n] [-R trust_casfile]]\n" "General Options\n" @@ -596,6 +615,9 @@ usage(const char *program, const char *version) { "\t-p port\t\tListen on specified port\n" "\t-v num \t\tVerbosity level (default 4, maximum is 8) for general\n" "\t \t\tCoAP logging\n" + "\t-w [port][,secure_port]\n" + "\t \t\tEnable WebSockets support on port (WS) and/or secure_port\n" + "\t \t\t(WSS), comma separated\n" "\t-A address\tInterface address to bind to\n" "\t-T max_token_length\tSet the maximum token length (8-65804)\n" "\t-V num \t\tVerbosity level (default 3, maximum is 7) for (D)TLS\n" @@ -701,79 +723,54 @@ fill_keystore(coap_context_t *ctx) { static coap_context_t * get_context(const char *node, const char *port) { coap_context_t *ctx = NULL; - int s; - struct addrinfo hints; - struct addrinfo *result, *rp; + coap_addr_info_t *info = NULL; + coap_addr_info_t *info_list = NULL; + coap_str_const_t local; + int have_ep = 0; + uint16_t u_s_port = 0; + uint16_t s_port = 0; + int scheme_hint_bits = 0; ctx = coap_new_context(NULL); if (!ctx) { return NULL; } - /* Need PSK set up before we set up (D)TLS endpoints */ + + /* Need PKI/RPK/PSK set up before we set up (D)TLS endpoints */ fill_keystore(ctx); - memset(&hints, 0, sizeof(struct addrinfo)); - hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ - hints.ai_socktype = SOCK_DGRAM; /* Coap uses UDP */ - hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; + if (node) { + local.s = (const uint8_t *)node; + local.length = strlen(node); + } - s = getaddrinfo(node, port, &hints, &result); - if ( s != 0 ) { - fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s)); + if (port) { + u_s_port = atoi(port); + s_port = u_s_port + 1; + } + scheme_hint_bits = + coap_get_available_scheme_hint_bits(cert_file != NULL || key_defined != 0, + enable_ws, COAP_PROTO_NONE); + info_list = coap_resolve_address_info(node ? &local : NULL, u_s_port, s_port, + AI_PASSIVE | AI_NUMERICHOST, + scheme_hint_bits); + for (info = info_list; info != NULL; info = info->next) { + coap_endpoint_t *ep; + + ep = coap_new_endpoint(ctx, &info->addr, info->proto); + if (!ep) { + coap_log_warn("cannot create endpoint for proto %u\n", + info->proto); + } else { + have_ep = 1; + } + } + coap_free_address_info(info_list); + if (!have_ep) { + coap_log_err("No context available for interface '%s'\n", node); coap_free_context(ctx); return NULL; } - - /* iterate through results until success */ - for (rp = result; rp != NULL; rp = rp->ai_next) { - coap_address_t addr, addrs; - coap_endpoint_t *ep_udp = NULL, *ep_dtls = NULL, *ep_tcp = NULL, *ep_tls = NULL; - - if (rp->ai_addrlen <= (socklen_t)sizeof(addr.addr)) { - coap_address_init(&addr); - addr.size = (socklen_t)rp->ai_addrlen; - memcpy(&addr.addr, rp->ai_addr, rp->ai_addrlen); - addrs = addr; - if (addr.addr.sa.sa_family == AF_INET) { - uint16_t temp = ntohs(addr.addr.sin.sin_port) + 1; - addrs.addr.sin.sin_port = htons(temp); - } else if (addr.addr.sa.sa_family == AF_INET6) { - uint16_t temp = ntohs(addr.addr.sin6.sin6_port) + 1; - addrs.addr.sin6.sin6_port = htons(temp); - } else { - goto finish; - } - - ep_udp = coap_new_endpoint(ctx, &addr, COAP_PROTO_UDP); - if (ep_udp) { - if (coap_dtls_is_supported() && (key_defined || cert_file)) { - ep_dtls = coap_new_endpoint(ctx, &addrs, COAP_PROTO_DTLS); - if (!ep_dtls) - coap_log_crit("cannot create DTLS endpoint\n"); - } - } else { - coap_log_crit("cannot create UDP endpoint\n"); - continue; - } - ep_tcp = coap_new_endpoint(ctx, &addr, COAP_PROTO_TCP); - if (ep_tcp) { - if (coap_tls_is_supported() && (key_defined || cert_file)) { - ep_tls = coap_new_endpoint(ctx, &addrs, COAP_PROTO_TLS); - if (!ep_tls) - coap_log_crit("cannot create TLS endpoint\n"); - } - } else { - coap_log_crit("cannot create TCP endpoint\n"); - } - if (ep_udp) - goto finish; - } - } - - fprintf(stderr, "no context available for interface '%s'\n", node); - -finish: - freeaddrinfo(result); return ctx; } @@ -806,7 +803,7 @@ main(int argc, char **argv) { struct sigaction sa; #endif - while ((opt = getopt(argc, argv, "A:c:C:g:G:h:k:n:R:p:v:T:V:")) != -1) { + while ((opt = getopt(argc, argv, "A:c:C:g:G:h:k:n:R:p:v:T:V:w:")) != -1) { switch (opt) { case 'A' : strncpy(addr_str, optarg, NI_MAXHOST-1); @@ -860,6 +857,13 @@ main(int argc, char **argv) { case 'V': dtls_log_level = strtol(optarg, NULL, 10); break; + case 'w': + if (!coap_ws_is_supported() || !cmdline_ws(optarg)) { + fprintf(stderr, "WebSockets not enabled in libcoap\n"); + exit(1); + } + enable_ws = 1; + break; default: usage( argv[0], LIBCOAP_PACKAGE_VERSION ); exit( 1 ); diff --git a/examples/coap-server.c b/examples/coap-server.c index cf6917f2..166b0452 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -908,33 +908,6 @@ get_ongoing_proxy_session(coap_session_t *session, scheme = uri->scheme; } - switch(scheme) { - case COAP_URI_SCHEME_COAP: - proto = COAP_PROTO_UDP; - break; - case COAP_URI_SCHEME_COAPS: - proto = COAP_PROTO_DTLS; - break; - case COAP_URI_SCHEME_COAP_TCP: - proto = COAP_PROTO_TCP; - break; - case COAP_URI_SCHEME_COAPS_TCP: - proto = COAP_PROTO_TLS; - break; - case COAP_URI_SCHEME_COAP_WS: - proto = COAP_PROTO_WS; - break; - case COAP_URI_SCHEME_COAPS_WS: - proto = COAP_PROTO_WSS; - break; - case COAP_URI_SCHEME_LAST: - default: - case COAP_URI_SCHEME_HTTP: - case COAP_URI_SCHEME_HTTPS: - coap_log_err("http:// or https:// not supported\n"); - return NULL; - } - /* resolve destination address where data should be sent */ info_list = coap_resolve_address_info(&server, port, port, 0, @@ -945,6 +918,7 @@ get_ongoing_proxy_session(coap_session_t *session, remove_proxy_association(session, 0); return NULL; } + proto = info_list->proto; memcpy(&dst, &info_list->addr, sizeof(dst)); coap_free_address_info(info_list); @@ -2323,8 +2297,7 @@ get_context(const char *node, const char *port) { int have_ep = 0; uint16_t u_s_port = 0; uint16_t s_port = 0; - int scheme_hint_bits = 0; - coap_uri_scheme_t scheme; + uint32_t scheme_hint_bits = 0; ctx = coap_new_context(NULL); if (!ctx) { @@ -2343,102 +2316,26 @@ get_context(const char *node, const char *port) { u_s_port = atoi(port); s_port = u_s_port + 1; } - for (scheme = 0; scheme < COAP_URI_SCHEME_LAST; scheme++) { - switch (scheme) { - case COAP_URI_SCHEME_COAP: - scheme_hint_bits |= 1 << scheme; - break; - case COAP_URI_SCHEME_COAPS: - if (!coap_dtls_is_supported() || (cert_file == NULL && key_defined == 0)) - continue; - scheme_hint_bits |= 1 << scheme; - break; - case COAP_URI_SCHEME_COAP_TCP: - if (!coap_tcp_is_supported()) - continue; - scheme_hint_bits |= 1 << scheme; - break; - case COAP_URI_SCHEME_COAPS_TCP: - if (!coap_tls_is_supported() || (cert_file == NULL && key_defined == 0)) - continue; - scheme_hint_bits |= 1 << scheme; - break; - case COAP_URI_SCHEME_COAP_WS: - if (!enable_ws || !coap_ws_is_supported()) - continue; - scheme_hint_bits |= 1 << scheme; - break; - case COAP_URI_SCHEME_COAPS_WS: - if (!enable_ws || !coap_wss_is_supported() || (cert_file == NULL && key_defined == 0)) - continue; - scheme_hint_bits |= 1 << scheme; - break; - case COAP_URI_SCHEME_HTTP: - case COAP_URI_SCHEME_HTTPS: - case COAP_URI_SCHEME_LAST: - default: - continue; - } - } - - switch (use_unix_proto) { - /* For AF_UNIX, can only listen on a single endpoint */ - case COAP_PROTO_UDP: scheme_hint_bits = 1 << COAP_URI_SCHEME_COAP; break; - case COAP_PROTO_TCP: scheme_hint_bits = 1 << COAP_URI_SCHEME_COAP_TCP; break; - case COAP_PROTO_DTLS: scheme_hint_bits = 1 << COAP_URI_SCHEME_COAPS; break; - case COAP_PROTO_TLS: scheme_hint_bits = 1 << COAP_URI_SCHEME_COAPS_TCP; break; - case COAP_PROTO_WS: scheme_hint_bits = 1 << COAP_URI_SCHEME_COAP_WS; break; - case COAP_PROTO_WSS: scheme_hint_bits = 1 << COAP_URI_SCHEME_COAPS_WS; break; - case COAP_PROTO_NONE: /* If -U was not defined */ - case COAP_PROTO_LAST: - default: - break; - } - + scheme_hint_bits = + coap_get_available_scheme_hint_bits(cert_file != NULL || key_defined != 0, + enable_ws, use_unix_proto); info_list = coap_resolve_address_info(node ? &local : NULL, u_s_port, s_port, AI_PASSIVE | AI_NUMERICHOST, scheme_hint_bits); for (info = info_list; info != NULL; info = info->next) { coap_endpoint_t *ep; - coap_proto_t proto = COAP_PROTO_UDP; - switch (info->scheme) { - case COAP_URI_SCHEME_COAP: - proto = COAP_PROTO_UDP; - break; - case COAP_URI_SCHEME_COAPS: - proto = COAP_PROTO_DTLS; - break; - case COAP_URI_SCHEME_COAP_TCP: - proto = COAP_PROTO_TCP; - break; - case COAP_URI_SCHEME_COAPS_TCP: - proto = COAP_PROTO_TLS; - break; - case COAP_URI_SCHEME_COAP_WS: - proto = COAP_PROTO_WS; - coap_address_set_port(&info->addr, ws_port); - break; - case COAP_URI_SCHEME_COAPS_WS: - proto = COAP_PROTO_WSS; - coap_address_set_port(&info->addr, wss_port); - break; - case COAP_URI_SCHEME_HTTP: - case COAP_URI_SCHEME_HTTPS: - case COAP_URI_SCHEME_LAST: - default: - continue; - } - ep = coap_new_endpoint(ctx, &info->addr, proto); + ep = coap_new_endpoint(ctx, &info->addr, info->proto); if (!ep) { coap_log_warn("cannot create endpoint for proto %u\n", - proto); + info->proto); } else { have_ep = 1; } } coap_free_address_info(info_list); if (!have_ep) { + coap_log_err("No context available for interface '%s'\n", node); coap_free_context(ctx); return NULL; } diff --git a/examples/lwip/server-coap.c b/examples/lwip/server-coap.c index cf7f87ee..d9612883 100644 --- a/examples/lwip/server-coap.c +++ b/examples/lwip/server-coap.c @@ -117,11 +117,15 @@ init_coap_resources(coap_context_t *ctx) { void server_coap_init(coap_lwip_input_wait_handler_t input_wait, void *input_arg, int argc, char **argv) { - coap_address_t listenaddress; int opt; coap_log_t log_level = COAP_LOG_WARN; coap_log_t dtls_log_level = COAP_LOG_ERR; const char *use_psk = "secretPSK"; + uint32_t scheme_hint_bits = 0; + coap_addr_info_t *info = NULL; + coap_addr_info_t *info_list = NULL; + int have_ep = 0; + coap_str_const_t node; while ((opt = getopt(argc, argv, ":k:v:V:")) != -1) { switch (opt) { @@ -140,24 +144,16 @@ void server_coap_init(coap_lwip_input_wait_handler_t input_wait, } } - coap_address_init(&listenaddress); - - /* looks like a server address, but is used as end point for clients too */ - listenaddress.addr = *(IP_ANY_TYPE); - listenaddress.port = COAP_DEFAULT_PORT; - + coap_startup(); coap_set_log_level(log_level); coap_dtls_set_log_level(dtls_log_level); - main_coap_context = coap_new_context(&listenaddress); + + main_coap_context = coap_new_context(NULL); LWIP_ASSERT("Failed to initialize context", main_coap_context != NULL); if (coap_dtls_is_supported()) { coap_dtls_spsk_t setup_data; - coap_endpoint_t *ep; - listenaddress.port = COAP_DEFAULT_PORT + 1; - ep = coap_new_endpoint(main_coap_context, &listenaddress, COAP_PROTO_DTLS); - LWIP_ASSERT("Failed to initialize dtls endpoint", ep != NULL); memset(&setup_data, 0, sizeof(setup_data)); setup_data.version = COAP_DTLS_SPSK_SETUP_VERSION; setup_data.psk_info.key.s = (const uint8_t *)use_psk; @@ -165,6 +161,28 @@ void server_coap_init(coap_lwip_input_wait_handler_t input_wait, coap_context_set_psk2(main_coap_context, &setup_data); } + node.s = (const uint8_t*)"::"; + node.length = 2; + scheme_hint_bits = + coap_get_available_scheme_hint_bits(use_psk[0], + 0, COAP_PROTO_NONE); + info_list = coap_resolve_address_info(&node, 0, 0, + 0, + scheme_hint_bits); + for (info = info_list; info != NULL; info = info->next) { + coap_endpoint_t *ep; + + ep = coap_new_endpoint(main_coap_context, &info->addr, info->proto); + if (!ep) { + coap_log_warn("cannot create endpoint for proto %u\n", + info->proto); + } else { + have_ep = 1; + } + } + coap_free_address_info(info_list); + LWIP_ASSERT("Failed to initialize context", have_ep != 0); + /* Limit the number of idle sessions to save RAM (MEMP_NUM_COAPSESSION) */ LWIP_ASSERT("Need a minimum of 2 for MEMP_NUM_COAPSESSION", MEMP_NUM_COAPSESSION > 1); coap_context_set_max_idle_sessions(main_coap_context, MEMP_NUM_COAPSESSION -1); diff --git a/include/coap3/coap_address.h b/include/coap3/coap_address.h index ac3d5802..4b2101a4 100644 --- a/include/coap3/coap_address.h +++ b/include/coap3/coap_address.h @@ -155,9 +155,26 @@ _coap_address_isany_impl(const coap_address_t *a) { typedef struct coap_addr_info_t { struct coap_addr_info_t *next; /**< Next entry in the chain */ coap_uri_scheme_t scheme; /**< CoAP scheme to use */ + coap_proto_t proto; /**< CoAP protocol to use */ coap_address_t addr; /**< The address to connect / bind to */ } coap_addr_info_t; +/** + * Determine and set up scheme_hint_bits for a server that can be used in + * a call to coap_resolve_address_info(). + * + * @param have_pki_psk Set to @c 1 if PSK/PKI information is known else @c 0. + * @param ws_check Set to @c 1 is WebSockets is to be included in the list + * else @c 0. + * @param use_unix_proto Set to the appropriate protocol to use for Unix + * sockets, else set to COAP_PROTO_NONE for INET / INET6 + * sockets. + * @return A bit mask of the available CoAP protocols (can be @c 0 if none) + * suitable for passing to coap_resolve_address_info(). + */ +uint32_t coap_get_available_scheme_hint_bits(int have_pki_psk, int ws_check, + coap_proto_t use_unix_proto); + /** * Resolve the specified @p server into a set of coap_address_t that can * be used to bind() or connect() to. diff --git a/libcoap-3.map b/libcoap-3.map index 30a7438a..c5f513ee 100644 --- a/libcoap-3.map +++ b/libcoap-3.map @@ -95,6 +95,7 @@ global: coap_free_endpoint; coap_free_type; coap_get_app_data; + coap_get_available_scheme_hint_bits; coap_get_block; coap_get_block_b; coap_get_data; diff --git a/libcoap-3.sym b/libcoap-3.sym index ffb785ae..e3da0c42 100644 --- a/libcoap-3.sym +++ b/libcoap-3.sym @@ -93,6 +93,7 @@ coap_free_context coap_free_endpoint coap_free_type coap_get_app_data +coap_get_available_scheme_hint_bits coap_get_block coap_get_block_b coap_get_data diff --git a/man/coap-rd.txt.in b/man/coap-rd.txt.in index 3fdb3814..2b3d3cbd 100644 --- a/man/coap-rd.txt.in +++ b/man/coap-rd.txt.in @@ -19,8 +19,8 @@ coap-rd-notls SYNOPSIS -------- -*coap-rd* [*-g* group] [*-G* group_if] [*-p* port] [*-v* num] [*-A* address] - [*-T* max_token_size] [*-V* num] +*coap-rd* [*-g* group] [*-p* port] [*-v* num] [*-w* [port][,secure_port]] + [*-A* address] [*-G* group_if] [*-T* max_token_size] [*-V* num] [[*-h* hint] [*-k* key]] [[*-c* certfile] [*-n*] [*-C* cafile] [*-R* trusted_casfile]] @@ -40,10 +40,6 @@ OPTIONS Join specified multicast 'group' on startup. *Note:* DTLS over multicast is not currently supported. -*-G* group_if:: - Use this interface for listening for the multicast group. This can be - different from the implied interface if the *-A* option is used. - *-p* port:: The 'port' on the given address will be listening for incoming connections. If (D)TLS is supported, then 'port' + 1 will also be listened on for @@ -54,9 +50,17 @@ OPTIONS The verbosity level to use (default 4, maximum is 8) for general CoAP logging. +*-w* [port][,secure_port]:: + Enable WebSockets support support on port (WS) and/or secure_port (WSS), + comma separated. + *-A* address:: The local address of the interface which the server has to listen on. +*-G* group_if:: + Use this interface for listening for the multicast group. This can be + different from the implied interface if the *-A* option is used. + *-T* max_token_size:: Set the maximum token length (8-65804). diff --git a/man/coap_address.txt.in b/man/coap_address.txt.in index 2b724205..7a3eeb16 100644 --- a/man/coap_address.txt.in +++ b/man/coap_address.txt.in @@ -25,6 +25,9 @@ SYNOPSIS *void coap_address_init(coap_address_t *_addr_);* +*uint32_t coap_get_available_scheme_hint_bits(int _have_pki_psk_, +int _ws_check_, coap_proto_t _use_unix_proto_);* + *coap_addr_info_t *coap_resolve_address_info(const coap_str_const_t *_server_, uint16_t _port_, uint16_t _secure_port_, int _ai_hints_flags_, int _scheme_hint_bits_);* @@ -74,6 +77,7 @@ typedef struct coap_address_t { which is used in the *coap_addr_info_t* structure as returned by *coap_resolve_address_info()*. +---- *Structure coap_addr_info_t* @@ -83,6 +87,7 @@ which is used in the *coap_addr_info_t* structure as returned by typedef struct coap_addr_info_t { struct coap_addr_info_t *next; /* Next entry in the chain */ coap_uri_scheme_t scheme; /* CoAP scheme to use */ + coap_proto_t proto; /* CoAP protocol to use */ coap_address_t addr; /* The address to connect / bind to */ } coap_addr_info_t; ---- @@ -116,6 +121,16 @@ It is then the responsibility of the application to set the address family in addr.sa.sa_family and then fill in the the appropriate union structure based on the address family before the coap_address_t _addr_ is used. +*Function: coap_get_available_scheme_hint_bits()* + +The *coap_get_available_scheme_hint_bits*() function is used for servers to +determine what coap schemes are supported in the underlying libcoap library. +_have_pki_psk_ can be set to 1 to check for (D)DTLS support, else 0. +_ws_check_ can be set to 1 to check for WebSockets support, else 0. +_use_unix_proto_, if not set to COAP_PROTO_NONE, hints at the specific CoAP +protocol to use over a Unix socket. The output is suitable for input for the +*coap_address_resolve_info*()'s _scheme_hint_bits_. + *Function: coap_resolve_address_info()* The *coap_resolve_address_info*() function resolves the address _server_ into @@ -124,7 +139,8 @@ abstracted from _scheme_hint_bits_ either _port_ or _secure_port_ is used to update the addr variable of coap_addr_info_t. If _port_ (or _secure_port_) is 0, then the default port for the scheme is used. _ai_hints_flags_ is used for the internally called getaddrinfo(3) function. _scheme_hint_bits_ is a set of -one or more COAP_URI_SCHEME_*_BIT or'd together. +one or more COAP_URI_SCHEME_*_BIT or'd together. _scheme_hint_bits_ can also +(for servers) be the output from *coap_get_available_scheme_hint_bits*(). The returned set of coap_addr_info_t structures must be freed off by the caller using *coap_free_address_info*(). @@ -143,6 +159,9 @@ is copied into _addr_'s _sun_path_ translating any %2F encoding to /. RETURN VALUES ------------- +*coap_get_available_scheme_hint_bits*() returns a set of COAP_URI_SCHEME_*_BIT +or'd together based on the supported libcoap functionality. + *coap_resolve_address_info*() returns a linked list of addresses that can be used for session setup or NULL if there is a failure. diff --git a/src/coap_address.c b/src/coap_address.c index d8a9ffcf..77e14fdb 100644 --- a/src/coap_address.c +++ b/src/coap_address.c @@ -117,7 +117,7 @@ void coap_address_init(coap_address_t *addr) { int coap_address_set_unix_domain(coap_address_t *addr, const uint8_t *host, size_t host_len) { -#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI) +#if !defined(WITH_LWIP) size_t i; size_t ofs = 0; @@ -139,12 +139,12 @@ coap_address_set_unix_domain(coap_address_t *addr, else addr->addr.cun.sun_path[ofs-1] = '\000'; return 1; -#else /* defined(WITH_LWIP) || defined(WITH_CONTIKI) */ +#else /* WITH_LWIP */ (void)addr; (void)host; (void)host_len; return 0; -#endif /* defined(WITH_LWIP) || defined(WITH_CONTIKI) */ +#endif /* WITH_LWIP */ } static void @@ -167,6 +167,66 @@ update_port(coap_address_t *addr, uint16_t port, uint16_t default_port) { #include #endif +uint32_t +coap_get_available_scheme_hint_bits(int have_pki_psk, int ws_check, + coap_proto_t use_unix_proto) { + uint32_t scheme_hint_bits = 0; + coap_uri_scheme_t scheme; + + for (scheme = 0; scheme < COAP_URI_SCHEME_LAST; scheme++) { + switch (scheme) { + case COAP_URI_SCHEME_COAP: + scheme_hint_bits |= 1 << scheme; + break; + case COAP_URI_SCHEME_COAPS: + if (!(coap_dtls_is_supported() && have_pki_psk)) + continue; + scheme_hint_bits |= 1 << scheme; + break; + case COAP_URI_SCHEME_COAP_TCP: + if (!coap_tcp_is_supported()) + continue; + scheme_hint_bits |= 1 << scheme; + break; + case COAP_URI_SCHEME_COAPS_TCP: + if (!(coap_tls_is_supported() && have_pki_psk)) + continue; + scheme_hint_bits |= 1 << scheme; + break; + case COAP_URI_SCHEME_COAP_WS: + if (!ws_check || !coap_ws_is_supported()) + continue; + scheme_hint_bits |= 1 << scheme; + break; + case COAP_URI_SCHEME_COAPS_WS: + if (!ws_check || !(coap_wss_is_supported() && have_pki_psk)) + continue; + scheme_hint_bits |= 1 << scheme; + break; + case COAP_URI_SCHEME_HTTP: + case COAP_URI_SCHEME_HTTPS: + case COAP_URI_SCHEME_LAST: + default: + continue; + } + } + + switch (use_unix_proto) { + /* For AF_UNIX, can only listen on a single endpoint */ + case COAP_PROTO_UDP: scheme_hint_bits = 1 << COAP_URI_SCHEME_COAP; break; + case COAP_PROTO_TCP: scheme_hint_bits = 1 << COAP_URI_SCHEME_COAP_TCP; break; + case COAP_PROTO_DTLS: scheme_hint_bits = 1 << COAP_URI_SCHEME_COAPS; break; + case COAP_PROTO_TLS: scheme_hint_bits = 1 << COAP_URI_SCHEME_COAPS_TCP; break; + case COAP_PROTO_WS: scheme_hint_bits = 1 << COAP_URI_SCHEME_COAP_WS; break; + case COAP_PROTO_WSS: scheme_hint_bits = 1 << COAP_URI_SCHEME_COAPS_WS; break; + case COAP_PROTO_NONE: /* If use_unix_proto was not defined */ + case COAP_PROTO_LAST: + default: + break; + } + return scheme_hint_bits; +} + coap_addr_info_t * coap_resolve_address_info(const coap_str_const_t *server, uint16_t port, @@ -182,6 +242,7 @@ coap_resolve_address_info(const coap_str_const_t *server, coap_addr_info_t *info_prev = NULL; coap_addr_info_t *info_list = NULL; coap_uri_scheme_t scheme; + coap_proto_t proto; #if !defined(WITH_LWIP) && !defined(WITH_CONTIKI) if (server && coap_host_is_unix_domain(server)) { @@ -190,24 +251,71 @@ coap_resolve_address_info(const coap_str_const_t *server, coap_log_err("Unix Domain host too long\n"); return NULL; } + /* Need to chose the first defined one in scheme_hint_bits */ + for (scheme = 0; scheme < COAP_URI_SCHEME_LAST; scheme++) { + if (scheme_hint_bits & (1 << scheme)) { + break; + } + } + if (scheme == COAP_URI_SCHEME_LAST) { + return NULL; + } + switch (scheme) { + case COAP_URI_SCHEME_COAP: + proto = COAP_PROTO_UDP; + break; + case COAP_URI_SCHEME_COAPS: + if (!coap_dtls_is_supported()) + return NULL; + proto = COAP_PROTO_DTLS; + break; + case COAP_URI_SCHEME_COAP_TCP: + if (!coap_tcp_is_supported()) + return NULL; + proto = COAP_PROTO_TCP; + break; + case COAP_URI_SCHEME_COAPS_TCP: + if (!coap_tls_is_supported()) + return NULL; + proto = COAP_PROTO_TLS; + break; + case COAP_URI_SCHEME_HTTP: + if (!coap_tcp_is_supported()) + return NULL; + proto = COAP_PROTO_NONE; + break; + case COAP_URI_SCHEME_HTTPS: + if (!coap_tls_is_supported()) + return NULL; + proto = COAP_PROTO_NONE; + break; + case COAP_URI_SCHEME_COAP_WS: + if (!coap_ws_is_supported()) + return NULL; + proto = COAP_PROTO_WS; + break; + case COAP_URI_SCHEME_COAPS_WS: + if (!coap_wss_is_supported()) + return NULL; + proto = COAP_PROTO_WSS; + break; + case COAP_URI_SCHEME_LAST: + default: + return NULL; + } info = coap_malloc_type(COAP_STRING, sizeof(coap_addr_info_t)); if (info == NULL) return NULL; info->next = NULL; - - /* Need to chose the first defined one in scheme_hint_bits */ - for (scheme = 0; scheme < COAP_URI_SCHEME_LAST; scheme++) { - if (scheme_hint_bits & (1 << scheme)) { - info->scheme = scheme; - break; - } - } - if (scheme == COAP_URI_SCHEME_LAST) - return NULL; + info->proto = proto; + info->scheme = scheme; coap_address_init(&info->addr); - coap_address_set_unix_domain(&info->addr, server->s, - server->length); + if (!coap_address_set_unix_domain(&info->addr, server->s, + server->length)) { + coap_free_type(COAP_STRING, info); + return NULL; + } return info; } #endif /* ! WITH_LWIP && ! WITH_CONTIKI */ @@ -231,8 +339,10 @@ coap_resolve_address_info(const coap_str_const_t *server, } for (ainfo = res; ainfo != NULL; ainfo = ainfo->ai_next) { +#if !defined(WITH_LWIP) if (ainfo->ai_addrlen > (socklen_t)sizeof(info->addr.addr)) continue; +#endif /* ! WITH_LWIP */ switch (ainfo->ai_family) { case AF_INET6: @@ -241,6 +351,7 @@ coap_resolve_address_info(const coap_str_const_t *server, if (scheme_hint_bits & (1 << scheme)) { switch (scheme) { case COAP_URI_SCHEME_COAP: + proto = COAP_PROTO_UDP; #if !defined(__MINGW32__) if (ainfo->ai_socktype != SOCK_DGRAM) continue; @@ -249,6 +360,7 @@ coap_resolve_address_info(const coap_str_const_t *server, case COAP_URI_SCHEME_COAPS: if (!coap_dtls_is_supported()) continue; + proto = COAP_PROTO_DTLS; #if !defined(__MINGW32__) if (ainfo->ai_socktype != SOCK_DGRAM) continue; @@ -257,6 +369,7 @@ coap_resolve_address_info(const coap_str_const_t *server, case COAP_URI_SCHEME_COAP_TCP: if (!coap_tcp_is_supported()) continue; + proto = COAP_PROTO_TCP; #if !defined(__MINGW32__) if (ainfo->ai_socktype != SOCK_STREAM) continue; @@ -265,6 +378,7 @@ coap_resolve_address_info(const coap_str_const_t *server, case COAP_URI_SCHEME_COAPS_TCP: if (!coap_tls_is_supported()) continue; + proto = COAP_PROTO_TLS; #if !defined(__MINGW32__) if (ainfo->ai_socktype != SOCK_STREAM) continue; @@ -273,6 +387,7 @@ coap_resolve_address_info(const coap_str_const_t *server, case COAP_URI_SCHEME_HTTP: if (!coap_tcp_is_supported()) continue; + proto = COAP_PROTO_NONE; #if !defined(__MINGW32__) if (ainfo->ai_socktype != SOCK_STREAM) continue; @@ -281,6 +396,7 @@ coap_resolve_address_info(const coap_str_const_t *server, case COAP_URI_SCHEME_HTTPS: if (!coap_tls_is_supported()) continue; + proto = COAP_PROTO_NONE; #if !defined(__MINGW32__) if (ainfo->ai_socktype != SOCK_STREAM) continue; @@ -289,6 +405,7 @@ coap_resolve_address_info(const coap_str_const_t *server, case COAP_URI_SCHEME_COAP_WS: if (!coap_ws_is_supported()) continue; + proto = COAP_PROTO_WS; #if !defined(__MINGW32__) if (ainfo->ai_socktype != SOCK_STREAM) continue; @@ -297,6 +414,7 @@ coap_resolve_address_info(const coap_str_const_t *server, case COAP_URI_SCHEME_COAPS_WS: if (!coap_wss_is_supported()) continue; + proto = COAP_PROTO_WSS; #if !defined(__MINGW32__) if (ainfo->ai_socktype != SOCK_STREAM) continue; @@ -323,6 +441,7 @@ coap_resolve_address_info(const coap_str_const_t *server, } info->scheme = scheme; + info->proto = proto; coap_address_init(&info->addr); #if !defined(WITH_LWIP) info->addr.size = (socklen_t)ainfo->ai_addrlen; diff --git a/src/coap_io_lwip.c b/src/coap_io_lwip.c index 85085522..895e3ddd 100644 --- a/src/coap_io_lwip.c +++ b/src/coap_io_lwip.c @@ -332,18 +332,21 @@ coap_socket_bind_udp(coap_socket_t *sock, const coap_address_t *listen_addr, coap_address_t *bound_addr) { int err; + coap_address_t l_listen = *listen_addr; sock->pcb = udp_new_ip_type(IPADDR_TYPE_ANY); if (sock->pcb == NULL) return 0; + if (l_listen.addr.type == IPADDR_TYPE_V6) + l_listen.addr.type = IPADDR_TYPE_ANY; udp_recv(sock->pcb, coap_recvs, (void*)sock->endpoint); - err = udp_bind(sock->pcb, &listen_addr->addr, listen_addr->port); + err = udp_bind(sock->pcb, &l_listen.addr, l_listen.port); if (err) { udp_remove(sock->pcb); sock->pcb = NULL; } - *bound_addr = *listen_addr; + *bound_addr = l_listen; return err ? 0 : 1; } #endif /* COAP_SERVER_SUPPORT */