Next: , Previous: , Up: Library   [Contents][Index]


5.23 Internet address manipulation functions

Following functions operate on IPv4 addresses in numeric form.

Built-in Function: number ntohl (number n)

Converts the number n, from host to network byte order. The argument n is treated as an unsigned 32-bit number.

Built-in Function: number htonl (number n)

Converts the number n, from network to host byte order. The argument n is treated as an unsigned 32-bit number.

Built-in Function: number ntohs (number n)

The argument n is treated as an unsigned 16-bit number. The function converts this number from network to host order.

Built-in Function: number htons (number n)

The argument n is treated as an unsigned 16-bit number. The function converts this number from host to network order.

Built-in Function: number inet_aton (string s)

Converts the Internet host address s from the standard numbers-and-dots notation into the equivalent integer in host byte order.

inet_aton("127.0.0.1") ⇒ 2130706433

The numeric data type in MFL is signed, therefore on machines with 32 bit integers, this conversion can result in a negative number:

inet_aton("255.255.255.255") ⇒ -1

However, this does not affect arithmetical operations on IP addresses.

Built-in Function: string inet_ntoa (number n)

Converts the Internet host address n, given in host byte order to string in standard numbers-and-dots notation:

inet_ntoa(2130706433) ⇒ "127.0.0.1"
Built-in Function: number len_to_netmask (number n)

Convert number of masked bits n to IPv4 netmask:

inet_ntoa(len_to_netmask(24)) ⇒ 255.255.255.0
inet_ntoa(len_to_netmask(7)) ⇒ 254.0.0.0

If n is greater than 32 the function raises e_range exception.

Built-in Function: number netmask_to_len (number mask)

Convert IPv4 netmask mask into netmask length (number of bits preserved by the mask):

netmask_to_len(inet_aton("255.255.255.0")) ⇒ 24
netmask_to_len(inet_aton("254.0.0.0")) ⇒ 7

The following functions operate on string representation of IPv4 and IPv6 addresses.

Built-in Function: string reverse_ipstr(string ip)

Converts the IP address ip to reverse format suitable for use in DNS labels. That is, if ip is an IPv4 address, the return value is obtained by reversing the order of octets in the input:

reverse_ipstr("192.0.2.10") ⇒ "10.2.0.192"

If IP is an IPv6 address, the return string is formed as a sequence of nibbles separated by dots, encoded in reverse order, i.e. the low-order nibble is encoded first, followed by the next low-order nibble and so on. Each nibble is represented by a hexadecimal digit appending each byte from the IP represented in hex from the last to first, delimited by dots, e.g.:

reverse_ipstr("2001:db8:0:0:1::2")
⇒
"2.0.0.0.0.0.0.0.0.0.0.0.1.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2"
Built-in Function: number is_ipstr(string s)

Returns 1 if s is a string representation of an IP address (IPv4 or IPv6) and 0 otherwise.

Built-in Function: number is_ip4str(string s)

Returns 1 if s is a string representation of an IPv4 address, and 0 otherwise.

Built-in Function: number is_ip6str(string s)

Returns 1 if s is a string representation of an IPv6 address, and 0 otherwise.

The following functions operate on IP addresses or CIDRs represented as strings:

Built-in Function: boolean match_cidr (string ip, string cidr)

It returns true if the IP address ip pertains to the IP range cidr. The first argument, ip, is a string representation of an IP address (IPv4 or IPv6). The second argument, cidr, is a string representation of a IP range in CIDR notation, i.e. ‘addr/N’, where addr is IP address and N specifies address prefix length – the number of meaningful initial bits, counting from the left side of the address.

The following example will reject the mail if the IP address of the sending machine does not belong to the block 192.0.2.0/24:

if not match_cidr(${client_addr}, "192.0.2.0/24")
  reject
fi

The following example does the same for a IPv6 CIDR 2001:DB8::/56:

if not match_cidr(${client_addr}, "2001:DB8::/56")
  reject
fi

Notice, that in previous versions of mailfromd this function was implemented as MFL function and required the use of match_cidr.mfl module (see 8170-9000. This is no longer the case. The module itself is retained in the distribution for compatibility reasons. If your code uses it, you will see the following warning during compilation phase (split in two lines for typesetting reasons):

mailfromd: match_cidr.mfl:18.1-78: warning: This module is
deprecated. The match_cidr function is now built-in.

Just remove the require 'match_cidr' from your code to make this warning disappear.


Next: , Previous: , Up: Library   [Contents][Index]