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.
This commit is contained in:
Olaf Bergmann authored and Jon Shallow committed 2020-04-24 20:16:10 +01:00
1 parent 391e198ab5
commit 842c2467c0
2 files changed
+69

No files matched your search

+42
View File
@@ -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
+27
View File
@@ -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);