-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_stack.go
143 lines (118 loc) · 3.72 KB
/
tcp_stack.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
package ftcp
import (
"errors"
"net"
"sync"
"github.com/akmistry/ftcp/pb"
)
var (
errTcpChecksum = errors.New("tcp_stack: invalid checksum")
)
type TCPSender interface {
SendTCPPacket(b []byte, addr *net.IPAddr, port uint16) error
SendSyncRequest(req *pb.SyncRequest, reply *pb.SyncReply) error
}
type TCPStack struct {
ipSender IPSender
localAddr *net.IPAddr
connMap *TCPConnMap
connCh chan *TCPConnState
syncClient *SyncClient
lock sync.Mutex
}
func NewTCPStack(ipSender IPSender, localAddr *net.IPAddr) *TCPStack {
s := &TCPStack{
ipSender: ipSender,
localAddr: localAddr,
connMap: MakeTCPConnMap(),
connCh: make(chan *TCPConnState, 64),
}
return s
}
func (s *TCPStack) SetSyncClient(c *SyncClient) {
s.lock.Lock()
s.syncClient = c
s.lock.Unlock()
}
func (s *TCPStack) HandleIPPacket(packet *IPPacket) error {
// TODO: Handle multiple listeners
listenPort := uint16(9999)
tcpPacket := packet.Payload()
tcpHeader, err := ParseTCPHeader(tcpPacket)
if err != nil {
return err
}
if tcpHeader.DstPort != listenPort {
//LogDebug("Not listening to TCP dst port: %d", tcpHeader.DstPort)
// Not the port we're listening on
return nil
}
tcpData := tcpPacket[tcpHeader.DataOff:]
LogDebug("Received TCP packet with %d bytes data: %v", len(tcpData), tcpHeader)
// Verify TCP checksum
calcTcpChecksum := TCPChecksum(tcpPacket, packet.Header.Src, packet.Header.Dst)
if tcpHeader.Checksum != calcTcpChecksum {
LogWarn("TCP checksum 0x%04x != calculated checksum 0x%04x",
tcpHeader.Checksum, calcTcpChecksum)
// Ignore checksum errors. There seem to be too many of them.
//return errTcpChecksum
}
// TODO: Support the full 4-tuple for connection identity
tcpConn := s.connMap.GetState(packet.Header.Src, tcpHeader.SrcPort)
if tcpConn == nil && tcpHeader.Syn {
tcpConn = NewTCPConnState(tcpHeader.DstPort, tcpHeader.SrcPort, s, &net.IPAddr{IP: packet.Header.Src})
s.connMap.PutState(packet.Header.Src, tcpHeader.SrcPort, tcpConn)
// TODO: This is wrong. We should only declare a new connection when we
// receive the first ACK from the remote end, completing the 3-way
// handshake. For now, this is the easy way.
s.connCh <- tcpConn
}
if tcpConn == nil {
LogDebug("No TCP connection found for src %v:%d", packet.Header.Src, tcpHeader.SrcPort)
return nil
}
err = tcpConn.ConsumePacket(tcpHeader, packet.Payload()[tcpHeader.DataOff:])
if err != nil {
LogWarn("tcpConn.ConsumePacket error: %v", err)
return nil
}
return nil
}
func (s *TCPStack) SendTCPPacket(b []byte, addr *net.IPAddr, port uint16) error {
TCPSetChecksum(b, TCPChecksum(b, addr.IP, s.localAddr.IP))
return s.ipSender.SendIPPacket(b, addr)
}
func (s *TCPStack) SendSyncRequest(req *pb.SyncRequest, reply *pb.SyncReply) error {
s.lock.Lock()
c := s.syncClient
s.lock.Unlock()
if c == nil {
return nil
}
return c.SendSync(req, reply)
}
func (s *TCPStack) Listen() (*TCPConnState, error) {
c := <-s.connCh
return c, nil
}
func (s *TCPStack) GetConn(remoteAddr *net.IPAddr, remotePort uint16) *TCPConnState {
return s.connMap.GetState(remoteAddr.IP, remotePort)
}
func (s *TCPStack) Sync(req *pb.SyncRequest, reply *pb.SyncReply) error {
if req.Key == nil {
return errors.New("TCPStack.Sync: request missing key")
}
tcpConn := s.connMap.GetState(req.Key.RemoteAddr, uint16(req.Key.RemotePort))
if tcpConn == nil && req.State != pb.TcpConnState_CLOSED {
tcpConn = NewTCPConnState(uint16(req.Key.LocalPort), uint16(req.Key.RemotePort), s, &net.IPAddr{IP: req.Key.RemoteAddr})
s.connMap.PutState(req.Key.RemoteAddr, uint16(req.Key.RemotePort), tcpConn)
}
if tcpConn == nil {
return nil
}
if req.State == pb.TcpConnState_CLOSED {
// TODO: Delete state.
return nil
}
return tcpConn.Sync(req, reply)
}