From 60d68d7d6225ef016f243387f0a577bf4a47df58 Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Wed, 22 Jun 2011 18:07:50 +0200 Subject: [PATCH] fixed data pointer in coap_read() some minor bugfixes example for separate responses (server side only) --- examples/client.c | 42 ++++++++-- examples/server.c | 129 +++++++++++++++++++++++++++++-- net.c | 190 +++++++++++++++++++++++++++------------------- net.h | 67 +++++++++++++--- resource.h | 3 +- 5 files changed, 326 insertions(+), 105 deletions(-) diff --git a/examples/client.c b/examples/client.c index 5980f9a4..0f1d4559 100644 --- a/examples/client.c +++ b/examples/client.c @@ -162,7 +162,10 @@ resolve_address(const str *server, struct sockaddr *dst) { int error; memset(addrstr, 0, sizeof(addrstr)); - memcpy(addrstr, server->s, server->length); + if (server->length) + memcpy(addrstr, server->s, server->length); + else + memcpy(addrstr, "localhost", 9); memset ((char *)&hints, 0, sizeof(hints)); hints.ai_socktype = SOCK_DGRAM; @@ -208,10 +211,10 @@ _read_blk_nr(coap_opt_t *opt) { void message_handler(struct coap_context_t *ctx, - const coap_tid_t id, const coap_address_t *remote, coap_pdu_t *sent, - coap_pdu_t *received) { + coap_pdu_t *received, + const coap_tid_t id) { coap_pdu_t *pdu = NULL; coap_opt_t *block, *ct, *sub; @@ -293,11 +296,13 @@ message_handler(struct coap_context_t *ctx, /* check if an error was signaled and output payload if so */ if (COAP_RESPONSE_CLASS(received->hdr->code) >= 4) { - char *phrase = coap_response_phrase(received->hdr->code); fprintf(stderr, "%d.%02d", (received->hdr->code >> 5), received->hdr->code & 0x1F); - if (phrase) - fprintf(stderr, " %s", phrase); + if (coap_get_data(received, &len, &databuf)) { + fprintf(stderr, " "); + while(len--) + fprintf(stderr, "%c", *databuf++); + } fprintf(stderr, "\n"); } @@ -330,7 +335,8 @@ usage( const char *program, const char *version) { fprintf( stderr, "%s v%s -- a small CoAP implementation\n" "(c) 2010 Olaf Bergmann \n\n" - "usage: %s [-b num] [-g group] [-m method] [-o file] [-p port] [-s num] [-t type...] [-T string] URI\n\n" + "usage: %s [-b num] [-g group] [-m method] [-o file] [-p port] [-s num] \n" + "\t\t[-t type...] [-O num,text] [-T string] URI\n\n" "\tURI can be an absolute or relative coap URI,\n" "\t-b size\t\tblock size to be used in GET/PUT/POST requests\n" "\t \t\t(value must be a multiple of 16 not larger than 2048)\n" @@ -342,6 +348,7 @@ usage( const char *program, const char *version) { "\t-s duration\tsubscribe for given duration [s]\n" "\t-A types\taccepted content for GET (comma-separated list)\n" "\t-t type\t\tcontent type for given resource for PUT/POST\n" + "\t-O num,text\tadd option num with contents text to request\n" "\t-P addr[:port]\tuse proxy (automatically adds Proxy-Uri option to request)\n" "\t-T token\tinclude specified token\n", program, version, program ); @@ -562,6 +569,22 @@ cmdline_token(char *arg) { (unsigned char *)arg), order_opts); } +void +cmdline_option(char *arg) { + unsigned int num = 0; + + while (*arg && *arg != ',') { + num = num * 10 + (*arg - '0'); + ++arg; + } + if (*arg == ',') + ++arg; + + coap_insert( &optlist, new_option_node(num, + strlen(arg), + (unsigned char *)arg), order_opts); +} + int cmdline_input_from_file(char *filename, str *buf) { FILE *inputfile = NULL; @@ -683,7 +706,7 @@ main(int argc, char **argv) { int opt, res; char *group = NULL; - while ((opt = getopt(argc, argv, "b:f:g:m:p:s:t:o:A:P:T:")) != -1) { + while ((opt = getopt(argc, argv, "b:f:g:m:p:s:t:o:A:O:P:T:")) != -1) { switch (opt) { case 'b' : cmdline_blocksize(optarg); @@ -722,6 +745,9 @@ main(int argc, char **argv) { case 't' : cmdline_content_type(optarg,COAP_OPTION_CONTENT_TYPE); break; + case 'O' : + cmdline_option(optarg); + break; case 'P' : cmdline_proxy(optarg); break; diff --git a/examples/server.c b/examples/server.c index ce501ec7..830d5fcc 100644 --- a/examples/server.c +++ b/examples/server.c @@ -39,6 +39,18 @@ static int quit = 0; /* changeable clock base (see handle_put_time()) */ static time_t my_clock_base = 0; +/* These variables are used to mimic long-running tasks that require asynchronous + * responses. */ +struct async_t { + coap_tick_t t; + coap_tid_t id; /**< transaction id */ + coap_address_t peer; + size_t toklen; + unsigned char token[]; +}; + +static struct async_t *async = NULL; + /* SIGINT handler: set quit to 1 for graceful termination */ void handle_sigint(int signum) { @@ -50,7 +62,7 @@ handle_sigint(int signum) { void hnd_get_index(coap_context_t *ctx, struct coap_resource_t *resource, - coap_address_t *peer, coap_pdu_t *request) { + coap_address_t *peer, coap_pdu_t *request, coap_tid_t id) { coap_opt_iterator_t opt_iter; coap_opt_t *token; coap_pdu_t *response; @@ -96,7 +108,7 @@ hnd_get_index(coap_context_t *ctx, struct coap_resource_t *resource, void hnd_get_time(coap_context_t *ctx, struct coap_resource_t *resource, - coap_address_t *peer, coap_pdu_t *request) { + coap_address_t *peer, coap_pdu_t *request, coap_tid_t id) { coap_opt_iterator_t opt_iter; coap_opt_t *token; coap_pdu_t *response; @@ -172,7 +184,7 @@ hnd_get_time(coap_context_t *ctx, struct coap_resource_t *resource, void hnd_put_time(coap_context_t *ctx, struct coap_resource_t *resource, - coap_address_t *peer, coap_pdu_t *request) { + coap_address_t *peer, coap_pdu_t *request, coap_tid_t id) { coap_opt_iterator_t opt_iter; coap_opt_t *token; @@ -232,7 +244,7 @@ hnd_put_time(coap_context_t *ctx, struct coap_resource_t *resource, void hnd_delete_time(coap_context_t *ctx, struct coap_resource_t *resource, - coap_address_t *peer, coap_pdu_t *request) { + coap_address_t *peer, coap_pdu_t *request, coap_tid_t id) { coap_opt_t *token; coap_pdu_t *response; coap_opt_iterator_t opt_iter; @@ -267,11 +279,98 @@ hnd_delete_time(coap_context_t *ctx, struct coap_resource_t *resource, } } +void +hnd_get_async(coap_context_t *ctx, struct coap_resource_t *resource, + coap_address_t *peer, coap_pdu_t *request, coap_tid_t id) { + + coap_opt_iterator_t opt_iter; + coap_opt_t *token; + size_t size = sizeof(struct async_t); + coap_tick_t now; + unsigned int delay = 5; + + /* There is already a request being handled asynchronously. Check if + * it is the same request. If not, return a 5.03. */ + if (async) { + if (memcmp(&id, &async->id, sizeof(coap_tid_t)) == 0) { + coap_send_ack(ctx, peer, request); + } else { + coap_opt_filter_t f; + coap_option_filter_clear(f); + coap_send_error(ctx, request, peer, COAP_RESPONSE_CODE(503), f); + } + return; + } + + /* store information for handling the asynchronous task */ + token = coap_check_option(request, COAP_OPTION_TOKEN, &opt_iter); + if (token) + size += COAP_OPT_LENGTH(token); + + async = (struct async_t *)coap_malloc(size); + if (!async) { + warn("hnd_get_async: insufficient memory\n"); + return; + } + memset(async, 0, size); /* the easiest way to clean token */ + + if (coap_check_option(request, COAP_OPTION_URI_QUERY, &opt_iter)) { + unsigned char *p = COAP_OPT_VALUE(opt_iter.option); + + delay = 0; + for (size = COAP_OPT_LENGTH(opt_iter.option); size; --size, ++p) + delay = delay * 10 + (*p - '0'); + } + + coap_ticks(&now); + async->t = now + COAP_TICKS_PER_SECOND * delay; + memcpy(&async->id, &id, sizeof(coap_tid_t)); + memcpy(&async->peer, peer, sizeof(coap_address_t)); + + if (token) { + async->toklen = COAP_OPT_LENGTH(opt_iter.option); + memcpy(async->token, COAP_OPT_VALUE(opt_iter.option), async->toklen); + } +} + +void +check_async(coap_context_t *ctx, coap_tick_t now) { + coap_pdu_t *response; + size_t size = sizeof(coap_hdr_t) + 8; + + if (!async || now < async->t) + return; + + size += async->toklen; + + response = coap_pdu_init(COAP_MESSAGE_CON, COAP_RESPONSE_CODE(205), 0, size); + if (!response) { + debug("check_async: insufficient memory, we'll try later\n"); + async->t += 15; + return; + } + prng((unsigned char *)&response->hdr->id, 2); + + if (async->toklen) + coap_add_option(response, COAP_OPTION_TOKEN, async->toklen, async->token); + + coap_add_data(response, 4, (unsigned char *)"done"); + + if (coap_send(ctx, &async->peer, response) == COAP_INVALID_TID) { + debug("check_async: cannot send response for message %d\n", + response->hdr->id); + coap_delete_pdu(response); + } + + coap_free(async); + async = NULL; +} + void init_resources(coap_context_t *ctx) { coap_resource_t *r; - r = coap_resource_init((unsigned char *)"", 0, 0); + r = coap_resource_init(NULL, 0, 0); coap_register_handler(r, COAP_REQUEST_GET, hnd_get_index); coap_add_attr(r, (unsigned char *)"ct", 2, (unsigned char *)"0", 1, 0); @@ -293,6 +392,12 @@ init_resources(coap_context_t *ctx) { coap_add_attr(r, (unsigned char *)"if", 2, (unsigned char *)"\"clock\"", 7, 0); coap_add_resource(ctx, r); + + r = coap_resource_init((unsigned char *)"async", 5, 0); + coap_register_handler(r, COAP_REQUEST_GET, hnd_get_async); + + coap_add_attr(r, (unsigned char *)"ct", 2, (unsigned char *)"0", 1, 0); + coap_add_resource(ctx, r); } void @@ -307,7 +412,8 @@ usage( const char *program, const char *version) { "(c) 2010,2011 Olaf Bergmann \n\n" "usage: %s [-A address] [-p port]\n\n" "\t-A address\tinterface address to bind to\n" - "\t-p port\t\tlisten on specified port\n", + "\t-p port\t\tlisten on specified port\n" + "\t-v num\t\tverbosity level (default: 3)\n", program, version, program ); } @@ -356,8 +462,9 @@ main(int argc, char **argv) { char addr_str[NI_MAXHOST] = "::"; char port_str[NI_MAXSERV] = "5683"; int opt; + coap_log_t log_level = LOG_WARN; - while ((opt = getopt(argc, argv, "A:p:")) != -1) { + while ((opt = getopt(argc, argv, "A:p:v:")) != -1) { switch (opt) { case 'A' : strncpy(addr_str, optarg, NI_MAXHOST-1); @@ -367,12 +474,17 @@ main(int argc, char **argv) { strncpy(port_str, optarg, NI_MAXSERV-1); port_str[NI_MAXSERV - 1] = '\0'; break; + case 'v' : + log_level = strtol(optarg, NULL, 10); + break; default: usage( argv[0], PACKAGE_VERSION ); exit( 1 ); } } + coap_set_log_level(log_level); + ctx = get_context(addr_str, port_str); if (!ctx) return -1; @@ -416,6 +528,9 @@ main(int argc, char **argv) { } else { /* timeout */ /* coap_check_resource_list( ctx ); */ } + + /* check if we have to send asynchronous responses */ + check_async(ctx, now); } coap_free_context( ctx ); diff --git a/net.c b/net.c index 087c491d..077e133f 100644 --- a/net.c +++ b/net.c @@ -260,7 +260,19 @@ coap_option_check_critical(coap_context_t *ctx, static inline coap_tid_t make_transaction_id(const coap_address_t *peer, coap_pdu_t *pdu) { /* FIXME: hash transport address and token instead of id */ - return ntohs(pdu->hdr->id); + coap_key_t h; + coap_opt_iterator_t opt_iter; + + memset(h, 0, sizeof(coap_key_t)); + coap_hash((const unsigned char *)&peer->addr.sa, peer->size, h); + + if (coap_check_option(pdu, COAP_OPTION_TOKEN, &opt_iter)) + coap_hash(COAP_OPT_VALUE(opt_iter.option), + COAP_OPT_LENGTH(opt_iter.option), + h); + + debug("transaction id: %u\n", ((h[0] << 8) | h[1]) ^ ((h[2] << 8) | h[3])); + return ((h[0] << 8) | h[1]) ^ ((h[2] << 8) | h[3]); } /* releases space allocated by PDU if free_pdu is set */ @@ -291,13 +303,35 @@ coap_send_impl(coap_context_t *context, return id; } -coap_tid_t -coap_send( coap_context_t *context, - const coap_address_t *dst, - coap_pdu_t *pdu ) { +coap_tid_t +coap_send(coap_context_t *context, + const coap_address_t *dst, + coap_pdu_t *pdu) { return coap_send_impl(context, dst, pdu, 1); } +coap_tid_t +coap_send_error(coap_context_t *context, + coap_pdu_t *request, + const coap_address_t *dst, + unsigned char code, + coap_opt_filter_t opts) { + coap_pdu_t *response; + coap_tid_t result = COAP_INVALID_TID; + + assert(request); + assert(dst); + + response = coap_new_error_response(request, code, opts); + if (response) { + result = coap_send(context, dst, response); + if (result == COAP_INVALID_TID) + coap_delete_pdu(response); + } + + return result; +} + int _order_timestamp( coap_queue_t *lhs, coap_queue_t *rhs ) { return lhs && rhs && ( lhs->t < rhs->t ) ? -1 : 1; @@ -311,7 +345,11 @@ coap_send_confirmed(coap_context_t *context, int r; node = coap_new_node(); - + if (!node) { + debug("coap_send_confirmed: insufficient memory\n"); + return COAP_INVALID_TID; + } + r = rand(); coap_ticks(&node->t); @@ -324,15 +362,11 @@ coap_send_confirmed(coap_context_t *context, memcpy(&node->remote, dst, sizeof(coap_address_t)); node->pdu = pdu; - if ( !coap_insert_node( &context->sendqueue, node, _order_timestamp ) ) { -#ifndef NDEBUG - fprintf(stderr, "coap_send_confirmed: cannot insert node:into sendqueue\n"); -#endif - coap_delete_node ( node ); - return COAP_INVALID_TID; - } + assert(&context->sendqueue); + coap_insert_node(&context->sendqueue, node, _order_timestamp); - return coap_send_impl( context, dst, pdu, 0 ); + node->id = coap_send_impl(context, dst, pdu, 0); + return node->id; } coap_tid_t @@ -348,13 +382,14 @@ coap_retransmit( coap_context_t *context, coap_queue_t *node ) { debug("** retransmission #%d of transaction %d\n", node->retransmit_cnt, ntohs(node->pdu->hdr->id)); - - return coap_send_impl(context, &node->remote, node->pdu, 0); + + node->id = coap_send_impl(context, &node->remote, node->pdu, 0); + return node->id; } /* no more retransmissions, remove node from system */ - debug("** removed transaction %d\n", ntohs(node->pdu->hdr->id)); + debug("** removed transaction %d\n", node->id); coap_delete_node( node ); return COAP_INVALID_TID; @@ -363,7 +398,7 @@ coap_retransmit( coap_context_t *context, coap_queue_t *node ) { int _order_transaction_id( coap_queue_t *lhs, coap_queue_t *rhs ) { return ( lhs && rhs && lhs->pdu && rhs->pdu && - ( lhs->pdu->hdr->id < lhs->pdu->hdr->id ) ) + ( lhs->id < lhs->id ) ) ? -1 : 1; } @@ -381,7 +416,7 @@ coap_read( coap_context_t *ctx ) { &src.addr.sa, &src.size); if ( bytes_read < 0 ) { - perror("coap_read: recvfrom"); + warn("coap_read: recvfrom"); return -1; } @@ -399,7 +434,7 @@ coap_read( coap_context_t *ctx ) { if ( !node ) return -1; - node->pdu = coap_new_pdu(); + node->pdu = coap_pdu_init(0, 0, 0, bytes_read); if ( !node->pdu ) { coap_delete_node( node ); return -1; @@ -413,16 +448,21 @@ coap_read( coap_context_t *ctx ) { node->pdu->length = bytes_read; /* finally calculate beginning of data block */ - node->pdu->data = (unsigned char *)node->pdu->hdr + sizeof(coap_hdr_t); { - coap_opt_iterator_t oi; - coap_option_iterator_init(node->pdu, &oi, COAP_OPT_ALL); - while(coap_option_next(&oi)) - node->pdu->data += COAP_OPT_SIZE(oi.option); + coap_opt_t *opt = options_start(node->pdu); + unsigned char cnt; + + /* Note that we cannot use the official options iterator here as + * it eats up the fence posts. */ + for (cnt = node->pdu->hdr->optcnt; cnt; --cnt) + opt = options_next(opt); + + node->pdu->data = (unsigned char *)opt; } /* and add new node to receive queue */ - coap_insert_node( &ctx->recvqueue, node, _order_transaction_id ); + node->id = make_transaction_id(&node->remote, node->pdu); + coap_insert_node(&ctx->recvqueue, node, _order_timestamp); #ifndef NDEBUG { @@ -450,12 +490,12 @@ coap_remove_from_queue(coap_queue_t **queue, coap_tid_t id, coap_queue_t **node) /* replace queue head if PDU's time is less than head's time */ - if ( id == (*queue)->pdu->hdr->id ) { /* found transaction */ + if ( id == (*queue)->id ) { /* found transaction */ *node = *queue; *queue = (*queue)->next; (*node)->next = NULL; /* coap_delete_node( q ); */ - debug("*** removed transaction %u\n", ntohs(id)); + debug("*** removed transaction %u\n", id); return 1; } @@ -464,14 +504,14 @@ coap_remove_from_queue(coap_queue_t **queue, coap_tid_t id, coap_queue_t **node) do { p = q; q = q->next; - } while ( q && id == q->pdu->hdr->id ); + } while ( q && id == q->id ); if ( q ) { /* found transaction */ p->next = q->next; q->next = NULL; *node = q; /* coap_delete_node( q ); */ - debug("*** removed transaction %u\n", ntohs(id)); + debug("*** removed transaction %u\n", id); return 1; } @@ -481,23 +521,20 @@ coap_remove_from_queue(coap_queue_t **queue, coap_tid_t id, coap_queue_t **node) coap_queue_t * coap_find_transaction(coap_queue_t *queue, coap_tid_t id) { - if ( !queue ) - return 0; + while (queue && queue->id != id) + queue = queue->next; - for (; queue; queue = queue->next) { - if (queue->pdu && queue->pdu->hdr && queue->pdu->hdr->id == id) - return queue; - } - return NULL; + return queue; } coap_pdu_t * -coap_new_error_response(coap_queue_t *node, unsigned char code, +coap_new_error_response(coap_pdu_t *request, unsigned char code, coap_opt_filter_t opts) { coap_opt_iterator_t opt_iter; coap_pdu_t *response; - size_t size = sizeof(coap_hdr_t); + size_t size = sizeof(coap_hdr_t) + 4; /* some bytes for fence-post options */ unsigned char buf[2]; + int type; #if COAP_ERROR_PHRASE_LENGTH > 0 char *phrase = coap_response_phrase(code); @@ -507,19 +544,26 @@ coap_new_error_response(coap_queue_t *node, unsigned char code, size += strlen(phrase) + 2; #endif + assert(request); + + /* cannot send ACK if original request was not confirmable */ + type = request->hdr->type == COAP_MESSAGE_CON + ? COAP_MESSAGE_ACK + : COAP_MESSAGE_NON; + /* Estimate how much space we need for options to copy from * request. We always need the Token, for 4.02 the unknown critical * options must be included as well. */ coap_option_clrb(opts, COAP_OPTION_CONTENT_TYPE); /* we do not want this */ coap_option_setb(opts, COAP_OPTION_TOKEN); - coap_option_iterator_init(node->pdu, &opt_iter, opts); + coap_option_iterator_init(request, &opt_iter, opts); while(coap_option_next(&opt_iter)) size += COAP_OPT_SIZE(opt_iter.option); /* Now create the response and fill with options and payload data. */ - response = coap_pdu_init(COAP_MESSAGE_ACK, code, node->pdu->hdr->id, size); + response = coap_pdu_init(type, code, request->hdr->id, size); if (response) { #if COAP_ERROR_PHRASE_LENGTH > 0 if (phrase) @@ -528,7 +572,7 @@ coap_new_error_response(coap_queue_t *node, unsigned char code, #endif /* copy all options */ - coap_option_iterator_init(node->pdu, &opt_iter, opts); + coap_option_iterator_init(request, &opt_iter, opts); while(coap_option_next(&opt_iter)) coap_add_option(response, opt_iter.type, COAP_OPT_LENGTH(opt_iter.option), @@ -641,7 +685,7 @@ handle_request(coap_context_t *context, coap_queue_t *node) { case COAP_REQUEST_GET: if (is_wkc(key)) { /* GET request for .well-known/core */ - debug("create default response for %s\n", COAP_DEFAULT_URI_WELLKNOWN); + info("create default response for %s\n", COAP_DEFAULT_URI_WELLKNOWN); response = wellknown_response(context, node->pdu); } else { /* GET request for any another resource, return 4.04 */ @@ -649,7 +693,8 @@ handle_request(coap_context_t *context, coap_queue_t *node) { debug("GET for unknown resource 0x%02x%02x%02x%02x, return 4.04\n", key[0], key[1], key[2], key[3]); response = - coap_new_error_response(node, COAP_RESPONSE_CODE(404), opt_filter); + coap_new_error_response(node->pdu, COAP_RESPONSE_CODE(404), + opt_filter); } break; @@ -657,13 +702,13 @@ handle_request(coap_context_t *context, coap_queue_t *node) { debug("unhandled request for unknown resource 0x%02x%02x%02x%02x," "return 4.05\n", key[0], key[1], key[2], key[3]); - response = - coap_new_error_response(node, COAP_RESPONSE_CODE(405), opt_filter); + response = coap_new_error_response(node->pdu, COAP_RESPONSE_CODE(405), + opt_filter); } if (!response || (coap_send(context, &node->remote, response) == COAP_INVALID_TID)) { - debug("cannot send response for message %d\n", node->pdu->hdr->id); + warn("cannot send response for transaction %u\n", node->id); coap_delete_pdu(response); } @@ -678,53 +723,36 @@ handle_request(coap_context_t *context, coap_queue_t *node) { if (h) { debug("call custom handler for resource 0x%02x%02x%02x%02x\n", key[0], key[1], key[2], key[3]); - h(context, resource, &node->remote, node->pdu); + h(context, resource, &node->remote, node->pdu, node->id); } else { if (WANT_WKC(node->pdu, key)) { debug("create default response for %s\n", COAP_DEFAULT_URI_WELLKNOWN); response = wellknown_response(context, node->pdu); } else - response = coap_new_error_response(node, COAP_RESPONSE_CODE(405), + response = coap_new_error_response(node->pdu, COAP_RESPONSE_CODE(405), opt_filter); if (!response || (coap_send(context, &node->remote, response) == COAP_INVALID_TID)) { - debug("cannot send response for message %d\n", node->pdu->hdr->id); + debug("cannot send response for transaction %u\n", node->id); coap_delete_pdu(response); } } } static inline void -handle_response(coap_context_t *context, coap_queue_t *sent, - coap_queue_t *rcvd) { - coap_tid_t id = COAP_INVALID_TID; - +handle_response(coap_context_t *context, + coap_queue_t *sent, coap_queue_t *rcvd) { + /* send ACK if rcvd is confirmable (i.e. a separate response) */ - if (rcvd->pdu->hdr->type == COAP_MESSAGE_CON) { - coap_pdu_t *ack; - coap_opt_iterator_t opt_iter; - size_t size = sizeof(coap_hdr_t); - - /* add token size, if present */ - if (coap_check_option(rcvd->pdu, COAP_OPTION_TOKEN, &opt_iter)) - size += COAP_OPT_SIZE(opt_iter.option); - - ack = coap_pdu_init(COAP_MESSAGE_ACK, 0, rcvd->pdu->hdr->id, size); - if (!ack) { - debug("cannot create ACK for confirmable response\n"); - } else { - if (coap_send(context, &rcvd->remote, ack) == COAP_INVALID_TID) { - debug("cannot send ACK for conffirmable response\n"); - coap_delete_pdu(ack); - } - } - } + if (rcvd->pdu->hdr->type == COAP_MESSAGE_CON) + coap_send_ack(context, &rcvd->remote, rcvd->pdu); - /* FIXME: only call response handler when id is valid */ + /* only call response handler when id is valid */ if (context->response_handler) { - context->response_handler(context, id, &rcvd->remote, - sent->pdu, rcvd->pdu); + context->response_handler(context, + &rcvd->remote, sent ? sent->pdu : NULL, + rcvd->pdu, rcvd->id); } } @@ -762,6 +790,8 @@ coap_dispatch( coap_context_t *context ) { case COAP_MESSAGE_ACK: /* find transaction in sendqueue to stop retransmission */ coap_remove_from_queue(&context->sendqueue, rcvd->pdu->hdr->id, &sent); + if (rcvd->pdu->hdr->code == 0) + goto cleanup; break; case COAP_MESSAGE_RST : @@ -769,7 +799,7 @@ coap_dispatch( coap_context_t *context ) { * not only the transaction but also the subscriptions we might * have. */ - fprintf(stderr, "* got RST for transaction %u\n", ntohs(rcvd->pdu->hdr->id) ); + coap_log(LOG_ALERT, "got RST for message %u\n", ntohs(rcvd->pdu->hdr->id)); /* find transaction in sendqueue to stop retransmission */ coap_remove_from_queue(&context->sendqueue, rcvd->pdu->hdr->id, &sent); @@ -784,14 +814,14 @@ coap_dispatch( coap_context_t *context ) { if (coap_option_check_critical(context, rcvd->pdu, opt_filter) == 0) { response = - coap_new_error_response(rcvd, COAP_RESPONSE_CODE(402), opt_filter); + coap_new_error_response(rcvd->pdu, COAP_RESPONSE_CODE(402), opt_filter); if (!response) - debug("coap_dispatch: cannot create error reponse\n"); + warn("coap_dispatch: cannot create error reponse\n"); else { if (coap_send(context, &rcvd->remote, response) == COAP_INVALID_TID) { - debug("coap_dispatch: error sending reponse\n"); + warn("coap_dispatch: error sending reponse\n"); coap_delete_pdu(response); } } diff --git a/net.h b/net.h index 1a944296..566c7837 100644 --- a/net.h +++ b/net.h @@ -115,6 +115,7 @@ typedef struct coap_queue_t { unsigned int timeout; /* the randomized timeout value */ coap_address_t remote; /**< remote address */ + coap_tid_t id; /**< unique transaction id */ coap_pdu_t *pdu; /**< the CoAP PDU to send */ } coap_queue_t; @@ -137,10 +138,10 @@ struct coap_context_t; /** Message handler that is used as call-back in coap_context_t */ typedef void (*coap_response_handler_t)(struct coap_context_t *, - const coap_tid_t id, const coap_address_t *remote, coap_pdu_t *sent, - coap_pdu_t *received); + coap_pdu_t *received, + const coap_tid_t id); /** The CoAP stack's global state is stored in a coap_context_t object */ typedef struct coap_context_t { @@ -207,23 +208,23 @@ coap_tid_t coap_send_confirmed(coap_context_t *context, coap_pdu_t *pdu); /** - * Creates a new RST PDU with specified error @p code. The options + * Creates a new ACK PDU with specified error @p code. The options * specified by the filter expression @p opts will be copied from the - * original request contained in @c node->pdu. Unless @c + * original request contained in @p request. Unless @c * SHORT_ERROR_RESPONSE was defined at build time, the textual reason - * phrase for @p code will be added as payload, with Content-Type @c 0. - * This function returns a pointer to the new response message, or + * phrase for @p code will be added as payload, with Content-Type @c + * 0. This function returns a pointer to the new response message, or * @c NULL on error. The storage allocated for the new message must be - * relased with coap_frree(). + * relased with coap_free(). * - * @param node Specification of the received (confirmable) request. + * @param request Specification of the received (confirmable) request. * @param code The error code to set. * @param opts An option filter that specifies which options to copy * from the original request in @p node. * * @return A pointer to the new message or @c NULL on error. */ -coap_pdu_t *coap_new_error_response(coap_queue_t *node, +coap_pdu_t *coap_new_error_response(coap_pdu_t *request, unsigned char code, coap_opt_filter_t opts); /** @@ -240,6 +241,54 @@ coap_tid_t coap_send(coap_context_t *context, const coap_address_t *dst, coap_pdu_t *pdu); +/** + * Sends an error response with code @p code for request @p request to + * @p dst. @p opts will be passed to coap_new_error_response() to + * copy marked options from the request. This function returns the + * transaction id if the message was sent, or @c COAP_INVALID_TID + * otherwise. + * + * @param context The context to use. + * @param request The original request to respond to. + * @param dst The remote peer that sent the request. + * @param code The reponse code. + * @param opts A filter that specifies the options to copy from the + * @p request. + * + * @return The transaction id if the message was sent, or @c + * COAP_INVALID_TID otherwise. + */ +coap_tid_t coap_send_error(coap_context_t *context, + coap_pdu_t *request, + const coap_address_t *dst, + unsigned char code, + coap_opt_filter_t opts); +/** + * Sends an ACK message with code @c 0 for the specified @p request to + * @p dst. This function returns the corresponding transaction id if + * the message was sent or @c COAP_INVALID_TID on error. + * + * @param context The context to use. + * @param dst The destination address. + * @param request The request to be acknowledged. + * + * @return The transaction id if ACK was sent or @c COAP_INVALID_TID + * on error. + */ +static inline coap_tid_t +coap_send_ack(coap_context_t *context, + const coap_address_t *dst, + coap_pdu_t *request) { + coap_opt_filter_t f; + + if (request && request->hdr->type == COAP_MESSAGE_CON) { + coap_option_filter_clear(f); + return coap_send_error(context, request, dst, 0, f); + } + return COAP_INVALID_TID; +} + + /** Handles retransmissions of confirmable messages */ coap_tid_t coap_retransmit( coap_context_t *context, coap_queue_t *node ); diff --git a/resource.h b/resource.h index 1a60c782..65eab311 100644 --- a/resource.h +++ b/resource.h @@ -31,7 +31,8 @@ /** Definition of message handler function (@sa coap_resource_t). */ typedef void (*coap_method_handler_t) - (coap_context_t *, struct coap_resource_t *, coap_address_t *, coap_pdu_t *); + (coap_context_t *, struct coap_resource_t *, coap_address_t *, coap_pdu_t *, + coap_tid_t); typedef struct coap_attr_t { struct coap_attr_t *next;