-
Notifications
You must be signed in to change notification settings - Fork 2
/
simple_client.c
326 lines (281 loc) · 9.86 KB
/
simple_client.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
// Copyright (c) 2023 The TQUIC Authors.
//
// Licensed 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.
#include <errno.h>
#include <ev.h>
#include <fcntl.h>
#include <inttypes.h>
#include <netdb.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include "openssl/ssl.h"
#include "tquic.h"
#define READ_BUF_SIZE 4096
#define MAX_DATAGRAM_SIZE 1200
// A simple client that supports HTTP/0.9 over QUIC
struct simple_client {
struct quic_endpoint_t *quic_endpoint;
ev_timer timer;
int sock;
struct sockaddr_storage local_addr;
socklen_t local_addr_len;
struct quic_tls_config_t *tls_config;
struct quic_conn_t *conn;
struct ev_loop *loop;
};
void client_on_conn_created(void *tctx, struct quic_conn_t *conn) {
struct simple_client *client = tctx;
client->conn = conn;
}
void client_on_conn_established(void *tctx, struct quic_conn_t *conn) {
const char *data = "GET /\r\n";
quic_stream_write(conn, 0, (uint8_t *)data, strlen(data), true);
}
void client_on_conn_closed(void *tctx, struct quic_conn_t *conn) {
struct simple_client *client = tctx;
ev_break(client->loop, EVBREAK_ALL);
}
void client_on_stream_created(void *tctx, struct quic_conn_t *conn,
uint64_t stream_id) {}
void client_on_stream_readable(void *tctx, struct quic_conn_t *conn,
uint64_t stream_id) {
static uint8_t buf[READ_BUF_SIZE];
bool fin = false;
ssize_t r = quic_stream_read(conn, stream_id, buf, READ_BUF_SIZE, &fin);
if (r < 0) {
fprintf(stderr, "stream[%ld] read error\n", stream_id);
return;
}
printf("%.*s", (int)r, buf);
if (fin) {
const char *reason = "ok";
quic_conn_close(conn, true, 0, (const uint8_t *)reason, strlen(reason));
}
}
void client_on_stream_writable(void *tctx, struct quic_conn_t *conn,
uint64_t stream_id) {
quic_stream_wantwrite(conn, stream_id, false);
}
void client_on_stream_closed(void *tctx, struct quic_conn_t *conn,
uint64_t stream_id) {}
int client_on_packets_send(void *psctx, struct quic_packet_out_spec_t *pkts,
unsigned int count) {
struct simple_client *client = psctx;
unsigned int sent_count = 0;
int i, j = 0;
for (i = 0; i < count; i++) {
struct quic_packet_out_spec_t *pkt = pkts + i;
for (j = 0; j < (*pkt).iovlen; j++) {
const struct iovec *iov = pkt->iov + j;
ssize_t sent =
sendto(client->sock, iov->iov_base, iov->iov_len, 0,
(struct sockaddr *)pkt->dst_addr, pkt->dst_addr_len);
if (sent != iov->iov_len) {
if ((errno == EWOULDBLOCK) || (errno == EAGAIN)) {
fprintf(stderr, "send would block, already sent: %d\n",
sent_count);
return sent_count;
}
return -1;
}
sent_count++;
}
}
return sent_count;
}
const struct quic_transport_methods_t quic_transport_methods = {
.on_conn_created = client_on_conn_created,
.on_conn_established = client_on_conn_established,
.on_conn_closed = client_on_conn_closed,
.on_stream_created = client_on_stream_created,
.on_stream_readable = client_on_stream_readable,
.on_stream_writable = client_on_stream_writable,
.on_stream_closed = client_on_stream_closed,
};
const struct quic_packet_send_methods_t quic_packet_send_methods = {
.on_packets_send = client_on_packets_send,
};
static void process_connections(struct simple_client *client) {
quic_endpoint_process_connections(client->quic_endpoint);
double timeout = quic_endpoint_timeout(client->quic_endpoint) / 1e3f;
if (timeout < 0.0001) {
timeout = 0.0001;
}
client->timer.repeat = timeout;
ev_timer_again(client->loop, &client->timer);
}
static void read_callback(EV_P_ ev_io *w, int revents) {
struct simple_client *client = w->data;
static uint8_t buf[READ_BUF_SIZE];
while (true) {
struct sockaddr_storage peer_addr;
socklen_t peer_addr_len = sizeof(peer_addr);
memset(&peer_addr, 0, peer_addr_len);
ssize_t read = recvfrom(client->sock, buf, sizeof(buf), 0,
(struct sockaddr *)&peer_addr, &peer_addr_len);
if (read < 0) {
if ((errno == EWOULDBLOCK) || (errno == EAGAIN)) {
break;
}
fprintf(stderr, "failed to read\n");
return;
}
quic_packet_info_t quic_packet_info = {
.src = (struct sockaddr *)&peer_addr,
.src_len = peer_addr_len,
.dst = (struct sockaddr *)&client->local_addr,
.dst_len = client->local_addr_len,
};
int r = quic_endpoint_recv(client->quic_endpoint, buf, read,
&quic_packet_info);
if (r != 0) {
fprintf(stderr, "recv failed %d\n", r);
continue;
}
}
process_connections(client);
}
static void timeout_callback(EV_P_ ev_timer *w, int revents) {
struct simple_client *client = w->data;
quic_endpoint_on_timeout(client->quic_endpoint);
process_connections(client);
}
static void debug_log(const uint8_t *data, size_t data_len, void *argp) {
fwrite(data, sizeof(uint8_t), data_len, stderr);
}
static int create_socket(const char *host, const char *port,
struct addrinfo **peer, struct simple_client *client) {
const struct addrinfo hints = {.ai_family = PF_UNSPEC,
.ai_socktype = SOCK_DGRAM,
.ai_protocol = IPPROTO_UDP};
if (getaddrinfo(host, port, &hints, peer) != 0) {
fprintf(stderr, "failed to resolve host\n");
return -1;
}
int sock = socket((*peer)->ai_family, SOCK_DGRAM, 0);
if (sock < 0) {
fprintf(stderr, "failed to create socket\n");
return -1;
}
if (fcntl(sock, F_SETFL, O_NONBLOCK) != 0) {
fprintf(stderr, "failed to make socket non-blocking\n");
return -1;
}
client->local_addr_len = sizeof(client->local_addr);
if (getsockname(sock, (struct sockaddr *)&client->local_addr,
&client->local_addr_len) != 0) {
fprintf(stderr, "failed to get local address of socket\n");
return -1;
};
client->sock = sock;
return 0;
}
int main(int argc, char *argv[]) {
if (argc < 3) {
fprintf(stderr, "%s <dest_addr> <dest_port>\n", argv[0]);
return -1;
}
// Set logger.
quic_set_logger(debug_log, NULL, "TRACE");
// Create client.
struct simple_client client;
client.quic_endpoint = NULL;
client.tls_config = NULL;
client.conn = NULL;
client.loop = NULL;
quic_config_t *config = NULL;
int ret = 0;
// Create socket.
const char *host = argv[1];
const char *port = argv[2];
struct addrinfo *peer = NULL;
if (create_socket(host, port, &peer, &client) != 0) {
ret = -1;
goto EXIT;
}
// Create quic config.
config = quic_config_new();
if (config == NULL) {
fprintf(stderr, "failed to create config\n");
ret = -1;
goto EXIT;
}
quic_config_set_max_idle_timeout(config, 5000);
quic_config_set_recv_udp_payload_size(config, MAX_DATAGRAM_SIZE);
// Create and set tls config.
const char *const protos[1] = {"http/0.9"};
client.tls_config = quic_tls_config_new_client_config(protos, 1, true);
if (client.tls_config == NULL) {
ret = -1;
goto EXIT;
}
quic_config_set_tls_config(config, client.tls_config);
// Create quic endpoint
client.quic_endpoint =
quic_endpoint_new(config, false, &quic_transport_methods, &client,
&quic_packet_send_methods, &client);
if (client.quic_endpoint == NULL) {
fprintf(stderr, "failed to create quic endpoint\n");
ret = -1;
goto EXIT;
}
// Init event loop.
client.loop = ev_default_loop(0);
ev_init(&client.timer, timeout_callback);
client.timer.data = &client;
// Connect to server.
ret = quic_endpoint_connect(
client.quic_endpoint, (struct sockaddr *)&client.local_addr,
client.local_addr_len, peer->ai_addr, peer->ai_addrlen,
NULL /* server_name */, NULL /* session */, 0 /* session_len */,
NULL /* token */, 0 /* token_len */, NULL /* config */,
NULL /* index */);
if (ret < 0) {
fprintf(stderr, "failed to connect to client: %d\n", ret);
ret = -1;
goto EXIT;
}
process_connections(&client);
// Start event loop.
ev_io watcher;
ev_io_init(&watcher, read_callback, client.sock, EV_READ);
ev_io_start(client.loop, &watcher);
watcher.data = &client;
ev_loop(client.loop, 0);
EXIT:
if (peer != NULL) {
freeaddrinfo(peer);
}
if (client.tls_config != NULL) {
quic_tls_config_free(client.tls_config);
}
if (client.sock > 0) {
close(client.sock);
}
if (client.quic_endpoint != NULL) {
quic_endpoint_free(client.quic_endpoint);
}
if (client.loop != NULL) {
ev_loop_destroy(client.loop);
}
if (config != NULL) {
quic_config_free(config);
}
return ret;
}