-
Notifications
You must be signed in to change notification settings - Fork 0
/
net_userstack.go
38 lines (32 loc) · 974 Bytes
/
net_userstack.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
package netstack
import (
"context"
"fmt"
"net"
)
// NetUserStack provides a UserStack that is a thin wrapper around the Go net library. Used for testing.
type NetUserStack struct{}
func (nus *NetUserStack) DialTCP(addr net.IP, port uint16) (net.Conn, error) {
return net.Dial("tcp", fmt.Sprintf("%s:%d", addr.String(), port))
}
func (nus *NetUserStack) DialContextTCP(ctx context.Context, addr net.IP, port uint16) (net.Conn, error) {
var d net.Dialer
return d.DialContext(ctx, "tcp", fmt.Sprintf("%s:%d", addr.String(), port))
}
func (nus *NetUserStack) ListenTCP(port uint16) (net.Listener, error) {
return net.Listen("tcp", fmt.Sprintf(":%d", port))
}
func (nus *NetUserStack) DialUDP(lport uint16, addr net.IP, rport uint16) (UDPConn, error) {
var laddr *net.UDPAddr
if lport != 0 {
laddr = &net.UDPAddr{
IP: nil,
Port: int(lport),
}
}
raddr := &net.UDPAddr{
IP: addr,
Port: int(rport),
}
return net.DialUDP("udp", laddr, raddr)
}