-
Notifications
You must be signed in to change notification settings - Fork 73
/
net.go
180 lines (154 loc) · 4.57 KB
/
net.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
//
// Copyright (C) 2019-2020 Vdaas.org Vald team ( kpango, rinx, kmrmt )
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Package net provides net functionality for grpc
package net
import (
"context"
"fmt"
"net"
"strconv"
"strings"
"sync"
"syscall"
"github.com/vdaas/vald/internal/errgroup"
"github.com/vdaas/vald/internal/errors"
"github.com/vdaas/vald/internal/log"
)
const (
localIPv4 = "127.0.0.1"
localIPv6 = "::1"
localHost = "localhost"
defaultPort = 80
)
type (
// Conn is an alias of net.Conn
Conn = net.Conn
// Dialer is an alias of net.Dialer
Dialer = net.Dialer
// ListenConfig is an alias of net.ListenConfig
ListenConfig = net.ListenConfig
// Listener is an alias of net.Listener
Listener = net.Listener
// Resolver is an alias of net.Resolver
Resolver = net.Resolver
)
var (
// DefaultResolver is an alias of net.DefaultResolver
DefaultResolver = net.DefaultResolver
)
// Listen is a wrapper function of the net.Listen function.
func Listen(network, address string) (Listener, error) {
return net.Listen(network, address)
}
// IsLocal returns if the host is the localhost address.
func IsLocal(host string) bool {
return host == localHost ||
host == localIPv4 ||
host == localIPv6
}
// Dial is a wrapper function of the net.Dial function.
func Dial(network, addr string) (conn Conn, err error) {
return net.Dial(network, addr)
}
// Parse parses the hostname, IPv4 or IPv6 address and return the hostname/IP, port number,
// whether the address is IP, and any parsing error occurred.
// The address should contains the port number, otherwise an error will return.
func Parse(addr string) (host string, port uint16, isIP bool, err error) {
host, port, err = SplitHostPort(addr)
isIP = IsIPv6(host) || IsIPv4(host)
if err != nil {
return host, defaultPort, isIP, err
}
return host, port, isIP, err
}
// IsIPv6 returns weather the address is IPv6 address.
func IsIPv6(addr string) bool {
return net.ParseIP(addr) != nil && strings.Count(addr, ":") >= 2
}
// IsIPv4 returns weather the address is IPv4 address.
func IsIPv4(addr string) bool {
return net.ParseIP(addr) != nil && strings.Count(addr, ":") < 2
}
// SplitHostPort splits the address, and return the host/IP address and the port number,
// and any error occurred.
// If it is the loopback address, it will return the loopback address and corresponding port number.
// IPv6 loopback address is not supported yet.
// For more information, please read https://github.com/vdaas/vald/projects/3#card-43504189
func SplitHostPort(hostport string) (host string, port uint16, err error) {
switch {
/* TODO: IPv6 loopback address support
case strings.HasPrefix(hostport, "::"):
hostport = localIPv6 + hostport
*/
case strings.HasPrefix(hostport, ":"):
hostport = localIPv4 + hostport
}
host, portStr, err := net.SplitHostPort(hostport)
if err != nil {
host = hostport
port = defaultPort
}
p, err := strconv.Atoi(portStr)
if err != nil {
port = defaultPort
} else {
port = uint16(p)
}
return host, port, err
}
// ScanPorts scans the given range of port numbers from the host (inclusively),
// and return the list of ports that can be connected through TCP, or any error occurred.
func ScanPorts(ctx context.Context, start, end uint16, host string) (ports []uint16, err error) {
if start > end {
start, end = end, start
}
var rl syscall.Rlimit
if err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rl); err != nil {
return nil, err
}
eg, egctx := errgroup.New(ctx)
eg.Limitation(int(rl.Max) / 2)
var mu sync.Mutex
for i := start; i >= start && i <= end; i++ {
port := i
eg.Go(func() error {
select {
case <-egctx.Done():
return egctx.Err()
default:
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", host, port))
if err != nil {
log.Warn(err)
return nil
}
mu.Lock()
ports = append(ports, port)
mu.Unlock()
if err = conn.Close(); err != nil {
log.Warn(err)
}
return nil
}
})
}
if err = eg.Wait(); err != nil {
return nil, err
}
if len(ports) == 0 {
return nil, errors.ErrNoPortAvailable
}
return ports, nil
}