#!/bin/sh
#
# This script generates the smallest pcap which causes a segmentation fault.

if [ -n "$T2HOME" ]; then
    T2="$T2HOME/tranalyzer2/src/tranalyzer"
elif [ -n "$(which tranalyzer)" ]; then
    T2="$(which tranalyzer)"
else
    T2="$(dirname "$0")/../tranalyzer2/src/tranalyzer"
fi

if [ ! -f "$T2" ]; then
    echo "Tranalyzer not found. Edit T2 variable at line 5"
    exit 1
fi

if [ ! -f "$1" ]; then
    echo "Usage: $0 <file.pcap>"
    exit 1
fi
FILE="$1"

IN="$FILE"
OUT="out"
CONT=1
NUM=0

# Configuration options
DIV=2         # Number of parts for the initial split
STEP=1        # Number of packets to eat when removing packets from the beginning/end
#TIMEOUT=4    # if set, kill t2 after TIMEOUT seconds
#ERRMSG="ERR" # if set, grep the output for this message

while [ $CONT -eq 1 ]; do
    CONT=0
    N=$(capinfos -c -M "$IN" | grep "Number of packets:" | cut -d: -f2 | sed 's/[[:space:]]\+//')
    N=$((1+N/DIV))
    if [ -n "$M" ] && [ $N -eq $M ]; then
        DIV=$((DIV+1))
        if [ $N -lt 20 ]; then
            break
        fi
        echo "Dividing by $DIV"
        continue
    fi
    M=$N
    echo "editcap -F pcap -c $N \"$IN\" \"$OUT$NUM\""
    ERR="$(editcap -F pcap -c "$N" "$IN" "$OUT$NUM" 2>&1 | grep error)"
    if [ -z "$ERR" ]; then
        for i in ${OUT}${NUM}_*; do
            if [ "$i" != "$IN" ] && [ -n "$(file -b "$i" | grep 'capture file')" ]; then
                if [ $CONT -eq 1 ] && [ "$i" != "$FILE" ]; then
                    rm "$i"
                else
                    echo "    Testing \"$i\""
                    if [ -n "$ERRMSG" ]; then
                        ERR="$($T2 -r "$i" -w /tmp/DEL 2>&1 | grep "$ERRMSG")"
                    elif [ -n "$TIMEOUT" ]; then
                        timeout -s 11 ${TIMEOUT}s $T2 -l -r "$i" -w /tmp/DEL 2>&1 > /dev/null
                    else
                        $T2 -l -r "$i" -w /tmp/DEL 2>&1 > /dev/null
                    fi
                    ret=$?
                    if [ $ret -eq 139 ] || [ $ret -eq 124 ] || [ -n "$ERR" ]; then
                        echo "    \"$i\" causes a segfault"
                        CONT=1
                        if [ "$IN" != "$FILE" ]; then
                            rm "$IN"
                        fi
                        IN="$i"
                    elif [ "$i" != "$FILE" ]; then
                        rm "$i"
                    fi
                fi
            fi
        done
    else
        echo "editcap failed... aborting"
        exit 1
    fi
    NUM=$((NUM+1))
done

N=$(capinfos -c -M "$IN" | grep "Number of packets:" | cut -d: -f2 | sed 's/[[:space:]]\+//')
printf "\nRemoving first packets $N\n"
IN2="$IN"
for i in $(seq 2 $STEP $N); do
    TEST="${OUT}${NUM}"
    echo "editcap -r \"$IN\" \"$TEST\" $i-$N"
    ERR="$(editcap -F pcap -r "$IN" "$TEST" $i-$N 2>&1 | grep error)"
    if [ -z "$ERR" ]; then
        echo "    Testing \"$TEST\""
        if [ -n "$ERRMSG" ]; then
            ERR="$($T2 -r "$TEST" -w /tmp/DEL 2>&1 | grep "$ERRMSG")"
        elif [ -n "$TIMEOUT" ]; then
            timeout -s 11 ${TIMEOUT}s $T2 -l -r "$TEST" -w /tmp/DEL 2>&1 > /dev/null
        else
            $T2 -l -r "$TEST" -w /tmp/DEL 2>&1 > /dev/null
        fi
        ret=$?
        if [ $ret -eq 139 ] || [ $ret -eq 124 ] || [ -n "$ERR" ]; then
            if [ -f "$IN2" ] && [ "$IN2" != "$IN" ] && [ "$IN2" != "$FILE" ]; then
                rm "$IN2"
            fi
            IN2="$TEST"
        elif [ "$TEST" != "$FILE" ]; then
            rm "$TEST"
            break
        fi
    else
        echo "editcap failed... aborting"
        exit 1
    fi
    NUM=$((NUM+1))
done

if [ "$IN2" != "$IN" ] && [ "$IN" != "$FILE" ]; then
    rm "$IN"
fi

IN="$IN2"
N=$(capinfos -c -M "$IN" | grep "Number of packets:" | cut -d: -f2 | sed 's/[[:space:]]\+//')
printf "\nRemoving last packets $N\n"
IN2="$IN"
for i in $(seq $N -$STEP 1); do
    TEST="${OUT}${NUM}"
    echo "editcap -r \"$IN\" \"$TEST\" 1-$i"
    ERR="$(editcap -F pcap -r "$IN" "$TEST" 1-$i 2>&1 | grep error)"
    if [ -z "$ERR" ]; then
        echo "    Testing \"$TEST\""
        if [ -n "$ERRMSG" ]; then
            ERR="$($T2 -r "$TEST" -w /tmp/DEL 2>&1 | grep "$ERRMSG")"
        elif [ -n "$TIMEOUT" ]; then
            timeout -s 11 ${TIMEOUT}s $T2 -l -r "$TEST" -w /tmp/DEL 2>&1 > /dev/null
        else
            $T2 -l -r "$TEST" -w /tmp/DEL 2>&1 > /dev/null
        fi
        ret=$?
        if [ $ret -eq 139 ] || [ $ret -eq 124 ] || [ -n "$ERR" ]; then
            if [ -f "$IN2" ] && [ "$IN2" != "$IN" ] && [ "$IN2" != "$FILE" ]; then
                rm "$IN2"
            fi
            IN2="$TEST"
        elif [ "$TEST" != "$FILE" ]; then
            rm "$TEST"
            break
        fi
    else
        echo "editcap failed... aborting"
        exit 1
    fi
    NUM=$((NUM+1))
done

if [ "$IN2" != "$IN" ] && [ "$IN" != "$FILE" ]; then
    rm "$IN"
fi
IN="$IN2"

if [ $IN != "$FILE" ]; then
    N=$(capinfos -c -M "$IN" | grep "Number of packets:" | cut -d: -f2 | sed 's/^[[:space:]]\+//')
    printf "\n$IN is the smallest PCAP which causes a segfault ($N packets)\n"
else
    echo "\nThis pcap does not cause a segmentation fault\n"
fi
