Skip to content

Commit 54dbe24

Browse files
committed
conn: reconstruct v4 vs v6 receive function based on symtab
This is kind of gross but it's better than the alternatives. Signed-off-by: Jason A. Donenfeld <[email protected]>
1 parent d2fd0c0 commit 54dbe24

File tree

4 files changed

+69
-15
lines changed

4 files changed

+69
-15
lines changed

conn/bind_linux.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,11 @@ again:
148148

149149
var fns []ReceiveFunc
150150
if sock4 != -1 {
151-
fns = append(fns, makeReceiveIPv4(sock4))
151+
fns = append(fns, bind.makeReceiveIPv4(sock4))
152152
bind.sock4 = sock4
153153
}
154154
if sock6 != -1 {
155-
fns = append(fns, makeReceiveIPv6(sock6))
155+
fns = append(fns, bind.makeReceiveIPv6(sock6))
156156
bind.sock6 = sock6
157157
}
158158
if len(fns) == 0 {
@@ -224,15 +224,15 @@ func (bind *LinuxSocketBind) Close() error {
224224
return err2
225225
}
226226

227-
func makeReceiveIPv6(sock int) ReceiveFunc {
227+
func (*LinuxSocketBind) makeReceiveIPv6(sock int) ReceiveFunc {
228228
return func(buff []byte) (int, Endpoint, error) {
229229
var end LinuxSocketEndpoint
230230
n, err := receive6(sock, buff, &end)
231231
return n, &end, err
232232
}
233233
}
234234

235-
func makeReceiveIPv4(sock int) ReceiveFunc {
235+
func (*LinuxSocketBind) makeReceiveIPv4(sock int) ReceiveFunc {
236236
return func(buff []byte) (int, Endpoint, error) {
237237
var end LinuxSocketEndpoint
238238
n, err := receive4(sock, buff, &end)

conn/bind_std.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ again:
118118
}
119119
var fns []ReceiveFunc
120120
if ipv4 != nil {
121-
fns = append(fns, makeReceiveFunc(ipv4, true))
121+
fns = append(fns, bind.makeReceiveIPv4(ipv4))
122122
bind.ipv4 = ipv4
123123
}
124124
if ipv6 != nil {
125-
fns = append(fns, makeReceiveFunc(ipv6, false))
125+
fns = append(fns, bind.makeReceiveIPv6(ipv6))
126126
bind.ipv6 = ipv6
127127
}
128128
if len(fns) == 0 {
@@ -152,16 +152,23 @@ func (bind *StdNetBind) Close() error {
152152
return err2
153153
}
154154

155-
func makeReceiveFunc(conn *net.UDPConn, isIPv4 bool) ReceiveFunc {
155+
func (*StdNetBind) makeReceiveIPv4(conn *net.UDPConn) ReceiveFunc {
156156
return func(buff []byte) (int, Endpoint, error) {
157157
n, endpoint, err := conn.ReadFromUDP(buff)
158-
if isIPv4 && endpoint != nil {
158+
if endpoint != nil {
159159
endpoint.IP = endpoint.IP.To4()
160160
}
161161
return n, (*StdNetEndpoint)(endpoint), err
162162
}
163163
}
164164

165+
func (*StdNetBind) makeReceiveIPv6(conn *net.UDPConn) ReceiveFunc {
166+
return func(buff []byte) (int, Endpoint, error) {
167+
n, endpoint, err := conn.ReadFromUDP(buff)
168+
return n, (*StdNetEndpoint)(endpoint), err
169+
}
170+
}
171+
165172
func (bind *StdNetBind) Send(buff []byte, endpoint Endpoint) error {
166173
var err error
167174
nend, ok := endpoint.(*StdNetEndpoint)

conn/conn.go

+51-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ package conn
88

99
import (
1010
"errors"
11+
"fmt"
1112
"net"
13+
"reflect"
14+
"runtime"
1215
"strings"
1316
)
1417

@@ -69,6 +72,54 @@ type Endpoint interface {
6972
SrcIP() net.IP
7073
}
7174

75+
var (
76+
ErrBindAlreadyOpen = errors.New("bind is already open")
77+
ErrWrongEndpointType = errors.New("endpoint type does not correspond with bind type")
78+
)
79+
80+
func (fn ReceiveFunc) PrettyName() string {
81+
name := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
82+
// 0. cheese/taco.beansIPv6.func12.func21218-fm
83+
name = strings.TrimSuffix(name, "-fm")
84+
// 1. cheese/taco.beansIPv6.func12.func21218
85+
if idx := strings.LastIndexByte(name, '/'); idx != -1 {
86+
name = name[idx+1:]
87+
// 2. taco.beansIPv6.func12.func21218
88+
}
89+
for {
90+
var idx int
91+
for idx = len(name) - 1; idx >= 0; idx-- {
92+
if name[idx] < '0' || name[idx] > '9' {
93+
break
94+
}
95+
}
96+
if idx == len(name)-1 {
97+
break
98+
}
99+
const dotFunc = ".func"
100+
if !strings.HasSuffix(name[:idx+1], dotFunc) {
101+
break
102+
}
103+
name = name[:idx+1-len(dotFunc)]
104+
// 3. taco.beansIPv6.func12
105+
// 4. taco.beansIPv6
106+
}
107+
if idx := strings.LastIndexByte(name, '.'); idx != -1 {
108+
name = name[idx+1:]
109+
// 5. beansIPv6
110+
}
111+
if name == "" {
112+
return fmt.Sprintf("%p", fn)
113+
}
114+
if strings.HasSuffix(name, "IPv4") {
115+
return "v4"
116+
}
117+
if strings.HasSuffix(name, "IPv6") {
118+
return "v6"
119+
}
120+
return name
121+
}
122+
72123
func parseEndpoint(s string) (*net.UDPAddr, error) {
73124
// ensure that the host is an IP address
74125

@@ -98,8 +149,3 @@ func parseEndpoint(s string) (*net.UDPAddr, error) {
98149
}
99150
return addr, err
100151
}
101-
102-
var (
103-
ErrBindAlreadyOpen = errors.New("bind is already open")
104-
ErrWrongEndpointType = errors.New("endpoint type does not correspond with bind type")
105-
)

device/receive.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,15 @@ func (peer *Peer) keepKeyFreshReceiving() {
6969
* IPv4 and IPv6 (separately)
7070
*/
7171
func (device *Device) RoutineReceiveIncoming(recv conn.ReceiveFunc) {
72+
recvName := recv.PrettyName()
7273
defer func() {
73-
device.log.Verbosef("Routine: receive incoming %p - stopped", recv)
74+
device.log.Verbosef("Routine: receive incoming %s - stopped", recvName)
7475
device.queue.decryption.wg.Done()
7576
device.queue.handshake.wg.Done()
7677
device.net.stopping.Done()
7778
}()
7879

79-
device.log.Verbosef("Routine: receive incoming %p - started", recv)
80+
device.log.Verbosef("Routine: receive incoming %s - started", recvName)
8081

8182
// receive datagrams until conn is closed
8283

0 commit comments

Comments
 (0)