[core] inet_ntop_cache -> sock_addr_cache
* rename inet_ntop_cache.[ch] to sock_addr_cache.[ch] * reimplement as separate caches for IPv4 and IPv6master
parent
1212f60991
commit
655453a195
@ -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
|
||||
}
|
@ -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
|
@ -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;
|
||||
}
|
@ -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…
Reference in New Issue