| 1 |
/* dnsmasq is Copyright (c) 2000-2026 Simon Kelley |
| 2 |
|
| 3 |
This program is free software; you can redistribute it and/or modify |
| 4 |
it under the terms of the GNU General Public License as published by |
| 5 |
the Free Software Foundation; version 2 dated June, 1991, or |
| 6 |
(at your option) version 3 dated 29 June, 2007. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, |
| 9 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License |
| 14 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "dnsmasq.h" |
| 18 |
|
| 19 |
static struct blockdata *keyblock_free; |
| 20 |
static unsigned int blockdata_count, blockdata_hwm, blockdata_alloced; |
| 21 |
|
| 22 |
static void add_blocks(int n) |
| 23 |
{ |
| 24 |
struct blockdata *new = whine_malloc(n * sizeof(struct blockdata)); |
| 25 |
|
| 26 |
if (new) |
| 27 |
{ |
| 28 |
int i; |
| 29 |
|
| 30 |
new[n-1].next = keyblock_free; |
| 31 |
keyblock_free = new; |
| 32 |
|
| 33 |
for (i = 0; i < n - 1; i++) |
| 34 |
new[i].next = &new[i+1]; |
| 35 |
|
| 36 |
blockdata_alloced += n; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
/* Preallocate some blocks, proportional to cachesize, to reduce heap fragmentation. */ |
| 41 |
void blockdata_init(void) |
| 42 |
{ |
| 43 |
keyblock_free = NULL; |
| 44 |
blockdata_alloced = 0; |
| 45 |
blockdata_count = 0; |
| 46 |
blockdata_hwm = 0; |
| 47 |
|
| 48 |
/* Note that daemon->cachesize is enforced to have non-zero size if OPT_DNSSEC_VALID is set */ |
| 49 |
if (option_bool(OPT_DNSSEC_VALID)) |
| 50 |
add_blocks(daemon->cachesize); |
| 51 |
} |
| 52 |
|
| 53 |
void blockdata_report(void) |
| 54 |
{ |
| 55 |
my_syslog(LOG_INFO, _("pool memory in use %zu, max %zu, allocated %zu"), |
| 56 |
blockdata_count * sizeof(struct blockdata), |
| 57 |
blockdata_hwm * sizeof(struct blockdata), |
| 58 |
blockdata_alloced * sizeof(struct blockdata)); |
| 59 |
} |
| 60 |
|
| 61 |
static struct blockdata *new_block(void) |
| 62 |
{ |
| 63 |
struct blockdata *block; |
| 64 |
|
| 65 |
if (!keyblock_free) |
| 66 |
add_blocks(50); |
| 67 |
|
| 68 |
if (keyblock_free) |
| 69 |
{ |
| 70 |
block = keyblock_free; |
| 71 |
keyblock_free = block->next; |
| 72 |
blockdata_count++; |
| 73 |
if (blockdata_hwm < blockdata_count) |
| 74 |
blockdata_hwm = blockdata_count; |
| 75 |
block->next = NULL; |
| 76 |
return block; |
| 77 |
} |
| 78 |
|
| 79 |
return NULL; |
| 80 |
} |
| 81 |
|
| 82 |
static struct blockdata *blockdata_alloc_real(int fd, char *data, size_t len) |
| 83 |
{ |
| 84 |
struct blockdata *block, *ret = NULL; |
| 85 |
struct blockdata **prev = &ret; |
| 86 |
size_t blen; |
| 87 |
|
| 88 |
do |
| 89 |
{ |
| 90 |
if (!(block = new_block())) |
| 91 |
{ |
| 92 |
/* failed to alloc, free partial chain */ |
| 93 |
blockdata_free(ret); |
| 94 |
return NULL; |
| 95 |
} |
| 96 |
|
| 97 |
if ((blen = len > KEYBLOCK_LEN ? KEYBLOCK_LEN : len) > 0) |
| 98 |
{ |
| 99 |
if (data) |
| 100 |
{ |
| 101 |
memcpy(block->key, data, blen); |
| 102 |
data += blen; |
| 103 |
} |
| 104 |
else if (!read_write(fd, block->key, blen, RW_READ)) |
| 105 |
{ |
| 106 |
/* failed read free partial chain */ |
| 107 |
blockdata_free(ret); |
| 108 |
return NULL; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
len -= blen; |
| 113 |
*prev = block; |
| 114 |
prev = &block->next; |
| 115 |
} while (len != 0); |
| 116 |
|
| 117 |
return ret; |
| 118 |
} |
| 119 |
|
| 120 |
struct blockdata *blockdata_alloc(char *data, size_t len) |
| 121 |
{ |
| 122 |
return blockdata_alloc_real(0, data, len); |
| 123 |
} |
| 124 |
|
| 125 |
/* Add data to the end of the block. |
| 126 |
newlen is length of new data, NOT total new length. |
| 127 |
Use blockdata_alloc(NULL, 0) to make empty block to add to. */ |
| 128 |
int blockdata_expand(struct blockdata *block, size_t oldlen, char *data, size_t newlen) |
| 129 |
{ |
| 130 |
struct blockdata *b; |
| 131 |
|
| 132 |
/* find size of current final block */ |
| 133 |
for (b = block; oldlen > KEYBLOCK_LEN && b; b = b->next, oldlen -= KEYBLOCK_LEN); |
| 134 |
|
| 135 |
/* chain to short for length, something is broken */ |
| 136 |
if (oldlen > KEYBLOCK_LEN) |
| 137 |
{ |
| 138 |
blockdata_free(block); |
| 139 |
return 0; |
| 140 |
} |
| 141 |
|
| 142 |
while (1) |
| 143 |
{ |
| 144 |
struct blockdata *new; |
| 145 |
size_t blocksize = KEYBLOCK_LEN - oldlen; |
| 146 |
size_t size = (newlen <= blocksize) ? newlen : blocksize; |
| 147 |
|
| 148 |
if (size != 0) |
| 149 |
{ |
| 150 |
memcpy(&b->key[oldlen], data, size); |
| 151 |
data += size; |
| 152 |
newlen -= size; |
| 153 |
} |
| 154 |
|
| 155 |
/* full blocks from now on. */ |
| 156 |
oldlen = 0; |
| 157 |
|
| 158 |
if (newlen == 0) |
| 159 |
break; |
| 160 |
|
| 161 |
if ((new = new_block())) |
| 162 |
{ |
| 163 |
b->next = new; |
| 164 |
b = new; |
| 165 |
} |
| 166 |
else |
| 167 |
{ |
| 168 |
/* failed to alloc, free partial chain */ |
| 169 |
blockdata_free(block); |
| 170 |
return 0; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
return 1; |
| 175 |
} |
| 176 |
|
| 177 |
void blockdata_free(struct blockdata *blocks) |
| 178 |
{ |
| 179 |
struct blockdata *tmp; |
| 180 |
|
| 181 |
if (blocks) |
| 182 |
{ |
| 183 |
for (tmp = blocks; tmp->next; tmp = tmp->next) |
| 184 |
blockdata_count--; |
| 185 |
tmp->next = keyblock_free; |
| 186 |
keyblock_free = blocks; |
| 187 |
blockdata_count--; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
/* if data == NULL, return pointer to static block of sufficient size */ |
| 192 |
void *blockdata_retrieve(struct blockdata *block, size_t len, void *data) |
| 193 |
{ |
| 194 |
size_t blen; |
| 195 |
struct blockdata *b; |
| 196 |
uint8_t *d; |
| 197 |
|
| 198 |
if (!data) |
| 199 |
{ |
| 200 |
static unsigned int buff_len = 0; |
| 201 |
static unsigned char *buff = NULL; |
| 202 |
uint8_t *new; |
| 203 |
|
| 204 |
if (len > buff_len) |
| 205 |
{ |
| 206 |
blen = len + 1024; |
| 207 |
if (!(new = whine_realloc(buff, blen))) |
| 208 |
return NULL; |
| 209 |
|
| 210 |
buff = new; |
| 211 |
buff_len = blen; |
| 212 |
} |
| 213 |
data = buff; |
| 214 |
} |
| 215 |
|
| 216 |
for (d = data, b = block; len > 0 && b; b = b->next) |
| 217 |
{ |
| 218 |
blen = len > KEYBLOCK_LEN ? KEYBLOCK_LEN : len; |
| 219 |
memcpy(d, b->key, blen); |
| 220 |
d += blen; |
| 221 |
len -= blen; |
| 222 |
} |
| 223 |
|
| 224 |
return data; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
void blockdata_write(struct blockdata *block, size_t len, int fd) |
| 229 |
{ |
| 230 |
for (; len > 0 && block; block = block->next) |
| 231 |
{ |
| 232 |
size_t blen = len > KEYBLOCK_LEN ? KEYBLOCK_LEN : len; |
| 233 |
read_write(fd, block->key, blen, RW_WRITE); |
| 234 |
len -= blen; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
struct blockdata *blockdata_read(int fd, size_t len) |
| 239 |
{ |
| 240 |
return blockdata_alloc_real(fd, NULL, len); |
| 241 |
} |