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

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

function shost(ip) {
    _validate_col(SrcIP4, "SrcIP4")
    if (index(ip, "/") != 0) {
        return ipinnet($SrcIP4, ip)
    } else if (length(ip) > 0) {
        return ip2hex($SrcIP4) == ip2hex(ip)
    } else if (!ip) {
        return $SrcIP4
    }
}
