1c87c5fbaSopenharmony_ci// -*- mode:doc; -*- 2c87c5fbaSopenharmony_ci// vim: set syntax=asciidoc tw=0 3c87c5fbaSopenharmony_ci 4c87c5fbaSopenharmony_cicoap_address(3) 5c87c5fbaSopenharmony_ci=============== 6c87c5fbaSopenharmony_ci:doctype: manpage 7c87c5fbaSopenharmony_ci:man source: coap_address 8c87c5fbaSopenharmony_ci:man version: @PACKAGE_VERSION@ 9c87c5fbaSopenharmony_ci:man manual: libcoap Manual 10c87c5fbaSopenharmony_ci 11c87c5fbaSopenharmony_ciNAME 12c87c5fbaSopenharmony_ci---- 13c87c5fbaSopenharmony_cicoap_address, 14c87c5fbaSopenharmony_cicoap_address_t, 15c87c5fbaSopenharmony_cicoap_address_init, 16c87c5fbaSopenharmony_cicoap_address_copy, 17c87c5fbaSopenharmony_cicoap_address_equals, 18c87c5fbaSopenharmony_cicoap_address_get_port, 19c87c5fbaSopenharmony_cicoap_address_set_port, 20c87c5fbaSopenharmony_cicoap_get_available_scheme_hint_bits, 21c87c5fbaSopenharmony_cicoap_resolve_address_info, 22c87c5fbaSopenharmony_cicoap_free_address_info, 23c87c5fbaSopenharmony_cicoap_sockaddr_un, 24c87c5fbaSopenharmony_cicoap_address_set_unix_domain, 25c87c5fbaSopenharmony_cicoap_host_is_unix_domain, 26c87c5fbaSopenharmony_cicoap_is_bcast, 27c87c5fbaSopenharmony_cicoap_is_mcast, 28c87c5fbaSopenharmony_cicoap_is_af_unix 29c87c5fbaSopenharmony_ci- Work with CoAP Socket Address Types 30c87c5fbaSopenharmony_ci 31c87c5fbaSopenharmony_ciSYNOPSIS 32c87c5fbaSopenharmony_ci-------- 33c87c5fbaSopenharmony_ci*#include <coap@LIBCOAP_API_VERSION@/coap.h>* 34c87c5fbaSopenharmony_ci 35c87c5fbaSopenharmony_ci*struct coap_address_t;* 36c87c5fbaSopenharmony_ci 37c87c5fbaSopenharmony_ci*struct coap_sockaddr_un;* 38c87c5fbaSopenharmony_ci 39c87c5fbaSopenharmony_ci*void coap_address_init(coap_address_t *_addr_);* 40c87c5fbaSopenharmony_ci 41c87c5fbaSopenharmony_ci*void coap_address_copy(coap_address_t *_dst_, const coap_address_t *_src_);* 42c87c5fbaSopenharmony_ci 43c87c5fbaSopenharmony_ci*int coap_address_equals(const coap_address_t *_a_, const coap_address_t *_b_);* 44c87c5fbaSopenharmony_ci 45c87c5fbaSopenharmony_ci*uint16_t coap_address_get_port(const coap_address_t *_addr_);* 46c87c5fbaSopenharmony_ci 47c87c5fbaSopenharmony_ci*void coap_address_set_port(coap_address_t *_addr_, uint16_t _port_);* 48c87c5fbaSopenharmony_ci 49c87c5fbaSopenharmony_ci*uint32_t coap_get_available_scheme_hint_bits(int _have_pki_psk_, 50c87c5fbaSopenharmony_ciint _ws_check_, coap_proto_t _use_unix_proto_);* 51c87c5fbaSopenharmony_ci 52c87c5fbaSopenharmony_ci*coap_addr_info_t *coap_resolve_address_info(const coap_str_const_t *_address_, 53c87c5fbaSopenharmony_ciuint16_t _port_, uint16_t _secure_port_, uint16_t _ws_port_, 54c87c5fbaSopenharmony_ciuint16_t _ws_secure_port_, int _ai_hints_flags_, 55c87c5fbaSopenharmony_ciint _scheme_hint_bits_, coap_resolve_type_t _type_);* 56c87c5fbaSopenharmony_ci 57c87c5fbaSopenharmony_ci*void coap_free_address_info(coap_addr_info_t *_info_list_);* 58c87c5fbaSopenharmony_ci 59c87c5fbaSopenharmony_ci*int coap_host_is_unix_domain(const coap_str_const_t *_host_);* 60c87c5fbaSopenharmony_ci 61c87c5fbaSopenharmony_ci*int coap_address_set_unix_domain(coap_address_t *_addr_, 62c87c5fbaSopenharmony_ciconst uint8_t *_host_, size_t _host_len_);* 63c87c5fbaSopenharmony_ci 64c87c5fbaSopenharmony_ci*int coap_is_bcast(const coap_address_t *_addr_);* 65c87c5fbaSopenharmony_ci 66c87c5fbaSopenharmony_ci*int coap_is_mcast(const coap_address_t *_addr_);* 67c87c5fbaSopenharmony_ci 68c87c5fbaSopenharmony_ci*int coap_is_af_unix(const coap_address_t *_addr_);* 69c87c5fbaSopenharmony_ci 70c87c5fbaSopenharmony_ciFor specific (D)TLS library support, link with 71c87c5fbaSopenharmony_ci*-lcoap-@LIBCOAP_API_VERSION@-notls*, *-lcoap-@LIBCOAP_API_VERSION@-gnutls*, 72c87c5fbaSopenharmony_ci*-lcoap-@LIBCOAP_API_VERSION@-openssl*, *-lcoap-@LIBCOAP_API_VERSION@-mbedtls* 73c87c5fbaSopenharmony_cior *-lcoap-@LIBCOAP_API_VERSION@-tinydtls*. Otherwise, link with 74c87c5fbaSopenharmony_ci*-lcoap-@LIBCOAP_API_VERSION@* to get the default (D)TLS library support. 75c87c5fbaSopenharmony_ci 76c87c5fbaSopenharmony_ciDESCRIPTION 77c87c5fbaSopenharmony_ci----------- 78c87c5fbaSopenharmony_ciThis man page focuses on setting up CoAP endpoint address definitions. 79c87c5fbaSopenharmony_ci 80c87c5fbaSopenharmony_ci*Supported Socket Types* 81c87c5fbaSopenharmony_ci 82c87c5fbaSopenharmony_ciLibcoap supports 3 different socket types that can be used: 83c87c5fbaSopenharmony_ci 84c87c5fbaSopenharmony_ci[source, c] 85c87c5fbaSopenharmony_ci---- 86c87c5fbaSopenharmony_ciAF_INET IPv4 IP addresses and ports 87c87c5fbaSopenharmony_ciAF_INET6 IPv6 IP addresses and ports and can be dual IPv4/IPv6 stacked 88c87c5fbaSopenharmony_ciAF_UNIX Unix Domain using file path names 89c87c5fbaSopenharmony_ci---- 90c87c5fbaSopenharmony_ci 91c87c5fbaSopenharmony_ciwhich are all handled by the *coap_address_t* structure. 92c87c5fbaSopenharmony_ci 93c87c5fbaSopenharmony_ci*Structure coap_address_t* 94c87c5fbaSopenharmony_ci 95c87c5fbaSopenharmony_ci[source, c] 96c87c5fbaSopenharmony_ci---- 97c87c5fbaSopenharmony_ci/* Multi-purpose address abstraction */ 98c87c5fbaSopenharmony_citypedef struct coap_address_t { 99c87c5fbaSopenharmony_ci socklen_t size; /* size of addr */ 100c87c5fbaSopenharmony_ci union { 101c87c5fbaSopenharmony_ci struct sockaddr sa; 102c87c5fbaSopenharmony_ci struct sockaddr_in sin; 103c87c5fbaSopenharmony_ci struct sockaddr_in6 sin6; 104c87c5fbaSopenharmony_ci struct coap_sockaddr_un cun; /* CoAP shortened special */ 105c87c5fbaSopenharmony_ci } addr; 106c87c5fbaSopenharmony_ci} coap_address_t; 107c87c5fbaSopenharmony_ci 108c87c5fbaSopenharmony_ciwhich is used in the *coap_addr_info_t* structure as returned by 109c87c5fbaSopenharmony_ci*coap_resolve_address_info()*. 110c87c5fbaSopenharmony_ci---- 111c87c5fbaSopenharmony_ci 112c87c5fbaSopenharmony_ci*Structure coap_addr_info_t* 113c87c5fbaSopenharmony_ci 114c87c5fbaSopenharmony_ci[source, c] 115c87c5fbaSopenharmony_ci---- 116c87c5fbaSopenharmony_ci/* Resolved addresses information */ 117c87c5fbaSopenharmony_citypedef struct coap_addr_info_t { 118c87c5fbaSopenharmony_ci struct coap_addr_info_t *next; /* Next entry in the chain */ 119c87c5fbaSopenharmony_ci coap_uri_scheme_t scheme; /* CoAP scheme to use */ 120c87c5fbaSopenharmony_ci coap_proto_t proto; /* CoAP protocol to use */ 121c87c5fbaSopenharmony_ci coap_address_t addr; /* The address to connect / bind to */ 122c87c5fbaSopenharmony_ci} coap_addr_info_t; 123c87c5fbaSopenharmony_ci---- 124c87c5fbaSopenharmony_ci 125c87c5fbaSopenharmony_ci*Structure coap_sockaddr_un* 126c87c5fbaSopenharmony_ci 127c87c5fbaSopenharmony_ci[source, c] 128c87c5fbaSopenharmony_ci---- 129c87c5fbaSopenharmony_ci#define COAP_UNIX_PATH_MAX (sizeof(struct sockaddr_in6) - sizeof(sa_family_t)) 130c87c5fbaSopenharmony_ci 131c87c5fbaSopenharmony_cistruct coap_sockaddr_un { 132c87c5fbaSopenharmony_ci sa_family_t sun_family; /* AF_UNIX */ 133c87c5fbaSopenharmony_ci char sun_path[COAP_UNIX_PATH_MAX]; /* pathname max 26 with NUL byte */ 134c87c5fbaSopenharmony_ci}; 135c87c5fbaSopenharmony_ci---- 136c87c5fbaSopenharmony_ci 137c87c5fbaSopenharmony_ciThe *coap_sockaddr_un* structure is modeled on the *sockaddr_un* structure 138c87c5fbaSopenharmony_ci(see *unix*(7)) but has a smaller sun_path so that the overall size of 139c87c5fbaSopenharmony_ci*coap_address_t* is not changed by its inclusion. COAP_UNIX_MAX_PATH includes 140c87c5fbaSopenharmony_cithe trailing zero terminator of a domain unix file name. 141c87c5fbaSopenharmony_ci 142c87c5fbaSopenharmony_ci*Enum coap_resolve_type_t* 143c87c5fbaSopenharmony_ci 144c87c5fbaSopenharmony_ci[source, c] 145c87c5fbaSopenharmony_ci---- 146c87c5fbaSopenharmony_citypedef enum coap_resolve_type_t { 147c87c5fbaSopenharmony_ci COAP_RESOLVE_TYPE_LOCAL, /* local side of session */ 148c87c5fbaSopenharmony_ci COAP_RESOLVE_TYPE_REMOTE, /* remote side of session */ 149c87c5fbaSopenharmony_ci} coap_resolve_type_t; 150c87c5fbaSopenharmony_ci---- 151c87c5fbaSopenharmony_ci 152c87c5fbaSopenharmony_ciUsed when determining how to do an address lookup when calling 153c87c5fbaSopenharmony_ci*coap_resolve_address_info()*. 154c87c5fbaSopenharmony_ci 155c87c5fbaSopenharmony_ciFUNCTIONS 156c87c5fbaSopenharmony_ci--------- 157c87c5fbaSopenharmony_ci 158c87c5fbaSopenharmony_ci*Function: coap_address_init()* 159c87c5fbaSopenharmony_ci 160c87c5fbaSopenharmony_ciThe *coap_address_init*() function initializes _addr_ for later use. In 161c87c5fbaSopenharmony_ciparticular it sets the _size_ variable and sets all other values to be 0. 162c87c5fbaSopenharmony_ci 163c87c5fbaSopenharmony_ciIt is then the responsibility of the application to set the address family 164c87c5fbaSopenharmony_ciin addr.sa.sa_family and then fill in the the appropriate union structure 165c87c5fbaSopenharmony_cibased on the address family before the coap_address_t _addr_ is used. 166c87c5fbaSopenharmony_ci 167c87c5fbaSopenharmony_ci*Function: coap_address_copy()* 168c87c5fbaSopenharmony_ci 169c87c5fbaSopenharmony_ciThe *coap_address_copy*() function copies the address _src_ into _dst_. 170c87c5fbaSopenharmony_ci 171c87c5fbaSopenharmony_ci*Function: coap_address_equals()* 172c87c5fbaSopenharmony_ci 173c87c5fbaSopenharmony_ciThe *coap_address_equals*() function checks whether the addresses _a_ and _b_ 174c87c5fbaSopenharmony_ciare identical. 175c87c5fbaSopenharmony_ci 176c87c5fbaSopenharmony_ci*Function: coap_address_get_port()* 177c87c5fbaSopenharmony_ci 178c87c5fbaSopenharmony_ciThe *coap_address_get_port*() function gets the the port from _addr_ if 179c87c5fbaSopenharmony_ci_addr_ is AF_INET or AF_INET6. 180c87c5fbaSopenharmony_ci 181c87c5fbaSopenharmony_ci*Function: coap_address_set_port()* 182c87c5fbaSopenharmony_ci 183c87c5fbaSopenharmony_ciThe *coap_address_set_port*() function sets the the _port_ in _addr_ if 184c87c5fbaSopenharmony_ci_addr_ is AF_INET or AF_INET6. 185c87c5fbaSopenharmony_ci 186c87c5fbaSopenharmony_ci*Function: coap_get_available_scheme_hint_bits()* 187c87c5fbaSopenharmony_ci 188c87c5fbaSopenharmony_ciThe *coap_get_available_scheme_hint_bits*() function is used for servers to 189c87c5fbaSopenharmony_cidetermine what coap schemes are supported in the underlying libcoap library. 190c87c5fbaSopenharmony_ci_have_pki_psk_ can be set to 1 to check for (D)DTLS support, else 0. 191c87c5fbaSopenharmony_ci_ws_check_ can be set to 1 to check for WebSockets support, else 0. 192c87c5fbaSopenharmony_ci_use_unix_proto_, if not set to COAP_PROTO_NONE, hints at the specific CoAP 193c87c5fbaSopenharmony_ciprotocol to use over a Unix socket. The output is suitable for input for the 194c87c5fbaSopenharmony_ci*coap_address_resolve_info*()'s _scheme_hint_bits_. 195c87c5fbaSopenharmony_ci 196c87c5fbaSopenharmony_ci*Function: coap_resolve_address_info()* 197c87c5fbaSopenharmony_ci 198c87c5fbaSopenharmony_ciThe *coap_resolve_address_info*() function resolves the address _address_ into 199c87c5fbaSopenharmony_cia set of one or more coap_addr_info_t structures. Depending on the scheme as 200c87c5fbaSopenharmony_ciabstracted from _scheme_hint_bits_, _port_, _secure_port_, _ws_port_ 201c87c5fbaSopenharmony_ci(WebSockets) or _ws_secure_port_ (WebSockets) is used to 202c87c5fbaSopenharmony_ciupdate the addr variable of coap_addr_info_t. If _port_ (or _secure_port_) is 203c87c5fbaSopenharmony_ci0, then the default port for the scheme is used if _type_ is set to 204c87c5fbaSopenharmony_ciCOAP_RESOLVE_TYPE_LOCAL. _ai_hints_flags_ is used for 205c87c5fbaSopenharmony_cithe internally called getaddrinfo(3) function. _scheme_hint_bits_ is a set of 206c87c5fbaSopenharmony_cione or more COAP_URI_SCHEME_*_BIT or'd together. _scheme_hint_bits_ can also 207c87c5fbaSopenharmony_ci(for servers) be the output from *coap_get_available_scheme_hint_bits*(). 208c87c5fbaSopenharmony_ci 209c87c5fbaSopenharmony_ciThe returned set of coap_addr_info_t structures must be freed off by the 210c87c5fbaSopenharmony_cicaller using *coap_free_address_info*(). 211c87c5fbaSopenharmony_ci 212c87c5fbaSopenharmony_ci*Function: coap_free_address_info()* 213c87c5fbaSopenharmony_ci 214c87c5fbaSopenharmony_ciThe *coap_free_address_info*() function frees off all the _info_list_ 215c87c5fbaSopenharmony_cilinked entries. 216c87c5fbaSopenharmony_ci 217c87c5fbaSopenharmony_ci*Function: coap_address_set_unix_domain()* 218c87c5fbaSopenharmony_ci 219c87c5fbaSopenharmony_ciThe *coap_address_set_unix_domain*() function initializes _addr_ and then 220c87c5fbaSopenharmony_cipopulates _addr_ with all the appropriate information for a Unix Domain Socket. 221c87c5fbaSopenharmony_ciThe _host_ information with length _host_len_ abstracted from a CoAP URI 222c87c5fbaSopenharmony_ciis copied into _addr_'s _sun_path_ translating any %2F encoding to /. 223c87c5fbaSopenharmony_ci 224c87c5fbaSopenharmony_ci*Function: coap_host_is_unix_domain()* 225c87c5fbaSopenharmony_ci 226c87c5fbaSopenharmony_ciThe *coap_host_is_unix_domain*() function checks whether _host_ is an 227c87c5fbaSopenharmony_cian AF_UNIX file name (encoded using %2F to indicate a /). 228c87c5fbaSopenharmony_ci 229c87c5fbaSopenharmony_ci*Function: coap_is_mcast()* 230c87c5fbaSopenharmony_ci 231c87c5fbaSopenharmony_ciThe *coap_is_mcast*() function checks whether _addr_ is a multicast 232c87c5fbaSopenharmony_ciaddress. 233c87c5fbaSopenharmony_ci 234c87c5fbaSopenharmony_ci*Function: coap_is_bcast()* 235c87c5fbaSopenharmony_ci 236c87c5fbaSopenharmony_ciThe *coap_is_mcast*() function checks whether _addr_ is a broadcast 237c87c5fbaSopenharmony_ciaddress. 238c87c5fbaSopenharmony_ci 239c87c5fbaSopenharmony_ci*Function: coap_is_af_unix()* 240c87c5fbaSopenharmony_ci 241c87c5fbaSopenharmony_ciThe *coap_is_mcast*() function checks whether _addr_ is of the type AF_UNIX. 242c87c5fbaSopenharmony_ci 243c87c5fbaSopenharmony_ciRETURN VALUES 244c87c5fbaSopenharmony_ci------------- 245c87c5fbaSopenharmony_ci*coap_address_equals*() returns 1 if the addresses are equal or 0 if not. 246c87c5fbaSopenharmony_ci 247c87c5fbaSopenharmony_ci*coap_address_get_port*() returns the port in network byte order. 248c87c5fbaSopenharmony_ci 249c87c5fbaSopenharmony_ci*coap_get_available_scheme_hint_bits*() returns a set of COAP_URI_SCHEME_*_BIT 250c87c5fbaSopenharmony_cior'd together based on the supported libcoap functionality. 251c87c5fbaSopenharmony_ci 252c87c5fbaSopenharmony_ci*coap_resolve_address_info*() returns a linked list of addresses that can be 253c87c5fbaSopenharmony_ciused for session setup or NULL if there is a failure. 254c87c5fbaSopenharmony_ci 255c87c5fbaSopenharmony_ci*coap_address_set_unix_domain*() returns 1 on success or 0 on failure. 256c87c5fbaSopenharmony_ci 257c87c5fbaSopenharmony_ci*coap_host_is_unix_domain*() returns 1 if encoded unix path name or 0 if not. 258c87c5fbaSopenharmony_ci 259c87c5fbaSopenharmony_ci*coap_is_mcast*() returns 1 if address is multicast or 0 if not. 260c87c5fbaSopenharmony_ci 261c87c5fbaSopenharmony_ci*coap_is_bcast*() returns 1 if address is broadcast or 0 if not. 262c87c5fbaSopenharmony_ci 263c87c5fbaSopenharmony_ci*coap_is_af_unix*() returns 1 if address is of type AF_UNIX or 0 if not. 264c87c5fbaSopenharmony_ci 265c87c5fbaSopenharmony_ciEXAMPLES 266c87c5fbaSopenharmony_ci-------- 267c87c5fbaSopenharmony_ci*Get client target address from uri* 268c87c5fbaSopenharmony_ci[source, c] 269c87c5fbaSopenharmony_ci---- 270c87c5fbaSopenharmony_ci#include <coap@LIBCOAP_API_VERSION@/coap.h> 271c87c5fbaSopenharmony_ci 272c87c5fbaSopenharmony_cistatic int 273c87c5fbaSopenharmony_ciget_address(coap_uri_t *uri, coap_address_t *dst) { 274c87c5fbaSopenharmony_ci coap_addr_info_t *info_list; 275c87c5fbaSopenharmony_ci 276c87c5fbaSopenharmony_ci info_list = coap_resolve_address_info(&uri->host, uri->port, uri->port, 277c87c5fbaSopenharmony_ci uri->port, uri->port,0, 278c87c5fbaSopenharmony_ci 1 << uri->scheme, COAP_RESOLVE_TYPE_LOCAL); 279c87c5fbaSopenharmony_ci if (info_list == NULL) 280c87c5fbaSopenharmony_ci return 0; 281c87c5fbaSopenharmony_ci memcpy(dst, &info_list->addr, sizeof(*dst)); 282c87c5fbaSopenharmony_ci coap_free_address_info(info_list); 283c87c5fbaSopenharmony_ci return 1; 284c87c5fbaSopenharmony_ci} 285c87c5fbaSopenharmony_ci---- 286c87c5fbaSopenharmony_ci 287c87c5fbaSopenharmony_ciSEE ALSO 288c87c5fbaSopenharmony_ci-------- 289c87c5fbaSopenharmony_ci*coap_endpoint_client*(3), *coap_endpoint_server*(3) and *coap_uri*(3) 290c87c5fbaSopenharmony_ci 291c87c5fbaSopenharmony_ciFURTHER INFORMATION 292c87c5fbaSopenharmony_ci------------------- 293c87c5fbaSopenharmony_ciSee 294c87c5fbaSopenharmony_ci 295c87c5fbaSopenharmony_ci"https://rfc-editor.org/rfc/rfc7252[RFC7252: The Constrained Application Protocol (CoAP)]" 296c87c5fbaSopenharmony_ci 297c87c5fbaSopenharmony_cifor further information. 298c87c5fbaSopenharmony_ci 299c87c5fbaSopenharmony_ciBUGS 300c87c5fbaSopenharmony_ci---- 301c87c5fbaSopenharmony_ciPlease report bugs on the mailing list for libcoap: 302c87c5fbaSopenharmony_cilibcoap-developers@lists.sourceforge.net or raise an issue on GitHub at 303c87c5fbaSopenharmony_cihttps://github.com/obgm/libcoap/issues 304c87c5fbaSopenharmony_ci 305c87c5fbaSopenharmony_ciAUTHORS 306c87c5fbaSopenharmony_ci------- 307c87c5fbaSopenharmony_ciThe libcoap project <libcoap-developers@lists.sourceforge.net> 308