Skip to content

Commit df909ea

Browse files
committed
revised create_ssl_nonblocking_connection() to use handshake()
1 parent c48e628 commit df909ea

File tree

1 file changed

+7
-57
lines changed

1 file changed

+7
-57
lines changed

tests/test_ssl.py

Lines changed: 7 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,9 @@ def create_ssl_nonblocking_connection(
448448
"""
449449
Create a pair of sockets and set up an SSL connection between them.
450450
mode: The mode to set if not None.
451-
Returns the raw sockets and the SSL Connection objects.
451+
request_send_buffer_size: requested size of the send buffer
452+
Returns the raw sockets, the SSL Connection objects
453+
and the actual send/receive buffer sizes.
452454
"""
453455
chain = _create_certificate_chain()
454456

@@ -509,8 +511,8 @@ def create_ssl_nonblocking_connection(
509511
client = Connection(client_ctx, client_socket)
510512
server = Connection(server_ctx, server_socket)
511513

512-
# Set the buffers to be small so we can easily fill them
513-
# although the OS may not respect the values.
514+
# Allow caller to request small buffer sizes so they can be easily filled.
515+
# Note the OS may not respect the requested values.
514516
# Make the receive buffer smaller than the send buffer.
515517
requested_receive_buffer_size = request_send_buffer_size // 2
516518
client_socket.setsockopt(SOL_SOCKET, SO_SNDBUF, request_send_buffer_size)
@@ -519,7 +521,6 @@ def create_ssl_nonblocking_connection(
519521
f"Attempted SO_SNDBUF: {request_send_buffer_size}, "
520522
f"Actual SO_SNDBUF: {actual_sndbuf}"
521523
)
522-
523524
server_socket.setsockopt(
524525
SOL_SOCKET, SO_RCVBUF, requested_receive_buffer_size
525526
)
@@ -529,63 +530,12 @@ def create_ssl_nonblocking_connection(
529530
f"Actual SO_RCVBUF: {actual_rcvbuf}"
530531
)
531532

532-
# Manually set the connection state
533+
# set the connection state
533534
client.set_connect_state()
534535
server.set_accept_state()
535536

536-
# Perform the handshake with proper completion detection
537-
client_handshake_done = False
538-
server_handshake_done = False
539-
max_handshake_attempts = 100 # Prevent infinite loops
540-
attempts = 0
541-
while (
542-
not (client_handshake_done and server_handshake_done)
543-
and attempts < max_handshake_attempts
544-
):
545-
attempts += 1
546-
# Try client handshake
547-
if not client_handshake_done:
548-
try:
549-
client.do_handshake()
550-
client_handshake_done = True
551-
except SSL.WantReadError:
552-
# Client needs to read data
553-
pass
554-
except SSL.WantWriteError:
555-
# Client needs to write data
556-
pass
557-
558-
# Try server handshake
559-
if not server_handshake_done:
560-
try:
561-
server.do_handshake()
562-
server_handshake_done = True
563-
except SSL.WantReadError:
564-
# Server needs to read data
565-
pass
566-
except SSL.WantWriteError:
567-
# Server needs to write data
568-
pass
569-
570-
# If neither handshake is complete, wait for socket activity
571-
if not (client_handshake_done and server_handshake_done):
572-
# Use select to wait for socket activity
573-
ready_read, ready_write, ready_err = select.select(
574-
[client_socket, server_socket],
575-
[client_socket, server_socket],
576-
[client_socket, server_socket],
577-
1.0, # 1 second timeout
578-
)
579-
580-
if ready_err:
581-
raise Exception(f"Socket error during handshake: {ready_err}")
582-
583-
if not (ready_read or ready_write):
584-
# Timeout occurred, but continue trying
585-
continue
537+
handshake(client, server)
586538

587-
if not (client_handshake_done and server_handshake_done):
588-
raise Exception("SSL handshake failed to complete")
589539
return (
590540
client_socket,
591541
server_socket,

0 commit comments

Comments
 (0)