64#define LOG_MODULE "coap-uip"
65#define LOG_LEVEL LOG_LEVEL_COAP
72#if COAP_MAX_PACKET_SIZE > (UIP_BUFSIZE - UIP_IPH_LEN - UIP_UDPH_LEN)
73#error "UIP_CONF_BUFFER_SIZE too small for COAP_MAX_CHUNK_SIZE"
76#define SERVER_LISTEN_PORT UIP_HTONS(COAP_DEFAULT_PORT)
77#define SERVER_LISTEN_SECURE_PORT UIP_HTONS(COAP_DEFAULT_SECURE_PORT)
82#ifdef COAP_DTLS_CONF_WITH_PSK
83static int coap_ep_get_dtls_psk_info(
const coap_endpoint_t *ep,
86#ifdef COAP_DTLS_CONF_WITH_CERT
87static int coap_ep_get_dtls_cert_info(
const coap_endpoint_t *ep,
93PROCESS(coap_engine,
"CoAP Engine");
102 LOG_OUTPUT(
"(NULL EP)");
106 LOG_OUTPUT(
"coaps://[");
108 LOG_OUTPUT(
"coap://[");
111 LOG_OUTPUT(
"]:%u", uip_ntohs(ep->port));
127 printf(
"]:%u", uip_ntohs(ep->port));
134 if(buf == NULL || size == 0) {
138 n = snprintf(buf, size - 1,
"(NULL EP)");
141 n = snprintf(buf, size - 1,
"coaps://[");
143 n = snprintf(buf, size - 1,
"coap://[");
149 n += snprintf(&buf[n], size -n - 1,
"]:%u", uip_ntohs(ep->port));
153 buf[size - 1] =
'\0';
160 const coap_endpoint_t *from)
163 destination->port = from->port;
164 destination->secure = from->secure;
170 if(!uip_ipaddr_cmp(&e1->ipaddr, &e2->ipaddr)) {
173 return e1->port == e2->port && e1->secure == e2->secure;
177index_of(
const char *data,
int offset,
int len, uint8_t c)
182 for(; offset < len; offset++) {
183 if(data[offset] == c) {
191get_port(
const char *inbuf,
size_t len, uint32_t *value)
195 for(i = 0; i < len; i++) {
196 if(inbuf[i] >=
'0' && inbuf[i] <=
'9') {
197 *value = *value * 10 + (inbuf[i] -
'0');
209 int start = index_of(text, 0, size,
'[');
210 int end = index_of(text,
start, size,
']');
213 ep->secure = strncmp(text,
"coaps:", 6) == 0;
216 if(text[end + 1] ==
':' &&
217 get_port(text + end + 2, size - end - 2, &port)) {
219 }
else if(ep->secure) {
221 ep->port = SERVER_LISTEN_SECURE_PORT;
223 ep->port = SERVER_LISTEN_PORT;
226 }
else if(size < UIPLIB_IPV6_MAX_STR_LEN) {
227 char buf[UIPLIB_IPV6_MAX_STR_LEN];
228 memcpy(buf, text, size);
231 ep->port = SERVER_LISTEN_PORT;
238static const coap_endpoint_t *
239get_src_endpoint(uint8_t secure)
241 static coap_endpoint_t src;
243 src.port = UIP_UDP_BUF->srcport;
257#ifndef CONTIKI_TARGET_NATIVE
267 LOG_DBG(
"DTLS peer state for ");
272 LOG_DBG(
"DTLS did not find peer ");
287#ifdef COAP_DTLS_CONF_WITH_CLIENT
288#ifdef COAP_DTLS_CONF_WITH_PSK
291#ifdef COAP_DTLS_CONF_WITH_CERT
298 LOG_DBG(
"connect to ");
305 LOG_DBG(
"DTLS connect to ");
309#ifdef COAP_DTLS_CONF_WITH_CLIENT
310#ifdef COAP_DTLS_CONF_WITH_PSK
311 if(coap_ep_get_dtls_psk_info(ep, &psk_info) == 1) {
312 return coap_ep_dtls_connect(ep, COAP_DTLS_SEC_MODE_PSK, &psk_info);
315#ifdef COAP_DTLS_CONF_WITH_CERT
316 if(coap_ep_get_dtls_cert_info(ep, &cert_info) == 1) {
317 return coap_ep_dtls_connect(ep, COAP_DTLS_SEC_MODE_CERT, &cert_info);
320 LOG_ERR(
"Unable to retrieve DTLS authorization info for \n");
330#ifdef COAP_DTLS_CONF_WITH_SERVER
335#ifdef COAP_DTLS_CONF_WITH_CERT
338#ifdef COAP_DTLS_CONF_WITH_PSK
342#ifdef COAP_DTLS_CONF_WITH_PSK
343 if(coap_ep_get_dtls_psk_info(&ep, &psk_info) == 1) {
344 return coap_dtls_server_setup(COAP_DTLS_SEC_MODE_PSK, &psk_info);
348#ifdef COAP_DTLS_CONF_WITH_CERT
349 if(coap_ep_get_dtls_cert_info(&ep, &cert_info) == 1) {
350 return coap_dtls_server_setup(COAP_DTLS_SEC_MODE_CERT, &cert_info);
379#if COAP_DTLS_KEYSTORE_CONF_WITH_SIMPLE
388process_secure_data(
void)
390 LOG_INFO(
"receiving secure UDP datagram from [");
392 LOG_INFO_(
"]:%u\n", uip_ntohs(UIP_UDP_BUF->srcport));
398 (
const coap_endpoint_t *) get_src_endpoint(1))) > 0) {
399 coap_receive(get_src_endpoint(1),
uip_appdata, ret);
407 LOG_INFO(
"receiving UDP datagram from [");
409 LOG_INFO_(
"]:%u\n", uip_ntohs(UIP_UDP_BUF->srcport));
416coap_sendto(
const coap_endpoint_t *ep,
const uint8_t *data, uint16_t length)
419 LOG_WARN(
"failed to send - no endpoint\n");
424 LOG_WARN(
"endpoint ");
425 LOG_WARN_COAP_EP(ep);
426 LOG_WARN_(
" not connected - dropping packet\n");
434 LOG_INFO(
"sent DTLS to ");
435 LOG_INFO_COAP_EP(ep);
437 LOG_INFO_(
" - error %d\n", ret);
439 LOG_INFO_(
" %d/%u bytes\n", ret, length);
445 uip_udp_packet_sendto(udp_conn, data, length, &ep->ipaddr, ep->port);
446 LOG_INFO(
"sent to ");
447 LOG_INFO_COAP_EP(ep);
448 LOG_INFO_(
" %u bytes\n", length);
457 udp_conn =
udp_new(NULL, 0, NULL);
458 if(udp_conn == NULL) {
459 LOG_ERR(
"No UDP connection available, exiting the process!\n");
463 udp_bind(udp_conn, SERVER_LISTEN_PORT);
464 LOG_INFO(
"Listening on port %u\n", uip_ntohs(udp_conn->
lport));
468 dtls_conn =
udp_new(NULL, 0, NULL);
469 if(dtls_conn != NULL) {
470 udp_bind(dtls_conn, SERVER_LISTEN_SECURE_PORT);
471 LOG_INFO(
"DTLS listening on port %u\n", uip_ntohs(dtls_conn->
lport));
479 if(ev == PROCESS_EVENT_POLL || ev == PROCESS_EVENT_TIMER) {
488 process_secure_data();
507 dtls_keystore = keystore;
510#ifdef COAP_DTLS_CONF_WITH_PSK
512coap_ep_get_dtls_psk_info(
const coap_endpoint_t *ep,
515 if(NULL != dtls_keystore) {
516 if(dtls_keystore->coap_get_psk_info) {
518 dtls_keystore->coap_get_psk_info(ep, info);
520 dtls_keystore->coap_get_psk_info(ep, info);
528#ifdef COAP_DTLS_CONF_WITH_CERT
530coap_ep_get_dtls_cert_info(
const coap_endpoint_t *ep,
533 if(dtls_keystore != NULL && dtls_keystore->coap_get_cert_info != NULL) {
534 return dtls_keystore->coap_get_cert_info(ep, info);
Collection of constants specified in the CoAP standard.
API to address CoAP endpoints.
CoAP engine implementation.
A simple keystore with fixed credentials.
CoAP module for reliable transport.
An implementation of the Constrained Application Protocol (RFC 7252).
void coap_keystore_simple_init(void)
Registers a simple CoAP DTLS keystore with fixed pre-shared key credentials.
void coap_set_keystore(const coap_keystore_t *keystore)
Set the CoAP keystore to use by CoAP.
int coap_secure_server_setup(void)
Setup a DTLS server session.
int coap_sendto(const coap_endpoint_t *ep, const uint8_t *data, uint16_t length)
Send a message to the specified CoAP endpoint.
void coap_endpoint_disconnect(coap_endpoint_t *ep)
Request that any connection to a CoAP endpoint is discontinued.
int coap_endpoint_cmp(const coap_endpoint_t *e1, const coap_endpoint_t *e2)
Compare two CoAP endpoints.
int coap_endpoint_parse(const char *text, size_t size, coap_endpoint_t *ep)
Parse a CoAP endpoint.
int coap_endpoint_connect(coap_endpoint_t *ep)
Request a connection to a CoAP endpoint.
uint8_t * coap_databuf(void)
Returns a common data buffer that can be used when generating CoAP messages for transmission.
void coap_endpoint_log(const coap_endpoint_t *ep)
Print a CoAP endpoint via the logging module.
void coap_endpoint_print(const coap_endpoint_t *ep)
Print a CoAP endpoint.
int coap_endpoint_snprint(char *buf, size_t size, const coap_endpoint_t *ep)
Print a CoAP endpoint to a string.
int coap_endpoint_is_secure(const coap_endpoint_t *ep)
Check if a CoAP endpoint is secure (encrypted).
void coap_transport_init(void)
Initialize the CoAP transport.
void coap_endpoint_copy(coap_endpoint_t *destination, const coap_endpoint_t *from)
Copy a CoAP endpoint from one memory area to another.
int coap_endpoint_is_connected(const coap_endpoint_t *ep)
Check if a CoAP endpoint is connected.
void log_6addr(const uip_ipaddr_t *ipaddr)
Logs an IPv6 address.
#define PROCESS(name, strname)
Declare a process.
#define PROCESS_EXIT()
Exit the currently running process.
#define PROCESS_CURRENT()
Get a pointer to the currently running process.
#define PROCESS_BEGIN()
Define the beginning of a process.
#define PROCESS_END()
Define the end of a process.
void process_start(struct process *p, process_data_t data)
Start a process.
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
#define PROCESS_YIELD()
Yield the currently running process.
static void start(void)
Start measurement.
process_event_t tcpip_event
The uIP event.
struct uip_udp_conn * udp_new(const uip_ipaddr_t *ripaddr, uint16_t port, void *appstate)
Create a new UDP connection.
#define udp_bind(conn, port)
Bind a UDP connection to a local port.
#define uiplib_ipaddrconv
Convert a textual representation of an IP address to a numerical representation.
void uiplib_ipaddr_print(const uip_ipaddr_t *addr)
Print an IP address using printf().
int uiplib_ipaddr_snprint(char *buf, size_t size, const uip_ipaddr_t *addr)
Write at most size - 1 characters of the IP address to the output string.
void * uip_appdata
Pointer to the application data in the packet buffer.
#define uip_is_addr_linklocal(a)
is addr (a) a link local unicast address, see RFC 4291 i.e.
#define UIP_IP_BUF
Direct access to IPv6 header.
#define uip_datalen()
The length of any incoming data that is currently available (if available) in the uip_appdata buffer.
#define uip_newdata()
Is new incoming data available?
#define UIP_HTONS(n)
Convert 16-bit quantity from host byte order to network byte order.
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
void coap_dtls_conn_init(struct uip_udp_conn *udp_conn, struct process *host_process)
Registers, 1.
int coap_ep_dtls_write(const coap_endpoint_t *ep, const unsigned char *message, int len)
Encrypt app.
bool coap_ep_is_dtls_peer(const coap_endpoint_t *ep)
Check if a CoAP endpoint is a peer in the list of DTLS sessions.
bool coap_ep_is_dtls_connected(const coap_endpoint_t *ep)
Check if a peer has completed the handshake successfully.
void coap_dtls_init(void)
Initializes CoAP-MbedTLS global info.
void coap_ep_dtls_disconnect(const coap_endpoint_t *ep)
Disconnect a peer.
void coap_dtls_event_handler(void)
Handler for timer, and process-poll events.
int coap_ep_dtls_handle_message(const coap_endpoint_t *ep)
Handler for new DTLS messages.
DTLS (Mbed TLS implementation) support for CoAP.
Routing driver header file.
The structure of a CoAP PKI certificate info.
The structure of a CoAP pre-shared key info.
The structure of a CoAP keystore.
int(* node_is_reachable)(void)
Tells whether the node is currently reachable as part of the network.
Representation of a uIP UDP connection.
uint16_t lport
The local port number in network byte order.
Header file for module for sending UDP packets through uIP.
Header file for the IP address manipulation library.