2015-09-20 14:03:12 +00:00
|
|
|
package net
|
|
|
|
|
2016-08-14 15:08:01 +00:00
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
2015-09-21 10:15:25 +00:00
|
|
|
// Destination represents a network destination including address and protocol (tcp / udp).
|
2016-09-20 09:53:05 +00:00
|
|
|
type Destination struct {
|
|
|
|
Network Network
|
|
|
|
Address Address
|
|
|
|
Port Port
|
2015-09-20 16:22:29 +00:00
|
|
|
}
|
|
|
|
|
2016-08-14 21:20:23 +00:00
|
|
|
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:
|
|
|
|
panic("Unknown address type.")
|
|
|
|
}
|
2016-08-14 15:08:01 +00:00
|
|
|
}
|
|
|
|
|
2015-12-16 22:53:38 +00:00
|
|
|
// TCPDestination creates a TCP destination with given address
|
|
|
|
func TCPDestination(address Address, port Port) Destination {
|
2016-09-20 09:53:05 +00:00
|
|
|
return Destination{
|
|
|
|
Network: Network_TCP,
|
|
|
|
Address: address,
|
|
|
|
Port: port,
|
|
|
|
}
|
2015-09-20 16:22:29 +00:00
|
|
|
}
|
|
|
|
|
2015-12-16 22:53:38 +00:00
|
|
|
// UDPDestination creates a UDP destination with given address
|
|
|
|
func UDPDestination(address Address, port Port) Destination {
|
2016-09-20 09:53:05 +00:00
|
|
|
return Destination{
|
|
|
|
Network: Network_UDP,
|
|
|
|
Address: address,
|
|
|
|
Port: port,
|
2016-01-20 16:31:43 +00:00
|
|
|
}
|
2015-09-20 14:03:12 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 09:53:05 +00:00
|
|
|
func (this Destination) NetAddr() string {
|
|
|
|
return this.Address.String() + ":" + this.Port.String()
|
2015-09-20 16:22:29 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 09:53:05 +00:00
|
|
|
func (this Destination) String() string {
|
|
|
|
return this.Network.UrlPrefix() + ":" + this.NetAddr()
|
2015-12-16 22:53:38 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 09:53:05 +00:00
|
|
|
func (this Destination) Equals(another Destination) bool {
|
|
|
|
return this.Network == another.Network && this.Port == another.Port && this.Address.Equals(another.Address)
|
2016-01-20 16:31:43 +00:00
|
|
|
}
|
2016-09-20 14:05:35 +00:00
|
|
|
|
2016-10-12 16:43:55 +00:00
|
|
|
func (this *Endpoint) AsDestination() Destination {
|
2016-09-20 14:05:35 +00:00
|
|
|
return Destination{
|
|
|
|
Network: this.Network,
|
|
|
|
Address: this.Address.AsAddress(),
|
|
|
|
Port: Port(this.Port),
|
|
|
|
}
|
|
|
|
}
|