hare

The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit d7256d34cde6066a1e897d02f2b1da9137333650
parent 7aecedfe04ffd70232b5a2eb9b3815966dc0a9ef
Author: Mykyta Holubakha <hilobakho@gmail.com>
Date:   Sat, 13 Mar 2021 16:33:11 +0200

rt: connect, bind, listen and accept syscalls

Added necessary sockaddr definitions for IP, IPv6 and unix sockets

Diffstat:
Mrt/+linux/socket.ha | 42++++++++++++++++++++++++++++++++++++++++++
Mrt/+linux/syscalls.ha | 20++++++++++++++++++++
2 files changed, 62 insertions(+), 0 deletions(-)

diff --git a/rt/+linux/socket.ha b/rt/+linux/socket.ha @@ -1,3 +1,45 @@ +export type sa_family_t = u16; + +export type in_addr = struct { + s_addr: u32 +}; + +export type sockaddr_in = struct { + sin_family: sa_family_t, + sin_port: u16, + sin_addr: in_addr, + __pad: [16]char, +}; + +export type in6_addr = struct { + union { + s6_addr: [16]u8, + s6_addr16: [8]u16, + s6_addr32: [4]u32, + } +}; + +export type sockaddr_in6 = struct { + sin6_family: sa_family_t, + sin6_port: u16, + sin6_flowinfo: u32, + sin6_addr: in_addr, + sin6_scope_id: u32, +}; + +export def UNIX_PATH_MAX: int = 108; + +export type sockaddr_un = struct { + sin_family: sa_family_t, + sun_path: [UNIX_PATH_MAX]char, +}; + +export type sockaddr = union { + in: sockaddr_in, + in6: sockaddr_in6, + un: sockaddr_un, +}; + // domain for socket(2) export def AF_UNSPEC: u16 = 0; export def AF_UNIX: u16 = 1; // Unix domain sockets diff --git a/rt/+linux/syscalls.ha b/rt/+linux/syscalls.ha @@ -366,3 +366,23 @@ export fn socket(domain: int, type_: int, protocol: int) (int | errno) = { return wrap_return(syscall3(SYS_socket, domain: u64, type_: u64, protocol: u64))?: int; }; + +export fn connect(sockfd: int, addr: *const sockaddr, addrlen: u32) (int | errno) = { + return wrap_return(syscall3(SYS_connect, + sockfd: u64, addr: uintptr: u64, addrlen: u64))?: int; +}; + +export fn bind(sockfd: int, addr: *const sockaddr, addrlen: u32) (int | errno) = { + return wrap_return(syscall3(SYS_bind, + sockfd: u64, addr: uintptr: u64, addrlen: u64))?: int; +}; + +export fn listen(sockfd: int, backlog: u32) (int | errno) = { + return wrap_return(syscall2(SYS_listen, + sockfd: u64, backlog: u64))?: int; +}; + +export fn accept(sockfd: int, addr: nullable *sockaddr, addrlen: nullable *u32) (int | errno) = { + return wrap_return(syscall3(SYS_accept, + sockfd: u64, addr: uintptr: u64, addrlen: uintptr: u64))?: int; +};