1 /* pdu.c -- CoAP message structure
3 * Copyright (C) 2010,2011 Olaf Bergmann <bergmann@tzi.org>
5 * This file is part of the CoAP library libcoap. Please see
6 * README for terms of use.
11 #if defined(HAVE_ASSERT_H) && !defined(assert)
18 #ifdef HAVE_ARPA_INET_H
19 #include <arpa/inet.h>
35 typedef unsigned char _pdu[sizeof(coap_pdu_t) + COAP_MAX_PDU_SIZE];
37 MEMB(pdu_storage, _pdu, COAP_PDU_MAXCNT);
41 coap_pdu_resources_init()
43 memb_init(&pdu_storage);
45 #else /* WITH_CONTIKI */
47 #endif /* WITH_CONTIKI */
49 void coap_pdu_clear(coap_pdu_t *pdu, size_t size, coap_transport_type transport, unsigned int length)
53 memset(pdu, 0, sizeof(coap_pdu_t) + size);
55 pdu->hdr = (coap_hdr_t *) ((unsigned char *) pdu + sizeof(coap_pdu_t));
57 if (coap_udp == transport)
59 pdu->hdr->coap_hdr_udp_t.version = COAP_DEFAULT_VERSION;
60 /* data is NULL unless explicitly set by coap_add_data() */
61 pdu->length = sizeof(pdu->hdr->coap_hdr_udp_t);
66 /* data is NULL unless explicitly set by coap_add_data() */
74 coap_pdu_from_pbuf(struct pbuf *pbuf)
76 LWIP_ASSERT("Can only deal with contiguous PBUFs", pbuf->tot_len == pbuf->len);
77 LWIP_ASSERT("coap_read needs to receive an exclusive copy of the incoming pbuf", pbuf->ref == 1);
79 void *data = pbuf->payload;
82 u8_t header_error = pbuf_header(pbuf, sizeof(coap_pdu_t));
83 LWIP_ASSERT("CoAP PDU header does not fit in existing header space", header_error == 0);
85 result = (coap_pdu_t *)pbuf->payload;
87 memset(result, 0, sizeof(coap_pdu_t));
89 result->max_size = pbuf->tot_len - sizeof(coap_pdu_t);
90 result->length = pbuf->tot_len - sizeof(coap_pdu_t);
99 coap_pdu_init(unsigned char type, unsigned char code, unsigned short id,
100 size_t size, coap_transport_type transport)
107 unsigned int length = 0;
111 length = sizeof(pdu->hdr->coap_hdr_udp_t);
115 length = COAP_TCP_HEADER_NO_FIELD;
118 length = COAP_TCP_HEADER_8_BIT;
121 length = COAP_TCP_HEADER_16_BIT;
124 length = COAP_TCP_HEADER_32_BIT;
128 debug("it has wrong type\n");
132 assert(size <= COAP_MAX_PDU_SIZE);
133 /* Size must be large enough to fit the header. */
134 if (size < length || size > COAP_MAX_PDU_SIZE)
138 /* size must be large enough for hdr */
139 #if defined(WITH_POSIX) || defined(WITH_ARDUINO)
140 pdu = (coap_pdu_t *) coap_malloc(sizeof(coap_pdu_t) + size);
143 pdu = (coap_pdu_t *)memb_alloc(&pdu_storage);
146 p = pbuf_alloc(PBUF_TRANSPORT, size, PBUF_RAM);
149 u8_t header_error = pbuf_header(p, sizeof(coap_pdu_t));
150 /* we could catch that case and allocate larger memory in advance, but then
151 * again, we'd run into greater trouble with incoming packages anyway */
152 LWIP_ASSERT("CoAP PDU header does not fit in transport header", header_error == 0);
162 coap_pdu_clear(pdu, size, transport, length);
167 pdu->hdr->coap_hdr_udp_t.id = id;
168 pdu->hdr->coap_hdr_udp_t.type = type;
169 pdu->hdr->coap_hdr_udp_t.code = code;
173 pdu->hdr->coap_hdr_tcp_t.message_length = 0;
174 pdu->hdr->coap_hdr_tcp_t.code = code;
177 pdu->hdr->coap_hdr_tcp_8bit_t.message_length = COAP_TCP_LENGTH_FIELD_NUM_8_BIT;
178 pdu->hdr->coap_hdr_tcp_8bit_t.length_byte = 0;
179 pdu->hdr->coap_hdr_tcp_8bit_t.code = code;
182 pdu->hdr->coap_hdr_tcp_16bit_t.header_data[0] = COAP_TCP_LENGTH_FIELD_NUM_16_BIT << 4;
183 pdu->hdr->coap_hdr_tcp_16bit_t.header_data[3] = code;
186 pdu->hdr->coap_hdr_tcp_32bit_t.header_data[0] = COAP_TCP_LENGTH_FIELD_NUM_32_BIT << 4;
187 pdu->hdr->coap_hdr_tcp_32bit_t.header_data[5] = code;
191 debug("it has wrong type\n");
202 coap_new_pdu(coap_transport_type transport, unsigned int size)
207 pdu = coap_pdu_init(0, 0,
208 ntohs(COAP_INVALID_TID),
215 #else /* WITH_CONTIKI */
216 pdu = coap_pdu_init(0, 0, uip_ntohs(COAP_INVALID_TID),
223 #endif /* WITH_CONTIKI */
227 coap_log(LOG_CRIT, "coap_new_pdu: cannot allocate memory for new PDU\n");
232 void coap_delete_pdu(coap_pdu_t *pdu)
234 #if defined(WITH_POSIX) || defined(WITH_ARDUINO)
238 if (pdu != NULL) /* accepting double free as the other implementation accept that too */
239 pbuf_free(pdu->pbuf);
242 memb_free(&pdu_storage, pdu);
247 coap_transport_type coap_get_tcp_header_type_from_size(unsigned int size)
249 if (COAP_TCP_LENGTH_LIMIT_8_BIT < size && COAP_TCP_LENGTH_LIMIT_16_BIT >= size)
251 return coap_tcp_8bit;
253 else if (COAP_TCP_LENGTH_LIMIT_16_BIT < size && COAP_TCP_LENGTH_LIMIT_32_BIT >= size)
255 return coap_tcp_16bit;
257 else if (COAP_TCP_LENGTH_LIMIT_32_BIT < size)
259 return coap_tcp_32bit;
267 coap_transport_type coap_get_tcp_header_type_from_initbyte(unsigned int length)
269 coap_transport_type type;
272 case COAP_TCP_LENGTH_FIELD_NUM_8_BIT:
273 type = coap_tcp_8bit;
275 case COAP_TCP_LENGTH_FIELD_NUM_16_BIT:
276 type = coap_tcp_16bit;
278 case COAP_TCP_LENGTH_FIELD_NUM_32_BIT:
279 type = coap_tcp_32bit;
287 void coap_add_length(const coap_pdu_t *pdu, coap_transport_type transport, unsigned int length)
294 pdu->hdr->coap_hdr_tcp_t.message_length = length;
297 if (length > COAP_TCP_LENGTH_FIELD_8_BIT)
299 pdu->hdr->coap_hdr_tcp_8bit_t.length_byte =
300 length - COAP_TCP_LENGTH_FIELD_8_BIT;
304 if (length > COAP_TCP_LENGTH_FIELD_16_BIT)
306 unsigned int total_length = length - COAP_TCP_LENGTH_FIELD_16_BIT;
307 pdu->hdr->coap_hdr_tcp_16bit_t.header_data[1] = (total_length >> 8) & 0x0000ff;
308 pdu->hdr->coap_hdr_tcp_16bit_t.header_data[2] = total_length & 0x000000ff;
312 if (length > COAP_TCP_LENGTH_FIELD_32_BIT)
314 unsigned int total_length = length - COAP_TCP_LENGTH_FIELD_32_BIT;
315 pdu->hdr->coap_hdr_tcp_32bit_t.header_data[1] = total_length >> 24;
316 pdu->hdr->coap_hdr_tcp_32bit_t.header_data[2] = (total_length >> 16) & 0x00ff;
317 pdu->hdr->coap_hdr_tcp_32bit_t.header_data[3] = (total_length >> 8) & 0x0000ff;
318 pdu->hdr->coap_hdr_tcp_32bit_t.header_data[4] = total_length & 0x000000ff;
322 debug("it has wrong type\n");
326 unsigned int coap_get_length_from_header(const unsigned char *header, coap_transport_type transport)
330 unsigned int length = 0;
331 unsigned int length_field_data = 0;
335 length = header[1] + COAP_TCP_LENGTH_FIELD_8_BIT;
338 length_field_data = (header[1] << 8 | header[2]);
339 length = length_field_data + COAP_TCP_LENGTH_FIELD_16_BIT;
342 length_field_data = header[1] << 24 | header[2] << 16 | header[3] << 8 | header[4];
343 length = length_field_data + COAP_TCP_LENGTH_FIELD_32_BIT;
346 debug("it has wrong type\n");
352 unsigned int coap_get_length(const coap_pdu_t *pdu, coap_transport_type transport)
356 unsigned int length = 0;
357 unsigned int length_field_data = 0;
361 length = pdu->hdr->coap_hdr_tcp_t.message_length;
364 length = pdu->hdr->coap_hdr_tcp_8bit_t.length_byte + COAP_TCP_LENGTH_FIELD_8_BIT;
368 pdu->hdr->coap_hdr_tcp_16bit_t.header_data[1] << 8 |
369 pdu->hdr->coap_hdr_tcp_16bit_t.header_data[2];
370 length = length_field_data + COAP_TCP_LENGTH_FIELD_16_BIT;
374 pdu->hdr->coap_hdr_tcp_32bit_t.header_data[1] << 24 |
375 pdu->hdr->coap_hdr_tcp_32bit_t.header_data[2] << 16 |
376 pdu->hdr->coap_hdr_tcp_32bit_t.header_data[3] << 8 |
377 pdu->hdr->coap_hdr_tcp_32bit_t.header_data[4];
378 length = length_field_data + COAP_TCP_LENGTH_FIELD_32_BIT;
381 debug("it has wrong type\n");
387 unsigned int coap_get_tcp_header_length(unsigned char *data)
391 unsigned int tokenLength = data[0] & 0x0f;
392 coap_transport_type transport =
393 coap_get_tcp_header_type_from_initbyte(data[0] >> 4);
394 unsigned int length = 0;
396 length = coap_get_tcp_header_length_for_transport(transport) + tokenLength;
400 unsigned int coap_get_tcp_header_length_for_transport(coap_transport_type transport)
402 unsigned int length = 0;
406 length = COAP_TCP_HEADER_NO_FIELD;
409 length = COAP_TCP_HEADER_8_BIT;
412 length = COAP_TCP_HEADER_16_BIT;
415 length = COAP_TCP_HEADER_32_BIT;
418 debug("it has wrong type\n");
426 void coap_add_code(const coap_pdu_t *pdu, coap_transport_type transport, unsigned int code)
433 pdu->hdr->coap_hdr_udp_t.code = COAP_RESPONSE_CODE(code);
437 pdu->hdr->coap_hdr_tcp_t.code = COAP_RESPONSE_CODE(code);
440 pdu->hdr->coap_hdr_tcp_8bit_t.code = COAP_RESPONSE_CODE(code);
443 pdu->hdr->coap_hdr_tcp_16bit_t.header_data[3] = COAP_RESPONSE_CODE(code);
446 pdu->hdr->coap_hdr_tcp_32bit_t.header_data[5] = COAP_RESPONSE_CODE(code);
450 debug("it has wrong type\n");
454 unsigned int coap_get_code(const coap_pdu_t *pdu, coap_transport_type transport)
458 unsigned int code = 0;
462 code = pdu->hdr->coap_hdr_udp_t.code;
466 code = pdu->hdr->coap_hdr_tcp_t.code;
469 code = pdu->hdr->coap_hdr_tcp_8bit_t.code;
472 code = pdu->hdr->coap_hdr_tcp_16bit_t.header_data[3];
475 code = pdu->hdr->coap_hdr_tcp_32bit_t.header_data[5];
479 debug("it has wrong type\n");
484 int coap_add_token(coap_pdu_t *pdu, size_t len, const unsigned char *data,
485 coap_transport_type transport)
487 const size_t HEADERLENGTH = len + 4;
488 /* must allow for pdu == NULL as callers may rely on this */
489 if (!pdu || len > 8 || pdu->max_size < HEADERLENGTH)
492 unsigned char* token = NULL;
496 pdu->hdr->coap_hdr_udp_t.token_length = len;
497 token = pdu->hdr->coap_hdr_udp_t.token;
498 pdu->length = HEADERLENGTH;
502 pdu->hdr->coap_hdr_tcp_t.token_length = len;
503 token = pdu->hdr->coap_hdr_tcp_t.token;
504 pdu->length = len + COAP_TCP_HEADER_NO_FIELD;
507 pdu->hdr->coap_hdr_tcp_8bit_t.token_length = len;
508 token = pdu->hdr->coap_hdr_tcp_8bit_t.token;
509 pdu->length = len + COAP_TCP_HEADER_8_BIT;
512 pdu->hdr->coap_hdr_tcp_16bit_t.header_data[0] =
513 pdu->hdr->coap_hdr_tcp_16bit_t.header_data[0] | len;
514 token = pdu->hdr->coap_hdr_tcp_16bit_t.token;
515 pdu->length = len + COAP_TCP_HEADER_16_BIT;
518 pdu->hdr->coap_hdr_tcp_32bit_t.header_data[0] =
519 pdu->hdr->coap_hdr_tcp_32bit_t.header_data[0] | len;
520 token = pdu->hdr->coap_hdr_tcp_32bit_t.token;
521 pdu->length = len + COAP_TCP_HEADER_32_BIT;
525 debug("it has wrong type\n");
530 memcpy(token, data, len);
539 void coap_get_token(const coap_hdr_t *pdu_hdr, coap_transport_type transport,
540 unsigned char **token, unsigned int *token_length)
544 assert(token_length);
549 *token_length = pdu_hdr->coap_hdr_udp_t.token_length;
550 *token = (unsigned char *)pdu_hdr->coap_hdr_udp_t.token;
554 *token_length = pdu_hdr->coap_hdr_tcp_t.token_length;
555 *token = (unsigned char *)pdu_hdr->coap_hdr_tcp_t.token;
558 *token_length = pdu_hdr->coap_hdr_tcp_8bit_t.token_length;
559 *token = (unsigned char *)pdu_hdr->coap_hdr_tcp_8bit_t.token;
562 *token_length = (pdu_hdr->coap_hdr_tcp_16bit_t.header_data[0]) & 0x0f;
563 *token = (unsigned char *)pdu_hdr->coap_hdr_tcp_16bit_t.token;
566 *token_length = (pdu_hdr->coap_hdr_tcp_32bit_t.header_data[0]) & 0x0f;
567 *token = (unsigned char *)pdu_hdr->coap_hdr_tcp_32bit_t.token;
571 debug("it has wrong type\n");
575 /** @FIXME de-duplicate code with coap_add_option_later */
576 size_t coap_add_option(coap_pdu_t *pdu, unsigned short type, unsigned int len,
577 const unsigned char *data, coap_transport_type transport)
585 if (type < pdu->max_delta)
587 warn("coap_add_option: options are not in correct order\n");
595 opt = (unsigned char *) &(pdu->hdr->coap_hdr_tcp_t) + pdu->length;
598 opt = (unsigned char *) &(pdu->hdr->coap_hdr_tcp_8bit_t) + pdu->length;
601 opt = (unsigned char *) &(pdu->hdr->coap_hdr_tcp_16bit_t) + pdu->length;
604 opt = (unsigned char *) &(pdu->hdr->coap_hdr_tcp_32bit_t) + pdu->length;
608 opt = (unsigned char *) &(pdu->hdr->coap_hdr_udp_t) + pdu->length;
612 /* encode option and check length */
613 optsize = coap_opt_encode(opt, pdu->max_size - pdu->length, type - pdu->max_delta, data, len);
617 warn("coap_add_option: cannot add option\n");
623 pdu->max_delta = type;
624 pdu->length += optsize;
630 /** @FIXME de-duplicate code with coap_add_option */
632 coap_add_option_later(coap_pdu_t *pdu, unsigned short type, unsigned int len)
640 if (type < pdu->max_delta)
642 warn("coap_add_option: options are not in correct order\n");
646 opt = (unsigned char *) pdu->hdr + pdu->length;
648 /* encode option and check length */
649 optsize = coap_opt_encode(opt, pdu->max_size - pdu->length, type - pdu->max_delta, NULL, len);
653 warn("coap_add_option: cannot add option\n");
659 pdu->max_delta = type;
660 pdu->length += optsize;
663 return ((unsigned char*) opt) + optsize - len;
666 int coap_add_data(coap_pdu_t *pdu, unsigned int len, const unsigned char *data)
669 assert(pdu->data == NULL);
674 if (pdu->length + len + 1 > pdu->max_size)
676 warn("coap_add_data: cannot add: data too large for PDU\n");
677 assert(pdu->data == NULL);
681 pdu->data = (unsigned char *) pdu->hdr + pdu->length;
682 *pdu->data = COAP_PAYLOAD_START;
685 memcpy(pdu->data, data, len);
686 pdu->length += len + 1;
690 int coap_get_data(const coap_pdu_t *pdu, size_t *len, unsigned char **data)
698 *len = (unsigned char *) pdu->hdr + pdu->length - pdu->data;
702 { /* no data, clear everything */
707 return *data != NULL;
710 #ifndef SHORT_ERROR_RESPONSE
717 /* if you change anything here, make sure, that the longest string does not
718 * exceed COAP_ERROR_PHRASE_LENGTH. */
719 error_desc_t coap_error[] =
721 { COAP_RESPONSE_CODE(65), "2.01 Created" },
722 { COAP_RESPONSE_CODE(66), "2.02 Deleted" },
723 { COAP_RESPONSE_CODE(67), "2.03 Valid" },
724 { COAP_RESPONSE_CODE(68), "2.04 Changed" },
725 { COAP_RESPONSE_CODE(69), "2.05 Content" },
726 { COAP_RESPONSE_CODE(400), "Bad Request" },
727 { COAP_RESPONSE_CODE(401), "Unauthorized" },
728 { COAP_RESPONSE_CODE(402), "Bad Option" },
729 { COAP_RESPONSE_CODE(403), "Forbidden" },
730 { COAP_RESPONSE_CODE(404), "Not Found" },
731 { COAP_RESPONSE_CODE(405), "Method Not Allowed" },
732 { COAP_RESPONSE_CODE(408), "Request Entity Incomplete" },
733 { COAP_RESPONSE_CODE(413), "Request Entity Too Large" },
734 { COAP_RESPONSE_CODE(415), "Unsupported Media Type" },
735 { COAP_RESPONSE_CODE(500), "Internal Server Error" },
736 { COAP_RESPONSE_CODE(501), "Not Implemented" },
737 { COAP_RESPONSE_CODE(502), "Bad Gateway" },
738 { COAP_RESPONSE_CODE(503), "Service Unavailable" },
739 { COAP_RESPONSE_CODE(504), "Gateway Timeout" },
740 { COAP_RESPONSE_CODE(505), "Proxying Not Supported" },
741 { 0, NULL } /* end marker */
745 coap_response_phrase(unsigned char code)
748 for (i = 0; coap_error[i].code; ++i)
750 if (coap_error[i].code == code)
751 return coap_error[i].phrase;
758 * Advances *optp to next option if still in PDU. This function
759 * returns the number of bytes opt has been advanced or @c 0
762 static size_t next_option_safe(coap_opt_t **optp, size_t *length, coap_option_t* option)
764 //coap_option_t option;
770 optsize = coap_opt_parse(*optp, *length, option);
773 assert(optsize <= *length);
782 int coap_pdu_parse(unsigned char *data, size_t length, coap_pdu_t *pdu,
783 coap_transport_type transport)
788 if (pdu->max_size < length)
790 debug("insufficient space to store parsed PDU\n");
791 printf("[COAP] insufficient space to store parsed PDU\n");
795 unsigned int headerSize = 0;
797 if (coap_udp == transport)
799 headerSize = sizeof(pdu->hdr->coap_hdr_udp_t);
804 headerSize = coap_get_tcp_header_length_for_transport(transport);
808 if (length < headerSize)
810 debug("discarded invalid PDU\n");
813 coap_opt_t *opt = NULL;
814 unsigned int tokenLength = 0;
821 pdu->hdr->coap_hdr_tcp_t.message_length = data[0] >> 4;
822 pdu->hdr->coap_hdr_tcp_t.token_length = data[0] & 0x0f;
823 pdu->hdr->coap_hdr_tcp_t.code = data[1];
824 tokenLength = pdu->hdr->coap_hdr_tcp_t.token_length;
825 opt = (unsigned char *) (&(pdu->hdr->coap_hdr_tcp_t) + 1) + tokenLength;
828 pdu->hdr->coap_hdr_tcp_8bit_t.message_length = data[0] >> 4;
829 pdu->hdr->coap_hdr_tcp_8bit_t.token_length = data[0] & 0x0f;
830 pdu->hdr->coap_hdr_tcp_8bit_t.length_byte = data[1];
831 pdu->hdr->coap_hdr_tcp_8bit_t.code = data[2];
832 tokenLength = pdu->hdr->coap_hdr_tcp_8bit_t.token_length;
833 opt = (unsigned char *) (&(pdu->hdr->coap_hdr_tcp_8bit_t))
834 + tokenLength + COAP_TCP_HEADER_8_BIT;
837 for (size_t i = 0 ; i < headerSize ; i++)
839 pdu->hdr->coap_hdr_tcp_16bit_t.header_data[i] = data[i];
842 tokenLength = data[0] & 0x0f;
843 opt = (unsigned char *) (&(pdu->hdr->coap_hdr_tcp_16bit_t) + 1) + tokenLength;
846 for (size_t i = 0 ; i < headerSize ; i++)
848 pdu->hdr->coap_hdr_tcp_32bit_t.header_data[i] = data[i];
851 tokenLength = data[0] & 0x0f;
852 opt = ((unsigned char *) &(pdu->hdr->coap_hdr_tcp_32bit_t)) +
853 headerSize + tokenLength;
856 printf("it has wrong type\n");
859 pdu->length = length;
861 if (coap_udp == transport)
863 pdu->hdr->coap_hdr_udp_t.version = data[0] >> 6;
864 pdu->hdr->coap_hdr_udp_t.type = (data[0] >> 4) & 0x03;
865 pdu->hdr->coap_hdr_udp_t.token_length = data[0] & 0x0f;
866 pdu->hdr->coap_hdr_udp_t.code = data[1];
869 tokenLength = pdu->hdr->coap_hdr_udp_t.token_length;
872 if (pdu->hdr->coap_hdr_udp_t.code == 0)
874 if (length != headerSize || tokenLength)
876 debug("coap_pdu_parse: empty message is not empty\n");
881 if (length < headerSize + tokenLength || tokenLength > 8)
883 debug("coap_pdu_parse: invalid Token\n");
887 memcpy(&pdu->hdr->coap_hdr_udp_t.id, data + 2, 2);
889 /* Finally calculate beginning of data block and thereby check integrity
890 * of the PDU structure. */
892 /* append data (including the Token) to pdu structure */
893 memcpy(&(pdu->hdr->coap_hdr_udp_t) + 1, data + headerSize, length - headerSize);
895 /* skip header + token */
896 length -= (tokenLength + headerSize);
897 opt = (unsigned char *) (&(pdu->hdr->coap_hdr_udp_t) + 1) + tokenLength;
900 else // common for tcp header setting
904 if (length < headerSize + tokenLength || tokenLength > 8)
906 debug("coap_pdu_parse: invalid Token\n");
909 /* Finally calculate beginning of data block and thereby check integrity
910 * of the PDU structure. */
912 /* append data (including the Token) to pdu structure */
913 memcpy(((unsigned char *) pdu->hdr) + headerSize,
914 data + headerSize, length - headerSize);
916 /* skip header + token */
917 length -= (tokenLength + headerSize);
921 while (length && *opt != COAP_PAYLOAD_START)
923 coap_option_t option;
924 memset(&option, 0, sizeof(coap_option_t));
925 if (!next_option_safe(&opt, (size_t *) &length, &option))
927 debug("coap_pdu_parse: drop\n");
932 /* end of packet or start marker */
935 assert(*opt == COAP_PAYLOAD_START);
941 debug("coap_pdu_parse: message ending in payload start marker\n");
946 "set data to %p (pdu ends at %p)\n", (unsigned char *)opt,
947 (unsigned char *)pdu->hdr + pdu->length);
948 pdu->data = (unsigned char *) opt;
949 //printf("[COAP] pdu - data : %s\n", pdu->data);