destination.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package net
  2. // Destination represents a network destination including address and protocol (tcp / udp).
  3. type Destination interface {
  4. Network() string // Protocol of communication (tcp / udp)
  5. Address() Address // Address of destination
  6. Port() Port
  7. String() string // String representation of the destination
  8. NetAddr() string
  9. IsTCP() bool // True if destination is reachable via TCP
  10. IsUDP() bool // True if destination is reachable via UDP
  11. }
  12. // TCPDestination creates a TCP destination with given address
  13. func TCPDestination(address Address, port Port) Destination {
  14. return &tcpDestination{address: address, port: port}
  15. }
  16. // UDPDestination creates a UDP destination with given address
  17. func UDPDestination(address Address, port Port) Destination {
  18. return &udpDestination{address: address, port: port}
  19. }
  20. type tcpDestination struct {
  21. address Address
  22. port Port
  23. }
  24. func (dest *tcpDestination) Network() string {
  25. return "tcp"
  26. }
  27. func (dest *tcpDestination) Address() Address {
  28. return dest.address
  29. }
  30. func (dest *tcpDestination) NetAddr() string {
  31. return dest.address.String() + ":" + dest.port.String()
  32. }
  33. func (dest *tcpDestination) String() string {
  34. return "tcp:" + dest.NetAddr()
  35. }
  36. func (dest *tcpDestination) IsTCP() bool {
  37. return true
  38. }
  39. func (dest *tcpDestination) IsUDP() bool {
  40. return false
  41. }
  42. func (dest *tcpDestination) Port() Port {
  43. return dest.port
  44. }
  45. type udpDestination struct {
  46. address Address
  47. port Port
  48. }
  49. func (dest *udpDestination) Network() string {
  50. return "udp"
  51. }
  52. func (dest *udpDestination) Address() Address {
  53. return dest.address
  54. }
  55. func (dest *udpDestination) NetAddr() string {
  56. return dest.address.String() + ":" + dest.port.String()
  57. }
  58. func (dest *udpDestination) String() string {
  59. return "udp:" + dest.NetAddr()
  60. }
  61. func (dest *udpDestination) IsTCP() bool {
  62. return false
  63. }
  64. func (dest *udpDestination) IsUDP() bool {
  65. return true
  66. }
  67. func (dest *udpDestination) Port() Port {
  68. return dest.port
  69. }