Skip to content

Commit

Permalink
fixup! improve endpoint comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
kb2ma committed Dec 17, 2017
1 parent b322b31 commit 692bd83
Showing 1 changed file with 15 additions and 25 deletions.
40 changes: 15 additions & 25 deletions sys/net/application_layer/gcoap/gcoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static size_t _handle_req(coap_pkt_t *pdu, uint8_t *buf, size_t len,
sock_udp_ep_t *remote);
static ssize_t _finish_pdu(coap_pkt_t *pdu, uint8_t *buf, size_t len);
static void _expire_request(gcoap_request_memo_t *memo);
static int _cmp_endpoints(const sock_udp_ep_t *ep1, const sock_udp_ep_t *ep2);
static bool _endpoints_equal(const sock_udp_ep_t *ep1, const sock_udp_ep_t *ep2);
static void _find_req_memo(gcoap_request_memo_t **memo_ptr, coap_pkt_t *pdu,
const sock_udp_ep_t *remote);
static void _find_resource(coap_pkt_t *pdu, coap_resource_t **resource_ptr,
Expand Down Expand Up @@ -359,8 +359,8 @@ static void _find_req_memo(gcoap_request_memo_t **memo_ptr, coap_pkt_t *src_pdu,

if (coap_get_token_len(memo_pdu) == cmplen) {
memo_pdu->token = &memo_pdu->hdr->data[0];
if (memcmp(src_pdu->token, memo_pdu->token, cmplen) == 0
&& _cmp_endpoints(&memo->remote_ep, remote) == 0) {
if ((memcmp(src_pdu->token, memo_pdu->token, cmplen) == 0)
&& _endpoints_equal(&memo->remote_ep, remote)) {
*memo_ptr = memo;
break;
}
Expand Down Expand Up @@ -466,32 +466,22 @@ static ssize_t _write_options(coap_pkt_t *pdu, uint8_t *buf, size_t len)
return bufpos - buf;
}

/*
* Compares the addresses and ports for two sock UDP endpoints.
*
* ep1[in] -- endpoint
* ep2[in] -- endpoint
*
* return Result of memcmp (-1, 0, 1) if address family and port match;
* otherwise -2.
*/
static int _cmp_endpoints(const sock_udp_ep_t *ep1, const sock_udp_ep_t *ep2)
static bool _endpoints_equal(const sock_udp_ep_t *ep1, const sock_udp_ep_t *ep2)
{
size_t cmplen;
if (ep1->family != ep2->family) {
return false;
}
if (ep1->port != ep2->port) {
return false;
}

switch (ep1->family) {
case AF_INET6:
cmplen = 16;
break;
return memcmp(&ep1->addr.ipv6[0], &ep2->addr.ipv6[0], 16) == 0;
case AF_INET:
cmplen = 4;
break;
default:
return -2;
}
return (ep2->family == ep1->family && ep2->port == ep1->port)
? memcmp(&ep1->addr.ipv6[0], &ep2->addr.ipv6[0], cmplen)
: -2;
return ep1->addr.ipv4_u32 == ep2->addr.ipv4_u32;
}
return false;
}

/*
Expand All @@ -512,7 +502,7 @@ static int _find_observer(sock_udp_ep_t **observer, sock_udp_ep_t *remote)
if (_coap_state.observers[i].family == AF_UNSPEC) {
empty_slot = i;
}
else if (_cmp_endpoints(&_coap_state.observers[i], remote) == 0) {
else if (_endpoints_equal(&_coap_state.observers[i], remote)) {
*observer = &_coap_state.observers[i];
break;
}
Expand Down

0 comments on commit 692bd83

Please sign in to comment.