labels.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 s == "" {
  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 IsFqdn(s) {
  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. for _, end := range idx[1:] {
  29. labels = append(labels, s[begin:end-1])
  30. begin = end
  31. }
  32. }
  33. return append(labels, s[begin:fqdnEnd])
  34. }
  35. // CompareDomainName compares the names s1 and s2 and
  36. // returns how many labels they have in common starting from the *right*.
  37. // The comparison stops at the first inequality. The names are downcased
  38. // before the comparison.
  39. //
  40. // www.miek.nl. and miek.nl. have two labels in common: miek and nl
  41. // www.miek.nl. and www.bla.nl. have one label in common: nl
  42. //
  43. // s1 and s2 must be syntactically valid domain names.
  44. func CompareDomainName(s1, s2 string) (n int) {
  45. // the first check: root label
  46. if s1 == "." || s2 == "." {
  47. return 0
  48. }
  49. l1 := Split(s1)
  50. l2 := Split(s2)
  51. j1 := len(l1) - 1 // end
  52. i1 := len(l1) - 2 // start
  53. j2 := len(l2) - 1
  54. i2 := len(l2) - 2
  55. // the second check can be done here: last/only label
  56. // before we fall through into the for-loop below
  57. if equal(s1[l1[j1]:], s2[l2[j2]:]) {
  58. n++
  59. } else {
  60. return
  61. }
  62. for {
  63. if i1 < 0 || i2 < 0 {
  64. break
  65. }
  66. if equal(s1[l1[i1]:l1[j1]], s2[l2[i2]:l2[j2]]) {
  67. n++
  68. } else {
  69. break
  70. }
  71. j1--
  72. i1--
  73. j2--
  74. i2--
  75. }
  76. return
  77. }
  78. // CountLabel counts the number of labels in the string s.
  79. // s must be a syntactically valid domain name.
  80. func CountLabel(s string) (labels int) {
  81. if s == "." {
  82. return
  83. }
  84. off := 0
  85. end := false
  86. for {
  87. off, end = NextLabel(s, off)
  88. labels++
  89. if end {
  90. return
  91. }
  92. }
  93. }
  94. // Split splits a name s into its label indexes.
  95. // www.miek.nl. returns []int{0, 4, 9}, www.miek.nl also returns []int{0, 4, 9}.
  96. // The root name (.) returns nil. Also see SplitDomainName.
  97. // s must be a syntactically valid domain name.
  98. func Split(s string) []int {
  99. if s == "." {
  100. return nil
  101. }
  102. idx := make([]int, 1, 3)
  103. off := 0
  104. end := false
  105. for {
  106. off, end = NextLabel(s, off)
  107. if end {
  108. return idx
  109. }
  110. idx = append(idx, off)
  111. }
  112. }
  113. // NextLabel returns the index of the start of the next label in the
  114. // string s starting at offset.
  115. // The bool end is true when the end of the string has been reached.
  116. // Also see PrevLabel.
  117. func NextLabel(s string, offset int) (i int, end bool) {
  118. if s == "" {
  119. return 0, true
  120. }
  121. for i = offset; i < len(s)-1; i++ {
  122. if s[i] != '.' {
  123. continue
  124. }
  125. j := i - 1
  126. for j >= 0 && s[j] == '\\' {
  127. j--
  128. }
  129. if (j-i)%2 == 0 {
  130. continue
  131. }
  132. return i + 1, false
  133. }
  134. return i + 1, true
  135. }
  136. // PrevLabel returns the index of the label when starting from the right and
  137. // jumping n labels to the left.
  138. // The bool start is true when the start of the string has been overshot.
  139. // Also see NextLabel.
  140. func PrevLabel(s string, n int) (i int, start bool) {
  141. if s == "" {
  142. return 0, true
  143. }
  144. if n == 0 {
  145. return len(s), false
  146. }
  147. l := len(s) - 1
  148. if s[l] == '.' {
  149. l--
  150. }
  151. for ; l >= 0 && n > 0; l-- {
  152. if s[l] != '.' {
  153. continue
  154. }
  155. j := l - 1
  156. for j >= 0 && s[j] == '\\' {
  157. j--
  158. }
  159. if (j-l)%2 == 0 {
  160. continue
  161. }
  162. n--
  163. if n == 0 {
  164. return l + 1, false
  165. }
  166. }
  167. return 0, n > 1
  168. }
  169. // equal compares a and b while ignoring case. It returns true when equal otherwise false.
  170. func equal(a, b string) bool {
  171. // might be lifted into API function.
  172. la := len(a)
  173. lb := len(b)
  174. if la != lb {
  175. return false
  176. }
  177. for i := la - 1; i >= 0; i-- {
  178. ai := a[i]
  179. bi := b[i]
  180. if ai >= 'A' && ai <= 'Z' {
  181. ai |= 'a' - 'A'
  182. }
  183. if bi >= 'A' && bi <= 'Z' {
  184. bi |= 'a' - 'A'
  185. }
  186. if ai != bi {
  187. return false
  188. }
  189. }
  190. return true
  191. }