9 #include "coap_config.h" 11 #if defined(HAVE_ASSERT_H) && !defined(assert) 36 static inline unsigned char *
37 strnchr(
unsigned char *s,
size_t len,
unsigned char c) {
38 while (len && *s++ != c)
41 return len ? s : NULL;
44 #define ISEQUAL_CI(a,b) \ 45 ((a) == (b) || (islower(b) && ((a) == ((b) - 0x20)))) 49 const unsigned char *p, *q;
50 int secure = 0, res = 0;
78 if (len && (secure = *p ==
's')) {
82 q = (
unsigned char *)
"://";
83 while (len && *q && *p == *q) {
94 if (len && *p ==
'[') {
97 while (len && *q !=
']') {
101 if (!len || *q !=
']' || p == q) {
109 while (len && *q !=
':' && *q !=
'/' && *q !=
'?') {
123 if (len && *q ==
':') {
127 while (len && isdigit(*q)) {
136 uri_port = uri_port * 10 + (*p++ -
'0');
139 if (uri_port > 65535) {
144 uri->
port = uri_port;
157 while (len && *q !=
'?') {
169 if (len && *p ==
'?') {
190 #define hexchar_to_dec(c) ((c) & 0x40 ? ((c) & 0x0F) + 9 : ((c) & 0x0F)) 212 seg += 2; length -= 2;
233 if (length < 2 || !(isxdigit(s[1]) && isxdigit(s[2])))
267 unsigned char *buf,
size_t buflen) {
272 debug(
"make_decoded_option(): buflen is 0!\n");
283 assert(written <= buflen);
291 if (buflen < (
size_t)res) {
292 debug(
"buffer too small for option\n");
298 return written + res;
303 #define min(a,b) ((a) < (b) ? (a) : (b)) 312 dots(
unsigned char *s,
size_t len) {
313 return *s ==
'.' && (len == 1 || (*(s+1) ==
'.' && len == 2));
331 const unsigned char *p, *q;
334 while (length > 0 && !
strnchr((
unsigned char *)
"?#", 2, *q)) {
337 if (!
dots((
unsigned char *)p, q - p)) {
338 h((
unsigned char *)p, q - p, data);
349 if (!
dots((
unsigned char *)p, q - p)) {
350 h((
unsigned char *)p, q - p, data);
377 unsigned char *
buf,
size_t *buflen) {
378 struct cnt_str tmp = { { *buflen, buf }, 0 };
389 unsigned char *
buf,
size_t *buflen) {
390 struct cnt_str tmp = { { *buflen, buf }, 0 };
391 const unsigned char *p;
394 while (length > 0 && *s !=
'#') {
411 #define URI_DATA(uriobj) ((unsigned char *)(uriobj) + sizeof(coap_uri_t)) 415 unsigned char *result;
422 memcpy(
URI_DATA(result), uri, length);
void(* segment_handler_t)(unsigned char *, size_t, void *)
Representation of parsed URI.
str query
The query part if present.
static void decode_segment(const unsigned char *seg, size_t length, unsigned char *buf)
Decodes percent-encoded characters while copying the string seg of size length to buf...
unsigned char coap_key_t[4]
static void write_option(unsigned char *s, size_t len, void *data)
static int check_segment(const unsigned char *s, size_t length)
Runs through the given path (or query) segment and checks if percent-encodings are correct...
static void * coap_malloc(size_t size)
Wrapper function to coap_malloc_type() for backwards compatibility.
str host
host part of the URI
str path
Beginning of the first path segment.
Helpers for handling options in CoAP PDUs.
int coap_split_uri(const unsigned char *str_var, size_t len, coap_uri_t *uri)
Parses a given string into URI components.
unsigned short port
The port in host byte order.
#define COAP_SET_STR(st, l, v)
static unsigned char * strnchr(unsigned char *s, size_t len, unsigned char c)
A length-safe version of strchr().
static void hash_segment(unsigned char *s, size_t len, void *data)
coap_uri_t * coap_new_uri(const unsigned char *uri, unsigned int length)
Creates a new coap_uri_t object from the specified URI.
static size_t coap_split_path_impl(const unsigned char *s, size_t length, segment_handler_t h, void *data)
Splits the given string into segments.
int coap_split_path(const unsigned char *s, size_t length, unsigned char *buf, size_t *buflen)
Splits the given URI path into segments.
#define coap_hash(String, Length, Result)
#define COAP_DEFAULT_SCHEME
#define COAP_DEFAULT_PORT
Pre-defined constants that reflect defaults for CoAP.
static int dots(unsigned char *s, size_t len)
Checks if path segment s consists of one or two dots.
int coap_hash_path(const unsigned char *path, size_t len, coap_key_t key)
Calculates a hash over the given path and stores the result in key.
coap_uri_t * coap_clone_uri(const coap_uri_t *uri)
Clones the specified coap_uri_t object.
int coap_split_query(const unsigned char *s, size_t length, unsigned char *buf, size_t *buflen)
Splits the given URI query into segments.
static void coap_free(void *object)
Wrapper function to coap_free_type() for backwards compatibility.
size_t coap_opt_setheader(coap_opt_t *opt, size_t maxlen, unsigned short delta, size_t length)
Encodes the given delta and length values into opt.
static int make_decoded_option(const unsigned char *s, size_t length, unsigned char *buf, size_t buflen)
Writes a coap option from given string s to buf.
#define hexchar_to_dec(c)
Calculates decimal value from hexadecimal ASCII character given in c.