summaryrefslogtreecommitdiff
path: root/libraries/libvmime-zarafa/zarafa-patches/vmime-socket-backport-and-timeout-fix.diff
blob: f9a1faea07536f28b101bb175668571837ceefa6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
diff -urb libvmime-0.7.1/src/platforms/posix/posixSocket.cpp libvmime-0.7.1.patched/src/platforms/posix/posixSocket.cpp
--- libvmime-0.7.1/src/platforms/posix/posixSocket.cpp	2007-10-18 11:19:47.000000000 +0200
+++ libvmime-0.7.1.patched/src/platforms/posix/posixSocket.cpp	2007-10-18 11:02:54.000000000 +0200
@@ -26,6 +26,7 @@
 #include <netinet/in.h>
 #include <netdb.h>
 #include <fcntl.h>
+#include <errno.h>
 
 #include "vmime/exception.hpp"
 
@@ -43,7 +44,7 @@
 //
 
 posixSocket::posixSocket()
-	: m_desc(-1)
+	: m_desc(-1), m_timeouts(0)
 {
 }
 
@@ -101,6 +102,8 @@
 		// Error
 		throw vmime::exceptions::connection_error("Error while connecting socket.");
 	}
+
+	m_timeouts = 0;
 }
 
 
@@ -121,38 +124,62 @@
 	}
 }
 
-
 void posixSocket::receive(vmime::string& buffer)
 {
-	::ssize_t ret = ::recv(m_desc, m_buffer, sizeof(m_buffer), 0);
-
-	if (ret == -1)
-	{
-		// Error or no data
-		return;
-	}
-	else if (ret > 0)
-	{
-		buffer = vmime::string(m_buffer, ret);
-	}
+	const int size = receiveRaw(m_buffer, sizeof(m_buffer));
+	buffer = vmime::string(m_buffer, size);
 }
 
 
 const int posixSocket::receiveRaw(char* buffer, const int count)
 {
-	::ssize_t ret = ::recv(m_desc, buffer, count, 0);
+	fd_set fds;
+	struct timeval tv;
+	int ret;
+
+	FD_ZERO(&fds);
+	FD_SET(m_desc, &fds);
+
+	tv.tv_sec = 10;
+	tv.tv_usec = 0;
+
+	ret = ::select(m_desc+1, &fds, NULL, NULL, &tv);
+	if (ret < 0)
+	{
+		if (errno != EAGAIN)
+			throwSocketError(errno);
+
+		m_timeouts++;
+		if (m_timeouts > (5*60))
+			throwSocketError(errno); // SMTP server did not react within 5 minutes
+
+		// No data available at this time
+		return 0;
+	}
 
-	if (ret == -1)
+	ret = ::recv(m_desc, buffer, count, 0);
+
+	if (ret < 0)
 	{
-		// Error or no data
-		return (0);
+		if (errno != EAGAIN)
+			throwSocketError(errno);
+
+		m_timeouts++;
+		if (m_timeouts > (5*60))
+			throwSocketError(errno); // SMTP server did not react within 5 minutes
+
+		// No data available at this time
+		return 0;
 	}
-	else
+	else if (ret == 0)
 	{
-		return (ret);
+		// Host shutdown
+		throwSocketError(ENOTCONN);
 	}
-}
 
+	m_timeouts = 0;
+	return ret;
+}
 
 void posixSocket::send(const vmime::string& buffer)
 {
@@ -166,6 +193,41 @@
 }
 
 
+void posixSocket::throwSocketError(const int err)
+{
+	string msg;
+
+	switch (err)
+	{
+	case EACCES:          msg = "EACCES: permission denied"; break;
+	case EAFNOSUPPORT:    msg = "EAFNOSUPPORT: address family not supported"; break;
+	case EMFILE:          msg = "EMFILE: process file table overflow"; break;
+	case ENFILE:          msg = "ENFILE: system limit reached"; break;
+	case EPROTONOSUPPORT: msg = "EPROTONOSUPPORT: protocol not supported"; break;
+	case EAGAIN:          msg = "EAGAIN: blocking operation"; break;
+	case EBADF:           msg = "EBADF: invalid descriptor"; break;
+	case ECONNRESET:      msg = "ECONNRESET: connection reset by peer"; break;
+	case EFAULT:          msg = "EFAULT: bad user space address"; break;
+	case EINTR:           msg = "EINTR: signal occured before transmission"; break;
+	case EINVAL:          msg = "EINVAL: invalid argument"; break;
+	case EMSGSIZE:        msg = "EMSGSIZE: message cannot be sent atomically"; break;
+	case ENOBUFS:         msg = "ENOBUFS: output queue is full"; break;
+	case ENOMEM:          msg = "ENOMEM: out of memory"; break;
+	case EPIPE:
+	case ENOTCONN:        msg = "ENOTCONN: not connected"; break;
+	case ECONNREFUSED:    msg = "ECONNREFUSED: connection refused"; break;
+	default:
+
+		std::ostringstream oss;
+		oss << ::strerror(err);
+
+		msg = oss.str();
+		break;
+	}
+
+	throw vmime::exceptions::connection_error(msg);
+}
+
 
 
 //
diff -urb libvmime-0.7.1/vmime/platforms/posix/posixSocket.hpp libvmime-0.7.1.patched/vmime/platforms/posix/posixSocket.hpp
--- libvmime-0.7.1/vmime/platforms/posix/posixSocket.hpp	2007-10-18 11:19:47.000000000 +0200
+++ libvmime-0.7.1.patched/vmime/platforms/posix/posixSocket.hpp	2007-10-18 10:34:46.000000000 +0200
@@ -49,10 +49,13 @@
 	void send(const vmime::string& buffer);
 	void sendRaw(const char* buffer, const int count);
 
+	void throwSocketError(const int err);
+
 private:
 
 	char m_buffer[65536];
 	int m_desc;
+	int m_timeouts;
 };