labels.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package dns
  2. // Holds a bunch of helper functions for dealing with labels.
  3. // SplitDomainName splits a name string into it's labels.
  4. // www.miek.nl. returns []string{"www", "miek", "nl"}
  5. // .www.miek.nl. returns []string{"", "www", "miek", "nl"},
  6. // The root label (.) returns nil. Note that using
  7. // strings.Split(s) will work in most cases, but does not handle
  8. // escaped dots (\.) for instance.
  9. // s must be a syntactically valid domain name, see IsDomainName.
  10. func SplitDomainName(s string) (labels []string) {
  11. if len(s) == 0 {
  12. return nil
  13. }
  14. fqdnEnd := 0 // offset of the final '.' or the length of the name
  15. idx := Split(s)
  16. begin := 0
  17. if s[len(s)-1] == '.' {
  18. fqdnEnd = len(s) - 1
  19. } else {
  20. fqdnEnd = len(s)
  21. }
  22. switch len(idx) {
  23. case 0:
  24. return nil
  25. case 1:
  26. // no-op
  27. default:
  28. end := 0
  29. for i := 1; i < len(idx); i++ {
  30. end = idx[i]
  31. labels = append(labels, s[begin:end-1])
  32. begin = end
  33. }
  34. }
  35. labels = append(labels, s[begin:fqdnEnd])
  36. return labels
  37. }
  38. // CompareDomainName compares the names s1 and s2 and
  39. // returns how many labels they have in common starting from the *right*.
  40. // The comparison stops at the first inequality. The names are not downcased
  41. // before the comparison.
  42. //
  43. // www.miek.nl. and miek.nl. have two labels in common: miek and nl
  44. // www.miek.nl. and www.bla.nl. have one label in common: nl
  45. //
  46. // s1 and s2 must be syntactically valid domain names.
  47. func CompareDomainName(s1, s2 string) (n int) {
  48. s1 = Fqdn(s1)
  49. s2 = Fqdn(s2)
  50. l1 := Split(s1)
  51. l2 := Split(s2)
  52. // the first check: root label
  53. if l1 == nil || l2 == nil {
  54. return
  55. }
  56. j1 := len(l1) - 1 // end
  57. i1 := len(l1) - 2 // start
  58. j2 := len(l2) - 1
  59. i2 := len(l2) - 2
  60. // the second check can be done here: last/only label
  61. // before we fall through into the for-loop below
  62. if s1[l1[j1]:] == s2[l2[j2]:] {
  63. n++
  64. } else {
  65. return
  66. }
  67. for {
  68. if i1 < 0 || i2 < 0 {
  69. break
  70. }
  71. if s1[l1[i1]:l1[j1]] == s2[l2[i2]:l2[j2]] {
  72. n++
  73. } else {
  74. break
  75. }
  76. j1--
  77. i1--
  78. j2--
  79. i2--
  80. }
  81. return
  82. }
  83. // CountLabel counts the the number of labels in the string s.
  84. // s must be a syntactically valid domain name.
  85. func CountLabel(s string) (labels int) {
  86. if s == "." {
  87. return
  88. }
  89. off := 0
  90. end := false
  91. for {
  92. off, end = NextLabel(s, off)
  93. labels++
  94. if end {
  95. return
  96. }
  97. }
  98. }
  99. // Split splits a name s into its label indexes.
  100. // www.miek.nl. returns []int{0, 4, 9}, www.miek.nl also returns []int{0, 4, 9}.
  101. // The root name (.) returns nil. Also see SplitDomainName.
  102. // s must be a syntactically valid domain name.
  103. func Split(s string) []int {
  104. if s == "." {
  105. return nil
  106. }
  107. idx := make([]int, 1, 3)
  108. off := 0
  109. end := false
  110. for {
  111. off, end = NextLabel(s, off)
  112. if end {
  113. return idx
  114. }
  115. idx = append(idx, off)
  116. }
  117. }
  118. // NextLabel returns the index of the start of the next label in the
  119. // string s starting at offset.
  120. // The bool end is true when the end of the string has been reached.
  121. // Also see PrevLabel.
  122. func NextLabel(s string, offset int) (i int, end bool) {
  123. quote := false
  124. for i = offset; i < len(s)-1; i++ {
  125. switch s[i] {
  126. case '\\':
  127. quote = !quote
  128. default:
  129. quote = false
  130. case '.':
  131. if quote {
  132. quote = !quote
  133. continue
  134. }
  135. return i + 1, false
  136. }
  137. }
  138. return i + 1, true
  139. }
  140. // PrevLabel returns the index of the label when starting from the right and
  141. // jumping n labels to the left.
  142. // The bool start is true when the start of the string has been overshot.
  143. // Also see NextLabel.
  144. func PrevLabel(s string, n int) (i int, start bool) {
  145. if n == 0 {
  146. return len(s), false
  147. }
  148. lab := Split(s)
  149. if lab == nil {
  150. return 0, true
  151. }
  152. if n > len(lab) {
  153. return 0, true
  154. }
  155. return lab[len(lab)-n], false
  156. }