From 10c628ed17054b017aeaced466d343b973b5e83b Mon Sep 17 00:00:00 2001 From: Francisco Llobet Date: Sun, 11 Feb 2024 00:05:46 +0100 Subject: [PATCH] Making it compilable --- src/connection.zig | 49 +++++++------------------------------ src/http.zig | 4 ++-- src/mbedtls.zig | 52 +++++++++++++++++++--------------------- src/simpleConnection.zig | 12 +++++++++- 4 files changed, 46 insertions(+), 71 deletions(-) diff --git a/src/connection.zig b/src/connection.zig index a1fcd40..43461d7 100644 --- a/src/connection.zig +++ b/src/connection.zig @@ -153,6 +153,7 @@ pub const schemes = enum(u32) { }; pub fn Connection(comptime id: connection_id, comptime sslType: type) type { + _ = id; // Compile time checks if (sslType != void) { if (!@hasDecl(sslType, "init")) { @@ -164,62 +165,28 @@ pub fn Connection(comptime id: connection_id, comptime sslType: type) type { } return struct { - ctx: network_ctx, - proto: proto, ssl: sslType, /// Initialize the connection pub fn init(self: *@This()) void { - self.ctx = c.miso_get_network_ctx(@as(c_uint, @intCast(@intFromEnum(id)))); - self.proto = .no_protocol; + _ = self; + //self.ssl.init(); } pub fn create(self: *@This(), uri: std.Uri, local_port: ?u16) !void { - self.proto = schemes.match(uri.scheme).?.getProtocol(); - - const host = uri.host.?; - const port = uri.port.?; - - if (sslType != void) { - _ = self.ssl.init(self.proto) catch { - return connection_error.ssl_init_error; - }; - _ = c.miso_network_register_ssl_context(@ptrCast(self.ctx), @ptrCast(&self.ssl.context)); - } - - if (0 != c.miso_create_network_connection(self.ctx, @as([*c]const u8, host.ptr), host.len, port, local_port orelse 0, @as(c.enum_miso_protocol, @intFromEnum(self.proto)))) { - return connection_error.create_error; - } + try self.ssl.open(uri, local_port); } pub fn close(self: *@This()) !void { - defer { - if (sslType != void) { - _ = self.ssl.deinit(); - } - } - - if (0 != c.miso_close_network_connection(self.ctx)) { - return connection_error.close_error; - } + try self.ssl.close(); } pub fn send(self: *@This(), buffer: []const u8) !usize { - const len: isize = c.miso_network_send(self.ctx, @as([*c]const u8, buffer.ptr), buffer.len); - return if (len <= 0) connection_error.send_error else @intCast(len); + return self.ssl.send(buffer); } pub fn recieve(self: *@This(), buffer: []u8) ![]u8 { - const len: isize = c.miso_network_read(self.ctx, buffer.ptr, buffer.len); - return if (len <= 0) - connection_error.recieve_error - else if (@as(usize, @intCast(len)) > buffer.len) - connection_error.buffer_owerflow - else - buffer[0..@intCast(len)]; + return self.ssl.recieve(buffer); } pub fn waitRx(self: *@This(), timeout_s: u32) i32 { - return c.wait_rx(self.ctx, timeout_s); - } - pub fn waitTx(self: *@This(), timeout_s: u32) i32 { - return c.wait_tx(self.ctx, timeout_s); + return self.waitRx(timeout_s); } }; } diff --git a/src/http.zig b/src/http.zig index af5ca3a..fd77169 100644 --- a/src/http.zig +++ b/src/http.zig @@ -5,14 +5,14 @@ const system = @import("system.zig"); const connection = @import("connection.zig"); const file = @import("fatfs.zig").file; const led = @import("leds.zig"); - +const simpleConnection = @import("simpleConnection.zig"); const c = @cImport({ @cInclude("board.h"); @cInclude("picohttpparser.h"); }); /// Connection instance -connection: connection.Connection(.http, void), +connection: connection.Connection(.http, simpleConnection.SimpleLinkConnection(.tcp_ip4)), /// Array to store parsed header information headers: [24]c.phr_header, diff --git a/src/mbedtls.zig b/src/mbedtls.zig index 957cb27..41e5477 100644 --- a/src/mbedtls.zig +++ b/src/mbedtls.zig @@ -141,27 +141,31 @@ pub fn TlsContext(comptime T: type, comptime conn: type, comptime mode: connecti try self.init(proto); errdefer { - self.deinit(); + _ = self.deinit(); } try self.connection.open(uri, local_port); } + pub fn close(self: *@This()) !void { + defer { + _ = self.deinit(); + } + try self.connection.close(); + } pub fn send(self: *@This(), buffer: []const u8) !usize { - if(self.conn.getProtocol().isTls()) - { - // - } - else { - return self.connection.send_dtls(buffer); + if (self.connection.getProto().isTls()) { + return 1; + } else { + return self.send_dtls(buffer); } } fn send_dtls(self: *@This(), buffer: []const u8) !usize { var offset: usize = 0; var cid_enabled: c_int = c.MBEDTLS_SSL_CID_DISABLED; - var ret: i32 = c.mbedtls_ssl_get_peer_cid(&self.context, &cid_enabled, &self.cid, null, 0); - if (cid_enabled == c.MBEDTLS_CID_DISABLED) { + var ret: i32 = c.mbedtls_ssl_get_peer_cid(&self.context, &cid_enabled, null, 0); + if (cid_enabled == c.MBEDTLS_SSL_CID_DISABLED) { // } else { ret = 0; @@ -171,41 +175,35 @@ pub fn TlsContext(comptime T: type, comptime conn: type, comptime mode: connecti while (offset != buffer.len) { const slice = buffer[offset..]; const num_bytes = c.mbedtls_ssl_write(&self.context, @ptrCast(slice.ptr), @intCast(slice.len)); - if ((c.MBEDTLS_ERR_SSL_WANT_READ == num_bytes) || (c.MBEDTLS_ERR_SSL_WANT_WRITE == num_bytes) || (c.MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS == num_bytes) || (c.MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS == num_bytes)) - { + if ((c.MBEDTLS_ERR_SSL_WANT_READ == num_bytes) or (c.MBEDTLS_ERR_SSL_WANT_WRITE == num_bytes) or (c.MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS == num_bytes) or (c.MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS == num_bytes)) { continue; - } - else if (num_bytes < 0) - { - ret = -1; - break; - } - else{ + } else if (num_bytes < 0) { + ret = -1; + break; + } else { offset += @as(usize, @intCast(num_bytes)); } } } - return if(ret == 0) offset else 0; + return if (ret == 0) offset else connection.connection_error.send_error; } - fn read_dlts(self: *@This(), buffer: []u8) ![]u8 { + fn read_dtls(self: *@This(), buffer: []u8) ![]u8 { var numBytes: isize = c.MBEDTLS_ERR_SSL_WANT_READ; while ((numBytes == c.MBEDTLS_ERR_SSL_WANT_READ) or (numBytes == c.MBEDTLS_ERR_SSL_WANT_WRITE) or (numBytes == c.MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS) or (numBytes == c.MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) or (numBytes == c.MBEDTLS_ERR_SSL_CLIENT_RECONNECT)) { numBytes = c.mbedtls_ssl_read(&self.context, @ptrCast(buffer.ptr), @intCast(buffer.len)); } - return if (numBytes < 0) 0 else buffer[0..@intCast(numBytes)]; + return if (numBytes < 0) connection.connection_error.recieve_error else buffer[0..@intCast(numBytes)]; } pub fn recieve(self: *@This(), buffer: []u8) ![]u8 { - if(self.conn.getProtocol().isTls()) - { - // - } - else { - return self.connection.read_dtls(buffer); + if (self.connection.getProto().isTls()) { + return buffer; + } else { + return self.read_dtls(buffer); } } /// Send data callback for MbedTLS diff --git a/src/simpleConnection.zig b/src/simpleConnection.zig index 819510f..be47b15 100644 --- a/src/simpleConnection.zig +++ b/src/simpleConnection.zig @@ -131,6 +131,16 @@ pub fn SimpleLinkConnection(comptime proto: connection.proto) type { }; } + pub fn init(self: *@This()) !void { + self.sd = @intFromEnum(sd_e.invalid); + } + pub fn deinit(self: *@This()) !void { + _ = self; + //if (self.sd != @intFromEnum(sd_e.invalid)) { + // _ = self.close(); + //} + } + /// Open a connection to designated peer pub fn open(self: *@This(), uri: std.Uri, local_port: ?u16) !void { const host = uri.host.?; @@ -195,7 +205,7 @@ pub fn SimpleLinkConnection(comptime proto: connection.proto) type { } /// Send - pub fn send(self: *@This(), data: []u8) !usize { + pub fn send(self: *@This(), data: []const u8) !usize { var ret: isize = undefined; if (comptime proto.isTcp()) { ret = c.sl_Send(self.sd, @ptrCast(data.ptr), @intCast(data.len), 0);