#!/usr/bin/env awk
#
# Returns true if the source or destination port is equal to 'p' (int or hex)
# If 'p' is omitted, returns the source and destination port
#
# Parameters:
#   - [p] : the port number(s) to test
#
# Dependencies:
#   - basicFlow
#
# See Also:
#   - dport: destination port
#   - sport: source port
#
# Examples:
#   - tawk 'port(80)' file.txt           # test for port 80
#   - tawk 'port("53;80")' file.txt      # test for port 53 or 80
#   - tawk 'port("6000-6008")' file.txt  # test for port 6000, 6001, ... or 6008
#   - tawk '{ print port() }' file.txt

@include "_validate_col"

function port(p,        _i, _l, _portnum, _ports, _low, _high) {
    _validate_col(SrcPort, "SrcPort")
    _validate_col(DstPort, "DstPort")
    if (p ~ /^(0x[[:xdigit:]]+|[0-9]+)-(0x[[:xdigit:]]+|[0-9]+)$/) {
        split(p, _ports, /-/)
        _low = strtonum(_ports[1])
        _high = strtonum(_ports[2])
        return (($SrcPort >= _low && $SrcPort <= _high) ||
                ($DstPort >= _low && $DstPort <= _high))
    } else if (p ~ /^(0x[[:xdigit:]]+|[0-9]+)(;(0x[[:xdigit:]]+|[0-9]+))*$/) {
        _l = split(p, _ports, /;/)
        for (_i = 1; _i <= _l; _i++) {
            _portnum = strtonum(_ports[_i])
            if ($SrcPort == _portnum ||
                $DstPort == _portnum)
            {
                return 1
            }
        }
        return 0
    } else if (!p) return $SrcPort "\t" $DstPort
}
