1 // -*- mode: cpp; mode: fold -*-
3 /* ######################################################################
7 ##################################################################### */
13 #include <netinet/in.h>
14 #include <arpa/nameser.h>
20 #include <apt-pkg/configuration.h>
21 #include <apt-pkg/error.h>
22 #include <apt-pkg/strutl.h>
28 bool GetSrvRecords(std::string host
, int port
, std::vector
<SrvRec
> &Result
)
31 struct servent
*s_ent
= getservbyport(htons(port
), "tcp");
35 strprintf(target
, "_%s._tcp.%s", s_ent
->s_name
, host
.c_str());
36 return GetSrvRecords(target
, Result
);
39 bool GetSrvRecords(std::string name
, std::vector
<SrvRec
> &Result
)
41 unsigned char answer
[PACKETSZ
];
42 int answer_len
, compressed_name_len
;
46 return _error
->Errno("res_init", "Failed to init resolver");
48 answer_len
= res_query(name
.c_str(), C_IN
, T_SRV
, answer
, sizeof(answer
));
51 if (answer_len
< (int)sizeof(HEADER
))
52 return _error
->Warning("Not enough data from res_query (%i)", answer_len
);
55 HEADER
*header
= (HEADER
*)answer
;
56 if (header
->rcode
!= NOERROR
)
57 return _error
->Warning("res_query returned rcode %i", header
->rcode
);
58 answer_count
= ntohs(header
->ancount
);
59 if (answer_count
<= 0)
60 return _error
->Warning("res_query returned no answers (%i) ", answer_count
);
63 compressed_name_len
= dn_skipname(answer
+sizeof(HEADER
), answer
+answer_len
);
64 if(compressed_name_len
< 0)
65 return _error
->Warning("dn_skipname failed %i", compressed_name_len
);
67 // pt points to the first answer record, go over all of them now
68 unsigned char *pt
= answer
+sizeof(HEADER
)+compressed_name_len
+QFIXEDSZ
;
69 while ((int)Result
.size() < answer_count
&& pt
< answer
+answer_len
)
71 u_int16_t type
, klass
, priority
, weight
, port
, dlen
;
74 compressed_name_len
= dn_skipname(pt
, answer
+answer_len
);
75 if (compressed_name_len
< 0)
76 return _error
->Warning("dn_skipname failed (2): %i",
78 pt
+= compressed_name_len
;
79 if (((answer
+answer_len
) - pt
) < 16)
80 return _error
->Warning("packet too short");
82 // extract the data out of the result buffer
83 #define extract_u16(target, p) target = *p++ << 8; target |= *p++;
85 extract_u16(type
, pt
);
87 return _error
->Warning("Unexpected type excepted %x != %x",
89 extract_u16(klass
, pt
);
91 return _error
->Warning("Unexpected class excepted %x != %x",
94 extract_u16(dlen
, pt
);
95 extract_u16(priority
, pt
);
96 extract_u16(weight
, pt
);
97 extract_u16(port
, pt
);
101 compressed_name_len
= dn_expand(answer
, answer
+answer_len
, pt
, buf
, sizeof(buf
));
102 if(compressed_name_len
< 0)
103 return _error
->Warning("dn_expand failed %i", compressed_name_len
);
104 pt
+= compressed_name_len
;
106 // add it to our class
107 Result
.emplace_back(buf
, priority
, weight
, port
);
110 // implement load balancing as specified in RFC-2782
112 // sort them by priority
113 std::stable_sort(Result
.begin(), Result
.end());
115 for(std::vector
<SrvRec
>::iterator I
= Result
.begin();
116 I
!= Result
.end(); ++I
)
118 if (_config
->FindB("Debug::Acquire::SrvRecs", false) == true)
120 std::cerr
<< "SrvRecs: got " << I
->target
121 << " prio: " << I
->priority
122 << " weight: " << I
->weight
130 SrvRec
PopFromSrvRecs(std::vector
<SrvRec
> &Recs
)
132 // FIXME: instead of the simplistic shuffle below use the algorithm
133 // described in rfc2782 (with weights)
134 // and figure out how the weights need to be adjusted if
135 // a host refuses connections
137 #if 0 // all code below is only needed for the weight adjusted selection
138 // assign random number ranges
140 int prev_priority
= 0;
141 for(std::vector
<SrvRec
>::iterator I
= Result
.begin();
142 I
!= Result
.end(); ++I
)
144 if(prev_priority
!= I
->priority
)
146 I
->random_number_range_start
= prev_weight
;
147 I
->random_number_range_end
= prev_weight
+ I
->weight
;
148 prev_weight
= I
->random_number_range_end
;
149 prev_priority
= I
->priority
;
151 if (_config
->FindB("Debug::Acquire::SrvRecs", false) == true)
152 std::cerr
<< "SrvRecs: got " << I
->target
153 << " prio: " << I
->priority
154 << " weight: " << I
->weight
158 // go over the code in reverse order and note the max random range
161 for(std::vector
<SrvRec
>::iterator I
= Result
.end();
162 I
!= Result
.begin(); --I
)
164 if(prev_priority
!= I
->priority
)
165 max
= I
->random_number_range_end
;
166 I
->random_number_range_max
= max
;
170 // shuffle in a very simplistic way for now (equal weights)
171 std::vector
<SrvRec
>::iterator I
= Recs
.begin();
172 std::vector
<SrvRec
>::iterator
const J
= std::find_if(Recs
.begin(), Recs
.end(),
173 [&I
](SrvRec
const &J
) { return I
->priority
!= J
.priority
; });
175 // clock seems random enough.
176 I
+= clock() % std::distance(I
, J
);
177 SrvRec
const selected
= std::move(*I
);
180 if (_config
->FindB("Debug::Acquire::SrvRecs", false) == true)
181 std::cerr
<< "PopFromSrvRecs: selecting " << selected
.target
<< std::endl
;