Friday 2012-04-06

Mutt plus IMAP works well... until you hop from network to network. Then Mutt locks up until its IMAP TCP connection (the one from your old IP to your server) times out.

hping 3 supports arbitrary packet generation via TCL scripts, so it's pretty easy to brute force all the sequence numbers to reset the connection.


proc tcp_reset { saddr sport daddr dport } {
	for { set sn 0 } { $sn < [ expr 2 ** 32 ] } { incr sn [ expr 2 ** 16 ] } {
		hping send "ip(saddr=$saddr,daddr=$daddr)+tcp(sport=$sport,dport=$dport,seq=$sn,flags=r)"
	}
}

set fd [ open "| netstat -an "  "r" ]
while { [ gets $fd line ] >= 0 } {
	set F [ regexp -all -inline {\S+} $line ]
	if { [ lindex $F 0 ] != "tcp" } { continue }

	set local_ap [ lindex $F 3 ]
	set imap_ap  [ lindex $F 4 ]
	set F [concat [ split $imap_ap ":" ] [ split $local_ap ":" ] ]

	if { [ lindex $F 1 ] != "993" && [ lindex $F 1 ] != "143" } { continue }

	tcp_reset [ lindex $F 0 ] [ lindex $F 1 ] [ lindex $F 2 ] [ lindex $F 3 ]
}

if { [ catch {close $fd} err ] } {
	puts "command failed: $err"
}