* citserver locate_host() migrated to getpeername() and getnameinfo(). These API...
authorArt Cancro <ajc@citadel.org>
Mon, 16 Aug 2010 17:04:07 +0000 (17:04 +0000)
committerArt Cancro <ajc@citadel.org>
Mon, 16 Aug 2010 17:04:07 +0000 (17:04 +0000)
citadel/citserver.c
citadel/locate_host.c
citadel/locate_host.h

index 44139cce1d64273bb2205947d4e2c1b42aa721ef..9b522032daa9a5d51bf08acc22916c46bd0bbed1 100644 (file)
@@ -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... */
index 3e0a36d0b65fa904433e775d0632f82d1ed1fc0f..0331d82df174d7b8efbfa95ea47593f554fa276d 100644 (file)
 #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);
 }
 
 
index b71d389c74d0e6503c4eb50056b95a33b4e3a139..21ae5bdfdd48a55b02cae5ce53b85198f7ccadad 100644 (file)
@@ -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);