From 3e60dd410992e3b732d5633e25d3cbf76d8b1e2d Mon Sep 17 00:00:00 2001 From: Jeremy Harris Date: Tue, 9 Dec 2014 10:42:38 +0000 Subject: [PATCH] Make useful socket functions more generally available --- src/src/functions.h | 4 +++ src/src/ip.c | 52 ++++++++++++++++++++++++++++++++++++ src/src/malware.c | 65 +++++---------------------------------------- 3 files changed, 63 insertions(+), 58 deletions(-) diff --git a/src/src/functions.h b/src/src/functions.h index 9d0ca190c..3d8c2b994 100644 --- a/src/src/functions.h +++ b/src/src/functions.h @@ -216,6 +216,10 @@ extern void ip_keepalive(int, uschar *, BOOL); extern int ip_recv(int, uschar *, int, int); extern int ip_socket(int, int); +extern int ip_tcpsocket(const uschar *, uschar **, int); +extern int ip_unixsocket(const uschar *, uschar **); +extern int ip_streamsocket(const uschar *, uschar **, int); + extern uschar *local_part_quote(uschar *); extern int log_create(uschar *); extern int log_create_as_exim(uschar *); diff --git a/src/src/ip.c b/src/src/ip.c index 91b74e20e..e5945090a 100644 --- a/src/src/ip.c +++ b/src/src/ip.c @@ -354,6 +354,58 @@ bad: } +int +ip_tcpsocket(const uschar * hostport, uschar ** errstr, int tmo) +{ + 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, + tmo, NULL, errstr); +} + +int +ip_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; +} + +int +ip_streamsocket(const uschar * spec, uschar ** errstr, int tmo) +{ + return *spec == '/' + ? ip_unixsocket(spec, errstr) : ip_tcpsocket(spec, errstr, tmo); +} + /************************************************* * Set keepalive on a socket * *************************************************/ diff --git a/src/src/malware.c b/src/src/malware.c index 43f031494..2a1e21654 100644 --- a/src/src/malware.c +++ b/src/src/malware.c @@ -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; -- 2.30.2