Make useful socket functions more generally available
[exim.git] / src / src / malware.c
index 43f0314942312c110d6cf5aea394ac37e0422f73..2a1e2165441793b5e92f75e39bfd47c461701f45 100644 (file)
@@ -144,58 +144,6 @@ m_tcpsocket(const uschar * hostname, unsigned int port,
   return ip_connectedsocket(SOCK_STREAM, hostname, port, port, 5, host, errstr);
 }
 
-static int
-m_tcpsocket_fromdef(const uschar * hostport, uschar ** errstr)
-{
-  int scan;
-  uschar hostname[256];
-  unsigned int portlow, porthigh;
-
-  /* extract host and port part */
-  scan = sscanf(CS hostport, "%255s %u-%u", hostname, &portlow, &porthigh);
-  if ( scan != 3 ) {
-    if ( scan != 2 ) {
-      *errstr = string_sprintf("invalid socket '%s'", hostport);
-      return -1;
-    }
-    porthigh = portlow;
-  }
-
-  return ip_connectedsocket(SOCK_STREAM, hostname, portlow, porthigh,
-                           5, NULL, errstr);
-}
-
-static int
-m_unixsocket(const uschar * path, uschar ** errstr)
-{
-  int sock;
-  struct sockaddr_un server;
-
-  if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
-    *errstr = US"can't open UNIX socket.";
-    return -1;
-  }
-
-  server.sun_family = AF_UNIX;
-  Ustrncpy(server.sun_path, path, sizeof(server.sun_path)-1);
-  server.sun_path[sizeof(server.sun_path)-1] = '\0';
-  if (connect(sock, (struct sockaddr *) &server, sizeof(server)) < 0) {
-    int err = errno;
-    (void)close(sock);
-    *errstr =  string_sprintf("unable to connect to UNIX socket (%s): %s",
-                 path, strerror(err));
-    return -1;
-    }
-  return sock;
-}
-
-static inline int
-m_streamsocket(const uschar * spec, uschar ** errstr)
-{
-  return *spec == '/'
-    ? m_unixsocket(spec, errstr) : m_tcpsocket_fromdef(spec, errstr);
-}
-
 static int
 m_sock_send(int sock, uschar * buf, int cnt, uschar ** errstr)
 {
@@ -518,7 +466,8 @@ if (!malware_ok)
   if (!timeout) timeout = MALWARE_TIMEOUT;
   tmo = time(NULL) + timeout;
 
-  for (scanent = m_scans; ; scanent++) {
+  for (scanent = m_scans; ; scanent++)
+    {
     if (!scanent->name)
       return malware_errlog_defer(string_sprintf("unknown scanner type '%s'",
        scanner_name));
@@ -530,9 +479,9 @@ if (!malware_ok)
       break;
     switch(scanent->conn)
     {
-    case MC_TCP:  sock = m_tcpsocket_fromdef(scanner_options, &errstr); break;
-    case MC_UNIX: sock = m_unixsocket(scanner_options, &errstr);         break;
-    case MC_STRM: sock = m_streamsocket(scanner_options, &errstr);       break;
+    case MC_TCP:  sock = ip_tcpsocket(scanner_options, &errstr, 5);      break;
+    case MC_UNIX: sock = ip_unixsocket(scanner_options, &errstr);        break;
+    case MC_STRM: sock = ip_streamsocket(scanner_options, &errstr, 5);  break;
     default: /* compiler quietening */ break;
     }
     if (sock < 0)
@@ -1374,7 +1323,7 @@ if (!malware_ok)
        }
       else
        {
-       if ((sock = m_unixsocket(scanner_options, &errstr)) < 0)
+       if ((sock = ip_unixsocket(scanner_options, &errstr)) < 0)
          return m_errlog_defer(scanent, errstr);
        }
 
@@ -1749,7 +1698,7 @@ if (!malware_ok)
            string_sprintf("invalid option '%s'", scanner_options));
        }
 
-      if((sock = m_unixsocket(US "/var/run/mksd/socket", &errstr)) < 0)
+      if((sock = ip_unixsocket(US "/var/run/mksd/socket", &errstr)) < 0)
        return m_errlog_defer(scanent, errstr);
 
       malware_name = NULL;