#! /usr/bin/env bash
# Author: Marmotte <marmotte@garamotte.net>
# Inspired from https://serverfault.com/a/699082

FILE=$1

# Get current ticks value
TICKS=$(grep CONFIG_HZ= /boot/config-$(uname -r) | cut -d= -f2)

cat $FILE | while read LINE
do
    # Get IP from the line
    IP=$(expr "$LINE" : '^src=\(.\+\) ttl:')

    # Compute elapsed time between 1970-01-01 and the line's value
    LAST_SEEN=$(expr "$LINE" : '.*last_seen: \(.\+\) oldest_pkt:')
    JIFFIES=$(grep -m1 ^jiffies /proc/timer_list | cut -d' ' -f2)
    ELAPSED_TIME=$((($JIFFIES - $LAST_SEEN) / $TICKS))

    # Compute the line's date and time value in a human readable format
    LINE_TIME=$(($(date +'%s') - $ELAPSED_TIME))
    DATE=$(date -d@$LINE_TIME)

    echo -e "$IP\t\t$DATE"
done
