comparison src/event/quic/ngx_event_quic_socket.c @ 8955:32daba3aabb2 quic

QUIC: got rid of ngx_quic_create_temp_socket(). It was mostly copy of the ngx_quic_listen(). Now ngx_quic_listen() no longer generates server id and increments seqnum. Instead, the server id is generated when the socket is created. The ngx_quic_alloc_socket() function is renamed to ngx_quic_create_socket().
author Vladimir Homutov <vl@nginx.com>
date Mon, 27 Dec 2021 13:49:56 +0300
parents fb41e37ddeb0
children 1e2f4e9c8195
comparison
equal deleted inserted replaced
8954:584e9951b044 8955:32daba3aabb2
8 #include <ngx_core.h> 8 #include <ngx_core.h>
9 #include <ngx_event.h> 9 #include <ngx_event.h>
10 #include <ngx_event_quic_connection.h> 10 #include <ngx_event_quic_connection.h>
11 11
12 12
13 static ngx_int_t ngx_quic_create_temp_socket(ngx_connection_t *c,
14 ngx_quic_connection_t *qc, ngx_str_t *dcid, ngx_quic_path_t *path,
15 ngx_quic_client_id_t *cid);
16
17
18 ngx_int_t 13 ngx_int_t
19 ngx_quic_open_sockets(ngx_connection_t *c, ngx_quic_connection_t *qc, 14 ngx_quic_open_sockets(ngx_connection_t *c, ngx_quic_connection_t *qc,
20 ngx_quic_header_t *pkt) 15 ngx_quic_header_t *pkt)
21 { 16 {
22 ngx_quic_path_t *path; 17 ngx_quic_path_t *path;
23 ngx_quic_socket_t *qsock; 18 ngx_quic_socket_t *qsock, *tmp;
24 ngx_quic_client_id_t *cid; 19 ngx_quic_client_id_t *cid;
25 20
26 /* 21 /*
27 * qc->nclient_ids = 0 22 * qc->nclient_ids = 0
28 * qc->nsockets = 0 23 * qc->nsockets = 0
43 qc->tp.original_dcid.data = ngx_pstrdup(c->pool, &pkt->odcid); 38 qc->tp.original_dcid.data = ngx_pstrdup(c->pool, &pkt->odcid);
44 if (qc->tp.original_dcid.data == NULL) { 39 if (qc->tp.original_dcid.data == NULL) {
45 return NGX_ERROR; 40 return NGX_ERROR;
46 } 41 }
47 42
48 /* socket to use for further processing */ 43 /* socket to use for further processing (id auto-generated) */
49 qsock = ngx_quic_alloc_socket(c, qc); 44 qsock = ngx_quic_create_socket(c, qc);
50 if (qsock == NULL) { 45 if (qsock == NULL) {
51 return NGX_ERROR; 46 return NGX_ERROR;
52 } 47 }
53 48
54 /* socket is listening at new server id (autogenerated) */ 49 /* socket is listening at new server id */
55 if (ngx_quic_listen(c, qc, qsock) != NGX_OK) { 50 if (ngx_quic_listen(c, qc, qsock) != NGX_OK) {
56 return NGX_ERROR; 51 return NGX_ERROR;
57 } 52 }
58 53
59 qc->tp.initial_scid.len = qsock->sid.len; 54 qc->tp.initial_scid.len = qsock->sid.len;
86 } 81 }
87 82
88 /* now bind socket to client and path */ 83 /* now bind socket to client and path */
89 ngx_quic_connect(c, qsock, path, cid); 84 ngx_quic_connect(c, qsock, path, cid);
90 85
91 if (ngx_quic_create_temp_socket(c, qc, &pkt->odcid, path, cid) != NGX_OK) { 86 tmp = ngx_pcalloc(c->pool, sizeof(ngx_quic_socket_t));
92 goto failed; 87 if (tmp == NULL) {
93 } 88 goto failed;
89 }
90
91 tmp->sid.seqnum = NGX_QUIC_UNSET_PN; /* temporary socket */
92
93 ngx_memcpy(tmp->sid.id, pkt->odcid.data, pkt->odcid.len);
94 tmp->sid.len = pkt->odcid.len;
95
96 if (ngx_quic_listen(c, qc, tmp) != NGX_OK) {
97 goto failed;
98 }
99
100 ngx_quic_connect(c, tmp, path, cid);
94 101
95 /* use this socket as default destination */ 102 /* use this socket as default destination */
96 qc->socket = qsock; 103 qc->socket = qsock;
97 104
98 ngx_log_debug4(NGX_LOG_DEBUG_EVENT, c->log, 0, 105 ngx_log_debug4(NGX_LOG_DEBUG_EVENT, c->log, 0,
109 116
110 return NGX_ERROR; 117 return NGX_ERROR;
111 } 118 }
112 119
113 120
114 static ngx_int_t
115 ngx_quic_create_temp_socket(ngx_connection_t *c, ngx_quic_connection_t *qc,
116 ngx_str_t *dcid, ngx_quic_path_t *path, ngx_quic_client_id_t *cid)
117 {
118 ngx_str_t id;
119 ngx_quic_socket_t *qsock;
120 ngx_quic_server_id_t *sid;
121
122 qsock = ngx_quic_alloc_socket(c, qc);
123 if (qsock == NULL) {
124 return NGX_ERROR;
125 }
126
127 sid = &qsock->sid;
128
129 sid->seqnum = NGX_QUIC_UNSET_PN; /* mark socket as temporary */
130
131 sid->len = dcid->len;
132 ngx_memcpy(sid->id, dcid->data, dcid->len);
133
134 id.len = sid->len;
135 id.data = sid->id;
136
137 ngx_insert_udp_connection(c, &qsock->udp, &id);
138
139 ngx_queue_insert_tail(&qc->sockets, &qsock->queue);
140
141 qc->nsockets++;
142 qsock->quic = qc;
143
144 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0,
145 "quic socket #%L listening at sid:%xV nsock:%ui",
146 (int64_t) sid->seqnum, &id, qc->nsockets);
147
148 ngx_quic_connect(c, qsock, path, cid);
149
150 return NGX_OK;
151 }
152
153
154 ngx_quic_socket_t * 121 ngx_quic_socket_t *
155 ngx_quic_alloc_socket(ngx_connection_t *c, ngx_quic_connection_t *qc) 122 ngx_quic_create_socket(ngx_connection_t *c, ngx_quic_connection_t *qc)
156 { 123 {
157 ngx_queue_t *q; 124 ngx_queue_t *q;
158 ngx_quic_socket_t *sock; 125 ngx_quic_socket_t *sock;
159 126
160 if (!ngx_queue_empty(&qc->free_sockets)) { 127 if (!ngx_queue_empty(&qc->free_sockets)) {
172 if (sock == NULL) { 139 if (sock == NULL) {
173 return NULL; 140 return NULL;
174 } 141 }
175 } 142 }
176 143
144 sock->sid.len = NGX_QUIC_SERVER_CID_LEN;
145 if (ngx_quic_create_server_id(c, sock->sid.id) != NGX_OK) {
146 return NULL;
147 }
148
149 sock->sid.seqnum = qc->server_seqnum++;
150
177 return sock; 151 return sock;
178 } 152 }
179 153
180 154
181 void 155 void
234 ngx_str_t id; 208 ngx_str_t id;
235 ngx_quic_server_id_t *sid; 209 ngx_quic_server_id_t *sid;
236 210
237 sid = &qsock->sid; 211 sid = &qsock->sid;
238 212
239 sid->len = NGX_QUIC_SERVER_CID_LEN;
240
241 if (ngx_quic_create_server_id(c, sid->id) != NGX_OK) {
242 return NGX_ERROR;
243 }
244
245 sid->seqnum = qc->server_seqnum++;
246
247 id.data = sid->id; 213 id.data = sid->id;
248 id.len = sid->len; 214 id.len = sid->len;
249 215
250 ngx_insert_udp_connection(c, &qsock->udp, &id); 216 ngx_insert_udp_connection(c, &qsock->udp, &id);
251 217
253 219
254 qc->nsockets++; 220 qc->nsockets++;
255 qsock->quic = qc; 221 qsock->quic = qc;
256 222
257 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0, 223 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0,
258 "quic socket #%uL listening at sid:%xV nsock:%ui", 224 "quic socket #%L listening at sid:%xV nsock:%ui",
259 sid->seqnum, &id, qc->nsockets); 225 (int64_t) sid->seqnum, &id, qc->nsockets);
260 226
261 return NGX_OK; 227 return NGX_OK;
262 } 228 }
263 229
264 230