70 lines
1.8 KiB
Go
Raw Normal View History

2015-12-02 12:47:54 +01:00
package net
import (
2016-02-06 00:13:13 +01:00
"errors"
"strconv"
2016-08-20 20:55:45 +02:00
"v2ray.com/core/common/serial"
2015-12-02 12:47:54 +01:00
)
2016-02-06 00:13:13 +01:00
var (
2016-06-27 08:53:35 +02:00
// ErrInvalidPortRage indicates an error during port range parsing.
ErrInvalidPortRange = errors.New("Invalid port range.")
2016-02-06 00:13:13 +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 {
2016-05-24 22:09:22 +02:00
return Port(serial.BytesToUint16(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 {
2016-06-27 08:53:35 +02:00
return Port(0), ErrInvalidPortRange
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 {
2016-06-27 08:53:35 +02:00
return Port(0), ErrInvalidPortRange
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
}
2016-02-06 10:54:41 +01:00
// Value return the correspoding uint16 value of this Port.
2015-12-02 12:47:54 +01:00
func (this Port) Value() uint16 {
return uint16(this)
}
2016-02-06 10:54:41 +01:00
// Bytes returns the correspoding bytes of this Port, in big endian order.
2016-06-26 22:34:48 +02:00
func (this Port) Bytes(b []byte) []byte {
return serial.Uint16ToBytes(this.Value(), b)
2015-12-02 12:47:54 +01:00
}
2016-02-06 10:54:41 +01:00
// String returns the string presentation of this Port.
2015-12-02 12:47:54 +01:00
func (this Port) String() string {
2016-05-24 21:55:46 +02:00
return serial.Uint16ToString(this.Value())
2015-12-02 12:47:54 +01:00
}
2016-01-15 13:39:36 +01:00
2016-08-27 00:04:35 +02:00
func (this PortRange) FromPort() Port {
return Port(this.From)
}
func (this PortRange) ToPort() Port {
return Port(this.To)
2016-01-15 13:39:36 +01:00
}
2016-01-17 15:20:49 +00:00
2016-02-06 10:54:41 +01:00
// Contains returns true if the given port is within the range of this PortRange.
2016-01-17 15:20:49 +00:00
func (this PortRange) Contains(port Port) bool {
2016-08-27 00:04:35 +02:00
return this.FromPort() <= port && port <= this.ToPort()
2016-01-17 15:20:49 +00:00
}