---
name: "rgxg_net_cidr_string"
section: "3"
platform: "linux"
package: "librgxg-dev"
version: "0.1.2-7"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/rgxg_net_cidr_string"
---

# rgxg\_net\_cidr\_string(3)

create regex that matches all addresses of the given CIDR block

## NAME

rgxg\_net\_cidr\_string, rgxg\_net\_cidr\_ipv4, rgxg\_net\_cidr\_ipv6 - create regex that matches all addresses of the given CIDR block

## SYNOPSIS

```
#include <rgxg/net.h>
int rgxg_net_cidr_string (const char *cidr, char **endptr, char *regex,
rgxg_options_t options);
int rgxg_net_cidr_ipv4 (const rgxg_ipv4_t *address, int prefix, char *regex,
rgxg_options_t options);
int rgxg_net_cidr_ipv6 (const rgxg_ipv6_t *address, int prefix, char *regex,
rgxg_options_t options);
```

## DESCRIPTION

**rgxg\_net\_cidr\_string**() generates the (extended) regular expression that matches all addresses of the CIDR block given as string in *cidr* (e.g. "192.168.0.0/24" or "2001:db8:aaaa::/64"). *cidr* must be valid and not NULL.

If *endptr* is not NULL, **rgxg\_net\_cidr\_string**() stores on success the address of the first character which is not part of the CIDR block in *\*endptr*. If there is no error and *\*\*endptr* is '\\0', then the entire string has been parsed. The *\*endptr* values in case of an error are described in the **ERRORS** section below.

**rgxg\_net\_cidr\_ipv4**() and **rgxg\_net\_cidr\_ipv6**() generate the (extended) regular expression that matches all addresses of the CIDR block specified by *address* and *prefix* (network identifier), which must be between 0 and 32 (**rgxg\_net\_cidr\_ipv4**()) respectively 128 (**rgxg\_net\_cidr\_ipv6**()) inclusive. *address* must be valid and not NULL.

The *rgxg\_ipv4\_t* and *rgxg\_ipv6\_t* structures which are the types of *address* are defined in *\<rgxg/net.h\>*.

```
typedef struct {
    uint8_t octet[4];
} rgxg_ipv4_t;
typedef struct {
    uint16_t hextet[8];
} rgxg_ipv6_t;
```

The **n**-th element of the *octet*/*hextet* array stores the **(n+1)**-th octet/hextet of the IPv4 respectively IPv6 address (i.e. for instance the first octet '**192**' in '**192.168.0.0**' is stored in *octet\[0\]*).

The generated regular expression is written to the character string *regex*, including the terminating null byte ('\\0'), unless **RGXG\_NONULLBYTE** is set. If *regex* is NULL the return value is the number of characters (excluding the terminating null byte) that would have been written in case *regex* has been initialized. Note that the functions assume an arbitrarily long *regex* string, callers must **ensure** that the given string is large enough to not overflow the actual space.

*options* is either zero, or the bitwise-**or** of one or more of the following macros:

Options marked as '**IPv6** only' can be used with **rgxg\_net\_cidr\_string**() (when parsing IPv6 CIDR blocks) and with **rgxg\_net\_cidr\_ipv6**().

- **RGXG\_NOUPPERCASE** (**IPv6** only)

  The generated regular expression only matches IPv6 addresses with lower case letters. By default both lower and upper case letters are matched.

- **RGXG\_NOLOWERCASE** (**IPv6** only)

  The generated regular expression only matches IPv6 addresses with upper case letters. By default both lower and upper case letters are matched.

- **RGXG\_NOZEROCOMPRESSION** (**IPv6** only)

  The generated regular expression does not match IPv6 addresses with zero compression (second form of text representation of IPv6 addresses mentioned in section 2.2 of **RFC 4291**).

- **RGXG\_NOMIXEDNOTATION** (**IPv6** only)

  The generated regular expression does not match IPv6 addresses in mixed notation (third form of text representation of IPv6 addresses mentioned in section 2.2 of **RFC 4291**).

- **RGXG\_NOOUTERPARENS** (**IPv6** only)

  Omit the outer parenthesis, if any, of the regular expression.

- **RGXG\_NONULLBYTE**

  Do not add the terminating null byte ('\\0') to the *regex* string.

Note that **RGXG\_NOUPPERCASE** and **RGXG\_NOLOWERCASE** are mutual exclusive options.

## RETURN VALUE

Upon successful return, these functions return the number of characters (excluding the terminating null byte) written to *regex*.

If an error occurs, a (negative) error code is returned (see **ERRORS** section).

## ERRORS

The following errors can be returned:

- **RGXG\_ERROR\_PREFIX**

  Invalid network identifier (e.g. the prefix is lesser than 0 or greater than 32 (IPv4) respectively 128 (IPv6)).

  **rgxg\_net\_cidr\_string**(): If *endptr* is not NULL, *\*endptr* points to the first character of the invalid prefix.

- **RGXG\_ERROR\_ARG2BIG** (**rgxg\_net\_cidr\_string**() only)

  At least one octet/hextet is too large.

  **rgxg\_net\_cidr\_string**(): If *endptr* is not NULL, *\*endptr* points to the first digit of the too large octet/hextet.

- **RGXG\_ERROR\_SYNTAX** (**rgxg\_net\_cidr\_string**() only)

  Syntax error in *cidr* string. If *endptr* is not NULL, *\*endptr* points to the character that causes the syntax error.

- **RGXG\_ERROR\_MUTEXOPTIONS** (**rgxg\_net\_cidr\_string**()/**rgxg\_net\_cidr\_ipv6**() only)

  Two mutual exclusive options were specified.

## AVAILABILITY

These functions are available since rgxg 0.1.

## SEE ALSO

[**regex**(7)](/regex/7.md), [**rgxg\_number\_greaterequal**(3)](/rgxg_number_greaterequal/3.md), [**rgxg\_number\_range**(3)](/rgxg_number_range/3.md).

## AUTHOR

Hannes von Haugwitz \<hannes@vonhaugwitz.com\>
