labels.go 4.0 KB

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