srvrec.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. class SrvRec
  13. {
  14. public:
  15. std::string target;
  16. u_int16_t priority;
  17. u_int16_t weight;
  18. u_int16_t port;
  19. // each server is assigned a interval [start, end] in the space of [0, max]
  20. int random_number_range_start;
  21. int random_number_range_end;
  22. int random_number_range_max;
  23. bool operator<(SrvRec const &other) const {
  24. return this->priority < other.priority;
  25. }
  26. SrvRec(std::string const Target, u_int16_t const Priority,
  27. u_int16_t const Weight, u_int16_t const Port) :
  28. target(Target), priority(Priority), weight(Weight), port(Port),
  29. random_number_range_start(0), random_number_range_end(0),
  30. random_number_range_max(0) {}
  31. };
  32. /** \brief Get SRV records from host/port (builds the query string internally)
  33. */
  34. bool GetSrvRecords(std::string name, std::vector<SrvRec> &Result);
  35. /** \brief Get SRV records for query string like: _http._tcp.example.com
  36. */
  37. bool GetSrvRecords(std::string host, int port, std::vector<SrvRec> &Result);
  38. /** \brief Pop a single SRV record from the vector of SrvRec taking
  39. * priority and weight into account
  40. */
  41. SrvRec PopFromSrvRecs(std::vector<SrvRec> &Recs);
  42. #endif