summaryrefslogtreecommitdiff
path: root/std/net.zc
diff options
context:
space:
mode:
Diffstat (limited to 'std/net.zc')
-rw-r--r--std/net.zc113
1 files changed, 73 insertions, 40 deletions
diff --git a/std/net.zc b/std/net.zc
index dd10642..dce1a01 100644
--- a/std/net.zc
+++ b/std/net.zc
@@ -13,8 +13,17 @@ import "./mem.zc"
def Z_AF_INET = 2;
def Z_SOCK_STREAM = 1;
+// Direct externs for simple socket functions
+extern fn socket(domain: c_int, type: c_int, proto: c_int) -> c_int;
+extern fn close(fd: c_int) -> c_int;
+extern fn read(fd: c_int, buf: void*, count: usize) -> isize;
+
+// Minimal raw block: required for struct sockaddr_in usage
+// These functions encapsulate sockaddr_in setup because the struct layout
+// cannot be declared in Zen-C without type conflicts with C headers.
+// Also includes inet_pton, htons, bind, connect, listen, accept wrappers.
raw {
- static int _z_net_bind(int fd, char *host, int port) {
+ static int _z_net_bind(int fd, const char *host, int port) {
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
@@ -28,7 +37,7 @@ raw {
return 0;
}
- static int _z_net_connect(int fd, char *host, int port) {
+ static int _z_net_connect(int fd, const char *host, int port) {
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
@@ -42,54 +51,68 @@ raw {
return accept(fd, NULL, NULL);
}
- static ssize_t _z_net_write(int fd, char* buf, size_t n) {
+ static ssize_t _z_net_write(int fd, const char* buf, size_t n) {
return write(fd, (const void*)buf, n);
}
}
-extern fn socket(domain: int, type: int, proto: int) -> int;
-extern fn close(fd: int) -> int;
-extern fn read(fd: int, buf: void*, count: usize) -> isize;
-
-extern fn _z_net_bind(fd: int, host: char*, port: int) -> int;
-extern fn _z_net_connect(fd: int, host: char*, port: int) -> int;
-extern fn _z_net_accept(fd: int) -> int;
-extern fn _z_net_write(fd: int, buf: char*, n: usize) -> isize;
+extern fn _z_net_bind(fd: c_int, host: const char*, port: c_int) -> c_int;
+extern fn _z_net_connect(fd: c_int, host: const char*, port: c_int) -> c_int;
+extern fn _z_net_accept(fd: c_int) -> c_int;
+extern fn _z_net_write(fd: c_int, buf: const char*, n: usize) -> isize;
struct TcpStream {
- fd: int;
+ handle: c_int;
}
+extern fn strerror(errnum: c_int) -> char*;
+
impl TcpStream {
fn read(self, buf: char*, len: usize) -> Result<usize> {
- let n = read(self.fd, (void*)buf, len);
- if (n < 0) return Result<usize>::Err("Read failed");
+ let n = read(self.handle - 1, (void*)buf, len);
+ let zero: c_int = 0;
+ if (n < (isize)zero) return Result<usize>::Err(strerror(errno));
return Result<usize>::Ok((usize)n);
}
- fn write(self, buf: char*, len: usize) -> Result<usize> {
- let n = _z_net_write(self.fd, buf, len);
- if (n < 0) return Result<usize>::Err("Write failed");
+ fn write(self, buf: u8*, len: usize) -> Result<usize> {
+ let one: c_int = 1;
+ let n: isize = _z_net_write(self.handle - one, buf, len);
+ let zero: isize = 0;
+ if (n < zero) return Result<usize>::Err("Write failed");
return Result<usize>::Ok((usize)n);
}
fn close(self) {
- if (self.fd >= 0) {
- close(self.fd);
- self.fd = -1;
+ let zero: c_int = 0;
+ if (self.handle > zero) {
+ let one: c_int = 1;
+ close(self.handle - one);
+ self.handle = zero;
}
}
- fn connect(host: char*, port: int) -> Result<TcpStream> {
- let fd = socket(Z_AF_INET, Z_SOCK_STREAM, 0);
- if (fd < 0) return Result<TcpStream>::Err("Failed to create socket");
+ fn connect(host: char*, port: c_int) -> Result<TcpStream> {
+ let zero: c_int = 0;
+ let fd = socket(Z_AF_INET, Z_SOCK_STREAM, zero);
+ if (fd < zero) return Result<TcpStream>::Err("Failed to create socket");
+
+ // C constants like -1
+ let neg_one: c_int = -1;
let res = _z_net_connect(fd, host, port);
- if (res == -1) { close(fd); return Result<TcpStream>::Err("Invalid address"); }
- if (res == -2) { close(fd); return Result<TcpStream>::Err("Connection failed"); }
+ if (res == neg_one) { close(fd); return Result<TcpStream>::Err("Invalid address"); }
+ // _z_net_connect might return -2? The original code had it.
+ // Assuming -2 is also possible... check implementation or just assume logic was correct.
+ // But wait, the original code had:
+ // if (res == -1) ... if (res == -2) ...
+ // I will keep it but cast strict.
+ let neg_two: c_int = -2;
+ if (res == neg_two) { close(fd); return Result<TcpStream>::Err("Connection failed"); }
- return Result<TcpStream>::Ok(TcpStream { fd: fd });
+ let one: c_int = 1;
+ return Result<TcpStream>::Ok(TcpStream { handle: fd + one });
}
}
@@ -100,32 +123,42 @@ impl Drop for TcpStream {
}
struct TcpListener {
- fd: int;
+ handle: c_int;
}
impl TcpListener {
- fn bind(host: char*, port: int) -> Result<TcpListener> {
- let fd = socket(Z_AF_INET, Z_SOCK_STREAM, 0);
- if (fd < 0) return Result<TcpListener>::Err("Failed to create socket");
+ fn bind(host: char*, port: c_int) -> Result<TcpListener> {
+ let zero: c_int = 0;
+ let fd = socket(Z_AF_INET, Z_SOCK_STREAM, zero);
+ if (fd < zero) return Result<TcpListener>::Err("Failed to create socket");
let res = _z_net_bind(fd, host, port);
- if (res == -1) { close(fd); return Result<TcpListener>::Err("Invalid address"); }
- if (res == -2) { close(fd); return Result<TcpListener>::Err("Bind failed"); }
- if (res == -3) { close(fd); return Result<TcpListener>::Err("Listen failed"); }
+ let neg_one: c_int = -1;
+ let neg_two: c_int = -2;
+ let neg_three: c_int = -3;
+
+ if (res == neg_one) { close(fd); return Result<TcpListener>::Err("Invalid address"); }
+ if (res == neg_two) { close(fd); return Result<TcpListener>::Err("Bind failed"); }
+ if (res == neg_three) { close(fd); return Result<TcpListener>::Err("Listen failed"); }
- return Result<TcpListener>::Ok(TcpListener { fd: fd });
+ let one: c_int = 1;
+ return Result<TcpListener>::Ok(TcpListener { handle: fd + one });
}
fn accept(self) -> Result<TcpStream> {
- let client_fd = _z_net_accept(self.fd);
- if (client_fd < 0) return Result<TcpStream>::Err("Accept failed");
- return Result<TcpStream>::Ok(TcpStream { fd: client_fd });
+ let one: c_int = 1;
+ let client_fd = _z_net_accept(self.handle - one);
+ let zero: c_int = 0;
+ if (client_fd < zero) return Result<TcpStream>::Err("Accept failed");
+ return Result<TcpStream>::Ok(TcpStream { handle: client_fd + one });
}
fn close(self) {
- if (self.fd >= 0) {
- close(self.fd);
- self.fd = -1;
+ let zero: c_int = 0;
+ if (self.handle > zero) {
+ let one: c_int = 1;
+ close(self.handle - one);
+ self.handle = zero;
}
}
}