#!/usr/bin/env awk
#
# Converts an IPv4 address to a number
#
# Dependencies:
#   - None
#
# See Also:
#   - ip2hex: converts an IPv4 address to hex
#   - ip2str: converts an IPv4 address to string
#
# Examples:
#   - tawk '{ print ip2num($SrcIP4) }' file.txt

function ip2num(ip,        _tmp) {
    if (ip ~ /^0x[[:xdigit:]]{1,8}$/) {
        return strtonum(ip)
    } else if (ip ~ /^[0-9]{1,3}(\.[0-9]{1,3}){3}$/) {
        split(ip, _tmp, /\./)
        return lshift(_tmp[1], 24) + lshift(_tmp[2], 16) + lshift(_tmp[3], 8) + _tmp[4]
    } else {
        return ip
    }
}
