-
Notifications
You must be signed in to change notification settings - Fork 6
/
netutil.go
163 lines (145 loc) · 3.42 KB
/
netutil.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
// Package netutil provides helper functions for network.
package netutil
import (
"context"
"errors"
"net"
"sort"
"strconv"
"time"
"github.com/kakao/varlog/pkg/verrors"
)
var (
errNotSupportedNetwork = errors.New("not supported network")
errNotGlobalUnicastAddress = errors.New("not global unicast address")
)
type StoppableListener struct {
*net.TCPListener
ctx context.Context
}
func NewStoppableListener(ctx context.Context, addr string) (*StoppableListener, error) {
ln, err := Listen("tcp", addr)
if err != nil {
return nil, err
}
return &StoppableListener{ln.(*net.TCPListener), ctx}, nil
}
func (ln StoppableListener) Accept() (c net.Conn, err error) {
connc := make(chan *net.TCPConn, 1)
errc := make(chan error, 1)
go func() {
tc, err := ln.AcceptTCP()
if err != nil {
errc <- err
return
}
connc <- tc
}()
select {
case <-ln.ctx.Done():
return nil, verrors.ErrStopped
case err := <-errc:
return nil, err
case tc := <-connc:
if err := tc.SetKeepAlive(true); err != nil {
return nil, err
}
if err := tc.SetKeepAlivePeriod(3 * time.Minute); err != nil {
return nil, err
}
return tc, nil
}
}
func Listen(network, address string) (net.Listener, error) {
lc := net.ListenConfig{
Control: ControlRawNetworkConnection,
}
return lc.Listen(context.Background(), network, address)
}
// GetListenerAddrs returns ip address of given net.Listener. If the net.Listener is not TCP
// listener, GetListenerAddrs returns errNotSupportedNetwork. Returned addresses are sorted that
// advertisable address comes first.
func GetListenerAddrs(addr net.Addr) ([]string, error) {
tcpAddr, err := getTCPListenerAddr(addr)
if err != nil {
return nil, err
}
var ips []net.IP
if tcpAddr.IP.IsUnspecified() {
ips, err = IPs()
if err != nil {
return nil, err
}
} else if tcpAddr.IP.IsLoopback() {
ips = append(ips, tcpAddr.IP)
} else if ip, err := getIP(tcpAddr); err == nil {
ips = append(ips, ip)
}
port := strconv.Itoa(tcpAddr.Port)
ret := make([]string, len(ips))
for i, ip := range ips {
ret[i] = net.JoinHostPort(ip.String(), port)
}
return ret, nil
}
func getIP(addr *net.TCPAddr) (net.IP, error) {
ip := net.ParseIP(addr.IP.String())
if ip == nil {
return nil, errNotGlobalUnicastAddress
}
if !ip.IsGlobalUnicast() {
return nil, errNotGlobalUnicastAddress
}
return ip, nil
}
func getTCPListenerAddr(addr net.Addr) (*net.TCPAddr, error) {
if addr.Network() != "tcp" {
return nil, errNotSupportedNetwork
}
return addr.(*net.TCPAddr), nil
}
// AdvertisableIPs returns a slice of global unicast IPs.
func AdvertisableIPs() ([]net.IP, error) {
ips, err := IPs()
if err != nil {
return nil, err
}
ret := make([]net.IP, 0, len(ips))
for _, ip := range ips {
if ip.IsGlobalUnicast() {
ret = append(ret, ip)
}
}
return ret, nil
}
// IPs returns a slice of net.IP that is usable. Advertisable IP comes first in the returned slice.
func IPs() ([]net.IP, error) {
ifaces, err := net.Interfaces()
if err != nil {
return nil, err
}
var ips []net.IP
for _, iface := range ifaces {
if iface.Flags&net.FlagUp == 0 {
continue
}
addrs, err := iface.Addrs()
if err != nil {
return nil, err
}
if len(addrs) == 0 {
continue
}
for _, addr := range addrs {
addr := addr.(*net.IPNet)
ip := addr.IP.To4()
if ip != nil {
ips = append(ips, ip)
}
}
}
sort.Slice(ips, func(i, j int) bool {
return ips[i].IsGlobalUnicast()
})
return ips, nil
}