aboutsummaryrefslogtreecommitdiffstats
path: root/socket.c
diff options
context:
space:
mode:
authorMatthias Andree <matthias.andree@gmx.de>2011-04-29 15:08:49 +0200
committerMatthias Andree <matthias.andree@gmx.de>2011-04-29 16:54:13 +0200
commit72ce8bce8dd655b6aefa33d0a74e883dad5202b5 (patch)
tree711c204306cdcd69ee14d7f29fbfdf597a19b96f /socket.c
parentab7a12a1c5b074b0ccfeb7f4a43ed71d5bb36175 (diff)
downloadfetchmail-72ce8bce8dd655b6aefa33d0a74e883dad5202b5.tar.gz
fetchmail-72ce8bce8dd655b6aefa33d0a74e883dad5202b5.tar.bz2
fetchmail-72ce8bce8dd655b6aefa33d0a74e883dad5202b5.zip
Add a SockTimeout(int socket, int seconds) function.
Uses setsockopt(..., SOL_SOCKET, SO_{SND|RCV}TIMEO, ...).
Diffstat (limited to 'socket.c')
-rw-r--r--socket.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/socket.c b/socket.c
index d48a6369..930e1b0d 100644
--- a/socket.c
+++ b/socket.c
@@ -200,6 +200,31 @@ static int handle_plugin(const char *host,
}
#endif /* HAVE_SOCKETPAIR */
+static int setsocktimeout(int sock, int which, int timeout) {
+ struct timeval tv;
+ int rc;
+
+ tv.tv_sec = timeout;
+ tv.tv_usec = 0;
+ rc = setsockopt(sock, SOL_SOCKET, which, &tv, sizeof(tv));
+ if (rc) {
+ report(stderr, GT_("setsockopt(%d, SOL_SOCKET) failed: %s"), sock, strerror(errno));
+ }
+ return rc;
+}
+
+/** Configure socket options such as send/receive timeout at the socket
+ * level, to avoid network-induced stalls.
+ */
+int SockTimeout(int sock, int timeout)
+{
+ int err = 0;
+
+ if (setsocktimeout(sock, SO_RCVTIMEO, timeout)) err = 1;
+ if (setsocktimeout(sock, SO_SNDTIMEO, timeout)) err = 1;
+ return err;
+}
+
int UnixOpen(const char *path)
{
int sock = -1;