2015-12-02 12:47:54 +01:00
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2018-11-02 15:47:58 +01:00
|
|
|
"encoding/binary"
|
2016-02-06 00:13:13 +01:00
|
|
|
"strconv"
|
2015-12-02 12:47:54 +01:00
|
|
|
)
|
|
|
|
|
2016-02-06 10:54:41 +01:00
|
|
|
// Port represents a network port in TCP and UDP protocol.
|
2016-05-24 21:55:46 +02:00
|
|
|
type Port uint16
|
2015-12-02 12:47:54 +01:00
|
|
|
|
2016-02-06 10:54:41 +01:00
|
|
|
// PortFromBytes converts a byte array to a Port, assuming bytes are in big endian order.
|
|
|
|
// @unsafe Caller must ensure that the byte array has at least 2 elements.
|
2015-12-02 20:44:01 +00:00
|
|
|
func PortFromBytes(port []byte) Port {
|
2018-11-02 15:47:58 +01:00
|
|
|
return Port(binary.BigEndian.Uint16(port))
|
2015-12-02 12:47:54 +01:00
|
|
|
}
|
|
|
|
|
2016-02-06 10:54:41 +01:00
|
|
|
// PortFromInt converts an integer to a Port.
|
|
|
|
// @error when the integer is not positive or larger then 65535
|
2016-11-27 17:01:44 +01:00
|
|
|
func PortFromInt(val uint32) (Port, error) {
|
|
|
|
if val > 65535 {
|
2017-04-09 15:04:04 +02:00
|
|
|
return Port(0), newError("invalid port range: ", val)
|
2016-02-06 00:13:13 +01:00
|
|
|
}
|
2016-11-27 17:01:44 +01:00
|
|
|
return Port(val), nil
|
2016-02-06 00:13:13 +01:00
|
|
|
}
|
|
|
|
|
2016-02-06 10:54:41 +01:00
|
|
|
// PortFromString converts a string to a Port.
|
|
|
|
// @error when the string is not an integer or the integral value is a not a valid Port.
|
2016-02-06 00:13:13 +01:00
|
|
|
func PortFromString(s string) (Port, error) {
|
2016-11-27 17:01:44 +01:00
|
|
|
val, err := strconv.ParseUint(s, 10, 32)
|
2016-02-06 00:13:13 +01:00
|
|
|
if err != nil {
|
2017-04-09 15:04:04 +02:00
|
|
|
return Port(0), newError("invalid port range: ", s)
|
2016-02-06 00:13:13 +01:00
|
|
|
}
|
2016-11-27 17:01:44 +01:00
|
|
|
return PortFromInt(uint32(val))
|
2016-02-06 00:13:13 +01:00
|
|
|
}
|
|
|
|
|
2017-08-22 15:15:09 +02:00
|
|
|
// Value return the correspoding uint16 value of a Port.
|
|
|
|
func (p Port) Value() uint16 {
|
|
|
|
return uint16(p)
|
2015-12-02 12:47:54 +01:00
|
|
|
}
|
|
|
|
|
2017-08-22 15:15:09 +02:00
|
|
|
// String returns the string presentation of a Port.
|
|
|
|
func (p Port) String() string {
|
2018-11-02 18:20:02 +01:00
|
|
|
return strconv.Itoa(int(p))
|
2015-12-02 12:47:54 +01:00
|
|
|
}
|
2016-01-15 13:39:36 +01:00
|
|
|
|
2017-12-13 15:55:39 +01:00
|
|
|
// FromPort returns the beginning port of this PortRange.
|
2017-08-22 15:15:09 +02:00
|
|
|
func (p PortRange) FromPort() Port {
|
|
|
|
return Port(p.From)
|
2016-08-27 00:04:35 +02:00
|
|
|
}
|
|
|
|
|
2017-08-22 15:15:09 +02:00
|
|
|
// ToPort returns the end port of this PortRange.
|
|
|
|
func (p PortRange) ToPort() Port {
|
|
|
|
return Port(p.To)
|
2016-01-15 13:39:36 +01:00
|
|
|
}
|
2016-01-17 15:20:49 +00:00
|
|
|
|
2017-08-22 15:15:09 +02:00
|
|
|
// Contains returns true if the given port is within the range of a PortRange.
|
|
|
|
func (p PortRange) Contains(port Port) bool {
|
|
|
|
return p.FromPort() <= port && port <= p.ToPort()
|
2016-01-17 15:20:49 +00:00
|
|
|
}
|
2016-12-16 21:39:00 +01:00
|
|
|
|
2017-02-20 11:25:05 +01:00
|
|
|
// SinglePortRange returns a PortRange contains a single port.
|
2017-08-22 15:15:09 +02:00
|
|
|
func SinglePortRange(p Port) *PortRange {
|
2016-12-16 21:39:00 +01:00
|
|
|
return &PortRange{
|
2017-08-22 15:15:09 +02:00
|
|
|
From: uint32(p),
|
|
|
|
To: uint32(p),
|
2016-12-16 21:39:00 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-24 23:43:00 +01:00
|
|
|
|
|
|
|
type MemoryPortRange struct {
|
|
|
|
From Port
|
|
|
|
To Port
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r MemoryPortRange) Contains(port Port) bool {
|
|
|
|
return r.From <= port && port <= r.To
|
|
|
|
}
|
|
|
|
|
|
|
|
type MemoryPortList []MemoryPortRange
|
|
|
|
|
|
|
|
func PortListFromProto(l *PortList) MemoryPortList {
|
|
|
|
mpl := make(MemoryPortList, 0, len(l.Range))
|
|
|
|
for _, r := range l.Range {
|
|
|
|
mpl = append(mpl, MemoryPortRange{From: Port(r.From), To: Port(r.To)})
|
|
|
|
}
|
|
|
|
return mpl
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mpl MemoryPortList) Contains(port Port) bool {
|
|
|
|
for _, pr := range mpl {
|
|
|
|
if pr.Contains(port) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|