diff --git a/lib/net/_linux_arm.q b/lib/net/_linux_arm.q index 8270ad5..cfadbe0 100644 --- a/lib/net/_linux_arm.q +++ b/lib/net/_linux_arm.q @@ -1,6 +1,7 @@ const { _close = 57 _socket = 198 + _connect = 203 _accept = 202 _bind = 200 _listen = 201 diff --git a/lib/net/_linux_x86.q b/lib/net/_linux_x86.q index 1a535f3..a9fa29f 100644 --- a/lib/net/_linux_x86.q +++ b/lib/net/_linux_x86.q @@ -1,6 +1,7 @@ const { _close = 3 _socket = 41 + _connect = 42 _accept = 43 _bind = 49 _listen = 50 diff --git a/lib/net/_mac_arm.q b/lib/net/_mac_arm.q index 64b72b3..7a5dc6d 100644 --- a/lib/net/_mac_arm.q +++ b/lib/net/_mac_arm.q @@ -1,6 +1,7 @@ const { _close = 0x6 _socket = 0x61 + _connect = 0x62 _accept = 0x1E _bind = 0x68 _listen = 0x6A diff --git a/lib/net/_mac_x86.q b/lib/net/_mac_x86.q index d427b29..142ec13 100644 --- a/lib/net/_mac_x86.q +++ b/lib/net/_mac_x86.q @@ -1,6 +1,7 @@ const { _close = 0x2000006 _socket = 0x2000061 + _connect = 0x2000062 _accept = 0x200001E _bind = 0x2000068 _listen = 0x200006A diff --git a/lib/net/net_unix.q b/lib/net/net_unix.q index aebbbae..afbf840 100644 --- a/lib/net/net_unix.q +++ b/lib/net/net_unix.q @@ -14,6 +14,10 @@ close(fd int) -> error { return syscall(_close, fd) } +connect(fd int, address *any, length uint) -> error { + return syscall(_connect, fd, address, length) +} + listen(fd int, backlog int) -> error { return syscall(_listen, fd, backlog) } diff --git a/lib/net/net_windows.q b/lib/net/net_windows.q index 8f9089d..099904c 100755 --- a/lib/net/net_windows.q +++ b/lib/net/net_windows.q @@ -24,6 +24,10 @@ close(fd int) -> error { return 0 } +connect(fd int, address *any, length uint) -> error { + return ws2_32.connect(fd, address, length as int) +} + listen(fd int, backlog int) -> error { return ws2_32.listen(fd, backlog) } @@ -63,6 +67,7 @@ extern { accept(fd int, address *any|nil, length int) -> int bind(socket int, address *any, length int) -> (error int) closesocket(fd int) -> int + connect(fd int, address *any, length int) -> int listen(fd int, backlog int) -> int recv(fd int, address *byte|nil, length int, flags int) -> int socket(family int, type int, protocol int) -> int diff --git a/lib/unix/_linux.q b/lib/unix/_linux.q new file mode 100644 index 0000000..a34cf57 --- /dev/null +++ b/lib/unix/_linux.q @@ -0,0 +1,5 @@ +const { + AF_UNIX = 1 + SOCK_STREAM = 1 + maxPathLength = 107 +} \ No newline at end of file diff --git a/lib/unix/_mac.q b/lib/unix/_mac.q new file mode 100644 index 0000000..0494176 --- /dev/null +++ b/lib/unix/_mac.q @@ -0,0 +1,5 @@ +const { + AF_UNIX = 1 + SOCK_STREAM = 1 + maxPathLength = 103 +} \ No newline at end of file diff --git a/lib/unix/address_linux.q b/lib/unix/address_linux.q new file mode 100644 index 0000000..3cb264c --- /dev/null +++ b/lib/unix/address_linux.q @@ -0,0 +1,8 @@ +import mem + +address(path string) -> !string { + addr := mem.alloc(2 + path.len + 1) + addr[0] = AF_UNIX + mem.copy(addr[2..addr.len-1], path) + return addr +} \ No newline at end of file diff --git a/lib/unix/address_mac.q b/lib/unix/address_mac.q new file mode 100644 index 0000000..2348657 --- /dev/null +++ b/lib/unix/address_mac.q @@ -0,0 +1,9 @@ +import mem + +address(path string) -> !string { + addr := mem.alloc(2 + path.len + 1) + addr[0] = addr.len as byte + addr[1] = AF_UNIX + mem.copy(addr[2..addr.len-1], path) + return addr +} \ No newline at end of file diff --git a/lib/unix/connect_unix.q b/lib/unix/connect_unix.q new file mode 100644 index 0000000..6b8f814 --- /dev/null +++ b/lib/unix/connect_unix.q @@ -0,0 +1,18 @@ +import net + +connect(path string) -> (int, error) { + if path.len > maxPathLength { + return 0, -1 + } + + s, err := net.socket(AF_UNIX, SOCK_STREAM, 0) + + if err != 0 { + return 0, err + } + + addr := address(path) + err := net.connect(s, addr.ptr, addr.len) + delete(addr) + return s, err +} \ No newline at end of file