Refactor clamd socket connection
[users/jgh/exim.git] / src / src / acl.c
index 11626709d4e3c4755ee7a93ac4ceff444a5be124..f25debd93d1c2ac2ed78c677e1fe31723b7d8ba8 100644 (file)
@@ -2,7 +2,7 @@
 *     Exim - an Internet mail transport agent    *
 *************************************************/
 
-/* Copyright (c) University of Cambridge 1995 - 2012 */
+/* Copyright (c) University of Cambridge 1995 - 2013 */
 /* See the file NOTICE for conditions of use and distribution. */
 
 /* Code for handling Access Control Lists (ACLs) */
@@ -2846,32 +2846,32 @@ uschar *portstr;
 uschar *portend;
 host_item *h;
 int portnum;
-int host_af;
 int len;
 int r, s;
+uschar * errstr;
 
 hostname = string_nextinlist(&arg, &sep, NULL, 0);
 portstr = string_nextinlist(&arg, &sep, NULL, 0);
 
 if (hostname == NULL)
   {
-  *log_msgptr = "missing destination host in \"udpsend\" modifier";
+  *log_msgptr = US"missing destination host in \"udpsend\" modifier";
   return ERROR;
   }
 if (portstr == NULL)
   {
-  *log_msgptr = "missing destination port in \"udpsend\" modifier";
+  *log_msgptr = US"missing destination port in \"udpsend\" modifier";
   return ERROR;
   }
 if (arg == NULL)
   {
-  *log_msgptr = "missing datagram payload in \"udpsend\" modifier";
+  *log_msgptr = US"missing datagram payload in \"udpsend\" modifier";
   return ERROR;
   }
 portnum = Ustrtol(portstr, &portend, 10);
 if (*portend != '\0')
   {
-  *log_msgptr = "bad destination port in \"udpsend\" modifier";
+  *log_msgptr = US"bad destination port in \"udpsend\" modifier";
   return ERROR;
   }
 
@@ -2888,21 +2888,24 @@ else
   r = host_find_byname(h, NULL, 0, NULL, FALSE);
 if (r == HOST_FIND_FAILED || r == HOST_FIND_AGAIN)
   {
-  *log_msgptr = "DNS lookup failed in \"udpsend\" modifier";
+  *log_msgptr = US"DNS lookup failed in \"udpsend\" modifier";
   return DEFER;
   }
 
 HDEBUG(D_acl)
   debug_printf("udpsend [%s]:%d %s\n", h->address, portnum, arg);
 
-host_af = (Ustrchr(h->address, ':') == NULL)? AF_INET:AF_INET6;
-r = s = ip_socket(SOCK_DGRAM, host_af);
-if (r < 0) goto defer;
-r = ip_connect(s, host_af, h->address, portnum, 1);
-if (r < 0) goto defer;
-len = strlen(arg);
-r = send(s, arg, len, MSG_NOSIGNAL);
+r = s = ip_connectedsocket(SOCK_DGRAM, h->address, portnum, 1, NULL, &errstr);
 if (r < 0) goto defer;
+len = Ustrlen(arg);
+r = send(s, arg, len, 0);
+if (r < 0)
+  {
+  errstr = strerror(errno);
+  close(s);
+  goto defer;
+  }
+close(s);
 if (r < len)
   {
   *log_msgptr =
@@ -2916,7 +2919,7 @@ HDEBUG(D_acl)
 return OK;
 
 defer:
-*log_msgptr = string_sprintf("\"udpsend\" failed: %s", strerror(errno));
+*log_msgptr = string_sprintf("\"udpsend\" failed: %s", errstr);
 return DEFER;
 }