-
Notifications
You must be signed in to change notification settings - Fork 611
/
port_allocate_linux.go
108 lines (94 loc) · 3.1 KB
/
port_allocate_linux.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
/*
Copyright The containerd Authors.
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
http://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 portutil
import (
"fmt"
"github.com/containerd/nerdctl/v2/pkg/portutil/iptable"
"github.com/containerd/nerdctl/v2/pkg/portutil/procnet"
)
const (
// This port range is compatible with Docker, FYI https://github.com/moby/moby/blob/eb9e42a09ee123af1d95bf7d46dd738258fa2109/libnetwork/portallocator/portallocator_unix.go#L7-L12
allocateEnd = 60999
)
var (
allocateStart = 49153
)
func filter(ss []procnet.NetworkDetail, filterFunc func(detail procnet.NetworkDetail) bool) (ret []procnet.NetworkDetail) {
for _, s := range ss {
if filterFunc(s) {
ret = append(ret, s)
}
}
return
}
func portAllocate(protocol string, ip string, count uint64) (uint64, uint64, error) {
netprocData, err := procnet.ReadStatsFileData(protocol)
if err != nil {
return 0, 0, err
}
netprocItems := procnet.Parse(netprocData)
// In some circumstances, when we bind address like "0.0.0.0:80", we will get the formation of ":::80" in /proc/net/tcp6.
// So we need some trick to process this situation.
if protocol == "tcp" {
tempTCPV6Data, err := procnet.ReadStatsFileData("tcp6")
if err != nil {
return 0, 0, err
}
netprocItems = append(netprocItems, procnet.Parse(tempTCPV6Data)...)
}
if protocol == "udp" {
tempUDPV6Data, err := procnet.ReadStatsFileData("udp6")
if err != nil {
return 0, 0, err
}
netprocItems = append(netprocItems, procnet.Parse(tempUDPV6Data)...)
}
if ip != "" {
netprocItems = filter(netprocItems, func(s procnet.NetworkDetail) bool {
// In some circumstances, when we bind address like "0.0.0.0:80", we will get the formation of ":::80" in /proc/net/tcp6.
// So we need some trick to process this situation.
return s.LocalIP.String() == "::" || s.LocalIP.String() == ip
})
}
usedPort := make(map[uint64]bool)
for _, value := range netprocItems {
usedPort[value.LocalPort] = true
}
ipTableItems, err := iptable.ReadIPTables("nat")
if err != nil {
return 0, 0, err
}
destinationPorts := iptable.ParseIPTableRules(ipTableItems)
for _, port := range destinationPorts {
usedPort[port] = true
}
start := uint64(allocateStart)
if count > uint64(allocateEnd-allocateStart+1) {
return 0, 0, fmt.Errorf("can not allocate %d ports", count)
}
for start < allocateEnd {
needReturn := true
for i := start; i < start+count; i++ {
if _, ok := usedPort[i]; ok {
needReturn = false
break
}
}
if needReturn {
allocateStart = int(start + count)
return start, start + count - 1, nil
}
start += count
}
return 0, 0, fmt.Errorf("there is not enough %d free ports", count)
}