#!/usr/bin/env awk
#
# Returns a string representation of the TCP flags.
#
# The order of the flags and the abbreviations are as follows:
#
#   F: FIN
#   S: SYN
#   R: RST
#   P: PSH
#   A: ACK
#   U: URG
#   E: ECE
#   W: CWR
#
# A dot ('.') is used to represent a flag which was not set
#
# Dependencies:
#   - tcpFlags
#
# Examples:
#   - tawk '{ print tcpFlags() }' file.txt
#   - tawk '{ $tcpAggrFlags = tcpFlags(); print }' file.txt

@include "_validate_col"

function tcpflags(        _f, _s) {
    _validate_col(tcpAggrFlags, "tcpAggrFlags")
    _f = strtonum($tcpAggrFlags)
    if (and(_f, 0x01)) _s = "F" _s # FIN
    else _s = "." _s
    if (and(_f, 0x02)) _s = "S" _s # SYN
    else _s = "." _s
    if (and(_f, 0x04)) _s = "R" _s # RST
    else _s = "." _s
    if (and(_f, 0x08)) _s = "P" _s # PSH
    else _s = "." _s
    if (and(_f, 0x10)) _s = "A" _s # ACK
    else _s = "." _s
    if (and(_f, 0x20)) _s = "U" _s # URG
    else _s = "." _s
    if (and(_f, 0x40)) _s = "E" _s # ECE
    else _s = "." _s
    if (and(_f, 0x80)) _s = "W" _s # CWR
    else _s = "." _s
    return _s
}
