#!/usr/bin/env awk
#
# Convert an IPv4 address 'ip' to a string.
#
# Parameters:
#   - ip : the IPv4 address to convert
#
# Dependencies:
#   - None
#
# See also:
#   - ip2hex: converts an IPv4 address to hex
#   - ip2num: converts an IP address to a number
#
# Examples:
#   - tawk '{ print ip2str($srcIP) }' file.txt

@include "isiphex"
@include "isipnum"

function ip2str(ip) {
    if (!isiphex(ip) && !isipnum(ip)) {
        return ip
    } else {
        ip = strtonum(ip)
        return sprintf("%d.%d.%d.%d",
               rshift(and(ip, 0xff000000), 24),
               rshift(and(ip, 0x00ff0000), 16),
               rshift(and(ip, 0x0000ff00),  8),
                      and(ip, 0x000000ff))
    }
}
