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
|
package main
import (
"context"
"fmt"
"os"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/buffer"
"gvisor.dev/gvisor/pkg/tcpip/header"
"gvisor.dev/gvisor/pkg/tcpip/link/channel"
"gvisor.dev/gvisor/pkg/tcpip/stack"
wgtun "golang.zx2c4.com/wireguard/tun"
)
type GoTun struct {
events chan wgtun.Event
ch *channel.Endpoint
stack *stack.Stack
ctx context.Context
cancel context.CancelFunc
}
func (tun *GoTun) File() *os.File {
fmt.Println("File")
return nil
}
func (tun *GoTun) Read(buff []byte, offset int) (int, error) {
fmt.Println("Read ", len(buff), offset)
p, ok := tun.ch.ReadContext(tun.ctx)
if ok == false {
fmt.Println("Read error")
return 0, nil // FIXME error
}
vv := p.Pkt.Data
v := vv.ToView()
h := p.Pkt.Header.View()
fmt.Println("Read packet", vv.Size(), len(v), len(h), h)
if len(buff) - offset < len(h) + len(v) {
fmt.Println("Short buffer")
return 0, nil // FIXME error
}
copy(buff[offset:], h)
copy(buff[offset+len(h):], v)
return len(h)+len(v), nil
}
func versionToProtocol(version int) tcpip.NetworkProtocolNumber {
switch version {
case header.IPv4Version: return header.IPv4ProtocolNumber
case header.IPv6Version: return header.IPv6ProtocolNumber
}
return 0
}
func (tun *GoTun) Write(buff []byte, offset int) (int, error) {
size := len(buff) - offset
fmt.Println("Write ", len(buff), offset, size)
if size < 1 {
return 0, nil // FIXME error
}
buffSlice := buff[offset : offset+size]
pkt := tcpip.PacketBuffer{
Data: buffer.NewViewFromBytes(buffSlice).ToVectorisedView(),
}
//version := buff[offset] & 0x0f
protocol := versionToProtocol(header.IPVersion(buffSlice))
netProto := tun.stack.NetworkProtocolInstance(protocol)
if netProto == nil {
fmt.Println("Write not ok")
return 0, nil
}
src, dst := netProto.ParseAddresses(pkt.Data.First())
fmt.Println("Write ", src, dst)
// TODO change destination address
tun.ch.InjectInbound(protocol, pkt) // FIXME detect protocol number
return size, nil
}
func (tun *GoTun) Flush() error {
// TODO: can flushing be implemented by buffering and using sendmmsg?
fmt.Println("Flush")
return nil
}
func (tun *GoTun) MTU() (int, error) {
fmt.Println("MTU")
return 1280, nil
}
func (tun *GoTun) Name() (string, error) {
fmt.Println("Name")
return "foobar", nil
}
func (tun *GoTun) Events() chan wgtun.Event {
fmt.Println("Events")
return tun.events
}
func (tun *GoTun) Close() error {
fmt.Println("Close")
// TODO
// tun.cancel()
return nil
}
func CreateGoTun(s *stack.Stack, ch *channel.Endpoint) (wgtun.Device, error) {
size := 16
ctx, cancel := context.WithCancel(context.Background())
tun := &GoTun{
ch: ch,
events: make(chan wgtun.Event, size),
stack: s,
ctx: ctx,
cancel: cancel,
}
// go func() {
fmt.Println("Post event")
tun.events <- wgtun.EventUp
fmt.Println("Posted event")
// }()
return tun, nil
}
|