From 842c2467c01069836b9329fff457ca3e5d2fd3eb Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Fri, 12 Apr 2019 16:35:04 +0200 Subject: [PATCH] address.[hc]: Add convenience functions to get/set port The layout of struct coap_address_t is platform-dependent. This change introduces getter/setter functions to retrieve and modify the port component: uint16_t coap_address_get_port(const coap_address_t *addr) void coap_address_set_port(coap_address_t *addr, uint16_t port) The respective port is given in host byte order and will be converted to/from network byte order automatically. --- include/coap2/address.h | 42 +++++++++++++++++++++++++++++++++++++++++ src/address.c | 27 ++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/include/coap2/address.h b/include/coap2/address.h index a77db792..857fd8c3 100644 --- a/include/coap2/address.h +++ b/include/coap2/address.h @@ -30,6 +30,22 @@ typedef struct coap_address_t { ip_addr_t addr; } coap_address_t; +/** + * Returns the port from @p addr in host byte order. + */ +COAP_STATIC_INLINE uint16_t +coap_address_get_port(const coap_address_t *addr) { + return ntohs(addr->port); +} + +/** + * Sets the port field of @p addr to @p port (in host byte order). + */ +COAP_STATIC_INLINE void +coap_address_set_port(coap_address_t *addr, uint16_t port) { + addr->port = htons(port); +} + #define _coap_address_equals_impl(A, B) \ ((A)->port == (B)->port \ && (!!ip_addr_cmp(&(A)->addr,&(B)->addr))) @@ -47,6 +63,22 @@ typedef struct coap_address_t { uint16_t port; } coap_address_t; +/** + * Returns the port from @p addr in host byte order. + */ +COAP_STATIC_INLINE uint16_t +coap_address_get_port(const coap_address_t *addr) { + return uip_ntohs(addr->port); +} + +/** + * Sets the port field of @p addr to @p port (in host byte order). + */ +COAP_STATIC_INLINE void +coap_address_set_port(coap_address_t *addr, uint16_t port) { + addr->port = uip_htons(port); +} + #define _coap_address_equals_impl(A,B) \ ((A)->port == (B)->port \ && uip_ipaddr_cmp(&((A)->addr),&((B)->addr))) @@ -68,6 +100,16 @@ typedef struct coap_address_t { } addr; } coap_address_t; +/** + * Returns the port from @p addr in host byte order. + */ +uint16_t coap_address_get_port(const coap_address_t *addr); + +/** + * Set the port field of @p addr to @p port (in host byte order). + */ +void coap_address_set_port(coap_address_t *addr, uint16_t port); + /** * Compares given address objects @p a and @p b. This function returns @c 1 if * addresses are equal, @c 0 otherwise. The parameters @p a and @p b must not be diff --git a/src/address.c b/src/address.c index b53f20de..42567d36 100644 --- a/src/address.c +++ b/src/address.c @@ -27,6 +27,33 @@ #define IN_MULTICAST(Address) (0) #endif /* RIOT_VERSION */ +uint16_t +coap_address_get_port(const coap_address_t *addr) { + assert(addr != NULL); + switch (addr->addr.sa.sa_family) { + case AF_INET: return ntohs(addr->addr.sin.sin_port); + case AF_INET6: return ntohs(addr->addr.sin6.sin6_port); + default: /* undefined */ + ; + } + return 0; +} + +void +coap_address_set_port(coap_address_t *addr, uint16_t port) { + assert(addr != NULL); + switch (addr->addr.sa.sa_family) { + case AF_INET: + addr->addr.sin.sin_port = htons(port); + break; + case AF_INET6: + addr->addr.sin6.sin6_port = htons(port); + break; + default: /* undefined */ + ; + } +} + int coap_address_equals(const coap_address_t *a, const coap_address_t *b) { assert(a); assert(b);