diff --git a/coap_config.h.contiki b/coap_config.h.contiki index 6e4f5415..c2960844 100644 --- a/coap_config.h.contiki +++ b/coap_config.h.contiki @@ -131,6 +131,7 @@ #define HAVE_STRNLEN 1 #define HAVE_SNPRINTF 1 #define HAVE_STRINGS_H 1 +#define HAVE_NETDB_H 1 /* there is no file-oriented output */ #define COAP_DEBUG_FD NULL diff --git a/examples/coap-client.c b/examples/coap-client.c index ae68bdd9..47297ece 100644 --- a/examples/coap-client.c +++ b/examples/coap-client.c @@ -291,54 +291,6 @@ coap_new_request(coap_context_t *ctx, return pdu; } -static int -resolve_address(const coap_str_const_t *server, struct sockaddr *dst) { - - struct addrinfo *res, *ainfo; - struct addrinfo hints; - static char addrstr[256]; - int error, len=-1; - - memset(addrstr, 0, sizeof(addrstr)); - if (server->length) - memcpy(addrstr, server->s, server->length); - else - memcpy(addrstr, "localhost", 9); - - memset ((char *)&hints, 0, sizeof(hints)); - hints.ai_socktype = SOCK_DGRAM; - hints.ai_family = AF_UNSPEC; - - error = getaddrinfo(addrstr, NULL, &hints, &res); - - if (error != 0) { - fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(error)); - return error; - } - - for (ainfo = res; ainfo != NULL; ainfo = ainfo->ai_next) { - switch (ainfo->ai_family) { - case AF_INET6: - case AF_INET: - len = (int)ainfo->ai_addrlen; - memcpy(dst, ainfo->ai_addr, len); - goto finish; - default: - ; - } - } - - finish: - freeaddrinfo(res); - return len; -} - -#define HANDLE_BLOCK1(Pdu) \ - ((method == COAP_REQUEST_PUT || method == COAP_REQUEST_POST) && \ - ((flags & FLAGS_BLOCK) == 0) && \ - ((Pdu)->hdr->code == COAP_RESPONSE_CODE_CREATED || \ - (Pdu)->hdr->code == COAP_RESPONSE_CODE_CHANGED)) - static int event_handler(coap_session_t *session COAP_UNUSED, const coap_event_t event) { @@ -1518,45 +1470,70 @@ static coap_session_t * get_session(coap_context_t *ctx, const char *local_addr, const char *local_port, - coap_proto_t proto, + coap_uri_scheme_t scheme, 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); - if ( local_addr ) { - int s; - struct addrinfo hints; - struct addrinfo *result = NULL, *rp; + 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_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) { + { + coap_addr_info_t *info_list = NULL; + coap_addr_info_t *info; + coap_str_const_t local; + uint16_t port = local_port ? atoi(local_port) : 0; - memset( &hints, 0, sizeof( struct addrinfo ) ); - hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ - hints.ai_socktype = COAP_PROTO_RELIABLE(proto) ? SOCK_STREAM : SOCK_DGRAM; /* Coap uses UDP */ - hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST | AI_NUMERICSERV | AI_ALL; + local.s = (const uint8_t*)local_addr; + local.length = strlen(local_addr); + /* resolve local address where data should be sent from */ + info_list = coap_resolve_address_info(&local, port, port, + AI_PASSIVE | AI_NUMERICHOST | AI_NUMERICSERV | AI_ALL, + 1 << scheme); + if (!info_list) { + fprintf(stderr, "coap_resolve_address_info: %s: failed\n", local_addr); + return NULL; + } - s = getaddrinfo( local_addr, local_port, &hints, &result ); - if ( s != 0 ) { - fprintf( stderr, "getaddrinfo: %s\n", gai_strerror( s ) ); - return NULL; - } - - /* iterate through results until success */ - for ( rp = result; rp != NULL; rp = rp->ai_next ) { - coap_address_t bind_addr; - if ( rp->ai_addrlen <= (socklen_t)sizeof( bind_addr.addr ) ) { - coap_address_init( &bind_addr ); - bind_addr.size = (socklen_t)rp->ai_addrlen; - memcpy( &bind_addr.addr, rp->ai_addr, rp->ai_addrlen ); - session = open_session(ctx, proto, &bind_addr, dst, + /* iterate through results until success */ + for (info = info_list; info != NULL; info = info->next ) { + if (!local_port) { + /* Need to reset port back to 0 rather than the default */ + if (info->addr.addr.sa.sa_family == AF_INET) + info->addr.addr.sin.sin_port = htons(port); + else if (info->addr.addr.sa.sa_family == AF_INET6) + info->addr.addr.sin6.sin6_port = htons(port); + } + session = open_session(ctx, proto, &info->addr, dst, identity, identity_len, key, key_len); - if ( session ) + if (session) break; } + coap_free_address_info(info_list); } - freeaddrinfo( result ); } else if (local_port) { coap_address_t bind_addr; @@ -1587,7 +1564,7 @@ main(int argc, char **argv) { uint16_t port = COAP_DEFAULT_PORT; char port_str[NI_MAXSERV] = "0"; char node_str[NI_MAXHOST] = ""; - int opt, res; + int opt; coap_log_t log_level = COAP_LOG_WARN; coap_log_t dtls_log_level = COAP_LOG_ERR; unsigned char *user = NULL, *key = NULL; @@ -1598,6 +1575,7 @@ main(int argc, char **argv) { uint32_t repeat_ms = REPEAT_DELAY_MS; uint8_t *data = NULL; size_t data_len = 0; + coap_addr_info_t *info_list = NULL; #ifndef _WIN32 struct sigaction sa; #endif @@ -1797,14 +1775,24 @@ main(int argc, char **argv) { port = proxy_scheme_option ? proxy.port : uri.port; scheme = proxy_scheme_option ? proxy.scheme : uri.scheme; } + if (reliable) { + if (scheme == COAP_URI_SCHEME_COAP) + scheme = COAP_URI_SCHEME_COAP_TCP; + if (scheme == COAP_URI_SCHEME_COAPS) + scheme = COAP_URI_SCHEME_COAPS_TCP; + } - /* resolve destination address where server should be sent */ - res = resolve_address(&server, &dst.addr.sa); + /* resolve destination address where data should be sent */ + info_list = coap_resolve_address_info(&server, port, port, + 0, + 1 << scheme); - if (res < 0) { - fprintf(stderr, "failed to resolve address\n"); + if (info_list == NULL) { + coap_log_err("failed to resolve address\n"); exit(1); } + memcpy(&dst, &info_list->addr, sizeof(dst)); + coap_free_address_info(info_list); ctx = coap_new_context( NULL ); if ( !ctx ) { @@ -1825,22 +1813,15 @@ main(int argc, char **argv) { coap_register_event_handler(ctx, event_handler); coap_register_nack_handler(ctx, nack_handler); - dst.size = res; - dst.addr.sin.sin_port = htons( port ); - - session = get_session( - ctx, - node_str[0] ? node_str : NULL, port_str, - scheme==COAP_URI_SCHEME_COAP_TCP ? COAP_PROTO_TCP : - scheme==COAP_URI_SCHEME_COAPS_TCP ? COAP_PROTO_TLS : - (reliable ? - scheme==COAP_URI_SCHEME_COAPS ? COAP_PROTO_TLS : COAP_PROTO_TCP - : scheme==COAP_URI_SCHEME_COAPS ? COAP_PROTO_DTLS : COAP_PROTO_UDP), - &dst, - user_length >= 0 ? user : NULL, - user_length >= 0 ? user_length : 0, - key_length > 0 ? key : NULL, - key_length > 0 ? key_length : 0 + session = get_session(ctx, + node_str[0] ? node_str : NULL, + port_str[0] ? port_str : NULL, + scheme, + &dst, + user_length >= 0 ? user : NULL, + user_length >= 0 ? user_length : 0, + key_length > 0 ? key : NULL, + key_length > 0 ? key_length : 0 ); if ( !session ) { diff --git a/examples/coap-server.c b/examples/coap-server.c index 808252e0..33ac1c5e 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -606,47 +606,6 @@ hnd_put_example_data(coap_resource_t *resource, } #if SERVER_CAN_PROXY -static int -resolve_address(const coap_str_const_t *server, struct sockaddr *dst) { - - struct addrinfo *res, *ainfo; - struct addrinfo hints; - static char addrstr[256]; - int error, len=-1; - - memset(addrstr, 0, sizeof(addrstr)); - if (server->length) - memcpy(addrstr, server->s, server->length); - else - memcpy(addrstr, "localhost", 9); - - memset ((char *)&hints, 0, sizeof(hints)); - hints.ai_socktype = SOCK_DGRAM; - hints.ai_family = AF_UNSPEC; - - error = getaddrinfo(addrstr, NULL, &hints, &res); - - if (error != 0) { - fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(error)); - return error; - } - - for (ainfo = res; ainfo != NULL; ainfo = ainfo->ai_next) { - switch (ainfo->ai_family) { - case AF_INET6: - case AF_INET: - len = (int)ainfo->ai_addrlen; - memcpy(dst, ainfo->ai_addr, len); - goto finish; - default: - ; - } - } - - finish: - freeaddrinfo(res); - return len; -} #define MAX_USER 128 /* Maximum length of a user name (i.e., PSK * identity) in bytes. */ @@ -903,7 +862,8 @@ get_ongoing_proxy_session(coap_session_t *session, coap_uri_scheme_t scheme; static char client_sni[256]; coap_str_const_t server; - uint16_t port = COAP_DEFAULT_PORT; + uint16_t port; + coap_addr_info_t *info_list = NULL; proxy_list_t *new_proxy_list; coap_context_t *context = coap_session_get_context(session); @@ -915,8 +875,6 @@ get_ongoing_proxy_session(coap_session_t *session, if (new_proxy_list->ongoing) return new_proxy_list->ongoing; - coap_address_init(&dst); - if (proxy.host.length) { server = proxy.host; port = proxy.port; @@ -926,21 +884,20 @@ get_ongoing_proxy_session(coap_session_t *session, port = uri->port; scheme = uri->scheme; } - if (resolve_address(&server, &dst.addr.sa) < 0) { + + /* resolve destination address where data should be sent */ + info_list = coap_resolve_address_info(&server, port, port, + 0, + 1 << scheme); + + if (info_list == NULL) { coap_pdu_set_code(response, COAP_RESPONSE_CODE_BAD_GATEWAY); remove_proxy_association(session, 0); return NULL; } - switch (dst.addr.sa.sa_family) { - case AF_INET: - dst.addr.sin.sin_port = ntohs(port); - break; - case AF_INET6: - dst.addr.sin6.sin6_port = ntohs(port); - break; - default: - break; - } + memcpy(&dst, &info_list->addr, sizeof(dst)); + coap_free_address_info(info_list); + switch (scheme) { case COAP_URI_SCHEME_COAP: case COAP_URI_SCHEME_COAP_TCP: @@ -2303,85 +2260,74 @@ usage( const char *program, const char *version) { 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 = 5683; + uint16_t s_port = 5684; + int scheme_hint_bits = COAP_URI_SCHEME_ALL_COAP_BITS; ctx = coap_new_context(NULL); if (!ctx) { return NULL; } + /* 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; + } + + if (cert_file == NULL && key_defined == 0) + scheme_hint_bits = COAP_URI_SCHEME_COAP_BIT | COAP_URI_SCHEME_COAP_TCP_BIT; + + 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_HTTP: + case COAP_URI_SCHEME_HTTPS: + case COAP_URI_SCHEME_LAST: + default: + continue; + } + ep = coap_new_endpoint(ctx, &info->addr, proto); + if (!ep) { + coap_log_warn("cannot create endpoint for proto %u\n", + proto); + } else { + have_ep = 1; + } + } + coap_free_address_info(info_list); + if (!have_ep) { 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; - - 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; - } - if (coap_tcp_is_supported()) { - coap_endpoint_t *ep_tcp; - ep_tcp = coap_new_endpoint(ctx, &addr, COAP_PROTO_TCP); - if (ep_tcp) { - if (coap_tls_is_supported() && (key_defined || cert_file)) { - coap_endpoint_t *ep_tls; - 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); - coap_free_context(ctx); - ctx = NULL; - -finish: - freeaddrinfo(result); return ctx; } diff --git a/examples/lwip/client-coap.c b/examples/lwip/client-coap.c index 12e81660..c95a769a 100644 --- a/examples/lwip/client-coap.c +++ b/examples/lwip/client-coap.c @@ -75,50 +75,26 @@ nack_handler(coap_session_t *session COAP_UNUSED, } static int -resolve_address(const char *host, const char *service, coap_address_t *dst) { +resolve_address(const char *host, const char *service, coap_address_t *dst, + int scheme_hint_bits) { - struct addrinfo *res, *ainfo; - struct addrinfo hints; - int error, len=-1; - struct sockaddr_in *sock4; - struct sockaddr_in6 *sock6; + coap_addr_info_t *addr_info; + coap_str_const_t str_host; + uint16_t port = service ? atoi(service) : 0; + int ret = 0; - memset(&hints, 0, sizeof(hints)); - memset(dst, 0, sizeof(*dst)); - hints.ai_socktype = SOCK_DGRAM; - hints.ai_family = AF_UNSPEC; + str_host.s = (const uint8_t *)host; + str_host.length = strlen(host); - error = getaddrinfo(host, service, &hints, &res); - - if (error != 0) { - fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(error)); - return error; + addr_info = coap_resolve_address_info(&str_host, port, port, AF_UNSPEC, + scheme_hint_bits); + if (addr_info) { + ret = 1; + *dst = addr_info->addr; } - for (ainfo = res; ainfo != NULL; ainfo = ainfo->ai_next) { - switch (ainfo->ai_family) { - case AF_INET: - sock4 = (struct sockaddr_in *)ainfo->ai_addr; - dst->port = ntohs(sock4->sin_port); - len = ainfo->ai_addrlen; - memcpy(&dst->addr, &sock4->sin_addr, 4); - dst->addr.type = IPADDR_TYPE_V4; - goto finish; - case AF_INET6: - sock6 = (struct sockaddr_in6 *)ainfo->ai_addr; - dst->port = ntohs(sock6->sin6_port); - len = ainfo->ai_addrlen; - memcpy(&dst->addr, &sock6->sin6_addr, 16); - dst->addr.type = IPADDR_TYPE_V6; - goto finish; - default: - ; - } - } - - finish: - freeaddrinfo(res); - return len; + coap_free_address_info(addr_info); + return ret; } void @@ -173,12 +149,15 @@ client_coap_init(coap_lwip_input_wait_handler_t input_wait, void *input_arg, LWIP_ASSERT("Failed to parse uri", len == 0); LWIP_ASSERT("Unsupported URI type", uri.scheme == COAP_URI_SCHEME_COAP || uri.scheme == COAP_URI_SCHEME_COAPS); + if (uri.scheme == COAP_URI_SCHEME_COAPS) { + LWIP_ASSERT("DTLS not supported", coap_dtls_is_supported()); + } snprintf(portbuf, sizeof(portbuf), "%d", uri.port); snprintf((char *)buf, sizeof(buf), "%*.*s", (int)uri.host.length, (int)uri.host.length, (const char *)uri.host.s); /* resolve destination address where server should be sent */ - len = resolve_address((const char*)buf, portbuf, &dst); + len = resolve_address((const char*)buf, portbuf, &dst, 1 << uri.scheme); LWIP_ASSERT("Failed to resolve address", len > 0); main_coap_context = coap_new_context(NULL); diff --git a/examples/lwip/config/coap_config.h b/examples/lwip/config/coap_config.h index a766feb8..37a7bc6c 100644 --- a/examples/lwip/config/coap_config.h +++ b/examples/lwip/config/coap_config.h @@ -39,6 +39,8 @@ #define HAVE_LIMITS_H +#define HAVE_NETDB_H + #define HAVE_SNPRINTF #endif /* COAP_CONFIG_H_ */ diff --git a/examples/lwip/config/coap_config.h.in b/examples/lwip/config/coap_config.h.in index 56bfa792..74e8e946 100644 --- a/examples/lwip/config/coap_config.h.in +++ b/examples/lwip/config/coap_config.h.in @@ -39,6 +39,8 @@ #define HAVE_LIMITS_H +#define HAVE_NETDB_H + #define HAVE_SNPRINTF #endif /* COAP_CONFIG_H_ */ diff --git a/include/coap3/coap_address.h b/include/coap3/coap_address.h index 6ca85759..482e798d 100644 --- a/include/coap3/coap_address.h +++ b/include/coap3/coap_address.h @@ -23,6 +23,8 @@ #include #include "libcoap.h" +#include "coap3/pdu.h" + #if defined(WITH_LWIP) #include @@ -92,7 +94,7 @@ coap_address_set_port(coap_address_t *addr, uint16_t port) { #else /* WITH_LWIP || WITH_CONTIKI */ - /** multi-purpose address abstraction */ +/** Multi-purpose address abstraction */ typedef struct coap_address_t { socklen_t size; /**< size of addr */ union { @@ -137,6 +139,40 @@ _coap_address_isany_impl(const coap_address_t *a) { } #endif /* WITH_LWIP || WITH_CONTIKI */ +/** Resolved addresses information */ +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_address_t addr; /**< The address to connect / bind to */ +} coap_addr_info_t; + +/** + * Resolve the specified @p server into a set of coap_address_t that can + * be used to bind() or connect() to. + * + * @param server The Address to resolve. + * @param port The unsecured protocol port to use. + * @param secure_port The secured protocol port to use. + * @param ai_hints_flags AI_* Hint flags to use for internal getaddrinfo(). + * @param scheme_hint_bits Which schemes to return information for. One or + * more of COAP_URI_SCHEME_*_BIT or'd together. + * + * @return One or more linked sets of coap_addr_info_t or @c NULL if error. + */ +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); + +/** + * Free off the one or more linked sets of coap_addr_info_t returned from + * coap_resolve_address_info(). + * + * @param info_list The set of coap_addr_info_t to free off. + */ +void coap_free_address_info(coap_addr_info_t *info_list); + /** * Resets the given coap_address_t object @p addr to its default values. In * particular, the member size must be initialized to the available size for diff --git a/include/coap3/uri.h b/include/coap3/uri.h index b5b814c4..0ac57de5 100644 --- a/include/coap3/uri.h +++ b/include/coap3/uri.h @@ -38,6 +38,18 @@ typedef enum coap_uri_scheme_t { /** This mask can be used to check if a parsed URI scheme is secure. */ #define COAP_URI_SCHEME_SECURE_MASK 0x01 +#define COAP_URI_SCHEME_COAP_BIT (1 << COAP_URI_SCHEME_COAP) +#define COAP_URI_SCHEME_COAPS_BIT (1 << COAP_URI_SCHEME_COAPS) +#define COAP_URI_SCHEME_COAP_TCP_BIT (1 << COAP_URI_SCHEME_COAP_TCP) +#define COAP_URI_SCHEME_COAPS_TCP_BIT (1 << COAP_URI_SCHEME_COAPS_TCP) +#define COAP_URI_SCHEME_HTTP_BIT (1 << COAP_URI_SCHEME_HTTP) +#define COAP_URI_SCHEME_HTTPS_BIT (1 << COAP_URI_SCHEME_HTTPS) + +#define COAP_URI_SCHEME_ALL_COAP_BITS (COAP_URI_SCHEME_COAP_BIT | \ + COAP_URI_SCHEME_COAPS_BIT | \ + COAP_URI_SCHEME_COAP_TCP_BIT | \ + COAP_URI_SCHEME_COAPS_TCP_BIT) + /** * Representation of parsed URI. Components may be filled from a string with * coap_split_uri() or coap_split_proxy_uri() and can be used as input for diff --git a/libcoap-3.map b/libcoap-3.map index 2fe2fd9b..9bce5afc 100644 --- a/libcoap-3.map +++ b/libcoap-3.map @@ -87,6 +87,7 @@ global: coap_find_attr; coap_fls; coap_flsll; + coap_free_address_info; coap_free_async; coap_free_context; coap_free_endpoint; @@ -182,6 +183,7 @@ global: coap_register_request_handler; coap_register_response_handler; coap_resize_binary; + coap_resolve_address_info; coap_resource_get_uri_path; coap_resource_get_userdata; coap_resource_init; diff --git a/libcoap-3.sym b/libcoap-3.sym index 4f72996e..0d2ac669 100644 --- a/libcoap-3.sym +++ b/libcoap-3.sym @@ -85,6 +85,7 @@ coap_find_async coap_find_attr coap_fls coap_flsll +coap_free_address_info coap_free_async coap_free_context coap_free_endpoint @@ -180,6 +181,7 @@ coap_register_pong_handler coap_register_request_handler coap_register_response_handler coap_resize_binary +coap_resolve_address_info coap_resource_get_uri_path coap_resource_get_userdata coap_resource_init diff --git a/man/coap_address.txt.in b/man/coap_address.txt.in index 3065577e..2ddbba91 100644 --- a/man/coap_address.txt.in +++ b/man/coap_address.txt.in @@ -11,7 +11,7 @@ coap_address(3) NAME ---- coap_address, -coap_address_t, +coap_address_t - Work with CoAP Socket Address Types SYNOPSIS @@ -24,6 +24,12 @@ SYNOPSIS *void coap_address_init(coap_address_t *_addr_);* +*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_);* + +*void coap_free_address_info(coap_addr_info_t *_info_list_);* + For specific (D)TLS library support, link with *-lcoap-@LIBCOAP_API_VERSION@-notls*, *-lcoap-@LIBCOAP_API_VERSION@-gnutls*, *-lcoap-@LIBCOAP_API_VERSION@-openssl*, *-lcoap-@LIBCOAP_API_VERSION@-mbedtls* @@ -50,7 +56,7 @@ which are all handled by the *coap_address_t* structure. [source, c] ---- - /** multi-purpose address abstraction */ +/* Multi-purpose address abstraction */ typedef struct coap_address_t { socklen_t size; /* size of addr */ union { @@ -61,6 +67,21 @@ typedef struct coap_address_t { } 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* + +[source, c] +---- +/* Resolved addresses information */ +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_address_t addr; /* The address to connect / bind to */ +} coap_addr_info_t; +---- + FUNCTIONS --------- @@ -73,9 +94,53 @@ 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_resolve_address_info()* + +The *coap_resolve_address_info*() function resolves the address _server_ into +a set of one or more coap_addr_info_t structures. Depending on the scheme as +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. + +The returned set of coap_addr_info_t structures must be freed off by the +caller using *coap_free_address_info*(). + +*Function: coap_free_address_info()* + +The *coap_free_address_info*() function frees off all the _info_list_ +linked entries. + +RETURN VALUES +------------- +*coap_resolve_address_info*() returns a linked list of addresses that can be +used for session setup or NULL if there is a failure. + +EXAMPLES +-------- +*Get target address from uri* +[source, c] +---- +#include + +static int +get_address(coap_uri_t *uri, coap_address_t *dst) { + coap_addr_info_t *info_list; + + info_list = coap_resolve_address_info(&uri->host, uri->port, uri->port, 0, + 1 << uri->scheme); + if (info_list == NULL) + return 0; + memcpy(dst, &info_list->addr, sizeof(*dst)); + coap_free_address_info(info_list); + return 1; +} +---- + SEE ALSO -------- -*coap_endpoint_client*(3) and *coap_endpoint_server*(3) +*coap_endpoint_client*(3), *coap_endpoint_server*(3) and *coap_uri*(3) FURTHER INFORMATION ------------------- diff --git a/src/coap_address.c b/src/coap_address.c index f91118ca..46c4d21b 100644 --- a/src/coap_address.c +++ b/src/coap_address.c @@ -113,3 +113,195 @@ void coap_address_init(coap_address_t *addr) { #endif } +static void +update_port(coap_address_t *addr, uint16_t port, uint16_t default_port) { + if (port == 0) + port = default_port; + +#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI) + if (addr->addr.sa.sa_family == AF_INET) + addr->addr.sin.sin_port = htons(port); + else if (addr->addr.sa.sa_family == AF_INET6) + addr->addr.sin6.sin6_port = htons(port); +#else /* defined(WITH_LWIP) || defined(WITH_CONTIKI) */ + addr->port = port; +#endif /* defined(WITH_LWIP) || defined(WITH_CONTIKI) */ + return; +} + +#ifdef HAVE_NETDB_H +#include +#endif + +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) { + + struct addrinfo *res, *ainfo; + struct addrinfo hints; + static char addrstr[256]; + int error; + coap_addr_info_t *info = NULL; + coap_addr_info_t *info_prev = NULL; + coap_addr_info_t *info_list = NULL; + coap_uri_scheme_t scheme; + + memset(addrstr, 0, sizeof(addrstr)); + if (server && server->length) + memcpy(addrstr, server->s, server->length); + else + memcpy(addrstr, "localhost", 9); + + memset ((char *)&hints, 0, sizeof(hints)); + hints.ai_socktype = 0; + hints.ai_family = AF_UNSPEC; + hints.ai_flags = ai_hints_flags; + + error = getaddrinfo(addrstr, NULL, &hints, &res); + + if (error != 0) { + coap_log_warn("getaddrinfo: %s\n", gai_strerror(error)); + return NULL; + } + + for (ainfo = res; ainfo != NULL; ainfo = ainfo->ai_next) { + if (ainfo->ai_addrlen > (socklen_t)sizeof(info->addr.addr)) + continue; + + switch (ainfo->ai_family) { + case AF_INET6: + case AF_INET: + for (scheme = 0; scheme < COAP_URI_SCHEME_LAST; scheme++) { + if (scheme_hint_bits & (1 << scheme)) { + switch (scheme) { + case COAP_URI_SCHEME_COAP: + if (ainfo->ai_socktype != SOCK_DGRAM) + continue; + break; + case COAP_URI_SCHEME_COAPS: + if (!coap_dtls_is_supported()) + continue; + if (ainfo->ai_socktype != SOCK_DGRAM) + continue; + break; + case COAP_URI_SCHEME_COAP_TCP: + if (!coap_tcp_is_supported()) + continue; + if (ainfo->ai_socktype != SOCK_STREAM) + continue; + break; + case COAP_URI_SCHEME_COAPS_TCP: + if (!coap_tls_is_supported()) + continue; + if (ainfo->ai_socktype != SOCK_STREAM) + continue; + break; + case COAP_URI_SCHEME_HTTP: + if (!coap_tcp_is_supported()) + continue; + if (ainfo->ai_socktype != SOCK_STREAM) + continue; + break; + case COAP_URI_SCHEME_HTTPS: + if (!coap_tls_is_supported()) + continue; + if (ainfo->ai_socktype != SOCK_STREAM) + continue; + break; + case COAP_URI_SCHEME_LAST: + default: + continue; + } + + info = coap_malloc_type(COAP_STRING, sizeof(coap_addr_info_t)); + if (info == NULL) { + /* malloc failure - return what we have so far */ + return info_list; + } + info->next = NULL; + /* Need to return in same order as getaddrinfo() */ + if (!info_prev) { + info_list = info; + info_prev = info; + } else { + info_prev->next = info; + info_prev = info; + } + + info->scheme = scheme; + coap_address_init(&info->addr); +#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI) + info->addr.size = (socklen_t)ainfo->ai_addrlen; + memcpy(&info->addr.addr, ainfo->ai_addr, ainfo->ai_addrlen); +#else /* defined(WITH_LWIP) || defined(WITH_CONTIKI) */ + memset(&info->addr, 0, sizeof(info->addr)); + switch (ainfo->ai_family) { + struct sockaddr_in *sock4; + struct sockaddr_in6 *sock6; + case AF_INET: + sock4 = (struct sockaddr_in *)ainfo->ai_addr; + info->addr.port = ntohs(sock4->sin_port); + memcpy(&info->addr.addr, &sock4->sin_addr, 4); +#ifdef WITH_LWIP + info->addr.addr.type = IPADDR_TYPE_V4; +#endif /* WITH_LWIP */ + break; + case AF_INET6: + sock6 = (struct sockaddr_in6 *)ainfo->ai_addr; + info->addr.port = ntohs(sock6->sin6_port); + memcpy(&info->addr.addr, &sock6->sin6_addr, 16); +#ifdef WITH_LWIP + info->addr.addr.type = IPADDR_TYPE_V6; +#endif /* WITH_LWIP */ + break; + default: + ; + } +#endif /* defined(WITH_LWIP) || defined(WITH_CONTIKI) */ + switch (scheme) { + case COAP_URI_SCHEME_COAP: + update_port(&info->addr, port, 5683); + break; + case COAP_URI_SCHEME_COAPS: + update_port(&info->addr, secure_port, 5684); + break; + case COAP_URI_SCHEME_COAP_TCP: + update_port(&info->addr, port, 5683); + break; + case COAP_URI_SCHEME_COAPS_TCP: + update_port(&info->addr, secure_port, 5684); + break; + case COAP_URI_SCHEME_HTTP: + update_port(&info->addr, port, 80); + break; + case COAP_URI_SCHEME_HTTPS: + update_port(&info->addr, secure_port, 443); + break; + case COAP_URI_SCHEME_LAST: + default: + break; + } + } + } + break; + default: + break; + } + } + + freeaddrinfo(res); + return info_list; +} + +void +coap_free_address_info(coap_addr_info_t *info) { + while (info) { + coap_addr_info_t *info_next = info->next; + + coap_free_type(COAP_STRING, info); + info = info_next; + } +}