-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
nanocoap: added function for finding options #18
Closed
haukepetersen
wants to merge
3
commits into
kaspar030:master
from
haukepetersen:add_nanocoap_findopt
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,7 @@ int coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len) | |
} | ||
|
||
/* parse options */ | ||
pkt->options = (pkt_pos != pkt_end) ? pkt_pos : NULL; | ||
int option_nr = 0; | ||
while (pkt_pos != pkt_end) { | ||
uint8_t option_byte = *pkt_pos++; | ||
|
@@ -109,6 +110,13 @@ int coap_parse(coap_pkt_t *pkt, uint8_t *buf, size_t len) | |
} | ||
} | ||
|
||
/* set payload pointer to first byte after the options in case no payload | ||
* is present, so we can use it as reference to find the end of options | ||
* at a later point in time */ | ||
if (pkt_pos == pkt_end) { | ||
pkt->payload = pkt_end; | ||
} | ||
|
||
DEBUG("coap pkt parsed. code=%u detail=%u payload_len=%u, 0x%02x\n", | ||
coap_get_code_class(pkt), | ||
coap_get_code_detail(pkt), | ||
|
@@ -369,3 +377,68 @@ ssize_t coap_well_known_core_default_handler(coap_pkt_t* pkt, uint8_t *buf, \ | |
|
||
return coap_build_reply(pkt, COAP_CODE_205, buf, len, payload_len); | ||
} | ||
|
||
static size_t _decode_optlen(uint8_t *buf, uint16_t *val) | ||
{ | ||
size_t len = 0; | ||
|
||
if (*val == 13) { | ||
*val += *buf; | ||
len = 1; | ||
} | ||
else if (*val == 14) { | ||
memcpy(val, buf, 2); | ||
*val = htons(*val) + 269; | ||
len = 2; | ||
} | ||
|
||
return len; | ||
} | ||
|
||
static int _parse_opt(uint8_t *optpos, coap_opt_t *opt) | ||
{ | ||
opt->val = optpos + 1; | ||
opt->delta = ((*optpos & 0xf0) >> 4); | ||
opt->len = (*optpos & 0x0f); | ||
|
||
/* make sure delta and len raw values are valid */ | ||
if ((opt->delta == 15) || (opt->len == 15)) { | ||
return -1; | ||
} | ||
|
||
opt->val += _decode_optlen(opt->val, &opt->delta); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. through |
||
opt->val += _decode_optlen(opt->val, &opt->len); | ||
|
||
return (opt->val - optpos) + opt->len; | ||
} | ||
|
||
uint8_t *coap_find_option(uint8_t *payload_pos, uint8_t *bufpos, | ||
coap_opt_t *opt, uint16_t optnum) | ||
{ | ||
assert(opt); | ||
|
||
/* check if we reached the end of options */ | ||
if (!bufpos || (*bufpos == 0xff)) { | ||
return NULL; | ||
} | ||
|
||
uint16_t delta = 0; | ||
|
||
do { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add packet length checks here? Seems like a bogus header leads to crashes here. |
||
if (bufpos >= payload_pos) { | ||
return NULL; | ||
} | ||
int res = _parse_opt(bufpos, opt); | ||
if (res < 0) { | ||
return NULL; | ||
} | ||
bufpos += res; | ||
delta += opt->delta; | ||
} while (delta < optnum); | ||
|
||
if (delta != optnum) { | ||
bufpos = NULL; | ||
} | ||
|
||
return bufpos; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this might read past packet length