Skip to content

Commit f1ed4c3

Browse files
lxinchucklever
authored andcommitted
tlshd: clean up some unnecessary code in quic handshake
This patch introduces three cleanups with no functional changes: 1. The function quic_set_nonblocking() and its calls have been removed. Instead of setting the socket to non-blocking mode with fcntl, recvmsg(MSG_DONTWAIT) is now used directly to handle asynchronous receives. This simplifies the code by eliminating redundant operations. 2. The calls to tlshd_quic_session_configure() have been moved from client.c and server.c into tlshd_quic_start_handshake() within quic.c. This reduces code duplication and removes the need for an unnecessary external function declaration. 3. The QUIC_MAX_FRAG_LEN limitation in quic_read_func() was initially implemented for testing purposes. Since kernel handles fragmentation, the manual fragmentation loop in quic_read_func() has been removed, simplifying the code. Signed-off-by: Xin Long <[email protected]>
1 parent 2e807ba commit f1ed4c3

File tree

4 files changed

+27
-76
lines changed

4 files changed

+27
-76
lines changed

src/tlshd/client.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -528,9 +528,6 @@ static int tlshd_quic_client_set_x509_session(struct tlshd_quic_conn *conn)
528528
gnutls_handshake_set_hook_function(session, GNUTLS_HANDSHAKE_ANY,
529529
GNUTLS_HOOK_POST, tlshd_quic_client_ticket_recv);
530530
gnutls_session_set_ptr(session, conn);
531-
ret = tlshd_quic_session_configure(session, conn->alpns, conn->cipher);
532-
if (ret)
533-
goto err_session;
534531
if (conn->ticket_len) {
535532
ret = gnutls_session_set_data(session, conn->ticket, conn->ticket_len);
536533
if (ret)
@@ -591,9 +588,6 @@ static int tlshd_quic_client_set_psk_session(struct tlshd_quic_conn *conn)
591588
gnutls_handshake_set_hook_function(session, GNUTLS_HANDSHAKE_ANY,
592589
GNUTLS_HOOK_POST, tlshd_quic_client_ticket_recv);
593590
gnutls_session_set_ptr(session, conn);
594-
ret = tlshd_quic_session_configure(session, conn->alpns, conn->cipher);
595-
if (ret)
596-
goto err_session;
597591
ret = gnutls_credentials_set(session, GNUTLS_CRD_PSK, cred);
598592
if (ret)
599593
goto err_session;

src/tlshd/quic.c

Lines changed: 27 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include <linux/tls.h>
2424
#include <keyutils.h>
2525
#include <stdbool.h>
26-
#include <fcntl.h>
2726
#include <glib.h>
2827

2928
#include "config.h"
@@ -38,36 +37,12 @@ static void quic_timer_handler(union sigval arg)
3837
conn->errcode = ETIMEDOUT;
3938
}
4039

41-
static int quic_set_nonblocking(int sockfd, uint8_t nonblocking)
42-
{
43-
int flags = fcntl(sockfd, F_GETFL, 0);
44-
45-
if (flags == -1) {
46-
tlshd_log_error("socket fcntl getfl error %d", errno);
47-
return -1;
48-
}
49-
50-
if (nonblocking)
51-
flags |= O_NONBLOCK;
52-
else
53-
flags &= ~O_NONBLOCK;
54-
55-
if (fcntl(sockfd, F_SETFL, flags) == -1) {
56-
tlshd_log_error("socket fcntl setfl error %d %d", errno, flags);
57-
return -1;
58-
}
59-
return 0;
60-
}
61-
6240
static int quic_conn_setup_timer(struct tlshd_quic_conn *conn)
6341
{
6442
uint64_t msec = conn->parms->timeout_ms;
6543
struct itimerspec its = {};
6644
struct sigevent sev = {};
6745

68-
if (quic_set_nonblocking(conn->parms->sockfd, 1))
69-
return -1;
70-
7146
sev.sigev_notify = SIGEV_THREAD;
7247
sev.sigev_notify_function = quic_timer_handler;
7348
sev.sigev_value.sival_ptr = conn;
@@ -87,7 +62,6 @@ static int quic_conn_setup_timer(struct tlshd_quic_conn *conn)
8762

8863
static void quic_conn_delete_timer(struct tlshd_quic_conn *conn)
8964
{
90-
quic_set_nonblocking(conn->parms->sockfd, 0);
9165
timer_delete(conn->timer);
9266
}
9367

@@ -217,8 +191,6 @@ static int quic_tp_send_func(gnutls_session_t session, gnutls_buffer_t extdata)
217191
return 0;
218192
}
219193

220-
#define QUIC_MAX_FRAG_LEN 1200
221-
222194
static int quic_read_func(gnutls_session_t session, gnutls_record_encryption_level_t level,
223195
gnutls_handshake_description_t htype, const void *data, size_t datalen)
224196
{
@@ -229,28 +201,21 @@ static int quic_read_func(gnutls_session_t session, gnutls_record_encryption_lev
229201
if (htype == GNUTLS_HANDSHAKE_KEY_UPDATE)
230202
return 0;
231203

232-
while (len > 0) {
233-
msg = malloc(sizeof(*msg));
234-
if (!msg) {
235-
tlshd_log_error("msg malloc error %d", ENOMEM);
236-
return -1;
237-
}
238-
memset(msg, 0, sizeof(*msg));
239-
msg->len = len;
240-
if (len > QUIC_MAX_FRAG_LEN)
241-
msg->len = QUIC_MAX_FRAG_LEN;
242-
memcpy(msg->data, data, msg->len);
243-
244-
msg->level = quic_get_crypto_level(level);
245-
if (!conn->send_list)
246-
conn->send_list = msg;
247-
else
248-
conn->send_last->next = msg;
249-
conn->send_last = msg;
250-
251-
len -= msg->len;
252-
data += msg->len;
204+
msg = malloc(sizeof(*msg));
205+
if (!msg) {
206+
tlshd_log_debug("msg malloc error %d", ENOMEM);
207+
return -1;
253208
}
209+
memset(msg, 0, sizeof(*msg));
210+
msg->len = len;
211+
memcpy(msg->data, data, msg->len);
212+
213+
msg->level = quic_get_crypto_level(level);
214+
if (!conn->send_list)
215+
conn->send_list = msg;
216+
else
217+
conn->send_last->next = msg;
218+
conn->send_last = msg;
254219

255220
tlshd_log_debug(" Read func: %u %u %u", level, htype, datalen);
256221
return 0;
@@ -402,7 +367,7 @@ static int quic_handshake_recvmsg(int sockfd, struct tlshd_quic_msg *msg)
402367
inmsg.msg_control = incmsg;
403368
inmsg.msg_controllen = sizeof(incmsg);
404369

405-
ret = recvmsg(sockfd, &inmsg, 0);
370+
ret = recvmsg(sockfd, &inmsg, MSG_DONTWAIT);
406371
if (ret < 0)
407372
return ret;
408373
msg->len = ret;
@@ -512,24 +477,17 @@ void tlshd_quic_conn_destroy(struct tlshd_quic_conn *conn)
512477

513478
#define QUIC_TLSEXT_TP_PARAM 0x39u
514479

515-
/**
516-
* tlshd_quic_session_configure - Configure a handshake session
517-
* @session: TLS session to configure
518-
* @alpns: multiple ALPNs split by ','
519-
* @cipher: cipher perferred
520-
*
521-
* Returns: %GNUTLS_E_SUCCESS on success, or a negative error code
522-
*/
523-
int tlshd_quic_session_configure(gnutls_session_t session, char *alpns, uint32_t cipher)
480+
static int tlshd_quic_session_configure(struct tlshd_quic_conn *conn)
524481
{
482+
gnutls_session_t session = conn->session;
525483
int ret;
526484

527-
ret = quic_session_set_priority(session, cipher);
485+
ret = quic_session_set_priority(session, conn->cipher);
528486
if (ret)
529487
return ret;
530488

531-
if (alpns[0]) {
532-
ret = quic_session_set_alpns(session, alpns);
489+
if (conn->alpns[0]) {
490+
ret = quic_session_set_alpns(session, conn->alpns);
533491
if (ret)
534492
return ret;
535493
}
@@ -559,6 +517,13 @@ void tlshd_quic_start_handshake(struct tlshd_quic_conn *conn)
559517
FD_ZERO(&readfds);
560518
FD_SET(sockfd, &readfds);
561519

520+
ret = tlshd_quic_session_configure(conn);
521+
if (ret) {
522+
tlshd_log_gnutls_error(ret);
523+
conn->errcode = -ret;
524+
return;
525+
}
526+
562527
if (!conn->is_serv) {
563528
ret = quic_handshake_crypto_data(conn, QUIC_CRYPTO_INITIAL, NULL, 0);
564529
if (ret) {

src/tlshd/server.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -503,9 +503,6 @@ static int tlshd_quic_server_set_x509_session(struct tlshd_quic_conn *conn)
503503
goto err_session;
504504

505505
gnutls_session_set_ptr(session, conn);
506-
ret = tlshd_quic_session_configure(session, conn->alpns, conn->cipher);
507-
if (ret)
508-
goto err_session;
509506
ticket_key.data = conn->ticket;
510507
ticket_key.size = conn->ticket_len;
511508
ret = gnutls_session_ticket_enable_server(session, &ticket_key);
@@ -545,9 +542,6 @@ static int tlshd_quic_server_set_psk_session(struct tlshd_quic_conn *conn)
545542
if (ret)
546543
goto err_cred;
547544
gnutls_session_set_ptr(session, conn);
548-
ret = tlshd_quic_session_configure(session, conn->alpns, conn->cipher);
549-
if (ret)
550-
goto err_session;
551545
gnutls_handshake_set_hook_function(session, GNUTLS_HANDSHAKE_CLIENT_HELLO,
552546
GNUTLS_HOOK_POST, tlshd_quic_server_alpn_verify);
553547
ret = gnutls_credentials_set(session, GNUTLS_CRD_PSK, cred);

src/tlshd/tlshd.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,6 @@ struct tlshd_quic_conn {
160160
extern int tlshd_quic_conn_create(struct tlshd_quic_conn **conn_p,
161161
struct tlshd_handshake_parms *parms);
162162
extern void tlshd_quic_conn_destroy(struct tlshd_quic_conn *conn);
163-
extern int tlshd_quic_session_configure(gnutls_session_t session,
164-
char *alpns, uint32_t cipher);
165163
extern void tlshd_quic_start_handshake(struct tlshd_quic_conn *conn);
166164
#endif
167165

0 commit comments

Comments
 (0)