Refactor clamd socket connection
[users/jgh/exim.git] / src / src / malware.c
index 9a822fdc21e554066d152c4da212f915e585f4ec..298031aa39a2777905cbcdec9fdca5c5696499c6 100644 (file)
@@ -226,44 +226,14 @@ clmd_errlog(const uschar * str)
 
 /*************************************************/
 
-static int
-m_streamsocket(const uschar * hostname, struct in_addr * inp, uschar ** errstr)
-{
-  struct hostent * he;
-  int sock;
-
-  /* Lookup the host */
-  if(!(he = gethostbyname(CS hostname))) {
-    *errstr = string_sprintf("failed to lookup host '%s'", hostname);
-    return -1;
-  }
-/*XXX will fail for ipv6 */
-  *inp = *(struct in_addr *) he->h_addr_list[0];
-
-  /* Create the TCP socket */
-  if ((sock = ip_socket(SOCK_STREAM, AF_INET)) >= 0)
-    return sock;
-
-  *errstr = string_sprintf("unable to acquire socket (%s)", strerror(errno));
-  return -1;
-}
-
-static int
+/* Only used by the Clamav code, which is working from a list of servers and
+uses the returned in_addr to get a second connection to the same system.
+*/
+static inline int
 m_tcpsocket(const uschar * hostname, unsigned int port,
-       struct in_addr * inp, uschar ** errstr)
+       host_item * host, uschar ** errstr)
 {
-  int sock;
-
-  if ((sock = m_streamsocket(hostname, inp, errstr)) < 0)
-    return -1;
-
-  if (ip_connect(sock, AF_INET, (uschar*)inet_ntoa(*inp), port, 5) >= 0)
-    return sock;
-
-  *errstr = string_sprintf("connection to %s, port %u failed (%s)",
-                     inet_ntoa(*inp), port, strerror(errno));
-  (void)close(sock);
-  return -1;
+  return ip_connectedsocket(SOCK_STREAM, hostname, port, port, 5, host, errstr);
 }
 
 static int
@@ -284,7 +254,7 @@ m_tcpsocket_fromdef(const uschar * hostport, uschar ** errstr)
   }
 
   return ip_connectedsocket(SOCK_STREAM, hostname, portlow, porthigh,
-                           5, errstr);
+                           5, NULL, errstr);
 }
 
 static int
@@ -1171,7 +1141,7 @@ malware_internal(uschar **listptr, uschar *eml_filename, BOOL faking)
        uschar * file_name;
        uschar av_buffer[1024];
        uschar *hostname = "";
-       struct in_addr in;
+       host_item connhost;
        uschar *clamav_fbuf;
        int clam_fd, result;
        unsigned int fsize;
@@ -1261,7 +1231,7 @@ malware_internal(uschar **listptr, uschar *eml_filename, BOOL faking)
             * on both connections (as one host could resolve to multiple ips) */
            sock= m_tcpsocket(CS clamd_address_vector[current_server]->tcp_addr,
                                clamd_address_vector[current_server]->tcp_port,
-                               &in, &errstr);
+                               &connhost, &errstr);
            if (sock >= 0) {
              /* Connection successfully established with a server */
              hostname = clamd_address_vector[current_server]->tcp_addr;
@@ -1333,19 +1303,10 @@ malware_internal(uschar **listptr, uschar *eml_filename, BOOL faking)
                av_buffer2));
          }
 
-         if ( (sockData = ip_socket(SOCK_STREAM, AF_INET)) < 0) {
-           int err = errno;
+         sockData = m_tcpsocket(connhost.address, port, NULL, &errstr);
+         if (sockData < 0) {
            (void)close(sock);
-           return clmd_errlog_defer(
-             string_sprintf("unable to acquire socket (%s)", strerror(err)));
-         }
-
-         if (ip_connect(sockData, AF_INET, (uschar*)inet_ntoa(in), port, 5) < 0) {
-           int err = errno;
-           (void)close(sockData); (void)close(sock);
-           return clmd_errlog_defer(
-             string_sprintf("connection to %s, port %u failed (%s)",
-                   inet_ntoa(in), port, strerror(err)));
+           return clmd_errlog_defer(errstr);
          }
 
   #define CLOSE_SOCKDATA (void)close(sockData)