1c87c5fbaSopenharmony_ci// -*- mode:doc; -*- 2c87c5fbaSopenharmony_ci// vim: set syntax=asciidoc tw=0 3c87c5fbaSopenharmony_ci 4c87c5fbaSopenharmony_cicoap_uri(3) 5c87c5fbaSopenharmony_ci============ 6c87c5fbaSopenharmony_ci:doctype: manpage 7c87c5fbaSopenharmony_ci:man source: coap_uri 8c87c5fbaSopenharmony_ci:man version: @PACKAGE_VERSION@ 9c87c5fbaSopenharmony_ci:man manual: libcoap Manual 10c87c5fbaSopenharmony_ci 11c87c5fbaSopenharmony_ciNAME 12c87c5fbaSopenharmony_ci---- 13c87c5fbaSopenharmony_cicoap_uri, 14c87c5fbaSopenharmony_cicoap_split_uri, 15c87c5fbaSopenharmony_cicoap_split_proxy_uri, 16c87c5fbaSopenharmony_cicoap_new_uri, 17c87c5fbaSopenharmony_cicoap_clone_uri, 18c87c5fbaSopenharmony_cicoap_delete_uri, 19c87c5fbaSopenharmony_cicoap_uri_into_options 20c87c5fbaSopenharmony_ci- Work with CoAP URIs 21c87c5fbaSopenharmony_ci 22c87c5fbaSopenharmony_ciSYNOPSIS 23c87c5fbaSopenharmony_ci-------- 24c87c5fbaSopenharmony_ci*#include <coap@LIBCOAP_API_VERSION@/coap.h>* 25c87c5fbaSopenharmony_ci 26c87c5fbaSopenharmony_ci*int coap_split_uri(const uint8_t *_uri_def_, size_t _length_, 27c87c5fbaSopenharmony_cicoap_uri_t *_uri_);* 28c87c5fbaSopenharmony_ci 29c87c5fbaSopenharmony_ci*int coap_split_proxy_uri(const uint8_t *_uri_def_, size_t _length_, 30c87c5fbaSopenharmony_cicoap_uri_t *_uri_);* 31c87c5fbaSopenharmony_ci 32c87c5fbaSopenharmony_ci*coap_uri_t *coap_new_uri(const uint8_t *_uri_def_, unsigned int _length_);* 33c87c5fbaSopenharmony_ci 34c87c5fbaSopenharmony_ci*coap_uri_t *coap_clone_uri(const coap_uri_t *_uri_);* 35c87c5fbaSopenharmony_ci 36c87c5fbaSopenharmony_ci*void coap_delete_uri(coap_uri_t *_uri_);* 37c87c5fbaSopenharmony_ci 38c87c5fbaSopenharmony_ci*int coap_uri_into_options(const coap_uri_t *_uri_, 39c87c5fbaSopenharmony_ciconst coap_address_t *_dst_, coap_optlist_t **_optlist_chain_, 40c87c5fbaSopenharmony_ciint _create_port_host_opt_, uint8_t *_buf_, size_t _buflen_);* 41c87c5fbaSopenharmony_ci 42c87c5fbaSopenharmony_ciFor specific (D)TLS library support, link with 43c87c5fbaSopenharmony_ci*-lcoap-@LIBCOAP_API_VERSION@-notls*, *-lcoap-@LIBCOAP_API_VERSION@-gnutls*, 44c87c5fbaSopenharmony_ci*-lcoap-@LIBCOAP_API_VERSION@-openssl*, *-lcoap-@LIBCOAP_API_VERSION@-mbedtls* 45c87c5fbaSopenharmony_cior *-lcoap-@LIBCOAP_API_VERSION@-tinydtls*. Otherwise, link with 46c87c5fbaSopenharmony_ci*-lcoap-@LIBCOAP_API_VERSION@* to get the default (D)TLS library support. 47c87c5fbaSopenharmony_ci 48c87c5fbaSopenharmony_ciDESCRIPTION 49c87c5fbaSopenharmony_ci----------- 50c87c5fbaSopenharmony_ciThis man page describes the functionality available to work with CoAP URIs and 51c87c5fbaSopenharmony_cibreak them down into the component parts. 52c87c5fbaSopenharmony_ci 53c87c5fbaSopenharmony_ciA CoAP URI is of the form 54c87c5fbaSopenharmony_ci 55c87c5fbaSopenharmony_ci<scheme><host><(optional):port><(optional)path><(optional)?query> 56c87c5fbaSopenharmony_ci 57c87c5fbaSopenharmony_ciwhere _scheme_ can be one of coap://, coaps://, coap+tcp:// and coaps+tcp://. 58c87c5fbaSopenharmony_ci 59c87c5fbaSopenharmony_ci_host_ can be an IPv4 or IPv6 (enclosed in []) address, a DNS resolvable name or 60c87c5fbaSopenharmony_cia Unix domain socket name which is encoded as a unix file name with %2F 61c87c5fbaSopenharmony_cireplacing each / of the file name so that the start of the _path_ can easily be 62c87c5fbaSopenharmony_cidetermined. 63c87c5fbaSopenharmony_ci 64c87c5fbaSopenharmony_ci'(optional):port' is ignored for Unix domain socket's _host_ definitions. 65c87c5fbaSopenharmony_ci 66c87c5fbaSopenharmony_ciThe parsed uri structure is of the form 67c87c5fbaSopenharmony_ci 68c87c5fbaSopenharmony_ci[source, c] 69c87c5fbaSopenharmony_ci---- 70c87c5fbaSopenharmony_citypedef struct { 71c87c5fbaSopenharmony_ci coap_str_const_t host; /* The host part of the URI */ 72c87c5fbaSopenharmony_ci uint16_t port; /* The port in host byte order */ 73c87c5fbaSopenharmony_ci coap_str_const_t path; /* The complete path if present or {0, NULL}. 74c87c5fbaSopenharmony_ci Needs to be split using coap_split_path() 75c87c5fbaSopenharmony_ci or coap_uri_into_options(). */ 76c87c5fbaSopenharmony_ci coap_str_const_t query; /* The complete query if present or {0, NULL}. 77c87c5fbaSopenharmony_ci Needs to be split using coap_split_query() 78c87c5fbaSopenharmony_ci or coap_uri_into_options(). */ 79c87c5fbaSopenharmony_ci enum coap_uri_scheme_t scheme; /* The parsed scheme specifier. */ 80c87c5fbaSopenharmony_ci} coap_uri_t; 81c87c5fbaSopenharmony_ci---- 82c87c5fbaSopenharmony_ci 83c87c5fbaSopenharmony_ciFUNCTIONS 84c87c5fbaSopenharmony_ci--------- 85c87c5fbaSopenharmony_ci 86c87c5fbaSopenharmony_ci*Function: coap_split_uri()* 87c87c5fbaSopenharmony_ci 88c87c5fbaSopenharmony_ciThe *coap_split_uri*() function is used to parse the provided _uri_def_ of 89c87c5fbaSopenharmony_cilength _length_ into the component parts held in the _uri_ structure. These 90c87c5fbaSopenharmony_cicomponent parts are the host, port, path, query and the CoAP URI scheme. 91c87c5fbaSopenharmony_ci 92c87c5fbaSopenharmony_ci*Function: coap_split_proxy_uri()* 93c87c5fbaSopenharmony_ci 94c87c5fbaSopenharmony_ciThe *coap_split_proxy_uri*() function is used to parse the provided _uri_def_ of 95c87c5fbaSopenharmony_cilength _length_ into the component parts held in the _uri_ structure. These 96c87c5fbaSopenharmony_cicomponent parts are the host, port, path, query and the URI scheme. The 97c87c5fbaSopenharmony_cischemes also includes support for http:// and https:// as the proxy may need to 98c87c5fbaSopenharmony_cibe a coap-to-http proxy. 99c87c5fbaSopenharmony_ci 100c87c5fbaSopenharmony_ci*Function: coap_new_uri()* 101c87c5fbaSopenharmony_ci 102c87c5fbaSopenharmony_ciThe *coap_new_uri*() function creates a new coap_uri_t structure and populates 103c87c5fbaSopenharmony_ciit using *coap_split_uri*() with _uri_def_ and _length_ as input. The returned 104c87c5fbaSopenharmony_cicoap_uri_t structure needs to be freed off using *coap_delete_uri*(). 105c87c5fbaSopenharmony_ci 106c87c5fbaSopenharmony_ci*Function: coap_clone_uri()* 107c87c5fbaSopenharmony_ci 108c87c5fbaSopenharmony_ciThe *coap_clone_uri*() function duplicates a _uri_ coap_uri_t structure. 109c87c5fbaSopenharmony_ciThe returned coap_uri_t structure needs to be freed off using 110c87c5fbaSopenharmony_ci*coap_delete_uri*(). 111c87c5fbaSopenharmony_ci 112c87c5fbaSopenharmony_ci*Function: coap_delete_uri()* 113c87c5fbaSopenharmony_ci 114c87c5fbaSopenharmony_ciThe *coap_delete_uri*() function frees off a previously created _uri_ 115c87c5fbaSopenharmony_cicoap_uri_t structure. 116c87c5fbaSopenharmony_ci 117c87c5fbaSopenharmony_ci*Function: coap_uri_into_options()* 118c87c5fbaSopenharmony_ci 119c87c5fbaSopenharmony_ciThe *coap_uri_into_options*() function takes the _uri_ structure and then takes 120c87c5fbaSopenharmony_ciCoAP options derived from this information and adds them to _optlist_chain_. 121c87c5fbaSopenharmony_ciThe initial _optlist_chain_ entry should be set to NULL before 122c87c5fbaSopenharmony_cithis function is called (unless *coap_insert_optlist*(3) has been previously 123c87c5fbaSopenharmony_ciused). 124c87c5fbaSopenharmony_ci 125c87c5fbaSopenharmony_ciIf _dst_ is not NULL and _create_port_host_opt_ is not 0, then the Uri-Host 126c87c5fbaSopenharmony_cioption is added in if the _uri_ host definition is not an exact match with the 127c87c5fbaSopenharmony_ciascii readable version of _dst. 128c87c5fbaSopenharmony_ci 129c87c5fbaSopenharmony_ciIf the port is not the default port and _create_port_host_opt_ is not 0, then 130c87c5fbaSopenharmony_cithe Port option is added to _optlist_chain_. 131c87c5fbaSopenharmony_ci 132c87c5fbaSopenharmony_ciIf there is a path, then this is broken down into individual Path options for 133c87c5fbaSopenharmony_cieach segment which are then added to _optlist_chain_. 134c87c5fbaSopenharmony_ci 135c87c5fbaSopenharmony_ciLikewise, if 136c87c5fbaSopenharmony_cithere is a query, individual Query options for each segment are then added to 137c87c5fbaSopenharmony_ci_optlist_chain_. 138c87c5fbaSopenharmony_ci 139c87c5fbaSopenharmony_ci_buf_ provides a scratch buffer to use, of size _buflen_ bytes. _buf_ needs 140c87c5fbaSopenharmony_cito be as big as the path or query lengths. 141c87c5fbaSopenharmony_ci 142c87c5fbaSopenharmony_ci*NOTE:* It is the responsibility of the application to free off the entries 143c87c5fbaSopenharmony_ciadded to _optlist_chain_ using *coap_delete_optlist*(3). 144c87c5fbaSopenharmony_ci 145c87c5fbaSopenharmony_ciRETURN VALUES 146c87c5fbaSopenharmony_ci------------- 147c87c5fbaSopenharmony_ci*coap_split_uri*(), *coap_split_proxy_uri*(), and 148c87c5fbaSopenharmony_ci*coap_uri_into_options*() return 0 on success, else < 0 on failure. 149c87c5fbaSopenharmony_ci 150c87c5fbaSopenharmony_ci*coap_new_uri*() and *coap_clone_uri*() return a newly allocated coap_uri_t 151c87c5fbaSopenharmony_cistructure or NULL on failure. 152c87c5fbaSopenharmony_ci 153c87c5fbaSopenharmony_ciEXAMPLES 154c87c5fbaSopenharmony_ci-------- 155c87c5fbaSopenharmony_ci*Setup PDU and Transmit* 156c87c5fbaSopenharmony_ci 157c87c5fbaSopenharmony_ci[source, c] 158c87c5fbaSopenharmony_ci---- 159c87c5fbaSopenharmony_ci#include <coap@LIBCOAP_API_VERSION@/coap.h> 160c87c5fbaSopenharmony_ci 161c87c5fbaSopenharmony_ci/* 162c87c5fbaSopenharmony_ci * Returns 0 failure, 1 success 163c87c5fbaSopenharmony_ci */ 164c87c5fbaSopenharmony_cistatic int 165c87c5fbaSopenharmony_ciparse_and_send_uri(coap_session_t *session, const char *do_uri) { 166c87c5fbaSopenharmony_ci coap_uri_t uri; 167c87c5fbaSopenharmony_ci coap_optlist_t *optlist = NULL; 168c87c5fbaSopenharmony_ci coap_pdu_t *pdu; 169c87c5fbaSopenharmony_ci coap_proto_t proto = coap_session_get_proto(session); 170c87c5fbaSopenharmony_ci const coap_address_t *dst = coap_session_get_addr_remote(session); 171c87c5fbaSopenharmony_ci int res; 172c87c5fbaSopenharmony_ci coap_mid_t mid; 173c87c5fbaSopenharmony_ci#define BUFSIZE 100 174c87c5fbaSopenharmony_ci unsigned char buf[BUFSIZE]; 175c87c5fbaSopenharmony_ci 176c87c5fbaSopenharmony_ci /* Parse the URI */ 177c87c5fbaSopenharmony_ci res = coap_split_uri((const uint8_t*)do_uri, strlen(do_uri), &uri); 178c87c5fbaSopenharmony_ci if (res != 0) 179c87c5fbaSopenharmony_ci return 0; 180c87c5fbaSopenharmony_ci 181c87c5fbaSopenharmony_ci /* Check the scheme matches the session type */ 182c87c5fbaSopenharmony_ci switch (uri.scheme) { 183c87c5fbaSopenharmony_ci case COAP_URI_SCHEME_COAP: 184c87c5fbaSopenharmony_ci if (proto != COAP_PROTO_UDP) 185c87c5fbaSopenharmony_ci return 0; 186c87c5fbaSopenharmony_ci break; 187c87c5fbaSopenharmony_ci case COAP_URI_SCHEME_COAPS: 188c87c5fbaSopenharmony_ci if (proto != COAP_PROTO_DTLS) 189c87c5fbaSopenharmony_ci return 0; 190c87c5fbaSopenharmony_ci break; 191c87c5fbaSopenharmony_ci case COAP_URI_SCHEME_COAP_TCP: 192c87c5fbaSopenharmony_ci if (proto != COAP_PROTO_TCP) 193c87c5fbaSopenharmony_ci return 0; 194c87c5fbaSopenharmony_ci break; 195c87c5fbaSopenharmony_ci case COAP_URI_SCHEME_COAPS_TCP: 196c87c5fbaSopenharmony_ci if (proto != COAP_PROTO_TLS) 197c87c5fbaSopenharmony_ci return 0; 198c87c5fbaSopenharmony_ci break; 199c87c5fbaSopenharmony_ci case COAP_URI_SCHEME_COAP_WS: 200c87c5fbaSopenharmony_ci if (proto != COAP_PROTO_WS) 201c87c5fbaSopenharmony_ci return 0; 202c87c5fbaSopenharmony_ci break; 203c87c5fbaSopenharmony_ci case COAP_URI_SCHEME_COAPS_WS: 204c87c5fbaSopenharmony_ci if (proto != COAP_PROTO_WSS) 205c87c5fbaSopenharmony_ci return 0; 206c87c5fbaSopenharmony_ci break; 207c87c5fbaSopenharmony_ci /* Proxy support only */ 208c87c5fbaSopenharmony_ci case COAP_URI_SCHEME_HTTP: 209c87c5fbaSopenharmony_ci case COAP_URI_SCHEME_HTTPS: 210c87c5fbaSopenharmony_ci case COAP_URI_SCHEME_LAST: 211c87c5fbaSopenharmony_ci default: 212c87c5fbaSopenharmony_ci return 0; 213c87c5fbaSopenharmony_ci } 214c87c5fbaSopenharmony_ci 215c87c5fbaSopenharmony_ci /* construct CoAP message */ 216c87c5fbaSopenharmony_ci pdu = coap_pdu_init(COAP_MESSAGE_CON, 217c87c5fbaSopenharmony_ci COAP_REQUEST_CODE_GET, 218c87c5fbaSopenharmony_ci coap_new_message_id(session), 219c87c5fbaSopenharmony_ci coap_session_max_pdu_size(session)); 220c87c5fbaSopenharmony_ci if (pdu == NULL) 221c87c5fbaSopenharmony_ci return 0; 222c87c5fbaSopenharmony_ci 223c87c5fbaSopenharmony_ci /* Create all the necessary options from the URI */ 224c87c5fbaSopenharmony_ci res = coap_uri_into_options(&uri, dst, &optlist, 1, buf, sizeof(buf)); 225c87c5fbaSopenharmony_ci if (res != 0) 226c87c5fbaSopenharmony_ci return 0; 227c87c5fbaSopenharmony_ci 228c87c5fbaSopenharmony_ci /* Add option list (which will get sorted) to the PDU */ 229c87c5fbaSopenharmony_ci if (optlist) { 230c87c5fbaSopenharmony_ci res = coap_add_optlist_pdu(pdu, &optlist); 231c87c5fbaSopenharmony_ci coap_delete_optlist(optlist); 232c87c5fbaSopenharmony_ci if (res != 1) 233c87c5fbaSopenharmony_ci return 0; 234c87c5fbaSopenharmony_ci } 235c87c5fbaSopenharmony_ci 236c87c5fbaSopenharmony_ci /* and send the PDU */ 237c87c5fbaSopenharmony_ci mid = coap_send(session, pdu); 238c87c5fbaSopenharmony_ci if (mid == COAP_INVALID_MID) 239c87c5fbaSopenharmony_ci return 0; 240c87c5fbaSopenharmony_ci return 1; 241c87c5fbaSopenharmony_ci} 242c87c5fbaSopenharmony_ci 243c87c5fbaSopenharmony_ci---- 244c87c5fbaSopenharmony_ci 245c87c5fbaSopenharmony_ciSEE ALSO 246c87c5fbaSopenharmony_ci-------- 247c87c5fbaSopenharmony_ci*coap_endpoint_client*(3) and *coap_pdu_setup*(3). 248c87c5fbaSopenharmony_ci 249c87c5fbaSopenharmony_ciFURTHER INFORMATION 250c87c5fbaSopenharmony_ci------------------- 251c87c5fbaSopenharmony_ciSee 252c87c5fbaSopenharmony_ci 253c87c5fbaSopenharmony_ci"https://rfc-editor.org/rfc/rfc7252[RFC7252: The Constrained Application Protocol (CoAP)]" 254c87c5fbaSopenharmony_ci 255c87c5fbaSopenharmony_cifor further information. 256c87c5fbaSopenharmony_ci 257c87c5fbaSopenharmony_ciBUGS 258c87c5fbaSopenharmony_ci---- 259c87c5fbaSopenharmony_ciPlease report bugs on the mailing list for libcoap: 260c87c5fbaSopenharmony_cilibcoap-developers@lists.sourceforge.net or raise an issue on GitHub at 261c87c5fbaSopenharmony_cihttps://github.com/obgm/libcoap/issues 262c87c5fbaSopenharmony_ci 263c87c5fbaSopenharmony_ciAUTHORS 264c87c5fbaSopenharmony_ci------- 265c87c5fbaSopenharmony_ciThe libcoap project <libcoap-developers@lists.sourceforge.net> 266