-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrap.c
More file actions
217 lines (183 loc) · 4.54 KB
/
wrap.c
File metadata and controls
217 lines (183 loc) · 4.54 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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "wrap.h"
#include "vec.h"
#include "ngtcp2.h"
#include "nghttp3.h"
const char my_wrap_body_content_type[] = "text/plain";
const char my_wrap_body[] = "Tekstdokument";
struct my_wrap_data {
struct my_ngtcp2_ctx *ngtcp2_ctx;
struct my_nghttp3_ctx *nghttp3_ctx;
};
int my_wrap_submit_request_with_body (
int64_t *result,
struct my_wrap_data *data,
const char *endpoint,
const char *host,
const char *content_type,
struct my_nghttp3_data *body_data
) {
int ret = 0;
*result = 0;
int64_t stream_id = 0;
if ((ret = my_ngtcp2_new_stream(&stream_id, data->ngtcp2_ctx)) != 0) {
goto out;
}
if (body_data) {
assert(body_data->data != NULL);
assert(content_type != NULL);
if ((ret = my_nghttp3_submit_request_with_body(data->nghttp3_ctx, stream_id, endpoint, host, "POST", content_type, body_data)) != 0) {
goto out;
}
}
else {
if ((ret = my_nghttp3_submit_request(data->nghttp3_ctx, stream_id, endpoint, host)) != 0) {
goto out;
}
}
*result = stream_id;
out:
return ret;
}
int my_wrap_submit_request (
int64_t *result,
struct my_wrap_data *data,
const char *endpoint,
const char *host
) {
return my_wrap_submit_request_with_body(result, data, endpoint, host, NULL, NULL);
}
int my_wrap_cb_ready (void *arg) {
struct my_wrap_data *data = arg;
int64_t ctrl_stream_id = 0;
int64_t qpack_enc_stream_id = 0;
int64_t qpack_dec_stream_id = 0;
int64_t request_stream_id = 0;
int ret = 0;
if ((ret = my_ngtcp2_get_ctrl_streams(&ctrl_stream_id, &qpack_enc_stream_id, &qpack_dec_stream_id, data->ngtcp2_ctx)) != 0) {
goto out;
}
if ((ret = my_nghttp3_bind_ctrl_streams(data->nghttp3_ctx, ctrl_stream_id, qpack_enc_stream_id, qpack_dec_stream_id)) != 0) {
goto out;
}
if ((ret = my_wrap_submit_request(&request_stream_id, data, SERVER_ENDPOINT, SERVER_ADDR)) != 0) {
goto out;
}
static struct my_nghttp3_data body_data = {
my_wrap_body,
sizeof(my_wrap_body) - 1,
0
};
if ((ret = my_wrap_submit_request_with_body(&request_stream_id, data, SERVER_ENDPOINT, SERVER_ADDR, my_wrap_body_content_type, &body_data)) != 0) {
goto out;
}
out:
return ret;
}
int my_wrap_cb_ack_data (
int64_t stream_id,
size_t bytes,
void *arg
) {
struct my_wrap_data *data = arg;
return my_nghttp3_report_ack(data->nghttp3_ctx, stream_id, bytes);
}
int my_wrap_cb_block_stream (
int64_t stream_id,
int blocked,
void *arg
) {
struct my_wrap_data *data = arg;
return my_nghttp3_block_stream(data->nghttp3_ctx, stream_id, blocked);
}
int my_wrap_cb_get_data (
int64_t *stream_id,
my_ngtcp2_vec *vec,
size_t *vec_count,
int *fin,
void *arg
) {
struct my_wrap_data *data = arg;
return my_nghttp3_get_data(data->nghttp3_ctx, stream_id, vec, vec_count, fin);
}
int my_wrap_cb_deliver_data (
size_t *consumed,
int64_t stream_id,
const uint8_t *buf,
size_t buflen,
int fin,
void *arg
) {
struct my_wrap_data *data = arg;
return my_nghttp3_deliver_data(data->nghttp3_ctx, consumed, stream_id, buf, buflen, fin);
}
int my_wrap_data_new (
struct my_wrap_data **result,
const char *name_remote,
const struct sockaddr *addr_remote,
socklen_t addr_remote_len,
const struct sockaddr *addr_local,
socklen_t addr_local_len,
int fd,
struct event_base *event
) {
int ret = 0;
struct my_wrap_data *wrap_data;
if ((wrap_data = malloc(sizeof(*wrap_data))) == NULL) {
printf("Failed to allocate wrap data in %s\n", __func__);
ret = 1;
goto out;
}
memset(wrap_data, '\0', sizeof(*wrap_data));
if (my_ngtcp2_ctx_new (
&wrap_data->ngtcp2_ctx,
name_remote,
addr_remote,
addr_remote_len,
addr_local,
addr_local_len,
fd,
QUIC_CIPHERS,
QUIC_GROUPS,
H3_ALPN_H3_29 H3_ALPN_H3,
event,
my_wrap_cb_ready,
my_wrap_cb_get_data,
my_wrap_cb_ack_data,
my_wrap_cb_deliver_data,
my_wrap_cb_block_stream,
wrap_data
) != 0) {
goto out_destroy_wrap_data;
}
if ((ret = my_nghttp3_ctx_new (
&wrap_data->nghttp3_ctx
)) != 0) {
goto out_destroy_ngtcp2;
}
*result = wrap_data;
goto out;
// out_destroy_nghttp3:
// my_nghttp3_ctx_destroy(wrap_data->nghttp3_ctx);
out_destroy_ngtcp2:
my_ngtcp2_ctx_destroy(wrap_data->ngtcp2_ctx);
out_destroy_wrap_data:
free(wrap_data);
out:
return ret;
}
void my_wrap_data_destroy (
struct my_wrap_data *wrap_data
) {
my_nghttp3_ctx_destroy(wrap_data->nghttp3_ctx);
my_ngtcp2_ctx_destroy(wrap_data->ngtcp2_ctx);
free(wrap_data);
}
int my_wrap_get_event_ret (
struct my_wrap_data *wrap_data
) {
return my_ngtcp2_get_event_ret(wrap_data->ngtcp2_ctx);
}