COAP_ADDRESS(3)

πŸ’… COAP_ADDRESS πŸ’…

okay so bestie, welcome to the libcoap Manual page for coap_address β€” the one that teaches you all about CoAP endpoint address types. This is from libcoap, the libcoap project's very own manual, and we're gonna go through it together. No cap, it's a lot, but it's giving "foundational" and you're gonna slay it. 🫢


✨ 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 β€” Work with CoAP Socket Address Types

like, all of these. every single one. πŸ’…


🌸 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);

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 and you get the default (D)TLS library support. pick your fighter πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

This man page is hyper-focused on one thing: setting up CoAP endpoint address definitions. that's the whole vibe.

🌈 Supported Socket Types

Libcoap supports 3 different socket types that can be used, and honestly? iconic lineup:

  • AF_INET β€” IPv4 IP addresses and ports
  • AF_INET6 β€” IPv6 IP addresses and ports and can be dual IPv4/IPv6 stacked
  • AF_UNIX β€” Unix Domain using file path names

...which are all handled by the coap_address_t structure. one struct to hold them all, main character energy fr.

πŸŽ€ 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(). see how it all connects? obsessed. ✨

πŸ¦‹ 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 */
};

So the coap_sockaddr_un structure is modeled on the sockaddr_un structure (peep unix(7) bestie) but it has a smaller sun_path, so that the overall size of coap_address_t doesn't get changed by its inclusion. lowkey clever. 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(). iykyk. πŸ’…


πŸŽ€ 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.

Then it's your responsibility (yes, yours, bestie) to set the address family in addr.sa.sa_family and then fill in the appropriate union structure based on the address family before the coap_address_t addr is used. don't skip this step, fr. 🫢

Function: coap_address_copy()

The coap_address_copy() function copies the address src into dst. that's it. that's the whole function. iconic efficiency.

Function: coap_address_equals()

The coap_address_equals() function checks whether the addresses a and b are identical.

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. (yes, "the the" β€” we keep it real here πŸ’€)

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. slay.

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 of coap_addr_info_t structures must be freed off by the caller using coap_free_address_info(). not optional, bestie. memory leaks are not the vibe check you want. 😭

Function: coap_free_address_info()

The coap_free_address_info() function frees off all the info_list linked entries. every last one. 🧹

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 /. cute little decoder moment. πŸ’–

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 /). (again with the double "an an" β€” keeping the original energy πŸ’€)

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.

πŸ™ˆ lowkey the original manpage says coap_is_mcast() right there in the coap_is_bcast() section β€” we're reproducing it exactly as written, no cap. that's just how the source text is, bestie.

Function: coap_is_af_unix()

The coap_is_mcast() function checks whether addr is of the type AF_UNIX.

same deal here, we're keeping the text character for character πŸ’…

⭐ RETURN VALUES ⭐

  • coap_address_equals() returns 1 if the addresses are equal or 0 if not.
  • coap_address_get_port() returns the port in network byte order.
  • coap_get_available_scheme_hint_bits() returns a set of COAP_URI_SCHEME_*_BIT or'd together based on the supported libcoap functionality.
  • coap_resolve_address_info() returns a linked list of addresses that can be used for session setup or NULL if there is a failure.
  • coap_address_set_unix_domain() returns 1 on success or 0 on failure.
  • coap_host_is_unix_domain() returns 1 if encoded unix path name or 0 if not.
  • coap_is_mcast() returns 1 if address is multicast or 0 if not.
  • coap_is_bcast() returns 1 if address is broadcast or 0 if not.
  • coap_is_af_unix() returns 1 if address is of type AF_UNIX or 0 if not.

1 or 0, babes. binary queen behavior. πŸ‘‘


πŸ’– EXAMPLES πŸ’–

Get client target address from uri β€” here's the whole thing, exactly:

#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 how it frees the info_list at the end? that's the self-care. 🌸🫢


πŸ”₯ SEE ALSO πŸ”₯

coap_endpoint_client(3), coap_endpoint_server(3) and coap_uri(3)


🌈 FURTHER INFORMATION 🌈

See

"RFC7252: The Constrained Application Protocol (CoAP)"

for further information. go read it, bestie, it's giving lore. πŸ“šβœ¨


πŸ’€ BUGS πŸ’€

Please raise an issue on GitHub at https://github.com/obgm/libcoap/issues to report any bugs.

Please raise a Pull Request at https://github.com/obgm/libcoap/pulls for any fixes. we love a contributor moment πŸ’œ


πŸ‘‘ AUTHORS πŸ‘‘

The libcoap project <libcoap-developers@lists.sourceforge.net>


okay that's the whole page, you made it πŸŽ‰ this was coap_address from libcoap 4.3.5, dated 08/24/2026 β€” love you, go forth and resolve some addresses responsibly πŸ’…πŸ’–βœ¨

πŸ“¦ where this page comes from