2014-01-23 09:43:50 -05:00
|
|
|
package portallocator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2014-06-25 16:17:20 -04:00
|
|
|
"fmt"
|
2014-01-23 10:46:42 -05:00
|
|
|
"net"
|
2014-01-23 09:43:50 -05:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2014-06-26 03:09:19 -04:00
|
|
|
type portMap struct {
|
|
|
|
p map[int]struct{}
|
|
|
|
last int
|
|
|
|
}
|
|
|
|
|
2014-07-01 13:59:11 -04:00
|
|
|
func newPortMap() *portMap {
|
|
|
|
return &portMap{
|
|
|
|
p: map[int]struct{}{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type protoMap map[string]*portMap
|
|
|
|
|
|
|
|
func newProtoMap() protoMap {
|
|
|
|
return protoMap{
|
|
|
|
"tcp": newPortMap(),
|
|
|
|
"udp": newPortMap(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ipMapping map[string]protoMap
|
2014-05-20 18:05:57 -04:00
|
|
|
|
2014-01-23 09:43:50 -05:00
|
|
|
const (
|
|
|
|
BeginPortRange = 49153
|
|
|
|
EndPortRange = 65535
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-06-25 16:17:20 -04:00
|
|
|
ErrAllPortsAllocated = errors.New("all ports are allocated")
|
|
|
|
ErrUnknownProtocol = errors.New("unknown protocol")
|
2014-01-23 09:43:50 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-05-20 18:05:57 -04:00
|
|
|
mutex sync.Mutex
|
2014-01-23 09:43:50 -05:00
|
|
|
|
2014-05-20 18:05:57 -04:00
|
|
|
defaultIP = net.ParseIP("0.0.0.0")
|
|
|
|
globalMap = ipMapping{}
|
|
|
|
)
|
2014-01-23 09:43:50 -05:00
|
|
|
|
2014-06-25 16:17:20 -04:00
|
|
|
type ErrPortAlreadyAllocated struct {
|
|
|
|
ip string
|
|
|
|
port int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewErrPortAlreadyAllocated(ip string, port int) ErrPortAlreadyAllocated {
|
|
|
|
return ErrPortAlreadyAllocated{
|
|
|
|
ip: ip,
|
|
|
|
port: port,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e ErrPortAlreadyAllocated) IP() string {
|
|
|
|
return e.ip
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e ErrPortAlreadyAllocated) Port() int {
|
|
|
|
return e.port
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e ErrPortAlreadyAllocated) IPPort() string {
|
|
|
|
return fmt.Sprintf("%s:%d", e.ip, e.port)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e ErrPortAlreadyAllocated) Error() string {
|
|
|
|
return fmt.Sprintf("Bind for %s:%d failed: port is already allocated", e.ip, e.port)
|
|
|
|
}
|
|
|
|
|
2014-07-01 13:59:11 -04:00
|
|
|
// RequestPort requests new port from global ports pool for specified ip and proto.
|
|
|
|
// If port is 0 it returns first free port. Otherwise it cheks port availability
|
|
|
|
// in pool and return that port or error if port is already busy.
|
2014-01-23 10:46:42 -05:00
|
|
|
func RequestPort(ip net.IP, proto string, port int) (int, error) {
|
2014-05-20 18:05:57 -04:00
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
2014-01-23 09:43:50 -05:00
|
|
|
|
2014-07-01 13:59:11 -04:00
|
|
|
if proto != "tcp" && proto != "udp" {
|
|
|
|
return 0, ErrUnknownProtocol
|
2014-01-23 09:43:50 -05:00
|
|
|
}
|
|
|
|
|
2014-07-01 13:59:11 -04:00
|
|
|
if ip == nil {
|
|
|
|
ip = defaultIP
|
|
|
|
}
|
|
|
|
ipstr := ip.String()
|
|
|
|
protomap, ok := globalMap[ipstr]
|
|
|
|
if !ok {
|
|
|
|
protomap = newProtoMap()
|
|
|
|
globalMap[ipstr] = protomap
|
|
|
|
}
|
|
|
|
mapping := protomap[proto]
|
2014-01-30 17:52:59 -05:00
|
|
|
if port > 0 {
|
2014-07-01 13:59:11 -04:00
|
|
|
if _, ok := mapping.p[port]; !ok {
|
|
|
|
mapping.p[port] = struct{}{}
|
2014-05-20 18:05:57 -04:00
|
|
|
return port, nil
|
2014-01-23 10:46:42 -05:00
|
|
|
}
|
2014-07-01 13:59:11 -04:00
|
|
|
return 0, NewErrPortAlreadyAllocated(ipstr, port)
|
|
|
|
}
|
2014-05-20 18:05:57 -04:00
|
|
|
|
2014-07-01 13:59:11 -04:00
|
|
|
port, err := mapping.findPort()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2014-01-23 09:43:50 -05:00
|
|
|
}
|
2014-07-01 13:59:11 -04:00
|
|
|
return port, nil
|
2014-01-23 09:43:50 -05:00
|
|
|
}
|
|
|
|
|
2014-07-01 13:59:11 -04:00
|
|
|
// ReleasePort releases port from global ports pool for specified ip and proto.
|
2014-01-23 10:46:42 -05:00
|
|
|
func ReleasePort(ip net.IP, proto string, port int) error {
|
2014-05-20 18:05:57 -04:00
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
2014-01-23 09:43:50 -05:00
|
|
|
|
2014-07-01 13:59:11 -04:00
|
|
|
if ip == nil {
|
|
|
|
ip = defaultIP
|
|
|
|
}
|
|
|
|
protomap, ok := globalMap[ip.String()]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
delete(protomap[proto].p, port)
|
2014-01-23 09:43:50 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-01 13:59:11 -04:00
|
|
|
// ReleaseAll releases all ports for all ips.
|
2014-01-23 15:17:28 -05:00
|
|
|
func ReleaseAll() error {
|
2014-05-20 18:05:57 -04:00
|
|
|
mutex.Lock()
|
|
|
|
globalMap = ipMapping{}
|
2014-07-01 13:59:11 -04:00
|
|
|
mutex.Unlock()
|
2014-01-23 15:17:28 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-01 13:59:11 -04:00
|
|
|
func (pm *portMap) findPort() (int, error) {
|
|
|
|
if pm.last == 0 {
|
|
|
|
pm.p[BeginPortRange] = struct{}{}
|
|
|
|
pm.last = BeginPortRange
|
2014-06-26 03:09:19 -04:00
|
|
|
return BeginPortRange, nil
|
|
|
|
}
|
2014-01-23 15:17:28 -05:00
|
|
|
|
2014-07-01 13:59:11 -04:00
|
|
|
for port := pm.last + 1; port != pm.last; port++ {
|
2014-05-20 18:05:57 -04:00
|
|
|
if port > EndPortRange {
|
2014-06-26 03:09:19 -04:00
|
|
|
port = BeginPortRange
|
2014-04-01 01:21:52 -04:00
|
|
|
}
|
2014-05-20 18:05:57 -04:00
|
|
|
|
2014-07-01 13:59:11 -04:00
|
|
|
if _, ok := pm.p[port]; !ok {
|
|
|
|
pm.p[port] = struct{}{}
|
|
|
|
pm.last = port
|
2014-06-26 03:09:19 -04:00
|
|
|
return port, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, ErrAllPortsAllocated
|
2014-03-12 17:22:57 -04:00
|
|
|
}
|