changeset 8181:3cb4f16426a5 quic

Introduced quic_version macro, uint16/uint32 routines ported.
author Sergey Kandaurov <pluknet@nginx.com>
date Fri, 28 Feb 2020 13:09:52 +0300
parents 01dc595de244
children b28ea685a56e
files src/event/ngx_event_openssl.c src/event/ngx_event_quic.h src/http/ngx_http_request.c
diffstat 3 files changed, 50 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/event/ngx_event_openssl.c
+++ b/src/event/ngx_event_openssl.c
@@ -271,10 +271,7 @@ quic_add_handshake_data(ngx_ssl_conn_t *
     } else if (level == ssl_encryption_handshake) {
         *p++ = 0xe0;	// handshake, packet number len
     }
-    *p++ = 0xff;
-    *p++ = 0x00;
-    *p++ = 0x00;
-    *p++ = 0x18;
+    p = ngx_quic_write_uint32(p, quic_version);
     *p++ = qc->scid.len;
     p = ngx_cpymem(p, qc->scid.data, qc->scid.len);
     *p++ = qc->dcid.len;
--- a/src/event/ngx_event_quic.h
+++ b/src/event/ngx_event_quic.h
@@ -10,6 +10,8 @@
 
 #include <ngx_event_openssl.h>
 
+#define quic_version 0xff000018
+
 
 typedef struct {
     ngx_str_t          secret;
@@ -58,4 +60,45 @@ ngx_int_t
 ngx_quic_tls_hp(ngx_connection_t *c, const EVP_CIPHER *cipher,
     ngx_quic_secret_t *s, u_char *out, u_char *in);
 
+
+#if (NGX_HAVE_NONALIGNED)
+
+#define ngx_quic_parse_uint16(p)  ntohs(*(uint16_t *) (p))
+#define ngx_quic_parse_uint32(p)  ntohl(*(uint32_t *) (p))
+
+#else
+
+#define ngx_quic_parse_uint16(p)  ((p)[0] << 8 | (p)[1])
+#define ngx_quic_parse_uint32(p)                                              \
+    ((uint32_t) (p)[0] << 24 | (p)[1] << 16 | (p)[2] << 8 | (p)[3])
+
+#endif
+
+
+#define ngx_quic_write_uint16_aligned(p, s)                                   \
+    (*(uint16_t *) (p) = htons((uint16_t) (s)), (p) + sizeof(uint16_t))
+#define ngx_quic_write_uint32_aligned(p, s)                                   \
+    (*(uint32_t *) (p) = htonl((uint32_t) (s)), (p) + sizeof(uint32_t))
+
+#if (NGX_HAVE_NONALIGNED)
+
+#define ngx_quic_write_uint16  ngx_quic_write_uint16_aligned
+#define ngx_quic_write_uint32  ngx_quic_write_uint32_aligned
+
+#else
+
+#define ngx_quic_write_uint16(p, s)                                           \
+    ((p)[0] = (u_char) ((s) >> 8),                                            \
+     (p)[1] = (u_char)  (s),                                                  \
+     (p) + sizeof(uint16_t))
+
+#define ngx_quic_write_uint32(p, s)                                           \
+    ((p)[0] = (u_char) ((s) >> 24),                                           \
+     (p)[1] = (u_char) ((s) >> 16),                                           \
+     (p)[2] = (u_char) ((s) >> 8),                                            \
+     (p)[3] = (u_char)  (s),                                                  \
+     (p) + sizeof(uint32_t))
+
+#endif
+
 #endif /* _NGX_EVENT_QUIC_H_INCLUDED_ */
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -691,13 +691,13 @@ ngx_http_quic_handshake(ngx_event_t *rev
     }
 
     ngx_int_t flags = *b->pos++;
-    uint32_t version = ngx_http_v2_parse_uint32(b->pos);
-    b->pos += 4;
+    uint32_t version = ngx_quic_parse_uint32(b->pos);
+    b->pos += sizeof(uint32_t);
 
     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, rev->log, 0,
                    "quic flags:%xi version:%xD", flags, version);
 
-    if (version != 0xff000018) {
+    if (version != quic_version) {
         ngx_log_error(NGX_LOG_INFO, rev->log, 0, "unsupported quic version");
         ngx_http_close_connection(c);
         return;
@@ -1117,13 +1117,13 @@ ngx_http_quic_handshake_handler(ngx_even
     }
 
     ngx_int_t flags = *p++;
-    uint32_t version = ngx_http_v2_parse_uint32(p);
-    p += 4;
+    uint32_t version = ngx_quic_parse_uint32(p);
+    p += sizeof(uint32_t);
 
     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, rev->log, 0,
                    "quic flags:%xi version:%xD", flags, version);
 
-    if (version != 0xff000018) {
+    if (version != quic_version) {
         ngx_log_error(NGX_LOG_INFO, rev->log, 0, "unsupported quic version");
         ngx_http_close_connection(c);
         return;