177 |
*/ |
*/ |
178 |
int socket_read(char *buf) |
int socket_read(char *buf) |
179 |
{ |
{ |
180 |
int flags, e; |
int flags, r, s; |
181 |
fd_set fds; |
fd_set fds; |
182 |
struct timeval tv; |
struct timeval tv; |
183 |
|
|
184 |
memset(buf, 0, RESPONSE_BUF); |
memset(buf, 0, RESPONSE_BUF); |
185 |
|
|
186 |
tv.tv_sec = 20; |
tv.tv_sec = 30; |
187 |
tv.tv_usec = 0; |
tv.tv_usec = 0; |
188 |
|
|
189 |
flags = fcntl(sock, F_GETFL, 0); |
flags = fcntl(sock, F_GETFL, 0); |
192 |
FD_ZERO(&fds); |
FD_ZERO(&fds); |
193 |
FD_SET(sock, &fds); |
FD_SET(sock, &fds); |
194 |
|
|
|
if ((select(sock + 1, &fds, NULL, NULL, &tv) > 0) |
|
|
&& FD_ISSET(sock, &fds)) { |
|
|
|
|
195 |
#ifdef SSL_TLS |
#ifdef SSL_TLS |
196 |
if (ssl) |
if (ssl) { |
197 |
e = SSL_read(ssl, buf, RESPONSE_BUF - 1); |
if (SSL_pending(ssl) || ((s = select(sock + 1, &fds, NULL, NULL, &tv)) > 0 |
198 |
else |
&& FD_ISSET(sock, &fds))) |
199 |
|
r = SSL_read(ssl, buf, RESPONSE_BUF - 1); |
200 |
|
} else |
201 |
#endif |
#endif |
202 |
e = read(sock, buf, RESPONSE_BUF - 1); |
if ((s = select(sock + 1, &fds, NULL, NULL, &tv)) > 0 |
203 |
|
&& FD_ISSET(sock, &fds)) |
204 |
|
r = read(sock, buf, RESPONSE_BUF - 1); |
205 |
|
|
206 |
|
fcntl(sock, F_SETFL, flags); |
207 |
|
|
208 |
fcntl(sock, F_SETFL, flags); |
if (s <= 0) |
209 |
|
fatal(ERROR_NETWORK, "imapfilter: waiting input from socket; %s\n", |
210 |
|
strerror(errno)); |
211 |
|
|
212 |
#ifdef SSL_TLS |
#ifdef SSL_TLS |
213 |
if (ssl) { |
if (ssl) { |
214 |
if (e <= 0) |
if (r <= 0) |
|
fatal(ERROR_NETWORK, "imapfilter: reading data; %s", |
|
|
ERR_error_string(e, NULL)); |
|
|
} else |
|
|
#endif |
|
|
if (e == -1) |
|
215 |
fatal(ERROR_NETWORK, "imapfilter: reading data; %s", |
fatal(ERROR_NETWORK, "imapfilter: reading data; %s", |
216 |
strerror(errno)); |
ERR_error_string(r, NULL)); |
217 |
|
} else |
218 |
return 0; |
#endif |
219 |
} |
if (r == -1) |
220 |
fcntl(sock, F_SETFL, flags); |
fatal(ERROR_NETWORK, "imapfilter: reading data; %s", |
221 |
|
strerror(errno)); |
|
fatal(ERROR_NETWORK, "imapfilter: waiting input from socket; %s\n", |
|
|
strerror(errno)); |
|
222 |
|
|
223 |
return ERROR_NETWORK; /* NEVER REACHED. */ |
return 0; |
224 |
} |
} |
225 |
|
|
226 |
|
|