Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 322 lines (252 sloc) 6.929 kb
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/* Cherokee
*
* Authors:
* Alvaro Lopez Ortega <alvaro@alobbs.com>
*
* Copyright (C) 2001-2011 Alvaro Lopez Ortega
*
* 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.
*
* 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.
*/

#include "common-internal.h"

#ifndef _WIN32
# include <sys/socket.h>
# include <netinet/in.h>
# include <arpa/inet.h>
# include <netdb.h>
#endif

#include "resolv_cache.h"
#include "socket_lowlevel.h"
#include "util.h"
#include "avl.h"
#include "socket.h"
#include "bogotime.h"


#define ENTRIES "resolve"


typedef struct {
struct addrinfo *addr;
cherokee_buffer_t ip_str;
} cherokee_resolv_cache_entry_t;

struct cherokee_resolv_cache {
cherokee_avl_t table;
CHEROKEE_RWLOCK_T (lock);
};

static cherokee_resolv_cache_t *__global_resolv = NULL;


/* Entries
*/
static ret_t
entry_new (cherokee_resolv_cache_entry_t **entry)
{
CHEROKEE_NEW_STRUCT(n, resolv_cache_entry);

n->addr = NULL;
cherokee_buffer_init (&n->ip_str);

*entry = n;
return ret_ok;
}


static void
entry_free (void *entry)
{
cherokee_resolv_cache_entry_t *e = entry;

if (e->addr) {
freeaddrinfo (e->addr);
}

cherokee_buffer_mrproper (&e->ip_str);
free(entry);
}


static ret_t
entry_fill_up (cherokee_resolv_cache_entry_t *entry,
cherokee_buffer_t *domain)
{
ret_t ret;
char tmp[46]; // Max IPv6 length is 45
time_t eagain_at = 0;

while (true) {
ret = cherokee_gethostbyname (domain->buf, &entry->addr);
if (ret == ret_ok) {
break;

} else if (ret == ret_eagain) {
if (eagain_at == 0) {
eagain_at = cherokee_bogonow_now;

} else if (cherokee_bogonow_now > eagain_at + 3) {
LOG_WARNING (CHEROKEE_ERROR_RESOLVE_TIMEOUT, domain->buf);
return ret_error;
}

CHEROKEE_THREAD_YIELD;
continue;

} else {
return ret_error;
}
}

if (unlikely (entry->addr == NULL)) {
return ret_error;
}

/* Render the text representation
*/
ret = cherokee_ntop (entry->addr->ai_family, entry->addr->ai_addr, tmp, sizeof(tmp));
if (ret != ret_ok) {
return ret_error;
}

cherokee_buffer_add (&entry->ip_str, tmp, strlen(tmp));
return ret_ok;
}



/* Table
*/
ret_t
cherokee_resolv_cache_init (cherokee_resolv_cache_t *resolv)
{
ret_t ret;

ret = cherokee_avl_init (&resolv->table);
if (unlikely (ret != ret_ok)) return ret;

ret = cherokee_avl_set_case (&resolv->table, true);
if (unlikely (ret != ret_ok)) return ret;

CHEROKEE_RWLOCK_INIT (&resolv->lock, NULL);
return ret_ok;
}


ret_t
cherokee_resolv_cache_mrproper (cherokee_resolv_cache_t *resolv)
{
cherokee_avl_mrproper (&resolv->table, entry_free);
CHEROKEE_RWLOCK_DESTROY (&resolv->lock);

return ret_ok;
}


ret_t
cherokee_resolv_cache_get_default (cherokee_resolv_cache_t **resolv)
{
ret_t ret;

if (unlikely (__global_resolv == NULL)) {
CHEROKEE_NEW_STRUCT (n, resolv_cache);

ret = cherokee_resolv_cache_init (n);
if (ret != ret_ok) return ret;

__global_resolv = n;
}

*resolv = __global_resolv;
return ret_ok;
}


ret_t
cherokee_resolv_cache_clean (cherokee_resolv_cache_t *resolv)
{
CHEROKEE_RWLOCK_WRITER (&resolv->lock);
cherokee_avl_mrproper (&resolv->table, entry_free);
CHEROKEE_RWLOCK_UNLOCK (&resolv->lock);

return ret_ok;
}


static ret_t
table_add_new_entry (cherokee_resolv_cache_t *resolv,
cherokee_buffer_t *domain,
cherokee_resolv_cache_entry_t **entry)
{
ret_t ret;
cherokee_resolv_cache_entry_t *n = NULL;

/* Instance the entry
*/
ret = entry_new (&n);
if (unlikely (ret != ret_ok)) {
return ret;
}

/* Fill it up
*/
ret = entry_fill_up (n, domain);
if (unlikely (ret != ret_ok)) {
entry_free (n);
return ret;
}

/* Add it to the table
*/
CHEROKEE_RWLOCK_WRITER (&resolv->lock);
ret = cherokee_avl_add (&resolv->table, domain, (void **)n);
CHEROKEE_RWLOCK_UNLOCK (&resolv->lock);

*entry = n;
return ret_ok;
}


ret_t
cherokee_resolv_cache_get_ipstr (cherokee_resolv_cache_t *resolv,
cherokee_buffer_t *domain,
const char **ip)
{
ret_t ret;
cherokee_resolv_cache_entry_t *entry = NULL;

/* Look for the name in the cache
*/
CHEROKEE_RWLOCK_READER (&resolv->lock);
ret = cherokee_avl_get (&resolv->table, domain, (void **)&entry);
CHEROKEE_RWLOCK_UNLOCK (&resolv->lock);

if (ret != ret_ok) {
TRACE (ENTRIES, "Resolve '%s': missed.\n", domain->buf);

/* Bad luck: it wasn't cached
*/
ret = table_add_new_entry (resolv, domain, &entry);
if (ret != ret_ok) {
return ret;
}
TRACE (ENTRIES, "Resolve '%s': added succesfuly as '%s'.\n", domain->buf, entry->ip_str.buf);
} else {
TRACE (ENTRIES, "Resolve '%s': hit.\n", domain->buf);
}

/* Return the ip string
*/
if (ip != NULL) {
*ip = entry->ip_str.buf;
}

return ret_ok;
}


ret_t
cherokee_resolv_cache_get_host (cherokee_resolv_cache_t *resolv,
cherokee_buffer_t *domain,
void *sock_)
{
ret_t ret;
const struct addrinfo *addr = NULL;
cherokee_socket_t *sock = sock_;

/* Get addrinfo
*/
ret = cherokee_resolv_cache_get_addrinfo (resolv, domain, &addr);
if (ret != ret_ok) {
return ret;
}

/* Copy it to the socket object
*/
ret = cherokee_socket_update_from_addrinfo (sock, addr);
if (ret != ret_ok) {
return ret;
}

return ret_ok;
}


ret_t
cherokee_resolv_cache_get_addrinfo (cherokee_resolv_cache_t *resolv,
cherokee_buffer_t *domain,
const struct addrinfo **addr_info)
{
ret_t ret;
cherokee_resolv_cache_entry_t *entry = NULL;

/* Look for the name in the cache
*/
CHEROKEE_RWLOCK_READER (&resolv->lock);
ret = cherokee_avl_get (&resolv->table, domain, (void **)&entry);
CHEROKEE_RWLOCK_UNLOCK (&resolv->lock);

if (ret != ret_ok) {
TRACE (ENTRIES, "Resolve '%s': missed.\n", domain->buf);

/* Bad luck: it wasn't cached
*/
ret = table_add_new_entry (resolv, domain, &entry);
if (ret != ret_ok) {
TRACE (ENTRIES, "Resolve '%s': error ret=%d.\n", domain->buf, ret);
return ret;
}
TRACE (ENTRIES, "Resolve '%s': added succesfuly as '%s'.\n", domain->buf, entry->ip_str.buf);
} else {
TRACE (ENTRIES, "Resolve '%s': hit.\n", domain->buf);
}

*addr_info = entry->addr;
return ret_ok;
}
Something went wrong with that request. Please try again.