libcoap  4.3.0
coap_session.c
Go to the documentation of this file.
1 /* session.c -- Session management for libcoap
2 *
3 * Copyright (C) 2017 Jean-Claue Michelou <jcm@spinetix.com>
4 *
5  * SPDX-License-Identifier: BSD-2-Clause
6  *
7 * This file is part of the CoAP library libcoap. Please see
8 * README for terms of use.
9 */
10 
11 #include "coap3/coap_internal.h"
12 
13 #ifndef COAP_SESSION_C_
14 #define COAP_SESSION_C_
15 
16 #include <stdio.h>
17 
18 #ifdef COAP_EPOLL_SUPPORT
19 #include <sys/epoll.h>
20 #include <sys/timerfd.h>
21 #endif /* COAP_EPOLL_SUPPORT */
22 #include <errno.h>
23 
24 void
25 coap_session_set_max_retransmit (coap_session_t *session, unsigned int value) {
26  if (value > 0)
27  session->max_retransmit = value;
28  coap_log(LOG_DEBUG, "***%s: session max_retransmit set to %d\n",
29  coap_session_str(session), session->max_retransmit);
30  return;
31 }
32 
33 void
35  if (value.integer_part > 0 && value.fractional_part < 1000)
36  session->ack_timeout = value;
37  coap_log(LOG_DEBUG, "***%s: session ack_timeout set to %d.%03d\n",
38  coap_session_str(session), session->ack_timeout.integer_part,
39  session->ack_timeout.fractional_part);
40  return;
41 }
42 
43 void
45  coap_fixed_point_t value) {
46  if (value.integer_part > 0 && value.fractional_part < 1000)
47  session->ack_random_factor = value;
48  coap_log(LOG_DEBUG, "***%s: session ack_random_factor set to %d.%03d\n",
51  return;
52 }
53 
54 unsigned int
56  return session->max_retransmit;
57 }
58 
61  return session->ack_timeout;
62 }
63 
66  return session->ack_random_factor;
67 }
68 
71  ++session->ref;
72  return session;
73 }
74 
75 void
77  if (session) {
78 #ifndef __COVERITY__
79  assert(session->ref > 0);
80  if (session->ref > 0)
81  --session->ref;
82  if (session->ref == 0 && session->type == COAP_SESSION_TYPE_CLIENT)
83  coap_session_free(session);
84 #else /* __COVERITY__ */
85  /* Coverity scan is fooled by the reference counter leading to
86  * false positives for USE_AFTER_FREE. */
87  --session->ref;
88  __coverity_negative_sink__(session->ref);
89  /* Indicate that resources are released properly. */
90  if (session->ref == 0 && session->type == COAP_SESSION_TYPE_CLIENT) {
91  __coverity_free__(session);
92  }
93 #endif /* __COVERITY__ */
94  }
95 }
96 
97 void
98 coap_session_set_app_data(coap_session_t *session, void *app_data) {
99  assert(session);
100  session->app = app_data;
101 }
102 
103 void *
105  assert(session);
106  return session->app;
107 }
108 
109 static coap_session_t *
111  const coap_addr_hash_t *addr_hash, const coap_address_t *local_addr,
112  const coap_address_t *remote_addr, int ifindex, coap_context_t *context,
113  coap_endpoint_t *endpoint) {
115  if (!session)
116  return NULL;
117  memset(session, 0, sizeof(*session));
118  session->proto = proto;
119  session->type = type;
120  if (addr_hash)
121  memcpy(&session->addr_hash, addr_hash, sizeof(session->addr_hash));
122  else
123  memset(&session->addr_hash, 0, sizeof(session->addr_hash));
124  if (local_addr)
125  coap_address_copy(&session->addr_info.local, local_addr);
126  else
127  coap_address_init(&session->addr_info.local);
128  if (remote_addr)
129  coap_address_copy(&session->addr_info.remote, remote_addr);
130  else
132  session->ifindex = ifindex;
133  session->context = context;
134  session->endpoint = endpoint;
135  session->block_mode = context->block_mode;
136  if (endpoint)
137  session->mtu = endpoint->default_mtu;
138  else
139  session->mtu = COAP_DEFAULT_MTU;
140  if (proto == COAP_PROTO_DTLS) {
141  session->tls_overhead = 29;
142  if (session->tls_overhead >= session->mtu) {
143  session->tls_overhead = session->mtu;
144  coap_log(LOG_ERR, "DTLS overhead exceeds MTU\n");
145  }
146  }
150  session->dtls_event = -1;
151  session->last_ping_mid = COAP_INVALID_MID;
152 
153  /* initialize message id */
154  coap_prng((unsigned char *)&session->tx_mid, sizeof(session->tx_mid));
155 
156  return session;
157 }
158 
160  coap_queue_t *q, *tmp;
161  coap_cache_entry_t *cp, *ctmp;
162  coap_lg_xmit_t *lq, *ltmp;
163  coap_lg_crcv_t *cq, *etmp;
164  coap_lg_srcv_t *sq, *stmp;
165 
166  /* Need to do this before (D)TLS and socket is closed down */
167  LL_FOREACH_SAFE(session->lg_crcv, cq, etmp) {
168  if (cq->observe_set) {
169  /* Need to close down observe */
170  if (coap_cancel_observe(session, cq->app_token, COAP_MESSAGE_NON)) {
171  /* Need to delete node we set up for NON */
172  coap_queue_t *queue = session->context->sendqueue;
173 
174  while (queue) {
175  if (queue->session == session) {
176  coap_delete_node(queue);
177  break;
178  }
179  queue = queue->next;
180  }
181  }
182  }
183  LL_DELETE(session->lg_crcv, cq);
184  coap_block_delete_lg_crcv(session, cq);
185  }
186 
187  if (session->partial_pdu)
188  coap_delete_pdu(session->partial_pdu);
189  if (session->proto == COAP_PROTO_DTLS)
190  coap_dtls_free_session(session);
191 #if !COAP_DISABLE_TCP
192  else if (session->proto == COAP_PROTO_TLS)
193  coap_tls_free_session(session);
194 #endif /* !COAP_DISABLE_TCP */
195  if (session->sock.flags != COAP_SOCKET_EMPTY)
196  coap_socket_close(&session->sock);
197  if (session->psk_identity)
198  coap_free(session->psk_identity);
199  if (session->psk_key)
200  coap_free(session->psk_key);
201  if (session->psk_hint)
202  coap_free(session->psk_hint);
203 
204  HASH_ITER(hh, session->context->cache, cp, ctmp) {
205  /* cp->session is NULL if not session based */
206  if (cp->session == session) {
207  coap_delete_cache_entry(session->context, cp);
208  }
209  }
210  LL_FOREACH_SAFE(session->delayqueue, q, tmp) {
211  if (q->pdu->type==COAP_MESSAGE_CON && session->context && session->context->nack_handler)
213  coap_delete_node(q);
214  }
215  LL_FOREACH_SAFE(session->lg_xmit, lq, ltmp) {
216  LL_DELETE(session->lg_xmit, lq);
217  coap_block_delete_lg_xmit(session, lq);
218  }
219  LL_FOREACH_SAFE(session->lg_srcv, sq, stmp) {
220  LL_DELETE(session->lg_srcv, sq);
221  coap_block_delete_lg_srcv(session, sq);
222  }
223 }
224 
226  if (!session)
227  return;
228  assert(session->ref == 0);
229  if (session->ref)
230  return;
231  coap_session_mfree(session);
232  if (session->endpoint) {
233  if (session->endpoint->sessions)
234  SESSIONS_DELETE(session->endpoint->sessions, session);
235  } else if (session->context) {
236  if (session->context->sessions)
237  SESSIONS_DELETE(session->context->sessions, session);
238  }
239  coap_log(LOG_DEBUG, "***%s: session closed\n", coap_session_str(session));
240 
241  coap_free_type(COAP_SESSION, session);
242 }
243 
245  size_t max_with_header = (size_t)(session->mtu - session->tls_overhead);
246 #if COAP_DISABLE_TCP
247  return max_with_header > 4 ? max_with_header - 4 : 0;
248 #else /* !COAP_DISABLE_TCP */
249  if (COAP_PROTO_NOT_RELIABLE(session->proto))
250  return max_with_header > 4 ? max_with_header - 4 : 0;
251  /* we must assume there is no token to be on the safe side */
252  if (max_with_header <= 2)
253  return 0;
254  else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP0 + 2)
255  return max_with_header - 2;
256  else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP8 + 3)
257  return max_with_header - 3;
258  else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP16 + 4)
259  return max_with_header - 4;
260  else
261  return max_with_header - 6;
262 #endif /* !COAP_DISABLE_TCP */
263 }
264 
265 void coap_session_set_mtu(coap_session_t *session, unsigned mtu) {
266 #if defined(WITH_CONTIKI) || defined(WITH_LWIP)
267  if (mtu > COAP_MAX_MESSAGE_SIZE_TCP16 + 4)
268  mtu = COAP_MAX_MESSAGE_SIZE_TCP16 + 4;
269 #endif
270  session->mtu = mtu;
271  if (session->tls_overhead >= session->mtu) {
272  session->tls_overhead = session->mtu;
273  coap_log(LOG_ERR, "DTLS overhead exceeds MTU\n");
274  }
275 }
276 
277 ssize_t coap_session_send(coap_session_t *session, const uint8_t *data, size_t datalen) {
278  ssize_t bytes_written;
279 
280  coap_socket_t *sock = &session->sock;
281  if (sock->flags == COAP_SOCKET_EMPTY) {
282  assert(session->endpoint != NULL);
283  sock = &session->endpoint->sock;
284  }
285 
286  bytes_written = coap_socket_send(sock, session, data, datalen);
287  if (bytes_written == (ssize_t)datalen) {
288  coap_ticks(&session->last_rx_tx);
289  coap_log(LOG_DEBUG, "* %s: sent %zd bytes\n",
290  coap_session_str(session), datalen);
291  } else {
292  coap_log(LOG_DEBUG, "* %s: failed to send %zd bytes\n",
293  coap_session_str(session), datalen);
294  }
295  return bytes_written;
296 }
297 
298 ssize_t coap_session_write(coap_session_t *session, const uint8_t *data, size_t datalen) {
299  ssize_t bytes_written = coap_socket_write(&session->sock, data, datalen);
300  if (bytes_written > 0) {
301  coap_ticks(&session->last_rx_tx);
302  coap_log(LOG_DEBUG, "* %s: sent %zd bytes\n",
303  coap_session_str(session), datalen);
304  } else if (bytes_written < 0) {
305  coap_log(LOG_DEBUG, "* %s: failed to send %zd bytes\n",
306  coap_session_str(session), datalen );
307  }
308  return bytes_written;
309 }
310 
311 ssize_t
313  coap_queue_t *node)
314 {
315  if ( node ) {
316  coap_queue_t *removed = NULL;
317  coap_remove_from_queue(&session->context->sendqueue, session, node->id, &removed);
318  assert(removed == node);
320  node->session = NULL;
321  node->t = 0;
322  } else {
323  coap_queue_t *q = NULL;
324  /* Check that the same mid is not getting re-used in violation of RFC7252 */
325  LL_FOREACH(session->delayqueue, q) {
326  if (q->id == pdu->mid) {
327  coap_log(LOG_ERR, "** %s: mid=0x%x: already in-use - dropped\n",
328  coap_session_str(session), pdu->mid);
329  return COAP_INVALID_MID;
330  }
331  }
332  node = coap_new_node();
333  if (node == NULL)
334  return COAP_INVALID_MID;
335  node->id = pdu->mid;
336  node->pdu = pdu;
337  if (pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
338  uint8_t r;
339  coap_prng(&r, sizeof(r));
340  /* add timeout in range [ACK_TIMEOUT...ACK_TIMEOUT * ACK_RANDOM_FACTOR] */
341  node->timeout = coap_calc_timeout(session, r);
342  }
343  }
344  LL_APPEND(session->delayqueue, node);
345  coap_log(LOG_DEBUG, "** %s: mid=0x%x: delayed\n",
346  coap_session_str(session), node->id);
347  return COAP_PDU_DELAYED;
348 }
349 
350 #if !COAP_DISABLE_TCP
352  coap_pdu_t *pdu;
353  uint8_t buf[4];
354  assert(COAP_PROTO_RELIABLE(session->proto));
355  coap_log(LOG_DEBUG, "***%s: sending CSM\n", coap_session_str(session));
356  session->state = COAP_SESSION_STATE_CSM;
357  session->partial_write = 0;
358  if (session->mtu == 0)
359  session->mtu = COAP_DEFAULT_MTU; /* base value */
361  if ( pdu == NULL
363  coap_encode_var_safe(buf, sizeof(buf),
364  COAP_DEFAULT_MAX_PDU_RX_SIZE), buf) == 0
366  coap_encode_var_safe(buf, sizeof(buf),
367  0), buf) == 0
368  || coap_pdu_encode_header(pdu, session->proto) == 0
369  ) {
371  } else {
372  ssize_t bytes_written = coap_session_send_pdu(session, pdu);
373  if (bytes_written != (ssize_t)pdu->used_size + pdu->hdr_size)
375  }
376  if (pdu)
377  coap_delete_pdu(pdu);
378 }
379 #endif /* !COAP_DISABLE_TCP */
380 
382  coap_pdu_t *ping = NULL;
383  if (session->state != COAP_SESSION_STATE_ESTABLISHED)
384  return COAP_INVALID_MID;
385  if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
386  uint16_t mid = coap_new_message_id (session);
387  ping = coap_pdu_init(COAP_MESSAGE_CON, 0, mid, 0);
388  }
389 #if !COAP_DISABLE_TCP
390  else {
392  }
393 #endif /* !COAP_DISABLE_TCP */
394  if (!ping)
395  return COAP_INVALID_MID;
396  return coap_send_internal(session, ping);
397 }
398 
400  if (session->state != COAP_SESSION_STATE_ESTABLISHED) {
401  coap_log(LOG_DEBUG, "***%s: session connected\n",
402  coap_session_str(session));
403  if (session->state == COAP_SESSION_STATE_CSM)
405  }
406 
408  session->partial_write = 0;
409 
410  if ( session->proto==COAP_PROTO_DTLS) {
411  session->tls_overhead = coap_dtls_get_overhead(session);
412  if (session->tls_overhead >= session->mtu) {
413  session->tls_overhead = session->mtu;
414  coap_log(LOG_ERR, "DTLS overhead exceeds MTU\n");
415  }
416  }
417 
418  while (session->delayqueue && session->state == COAP_SESSION_STATE_ESTABLISHED) {
419  ssize_t bytes_written;
420  coap_queue_t *q = session->delayqueue;
421  if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
422  if (session->con_active >= COAP_DEFAULT_NSTART)
423  break;
424  session->con_active++;
425  }
426  /* Take entry off the queue */
427  session->delayqueue = q->next;
428  q->next = NULL;
429 
430  coap_log(LOG_DEBUG, "** %s: mid=0x%x: transmitted after delay\n",
431  coap_session_str(session), (int)q->pdu->mid);
432  bytes_written = coap_session_send_pdu(session, q->pdu);
433  if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
434  if (coap_wait_ack(session->context, session, q) >= 0)
435  q = NULL;
436  }
437  if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
438  if (q)
439  coap_delete_node(q);
440  if (bytes_written < 0)
441  break;
442  } else {
443  if (bytes_written <= 0 || (size_t)bytes_written < q->pdu->used_size + q->pdu->hdr_size) {
444  q->next = session->delayqueue;
445  session->delayqueue = q;
446  if (bytes_written > 0)
447  session->partial_write = (size_t)bytes_written;
448  break;
449  } else {
450  coap_delete_node(q);
451  }
452  }
453  }
454 }
455 
457 #if !COAP_DISABLE_TCP
458  coap_session_state_t state = session->state;
459 #endif /* !COAP_DISABLE_TCP */
460 
461  coap_log(LOG_DEBUG, "***%s: session disconnected (reason %d)\n",
462  coap_session_str(session), reason);
463  coap_delete_observers( session->context, session );
464 
465  if ( session->tls) {
466  if (session->proto == COAP_PROTO_DTLS)
467  coap_dtls_free_session(session);
468 #if !COAP_DISABLE_TCP
469  else if (session->proto == COAP_PROTO_TLS)
470  coap_tls_free_session(session);
471 #endif /* !COAP_DISABLE_TCP */
472  session->tls = NULL;
473  }
474 
475  if (session->proto == COAP_PROTO_UDP)
477  else
478  session->state = COAP_SESSION_STATE_NONE;
479 
480  session->con_active = 0;
481 
482  if (session->partial_pdu) {
483  coap_delete_pdu(session->partial_pdu);
484  session->partial_pdu = NULL;
485  }
486  session->partial_read = 0;
487 
488  while (session->delayqueue) {
489  coap_queue_t *q = session->delayqueue;
490  session->delayqueue = q->next;
491  q->next = NULL;
492  coap_log(LOG_DEBUG, "** %s: mid=0x%x: not transmitted after disconnect\n",
493  coap_session_str(session), q->id);
494  if (q->pdu->type==COAP_MESSAGE_CON
495  && COAP_PROTO_NOT_RELIABLE(session->proto)
496  && reason == COAP_NACK_ICMP_ISSUE)
497  {
498  /* Make sure that we try a re-transmit later on ICMP error */
499  if (coap_wait_ack(session->context, session, q) >= 0) {
500  if (session->context->nack_handler) {
501  session->context->nack_handler(session, q->pdu, reason, q->id);
502  }
503  q = NULL;
504  }
505  }
506  if (q && q->pdu->type == COAP_MESSAGE_CON
507  && session->context->nack_handler)
508  {
509  session->context->nack_handler(session, q->pdu, reason, q->id);
510  }
511  if (q)
512  coap_delete_node(q);
513  }
514  if (reason != COAP_NACK_ICMP_ISSUE) {
515  coap_cancel_session_messages(session->context, session, reason);
516  }
517  else if (session->context->nack_handler) {
518  coap_queue_t *q = session->context->sendqueue;
519  while (q) {
520  if (q->session == session) {
521  session->context->nack_handler(session, q->pdu, reason, q->id);
522  }
523  q = q->next;
524  }
525  }
526 
527 #if !COAP_DISABLE_TCP
528  if (COAP_PROTO_RELIABLE(session->proto)) {
529  if (session->sock.flags != COAP_SOCKET_EMPTY) {
530  coap_socket_close(&session->sock);
531  coap_handle_event(session->context,
534  }
535  if (state != COAP_SESSION_STATE_NONE) {
536  coap_handle_event(session->context,
539  }
540  }
541 #endif /* !COAP_DISABLE_TCP */
542 }
543 
544 static void
546  const coap_addr_tuple_t *addr_info) {
547  memset(addr_hash, 0, sizeof(coap_addr_hash_t));
548  coap_address_copy(&addr_hash->remote, &addr_info->remote);
549  addr_hash->lport = coap_address_get_port(&addr_info->local);
550  addr_hash->proto = proto;
551 }
552 
555  const coap_packet_t *packet, coap_tick_t now) {
556  coap_session_t *session;
557  coap_session_t *rtmp;
558  unsigned int num_idle = 0;
559  unsigned int num_hs = 0;
560  coap_session_t *oldest = NULL;
561  coap_session_t *oldest_hs = NULL;
562  coap_addr_hash_t addr_hash;
563 
564  coap_make_addr_hash(&addr_hash, endpoint->proto, &packet->addr_info);
565  SESSIONS_FIND(endpoint->sessions, addr_hash, session);
566  if (session) {
567  /* Maybe mcast or unicast IP address which is not in the hash */
568  coap_address_copy(&session->addr_info.local, &packet->addr_info.local);
569  session->ifindex = packet->ifindex;
570  session->last_rx_tx = now;
571  return session;
572  }
573 
574  SESSIONS_ITER(endpoint->sessions, session, rtmp) {
575  if (session->ref == 0 && session->delayqueue == NULL) {
576  if (session->type == COAP_SESSION_TYPE_SERVER) {
577  ++num_idle;
578  if (oldest==NULL || session->last_rx_tx < oldest->last_rx_tx)
579  oldest = session;
580 
581  if (session->state == COAP_SESSION_STATE_HANDSHAKE) {
582  ++num_hs;
583  /* See if this is a partial (D)TLS session set up
584  which needs to be cleared down to prevent DOS */
585  if ((session->last_rx_tx + COAP_PARTIAL_SESSION_TIMEOUT_TICKS) < now) {
586  if (oldest_hs == NULL ||
587  session->last_rx_tx < oldest_hs->last_rx_tx)
588  oldest_hs = session;
589  }
590  }
591  }
592  else if (session->type == COAP_SESSION_TYPE_HELLO) {
593  ++num_hs;
594  /* See if this is a partial (D)TLS session set up for Client Hello
595  which needs to be cleared down to prevent DOS */
596  if ((session->last_rx_tx + COAP_PARTIAL_SESSION_TIMEOUT_TICKS) < now) {
597  if (oldest_hs == NULL ||
598  session->last_rx_tx < oldest_hs->last_rx_tx)
599  oldest_hs = session;
600  }
601  }
602  }
603  }
604 
605  if (endpoint->context->max_idle_sessions > 0 &&
606  num_idle >= endpoint->context->max_idle_sessions) {
607  coap_session_free(oldest);
608  }
609  else if (oldest_hs) {
610  coap_log(LOG_WARNING, "***%s: Incomplete session timed out\n",
611  coap_session_str(oldest_hs));
612  coap_session_free(oldest_hs);
613  }
614 
615  if (num_hs > (endpoint->context->max_handshake_sessions ?
616  endpoint->context->max_handshake_sessions :
618  /* Maxed out on number of sessions in (D)TLS negotiation state */
620  "Oustanding sessions in COAP_SESSION_STATE_HANDSHAKE too "
621  "large. New request ignored\n");
622  return NULL;
623  }
624 
625  if (endpoint->proto == COAP_PROTO_DTLS) {
626  /*
627  * Need to check that this actually is a Client Hello before wasting
628  * time allocating and then freeing off session.
629  */
630 
631  /*
632  * Generic header structure of the DTLS record layer.
633  * typedef struct __attribute__((__packed__)) {
634  * uint8_t content_type; content type of the included message
635  * uint16_t version; Protocol version
636  * uint16_t epoch; counter for cipher state changes
637  * uint8_t sequence_number[6]; sequence number
638  * uint16_t length; length of the following fragment
639  * uint8_t handshake; If content_type == DTLS_CT_HANDSHAKE
640  * } dtls_record_handshake_t;
641  */
642 #define OFF_CONTENT_TYPE 0 /* offset of content_type in dtls_record_handshake_t */
643 #define DTLS_CT_ALERT 21 /* Content Type Alert */
644 #define DTLS_CT_HANDSHAKE 22 /* Content Type Handshake */
645 #define OFF_HANDSHAKE_TYPE 13 /* offset of handshake in dtls_record_handshake_t */
646 #define DTLS_HT_CLIENT_HELLO 1 /* Client Hello handshake type */
647 
648 #ifdef WITH_LWIP
649  const uint8_t *payload = (const uint8_t*)packet->pbuf->payload;
650  size_t length = packet->pbuf->len;
651 #else /* ! WITH_LWIP */
652  const uint8_t *payload = (const uint8_t*)packet->payload;
653  size_t length = packet->length;
654 #endif /* ! WITH_LWIP */
655  if (length < (OFF_HANDSHAKE_TYPE + 1)) {
657  "coap_dtls_hello: ContentType %d Short Packet (%zu < %d) dropped\n",
658  payload[OFF_CONTENT_TYPE], length,
659  OFF_HANDSHAKE_TYPE + 1);
660  return NULL;
661  }
662  if (payload[OFF_CONTENT_TYPE] != DTLS_CT_HANDSHAKE ||
664  /* only log if not a late alert */
665  if (payload[OFF_CONTENT_TYPE] != DTLS_CT_ALERT)
667  "coap_dtls_hello: ContentType %d Handshake %d dropped\n",
668  payload[OFF_CONTENT_TYPE], payload[OFF_HANDSHAKE_TYPE]);
669  return NULL;
670  }
671  }
672  session = coap_make_session(endpoint->proto, COAP_SESSION_TYPE_SERVER,
673  &addr_hash, &packet->addr_info.local,
674  &packet->addr_info.remote,
675  packet->ifindex, endpoint->context, endpoint);
676  if (session) {
677  session->last_rx_tx = now;
678  if (endpoint->proto == COAP_PROTO_UDP)
680  else if (endpoint->proto == COAP_PROTO_DTLS) {
681  session->type = COAP_SESSION_TYPE_HELLO;
682  }
683  SESSIONS_ADD(endpoint->sessions, session);
684  coap_log(LOG_DEBUG, "***%s: new incoming session\n",
685  coap_session_str(session));
686  }
687  return session;
688 }
689 
692  coap_tick_t now) {
693  if (session) {
694  session->last_rx_tx = now;
695  session->type = COAP_SESSION_TYPE_SERVER;
696  session->tls = coap_dtls_new_server_session(session);
697  if (session->tls) {
699  } else {
700  coap_session_free(session);
701  session = NULL;
702  }
703  }
704  return session;
705 }
706 
707 #ifdef COAP_EPOLL_SUPPORT
708 static void
709 coap_epoll_ctl_add(coap_socket_t *sock,
710  uint32_t events,
711  const char *func
712 ) {
713  int ret;
714  struct epoll_event event;
715  coap_context_t *context;
716 
717  if (sock == NULL)
718  return;
719 
720  context = sock->session ? sock->session->context :
721  sock->endpoint ? sock->endpoint->context : NULL;
722  if (context == NULL)
723  return;
724 
725  /* Needed if running 32bit as ptr is only 32bit */
726  memset(&event, 0, sizeof(event));
727  event.events = events;
728  event.data.ptr = sock;
729 
730  ret = epoll_ctl(context->epfd, EPOLL_CTL_ADD, sock->fd, &event);
731  if (ret == -1) {
733  "%s: epoll_ctl ADD failed: %s (%d)\n",
734  func,
735  coap_socket_strerror(), errno);
736  }
737 }
738 #endif /* COAP_EPOLL_SUPPORT */
739 
740 static coap_session_t *
742  coap_context_t *ctx,
743  const coap_address_t *local_if,
744  const coap_address_t *server,
745  coap_proto_t proto
746 ) {
747  coap_session_t *session = NULL;
748 
749  assert(server);
750 
751  switch(proto) {
752  case COAP_PROTO_UDP:
753  break;
754  case COAP_PROTO_DTLS:
755  if (!coap_dtls_is_supported()) {
756  coap_log(LOG_CRIT, "coap_new_client_session*: DTLS not supported\n");
757  return NULL;
758  }
759  break;
760  case COAP_PROTO_TCP:
761  if (!coap_tcp_is_supported()) {
762  coap_log(LOG_CRIT, "coap_new_client_session*: TCP not supported\n");
763  return NULL;
764  }
765  break;
766  case COAP_PROTO_TLS:
767  if (!coap_tls_is_supported()) {
768  coap_log(LOG_CRIT, "coap_new_client_session*: TLS not supported\n");
769  return NULL;
770  }
771  break;
772  case COAP_PROTO_NONE:
773  default:
774  assert(0);
775  break;
776  }
777  session = coap_make_session(proto, COAP_SESSION_TYPE_CLIENT, NULL,
778  local_if, server, 0, ctx, NULL);
779  if (!session)
780  goto error;
781 
782  coap_session_reference(session);
783 
784  if (proto == COAP_PROTO_UDP || proto == COAP_PROTO_DTLS) {
785  if (!coap_socket_connect_udp(&session->sock, local_if, server,
787  &session->addr_info.local, &session->addr_info.remote)) {
788  goto error;
789  }
790 #if !COAP_DISABLE_TCP
791  } else if (proto == COAP_PROTO_TCP || proto == COAP_PROTO_TLS) {
792  if (!coap_socket_connect_tcp1(&session->sock, local_if, server,
794  &session->addr_info.local, &session->addr_info.remote)) {
795  goto error;
796  }
797 #endif /* !COAP_DISABLE_TCP */
798  }
799 
800 #ifdef COAP_EPOLL_SUPPORT
801  session->sock.session = session;
802  coap_epoll_ctl_add(&session->sock,
803  EPOLLIN |
804  ((session->sock.flags & COAP_SOCKET_WANT_CONNECT) ?
805  EPOLLOUT : 0),
806  __func__);
807 #endif /* COAP_EPOLL_SUPPORT */
808 
810  if (local_if)
811  session->sock.flags |= COAP_SOCKET_BOUND;
812  SESSIONS_ADD(ctx->sessions, session);
813  return session;
814 
815 error:
816  coap_session_release(session);
817  return NULL;
818 }
819 
820 static coap_session_t *
822  if (session->proto == COAP_PROTO_UDP) {
824  } else if (session->proto == COAP_PROTO_DTLS) {
825  session->tls = coap_dtls_new_client_session(session);
826  if (session->tls) {
828  } else {
829  /* Need to free session object. As a new session may not yet
830  * have been referenced, we call coap_session_reference() first
831  * before trying to release the object.
832  */
833  coap_session_reference(session);
834  coap_session_release(session);
835  return NULL;
836  }
837 #if !COAP_DISABLE_TCP
838  } else {
839  if (session->proto == COAP_PROTO_TCP || session->proto == COAP_PROTO_TLS) {
840  if (session->sock.flags & COAP_SOCKET_WANT_CONNECT) {
842  } else if (session->proto == COAP_PROTO_TLS) {
843  int connected = 0;
844  session->tls = coap_tls_new_client_session(session, &connected);
845  if (session->tls) {
847  if (connected)
848  coap_session_send_csm(session);
849  } else {
850  /* Need to free session object. As a new session may not yet
851  * have been referenced, we call coap_session_reference()
852  * first before trying to release the object.
853  */
854  coap_session_reference(session);
855  coap_session_release(session);
856  return NULL;
857  }
858  } else {
859  coap_session_send_csm(session);
860  }
861  }
862 #endif /* !COAP_DISABLE_TCP */
863  }
864  coap_ticks(&session->last_rx_tx);
865  return session;
866 }
867 
868 static coap_session_t *
870 #if !COAP_DISABLE_TCP
871  if (session->proto == COAP_PROTO_TCP || session->proto == COAP_PROTO_TLS)
873  if (session->proto == COAP_PROTO_TCP) {
874  coap_session_send_csm(session);
875  } else if (session->proto == COAP_PROTO_TLS) {
876  int connected = 0;
877  session->tls = coap_tls_new_server_session(session, &connected);
878  if (session->tls) {
880  if (connected) {
882  coap_session_send_csm(session);
883  }
884  } else {
885  /* Need to free session object. As a new session may not yet
886  * have been referenced, we call coap_session_reference() first
887  * before trying to release the object.
888  */
889  coap_session_reference(session);
890  coap_session_release(session);
891  session = NULL;
892  }
893  }
894 #endif /* COAP_DISABLE_TCP */
895  return session;
896 }
897 
899  coap_context_t *ctx,
900  const coap_address_t *local_if,
901  const coap_address_t *server,
902  coap_proto_t proto
903 ) {
904  coap_session_t *session = coap_session_create_client(ctx, local_if, server, proto);
905  if (session) {
906  coap_log(LOG_DEBUG, "***%s: new outgoing session\n",
907  coap_session_str(session));
908  session = coap_session_connect(session);
909  }
910  return session;
911 }
912 
914  coap_context_t *ctx,
915  const coap_address_t *local_if,
916  const coap_address_t *server,
917  coap_proto_t proto,
918  const char *identity,
919  const uint8_t *key,
920  unsigned key_len
921 ) {
922  coap_dtls_cpsk_t setup_data;
923 
924  memset (&setup_data, 0, sizeof(setup_data));
925 
926  if (identity) {
927  setup_data.psk_info.identity.s = (const uint8_t *)identity;
928  setup_data.psk_info.identity.length = strlen(identity);
929  }
930 
931  if (key && key_len > 0) {
932  setup_data.psk_info.key.s = key;
933  setup_data.psk_info.key.length = key_len;
934  }
935 
936  return coap_new_client_session_psk2(ctx, local_if, server,
937  proto, &setup_data);
938 }
939 
941  coap_context_t *ctx,
942  const coap_address_t *local_if,
943  const coap_address_t *server,
944  coap_proto_t proto,
945  coap_dtls_cpsk_t *setup_data
946 ) {
947  coap_session_t *session = coap_session_create_client(ctx, local_if,
948  server, proto);
949 
950  if (!session)
951  return NULL;
952 
953  session->cpsk_setup_data = *setup_data;
954  if (setup_data->psk_info.identity.s) {
955  session->psk_identity =
957  setup_data->psk_info.identity.length);
958  if (!session->psk_identity) {
959  coap_log(LOG_WARNING, "Cannot store session Identity (PSK)\n");
960  coap_session_release(session);
961  return NULL;
962  }
963  }
964  else if (coap_dtls_is_supported()) {
965  coap_log(LOG_WARNING, "Identity (PSK) not defined\n");
966  coap_session_release(session);
967  return NULL;
968  }
969 
970  if (setup_data->psk_info.key.s && setup_data->psk_info.key.length > 0) {
971  session->psk_key = coap_new_bin_const(setup_data->psk_info.key.s,
972  setup_data->psk_info.key.length);
973  if (!session->psk_key) {
974  coap_log(LOG_WARNING, "Cannot store session pre-shared key (PSK)\n");
975  coap_session_release(session);
976  return NULL;
977  }
978  }
979  else if (coap_dtls_is_supported()) {
980  coap_log(LOG_WARNING, "Pre-shared key (PSK) not defined\n");
981  coap_session_release(session);
982  return NULL;
983  }
984 
985  if (coap_dtls_is_supported()) {
986  if (!coap_dtls_context_set_cpsk(ctx, setup_data)) {
987  coap_session_release(session);
988  return NULL;
989  }
990  }
991  coap_log(LOG_DEBUG, "***%s: new outgoing session\n",
992  coap_session_str(session));
993  return coap_session_connect(session);
994 }
995 
997  const coap_bin_const_t *psk_hint
998 ) {
999  /* We may be refreshing the hint with the same hint */
1000  coap_bin_const_t *old_psk_hint = session->psk_hint;
1001 
1002  if (psk_hint && psk_hint->s) {
1003  if (session->psk_hint) {
1004  if (coap_binary_equal(session->psk_hint, psk_hint))
1005  return 1;
1006  }
1007  session->psk_hint = coap_new_bin_const(psk_hint->s,
1008  psk_hint->length);
1009  if (!session->psk_hint) {
1010  coap_log(LOG_ERR, "No memory to store identity hint (PSK)\n");
1011  if (old_psk_hint)
1012  coap_delete_bin_const(old_psk_hint);
1013  return 0;
1014  }
1015  }
1016  else {
1017  session->psk_hint = NULL;
1018  }
1019  if (old_psk_hint)
1020  coap_delete_bin_const(old_psk_hint);
1021 
1022  return 1;
1023 }
1024 
1026  const coap_bin_const_t *psk_key
1027 ) {
1028  /* We may be refreshing the key with the same key */
1029  coap_bin_const_t *old_psk_key = session->psk_key;
1030 
1031  if (psk_key && psk_key->s) {
1032  if (session->psk_key) {
1033  if (coap_binary_equal(session->psk_key, psk_key))
1034  return 1;
1035  }
1036  session->psk_key = coap_new_bin_const(psk_key->s, psk_key->length);
1037  if (!session->psk_key) {
1038  coap_log(LOG_ERR, "No memory to store pre-shared key (PSK)\n");
1039  if (old_psk_key)
1040  coap_delete_bin_const(old_psk_key);
1041  return 0;
1042  }
1043  }
1044  else {
1045  session->psk_key = NULL;
1046  }
1047  if (old_psk_key)
1048  coap_delete_bin_const(old_psk_key);
1049 
1050  return 1;
1051 }
1052 
1053 const coap_bin_const_t *
1055  if (session)
1056  return session->psk_hint;
1057  return NULL;
1058 }
1059 
1060 const coap_bin_const_t *
1062  if (session)
1063  return session->psk_key;
1064  return NULL;
1065 }
1066 
1068  coap_context_t *ctx,
1069  const coap_address_t *local_if,
1070  const coap_address_t *server,
1071  coap_proto_t proto,
1072  coap_dtls_pki_t* setup_data
1073 ) {
1074  coap_session_t *session;
1075 
1076  if (coap_dtls_is_supported()) {
1077  if (!setup_data) {
1078  return NULL;
1079  } else {
1080  if (setup_data->version != COAP_DTLS_PKI_SETUP_VERSION) {
1081  coap_log(LOG_ERR,
1082  "coap_new_client_session_pki: Wrong version of setup_data\n");
1083  return NULL;
1084  }
1085  }
1086 
1087  }
1088  session = coap_session_create_client(ctx, local_if, server, proto);
1089 
1090  if (!session) {
1091  return NULL;
1092  }
1093 
1094  if (coap_dtls_is_supported()) {
1095  /* we know that setup_data is not NULL */
1096  if (!coap_dtls_context_set_pki(ctx, setup_data, COAP_DTLS_ROLE_CLIENT)) {
1097  coap_session_release(session);
1098  return NULL;
1099  }
1100  }
1101  coap_log(LOG_DEBUG, "***%s: new outgoing session\n",
1102  coap_session_str(session));
1103  return coap_session_connect(session);
1104 }
1105 
1107  coap_context_t *ctx,
1108  coap_endpoint_t *ep
1109 ) {
1110  coap_session_t *session;
1112  NULL, NULL, NULL, 0, ctx, ep );
1113  if (!session)
1114  goto error;
1115 
1116 #if !COAP_DISABLE_TCP
1117  if (!coap_socket_accept_tcp(&ep->sock, &session->sock,
1118  &session->addr_info.local,
1119  &session->addr_info.remote))
1120  goto error;
1121  coap_make_addr_hash(&session->addr_hash, session->proto, &session->addr_info);
1122 
1123 #endif /* !COAP_DISABLE_TCP */
1126 #ifdef COAP_EPOLL_SUPPORT
1127  session->sock.session = session;
1128  coap_epoll_ctl_add(&session->sock,
1129  EPOLLIN,
1130  __func__);
1131 #endif /* COAP_EPOLL_SUPPORT */
1132  SESSIONS_ADD(ep->sessions, session);
1133  if (session) {
1134  coap_log(LOG_DEBUG, "***%s: new incoming session\n",
1135  coap_session_str(session));
1136  session = coap_session_accept(session);
1137  }
1138  return session;
1139 
1140 error:
1141  coap_session_free(session);
1142  return NULL;
1143 }
1144 
1145 void
1147  const uint8_t *data) {
1148  session->tx_token = coap_decode_var_bytes8(data, len);
1149 }
1150 
1151 void coap_session_new_token(coap_session_t *session, size_t *len,
1152  uint8_t *data) {
1153  *len = coap_encode_var_safe8(data,
1154  sizeof(session->tx_token), ++session->tx_token);
1155 }
1156 
1157 uint16_t
1159  return ++session->tx_mid;
1160 }
1161 
1162 const coap_address_t *
1164  if (session)
1165  return &session->addr_info.remote;
1166  return NULL;
1167 }
1168 
1169 const coap_address_t *
1171  if (session)
1172  return &session->addr_info.local;
1173  return NULL;
1174 }
1175 
1178  if (session)
1179  return session->context;
1180  return NULL;
1181 }
1182 
1185  if (session)
1186  return session->proto;
1187  return 0;
1188 }
1189 
1192  if (session)
1193  return session->type;
1194  return 0;
1195 }
1196 
1197 int
1199  if (session && session->type == COAP_SESSION_TYPE_SERVER) {
1200  coap_session_reference(session);
1201  session->type = COAP_SESSION_TYPE_CLIENT;
1202  return 1;
1203  }
1204  return 0;
1205 }
1206 
1209  if (session)
1210  return session->state;
1211  return 0;
1212 }
1213 
1215  if (session)
1216  return session->ifindex;
1217  return -1;
1218 }
1219 
1221  coap_tls_library_t *tls_lib) {
1222  if (session)
1223  return coap_dtls_get_tls(session, tls_lib);
1224  return NULL;
1225 }
1226 
1227 #ifndef WITH_LWIP
1229 coap_new_endpoint(coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto) {
1230  coap_endpoint_t *ep = NULL;
1231 
1232  assert(context);
1233  assert(listen_addr);
1234  assert(proto != COAP_PROTO_NONE);
1235 
1236  if (proto == COAP_PROTO_DTLS && !coap_dtls_is_supported()) {
1237  coap_log(LOG_CRIT, "coap_new_endpoint: DTLS not supported\n");
1238  goto error;
1239  }
1240 
1241  if (proto == COAP_PROTO_TLS && !coap_tls_is_supported()) {
1242  coap_log(LOG_CRIT, "coap_new_endpoint: TLS not supported\n");
1243  goto error;
1244  }
1245 
1246  if (proto == COAP_PROTO_TCP && !coap_tcp_is_supported()) {
1247  coap_log(LOG_CRIT, "coap_new_endpoint: TCP not supported\n");
1248  goto error;
1249  }
1250 
1251  if (proto == COAP_PROTO_DTLS || proto == COAP_PROTO_TLS) {
1252  if (!coap_dtls_context_check_keys_enabled(context)) {
1254  "coap_new_endpoint: one of coap_context_set_psk() or "
1255  "coap_context_set_pki() not called\n");
1256  goto error;
1257  }
1258  }
1259 
1260  ep = coap_malloc_endpoint();
1261  if (!ep) {
1262  coap_log(LOG_WARNING, "coap_new_endpoint: malloc");
1263  goto error;
1264  }
1265 
1266  memset(ep, 0, sizeof(coap_endpoint_t));
1267  ep->context = context;
1268  ep->proto = proto;
1269 
1270  if (proto==COAP_PROTO_UDP || proto==COAP_PROTO_DTLS) {
1271  if (!coap_socket_bind_udp(&ep->sock, listen_addr, &ep->bind_addr))
1272  goto error;
1274 #if !COAP_DISABLE_TCP
1275  } else if (proto==COAP_PROTO_TCP || proto==COAP_PROTO_TLS) {
1276  if (!coap_socket_bind_tcp(&ep->sock, listen_addr, &ep->bind_addr))
1277  goto error;
1279 #endif /* !COAP_DISABLE_TCP */
1280  } else {
1281  coap_log(LOG_CRIT, "coap_new_endpoint: protocol not supported\n");
1282  goto error;
1283  }
1284 
1285  if (LOG_DEBUG <= coap_get_log_level()) {
1286 #ifndef INET6_ADDRSTRLEN
1287 #define INET6_ADDRSTRLEN 40
1288 #endif
1289  unsigned char addr_str[INET6_ADDRSTRLEN + 8];
1290 
1291  if (coap_print_addr(&ep->bind_addr, addr_str, INET6_ADDRSTRLEN + 8)) {
1292  coap_log(LOG_DEBUG, "created %s endpoint %s\n",
1293  ep->proto == COAP_PROTO_TLS ? "TLS "
1294  : ep->proto == COAP_PROTO_TCP ? "TCP "
1295  : ep->proto == COAP_PROTO_DTLS ? "DTLS" : "UDP ",
1296  addr_str);
1297  }
1298  }
1299 
1301 
1303 
1304 #ifdef COAP_EPOLL_SUPPORT
1305  ep->sock.endpoint = ep;
1306  coap_epoll_ctl_add(&ep->sock,
1307  EPOLLIN,
1308  __func__);
1309 #endif /* COAP_EPOLL_SUPPORT */
1310 
1311  LL_PREPEND(context->endpoint, ep);
1312  return ep;
1313 
1314 error:
1315  coap_free_endpoint(ep);
1316  return NULL;
1317 }
1318 
1320  ep->default_mtu = (uint16_t)mtu;
1321 }
1322 
1323 void
1325  if (ep) {
1326  coap_session_t *session, *rtmp;
1327 
1328  SESSIONS_ITER_SAFE(ep->sessions, session, rtmp) {
1329  assert(session->ref == 0);
1330  if (session->ref == 0) {
1331  coap_session_free(session);
1332  }
1333  }
1334  if (ep->sock.flags != COAP_SOCKET_EMPTY) {
1335  /*
1336  * ep->sock.endpoint is set in coap_new_endpoint().
1337  * ep->sock.session is never set.
1338  *
1339  * session->sock.session is set for both clients and servers (when a
1340  * new session is accepted), but does not affect the endpoint.
1341  *
1342  * So, it is safe to call coap_socket_close() after all the sessions
1343  * have been freed above as we are only working with the endpoint sock.
1344  */
1345 #ifdef COAP_EPOLL_SUPPORT
1346  assert(ep->sock.session == NULL);
1347 #endif /* COAP_EPOLL_SUPPORT */
1348  coap_socket_close(&ep->sock);
1349  }
1350 
1351  if (ep->context && ep->context->endpoint) {
1352  LL_DELETE(ep->context->endpoint, ep);
1353  }
1354  coap_mfree_endpoint(ep);
1355  }
1356 }
1357 #endif /* WITH_LWIP */
1358 
1361  const coap_address_t *remote_addr,
1362  int ifindex) {
1363  coap_session_t *s, *rtmp;
1364  coap_endpoint_t *ep;
1365  SESSIONS_ITER(ctx->sessions, s, rtmp) {
1366  if (s->ifindex == ifindex && coap_address_equals(&s->addr_info.remote,
1367  remote_addr))
1368  return s;
1369  }
1370  LL_FOREACH(ctx->endpoint, ep) {
1371  SESSIONS_ITER(ep->sessions, s, rtmp) {
1372  if (s->ifindex == ifindex && coap_address_equals(&s->addr_info.remote,
1373  remote_addr))
1374  return s;
1375  }
1376  }
1377  return NULL;
1378 }
1379 
1380 const char *coap_session_str(const coap_session_t *session) {
1381  static char szSession[2 * (INET6_ADDRSTRLEN + 8) + 24];
1382  char *p = szSession, *end = szSession + sizeof(szSession);
1383  if (coap_print_addr(&session->addr_info.local,
1384  (unsigned char*)p, end - p) > 0)
1385  p += strlen(p);
1386  if (p + 6 < end) {
1387  strcpy(p, " <-> ");
1388  p += 5;
1389  }
1390  if (p + 1 < end) {
1391  if (coap_print_addr(&session->addr_info.remote,
1392  (unsigned char*)p, end - p) > 0)
1393  p += strlen(p);
1394  }
1395  if (session->ifindex > 0 && p + 1 < end)
1396  p += snprintf(p, end - p, " (if%d)", session->ifindex);
1397  if (p + 6 < end) {
1398  if (session->proto == COAP_PROTO_UDP) {
1399  strcpy(p, " UDP ");
1400  p += 4;
1401  } else if (session->proto == COAP_PROTO_DTLS) {
1402  strcpy(p, " DTLS");
1403  p += 5;
1404  } else if (session->proto == COAP_PROTO_TCP) {
1405  strcpy(p, " TCP ");
1406  p += 4;
1407  } else if (session->proto == COAP_PROTO_TLS) {
1408  strcpy(p, " TLS ");
1409  p += 4;
1410  } else {
1411  strcpy(p, " NONE");
1412  p += 5;
1413  }
1414  }
1415 
1416  return szSession;
1417 }
1418 
1419 const char *coap_endpoint_str(const coap_endpoint_t *endpoint) {
1420  static char szEndpoint[128];
1421  char *p = szEndpoint, *end = szEndpoint + sizeof(szEndpoint);
1422  if (coap_print_addr(&endpoint->bind_addr, (unsigned char*)p, end - p) > 0)
1423  p += strlen(p);
1424  if (p + 6 < end) {
1425  if (endpoint->proto == COAP_PROTO_UDP) {
1426  strcpy(p, " UDP");
1427  p += 4;
1428  } else if (endpoint->proto == COAP_PROTO_DTLS) {
1429  strcpy(p, " DTLS");
1430  p += 5;
1431  } else {
1432  strcpy(p, " NONE");
1433  p += 5;
1434  }
1435  }
1436 
1437  return szEndpoint;
1438 }
1439 
1440 #endif /* COAP_SESSION_C_ */
void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
Definition: address.c:102
uint16_t coap_address_get_port(const coap_address_t *addr)
Returns the port from addr in host byte order.
Definition: address.c:33
int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
Compares given address objects a and b.
Definition: address.c:60
COAP_STATIC_INLINE void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
Definition: address.h:152
Pulls together all the internal only header files.
int coap_socket_connect_udp(coap_socket_t *sock, const coap_address_t *local_if, const coap_address_t *server, int default_port, coap_address_t *local_addr, coap_address_t *remote_addr)
Definition: coap_io.c:242
void coap_socket_close(coap_socket_t *sock)
Definition: coap_io.c:368
ssize_t coap_socket_send(coap_socket_t *sock, coap_session_t *session, const uint8_t *data, size_t data_len)
Definition: coap_io.c:1508
ssize_t coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len)
Definition: coap_io.c:427
int coap_socket_bind_udp(coap_socket_t *sock, const coap_address_t *listen_addr, coap_address_t *bound_addr)
Definition: coap_io.c:154
const char * coap_socket_strerror(void)
Definition: coap_io.c:1502
void coap_mfree_endpoint(coap_endpoint_t *ep)
Definition: coap_io.c:149
coap_endpoint_t * coap_malloc_endpoint(void)
Definition: coap_io.c:144
coap_nack_reason_t
Definition: coap_io.h:64
@ COAP_NACK_NOT_DELIVERABLE
Definition: coap_io.h:66
@ COAP_NACK_TLS_FAILED
Definition: coap_io.h:68
@ COAP_NACK_ICMP_ISSUE
Definition: coap_io.h:69
#define COAP_SOCKET_WANT_ACCEPT
non blocking server socket is waiting for accept
#define COAP_SOCKET_NOT_EMPTY
the socket is not empty
#define COAP_SOCKET_BOUND
the socket is bound
#define COAP_SOCKET_WANT_READ
non blocking socket is waiting for reading
#define COAP_SOCKET_WANT_CONNECT
non blocking client socket is waiting for connect
#define COAP_SOCKET_CONNECTED
the socket is connected
#define COAP_SOCKET_EMPTY
coap_socket_flags_t values
void * coap_dtls_new_server_session(coap_session_t *session COAP_UNUSED)
Definition: coap_notls.c:105
int coap_dtls_context_set_pki(coap_context_t *ctx COAP_UNUSED, const coap_dtls_pki_t *setup_data COAP_UNUSED, const coap_dtls_role_t role COAP_UNUSED)
Definition: coap_notls.c:35
void * coap_dtls_new_client_session(coap_session_t *session COAP_UNUSED)
Definition: coap_notls.c:109
unsigned int coap_dtls_get_overhead(coap_session_t *session COAP_UNUSED)
Definition: coap_notls.c:159
void * coap_tls_new_client_session(coap_session_t *session COAP_UNUSED, int *connected COAP_UNUSED)
Definition: coap_notls.c:163
void * coap_tls_new_server_session(coap_session_t *session COAP_UNUSED, int *connected COAP_UNUSED)
Definition: coap_notls.c:167
int coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED)
Definition: coap_notls.c:65
int coap_dtls_context_set_cpsk(coap_context_t *ctx COAP_UNUSED, coap_dtls_cpsk_t *setup_data COAP_UNUSED)
Definition: coap_notls.c:51
void coap_dtls_free_session(coap_session_t *coap_session COAP_UNUSED)
Definition: coap_notls.c:113
void * coap_dtls_get_tls(const coap_session_t *c_session COAP_UNUSED, coap_tls_library_t *tls_lib)
Definition: coap_notls.c:76
void coap_tls_free_session(coap_session_t *coap_session COAP_UNUSED)
Definition: coap_notls.c:171
void coap_session_set_max_retransmit(coap_session_t *session, unsigned int value)
Set the CoAP maximum retransmit count before failure.
Definition: coap_session.c:25
coap_mid_t coap_session_send_ping(coap_session_t *session)
Send a ping message for the session.
Definition: coap_session.c:381
#define DTLS_HT_CLIENT_HELLO
#define OFF_HANDSHAKE_TYPE
void coap_session_set_ack_random_factor(coap_session_t *session, coap_fixed_point_t value)
Set the CoAP ack randomize factor.
Definition: coap_session.c:44
coap_fixed_point_t coap_session_get_ack_random_factor(const coap_session_t *session)
Get the CoAP ack randomize factor.
Definition: coap_session.c:65
static void coap_make_addr_hash(coap_addr_hash_t *addr_hash, coap_proto_t proto, const coap_addr_tuple_t *addr_info)
Definition: coap_session.c:545
#define DTLS_CT_HANDSHAKE
static coap_session_t * coap_session_create_client(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto)
Definition: coap_session.c:741
coap_session_t * coap_session_get_by_peer(const coap_context_t *ctx, const coap_address_t *remote_addr, int ifindex)
void coap_session_set_ack_timeout(coap_session_t *session, coap_fixed_point_t value)
Set the CoAP initial ack response timeout before the next re-transmit.
Definition: coap_session.c:34
#define OFF_CONTENT_TYPE
static coap_session_t * coap_make_session(coap_proto_t proto, coap_session_type_t type, const coap_addr_hash_t *addr_hash, const coap_address_t *local_addr, const coap_address_t *remote_addr, int ifindex, coap_context_t *context, coap_endpoint_t *endpoint)
Definition: coap_session.c:110
static coap_session_t * coap_session_accept(coap_session_t *session)
Definition: coap_session.c:869
static coap_session_t * coap_session_connect(coap_session_t *session)
Definition: coap_session.c:821
coap_fixed_point_t coap_session_get_ack_timeout(const coap_session_t *session)
Get the CoAP initial ack response timeout before the next re-transmit.
Definition: coap_session.c:60
#define INET6_ADDRSTRLEN
#define DTLS_CT_ALERT
unsigned int coap_session_get_max_retransmit(const coap_session_t *session)
Get the CoAP maximum retransmit before failure.
Definition: coap_session.c:55
#define SESSIONS_ADD(e, obj)
#define SESSIONS_ITER_SAFE(e, el, rtmp)
#define COAP_PARTIAL_SESSION_TIMEOUT_TICKS
#define SESSIONS_DELETE(e, obj)
#define COAP_DEFAULT_MAX_HANDSHAKE_SESSIONS
#define SESSIONS_ITER(e, el, rtmp)
#define SESSIONS_FIND(e, k, res)
int coap_tcp_is_supported(void)
Check whether TCP is available.
Definition: coap_tcp.c:33
void coap_block_delete_lg_srcv(coap_session_t *session, coap_lg_srcv_t *lg_srcv)
Definition: block.c:877
void coap_block_delete_lg_crcv(coap_session_t *session, coap_lg_crcv_t *lg_crcv)
Definition: block.c:862
void coap_block_delete_lg_xmit(coap_session_t *session, coap_lg_xmit_t *lg_xmit)
Definition: block.c:890
int coap_cancel_observe(coap_session_t *session, coap_binary_t *token, coap_pdu_type_t type)
Cancel an observe that is being tracked by the client large receive logic.
Definition: block.c:279
void coap_delete_cache_entry(coap_context_t *ctx, coap_cache_entry_t *cache_entry)
Remove a cache-entry from the hash list and free off all the appropriate contents apart from app_data...
Definition: coap_cache.c:220
#define COAP_DEFAULT_ACK_RANDOM_FACTOR
A factor that is used to randomize the wait time before a message is retransmitted to prevent synchro...
Definition: coap_session.h:412
#define COAP_DEFAULT_MAX_RETRANSMIT
Number of message retransmissions before message sending is stopped RFC 7252, Section 4....
Definition: coap_session.h:420
#define COAP_DEFAULT_ACK_TIMEOUT
Number of seconds when to expect an ACK or a response to an outstanding CON message.
Definition: coap_session.h:403
#define COAP_DEFAULT_NSTART
The number of simultaneous outstanding interactions that a client maintains to a given server.
Definition: coap_session.h:427
void coap_ticks(coap_tick_t *t)
Sets t to the internal time with COAP_TICKS_PER_SECOND resolution.
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:122
int coap_prng(void *buf, size_t len)
Fills buf with len random bytes using the default pseudo random number generator.
Definition: coap_prng.c:87
int coap_remove_from_queue(coap_queue_t **queue, coap_session_t *session, coap_mid_t id, coap_queue_t **node)
This function removes the element with given id from the list given list.
Definition: net.c:1935
int coap_delete_node(coap_queue_t *node)
Destroys specified node.
Definition: net.c:231
coap_queue_t * coap_new_node(void)
Creates a new node suitable for adding to the CoAP sendqueue.
Definition: net.c:260
coap_mid_t coap_send_internal(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
Definition: net.c:1154
unsigned int coap_calc_timeout(coap_session_t *session, unsigned char r)
Calculates the initial timeout based on the session CoAP transmission parameters 'ack_timeout',...
Definition: net.c:947
coap_mid_t coap_wait_ack(coap_context_t *context, coap_session_t *session, coap_queue_t *node)
Definition: net.c:973
void coap_cancel_session_messages(coap_context_t *context, coap_session_t *session, coap_nack_reason_t reason)
Cancels all outstanding messages for session session.
Definition: net.c:1979
uint16_t coap_new_message_id(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
int coap_handle_event(coap_context_t *context, coap_event_t event, coap_session_t *session)
Invokes the event handler of context for the given event and data.
Definition: net.c:3163
coap_session_t * coap_session_new_dtls_session(coap_session_t *session, coap_tick_t now)
Create a new DTLS session for the session.
Definition: coap_session.c:691
int coap_tls_is_supported(void)
Check whether TLS is available.
Definition: coap_notls.c:22
#define COAP_DTLS_PKI_SETUP_VERSION
Latest PKI setup version.
Definition: coap_dtls.h:239
int coap_dtls_is_supported(void)
Check whether DTLS is available.
Definition: coap_notls.c:17
coap_tls_library_t
Definition: coap_dtls.h:52
@ COAP_DTLS_ROLE_CLIENT
Internal function invoked for client.
Definition: coap_dtls.h:32
unsigned int coap_encode_var_safe(uint8_t *buf, size_t length, unsigned int val)
Encodes multiple-length byte sequences.
Definition: encode.c:40
uint64_t coap_decode_var_bytes8(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
Definition: encode.c:60
unsigned int coap_encode_var_safe8(uint8_t *buf, size_t length, uint64_t val)
Encodes multiple-length byte sequences.
Definition: encode.c:70
#define COAP_EVENT_TCP_CLOSED
Definition: coap_event.h:41
#define COAP_EVENT_SESSION_CONNECTED
CSM exchange events for reliable protocols only.
Definition: coap_event.h:47
#define COAP_EVENT_SESSION_FAILED
Definition: coap_event.h:49
#define COAP_EVENT_TCP_FAILED
Definition: coap_event.h:42
#define COAP_EVENT_SESSION_CLOSED
Definition: coap_event.h:48
#define COAP_EVENT_DTLS_CONNECTED
Definition: coap_event.h:33
#define COAP_EVENT_TCP_CONNECTED
TCP events for COAP_PROTO_TCP and COAP_PROTO_TLS.
Definition: coap_event.h:40
coap_log_t coap_get_log_level(void)
Get the current logging level.
Definition: coap_debug.c:63
const char * coap_session_str(const coap_session_t *session)
Get session description.
size_t coap_print_addr(const coap_address_t *addr, unsigned char *buf, size_t len)
Print the address into the defined buffer.
Definition: coap_debug.c:173
const char * coap_endpoint_str(const coap_endpoint_t *endpoint)
Get endpoint description.
#define coap_log(level,...)
Logging function.
Definition: coap_debug.h:152
@ LOG_ERR
Error.
Definition: coap_debug.h:55
@ LOG_CRIT
Critical.
Definition: coap_debug.h:54
@ LOG_INFO
Information.
Definition: coap_debug.h:58
@ LOG_WARNING
Warning.
Definition: coap_debug.h:56
@ LOG_DEBUG
Debug.
Definition: coap_debug.h:59
#define COAP_PDU_DELAYED
#define COAP_MAX_MESSAGE_SIZE_TCP8
#define COAP_DEFAULT_MAX_PDU_RX_SIZE
#define COAP_MAX_MESSAGE_SIZE_TCP0
size_t coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto)
Compose the protocol specific header for the specified PDU.
Definition: pdu.c:1127
#define COAP_MAX_MESSAGE_SIZE_TCP16
#define COAP_DEFAULT_PORT
Definition: pdu.h:35
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
Definition: pdu.c:142
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition: pdu.h:231
coap_proto_t
CoAP protocol types.
Definition: pdu.h:280
#define COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER
Definition: pdu.h:178
size_t coap_add_option(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Adds option of given number to pdu that is passed as first parameter.
Definition: pdu.c:543
#define COAPS_DEFAULT_PORT
Definition: pdu.h:36
#define COAP_INVALID_MID
Indicates an invalid message id.
Definition: pdu.h:234
#define COAP_DEFAULT_MTU
Definition: pdu.h:39
coap_pdu_t * coap_pdu_init(coap_pdu_type_t type, coap_pdu_code_t code, coap_mid_t mid, size_t size)
Creates a new CoAP PDU with at least enough storage space for the given size maximum message size.
Definition: pdu.c:87
#define COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE
Definition: pdu.h:177
@ COAP_PROTO_DTLS
Definition: pdu.h:283
@ COAP_PROTO_UDP
Definition: pdu.h:282
@ COAP_PROTO_NONE
Definition: pdu.h:281
@ COAP_PROTO_TLS
Definition: pdu.h:285
@ COAP_PROTO_TCP
Definition: pdu.h:284
@ COAP_SIGNALING_CODE_CSM
Definition: pdu.h:331
@ COAP_SIGNALING_CODE_PING
Definition: pdu.h:332
@ COAP_MESSAGE_NON
Definition: pdu.h:58
@ COAP_MESSAGE_CON
Definition: pdu.h:57
coap_session_t * coap_endpoint_get_session(coap_endpoint_t *endpoint, const coap_packet_t *packet, coap_tick_t now)
Lookup the server session for the packet received on an endpoint, or create a new one.
Definition: coap_session.c:554
ssize_t coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu, coap_queue_t *node)
Definition: coap_session.c:312
int coap_session_refresh_psk_hint(coap_session_t *session, const coap_bin_const_t *psk_hint)
Refresh the session's current Identity Hint (PSK).
Definition: coap_session.c:996
void coap_session_send_csm(coap_session_t *session)
Notify session transport has just connected and CSM exchange can now start.
Definition: coap_session.c:351
coap_session_t * coap_new_server_session(coap_context_t *ctx, coap_endpoint_t *ep)
Creates a new server session for the specified endpoint.
ssize_t coap_session_send(coap_session_t *session, const uint8_t *data, size_t datalen)
Function interface for datagram data transmission.
Definition: coap_session.c:277
int coap_session_refresh_psk_key(coap_session_t *session, const coap_bin_const_t *psk_key)
Refresh the session's current pre-shared key (PSK).
void coap_session_connected(coap_session_t *session)
Notify session that it has just connected or reconnected.
Definition: coap_session.c:399
ssize_t coap_session_send_pdu(coap_session_t *session, coap_pdu_t *pdu)
Send a pdu according to the session's protocol.
Definition: net.c:766
void coap_session_free(coap_session_t *session)
Definition: coap_session.c:225
void coap_session_mfree(coap_session_t *session)
Definition: coap_session.c:159
ssize_t coap_session_write(coap_session_t *session, const uint8_t *data, size_t datalen)
Function interface for stream data transmission.
Definition: coap_session.c:298
void * coap_session_get_tls(const coap_session_t *session, coap_tls_library_t *tls_lib)
Get the session TLS security ptr (TLS type dependent)
coap_session_type_t
coap_session_type_t values
Definition: coap_session.h:41
void * coap_session_get_app_data(const coap_session_t *session)
Returns any application-specific data that has been stored with session using the function coap_sessi...
Definition: coap_session.c:104
void coap_free_endpoint(coap_endpoint_t *ep)
const coap_address_t * coap_session_get_addr_local(const coap_session_t *session)
Get the local IP address from the session.
void coap_session_set_mtu(coap_session_t *session, unsigned mtu)
Set the session MTU.
Definition: coap_session.c:265
const coap_bin_const_t * coap_session_get_psk_key(const coap_session_t *session)
Get the session's current pre-shared key (PSK).
coap_proto_t coap_session_get_proto(const coap_session_t *session)
Get the session protocol type.
coap_context_t * coap_session_get_context(const coap_session_t *session)
Get the session context.
size_t coap_session_max_pdu_size(const coap_session_t *session)
Get maximum acceptable PDU size.
Definition: coap_session.c:244
coap_session_state_t coap_session_get_state(const coap_session_t *session)
Get the session state.
coap_session_state_t
coap_session_state_t values
Definition: coap_session.h:52
coap_session_t * coap_new_client_session_psk(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, const char *identity, const uint8_t *key, unsigned key_len)
Creates a new client session to the designated server with PSK credentials.
Definition: coap_session.c:913
#define COAP_PROTO_NOT_RELIABLE(p)
Definition: coap_session.h:35
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
Definition: coap_session.c:70
coap_session_t * coap_new_client_session(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto)
Creates a new client session to the designated server.
Definition: coap_session.c:898
void coap_session_init_token(coap_session_t *session, size_t len, const uint8_t *data)
Initializes the token value to use as a starting point.
#define COAP_PROTO_RELIABLE(p)
Definition: coap_session.h:36
void coap_session_new_token(coap_session_t *session, size_t *len, uint8_t *data)
Creates a new token for use.
void coap_session_set_app_data(coap_session_t *session, void *app_data)
Stores data with the given session.
Definition: coap_session.c:98
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
Definition: coap_session.c:76
coap_session_t * coap_new_client_session_pki(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_pki_t *setup_data)
Creates a new client session to the designated server with PKI credentials.
void coap_endpoint_set_default_mtu(coap_endpoint_t *ep, unsigned mtu)
Set the endpoint's default MTU.
const coap_address_t * coap_session_get_addr_remote(const coap_session_t *session)
Get the remote IP address from the session.
const coap_bin_const_t * coap_session_get_psk_hint(const coap_session_t *session)
Get the server session's current Identity Hint (PSK).
coap_endpoint_t * coap_new_endpoint(coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto)
Create a new endpoint for communicating with peers.
int coap_session_set_type_client(coap_session_t *session)
Set the session type to client.
coap_session_t * coap_new_client_session_psk2(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_cpsk_t *setup_data)
Creates a new client session to the designated server with PSK credentials.
Definition: coap_session.c:940
void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
Definition: coap_session.c:456
int coap_session_get_ifindex(const coap_session_t *session)
Get the session if index.
coap_session_type_t coap_session_get_type(const coap_session_t *session)
Get the session type.
@ COAP_SESSION_TYPE_HELLO
server-side ephemeral session for responding to a client hello
Definition: coap_session.h:45
@ COAP_SESSION_TYPE_SERVER
server-side
Definition: coap_session.h:44
@ COAP_SESSION_TYPE_CLIENT
client-side
Definition: coap_session.h:43
@ COAP_SESSION_STATE_HANDSHAKE
Definition: coap_session.h:55
@ COAP_SESSION_STATE_CSM
Definition: coap_session.h:56
@ COAP_SESSION_STATE_ESTABLISHED
Definition: coap_session.h:57
@ COAP_SESSION_STATE_NONE
Definition: coap_session.h:53
@ COAP_SESSION_STATE_CONNECTING
Definition: coap_session.h:54
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
Definition: str.c:104
#define coap_binary_equal(binary1, binary2)
Compares the two binary data for equality.
Definition: str.h:197
coap_bin_const_t * coap_new_bin_const(const uint8_t *data, size_t size)
Take the specified byte array (text) and create a coap_bin_const_t * Returns a new const binary objec...
Definition: str.c:95
void coap_delete_observers(coap_context_t *context, coap_session_t *session)
Removes any subscription for session and releases the allocated storage.
Definition: resource.c:842
int coap_socket_bind_tcp(coap_socket_t *sock, const coap_address_t *listen_addr, coap_address_t *bound_addr)
Create a new TCP socket and then listen for new incoming TCP sessions.
int coap_socket_connect_tcp1(coap_socket_t *sock, const coap_address_t *local_if, const coap_address_t *server, int default_port, coap_address_t *local_addr, coap_address_t *remote_addr)
Create a new TCP socket and initiate the connection.
int coap_socket_accept_tcp(coap_socket_t *server, coap_socket_t *new_client, coap_address_t *local_addr, coap_address_t *remote_addr)
Accept a new incoming TCP session.
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
COAP_STATIC_INLINE void coap_free(void *object)
Wrapper function to coap_free_type() for backwards compatibility.
Definition: mem.h:105
@ COAP_SESSION
Definition: mem.h:46
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
Only used for servers for hashing incoming packets.
uint16_t lport
local port
coap_address_t remote
remote address and port
coap_proto_t proto
CoAP protocol.
coap_address_t remote
remote address and port
Definition: coap_io.h:51
coap_address_t local
local address and port
Definition: coap_io.h:52
multi-purpose address abstraction
Definition: address.h:96
CoAP binary data definition with const data.
Definition: str.h:58
size_t length
length of binary data
Definition: str.h:59
const uint8_t * s
read-only binary data
Definition: str.h:60
coap_session_t * session
The CoAP stack's global state is stored in a coap_context_t object.
coap_session_t * sessions
client sessions
coap_nack_handler_t nack_handler
unsigned int max_handshake_sessions
Maximum number of simultaneous negotating sessions per endpoint.
coap_queue_t * sendqueue
coap_cache_entry_t * cache
CoAP cache-entry cache.
coap_endpoint_t * endpoint
the endpoints used for listening
uint8_t block_mode
Zero or more COAP_BLOCK_ or'd options.
unsigned int max_idle_sessions
Maximum number of simultaneous unused sessions per endpoint.
coap_bin_const_t key
Definition: coap_dtls.h:309
coap_bin_const_t identity
Definition: coap_dtls.h:308
The structure used for defining the Client PSK setup data to be used.
Definition: coap_dtls.h:338
coap_dtls_cpsk_info_t psk_info
Client PSK definition.
Definition: coap_dtls.h:367
The structure used for defining the PKI setup data to be used.
Definition: coap_dtls.h:244
uint8_t version
Definition: coap_dtls.h:245
Abstraction of virtual endpoint that can be attached to coap_context_t.
coap_context_t * context
endpoint's context
uint16_t default_mtu
default mtu for this interface
coap_session_t * sessions
hash table or list of active sessions
coap_address_t bind_addr
local interface address
coap_socket_t sock
socket object for the interface, if any
coap_proto_t proto
protocol used on this interface
Abstraction of a fixed point number that can be used where necessary instead of a float.
Definition: coap_session.h:29
uint16_t fractional_part
Fractional part of fixed point variable 1/1000 (3 points) precision.
Definition: coap_session.h:31
uint16_t integer_part
Integer part of fixed point variable.
Definition: coap_session.h:30
Structure to hold large body (many blocks) client receive information.
coap_binary_t * app_token
app requesting PDU token
uint8_t observe_set
Set if this is an observe receive PDU.
Structure to hold large body (many blocks) server receive information.
Structure to hold large body (many blocks) transmission information.
size_t length
length of payload
coap_addr_tuple_t addr_info
local and remote addresses
unsigned char payload[COAP_RXBUFFER_SIZE]
payload
int ifindex
the interface index
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
uint8_t hdr_size
actual size used for protocol-specific header
coap_mid_t mid
message id, if any, in regular host byte order
size_t used_size
used bytes of storage for token, options and payload
coap_pdu_type_t type
message type
Queue entry.
coap_session_t * session
the CoAP session
coap_pdu_t * pdu
the CoAP PDU to send
unsigned int timeout
the randomized timeout value
struct coap_queue_t * next
coap_mid_t id
CoAP message id.
coap_tick_t t
when to send PDU for the next time
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
coap_lg_xmit_t * lg_xmit
list of large transmissions
coap_bin_const_t * psk_key
If client, this field contains the current pre-shared key for server; When this field is NULL,...
coap_endpoint_t * endpoint
session's endpoint
coap_socket_t sock
socket object for the session, if any
unsigned int max_retransmit
maximum re-transmit count (default 4)
coap_pdu_t * partial_pdu
incomplete incoming pdu
coap_bin_const_t * psk_identity
If client, this field contains the current identity for server; When this field is NULL,...
coap_session_state_t state
current state of relationaship with peer
uint64_t tx_token
Next token number to use.
uint8_t block_mode
Zero or more COAP_BLOCK_ or'd options.
coap_addr_tuple_t addr_info
key: remote/local address info
coap_proto_t proto
protocol used
uint16_t tx_mid
the last message id that was used in this session
coap_bin_const_t * psk_hint
If client, this field contains the server provided identity hint.
coap_dtls_cpsk_t cpsk_setup_data
client provided PSK initial setup data
size_t mtu
path or CSM mtu
size_t partial_read
if > 0 indicates number of bytes already read for an incoming message
int dtls_event
Tracking any (D)TLS events on this sesison.
void * tls
security parameters
coap_fixed_point_t ack_random_factor
ack random factor backoff (default 1.5)
uint8_t con_active
Active CON request sent.
coap_queue_t * delayqueue
list of delayed messages waiting to be sent
size_t tls_overhead
overhead of TLS layer
void * app
application-specific data
coap_mid_t last_ping_mid
the last keepalive message id that was used in this session
coap_lg_srcv_t * lg_srcv
Server list of expected large receives.
coap_lg_crcv_t * lg_crcv
Client list of expected large receives.
coap_fixed_point_t ack_timeout
timeout waiting for ack (default 2 secs)
coap_session_type_t type
client or server side socket
coap_context_t * context
session's context
size_t partial_write
if > 0 indicates number of bytes already written from the pdu at the head of sendqueue
coap_addr_hash_t addr_hash
Address hash for server incoming packets.
int ifindex
interface index
coap_session_t * session
coap_endpoint_t * endpoint
coap_socket_flags_t flags