-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtun.go
136 lines (116 loc) · 2.65 KB
/
tun.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
package net
import (
"fmt"
"io"
"log"
"github.com/spectrex02/proto/ethernet"
"github.com/spectrex02/proto/ip"
"github.com/spectrex02/proto/raw"
)
type Tun struct {
// file *os.File
file io.ReadWriteCloser
name string
address ethernet.HardwareAddress
netInfo ip.IPSubnetMask
registeredProtocol []LinkNetProtocol
MTU int
buffer chan *ethernet.EthernetFrame
}
func NewDeviceTun(name string, mtu int) (*Tun, error) {
t, err := raw.NewTunDevice(name)
if err != nil {
return nil, err
}
addr, err := ethernet.Address(t.Address())
if err != nil {
return nil, err
}
return &Tun{
file: t.File,
name: t.Name,
address: *addr,
MTU: mtu,
buffer: make(chan *ethernet.EthernetFrame),
}, nil
}
func (t *Tun) Read(data []byte) (int, error) {
return t.file.Read(data)
}
func (t *Tun) Write(data []byte) (int, error) {
return t.file.Write(data)
}
func (t *Tun) Close() error {
return t.file.Close()
}
func (t *Tun) RegisterProtocol(protocol LinkNetProtocol) error {
t.registeredProtocol = append(t.registeredProtocol, protocol)
return nil
}
func (t *Tun) Address() ethernet.HardwareAddress {
return t.address
}
func (t *Tun) Name() string {
return t.name
}
func (t *Tun) NetInfo() ip.IPSubnetMask {
return t.netInfo
}
func (t *Tun) IPAddress() ip.IPAddress {
return t.netInfo.Address
}
func (t *Tun) Subnet() ip.IPAddress {
return t.netInfo.Subnet
}
func (t *Tun) Netmask() ip.IPAddress {
return t.netInfo.Netmask
}
func (t *Tun) RegisterNetInfo(info string) error {
nInfo, err := ip.NewIPSubnetMask(info)
if err != nil {
return err
}
t.netInfo = *nInfo
return nil
}
func (t *Tun) DeviceInfo() {
fmt.Println("----------device info----------")
fmt.Println("name: ", t.name)
fmt.Println("hardware address: ", t.address)
}
func (t *Tun) Handle() {
buffer := make([]byte, t.MTU)
for {
_, err := t.Read(buffer)
if err != nil {
log.Printf("%v error (read): %v\n", t.name, err)
}
frame, err := ethernet.NewEthernet(buffer)
if err != nil {
log.Printf("%v error (read): %v\n", t.name, err)
}
t.buffer <- frame
}
}
func (t *Tun) Next() {
for {
if t.registeredProtocol == nil {
panic("next leyer protocol is not registered")
}
frame := <-t.buffer
for _, protocol := range t.registeredProtocol {
if protocol.Type() == frame.Header.Type {
err := protocol.Handle(frame.Payload())
if err != nil {
log.Printf("%v error: %v\n", t.name, err)
}
}
}
}
}
func (t *Tun) Buffer() chan *ethernet.EthernetFrame {
return t.buffer
}
func (t *Tun) RegisteredProtocol() []LinkNetProtocol {
return t.registeredProtocol
}