File: ednssubnet.cc

package info (click to toggle)
pdns-recursor 4.0.4-1+deb9u3~bpo8+1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 5,484 kB
  • sloc: cpp: 36,380; sh: 11,771; makefile: 305; xml: 37
file content (97 lines) | stat: -rw-r--r-- 3,465 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * This file is part of PowerDNS or dnsdist.
 * Copyright -- PowerDNS.COM B.V. and its contributors
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * In addition, for the avoidance of any doubt, permission is granted to
 * link this program with OpenSSL and to (re)distribute the binaries
 * produced as the result of such linking.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "ednssubnet.hh"
#include "dns.hh"

namespace {
        struct EDNSSubnetOptsWire
        {
                uint16_t family;
                uint8_t sourceMask;
                uint8_t scopeMask;
        } GCCPACKATTRIBUTE;  // BRRRRR

}

bool getEDNSSubnetOptsFromString(const string& options, EDNSSubnetOpts* eso)
{
  return getEDNSSubnetOptsFromString(options.c_str(), options.length(), eso);
}
bool getEDNSSubnetOptsFromString(const char* options, unsigned int len, EDNSSubnetOpts* eso)
{
  //cerr<<"options.size:"<<options.size()<<endl;
  EDNSSubnetOptsWire esow;
  static_assert (sizeof(esow) == 4, "sizeof(EDNSSubnetOptsWire) must be 4 bytes");
  if(len <= sizeof(esow))
    return false;
  memcpy(&esow, options, sizeof(esow));
  esow.family = ntohs(esow.family);
  //cerr<<"Family when parsing from string: "<<esow.family<<endl;
  ComboAddress address;
  unsigned int octetsin = ((esow.sourceMask - 1)>> 3)+1;
  //cerr<<"octetsin:"<<octetsin<<endl;
  if(esow.family == 1) {
    if(len != sizeof(esow)+octetsin)
      return false;
    if(octetsin > sizeof(address.sin4.sin_addr.s_addr))
      return false;
    memset(&address, 0, sizeof(address));
    address.sin4.sin_family = AF_INET;
    memcpy(&address.sin4.sin_addr.s_addr, options+sizeof(esow), octetsin);
  } else if(esow.family == 2) {
    if(len != sizeof(esow)+octetsin)
      return false;
    if(octetsin > sizeof(address.sin6.sin6_addr.s6_addr))
      return false;
    memset(&address, 0, sizeof(address));
    address.sin4.sin_family = AF_INET6;
    memcpy(&address.sin6.sin6_addr.s6_addr, options+sizeof(esow), octetsin);
  }
  else
    return false;
 // cerr<<"Source address: "<<address.toString()<<", mask: "<<(int)esow.sourceMask<<endl;
  eso->source = Netmask(address, esow.sourceMask);
  eso->scope = Netmask(address, esow.scopeMask);
  return true;
}

string makeEDNSSubnetOptsString(const EDNSSubnetOpts& eso)
{
  string ret;
  EDNSSubnetOptsWire esow;
  uint16_t family = htons(eso.source.getNetwork().sin4.sin_family == AF_INET ? 1 : 2);
  esow.family = family;
  esow.sourceMask = eso.source.getBits();
  esow.scopeMask = eso.scope.getBits();
  ret.assign((const char*)&esow, sizeof(esow));
  int octetsout = ((esow.sourceMask - 1)>> 3)+1;

  if(family == htons(1)) 
    ret.append((const char*) &eso.source.getNetwork().sin4.sin_addr.s_addr, octetsout);
  else
    ret.append((const char*) &eso.source.getNetwork().sin6.sin6_addr.s6_addr, octetsout);
  return ret;
}