Contiki-NG
Loading...
Searching...
No Matches
coap.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * This file is part of the Contiki operating system.
30 */
31
32/**
33 * \file
34 * An implementation of the Constrained Application Protocol (RFC).
35 * \author
36 * Matthias Kovatsch <kovatsch@inf.ethz.ch>
37 *
38 * Joakim Eriksson, joakim.eriksson@ri.se
39 * Niclas Finne, niclas.finne@ri.se
40 */
41
42/**
43 * \addtogroup coap
44 * @{
45 */
46
47
48#include <string.h>
49#include <stdlib.h>
50#include <inttypes.h>
51#include "sys/cc.h"
52#include "lib/random.h"
53
54#include "coap.h"
55#include "coap-transactions.h"
56
57/* Log configuration */
58#include "coap-log.h"
59#define LOG_MODULE "coap"
60#define LOG_LEVEL LOG_LEVEL_COAP
61
62/*---------------------------------------------------------------------------*/
63/*- Variables ---------------------------------------------------------------*/
64/*---------------------------------------------------------------------------*/
65static uint16_t current_mid = 0;
66
67coap_status_t coap_status_code = NO_ERROR;
68#if COAP_MESSAGE_ON_ERROR
69const char *coap_error_message = "";
70#endif
71/*---------------------------------------------------------------------------*/
72/*- Local helper functions --------------------------------------------------*/
73/*---------------------------------------------------------------------------*/
74static uint16_t
75coap_log_2(uint16_t value)
76{
77 uint16_t result = 0;
78
79 do {
80 value = value >> 1;
81 result++;
82 } while(value);
83
84 return result ? result - 1 : result;
85}
86/*---------------------------------------------------------------------------*/
87static uint32_t
88coap_parse_int_option(const uint8_t *bytes, size_t length)
89{
90 uint32_t var = 0;
91 int i = 0;
92
93 while(i < length) {
94 var <<= 8;
95 var |= bytes[i++];
96 }
97 return var;
98}
99/*---------------------------------------------------------------------------*/
100static uint8_t
101coap_option_nibble(unsigned int value)
102{
103 if(value < 13) {
104 return value;
105 } else if(value <= 0xFF + 13) {
106 return 13;
107 } else {
108 return 14;
109 }
110}
111/*---------------------------------------------------------------------------*/
112static size_t
113coap_set_option_header(unsigned int delta, size_t length, uint8_t *buffer)
114{
115 size_t written = 0;
116
117 buffer[0] = coap_option_nibble(delta) << 4 | coap_option_nibble(length);
118
119 if(delta > 268) {
120 buffer[++written] = ((delta - 269) >> 8) & 0xff;
121 buffer[++written] = (delta - 269) & 0xff;
122 } else if(delta > 12) {
123 buffer[++written] = (delta - 13);
124 }
125
126 if(length > 268) {
127 buffer[++written] = ((length - 269) >> 8) & 0xff;
128 buffer[++written] = (length - 269) & 0xff;
129 } else if(length > 12) {
130 buffer[++written] = (length - 13);
131 }
132
133 LOG_DBG("WRITTEN %zu B opt header\n", 1 + written);
134
135 return ++written;
136}
137/*---------------------------------------------------------------------------*/
138static size_t
139coap_serialize_int_option(unsigned int number, unsigned int current_number,
140 uint8_t *buffer, uint32_t value)
141{
142 size_t i = 0;
143
144 if(0xFF000000 & value) {
145 ++i;
146 }
147 if(0xFFFF0000 & value) {
148 ++i;
149 }
150 if(0xFFFFFF00 & value) {
151 ++i;
152 }
153 if(0xFFFFFFFF & value) {
154 ++i;
155 }
156 LOG_DBG("OPTION %u (delta %u, len %zu)\n", number, number - current_number,
157 i);
158
159 i = coap_set_option_header(number - current_number, i, buffer);
160
161 if(0xFF000000 & value) {
162 buffer[i++] = (uint8_t)(value >> 24);
163 }
164 if(0xFFFF0000 & value) {
165 buffer[i++] = (uint8_t)(value >> 16);
166 }
167 if(0xFFFFFF00 & value) {
168 buffer[i++] = (uint8_t)(value >> 8);
169 }
170 if(0xFFFFFFFF & value) {
171 buffer[i++] = (uint8_t)(value);
172 }
173 return i;
174}
175/*---------------------------------------------------------------------------*/
176static size_t
177coap_serialize_array_option(unsigned int number, unsigned int current_number,
178 uint8_t *buffer, uint8_t *array, size_t length,
179 char split_char)
180{
181 size_t i = 0;
182
183 LOG_DBG("ARRAY type %u, len %zu, full [", number, length);
184 LOG_DBG_COAP_STRING((const char *)array, length);
185 LOG_DBG_("]\n");
186
187 if(split_char != '\0') {
188 int j;
189 uint8_t *part_start = array;
190 uint8_t *part_end = NULL;
191 size_t temp_length;
192
193 for(j = 0; j <= length + 1; ++j) {
194 LOG_DBG("STEP %u/%zu (%c)\n", j, length, array[j]);
195 if(array[j] == split_char || j == length) {
196 part_end = array + j;
197 temp_length = part_end - part_start;
198
199 i += coap_set_option_header(number - current_number, temp_length,
200 &buffer[i]);
201 memcpy(&buffer[i], part_start, temp_length);
202 i += temp_length;
203
204 LOG_DBG("OPTION type %u, delta %u, len %zu, part [", number,
205 number - current_number, i);
206 LOG_DBG_COAP_STRING((const char *)part_start, temp_length);
207 LOG_DBG_("]\n");
208 ++j; /* skip the splitter */
209 current_number = number;
210 part_start = array + j;
211 }
212 } /* for */
213 } else {
214 i += coap_set_option_header(number - current_number, length, &buffer[i]);
215 memcpy(&buffer[i], array, length);
216 i += length;
217
218 LOG_DBG("OPTION type %u, delta %u, len %zu\n", number,
219 number - current_number, length);
220 }
221
222 return i;
223}
224/*---------------------------------------------------------------------------*/
225static void
226coap_merge_multi_option(char **dst, size_t *dst_len, uint8_t *option,
227 size_t option_len, char separator)
228{
229 /* merge multiple options */
230 if(*dst_len > 0) {
231 /* dst already contains an option: concatenate */
232 (*dst)[*dst_len] = separator;
233 *dst_len += 1;
234
235 /* memmove handles 2-byte option headers */
236 memmove((*dst) + (*dst_len), option, option_len);
237
238 *dst_len += option_len;
239 } else {
240 /* dst is empty: set to option */
241 *dst = (char *)option;
242 *dst_len = option_len;
243 }
244}
245/*---------------------------------------------------------------------------*/
246static int
247coap_get_variable(const char *buffer, size_t length, const char *name,
248 const char **output)
249{
250 const char *start = NULL;
251 const char *end = NULL;
252 const char *value_end = NULL;
253 size_t name_len = 0;
254
255 /*initialize the output buffer first */
256 *output = 0;
257
258 name_len = strlen(name);
259 end = buffer + length;
260
261 for(start = buffer; start + name_len < end; ++start) {
262 if((start == buffer || start[-1] == '&') && start[name_len] == '='
263 && strncmp(name, start, name_len) == 0) {
264
265 /* Point start to variable value */
266 start += name_len + 1;
267
268 /* Point end to the end of the value */
269 value_end = (const char *)memchr(start, '&', end - start);
270 if(value_end == NULL) {
271 value_end = end;
272 }
273 *output = start;
274
275 return value_end - start;
276 }
277 }
278 return 0;
279}
280/*---------------------------------------------------------------------------*/
281/*- Internal API ------------------------------------------------------------*/
282/*---------------------------------------------------------------------------*/
283void
284coap_init_connection(void)
285{
286 /* initialize transaction ID */
287 current_mid = random_rand();
288}
289/*---------------------------------------------------------------------------*/
290uint16_t
291coap_get_mid()
292{
293 return ++current_mid;
294}
295/*---------------------------------------------------------------------------*/
296void
297coap_init_message(coap_message_t *coap_pkt, coap_message_type_t type,
298 uint8_t code, uint16_t mid)
299{
300 /* Important thing */
301 memset(coap_pkt, 0, sizeof(coap_message_t));
302
303 coap_pkt->type = type;
304 coap_pkt->code = code;
305 coap_pkt->mid = mid;
306}
307/*---------------------------------------------------------------------------*/
308size_t
309coap_serialize_message(coap_message_t *coap_pkt, uint8_t *buffer)
310{
311 uint8_t *option;
312 unsigned int current_number = 0;
313
314 /* Initialize */
315 coap_pkt->buffer = buffer;
316 coap_pkt->version = 1;
317
318 LOG_DBG("-Serializing MID %u to %p, ", coap_pkt->mid, coap_pkt->buffer);
319
320 /* set header fields */
321 coap_pkt->buffer[0] = 0x00;
322 coap_pkt->buffer[0] |= COAP_HEADER_VERSION_MASK
323 & (coap_pkt->version) << COAP_HEADER_VERSION_POSITION;
324 coap_pkt->buffer[0] |= COAP_HEADER_TYPE_MASK
325 & (coap_pkt->type) << COAP_HEADER_TYPE_POSITION;
326 coap_pkt->buffer[0] |= COAP_HEADER_TOKEN_LEN_MASK
327 & (coap_pkt->token_len) << COAP_HEADER_TOKEN_LEN_POSITION;
328 coap_pkt->buffer[1] = coap_pkt->code;
329 coap_pkt->buffer[2] = (uint8_t)((coap_pkt->mid) >> 8);
330 coap_pkt->buffer[3] = (uint8_t)(coap_pkt->mid);
331
332 /* empty message, dont need to do more stuff */
333 if(!coap_pkt->code) {
334 LOG_DBG_("-Done serializing empty message at %p-\n", coap_pkt->buffer);
335 return 4;
336 }
337
338 /* set Token */
339 LOG_DBG_("Token (len %u)", coap_pkt->token_len);
340 option = coap_pkt->buffer + COAP_HEADER_LEN;
341 for(current_number = 0; current_number < coap_pkt->token_len;
342 ++current_number) {
343 LOG_DBG_(" %02X", coap_pkt->token[current_number]);
344 *option = coap_pkt->token[current_number];
345 ++option;
346 }
347 LOG_DBG_("-\n");
348
349 /* Serialize options */
350 current_number = 0;
351
352 LOG_DBG("-Serializing options at %p-\n", option);
353
354 /* The options must be serialized in the order of their number */
355 COAP_SERIALIZE_BYTE_OPTION(COAP_OPTION_IF_MATCH, if_match, "If-Match");
356 COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_URI_HOST, uri_host, '\0',
357 "Uri-Host");
358 COAP_SERIALIZE_BYTE_OPTION(COAP_OPTION_ETAG, etag, "ETag");
359 COAP_SERIALIZE_INT_OPTION(COAP_OPTION_IF_NONE_MATCH,
360 content_format -
361 coap_pkt->
362 content_format /* hack to get a zero field */,
363 "If-None-Match");
364 COAP_SERIALIZE_INT_OPTION(COAP_OPTION_OBSERVE, observe, "Observe");
365 COAP_SERIALIZE_INT_OPTION(COAP_OPTION_URI_PORT, uri_port, "Uri-Port");
366 COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_LOCATION_PATH, location_path, '/',
367 "Location-Path");
368 COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_URI_PATH, uri_path, '/',
369 "Uri-Path");
370 LOG_DBG("Serialize content format: %d\n", coap_pkt->content_format);
371 COAP_SERIALIZE_INT_OPTION(COAP_OPTION_CONTENT_FORMAT, content_format,
372 "Content-Format");
373 COAP_SERIALIZE_INT_OPTION(COAP_OPTION_MAX_AGE, max_age, "Max-Age");
374 COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_URI_QUERY, uri_query, '&',
375 "Uri-Query");
376 COAP_SERIALIZE_INT_OPTION(COAP_OPTION_ACCEPT, accept, "Accept");
377 COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_LOCATION_QUERY, location_query,
378 '&', "Location-Query");
379 COAP_SERIALIZE_BLOCK_OPTION(COAP_OPTION_BLOCK2, block2, "Block2");
380 COAP_SERIALIZE_BLOCK_OPTION(COAP_OPTION_BLOCK1, block1, "Block1");
381 COAP_SERIALIZE_INT_OPTION(COAP_OPTION_SIZE2, size2, "Size2");
382 COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_PROXY_URI, proxy_uri, '\0',
383 "Proxy-Uri");
384 COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_PROXY_SCHEME, proxy_scheme, '\0',
385 "Proxy-Scheme");
386 COAP_SERIALIZE_INT_OPTION(COAP_OPTION_SIZE1, size1, "Size1");
387
388 LOG_DBG("-Done serializing at %p----\n", option);
389
390 /* Pack payload */
391 if((option - coap_pkt->buffer) <= COAP_MAX_HEADER_SIZE) {
392 /* Payload marker */
393 if(coap_pkt->payload_len) {
394 *option = 0xFF;
395 ++option;
396 }
397 memmove(option, coap_pkt->payload, coap_pkt->payload_len);
398 } else {
399 /* an error occurred: caller must check for !=0 */
400 coap_pkt->buffer = NULL;
401#if COAP_MESSAGE_ON_ERROR
402 coap_error_message = "Serialized header exceeds COAP_MAX_HEADER_SIZE";
403#endif
404 return 0;
405 }
406
407 LOG_DBG("-Done %u B (header len %u, payload len %u)-\n",
408 (unsigned int)(coap_pkt->payload_len + option - buffer),
409 (unsigned int)(option - buffer),
410 (unsigned int)coap_pkt->payload_len);
411
412 LOG_DBG("Dump [0x%02X %02X %02X %02X %02X %02X %02X %02X]\n",
413 coap_pkt->buffer[0], coap_pkt->buffer[1], coap_pkt->buffer[2],
414 coap_pkt->buffer[3], coap_pkt->buffer[4], coap_pkt->buffer[5],
415 coap_pkt->buffer[6], coap_pkt->buffer[7]);
416
417 return (option - buffer) + coap_pkt->payload_len; /* message length */
418}
419/*---------------------------------------------------------------------------*/
420coap_status_t
421coap_parse_message(coap_message_t *coap_pkt, uint8_t *data, uint16_t data_len)
422{
423#define CHECK_OPTION_BOUNDARY(byte_count) \
424 do { \
425 if(current_option + (byte_count) > data + data_len) { \
426 LOG_WARN("BAD REQUEST: option delta outside buffer (%u > %u)\n", \
427 (unsigned)(current_option + (byte_count) - data), \
428 data_len); \
429 return BAD_REQUEST_4_00; \
430 } \
431 } while (0)
432
433 if(data_len < COAP_HEADER_LEN) {
434 /* Too short - malformed CoAP message */
435 LOG_WARN("BAD REQUEST: message too short\n");
436 return BAD_REQUEST_4_00;
437 }
438
439 /* initialize message */
440 memset(coap_pkt, 0, sizeof(coap_message_t));
441
442 /* pointer to message bytes */
443 coap_pkt->buffer = data;
444
445 /* parse header fields */
446 coap_pkt->version = (COAP_HEADER_VERSION_MASK & coap_pkt->buffer[0])
447 >> COAP_HEADER_VERSION_POSITION;
448 coap_pkt->type = (COAP_HEADER_TYPE_MASK & coap_pkt->buffer[0])
449 >> COAP_HEADER_TYPE_POSITION;
450 coap_pkt->token_len = (COAP_HEADER_TOKEN_LEN_MASK & coap_pkt->buffer[0])
451 >> COAP_HEADER_TOKEN_LEN_POSITION;
452 coap_pkt->code = coap_pkt->buffer[1];
453 coap_pkt->mid = coap_pkt->buffer[2] << 8 | coap_pkt->buffer[3];
454
455 if(coap_pkt->version != 1) {
456#if COAP_MESSAGE_ON_ERROR
457 coap_error_message = "CoAP version must be 1";
458#endif
459 return BAD_REQUEST_4_00;
460 }
461
462 if(coap_pkt->token_len > COAP_TOKEN_LEN) {
463#if COAP_MESSAGE_ON_ERROR
464 coap_error_message = "Token Length must not be more than 8";
465#endif
466 return BAD_REQUEST_4_00;
467 }
468
469 uint8_t *current_option = data + COAP_HEADER_LEN;
470 CHECK_OPTION_BOUNDARY(coap_pkt->token_len);
471
472 memcpy(coap_pkt->token, current_option, coap_pkt->token_len);
473 LOG_DBG("Token (len %u) [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n",
474 coap_pkt->token_len, coap_pkt->token[0], coap_pkt->token[1],
475 coap_pkt->token[2], coap_pkt->token[3], coap_pkt->token[4],
476 coap_pkt->token[5], coap_pkt->token[6], coap_pkt->token[7]
477 ); /* FIXME always prints 8 bytes */
478
479 /* parse options */
480 memset(coap_pkt->options, 0, sizeof(coap_pkt->options));
481 current_option += coap_pkt->token_len;
482
483 unsigned int option_number = 0;
484 unsigned int option_delta = 0;
485 size_t option_length = 0;
486
487 while(current_option < data + data_len) {
488 /* payload marker 0xFF, currently only checking for 0xF* because rest is reserved */
489 if((current_option[0] & 0xF0) == 0xF0) {
490 coap_pkt->payload = ++current_option;
491 coap_pkt->payload_len = data_len - (coap_pkt->payload - data);
492
493 /* also for receiving, the Erbium upper bound is COAP_MAX_CHUNK_SIZE */
494 if(coap_pkt->payload_len > COAP_MAX_CHUNK_SIZE) {
495 coap_pkt->payload_len = COAP_MAX_CHUNK_SIZE;
496 /* null-terminate payload */
497 }
498 coap_pkt->payload[coap_pkt->payload_len] = '\0';
499
500 break;
501 }
502
503 option_delta = current_option[0] >> 4;
504 option_length = current_option[0] & 0x0F;
505 ++current_option;
506
507 if(option_delta == 13) {
508 CHECK_OPTION_BOUNDARY(1);
509 option_delta += current_option[0];
510 ++current_option;
511 } else if(option_delta == 14) {
512 CHECK_OPTION_BOUNDARY(2);
513 option_delta += 255;
514 option_delta += current_option[0] << 8;
515 ++current_option;
516 option_delta += current_option[0];
517 ++current_option;
518 }
519
520 if(option_length == 13) {
521 CHECK_OPTION_BOUNDARY(1);
522 option_length += current_option[0];
523 ++current_option;
524 } else if(option_length == 14) {
525 CHECK_OPTION_BOUNDARY(2);
526 option_length += 255;
527 option_length += current_option[0] << 8;
528 ++current_option;
529 option_length += current_option[0];
530 ++current_option;
531 }
532
533 CHECK_OPTION_BOUNDARY(option_length);
534 option_number += option_delta;
535
536 if(option_number > COAP_OPTION_SIZE1) {
537 /* Malformed CoAP - out of bounds */
538 LOG_WARN("BAD REQUEST: option number too large: %u\n", option_number);
539 return BAD_REQUEST_4_00;
540 }
541
542 LOG_DBG("OPTION %u (delta %u, len %zu): ", option_number, option_delta,
543 option_length);
544
545 coap_set_option(coap_pkt, option_number);
546
547 switch(option_number) {
548 case COAP_OPTION_CONTENT_FORMAT:
549 coap_pkt->content_format = coap_parse_int_option(current_option,
550 option_length);
551 LOG_DBG_("Content-Format [%u]\n", coap_pkt->content_format);
552 break;
553 case COAP_OPTION_MAX_AGE:
554 coap_pkt->max_age = coap_parse_int_option(current_option,
555 option_length);
556 LOG_DBG_("Max-Age [%"PRIu32"]\n", coap_pkt->max_age);
557 break;
558 case COAP_OPTION_ETAG:
559 coap_pkt->etag_len = MIN(COAP_ETAG_LEN, option_length);
560 memcpy(coap_pkt->etag, current_option, coap_pkt->etag_len);
561 LOG_DBG_("ETag %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n",
562 coap_pkt->etag_len, coap_pkt->etag[0], coap_pkt->etag[1],
563 coap_pkt->etag[2], coap_pkt->etag[3], coap_pkt->etag[4],
564 coap_pkt->etag[5], coap_pkt->etag[6], coap_pkt->etag[7]
565 ); /*FIXME always prints 8 bytes */
566 break;
567 case COAP_OPTION_ACCEPT:
568 coap_pkt->accept = coap_parse_int_option(current_option, option_length);
569 LOG_DBG_("Accept [%u]\n", coap_pkt->accept);
570 break;
571 case COAP_OPTION_IF_MATCH:
572 /* TODO support multiple ETags */
573 coap_pkt->if_match_len = MIN(COAP_ETAG_LEN, option_length);
574 memcpy(coap_pkt->if_match, current_option, coap_pkt->if_match_len);
575 LOG_DBG_("If-Match %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n",
576 coap_pkt->if_match_len, coap_pkt->if_match[0],
577 coap_pkt->if_match[1], coap_pkt->if_match[2],
578 coap_pkt->if_match[3], coap_pkt->if_match[4],
579 coap_pkt->if_match[5], coap_pkt->if_match[6],
580 coap_pkt->if_match[7]
581 ); /* FIXME always prints 8 bytes */
582 break;
583 case COAP_OPTION_IF_NONE_MATCH:
584 coap_pkt->if_none_match = 1;
585 LOG_DBG_("If-None-Match\n");
586 break;
587
588 case COAP_OPTION_PROXY_URI:
589#if COAP_PROXY_OPTION_PROCESSING
590 coap_pkt->proxy_uri = (char *)current_option;
591 coap_pkt->proxy_uri_len = option_length;
592 LOG_DBG_("Proxy-Uri NOT IMPLEMENTED [");
593 LOG_DBG_COAP_STRING(coap_pkt->proxy_uri, coap_pkt->proxy_uri_len);
594 LOG_DBG_("]\n");
595#endif /* COAP_PROXY_OPTION_PROCESSING */
596#if COAP_MESSAGE_ON_ERROR
597 coap_error_message = "This is a constrained server (Contiki-NG)";
598#endif
599 return PROXYING_NOT_SUPPORTED_5_05;
600 break;
601 case COAP_OPTION_PROXY_SCHEME:
602#if COAP_PROXY_OPTION_PROCESSING
603 coap_pkt->proxy_scheme = (char *)current_option;
604 coap_pkt->proxy_scheme_len = option_length;
605 LOG_DBG_("Proxy-Scheme NOT IMPLEMENTED [");
606 LOG_DBG_COAP_STRING(coap_pkt->proxy_scheme, coap_pkt->proxy_scheme_len);
607 LOG_DBG_("]\n");
608#endif
609#if COAP_MESSAGE_ON_ERROR
610 coap_error_message = "This is a constrained server (Contiki-NG)";
611#endif
612 return PROXYING_NOT_SUPPORTED_5_05;
613 break;
614
615 case COAP_OPTION_URI_HOST:
616 coap_pkt->uri_host = (char *)current_option;
617 coap_pkt->uri_host_len = option_length;
618 LOG_DBG_("Uri-Host [");
619 LOG_DBG_COAP_STRING(coap_pkt->uri_host, coap_pkt->uri_host_len);
620 LOG_DBG_("]\n");
621 break;
622 case COAP_OPTION_URI_PORT:
623 coap_pkt->uri_port = coap_parse_int_option(current_option,
624 option_length);
625 LOG_DBG_("Uri-Port [%u]\n", coap_pkt->uri_port);
626 break;
627 case COAP_OPTION_URI_PATH:
628 /* coap_merge_multi_option() operates in-place on the IPBUF, but final message field should be const string -> cast to string */
629 coap_merge_multi_option((char **)&(coap_pkt->uri_path),
630 &(coap_pkt->uri_path_len), current_option,
631 option_length, '/');
632 LOG_DBG_("Uri-Path [");
633 LOG_DBG_COAP_STRING(coap_pkt->uri_path, coap_pkt->uri_path_len);
634 LOG_DBG_("]\n");
635 break;
636 case COAP_OPTION_URI_QUERY:
637 /* coap_merge_multi_option() operates in-place on the IPBUF, but final message field should be const string -> cast to string */
638 coap_merge_multi_option((char **)&(coap_pkt->uri_query),
639 &(coap_pkt->uri_query_len), current_option,
640 option_length, '&');
641 LOG_DBG_("Uri-Query[");
642 LOG_DBG_COAP_STRING(coap_pkt->uri_query, coap_pkt->uri_query_len);
643 LOG_DBG_("]\n");
644 break;
645
646 case COAP_OPTION_LOCATION_PATH:
647 /* coap_merge_multi_option() operates in-place on the IPBUF, but final message field should be const string -> cast to string */
648 coap_merge_multi_option((char **)&(coap_pkt->location_path),
649 &(coap_pkt->location_path_len), current_option,
650 option_length, '/');
651
652 LOG_DBG_("Location-Path [");
653 LOG_DBG_COAP_STRING(coap_pkt->location_path, coap_pkt->location_path_len);
654 LOG_DBG_("]\n");
655 break;
656 case COAP_OPTION_LOCATION_QUERY:
657 /* coap_merge_multi_option() operates in-place on the IPBUF, but final message field should be const string -> cast to string */
658 coap_merge_multi_option((char **)&(coap_pkt->location_query),
659 &(coap_pkt->location_query_len), current_option,
660 option_length, '&');
661 LOG_DBG_("Location-Query [");
662 LOG_DBG_COAP_STRING(coap_pkt->location_query, coap_pkt->location_query_len);
663 LOG_DBG_("]\n");
664 break;
665
666 case COAP_OPTION_OBSERVE:
667 coap_pkt->observe = coap_parse_int_option(current_option,
668 option_length);
669 LOG_DBG_("Observe [%"PRId32"]\n", coap_pkt->observe);
670 break;
671 case COAP_OPTION_BLOCK2:
672 coap_pkt->block2_num = coap_parse_int_option(current_option,
673 option_length);
674 coap_pkt->block2_more = (coap_pkt->block2_num & 0x08) >> 3;
675 coap_pkt->block2_size = 16 << (coap_pkt->block2_num & 0x07);
676 coap_pkt->block2_offset = (coap_pkt->block2_num & ~0x0000000F)
677 << (coap_pkt->block2_num & 0x07);
678 coap_pkt->block2_num >>= 4;
679 LOG_DBG_("Block2 [%lu%s (%u B/blk)]\n",
680 (unsigned long)coap_pkt->block2_num,
681 coap_pkt->block2_more ? "+" : "", coap_pkt->block2_size);
682 break;
683 case COAP_OPTION_BLOCK1:
684 coap_pkt->block1_num = coap_parse_int_option(current_option,
685 option_length);
686 coap_pkt->block1_more = (coap_pkt->block1_num & 0x08) >> 3;
687 coap_pkt->block1_size = 16 << (coap_pkt->block1_num & 0x07);
688 coap_pkt->block1_offset = (coap_pkt->block1_num & ~0x0000000F)
689 << (coap_pkt->block1_num & 0x07);
690 coap_pkt->block1_num >>= 4;
691 LOG_DBG_("Block1 [%lu%s (%u B/blk)]\n",
692 (unsigned long)coap_pkt->block1_num,
693 coap_pkt->block1_more ? "+" : "", coap_pkt->block1_size);
694 break;
695 case COAP_OPTION_SIZE2:
696 coap_pkt->size2 = coap_parse_int_option(current_option, option_length);
697 LOG_DBG_("Size2 [%"PRIu32"]\n", coap_pkt->size2);
698 break;
699 case COAP_OPTION_SIZE1:
700 coap_pkt->size1 = coap_parse_int_option(current_option, option_length);
701 LOG_DBG_("Size1 [%"PRIu32"]\n", coap_pkt->size1);
702 break;
703 default:
704 LOG_DBG_("unknown (%u)\n", option_number);
705 /* check if critical (odd) */
706 if(option_number & 1) {
707#if COAP_MESSAGE_ON_ERROR
708 coap_error_message = "Unsupported critical option";
709#endif
710 return BAD_OPTION_4_02;
711 }
712 }
713
714 current_option += option_length;
715 } /* for */
716 LOG_DBG("-Done parsing-------\n");
717
718 return NO_ERROR;
719}
720/*---------------------------------------------------------------------------*/
721/*- CoAP Engine API ---------------------------------------------------------*/
722/*---------------------------------------------------------------------------*/
723int
724coap_get_query_variable(const coap_message_t *coap_pkt,
725 const char *name, const char **output)
726{
727 if(coap_is_option(coap_pkt, COAP_OPTION_URI_QUERY)) {
728 return coap_get_variable(coap_pkt->uri_query, coap_pkt->uri_query_len,
729 name, output);
730 }
731 return 0;
732}
733int
734coap_get_post_variable(const coap_message_t *coap_pkt,
735 const char *name, const char **output)
736{
737 if(coap_pkt->payload_len) {
738 return coap_get_variable((const char *)coap_pkt->payload,
739 coap_pkt->payload_len, name, output);
740 }
741 return 0;
742}
743/*---------------------------------------------------------------------------*/
744int
745coap_set_status_code(coap_message_t *message, unsigned int code)
746{
747 if(code <= 0xFF) {
748 message->code = (uint8_t)code;
749 return 1;
750 } else {
751 return 0;
752 }
753}
754/*---------------------------------------------------------------------------*/
755int
756coap_set_token(coap_message_t *coap_pkt, const uint8_t *token, size_t token_len)
757{
758 coap_pkt->token_len = MIN(COAP_TOKEN_LEN, token_len);
759 memcpy(coap_pkt->token, token, coap_pkt->token_len);
760
761 return coap_pkt->token_len;
762}
763/*---------------------------------------------------------------------------*/
764/*- CoAP Implementation API -------------------------------------------------*/
765/*---------------------------------------------------------------------------*/
766int
767coap_get_header_content_format(const coap_message_t *coap_pkt,
768 unsigned int *format)
769{
770 if(!coap_is_option(coap_pkt, COAP_OPTION_CONTENT_FORMAT)) {
771 return 0;
772 }
773 *format = coap_pkt->content_format;
774 return 1;
775}
776int
777coap_set_header_content_format(coap_message_t *coap_pkt, unsigned int format)
778{
779 coap_pkt->content_format = format;
780 coap_set_option(coap_pkt, COAP_OPTION_CONTENT_FORMAT);
781 return 1;
782}
783/*---------------------------------------------------------------------------*/
784int
785coap_get_header_accept(const coap_message_t *coap_pkt, unsigned int *accept)
786{
787 if(!coap_is_option(coap_pkt, COAP_OPTION_ACCEPT)) {
788 return 0;
789 }
790 *accept = coap_pkt->accept;
791 return 1;
792}
793int
794coap_set_header_accept(coap_message_t *coap_pkt, unsigned int accept)
795{
796 coap_pkt->accept = accept;
797 coap_set_option(coap_pkt, COAP_OPTION_ACCEPT);
798 return 1;
799}
800/*---------------------------------------------------------------------------*/
801int
802coap_get_header_max_age(const coap_message_t *coap_pkt, uint32_t *age)
803{
804 if(!coap_is_option(coap_pkt, COAP_OPTION_MAX_AGE)) {
805 *age = COAP_DEFAULT_MAX_AGE;
806 } else {
807 *age = coap_pkt->max_age;
808 } return 1;
809}
810int
811coap_set_header_max_age(coap_message_t *coap_pkt, uint32_t age)
812{
813 coap_pkt->max_age = age;
814 coap_set_option(coap_pkt, COAP_OPTION_MAX_AGE);
815 return 1;
816}
817/*---------------------------------------------------------------------------*/
818int
819coap_get_header_etag(const coap_message_t *coap_pkt, const uint8_t **etag)
820{
821 if(!coap_is_option(coap_pkt, COAP_OPTION_ETAG)) {
822 return 0;
823 }
824 *etag = coap_pkt->etag;
825 return coap_pkt->etag_len;
826}
827int
828coap_set_header_etag(coap_message_t *coap_pkt, const uint8_t *etag, size_t etag_len)
829{
830 coap_pkt->etag_len = MIN(COAP_ETAG_LEN, etag_len);
831 memcpy(coap_pkt->etag, etag, coap_pkt->etag_len);
832
833 coap_set_option(coap_pkt, COAP_OPTION_ETAG);
834 return coap_pkt->etag_len;
835}
836/*---------------------------------------------------------------------------*/
837/*FIXME support multiple ETags */
838int
839coap_get_header_if_match(const coap_message_t *coap_pkt, const uint8_t **etag)
840{
841 if(!coap_is_option(coap_pkt, COAP_OPTION_IF_MATCH)) {
842 return 0;
843 }
844 *etag = coap_pkt->if_match;
845 return coap_pkt->if_match_len;
846}
847int
848coap_set_header_if_match(coap_message_t *coap_pkt, const uint8_t *etag, size_t etag_len)
849{
850 coap_pkt->if_match_len = MIN(COAP_ETAG_LEN, etag_len);
851 memcpy(coap_pkt->if_match, etag, coap_pkt->if_match_len);
852
853 coap_set_option(coap_pkt, COAP_OPTION_IF_MATCH);
854 return coap_pkt->if_match_len;
855}
856/*---------------------------------------------------------------------------*/
857int
858coap_get_header_if_none_match(const coap_message_t *message)
859{
860 return coap_is_option(message, COAP_OPTION_IF_NONE_MATCH) ? 1 : 0;
861}
862int
863coap_set_header_if_none_match(coap_message_t *message)
864{
865 coap_set_option(message, COAP_OPTION_IF_NONE_MATCH);
866 return 1;
867}
868/*---------------------------------------------------------------------------*/
869int
870coap_get_header_proxy_uri(const coap_message_t *coap_pkt, const char **uri)
871{
872 if(!coap_is_option(coap_pkt, COAP_OPTION_PROXY_URI)) {
873 return 0;
874 }
875 *uri = coap_pkt->proxy_uri;
876 return coap_pkt->proxy_uri_len;
877}
878int
879coap_set_header_proxy_uri(coap_message_t *coap_pkt, const char *uri)
880{
881 /* TODO Provide alternative that sets Proxy-Scheme and Uri-* options and provide coap-conf define */
882
883 coap_pkt->proxy_uri = uri;
884 coap_pkt->proxy_uri_len = strlen(uri);
885
886 coap_set_option(coap_pkt, COAP_OPTION_PROXY_URI);
887 return coap_pkt->proxy_uri_len;
888}
889/*---------------------------------------------------------------------------*/
890int
891coap_get_header_uri_host(const coap_message_t *coap_pkt, const char **host)
892{
893 if(!coap_is_option(coap_pkt, COAP_OPTION_URI_HOST)) {
894 return 0;
895 }
896 *host = coap_pkt->uri_host;
897 return coap_pkt->uri_host_len;
898}
899int
900coap_set_header_uri_host(coap_message_t *coap_pkt, const char *host)
901{
902 coap_pkt->uri_host = host;
903 coap_pkt->uri_host_len = strlen(host);
904
905 coap_set_option(coap_pkt, COAP_OPTION_URI_HOST);
906 return coap_pkt->uri_host_len;
907}
908/*---------------------------------------------------------------------------*/
909int
910coap_get_header_uri_path(const coap_message_t *coap_pkt, const char **path)
911{
912 if(!coap_is_option(coap_pkt, COAP_OPTION_URI_PATH)) {
913 return 0;
914 }
915 *path = coap_pkt->uri_path;
916 return coap_pkt->uri_path_len;
917}
918int
919coap_set_header_uri_path(coap_message_t *coap_pkt, const char *path)
920{
921 while(path[0] == '/') {
922 ++path;
923 }
924
925 coap_pkt->uri_path = path;
926 coap_pkt->uri_path_len = strlen(path);
927
928 coap_set_option(coap_pkt, COAP_OPTION_URI_PATH);
929 return coap_pkt->uri_path_len;
930}
931/*---------------------------------------------------------------------------*/
932int
933coap_get_header_uri_query(const coap_message_t *coap_pkt,
934 const char **query)
935{
936 if(!coap_is_option(coap_pkt, COAP_OPTION_URI_QUERY)) {
937 return 0;
938 }
939 *query = coap_pkt->uri_query;
940 return coap_pkt->uri_query_len;
941}
942int
943coap_set_header_uri_query(coap_message_t *coap_pkt, const char *query)
944{
945 while(query[0] == '?') {
946 ++query;
947 }
948
949 coap_pkt->uri_query = query;
950 coap_pkt->uri_query_len = strlen(query);
951
952 coap_set_option(coap_pkt, COAP_OPTION_URI_QUERY);
953 return coap_pkt->uri_query_len;
954}
955/*---------------------------------------------------------------------------*/
956int
957coap_get_header_location_path(const coap_message_t *coap_pkt,
958 const char **path)
959{
960 if(!coap_is_option(coap_pkt, COAP_OPTION_LOCATION_PATH)) {
961 return 0;
962 }
963 *path = coap_pkt->location_path;
964 return coap_pkt->location_path_len;
965}
966int
967coap_set_header_location_path(coap_message_t *coap_pkt, const char *path)
968{
969 char *query;
970
971 while(path[0] == '/') {
972 ++path;
973 }
974
975 if((query = strchr(path, '?'))) {
976 coap_set_header_location_query(coap_pkt, query + 1);
977 coap_pkt->location_path_len = query - path;
978 } else {
979 coap_pkt->location_path_len = strlen(path);
980 } coap_pkt->location_path = path;
981
982 if(coap_pkt->location_path_len > 0) {
983 coap_set_option(coap_pkt, COAP_OPTION_LOCATION_PATH);
984 }
985 return coap_pkt->location_path_len;
986}
987/*---------------------------------------------------------------------------*/
988int
989coap_get_header_location_query(const coap_message_t *coap_pkt,
990 const char **query)
991{
992 if(!coap_is_option(coap_pkt, COAP_OPTION_LOCATION_QUERY)) {
993 return 0;
994 }
995 *query = coap_pkt->location_query;
996 return coap_pkt->location_query_len;
997}
998int
999coap_set_header_location_query(coap_message_t *coap_pkt, const char *query)
1000{
1001 while(query[0] == '?') {
1002 ++query;
1003 }
1004
1005 coap_pkt->location_query = query;
1006 coap_pkt->location_query_len = strlen(query);
1007
1008 coap_set_option(coap_pkt, COAP_OPTION_LOCATION_QUERY);
1009 return coap_pkt->location_query_len;
1010}
1011/*---------------------------------------------------------------------------*/
1012int
1013coap_get_header_observe(const coap_message_t *coap_pkt, uint32_t *observe)
1014{
1015 if(!coap_is_option(coap_pkt, COAP_OPTION_OBSERVE)) {
1016 return 0;
1017 }
1018 *observe = coap_pkt->observe;
1019 return 1;
1020}
1021int
1022coap_set_header_observe(coap_message_t *coap_pkt, uint32_t observe)
1023{
1024 coap_pkt->observe = observe;
1025 coap_set_option(coap_pkt, COAP_OPTION_OBSERVE);
1026 return 1;
1027}
1028/*---------------------------------------------------------------------------*/
1029int
1030coap_get_header_block2(const coap_message_t *coap_pkt, uint32_t *num,
1031 uint8_t *more, uint16_t *size, uint32_t *offset)
1032{
1033 if(!coap_is_option(coap_pkt, COAP_OPTION_BLOCK2)) {
1034 return 0;
1035 }
1036 /* pointers may be NULL to get only specific block parameters */
1037 if(num != NULL) {
1038 *num = coap_pkt->block2_num;
1039 }
1040 if(more != NULL) {
1041 *more = coap_pkt->block2_more;
1042 }
1043 if(size != NULL) {
1044 *size = coap_pkt->block2_size;
1045 }
1046 if(offset != NULL) {
1047 *offset = coap_pkt->block2_offset;
1048 }
1049 return 1;
1050}
1051int
1052coap_set_header_block2(coap_message_t *coap_pkt, uint32_t num, uint8_t more,
1053 uint16_t size)
1054{
1055 if(size < 16) {
1056 return 0;
1057 }
1058 if(size > 2048) {
1059 return 0;
1060 }
1061 if(num > 0x0FFFFF) {
1062 return 0;
1063 }
1064 coap_pkt->block2_num = num;
1065 coap_pkt->block2_more = more ? 1 : 0;
1066 coap_pkt->block2_size = size;
1067
1068 coap_set_option(coap_pkt, COAP_OPTION_BLOCK2);
1069 return 1;
1070}
1071/*---------------------------------------------------------------------------*/
1072int
1073coap_get_header_block1(const coap_message_t *coap_pkt, uint32_t *num,
1074 uint8_t *more, uint16_t *size, uint32_t *offset)
1075{
1076 if(!coap_is_option(coap_pkt, COAP_OPTION_BLOCK1)) {
1077 return 0;
1078 }
1079 /* pointers may be NULL to get only specific block parameters */
1080 if(num != NULL) {
1081 *num = coap_pkt->block1_num;
1082 }
1083 if(more != NULL) {
1084 *more = coap_pkt->block1_more;
1085 }
1086 if(size != NULL) {
1087 *size = coap_pkt->block1_size;
1088 }
1089 if(offset != NULL) {
1090 *offset = coap_pkt->block1_offset;
1091 }
1092 return 1;
1093}
1094int
1095coap_set_header_block1(coap_message_t *coap_pkt, uint32_t num, uint8_t more,
1096 uint16_t size)
1097{
1098 if(size < 16) {
1099 return 0;
1100 }
1101 if(size > 2048) {
1102 return 0;
1103 }
1104 if(num > 0x0FFFFF) {
1105 return 0;
1106 }
1107 coap_pkt->block1_num = num;
1108 coap_pkt->block1_more = more;
1109 coap_pkt->block1_size = size;
1110
1111 coap_set_option(coap_pkt, COAP_OPTION_BLOCK1);
1112 return 1;
1113}
1114/*---------------------------------------------------------------------------*/
1115int
1116coap_get_header_size2(const coap_message_t *coap_pkt, uint32_t *size)
1117{
1118 if(!coap_is_option(coap_pkt, COAP_OPTION_SIZE2)) {
1119 return 0;
1120 }
1121 *size = coap_pkt->size2;
1122 return 1;
1123}
1124int
1125coap_set_header_size2(coap_message_t *coap_pkt, uint32_t size)
1126{
1127 coap_pkt->size2 = size;
1128 coap_set_option(coap_pkt, COAP_OPTION_SIZE2);
1129 return 1;
1130}
1131/*---------------------------------------------------------------------------*/
1132int
1133coap_get_header_size1(const coap_message_t *coap_pkt, uint32_t *size)
1134{
1135 if(!coap_is_option(coap_pkt, COAP_OPTION_SIZE1)) {
1136 return 0;
1137 }
1138 *size = coap_pkt->size1;
1139 return 1;
1140}
1141int
1142coap_set_header_size1(coap_message_t *coap_pkt, uint32_t size)
1143{
1144 coap_pkt->size1 = size;
1145 coap_set_option(coap_pkt, COAP_OPTION_SIZE1);
1146 return 1;
1147}
1148/*---------------------------------------------------------------------------*/
1149int
1150coap_get_payload(const coap_message_t *coap_pkt, const uint8_t **payload)
1151{
1152 if(payload != NULL) {
1153 *payload = coap_pkt->payload;
1154 }
1155 return coap_pkt->payload != NULL ? coap_pkt->payload_len : 0;
1156}
1157int
1158coap_set_payload(coap_message_t *coap_pkt, const void *payload, size_t length)
1159{
1160 coap_pkt->payload = (uint8_t *)payload;
1161 coap_pkt->payload_len = MIN(COAP_MAX_CHUNK_SIZE, length);
1162
1163 return coap_pkt->payload_len;
1164}
1165/*---------------------------------------------------------------------------*/
1166/** @} */
Default definitions of C compiler quirk work-arounds.
Log support for CoAP.
CoAP module for reliable transport.
An implementation of the Constrained Application Protocol (RFC 7252).
unsigned short random_rand(void)
Generates a new random number using the cc2538 RNG.
Definition random.c:58
static uint8_t accept(uint8_t in)
Definition mpl.c:1390
static void start(void)
Start measurement.