hare

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

+linux.ha (1082B)


      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_family = rt::AF_INET,
     13 				sin_port = endian::htonu16(port),
     14 				sin_addr = rt::in_addr { s_addr = *(&v4[0]: *opaque: *u32) },
     15 				...
     16 			},
     17 			...
     18 		};
     19 	case let v6: addr6 =>
     20 		return rt::sockaddr {
     21 			in6 = rt::sockaddr_in6 {
     22 				sin6_family = rt::AF_INET6,
     23 				sin6_port = endian::htonu16(port),
     24 				sin6_addr = rt::in6_addr { s6_addr = v6 },
     25 				...
     26 			},
     27 			...
     28 		};
     29 	};
     30 };
     31 
     32 export fn from_native(a: rt::sockaddr) (addr, u16)  = {
     33 	let family = a.in.sin_family;
     34 	switch (family) {
     35 	case rt::AF_INET =>
     36 		let addr = a.in.sin_addr.s_addr;
     37 		return (
     38 			[addr: u8, (addr >> 8): u8, (addr >> 16): u8,
     39 				(addr >> 24):  u8]: addr4,
     40 			endian::ntohu16(a.in.sin_port)
     41 		);
     42 	case rt::AF_INET6 =>
     43 		return (
     44 			a.in6.sin6_addr.s6_addr: addr6,
     45 			endian::ntohu16(a.in6.sin6_port)
     46 		);
     47 	case =>
     48 		abort("Wrong address family!");
     49 	};
     50 };