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/destination.go

64 lines
1.4 KiB

package net
import (
"net"
)
9 years ago
// Destination represents a network destination including address and protocol (tcp / udp).
type Destination struct {
Network Network
Port Port
Address Address
}
8 years ago
// DestinationFromAddr generates a Destination from a net address.
func DestinationFromAddr(addr net.Addr) Destination {
switch addr := addr.(type) {
case *net.TCPAddr:
return TCPDestination(IPAddress(addr.IP), Port(addr.Port))
case *net.UDPAddr:
return UDPDestination(IPAddress(addr.IP), Port(addr.Port))
default:
8 years ago
panic("Net: Unknown address type.")
}
}
// TCPDestination creates a TCP destination with given address
func TCPDestination(address Address, port Port) Destination {
return Destination{
Network: Network_TCP,
Address: address,
Port: port,
}
}
// UDPDestination creates a UDP destination with given address
func UDPDestination(address Address, port Port) Destination {
return Destination{
Network: Network_UDP,
Address: address,
Port: port,
}
}
7 years ago
func (d Destination) NetAddr() string {
return d.Address.String() + ":" + d.Port.String()
}
7 years ago
func (d Destination) String() string {
return d.Network.URLPrefix() + ":" + d.NetAddr()
}
7 years ago
func (d Destination) IsValid() bool {
return d.Network != Network_Unknown
}
8 years ago
// AsDestination converts current Enpoint into Destination.
7 years ago
func (p *Endpoint) AsDestination() Destination {
return Destination{
7 years ago
Network: p.Network,
Address: p.Address.AsAddress(),
Port: Port(p.Port),
}
}