srvrec.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* ######################################################################
  4. SRV record support
  5. ##################################################################### */
  6. /*}}}*/
  7. #include <config.h>
  8. #include <netdb.h>
  9. #include <netinet/in.h>
  10. #include <arpa/nameser.h>
  11. #include <resolv.h>
  12. #include <time.h>
  13. #include <algorithm>
  14. #include <tuple>
  15. #include <apt-pkg/configuration.h>
  16. #include <apt-pkg/error.h>
  17. #include <apt-pkg/strutl.h>
  18. #include "srvrec.h"
  19. bool SrvRec::operator==(SrvRec const &other) const
  20. {
  21. return (std::tie(target, priority, weight, port) ==
  22. std::tie(other.target, other.priority, other.weight, other.port));
  23. }
  24. bool GetSrvRecords(std::string host, int port, std::vector<SrvRec> &Result)
  25. {
  26. std::string target;
  27. int res;
  28. struct servent s_ent_buf;
  29. struct servent *s_ent = nullptr;
  30. std::vector<char> buf(1024);
  31. res = getservbyport_r(htons(port), "tcp", &s_ent_buf, buf.data(), buf.size(), &s_ent);
  32. if (res != 0 || s_ent == nullptr)
  33. return false;
  34. strprintf(target, "_%s._tcp.%s", s_ent->s_name, host.c_str());
  35. return GetSrvRecords(target, Result);
  36. }
  37. bool GetSrvRecords(std::string name, std::vector<SrvRec> &Result)
  38. {
  39. unsigned char answer[NS_PACKETSZ];
  40. int answer_len, compressed_name_len;
  41. int answer_count;
  42. if (res_init() != 0)
  43. return _error->Errno("res_init", "Failed to init resolver");
  44. answer_len = res_query(name.c_str(), C_IN, T_SRV, answer, sizeof(answer));
  45. if (answer_len == -1)
  46. return false;
  47. if (answer_len < (int)sizeof(HEADER))
  48. return _error->Warning("Not enough data from res_query (%i)", answer_len);
  49. // check the header
  50. HEADER *header = (HEADER*)answer;
  51. if (header->rcode != NOERROR)
  52. return _error->Warning("res_query returned rcode %i", header->rcode);
  53. answer_count = ntohs(header->ancount);
  54. if (answer_count <= 0)
  55. return _error->Warning("res_query returned no answers (%i) ", answer_count);
  56. // skip the header
  57. compressed_name_len = dn_skipname(answer+sizeof(HEADER), answer+answer_len);
  58. if(compressed_name_len < 0)
  59. return _error->Warning("dn_skipname failed %i", compressed_name_len);
  60. // pt points to the first answer record, go over all of them now
  61. unsigned char *pt = answer+sizeof(HEADER)+compressed_name_len+NS_QFIXEDSZ;
  62. while ((int)Result.size() < answer_count && pt < answer+answer_len)
  63. {
  64. u_int16_t type, klass, priority, weight, port, dlen;
  65. char buf[MAXDNAME];
  66. compressed_name_len = dn_skipname(pt, answer+answer_len);
  67. if (compressed_name_len < 0)
  68. return _error->Warning("dn_skipname failed (2): %i",
  69. compressed_name_len);
  70. pt += compressed_name_len;
  71. if (((answer+answer_len) - pt) < 16)
  72. return _error->Warning("packet too short");
  73. // extract the data out of the result buffer
  74. #define extract_u16(target, p) target = *p++ << 8; target |= *p++;
  75. extract_u16(type, pt);
  76. if(type != T_SRV)
  77. return _error->Warning("Unexpected type excepted %x != %x",
  78. T_SRV, type);
  79. extract_u16(klass, pt);
  80. if(klass != C_IN)
  81. return _error->Warning("Unexpected class excepted %x != %x",
  82. C_IN, klass);
  83. pt += 4; // ttl
  84. extract_u16(dlen, pt);
  85. extract_u16(priority, pt);
  86. extract_u16(weight, pt);
  87. extract_u16(port, pt);
  88. #undef extract_u16
  89. compressed_name_len = dn_expand(answer, answer+answer_len, pt, buf, sizeof(buf));
  90. if(compressed_name_len < 0)
  91. return _error->Warning("dn_expand failed %i", compressed_name_len);
  92. pt += compressed_name_len;
  93. // add it to our class
  94. Result.emplace_back(buf, priority, weight, port);
  95. }
  96. // implement load balancing as specified in RFC-2782
  97. // sort them by priority
  98. std::stable_sort(Result.begin(), Result.end());
  99. for(std::vector<SrvRec>::iterator I = Result.begin();
  100. I != Result.end(); ++I)
  101. {
  102. if (_config->FindB("Debug::Acquire::SrvRecs", false) == true)
  103. {
  104. std::cerr << "SrvRecs: got " << I->target
  105. << " prio: " << I->priority
  106. << " weight: " << I->weight
  107. << std::endl;
  108. }
  109. }
  110. return true;
  111. }
  112. SrvRec PopFromSrvRecs(std::vector<SrvRec> &Recs)
  113. {
  114. // FIXME: instead of the simplistic shuffle below use the algorithm
  115. // described in rfc2782 (with weights)
  116. // and figure out how the weights need to be adjusted if
  117. // a host refuses connections
  118. #if 0 // all code below is only needed for the weight adjusted selection
  119. // assign random number ranges
  120. int prev_weight = 0;
  121. int prev_priority = 0;
  122. for(std::vector<SrvRec>::iterator I = Result.begin();
  123. I != Result.end(); ++I)
  124. {
  125. if(prev_priority != I->priority)
  126. prev_weight = 0;
  127. I->random_number_range_start = prev_weight;
  128. I->random_number_range_end = prev_weight + I->weight;
  129. prev_weight = I->random_number_range_end;
  130. prev_priority = I->priority;
  131. if (_config->FindB("Debug::Acquire::SrvRecs", false) == true)
  132. std::cerr << "SrvRecs: got " << I->target
  133. << " prio: " << I->priority
  134. << " weight: " << I->weight
  135. << std::endl;
  136. }
  137. // go over the code in reverse order and note the max random range
  138. int max = 0;
  139. prev_priority = 0;
  140. for(std::vector<SrvRec>::iterator I = Result.end();
  141. I != Result.begin(); --I)
  142. {
  143. if(prev_priority != I->priority)
  144. max = I->random_number_range_end;
  145. I->random_number_range_max = max;
  146. }
  147. #endif
  148. // shuffle in a very simplistic way for now (equal weights)
  149. std::vector<SrvRec>::iterator I = Recs.begin();
  150. std::vector<SrvRec>::iterator const J = std::find_if(Recs.begin(), Recs.end(),
  151. [&I](SrvRec const &J) { return I->priority != J.priority; });
  152. // clock seems random enough.
  153. I += std::max(static_cast<clock_t>(0), clock()) % std::distance(I, J);
  154. SrvRec const selected = std::move(*I);
  155. Recs.erase(I);
  156. if (_config->FindB("Debug::Acquire::SrvRecs", false) == true)
  157. std::cerr << "PopFromSrvRecs: selecting " << selected.target << std::endl;
  158. return selected;
  159. }