#!/usr/bin/env awk
#
# Return true if an IP address 'ip' lies between 'low' and 'high (included).
#
# Parameters:
#   - ip   : the IP address to test
#   - low  : the lowest allowed value for 'ip
#   - high : the highest allowed value for 'ip
#
# Dependencies:
#   - None
#
# See also:
#   - ipinnet
#   - host
#   - dhost
#   - shost
#   - net
#   - dnet
#   - snet
#
# Examples:
#   - tawk 'ipinrange($srcIP, "192.168.1.0", "192.168.1.255")' file.txt

@include "hdr"
@include "ip2num"

function ipinrange(ip, low, high,        _ipnum, _h6, _i6) {
    if (hdr()) return __PRIHDR__

    _i6 = isip6(ip)
    _h6 = isip6(high)
    if ((_i6 && !_h6) || (!_i6 && _h6)) return 0

    _ipnum = ip2num(ip)
    return _ipnum >= ip2num(low) && _ipnum <= ip2num(high)
}
