-
Notifications
You must be signed in to change notification settings - Fork 1
/
connection.c
291 lines (231 loc) · 5.33 KB
/
connection.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**
* Networking stuff
*
* connection_do_io() is called by proto_process() and contains the
* I/O loop which calls into a bunch of other proto_*() routines.
*
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/time.h>
#include <fcntl.h>
#include <errno.h>
#include <syslog.h>
#include "addr.h"
#include "buf.h"
#include "connection.h"
#include "proto.h"
static connection_t **root;
static int num_connections;
int connection_num_connections(void) {
return num_connections;
}
void *connection_priv(connection_t *c) {
return c->priv;
}
void connection_set_callbacks(connection_t *c, connection_callback_t proto_start, connection_callback_t proto_step, connection_callback_t proto_finish, void *priv) {
c->proto_start = proto_start;
c->proto_step = proto_step;
c->proto_finish = proto_finish;
c->priv = priv;
}
connection_t *connection_open(struct addrinfo *ai, char *hostname) {
connection_t *c;
int fd, flags, saved_errno;
fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if(fd < 0) {
return NULL;
}
flags = fcntl(fd, F_GETFL);
flags |= O_NONBLOCK;
if(fcntl(fd, F_SETFL, flags) < 0) {
saved_errno = errno;
close(fd);
errno = saved_errno;
return NULL;
}
if(connect(fd, ai->ai_addr, ai->ai_addrlen) && errno != EINPROGRESS) {
saved_errno = errno;
syslog(LOG_NOTICE, "connect() to %s (%s) failed: %s",
hostname, addr_ai2ip(ai), strerror(errno));
close(fd);
errno = saved_errno;
return NULL;
}
root = (connection_t **)realloc(root,
sizeof(connection_t *) * (num_connections + 1));
if(root == NULL) {
close(fd);
return NULL;
}
if((c = calloc(1, sizeof(*c))) == NULL) {
close(fd);
return NULL;
}
c->buf = buf_alloc(32 * 1024);
if(c->buf == NULL) {
close(fd);
free(c);
return NULL;
}
c->fd = fd;
c->is_connecting = 1;
c->is_pending_removal = 0;
gettimeofday(c->tv, NULL);
c->ai = ai;
c->hostname = hostname;
root[num_connections++] = c;
return c;
}
static void connection_release(connection_t *c) {
int i;
buf_free(c->buf);
close(c->fd);
free(c);
for(i = 0; i < num_connections; i++) {
if(root[i] != c) continue;
num_connections--;
root[i] = root[num_connections];
break;
}
}
void connection_set_expected_bytes(connection_t *c, size_t num) {
c->bytes_expected = num;
}
void connection_finish(connection_t *c) {
c->is_pending_removal = 1;
}
int connection_write(connection_t *c, void *data, size_t len) {
unsigned char *ptr = (unsigned char *)data;
ssize_t n;
while(len > 0) {
n = send(c->fd, ptr, len, 0);
if(n > 0) {
ptr += n;
len -= n;
continue;
}
if(errno == EINTR || errno == EAGAIN)
continue;
c->error = errno;
syslog(LOG_INFO, "%s -- send() failed: %s",
proto_ver(c), strerror(c->error));
return -1;
}
return 0;
}
static int connection_read(connection_t *c) {
unsigned char *p;
size_t len;
ssize_t n;
len = c->bytes_expected;
if(len == 0) {
len = buf_avail(c->buf);
}
if(len == 0) {
/* Shouldn't happen */
return 0;
}
p = buf_ptr(c->buf);
n = recv(c->fd, p, len, 0);
if(n < 0) {
if(errno == EINTR || errno == EAGAIN)
return 0;
c->error = errno;
return -1;
}
else if(n == 0) {
/* Remote peer shutdown its side */
c->error = ECONNRESET;
return -1;
}
buf_append(c->buf, p, n); /* only to update internal length */
if(c->bytes_expected)
c->bytes_expected -= n;
return 0;
}
int connection_do_io(void) {
fd_set rfds, wfds;
int i, n, ret, elapsed_ms;
struct timeval tv[1];
connection_t *c;
FD_ZERO(&rfds);
FD_ZERO(&wfds);
for(i = n = 0; i < num_connections; i++) {
c = root[i];
if(c->is_connecting)
FD_SET(c->fd, &wfds);
else
FD_SET(c->fd, &rfds);
if(c->fd > n)
n = c->fd;
}
tv->tv_sec = 1;
tv->tv_usec = 0;
ret = select(n + 1, &rfds, &wfds, NULL, tv);
if(ret < 0) {
syslog(LOG_ERR, "select() failed with error: %s", strerror(errno));
return -1;
}
gettimeofday(tv, NULL);
for(i = 0; i < num_connections; i++) {
c = root[i];
elapsed_ms = (tv->tv_sec - c->tv->tv_sec) * 1000;
elapsed_ms += (tv->tv_usec - c->tv->tv_usec) / 1000;
if(FD_ISSET(c->fd, &wfds)) {
socklen_t optlen = sizeof(c->error);
c->is_connecting = 0;
getsockopt(c->fd, SOL_SOCKET, SO_ERROR, &c->error, &optlen);
if(c->error != 0) {
connection_finish(c);
continue;
}
if(elapsed_ms > 10000)
syslog(LOG_INFO, "%s -- Connection succeeded "
"after %dms", proto_ver(c), elapsed_ms);
if(c->proto_start(c) < 0)
connection_finish(c);
continue;
}
else if(c->is_connecting && elapsed_ms >= 35000) {
c->error = ETIMEDOUT;
connection_finish(c);
continue;
}
if(!FD_ISSET(c->fd, &rfds)) {
if(elapsed_ms > 35000) {
syslog(LOG_INFO, "%s -- Read timed out "
"after %dms", proto_ver(c), elapsed_ms);
c->error = ETIMEDOUT;
connection_finish(c);
}
continue;
}
if(connection_read(c) < 0) {
syslog(LOG_DEBUG, "%s -- recv() failed with error: %s",
proto_ver(c), strerror(c->error));
connection_finish(c);
continue;
}
if(c->bytes_expected > 0) {
/* need more data */
continue;
}
if(c->proto_step(c) < 0) {
connection_finish(c);
continue;
}
}
/* finialize connections in pending removal state */
for(i = 0; i < num_connections; i++) {
c = root[i];
if(c->is_pending_removal == 0)
continue;
c->proto_finish(c);
connection_release(c);
i--;
}
return 0;
}