Annotation of embedaddon/lighttpd/src/inet_ntop_cache.c, revision 1.1.1.2
1.1.1.2 ! misho 1: #include "first.h"
! 2:
1.1 misho 3: #include "base.h"
4: #include "inet_ntop_cache.h"
5: #include "sys-socket.h"
6:
7: #include <sys/types.h>
8:
9: #include <string.h>
10:
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;
24:
25: }
26: }
27: }
28:
29: if (i == INET_NTOP_CACHE_MAX) {
30: /* not found in cache */
31:
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);
38:
39: srv->inet_ntop_cache[i].ts = srv->cur_ts;
40: srv->inet_ntop_cache[i].family = addr->plain.sa_family;
41:
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);
46: }
47: }
48:
49: return srv->inet_ntop_cache[i].b2;
50: #else
51: UNUSED(srv);
52: return inet_ntoa(addr->ipv4.sin_addr);
53: #endif
54: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>