From 879934a987d63e87b2960ff044be80b42caaf41a Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Mon, 16 Aug 2010 17:04:07 +0000 Subject: [PATCH] * citserver locate_host() migrated to getpeername() and getnameinfo(). These API's are both simpler and IPv4/IPv6 compatible. --- citadel/citserver.c | 13 +++---- citadel/locate_host.c | 88 ++++--------------------------------------- citadel/locate_host.h | 4 +- 3 files changed, 15 insertions(+), 90 deletions(-) diff --git a/citadel/citserver.c b/citadel/citserver.c index 44139cce1..9b522032d 100644 --- a/citadel/citserver.c +++ b/citadel/citserver.c @@ -907,15 +907,14 @@ void begin_session(CitContext *con) con->cs_host[sizeof con->cs_host - 1] = 0; len = sizeof sin; if (!CC->is_local_socket) { - if (!getpeername(con->client_socket, (struct sockaddr *) &sin, &len)) { - locate_host(con->cs_host, sizeof con->cs_host, - con->cs_addr, sizeof con->cs_addr, - &sin.sin_addr - ); - } + locate_host(con->cs_host, sizeof con->cs_host, + con->cs_addr, sizeof con->cs_addr, + con->client_socket + ); } else { - strcpy(con->cs_host, ""); + con->cs_host[0] = 0; + con->cs_addr[0] = 0; #ifdef HAVE_STRUCT_UCRED { /* as http://www.wsinnovations.com/softeng/articles/uds.html told us... */ diff --git a/citadel/locate_host.c b/citadel/locate_host.c index 3e0a36d0b..0331d82df 100644 --- a/citadel/locate_host.c +++ b/citadel/locate_host.c @@ -38,89 +38,17 @@ #endif -/* Hacks to work around nameser.h declarations missing on OpenBSD - * see also: http://search.cpan.org/src/MIKER/Net-DNS-ToolKit-0.30/ToolKit.h - */ - -#ifndef NS_INT16SZ -# ifdef INT16SZ -# define NS_INT16SZ INT16SZ -# endif -#endif - -#ifndef NS_INT32SZ -# ifdef INT32SZ -# define NS_INT32SZ INT32SZ -# endif -#endif - -#ifndef NS_GET16 -# ifdef GETSHORT -# define NS_GET16 GETSHORT -# endif -#endif - - -/***************************************************************************/ - - -void locate_host(char *tbuf, size_t n, - char *abuf, size_t na, - const struct in_addr *addr) +void locate_host(char *tbuf, size_t n, char *abuf, size_t na, int client_socket) { - struct hostent *ch; - const char *i; - char *j; - int a1, a2, a3, a4; - char address_string[SIZ]; - + struct sockaddr_in6 clientaddr; + unsigned int addrlen = sizeof(clientaddr); -#ifdef HAVE_NONREENTRANT_NETDB - begin_critical_section(S_NETDB); -#endif - - i = (const char *) addr; - a1 = ((*i++) & 0xff); - a2 = ((*i++) & 0xff); - a3 = ((*i++) & 0xff); - a4 = ((*i++) & 0xff); - sprintf(address_string, "%d.%d.%d.%d", a1, a2, a3, a4); - - if (abuf != NULL) { - safestrncpy(abuf, address_string, na); - } - - if ((ch = gethostbyaddr((const char *) addr, - sizeof(*addr), AF_INET)) == NULL) { -bad_dns: - safestrncpy(tbuf, address_string, n); - goto end; /* because we might need to end the critical - section */ - } - /* check if the forward DNS agrees; if not, they're spoofing */ - j = strdup(ch->h_name); - ch = gethostbyname(j); - free(j); - if (ch == NULL) - goto bad_dns; - - /* check address for consistency */ - for (; *ch->h_addr_list; ch->h_addr_list++) - if (!memcmp(*ch->h_addr_list, addr, - sizeof *addr)) { - safestrncpy(tbuf, ch->h_name, 63); - goto end; - } - goto bad_dns; /* they were spoofing. report a numeric IP - address. */ - - end: - -#ifdef HAVE_NONREENTRANT_NETDB - end_critical_section(S_NETDB); -#endif + tbuf[0] = 0; + abuf[0] = 0; - tbuf[63] = 0; + getpeername(client_socket, (struct sockaddr *)&clientaddr, &addrlen); + getnameinfo((struct sockaddr *)&clientaddr, addrlen, tbuf, n, NULL, 0, 0); + getnameinfo((struct sockaddr *)&clientaddr, addrlen, abuf, na, NULL, 0, NI_NUMERICHOST); } diff --git a/citadel/locate_host.h b/citadel/locate_host.h index b71d389c7..21ae5bdfd 100644 --- a/citadel/locate_host.h +++ b/citadel/locate_host.h @@ -1,7 +1,5 @@ /* $Id$ */ -void locate_host(char *tbuf, size_t n, - char *abuf, size_t na, - const struct in_addr *addr); +void locate_host(char *tbuf, size_t n, char *abuf, size_t na, int client_socket); int rbl_check(char *message_to_spammer); int hostname_to_dotted_quad(char *addr, char *host); int rblcheck_backend(char *domain, char *txtbuf, int txtbufsize); -- 2.30.2