interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / net / aienos_report.c

163 lines · 5183 bytes Raw Blame History
1/* aienos_report.c -- see aienos_report.h. Built on the M6-A stack
2 * (aienos_net.c) for every layer below the header line. */
3#include "aienos_report.h"
4
5#include <string.h>
6
7#include "aienos_net.h"
8
9static const net_ipv4 ANY = {{0, 0, 0, 0}}, BCAST = {{255, 255, 255, 255}};
10
11static size_t tag_len(const char *tag)
12{
13 size_t n = 0;
14 while (tag && tag[n] && n <= NET_REPORT_TAG_MAX)
15 n++;
16 return tag && n && n <= NET_REPORT_TAG_MAX ? n : 0;
17}
18
19static void hex16(char *out, uint64_t v)
20{
21 for (int i = 15; i >= 0; i--, v >>= 4)
22 out[i] = "0123456789abcdef"[v & 15];
23}
24
25static size_t dec(char *out, unsigned long v)
26{
27 char t[20];
28 size_t n = 0, k = 0;
29 do
30 t[n++] = (char)('0' + v % 10);
31 while ((v /= 10) && n < sizeof t);
32 while (n)
33 out[k++] = t[--n];
34 return k;
35}
36
37unsigned net_report_parts(size_t len)
38{
39 if (!len || len > NET_REPORT_MAX)
40 return 0;
41 return (unsigned)((len + NET_REPORT_CHUNK - 1) / NET_REPORT_CHUNK);
42}
43
44size_t net_report_frame(const char *tag, const uint8_t mac[6], uint64_t run, const char *log, size_t len,
45 unsigned part, uint8_t *out, size_t cap)
46{
47 uint8_t data[NET_REPORT_TAG_MAX + 96 + NET_REPORT_CHUNK], udp[8 + sizeof data], ip[20 + sizeof udp];
48 unsigned parts = net_report_parts(len);
49 size_t t = tag_len(tag);
50 if (!t || !mac || !log || !out || part >= parts)
51 return 0;
52 size_t off = (size_t)part * NET_REPORT_CHUNK;
53 size_t n = len - off < NET_REPORT_CHUNK ? len - off : NET_REPORT_CHUNK;
54 size_t h = 0;
55 memcpy(data, tag, t);
56 h += t;
57 memcpy(data + h, " v=1 run=", 9);
58 h += 9;
59 hex16((char *)data + h, run);
60 h += 16;
61 memcpy(data + h, " part=", 6);
62 h += 6;
63 h += dec((char *)data + h, part);
64 data[h++] = '/';
65 h += dec((char *)data + h, parts);
66 memcpy(data + h, " bytes=", 7);
67 h += 7;
68 h += dec((char *)data + h, (unsigned long)len);
69 data[h++] = '\n';
70 memcpy(data + h, log + off, n);
71
72 net_udp_header uh = {NET_REPORT_PORT, NET_REPORT_PORT};
73 size_t uw = 0, iw = 0, ew = 0;
74 if (net_udp_build(&uh, data, h + n, ANY, BCAST, udp, sizeof udp, &uw) != NET_OK)
75 return 0;
76 net_ipv4_header ih;
77 ih.dscp_ecn = 0;
78 ih.identification = (uint16_t)part;
79 ih.ttl = 1; /* this network only */
80 ih.protocol = NET_IPPROTO_UDP;
81 ih.source = ANY;
82 ih.destination = BCAST;
83 if (net_ipv4_build(&ih, udp, uw, ip, sizeof ip, &iw) != NET_OK)
84 return 0;
85 net_eth_frame e;
86 memset(e.destination.b, 0xff, 6);
87 memcpy(e.source.b, mac, 6);
88 e.ethertype = NET_ETHERTYPE_IPV4;
89 e.payload = ip;
90 e.payload_len = iw;
91 if (net_eth_build(&e, out, cap, &ew) != NET_OK)
92 return 0;
93 return ew;
94}
95
96/* Reads a decimal number ending at `stop`; -1 on no digits or overflow. */
97static long num(const uint8_t *p, size_t n, size_t *i, char stop)
98{
99 unsigned long v = 0;
100 size_t s = *i;
101 while (*i < n && p[*i] >= '0' && p[*i] <= '9' && *i - s < 9)
102 v = v * 10 + (unsigned long)(p[(*i)++] - '0');
103 if (*i == s || *i >= n || p[*i] != stop)
104 return -1;
105 (*i)++;
106 return (long)v;
107}
108
109static int lit(const uint8_t *p, size_t n, size_t *i, const char *s, size_t sn)
110{
111 if (n - *i < sn || memcmp(p + *i, s, sn) != 0)
112 return 0;
113 *i += sn;
114 return 1;
115}
116
117int net_report_parse(const char *tag, const uint8_t *frame, size_t n, net_report_part *out)
118{
119 size_t t = tag_len(tag);
120 if (!t || !frame || !out)
121 return -1;
122 net_eth_frame e;
123 if (net_eth_parse(frame, n, &e) != NET_OK || e.ethertype != NET_ETHERTYPE_IPV4)
124 return -2;
125 net_ipv4_header ih;
126 const uint8_t *ipp = 0, *up = 0;
127 size_t ipn = 0, un = 0;
128 if (net_ipv4_parse(e.payload, e.payload_len, &ih, &ipp, &ipn) != NET_OK || ih.protocol != NET_IPPROTO_UDP)
129 return -3;
130 net_udp_header uh;
131 if (net_udp_parse(ipp, ipn, ih.source, ih.destination, &uh, &up, &un) != NET_OK ||
132 uh.destination_port != NET_REPORT_PORT)
133 return -4;
134 size_t i = 0;
135 if (!lit(up, un, &i, tag, t) || !lit(up, un, &i, " v=1 run=", 9) || un - i < 16)
136 return -5;
137 uint64_t run = 0;
138 for (int k = 0; k < 16; k++) {
139 uint8_t c = up[i++];
140 unsigned v = c >= '0' && c <= '9' ? (unsigned)(c - '0') : c >= 'a' && c <= 'f' ? (unsigned)(c - 'a') + 10u : 16u;
141 if (v > 15)
142 return -5;
143 run = run << 4 | v;
144 }
145 long part, parts, total;
146 if (!lit(up, un, &i, " part=", 6) || (part = num(up, un, &i, '/')) < 0 || (parts = num(up, un, &i, ' ')) < 0 ||
147 !lit(up, un, &i, "bytes=", 6) || (total = num(up, un, &i, '\n')) < 0)
148 return -6;
149 if (net_report_parts((size_t)total) != (unsigned long)parts || part >= parts)
150 return -7;
151 size_t off = (size_t)part * NET_REPORT_CHUNK;
152 size_t want = (size_t)total - off < NET_REPORT_CHUNK ? (size_t)total - off : NET_REPORT_CHUNK;
153 if (un - i != want)
154 return -8;
155 out->run = run;
156 out->part = (unsigned)part;
157 out->parts = (unsigned)parts;
158 out->total = (size_t)total;
159 out->offset = off;
160 out->data = up + i;
161 out->len = want;
162 return 0;
163}