Annotation of embedaddon/lighttpd/src/inet_ntop_cache.c, revision 1.1.1.1
1.1 misho 1: #include "base.h"
2: #include "inet_ntop_cache.h"
3: #include "sys-socket.h"
4:
5: #include <sys/types.h>
6:
7: #include <string.h>
8:
9: const char * inet_ntop_cache_get_ip(server *srv, sock_addr *addr) {
10: #ifdef HAVE_IPV6
11: size_t ndx = 0, i;
12: for (i = 0; i < INET_NTOP_CACHE_MAX; i++) {
13: if (srv->inet_ntop_cache[i].ts != 0 && srv->inet_ntop_cache[i].family == addr->plain.sa_family) {
14: if (srv->inet_ntop_cache[i].family == AF_INET6 &&
15: 0 == memcmp(srv->inet_ntop_cache[i].addr.ipv6.s6_addr, addr->ipv6.sin6_addr.s6_addr, 16)) {
16: /* IPv6 found in cache */
17: break;
18: } else if (srv->inet_ntop_cache[i].family == AF_INET &&
19: srv->inet_ntop_cache[i].addr.ipv4.s_addr == addr->ipv4.sin_addr.s_addr) {
20: /* IPv4 found in cache */
21: break;
22:
23: }
24: }
25: }
26:
27: if (i == INET_NTOP_CACHE_MAX) {
28: /* not found in cache */
29:
30: i = ndx;
31: inet_ntop(addr->plain.sa_family,
32: addr->plain.sa_family == AF_INET6 ?
33: (const void *) &(addr->ipv6.sin6_addr) :
34: (const void *) &(addr->ipv4.sin_addr),
35: srv->inet_ntop_cache[i].b2, INET6_ADDRSTRLEN);
36:
37: srv->inet_ntop_cache[i].ts = srv->cur_ts;
38: srv->inet_ntop_cache[i].family = addr->plain.sa_family;
39:
40: if (srv->inet_ntop_cache[i].family == AF_INET) {
41: srv->inet_ntop_cache[i].addr.ipv4.s_addr = addr->ipv4.sin_addr.s_addr;
42: } else if (srv->inet_ntop_cache[i].family == AF_INET6) {
43: memcpy(srv->inet_ntop_cache[i].addr.ipv6.s6_addr, addr->ipv6.sin6_addr.s6_addr, 16);
44: }
45: }
46:
47: return srv->inet_ntop_cache[i].b2;
48: #else
49: UNUSED(srv);
50: return inet_ntoa(addr->ipv4.sin_addr);
51: #endif
52: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>