Debian Codegraph @

vnet/chunk.go

raw symbols SCIP
1 package vnet
2
3 import (
4 "fmt"
5 "net"
6 "strconv"
7 "strings"
8 "sync/atomic"
9 "time"
10 )
11
12 type tcpFlag uint8
13
14 const (
15 tcpFIN tcpFlag = 0x01
16 tcpSYN tcpFlag = 0x02
17 tcpRST tcpFlag = 0x04
18 tcpPSH tcpFlag = 0x08
19 tcpACK tcpFlag = 0x10
20 )
21
22 func (f tcpFlag) String() string {
23 var sa []string
24 if f&tcpFIN != 0 {
25 sa = append(sa, "FIN")
26 }
27 if f&tcpSYN != 0 {
28 sa = append(sa, "SYN")
29 }
30 if f&tcpRST != 0 {
31 sa = append(sa, "RST")
32 }
33 if f&tcpPSH != 0 {
34 sa = append(sa, "PSH")
35 }
36 if f&tcpACK != 0 {
37 sa = append(sa, "ACK")
38 }
39
40 return strings.Join(sa, "-")
41 }
42
43 // Generate a base36-encoded unique tag
44 // See: https://play.golang.org/p/0ZaAID1q-HN
45 var assignChunkTag = func() func() string { //nolint:gochecknoglobals
46 var tagCtr uint64
47
48 return func() string {
49 n := atomic.AddUint64(&tagCtr, 1)
50 return strconv.FormatUint(n, 36)
51 }
52 }()
53
54 // Chunk represents a packet passed around in the vnet
55 type Chunk interface {
56 setTimestamp() time.Time // used by router
57 getTimestamp() time.Time // used by router
58 getSourceIP() net.IP // used by router
59 getDestinationIP() net.IP // used by router
60 setSourceAddr(address string) error // used by nat
61 setDestinationAddr(address string) error // used by nat
62
63 SourceAddr() net.Addr
64 DestinationAddr() net.Addr
65 UserData() []byte
66 Tag() string
67 Clone() Chunk
68 Network() string // returns "udp" or "tcp"
69 String() string
70 }
71
72 type chunkIP struct {
73 timestamp time.Time
74 sourceIP net.IP
75 destinationIP net.IP
76 tag string
77 }
78
79 func (c *chunkIP) setTimestamp() time.Time {
80 c.timestamp = time.Now()
81 return c.timestamp
82 }
83
84 func (c *chunkIP) getTimestamp() time.Time {
85 return c.timestamp
86 }
87
88 func (c *chunkIP) getDestinationIP() net.IP {
89 return c.destinationIP
90 }
91
92 func (c *chunkIP) getSourceIP() net.IP {
93 return c.sourceIP
94 }
95
96 func (c *chunkIP) Tag() string {
97 return c.tag
98 }
99
100 type chunkUDP struct {
101 chunkIP
102 sourcePort int
103 destinationPort int
104 userData []byte
105 }
106
107 func newChunkUDP(srcAddr, dstAddr *net.UDPAddr) *chunkUDP {
108 return &chunkUDP{
109 chunkIP: chunkIP{
110 sourceIP: srcAddr.IP,
111 destinationIP: dstAddr.IP,
112 tag: assignChunkTag(),
113 },
114 sourcePort: srcAddr.Port,
115 destinationPort: dstAddr.Port,
116 }
117 }
118
119 func (c *chunkUDP) SourceAddr() net.Addr {
120 return &net.UDPAddr{
121 IP: c.sourceIP,
122 Port: c.sourcePort,
123 }
124 }
125
126 func (c *chunkUDP) DestinationAddr() net.Addr {
127 return &net.UDPAddr{
128 IP: c.destinationIP,
129 Port: c.destinationPort,
130 }
131 }
132
133 func (c *chunkUDP) UserData() []byte {
134 return c.userData
135 }
136
137 func (c *chunkUDP) Clone() Chunk {
138 var userData []byte
139 if c.userData != nil {
140 userData = make([]byte, len(c.userData))
141 copy(userData, c.userData)
142 }
143
144 return &chunkUDP{
145 chunkIP: chunkIP{
146 timestamp: c.timestamp,
147 sourceIP: c.sourceIP,
148 destinationIP: c.destinationIP,
149 tag: c.tag,
150 },
151 sourcePort: c.sourcePort,
152 destinationPort: c.destinationPort,
153 userData: userData,
154 }
155 }
156
157 func (c *chunkUDP) Network() string {
158 return udp
159 }
160
161 func (c *chunkUDP) String() string {
162 src := c.SourceAddr()
163 dst := c.DestinationAddr()
164 return fmt.Sprintf("%s chunk %s %s => %s",
165 src.Network(),
166 c.tag,
167 src.String(),
168 dst.String(),
169 )
170 }
171
172 func (c *chunkUDP) setSourceAddr(address string) error {
173 addr, err := net.ResolveUDPAddr(udp, address)
174 if err != nil {
175 return err
176 }
177 c.sourceIP = addr.IP
178 c.sourcePort = addr.Port
179 return nil
180 }
181
182 func (c *chunkUDP) setDestinationAddr(address string) error {
183 addr, err := net.ResolveUDPAddr(udp, address)
184 if err != nil {
185 return err
186 }
187 c.destinationIP = addr.IP
188 c.destinationPort = addr.Port
189 return nil
190 }
191
192 type chunkTCP struct {
193 chunkIP
194 sourcePort int
195 destinationPort int
196 flags tcpFlag // control bits
197 userData []byte // only with PSH flag
198 // seq uint32 // always starts with 0
199 // ack uint32 // always starts with 0
200 }
201
202 func newChunkTCP(srcAddr, dstAddr *net.TCPAddr, flags tcpFlag) *chunkTCP {
203 return &chunkTCP{
204 chunkIP: chunkIP{
205 sourceIP: srcAddr.IP,
206 destinationIP: dstAddr.IP,
207 tag: assignChunkTag(),
208 },
209 sourcePort: srcAddr.Port,
210 destinationPort: dstAddr.Port,
211 flags: flags,
212 }
213 }
214
215 func (c *chunkTCP) SourceAddr() net.Addr {
216 return &net.TCPAddr{
217 IP: c.sourceIP,
218 Port: c.sourcePort,
219 }
220 }
221
222 func (c *chunkTCP) DestinationAddr() net.Addr {
223 return &net.TCPAddr{
224 IP: c.destinationIP,
225 Port: c.destinationPort,
226 }
227 }
228
229 func (c *chunkTCP) UserData() []byte {
230 return c.userData
231 }
232
233 func (c *chunkTCP) Clone() Chunk {
234 userData := make([]byte, len(c.userData))
235 copy(userData, c.userData)
236
237 return &chunkTCP{
238 chunkIP: chunkIP{
239 timestamp: c.timestamp,
240 sourceIP: c.sourceIP,
241 destinationIP: c.destinationIP,
242 },
243 sourcePort: c.sourcePort,
244 destinationPort: c.destinationPort,
245 userData: userData,
246 }
247 }
248
249 func (c *chunkTCP) Network() string {
250 return "tcp"
251 }
252
253 func (c *chunkTCP) String() string {
254 src := c.SourceAddr()
255 dst := c.DestinationAddr()
256 return fmt.Sprintf("%s %s chunk %s %s => %s",
257 src.Network(),
258 c.flags.String(),
259 c.tag,
260 src.String(),
261 dst.String(),
262 )
263 }
264
265 func (c *chunkTCP) setSourceAddr(address string) error {
266 addr, err := net.ResolveTCPAddr("tcp", address)
267 if err != nil {
268 return err
269 }
270 c.sourceIP = addr.IP
271 c.sourcePort = addr.Port
272 return nil
273 }
274
275 func (c *chunkTCP) setDestinationAddr(address string) error {
276 addr, err := net.ResolveTCPAddr("tcp", address)
277 if err != nil {
278 return err
279 }
280 c.destinationIP = addr.IP
281 c.destinationPort = addr.Port
282 return nil
283 }