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;
47 int secure = 0, res = 0;
63 while (len && *q && tolower(*p) == *q) {
75 if (len && (secure = tolower(*p) ==
's')) {
79 q = (
unsigned char *)
"://";
80 while (len && *q && tolower(*p) == *q) {
91 if (len && *p ==
'[') {
94 while (len && *q !=
']') {
98 if (!len || *q !=
']' || p == q) {
106 while (len && *q !=
':' && *q !=
'/' && *q !=
'?') {
121 if (len && *q ==
':') {
125 while (len && isdigit(*q)) {
134 uri_port = uri_port * 10 + (*p++ -
'0');
137 if (uri_port > 65535) {
142 uri->
port = uri_port;
155 while (len && *q !=
'?') {
167 if (len && *p ==
'?') {
188 #define hexchar_to_dec(c) ((c) & 0x40 ? ((c) & 0x0F) + 9 : ((c) & 0x0F)) 210 seg += 2; length -= 2;
231 if (length < 2 || !(isxdigit(s[1]) && isxdigit(s[2])))
265 unsigned char *buf,
size_t buflen) {
270 debug(
"make_decoded_option(): buflen is 0!\n");
281 assert(written <= buflen);
289 if (buflen < (
size_t)res) {
290 debug(
"buffer too small for option\n");
296 return written + res;
301 #define min(a,b) ((a) < (b) ? (a) : (b)) 310 dots(
unsigned char *s,
size_t len) {
311 return *s ==
'.' && (len == 1 || (*(s+1) ==
'.' && len == 2));
329 const unsigned char *p, *q;
332 while (length > 0 && !
strnchr((
unsigned char *)
"?#", 2, *q)) {
335 if (!
dots((
unsigned char *)p, q - p)) {
336 h((
unsigned char *)p, q - p, data);
347 if (!
dots((
unsigned char *)p, q - p)) {
348 h((
unsigned char *)p, q - p, data);
375 unsigned char *
buf,
size_t *buflen) {
376 struct cnt_str tmp = { { *buflen, buf }, 0 };
387 unsigned char *
buf,
size_t *buflen) {
388 struct cnt_str tmp = { { *buflen, buf }, 0 };
389 const unsigned char *p;
392 while (length > 0 && *s !=
'#') {
409 #define URI_DATA(uriobj) ((unsigned char *)(uriobj) + sizeof(coap_uri_t)) 413 unsigned char *result;
420 memcpy(
URI_DATA(result), uri, length);
void(* segment_handler_t)(unsigned char *, size_t, void *)
Representation of parsed URI.
int coap_split_uri(unsigned char *str_var, size_t len, coap_uri_t *uri)
Parses a given string into URI components.
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.
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.