#!/usr/bin/env awk
#
# Returns true if source or destination IP is equal to 'ip' or belongs to 'ip', e.g., 1.2.3.4/24
# If 'ip' is omitted, returns the source and destination IP
#
# Dependencies:
#   - None
#
# See Also:
#   - net: alias for host
#
# Examples:
#   - tawk 'host("1.2.3.4")' file.txt
#   - tawk 'host("1.2.3.4/24")' file.txt
#   - tawk 'host(0x01020304) { print host() }' file.txt
#   - tawk '{ print host() }' file.txt

@include "ip2hex"
@include "ipinnet"
@include "_validate_col"

function host(ip,        _iphex) {
    _validate_col(SrcIP4, "SrcIP4")
    _validate_col(DstIP4, "DstIP4")
    if (index(ip, "/") != 0) {
        return ipinnet($SrcIP4, ip) || ipinnet($DstIP4, ip)
    } else if (length(ip) > 0) {
        _iphex = ip2hex(ip)
        return ip2hex($SrcIP4) == _iphex || ip2hex($DstIP4) == _iphex
    } else if (!ip) {
        return $SrcIP4 "\t" $DstIP4
    }
}
