Move slap_sockaddrstr into liblutil

This commit is contained in:
Paul B. Henson 2021-02-22 13:33:09 -08:00 committed by Quanah Gibson-Mount
parent 5fa0a651f5
commit dcca73370b
7 changed files with 113 additions and 148 deletions

View File

@ -18,6 +18,14 @@
#include <ldap_cdefs.h>
#include <lber_types.h>
#include <ac/socket.h>
#ifdef HAVE_TCPD
# include <tcpd.h>
# define LUTIL_STRING_UNKNOWN STRING_UNKNOWN
#else /* ! TCP Wrappers */
# define LUTIL_STRING_UNKNOWN "unknown"
#endif /* ! TCP Wrappers */
/*
* Include file for LDAP utility routine
@ -336,6 +344,29 @@ lutil_parse_time( const char *in, unsigned long *tp );
LDAP_LUTIL_F (int)
lutil_unparse_time( char *buf, size_t buflen, unsigned long t );
#ifdef LDAP_PF_LOCAL
#define LUTIL_ADDRLEN (MAXPATHLEN + sizeof("PATH="))
#elif defined(LDAP_PF_INET6)
#define LUTIL_ADDRLEN sizeof("IP=[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535")
#else
#define LUTIL_ADDRLEN sizeof("IP=255.255.255.255:65336")
#endif
typedef union Sockaddr {
struct sockaddr sa_addr;
struct sockaddr_in sa_in_addr;
#ifdef LDAP_PF_INET6
struct sockaddr_storage sa_storage;
struct sockaddr_in6 sa_in6_addr;
#endif
#ifdef LDAP_PF_LOCAL
struct sockaddr_un sa_un_addr;
#endif
} Sockaddr;
LDAP_LUTIL_F (void)
lutil_sockaddrstr(Sockaddr *sa, struct berval *);
#ifdef timerdiv
#define lutil_timerdiv timerdiv
#else /* ! timerdiv */

View File

@ -1070,3 +1070,74 @@ lutil_snprintf( char *buf, ber_len_t bufsize, char **next, ber_len_t *len, LDAP_
return 0;
}
void
lutil_sockaddrstr( Sockaddr *sa, struct berval *addrbuf )
{
char *addr;
switch( sa->sa_addr.sa_family ) {
#ifdef LDAP_PF_LOCAL
case AF_LOCAL:
addrbuf->bv_len = snprintf( addrbuf->bv_val, addrbuf->bv_len,
"PATH=%s", sa->sa_un_addr.sun_path );
break;
#endif
#ifdef LDAP_PF_INET6
case AF_INET6:
strcpy(addrbuf->bv_val, "IP=");
if ( IN6_IS_ADDR_V4MAPPED(&sa->sa_in6_addr.sin6_addr) ) {
#if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
addr = (char *)inet_ntop( AF_INET,
((struct in_addr *)&sa->sa_in6_addr.sin6_addr.s6_addr[12]),
addrbuf->bv_val+3, addrbuf->bv_len-3 );
#else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
addr = inet_ntoa( *((struct in_addr *)
&sa->sa_in6_addr.sin6_addr.s6_addr[12]) );
#endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
if ( !addr ) addr = LUTIL_STRING_UNKNOWN;
if ( addr != addrbuf->bv_val+3 ) {
addrbuf->bv_len = sprintf( addrbuf->bv_val+3, "%s:%d", addr,
(unsigned) ntohs( sa->sa_in6_addr.sin6_port ) ) + 3;
} else {
int len = strlen( addr );
addrbuf->bv_len = sprintf( addr+len, ":%d",
(unsigned) ntohs( sa->sa_in6_addr.sin6_port ) ) + len + 3;
}
} else {
addr = (char *)inet_ntop( AF_INET6,
&sa->sa_in6_addr.sin6_addr,
addrbuf->bv_val+4, addrbuf->bv_len-4 );
if ( !addr ) addr = LUTIL_STRING_UNKNOWN;
if ( addr != addrbuf->bv_val+4 ) {
addrbuf->bv_len = sprintf( addrbuf->bv_val+3, "[%s]:%d", addr,
(unsigned) ntohs( sa->sa_in6_addr.sin6_port ) ) + 3;
} else {
int len = strlen( addr );
addrbuf->bv_val[3] = '[';
addrbuf->bv_len = sprintf( addr+len, "]:%d",
(unsigned) ntohs( sa->sa_in6_addr.sin6_port ) ) + len + 4;
}
}
break;
#endif /* LDAP_PF_INET6 */
case AF_INET:
strcpy(addrbuf->bv_val, "IP=");
#if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
addr = (char *)inet_ntop( AF_INET, &sa->sa_in_addr.sin_addr,
addrbuf->bv_val+3, addrbuf->bv_len-3 );
#else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
addr = inet_ntoa( sa->sa_in_addr.sin_addr );
#endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
if ( !addr ) addr = LUTIL_STRING_UNKNOWN;
if ( addr != addrbuf->bv_val+3 ) {
addrbuf->bv_len = sprintf( addrbuf->bv_val+3, "%s:%d", addr,
(unsigned) ntohs( sa->sa_in_addr.sin_port ) ) + 3;
} else {
int len = strlen( addr );
addrbuf->bv_len = sprintf( addr+len, ":%d",
(unsigned) ntohs( sa->sa_in_addr.sin_port ) ) + len + 3;
}
break;
default:
addrbuf->bv_val[0] = '\0';
}
}

View File

@ -833,24 +833,8 @@ lload_listener(
LloadListener *sl = arg;
LloadConnection *c;
Sockaddr *from = (Sockaddr *)a;
#ifdef SLAPD_RLOOKUPS
char hbuf[NI_MAXHOST];
#endif /* SLAPD_RLOOKUPS */
const char *peeraddr = NULL;
/* we assume INET6_ADDRSTRLEN > INET_ADDRSTRLEN */
char addr[INET6_ADDRSTRLEN];
#ifdef LDAP_PF_LOCAL
char peername[MAXPATHLEN + sizeof("PATH=")];
#ifdef LDAP_PF_LOCAL_SENDMSG
char peerbuf[8];
struct berval peerbv = BER_BVNULL;
#endif
#elif defined(LDAP_PF_INET6)
char peername[sizeof("IP=[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535")];
#else /* ! LDAP_PF_LOCAL && ! LDAP_PF_INET6 */
char peername[sizeof("IP=255.255.255.255:65336")];
#endif /* LDAP_PF_LOCAL */
char peername[LUTIL_ADDRLEN];
struct berval peerbv = BER_BVC(peername);
int cflag;
int tid;
char ebuf[128];
@ -918,40 +902,10 @@ lload_listener(
#ifdef LDAP_PF_INET6
case AF_INET6:
if ( IN6_IS_ADDR_V4MAPPED( &from->sa_in6_addr.sin6_addr ) ) {
#if defined(HAVE_GETADDRINFO) && defined(HAVE_INET_NTOP)
peeraddr = inet_ntop( AF_INET,
( (struct in_addr *)&from->sa_in6_addr.sin6_addr
.s6_addr[12] ),
addr, sizeof(addr) );
#else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
peeraddr = inet_ntoa( *( (struct in_addr *)&from->sa_in6_addr
.sin6_addr.s6_addr[12] ) );
#endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
if ( !peeraddr ) peeraddr = SLAP_STRING_UNKNOWN;
sprintf( peername, "IP=%s:%d", peeraddr,
(unsigned)ntohs( from->sa_in6_addr.sin6_port ) );
} else {
peeraddr = inet_ntop( AF_INET6, &from->sa_in6_addr.sin6_addr,
addr, sizeof(addr) );
if ( !peeraddr ) peeraddr = SLAP_STRING_UNKNOWN;
sprintf( peername, "IP=[%s]:%d", peeraddr,
(unsigned)ntohs( from->sa_in6_addr.sin6_port ) );
}
break;
#endif /* LDAP_PF_INET6 */
case AF_INET: {
#if defined(HAVE_GETADDRINFO) && defined(HAVE_INET_NTOP)
peeraddr = inet_ntop(
AF_INET, &from->sa_in_addr.sin_addr, addr, sizeof(addr) );
#else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
peeraddr = inet_ntoa( from->sa_in_addr.sin_addr );
#endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
if ( !peeraddr ) peeraddr = SLAP_STRING_UNKNOWN;
sprintf( peername, "IP=%s:%d", peeraddr,
(unsigned)ntohs( from->sa_in_addr.sin_port ) );
} break;
case AF_INET:
lutil_sockaddrstr( from, &peerbv );
break;
default:
lloadd_close( s );

View File

@ -2079,78 +2079,6 @@ destroy_listeners( void )
slap_listeners = NULL;
}
void
slap_sockaddrstr( Sockaddr *sa, struct berval *addrbuf )
{
char *addr;
switch( sa->sa_addr.sa_family ) {
#ifdef LDAP_PF_LOCAL
case AF_LOCAL:
addrbuf->bv_len = snprintf( addrbuf->bv_val, addrbuf->bv_len,
"PATH=%s", sa->sa_un_addr.sun_path );
break;
#endif
#ifdef LDAP_PF_INET6
case AF_INET6:
strcpy(addrbuf->bv_val, "IP=");
if ( IN6_IS_ADDR_V4MAPPED(&sa->sa_in6_addr.sin6_addr) ) {
#if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
addr = (char *)inet_ntop( AF_INET,
((struct in_addr *)&sa->sa_in6_addr.sin6_addr.s6_addr[12]),
addrbuf->bv_val+3, addrbuf->bv_len-3 );
#else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
addr = inet_ntoa( *((struct in_addr *)
&sa->sa_in6_addr.sin6_addr.s6_addr[12]) );
#endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
if ( !addr ) addr = SLAP_STRING_UNKNOWN;
if ( addr != addrbuf->bv_val+3 ) {
addrbuf->bv_len = sprintf( addrbuf->bv_val+3, "%s:%d", addr,
(unsigned) ntohs( sa->sa_in6_addr.sin6_port ) ) + 3;
} else {
int len = strlen( addr );
addrbuf->bv_len = sprintf( addr+len, ":%d",
(unsigned) ntohs( sa->sa_in6_addr.sin6_port ) ) + len + 3;
}
} else {
addr = (char *)inet_ntop( AF_INET6,
&sa->sa_in6_addr.sin6_addr,
addrbuf->bv_val+4, addrbuf->bv_len-4 );
if ( !addr ) addr = SLAP_STRING_UNKNOWN;
if ( addr != addrbuf->bv_val+4 ) {
addrbuf->bv_len = sprintf( addrbuf->bv_val+3, "[%s]:%d", addr,
(unsigned) ntohs( sa->sa_in6_addr.sin6_port ) ) + 3;
} else {
int len = strlen( addr );
addrbuf->bv_val[3] = '[';
addrbuf->bv_len = sprintf( addr+len, "]:%d",
(unsigned) ntohs( sa->sa_in6_addr.sin6_port ) ) + len + 4;
}
}
break;
#endif /* LDAP_PF_INET6 */
case AF_INET:
strcpy(addrbuf->bv_val, "IP=");
#if defined( HAVE_GETADDRINFO ) && defined( HAVE_INET_NTOP )
addr = (char *)inet_ntop( AF_INET, &sa->sa_in_addr.sin_addr,
addrbuf->bv_val+3, addrbuf->bv_len-3 );
#else /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
addr = inet_ntoa( sa->sa_in_addr.sin_addr );
#endif /* ! HAVE_GETADDRINFO || ! HAVE_INET_NTOP */
if ( !addr ) addr = SLAP_STRING_UNKNOWN;
if ( addr != addrbuf->bv_val+3 ) {
addrbuf->bv_len = sprintf( addrbuf->bv_val+3, "%s:%d", addr,
(unsigned) ntohs( sa->sa_in_addr.sin_port ) ) + 3;
} else {
int len = strlen( addr );
addrbuf->bv_len = sprintf( addr+len, ":%d",
(unsigned) ntohs( sa->sa_in_addr.sin_port ) ) + len + 3;
}
break;
default:
addrbuf->bv_val[0] = '\0';
}
}
static int
slap_listener(
Listener *sl )
@ -2169,7 +2097,7 @@ slap_listener(
char *dnsname = NULL;
const char *peeraddr = NULL;
/* we assume INET6_ADDRSTRLEN > INET_ADDRSTRLEN */
char peername[SLAP_ADDRLEN];
char peername[LUTIL_ADDRLEN];
struct berval peerbv = BER_BVC(peername);
#ifdef LDAP_PF_LOCAL_SENDMSG
char peerbuf[8];
@ -2342,7 +2270,7 @@ slap_listener(
case AF_INET6:
# endif /* LDAP_PF_INET6 */
case AF_INET:
slap_sockaddrstr( &from, &peerbv );
lutil_sockaddrstr( &from, &peerbv );
break;
default:

View File

@ -894,7 +894,6 @@ LDAP_SLAPD_F (void) slap_resume_listeners LDAP_P((void));
LDAP_SLAPD_F (int) slap_pause_server LDAP_P((void));
LDAP_SLAPD_F (int) slap_unpause_server LDAP_P((void));
LDAP_SLAPD_F (void) slap_sockaddrstr LDAP_P((Sockaddr *sa, struct berval *));
LDAP_SLAPD_F (void) slapd_set_write LDAP_P((ber_socket_t s, int wake));
LDAP_SLAPD_F (void) slapd_clr_write LDAP_P((ber_socket_t s, int wake));

View File

@ -56,6 +56,8 @@
#include "ldap_pvt_thread.h"
#include "ldap_queue.h"
#include "lutil.h"
LDAP_BEGIN_DECL
#ifdef LDAP_DEVEL
@ -370,18 +372,6 @@ typedef struct Operation Operation;
typedef struct SlapReply SlapReply;
/* end of forward declarations */
typedef union Sockaddr {
struct sockaddr sa_addr;
struct sockaddr_in sa_in_addr;
#ifdef LDAP_PF_INET6
struct sockaddr_storage sa_storage;
struct sockaddr_in6 sa_in6_addr;
#endif
#ifdef LDAP_PF_LOCAL
struct sockaddr_un sa_un_addr;
#endif
} Sockaddr;
#ifdef LDAP_PF_INET6
extern int slap_inet4or6;
#endif
@ -2858,14 +2848,6 @@ typedef void (SEND_LDAP_INTERMEDIATE)(
typedef struct Listener Listener;
#ifdef LDAP_PF_LOCAL
#define SLAP_ADDRLEN (MAXPATHLEN + sizeof("PATH="))
#elif defined(LDAP_PF_INET6)
#define SLAP_ADDRLEN sizeof("IP=[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535")
#else
#define SLAP_ADDRLEN sizeof("IP=255.255.255.255:65336")
#endif
/*
* represents a connection from an ldap client
*/

View File

@ -167,7 +167,7 @@ typedef struct syncinfo_s {
struct berval si_lastCookieRcvd;
struct berval si_lastCookieSent;
struct berval si_monitor_ndn;
char si_connaddrbuf[SLAP_ADDRLEN];
char si_connaddrbuf[LUTIL_ADDRLEN];
ldap_pvt_thread_mutex_t si_monitor_mutex;
ldap_pvt_thread_mutex_t si_mutex;
@ -2056,7 +2056,7 @@ reload:
if ( !getsockname( s, &addr.sa_addr, &len )) {
si->si_connaddr.bv_val = si->si_connaddrbuf;
si->si_connaddr.bv_len = sizeof( si->si_connaddrbuf );
slap_sockaddrstr( &addr, &si->si_connaddr );
lutil_sockaddrstr( &addr, &si->si_connaddr );
}
}