From 322e1c4dc8b3c06dd9bacb8a8206dd325abfc43f Mon Sep 17 00:00:00 2001 From: JalonSolov Date: Sun, 5 Nov 2023 11:57:10 -0500 Subject: [PATCH] flag,json,net: handle C calls in .v files --- cmd/tools/modules/testing/common.v | 4 +-- cmd/tools/vtest-self.v | 2 +- vlib/flag/usage_example_test.v | 7 ++-- .../{cjson_wrapper.v => cjson_wrapper.c.v} | 0 ...{json_primitives.v => json_primitives.c.v} | 0 vlib/net/{address.v => address.c.v} | 0 vlib/net/{address_test.v => address_test.c.v} | 0 vlib/net/{common.v => common.c.v} | 0 vlib/net/{errors.v => errors.c.v} | 0 vlib/net/mbedtls/{c.v => mbedtls.c.v} | 0 .../{ssl_connection.v => ssl_connection.c.v} | 0 vlib/net/openssl/{c.v => openssl.c.v} | 34 ++++++++++++++++++ vlib/net/openssl/openssl.v | 35 ------------------- ...piles_test.v => openssl_compiles_test.c.v} | 0 .../{ssl_connection.v => ssl_connection.c.v} | 0 vlib/net/{tcp.v => tcp.c.v} | 0 .../{tcp_read_line.v => tcp_read_line.c.v} | 0 vlib/net/{udp.v => udp.c.v} | 0 vlib/net/unix/{common.v => common.c.v} | 0 .../net/unix/{stream_nix.v => stream_nix.c.v} | 0 vlib/net/websocket/websocket_client.v | 18 +++++----- 21 files changed, 50 insertions(+), 50 deletions(-) rename vlib/json/cjson/{cjson_wrapper.v => cjson_wrapper.c.v} (100%) rename vlib/json/{json_primitives.v => json_primitives.c.v} (100%) rename vlib/net/{address.v => address.c.v} (100%) rename vlib/net/{address_test.v => address_test.c.v} (100%) rename vlib/net/{common.v => common.c.v} (100%) rename vlib/net/{errors.v => errors.c.v} (100%) rename vlib/net/mbedtls/{c.v => mbedtls.c.v} (100%) rename vlib/net/mbedtls/{ssl_connection.v => ssl_connection.c.v} (100%) rename vlib/net/openssl/{c.v => openssl.c.v} (75%) delete mode 100644 vlib/net/openssl/openssl.v rename vlib/net/openssl/{openssl_compiles_test.v => openssl_compiles_test.c.v} (100%) rename vlib/net/openssl/{ssl_connection.v => ssl_connection.c.v} (100%) rename vlib/net/{tcp.v => tcp.c.v} (100%) rename vlib/net/{tcp_read_line.v => tcp_read_line.c.v} (100%) rename vlib/net/{udp.v => udp.c.v} (100%) rename vlib/net/unix/{common.v => common.c.v} (100%) rename vlib/net/unix/{stream_nix.v => stream_nix.c.v} (100%) diff --git a/cmd/tools/modules/testing/common.v b/cmd/tools/modules/testing/common.v index 0ffa3b3599c07e..585c8b1a2c417f 100644 --- a/cmd/tools/modules/testing/common.v +++ b/cmd/tools/modules/testing/common.v @@ -239,11 +239,11 @@ pub fn new_test_session(_vargs string, will_compile bool) TestSession { skip_files << 'examples/macos_tray/tray.v' } if testing.github_job == 'ubuntu-docker-musl' { - skip_files << 'vlib/net/openssl/openssl_compiles_test.v' + skip_files << 'vlib/net/openssl/openssl_compiles_test.c.v' skip_files << 'vlib/x/ttf/ttf_test.v' } if testing.github_job == 'tests-sanitize-memory-clang' { - skip_files << 'vlib/net/openssl/openssl_compiles_test.v' + skip_files << 'vlib/net/openssl/openssl_compiles_test.c.v' } if testing.github_job != 'misc-tooling' { // These examples need .h files that are produced from the supplied .glsl files, diff --git a/cmd/tools/vtest-self.v b/cmd/tools/vtest-self.v index 407076f5dea70d..83d2d53bc31f75 100644 --- a/cmd/tools/vtest-self.v +++ b/cmd/tools/vtest-self.v @@ -271,7 +271,7 @@ const ( 'vlib/net/unix/unix_test.v', 'vlib/net/unix/use_net_and_net_unix_together_test.v', 'vlib/net/websocket/websocket_test.v', - 'vlib/net/openssl/openssl_compiles_test.v', + 'vlib/net/openssl/openssl_compiles_test.c.v', 'vlib/net/http/request_test.v', 'vlib/net/smtp/smtp_test.v', 'vlib/net/ssl/ssl_compiles_test.v', diff --git a/vlib/flag/usage_example_test.v b/vlib/flag/usage_example_test.v index 4e7651d7a4468e..a28890db4a6dae 100644 --- a/vlib/flag/usage_example_test.v +++ b/vlib/flag/usage_example_test.v @@ -10,9 +10,10 @@ fn testsuite_begin() { res := os.execute('${os.quoted_path(@VEXE)} -o ${os.quoted_path(the_executable)} ${os.quoted_path(the_source)}') assert res.exit_code == 0 assert os.execute(os.quoted_path(the_executable)).exit_code == 0 - C.atexit(fn () { - os.rm(the_executable) or {} - }) +} + +fn testsuite_end() { + os.rm(the_executable) or {} } fn normalise_lines(lines []string) string { diff --git a/vlib/json/cjson/cjson_wrapper.v b/vlib/json/cjson/cjson_wrapper.c.v similarity index 100% rename from vlib/json/cjson/cjson_wrapper.v rename to vlib/json/cjson/cjson_wrapper.c.v diff --git a/vlib/json/json_primitives.v b/vlib/json/json_primitives.c.v similarity index 100% rename from vlib/json/json_primitives.v rename to vlib/json/json_primitives.c.v diff --git a/vlib/net/address.v b/vlib/net/address.c.v similarity index 100% rename from vlib/net/address.v rename to vlib/net/address.c.v diff --git a/vlib/net/address_test.v b/vlib/net/address_test.c.v similarity index 100% rename from vlib/net/address_test.v rename to vlib/net/address_test.c.v diff --git a/vlib/net/common.v b/vlib/net/common.c.v similarity index 100% rename from vlib/net/common.v rename to vlib/net/common.c.v diff --git a/vlib/net/errors.v b/vlib/net/errors.c.v similarity index 100% rename from vlib/net/errors.v rename to vlib/net/errors.c.v diff --git a/vlib/net/mbedtls/c.v b/vlib/net/mbedtls/mbedtls.c.v similarity index 100% rename from vlib/net/mbedtls/c.v rename to vlib/net/mbedtls/mbedtls.c.v diff --git a/vlib/net/mbedtls/ssl_connection.v b/vlib/net/mbedtls/ssl_connection.c.v similarity index 100% rename from vlib/net/mbedtls/ssl_connection.v rename to vlib/net/mbedtls/ssl_connection.c.v diff --git a/vlib/net/openssl/c.v b/vlib/net/openssl/openssl.c.v similarity index 75% rename from vlib/net/openssl/c.v rename to vlib/net/openssl/openssl.c.v index 577ff2b02c670f..3b91918892bbce 100644 --- a/vlib/net/openssl/c.v +++ b/vlib/net/openssl/openssl.c.v @@ -154,3 +154,37 @@ fn init() { pub const ( is_used = 1 ) + +// ssl_error returns non error ssl code or error if unrecoverable and we should panic +fn ssl_error(ret int, ssl voidptr) !SSLError { + res := C.SSL_get_error(ssl, ret) + $if trace_ssl ? { + eprintln('${@METHOD} ret: ${ret} | ssl: ${ssl:x} | res: ${res}') + } + match unsafe { SSLError(res) } { + .ssl_error_syscall { + return error_with_code('unrecoverable syscall (${res})', res) + } + .ssl_error_ssl { + return error_with_code('unrecoverable ssl protocol error (${res})', res) + } + else { + return unsafe { SSLError(res) } + } + } +} + +enum SSLError { + ssl_error_none = 0 // SSL_ERROR_NONE + ssl_error_ssl = 1 // SSL_ERROR_SSL + ssl_error_want_read = 2 // SSL_ERROR_WANT_READ + ssl_error_want_write = 3 // SSL_ERROR_WANT_WRITE + ssl_error_want_x509_lookup = 4 // SSL_ERROR_WANT_X509_LOOKUP + ssl_error_syscall = 5 // SSL_ERROR_SYSCALL + ssl_error_zero_return = 6 // SSL_ERROR_ZERO_RETURN + ssl_error_want_connect = 7 // SSL_ERROR_WANT_CONNECT + ssl_error_want_accept = 8 // SSL_ERROR_WANT_ACCEPT + ssl_error_want_async = 9 // SSL_ERROR_WANT_ASYNC + ssl_error_want_async_job = 10 // SSL_ERROR_WANT_ASYNC_JOB + ssl_error_want_early = 11 // SSL_ERROR_WANT_EARLY +} diff --git a/vlib/net/openssl/openssl.v b/vlib/net/openssl/openssl.v deleted file mode 100644 index 03eb553ba71c52..00000000000000 --- a/vlib/net/openssl/openssl.v +++ /dev/null @@ -1,35 +0,0 @@ -module openssl - -// ssl_error returns non error ssl code or error if unrecoverable and we should panic -fn ssl_error(ret int, ssl voidptr) !SSLError { - res := C.SSL_get_error(ssl, ret) - $if trace_ssl ? { - eprintln('${@METHOD} ret: ${ret} | ssl: ${ssl:x} | res: ${res}') - } - match unsafe { SSLError(res) } { - .ssl_error_syscall { - return error_with_code('unrecoverable syscall (${res})', res) - } - .ssl_error_ssl { - return error_with_code('unrecoverable ssl protocol error (${res})', res) - } - else { - return unsafe { SSLError(res) } - } - } -} - -enum SSLError { - ssl_error_none = 0 // SSL_ERROR_NONE - ssl_error_ssl = 1 // SSL_ERROR_SSL - ssl_error_want_read = 2 // SSL_ERROR_WANT_READ - ssl_error_want_write = 3 // SSL_ERROR_WANT_WRITE - ssl_error_want_x509_lookup = 4 // SSL_ERROR_WANT_X509_LOOKUP - ssl_error_syscall = 5 // SSL_ERROR_SYSCALL - ssl_error_zero_return = 6 // SSL_ERROR_ZERO_RETURN - ssl_error_want_connect = 7 // SSL_ERROR_WANT_CONNECT - ssl_error_want_accept = 8 // SSL_ERROR_WANT_ACCEPT - ssl_error_want_async = 9 // SSL_ERROR_WANT_ASYNC - ssl_error_want_async_job = 10 // SSL_ERROR_WANT_ASYNC_JOB - ssl_error_want_early = 11 // SSL_ERROR_WANT_EARLY -} diff --git a/vlib/net/openssl/openssl_compiles_test.v b/vlib/net/openssl/openssl_compiles_test.c.v similarity index 100% rename from vlib/net/openssl/openssl_compiles_test.v rename to vlib/net/openssl/openssl_compiles_test.c.v diff --git a/vlib/net/openssl/ssl_connection.v b/vlib/net/openssl/ssl_connection.c.v similarity index 100% rename from vlib/net/openssl/ssl_connection.v rename to vlib/net/openssl/ssl_connection.c.v diff --git a/vlib/net/tcp.v b/vlib/net/tcp.c.v similarity index 100% rename from vlib/net/tcp.v rename to vlib/net/tcp.c.v diff --git a/vlib/net/tcp_read_line.v b/vlib/net/tcp_read_line.c.v similarity index 100% rename from vlib/net/tcp_read_line.v rename to vlib/net/tcp_read_line.c.v diff --git a/vlib/net/udp.v b/vlib/net/udp.c.v similarity index 100% rename from vlib/net/udp.v rename to vlib/net/udp.c.v diff --git a/vlib/net/unix/common.v b/vlib/net/unix/common.c.v similarity index 100% rename from vlib/net/unix/common.v rename to vlib/net/unix/common.c.v diff --git a/vlib/net/unix/stream_nix.v b/vlib/net/unix/stream_nix.c.v similarity index 100% rename from vlib/net/unix/stream_nix.v rename to vlib/net/unix/stream_nix.c.v diff --git a/vlib/net/websocket/websocket_client.v b/vlib/net/websocket/websocket_client.v index 649a8a0faf8796..f0f585e31324f7 100644 --- a/vlib/net/websocket/websocket_client.v +++ b/vlib/net/websocket/websocket_client.v @@ -264,11 +264,11 @@ pub fn (mut ws Client) write_ptr(bytes &u8, payload_len int, code OPCode) !int { } else if payload_len > 125 && payload_len <= 0xffff { len16 := conv.hton16(u16(payload_len)) header[1] = 126 - unsafe { C.memcpy(&header[2], &len16, 2) } + unsafe { vmemcpy(&header[2], &len16, 2) } } else if payload_len > 0xffff && payload_len <= 0x7fffffff { len_bytes := htonl64(u64(payload_len)) header[1] = 127 - unsafe { C.memcpy(&header[2], len_bytes.data, 8) } + unsafe { vmemcpy(&header[2], len_bytes.data, 8) } } } else { if payload_len <= 125 { @@ -280,7 +280,7 @@ pub fn (mut ws Client) write_ptr(bytes &u8, payload_len int, code OPCode) !int { } else if payload_len > 125 && payload_len <= 0xffff { len16 := conv.hton16(u16(payload_len)) header[1] = (126 | 0x80) - unsafe { C.memcpy(&header[2], &len16, 2) } + unsafe { vmemcpy(&header[2], &len16, 2) } header[4] = masking_key[0] header[5] = masking_key[1] header[6] = masking_key[2] @@ -288,7 +288,7 @@ pub fn (mut ws Client) write_ptr(bytes &u8, payload_len int, code OPCode) !int { } else if payload_len > 0xffff && payload_len <= 0x7fffffff { len64 := htonl64(u64(payload_len)) header[1] = (127 | 0x80) - unsafe { C.memcpy(&header[2], len64.data, 8) } + unsafe { vmemcpy(&header[2], len64.data, 8) } header[10] = masking_key[0] header[11] = masking_key[1] header[12] = masking_key[2] @@ -301,9 +301,9 @@ pub fn (mut ws Client) write_ptr(bytes &u8, payload_len int, code OPCode) !int { len := header.len + payload_len mut frame_buf := []u8{len: len} unsafe { - C.memcpy(&frame_buf[0], &u8(header.data), header.len) + vmemcpy(&frame_buf[0], &u8(header.data), header.len) if payload_len > 0 { - C.memcpy(&frame_buf[header.len], bytes, payload_len) + vmemcpy(&frame_buf[header.len], bytes, payload_len) } } if !ws.is_server { @@ -396,14 +396,14 @@ fn (mut ws Client) send_control_frame(code OPCode, frame_typ string, payload []u if payload.len >= 2 { if !ws.is_server { mut parsed_payload := []u8{len: payload.len + 1} - unsafe { C.memcpy(parsed_payload.data, &payload[0], payload.len) } + unsafe { vmemcpy(parsed_payload.data, &payload[0], payload.len) } parsed_payload[payload.len] = `\0` for i in 0 .. payload.len { control_frame[6 + i] = (parsed_payload[i] ^ masking_key[i % 4]) & 0xff } unsafe { parsed_payload.free() } } else { - unsafe { C.memcpy(&control_frame[2], &payload[0], payload.len) } + unsafe { vmemcpy(&control_frame[2], &payload[0], payload.len) } } } } else { @@ -415,7 +415,7 @@ fn (mut ws Client) send_control_frame(code OPCode, frame_typ string, payload []u } } else { if payload.len > 0 { - unsafe { C.memcpy(&control_frame[2], &payload[0], payload.len) } + unsafe { vmemcpy(&control_frame[2], &payload[0], payload.len) } } } }