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/kernel/dev/net_udp.c # 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 / kernel / dev / net_udp.c` 139 lines ยท 5544 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 /* net_udp.c -- UDP round-trip frames (see net_udp.h). Freestanding. */ 18 #include "net_udp.h" 19 #include 20 21 static const net_mac k_bcast = {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}; 22 23 static int mac_eq(const net_mac *a, const net_mac *b) { return memcmp(a->b, b->b, 6) == 0; } 24 static int ip_eq(net_ipv4 a, net_ipv4 b) { return memcmp(a.b, b.b, 4) == 0; } 25 static int mac_unicast(const net_mac *m) 26 { 27 static const net_mac zero = {{0, 0, 0, 0, 0, 0}}; 28 return !(m->b[0] & 1u) && !mac_eq(m, &zero); 29 } 30 31 static size_t eth_wrap(const net_mac *dst, const net_mac *src, uint16_t type, const uint8_t *pl, size_t n, 32 uint8_t *out, size_t cap) 33 { 34 net_eth_frame f; 35 f.destination = *dst; 36 f.source = *src; 37 f.ethertype = type; 38 f.payload = pl; 39 f.payload_len = n; 40 size_t w = 0; 41 return net_eth_build(&f, out, cap, &w) == NET_OK ? w : 0; 42 } 43 44 static size_t arp_frame(const ck_net_peer *p, uint16_t op, const net_mac *tmac, net_ipv4 tip, 45 const net_mac *eth_dst, uint8_t *out, size_t cap) 46 { 47 net_arp_packet a; 48 uint8_t body[64]; 49 size_t w = 0; 50 a.operation = op; 51 a.sender_mac = p->mac; 52 a.sender_ip = p->ip; 53 a.target_mac = *tmac; 54 a.target_ip = tip; 55 if (net_arp_build(&a, body, sizeof body, &w) != NET_OK) return 0; 56 return eth_wrap(eth_dst, &p->mac, NET_ETHERTYPE_ARP, body, w, out, cap); 57 } 58 59 size_t ck_net_arp_request(const ck_net_peer *p, uint8_t *out, size_t cap) 60 { 61 static const net_mac zero = {{0, 0, 0, 0, 0, 0}}; 62 if (!p || !out) return 0; 63 return arp_frame(p, NET_ARP_REQUEST, &zero, p->gw_ip, &k_bcast, out, cap); 64 } 65 66 size_t ck_net_arp_reply(const ck_net_peer *p, const net_arp_packet *req, uint8_t *out, size_t cap) 67 { 68 if (!p || !req || !out || !mac_unicast(&req->sender_mac)) return 0; 69 return arp_frame(p, NET_ARP_REPLY, &req->sender_mac, req->sender_ip, &req->sender_mac, out, cap); 70 } 71 72 size_t ck_net_udp_frame(const ck_net_peer *p, uint16_t ip_id, const uint8_t *data, size_t n, uint8_t *out, 73 size_t cap) 74 { 75 static uint8_t udp[1500], ip[1500]; 76 if (!p || !out || (n && !data) || !mac_unicast(&p->gw_mac)) return 0; 77 net_udp_header uh = {p->lport, p->rport}; 78 size_t uw = 0, iw = 0; 79 if (net_udp_build(&uh, data, n, p->ip, p->gw_ip, udp, sizeof udp, &uw) != NET_OK) return 0; 80 net_ipv4_header ih; 81 ih.dscp_ecn = 0; 82 ih.identification = ip_id; 83 ih.ttl = 64; 84 ih.protocol = NET_IPPROTO_UDP; 85 ih.source = p->ip; 86 ih.destination = p->gw_ip; 87 if (net_ipv4_build(&ih, udp, uw, ip, sizeof ip, &iw) != NET_OK) return 0; 88 return eth_wrap(&p->gw_mac, &p->mac, NET_ETHERTYPE_IPV4, ip, iw, out, cap); 89 } 90 91 ck_net_kind ck_net_classify(const ck_net_peer *p, const uint8_t *frame, size_t len, net_mac *mac, 92 net_arp_packet *arp, const uint8_t **payload, size_t *payload_len, int *csum) 93 { 94 net_eth_frame e; 95 if (!p || !frame || net_eth_parse(frame, len, &e) != NET_OK) return CK_NET_IGNORED; 96 if (!mac_eq(&e.destination, &p->mac) && !mac_eq(&e.destination, &k_bcast)) return CK_NET_IGNORED; 97 if (e.ethertype == NET_ETHERTYPE_ARP) { 98 net_arp_packet a; 99 if (net_arp_parse(e.payload, e.payload_len, &a) != NET_OK) return CK_NET_IGNORED; 100 if (!ip_eq(a.target_ip, p->ip) || !mac_unicast(&a.sender_mac)) return CK_NET_IGNORED; 101 if (a.operation == NET_ARP_REPLY && ip_eq(a.sender_ip, p->gw_ip) && mac_eq(&a.target_mac, &p->mac) && 102 mac_eq(&e.destination, &p->mac)) { 103 if (mac) *mac = a.sender_mac; 104 return CK_NET_ARP_GW; 105 } 106 if (a.operation == NET_ARP_REQUEST) { 107 if (arp) *arp = a; 108 return CK_NET_ARP_ASK; 109 } 110 return CK_NET_IGNORED; 111 } 112 if (e.ethertype != NET_ETHERTYPE_IPV4 || !mac_eq(&e.destination, &p->mac)) return CK_NET_IGNORED; 113 net_ipv4_header ih; 114 const uint8_t *ipl = 0; 115 size_t ipn = 0; 116 if (net_ipv4_parse(e.payload, e.payload_len, &ih, &ipl, &ipn) != NET_OK) return CK_NET_IGNORED; 117 if (ih.protocol != NET_IPPROTO_UDP || !ip_eq(ih.source, p->gw_ip) || !ip_eq(ih.destination, p->ip)) 118 return CK_NET_IGNORED; 119 net_udp_header uh; 120 const uint8_t *pl = 0; 121 size_t pn = 0; 122 if (net_udp_parse(ipl, ipn, ih.source, ih.destination, &uh, &pl, &pn) != NET_OK) return CK_NET_IGNORED; 123 if (uh.source_port != p->rport || uh.destination_port != p->lport) return CK_NET_IGNORED; 124 if (payload) *payload = pl; 125 if (payload_len) *payload_len = pn; 126 if (csum) *csum = (ipl[6] | ipl[7]) != 0; 127 return CK_NET_UDP_REPLY; 128 } 129 130 void ck_net_printable(const uint8_t *b, size_t n, char *out, size_t cap) 131 { 132 if (!out || !cap) return; 133 size_t i = 0; 134 for (; b && i < n && i + 1 < cap; i++) { 135 uint8_t c = b[i]; 136 out[i] = (c == '"' || c == '\\') ? '\'' : (c >= 0x20 && c < 0x7f) ? (char)c : '.'; 137 } 138 out[i] = 0; 139 }