HostAddress.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package types
  2. // Reference: https://www.ietf.org/rfc/rfc4120.txt
  3. // Section: 5.2.5
  4. import (
  5. "bytes"
  6. "fmt"
  7. "github.com/jcmturner/asn1"
  8. "net"
  9. )
  10. /*
  11. HostAddress and HostAddresses
  12. HostAddress ::= SEQUENCE {
  13. addr-type [0] Int32,
  14. address [1] OCTET STRING
  15. }
  16. -- NOTE: HostAddresses is always used as an OPTIONAL field and
  17. -- should not be empty.
  18. HostAddresses -- NOTE: subtly different from rfc1510,
  19. -- but has a value mapping and encodes the same
  20. ::= SEQUENCE OF HostAddress
  21. The host address encodings consist of two fields:
  22. addr-type
  23. This field specifies the type of address that follows. Pre-
  24. defined values for this field are specified in Section 7.5.3.
  25. address
  26. This field encodes a single address of type addr-type.
  27. */
  28. const (
  29. addrTypeIPv4 = 2
  30. addrTypeDirectional = 3
  31. addrTypeChaosNet = 5
  32. addrTypeXNS = 6
  33. addrTypeISO = 7
  34. addrTypeDECNETPhaseIV = 12
  35. addrTypeAppleTalkDDP = 16
  36. addrTypeNetBios = 20
  37. addrTypeIPv6 = 24
  38. )
  39. // HostAddresses implements RFC 4120 type: https://tools.ietf.org/html/rfc4120#section-5.2.5
  40. type HostAddresses []HostAddress
  41. // HostAddress implements RFC 4120 type: https://tools.ietf.org/html/rfc4120#section-5.2.5
  42. type HostAddress struct {
  43. AddrType int `asn1:"explicit,tag:0"`
  44. Address []byte `asn1:"explicit,tag:1"`
  45. }
  46. // GetHostAddress returns a HostAddress struct from a string in the format <hostname>:<port>
  47. func GetHostAddress(s string) (HostAddress, error) {
  48. var h HostAddress
  49. cAddr, _, err := net.SplitHostPort(s)
  50. if err != nil {
  51. return h, fmt.Errorf("Invalid format of client address: %v", err)
  52. }
  53. ip := net.ParseIP(cAddr)
  54. hb, err := ip.MarshalText()
  55. if err != nil {
  56. return h, fmt.Errorf("Could not marshal client's address into bytes: %v", err)
  57. }
  58. var ht int
  59. if ip.To4() != nil {
  60. ht = addrTypeIPv4
  61. } else if ip.To16() != nil {
  62. ht = addrTypeIPv6
  63. } else {
  64. return h, fmt.Errorf("Could not determine client's address types: %v", err)
  65. }
  66. h = HostAddress{
  67. AddrType: ht,
  68. Address: hb,
  69. }
  70. return h, nil
  71. }
  72. // GetAddress returns a string representation of the HostAddress.
  73. func (h *HostAddress) GetAddress() (string, error) {
  74. var b []byte
  75. _, err := asn1.Unmarshal(h.Address, &b)
  76. return string(b), err
  77. }
  78. // HostAddressesEqual tests if two HostAddress slices are equal.
  79. func HostAddressesEqual(h, a []HostAddress) bool {
  80. if len(h) != len(a) {
  81. return false
  82. }
  83. for _, e := range a {
  84. var found bool
  85. found = false
  86. for _, i := range h {
  87. if e.Equal(i) {
  88. found = true
  89. break
  90. }
  91. }
  92. if !found {
  93. return false
  94. }
  95. }
  96. return true
  97. }
  98. // HostAddressesContains tests if a HostAddress is contained in a HostAddress slice.
  99. func HostAddressesContains(h []HostAddress, a HostAddress) bool {
  100. for _, e := range h {
  101. if e.Equal(a) {
  102. return true
  103. }
  104. }
  105. return false
  106. }
  107. // Equal tests if the HostAddress is equal to another HostAddress provided.
  108. func (h *HostAddress) Equal(a HostAddress) bool {
  109. if h.AddrType != a.AddrType {
  110. return false
  111. }
  112. return bytes.Equal(h.Address, a.Address)
  113. }
  114. // Contains tests if a HostAddress is contained within the HostAddresses struct.
  115. func (h *HostAddresses) Contains(a HostAddress) bool {
  116. for _, e := range *h {
  117. if e.Equal(a) {
  118. return true
  119. }
  120. }
  121. return false
  122. }
  123. // Equal tests if a HostAddress slice is equal to the HostAddresses struct.
  124. func (h *HostAddresses) Equal(a []HostAddress) bool {
  125. if len(*h) != len(a) {
  126. return false
  127. }
  128. for _, e := range a {
  129. if !h.Contains(e) {
  130. return false
  131. }
  132. }
  133. return true
  134. }