Skip to content

Commit

Permalink
flag,json,net: handle C calls in .v files
Browse files Browse the repository at this point in the history
  • Loading branch information
JalonSolov committed Nov 5, 2023
1 parent 9ec8807 commit 322e1c4
Show file tree
Hide file tree
Showing 21 changed files with 50 additions and 50 deletions.
4 changes: 2 additions & 2 deletions cmd/tools/modules/testing/common.v
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion cmd/tools/vtest-self.v
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
7 changes: 4 additions & 3 deletions vlib/flag/usage_example_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
34 changes: 34 additions & 0 deletions vlib/net/openssl/c.v → vlib/net/openssl/openssl.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
35 changes: 0 additions & 35 deletions vlib/net/openssl/openssl.v

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 9 additions & 9 deletions vlib/net/websocket/websocket_client.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -280,15 +280,15 @@ 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]
header[7] = masking_key[3]
} 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]
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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) }
}
}
}
Expand Down

0 comments on commit 322e1c4

Please sign in to comment.