srvrec.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* ######################################################################
  4. SRV record support
  5. ##################################################################### */
  6. /*}}}*/
  7. #ifndef SRVREC_H
  8. #define SRVREC_H
  9. #include <arpa/nameser.h>
  10. #include <vector>
  11. #include <string>
  12. #include <tuple>
  13. class SrvRec
  14. {
  15. public:
  16. std::string target;
  17. u_int16_t priority;
  18. u_int16_t weight;
  19. u_int16_t port;
  20. // each server is assigned a interval [start, end] in the space of [0, max]
  21. int random_number_range_start;
  22. int random_number_range_end;
  23. int random_number_range_max;
  24. bool operator<(SrvRec const &other) const {
  25. return this->priority < other.priority;
  26. }
  27. bool operator==(SrvRec const &other) const {
  28. return std::tie(target, priority, weight, port) == std::tie(other.target, other.priority, other.weight, other.port);
  29. }
  30. SrvRec(std::string const Target, u_int16_t const Priority,
  31. u_int16_t const Weight, u_int16_t const Port) :
  32. target(Target), priority(Priority), weight(Weight), port(Port),
  33. random_number_range_start(0), random_number_range_end(0),
  34. random_number_range_max(0) {}
  35. };
  36. /** \brief Get SRV records from host/port (builds the query string internally)
  37. */
  38. bool GetSrvRecords(std::string name, std::vector<SrvRec> &Result);
  39. /** \brief Get SRV records for query string like: _http._tcp.example.com
  40. */
  41. bool GetSrvRecords(std::string host, int port, std::vector<SrvRec> &Result);
  42. /** \brief Pop a single SRV record from the vector of SrvRec taking
  43. * priority and weight into account
  44. */
  45. SrvRec PopFromSrvRecs(std::vector<SrvRec> &Recs);
  46. #endif