1c87c5fbaSopenharmony_ci// -*- mode:doc; -*- 2c87c5fbaSopenharmony_ci// vim: set syntax=asciidoc tw=0 3c87c5fbaSopenharmony_ci 4c87c5fbaSopenharmony_cicoap_endpoint_client(3) 5c87c5fbaSopenharmony_ci======================= 6c87c5fbaSopenharmony_ci:doctype: manpage 7c87c5fbaSopenharmony_ci:man source: coap_endpoint_client 8c87c5fbaSopenharmony_ci:man version: @PACKAGE_VERSION@ 9c87c5fbaSopenharmony_ci:man manual: libcoap Manual 10c87c5fbaSopenharmony_ci 11c87c5fbaSopenharmony_ciNAME 12c87c5fbaSopenharmony_ci---- 13c87c5fbaSopenharmony_cicoap_endpoint_client, 14c87c5fbaSopenharmony_cicoap_new_client_session, 15c87c5fbaSopenharmony_cicoap_new_client_session_psk2, 16c87c5fbaSopenharmony_cicoap_new_client_session_pki, 17c87c5fbaSopenharmony_cicoap_session_set_mtu, 18c87c5fbaSopenharmony_cicoap_session_max_pdu_size 19c87c5fbaSopenharmony_ci- Work with CoAP client endpoints 20c87c5fbaSopenharmony_ci 21c87c5fbaSopenharmony_ciSYNOPSIS 22c87c5fbaSopenharmony_ci-------- 23c87c5fbaSopenharmony_ci*#include <coap@LIBCOAP_API_VERSION@/coap.h>* 24c87c5fbaSopenharmony_ci 25c87c5fbaSopenharmony_ci*coap_session_t *coap_new_client_session(coap_context_t *_context_, 26c87c5fbaSopenharmony_ciconst coap_address_t *_local_if_, const coap_address_t *_server_, 27c87c5fbaSopenharmony_cicoap_proto_t _proto_);* 28c87c5fbaSopenharmony_ci 29c87c5fbaSopenharmony_ci*coap_session_t *coap_new_client_session_psk2(coap_context_t *_context_, 30c87c5fbaSopenharmony_ciconst coap_address_t *_local_if_, const coap_address_t *_server_, coap_proto_t 31c87c5fbaSopenharmony_ci_proto_, coap_dtls_cpsk_t *_setup_data_);* 32c87c5fbaSopenharmony_ci 33c87c5fbaSopenharmony_ci*coap_session_t *coap_new_client_session_pki(coap_context_t *_context_, 34c87c5fbaSopenharmony_ciconst coap_address_t *_local_if_, const coap_address_t *_server_, coap_proto_t 35c87c5fbaSopenharmony_ci_proto_, coap_dtls_pki_t *_setup_data_);* 36c87c5fbaSopenharmony_ci 37c87c5fbaSopenharmony_ci*void coap_session_set_mtu(coap_session_t *_session_, unsigned _mtu_);* 38c87c5fbaSopenharmony_ci 39c87c5fbaSopenharmony_ci*size_t coap_session_max_pdu_size(const coap_session_t *_session_);* 40c87c5fbaSopenharmony_ci 41c87c5fbaSopenharmony_ciFor specific (D)TLS library support, link with 42c87c5fbaSopenharmony_ci*-lcoap-@LIBCOAP_API_VERSION@-notls*, *-lcoap-@LIBCOAP_API_VERSION@-gnutls*, 43c87c5fbaSopenharmony_ci*-lcoap-@LIBCOAP_API_VERSION@-openssl*, *-lcoap-@LIBCOAP_API_VERSION@-mbedtls* 44c87c5fbaSopenharmony_cior *-lcoap-@LIBCOAP_API_VERSION@-tinydtls*. Otherwise, link with 45c87c5fbaSopenharmony_ci*-lcoap-@LIBCOAP_API_VERSION@* to get the default (D)TLS library support. 46c87c5fbaSopenharmony_ci 47c87c5fbaSopenharmony_ciDESCRIPTION 48c87c5fbaSopenharmony_ci----------- 49c87c5fbaSopenharmony_ciThis man page focuses on the setting up of a CoAP client endpoint and hence 50c87c5fbaSopenharmony_cicreation of a CoAP _session_ used to connect to a server. For a CoAP server 51c87c5fbaSopenharmony_ciendpoint, see *coap_endpoint_server*(3). There is no need to call 52c87c5fbaSopenharmony_ci*coap_new_endpoint*(3) for a client as well as one of the 53c87c5fbaSopenharmony_ci*coap_new_client_server**() functions. 54c87c5fbaSopenharmony_ci 55c87c5fbaSopenharmony_ciThe CoAP stack's global state is stored in a coap_context_t _context_ object. 56c87c5fbaSopenharmony_ciResources, Endpoints and Sessions are associated with this _context_ object. 57c87c5fbaSopenharmony_ciThere can be more than one coap_context_t object per application, it is up to 58c87c5fbaSopenharmony_cithe application to manage each one accordingly. 59c87c5fbaSopenharmony_ci 60c87c5fbaSopenharmony_ciA CoAP _session_ maintains the state of an ongoing connection between a Client 61c87c5fbaSopenharmony_ciand Server which is stored in a coap_session_t _session_ object. A CoAP 62c87c5fbaSopenharmony_ci_session_ is tracked by local port, CoAP protocol, remote IP address and 63c87c5fbaSopenharmony_ciremote port, or in the case of Unix Domain sockets, the local path and the 64c87c5fbaSopenharmony_ciremote path. 65c87c5fbaSopenharmony_ci 66c87c5fbaSopenharmony_ciThe _session_ network traffic can be encrypted or un-encrypted if there is an 67c87c5fbaSopenharmony_ciunderlying TLS library. 68c87c5fbaSopenharmony_ci 69c87c5fbaSopenharmony_ciIf (D)TLS is going to be used for encrypting the network traffic, then the 70c87c5fbaSopenharmony_ci(D)TLS 71c87c5fbaSopenharmony_ciinformation for Pre-Shared Keys (PSK) or Public Key Infrastructure (PKI) needs 72c87c5fbaSopenharmony_cito be configured before any network traffic starts to flow. For Clients, this 73c87c5fbaSopenharmony_ciis done during the Client _session_ set up. 74c87c5fbaSopenharmony_ci 75c87c5fbaSopenharmony_ciFor Clients, all the encryption information can be held at the (D)TLS 76c87c5fbaSopenharmony_cicontext and CoAP _context_ levels, or at the (D)TLS session and CoAP 77c87c5fbaSopenharmony_ci_session_ levels. If defined at the _context_ level, then when a _session_ is 78c87c5fbaSopenharmony_cicreated, it will inherit the _context_ definitions, unless they have separately 79c87c5fbaSopenharmony_cibeen defined for the _session_ level, in which case the _session_ version will 80c87c5fbaSopenharmony_ciget used. Typically the information will be configured at the _session_ level 81c87c5fbaSopenharmony_cifor Clients. 82c87c5fbaSopenharmony_ci 83c87c5fbaSopenharmony_ciIn principle the set-up sequence for CoAP client endpoints looks like 84c87c5fbaSopenharmony_ci---- 85c87c5fbaSopenharmony_cicoap_new_context() 86c87c5fbaSopenharmony_cicoap_context_set_pki_root_cas() - if the root CAs need to be updated and using PKI 87c87c5fbaSopenharmony_cicoap_new_client_session(), coap_new_client_session_pki() or coap_new_client_session_psk2() 88c87c5fbaSopenharmony_ci---- 89c87c5fbaSopenharmony_ci 90c87c5fbaSopenharmony_ciMultiple client endpoints and hence sessions are supported per _context_. 91c87c5fbaSopenharmony_ci 92c87c5fbaSopenharmony_ciDifferent CoAP protocols can be defined for _proto_ - the current supported 93c87c5fbaSopenharmony_cilist is: 94c87c5fbaSopenharmony_ci 95c87c5fbaSopenharmony_ci[source, c] 96c87c5fbaSopenharmony_ci---- 97c87c5fbaSopenharmony_ciCOAP_PROTO_UDP 98c87c5fbaSopenharmony_ciCOAP_PROTO_DTLS 99c87c5fbaSopenharmony_ciCOAP_PROTO_TCP 100c87c5fbaSopenharmony_ciCOAP_PROTO_TLS 101c87c5fbaSopenharmony_ciCOAP_PROTO_WS 102c87c5fbaSopenharmony_ciCOAP_PROTO_WSS 103c87c5fbaSopenharmony_ci---- 104c87c5fbaSopenharmony_ci 105c87c5fbaSopenharmony_ci*coap_tcp_is_supported*(3), *coap_dtls_is_supported*(3), 106c87c5fbaSopenharmony_ci*coap_tls_is_supported*(3), *coap_ws_is_supported*(3) and 107c87c5fbaSopenharmony_ci*coap_wss_is_supported*(3) can be used for checking whether the underlying 108c87c5fbaSopenharmony_ciTCP, (D)TLS or WebSocket protocol support is available. 109c87c5fbaSopenharmony_ciSee *coap_tls_library(3)* for further information on the types of (D)TLS 110c87c5fbaSopenharmony_cisessions supported. 111c87c5fbaSopenharmony_ci 112c87c5fbaSopenharmony_ciLibcoap supports 3 different socket types: 113c87c5fbaSopenharmony_ci 114c87c5fbaSopenharmony_ci[source, c] 115c87c5fbaSopenharmony_ci---- 116c87c5fbaSopenharmony_ciAF_INET IPv4 IP addresses and ports 117c87c5fbaSopenharmony_ciAF_INET6 IPv6 IP addresses and ports and can be dual IPv4/IPv6 stacked 118c87c5fbaSopenharmony_ciAF_UNIX Unix Domain using file path names 119c87c5fbaSopenharmony_ci---- 120c87c5fbaSopenharmony_ci 121c87c5fbaSopenharmony_ciFor AF_INET and AF_INET6, the client does not need to specify a local IP 122c87c5fbaSopenharmony_ciaddress and/or port as default values will get filled in. However for AF_UNIX, 123c87c5fbaSopenharmony_cithe local pathname must be provided and must be unique per client session. This 124c87c5fbaSopenharmony_ciunique local pathname will get deleted on the session being properly closed at 125c87c5fbaSopenharmony_ciapplication exit. 126c87c5fbaSopenharmony_ci 127c87c5fbaSopenharmony_ciThe client must specify IP and port when defining the *coap_address_t* (see 128c87c5fbaSopenharmony_ci*coap_address_t*(3)) for the remote end of the session if AF_INET or AF_INET6. 129c87c5fbaSopenharmony_ciIf port is 0, then the default CoAP port is used instead. If AF_UNIX, the 130c87c5fbaSopenharmony_ciunix domain path to connect to must be specified. 131c87c5fbaSopenharmony_ci 132c87c5fbaSopenharmony_ciFUNCTIONS 133c87c5fbaSopenharmony_ci--------- 134c87c5fbaSopenharmony_ci 135c87c5fbaSopenharmony_ci*Function: coap_new_client_session()* 136c87c5fbaSopenharmony_ci 137c87c5fbaSopenharmony_ciThe *coap_new_client_session*() function creates a client endpoint for a 138c87c5fbaSopenharmony_cispecific _context_ and initiates a new client session to the specified 139c87c5fbaSopenharmony_ci_server_ using the CoAP protocol _proto_ as defined above. If the port is set 140c87c5fbaSopenharmony_cito 0 in _server_ (for AF_INET or AF_INET6), then the default CoAP port is used. 141c87c5fbaSopenharmony_ci 142c87c5fbaSopenharmony_ciNormally _local_if_ would be set to NULL, but by specifying 143c87c5fbaSopenharmony_ci_local_if_ the source of the network session can be bound to a specific IP 144c87c5fbaSopenharmony_ciaddress or port. For AF_UNIX, _local_if_ must be specified pointing to an 145c87c5fbaSopenharmony_ciappropriate *coap_address_t*. If _local_if_ is defined, the address families 146c87c5fbaSopenharmony_cifor _local_if_ and _server_ must be identical. The session will initially have 147c87c5fbaSopenharmony_cia reference count of 1. 148c87c5fbaSopenharmony_ci 149c87c5fbaSopenharmony_ciTo stop using a client session, the reference count must be decremented to 0 150c87c5fbaSopenharmony_ciby calling *coap_session_release*(3). See *coap_session*(3). This will remove 151c87c5fbaSopenharmony_cithe client endpoint's _session_ and all its associated information. 152c87c5fbaSopenharmony_ci 153c87c5fbaSopenharmony_ci*Function: coap_new_client_session_pki()* 154c87c5fbaSopenharmony_ci 155c87c5fbaSopenharmony_ciThe *coap_new_client_session_pki*() function, for a specific _context_, is 156c87c5fbaSopenharmony_ciused to configure the (D)TLS context using the _setup_data_ variables as defined 157c87c5fbaSopenharmony_ciin the coap_dtls_pki_t structure in the newly created endpoint session - 158c87c5fbaSopenharmony_cisee *coap_encryption*(3). The connection is to the specified _server_ using 159c87c5fbaSopenharmony_cithe CoAP protocol _proto_ as defined above. If the port is set to 0 in 160c87c5fbaSopenharmony_ci_server_ (for AF_INET or AF_INET6), then the default CoAP port is used. 161c87c5fbaSopenharmony_ci 162c87c5fbaSopenharmony_ciNormally _local_if_ would be set to NULL, but by specifying 163c87c5fbaSopenharmony_ci_local_if_ the source of the network session can be bound to a specific IP 164c87c5fbaSopenharmony_ciaddress or port. For AF_UNIX, _local_if_ must be specified pointing to an 165c87c5fbaSopenharmony_ciappropriate *coap_address_t*. If _local_if_ is defined, the address families 166c87c5fbaSopenharmony_cifor _local_if_ and _server_ must be identical. The session will initially have 167c87c5fbaSopenharmony_cia reference count of 1. 168c87c5fbaSopenharmony_ci 169c87c5fbaSopenharmony_ciTo stop using a client session, the reference count must be decremented to 0 170c87c5fbaSopenharmony_ciby calling *coap_session_release*(3). See *coap_session*(3). This will remove 171c87c5fbaSopenharmony_cithe client endpoint's _session_ and all its associated information. 172c87c5fbaSopenharmony_ci 173c87c5fbaSopenharmony_ci*Function: coap_new_client_session_psk2()* 174c87c5fbaSopenharmony_ci 175c87c5fbaSopenharmony_ciThe *coap_new_client_session_psk2*() function, for a specific _context_, is 176c87c5fbaSopenharmony_ciused to configure the (D)TLS context using the _setup_data_ variables as defined 177c87c5fbaSopenharmony_ciin the coap_dtls_cpsk_t structure in the newly created endpoint session - 178c87c5fbaSopenharmony_cisee *coap_encryption*(3). The connection is to the specified _server_ using 179c87c5fbaSopenharmony_cithe CoAP protocol _proto_ as defined above. If the port is set to 0 in 180c87c5fbaSopenharmony_ci_server_ (for AF_INET or AF_INET6), then the default CoAP port is used. 181c87c5fbaSopenharmony_ci 182c87c5fbaSopenharmony_ciNormally _local_if_ would be set to NULL, but by specifying 183c87c5fbaSopenharmony_ci_local_if_ the source of the network session can be bound to a specific IP 184c87c5fbaSopenharmony_ciaddress or port. For AF_UNIX, _local_if_ must be specified pointing to an 185c87c5fbaSopenharmony_ciappropriate *coap_address_t*. If _local_if_ is defined, the address families 186c87c5fbaSopenharmony_cifor _local_if_ and _server_ must be identical. The session will initially have 187c87c5fbaSopenharmony_cia reference count of 1. 188c87c5fbaSopenharmony_ci 189c87c5fbaSopenharmony_ciTo stop using a client session, the reference count must be decremented to 0 190c87c5fbaSopenharmony_ciby calling *coap_session_release*(3). See *coap_session*(3). This will remove 191c87c5fbaSopenharmony_cithe client endpoint's _session_ and all its associated information. 192c87c5fbaSopenharmony_ci 193c87c5fbaSopenharmony_ci*Function: coap_session_set_mtu()* 194c87c5fbaSopenharmony_ci 195c87c5fbaSopenharmony_ciThe *coap_session_set_mtu*() function is used to set the MTU size 196c87c5fbaSopenharmony_ci(the maximum message size) of the data in a packet, excluding any IP or 197c87c5fbaSopenharmony_ciTCP/UDP overhead to _mtu_ for the client endpoint's _session_. The default 198c87c5fbaSopenharmony_ciMTU is 1152. 199c87c5fbaSopenharmony_ci 200c87c5fbaSopenharmony_ci*Function: coap_session_max_pdu_size()* 201c87c5fbaSopenharmony_ci 202c87c5fbaSopenharmony_ciThe *coap_session_max_pdu_size*() function is used to get the maximum MTU 203c87c5fbaSopenharmony_cisize of the data for the client endpoint's _session_. 204c87c5fbaSopenharmony_ci 205c87c5fbaSopenharmony_ciRETURN VALUES 206c87c5fbaSopenharmony_ci------------- 207c87c5fbaSopenharmony_ci*coap_new_client_session*(), *coap_new_client_session_psk2*(), 208c87c5fbaSopenharmony_ci*coap_new_client_session_pki*() return a newly created client. 209c87c5fbaSopenharmony_cisession or NULL if there is a creation failure. 210c87c5fbaSopenharmony_ci 211c87c5fbaSopenharmony_ci*coap_session_max_pdu_size*() returns the MTU size. 212c87c5fbaSopenharmony_ci 213c87c5fbaSopenharmony_ciEXAMPLES 214c87c5fbaSopenharmony_ci-------- 215c87c5fbaSopenharmony_ci*CoAP Client Non-Encrypted Setup* 216c87c5fbaSopenharmony_ci[source, c] 217c87c5fbaSopenharmony_ci---- 218c87c5fbaSopenharmony_ci#include <coap@LIBCOAP_API_VERSION@/coap.h> 219c87c5fbaSopenharmony_ci 220c87c5fbaSopenharmony_ci#include <netinet/in.h> 221c87c5fbaSopenharmony_ci 222c87c5fbaSopenharmony_cistatic coap_session_t * 223c87c5fbaSopenharmony_cisetup_client_session (struct in_addr ip_address) { 224c87c5fbaSopenharmony_ci coap_session_t *session; 225c87c5fbaSopenharmony_ci coap_address_t server; 226c87c5fbaSopenharmony_ci /* See coap_context(3) */ 227c87c5fbaSopenharmony_ci coap_context_t *context = coap_new_context(NULL); 228c87c5fbaSopenharmony_ci 229c87c5fbaSopenharmony_ci if (!context) 230c87c5fbaSopenharmony_ci return NULL; 231c87c5fbaSopenharmony_ci /* See coap_block(3) */ 232c87c5fbaSopenharmony_ci coap_context_set_block_mode(context, 233c87c5fbaSopenharmony_ci COAP_BLOCK_USE_LIBCOAP | COAP_BLOCK_SINGLE_BODY); 234c87c5fbaSopenharmony_ci 235c87c5fbaSopenharmony_ci 236c87c5fbaSopenharmony_ci /* See coap_address(3) */ 237c87c5fbaSopenharmony_ci coap_address_init(&server); 238c87c5fbaSopenharmony_ci server.addr.sa.sa_family = AF_INET; 239c87c5fbaSopenharmony_ci server.addr.sin.sin_addr = ip_address; 240c87c5fbaSopenharmony_ci server.addr.sin.sin_port = htons (5683); 241c87c5fbaSopenharmony_ci 242c87c5fbaSopenharmony_ci session = coap_new_client_session(context, NULL, &server, COAP_PROTO_UDP); 243c87c5fbaSopenharmony_ci if (!session) { 244c87c5fbaSopenharmony_ci coap_free_context(context); 245c87c5fbaSopenharmony_ci return NULL; 246c87c5fbaSopenharmony_ci } 247c87c5fbaSopenharmony_ci /* The context is in session->context */ 248c87c5fbaSopenharmony_ci return session; 249c87c5fbaSopenharmony_ci} 250c87c5fbaSopenharmony_ci---- 251c87c5fbaSopenharmony_ci 252c87c5fbaSopenharmony_ci*CoAP Client Non-Encrypted Unix Domain Setup* 253c87c5fbaSopenharmony_ci[source, c] 254c87c5fbaSopenharmony_ci---- 255c87c5fbaSopenharmony_ci#include <coap@LIBCOAP_API_VERSION@/coap.h> 256c87c5fbaSopenharmony_ci 257c87c5fbaSopenharmony_ci#include <stdio.h> 258c87c5fbaSopenharmony_ci#include <sys/types.h> 259c87c5fbaSopenharmony_ci#include <unistd.h> 260c87c5fbaSopenharmony_ci 261c87c5fbaSopenharmony_cistatic coap_session_t * 262c87c5fbaSopenharmony_cisetup_client_session (const char *server_ud) { 263c87c5fbaSopenharmony_ci coap_session_t *session; 264c87c5fbaSopenharmony_ci coap_address_t server; 265c87c5fbaSopenharmony_ci coap_address_t local; 266c87c5fbaSopenharmony_ci /* See coap_context(3) */ 267c87c5fbaSopenharmony_ci coap_context_t *context = coap_new_context(NULL); 268c87c5fbaSopenharmony_ci 269c87c5fbaSopenharmony_ci if (!context) 270c87c5fbaSopenharmony_ci return NULL; 271c87c5fbaSopenharmony_ci /* See coap_block(3) */ 272c87c5fbaSopenharmony_ci coap_context_set_block_mode(context, 273c87c5fbaSopenharmony_ci COAP_BLOCK_USE_LIBCOAP | COAP_BLOCK_SINGLE_BODY); 274c87c5fbaSopenharmony_ci 275c87c5fbaSopenharmony_ci 276c87c5fbaSopenharmony_ci /* See coap_address(3) */ 277c87c5fbaSopenharmony_ci coap_address_init(&server); 278c87c5fbaSopenharmony_ci server.addr.sa.sa_family = AF_UNIX; 279c87c5fbaSopenharmony_ci snprintf(server.addr.cun.sun_path, sizeof(server.addr.cun.sun_path), 280c87c5fbaSopenharmony_ci "%s", server_ud); 281c87c5fbaSopenharmony_ci 282c87c5fbaSopenharmony_ci /* Need to have a uniquely named local address */ 283c87c5fbaSopenharmony_ci coap_address_init(&local); 284c87c5fbaSopenharmony_ci local.addr.sa.sa_family = AF_UNIX; 285c87c5fbaSopenharmony_ci snprintf(local.addr.cun.sun_path, sizeof(server.addr.cun.sun_path), 286c87c5fbaSopenharmony_ci "/tmp/client.%d", getpid()); 287c87c5fbaSopenharmony_ci /* Only do this if you know it is safe to do so */ 288c87c5fbaSopenharmony_ci unlink(local.addr.cun.sun_path); 289c87c5fbaSopenharmony_ci 290c87c5fbaSopenharmony_ci session = coap_new_client_session(context, &local, &server, COAP_PROTO_UDP); 291c87c5fbaSopenharmony_ci if (!session) { 292c87c5fbaSopenharmony_ci coap_free_context(context); 293c87c5fbaSopenharmony_ci return NULL; 294c87c5fbaSopenharmony_ci } 295c87c5fbaSopenharmony_ci /* The context is in session->context */ 296c87c5fbaSopenharmony_ci return session; 297c87c5fbaSopenharmony_ci} 298c87c5fbaSopenharmony_ci---- 299c87c5fbaSopenharmony_ci 300c87c5fbaSopenharmony_ci*CoAP Client PKI Setup* 301c87c5fbaSopenharmony_ci[source, c] 302c87c5fbaSopenharmony_ci---- 303c87c5fbaSopenharmony_ci#include <coap@LIBCOAP_API_VERSION@/coap.h> 304c87c5fbaSopenharmony_ci 305c87c5fbaSopenharmony_ci#include <netinet/in.h> 306c87c5fbaSopenharmony_ci 307c87c5fbaSopenharmony_cistatic int 308c87c5fbaSopenharmony_civerify_cn_callback(const char *cn, 309c87c5fbaSopenharmony_ci const uint8_t *asn1_public_cert, 310c87c5fbaSopenharmony_ci size_t asn1_length, 311c87c5fbaSopenharmony_ci coap_session_t *c_session, 312c87c5fbaSopenharmony_ci unsigned int depth, 313c87c5fbaSopenharmony_ci int validated, 314c87c5fbaSopenharmony_ci void *arg 315c87c5fbaSopenharmony_ci) { 316c87c5fbaSopenharmony_ci /* Remove (void) definition if variable is used */ 317c87c5fbaSopenharmony_ci (void)cn; 318c87c5fbaSopenharmony_ci (void)asn1_public_cert; 319c87c5fbaSopenharmony_ci (void)asn1_length; 320c87c5fbaSopenharmony_ci (void)c_session; 321c87c5fbaSopenharmony_ci (void)depth; 322c87c5fbaSopenharmony_ci (void)validated; 323c87c5fbaSopenharmony_ci (void)arg; 324c87c5fbaSopenharmony_ci 325c87c5fbaSopenharmony_ci /* Check that the CN is valid */ 326c87c5fbaSopenharmony_ci 327c87c5fbaSopenharmony_ci /* ... */ 328c87c5fbaSopenharmony_ci 329c87c5fbaSopenharmony_ci return 1; 330c87c5fbaSopenharmony_ci} 331c87c5fbaSopenharmony_ci 332c87c5fbaSopenharmony_cistatic coap_session_t * 333c87c5fbaSopenharmony_cisetup_client_session_pki (struct in_addr ip_address, 334c87c5fbaSopenharmony_ci const char *public_cert_file, 335c87c5fbaSopenharmony_ci const char *private_key_file, 336c87c5fbaSopenharmony_ci const char *ca_file 337c87c5fbaSopenharmony_ci) { 338c87c5fbaSopenharmony_ci coap_session_t *session; 339c87c5fbaSopenharmony_ci coap_address_t server; 340c87c5fbaSopenharmony_ci coap_dtls_pki_t dtls_pki; 341c87c5fbaSopenharmony_ci /* See coap_context(3) */ 342c87c5fbaSopenharmony_ci coap_context_t *context = coap_new_context(NULL); 343c87c5fbaSopenharmony_ci 344c87c5fbaSopenharmony_ci if (!context) 345c87c5fbaSopenharmony_ci return NULL; 346c87c5fbaSopenharmony_ci /* See coap_block(3) */ 347c87c5fbaSopenharmony_ci coap_context_set_block_mode(context, 348c87c5fbaSopenharmony_ci COAP_BLOCK_USE_LIBCOAP | COAP_BLOCK_SINGLE_BODY); 349c87c5fbaSopenharmony_ci 350c87c5fbaSopenharmony_ci 351c87c5fbaSopenharmony_ci /* See coap_address(3) */ 352c87c5fbaSopenharmony_ci coap_address_init(&server); 353c87c5fbaSopenharmony_ci server.addr.sa.sa_family = AF_INET; 354c87c5fbaSopenharmony_ci server.addr.sin.sin_addr = ip_address; 355c87c5fbaSopenharmony_ci server.addr.sin.sin_port = htons (5684); 356c87c5fbaSopenharmony_ci 357c87c5fbaSopenharmony_ci memset (&dtls_pki, 0, sizeof (dtls_pki)); 358c87c5fbaSopenharmony_ci 359c87c5fbaSopenharmony_ci /* See coap_encryption(3) */ 360c87c5fbaSopenharmony_ci dtls_pki.version = COAP_DTLS_PKI_SETUP_VERSION; 361c87c5fbaSopenharmony_ci dtls_pki.verify_peer_cert = 1; 362c87c5fbaSopenharmony_ci dtls_pki.check_common_ca = 1; 363c87c5fbaSopenharmony_ci dtls_pki.allow_self_signed = 1; 364c87c5fbaSopenharmony_ci dtls_pki.allow_expired_certs = 1; 365c87c5fbaSopenharmony_ci dtls_pki.cert_chain_validation = 1; 366c87c5fbaSopenharmony_ci dtls_pki.cert_chain_verify_depth = 1; 367c87c5fbaSopenharmony_ci dtls_pki.check_cert_revocation = 1; 368c87c5fbaSopenharmony_ci dtls_pki.allow_no_crl = 1; 369c87c5fbaSopenharmony_ci dtls_pki.allow_expired_crl = 1; 370c87c5fbaSopenharmony_ci dtls_pki.allow_bad_md_hash = 0; 371c87c5fbaSopenharmony_ci dtls_pki.allow_short_rsa_length = 0; 372c87c5fbaSopenharmony_ci dtls_pki.is_rpk_not_cert = 0; /* Set to 1 if RPK */ 373c87c5fbaSopenharmony_ci dtls_pki.validate_cn_call_back = verify_cn_callback; 374c87c5fbaSopenharmony_ci dtls_pki.cn_call_back_arg = NULL; 375c87c5fbaSopenharmony_ci dtls_pki.validate_sni_call_back = NULL; 376c87c5fbaSopenharmony_ci dtls_pki.sni_call_back_arg = NULL; 377c87c5fbaSopenharmony_ci dtls_pki.additional_tls_setup_call_back = NULL; 378c87c5fbaSopenharmony_ci dtls_pki.client_sni = NULL; 379c87c5fbaSopenharmony_ci dtls_pki.pki_key.key_type = COAP_PKI_KEY_PEM; 380c87c5fbaSopenharmony_ci dtls_pki.pki_key.key.pem.ca_file = ca_file; 381c87c5fbaSopenharmony_ci dtls_pki.pki_key.key.pem.public_cert = public_cert_file; 382c87c5fbaSopenharmony_ci dtls_pki.pki_key.key.pem.private_key = private_key_file; 383c87c5fbaSopenharmony_ci 384c87c5fbaSopenharmony_ci session = coap_new_client_session_pki(context, NULL, &server, 385c87c5fbaSopenharmony_ci COAP_PROTO_DTLS, &dtls_pki); 386c87c5fbaSopenharmony_ci if (!session) { 387c87c5fbaSopenharmony_ci coap_free_context(context); 388c87c5fbaSopenharmony_ci return NULL; 389c87c5fbaSopenharmony_ci } 390c87c5fbaSopenharmony_ci /* The context is in session->context */ 391c87c5fbaSopenharmony_ci return session; 392c87c5fbaSopenharmony_ci} 393c87c5fbaSopenharmony_ci---- 394c87c5fbaSopenharmony_ci 395c87c5fbaSopenharmony_ci*CoAP Client PSK Setup* 396c87c5fbaSopenharmony_ci[source, c] 397c87c5fbaSopenharmony_ci---- 398c87c5fbaSopenharmony_ci#include <coap@LIBCOAP_API_VERSION@/coap.h> 399c87c5fbaSopenharmony_ci 400c87c5fbaSopenharmony_ci#include <stdio.h> 401c87c5fbaSopenharmony_ci#include <netinet/in.h> 402c87c5fbaSopenharmony_ci 403c87c5fbaSopenharmony_ci#ifndef min 404c87c5fbaSopenharmony_ci#define min(a,b) ((a) < (b) ? (a) : (b)) 405c87c5fbaSopenharmony_ci#endif 406c87c5fbaSopenharmony_ci 407c87c5fbaSopenharmony_cistatic const coap_dtls_cpsk_info_t * 408c87c5fbaSopenharmony_civerify_ih_callback(coap_str_const_t *hint, 409c87c5fbaSopenharmony_ci coap_session_t *c_session, 410c87c5fbaSopenharmony_ci void *arg 411c87c5fbaSopenharmony_ci) { 412c87c5fbaSopenharmony_ci coap_dtls_cpsk_info_t *psk_info = (coap_dtls_cpsk_info_t *)arg; 413c87c5fbaSopenharmony_ci /* Remove (void) definition if variable is used */ 414c87c5fbaSopenharmony_ci (void)c_session; 415c87c5fbaSopenharmony_ci 416c87c5fbaSopenharmony_ci coap_log_info("Identity Hint '%.*s' provided\n", (int)hint->length, hint->s); 417c87c5fbaSopenharmony_ci 418c87c5fbaSopenharmony_ci /* Just use the defined information for now as passed in by arg */ 419c87c5fbaSopenharmony_ci return psk_info; 420c87c5fbaSopenharmony_ci} 421c87c5fbaSopenharmony_ci 422c87c5fbaSopenharmony_cistatic coap_dtls_cpsk_t dtls_psk; 423c87c5fbaSopenharmony_cistatic char client_sni[256]; 424c87c5fbaSopenharmony_ci 425c87c5fbaSopenharmony_cistatic coap_session_t * 426c87c5fbaSopenharmony_cisetup_client_session_psk (const char *uri, 427c87c5fbaSopenharmony_ci struct in_addr ip_address, 428c87c5fbaSopenharmony_ci const uint8_t *identity, 429c87c5fbaSopenharmony_ci unsigned int identity_len, 430c87c5fbaSopenharmony_ci const uint8_t *key, 431c87c5fbaSopenharmony_ci unsigned int key_len 432c87c5fbaSopenharmony_ci) { 433c87c5fbaSopenharmony_ci coap_session_t *session; 434c87c5fbaSopenharmony_ci coap_address_t server; 435c87c5fbaSopenharmony_ci /* See coap_context(3) */ 436c87c5fbaSopenharmony_ci coap_context_t *context = coap_new_context(NULL); 437c87c5fbaSopenharmony_ci 438c87c5fbaSopenharmony_ci if (!context) 439c87c5fbaSopenharmony_ci return NULL; 440c87c5fbaSopenharmony_ci /* See coap_block(3) */ 441c87c5fbaSopenharmony_ci coap_context_set_block_mode(context, 442c87c5fbaSopenharmony_ci COAP_BLOCK_USE_LIBCOAP | COAP_BLOCK_SINGLE_BODY); 443c87c5fbaSopenharmony_ci 444c87c5fbaSopenharmony_ci 445c87c5fbaSopenharmony_ci /* See coap_address(3) */ 446c87c5fbaSopenharmony_ci coap_address_init(&server); 447c87c5fbaSopenharmony_ci server.addr.sa.sa_family = AF_INET; 448c87c5fbaSopenharmony_ci server.addr.sin.sin_addr = ip_address; 449c87c5fbaSopenharmony_ci server.addr.sin.sin_port = htons (5684); 450c87c5fbaSopenharmony_ci 451c87c5fbaSopenharmony_ci /* See coap_encryption(3) */ 452c87c5fbaSopenharmony_ci memset (&dtls_psk, 0, sizeof(dtls_psk)); 453c87c5fbaSopenharmony_ci dtls_psk.version = COAP_DTLS_CPSK_SETUP_VERSION; 454c87c5fbaSopenharmony_ci dtls_psk.validate_ih_call_back = verify_ih_callback; 455c87c5fbaSopenharmony_ci dtls_psk.ih_call_back_arg = &dtls_psk.psk_info; 456c87c5fbaSopenharmony_ci if (uri) 457c87c5fbaSopenharmony_ci memcpy(client_sni, uri, min(strlen(uri), sizeof(client_sni)-1)); 458c87c5fbaSopenharmony_ci else 459c87c5fbaSopenharmony_ci memcpy(client_sni, "localhost", 9); 460c87c5fbaSopenharmony_ci dtls_psk.client_sni = client_sni; 461c87c5fbaSopenharmony_ci dtls_psk.psk_info.identity.s = identity; 462c87c5fbaSopenharmony_ci dtls_psk.psk_info.identity.length = identity_len; 463c87c5fbaSopenharmony_ci dtls_psk.psk_info.key.s = key; 464c87c5fbaSopenharmony_ci dtls_psk.psk_info.key.length = key_len; 465c87c5fbaSopenharmony_ci session = coap_new_client_session_psk2(context, NULL, &server, 466c87c5fbaSopenharmony_ci COAP_PROTO_DTLS, &dtls_psk); 467c87c5fbaSopenharmony_ci if (!session) { 468c87c5fbaSopenharmony_ci coap_free_context(context); 469c87c5fbaSopenharmony_ci return NULL; 470c87c5fbaSopenharmony_ci } 471c87c5fbaSopenharmony_ci /* The context is in session->context */ 472c87c5fbaSopenharmony_ci return session; 473c87c5fbaSopenharmony_ci} 474c87c5fbaSopenharmony_ci---- 475c87c5fbaSopenharmony_ci 476c87c5fbaSopenharmony_ci*CoAP Client Anonymous PKI Setup* 477c87c5fbaSopenharmony_ci[source, c] 478c87c5fbaSopenharmony_ci---- 479c87c5fbaSopenharmony_ci#include <coap@LIBCOAP_API_VERSION@/coap.h> 480c87c5fbaSopenharmony_ci 481c87c5fbaSopenharmony_ci#include <netinet/in.h> 482c87c5fbaSopenharmony_ci 483c87c5fbaSopenharmony_cistatic coap_session_t * 484c87c5fbaSopenharmony_cisetup_client_session_dtls (struct in_addr ip_address) { 485c87c5fbaSopenharmony_ci coap_session_t *session; 486c87c5fbaSopenharmony_ci coap_address_t server; 487c87c5fbaSopenharmony_ci /* See coap_context(3) */ 488c87c5fbaSopenharmony_ci coap_context_t *context = coap_new_context(NULL); 489c87c5fbaSopenharmony_ci 490c87c5fbaSopenharmony_ci if (!context) 491c87c5fbaSopenharmony_ci return NULL; 492c87c5fbaSopenharmony_ci /* See coap_block(3) */ 493c87c5fbaSopenharmony_ci coap_context_set_block_mode(context, 494c87c5fbaSopenharmony_ci COAP_BLOCK_USE_LIBCOAP | COAP_BLOCK_SINGLE_BODY); 495c87c5fbaSopenharmony_ci 496c87c5fbaSopenharmony_ci 497c87c5fbaSopenharmony_ci /* See coap_address(3) */ 498c87c5fbaSopenharmony_ci coap_address_init(&server); 499c87c5fbaSopenharmony_ci server.addr.sa.sa_family = AF_INET; 500c87c5fbaSopenharmony_ci server.addr.sin.sin_addr = ip_address; 501c87c5fbaSopenharmony_ci server.addr.sin.sin_port = htons (5683); 502c87c5fbaSopenharmony_ci 503c87c5fbaSopenharmony_ci session = coap_new_client_session(context, NULL, &server, 504c87c5fbaSopenharmony_ci COAP_PROTO_DTLS); 505c87c5fbaSopenharmony_ci if (!session) { 506c87c5fbaSopenharmony_ci coap_free_context(context); 507c87c5fbaSopenharmony_ci return NULL; 508c87c5fbaSopenharmony_ci } 509c87c5fbaSopenharmony_ci /* The context is in session->context */ 510c87c5fbaSopenharmony_ci return session; 511c87c5fbaSopenharmony_ci} 512c87c5fbaSopenharmony_ci---- 513c87c5fbaSopenharmony_ci 514c87c5fbaSopenharmony_ciSEE ALSO 515c87c5fbaSopenharmony_ci-------- 516c87c5fbaSopenharmony_ci*coap_address*(3), *coap_block*(3), *coap_context*(3), *coap_encryption*(3), 517c87c5fbaSopenharmony_ci*coap_endpoint_server*(3), *coap_resource*(3), *coap_session*(3) and 518c87c5fbaSopenharmony_ci*coap_tls_library*(3) 519c87c5fbaSopenharmony_ci 520c87c5fbaSopenharmony_ciFURTHER INFORMATION 521c87c5fbaSopenharmony_ci------------------- 522c87c5fbaSopenharmony_ciSee 523c87c5fbaSopenharmony_ci 524c87c5fbaSopenharmony_ci"https://rfc-editor.org/rfc/rfc7252[RFC7252: The Constrained Application Protocol (CoAP)]" 525c87c5fbaSopenharmony_ci 526c87c5fbaSopenharmony_ci"https://rfc-editor.org/rfc/rfc8323[RFC8323: CoAP (Constrained Application Protocol) over TCP, TLS, and WebSockets]" 527c87c5fbaSopenharmony_ci 528c87c5fbaSopenharmony_cifor further information. 529c87c5fbaSopenharmony_ci 530c87c5fbaSopenharmony_ciBUGS 531c87c5fbaSopenharmony_ci---- 532c87c5fbaSopenharmony_ciPlease report bugs on the mailing list for libcoap: 533c87c5fbaSopenharmony_cilibcoap-developers@lists.sourceforge.net or raise an issue on GitHub at 534c87c5fbaSopenharmony_cihttps://github.com/obgm/libcoap/issues 535c87c5fbaSopenharmony_ci 536c87c5fbaSopenharmony_ciAUTHORS 537c87c5fbaSopenharmony_ci------- 538c87c5fbaSopenharmony_ciThe libcoap project <libcoap-developers@lists.sourceforge.net> 539