-
Notifications
You must be signed in to change notification settings - Fork 70
/
utils.c
269 lines (227 loc) · 5.95 KB
/
utils.c
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* <@LICENSE>
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </@LICENSE>
*/
#ifndef _WIN32
#include <unistd.h>
#include <sys/uio.h>
#include <sys/types.h>
#include <sys/socket.h>
#else
#ifdef _MSC_VER
/* ignore MSVC++ warnings that are annoying and hard to remove:
4115 named type definition in parentheses
4127 conditional expression is constant
4514 unreferenced inline function removed
*/
#pragma warning( disable : 4115 4127 4514 )
#endif
#include <io.h>
#endif
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include "utils.h"
/* Dec 13 2001 jm: added safe full-read and full-write functions. These
* can cope with networks etc., where a write or read may not read all
* the data that's there, in one call.
*/
/* Aug 14, 2002 bj: EINTR and EAGAIN aren't fatal, are they? */
/* Aug 14, 2002 bj: moved these to utils.c */
/* Jan 13, 2003 ym: added timeout functionality */
/* Apr 24, 2003 sjf: made full_read and full_write void* params */
/* -------------------------------------------------------------------------- */
#ifndef _WIN32
typedef void sigfunc(int); /* for signal handlers */
sigfunc *sig_catch(int sig, void (*f) (int))
{
struct sigaction act, oact;
act.sa_handler = f;
act.sa_flags = 0;
sigemptyset(&act.sa_mask);
sigaction(sig, &act, &oact);
return oact.sa_handler;
}
static void catch_alrm(int x)
{
UNUSED_VARIABLE(x);
}
#endif
int timeout_connect (int sockfd, const struct sockaddr *serv_addr, size_t addrlen)
{
int ret;
#ifndef _WIN32
sigfunc* sig;
sig = sig_catch(SIGALRM, catch_alrm);
if (libspamc_timeout > 0) {
alarm(libspamc_timeout);
}
#endif
ret = connect(sockfd, serv_addr, addrlen);
#ifndef _WIN32
if (libspamc_timeout > 0) {
alarm(0);
}
/* restore old signal handler */
sig_catch(SIGALRM, sig);
#endif
return ret;
}
int fd_timeout_read(int fd, char fdflag, void *buf, size_t nbytes)
{
int nred;
int origerr;
#ifndef _WIN32
sigfunc *sig;
sig = sig_catch(SIGALRM, catch_alrm);
if (libspamc_timeout > 0) {
alarm(libspamc_timeout);
}
#endif
do {
if (fdflag) {
nred = (int)read(fd, buf, nbytes);
origerr = errno;
}
else {
nred = (int)recv(fd, buf, nbytes, 0);
#ifndef _WIN32
origerr = errno;
#else
origerr = WSAGetLastError();
#endif
}
} while (nred < 0 && origerr == EWOULDBLOCK);
#ifndef _WIN32
if (nred < 0 && origerr == EINTR)
errno = ETIMEDOUT;
if (libspamc_timeout > 0) {
alarm(0);
}
/* restore old signal handler */
sig_catch(SIGALRM, sig);
#endif
return nred;
}
int ssl_timeout_read(SSL * ssl, void *buf, int nbytes)
{
int nred;
#ifndef _WIN32
sigfunc *sig;
sig = sig_catch(SIGALRM, catch_alrm);
if (libspamc_timeout > 0) {
alarm(libspamc_timeout);
}
#endif
do {
#ifdef SPAMC_SSL
nred = SSL_read(ssl, buf, nbytes);
#else
UNUSED_VARIABLE(ssl);
UNUSED_VARIABLE(buf);
UNUSED_VARIABLE(nbytes);
nred = 0; /* never used */
#endif
} while (nred < 0 && errno == EWOULDBLOCK);
#ifndef _WIN32
if (nred < 0 && errno == EINTR)
errno = ETIMEDOUT;
if (libspamc_timeout > 0) {
alarm(0);
}
/* restore old signal handler */
sig_catch(SIGALRM, sig);
#endif
return nred;
}
/* -------------------------------------------------------------------------- */
int full_read(int fd, char fdflag, void *vbuf, int min, int len)
{
unsigned char *buf = (unsigned char *) vbuf;
int total;
int thistime;
for (total = 0; total < min;) {
thistime = fd_timeout_read(fd, fdflag, buf + total, len - total);
if (thistime < 0) {
if (total >= min) {
/* error, but we read *some*. return what we've read
* so far and next read (if there is one) will return -1. */
return total;
} else {
return -1;
}
}
else if (thistime == 0) {
/* EOF, but we didn't read the minimum. return what we've read
* so far and next read (if there is one) will return 0. */
return total;
}
total += thistime;
}
return total;
}
int full_read_ssl(SSL * ssl, unsigned char *buf, int min, int len)
{
int total;
int thistime;
for (total = 0; total < min;) {
thistime = ssl_timeout_read(ssl, buf + total, len - total);
if (thistime < 0) {
if (total >= min) {
/* error, but we read *some*. return what we've read
* so far and next read (if there is one) will return -1. */
return total;
} else {
return -1;
}
}
else if (thistime == 0) {
/* EOF, but we didn't read the minimum. return what we've read
* so far and next read (if there is one) will return 0. */
return total;
}
total += thistime;
}
return total;
}
int full_write(int fd, char fdflag, const void *vbuf, int len)
{
const char *buf = (const char *) vbuf;
int total;
int thistime;
int origerr;
for (total = 0; total < len;) {
if (fdflag) {
thistime = write(fd, buf + total, len - total);
origerr = errno;
}
else {
thistime = send(fd, buf + total, len - total, 0);
#ifndef _WIN32
origerr = errno;
#else
origerr = WSAGetLastError();
#endif
}
if (thistime < 0) {
if (EINTR == origerr || EWOULDBLOCK == origerr)
continue;
return thistime; /* always an error for writes */
}
total += thistime;
}
return total;
}