Commit 829263c4 authored by Howard Chu's avatar Howard Chu
Browse files

ITS#8847 move lutil_sockaddrstr() to ldap_pvt_sockaddrstr()

parent 8382d3c3
......@@ -34,6 +34,7 @@
#include <ac/stdlib.h>
#include <stdio.h>
#include <ac/unistd.h>
#include <ac/socket.h>
#include "ldap.h"
#include "ldap_pvt.h"
......
......@@ -170,6 +170,36 @@ ldap_pvt_get_hname LDAP_P((
int namelen,
char **herr ));
#ifdef LDAP_PF_LOCAL
#define LDAP_IPADDRLEN (MAXPATHLEN + sizeof("PATH="))
#elif defined(LDAP_PF_INET6)
#define LDAP_IPADDRLEN sizeof("IP=[ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:65535")
#else
#define LDAP_IPADDRLEN sizeof("IP=255.255.255.255:65336")
#endif
struct sockaddr_in;
struct sockaddr_in6;
struct sockaddr_storage;
struct sockaddr_un;
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_F (void)
ldap_pvt_sockaddrstr LDAP_P((
Sockaddr *sa,
struct berval * ));
/* charray.c */
......
......@@ -344,29 +344,6 @@ 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 */
......
......@@ -864,14 +864,14 @@ ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
ld->ld_sb ) ? " (default)" : "" );
}
if ( lc->lconn_sb != NULL ) {
char from[LUTIL_ADDRLEN];
char from[LDAP_IPADDRLEN];
struct berval frombv = BER_BVC(from);
ber_socket_t sb;
if ( ber_sockbuf_ctrl( lc->lconn_sb, LBER_SB_OPT_GET_FD, &sb ) == 1 ) {
struct sockaddr_in sin;
socklen_t len = sizeof( sin );
if ( getsockname( sb, (struct sockaddr *)&sin, &len ) == 0 ) {
lutil_sockaddrstr( (Sockaddr *) &sin, &frombv );
ldap_pvt_sockaddrstr( (Sockaddr *) &sin, &frombv );
Debug1( LDAP_DEBUG_TRACE, "* from: %s\n",
( from == NULL ) ? "(null)" : from );
}
......
......@@ -917,3 +917,84 @@ char *ldap_pvt_gai_strerror (int code) {
return _("Unknown error");
}
#endif
/* format a socket address as a string */
#ifdef HAVE_TCPD
# include <tcpd.h>
# define SOCKADDR_STRING_UNKNOWN STRING_UNKNOWN
#else /* ! TCP Wrappers */
# define SOCKADDR_STRING_UNKNOWN "unknown"
#endif /* ! TCP Wrappers */
void
ldap_pvt_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 = SOCKADDR_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 = SOCKADDR_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 = SOCKADDR_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';
}
}
......@@ -1069,75 +1069,3 @@ 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';
}
}
......@@ -835,7 +835,7 @@ lload_listener(
LloadListener *sl = arg;
LloadConnection *c;
Sockaddr *from = (Sockaddr *)a;
char peername[LUTIL_ADDRLEN];
char peername[LDAP_IPADDRLEN];
struct berval peerbv = BER_BVC(peername);
int cflag;
int tid;
......@@ -916,7 +916,7 @@ lload_listener(
case AF_INET6:
#endif /* LDAP_PF_INET6 */
case AF_INET:
lutil_sockaddrstr( from, &peerbv );
ldap_pvt_sockaddrstr( from, &peerbv );
break;
default:
......
......@@ -1575,14 +1575,14 @@ retry_bind:
}
if ( LogTest( LDAP_DEBUG_TRACE )) {
ber_socket_t s;
char sockname[LUTIL_ADDRLEN];
char sockname[LDAP_IPADDRLEN];
struct berval sockbv = BER_BVC( sockname );
Sockaddr addr;
socklen_t len = sizeof( addr );
ldap_get_option( msc->msc_ld, LDAP_OPT_DESC, &s );
getsockname( s, &addr.sa_addr, &len );
lutil_sockaddrstr( &addr, &sockbv );
ldap_pvt_sockaddrstr( &addr, &sockbv );
Debug( LDAP_DEBUG_TRACE, "%s asyncmeta_dobind_init msc %p ld %p ldr %p fd %d addr %s\n",
op->o_log_prefix, msc, msc->msc_ld, msc->msc_ldr, s, sockname );
}
......
......@@ -2098,7 +2098,7 @@ slap_listener(
char *dnsname = NULL;
/* we assume INET6_ADDRSTRLEN > INET_ADDRSTRLEN */
char peername[LUTIL_ADDRLEN];
char peername[LDAP_IPADDRLEN];
struct berval peerbv = BER_BVC(peername);
#ifdef LDAP_PF_LOCAL_SENDMSG
char peerbuf[8];
......@@ -2278,7 +2278,7 @@ slap_listener(
return 0;
}
}
lutil_sockaddrstr( &from, &peerbv );
ldap_pvt_sockaddrstr( &from, &peerbv );
break;
default:
......
......@@ -61,7 +61,7 @@ int
proxyp( ber_socket_t sfd, Sockaddr *from ) {
proxyp_header pph;
proxyp_addr ppa;
char peername[LUTIL_ADDRLEN];
char peername[LDAP_IPADDRLEN];
struct berval peerbv = BER_BVC(peername);
/* Maximum size of header minus static component size is max option size */
uint8_t proxyp_options[536 - 16];
......@@ -152,7 +152,7 @@ proxyp( ber_socket_t sfd, Sockaddr *from ) {
case 0x01: /* PROXY command */
switch ( pph.fam ) {
case 0x11: /* TCPv4 */
lutil_sockaddrstr( from, &peerbv );
ldap_pvt_sockaddrstr( from, &peerbv );
Debug( LDAP_DEBUG_STATS, "proxyp(%ld): via %s\n",
(long)sfd, peername );
......@@ -163,7 +163,7 @@ proxyp( ber_socket_t sfd, Sockaddr *from ) {
case 0x21: /* TCPv6 */
#ifdef LDAP_PF_INET6
lutil_sockaddrstr( from, &peerbv );
ldap_pvt_sockaddrstr( from, &peerbv );
Debug( LDAP_DEBUG_STATS, "proxyp(%ld): via %s\n",
(long)sfd, peername );
from->sa_in6_addr.sin6_family = AF_INET6;
......
......@@ -167,7 +167,7 @@ typedef struct syncinfo_s {
struct berval si_lastCookieRcvd;
struct berval si_lastCookieSent;
struct berval si_monitor_ndn;
char si_connaddrbuf[LUTIL_ADDRLEN];
char si_connaddrbuf[LDAP_IPADDRLEN];
ldap_pvt_thread_mutex_t si_monitor_mutex;
ldap_pvt_thread_mutex_t si_mutex;
......@@ -2055,7 +2055,7 @@ do_syncrepl(
if ( !getsockname( s, &addr.sa_addr, &len )) {
si->si_connaddr.bv_val = si->si_connaddrbuf;
si->si_connaddr.bv_len = sizeof( si->si_connaddrbuf );
lutil_sockaddrstr( &addr, &si->si_connaddr );
ldap_pvt_sockaddrstr( &addr, &si->si_connaddr );
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment