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/aienos.com/blob/main/om/agora/client.om # aienos.com Source of www.aienos.com, served from my own hardware 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 `aienos.com / om / agora / client.om` 561 lines ยท 12214 bytes Raw Blame History 1 note client.om: the HTTP client and Agora text-wire helpers shared by aien-agora.om, agora-sync.om and 2 note agora-edge.om. The text API's address is one packed ulong ap = ip * 65536 + port (ac_apiarg), so a 3 note node listening on another interface (agora serve --listen) is reachable as --api A.B.C.D:PORT. 4 note A library unit (no om2 line, no main, no globals). Compose after http.om and sha256.om: 5 note { printf 'om2\n'; cat http.om sha256.om json.om jsonw.om client.om aien-agora.om; } | omc 6 note Names: functions ac_*, texts ac*. Connection state is the caller's cs (ulong 320) and io (uchar 65536). 7 8 text achost "Host" 9 text acctype "Content-Type" 10 text actxt "text/plain; charset=utf-8" 11 text acjson "application/json" 12 text acget "GET" 13 text acpost "POST" 14 text acloop "127.0.0.1" 15 text acok "agora 1\nok\n" 16 text acerr "agora 1\nerr " 17 text accl "challenge\t" 18 text acnon "\nnonce\t" 19 text acpchal "/agora/api/challenge" 20 text acphel "/agora/api/hello" 21 text acppost "/agora/api/post" 22 text acpnote "/agora/api/note" 23 text acpimp "/agora/api/import" 24 text actmp ".tmp" 25 text achexd "`0123456789abcdef`" 26 27 note ======== transport ======== 28 29 note ac_dial: connect to ip:port (host order, `2130706433` = 127.0.0.1). tmo bounds every wait in ns. 30 fn ac_dial cs edit ulong 320 io edit uchar 65536 ip ulong port ulong tmo long to long 31 a = new uchar 16 0 32 r = haddr4 a uint ip port 33 if lt r 0 34 ret r 35 end 36 fd = hdial a tmo 37 if lt fd 0 38 ret fd 39 end 40 r = hinit cs io int fd 0 41 if lt r 0 42 c = fclose int fd 43 ret r 44 end 45 hsettmo cs tmo tmo 46 hsetbdl cs tmo 47 ret 0 48 end 49 50 note ac_dialap: connect to the packed text API address ap (ac_apiarg) 51 fn ac_dialap cs edit ulong 320 io edit uchar 65536 ap ulong tmo long to long 52 ret ac_dial cs io shr ap 16 and ap 65535 tmo 53 end 54 55 note ac_get: GET pa with Host ho; the final status or a negative http.om error 56 fn ac_get cs edit ulong 320 io edit uchar 65536 pa view uchar * ho view uchar * to long 57 r = hreqst cs io acget 58 r = hfe r hreqtg cs io pa 59 r = hfe r hhname cs io achost 60 r = hfe r hhval cs io ho 61 r = hfe r hhend cs io 0 0 62 r = hfe r hwfin cs io 63 if lt r 0 64 ret r 65 end 66 ret hrsprd cs io 67 end 68 69 note ac_phead: request line, Host and Content-Type of a POST to pa on loopback; js 1 for application/json 70 fn ac_phead cs edit ulong 320 io edit uchar 65536 pa view uchar * js ulong to long 71 r = hreqst cs io acpost 72 r = hfe r hreqtg cs io pa 73 r = hfe r hhname cs io achost 74 r = hfe r hhval cs io acloop 75 r = hfe r hhname cs io acctype 76 if eq js 1 77 ret hfe r hhval cs io acjson 78 end 79 ret hfe r hhval cs io actxt 80 end 81 82 note ac_pfin: Content-Length, the body bd and the final status (a negative r passes through) 83 fn ac_pfin cs edit ulong 320 io edit uchar 65536 r long bd view uchar * to long 84 x = hfe r hhend cs io 0 len bd 85 x = hfe x hput cs io bd 86 x = hfe x hwfin cs io 87 if lt x 0 88 ret x 89 end 90 ret hrsprd cs io 91 end 92 93 note ac_post: POST the text bd to pa on loopback; the final status or a negative error 94 fn ac_post cs edit ulong 320 io edit uchar 65536 pa view uchar * bd view uchar * to long 95 r = ac_phead cs io pa 0 96 ret ac_pfin cs io r bd 97 end 98 99 note ac_postj: POST the JSON bd to pa on loopback 100 fn ac_postj cs edit ulong 320 io edit uchar 65536 pa view uchar * bd view uchar * to long 101 r = ac_phead cs io pa 1 102 ret ac_pfin cs io r bd 103 end 104 105 note ac_body: the whole response body into d, then close; the length or a negative error 106 fn ac_body cs edit ulong 320 io edit uchar 65536 d edit uchar * to long 107 put cs 37 add len d 1 108 n = hbodyall cs io d 109 c = hclose cs 110 ret n 111 end 112 113 note ======== text wire ======== 114 115 note ac_okhdr: offset of the first record after "agora 1\nok\n"; -1 for an err reply, -2 otherwise 116 fn ac_okhdr d view uchar * n ulong to long 117 if ge n len acok 118 if ac_peq d 0 len acok acok 119 ret long len acok 120 end 121 end 122 if ge n len acerr 123 if ac_peq d 0 len acerr acerr 124 ret -1 125 end 126 end 127 ret -2 128 end 129 130 note ac_lend: index of the first LF at or after p before n, or n 131 fn ac_lend d view uchar * p ulong n ulong to ulong 132 i = p 133 while lt i n bound 999999 134 if eq get d i 10 135 ret i 136 end 137 i = add i 1 138 end 139 ret n 140 end 141 142 note ac_fend: end of the TAB field starting at p (the next TAB or e) 143 fn ac_fend d view uchar * p ulong e ulong to ulong 144 i = p 145 while lt i e bound 999999 146 if eq get d i 9 147 ret i 148 end 149 i = add i 1 150 end 151 ret e 152 end 153 154 note ac_fld: start of TAB field k of the line [p, e); e + 1 when the line has fewer fields 155 fn ac_fld d view uchar * p ulong e ulong k ulong to ulong 156 q = p 157 for i ulong k 158 f = ac_fend d q e 159 if ge f e 160 ret add e 1 161 end 162 q = add f 1 163 end 164 ret q 165 end 166 167 note ac_peq: whether d[p, e) equals t 168 fn ac_peq d view uchar * p ulong e ulong t view uchar * to bool 169 if ne sub e p len t 170 ret false 171 end 172 n = len t 173 for i ulong n 174 if ne get d add p i get t i 175 ret false 176 end 177 end 178 ret true 179 end 180 181 note ac_pre: whether d[p, e) starts with t 182 fn ac_pre d view uchar * p ulong e ulong t view uchar * to bool 183 if lt sub e p len t 184 ret false 185 end 186 ret ac_peq d p add p len t t 187 end 188 189 note ac_dec: the decimal d[p, e); 2^64 - 1 when empty, not all digits or longer than 18 digits 190 fn ac_dec d view uchar * p ulong e ulong to ulong 191 bad = ulong `18446744073709551615` 192 if or ge p e gt sub e p 18 193 ret bad 194 end 195 v = ulong 0 196 k = sub e p 197 for i ulong k 198 c = get d add p i 199 if or lt c 48 gt c 57 200 ret bad 201 end 202 v = add mul v 10 ulong sub c 48 203 end 204 ret v 205 end 206 207 note ======== byte building ======== 208 209 note ac_cat: s appended to dst at q; the new q, or len dst + 1 when it does not fit 210 fn ac_cat dst edit uchar * q ulong s view uchar * to ulong 211 n = len s 212 if gt add q n len dst 213 ret add len dst 1 214 end 215 for i ulong n 216 put dst add q i get s i 217 end 218 ret add q n 219 end 220 221 note ac_catc: one byte c appended to dst at q 222 fn ac_catc dst edit uchar * q ulong c uchar to ulong 223 if ge q len dst 224 ret add len dst 1 225 end 226 put dst q c 227 ret add q 1 228 end 229 230 note ac_catu: the decimal of v appended to dst at q 231 fn ac_catu dst edit uchar * q ulong v ulong to ulong 232 t = new uchar 24 0 233 n = ulong 0 234 x = v 235 for i ulong 20 236 put t n uchar add 48 rem x 10 237 n = add n 1 238 x = div x 10 239 if eq x 0 240 break 241 end 242 end 243 w = q 244 for j ulong n 245 w = ac_catc dst w get t sub sub n 1 j 246 end 247 ret w 248 end 249 250 note ======== addresses ======== 251 252 note ac_ipv4: dotted quad d[p, e) in host order; 2^64 - 1 when malformed 253 fn ac_ipv4 d view uchar * p ulong e ulong to ulong 254 bad = ulong `18446744073709551615` 255 v = ulong 0 256 q = p 257 for k ulong 4 258 f = q 259 while lt f e bound 64 260 if eq get d f 46 261 break 262 end 263 f = add f 1 264 end 265 if and lt k 3 ge f e 266 ret bad 267 end 268 x = ac_dec d q f 269 if gt x 255 270 ret bad 271 end 272 v = or shl v 8 x 273 q = add f 1 274 end 275 if lt q add e 1 276 ret bad 277 end 278 ret v 279 end 280 281 note ac_hostp: "a.b.c.d:port" in d[p, e) into o[0] (ip) and o[1] (port); false when malformed 282 fn ac_hostp d view uchar * p ulong e ulong o edit ulong 4 to bool 283 c = p 284 while lt c e bound 256 285 if eq get d c 58 286 break 287 end 288 c = add c 1 289 end 290 if ge c e 291 ret false 292 end 293 ip = ac_ipv4 d p c 294 pt = ac_dec d add c 1 e 295 if or gt ip `4294967295` or eq pt 0 gt pt 65535 296 ret false 297 end 298 put o 0 ip 299 put o 1 pt 300 ret true 301 end 302 303 note ac_apiarg: an --api value d[p, e), "PORT" (127.0.0.1) or "A.B.C.D:PORT", as the packed address 304 note ip * 65536 + port; 0 when malformed 305 fn ac_apiarg d view uchar * p ulong e ulong to ulong 306 o = new ulong 4 0 307 if ac_hostp d p e o 308 ret or shl get o 0 16 get o 1 309 end 310 pt = ac_dec d p e 311 if or eq pt 0 gt pt 65535 312 ret 0 313 end 314 ret or shl ulong `2130706433` 16 pt 315 end 316 317 note ======== proof of work ======== 318 319 note ac_lz: leading zero bits of a 32-byte digest 320 fn ac_lz g view uchar 32 to ulong 321 z = ulong 0 322 for i ulong 32 323 b = ulong get g i 324 if ne b 0 325 for j ulong 8 326 if ne and b 128 0 327 ret z 328 end 329 z = add z 1 330 b = shl b 1 331 end 332 end 333 z = add z 8 334 end 335 ret z 336 end 337 338 note ac_powok: whether SHA-256(m[0, n)) has >= bits leading zero bits 339 fn ac_powok m view uchar * n ulong bits ulong to bool 340 g = new uchar 32 0 341 sha256_hash view part m 0 n g 342 ret ge ac_lz g bits 343 end 344 345 note ac_pow: the smallest nonce with SHA-256(ch LF nonce LF rest) having >= bits leading zero bits; ch is the 346 note 32 hex bytes of the challenge. 2^64 - 1 when rest is too large or 2^26 nonces fail. 347 fn ac_pow ch view uchar * bits ulong rest view uchar * to ulong 348 m = new uchar 20480 0 349 if or ne len ch 32 gt len rest 20000 350 ret ulong `18446744073709551615` 351 end 352 q = ac_cat m 0 ch 353 q = ac_catc m q 10 354 nd = ulong 0 355 nl = ulong 0 356 v = ulong 0 357 for a ulong 128 358 for b ulong 524288 359 w = ac_catu m q v 360 if ne w nd 361 nd = w 362 nl = ac_cat m ac_catc m w 10 rest 363 end 364 if ac_powok view m nl bits 365 ret v 366 end 367 v = add v 1 368 end 369 end 370 ret ulong `18446744073709551615` 371 end 372 373 note ac_wbody: "challenge\t\nnonce\t\n" then rest into dst; the length, or len dst + 1 374 fn ac_wbody dst edit uchar * ch view uchar 32 non ulong rest view uchar * to ulong 375 q = ac_cat dst 0 accl 376 q = ac_cat dst q ch 377 q = ac_cat dst q acnon 378 q = ac_catu dst q non 379 q = ac_catc dst q 10 380 ret ac_cat dst q rest 381 end 382 383 note ac_chal: GET the challenge from the text API at ap; its 32 hex bytes into ch; the bits, or negative 384 fn ac_chal ap ulong ch edit uchar 32 to long 385 cs = new ulong 320 0 386 io = new uchar 65536 0 387 d = new uchar 1024 0 388 r = ac_dialap cs io ap `10000000000` 389 if lt r 0 390 ret r 391 end 392 st = ac_get cs io acpchal acloop 393 if lt st 0 394 c = hclose cs 395 ret st 396 end 397 n = ac_body cs io d 398 if lt n 0 399 ret n 400 end 401 h = ac_okhdr d ulong n 402 if lt h 0 403 ret -1 404 end 405 p = ulong h 406 e = ac_lend d p ulong n 407 if not ac_pre d p e accl 408 ret -1 409 end 410 x = ac_fld d p e 1 411 y = ac_fend d x e 412 if ne sub y x 32 413 ret -1 414 end 415 for i ulong 32 416 put ch i get d add x i 417 end 418 z = ac_fld d p e 2 419 bits = ac_dec d z ac_fend d z e 420 if gt bits 256 421 ret -1 422 end 423 ret long bits 424 end 425 426 note ac_send: POST the text bd to endpoint ep (1 hello, 2 post, 3 note, 4 import) of the text API at ap; the 427 note reply body goes into d; its length, or negative 428 fn ac_send ap ulong ep ulong bd view uchar * d edit uchar 65536 to long 429 cs = new ulong 320 0 430 io = new uchar 65536 0 431 r = ac_dialap cs io ap `10000000000` 432 if lt r 0 433 ret r 434 end 435 st = long -4 436 if eq ep 1 437 st = ac_post cs io acphel bd 438 end 439 if eq ep 2 440 st = ac_post cs io acppost bd 441 end 442 if eq ep 3 443 st = ac_post cs io acpnote bd 444 end 445 if eq ep 4 446 st = ac_post cs io acpimp bd 447 end 448 if lt st 0 449 c = hclose cs 450 ret st 451 end 452 ret ac_body cs io d 453 end 454 455 note ac_solve: a fresh challenge from the text API at ap solved for rest; the whole request body into bd 456 note (20480 bytes); its length, or 0 on failure 457 fn ac_solve ap ulong rest view uchar * bd edit uchar 20480 to ulong 458 ch = new uchar 32 0 459 bits = ac_chal ap ch 460 if lt bits 0 461 ret 0 462 end 463 non = ac_pow view ch ulong bits rest 464 if eq non `18446744073709551615` 465 ret 0 466 end 467 n = ac_wbody bd ch non rest 468 if gt n 20480 469 ret 0 470 end 471 ret n 472 end 473 474 note ac_write: solve a challenge for rest and POST it to endpoint ep (ac_send); the reply length or negative 475 fn ac_write ap ulong ep ulong rest view uchar * d edit uchar 65536 to long 476 bd = new uchar 20480 0 477 n = ac_solve ap rest bd 478 if eq n 0 479 ret -3 480 end 481 ret ac_send ap ep view part bd 0 n d 482 end 483 484 note ======== files ======== 485 486 note ac_join: dir "/" name into dst; the length (dst holds 512 bytes) 487 fn ac_join dir view uchar * name view uchar * dst edit uchar 512 to ulong 488 q = ac_cat dst 0 dir 489 q = ac_catc dst q 47 490 ret ac_cat dst q name 491 end 492 493 note ac_rdat: up to len d bytes of file p from offset off into d; the count, -2 when p does not exist 494 fn ac_rdat p view uchar * off ulong d edit uchar * to long 495 fd = int fopen p 0 len p 0 0 496 if lt fd 0 497 ret -2 498 end 499 got = ulong 0 500 while lt got len d bound 999999 501 r = pread fd d got sub len d got long add off got 502 if lt r 0 503 c = fclose fd 504 ret -1 505 end 506 if eq r 0 507 break 508 end 509 got = add got ulong r 510 end 511 c = fclose fd 512 ret long got 513 end 514 515 note ac_put: replace file p with s atomically (p.tmp, fsync, rename); mode is the permission bits 516 fn ac_put p view uchar * s view uchar * mode int to bool 517 t = new uchar 520 0 518 pl = new uchar 1100 0 519 q = ac_cat t 0 p 520 q = ac_cat t q actmp 521 if gt q 512 522 ret false 523 end 524 fd = int fopen t 0 q 577 mode 525 if lt fd 0 526 ret false 527 end 528 w = write fd s 0 len s 529 y = fsync fd 530 c = fclose fd 531 if ne w long len s 532 ret false 533 end 534 k = ac_catc pl 0 2 535 k = ac_catc pl k 0 536 k = ac_catc pl k uchar and q 255 537 k = ac_catc pl k uchar shr q 8 538 k = ac_cat pl k view part t 0 q 539 k = ac_catc pl k uchar and len p 255 540 k = ac_catc pl k uchar shr len p 8 541 k = ac_cat pl k p 542 r = renames pl 0 k 0 543 ret ge r 0 544 end 545 546 note ac_app: append s to file p (created with mode); false on any failure 547 fn ac_app p view uchar * s view uchar * mode int to bool 548 fd = int fopen p 0 len p 1089 mode 549 if lt fd 0 550 ret false 551 end 552 w = write fd s 0 len s 553 y = fsync fd 554 c = fclose fd 555 ret eq w long len s 556 end 557 558 note ac_say: s on stdout 559 fn ac_say s view uchar * to void 560 w = write 1 s 0 len s 561 end