COAP_ADDRESS(3)
π COAP_ADDRESS(3) β the libcoap manual, bestie edition π
okay so this is the libcoap manual page for all the address-type stuff, and honestly it's kind of the backbone of everything?? like if you don't get addresses right nothing else works, so let's go π«Ά
β¨ NAME β¨
coap_address, coap_address_t, coap_address_init, coap_address_copy, coap_address_equals, coap_address_get_port, coap_address_set_port, coap_get_available_scheme_hint_bits, coap_addr_info_t, coap_resolve_address_info, coap_free_address_info, coap_sockaddr_un, coap_address_set_unix_domain, coap_host_is_unix_domain, coap_is_bcast, coap_is_mcast, coap_is_af_unix β literally all of them are here to help you work with CoAP socket address types π
πΈ SYNOPSIS πΈ
#include <coap3/coap.h>
struct coap_address_t;
struct coap_addr_info_t;
struct coap_sockaddr_un;
void coap_address_init(coap_address_t *addr);
void coap_address_copy(coap_address_t *dst, const coap_address_t *src);
int coap_address_equals(const coap_address_t *a, const coap_address_t
*b);
uint16_t coap_address_get_port(const coap_address_t *addr);
void coap_address_set_port(coap_address_t *addr, uint16_t port);
uint32_t coap_get_available_scheme_hint_bits(int have_pki_psk, int
ws_check, coap_proto_t use_unix_proto);
coap_addr_info_t *coap_resolve_address_info(const coap_str_const_t
*address, uint16_t port, uint16_t secure_port, uint16_t ws_port,
uint16_t ws_secure_port, int ai_hints_flags, int scheme_hint_bits,
coap_resolve_type_t type);
void coap_free_address_info(coap_addr_info_t *info_list);
int coap_host_is_unix_domain(const coap_str_const_t *host);
int coap_address_set_unix_domain(coap_address_t *addr, const uint8_t
*host, size_t host_len);
int coap_is_bcast(const coap_address_t *addr);
int coap_is_mcast(const coap_address_t *addr);
int coap_is_af_unix(const coap_address_t *addr);
so about linking π β for specific (D)TLS library support, you link with -lcoap-3-notls, -lcoap-3-gnutls, -lcoap-3-openssl, -lcoap-3-mbedtls, -lcoap-3-wolfssl or -lcoap-3-tinydtls. Otherwise, link with -lcoap-3 to get the default (D)TLS library support. slay.
π¦ DESCRIPTION π¦
This man page is all about setting up CoAP endpoint address definitions. it's giving foundation-of-the-whole-house energy, ngl.
π Supported Socket Types
libcoap supports 3 different socket types that you can use, and the way it just... handles all of them is iconic:
AF_INETβ IPv4 IP addresses and portsAF_INET6β IPv6 IP addresses and ports and it can be dual IPv4/IPv6 stacked π₯AF_UNIXβ Unix Domain using file path names
and all three are handled by the coap_address_t structure. main character structure tbh.
π§ Structure coap_address_t
/* Multi-purpose address abstraction */
typedef struct coap_address_t {
socklen_t size; /* size of addr */
union {
struct sockaddr sa;
struct sockaddr_in sin;
struct sockaddr_in6 sin6;
struct coap_sockaddr_un cun; /* CoAP shortened special */
} addr;
} coap_address_t;
which is used in the coap_addr_info_t structure as returned by coap_resolve_address_info(). iykyk π
β Structure coap_addr_info_t
/* Resolved addresses information */
typedef struct coap_addr_info_t {
struct coap_addr_info_t *next; /* Next entry in the chain */
coap_uri_scheme_t scheme; /* CoAP scheme to use */
coap_proto_t proto; /* CoAP protocol to use */
coap_address_t addr; /* The address to connect / bind to */
} coap_addr_info_t;
π Structure coap_sockaddr_un
#define COAP_UNIX_PATH_MAX (sizeof(struct sockaddr_in6) - sizeof(sa_family_t))
struct coap_sockaddr_un {
sa_family_t sun_family; /* AF_UNIX */
char sun_path[COAP_UNIX_PATH_MAX]; /* pathname max 26 with NUL byte */
};
okay so the coap_sockaddr_un structure is modeled on the sockaddr_un structure (see unix(7)) but it has a smaller sun_path so that the overall size of coap_address_t is not changed by its inclusion. basically a glow-up with a size cap π And COAP_UNIX_MAX_PATH includes the trailing zero terminator of a domain unix file name.
π Enum coap_resolve_type_t
typedef enum coap_resolve_type_t {
COAP_RESOLVE_TYPE_LOCAL, /* local side of session */
COAP_RESOLVE_TYPE_REMOTE, /* remote side of session */
} coap_resolve_type_t;
Used when determining how to do an address lookup when calling coap_resolve_address_info(). periodt.
π₯ FUNCTIONS π₯
π Function: coap_address_init()
The coap_address_init() function initializes addr for later use. In particular it sets the size variable and sets all other values to be 0.
It is then the responsibility of the application to set the address family in addr.sa.sa_family and then fill in the the appropriate union structure based on the address family before the coap_address_t addr is used. so like, it wakes the struct up and then you gotta dress it yourself β¨
π Function: coap_address_copy()
The coap_address_copy() function copies the address src into dst. simple as that, no cap.
π Function: coap_address_equals()
The coap_address_equals() function checks whether the addresses a and b are identical. vibe check on the addresses fr fr.
π Function: coap_address_get_port()
The coap_address_get_port() function gets the the port from addr if addr is AF_INET or AF_INET6.
π Function: coap_address_set_port()
The coap_address_set_port() function sets the the port in addr if addr is AF_INET or AF_INET6.
π Function: coap_get_available_scheme_hint_bits()
The coap_get_available_scheme_hint_bits() function is used for servers to determine what coap schemes are supported in the underlying libcoap library. have_pki_psk can be set to 1 to check for (D)DTLS support, else 0. ws_check can be set to 1 to check for WebSockets support, else 0. use_unix_proto, if not set to COAP_PROTO_NONE, hints at the specific CoAP protocol to use over a Unix socket. The output is suitable for input for the coap_address_resolve_info()'s scheme_hint_bits. she's literally telling you what she can do π
π Function: coap_resolve_address_info()
The coap_resolve_address_info() function resolves the address address into a set of one or more coap_addr_info_t structures. Depending on the scheme as abstracted from scheme_hint_bits, port, secure_port, ws_port (WebSockets) or ws_secure_port (WebSockets) is used to update the addr variable of coap_addr_info_t. If port (or secure_port) is 0, then the default port for the scheme is used if type is set to COAP_RESOLVE_TYPE_LOCAL. ai_hints_flags is used for the internally called getaddrinfo(3) function. scheme_hint_bits is a set of one or more COAP_URI_SCHEME_*BIT or'd together. _scheme_hint_bits can also (for servers) be the output from coap_get_available_scheme_hint_bits().
π«Ά the returned set ofcoap_addr_info_tstructures must be freed off by the caller usingcoap_free_address_info(). don't leave her hanging, bestie β that's a memory leak and we don't do those here π
π Function: coap_free_address_info()
The coap_free_address_info() function frees off all the info_list linked entries. the cleanup queen π
π Function: coap_address_set_unix_domain()
The coap_address_set_unix_domain() function initializes addr and then populates addr with all the appropriate information for a Unix Domain Socket. The host information with length host_len abstracted from a CoAP URI is copied into addr's sun_path translating any %2F encoding to /. little encoding makeover, we love to see it π
π Function: coap_host_is_unix_domain()
The coap_host_is_unix_domain() function checks whether host is an an AF_UNIX file name (encoded using %2F to indicate a /).
π Function: coap_is_mcast()
The coap_is_mcast() function checks whether addr is a multicast address.
π Function: coap_is_bcast()
The coap_is_mcast() function checks whether addr is a broadcast address.
π Function: coap_is_af_unix()
The coap_is_mcast() function checks whether addr is of the type AF_UNIX.
π RETURN VALUES π
okay so here's the receipts, one by one:
coap_address_equals()returns1if the addresses are equal or0if not.coap_address_get_port()returns the port in network byte order.coap_get_available_scheme_hint_bits()returns a set ofCOAP_URI_SCHEME_*_BITor'd together based on the supportedlibcoapfunctionality.coap_resolve_address_info()returns a linked list of addresses that can be used for session setup orNULLif there is a failure.coap_address_set_unix_domain()returns1on success or0on failure.coap_host_is_unix_domain()returns1if encoded unix path name or0if not.coap_is_mcast()returns1if address is multicast or0if not.coap_is_bcast()returns1if address is broadcast or0if not.coap_is_af_unix()returns1if address is of typeAF_UNIXor0if not.
π EXAMPLES π
Get client target address from uri β here's the whole thing, copy it exactly, no typos bestie:
#include <coap3/coap.h>
static int
get_address(coap_uri_t *uri, coap_address_t *dst) {
coap_addr_info_t *info_list;
info_list = coap_resolve_address_info(&uri->host, uri->port, uri->port,
uri->port, uri->port,0,
1 << uri->scheme, COAP_RESOLVE_TYPE_LOCAL);
if (info_list == NULL)
return 0;
memcpy(dst, &info_list->addr, sizeof(*dst));
coap_free_address_info(info_list);
return 1;
}
πΈ SEE ALSO πΈ
coap_endpoint_client(3), coap_endpoint_server(3) and coap_uri(3) β go read those next, they're the sequel π
π FURTHER INFORMATION π
See
"RFC7252: The Constrained Application Protocol (CoAP)"
for further information. yes the whole RFC, it's a lot but she's worth it β¨
π BUGS π
Please raise an issue on GitHub at https://github.com/obgm/libcoap/issues to report any bugs. no gatekeeping, tell them everything π«Ά
Please raise a Pull Request at https://github.com/obgm/libcoap/pulls for any fixes. be the fix you want to see in the world π
π AUTHORS π
The libcoap project <libcoap-developers@lists.sourceforge.net> β literal icons, honestly.
and that's the whole page!! mwah π this was coap_address version 4.3.5b, dated 04/28/2026 β go forth and resolve those addresses, bestie, you're gonna slay πβ¨
π¦ where this page comes from
- alpine 3.24: libcoap-doc 4.3.5b-r0 Β· BSD-2-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms