
puts "IP Source address to use:"
gets stdin def_ip_src

puts "IP destination address to use:"
gets stdin def_ip_dst

popen tcp t $def_ip_src 333 $def_ip_dst chargen

puts ""
puts ""


pinit tcp tcpsnd
pset tcpsnd tcp sport 333
pset tcpsnd tcp dport chargen
pset tcpsnd tcp seq 55555
pset tcpsnd tcp flags SYN
pset tcpsnd tcp addopt mss 0x5b4
pset tcpsnd tcp win 0
pset tcpsnd tcp urp 0

pinit tcp tcprcv

# Send the SYN
t.send tcpsnd

# Wait up to 5.000s for the SYN-ACK to come back
if {[t.recv tcprcv 5000] == "timeout"} {
	puts "Failed to connect to $def_ip_dst"
	exit 1
}

puts "Got syn-ack packet: [plist tcprcv tcp]"

# Compose reply to SYN-ACK: Only need MSS option on initial SYN
# XXX: pset tcpsnd tcp delopt mss
pset tcpsnd tcp flags ACK
# Only ACK the SYN
pset tcpsnd tcp ack [expr [pget tcprcv tcp seq] + 1]
pset tcpsnd tcp seq [expr [pget tcpsnd tcp seq] + 1]

t.send tcpsnd

# Wait up to timeout seconds for the whole deal
set timeout 600
set endtime [expr [pcktime] + $timeout]
set lastprobe [pcktime]

while {$endtime > [pcktime]} {
	set now [pcktime]
	set timeout [expr 1000 * ($endtime - $now)]
	puts "new timeout is $timeout from $endtime and $now"
	if {[t.recv tcprcv $timeout] == "packet"} {
		set newprobe [pcktime]
		puts "Duration since last probe: [expr $newprobe - $lastprobe]"
		puts "[plist tcprcv tcp]"
		set lastprobe $newprobe
	}
}

puts "Test complete."
puts ""

if {[pcktime]-$lastprobe > 61} {
	puts "Test passed, system $def_ip_dst."
} else {
	puts "Test FAILED, system $def_ip_dst."
}
puts ""
puts "Reset connection now.  (Just in case)"

pset tcpsnd tcp flags rst
t.send tcpsnd

