-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp.c
206 lines (166 loc) · 3.73 KB
/
tcp.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
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/param.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include "util.h"
#include "tcp_transport.h"
struct node ctrl_root;
struct node ctrl_node;
struct node self_node;
long read_count;
long write_count;
long read_partial;
long write_partial;
#define TIMEOUT_MSEC 10
static void
tcp_normalize(int fd)
{
int rc;
int one = 1;
struct timeval tv = {
.tv_sec = TIMEOUT_MSEC / 1000,
.tv_usec = (TIMEOUT_MSEC % 1000) * 1000,
};
size_t bufsize = 32 * 1024 * 1024;
set_blocking_mode(fd, !opt.nonblock);
rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
if (rc == -1)
err(1, "setsockopt(TCP_NODELAY)");
rc = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
if (rc == -1)
err(1, "setsockopt(SO_RCVTIMEO)");
rc = setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
if (rc == -1)
err(1, "setsockopt(SO_SNDTIMEO)");
rc = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &bufsize, sizeof(bufsize));
if (rc == -1)
err(1, "setsockopt(SO_SNDBUF)");
rc = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &bufsize, sizeof(bufsize));
if (rc == -1)
err(1, "setsockopt(SO_RCVBUF)");
}
int
tcp_wait_accept(struct node *listen)
{
struct sockaddr_storage ss;
socklen_t addrlen;
int fd;
fd = accept(listen->fd, (struct sockaddr *)&ss, &addrlen);
if (fd == -1)
err(1, "accept");
tcp_normalize(fd);
return fd;
}
void
tcp_listen(struct node *node, int port)
{
int fd, rc;
int one = 1;
set_port(node, port);
fd = socket(node->family, node->socktype, node->protocol);
if (fd == -1)
err(1, "socket(listen)");
rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
if (rc == -1)
err(1, "setsockopt(REUSEADDR)");
rc = bind(fd, (struct sockaddr *)&node->addr, node->addrlen);
if (rc == -1)
err(1, "bind, port %d", port);
if (opt.zerocopy) {
rc = setsockopt(fd, SOL_SOCKET, SO_ZEROCOPY, &one, sizeof(one));
if (rc == -1)
err(1, "setsockopt(SO_ZEROCOPY)");
}
rc = listen(fd, 1);
if (rc == -1)
err(1, "listen");
node->fd = fd;
}
bool
tcp_complete_connect(struct node *node)
{
int rc;
rc = connect(node->fd, (struct sockaddr *)&node->addr, node->addrlen);
if (rc == -1) {
if (errno == EINPROGRESS || errno == ECONNREFUSED)
return false;
err(1, "connect");
}
tcp_normalize(node->fd);
return true;
}
bool
tcp_connect(struct node *node, int port)
{
int fd, rc;
int one = 1;
set_port(node, port);
fd = socket(node->family, node->socktype, node->protocol);
if (fd == -1)
err(1, "socket(connect)");
node->fd = fd;
rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
if (rc == -1)
err(1, "setsockopt(REUSEADDR)");
if (opt.zerocopy) {
rc = setsockopt(fd, SOL_SOCKET, SO_ZEROCOPY, &one, sizeof(one));
if (rc == -1)
err(1, "setsockopt(SO_ZEROCOPY)");
}
return tcp_complete_connect(node);
}
void
tcp_wait_connect(struct node *node, int port)
{
int count = 50;
bool connected;
connected = tcp_connect(node, port);
while (!connected && count--) {
usleep(200 * 1000);
connected = tcp_complete_connect(node);
}
if (!connected)
errx(1, "can't connect to %s:%d", show_node_addr(node), port);
}
void
tcp_write(int fd, void *buf, int len)
{
int n;
write_count++;
for (;;) {
n = write(fd, buf, len);
if (n == len)
break;
if (n < 1)
err(1, "tcp_write: %d", n);
len -= n;
buf += n;
write_partial++;
}
}
void
tcp_read(int fd, void *buf, int len)
{
int n;
read_count++;
for (;;) {
n = read(fd, buf, len);
if (n == len)
break;
if (n == -1 && errno == EAGAIN)
continue;
if (n < 1)
err(1, "tcp_read: %d", n);
len -= n;
buf += n;
read_partial++;
}
}