---
name: "libcidr"
section: "3"
platform: "linux"
package: "libcidr-dev"
version: "1.2.3-3+b2"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/libcidr"
---

# libcidr(3)

CIDR-style IP address manipulation functions

## NAME

**libcidr** – CIDR-style IP address manipulation functions

## SYNOPSIS

**#include \<libcidr.h\>**

Library libcidr (-lcidr)

## SUMMARY

**libcidr** is a C library implementing a variety of functions to manipulate IP addresses and netblocks. It handles both IPv4 and IPv6 networks, will transform them to and from text strings in a variety of formats, cram them into and lever them out of *in\_addr* and *in6\_addr* structures, and give you all sorts of stats about them. Functions are provided also to tell you a few things about the network any given CIDR block is in, what its parent and child networks are, and so on. If you're really nice, **libcidr** may even cook you breakfast.

The primary documentation for **libcidr** is maintained elsewhere, with a full description of the functions, their options, and their return values. Your installation probably has a copy of the reference manual installed somewhere, perhaps in */usr/share/libcidr/docs*. Documentation is also available on the webpage (see below). This manpage is provided as a quick reference.

## DESCRIPTION OF FUNCTIONS

- **CIDR** **\*cidr\_addr\_broadcast(const** **CIDR** **\*cidr)**

  Return a *CIDR* structure describing the broadcast address of the network represented by *cidr*.

- **CIDR** **\*cidr\_addr\_hostmax(const** **CIDR** **\*cidr)**

  Return a *CIDR* structure describing the "highest" host in the subnet represented by *cidr*.

- **CIDR** **\*cidr\_addr\_hostmin(const** **CIDR** **\*cidr)**

  Return a *CIDR* structure describing the "lowest" host in the subnet represented by *cidr*.

- **CIDR** **\*cidr\_addr\_network(const** **CIDR** **\*cidr)**

  Return a *CIDR* structure describing the network address of the network represented by *cidr*.

- **CIDR** **\*cidr\_alloc(void)**

  Allocate and initialize a *CIDR* structure.

- **int** **cidr\_contains(const** **CIDR** **\*big,** **const** **CIDR** **\*little)**

  Determine whether *little* is a CIDR block entirely contained within the block described by *big*. Return 0 if so, else non-zero.

- **CIDR** **\*cidr\_dup(const** **CIDR** **\*cidr)**

  Return a duplicate of *cidr*.

- **int** **cidr\_equals(const** **CIDR** **\*first,** **const** **CIDR** **\*second)**

  Determine whether *first* and *second* describe the same CIDR block. Return 0 if they do, else non-zero.

- **void** **cidr\_free(CIDR** **\*cidr)**

  Free the memory used by a *CIDR* *\**.

- **CIDR** **\*cidr\_from\_inaddr(const** **struct** **in\_addr** **\*in)**

  Generate a *CIDR* *\** based on a *struct* *in\_addr*.

- **CIDR** **\*cidr\_from\_in6addr(const** **struct** **in6\_addr** **\*in)**

  Generate a *CIDR* *\** based on a *struct* *in6\_addr*.

- **CIDR** **\*cidr\_from\_str(const** **char** **\*str)**

  Parse *str* and return a *CIDR* *\** representing it.

- **uint8\_t** **\*cidr\_get\_addr(const** **CIDR** **\*cidr)**

  Return the address in an array of binary octets.

- **uint8\_t** **\*cidr\_get\_mask(const** **CIDR** **\*cidr)**

  Return the netmask in an array of binary octets.

- **int** **cidr\_get\_pflen(const** **CIDR** **\*cidr)**

  Return the length of the netmask prefix of *cidr*.

- **int** **cidr\_get\_proto(const** **CIDR** **\*cidr)**

  Return the protocol of an address.

- **int** **cidr\_is\_v4mapped(const** **CIDR** **\*addr)**

  Checks if an IPv6 address is in the IPv4-mapped range.

- **CIDR** **\*\*cidr\_net\_subnets(const** **CIDR** **\*cidr)**

  Return a 2-element array of *CIDR* *\** *'s* representing the two immediate subnets of the network described by *cidr*. That is to say, the two networks, within *cidr*, which have a netmask 1 bit longer.

- **CIDR** **\*cidr\_net\_supernet(const** **CIDR** **\*cidr)**

  Return a *CIDR* *\** describing the "parent" network of *cidr*. That is, the network with a netmask 1 bit shorter, which wholly contains *cidr*.

- **const** **char** **\*cidr\_numaddr(const** **CIDR** **\*cidr)**

  Return a pointer to a static string giving the number of total addresses available in the network described by *cidr*. Do not attempt to alter or [free(3)](/free/3.md) this string.

- **const** **char** **\*cidr\_numaddr\_pflen(int** **pflen)**

  Return a pointer to a static string giving the number of total addresses available in a network having a prefix length of *pflen*. Do not attempt to alter or [free(3)](/free/3.md) this string. You probably don't want to call this function manually often.

- **const** **char** **\*cidr\_numhost(const** **CIDR** **\*cidr)**

  Return a pointer to a static string giving the number of addresses available for hosts available in the network described by *cidr*. Do not attempt to alter or [free(3)](/free/3.md) this string.

- **const** **char** **\*cidr\_numhost\_pflen(int** **pflen)**

  Return a pointer to a static string giving the number of addresses available for hosts in a network having a prefix length of *pflen*. Do not attempt to alter or [free(3)](/free/3.md) this string. You probably don't want to call this function manually often.

- **struct** **in\_addr** **\*cidr\_to\_inaddr(const** **CIDR** **\*cidr,** **struct** **in\_addr** **\*in)**

  Fill in a *struct* *in\_addr* with the address given in *cidr*. A user-supplied *struct* *in\_addr* is used if passed in; if a null pointer is passed, a new one will be allocated, filled in, and returned.

- **struct** **in6\_addr** **\*cidr\_to\_in6addr(const** **CIDR** **\*cidr,** **struct** **in6\_addr** **\*in)**

  Fill in a *struct* *in6\_addr* with the address given in *cidr*. A user-supplied *struct* *in6\_addr* is used if passed in; if a null pointer is passed, a new one will be allocated, filled in, and returned.

- **char** **\*cidr\_to\_str(const** **CIDR** **\*cidr,** **int** **flags)**

  Return a string version of the passed-in *cidr*, as described by *flags*. By default, it will print in the standard address form for whatever address family *cidr* represents, followed by a slash, and then the prefix length of the netmask.

- **const** **char** **\*cidr\_version(void)**

  Return a static string containing version information.

## AUTHOR

**libcidr** is written and maintained by Matthew Fuller ⟨fullermd@over-yonder.net⟩. Its home on the web is at ⟨http://www.over-yonder.net/~fullermd/projects/libcidr⟩; check there for updates and additional documentation.
