1 // -*- mode: cpp; mode: fold -*-
3 /* ######################################################################
7 ##################################################################### */
13 #include <netinet/in.h>
14 #include <arpa/nameser.h>
19 #include <apt-pkg/strutl.h>
20 #include <apt-pkg/error.h>
23 bool GetSrvRecords(std::string host
, int port
, std::vector
<SrvRec
> &Result
)
26 struct servent
*s_ent
= getservbyport(htons(port
), "tcp");
30 strprintf(target
, "_%s._tcp.%s", s_ent
->s_name
, host
.c_str());
31 return GetSrvRecords(target
, Result
);
34 bool GetSrvRecords(std::string name
, std::vector
<SrvRec
> &Result
)
36 unsigned char answer
[PACKETSZ
];
37 int answer_len
, compressed_name_len
;
41 return _error
->Errno("res_init", "Failed to init resolver");
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
);
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
);
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
);
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
)
65 u_int16_t type
, klass
, priority
, weight
, port
, dlen
;
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",
72 pt
+= compressed_name_len
;
73 if (((answer
+answer_len
) - pt
) < 16)
74 return _error
->Warning("packet too short");
76 // extract the data out of the result buffer
77 #define extract_u16(target, p) target = *p++ << 8; target |= *p++;
79 extract_u16(type
, pt
);
81 return _error
->Warning("Unexpected type excepted %x != %x",
83 extract_u16(klass
, pt
);
85 return _error
->Warning("Unexpected class excepted %x != %x",
88 extract_u16(dlen
, pt
);
89 extract_u16(priority
, pt
);
90 extract_u16(weight
, pt
);
91 extract_u16(port
, pt
);
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
;
100 // add it to our class
101 rec
.priority
= priority
;
105 Result
.push_back(rec
);
108 // implement load balancing as specified in RFC-2782
110 // sort them by priority
111 std::stable_sort(Result
.begin(), Result
.end());
113 // assign random number ranges
115 int prev_priority
= 0;
116 for(std::vector
<SrvRec
>::iterator I
= Result
.begin();
117 I
!= Result
.end(); ++I
)
119 if(prev_priority
!= I
->priority
)
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
;
127 // go over the code in reverse order and note the max random range
130 for(std::vector
<SrvRec
>::iterator I
= Result
.end();
131 I
!= Result
.begin(); --I
)
133 if(prev_priority
!= I
->priority
)
134 max
= I
->random_number_range_end
;
135 I
->random_number_range_max
= max
;