hare

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

commit 280d4ad0a11d1f9cac9de4788dc14970b572c168
parent 7e363afbd6cecfcb97299a9114bf4b1f49dd27cd
Author: Lorenz (xha) <me@xha.li>
Date:   Sat, 25 Nov 2023 15:18:22 +0100

OpenBSD: add net::ip

Signed-off-by: Lorenz (xha) <me@xha.li>

Diffstat:
Anet/ip/+openbsd.ha | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 61 insertions(+), 0 deletions(-)

diff --git a/net/ip/+openbsd.ha b/net/ip/+openbsd.ha @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: MPL-2.0 +// (c) Hare authors <https://harelang.org> + +use endian; +use rt; + +export fn to_native(a: addr, port: u16) rt::sockaddr = { + match (a) { + case let v4: addr4 => + return rt::sockaddr { + in = rt::sockaddr_in { + sin_len = size(rt::in_addr): u8, + sin_family = rt::AF_INET, + sin_port = endian::htonu16(port), + sin_addr = rt::in_addr { s_addr = *(&v4[0]: *u32) }, + ... + }, + ... + }; + case let v6: addr6 => + return rt::sockaddr { + in6 = rt::sockaddr_in6 { + sin6_len = size(rt::in6_addr): u8, + sin6_family = rt::AF_INET6, + sin6_port = endian::htonu16(port), + sin6_addr = rt::in6_addr { s6_addr = v6 }, + ... + }, + ... + }; + }; +}; + +export fn from_native(a: rt::sockaddr) (addr, u16) = { + let family = a.in.sin_family; + switch (family) { + case rt::AF_INET => + let addr = a.in.sin_addr.s_addr; + return ( + [addr: u8, (addr >> 8): u8, (addr >> 16): u8, + (addr >> 24): u8]: addr4, + endian::ntohu16(a.in.sin_port) + ); + case rt::AF_INET6 => + return ( + a.in6.sin6_addr.s6_addr: addr6, + endian::ntohu16(a.in6.sin6_port) + ); + case => + abort("Wrong address family!"); + }; +}; + +export fn native_addrlen(a: addr) u32 = { + match (a) { + case addr4 => + return size(rt::sockaddr_in): u32; + case addr6 => + return size(rt::sockaddr_in6): u32; + }; +};