diff --git a/examples/server.c b/examples/server.c index b04a5a0e..d08f6f7f 100644 --- a/examples/server.c +++ b/examples/server.c @@ -39,6 +39,8 @@ static int quit = 0; /* changeable clock base (see handle_put_time()) */ static time_t my_clock_base = 0; +struct coap_resource_t *time_resource = NULL; + #ifndef WITHOUT_ASYNC /* This variable is used to mimic long-running tasks that require * asynchronous responses. */ @@ -81,6 +83,7 @@ hnd_get_time(coap_context_t *ctx, struct coap_resource_t *resource, size_t len; time_t now; coap_tick_t t; + coap_subscription_t *subscription; /* FIXME: return time, e.g. in human-readable by default and ticks * when query ?ticks is given. */ @@ -91,10 +94,11 @@ hnd_get_time(coap_context_t *ctx, struct coap_resource_t *resource, if (request != NULL && coap_check_option(request, COAP_OPTION_OBSERVE, &opt_iter)) { - coap_add_observer(resource, - peer, - token); - coap_add_option(response, COAP_OPTION_OBSERVE, 0, NULL); + subscription = coap_add_observer(resource, peer, token); + if (subscription) { + subscription->non = request->hdr->type == COAP_MESSAGE_NON; + coap_add_option(response, COAP_OPTION_OBSERVE, 0, NULL); + } } if (resource->dirty == 1) coap_add_option(response, COAP_OPTION_OBSERVE, @@ -275,6 +279,7 @@ init_resources(coap_context_t *ctx) { coap_add_attr(r, (unsigned char *)"if", 2, (unsigned char *)"\"clock\"", 7, 0); coap_add_resource(ctx, r); + time_resource = r; #ifndef WITHOUT_ASYNC r = coap_resource_init((unsigned char *)"async", 5, 0); @@ -419,7 +424,9 @@ main(int argc, char **argv) { coap_dispatch( ctx ); /* and dispatch PDUs from receivequeue */ } } else { /* timeout */ - /* coap_check_resource_list( ctx ); */ + if (time_resource) { + time_resource->dirty = 1; + } } #ifndef WITHOUT_ASYNC diff --git a/net.c b/net.c index b82df3dc..aaa3a5ca 100644 --- a/net.c +++ b/net.c @@ -581,7 +581,9 @@ coap_retransmit( coap_context_t *context, coap_queue_t *node ) { /* no more retransmissions, remove node from system */ - debug("** removed transaction %d\n", node->id); +#ifndef WITH_CONTIKI + debug("** removed transaction %d\n", ntohs(node->id)); +#endif #ifndef WITHOUT_OBSERVE /* Check if subscriptions exist that should be canceled after @@ -756,6 +758,53 @@ coap_remove_from_queue(coap_queue_t **queue, coap_tid_t id, coap_queue_t **node) } +static inline int +token_match(const unsigned char *a, size_t alen, + const unsigned char *b, size_t blen) { + return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0); +} + +void +coap_cancel_all_messages(coap_context_t *context, const coap_address_t *dst, + const unsigned char *token, size_t token_length) { + /* cancel all messages in sendqueue that are for dst + * and use the specified token */ + coap_queue_t *p, *q; + + debug("cancel_all_messages\n"); + while (context->sendqueue && + coap_address_equals(dst, &context->sendqueue->remote) && + token_match(token, token_length, + context->sendqueue->pdu->hdr->token, + context->sendqueue->pdu->hdr->token_length)) { + q = context->sendqueue; + context->sendqueue = q->next; + debug("**** removed transaction %d\n", ntohs(q->pdu->hdr->id)); + coap_delete_node(q); + } + + if (!context->sendqueue) + return; + + p = context->sendqueue; + q = p->next; + + /* when q is not NULL, it does not match (dst, token), so we can skip it */ + while (q) { + if (coap_address_equals(dst, &q->remote) && + token_match(token, token_length, + q->pdu->hdr->token, q->pdu->hdr->token_length)) { + p->next = q->next; + debug("**** removed transaction %d\n", ntohs(q->pdu->hdr->id)); + coap_delete_node(q); + q = p->next; + } else { + p = q; + q = q->next; + } + } +} + coap_queue_t * coap_find_transaction(coap_queue_t *queue, coap_tid_t id) { while (queue && queue->id != id) @@ -1022,6 +1071,39 @@ handle_locally(coap_context_t *context, coap_queue_t *node) { return 1; } +/** + * This function handles RST messages received for the message passed + * in @p sent. + */ +static void +coap_handle_rst(coap_context_t *context, const coap_queue_t *sent) { +#ifndef WITHOUT_OBSERVE + coap_resource_t *r, *tmp; + str token = { 0, NULL }; + + /* remove observer for this resource, if any + * get token from sent and try to find a matching resource. Uh! + */ + + COAP_SET_STR(&token, sent->pdu->hdr->token_length, sent->pdu->hdr->token); + +#ifndef WITH_CONTIKI + HASH_ITER(hh, context->resources, r, tmp) { + coap_delete_observer(r, &sent->remote, &token); + coap_cancel_all_messages(context, &sent->remote, token.s, token.length); + } +#else /* WITH_CONTIKI */ + r = (coap_resource_t *)resource_storage.mem; + for (i = 0; i < resource_storage.num; ++i, ++r) { + if (resource_storage.count[i]) { + coap_delete_observer(r, &sent->remote, &token); + coap_cancel_all_messages(context, &sent->remote, token.s, token.length); + } + } +#endif /* WITH_CONTIKI */ +#endif /* WITOUT_OBSERVE */ +} + void coap_dispatch( coap_context_t *context ) { coap_queue_t *rcvd = NULL, *sent = NULL; @@ -1056,6 +1138,11 @@ coap_dispatch( coap_context_t *context ) { /* FIXME: if sent code was >= 64 the message might have been a * notification. Then, we must flag the observer to be alive * by setting obs->fail_cnt = 0. */ + if (sent && COAP_RESPONSE_CLASS(sent->pdu->hdr->code) == 2) { + const str token = + { sent->pdu->hdr->token_length, sent->pdu->hdr->token }; + coap_touch_observer(context, &sent->remote, &token); + } break; case COAP_MESSAGE_RST : @@ -1068,14 +1155,11 @@ coap_dispatch( coap_context_t *context ) { #else /* WITH_CONTIKI */ coap_log(LOG_ALERT, "got RST for message %u\n", uip_ntohs(rcvd->pdu->hdr->id)); #endif /* WITH_CONTIKI */ - - /* find transaction in sendqueue to stop retransmission */ coap_remove_from_queue(&context->sendqueue, rcvd->id, &sent); - /* @todo remove observer for this resource, if any - * get token from sent and try to find a matching resource. Uh! - */ - break; + if (sent) + coap_handle_rst(context, sent); + goto cleanup; case COAP_MESSAGE_NON : /* check for unknown critical options */ if (coap_option_check_critical(context, rcvd->pdu, opt_filter) == 0) diff --git a/net.h b/net.h index 75ae58c0..37119fc1 100644 --- a/net.h +++ b/net.h @@ -118,7 +118,7 @@ typedef struct coap_context_t { * The next value to be used for Observe. This field is global for * all resources and will be updated when notifications are created. */ - unsigned short observe; + unsigned int observe; coap_response_handler_t response_handler; } coap_context_t; @@ -370,6 +370,20 @@ coap_remove_transaction(coap_queue_t **queue, coap_tid_t id) { */ coap_queue_t *coap_find_transaction(coap_queue_t *queue, coap_tid_t id); +/** + * Cancels all outstanding messages for peer @p dst that have the + * specified token. + * + * @param context The context in use + * @param dst Destination address of the messages to remove. + * @param token Message token + * @param token_length Actual length of @p token + */ +void coap_cancel_all_messages(coap_context_t *context, + const coap_address_t *dst, + const unsigned char *token, + size_t token_length); + /** Dispatches the PDUs from the receive queue in given context. */ void coap_dispatch( coap_context_t *context ); diff --git a/pdu.h b/pdu.h index f7302638..6f27487d 100644 --- a/pdu.h +++ b/pdu.h @@ -67,9 +67,9 @@ #define COAP_OPTION_PROXY_SCHEME 39 /* C, String, 1-255 B, (none) */ #define COAP_OPTION_SIZE1 60 /* E, uint, 0-4 B, (none) */ -/* option types from draft-hartke-coap-observe-07 */ +/* option types from draft-ietf-coap-observe-09 */ -#define COAP_OPTION_OBSERVE 6 /* E, empty/uint, 0 B/0-2 B, (none) */ +#define COAP_OPTION_OBSERVE 6 /* E, empty/uint, 0 B/0-3 B, (none) */ #define COAP_OPTION_SUBSCRIPTION COAP_OPTION_OBSERVE /* selected option types from draft-core-block-04 */ diff --git a/resource.c b/resource.c index 2a616330..e4348d35 100644 --- a/resource.c +++ b/resource.c @@ -1,6 +1,6 @@ /* resource.c -- generic resource handling * - * Copyright (C) 2010--2012 Olaf Bergmann + * Copyright (C) 2010--2013 Olaf Bergmann * * This file is part of the CoAP library libcoap. Please see * README for terms of use. @@ -15,6 +15,11 @@ #ifndef WITH_CONTIKI #include "utlist.h" #include "mem.h" + +#define COAP_MALLOC_TYPE(Type) \ + ((coap_##Type##_t *)coap_malloc(sizeof(coap_##Type##_t))) +#define COAP_FREE_TYPE(Type, Object) coap_free(Object) + #else /* WITH_CONTIKI */ #include "memb.h" @@ -28,6 +33,16 @@ coap_resources_init() { memb_init(&attribute_storage); memb_init(&subscription_storage); } + +static inline coap_subscription_t * +coap_malloc_subscription() { + return memb_alloc(&subscription_storage); +} + +static inline void +coap_free_subscription(coap_subscription_t *subscription) { + memb_free(&subscription_storage, subscription); +} #endif /* WITH_CONTIKI */ #define min(a,b) ((a) < (b) ? (a) : (b)) @@ -222,8 +237,8 @@ coap_resource_init(const unsigned char *uri, size_t len, int flags) { #ifdef WITH_CONTIKI LIST_STRUCT_INIT(r, link_attr); - LIST_STRUCT_INIT(r, subscribers); #endif /* WITH_CONTIKI */ + LIST_STRUCT_INIT(r, subscribers); r->uri.s = (unsigned char *)uri; r->uri.length = len; @@ -457,11 +472,7 @@ coap_find_observer(coap_resource_t *resource, const coap_address_t *peer, assert(resource); assert(peer); -#ifndef WITH_CONTIKI - LL_FOREACH(resource->subscribers, s) { -#else /* WITH_CONTIKI */ for (s = list_head(resource->subscribers); s; s = list_item_next(s)) { -#endif /* WITH_CONTIKI */ if (coap_address_equals(&s->subscriber, peer) && (!token || (token->length == s->token_length && memcmp(token->s, s->token, token->length) == 0))) @@ -488,11 +499,7 @@ coap_add_observer(coap_resource_t *resource, /* s points to a different subscription, so we have to create * another one. */ -#ifndef WITH_CONTIKI - s = (coap_subscription_t *)coap_malloc(sizeof(coap_subscription_t)); -#else /* WITH_CONTIKI */ - s = memb_alloc(&subscription_storage); -#endif /* WITH_CONTIKI */ + s = COAP_MALLOC_TYPE(subscription); if (!s) return NULL; @@ -506,114 +513,179 @@ coap_add_observer(coap_resource_t *resource, } /* add subscriber to resource */ -#ifndef WITH_CONTIKI - LL_PREPEND(resource->subscribers, s); -#else /* WITH_CONTIKI */ list_add(resource->subscribers, s); -#endif /* WITH_CONTIKI */ return s; } void -coap_delete_observer(coap_resource_t *resource, coap_address_t *observer, +coap_touch_observer(coap_context_t *context, const coap_address_t *observer, + const str *token) { + coap_resource_t *r, *tmp; + coap_subscription_t *s; + +#ifndef WITH_CONTIKI + HASH_ITER(hh, context->resources, r, tmp) { + s = coap_find_observer(r, observer, token); + if (s) { + s->fail_cnt = 0; + } + } +#else /* WITH_CONTIKI */ + r = (coap_resource_t *)resource_storage.mem; + for (i = 0; i < resource_storage.num; ++i, ++r) { + if (resource_storage.count[i]) { + s = coap_find_observer(r, observer, token); + if (s) { + s->fail_cnt = 0; + } + } + } +#endif /* WITH_CONTIKI */ +} + +void +coap_delete_observer(coap_resource_t *resource, const coap_address_t *observer, const str *token) { coap_subscription_t *s; s = coap_find_observer(resource, observer, token); if (s) { -#ifndef WITH_CONTIKI - LL_DELETE(resource->subscribers, s); -#else /* WITH_CONTIKI */ list_remove(resource->subscribers, s); -#endif /* WITH_CONTIKI */ - /* FIXME: notify observer that its subscription has been removed */ -#ifndef WITH_CONTIKI - coap_free(s); -#else /* WITH_CONTIKI */ - memb_free(&subscription_storage, s); -#endif /* WITH_CONTIKI */ + COAP_FREE_TYPE(subscription,s); } } +static void +coap_notify_observers(coap_context_t *context, coap_resource_t *r) { + coap_method_handler_t h; + coap_subscription_t *obs; + str token; + coap_pdu_t *response; + + /* retrieve GET handler, prepare response */ + h = r->handler[COAP_REQUEST_GET - 1]; + assert(h); /* we do not allow subscriptions if no + * GET handler is defined */ + + if (r->observable && r->dirty) { + for (obs = list_head(r->subscribers); obs; obs = list_item_next(obs)) { + coap_tid_t tid = COAP_INVALID_TID; + /* initialize response */ + response = coap_pdu_init(COAP_MESSAGE_CON, 0, 0, COAP_MAX_PDU_SIZE); + if (!response) { + debug("coap_check_notify: pdu init failed\n"); + continue; + } + + if (!coap_add_token(response, obs->token_length, obs->token)) { + debug("coap_check_notify: cannot add token\n"); + coap_delete_pdu(response); + continue; + } + + token.length = obs->token_length; + token.s = obs->token; + + response->hdr->id = coap_new_message_id(context); + if (obs->non && obs->non_cnt < COAP_OBS_MAX_NON) { + response->hdr->type = COAP_MESSAGE_NON; + } else { + response->hdr->type = COAP_MESSAGE_CON; + } + /* fill with observer-specific data */ + h(context, r, &obs->subscriber, NULL, &token, response); + + if (response->hdr->type == COAP_MESSAGE_CON) { + tid = coap_send_confirmed(context, &obs->subscriber, response); + obs->non_cnt = 0; + } else { + tid = coap_send(context, &obs->subscriber, response); + obs->non_cnt++; + } + + if (COAP_INVALID_TID == tid || response->hdr->type != COAP_MESSAGE_CON) + coap_delete_pdu(response); + } + + /* Increment value for next Observe use. */ + context->observe++; + } + r->dirty = 0; +} void coap_check_notify(coap_context_t *context) { coap_resource_t *r; - coap_pdu_t *response; #ifndef WITH_CONTIKI coap_resource_t *tmp; - + HASH_ITER(hh, context->resources, r, tmp) { - if (r->observable && r->dirty && r->subscribers) { + coap_notify_observers(context, r); + } #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; - - if (r->observable && r->dirty && list_head(r->subscribers)) { -#endif /* WITH_CONTIKI */ - coap_method_handler_t h; - coap_subscription_t *obs; - str token; - - /* retrieve GET handler, prepare response */ - h = r->handler[COAP_REQUEST_GET - 1]; - assert(h); /* we do not allow subscriptions if no - * GET handler is defined */ - -#ifndef WITH_CONTIKI - /* FIXME: */ - LL_FOREACH(r->subscribers, obs) { -#else /* WITH_CONTIKI */ - for (obs = list_head(r->subscribers); obs; obs = list_item_next(obs)) { -#endif /* WITH_CONTIKI */ - coap_tid_t tid = COAP_INVALID_TID; - /* initialize response */ - response = coap_pdu_init(COAP_MESSAGE_CON, 0, 0, COAP_MAX_PDU_SIZE); - if (!response) { - debug("coap_check_notify: pdu init failed\n"); - continue; - } - if (!coap_add_token(response, obs->token_length, obs->token)) { - debug("coap_check_notify: cannot add token\n"); - coap_delete_pdu(response); - continue; - } - - token.length = obs->token_length; - token.s = obs->token; - - response->hdr->id = coap_new_message_id(context); - if (obs->non && obs->non_cnt < COAP_OBS_MAX_NON) - response->hdr->type = COAP_MESSAGE_NON; - else - response->hdr->type = COAP_MESSAGE_CON; - - /* fill with observer-specific data */ - h(context, r, &obs->subscriber, NULL, &token, response); - - if (response->hdr->type == COAP_MESSAGE_CON) { - tid = coap_send_confirmed(context, &obs->subscriber, response); - obs->non_cnt = 0; - } else { - tid = coap_send(context, &obs->subscriber, response); - obs->non_cnt++; - } - - if (COAP_INVALID_TID == tid || response->hdr->type != COAP_MESSAGE_CON) - coap_delete_pdu(response); - } - - /* Increment value for next Observe use. */ - context->observe++; + if (resource_storage.count[i]) { + coap_notify_observers(context, r); } - r->dirty = 0; + } +#endif /* WITH_CONTIKI */ +} + +/** + * Checks the failure counter for (peer, token) and removes peer from + * the list of observers for the given resource when COAP_OBS_MAX_FAIL + * is reached. + * + * @param context The CoAP context to use + * @param resource The resource to check for (peer, token) + * @param peer The observer's address + * @param token The token that has been used for subscription. + */ +static void +coap_remove_failed_observers(coap_context_t *context, + coap_resource_t *resource, + const coap_address_t *peer, + const str *token) { + coap_subscription_t *obs; + + for (obs = list_head(resource->subscribers); obs; + obs = list_item_next(obs)) { + if (coap_address_equals(peer, &obs->subscriber) && + token->length == obs->token_length && + memcmp(token->s, obs->token, token->length) == 0) { + + /* count failed notifies and remove when + * COAP_MAX_FAILED_NOTIFY is reached */ + if (obs->fail_cnt < COAP_OBS_MAX_FAIL) + obs->fail_cnt++; + else { + list_remove(resource->subscribers, obs); + obs->fail_cnt = 0; + +#ifndef NDEBUG + if (LOG_DEBUG <= coap_get_log_level()) { +#ifndef INET6_ADDRSTRLEN +#define INET6_ADDRSTRLEN 40 +#endif + unsigned char addr[INET6_ADDRSTRLEN+8]; + + if (coap_print_addr(&obs->subscriber, addr, INET6_ADDRSTRLEN+8)) + debug("** removed observer %s\n", addr); + } +#endif + coap_cancel_all_messages(context, &obs->subscriber, + obs->token, obs->token_length); + + COAP_FREE_TYPE(subscription, obs); + } + } + break; /* break loop if observer was found */ } } @@ -622,50 +694,22 @@ coap_handle_failed_notify(coap_context_t *context, const coap_address_t *peer, const str *token) { coap_resource_t *r; - coap_subscription_t *obs; #ifndef WITH_CONTIKI - ; + coap_resource_t *tmp; + + HASH_ITER(hh, context->resources, r, tmp) { + coap_remove_failed_observers(context, r, peer, token); + } #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; - - again: - for (obs = list_head(r->subscribers); obs; obs = list_item_next(obs)) { - if (coap_address_equals(peer, &obs->subscriber) && - token->length == obs->token_length && - memcmp(token->s, obs->token, token->length) == 0) { - - /* FIXME: count failed notifies and remove when - * COAP_MAX_FAILED_NOTIFY is reached */ - if (obs->fail_cnt < COAP_OBS_MAX_FAIL) - obs->fail_cnt++; - else { - list_remove(r->subscribers, obs); - obs->fail_cnt = 0; - - debug("removed observer [%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]:%d\r\n", - obs->subscriber.addr.u8[0], obs->subscriber.addr.u8[1], - obs->subscriber.addr.u8[2], obs->subscriber.addr.u8[3], - obs->subscriber.addr.u8[4], obs->subscriber.addr.u8[5], - obs->subscriber.addr.u8[6], obs->subscriber.addr.u8[7], - obs->subscriber.addr.u8[8], obs->subscriber.addr.u8[9], - obs->subscriber.addr.u8[10], obs->subscriber.addr.u8[11], - obs->subscriber.addr.u8[12], obs->subscriber.addr.u8[13], - obs->subscriber.addr.u8[14], obs->subscriber.addr.u8[15] , - uip_ntohs(obs->subscriber.port)); - - memb_free(&subscription_storage, obs); - goto again; - } - } + if (resource_storage.count[i]) { + coap_remove_failed_observers(context, r, peer, token); } } #endif /* WITH_CONTIKI */ } - #endif /* WITHOUT_NOTIFY */ diff --git a/resource.h b/resource.h index e5278a6b..cad813fd 100644 --- a/resource.h +++ b/resource.h @@ -15,6 +15,7 @@ #define _COAP_RESOURCE_H_ #include "config.h" +#include "t_list.h" #if defined(HAVE_ASSERT_H) && !defined(assert) # include @@ -28,7 +29,6 @@ #ifndef WITH_CONTIKI #include "uthash.h" #else /* WITH_CONTIKI */ -#include "list.h" #endif /* WITH_CONTIKI */ #include "hashkey.h" #include "async.h" @@ -72,11 +72,10 @@ typedef struct coap_resource_t { #ifndef WITH_CONTIKI UT_hash_handle hh; coap_attr_t *link_attr; /**< attributes to be included with the link format */ - coap_subscription_t *subscribers; /**< list of observers for this resource */ #else /* WITH_CONTIKI */ LIST_STRUCT(link_attr); /**< attributes to be included with the link format */ - LIST_STRUCT(subscribers); /**< list of observers for this resource */ #endif /* WITH_CONTIKI */ + LIST_STRUCT(subscribers); /**< list of observers for this resource */ /** @@ -253,6 +252,18 @@ coap_subscription_t *coap_find_observer(coap_resource_t *resource, const coap_address_t *peer, const str *token); +/** + * Marks an observer as alive. + * + * @param context The CoAP context to use + * @param observer The transport address of the observer + * @param token The corresponding token that has been used for + * the subscription + */ +void coap_touch_observer(coap_context_t *context, + const coap_address_t *observer, + const str *token); + /** * Removes any subscription for @p observer from @p resource and releases * the allocated storage. @@ -263,7 +274,7 @@ coap_subscription_t *coap_find_observer(coap_resource_t *resource, * token. */ void coap_delete_observer(coap_resource_t *resource, - coap_address_t *observer, + const coap_address_t *observer, const str *token); /** diff --git a/subscribe.c b/subscribe.c index 65db0f28..d6834198 100644 --- a/subscribe.c +++ b/subscribe.c @@ -1,7 +1,7 @@ /* subscribe.c -- subscription handling for CoAP - * see draft-hartke-coap-observe-03 + * see draft-ietf-coap-observe-09 * - * Copyright (C) 2010--2012 Olaf Bergmann + * Copyright (C) 2010--2013 Olaf Bergmann * * This file is part of the CoAP library libcoap. Please see * README for terms of use.