-
Notifications
You must be signed in to change notification settings - Fork 4
/
zsockif.go
242 lines (223 loc) · 5.24 KB
/
zsockif.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// +build linux
package MoldUDP
import (
"net"
"time"
"github.com/kjx98/golib/nettypes"
)
type zsockIf struct {
zs *ZSocket
dst HardwareAddr
src HardwareAddr
dstIP [4]byte
srcIP [4]byte
bRead bool
port int
fake bool
}
func newZSockIf() McastConn {
return &zsockIf{}
}
func init() {
registerIf("zsock", newZSockIf)
registerIf("zsocket", newZSockIf)
}
func (c *zsockIf) Enabled(opts int) bool {
if (opts & HasRingBuffer) != 0 {
return true
}
if (opts & HasMmsg) != 0 {
return true
}
return false
}
func (c *zsockIf) String() string {
return "ZSocket Intf"
}
func (c *zsockIf) Close() error {
if c.zs == nil {
return errClosed
}
err := c.zs.Close()
c.zs = nil
return err
}
func (c *zsockIf) Open(ip net.IP, port int, ifn *net.Interface) (err error) {
if c.zs != nil {
return errOpened
}
//c.zs, err = NewZSocket(ifn.Index, ENABLE_RX, 1024, 16384, ETH_IP)
c.zs, err = NewZSocket(ifn.Index, ENABLE_RX, 2048, 8192, ETH_IP)
if err != nil {
return
}
c.port = port
c.src = HardwareAddr(make([]byte, 6))
copy(c.src, ifn.HardwareAddr)
log.Info("Using zsocket, listen on", c.src)
//log.Info("Using zsocket, max PacketSize:", c.zs.MaxPacketSize())
fd := c.zs.Fd()
//ReserveRecvBuf(fd)
if err := setBPF(fd, port); err != nil {
log.Info("setBPF", err)
}
if err := JoinPacketMulticast(fd, ip.To4(), ifn); err != nil {
log.Info("add Packet multicast group", err)
} else {
copy(c.dstIP[:], ip.To4())
}
c.bRead = true
return nil
}
func (c *zsockIf) OpenSend(ip net.IP, port int, bLoop bool, ifn *net.Interface) (err error) {
if c.zs != nil {
return errOpened
}
c.zs, err = NewZSocket(ifn.Index, ENABLE_TX|DISABLE_TX_LOSS, 2048, 4096, ETH_IP)
if err != nil {
// if in testing, no return now
if !c.fake {
return
}
}
c.port = port
c.src = HardwareAddr(make([]byte, 6))
copy(c.src, ifn.HardwareAddr)
if adr, err := getIfAddr(ifn); err == nil {
copy(c.srcIP[:], adr.To4())
log.Infof("Use %s for Multicast interface", adr)
}
if dst := ip.To4(); dst != nil {
copy(c.dstIP[:], dst)
}
c.dst = GetMulticastHWAddr(ip)
log.Info("Using zsocket, via", c.src, "mcast on", c.dst)
//log.Info("Using zsocket, max PacketSize:", c.zs.MaxPacketSize())
c.bRead = false
return nil
}
func (c *zsockIf) Send(buff []byte) (int, error) {
if c.bRead {
return 0, errModeRW
}
n := len(buff)
if _, err := c.zs.CopyToBuffer(buff, uint16(len(buff)), c.copyFx); err != nil {
return 0, err
}
if _, err, _ := c.zs.FlushFrames(); err != nil {
log.Error("zsocket flushFrame", err)
return 0, err
}
return n, nil
}
func (c *zsockIf) copyFx(dst, src []byte, l int) uint16 {
if l <= 0 {
l = len(src)
}
copy(dst, c.dst)
copy(dst[6:], c.src)
//dst[12] = 8
//dst[13] = 0
buildRawUDP(dst, l, c.port, c.srcIP[:], c.dstIP[:])
copy(dst[14+28:], src)
/*
if time.Now().Unix() > logTime+1 {
logTime = time.Now().Unix()
f := nettypes.Frame(dst[:l+42])
log.Info("IP packet:", f.String(uint16(l+42), 0))
}
*/
return uint16(l + 28 + 14)
}
func (c *zsockIf) Recv(buff []byte) (int, *net.UDPAddr, error) {
if !c.bRead {
return 0, nil, errModeRW
}
return 0, nil, errNotSupport
}
func (c *zsockIf) MSend(buffs []Packet) (int, error) {
if len(buffs) == 0 {
return 0, nil
}
var n int
for n = 0; n < len(buffs); n++ {
buf := buffs[n]
if _, err := c.zs.CopyToBuffer(buf, uint16(len(buf)), c.copyFx); err != nil {
break
}
}
if n > 0 {
if _, err, _ := c.zs.FlushFrames(); err != nil {
log.Error("zsocket flushFrame", err)
return 0, err
}
return n, nil
}
return 0, nil
}
func (c *zsockIf) MRecv() (buffs []Packet, rAddr *net.UDPAddr, errRet error) {
errRet = errNotSupport
return
}
var logTime int64
func tryLog(ss string) {
if time.Now().Unix() > logTime {
logTime = time.Now().Unix()
log.Info(ss)
}
}
func (c *zsockIf) Listen(fx func([]byte, *net.UDPAddr)) {
// args: interface index, options, ring block count, frameOrder, framesInBlock packet types
// unless you know what you're doing just pay attention to the interface index, whether
// or not you want the tx ring, rx ring, or both enabled, and what nettype you are listening
// for.
rAddr := net.UDPAddr{IP: net.IPv4zero}
c.zs.Listen(func(fb []byte, frameLen, capturedLen uint16) {
ln := capturedLen
f := nettypes.Frame(fb[:ln])
if f.MACEthertype(0) != nettypes.IPv4 {
tryLog("MAC EtherType dismatch")
return
}
mPay, mOff := f.MACPayload(0)
ln -= mOff
ip := nettypes.IPv4_P(mPay)
if ip.Protocol() != nettypes.UDP {
if time.Now().Unix() > logTime {
logTime = time.Now().Unix()
log.Info("IP Proto dismatch")
log.Info("IP packet:", ip.String(ln, 0))
}
return
}
if ln < ip.Length() {
tryLog("IP length too short")
return
}
iPay, iOff := ip.Payload()
udp := nettypes.UDP_P(iPay)
ips := ip.SourceIP()
rAddr.IP = net.IPv4(ips[0], ips[1], ips[2], ips[3])
if int(udp.DestinationPort()) != c.port {
tryLog("UDP port dismatch")
return
}
rAddr.Port = int(udp.SourcePort())
/*
if time.Now().Unix() > logTime+1 {
logTime = time.Now().Unix()
log.Info("IP packet:", ip.String(ln, 0))
}
*/
ln -= iOff
if ln < udp.Length() {
tryLog("UDP length too short")
return
}
// we don't verify checksum
uBuff, uOff := udp.Payload()
ln -= uOff
//fx(uBuff[:ln], &rAddr)
fx(uBuff, &rAddr)
})
}