summaryrefslogtreecommitdiffstats
path: root/apt-pkg/contrib/srvrec.h
blob: e22b7a1c688ce9c46280a9629d7250479abbebe8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// -*- mode: cpp; mode: fold -*-
// Description								/*{{{*/
/* ######################################################################

   SRV record support

   ##################################################################### */
									/*}}}*/
#ifndef SRVREC_H
#define SRVREC_H

#include <string>
#include <vector>
#include <arpa/nameser.h>

class SrvRec
{
 public:
   std::string target;
   u_int16_t priority;
   u_int16_t weight;
   u_int16_t port;

   // each server is assigned a interval [start, end] in the space of [0, max]
   int random_number_range_start;
   int random_number_range_end;
   int random_number_range_max;

   bool operator<(SrvRec const &other) const {
      return this->priority < other.priority;
   }
   bool operator==(SrvRec const &other) const;

   SrvRec(std::string const Target, u_int16_t const Priority,
	 u_int16_t const Weight, u_int16_t const Port) :
      target(Target), priority(Priority), weight(Weight), port(Port),
      random_number_range_start(0), random_number_range_end(0),
      random_number_range_max(0) {}
};

/** \brief Get SRV records from host/port (builds the query string internally) 
 */
bool GetSrvRecords(std::string name, std::vector<SrvRec> &Result);

/** \brief Get SRV records for query string like: _http._tcp.example.com
 */
bool GetSrvRecords(std::string host, int port, std::vector<SrvRec> &Result);

/** \brief Pop a single SRV record from the vector of SrvRec taking
 *         priority and weight into account
 */
SrvRec PopFromSrvRecs(std::vector<SrvRec> &Recs);

#endif