---
name: "ipsec_initsubnet"
section: "3"
platform: "linux"
package: "openswan"
version: "1:2.6.37-3+deb7u1"
release: "Debian 7 (Wheezy)"
platforms: ["linux"]
url: "https://new.carta.tech/ipsec_initsubnet"
---

# ipsec\_initsubnet(3)

initialize an ip\_subnet, initialize a singleton ip\_subnet, get address type of an ip\_subnet, convert subnet mask to bit count, get base address of an ip\_subnet, get subnet mask of an ip\_subnet

## NAME

ipsec\_initsubnet, ipsec\_addrtosubnet, ipsec\_subnettypeof, ipsec\_masktocount, ipsec\_networkof, ipsec\_maskof - initialize an ip\_subnet, initialize a singleton ip\_subnet, get address type of an ip\_subnet, convert subnet mask to bit count, get base address of an ip\_subnet, get subnet mask of an ip\_subnet

## SYNOPSIS

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

**const char \*initsubnet(const ip\_address \*addr,** **int maskbits, int clash, ip\_subnet \*dst);**\
 **const char \*addrtosubnet(const ip\_address \*addr,** **ip\_subnet \*dst);**

**int subnettypeof(const ip\_subnet \*src);**\
 **int masktocount(const ip\_address \*src);**\
 **void networkof(const ip\_subnet \*src, ip\_address \*dst);**\
 **void maskof(const ip\_subnet \*src, ip\_address \*dst);**

## DESCRIPTION

The **\<freeswan.h\>** library uses an internal type *ip\_subnet* to contain a description of an IP subnet (base address plus mask). These functions provide basic tools for creating and examining this type.

*Initsubnet* initializes a variable *\*dst* of type *ip\_subnet* from a base address and a count of mask bits. The *clash* parameter specifies what to do if the base address includes **1** bits outside the prefix specified by the mask (that is, in the \`\`host number'' part of the address):

- '0'

  zero out host-number bits

- 'x'

  non-zero host-number bits are an error

*Initsubnet* returns **NULL** for success and a pointer to a string-literal error message for failure; see DIAGNOSTICS.

*Addrtosubnet* initializes an *ip\_subnet* variable *\*dst* to a \`\`singleton subnet'' containing the single address *\*addr*. It returns **NULL** for success and a pointer to a string-literal error message for failure.

*Subnettypeof* returns the address type of a subnet, normally **AF\_INET** or **AF\_INET6**. (The **\<freeswan.h\>** header file arranges to include the necessary headers for these names to be known.)

*Masktocount* converts a subnet mask, expressed as an address, to a bit count suitable for use with *initsubnet*. It returns **-1** for error; see DIAGNOSTICS.

*Networkof* fills in *\*dst* with the base address of subnet *src*.

*Maskof* fills in *\*dst* with the subnet mask of subnet *src*, expressed as an address.

## SEE ALSO

[inet(3)](/inet/3.md), [ipsec\_ttosubnet(3)](/ipsec_ttosubnet/3.md), [ipsec\_rangetosubnet(3)](/ipsec_rangetosubnet/3.md)

## DIAGNOSTICS

Fatal errors in *initsubnet* are: unknown address family; unknown *clash* value; impossible mask bit count; non-zero host-number bits and *clash* is **'x'**. Fatal errors in *addrtosubnet* are: unknown address family. Fatal errors in *masktocount* are: unknown address family; mask bits not contiguous.

## HISTORY

Written for the FreeS/WAN project by Henry Spencer.
