forked from xjasonlyu/tun2socks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stop.go
34 lines (30 loc) · 870 Bytes
/
stop.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package tun
import (
"bytes"
"log"
"net"
)
var stopMarker = []byte{2, 2, 2, 2, 2, 2, 2, 2}
// Close of Windows and Linux tun/tap device do not interrupt blocking Read.
// sendStopMarker is used to issue a specific packet to notify threads blocking
// on Read.
func sendStopMarker(src, dst string) {
l, _ := net.ResolveUDPAddr("udp", src+":2222")
r, _ := net.ResolveUDPAddr("udp", dst+":2222")
conn, err := net.DialUDP("udp", l, r)
if err != nil {
log.Printf("fail to send stopmarker: %s", err)
return
}
defer conn.Close()
conn.Write(stopMarker)
}
func isStopMarker(pkt []byte, src, dst net.IP) bool {
n := len(pkt)
// at least should be 20(ip) + 8(udp) + 8(stopmarker)
if n < 20+8+8 {
return false
}
return pkt[0]&0xf0 == 0x40 && pkt[9] == 0x11 && src.Equal(pkt[12:16]) &&
dst.Equal(pkt[16:20]) && bytes.Compare(pkt[n-8:n], stopMarker) == 0
}