NAME

IP - Functions for manipulating IP addresses and prefixes

SYNOPSIS

    declare

        i          int;
        n_address  IP.address;
        n_prefix   IP.prefix;
        address    varchar(64);
        prefix     varchar(64);

    begin

        i := IP.family(address|prefix);
    
        n_address := IP.normalize(address);
        n_prefix  := IP.normalize(prefix);

        address := IP.preferred(address);
        prefix  := IP.preferred(prefix);
    
        address := IP.host(address|prefix);
    
        prefix  := IP.text(address|prefix);
        prefix  := IP.network(address|prefix);
    
        address := IP.broadcast(address|prefix);
        address := IP.netmask(address|prefix);
    
        i := IP.masklen(address|prefix);
    
        i := IP.difference(address, address);

        address := IP.plus(address, delta);

    end;

DESCRIPTION

There are many valid formats for IP addresses and prefixes, but only three of them are used in the NetDB database. The functions in this package can be used to convert input IP addresses and prefixes to those formats or between those formats. This package also provides a function for calculating the nth IP address from a given IP address.

DEFINITIONS

When used as functional input or output, address and prefix have specific meanings in this document:

    address == d.d.d.d          where 0 <= d <= 255  (IPv4)
               x:x:x:x:x:x:x:x  where 0 <= x <= ffff (IPv6)

    prefix  == address/l        where 0 <= l <=  32  (IPv4)
                                      0 <= l <= 128  (IPv6)

Throughout this document we refer to the IP address and prefix formats shown above as familiar formats to distinguish them from the NetDB normalized formats.

IPv6 addresses can be written in a compressed format where ``::'' is used to indicate one or more groups of zeros. NetDB uses that format as the familiar format for IPv6 addresses. The uncompressed format is also supported and is known as the preferred format.

A normalized IP address or prefix is one that has been recast into the NetDB internal storage format. Unlike the familiar forms of IP addresses and prefixes, the normalized versions sort properly. It is important to use normalized IP addresses and prefixes when doing comparisons in NetDB.

TYPES

Always use these types for normalized IP addresses and prefixes.

IP.address

Datatype of normalized IP addresses.

IP.prefix

Datatype of normalized prefixes.

FUNCTIONS

family ( address | prefix )

Returns IP address family as an integer - 4 for IPv4, 6 for IPv6.

normalize ( address | prefix )

Returns the IP address or prefix normalized to the NetDB internal format. Use this function when comparing input with normalized columns in the NetDB database.

preferred ( address | prefix )

Returns the IP address or prefix in the preferred format. Returns NULL for IPv4 addresses and prefixes.

host ( address | prefix )

Returns an IP address or the IP address part of a prefix in familiar form.

text ( address | prefix )

Returns a prefix in familiar form. If no prefix length is specified, i.e., the input is an IP address, text assumes and returns the maximum possible prefix length.

network ( address | prefix )

Returns a prefix in familiar form. network clears all bits in the host part of the prefix, based on the input prefix length. If no prefix length is specified, network assumes and returns the maximum possible prefix length.

broadcast ( address | prefix )

Returns the network broadcast IP address in familiar form. broadcast sets all bits in the host part of the IP address based on the input prefix length. If no prefix length is specified, broadcast assumes the maximum possible prefix length.

netmask ( address | prefix )

Returns the network netmask in familiar form. netmask sets all bits in the network part of the netmask and clears all the bits in the host part of the netmask based on the input prefix length. If no prefix length is specified, netmask assumes the maximum possible prefix length.

masklen ( address | prefix )

Returns the network netmask length in bits based on the input prefix length. If no prefix length is specified, masklen assumes and returns the maximum possible prefix length.

difference ( address, address )

Returns the difference between two IP addresses. The first address is the minuend.

plus ( address, delta )

Returns the IP address, in familiar form, delta IP addresses away from the input IP address. delta may be negative, but not imaginary nor irrational.

EXAMPLES

Check for the input IP address in the NetDB IPaddress table:

    select count(*) from IPaddress
     where IPaddress = IP.normalize(input)

Get the prefix (in familiar format) of the address space containing the input IP address:

    select prefix_txt from IPaddressSpace
     where IP.normalize(input) between address and broadcast