]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/srvrec.cc
Merge branch 'debian/experimental' into feature/srv-records
[apt.git] / apt-pkg / contrib / srvrec.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 /* ######################################################################
4
5 SRV record support
6
7 ##################################################################### */
8 /*}}}*/
9 #include <config.h>
10
11 #include <netdb.h>
12
13 #include <netinet/in.h>
14 #include <arpa/nameser.h>
15 #include <resolv.h>
16
17 #include <algorithm>
18
19 #include <apt-pkg/strutl.h>
20 #include <apt-pkg/error.h>
21 #include "srvrec.h"
22
23 bool GetSrvRecords(std::string host, int port, std::vector<SrvRec> &Result)
24 {
25 std::string target;
26 struct servent *s_ent = getservbyport(htons(port), "tcp");
27 if (s_ent == NULL)
28 return false;
29
30 strprintf(target, "_%s._tcp.%s", s_ent->s_name, host.c_str());
31 return GetSrvRecords(target, Result);
32 }
33
34 bool GetSrvRecords(std::string name, std::vector<SrvRec> &Result)
35 {
36 unsigned char answer[PACKETSZ];
37 int answer_len, compressed_name_len;
38 int answer_count;
39
40 if (res_init() != 0)
41 return _error->Errno("res_init", "Failed to init resolver");
42
43 answer_len = res_query(name.c_str(), C_IN, T_SRV, answer, sizeof(answer));
44 if (answer_len < (int)sizeof(HEADER))
45 return _error->Warning("Not enough data from res_query (%i)", answer_len);
46
47 // check the header
48 HEADER *header = (HEADER*)answer;
49 if (header->rcode != NOERROR)
50 return _error->Warning("res_query returned rcode %i", header->rcode);
51 answer_count = ntohs(header->ancount);
52 if (answer_count <= 0)
53 return _error->Warning("res_query returned no answers (%i) ", answer_count);
54
55 // skip the header
56 compressed_name_len = dn_skipname(answer+sizeof(HEADER), answer+answer_len);
57 if(compressed_name_len < 0)
58 return _error->Warning("dn_skipname failed %i", compressed_name_len);
59
60 // pt points to the first answer record, go over all of them now
61 unsigned char *pt = answer+sizeof(HEADER)+compressed_name_len+QFIXEDSZ;
62 while ((int)Result.size() < answer_count && pt < answer+answer_len)
63 {
64 SrvRec rec;
65 u_int16_t type, klass, priority, weight, port, dlen;
66 char buf[MAXDNAME];
67
68 compressed_name_len = dn_skipname(pt, answer+answer_len);
69 if (compressed_name_len < 0)
70 return _error->Warning("dn_skipname failed (2): %i",
71 compressed_name_len);
72 pt += compressed_name_len;
73 if (((answer+answer_len) - pt) < 16)
74 return _error->Warning("packet too short");
75
76 // extract the data out of the result buffer
77 #define extract_u16(target, p) target = *p++ << 8; target |= *p++;
78
79 extract_u16(type, pt);
80 if(type != T_SRV)
81 return _error->Warning("Unexpected type excepted %x != %x",
82 T_SRV, type);
83 extract_u16(klass, pt);
84 if(klass != C_IN)
85 return _error->Warning("Unexpected class excepted %x != %x",
86 C_IN, klass);
87 pt += 4; // ttl
88 extract_u16(dlen, pt);
89 extract_u16(priority, pt);
90 extract_u16(weight, pt);
91 extract_u16(port, pt);
92
93 #undef extract_u16
94
95 compressed_name_len = dn_expand(answer, answer+answer_len, pt, buf, sizeof(buf));
96 if(compressed_name_len < 0)
97 return _error->Warning("dn_expand failed %i", compressed_name_len);
98 pt += compressed_name_len;
99
100 // add it to our class
101 rec.priority = priority;
102 rec.weight = weight;
103 rec.port = port;
104 rec.target = buf;
105 Result.push_back(rec);
106 }
107
108 // implement load balancing as specified in RFC-2782
109
110 // sort them by priority
111 std::stable_sort(Result.begin(), Result.end());
112
113 // assign random number ranges
114 int prev_weight = 0;
115 int prev_priority = 0;
116 for(std::vector<SrvRec>::iterator I = Result.begin();
117 I != Result.end(); ++I)
118 {
119 if(prev_priority != I->priority)
120 prev_weight = 0;
121 I->random_number_range_start = prev_weight;
122 I->random_number_range_end = prev_weight + I->weight;
123 prev_weight = I->random_number_range_end;
124 prev_priority = I->priority;
125 }
126
127 // go over the code in reverse order and note the max random range
128 int max = 0;
129 prev_priority = 0;
130 for(std::vector<SrvRec>::iterator I = Result.end();
131 I != Result.begin(); --I)
132 {
133 if(prev_priority != I->priority)
134 max = I->random_number_range_end;
135 I->random_number_range_max = max;
136 }
137
138 // now shuffle
139
140 return true;
141 }