destination.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package net
  2. import (
  3. "net"
  4. "strings"
  5. )
  6. // Destination represents a network destination including address and protocol (tcp / udp).
  7. type Destination struct {
  8. Address Address
  9. Port Port
  10. Network Network
  11. }
  12. // DestinationFromAddr generates a Destination from a net address.
  13. func DestinationFromAddr(addr net.Addr) Destination {
  14. switch addr := addr.(type) {
  15. case *net.TCPAddr:
  16. return TCPDestination(IPAddress(addr.IP), Port(addr.Port))
  17. case *net.UDPAddr:
  18. return UDPDestination(IPAddress(addr.IP), Port(addr.Port))
  19. case *net.UnixAddr:
  20. // TODO: deal with Unix domain socket
  21. return TCPDestination(LocalHostIP, Port(9))
  22. default:
  23. panic("Net: Unknown address type.")
  24. }
  25. }
  26. // ParseDestination converts a destination from its string presentation.
  27. func ParseDestination(dest string) (Destination, error) {
  28. d := Destination{
  29. Address: AnyIP,
  30. Port: Port(0),
  31. }
  32. if strings.HasPrefix(dest, "tcp:") {
  33. d.Network = Network_TCP
  34. dest = dest[4:]
  35. } else if strings.HasPrefix(dest, "udp:") {
  36. d.Network = Network_UDP
  37. dest = dest[4:]
  38. }
  39. hstr, pstr, err := SplitHostPort(dest)
  40. if err != nil {
  41. return d, err
  42. }
  43. if len(hstr) > 0 {
  44. d.Address = ParseAddress(hstr)
  45. }
  46. if len(pstr) > 0 {
  47. port, err := PortFromString(pstr)
  48. if err != nil {
  49. return d, err
  50. }
  51. d.Port = port
  52. }
  53. return d, nil
  54. }
  55. // TCPDestination creates a TCP destination with given address
  56. func TCPDestination(address Address, port Port) Destination {
  57. return Destination{
  58. Network: Network_TCP,
  59. Address: address,
  60. Port: port,
  61. }
  62. }
  63. // UDPDestination creates a UDP destination with given address
  64. func UDPDestination(address Address, port Port) Destination {
  65. return Destination{
  66. Network: Network_UDP,
  67. Address: address,
  68. Port: port,
  69. }
  70. }
  71. // NetAddr returns the network address in this Destination in string form.
  72. func (d Destination) NetAddr() string {
  73. return d.Address.String() + ":" + d.Port.String()
  74. }
  75. // String returns the strings form of this Destination.
  76. func (d Destination) String() string {
  77. return d.Network.URLPrefix() + ":" + d.NetAddr()
  78. }
  79. // IsValid returns true if this Destination is valid.
  80. func (d Destination) IsValid() bool {
  81. return d.Network != Network_Unknown
  82. }
  83. // AsDestination converts current Endpoint into Destination.
  84. func (p *Endpoint) AsDestination() Destination {
  85. return Destination{
  86. Network: p.Network,
  87. Address: p.Address.AsAddress(),
  88. Port: Port(p.Port),
  89. }
  90. }