Coverage Report

Created: 2023-03-26 06:07

/src/unbound/util/edns.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * util/edns.c - handle base EDNS options.
3
 *
4
 * Copyright (c) 2018, NLnet Labs. All rights reserved.
5
 *
6
 * This software is open source.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 *
12
 * Redistributions of source code must retain the above copyright notice,
13
 * this list of conditions and the following disclaimer.
14
 *
15
 * Redistributions in binary form must reproduce the above copyright notice,
16
 * this list of conditions and the following disclaimer in the documentation
17
 * and/or other materials provided with the distribution.
18
 *
19
 * Neither the name of the NLNET LABS nor the names of its contributors may
20
 * be used to endorse or promote products derived from this software without
21
 * specific prior written permission.
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
 */
35
36
/**
37
 * \file
38
 *
39
 * This file contains functions for base EDNS options.
40
 */
41
42
#include "config.h"
43
#include "util/edns.h"
44
#include "util/config_file.h"
45
#include "util/netevent.h"
46
#include "util/net_help.h"
47
#include "util/regional.h"
48
#include "util/data/msgparse.h"
49
#include "util/data/msgreply.h"
50
51
struct edns_strings* edns_strings_create(void)
52
0
{
53
0
  struct edns_strings* edns_strings = calloc(1,
54
0
    sizeof(struct edns_strings));
55
0
  if(!edns_strings)
56
0
    return NULL;
57
0
  if(!(edns_strings->region = regional_create())) {
58
0
    edns_strings_delete(edns_strings);
59
0
    return NULL;
60
0
  }
61
0
  return edns_strings;
62
0
}
63
64
void edns_strings_delete(struct edns_strings* edns_strings)
65
0
{
66
0
  if(!edns_strings)
67
0
    return;
68
0
  regional_destroy(edns_strings->region);
69
0
  free(edns_strings);
70
0
}
71
72
static int
73
edns_strings_client_insert(struct edns_strings* edns_strings,
74
  struct sockaddr_storage* addr, socklen_t addrlen, int net,
75
  const char* string)
76
0
{
77
0
  struct edns_string_addr* esa = regional_alloc_zero(edns_strings->region,
78
0
    sizeof(struct edns_string_addr));
79
0
  if(!esa)
80
0
    return 0;
81
0
  esa->string_len = strlen(string);
82
0
  esa->string = regional_alloc_init(edns_strings->region, string,
83
0
    esa->string_len);
84
0
  if(!esa->string)
85
0
    return 0;
86
0
  if(!addr_tree_insert(&edns_strings->client_strings, &esa->node, addr,
87
0
    addrlen, net)) {
88
0
    verbose(VERB_QUERY, "duplicate EDNS client string ignored.");
89
0
  }
90
0
  return 1;
91
0
}
92
93
int edns_strings_apply_cfg(struct edns_strings* edns_strings,
94
  struct config_file* config)
95
0
{
96
0
  struct config_str2list* c;
97
0
  regional_free_all(edns_strings->region);
98
0
  addr_tree_init(&edns_strings->client_strings);
99
100
0
  for(c=config->edns_client_strings; c; c=c->next) {
101
0
    struct sockaddr_storage addr;
102
0
    socklen_t addrlen;
103
0
    int net;
104
0
    log_assert(c->str && c->str2);
105
106
0
    if(!netblockstrtoaddr(c->str, UNBOUND_DNS_PORT, &addr, &addrlen,
107
0
      &net)) {
108
0
      log_err("cannot parse EDNS client string IP netblock: "
109
0
        "%s", c->str);
110
0
      return 0;
111
0
    }
112
0
    if(!edns_strings_client_insert(edns_strings, &addr, addrlen,
113
0
      net, c->str2)) {
114
0
      log_err("out of memory while adding EDNS strings");
115
0
      return 0;
116
0
    }
117
0
  }
118
0
  edns_strings->client_string_opcode = config->edns_client_string_opcode;
119
120
0
  addr_tree_init_parents(&edns_strings->client_strings);
121
0
  return 1;
122
0
}
123
124
struct edns_string_addr*
125
edns_string_addr_lookup(rbtree_type* tree, struct sockaddr_storage* addr,
126
  socklen_t addrlen)
127
0
{
128
0
  return (struct edns_string_addr*)addr_tree_lookup(tree, addr, addrlen);
129
0
}
130