Skip to content

Commit

Permalink
Updates microcoap to be able to handle tokens in built packets.
Browse files Browse the repository at this point in the history
These are a requirement for correct responses to packets with tokens in.
  • Loading branch information
Heds Simons committed Jul 24, 2014
1 parent 947a5b7 commit 9e6c22b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
34 changes: 24 additions & 10 deletions coap.c
Expand Up @@ -248,30 +248,39 @@ int coap_build(uint8_t *buf, size_t *buflen, const coap_packet_t *pkt)
uint8_t *p;
uint16_t running_delta = 0;
// build header
if (*buflen < 4)
if (*buflen < 4 + pkt->hdr.tkl)
return COAP_ERR_BUFFER_TOO_SMALL;

buf[0] = (pkt->hdr.ver & 0x03) << 6;
buf[0] |= (pkt->hdr.t & 0x03) << 4;
buf[0] |= (pkt->hdr.tkl & 0x0F);

buf[1] = pkt->hdr.code;

buf[2] = pkt->hdr.id[0];
buf[3] = pkt->hdr.id[1];

// inject options
// inject token
p = buf + 4;
if ((pkt->hdr.tkl > 0) && (pkt->hdr.tkl != pkt->tok.len))
return COAP_ERR_UNSUPPORTED;

if (pkt->hdr.tkl > 0)
memcpy(p, pkt->tok.p, pkt->hdr.tkl);

// inject options
p += pkt->hdr.tkl;

for (i=0;i<pkt->numopts;i++)
{
uint8_t delta;
if (p-buf > *buflen)
return COAP_ERR_BUFFER_TOO_SMALL;
delta = pkt->opts[i].num - running_delta;
if (delta > 12)
if (delta > 12) {
return COAP_ERR_UNSUPPORTED; // FIXME
if (pkt->opts[i].buf.len > 12)
}
if (pkt->opts[i].buf.len > 12) {
return COAP_ERR_UNSUPPORTED; // FIXME
}
*p++ = (delta << 4) | (pkt->opts[i].buf.len & 0x0F);
if ((p+pkt->opts[i].buf.len) - buf > *buflen)
return COAP_ERR_BUFFER_TOO_SMALL;
Expand All @@ -295,15 +304,21 @@ int coap_build(uint8_t *buf, size_t *buflen, const coap_packet_t *pkt)
return 0;
}

int coap_make_response(coap_rw_buffer_t *scratch, coap_packet_t *pkt, const uint8_t *content, size_t content_len, uint8_t msgid_hi, uint8_t msgid_lo, coap_responsecode_t rspcode, coap_content_type_t content_type)
int coap_make_response(coap_rw_buffer_t *scratch, coap_packet_t *pkt, const uint8_t *content, size_t content_len, uint8_t msgid_hi, uint8_t msgid_lo, const coap_buffer_t* tok, coap_responsecode_t rspcode, coap_content_type_t content_type)
{
pkt->hdr.ver = 0x01;
pkt->hdr.t = COAP_TYPE_ACK;
pkt->hdr.tkl = 0;
pkt->hdr.code = rspcode;
pkt->hdr.id[0] = msgid_hi;
pkt->hdr.id[1] = msgid_lo;
pkt->numopts = 1;
pkt->numopts = 0;

// need token in response
if (tok) {
pkt->hdr.tkl = tok->len;
pkt->tok = *tok;
}

// safe because 1 < MAXOPT
pkt->opts[0].num = COAP_OPTION_CONTENT_FORMAT;
Expand All @@ -313,7 +328,6 @@ int coap_make_response(coap_rw_buffer_t *scratch, coap_packet_t *pkt, const uint
scratch->p[0] = ((uint16_t)content_type & 0xFF00) >> 8;
scratch->p[1] = ((uint16_t)content_type & 0x00FF);
pkt->opts[0].buf.len = 2;

pkt->payload.p = content;
pkt->payload.len = content_len;
return 0;
Expand Down Expand Up @@ -350,7 +364,7 @@ int coap_handle_req(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_
ep++;
}

coap_make_response(scratch, outpkt, NULL, 0, inpkt->hdr.id[0], inpkt->hdr.id[1], COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE);
coap_make_response(scratch, outpkt, NULL, 0, inpkt->hdr.id[0], inpkt->hdr.id[1], 0, COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE);

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion coap.h
Expand Up @@ -142,7 +142,7 @@ int coap_buffer_to_string(char *strbuf, size_t strbuflen, const coap_buffer_t *b
const coap_option_t *coap_findOptions(const coap_packet_t *pkt, uint8_t num, uint8_t *count);
int coap_build(uint8_t *buf, size_t *buflen, const coap_packet_t *pkt);
void coap_dump(const uint8_t *buf, size_t buflen, bool bare);
int coap_make_response(coap_rw_buffer_t *scratch, coap_packet_t *pkt, const uint8_t *content, size_t content_len, uint8_t msgid_hi, uint8_t msgid_lo, coap_responsecode_t rspcode, coap_content_type_t content_type);
int coap_make_response(coap_rw_buffer_t *scratch, coap_packet_t *pkt, const uint8_t *content, size_t content_len, uint8_t msgid_hi, uint8_t msgid_lo, const coap_buffer_t* tok, coap_responsecode_t rspcode, coap_content_type_t content_type);
int coap_handle_req(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt);

#endif
Expand Down

0 comments on commit 9e6c22b

Please sign in to comment.