NaviServer - programmable web server
4.99  5.0

[ Main Table Of Contents | Table Of Contents | Keyword Index ]

ns_subnetmatch(n) 4.99.30 naviserver "NaviServer Built-in Commands"

Name

ns_subnetmatch - Determine if IP address is included in a subnet specification

Table Of Contents

Synopsis

Description

Determine, if a provided IP address (IPv4 or IPv6) is included in a subnet specification, which is provided in CIDR notation. https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing

COMMANDS

ns_subnetmatch subnet ipaddr

The command validates the provided subnet specification (IPv4 or IPv6 address followed by slash and number of significant bits) and the provided ipaddr and tests whether the IP address is in the implied range. The command returns a boolean value as the result. When comparing an IPv4 and IPv6 address/CIDR specification or vice versa, the result is always false.

The function can be used as well to check, whether an IP address is an IPv4 or IPv6 address.

 % ns_subnetmatch 137.208.0.0/16 137.208.116.31
 1
 
 % ns_subnetmatch 137.208.0.0/16 112.207.16.33
 0
 % ns_subnetmatch 2001:628:404:74::31/64 [ns_conn peeraddr]
 ...
 # Is IP address a valid IPv6 address?
 % set ip [ns_conn peeraddr]
 % ns_subnetmatch ::/0 $ip
 
 # Is IP address a valid IPv4 address?
 % ns_subnetmatch 0.0.0.0/0 $ip

See Also

ns_conn

Keywords

CIDR, IPv4, IPv6, global built-in, subnet