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/tools/ck_udp_echo.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 / tools / ck_udp_echo.c` 124 lines ยท 4647 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 /* ck_udp_echo.c -- host-side UDP echo helper for scripts/qemu_ck_net_test.sh 18 * (gate AIENOS_CK_NET). Hosted C (POSIX sockets), test tooling only, never 19 * in the kernel image. No Python, no nc/socat. 20 * 21 * ck_udp_echo PORT TOKEN LOGFILE SECONDS 22 * 23 * Binds 127.0.0.1:PORT (QEMU user networking forwards guest datagrams for 24 * 10.0.2.2:PORT there), writes "ready port=PORT" to LOGFILE, then for SECONDS 25 * answers every datagram that starts with "AIENOS-CK-NET ping " with 26 * "AIENOS-CK-NET pong token=TOKEN echo=" 27 * and logs each one ("rx ..." / "tx ..."). TOKEN (hex, 8..64 chars) is chosen 28 * by the script per run, so a guest line carrying it proves the reply came 29 * from this process. Exit 0 at the deadline, 2 usage, 3 socket/bind error. */ 30 #define _POSIX_C_SOURCE 200809L 31 #include 32 #include 33 #include 34 #include 35 #include 36 #include 37 #include 38 #include 39 #include 40 #include 41 42 #define PING "AIENOS-CK-NET ping " 43 #define MAX_ECHO 400 44 45 static double now_s(void) 46 { 47 struct timespec ts; 48 clock_gettime(CLOCK_MONOTONIC, &ts); 49 return (double)ts.tv_sec + (double)ts.tv_nsec / 1e9; 50 } 51 52 static void show(FILE *lg, const unsigned char *b, size_t n) 53 { 54 for (size_t i = 0; i < n; i++) fputc((b[i] >= 0x20 && b[i] < 0x7f && b[i] != '"') ? b[i] : '.', lg); 55 } 56 57 int main(int argc, char **argv) 58 { 59 if (argc != 5) { 60 fprintf(stderr, "usage: ck_udp_echo PORT TOKEN LOGFILE SECONDS\n"); 61 return 2; 62 } 63 char *end = 0; 64 long port = strtol(argv[1], &end, 10); 65 long secs = strtol(argv[4], 0, 10); 66 const char *token = argv[2]; 67 size_t tl = strlen(token); 68 if (*end || port < 1024 || port > 65535 || secs < 1 || secs > 3600 || tl < 8 || tl > 64 || 69 strspn(token, "`0123456789abcdef`") != tl) { 70 fprintf(stderr, "ck_udp_echo: bad arguments\n"); 71 return 2; 72 } 73 FILE *lg = fopen(argv[3], "w"); 74 if (!lg) { 75 perror("ck_udp_echo: log"); 76 return 2; 77 } 78 setvbuf(lg, 0, _IOLBF, 0); 79 int s = socket(AF_INET, SOCK_DGRAM, 0); 80 struct sockaddr_in a; 81 memset(&a, 0, sizeof a); 82 a.sin_family = AF_INET; 83 a.sin_port = htons((unsigned short)port); 84 a.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 85 if (s < 0 || bind(s, (struct sockaddr *)&a, sizeof a) != 0) { 86 fprintf(lg, "bind failed port=%ld errno=%d (%s)\n", port, errno, strerror(errno)); 87 fclose(lg); 88 return 3; 89 } 90 fprintf(lg, "ready port=%ld\n", port); 91 double deadline = now_s() + (double)secs; 92 unsigned replies = 0; 93 for (;;) { 94 double left = deadline - now_s(); 95 if (left <= 0) break; 96 struct pollfd pf = {s, POLLIN, 0}; 97 int pr = poll(&pf, 1, (int)(left * 1000.0) + 1); 98 if (pr < 0 && errno == EINTR) continue; 99 if (pr <= 0) continue; 100 unsigned char in[2048], out[2048]; 101 struct sockaddr_in from; 102 socklen_t fl = sizeof from; 103 ssize_t n = recvfrom(s, in, sizeof in, 0, (struct sockaddr *)&from, &fl); 104 if (n < 0) continue; 105 char fa[INET_ADDRSTRLEN] = "?"; 106 inet_ntop(AF_INET, &from.sin_addr, fa, sizeof fa); 107 fprintf(lg, "rx from %s:%u len=%zd payload=\"", fa, ntohs(from.sin_port), n); 108 show(lg, in, (size_t)n); 109 fprintf(lg, "\"\n"); 110 if ((size_t)n < sizeof PING - 1 || memcmp(in, PING, sizeof PING - 1) != 0) { 111 fprintf(lg, "ignored (not a ping)\n"); 112 continue; 113 } 114 int h = snprintf((char *)out, sizeof out, "AIENOS-CK-NET pong token=%s echo=", token); 115 size_t m = (size_t)n > MAX_ECHO ? MAX_ECHO : (size_t)n; 116 memcpy(out + h, in, m); 117 ssize_t sent = sendto(s, out, (size_t)h + m, 0, (struct sockaddr *)&from, fl); 118 fprintf(lg, "tx to %s:%u len=%zd reply=%u\n", fa, ntohs(from.sin_port), sent, ++replies); 119 } 120 fprintf(lg, "done replies=%u\n", replies); 121 fclose(lg); 122 close(s); 123 return 0; 124 }