-
Notifications
You must be signed in to change notification settings - Fork 2
/
runtime.go
76 lines (65 loc) · 1.7 KB
/
runtime.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
// Ethernet over USB driver
//
// Copyright (c) WithSecure Corporation
// https://foundry.withsecure.com
//
// Use of this source code is governed by the license
// that can be found in the LICENSE file.
package usbnet
import (
"context"
"errors"
"net"
"syscall"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/adapters/gonet"
"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
)
// Socket can be used as net.SocketFunc under GOOS=tamago to allow its use
// internal use within the Go runtime.
func (iface *Interface) Socket(ctx context.Context, network string, family, sotype int, laddr, raddr net.Addr) (c interface{}, err error) {
var proto tcpip.NetworkProtocolNumber
var lFullAddr tcpip.FullAddress
var rFullAddr tcpip.FullAddress
if laddr != nil {
if lFullAddr, err = fullAddr(laddr.String()); err != nil {
return
}
}
if raddr != nil {
if rFullAddr, err = fullAddr(raddr.String()); err != nil {
return
}
}
switch family {
case syscall.AF_INET:
proto = ipv4.ProtocolNumber
default:
return nil, errors.New("unsupported address family")
}
switch network {
case "udp", "udp4":
if sotype != syscall.SOCK_DGRAM {
return nil, errors.New("unsupported socket type")
}
if c, err = gonet.DialUDP(iface.Stack, &lFullAddr, &rFullAddr, proto); c != nil {
return
}
case "tcp", "tcp4":
if sotype != syscall.SOCK_STREAM {
return nil, errors.New("unsupported socket type")
}
if raddr != nil {
if c, err = gonet.DialContextTCP(ctx, iface.Stack, rFullAddr, proto); err != nil {
return
}
} else {
if c, err = gonet.ListenTCP(iface.Stack, lFullAddr, proto); err != nil {
return
}
}
default:
return nil, errors.New("unsupported network")
}
return
}