tcp.c
8.4 KB
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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2004, 2006, 2007, 2010, 2011 Free Software
Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <mailutils/errno.h>
#include <mailutils/stream.h>
#include <mailutils/util.h>
#include <mailutils/sockaddr.h>
#include <mailutils/sys/stream.h>
#define TCP_STATE_INIT 1
#define TCP_STATE_RESOLVE 2
#define TCP_STATE_RESOLVING 3
#define TCP_STATE_CONNECTING 4
#define TCP_STATE_CONNECTED 5
struct _tcp_instance
{
struct _mu_stream stream;
int fd;
int state;
struct mu_sockaddr *remote_addr;
struct mu_sockaddr *source_addr;
};
static int
_tcp_close (mu_stream_t stream)
{
struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
int err = 0;
if (tcp->fd != -1)
{
if (close (tcp->fd) != 0)
{
err = errno;
}
}
tcp->fd = -1;
tcp->state = TCP_STATE_INIT;
return err;
}
static int
_tcp_open (mu_stream_t stream)
{
struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
int flgs, ret;
socklen_t namelen;
struct sockaddr_in peer_addr;
int flags;
mu_stream_get_flags (stream, &flags);
switch (tcp->state)
{
case TCP_STATE_INIT:
if (tcp->fd == -1)
{
tcp->fd = socket (tcp->remote_addr->addr->sa_family, SOCK_STREAM, 0);
if (tcp->fd == -1)
return errno;
}
if (flags & MU_STREAM_NONBLOCK)
{
flgs = fcntl (tcp->fd, F_GETFL);
flgs |= O_NONBLOCK;
fcntl (tcp->fd, F_SETFL, flgs);
mu_stream_set_flags (stream, MU_STREAM_NONBLOCK);
}
if (tcp->source_addr)
{
if (bind (tcp->fd, tcp->source_addr->addr,
tcp->source_addr->addrlen) < 0)
{
int e = errno;
close (tcp->fd);
tcp->fd = -1;
return e;
}
}
tcp->state = TCP_STATE_RESOLVING;
case TCP_STATE_RESOLVING:
tcp->state = TCP_STATE_RESOLVE;
case TCP_STATE_RESOLVE:
if (connect (tcp->fd, tcp->remote_addr->addr,
tcp->remote_addr->addrlen) == -1)
{
ret = errno;
if (ret == EINPROGRESS || ret == EAGAIN)
{
tcp->state = TCP_STATE_CONNECTING;
ret = EAGAIN;
}
else
_tcp_close (stream);
return ret;
}
tcp->state = TCP_STATE_CONNECTING;
case TCP_STATE_CONNECTING:
namelen = sizeof (peer_addr);
if (getpeername (tcp->fd,
(struct sockaddr *) &peer_addr, &namelen) == 0)
tcp->state = TCP_STATE_CONNECTED;
else
{
ret = errno;
_tcp_close (stream);
return ret;
}
break;
}
return 0;
}
static int
_tcp_ioctl (mu_stream_t stream, int code, int opcode, void *ptr)
{
struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
switch (code)
{
case MU_IOCTL_TRANSPORT:
if (!ptr)
return EINVAL;
else
{
mu_transport_t *ptrans = ptr;
switch (opcode)
{
case MU_IOCTL_OP_GET:
ptrans[0] = (mu_transport_t) tcp->fd;
ptrans[1] = NULL;
break;
case MU_IOCTL_OP_SET:
return ENOSYS;
default:
return EINVAL;
}
}
break;
default:
return ENOSYS;
}
return 0;
}
static int
_tcp_read (mu_stream_t stream, char *buf, size_t size, size_t *pret)
{
struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
ssize_t bytes;
if ((bytes = recv (tcp->fd, buf, size, 0)) == -1)
return errno;
*pret = bytes;
return 0;
}
static int
_tcp_write (mu_stream_t stream, const char *buf, size_t size, size_t *pret)
{
struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
ssize_t bytes;
if ((bytes = send (tcp->fd, buf, size, 0)) == -1)
return errno;
*pret = bytes;
return 0;
}
static void
_tcp_done (mu_stream_t stream)
{
struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
mu_sockaddr_free (tcp->remote_addr);
mu_sockaddr_free (tcp->source_addr);
}
int
_tcp_wait (mu_stream_t stream, int *pflags, struct timeval *tvp)
{
struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
if (tcp->fd == -1)
return EINVAL;
return mu_fd_wait (tcp->fd, pflags, tvp);
}
int
_tcp_shutdown (mu_stream_t stream, int how)
{
struct _tcp_instance *tcp = (struct _tcp_instance *)stream;
int flag;
if (tcp->fd == -1)
return EINVAL;
switch (how)
{
case MU_STREAM_READ:
flag = SHUT_RD;
break;
case MU_STREAM_WRITE:
flag = SHUT_WR;
}
if (shutdown (tcp->fd, flag))
return errno;
return 0;
}
static struct _tcp_instance *
_create_tcp_stream (int flags)
{
struct _tcp_instance *tcp =
(struct _tcp_instance *)_mu_stream_create (sizeof (*tcp), flags);
if (tcp)
{
tcp->stream.open = _tcp_open;
tcp->stream.close = _tcp_close;
tcp->stream.read = _tcp_read;
tcp->stream.write = _tcp_write;
tcp->stream.ctl = _tcp_ioctl;
tcp->stream.done = _tcp_done;
tcp->stream.wait = _tcp_wait;
tcp->stream.shutdown = _tcp_shutdown;
tcp->fd = -1;
tcp->state = TCP_STATE_INIT;
}
return tcp;
}
int
mu_tcp_stream_create_from_sa (mu_stream_t *pstream,
struct mu_sockaddr *remote_addr,
struct mu_sockaddr *source_addr, int flags)
{
int rc;
mu_stream_t stream;
struct _tcp_instance *tcp;
tcp = _create_tcp_stream (flags | MU_STREAM_RDWR);
if (!tcp)
return ENOMEM;
tcp->remote_addr = remote_addr;
tcp->source_addr = source_addr;
stream = (mu_stream_t) tcp;
rc = mu_stream_open (stream);
if (rc == 0 || rc == EAGAIN || rc == EINPROGRESS)
*pstream = stream;
else
mu_stream_destroy (&stream);
return rc;
}
int
mu_tcp_stream_create_with_source_ip (mu_stream_t *pstream,
const char *host, unsigned port,
unsigned long source_ip,
int flags)
{
int rc;
struct mu_sockaddr *remote_addr, *source_addr = NULL;
struct mu_sockaddr_hints hints;
memset (&hints, 0, sizeof hints);
hints.family = AF_INET;
hints.socktype = SOCK_STREAM;
hints.protocol = IPPROTO_TCP;
hints.port = port;
rc = mu_sockaddr_from_node (&remote_addr, host, NULL, &hints);
if (rc)
return rc;
if (source_ip)
{
struct sockaddr_in s;
s.sin_family = AF_INET;
s.sin_addr.s_addr = source_ip;
s.sin_port = 0;
rc = mu_sockaddr_create (&source_addr, (struct sockaddr*)&s,
sizeof (s));
if (rc)
{
mu_sockaddr_free (remote_addr);
return 0;
}
}
rc = mu_tcp_stream_create_from_sa (pstream, remote_addr, source_addr, flags);
if (rc)
{
mu_sockaddr_free (remote_addr);
mu_sockaddr_free (source_addr);
}
return rc;
}
int
mu_tcp_stream_create_with_source_host (mu_stream_t *stream,
const char *host, unsigned port,
const char *source_host,
int flags)
{
int rc;
struct mu_sockaddr *remote_addr, *source_addr = NULL;
struct mu_sockaddr_hints hints;
memset (&hints, 0, sizeof hints);
hints.family = AF_INET;
hints.socktype = SOCK_STREAM;
hints.port = port;
rc = mu_sockaddr_from_node (&remote_addr, host, NULL, &hints);
if (rc)
return rc;
if (source_host)
{
hints.flags = MU_AH_PASSIVE;
hints.port = 0;
rc = mu_sockaddr_from_node (&source_addr, source_host, NULL, &hints);
if (rc)
{
mu_sockaddr_free (remote_addr);
return 0;
}
}
rc = mu_tcp_stream_create_from_sa (stream, remote_addr, source_addr, flags);
if (rc)
{
mu_sockaddr_free (remote_addr);
mu_sockaddr_free (source_addr);
}
return rc;
}
int
mu_tcp_stream_create (mu_stream_t *stream, const char *host, unsigned port,
int flags)
{
return mu_tcp_stream_create_with_source_host (stream, host, port,
NULL, flags);
}