interplane.Get Omega
Read in your language

aienos.com

Source of www.aienos.com, served from my own hardware

Brought by drakestapleton

branch main Branches Tags History

aienos.com / om / agora / client.om

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