destination.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. return UnixDestination(DomainAddress(addr.Name))
  21. default:
  22. panic("Net: Unknown address type.")
  23. }
  24. }
  25. // ParseDestination converts a destination from its string presentation.
  26. func ParseDestination(dest string) (Destination, error) {
  27. d := Destination{
  28. Address: AnyIP,
  29. Port: Port(0),
  30. }
  31. switch {
  32. case strings.HasPrefix(dest, "tcp:"):
  33. d.Network = Network_TCP
  34. dest = dest[4:]
  35. case strings.HasPrefix(dest, "udp:"):
  36. d.Network = Network_UDP
  37. dest = dest[4:]
  38. case strings.HasPrefix(dest, "unix:"):
  39. d = UnixDestination(DomainAddress(dest[5:]))
  40. return d, nil
  41. }
  42. hstr, pstr, err := SplitHostPort(dest)
  43. if err != nil {
  44. return d, err
  45. }
  46. if len(hstr) > 0 {
  47. d.Address = ParseAddress(hstr)
  48. }
  49. if len(pstr) > 0 {
  50. port, err := PortFromString(pstr)
  51. if err != nil {
  52. return d, err
  53. }
  54. d.Port = port
  55. }
  56. return d, nil
  57. }
  58. // TCPDestination creates a TCP destination with given address
  59. func TCPDestination(address Address, port Port) Destination {
  60. return Destination{
  61. Network: Network_TCP,
  62. Address: address,
  63. Port: port,
  64. }
  65. }
  66. // UDPDestination creates a UDP destination with given address
  67. func UDPDestination(address Address, port Port) Destination {
  68. return Destination{
  69. Network: Network_UDP,
  70. Address: address,
  71. Port: port,
  72. }
  73. }
  74. // UnixDestination creates a Unix destination with given address
  75. func UnixDestination(address Address) Destination {
  76. return Destination{
  77. Network: Network_UNIX,
  78. Address: address,
  79. }
  80. }
  81. // NetAddr returns the network address in this Destination in string form.
  82. func (d Destination) NetAddr() string {
  83. addr := ""
  84. if d.Network == Network_TCP || d.Network == Network_UDP {
  85. addr = d.Address.String() + ":" + d.Port.String()
  86. } else if d.Network == Network_UNIX {
  87. addr = d.Address.String()
  88. }
  89. return addr
  90. }
  91. // String returns the strings form of this Destination.
  92. func (d Destination) String() string {
  93. prefix := "unknown:"
  94. switch d.Network {
  95. case Network_TCP:
  96. prefix = "tcp:"
  97. case Network_UDP:
  98. prefix = "udp:"
  99. case Network_UNIX:
  100. prefix = "unix:"
  101. }
  102. return prefix + d.NetAddr()
  103. }
  104. // IsValid returns true if this Destination is valid.
  105. func (d Destination) IsValid() bool {
  106. return d.Network != Network_Unknown
  107. }
  108. // AsDestination converts current Endpoint into Destination.
  109. func (p *Endpoint) AsDestination() Destination {
  110. return Destination{
  111. Network: p.Network,
  112. Address: p.Address.AsAddress(),
  113. Port: Port(p.Port),
  114. }
  115. }