-
Notifications
You must be signed in to change notification settings - Fork 271
/
connection.c
525 lines (454 loc) · 13.5 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "ioloop.h"
#include "istream.h"
#include "istream-unix.h"
#include "ostream.h"
#include "ostream-unix.h"
#include "iostream.h"
#include "net.h"
#include "strescape.h"
#include "llist.h"
#include "time-util.h"
#include "connection.h"
#include <unistd.h>
static void connection_idle_timeout(struct connection *conn)
{
conn->disconnect_reason = CONNECTION_DISCONNECT_IDLE_TIMEOUT;
conn->list->v.destroy(conn);
}
static void connection_connect_timeout(struct connection *conn)
{
conn->disconnect_reason = CONNECTION_DISCONNECT_CONNECT_TIMEOUT;
conn->list->v.destroy(conn);
}
void connection_input_default(struct connection *conn)
{
const char *line;
struct istream *input;
struct ostream *output;
int ret = 0;
switch (connection_input_read(conn)) {
case -1:
return;
case 0: /* allow calling this function for buffered input */
case 1:
break;
default:
i_unreached();
}
input = conn->input;
output = conn->output;
i_stream_ref(input);
if (output != NULL) {
o_stream_ref(output);
o_stream_cork(output);
}
while (!input->closed && (line = i_stream_next_line(input)) != NULL) {
T_BEGIN {
ret = conn->list->v.input_line(conn, line);
} T_END;
if (ret <= 0)
break;
}
if (output != NULL) {
o_stream_uncork(output);
o_stream_unref(&output);
}
if (ret < 0 && !input->closed) {
conn->disconnect_reason = CONNECTION_DISCONNECT_DEINIT;
conn->list->v.destroy(conn);
}
i_stream_unref(&input);
}
int connection_verify_version(struct connection *conn,
const char *const *args)
{
unsigned int recv_major_version;
/* VERSION <tab> service_name <tab> major version <tab> minor version */
if (str_array_length(args) != 4 ||
strcmp(args[0], "VERSION") != 0 ||
str_to_uint(args[2], &recv_major_version) < 0 ||
str_to_uint(args[3], &conn->minor_version) < 0) {
i_error("%s didn't reply with a valid VERSION line: %s",
conn->name, t_strarray_join(args, "\t"));
return -1;
}
if (strcmp(args[1], conn->list->set.service_name_in) != 0) {
i_error("%s: Connected to wrong socket type. "
"We want '%s', but received '%s'", conn->name,
conn->list->set.service_name_in, args[1]);
return -1;
}
if (recv_major_version != conn->list->set.major_version) {
i_error("%s: Socket supports major version %u, "
"but we support only %u (mixed old and new binaries?)",
conn->name, recv_major_version,
conn->list->set.major_version);
return -1;
}
return 0;
}
int connection_input_line_default(struct connection *conn, const char *line)
{
const char *const *args;
args = t_strsplit_tabescaped(line);
if (!conn->version_received) {
if (connection_verify_version(conn, args) < 0)
return -1;
conn->version_received = TRUE;
return 1;
}
if (args[0] == NULL && !conn->list->set.allow_empty_args_input) {
i_error("%s: Unexpectedly received empty line", conn->name);
return -1;
}
return conn->list->v.input_args(conn, args);
}
void connection_input_halt(struct connection *conn)
{
io_remove(&conn->io);
}
void connection_input_resume(struct connection *conn)
{
const struct connection_settings *set = &conn->list->set;
if (conn->io != NULL)
return;
if (conn->from_streams || set->input_max_size != 0) {
conn->io = io_add_istream_to(conn->ioloop, conn->input,
*conn->list->v.input, conn);
} else {
conn->io = io_add_to(conn->ioloop, conn->fd_in, IO_READ,
*conn->list->v.input, conn);
}
}
static void connection_init_streams(struct connection *conn)
{
const struct connection_settings *set = &conn->list->set;
i_assert(conn->io == NULL);
i_assert(conn->input == NULL);
i_assert(conn->output == NULL);
i_assert(conn->to == NULL);
conn->version_received = set->major_version == 0;
if (set->input_max_size != 0) {
if (conn->unix_socket)
conn->input = i_stream_create_unix(conn->fd_in,
set->input_max_size);
else
conn->input = i_stream_create_fd(conn->fd_in,
set->input_max_size);
i_stream_set_name(conn->input, conn->name);
i_stream_switch_ioloop_to(conn->input, conn->ioloop);
}
if (set->output_max_size != 0) {
if (conn->unix_socket)
conn->output = o_stream_create_unix(conn->fd_out,
set->output_max_size);
else
conn->output = o_stream_create_fd(conn->fd_out,
set->output_max_size);
o_stream_set_no_error_handling(conn->output, TRUE);
o_stream_set_finish_via_child(conn->output, FALSE);
o_stream_set_name(conn->output, conn->name);
o_stream_switch_ioloop_to(conn->output, conn->ioloop);
}
connection_input_resume(conn);
if (set->input_idle_timeout_secs != 0) {
conn->to = timeout_add_to(conn->ioloop,
set->input_idle_timeout_secs*1000,
connection_idle_timeout, conn);
}
if (set->major_version != 0 && !set->dont_send_version) {
o_stream_nsend_str(conn->output, t_strdup_printf(
"VERSION\t%s\t%u\t%u\n", set->service_name_out,
set->major_version, set->minor_version));
}
}
void connection_streams_changed(struct connection *conn)
{
const struct connection_settings *set = &conn->list->set;
if (set->input_max_size != 0 && conn->io != NULL) {
connection_input_halt(conn);
connection_input_resume(conn);
}
}
static void connection_client_connected(struct connection *conn, bool success)
{
i_assert(conn->list->set.client);
conn->connect_finished = ioloop_timeval;
if (success)
connection_init_streams(conn);
if (conn->list->v.client_connected != NULL)
conn->list->v.client_connected(conn, success);
if (!success) {
conn->disconnect_reason =
CONNECTION_DISCONNECT_CONN_CLOSED;
conn->list->v.destroy(conn);
}
}
void connection_init(struct connection_list *list,
struct connection *conn)
{
conn->ioloop = current_ioloop;
conn->fd_in = -1;
conn->fd_out = -1;
conn->name = NULL;
if (conn->list != NULL) {
i_assert(conn->list == list);
} else {
conn->list = list;
DLLIST_PREPEND(&list->connections, conn);
list->connections_count++;
}
}
void connection_init_server(struct connection_list *list,
struct connection *conn, const char *name,
int fd_in, int fd_out)
{
i_assert(name != NULL);
i_assert(!list->set.client);
connection_init(list, conn);
conn->name = i_strdup(name);
conn->fd_in = fd_in;
conn->fd_out = fd_out;
connection_init_streams(conn);
}
void connection_init_client_ip_from(struct connection_list *list,
struct connection *conn,
const struct ip_addr *ip, in_port_t port,
const struct ip_addr *my_ip)
{
i_assert(list->set.client);
connection_init(list, conn);
conn->fd_in = conn->fd_out = -1;
conn->name = i_strdup_printf("%s:%u", net_ip2addr(ip), port);
conn->ip = *ip;
conn->port = port;
if (my_ip != NULL)
conn->my_ip = *my_ip;
else
i_zero(&conn->my_ip);
}
void connection_init_client_ip(struct connection_list *list,
struct connection *conn,
const struct ip_addr *ip, in_port_t port)
{
connection_init_client_ip_from(list, conn, ip, port, NULL);
}
void connection_init_client_unix(struct connection_list *list,
struct connection *conn, const char *path)
{
i_assert(list->set.client);
connection_init(list, conn);
conn->fd_in = conn->fd_out = -1;
conn->name = i_strdup(path);
conn->unix_socket = TRUE;
}
void connection_init_from_streams(struct connection_list *list,
struct connection *conn, const char *name,
struct istream *input, struct ostream *output)
{
i_assert(name != NULL);
connection_init(list, conn);
conn->name = i_strdup(name);
conn->from_streams = TRUE;
conn->fd_in = i_stream_get_fd(input);
conn->fd_out = o_stream_get_fd(output);
i_assert(conn->fd_in >= 0);
i_assert(conn->fd_out >= 0);
i_assert(conn->io == NULL);
i_assert(conn->input == NULL);
i_assert(conn->output == NULL);
i_assert(conn->to == NULL);
conn->input = input;
i_stream_ref(conn->input);
i_stream_set_name(conn->input, conn->name);
conn->output = output;
o_stream_ref(conn->output);
o_stream_set_no_error_handling(conn->output, TRUE);
o_stream_set_name(conn->output, conn->name);
connection_input_resume(conn);
if (list->v.client_connected != NULL)
list->v.client_connected(conn, TRUE);
}
static void connection_socket_connected(struct connection *conn)
{
io_remove(&conn->io);
timeout_remove(&conn->to);
errno = net_geterror(conn->fd_in);
connection_client_connected(conn, errno == 0);
}
int connection_client_connect(struct connection *conn)
{
const struct connection_settings *set = &conn->list->set;
int fd;
i_assert(conn->list->set.client);
i_assert(conn->fd_in == -1);
if (conn->port != 0) {
fd = net_connect_ip(&conn->ip, conn->port,
(conn->my_ip.family != 0 ? &conn->my_ip : NULL));
} else if (conn->list->set.unix_client_connect_msecs == 0)
fd = net_connect_unix(conn->name);
else
fd = net_connect_unix_with_retries(conn->name, conn->list->set.unix_client_connect_msecs);
if (fd == -1)
return -1;
conn->fd_in = conn->fd_out = fd;
conn->connect_started = ioloop_timeval;
if (conn->port != 0 ||
conn->list->set.delayed_unix_client_connected_callback) {
conn->io = io_add_to(conn->ioloop, conn->fd_out, IO_WRITE,
connection_socket_connected, conn);
if (set->client_connect_timeout_msecs != 0) {
conn->to = timeout_add_to(conn->ioloop,
set->client_connect_timeout_msecs,
connection_connect_timeout, conn);
}
} else {
connection_client_connected(conn, TRUE);
}
return 0;
}
void connection_disconnect(struct connection *conn)
{
conn->last_input = 0;
i_zero(&conn->last_input_tv);
timeout_remove(&conn->to);
io_remove(&conn->io);
i_stream_close(conn->input);
i_stream_destroy(&conn->input);
o_stream_close(conn->output);
o_stream_destroy(&conn->output);
fd_close_maybe_stdio(&conn->fd_in, &conn->fd_out);
}
void connection_deinit(struct connection *conn)
{
i_assert(conn->list->connections_count > 0);
conn->list->connections_count--;
DLLIST_REMOVE(&conn->list->connections, conn);
connection_disconnect(conn);
i_free(conn->name);
}
int connection_input_read(struct connection *conn)
{
conn->last_input = ioloop_time;
conn->last_input_tv = ioloop_timeval;
if (conn->to != NULL)
timeout_reset(conn->to);
switch (i_stream_read(conn->input)) {
case -2:
/* buffer full */
switch (conn->list->set.input_full_behavior) {
case CONNECTION_BEHAVIOR_DESTROY:
conn->disconnect_reason =
CONNECTION_DISCONNECT_BUFFER_FULL;
conn->list->v.destroy(conn);
return -1;
case CONNECTION_BEHAVIOR_ALLOW:
return -2;
}
i_unreached();
case -1:
/* disconnected */
conn->disconnect_reason =
CONNECTION_DISCONNECT_CONN_CLOSED;
conn->list->v.destroy(conn);
return -1;
case 0:
/* nothing new read */
return 0;
default:
/* something was read */
return 1;
}
}
const char *connection_disconnect_reason(struct connection *conn)
{
switch (conn->disconnect_reason) {
case CONNECTION_DISCONNECT_DEINIT:
return "Deinitializing";
case CONNECTION_DISCONNECT_CONNECT_TIMEOUT: {
unsigned int msecs =
conn->list->set.client_connect_timeout_msecs;
return t_strdup_printf("connect() timed out in %u.%03u secs",
msecs/1000, msecs%1000);
}
case CONNECTION_DISCONNECT_IDLE_TIMEOUT:
return "Idle timeout";
case CONNECTION_DISCONNECT_CONN_CLOSED:
if (conn->input == NULL)
return t_strdup_printf("connect() failed: %m");
/* fall through */
case CONNECTION_DISCONNECT_NOT:
case CONNECTION_DISCONNECT_BUFFER_FULL:
return io_stream_get_disconnect_reason(conn->input, conn->output);
}
i_unreached();
}
const char *connection_input_timeout_reason(struct connection *conn)
{
if (conn->last_input_tv.tv_sec != 0) {
int diff = timeval_diff_msecs(&ioloop_timeval, &conn->last_input_tv);
return t_strdup_printf("No input for %u.%03u secs",
diff/1000, diff%1000);
} else if (conn->connect_finished.tv_sec != 0) {
int diff = timeval_diff_msecs(&ioloop_timeval, &conn->connect_finished);
return t_strdup_printf(
"No input since connected %u.%03u secs ago",
diff/1000, diff%1000);
} else {
int diff = timeval_diff_msecs(&ioloop_timeval, &conn->connect_started);
return t_strdup_printf("connect() timed out after %u.%03u secs",
diff/1000, diff%1000);
}
}
void connection_switch_ioloop_to(struct connection *conn,
struct ioloop *ioloop)
{
conn->ioloop = ioloop;
if (conn->io != NULL)
conn->io = io_loop_move_io_to(ioloop, &conn->io);
if (conn->to != NULL)
conn->to = io_loop_move_timeout_to(ioloop, &conn->to);
if (conn->input != NULL)
i_stream_switch_ioloop_to(conn->input, ioloop);
if (conn->output != NULL)
o_stream_switch_ioloop_to(conn->output, ioloop);
}
void connection_switch_ioloop(struct connection *conn)
{
connection_switch_ioloop_to(conn, current_ioloop);
}
struct connection_list *
connection_list_init(const struct connection_settings *set,
const struct connection_vfuncs *vfuncs)
{
struct connection_list *list;
i_assert(vfuncs->input != NULL ||
set->input_full_behavior != CONNECTION_BEHAVIOR_ALLOW);
i_assert(set->major_version == 0 ||
(set->service_name_in != NULL &&
set->service_name_out != NULL &&
set->output_max_size != 0));
list = i_new(struct connection_list, 1);
list->set = *set;
list->v = *vfuncs;
if (list->v.input == NULL)
list->v.input = connection_input_default;
if (list->v.input_line == NULL)
list->v.input_line = connection_input_line_default;
return list;
}
void connection_list_deinit(struct connection_list **_list)
{
struct connection_list *list = *_list;
struct connection *conn;
*_list = NULL;
while (list->connections != NULL) {
conn = list->connections;
conn->disconnect_reason = CONNECTION_DISCONNECT_DEINIT;
list->v.destroy(conn);
i_assert(conn != list->connections);
}
i_free(list);
}