2015-12-02 06:47:54 -05:00
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2016-02-05 18:13:13 -05:00
|
|
|
"strconv"
|
|
|
|
|
2016-12-04 03:10:47 -05:00
|
|
|
"v2ray.com/core/common/errors"
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common/serial"
|
2015-12-02 06:47:54 -05:00
|
|
|
)
|
|
|
|
|
2016-02-06 04:54:41 -05:00
|
|
|
// Port represents a network port in TCP and UDP protocol.
|
2016-05-24 15:55:46 -04:00
|
|
|
type Port uint16
|
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-05-24 16:09:22 -04:00
|
|
|
return Port(serial.BytesToUint16(port))
|
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-11-27 11:01:44 -05:00
|
|
|
func PortFromInt(val uint32) (Port, error) {
|
|
|
|
if val > 65535 {
|
2017-02-02 08:42:31 -05:00
|
|
|
return Port(0), errors.New("Net: Invalid port range: ", val)
|
2016-02-05 18:13:13 -05:00
|
|
|
}
|
2016-11-27 11:01:44 -05:00
|
|
|
return Port(val), nil
|
2016-02-05 18:13:13 -05:00
|
|
|
}
|
|
|
|
|
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) {
|
2016-11-27 11:01:44 -05:00
|
|
|
val, err := strconv.ParseUint(s, 10, 32)
|
2016-02-05 18:13:13 -05:00
|
|
|
if err != nil {
|
2017-02-02 08:42:31 -05:00
|
|
|
return Port(0), errors.New("Net: Invalid port range: ", s)
|
2016-02-05 18:13:13 -05:00
|
|
|
}
|
2016-11-27 11:01:44 -05:00
|
|
|
return PortFromInt(uint32(val))
|
2016-02-05 18:13:13 -05:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
// Value return the correspoding uint16 value of v Port.
|
|
|
|
func (v Port) Value() uint16 {
|
|
|
|
return uint16(v)
|
2015-12-02 06:47:54 -05:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
// Bytes returns the correspoding bytes of v Port, in big endian order.
|
|
|
|
func (v Port) Bytes(b []byte) []byte {
|
|
|
|
return serial.Uint16ToBytes(v.Value(), b)
|
2015-12-02 06:47:54 -05:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
// String returns the string presentation of v Port.
|
|
|
|
func (v Port) String() string {
|
|
|
|
return serial.Uint16ToString(v.Value())
|
2015-12-02 06:47:54 -05:00
|
|
|
}
|
2016-01-15 07:39:36 -05:00
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v PortRange) FromPort() Port {
|
|
|
|
return Port(v.From)
|
2016-08-26 18:04:35 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v PortRange) ToPort() Port {
|
|
|
|
return Port(v.To)
|
2016-01-15 07:39:36 -05:00
|
|
|
}
|
2016-01-17 10:20:49 -05:00
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
// Contains returns true if the given port is within the range of v PortRange.
|
|
|
|
func (v PortRange) Contains(port Port) bool {
|
|
|
|
return v.FromPort() <= port && port <= v.ToPort()
|
2016-01-17 10:20:49 -05:00
|
|
|
}
|
2016-12-16 15:39:00 -05:00
|
|
|
|
2017-02-20 05:25:05 -05:00
|
|
|
// SinglePortRange returns a PortRange contains a single port.
|
2016-12-16 15:39:00 -05:00
|
|
|
func SinglePortRange(v Port) *PortRange {
|
|
|
|
return &PortRange{
|
|
|
|
From: uint32(v),
|
|
|
|
To: uint32(v),
|
|
|
|
}
|
|
|
|
}
|