aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / net / tcp / peer.om
| 1 | note peer.om: host side of the L232 QEMU gate. Seeded Omega over host TCP sockets |
| 2 | note on 127.0.0.1; QEMU slirp carries each connection into the guest stack. |
| 3 | note Arguments: MODE PORT FILE LIMIT [OUT] |
| 4 | note e: connect, send FILE, close the write side, require the echo to equal FILE |
| 5 | note s: connect, send FILE, close the write side, require EOF and no data back |
| 6 | note r: listen on PORT, accept one connection, require the stream to equal FILE |
| 7 | note and end in EOF; write the received bytes to OUT |
| 8 | note Prints PEER_PASS or PEER_FAIL with counters; exit 0 only on PEER_PASS. |
| 9 | global Pbuf uchar 16777216 |
| 10 | global Pin uchar 65536 |
| 11 | global Psa uchar 16 |
| 12 | global Parg uchar 256 |
| 13 | note Pcf: 0 file length, 1 sent, 2 received, 3 mismatched bytes, 4 eof seen, |
| 14 | note 5 write side shut, 6 out fd (0 none), 7 errors. |
| 15 | global Pcf ulong 8 |
| 16 | global Pnum uchar 32 |
| 17 | text Mpass "PEER_PASS" |
| 18 | text Mfail "PEER_FAIL" |
| 19 | text Msent " sent=" |
| 20 | text Mgot " got=" |
| 21 | text Mbad " bad=" |
| 22 | text Meof " eof=" |
| 23 | text Merr " err=" |
| 24 | text Mnl "\n" |
| 25 | fn pnum v ulong to void |
| 26 | i = ulong 32 |
| 27 | while true bound 21 |
| 28 | i = sub i 1 |
| 29 | put Pnum i uchar add 48 rem v 10 |
| 30 | v = div v 10 |
| 31 | if eq v 0 |
| 32 | break |
| 33 | end |
| 34 | end |
| 35 | k = write 1 Pnum i sub 32 i |
| 36 | end |
| 37 | fn parg i ulong to ulong |
| 38 | if ge long i argcnt |
| 39 | ret 0 |
| 40 | end |
| 41 | k = argread i Parg 0 255 |
| 42 | if lt k 1 |
| 43 | exit 2 |
| 44 | end |
| 45 | ret ulong k |
| 46 | end |
| 47 | fn pdec n ulong to ulong |
| 48 | v = ulong 0 |
| 49 | if eq n 0 |
| 50 | exit 2 |
| 51 | end |
| 52 | for i ulong n bound 20 |
| 53 | c = ulong get Parg i |
| 54 | if or lt c 48 gt c 57 |
| 55 | exit 2 |
| 56 | end |
| 57 | v = add mul v 10 sub c 48 |
| 58 | end |
| 59 | ret v |
| 60 | end |
| 61 | fn pload n ulong to void |
| 62 | f = fopen Parg 0 n 0 0 |
| 63 | if lt f 0 |
| 64 | exit 3 |
| 65 | end |
| 66 | o = ulong 0 |
| 67 | while true bound 100000 |
| 68 | k = read int f Pbuf o umin 1048576 sub 16777216 o |
| 69 | if lt k 0 |
| 70 | exit 3 |
| 71 | end |
| 72 | if eq k 0 |
| 73 | break |
| 74 | end |
| 75 | o = add o ulong k |
| 76 | if eq o 16777216 |
| 77 | exit 3 |
| 78 | end |
| 79 | end |
| 80 | c = fclose int f |
| 81 | put Pcf 0 o |
| 82 | end |
| 83 | fn umin a ulong b ulong to ulong |
| 84 | if lt a b |
| 85 | ret a |
| 86 | end |
| 87 | ret b |
| 88 | end |
| 89 | fn psa port ulong to void |
| 90 | put Psa 0 2 |
| 91 | put Psa 1 0 |
| 92 | put Psa 2 uchar shr port 8 |
| 93 | put Psa 3 uchar and port 255 |
| 94 | put Psa 4 127 |
| 95 | put Psa 5 0 |
| 96 | put Psa 6 0 |
| 97 | put Psa 7 1 |
| 98 | for i ulong 8 |
| 99 | put Psa add 8 i 0 |
| 100 | end |
| 101 | end |
| 102 | fn pnow to ulong |
| 103 | t = monons |
| 104 | if lt t 0 |
| 105 | exit 4 |
| 106 | end |
| 107 | ret div ulong t 1000000 |
| 108 | end |
| 109 | fn pdial port ulong lim ulong to int |
| 110 | psa port |
| 111 | s = socknew 2 526337 0 |
| 112 | if lt s 0 |
| 113 | exit 5 |
| 114 | end |
| 115 | r = connect int s Psa 0 16 |
| 116 | if and ne r 0 ne r neg long 115 |
| 117 | exit 5 |
| 118 | end |
| 119 | d = add pnow lim |
| 120 | while lt pnow d bound 100000000 |
| 121 | w = waitio int s 4 10000000 |
| 122 | if ne and w 24 0 |
| 123 | exit 5 |
| 124 | end |
| 125 | if ne and w 4 0 |
| 126 | ret int s |
| 127 | end |
| 128 | end |
| 129 | exit 5 |
| 130 | end |
| 131 | fn paccept port ulong lim ulong to int |
| 132 | psa port |
| 133 | s = socknew 2 524289 0 |
| 134 | if lt s 0 |
| 135 | exit 5 |
| 136 | end |
| 137 | if ne bind int s Psa 0 16 0 |
| 138 | exit 5 |
| 139 | end |
| 140 | if ne listen int s 1 0 |
| 141 | exit 5 |
| 142 | end |
| 143 | d = add pnow lim |
| 144 | while lt pnow d bound 100000000 |
| 145 | w = waitio int s 1 10000000 |
| 146 | if ne and w 1 0 |
| 147 | a = accept int s 526336 |
| 148 | if lt a 0 |
| 149 | exit 5 |
| 150 | end |
| 151 | c = fclose int s |
| 152 | ret int a |
| 153 | end |
| 154 | end |
| 155 | exit 5 |
| 156 | end |
| 157 | note Compare n received bytes with the file at the current receive offset. |
| 158 | fn pcheck n ulong to void |
| 159 | g = get Pcf 2 |
| 160 | for i ulong n bound 65536 |
| 161 | j = add g i |
| 162 | if ge j get Pcf 0 |
| 163 | put Pcf 3 add get Pcf 3 1 |
| 164 | else |
| 165 | if ne get Pin i get Pbuf j |
| 166 | put Pcf 3 add get Pcf 3 1 |
| 167 | end |
| 168 | end |
| 169 | end |
| 170 | put Pcf 2 add g n |
| 171 | end |
| 172 | fn pout n ulong to void |
| 173 | f = get Pcf 6 |
| 174 | if eq f 0 |
| 175 | ret |
| 176 | end |
| 177 | o = ulong 0 |
| 178 | while lt o n bound 65536 |
| 179 | k = write int f Pin o sub n o |
| 180 | if le k 0 |
| 181 | exit 3 |
| 182 | end |
| 183 | o = add o ulong k |
| 184 | end |
| 185 | end |
| 186 | fn psend s int to void |
| 187 | o = get Pcf 1 |
| 188 | if lt o get Pcf 0 |
| 189 | k = send s Pbuf o umin 65536 sub get Pcf 0 o 64 |
| 190 | if gt k 0 |
| 191 | put Pcf 1 add o ulong k |
| 192 | else |
| 193 | if ne k neg long 11 |
| 194 | put Pcf 7 add get Pcf 7 1 |
| 195 | put Pcf 1 get Pcf 0 |
| 196 | end |
| 197 | end |
| 198 | end |
| 199 | end |
| 200 | fn precv s int to void |
| 201 | k = recv s Pin 0 65536 64 |
| 202 | if gt k 0 |
| 203 | pout ulong k |
| 204 | pcheck ulong k |
| 205 | ret |
| 206 | end |
| 207 | if eq k 0 |
| 208 | put Pcf 4 1 |
| 209 | ret |
| 210 | end |
| 211 | if ne k neg long 11 |
| 212 | put Pcf 7 add get Pcf 7 1 |
| 213 | put Pcf 4 1 |
| 214 | end |
| 215 | end |
| 216 | fn prun s int mode ulong lim ulong to void |
| 217 | d = add pnow lim |
| 218 | while lt pnow d bound 100000000 |
| 219 | ev = ulong 1 |
| 220 | if and ne mode 114 lt get Pcf 1 get Pcf 0 |
| 221 | ev = ulong 5 |
| 222 | end |
| 223 | w = waitio s int ev 10000000 |
| 224 | if ne and w 4 0 |
| 225 | psend s |
| 226 | end |
| 227 | if and ne mode 114 eq get Pcf 5 0 |
| 228 | if ge get Pcf 1 get Pcf 0 |
| 229 | r = shutdown int s 1 |
| 230 | put Pcf 5 1 |
| 231 | end |
| 232 | end |
| 233 | if ne and w 25 0 |
| 234 | precv s |
| 235 | end |
| 236 | if ne get Pcf 4 0 |
| 237 | ret |
| 238 | end |
| 239 | end |
| 240 | put Pcf 7 add get Pcf 7 1 |
| 241 | end |
| 242 | fn presult mode ulong to bool |
| 243 | ok = and eq get Pcf 7 0 ne get Pcf 4 0 |
| 244 | ok = and ok eq get Pcf 3 0 |
| 245 | if eq mode 115 |
| 246 | ok = and ok eq get Pcf 2 0 |
| 247 | else |
| 248 | ok = and ok eq get Pcf 2 get Pcf 0 |
| 249 | end |
| 250 | if ne mode 114 |
| 251 | ok = and ok eq get Pcf 1 get Pcf 0 |
| 252 | end |
| 253 | if ok |
| 254 | k = write 1 Mpass 0 len Mpass |
| 255 | else |
| 256 | k = write 1 Mfail 0 len Mfail |
| 257 | end |
| 258 | k = write 1 Msent 0 len Msent |
| 259 | pnum get Pcf 1 |
| 260 | k = write 1 Mgot 0 len Mgot |
| 261 | pnum get Pcf 2 |
| 262 | k = write 1 Mbad 0 len Mbad |
| 263 | pnum get Pcf 3 |
| 264 | k = write 1 Meof 0 len Meof |
| 265 | pnum get Pcf 4 |
| 266 | k = write 1 Merr 0 len Merr |
| 267 | pnum get Pcf 7 |
| 268 | k = write 1 Mnl 0 len Mnl |
| 269 | ret ok |
| 270 | end |
| 271 | fn main to int |
| 272 | n = parg 1 |
| 273 | if ne n 1 |
| 274 | exit 2 |
| 275 | end |
| 276 | mode = ulong get Parg 0 |
| 277 | port = pdec parg 2 |
| 278 | pload parg 3 |
| 279 | lim = mul pdec parg 4 1000 |
| 280 | n = parg 5 |
| 281 | if gt n 0 |
| 282 | f = fopen Parg 0 n 577 420 |
| 283 | if lt f 0 |
| 284 | exit 3 |
| 285 | end |
| 286 | put Pcf 6 ulong f |
| 287 | end |
| 288 | s = int 0 |
| 289 | if eq mode 114 |
| 290 | s = paccept port lim |
| 291 | else |
| 292 | s = pdial port lim |
| 293 | end |
| 294 | prun s mode lim |
| 295 | if presult mode |
| 296 | ret 0 |
| 297 | end |
| 298 | ret 1 |
| 299 | end |