You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/common/net/port.go

74 lines
1.9 KiB

package net
import (
"strconv"
"v2ray.com/core/common/serial"
)
9 years ago
// Port represents a network port in TCP and UDP protocol.
9 years ago
type Port uint16
9 years ago
// 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.
func PortFromBytes(port []byte) Port {
return Port(serial.BytesToUint16(port))
}
9 years ago
// PortFromInt converts an integer to a Port.
// @error when the integer is not positive or larger then 65535
func PortFromInt(val uint32) (Port, error) {
if val > 65535 {
return Port(0), newError("invalid port range: ", val)
}
return Port(val), nil
}
9 years ago
// 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.
func PortFromString(s string) (Port, error) {
val, err := strconv.ParseUint(s, 10, 32)
if err != nil {
return Port(0), newError("invalid port range: ", s)
}
return PortFromInt(uint32(val))
}
7 years ago
// Value return the correspoding uint16 value of a Port.
func (p Port) Value() uint16 {
return uint16(p)
}
7 years ago
// Bytes returns the correspoding bytes of a Port, in big endian order.
func (p Port) Bytes(b []byte) []byte {
return serial.Uint16ToBytes(p.Value(), b)
}
7 years ago
// String returns the string presentation of a Port.
func (p Port) String() string {
return serial.Uint16ToString(p.Value())
}
7 years ago
// FromPort returns the begining port of this PortRange.
func (p PortRange) FromPort() Port {
return Port(p.From)
8 years ago
}
7 years ago
// ToPort returns the end port of this PortRange.
func (p PortRange) ToPort() Port {
return Port(p.To)
}
7 years ago
// 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()
}
8 years ago
8 years ago
// SinglePortRange returns a PortRange contains a single port.
7 years ago
func SinglePortRange(p Port) *PortRange {
8 years ago
return &PortRange{
7 years ago
From: uint32(p),
To: uint32(p),
8 years ago
}
}