types.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package main
  2. import (
  3. "github.com/miekg/dns"
  4. )
  5. type Options struct {
  6. Serial int
  7. Ttl int
  8. }
  9. type Record struct {
  10. RR dns.RR
  11. Weight int
  12. }
  13. type Records []Record
  14. func (s Records) Len() int { return len(s) }
  15. func (s Records) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  16. type RecordsByWeight struct{ Records }
  17. func (s RecordsByWeight) Less(i, j int) bool { return s.Records[i].Weight > s.Records[j].Weight }
  18. type Label struct {
  19. Label string
  20. MaxHosts int
  21. Ttl int
  22. Records map[uint16]Records
  23. Weight map[uint16]int
  24. }
  25. type labels map[string]*Label
  26. type Zones map[string]*Zone
  27. type Zone struct {
  28. Origin string
  29. Labels labels
  30. LenLabels int
  31. Options Options
  32. }
  33. func (z *Zone) findLabels(s, cc string, qtype uint16) *Label {
  34. selectors := []string{}
  35. if len(cc) > 0 {
  36. if len(s) > 0 {
  37. cc = s + "." + cc
  38. }
  39. selectors = append(selectors, cc)
  40. }
  41. // TODO(ask) Add continent, see https://github.com/abh/geodns/issues/1
  42. selectors = append(selectors, s)
  43. for _, name := range selectors {
  44. if label, ok := z.Labels[name]; ok {
  45. // return the label if it has the right records
  46. if label.Records[qtype] != nil {
  47. return label
  48. }
  49. }
  50. }
  51. return z.Labels[s]
  52. }