Browse Source

[core] inet_ntop_cache now 4-element cache

(round-robin replacement; fixes what was effectively a 1-element cache)
personal/stbuehler/mod-csrf
Glenn Strauss 4 years ago
parent
commit
ba679290f9
  1. 16
      src/base.h
  2. 44
      src/inet_ntop_cache.c
  3. 1
      src/settings.h

16
src/base.h

@ -425,19 +425,6 @@ typedef struct {
} connections;
#ifdef HAVE_IPV6
typedef struct {
int family;
union {
struct in6_addr ipv6;
struct in_addr ipv4;
} addr;
char b2[INET6_ADDRSTRLEN + 1];
time_t ts;
} inet_ntop_cache_type;
#endif
typedef struct {
buffer *uri;
time_t mtime;
@ -564,9 +551,6 @@ typedef struct server {
buffer *cond_check_buf;
/* caches */
#ifdef HAVE_IPV6
inet_ntop_cache_type inet_ntop_cache[INET_NTOP_CACHE_MAX];
#endif
mtime_cache_type mtime_cache[FILE_CACHE_MAX];
array *split_vals;

44
src/inet_ntop_cache.c

@ -87,15 +87,28 @@ int sock_addr_inet_ntop_append_buffer(buffer *b, const sock_addr *addr)
const char * inet_ntop_cache_get_ip(server *srv, sock_addr *addr) {
#ifdef HAVE_IPV6
size_t ndx = 0, i;
typedef struct {
int family;
union {
struct in6_addr ipv6;
struct in_addr ipv4;
} addr;
char b2[INET6_ADDRSTRLEN + 1];
} inet_ntop_cache_type;
#define INET_NTOP_CACHE_MAX 4
static inet_ntop_cache_type inet_ntop_cache[INET_NTOP_CACHE_MAX];
static int ndx;
int i;
UNUSED(srv);
for (i = 0; i < INET_NTOP_CACHE_MAX; i++) {
if (srv->inet_ntop_cache[i].ts != 0 && srv->inet_ntop_cache[i].family == addr->plain.sa_family) {
if (srv->inet_ntop_cache[i].family == AF_INET6 &&
0 == memcmp(srv->inet_ntop_cache[i].addr.ipv6.s6_addr, addr->ipv6.sin6_addr.s6_addr, 16)) {
if (inet_ntop_cache[i].family == addr->plain.sa_family) {
if (inet_ntop_cache[i].family == AF_INET6 &&
0 == memcmp(inet_ntop_cache[i].addr.ipv6.s6_addr, addr->ipv6.sin6_addr.s6_addr, 16)) {
/* IPv6 found in cache */
break;
} else if (srv->inet_ntop_cache[i].family == AF_INET &&
srv->inet_ntop_cache[i].addr.ipv4.s_addr == addr->ipv4.sin_addr.s_addr) {
} else if (inet_ntop_cache[i].family == AF_INET &&
inet_ntop_cache[i].addr.ipv4.s_addr == addr->ipv4.sin_addr.s_addr) {
/* IPv4 found in cache */
break;
@ -107,29 +120,26 @@ const char * inet_ntop_cache_get_ip(server *srv, sock_addr *addr) {
/* not found in cache */
const char *s;
/* TODO: ndx is never modified above;
* inet_ntop_cache is effectively a 1-element cache */
i = ndx;
if (++ndx >= INET_NTOP_CACHE_MAX) ndx = 0;
s =
inet_ntop(addr->plain.sa_family,
addr->plain.sa_family == AF_INET6 ?
(const void *) &(addr->ipv6.sin6_addr) :
(const void *) &(addr->ipv4.sin_addr),
srv->inet_ntop_cache[i].b2, INET6_ADDRSTRLEN);
inet_ntop_cache[i].b2, INET6_ADDRSTRLEN);
if (NULL == s) return "";
srv->inet_ntop_cache[i].ts = srv->cur_ts;
srv->inet_ntop_cache[i].family = addr->plain.sa_family;
inet_ntop_cache[i].family = addr->plain.sa_family;
if (srv->inet_ntop_cache[i].family == AF_INET) {
srv->inet_ntop_cache[i].addr.ipv4.s_addr = addr->ipv4.sin_addr.s_addr;
} else if (srv->inet_ntop_cache[i].family == AF_INET6) {
memcpy(srv->inet_ntop_cache[i].addr.ipv6.s6_addr, addr->ipv6.sin6_addr.s6_addr, 16);
if (inet_ntop_cache[i].family == AF_INET) {
inet_ntop_cache[i].addr.ipv4.s_addr = addr->ipv4.sin_addr.s_addr;
} else if (inet_ntop_cache[i].family == AF_INET6) {
memcpy(inet_ntop_cache[i].addr.ipv6.s6_addr, addr->ipv6.sin6_addr.s6_addr, 16);
}
}
return srv->inet_ntop_cache[i].b2;
return inet_ntop_cache[i].b2;
#else
UNUSED(srv);
return inet_ntoa(addr->ipv4.sin_addr);

1
src/settings.h

@ -12,7 +12,6 @@
#define BV(x) (1 << x)
#define INET_NTOP_CACHE_MAX 4
#define FILE_CACHE_MAX 16
/**

Loading…
Cancel
Save