ClamAV-Clamd

 view release on metacpan or  search on metacpan

include/clamav/clamd_compat.h  view on Meta::CPAN

#  define cc_sock_errno()   WSAGetLastError()
   /* Windows has AF_UNIX from Win10 1803, but it has no SCM_RIGHTS and
    * no descriptor passing at all - so FILDES can never work there. That
    * is a phase 2 decision; what matters here is that it is a platform
    * fact and not a build accident. */
#  define CC_HAVE_FD_PASSING 0
#else
#  include <sys/types.h>
#  include <sys/socket.h>
#  include <sys/un.h>
#  include <netinet/in.h>
#  include <netinet/tcp.h>
#  include <arpa/inet.h>
#  include <netdb.h>
#  include <unistd.h>
#  include <fcntl.h>
#  include <errno.h>
   typedef socklen_t cc_socklen_t;
#  define CC_INVALID_SOCK   (-1)
#  define cc_close_sock(s)  close(s)
#  define cc_sock_errno()   errno
   /* Descriptor passing is a fact about the headers in front of us, not
    * about the operating system. Solaris and illumos keep the 4.3BSD
    * msghdr - the one with msg_accrights and no msg_control - and hide
    * the CMSG_* macros until _XPG4_2 is defined, and perl.h has already
    * included <sys/socket.h> by the time this file is read, so the define
    * has to arrive on the command line. Makefile.PL probes for the
    * spelling that works there.
    *
    * Where the macros stay hidden, SCM_RIGHTS cannot be spoken at all:
    * FILDES compiles out and cc_scan_fd falls back to INSTREAM, which is
    * slower and correct. Testing the macros rather than the platform is
    * also what catches the case where the probe never ran.
    */
#  if defined(CC_NO_FD_PASSING) || !defined(SCM_RIGHTS) \
   || !defined(CMSG_SPACE) || !defined(CMSG_LEN) \
   || !defined(CMSG_FIRSTHDR) || !defined(CMSG_DATA)
#    define CC_HAVE_FD_PASSING 0
#  else
#    define CC_HAVE_FD_PASSING 1
#  endif
#endif

#include <string.h>
#include <stdlib.h>
#include <time.h>

#ifndef _WIN32
#  include <sys/time.h>
#  include <sys/stat.h>
#  include <poll.h>
#endif

/* The file side. The underscore names on Windows are deliberate: perl
 * renames the plain ones (read, open, close, fstat) there, and its fstat
 * wants perl's own stat struct. The descriptor is opened binary, or the
 * C runtime rewrites line endings and stops at ^Z - in a scanner, that
 * is a file clamd never saw the whole of. */
#ifdef _WIN32
   typedef struct _stat64 cc_stat_t;
#  define cc_fstat(fd, st)   _fstat64((fd), (st))
#  define cc_open_ro(path)   _open((path), _O_RDONLY | _O_BINARY)
#  define cc_read_fd(fd, b, n) _read((fd), (b), (unsigned int)(n))
#  define cc_close_fd(fd)    _close(fd)
#  define CC_S_ISREG(m)      (((m) & _S_IFMT) == _S_IFREG)
#else
   typedef struct stat cc_stat_t;
#  define cc_fstat(fd, st)   fstat((fd), (st))
#  define cc_open_ro(path)   open((path), O_RDONLY)
#  define cc_read_fd(fd, b, n) read((fd), (b), (n))
#  define cc_close_fd(fd)    close(fd)
#  define CC_S_ISREG(m)      S_ISREG(m)
#endif

/* Writing to a socket the peer has closed raises SIGPIPE, whose default
 * disposition kills the process. A library that takes down its caller
 * because clamd hung up is not usable inside a server, and clamd hangs
 * up routinely - it does exactly that when a stream exceeds
 * StreamMaxLength.
 *
 * There is no single portable answer, so both are used:
 *   MSG_NOSIGNAL   per-call, Linux and modern POSIX
 *   SO_NOSIGPIPE   per-socket, macOS and the BSDs
 * Neither exists everywhere; where neither does, every send still
 * reports EPIPE and the caller's own disposition governs.
 *
 * This is also why nothing below uses write() or writev(): they take no
 * flags. sendmsg() carries an iovec and takes flags, so it does both
 * jobs at once.
 */
#ifdef MSG_NOSIGNAL
#  define CC_MSG_NOSIGNAL MSG_NOSIGNAL
#else
#  define CC_MSG_NOSIGNAL 0
#endif

/* "The peer has gone" is spelled differently per platform, and getting
 * the set wrong turns a stream clamd deliberately closed into a generic
 * IO error - which loses the distinction between "no verdict" and
 * "something broke". macOS in particular answers ENOTCONN where Linux
 * answers EPIPE. */
static int cc_errno_is_closed(int e) {
#ifdef _WIN32
    return e == EPIPE;
#else
    if (e == EPIPE || e == ECONNRESET || e == ENOTCONN) return 1; /* # uncoverable condition left count:3 */
#  ifdef ESHUTDOWN
    /*
     # uncoverable branch true
     # uncoverable condition left count:1 */
    if (e == ESHUTDOWN) return 1;
#  endif
#  ifdef ECONNABORTED
    /*
     # uncoverable branch true
     # uncoverable condition left count:1 */
    if (e == ECONNABORTED) return 1;
#  endif
    return 0;
#endif
}

/* Winsock reports through WSAGetLastError and never touches errno, and
 * every caller below reads errno. So the socket calls go through these,
 * which translate once: would-block to EAGAIN, every spelling of "the
 * peer has gone" to EPIPE, the rest to EIO. Elsewhere they are the plain
 * calls. */
#ifdef _WIN32



( run in 0.930 second using v1.01-cache-2.11-cpan-eb134014566 )