destination.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 TCPDestinationFromAddr(addr *net.TCPAddr) Destination {
  17. return TCPDestination(IPAddress(addr.IP), Port(addr.Port))
  18. }
  19. // TCPDestination creates a TCP destination with given address
  20. func TCPDestination(address Address, port Port) Destination {
  21. return &tcpDestination{address: address, port: port}
  22. }
  23. func UDPDestinationFromAddr(addr *net.UDPAddr) Destination {
  24. return UDPDestination(IPAddress(addr.IP), Port(addr.Port))
  25. }
  26. // UDPDestination creates a UDP destination with given address
  27. func UDPDestination(address Address, port Port) Destination {
  28. return &udpDestination{address: address, port: port}
  29. }
  30. type tcpDestination struct {
  31. address Address
  32. port Port
  33. }
  34. func (dest *tcpDestination) Network() Network {
  35. return TCPNetwork
  36. }
  37. func (dest *tcpDestination) Address() Address {
  38. return dest.address
  39. }
  40. func (dest *tcpDestination) NetAddr() string {
  41. return dest.address.String() + ":" + dest.port.String()
  42. }
  43. func (dest *tcpDestination) String() string {
  44. return "tcp:" + dest.NetAddr()
  45. }
  46. func (dest *tcpDestination) IsTCP() bool {
  47. return true
  48. }
  49. func (dest *tcpDestination) IsUDP() bool {
  50. return false
  51. }
  52. func (dest *tcpDestination) Port() Port {
  53. return dest.port
  54. }
  55. func (dest *tcpDestination) Equals(another Destination) bool {
  56. if dest == nil && another == nil {
  57. return true
  58. }
  59. if dest == nil || another == nil {
  60. return false
  61. }
  62. if !another.IsTCP() {
  63. return false
  64. }
  65. return dest.Port() == another.Port() && dest.Address().Equals(another.Address())
  66. }
  67. type udpDestination struct {
  68. address Address
  69. port Port
  70. }
  71. func (dest *udpDestination) Network() Network {
  72. return UDPNetwork
  73. }
  74. func (dest *udpDestination) Address() Address {
  75. return dest.address
  76. }
  77. func (dest *udpDestination) NetAddr() string {
  78. return dest.address.String() + ":" + dest.port.String()
  79. }
  80. func (dest *udpDestination) String() string {
  81. return "udp:" + dest.NetAddr()
  82. }
  83. func (dest *udpDestination) IsTCP() bool {
  84. return false
  85. }
  86. func (dest *udpDestination) IsUDP() bool {
  87. return true
  88. }
  89. func (dest *udpDestination) Port() Port {
  90. return dest.port
  91. }
  92. func (dest *udpDestination) Equals(another Destination) bool {
  93. if dest == nil && another == nil {
  94. return true
  95. }
  96. if dest == nil || another == nil {
  97. return false
  98. }
  99. if !another.IsUDP() {
  100. return false
  101. }
  102. return dest.Port() == another.Port() && dest.Address().Equals(another.Address())
  103. }