-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtcpconn.go
169 lines (135 loc) · 3.21 KB
/
tcpconn.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package egressguy
import (
"crypto/rand"
"log"
"net"
"sync"
"github.com/gopacket/gopacket"
"github.com/gopacket/gopacket/layers"
)
const (
TCP_CONNECTION_NONE uint8 = iota
TCP_CONNECTION_SYN_SENT
TCP_CONNECTION_ESTABLISHED
TCP_CONNECTION_FINISHED
)
const TCP_WINDOW_SCALE uint8 = 9
type TcpConn struct {
TcpHandler
Instance *EgressGuy
State uint8
Seq uint32
Ack uint32
Win uint16
Mss uint16
IPv4Layer layers.IPv4
SrcPort layers.TCPPort
DstPort layers.TCPPort
onClose chan struct{}
onCloseOnce sync.Once
}
func NewTcpConn(instance *EgressGuy, src, dst net.IP, srcPort, dstPort layers.TCPPort, handler TcpHandler) (*TcpConn, error) {
ip4 := layers.IPv4{
SrcIP: src,
DstIP: dst,
Version: 4,
TTL: 255,
Protocol: layers.IPProtocolTCP,
}
conn := TcpConn{
Instance: instance,
State: TCP_CONNECTION_SYN_SENT,
IPv4Layer: ip4,
SrcPort: srcPort,
DstPort: dstPort,
Mss: uint16(instance.Interface.MTU - 40),
Win: 65535,
onClose: make(chan struct{}),
}
var seq [4]byte
rand.Read(seq[:])
conn.Seq = uint32(seq[0])<<24 | uint32(seq[1])<<16 | uint32(seq[2])<<8 | uint32(seq[3])
tcp := conn.NewPacket()
tcp.SYN = true
tcp.Options = []layers.TCPOption{
{
OptionType: layers.TCPOptionKindMSS,
OptionData: []byte{byte(conn.Mss >> 8), byte(conn.Mss)},
},
{
OptionType: layers.TCPOptionKindWindowScale,
OptionData: []byte{TCP_WINDOW_SCALE},
},
}
if err := conn.SendPacket(&tcp, nil); err != nil {
return nil, err
}
conn.SetHandler(handler)
conn.Instance.AddListener(&conn)
return &conn, nil
}
func (c *TcpConn) Close() error {
return c.setClosed(true)
}
func (c *TcpConn) OnClose() <-chan struct{} {
return c.onClose
}
func (c *TcpConn) setClosed(reset bool) error {
c.Instance.RemoveListener(c)
c.onCloseOnce.Do(func() {
close(c.onClose)
})
if c.State == TCP_CONNECTION_FINISHED {
return nil
}
if reset {
tcp := c.NewPacket()
tcp.RST = true
if err := c.SendPacket(&tcp, nil); err != nil {
return err
}
}
c.State = TCP_CONNECTION_FINISHED
return nil
}
func (c *TcpConn) NewPacket() layers.TCP {
tcp := layers.TCP{
SrcPort: c.SrcPort,
DstPort: c.DstPort,
Seq: c.Seq,
Ack: c.Ack,
Window: c.Win,
}
tcp.SetNetworkLayerForChecksum(&c.IPv4Layer)
return tcp
}
func (c *TcpConn) SendPacket(t *layers.TCP, payload []byte) error {
if payload != nil {
c.Seq += uint32(len(payload))
return c.Instance.SendPacket(&c.IPv4Layer, t, gopacket.Payload(payload))
} else {
return c.Instance.SendPacket(&c.IPv4Layer, t)
}
}
func (c *TcpConn) ConnectionTuple() (proto layers.IPProtocol, src, dst net.IP, srcPort, dstPort layers.TCPPort) {
return layers.IPProtocolTCP, c.IPv4Layer.SrcIP, c.IPv4Layer.DstIP, c.SrcPort, c.DstPort
}
func (c *TcpConn) SetHandler(handler TcpHandler) {
if handler == nil {
log.Fatal("handler cannot be nil")
}
if c.TcpHandler != nil {
c.TcpHandler.SetConn(nil)
}
c.TcpHandler = handler
handler.SetConn(c)
}
func (c *TcpConn) HandlePacket(packet gopacket.Packet, layer gopacket.Layer) {
if c.TcpHandler == nil {
log.Fatal("handler cannot be nil")
}
err := c.TcpHandler.HandlePacket(packet, layer.(*layers.TCP))
if err != nil {
log.Println("tcp handler: ", err)
}
}