destination.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package net
  2. import (
  3. "net"
  4. )
  5. // Destination represents a network destination including address and protocol (tcp / udp).
  6. type Destination interface {
  7. Network() Network // Protocol of communication (tcp / udp)
  8. Address() Address // Address of destination
  9. Port() Port
  10. String() string // String representation of the destination
  11. NetAddr() string
  12. Equals(Destination) bool
  13. IsTCP() bool // True if destination is reachable via TCP
  14. IsUDP() bool // True if destination is reachable via UDP
  15. }
  16. func DestinationFromAddr(addr net.Addr) Destination {
  17. switch addr := addr.(type) {
  18. case *net.TCPAddr:
  19. return TCPDestination(IPAddress(addr.IP), Port(addr.Port))
  20. case *net.UDPAddr:
  21. return UDPDestination(IPAddress(addr.IP), Port(addr.Port))
  22. default:
  23. panic("Unknown address type.")
  24. }
  25. }
  26. // TCPDestination creates a TCP destination with given address
  27. func TCPDestination(address Address, port Port) Destination {
  28. return &tcpDestination{address: address, port: port}
  29. }
  30. // UDPDestination creates a UDP destination with given address
  31. func UDPDestination(address Address, port Port) Destination {
  32. return &udpDestination{address: address, port: port}
  33. }
  34. type tcpDestination struct {
  35. address Address
  36. port Port
  37. }
  38. func (dest *tcpDestination) Network() Network {
  39. return TCPNetwork
  40. }
  41. func (dest *tcpDestination) Address() Address {
  42. return dest.address
  43. }
  44. func (dest *tcpDestination) NetAddr() string {
  45. return dest.address.String() + ":" + dest.port.String()
  46. }
  47. func (dest *tcpDestination) String() string {
  48. return "tcp:" + dest.NetAddr()
  49. }
  50. func (dest *tcpDestination) IsTCP() bool {
  51. return true
  52. }
  53. func (dest *tcpDestination) IsUDP() bool {
  54. return false
  55. }
  56. func (dest *tcpDestination) Port() Port {
  57. return dest.port
  58. }
  59. func (dest *tcpDestination) Equals(another Destination) bool {
  60. if dest == nil && another == nil {
  61. return true
  62. }
  63. if dest == nil || another == nil {
  64. return false
  65. }
  66. if !another.IsTCP() {
  67. return false
  68. }
  69. return dest.Port() == another.Port() && dest.Address().Equals(another.Address())
  70. }
  71. type udpDestination struct {
  72. address Address
  73. port Port
  74. }
  75. func (dest *udpDestination) Network() Network {
  76. return UDPNetwork
  77. }
  78. func (dest *udpDestination) Address() Address {
  79. return dest.address
  80. }
  81. func (dest *udpDestination) NetAddr() string {
  82. return dest.address.String() + ":" + dest.port.String()
  83. }
  84. func (dest *udpDestination) String() string {
  85. return "udp:" + dest.NetAddr()
  86. }
  87. func (dest *udpDestination) IsTCP() bool {
  88. return false
  89. }
  90. func (dest *udpDestination) IsUDP() bool {
  91. return true
  92. }
  93. func (dest *udpDestination) Port() Port {
  94. return dest.port
  95. }
  96. func (dest *udpDestination) Equals(another Destination) bool {
  97. if dest == nil && another == nil {
  98. return true
  99. }
  100. if dest == nil || another == nil {
  101. return false
  102. }
  103. if !another.IsUDP() {
  104. return false
  105. }
  106. return dest.Port() == another.Port() && dest.Address().Equals(another.Address())
  107. }