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