HostAddress.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package types
  2. // Reference: https://www.ietf.org/rfc/rfc4120.txt
  3. // Section: 5.2.5
  4. import (
  5. "bytes"
  6. "github.com/jcmturner/asn1"
  7. "net"
  8. "fmt"
  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. AddrType_IPv4 = 2
  30. AddrType_Directional = 3
  31. AddrType_ChaosNet = 5
  32. AddrType_XNS = 6
  33. AddrType_ISO = 7
  34. AddrType_DECNET_Phase_IV = 12
  35. AddrType_AppleTalk_DDP = 16
  36. AddrType_NetBios = 20
  37. AddrType_IPv6 = 24
  38. )
  39. type HostAddresses []HostAddress
  40. type HostAddress struct {
  41. AddrType int `asn1:"explicit,tag:0"`
  42. Address []byte `asn1:"explicit,tag:1"`
  43. }
  44. func GetHostAddress(s string) (HostAddress, error) {
  45. var h HostAddress
  46. cAddr, _, err := net.SplitHostPort(s)
  47. if err != nil {
  48. return h, fmt.Errorf("Invalid format of client address: %v", err)
  49. }
  50. ip := net.ParseIP(cAddr)
  51. hb, err := ip.MarshalText()
  52. if err != nil {
  53. return h, fmt.Errorf("Could not marshal client's address into bytes: %v", err)
  54. }
  55. var ht int
  56. if ip.To4() != nil {
  57. ht = AddrType_IPv4
  58. } else if ip.To16() != nil {
  59. ht = AddrType_IPv6
  60. } else {
  61. return h, fmt.Errorf("Could not determine client's address types: %v", err)
  62. }
  63. h = HostAddress{
  64. AddrType: ht,
  65. Address: hb,
  66. }
  67. return h, nil
  68. }
  69. func (h *HostAddress) GetAddress() (string, error) {
  70. var b []byte
  71. _, err := asn1.Unmarshal(h.Address, &b)
  72. return string(b), err
  73. }
  74. func HostAddressesEqual(h, a []HostAddress) bool {
  75. if len(h) != len(a) {
  76. return false
  77. }
  78. for _, e := range a {
  79. var found bool
  80. found = false
  81. for _, i := range h {
  82. if e.Equal(i) {
  83. found = true
  84. break
  85. }
  86. }
  87. if !found {
  88. return false
  89. }
  90. }
  91. return true
  92. }
  93. func HostAddressesContains(h []HostAddress, a HostAddress) bool {
  94. for _, e := range h {
  95. if e.Equal(a) {
  96. return true
  97. }
  98. }
  99. return false
  100. }
  101. func (h *HostAddress) Equal(a HostAddress) bool {
  102. if h.AddrType != a.AddrType {
  103. return false
  104. }
  105. return bytes.Equal(h.Address, a.Address)
  106. }
  107. func (h *HostAddresses) Contains(a HostAddress) bool {
  108. for _, e := range *h {
  109. if e.Equal(a) {
  110. return true
  111. }
  112. }
  113. return false
  114. }
  115. func (h *HostAddresses) Equal(a []HostAddress) bool {
  116. if len(*h) != len(a) {
  117. return false
  118. }
  119. for _, e := range a {
  120. if !h.Contains(e) {
  121. return false
  122. }
  123. }
  124. return true
  125. }