forked from named-data/YaNFD
/
unicast-udp-transport.go
213 lines (181 loc) · 5.59 KB
/
unicast-udp-transport.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
/* YaNFD - Yet another NDN Forwarding Daemon
*
* Copyright (C) 2020-2021 Eric Newberry.
*
* This file is licensed under the terms of the MIT License, as found in LICENSE.md.
*/
package face
import (
"errors"
"net"
"runtime"
"strconv"
"time"
"github.com/eric135/YaNFD/core"
"github.com/eric135/YaNFD/face/impl"
"github.com/eric135/YaNFD/ndn"
"github.com/eric135/YaNFD/ndn/tlv"
)
// UnicastUDPTransport is a unicast UDP transport.
type UnicastUDPTransport struct {
dialer *net.Dialer
conn *net.UDPConn
localAddr net.UDPAddr
remoteAddr net.UDPAddr
transportBase
}
// MakeUnicastUDPTransport creates a new unicast UDP transport.
func MakeUnicastUDPTransport(remoteURI *ndn.URI, localURI *ndn.URI, persistency Persistency) (*UnicastUDPTransport, error) {
// Validate URIs
if !remoteURI.IsCanonical() || (remoteURI.Scheme() != "udp4" && remoteURI.Scheme() != "udp6") || (localURI != nil && !localURI.IsCanonical()) || (localURI != nil && remoteURI.Scheme() != localURI.Scheme()) {
return nil, core.ErrNotCanonical
}
t := new(UnicastUDPTransport)
// All persistencies are accepted
t.makeTransportBase(remoteURI, localURI, persistency, ndn.NonLocal, ndn.PointToPoint, tlv.MaxNDNPacketSize)
t.expirationTime = new(time.Time)
*t.expirationTime = time.Now().Add(udpLifetime)
// Set scope
ip := net.ParseIP(remoteURI.Path())
if ip.IsLoopback() {
t.scope = ndn.Local
} else {
t.scope = ndn.NonLocal
}
// Set local and remote addresses
if localURI != nil {
t.localAddr.IP = net.ParseIP(localURI.Path())
t.localAddr.Port = int(localURI.Port())
} else {
t.localAddr.Port = int(UDPUnicastPort)
}
t.remoteAddr.IP = net.ParseIP(remoteURI.Path())
t.remoteAddr.Port = int(remoteURI.Port())
// Attempt to "dial" remote URI
var err error
// Configure dialer so we can allow address reuse
t.dialer = &net.Dialer{LocalAddr: &t.localAddr, Control: impl.SyscallReuseAddr}
conn, err := t.dialer.Dial(t.remoteURI.Scheme(), net.JoinHostPort(t.remoteURI.Path(), strconv.Itoa(int(t.remoteURI.Port()))))
if err != nil {
return nil, errors.New("Unable to connect to remote endpoint: " + err.Error())
}
t.conn = conn.(*net.UDPConn)
if localURI == nil {
t.localAddr = *t.conn.LocalAddr().(*net.UDPAddr)
t.localURI = ndn.DecodeURIString("udp://" + t.localAddr.String())
}
t.changeState(ndn.Up)
go t.expirationHandler()
return t, nil
}
func (t *UnicastUDPTransport) String() string {
return "UnicastUDPTransport, FaceID=" + strconv.FormatUint(t.faceID, 10) + ", RemoteURI=" + t.remoteURI.String() + ", LocalURI=" + t.localURI.String()
}
// SetPersistency changes the persistency of the face.
func (t *UnicastUDPTransport) SetPersistency(persistency Persistency) bool {
if persistency == t.persistency {
return true
}
t.persistency = persistency
return true
}
// GetSendQueueSize returns the current size of the send queue.
func (t *UnicastUDPTransport) GetSendQueueSize() uint64 {
rawConn, err := t.conn.SyscallConn()
if err != nil {
core.LogWarn(t, "Unable to get raw connection to get socket length: ", err)
}
return impl.SyscallGetSocketSendQueueSize(rawConn)
}
func (t *UnicastUDPTransport) onTransportFailure(fromReceive bool) {
switch t.persistency {
case PersistencyPermanent:
// Restart socket
t.conn.Close()
var err error
conn, err := t.dialer.Dial(t.remoteURI.Scheme(), net.JoinHostPort(t.remoteURI.Path(), strconv.Itoa(int(t.remoteURI.Port()))))
if err != nil {
core.LogError(t, "Unable to connect to remote endpoint: ", err)
}
t.conn = conn.(*net.UDPConn)
if fromReceive {
t.runReceive()
} else {
// Old receive thread will error out, so we need to replace it
go t.runReceive()
}
default:
t.changeState(ndn.Down)
}
}
// expirationHandler checks if the face should expire (if on demand)
func (t *UnicastUDPTransport) expirationHandler() {
for {
time.Sleep(time.Duration(10) * time.Second)
if t.state == ndn.Down {
break
}
if t.persistency == PersistencyOnDemand && (t.expirationTime.Before(time.Now()) || t.expirationTime.Equal(time.Now())) {
core.LogInfo(t, "Face expired")
t.changeState(ndn.Down)
break
}
}
}
func (t *UnicastUDPTransport) sendFrame(frame []byte) {
if len(frame) > t.MTU() {
core.LogWarn(t, "Attempted to send frame larger than MTU - DROP")
return
}
core.LogDebug(t, "Sending frame of size ", len(frame))
_, err := t.conn.Write(frame)
if err != nil {
core.LogWarn(t, "Unable to send on socket - DROP")
t.onTransportFailure(false)
return
}
t.nOutBytes += uint64(len(frame))
*t.expirationTime = time.Now().Add(udpLifetime)
}
func (t *UnicastUDPTransport) runReceive() {
if lockThreadsToCores {
runtime.LockOSThread()
}
recvBuf := make([]byte, tlv.MaxNDNPacketSize)
for {
readSize, err := t.conn.Read(recvBuf)
if err != nil {
if err.Error() == "EOF" {
core.LogDebug(t, "EOF")
} else {
core.LogWarn(t, "Unable to read from socket (", err, ") - DROP")
t.onTransportFailure(true)
}
break
}
core.LogTrace(t, "Receive of size ", readSize)
t.nInBytes += uint64(readSize)
*t.expirationTime = time.Now().Add(udpLifetime)
if readSize > tlv.MaxNDNPacketSize {
core.LogWarn(t, "Received too much data without valid TLV block - DROP")
continue
}
// Send up to link service
t.linkService.handleIncomingFrame(recvBuf[:readSize])
}
}
func (t *UnicastUDPTransport) changeState(new ndn.State) {
if t.state == new {
return
}
core.LogInfo(t, "state: ", t.state, " -> ", new)
t.state = new
if t.state != ndn.Up {
core.LogInfo(t, "Closing UDP socket")
t.hasQuit <- true
t.conn.Close()
// Stop link service
t.linkService.tellTransportQuit()
FaceTable.Remove(t.faceID)
}
}