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/x86/tests/courier_udp_frame_test.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 / x86 / tests / courier_udp_frame_test.c` 235 lines ยท 9988 bytes Raw Blame History 1 /* Actual Ethernet test: no UDP unwrapping/rewrapping between the kernel 2 * courier and allen-courier. Run only inside ck_courier_udp_frame_test.sh's 3 * disposable network namespace. The captured relay LINE also drives refusal 4 * and duplicate controls through the courier's real receive filter. */ 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 #include 13 #include 14 #include 15 #include 16 #include 17 #include "../../../net/aienos_net.h" 18 #include "../courier_udp.h" 19 20 #define FRAME_MAX 1600u 21 static struct { 22 int fd, hold, drop_got; 23 struct sockaddr_ll wire; 24 uint8_t mac[6], first[FRAME_MAX], inject[FRAME_MAX]; 25 size_t first_n, inject_n; 26 unsigned got, lines; 27 const char *out; 28 } dev; 29 30 static void require(int ok, const char *why) 31 { 32 if (!ok) { 33 fprintf(stderr, "COURIER_FRAMES: FAIL %s (errno=%d)\n", why, errno); 34 exit(1); 35 } 36 } 37 static uint64_t now(void *ctx) 38 { 39 (void)ctx; 40 struct timespec t; 41 require(clock_gettime(CLOCK_MONOTONIC, &t) == 0, "clock"); 42 return (uint64_t)t.tv_sec * 1000u + (uint64_t)t.tv_nsec / 1000000u; 43 } 44 static int link_up(void *ctx) { (void)ctx; return 1; } 45 static void save(const char *name, const uint8_t *f, size_t n) 46 { 47 char path[4096]; 48 require(snprintf(path, sizeof path, "%s/%s.frame", dev.out, name) < (int)sizeof path, "frame path"); 49 int file_fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644); 50 FILE *file = file_fd < 0 ? NULL : fdopen(file_fd, "wb"); 51 require(file != NULL, "open frame output"); 52 require(fwrite(f, 1, n, file) == n, "write frame output"); 53 require(fclose(file) == 0, "close frame output"); 54 } 55 static int decode(const uint8_t *f, size_t n, net_eth_frame *e, net_ipv4_header *ih, 56 net_udp_header *uh, const uint8_t **data, size_t *dn) 57 { 58 const uint8_t *ip; 59 size_t ipn; 60 return net_eth_parse(f, n, e) == NET_OK && e->ethertype == NET_ETHERTYPE_IPV4 && 61 net_ipv4_parse(e->payload, e->payload_len, ih, &ip, &ipn) == NET_OK && 62 ih->protocol == NET_IPPROTO_UDP && 63 net_udp_parse(ip, ipn, ih->source, ih->destination, uh, data, dn) == NET_OK && 64 uh->source_port == CK_COURIER_PORT && uh->destination_port == CK_COURIER_PORT; 65 } 66 static int kind(const uint8_t *d, size_t n, const char *name) 67 { 68 char text[2048], key[32]; 69 if (n >= sizeof text) return 0; 70 memcpy(text, d, n); text[n] = 0; 71 snprintf(key, sizeof key, " kind=%s ", name); 72 return strstr(text, key) != NULL; 73 } 74 static int transmit(void *ctx, const uint8_t *f, size_t n) 75 { 76 (void)ctx; 77 net_eth_frame e; 78 net_ipv4_header ih; 79 net_udp_header uh; 80 const uint8_t *data; 81 size_t dn; 82 const uint8_t zero[4] = {0}, limited[4] = {255,255,255,255}, broadcast[6] = {255,255,255,255,255,255}; 83 require(decode(f, n, &e, &ih, &uh, &data, &dn), "emitted frame/checksum"); 84 require(!memcmp(ih.source.b, zero, 4) && !memcmp(ih.destination.b, limited, 4) && 85 !memcmp(e.destination.b, broadcast, 6), "outgoing zero-IP limited broadcast"); 86 if (kind(data, dn, "HELLO")) save("hello", f, n); 87 if (kind(data, dn, "JOB")) save("job", f, n); 88 if (kind(data, dn, "GOT")) { 89 dev.got++; 90 save("got", f, n); 91 if (dev.drop_got) return 0; 92 } 93 return sendto(dev.fd, f, n, 0, (struct sockaddr *)&dev.wire, sizeof dev.wire) == (ssize_t)n ? 0 : -1; 94 } 95 static size_t receive(void *ctx, uint8_t *f, size_t cap) 96 { 97 (void)ctx; 98 if (dev.inject_n) { 99 size_t n = dev.inject_n; 100 require(n <= cap, "injected frame bound"); 101 memcpy(f, dev.inject, n); dev.inject_n = 0; 102 return n; 103 } 104 if (dev.hold == 2) return 0; 105 for (;;) { 106 struct sockaddr_ll from; 107 socklen_t flen = sizeof from; 108 ssize_t n = recvfrom(dev.fd, f, cap, MSG_DONTWAIT, (struct sockaddr *)&from, &flen); 109 if (n < 0) { require(errno == EAGAIN || errno == EWOULDBLOCK, "raw receive"); return 0; } 110 if (from.sll_pkttype == PACKET_OUTGOING) continue; 111 net_eth_frame e; 112 net_ipv4_header ih; 113 net_udp_header uh; 114 const uint8_t *data; 115 size_t dn; 116 if (!decode(f, (size_t)n, &e, &ih, &uh, &data, &dn)) continue; 117 const uint8_t spark[4] = {192,168,1,108}, subnet[4] = {192,168,1,255}; 118 require(!memcmp(ih.source.b, spark, 4) && !memcmp(ih.destination.b, subnet, 4), "real relay reply addresses"); 119 for (unsigned i = 0; i < 6; i++) require(e.destination.b[i] == 255, "real relay broadcast MAC"); 120 if (kind(data, dn, "HI")) save("hi", f, (size_t)n); 121 if (kind(data, dn, "ACK")) save("ack", f, (size_t)n); 122 if (kind(data, dn, "LINE")) { 123 dev.lines++; 124 char name[32]; 125 snprintf(name, sizeof name, "line-%u", dev.lines); 126 save(name, f, (size_t)n); 127 if (dev.hold == 1) { 128 memcpy(dev.first, f, (size_t)n); dev.first_n = (size_t)n; 129 save("line", f, (size_t)n); 130 dev.hold = 2; 131 return 0; 132 } 133 } 134 return (size_t)n; 135 } 136 } 137 static void inject(const uint8_t *f, size_t n) 138 { 139 require(n <= sizeof dev.inject && !dev.inject_n, "single injected frame"); 140 memcpy(dev.inject, f, n); dev.inject_n = n; 141 } 142 static void refusals(const ck_courier *c) 143 { 144 uint8_t bad[FRAME_MAX]; 145 char line[CK_COURIER_LINE_MAX]; 146 unsigned before = dev.got; 147 memcpy(bad, dev.first, dev.first_n); 148 bad[0] = 0x02; /* neither ours nor broadcast */ 149 inject(bad, dev.first_n); 150 require(!c->take(line, sizeof line) && dev.got == before, "not-ours MAC must not GOT"); 151 memcpy(bad, dev.first, dev.first_n); 152 bad[dev.first_n - 1] ^= 1; /* checksum no longer matches */ 153 inject(bad, dev.first_n); 154 require(!c->take(line, sizeof line) && dev.got == before, "bad UDP checksum must not GOT"); 155 inject(dev.first, 30); 156 require(!c->take(line, sizeof line) && dev.got == before, "truncated IP must not GOT"); 157 /* Change only run, then produce a valid UDP checksum: this is a protocol 158 * refusal, not accidentally another checksum refusal. */ 159 memcpy(bad, dev.first, dev.first_n); 160 net_eth_frame e; 161 net_ipv4_header ih; 162 net_udp_header uh; 163 const uint8_t *data; 164 size_t dn, written; 165 require(decode(bad, dev.first_n, &e, &ih, &uh, &data, &dn), "control decode"); 166 size_t off = (size_t)(data - bad); 167 size_t run_at = off + strlen("ALLEN-COURIER v=1 run="); 168 bad[run_at] = bad[run_at] == '0' ? '1' : '0'; 169 require(net_udp_build(&uh, bad + off, dn, ih.source, ih.destination, bad + off - 8, 170 sizeof bad - off + 8, &written) == NET_OK, "control checksum"); 171 require(decode(bad, dev.first_n, &e, &ih, &uh, &data, &dn), "wrong-run valid checksums"); 172 inject(bad, dev.first_n); 173 require(!c->take(line, sizeof line) && dev.got == before, "wrong run must not GOT"); 174 inject(dev.first, dev.first_n); 175 require(c->take(line, sizeof line) && dev.got == before + 1, "real LINE must GOT immediately"); 176 printf("real LINE body: %s\n", line); 177 inject(dev.first, dev.first_n); 178 require(!c->take(line, sizeof line) && dev.got == before + 2, "duplicate LINE must GOT without duplicate body"); 179 puts("COURIER_FRAME_FILTER: PASS wrong-run/not-ours/bad-checksum/truncated/valid/duplicate"); 180 } 181 int main(int argc, char **argv) 182 { 183 require(argc == 3 || argc == 4, "usage: courier_udp_frame_test IFACE OUTPUT_DIR [drop-got]"); 184 dev.out = argv[2]; dev.hold = 1; dev.drop_got = argc == 4; 185 dev.fd = socket(AF_PACKET, SOCK_RAW, htons(0x0800)); 186 require(dev.fd >= 0, "raw socket (requires isolated CAP_NET_RAW)"); 187 dev.wire.sll_family = AF_PACKET; 188 dev.wire.sll_protocol = htons(0x0800); 189 dev.wire.sll_ifindex = (int)if_nametoindex(argv[1]); 190 require(dev.wire.sll_ifindex != 0 && !bind(dev.fd, (struct sockaddr *)&dev.wire, sizeof dev.wire), "bind raw interface"); 191 struct ifreq req = {0}; 192 require(strlen(argv[1]) < sizeof req.ifr_name, "interface name"); 193 strcpy(req.ifr_name, argv[1]); 194 require(!ioctl(dev.fd, SIOCGIFHWADDR, &req), "interface MAC"); 195 memcpy(dev.mac, req.ifr_hwaddr.sa_data, 6); 196 const ck_netio io = {NULL, transmit, receive, link_up, now}; 197 const uint8_t zero[4] = {0}, limited[4] = {255,255,255,255}; 198 const ck_courier *c = ck_courier_udp_init(&io, dev.mac, zero, limited, CK_COURIER_PORT, 1000); 199 uint64_t start = now(NULL); 200 char line[CK_COURIER_LINE_MAX]; 201 while (!c->ready()) { 202 c->take(line, sizeof line); 203 require(now(NULL) - start < 5000, "no real HI for source-zero HELLO"); 204 usleep(1000); 205 } 206 const char job[] = "JOB 1 chat Answer as Allen.\thello allen, how was your day\tself=Allen\tname=Drake\tstarts=20\tnote asked: hi / said: Hello"; 207 require(!c->send(job, sizeof job - 1), "send real job"); 208 while (!dev.first_n) { 209 c->take(line, sizeof line); 210 require(now(NULL) - start < 5000, "no real LINE for job"); 211 usleep(1000); 212 } 213 refusals(c); 214 dev.hold = 0; 215 int whole = 0; 216 while (!whole) { 217 if (c->take(line, sizeof line)) { 218 unsigned seq, part, total; 219 printf("real LINE body: %s\n", line); 220 if (sscanf(line, "SAY %u %u/%u", &seq, &part, &total) == 3 && total && part == total) whole = 1; 221 } 222 require(now(NULL) - start < 5000, "answer did not finish"); 223 usleep(1000); 224 } 225 unsigned before = dev.lines; 226 uint64_t quiet = now(NULL); 227 while (now(NULL) - quiet < 700) { 228 c->take(line, sizeof line); 229 usleep(1000); 230 } 231 require(dev.lines == before, "relay retransmitted LINE after GOT (ACK path failed)"); 232 printf("COURIER_FRAMES: PASS zero-IP HELLO/JOB/GOT limited broadcast, real subnet HI/ACK/LINE, %u LINE frames %u GOT frames\n", dev.lines, dev.got); 233 close(dev.fd); 234 return 0; 235 }