]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/ednssubnet.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.
25 #include "ednssubnet.hh"
29 struct EDNSSubnetOptsWire
34 } GCCPACKATTRIBUTE
; // BRRRRR
38 bool getEDNSSubnetOptsFromString(const string
& options
, EDNSSubnetOpts
* eso
)
40 //cerr<<"options.size:"<<options.size()<<endl;
41 return getEDNSSubnetOptsFromString(options
.c_str(), options
.length(), eso
);
43 bool getEDNSSubnetOptsFromString(const char* options
, unsigned int len
, EDNSSubnetOpts
* eso
)
45 EDNSSubnetOptsWire esow
;
46 static_assert (sizeof(esow
) == 4, "sizeof(EDNSSubnetOptsWire) must be 4 bytes");
47 if(len
< sizeof(esow
))
49 memcpy(&esow
, options
, sizeof(esow
));
50 esow
.family
= ntohs(esow
.family
);
51 //cerr<<"Family when parsing from string: "<<esow.family<<endl;
53 unsigned int octetsin
= esow
.sourceMask
> 0 ? (((esow
.sourceMask
- 1)>> 3)+1) : 0;
54 //cerr<<"octetsin:"<<octetsin<<endl;
55 if(esow
.family
== 1) {
56 if(len
!= sizeof(esow
)+octetsin
)
58 if(octetsin
> sizeof(address
.sin4
.sin_addr
.s_addr
))
61 address
.sin4
.sin_family
= AF_INET
;
63 memcpy(&address
.sin4
.sin_addr
.s_addr
, options
+sizeof(esow
), octetsin
);
64 } else if(esow
.family
== 2) {
65 if(len
!= sizeof(esow
)+octetsin
)
67 if(octetsin
> sizeof(address
.sin6
.sin6_addr
.s6_addr
))
71 address
.sin4
.sin_family
= AF_INET6
;
73 memcpy(&address
.sin6
.sin6_addr
.s6_addr
, options
+sizeof(esow
), octetsin
);
77 //cerr<<"Source address: "<<address.toString()<<", mask: "<<(int)esow.sourceMask<<endl;
78 eso
->source
= Netmask(address
, esow
.sourceMask
);
79 /* 'address' has more bits set (potentially) than scopeMask. This leads to odd looking netmasks that promise
80 more precision than they have. For this reason we truncate the address to scopeMask bits */
82 address
.truncate(esow
.scopeMask
); // truncate will not throw for odd scopeMasks
83 eso
->scope
= Netmask(address
, esow
.scopeMask
);
88 string
makeEDNSSubnetOptsString(const EDNSSubnetOpts
& eso
)
91 EDNSSubnetOptsWire esow
;
92 uint16_t family
= htons(eso
.source
.getNetwork().sin4
.sin_family
== AF_INET
? 1 : 2);
94 esow
.sourceMask
= eso
.source
.getBits();
95 esow
.scopeMask
= eso
.scope
.getBits();
96 ret
.assign((const char*)&esow
, sizeof(esow
));
97 int octetsout
= ((esow
.sourceMask
- 1)>> 3)+1;
99 ComboAddress src
=eso
.source
.getNetwork();
100 src
.truncate(esow
.sourceMask
);
102 if(family
== htons(1))
103 ret
.append((const char*) &src
.sin4
.sin_addr
.s_addr
, octetsout
);
105 ret
.append((const char*) &src
.sin6
.sin6_addr
.s6_addr
, octetsout
);