From 3a127a381671d9b100b99c89d35fd9e63add4ea4 Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Tue, 8 Mar 2016 12:57:06 +0100 Subject: [PATCH] uri.[hc]: improved support for 'coaps' URI scheme --- include/coap/uri.h | 20 ++++++++++++++++++++ src/uri.c | 4 ++++ 2 files changed, 24 insertions(+) diff --git a/include/coap/uri.h b/include/coap/uri.h index c5fc2566..79b410b8 100644 --- a/include/coap/uri.h +++ b/include/coap/uri.h @@ -13,6 +13,18 @@ #include "hashkey.h" #include "str.h" +/** + * The scheme specifiers. Secure schemes have an odd numeric value, + * others are even. + */ +enum coap_uri_scheme_t { + COAP_URI_SCHEME_COAP=0, + COAP_URI_SCHEME_COAPS=1 +}; + +/** This mask can be used to check if a parsed URI scheme is secure. */ +#define COAP_URI_SCHEME_SECURE_MASK 0x01 + /** * Representation of parsed URI. Components may be filled from a string with * coap_split_uri() and can be used as input for option-creation functions. @@ -23,8 +35,16 @@ typedef struct { str path; /**< Beginning of the first path segment. Use coap_split_path() to create Uri-Path options */ str query; /**< The query part if present */ + + /** The parsed scheme specifier. */ + enum coap_uri_scheme_t scheme; } coap_uri_t; +static inline int +coap_uri_scheme_is_secure(const coap_uri_t *uri) { + return uri && ((uri->scheme & COAP_URI_SCHEME_SECURE_MASK) != 0); +} + /** * Creates a new coap_uri_t object from the specified URI. Returns the new * object or NULL on error. The memory allocated by the new coap_uri_t diff --git a/src/uri.c b/src/uri.c index ec6c57ee..ede35e9d 100644 --- a/src/uri.c +++ b/src/uri.c @@ -74,6 +74,10 @@ coap_split_uri(unsigned char *str_var, size_t len, coap_uri_t *uri) { /* There might be an additional 's', indicating the secure version: */ if (len && (secure = tolower(*p) == 's')) { ++p; --len; + uri->scheme = COAP_URI_SCHEME_COAPS; + uri->port = COAPS_DEFAULT_PORT; + } else { + uri->scheme = COAP_URI_SCHEME_COAP; } q = (unsigned char *)"://";