7 /* sanitize accept() error returns
9 /* #include <sane_accept.h>
11 /* int sane_accept(sock, buf, len)
13 /* struct sockaddr *buf;
14 /* SOCKADDR_SIZE *len;
16 /* sane_accept() implements the accept(2) socket call, and maps
17 /* known harmless error results to EAGAIN.
19 /* If the buf and len arguments are not null, then additional
20 /* workarounds may be enabled that depend on the socket type.
22 /* Bizarre systems may have other harmless error results. Such
23 /* systems encourage programmers to ignore error results, and
24 /* penalize programmers who code defensively.
28 /* The Secure Mailer license must be distributed with this software.
31 /* IBM T.J. Watson Research
33 /* Yorktown Heights, NY 10598, USA
39 #include <sys/socket.h>
42 /* Utility library. */
45 #include "sane_accept.h"
47 /* sane_accept - sanitize accept() error returns */
49 int sane_accept(int sock
, struct sockaddr
* sa
, SOCKADDR_SIZE
*len
)
51 static int accept_ok_errors
[] = {
65 EPROTO
, /* SunOS 5.5.1 */
74 * XXX Solaris 2.4 accept() returns EPIPE when a UNIX-domain client has
75 * disconnected in the mean time. From then on, UNIX-domain sockets are
76 * hosed beyond recovery. There is no point treating this as a beneficial
77 * error result because the program would go into a tight loop.
79 * XXX Solaris 2.5.1 accept() returns EPROTO when a TCP client has
80 * disconnected in the mean time. Since there is no connection, it is
81 * safe to map the error code onto EAGAIN.
83 * XXX LINUX < 2.1 accept() wakes up before the three-way handshake is
84 * complete, so it can fail with ECONNRESET and other "false alarm"
87 * XXX FreeBSD 4.2-STABLE accept() returns ECONNABORTED when a UNIX-domain
88 * client has disconnected in the mean time. The data that was sent with
89 * connect() write() close() is lost, even though the write() and close()
90 * reported successful completion. This was fixed shortly before FreeBSD
93 * XXX HP-UX 11 returns ENOBUFS when the client has disconnected in the mean
96 if ((fd
= accept(sock
, sa
, len
)) < 0) {
97 for (count
= 0; (err
= accept_ok_errors
[count
]) != 0; count
++) {
106 * XXX Solaris select() produces false read events, so that read() blocks
107 * forever on a blocking socket, and fails with EAGAIN on a non-blocking
108 * socket. Turning on keepalives will fix a blocking socket provided that
109 * the kernel's keepalive timer expires before the Postfix watchdog
112 * XXX Work around NAT induced damage by sending a keepalive before an idle
113 * connection is expired. This requires that the kernel keepalive timer
114 * is set to a short time, like 100s.
116 else if (sa
&& (sa
->sa_family
== AF_INET
118 || sa
->sa_family
== AF_INET6
123 (void) setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
,
124 (char *) &on
, sizeof(on
));