net.mbedtls: store the client ip (for ipv4), shutdown on handshake failure, in .accept() (#22184)

This commit is contained in:
Martin Skou 2024-09-09 17:55:28 +02:00 committed by GitHub
parent 6528efa405
commit f002fd4493
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -114,6 +114,7 @@ pub mut:
handle int handle int
duration time.Duration duration time.Duration
opened bool opened bool
ip string
owns_socket bool owns_socket bool
} }
@ -256,14 +257,18 @@ pub fn (mut l SSLListener) accept() !&SSLConn {
opened: true opened: true
} }
// TODO: save the client's IP address somewhere (maybe add a field to SSLConn ?) ip := [16]u8{}
mut ret := C.mbedtls_net_accept(&l.server_fd, &conn.server_fd, unsafe { nil }, 0, iplen := usize(0)
unsafe { nil })
mut ret := C.mbedtls_net_accept(&l.server_fd, &conn.server_fd, &ip, 16, &iplen)
if ret != 0 { if ret != 0 {
return error_with_code("can't accept connection", ret) return error_with_code("can't accept connection", ret)
} }
conn.handle = conn.server_fd.fd conn.handle = conn.server_fd.fd
conn.owns_socket = true conn.owns_socket = true
if iplen == 4 {
conn.ip = '${ip[0]}.${ip[1]}.${ip[2]}.${ip[3]}'
}
C.mbedtls_ssl_init(&conn.ssl) C.mbedtls_ssl_init(&conn.ssl)
C.mbedtls_ssl_config_init(&conn.conf) C.mbedtls_ssl_config_init(&conn.conf)
@ -279,6 +284,11 @@ pub fn (mut l SSLListener) accept() !&SSLConn {
ret = C.mbedtls_ssl_handshake(&conn.ssl) ret = C.mbedtls_ssl_handshake(&conn.ssl)
for ret != 0 { for ret != 0 {
if ret != C.MBEDTLS_ERR_SSL_WANT_READ && ret != C.MBEDTLS_ERR_SSL_WANT_WRITE { if ret != C.MBEDTLS_ERR_SSL_WANT_READ && ret != C.MBEDTLS_ERR_SSL_WANT_WRITE {
conn.shutdown() or {
$if trace_ssl ? {
eprintln('${@METHOD} shutdown ---> res: ${err}')
}
}
return error_with_code('SSL handshake failed', ret) return error_with_code('SSL handshake failed', ret)
} }
ret = C.mbedtls_ssl_handshake(&conn.ssl) ret = C.mbedtls_ssl_handshake(&conn.ssl)