2015-12-02 06:47:54 -05:00
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2016-02-05 18:13:13 -05:00
|
|
|
"errors"
|
|
|
|
"strconv"
|
|
|
|
|
2015-12-03 10:57:23 -05:00
|
|
|
"github.com/v2ray/v2ray-core/common/serial"
|
2015-12-02 06:47:54 -05:00
|
|
|
)
|
|
|
|
|
2016-02-05 18:13:13 -05:00
|
|
|
var (
|
|
|
|
// ErrorInvalidPortRage indicates an error during port range parsing.
|
|
|
|
ErrorInvalidPortRange = errors.New("Invalid port range.")
|
|
|
|
)
|
|
|
|
|
2016-02-06 04:54:41 -05:00
|
|
|
// Port represents a network port in TCP and UDP protocol.
|
2015-12-03 10:57:23 -05:00
|
|
|
type Port serial.Uint16Literal
|
2015-12-02 06:47:54 -05:00
|
|
|
|
2016-02-06 04:54:41 -05: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 15:44:01 -05:00
|
|
|
func PortFromBytes(port []byte) Port {
|
2016-01-24 16:28:44 -05:00
|
|
|
return Port(serial.BytesLiteral(port).Uint16Value())
|
2015-12-02 06:47:54 -05:00
|
|
|
}
|
|
|
|
|
2016-02-06 04:54:41 -05:00
|
|
|
// PortFromInt converts an integer to a Port.
|
|
|
|
// @error when the integer is not positive or larger then 65535
|
2016-02-05 18:13:13 -05:00
|
|
|
func PortFromInt(v int) (Port, error) {
|
|
|
|
if v <= 0 || v > 65535 {
|
|
|
|
return Port(0), ErrorInvalidPortRange
|
|
|
|
}
|
|
|
|
return Port(v), nil
|
|
|
|
}
|
|
|
|
|
2016-02-06 04:54:41 -05: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-05 18:13:13 -05:00
|
|
|
func PortFromString(s string) (Port, error) {
|
|
|
|
v, err := strconv.Atoi(s)
|
|
|
|
if err != nil {
|
|
|
|
return Port(0), ErrorInvalidPortRange
|
|
|
|
}
|
|
|
|
return PortFromInt(v)
|
|
|
|
}
|
|
|
|
|
2016-02-06 04:54:41 -05:00
|
|
|
// Value return the correspoding uint16 value of this Port.
|
2015-12-02 06:47:54 -05:00
|
|
|
func (this Port) Value() uint16 {
|
|
|
|
return uint16(this)
|
|
|
|
}
|
|
|
|
|
2016-02-06 04:54:41 -05:00
|
|
|
// Bytes returns the correspoding bytes of this Port, in big endian order.
|
2015-12-02 06:47:54 -05:00
|
|
|
func (this Port) Bytes() []byte {
|
2016-01-18 19:21:07 -05:00
|
|
|
return serial.Uint16Literal(this).Bytes()
|
2015-12-02 06:47:54 -05:00
|
|
|
}
|
|
|
|
|
2016-02-06 04:54:41 -05:00
|
|
|
// String returns the string presentation of this Port.
|
2015-12-02 06:47:54 -05:00
|
|
|
func (this Port) String() string {
|
2015-12-03 10:57:23 -05:00
|
|
|
return serial.Uint16Literal(this).String()
|
2015-12-02 06:47:54 -05:00
|
|
|
}
|
2016-01-15 07:39:36 -05:00
|
|
|
|
2016-02-06 04:54:41 -05:00
|
|
|
// PortRange represents a range of ports.
|
2016-01-15 07:39:36 -05:00
|
|
|
type PortRange struct {
|
|
|
|
From Port
|
|
|
|
To Port
|
|
|
|
}
|
2016-01-17 10:20:49 -05:00
|
|
|
|
2016-02-06 04:54:41 -05:00
|
|
|
// Contains returns true if the given port is within the range of this PortRange.
|
2016-01-17 10:20:49 -05:00
|
|
|
func (this PortRange) Contains(port Port) bool {
|
|
|
|
return this.From <= port && port <= this.To
|
|
|
|
}
|