Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/net/aienos_net.h # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / net / aienos_net.h` 145 lines ยท 5246 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* AIENOS M6-A hosted network protocol layer, in C. No Rust, no heap. 18 * 19 * Port of the Rust reference crates/aienos-kernel/src/net.rs (read as the 20 * spec, not extended). Every parser takes (pointer, length), checks every 21 * offset before reading, and returns views into the caller's buffer only. 22 * Error order matches the Rust reference exactly so the two can be compared 23 * byte for byte (see tests/net_diff.c). 24 * 25 * Deliberate difference: checksums accumulate in 64 bits, so inputs larger 26 * than 128 KiB cannot overflow the sum (the Rust u32 sum would overflow there), 27 * and the ARP cache reuses expired entries when learning (see README.md). 28 * 29 * Hosted only: no native binding (needs a C kernel) and no production keys 30 * (needs M5). See README.md. 31 */ 32 #ifndef AIENOS_NET_H 33 #define AIENOS_NET_H 34 35 #include 36 #include 37 38 typedef enum { 39 NET_OK = 0, 40 NET_ERR_TRUNCATED = 1, 41 NET_ERR_INVALID = 2, 42 NET_ERR_UNSUPPORTED = 3, 43 NET_ERR_CAPACITY = 4, 44 } net_err; 45 46 typedef struct { uint8_t b[6]; } net_mac; 47 typedef struct { uint8_t b[4]; } net_ipv4; 48 49 #define NET_ETHERTYPE_IPV4 0x0800u 50 #define NET_ETHERTYPE_ARP 0x0806u 51 #define NET_ARP_REQUEST 1u 52 #define NET_ARP_REPLY 2u 53 #define NET_IPPROTO_ICMP 1u 54 #define NET_IPPROTO_UDP 17u 55 56 /* RFC 1071 one's-complement checksum; a final odd byte is the high octet. */ 57 uint16_t net_checksum(const uint8_t *data, size_t len); 58 59 /* ---- Ethernet II ---- */ 60 typedef struct { 61 net_mac destination; 62 net_mac source; 63 uint16_t ethertype; 64 const uint8_t *payload; /* view into the parsed buffer */ 65 size_t payload_len; 66 } net_eth_frame; 67 68 net_err net_eth_parse(const uint8_t *b, size_t len, net_eth_frame *out); 69 net_err net_eth_build(const net_eth_frame *f, uint8_t *out, size_t cap, size_t *written); 70 71 /* ---- ARP (Ethernet/IPv4 only) ---- */ 72 typedef struct { 73 uint16_t operation; 74 net_mac sender_mac; 75 net_ipv4 sender_ip; 76 net_mac target_mac; 77 net_ipv4 target_ip; 78 } net_arp_packet; 79 80 net_err net_arp_parse(const uint8_t *b, size_t len, net_arp_packet *out); 81 net_err net_arp_build(const net_arp_packet *p, uint8_t *out, size_t cap, size_t *written); 82 83 /* Fixed-capacity ARP cache. The caller owns the clock and the expiry policy. 84 * Capacity is chosen at init (1..NET_ARP_CACHE_MAX); no allocation. */ 85 #define NET_ARP_CACHE_MAX 64 86 typedef struct { 87 uint8_t used; 88 net_ipv4 ip; 89 net_mac mac; 90 uint64_t expires; 91 } net_arp_entry; 92 typedef struct { 93 size_t capacity; 94 net_arp_entry entries[NET_ARP_CACHE_MAX]; 95 } net_arp_cache; 96 97 net_err net_arp_cache_init(net_arp_cache *c, size_t capacity); 98 /* Returns 1 and fills *mac on a live hit, 0 otherwise. Expired entries seen 99 * before the hit are evicted (same sweep as the Rust reference). */ 100 int net_arp_cache_lookup(net_arp_cache *c, net_ipv4 ip, uint64_t now, net_mac *mac); 101 /* Learn only solicited senders unless allow_unsolicited is non-zero. */ 102 net_err net_arp_cache_learn(net_arp_cache *c, const net_arp_packet *p, int solicited, 103 int allow_unsolicited, uint64_t now, uint64_t ttl); 104 105 /* ---- IPv4 (no options emitted, no fragment reassembly) ---- */ 106 typedef struct { 107 uint8_t dscp_ecn; 108 uint16_t identification; 109 uint8_t ttl; 110 uint8_t protocol; 111 net_ipv4 source; 112 net_ipv4 destination; 113 } net_ipv4_header; 114 115 net_err net_ipv4_parse(const uint8_t *b, size_t len, net_ipv4_header *out, 116 const uint8_t **payload, size_t *payload_len); 117 net_err net_ipv4_build(const net_ipv4_header *h, const uint8_t *payload, size_t payload_len, 118 uint8_t *out, size_t cap, size_t *written); 119 120 /* ---- ICMP echo ---- */ 121 typedef struct { 122 int reply; /* 1 = echo reply (type 0), 0 = echo request (type 8) */ 123 uint16_t identifier; 124 uint16_t sequence; 125 size_t data_len; 126 } net_icmp_echo; 127 128 net_err net_icmp_echo_parse(const uint8_t *b, size_t len, net_icmp_echo *out); 129 net_err net_icmp_echo_build(const net_icmp_echo *e, const uint8_t *data, size_t data_len, 130 uint8_t *out, size_t cap, size_t *written); 131 132 /* ---- UDP over IPv4 ---- */ 133 typedef struct { 134 uint16_t source_port; 135 uint16_t destination_port; 136 } net_udp_header; 137 138 net_err net_udp_parse(const uint8_t *b, size_t len, net_ipv4 src, net_ipv4 dst, 139 net_udp_header *out, const uint8_t **payload, size_t *payload_len); 140 net_err net_udp_build(const net_udp_header *h, const uint8_t *data, size_t data_len, 141 net_ipv4 src, net_ipv4 dst, uint8_t *out, size_t cap, size_t *written); 142 143 const char *net_err_name(net_err e); 144 145 #endif /* AIENOS_NET_H */