]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/query-local-address.cc
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "query-local-address.hh"
24 #include "dns_random.hh"
27 static const ComboAddress
local4("0.0.0.0");
28 static const ComboAddress
local6("::");
30 static vector
<ComboAddress
> g_localQueryAddresses4
;
31 static vector
<ComboAddress
> g_localQueryAddresses6
;
33 ComboAddress
getQueryLocalAddress(const sa_family_t family
, const in_port_t port
) {
35 if (family
==AF_INET
) {
36 if (g_localQueryAddresses4
.empty()) {
38 } else if (g_localQueryAddresses4
.size() == 1) {
39 ret
= g_localQueryAddresses4
.at(0);
41 ret
= g_localQueryAddresses4
[dns_random(g_localQueryAddresses4
.size())];
43 ret
.sin4
.sin_port
= htons(port
);
46 if (g_localQueryAddresses6
.empty()) {
48 } else if (g_localQueryAddresses6
.size() == 1) {
49 ret
= g_localQueryAddresses6
.at(0);
51 ret
= g_localQueryAddresses6
[dns_random(g_localQueryAddresses6
.size())];
53 ret
.sin6
.sin6_port
= htons(port
);
58 ComboAddress
getNonAnyQueryLocalAddress(const sa_family_t family
) {
59 if (family
== AF_INET
) {
60 for (const auto& addr
: pdns::g_localQueryAddresses4
) {
61 if (!IsAnyAddress(addr
)) {
66 if (family
== AF_INET6
) {
67 for (const auto& addr
: pdns::g_localQueryAddresses6
) {
68 if (!IsAnyAddress(addr
)) {
73 ComboAddress
ret("0.0.0.0");
74 ret
.reset(); // Ensure all is zero, even the addr family
78 void parseQueryLocalAddress(const std::string
&qla
) {
80 stringtok(addrs
, qla
, ", ;");
81 for(const string
& addr
: addrs
) {
82 ComboAddress
tmp(addr
);
84 g_localQueryAddresses4
.push_back(tmp
);
87 g_localQueryAddresses6
.push_back(tmp
);
91 bool isQueryLocalAddressFamilyEnabled(const sa_family_t family
) {
92 if (family
== AF_INET
) {
93 return !g_localQueryAddresses4
.empty();
95 if (family
== AF_INET6
) {
96 return !g_localQueryAddresses6
.empty();