Browse Source

[core] inet_ntop_cache -> sock_addr_cache

* rename inet_ntop_cache.[ch] to sock_addr_cache.[ch]
* reimplement as separate caches for IPv4 and IPv6
master
Glenn Strauss 10 months ago
parent
commit
655453a195
  1. 2
      src/CMakeLists.txt
  2. 5
      src/Makefile.am
  3. 2
      src/SConscript
  4. 4
      src/connections.c
  5. 76
      src/inet_ntop_cache.c
  6. 9
      src/inet_ntop_cache.h
  7. 2
      src/meson.build
  8. 69
      src/sock_addr_cache.c
  9. 10
      src/sock_addr_cache.h

2
src/CMakeLists.txt

@ -777,7 +777,7 @@ add_executable(lighttpd
connections.c
h2.c
reqpool.c
inet_ntop_cache.c
sock_addr_cache.c
ls-hpack/lshpack.c
algo_xxhash.c
network.c

5
src/Makefile.am

@ -92,7 +92,7 @@ common_src=base64.c buffer.c burl.c log.c \
safe_memclear.c
src = server.c response.c connections.c h2.c reqpool.c \
inet_ntop_cache.c \
sock_addr_cache.c \
network.c \
network_write.c \
ls-hpack/lshpack.c \
@ -463,7 +463,8 @@ hdr = base64.h buffer.h burl.h network.h log.h http_kv.h keyvalue.h \
plugin.h plugin_config.h \
etag.h array.h vector.h \
fdevent_impl.h network_write.h configfile.h \
mod_ssi.h mod_ssi_expr.h inet_ntop_cache.h \
mod_ssi.h mod_ssi_expr.h \
sock_addr_cache.h \
configparser.h mod_ssi_exprparser.h \
rand.h \
sys-crypto.h sys-crypto-md.h \

2
src/SConscript

@ -79,7 +79,7 @@ common_src = Split("base64.c buffer.c burl.c log.c \
")
src = Split("server.c response.c connections.c h2.c reqpool.c \
inet_ntop_cache.c \
sock_addr_cache.c \
ls-hpack/lshpack.c \
algo_xxhash.c \
network.c \

4
src/connections.c

@ -18,7 +18,7 @@
#include "plugin.h"
#include "inet_ntop_cache.h"
#include "sock_addr_cache.h"
#include <sys/stat.h>
@ -1007,7 +1007,7 @@ connection *connection_accepted(server *srv, server_socket *srv_socket, sock_add
con->connection_start = log_epoch_secs;
con->dst_addr = *cnt_addr;
buffer_copy_string(con->dst_addr_buf, inet_ntop_cache_get_ip(srv, &(con->dst_addr)));
sock_addr_cache_inet_ntop_copy_buffer(con->dst_addr_buf,&con->dst_addr);
con->srv_socket = srv_socket;
con->is_ssl_sock = srv_socket->is_ssl;
con->proto_default_port = 80; /* "http" */

76
src/inet_ntop_cache.c

@ -1,76 +0,0 @@
#include "first.h"
#include "inet_ntop_cache.h"
#include "sys-socket.h"
#include <sys/types.h>
#include <string.h>
#ifndef _WIN32
#include <arpa/inet.h>
#endif
#include "sock_addr.h"
const char * inet_ntop_cache_get_ip(server *srv, sock_addr *addr) {
#ifdef HAVE_IPV6
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);
#ifdef HAVE_SYS_UN_H
if (addr->plain.sa_family == AF_UNIX) return addr->un.sun_path;
#endif
for (i = 0; i < INET_NTOP_CACHE_MAX; i++) {
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 (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;
}
}
}
if (i == INET_NTOP_CACHE_MAX) {
/* not found in cache */
const char *s;
i = ndx;
if (++ndx >= INET_NTOP_CACHE_MAX) ndx = 0;
s = sock_addr_inet_ntop(addr, inet_ntop_cache[i].b2, INET6_ADDRSTRLEN);
if (NULL == s) return "";
inet_ntop_cache[i].family = addr->plain.sa_family;
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 inet_ntop_cache[i].b2;
#else
UNUSED(srv);
if (addr->plain.sa_family == AF_INET) return inet_ntoa(addr->ipv4.sin_addr);
#ifdef HAVE_SYS_UN_H
if (addr->plain.sa_family == AF_UNIX) return addr->un.sun_path;
#endif
return "";
#endif
}

9
src/inet_ntop_cache.h

@ -1,9 +0,0 @@
#ifndef _INET_NTOP_CACHE_H_
#define _INET_NTOP_CACHE_H_
#include "first.h"
#include "base_decls.h"
const char * inet_ntop_cache_get_ip(server *srv, sock_addr *addr);
#endif

2
src/meson.build

@ -738,7 +738,7 @@ main_src = [
'connections.c',
'data_config.c',
'h2.c',
'inet_ntop_cache.c',
'sock_addr_cache.c',
'ls-hpack/lshpack.c',
'algo_xxhash.c',
'network_write.c',

69
src/sock_addr_cache.c

@ -0,0 +1,69 @@
#include "first.h"
#include "sock_addr_cache.h"
#include "sys-socket.h"
#include <string.h>
#include "buffer.h"
#include "sock_addr.h"
int sock_addr_cache_inet_ntop_copy_buffer(buffer * const restrict b, const sock_addr * const restrict saddr)
{
#define NTOP_CACHE_MAX 4
static int ndx4;
static struct { struct in_addr ipv4; } ntop4_cache[NTOP_CACHE_MAX];
static struct { char s[INET_ADDRSTRLEN+1]; } ntop4_strs[NTOP_CACHE_MAX];
#ifdef HAVE_IPV6
static int ndx6;
static struct { struct in6_addr ipv6; } ntop6_cache[NTOP_CACHE_MAX];
static struct { char s[INET6_ADDRSTRLEN+1]; } ntop6_strs[NTOP_CACHE_MAX];
#endif
switch (saddr->plain.sa_family) {
case AF_INET:
for (int i = 0; i < NTOP_CACHE_MAX; ++i) {
if (ntop4_cache[i].ipv4.s_addr == saddr->ipv4.sin_addr.s_addr) {
buffer_copy_string(b, ntop4_strs[i].s);
return 0;
}
}
break;
#ifdef HAVE_IPV6
case AF_INET6:
for (int i = 0; i < NTOP_CACHE_MAX; ++i) {
if (0 == memcmp(ntop6_cache[i].ipv6.s6_addr,
saddr->ipv6.sin6_addr.s6_addr, 16)) {
buffer_copy_string(b, ntop6_strs[i].s);
return 0;
}
}
break;
#endif
default:
break;
}
if (0 != sock_addr_inet_ntop_copy_buffer(b, saddr)) {
buffer_string_set_length(b, 0);
return -1;
}
switch (saddr->plain.sa_family) {
case AF_INET:
ntop4_cache[ndx4].ipv4.s_addr = saddr->ipv4.sin_addr.s_addr;
memcpy(ntop4_strs+ndx4, b->ptr, buffer_string_length(b)+1);
if (++ndx4 == NTOP_CACHE_MAX) ndx4 = 0;
break;
#ifdef HAVE_IPV6
case AF_INET6:
memcpy(ntop6_cache[ndx6].ipv6.s6_addr,saddr->ipv6.sin6_addr.s6_addr,16);
memcpy(ntop6_strs+ndx6, b->ptr, buffer_string_length(b)+1);
if (++ndx6 == NTOP_CACHE_MAX) ndx6 = 0;
break;
#endif
default:
break;
}
return 0;
}

10
src/sock_addr_cache.h

@ -0,0 +1,10 @@
#ifndef INCLUDED_SOCK_ADDR_CACHE_H
#define INCLUDED_SOCK_ADDR_CACHE_H
#include "first.h"
#include "buffer.h"
#include "sock_addr.h"
int sock_addr_cache_inet_ntop_copy_buffer(buffer * restrict b, const sock_addr * restrict saddr);
#endif
Loading…
Cancel
Save