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
|
|
|
|
Port Port
|
2016-12-21 09:37:16 -05:00
|
|
|
Address Address
|
2015-09-20 12:22:29 -04:00
|
|
|
}
|
|
|
|
|
2017-02-20 05:25:05 -05:00
|
|
|
// DestinationFromAddr generates a Destination from a net address.
|
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:
|
2017-02-08 04:01:22 -05:00
|
|
|
panic("Net: Unknown address type.")
|
2016-08-14 17:20:23 -04:00
|
|
|
}
|
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-11-27 15:39:09 -05:00
|
|
|
func (v Destination) NetAddr() string {
|
|
|
|
return v.Address.String() + ":" + v.Port.String()
|
2015-09-20 12:22:29 -04:00
|
|
|
}
|
|
|
|
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v Destination) String() string {
|
2016-12-21 09:37:16 -05:00
|
|
|
return v.Network.URLPrefix() + ":" + v.NetAddr()
|
2015-12-16 17:53:38 -05:00
|
|
|
}
|
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
func (v Destination) IsValid() bool {
|
|
|
|
return v.Network != Network_Unknown
|
|
|
|
}
|
|
|
|
|
2017-02-20 05:25:05 -05:00
|
|
|
// AsDestination converts current Enpoint into Destination.
|
2016-11-27 15:39:09 -05:00
|
|
|
func (v *Endpoint) AsDestination() Destination {
|
2016-09-20 10:05:35 -04:00
|
|
|
return Destination{
|
2016-11-27 15:39:09 -05:00
|
|
|
Network: v.Network,
|
|
|
|
Address: v.Address.AsAddress(),
|
|
|
|
Port: Port(v.Port),
|
2016-09-20 10:05:35 -04:00
|
|
|
}
|
|
|
|
}
|