From 44ea7c58ba66fb9ddffc71ea76fe8e771dd1035d Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Wed, 21 Mar 2012 16:40:59 +0100 Subject: [PATCH] attribute filters for /.well-known/core --- ChangeLog | 4 +++ net.c | 5 +-- resource.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++--- resource.h | 13 ++++++++ 4 files changed, 107 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 56eed38b..8ae89fe7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2012-03-21 Olaf Bergmann + + * resource.c (print_wellknown): filtering by attributes + 2012-03-19 Olaf Bergmann * pdu.c (coap_add_option): allow more than 15 options. diff --git a/net.c b/net.c index ce6f7fc8..d838b6e2 100644 --- a/net.c +++ b/net.c @@ -62,7 +62,7 @@ MEMB(node_storage, coap_queue_t, COAP_PDU_MAXCNT); PROCESS(coap_retransmit_process, "message retransmit process"); #endif /* WITH_CONTIKI */ -int print_wellknown(coap_context_t *, unsigned char *, size_t *); +int print_wellknown(coap_context_t *, unsigned char *, size_t *, coap_opt_t *); void coap_handle_failed_notify(coap_context_t *, const coap_address_t *, const str *); @@ -846,7 +846,8 @@ wellknown_response(coap_context_t *context, coap_pdu_t *request) { /* set payload of response */ len = resp->max_size - resp->length; - if (!print_wellknown(context, resp->data, &len)) { + if (!print_wellknown(context, resp->data, &len, + coap_check_option(request, COAP_OPTION_URI_QUERY, &opt_iter))) { debug("print_wellknown failed\n"); coap_delete_pdu(resp); return NULL; diff --git a/resource.c b/resource.c index c5de0637..d7a72dcb 100644 --- a/resource.c +++ b/resource.c @@ -32,6 +32,17 @@ coap_resources_init() { #define min(a,b) ((a) < (b) ? (a) : (b)) +int +match(const str *text, const str *pattern, int match_substring) { + assert(text); assert(pattern); + + if (text->length < pattern->length) + return 0; + + return (match_substring || pattern->length == text->length) && + memcmp(text->s, pattern->s, pattern->length) == 0; +} + /** * Prints the names of all known resources to @p buf. This function * sets @p buflen to the number of bytes actually written and returns @@ -42,27 +53,77 @@ coap_resources_init() { * @param buf The buffer to write the result. * @param buflen Must be initialized to the maximum length of @p buf and will be * set to the number of bytes written on return. + * @param query_filter A filter query according to Link Format * * @return @c 0 on error or @c 1 on success. */ int -print_wellknown(coap_context_t *context, unsigned char *buf, size_t *buflen) { +print_wellknown(coap_context_t *context, unsigned char *buf, size_t *buflen, + coap_opt_t *query_filter) { coap_resource_t *r; unsigned char *p = buf; size_t left, written = 0; -#ifndef WITH_CONTIKI coap_resource_t *tmp; + str resource_param = { 0, NULL }, query_pattern = { 0, NULL }; + int flags = 0; /* MATCH_SUBSTRING, MATCH_URI */ +#define MATCH_URI 0x01 +#define MATCH_SUBSTRING 0x02 + +#ifdef WITH_CONTIKI + int i; +#endif /* WITH_CONTIKI */ + + /* split query filter, if any */ + if (query_filter) { + resource_param.s = COAP_OPT_VALUE(query_filter); + while (resource_param.length < COAP_OPT_LENGTH(query_filter) + && resource_param.s[resource_param.length] != '=') + resource_param.length++; + + if (resource_param.length < COAP_OPT_LENGTH(query_filter)) { + if (memcmp(resource_param.s, "uri", resource_param.length) == 0) + flags |= MATCH_URI; + + /* rest is query-pattern */ + query_pattern.s = + COAP_OPT_VALUE(query_filter) + resource_param.length + 1; + + assert((resource_param.length + 1) <= COAP_OPT_LENGTH(query_filter)); + query_pattern.length = + COAP_OPT_LENGTH(query_filter) - (resource_param.length + 1); + + if (query_pattern.length && + query_pattern.s[query_pattern.length-1] == '*') { + query_pattern.length--; + flags |= MATCH_SUBSTRING; + } + } + } + +#ifndef WITH_CONTIKI HASH_ITER(hh, context->resources, r, tmp) { #else /* WITH_CONTIKI */ - int i; - r = (coap_resource_t *)resource_storage.mem; for (i = 0; i < resource_storage.num; ++i, ++r) { if (!resource_storage.count[i]) continue; #endif /* WITH_CONTIKI */ + if (resource_param.length) { /* there is a query filter */ + + if (flags & MATCH_URI) { /* match resource URI */ + if (!match(&r->uri, &query_pattern, (flags & MATCH_SUBSTRING) != 0)) + continue; + } else { /* match attribute */ + coap_attr_t *attr; + attr = coap_find_attr(r, resource_param.s, resource_param.length); + if (!(attr && match(&attr->value, &query_pattern, + (flags & MATCH_SUBSTRING) != 0))) + continue; + } + } + left = *buflen - written; if (left < *buflen) { /* this is not the first resource */ @@ -144,6 +205,28 @@ coap_add_attr(coap_resource_t *resource, return attr; } +coap_attr_t * +coap_find_attr(coap_resource_t *resource, + const unsigned char *name, size_t nlen) { + coap_attr_t *attr; + + if (!resource || !name) + return NULL; + +#ifndef WITH_CONTIKI + LL_FOREACH(resource->link_attr, attr) { +#else /* WITH_CONTIKI */ + for (attr = list_head(resource->link_attr); attr; + attr = list_item_next(attr)) { +#endif /* WITH_CONTIKI */ + if (attr->name.length == nlen && + memcmp(attr->name.s, name, nlen) == 0) + return attr; + } + + return NULL; +} + void coap_hash_request_uri(const coap_pdu_t *request, coap_key_t key) { coap_opt_iterator_t opt_iter; diff --git a/resource.h b/resource.h index 4bbdc10c..0a0e7f5b 100644 --- a/resource.h +++ b/resource.h @@ -130,6 +130,19 @@ coap_attr_t *coap_add_attr(coap_resource_t *resource, const unsigned char *name, size_t nlen, const unsigned char *val, size_t vlen); +/** + * Returns @p resource's coap_attr_t object with given @p name if + * found, @c NULL otherwise. + * + * @param resource The resource to search for attribute @p name. + * @param name Name of the requested attribute. + * @param nlen Actual length of @p name. + * @return The first attribute with specified @p name or @c NULL if + * none was found. + */ +coap_attr_t *coap_find_attr(coap_resource_t *resource, + const unsigned char *name, size_t nlen); + /** * Writes a description of this resource in link-format to given text * buffer. @p len must be initialized to the maximum length of @p buf