/
tcp.go
67 lines (57 loc) · 1.6 KB
/
tcp.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
package balancer
import (
"fmt"
"net"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
// TCPState defines the state a TCP connection has.
type TCPState uint8
const (
_ = iota
TCP_STATE_SYN_SENT TCPState = iota
TCP_STATE_SYN_RECEIVED
TCP_STATE_ESTABLISHED
TCP_STATE_FIN_WAIT_1
TCP_STATE_FIN_WAIT_2
TCP_STATE_CLOSE_WAIT
TCP_STATE_CLOSING
TCP_STATE_LAST_ACK
TCP_STATE_TIME_WAIT
TCP_STATE_CLOSED
)
// TCPPacket represents a TCP packet.
type TCPPacket struct {
ip *layers.IPv4
tcp *layers.TCP
}
// NewTCPPacket creates and initializes a new TCP packet.
// The IP layer is needed to calculate the correct TCP checksum.
func NewTCPPacket(ip *layers.IPv4, tcp *layers.TCP) *TCPPacket {
return &TCPPacket{
ip: ip,
tcp: tcp,
}
}
// MarshalBinary returns the binary representation of the packet.
func (p *TCPPacket) MarshalBinary() ([]byte, error) {
p.tcp.SetNetworkLayerForChecksum(p.ip)
opts := gopacket.SerializeOptions{
ComputeChecksums: true,
FixLengths: true,
}
buf := gopacket.NewSerializeBuffer()
err := gopacket.SerializeLayers(buf, opts, p.tcp, gopacket.Payload(p.tcp.Payload))
return buf.Bytes(), err
}
// SetDstIP sets the destination IP.
func (p *TCPPacket) SetDstIP(dst net.IP) {
p.ip.DstIP = dst.To4()
}
// SetSrcIP sets the source IP.
func (p *TCPPacket) SetSrcIP(src net.IP) {
p.ip.SrcIP = src.To4()
}
func (p *TCPPacket) String() string {
return fmt.Sprintf("%s:%d -> %s:%d [ACK: %t, SYN: %t, RST: %t] [Seq: %d, Ack: %d]", p.ip.SrcIP, p.tcp.SrcPort, p.ip.DstIP, p.tcp.DstPort, p.tcp.ACK, p.tcp.SYN, p.tcp.RST, p.tcp.Seq, p.tcp.Ack)
}