reset response headers, write_queue for error docs
[lighttpd.git] / src / inet_ntop_cache.c
blob3d921404a0308048291234813fcff7d0110bc22c
1 #include "first.h"
3 #include "base.h"
4 #include "inet_ntop_cache.h"
5 #include "sys-socket.h"
7 #include <sys/types.h>
9 #include <string.h>
11 const char * inet_ntop_cache_get_ip(server *srv, sock_addr *addr) {
12 #ifdef HAVE_IPV6
13 size_t ndx = 0, i;
14 for (i = 0; i < INET_NTOP_CACHE_MAX; i++) {
15 if (srv->inet_ntop_cache[i].ts != 0 && srv->inet_ntop_cache[i].family == addr->plain.sa_family) {
16 if (srv->inet_ntop_cache[i].family == AF_INET6 &&
17 0 == memcmp(srv->inet_ntop_cache[i].addr.ipv6.s6_addr, addr->ipv6.sin6_addr.s6_addr, 16)) {
18 /* IPv6 found in cache */
19 break;
20 } else if (srv->inet_ntop_cache[i].family == AF_INET &&
21 srv->inet_ntop_cache[i].addr.ipv4.s_addr == addr->ipv4.sin_addr.s_addr) {
22 /* IPv4 found in cache */
23 break;
29 if (i == INET_NTOP_CACHE_MAX) {
30 /* not found in cache */
32 i = ndx;
33 inet_ntop(addr->plain.sa_family,
34 addr->plain.sa_family == AF_INET6 ?
35 (const void *) &(addr->ipv6.sin6_addr) :
36 (const void *) &(addr->ipv4.sin_addr),
37 srv->inet_ntop_cache[i].b2, INET6_ADDRSTRLEN);
39 srv->inet_ntop_cache[i].ts = srv->cur_ts;
40 srv->inet_ntop_cache[i].family = addr->plain.sa_family;
42 if (srv->inet_ntop_cache[i].family == AF_INET) {
43 srv->inet_ntop_cache[i].addr.ipv4.s_addr = addr->ipv4.sin_addr.s_addr;
44 } else if (srv->inet_ntop_cache[i].family == AF_INET6) {
45 memcpy(srv->inet_ntop_cache[i].addr.ipv6.s6_addr, addr->ipv6.sin6_addr.s6_addr, 16);
49 return srv->inet_ntop_cache[i].b2;
50 #else
51 UNUSED(srv);
52 return inet_ntoa(addr->ipv4.sin_addr);
53 #endif