changeset 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 584e9951b044
children eaf356a35f5d
files src/event/quic/ngx_event_quic_connid.c src/event/quic/ngx_event_quic_socket.c src/event/quic/ngx_event_quic_socket.h
diffstat 3 files changed, 29 insertions(+), 63 deletions(-) [+]
line wrap: on
line diff
--- a/src/event/quic/ngx_event_quic_connid.c
+++ b/src/event/quic/ngx_event_quic_connid.c
@@ -497,7 +497,7 @@ ngx_quic_create_sockets(ngx_connection_t
 
     while (qc->nsockets < n) {
 
-        qsock = ngx_quic_alloc_socket(c, qc);
+        qsock = ngx_quic_create_socket(c, qc);
         if (qsock == NULL) {
             return NGX_ERROR;
         }
--- a/src/event/quic/ngx_event_quic_socket.c
+++ b/src/event/quic/ngx_event_quic_socket.c
@@ -10,17 +10,12 @@
 #include <ngx_event_quic_connection.h>
 
 
-static ngx_int_t ngx_quic_create_temp_socket(ngx_connection_t *c,
-    ngx_quic_connection_t *qc, ngx_str_t *dcid, ngx_quic_path_t *path,
-    ngx_quic_client_id_t *cid);
-
-
 ngx_int_t
 ngx_quic_open_sockets(ngx_connection_t *c, ngx_quic_connection_t *qc,
     ngx_quic_header_t *pkt)
 {
     ngx_quic_path_t       *path;
-    ngx_quic_socket_t     *qsock;
+    ngx_quic_socket_t     *qsock, *tmp;
     ngx_quic_client_id_t  *cid;
 
     /*
@@ -45,13 +40,13 @@ ngx_quic_open_sockets(ngx_connection_t *
          return NGX_ERROR;
     }
 
-    /* socket to use for further processing */
-    qsock = ngx_quic_alloc_socket(c, qc);
+    /* socket to use for further processing (id auto-generated) */
+    qsock = ngx_quic_create_socket(c, qc);
     if (qsock == NULL) {
         return NGX_ERROR;
     }
 
-    /* socket is listening at new server id (autogenerated) */
+    /* socket is listening at new server id */
     if (ngx_quic_listen(c, qc, qsock) != NGX_OK) {
         return NGX_ERROR;
     }
@@ -88,10 +83,22 @@ ngx_quic_open_sockets(ngx_connection_t *
     /* now bind socket to client and path */
     ngx_quic_connect(c, qsock, path, cid);
 
-    if (ngx_quic_create_temp_socket(c, qc, &pkt->odcid, path, cid) != NGX_OK) {
+    tmp = ngx_pcalloc(c->pool, sizeof(ngx_quic_socket_t));
+    if (tmp == NULL) {
         goto failed;
     }
 
+    tmp->sid.seqnum = NGX_QUIC_UNSET_PN; /* temporary socket */
+
+    ngx_memcpy(tmp->sid.id, pkt->odcid.data, pkt->odcid.len);
+    tmp->sid.len = pkt->odcid.len;
+
+    if (ngx_quic_listen(c, qc, tmp) != NGX_OK) {
+        goto failed;
+    }
+
+    ngx_quic_connect(c, tmp, path, cid);
+
     /* use this socket as default destination */
     qc->socket = qsock;
 
@@ -111,48 +118,8 @@ failed:
 }
 
 
-static ngx_int_t
-ngx_quic_create_temp_socket(ngx_connection_t *c, ngx_quic_connection_t *qc,
-    ngx_str_t *dcid, ngx_quic_path_t *path, ngx_quic_client_id_t *cid)
-{
-    ngx_str_t              id;
-    ngx_quic_socket_t     *qsock;
-    ngx_quic_server_id_t  *sid;
-
-    qsock = ngx_quic_alloc_socket(c, qc);
-    if (qsock == NULL) {
-        return NGX_ERROR;
-    }
-
-    sid = &qsock->sid;
-
-    sid->seqnum = NGX_QUIC_UNSET_PN; /* mark socket as temporary */
-
-    sid->len = dcid->len;
-    ngx_memcpy(sid->id, dcid->data, dcid->len);
-
-    id.len = sid->len;
-    id.data = sid->id;
-
-    ngx_insert_udp_connection(c, &qsock->udp, &id);
-
-    ngx_queue_insert_tail(&qc->sockets, &qsock->queue);
-
-    qc->nsockets++;
-    qsock->quic = qc;
-
-    ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0,
-                   "quic socket #%L listening at sid:%xV nsock:%ui",
-                   (int64_t) sid->seqnum, &id, qc->nsockets);
-
-    ngx_quic_connect(c, qsock, path, cid);
-
-    return NGX_OK;
-}
-
-
 ngx_quic_socket_t *
-ngx_quic_alloc_socket(ngx_connection_t *c, ngx_quic_connection_t *qc)
+ngx_quic_create_socket(ngx_connection_t *c, ngx_quic_connection_t *qc)
 {
     ngx_queue_t        *q;
     ngx_quic_socket_t  *sock;
@@ -174,6 +141,13 @@ ngx_quic_alloc_socket(ngx_connection_t *
         }
     }
 
+    sock->sid.len = NGX_QUIC_SERVER_CID_LEN;
+    if (ngx_quic_create_server_id(c, sock->sid.id) != NGX_OK) {
+        return NULL;
+    }
+
+    sock->sid.seqnum = qc->server_seqnum++;
+
     return sock;
 }
 
@@ -236,14 +210,6 @@ ngx_quic_listen(ngx_connection_t *c, ngx
 
     sid = &qsock->sid;
 
-    sid->len = NGX_QUIC_SERVER_CID_LEN;
-
-    if (ngx_quic_create_server_id(c, sid->id) != NGX_OK) {
-        return NGX_ERROR;
-    }
-
-    sid->seqnum = qc->server_seqnum++;
-
     id.data = sid->id;
     id.len = sid->len;
 
@@ -255,8 +221,8 @@ ngx_quic_listen(ngx_connection_t *c, ngx
     qsock->quic = qc;
 
     ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0,
-                   "quic socket #%uL listening at sid:%xV nsock:%ui",
-                   sid->seqnum, &id, qc->nsockets);
+                   "quic socket #%L listening at sid:%xV nsock:%ui",
+                   (int64_t) sid->seqnum, &id, qc->nsockets);
 
     return NGX_OK;
 }
--- a/src/event/quic/ngx_event_quic_socket.h
+++ b/src/event/quic/ngx_event_quic_socket.h
@@ -16,7 +16,7 @@ ngx_int_t ngx_quic_open_sockets(ngx_conn
     ngx_quic_connection_t *qc, ngx_quic_header_t *pkt);
 void ngx_quic_close_sockets(ngx_connection_t *c);
 
-ngx_quic_socket_t *ngx_quic_alloc_socket(ngx_connection_t *c,
+ngx_quic_socket_t *ngx_quic_create_socket(ngx_connection_t *c,
     ngx_quic_connection_t *qc);
 ngx_int_t ngx_quic_listen(ngx_connection_t *c, ngx_quic_connection_t *qc,
     ngx_quic_socket_t *qsock);