hare

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

+freebsd.ha (1326B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use endian;
      5 use rt;
      6 
      7 export fn to_native(a: addr, port: u16) rt::sockaddr = {
      8 	match (a) {
      9 	case let v4: addr4 =>
     10 		return rt::sockaddr {
     11 			in = rt::sockaddr_in {
     12 				sin_len = size(rt::in_addr): u8,
     13 				sin_family = rt::AF_INET,
     14 				sin_port = endian::htonu16(port),
     15 				sin_addr = rt::in_addr { s_addr = *(&v4[0]: *opaque: *u32) },
     16 				...
     17 			},
     18 			...
     19 		};
     20 	case let v6: addr6 =>
     21 		return rt::sockaddr {
     22 			in6 = rt::sockaddr_in6 {
     23 				sin6_len = size(rt::in6_addr): u8,
     24 				sin6_family = rt::AF_INET6,
     25 				sin6_port = endian::htonu16(port),
     26 				sin6_addr = rt::in6_addr { s6_addr = v6 },
     27 				...
     28 			},
     29 			...
     30 		};
     31 	};
     32 };
     33 
     34 export fn from_native(a: rt::sockaddr) (addr, u16)  = {
     35 	let family = a.in.sin_family;
     36 	switch (family) {
     37 	case rt::AF_INET =>
     38 		let addr = a.in.sin_addr.s_addr;
     39 		return (
     40 			[addr: u8, (addr >> 8): u8, (addr >> 16): u8,
     41 				(addr >> 24):  u8]: addr4,
     42 			endian::ntohu16(a.in.sin_port)
     43 		);
     44 	case rt::AF_INET6 =>
     45 		return (
     46 			a.in6.sin6_addr.s6_addr: addr6,
     47 			endian::ntohu16(a.in6.sin6_port)
     48 		);
     49 	case =>
     50 		abort("Wrong address family!");
     51 	};
     52 };
     53 
     54 export fn native_addrlen(a: addr) u32 = {
     55 	match (a) {
     56 	case addr4 =>
     57 		return size(rt::sockaddr_in): u32;
     58 	case addr6 =>
     59 		return size(rt::sockaddr_in6): u32;
     60 	};
     61 };