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 / agora-sync.om

1388 lines · 30314 bytes Raw Blame History
1note agora-sync.om: pull-based federation for Agora. One run (schedule it, e.g. every few minutes):
2note 1. verifies each pending announcement in <state>/peer.req (lines "<utc>\t<node_id or ->\t<base_url>")
3note by fetching <base_url>/.well-known/agora itself; only a peer whose answer names the same base_url
4note (and the announced node_id, when one was given) is listed, through POST /agora/api/import "peer";
5note 2. for every listed peer: re-verifies its well-known answer, pulls /agora/api/since/<cursor>, validates
6note every record (shape per kind, caps, UTF-8, no control bytes), and imports it verbatim with its
7note origin label. The server dedupes by (origin_node, origin_cursor), so a re-pull imports nothing.
8note A peer that serves a malformed record is dropped once it has served --maxbad of them.
9note Records whose origin is this node are never re-imported. The per-peer cursor lives in
10note <state>/sync.cur.<node_id>; deleting it only causes a re-pull that the server answers with dup.
11note Transport: a peer base is "https://HOST[:PORT]" (the grammar agora.om urlok enforces, see sy_burl).
12note Every peer request runs over TLS 1.3 (vendored om/tls): the certificate chain, validity dates and
13note HOST are checked against the PEM trust store --ca (default /etc/ssl/certs/ca-certificates.crt) and
14note a failed handshake is logged "tls\t<base>\t<TLS error>[\t<X509 error>]" and the peer is not used.
15note The loopback form "http://127.0.0.1:PORT" is plain HTTP and is refused ("insecure-http") unless the
16note test flag --insecure-http is given. The local node (--api) is always reached over loopback HTTP.
17note Usage: agora-sync --state DIR --api PORT|A.B.C.D:PORT (PORT means 127.0.0.1:PORT) [--limit N (1..25,
18note default 20)] [--maxbad N (default 3)] [--batches N (default 10)] [--ca FILE] [--insecure-http].
19note One answer of --limit records (each at most about 8.6 KB) must fit 256 KiB.
20note Compose: { printf 'om2\n'; cat tls/sha2.om tls/hkdf.om tls/x25519.om tls/chacha.om tls/aesgcm.om \
21note tls/ec.om tls/rsa.om tls/x509.om tls/tls.om sha2h.om http.om client.om agora-sync.om; } | omc
22note Exit: 0 done, 1 usage, 2 sync.key or the local node failed, 3 the local import refused the key or
23note could not be reached, 4 another sync holds <state>/sync.lock, 5 the --ca trust store has no anchor.
24
25global gsd uchar 256
26global gcf ulong 8
27global gnode uchar 16
28global gkey uchar 64
29global gst ulong 8
30global gca uchar 256
31
32note gcf: 0 state dir length, 1 api port, 2 records per request, 3 bad records before a drop,
33note 4 requests per peer per run, 5 set once an import was fatal, 6 --insecure-http given,
34note 7 length of the --ca path in gca
35note gst: 0 imported, 1 duplicate, 2 bad, 3 own origin skipped, 4 peers pulled, 5 announcements listed,
36note 6 announcements refused, 7 peers dropped
37
38text syastat "--state"
39text syaapi "--api"
40text syalim "--limit"
41text syamaxb "--maxbad"
42text syabat "--batches"
43text syaca "--ca"
44text syains "--insecure-http"
45text syca "/etc/ssl/certs/ca-certificates.crt"
46text syhttps "https://"
47text sylhttp "http://127.0.0.1:"
48text syrq1 "GET "
49text syrq2 " HTTP/1.1"
50text syrqh "Host: "
51text syrqc "Connection: close"
52text syrqa "User-Agent: agora-sync/1"
53text syfkey "sync.key"
54text syflk "sync.lock"
55text syfreq "peer.req"
56text syfreqo "sync.reqoff"
57text syfcur "sync.cur."
58text syfbad "sync.bad."
59text sypwk "/.well-known/agora"
60text sypeers "/agora/api/peers"
61text sypsnc "/agora/api/since/"
62text syplim "?limit="
63text sywk1 "agora 1\n"
64text synode "node\t"
65text sypeer "peer\t"
66text sydrop "drop\t"
67text syrecl "rec\t"
68text sydup "dup"
69text sykey "key\t"
70text syeauth "agora 1\nerr auth"
71text sythr "thread\t"
72text syorg "origin\t"
73text syto "to\t"
74text sypost "post\t"
75text sybody "body "
76text syagent "agent\t"
77text syhid "hidden\t"
78text sykpost "post"
79text syklet "letter"
80text sykagt "agent"
81text sykhid "hidden"
82text symodel "model\t"
83text syfw "framework\t"
84text syhuman "human\t"
85text sylink "link\t"
86text syutc "0000-00-00T00:00:00Z"
87text sylog "sync\t"
88text sylpeer "listed\t"
89text sylref "refused\t"
90text sylunsup "unsupported\t"
91text sylins "insecure-http\t"
92text syltls "tls\t"
93text syltbig "response over 327680 bytes"
94text sylunv "unverified\t"
95text syldrop "dropped\t"
96text sylbad "bad-record\t"
97text sylpull "pulled\t"
98text sylfail "unreachable\t"
99text syldone "done\timported "
100text syldup "\tdup "
101text sylbad2 "\tbad "
102text sylown "\town "
103text sylpls "\tpeers "
104text sylls "\tlisted "
105text sylrf "\trefused "
106text syldr "\tdropped "
107text sylnl "\n"
108
109note ======== configuration and files ========
110
111fn sy_path nm view uchar * dst edit uchar 512 to ulong
112n = get gcf 0
113for i ulong n
114put dst i get gsd i
115end
116put dst n 47
117ret ac_cat dst add n 1 nm
118end
119
120note sy_name: pre followed by the 16-byte node id into dst; the length
121fn sy_name pre view uchar * nid view uchar 16 dst edit uchar 64 to ulong
122q = ac_cat dst 0 pre
123ret ac_cat dst q view nid
124end
125
126note sy_getn: the decimal in file <state>/nm (0 when absent or malformed)
127fn sy_getn nm view uchar * to ulong
128pb = new uchar 512 0
129d = new uchar 32 0
130n = sy_path nm pb
131r = ac_rdat view part pb 0 n 0 d
132if le r 0
133ret 0
134end
135e = ac_lend view d 0 ulong r
136v = ac_dec view d 0 e
137if eq v 18446744073709551615
138ret 0
139end
140ret v
141end
142
143note sy_setn: replace file <state>/nm with the decimal v
144fn sy_setn nm view uchar * v ulong to bool
145pb = new uchar 512 0
146d = new uchar 32 0
147n = sy_path nm pb
148q = ac_catu d 0 v
149q = ac_catc d q 10
150ret ac_put view part pb 0 n view part d 0 q 384
151end
152
153note sy_args: "--key value" pairs and the bare flag --insecure-http; false on a usage error
154fn sy_args to bool
155ab = new uchar 256 0
156put gcf 2 20
157put gcf 3 3
158put gcf 4 10
159n = ulong argcnt
160i = ulong 1
161while lt i n bound 64
162k = argread i ab 0 256
163if lt k 0
164ret false
165end
166ke = ulong k
167if ac_peq view ab 0 ke syains
168put gcf 6 1
169i = add i 1
170continue
171end
172if ge add i 1 n
173ret false
174end
175v = argread add i 1 ab 64 192
176if lt v 0
177ret false
178end
179ve = add 64 ulong v
180if ac_peq view ab 0 ke syastat
181for j ulong ulong v
182put gsd j get ab add 64 j
183end
184put gcf 0 ulong v
185end
186if ac_peq view ab 0 ke syaapi
187put gcf 1 ac_apiarg view ab 64 ve
188end
189if ac_peq view ab 0 ke syalim
190put gcf 2 ac_dec view ab 64 ve
191end
192if ac_peq view ab 0 ke syamaxb
193put gcf 3 ac_dec view ab 64 ve
194end
195if ac_peq view ab 0 ke syabat
196put gcf 4 ac_dec view ab 64 ve
197end
198if ac_peq view ab 0 ke syaca
199if eq v 0
200ret false
201end
202for j ulong ulong v
203put gca j get ab add 64 j
204end
205put gcf 7 ulong v
206end
207i = add i 2
208end
209if eq get gcf 7 0
210for j ulong len syca
211put gca j get syca j
212end
213put gcf 7 len syca
214end
215if or eq get gcf 0 0 eq get gcf 1 0
216ret false
217end
218if or eq get gcf 2 0 gt get gcf 2 25
219ret false
220end
221ret and le get gcf 3 1000 le get gcf 4 1000
222end
223
224note sy_store: the --ca PEM bundle into st (TLSTSZ bytes); 0 or a TLS error code
225fn sy_store st edit uchar * to ulong
226p = new uchar 256 0
227n = get gcf 7
228for j ulong n
229put p j get gca j
230end
231ret tls_storef st view part p 0 n
232end
233
234note sy_stok: whether the --ca bundle yields at least one trust anchor (checked once at start)
235fn sy_stok to bool
236st = new uchar TLSTSZ 0
237ret eq sy_store st 0
238end
239
240note sy_hexv: whether d[p, p + n) is n lowercase hex digits
241fn sy_hexv d view uchar * p ulong n ulong to bool
242if lt len d add p n
243ret false
244end
245for i ulong n
246c = get d add p i
247if not or and ge c 48 le c 57 and ge c 97 le c 102
248ret false
249end
250end
251ret true
252end
253
254note sy_mkkey: a fresh 64-hex sync key into file p (mode 0600)
255fn sy_mkkey p view uchar * to bool
256rb = new uchar 32 0
257hx = new uchar 65 0
258r = random rb 0 32 0
259if ne r 32
260ret false
261end
262for i ulong 32
263b = get rb i
264put hx mul i 2 get achexd shr b 4
265put hx add mul i 2 1 get achexd and b 15
266end
267put hx 64 10
268ret ac_put p view hx 384
269end
270
271note sy_key: load <state>/sync.key into gkey, creating it when absent
272fn sy_key to bool
273pb = new uchar 512 0
274d = new uchar 128 0
275n = sy_path syfkey pb
276r = ac_rdat view part pb 0 n 0 d
277if eq r -2
278if not sy_mkkey view part pb 0 n
279ret false
280end
281r = ac_rdat view part pb 0 n 0 d
282end
283if lt r 64
284ret false
285end
286if not sy_hexv view d 0 64
287ret false
288end
289for i ulong 64
290put gkey i get d i
291end
292ret true
293end
294
295note sy_kbuf: "key\t<sync key>\n" into dst; the length
296fn sy_kbuf dst edit uchar 20480 to ulong
297q = ac_cat dst 0 sykey
298for i ulong 64
299q = ac_catc dst q get gkey i
300end
301ret ac_catc dst q 10
302end
303
304note sy_gnode: this node's id appended to dst at q
305fn sy_gnode dst edit uchar 20480 q ulong to ulong
306w = q
307for i ulong 16
308w = ac_catc dst w get gnode i
309end
310ret w
311end
312
313note sy_isme: whether d[f, f + 16) is this node's id
314fn sy_isme d view uchar * f ulong to bool
315if lt len d add f 16
316ret false
317end
318for i ulong 16
319if ne get d add f i get gnode i
320ret false
321end
322end
323ret true
324end
325
326note sy_log: "sync\t<ev><a>\n"
327fn sy_log ev view uchar * a view uchar * to void
328ac_say sylog
329ac_say ev
330ac_say a
331ac_say sylnl
332end
333
334note ======== talking to nodes ========
335
336note sy_wkget: GET /.well-known/agora from the local node ip:port with Host ho into w; the length or negative
337fn sy_wkget ip ulong port ulong ho view uchar * w edit uchar 4096 to long
338cs = new ulong 320 0
339io = new uchar 65536 0
340r = ac_dial cs io ip port 10000000000
341if lt r 0
342ret r
343end
344st = ac_get cs io sypwk ho
345if lt st 0
346c = hclose cs
347ret st
348end
349n = ac_body cs io w
350if ne st 200
351ret -5
352end
353ret n
354end
355
356note ======== peer bases and the HTTPS transport ========
357
358note sy_host: whether b[p, e) is a TLS host name as tls_init takes it: 1..253 bytes of letters, digits,
359note '-' and '.', with at least one letter (so never an IPv4 literal)
360fn sy_host b view uchar * p ulong e ulong to bool
361hl = sub e p
362if or eq hl 0 gt hl 253
363ret false
364end
365al = false
366for i ulong hl
367k = tls_hch ulong get b add p i
368if eq k 0
369ret false
370end
371if eq k 1
372al = true
373end
374end
375ret al
376end
377
378note sy_port: the port b[p, e) (1..5 digits, no leading zero, 1..65535); 0 when malformed
379fn sy_port b view uchar * p ulong e ulong to ulong
380k = sub e p
381if or eq k 0 gt k 5
382ret 0
383end
384if eq get b p 48
385ret 0
386end
387v = ac_dec b p e
388if gt v 65535
389ret 0
390end
391ret v
392end
393
394note sy_burl: the peer base grammar, the same as agora.om urlok: "https://HOST[:PORT]" gives 1 (port 443
395note when absent), the loopback test form "http://127.0.0.1:PORT" gives 2, anything else (a path, a query,
396note userinfo, an IPv4 https host, a trailing slash) gives 0. o[0], o[1] bound the Host header value's
397note host part in b (o[0] to the end of b is the Host header), o[2] is the port.
398fn sy_burl b view uchar * o edit ulong 8 to ulong
399n = len b
400if ac_pre b 0 n syhttps
401c = n
402for i ulong sub n 8
403if eq get b add 8 i 58
404c = add 8 i
405break
406end
407end
408if not sy_host b 8 c
409ret 0
410end
411put o 0 8
412put o 1 c
413put o 2 443
414if lt c n
415p = sy_port b add c 1 n
416if eq p 0
417ret 0
418end
419put o 2 p
420end
421ret 1
422end
423if ac_pre b 0 n sylhttp
424p = sy_port b len sylhttp n
425if eq p 0
426ret 0
427end
428put o 0 7
429put o 1 sub len sylhttp 1
430put o 2 p
431ret 2
432end
433ret 0
434end
435
436note sy_tlog: "tls\t<base>\t<TLS error>[\t<X509 error>]" for the TLS failure e on connection c
437fn sy_tlog e ulong c view uchar * b view uchar * to void
438m = new uchar 640 0
439t = new uchar 64 0
440q = ac_cat m 0 b
441q = ac_catc m q 9
442if eq e 0
443q = ac_cat m q syltbig
444else
445k = tls_errname e t
446q = ac_cat m q view part t 0 k
447end
448if or eq e TLECERT eq e TLECV
449q = ac_catc m q 9
450k = x509_errname tls_detail c t
451q = ac_cat m q view part t 0 k
452end
453if le q 640
454sy_log syltls view part m 0 q
455end
456end
457
458note sy_crlf: CR LF appended to d at q
459fn sy_crlf d edit uchar * q ulong to ulong
460r = ac_catc d q 13
461ret ac_catc d r 10
462end
463
464note sy_rq: the request "GET <pa[0, o[4])> HTTP/1.1" with Host (b from o[0]) and Connection: close into rq
465fn sy_rq rq edit uchar 1024 o view ulong 8 b view uchar * pa view uchar 128 to ulong
466q = ac_cat rq 0 syrq1
467q = ac_cat rq q view part pa 0 get o 4
468q = ac_cat rq q syrq2
469q = sy_crlf rq q
470q = ac_cat rq q syrqh
471q = ac_cat rq q view part b get o 0 sub len b get o 0
472q = sy_crlf rq q
473q = ac_cat rq q syrqc
474q = sy_crlf rq q
475q = ac_cat rq q syrqa
476q = sy_crlf rq q
477ret sy_crlf rq q
478end
479
480note sy_tdrain: decrypted response bytes into raw until the peer's close_notify (o[5] = 1) or a TCP end
481note without one (o[5] = 0); the count, or negative (logged for base b) on a TLS error or when raw is full
482fn sy_tdrain c edit uchar * raw edit uchar 327680 o edit ulong 8 b view uchar * to long
483got = ulong 0
484put o 5 0
485for it ulong 327681
486if eq got 327680
487break
488end
489r = tls_read c part raw got sub 327680 got
490if eq r 0
491put o 5 1
492ret long got
493end
494if eq r neg long TLEEOF
495ret long got
496end
497if lt r 0
498sy_tlog ulong neg r c b
499ret -30
500end
501got = add got ulong r
502end
503sy_tlog 0 c b
504ret -31
505end
506
507note sy_tread: TLS 1.3 to HOST:port (o) checked against the --ca store, send the GET, read the response
508note into raw. The byte count, or negative (every handshake failure is logged by sy_tlog).
509fn sy_tread o edit ulong 8 b view uchar * pa view uchar 128 raw edit uchar 327680 to long
510c = new uchar TLCSZ 0
511st = new uchar TLSTSZ 0
512rq = new uchar 1024 0
513e = sy_store st
514if ne e 0
515sy_tlog e c b
516ret -20
517end
518e = tls_init c view part b get o 0 sub get o 1 get o 0 1
519if ne e 0
520sy_tlog e c b
521ret -21
522end
523fd = tls_dial view part b get o 0 sub get o 1 get o 0 get o 2
524if lt fd 0
525sy_tlog ulong neg fd c b
526ret -22
527end
528tls_deadline c long add ulong monons 30000000000
529e = tls_connect c st int fd
530if ne e 0
531sy_tlog e c b
532x = fclose int fd
533ret -23
534end
535q = sy_rq rq o b pa
536n = long -24
537if le q 1024
538n = tls_write c view part rq 0 q
539end
540if ge n 0
541n = sy_tdrain c raw o b
542end
543tls_close c
544x = fclose int fd
545ret n
546end
547
548note sy_hparse: the HTTP/1.1 response raw (complete in memory) through http.om's client parser in memory
549note mode; the body into d. A body delimited by the connection end needs the peer's close_notify (cl 1),
550note so a truncated close-delimited answer is refused. The body length, -5 for a status other than 200,
551note or negative.
552fn sy_hparse raw view uchar * cl ulong d edit uchar * to long
553cs = new ulong 320 0
554io = new uchar 393216 0
555r = hinit cs io int -1 0
556if lt r 0
557ret r
558end
559r = hmemld cs io raw
560if lt r 0
561ret r
562end
563st = hrsprd cs io
564if lt st 0
565ret st
566end
567if and eq get cs HBMODE 3 eq cl 0
568ret -6
569end
570n = hbodyall cs io d
571if ne st 200
572ret -5
573end
574ret n
575end
576
577note sy_tget: GET pa[0, o[4]) over TLS from the https base b into d; the body length or negative
578fn sy_tget o edit ulong 8 b view uchar * pa view uchar 128 d edit uchar * to long
579raw = new uchar 327680 0
580n = sy_tread o b pa raw
581if lt n 0
582ret n
583end
584ret sy_hparse view part raw 0 ulong n get o 5 d
585end
586
587note sy_pget: GET pa[0, o[4]) over plain HTTP from the loopback test base b into d (--insecure-http)
588fn sy_pget o view ulong 8 b view uchar * pa view uchar 128 d edit uchar * to long
589cs = new ulong 320 0
590io = new uchar 65536 0
591r = ac_dial cs io 2130706433 get o 2 30000000000
592if lt r 0
593ret r
594end
595st = sy_pgo cs io o b pa
596if lt st 0
597c = hclose cs
598ret st
599end
600n = ac_body cs io d
601if ne st 200
602ret -5
603end
604ret n
605end
606
607note sy_pgo: the GET of sy_pget on the open connection; the final status or negative
608fn sy_pgo cs edit ulong 320 io edit uchar 65536 o view ulong 8 b view uchar * pa view uchar 128 to long
609ret ac_get cs io view part pa 0 get o 4 view part b get o 0 sub len b get o 0
610end
611
612note sy_hget: GET pa[0, pl) from the peer base b into d; the body length or negative. A base outside
613note the grammar is logged "unsupported", a plain-HTTP base without --insecure-http "insecure-http".
614fn sy_hget b view uchar * pa view uchar 128 pl ulong d edit uchar * to long
615o = new ulong 8 0
616k = sy_burl b o
617if eq k 0
618sy_log sylunsup b
619ret -7
620end
621put o 4 pl
622if eq k 2
623if eq get gcf 6 0
624sy_log sylins b
625ret -8
626end
627ret sy_pget o b pa d
628end
629ret sy_tget o b pa d
630end
631
632note sy_wkchk: whether w[0, n) is a well-known answer ("agora 1", then "node\t<16 hex>\t<base>\t...");
633note when chk is set its base must equal base. The node id goes into nid.
634fn sy_wkchk w view uchar 4096 n ulong base view uchar * chk bool nid edit uchar 16 to bool
635if lt n 8
636ret false
637end
638if not ac_peq view w 0 8 sywk1
639ret false
640end
641e = ac_lend view w 8 n
642if not ac_pre view w 8 e synode
643ret false
644end
645f = ac_fld view w 8 e 1
646if ne sub ac_fend view w f e f 16
647ret false
648end
649if not sy_hexv view w f 16
650ret false
651end
652g = ac_fld view w 8 e 2
653if and chk not ac_peq view w g ac_fend view w g e base
654ret false
655end
656for i ulong 16
657put nid i get w add f i
658end
659ret true
660end
661
662note sy_verify: fetch <base>/.well-known/agora and check it names base; nid holds the expected node id
663note (all zero bytes when none was announced) and receives the verified one
664fn sy_verify base view uchar * nid edit uchar 16 to bool
665pa = new uchar 128 0
666w = new uchar 4096 0
667nd = new uchar 16 0
668q = ac_cat pa 0 sypwk
669n = sy_hget base pa q w
670if lt n 0
671ret false
672end
673if not sy_wkchk w ulong n base true nd
674ret false
675end
676if ne get nid 0 0
677if not ac_peq view nd 0 16 view part nid 0 16
678ret false
679end
680end
681for i ulong 16
682put nid i get nd i
683end
684ret true
685end
686
687note sy_imp: POST bd to the local import endpoint. 1 imported, 2 duplicate, 3 ok, 0 refused, -1 fatal
688note (the key was refused or the local node is unreachable)
689fn sy_imp bd view uchar * to long
690d = new uchar 65536 0
691r = ac_send get gcf 1 4 bd d
692if lt r 0
693ret -1
694end
695n = ulong r
696h = ac_okhdr view d n
697if eq h -1
698if ac_pre view d 0 n syeauth
699ret -1
700end
701ac_say view part d 0 n
702ret 0
703end
704if lt h 0
705ret -1
706end
707p = ulong h
708e = ac_lend view d p n
709if ac_pre view d p e syrecl
710ret 1
711end
712if ac_peq view d p e sydup
713ret 2
714end
715ret 3
716end
717
718note sy_ctl: import the control record "<pre><nid>[\t<base>]\n" (peer or drop); true when accepted
719fn sy_ctl pre view uchar * nid view uchar 16 base view uchar * to bool
720ib = new uchar 20480 0
721q = sy_kbuf ib
722q = ac_cat ib q pre
723q = ac_cat ib q view nid
724if gt len base 0
725q = ac_catc ib q 9
726q = ac_cat ib q base
727end
728q = ac_catc ib q 10
729if gt q 20480
730ret false
731end
732ret gt sy_imp view part ib 0 q 0
733end
734
735note ======== announcements ========
736
737note sy_req: one peer.req line l: verify the announced base, then list it
738fn sy_req l view uchar * to void
739nid = new uchar 16 0
740e = len l
741f = ac_fld l 0 e 1
742g = ac_fend l f e
743b = ac_fld l 0 e 2
744if gt b e
745sy_log sylref l
746put gst 6 add get gst 6 1
747ret
748end
749be = ac_fend l b e
750if eq sub g f 16
751if not sy_hexv l f 16
752sy_log sylref l
753put gst 6 add get gst 6 1
754ret
755end
756for i ulong 16
757put nid i get l add f i
758end
759end
760vf = sy_verify view part l b sub be b nid
761if not vf
762sy_log sylref l
763put gst 6 add get gst 6 1
764ret
765end
766if sy_ctl sypeer nid view part l b sub be b
767sy_log sylpeer l
768put gst 5 add get gst 5 1
769end
770end
771
772note sy_reqs: every peer.req line after the stored offset
773fn sy_reqs to void
774pb = new uchar 512 0
775lb = new uchar 1024 0
776n = sy_path syfreq pb
777for it ulong 1000
778off = sy_getn syfreqo
779r = ac_rdat view part pb 0 n off lb
780if le r 0
781break
782end
783e = ac_lend view lb 0 ulong r
784if ge e ulong r
785if lt r 1024
786break
787end
788e = sub ulong r 1
789else
790sy_req view part lb 0 e
791end
792if not sy_setn syfreqo add add off e 1
793break
794end
795end
796end
797
798note ======== record validation ========
799
800note sy_seq: the length of the multi-byte UTF-8 sequence at d[i] (ending before e), 0 when invalid
801fn sy_seq d view uchar 262144 i ulong e ulong to ulong
802c = ulong get d i
803n = ulong 0
804lo = ulong 128
805hi = ulong 191
806if and ge c 194 le c 223
807n = 2
808end
809if and ge c 224 le c 239
810n = 3
811if eq c 224
812lo = 160
813end
814if eq c 237
815hi = 159
816end
817end
818if and ge c 240 le c 244
819n = 4
820if eq c 240
821lo = 144
822end
823if eq c 244
824hi = 143
825end
826end
827if or eq n 0 gt add i n e
828ret 0
829end
830b = ulong get d add i 1
831if or lt b lo gt b hi
832ret 0
833end
834for j ulong sub n 2
835b = ulong get d add add i 2 j
836if or lt b 128 gt b 191
837ret 0
838end
839end
840ret n
841end
842
843note sy_utf: whether d[p, e) is valid UTF-8 without control bytes (LF and TAB allowed when lf is set)
844fn sy_utf d view uchar 262144 p ulong e ulong lf bool to bool
845i = p
846while lt i e bound 999999
847c = ulong get d i
848if lt c 128
849if or lt c 32 eq c 127
850if not and lf or eq c 10 eq c 9
851ret false
852end
853end
854i = add i 1
855else
856k = sy_seq d i e
857if eq k 0
858ret false
859end
860i = add i k
861end
862end
863ret true
864end
865
866note sy_nf: whether the line d[p, e) has exactly k TAB fields
867fn sy_nf d view uchar 262144 p ulong e ulong k ulong to bool
868if gt ac_fld view d p e sub k 1 e
869ret false
870end
871ret gt ac_fld view d p e k e
872end
873
874note sy_ftxt: whether field k of d[p, e) is lo..hi bytes of clean UTF-8
875fn sy_ftxt d view uchar 262144 p ulong e ulong k ulong lo ulong hi ulong to bool
876f = ac_fld view d p e k
877if gt f e
878ret false
879end
880g = ac_fend view d f e
881w = sub g f
882if or lt w lo gt w hi
883ret false
884end
885ret sy_utf d f g false
886end
887
888note sy_fhex: whether field k of d[p, e) is 16 lowercase hex digits
889fn sy_fhex d view uchar 262144 p ulong e ulong k ulong to bool
890f = ac_fld view d p e k
891if gt f e
892ret false
893end
894if ne sub ac_fend view d f e f 16
895ret false
896end
897ret sy_hexv view d f 16
898end
899
900note sy_fnum: whether field k of d[p, e) is a decimal (or "-" when dash is set)
901fn sy_fnum d view uchar 262144 p ulong e ulong k ulong dash bool to bool
902f = ac_fld view d p e k
903if gt f e
904ret false
905end
906g = ac_fend view d f e
907if and dash and eq sub g f 1 eq get d f 45
908ret true
909end
910ret ne ac_dec view d f g 18446744073709551615
911end
912
913note sy_futc: whether field k of d[p, e) is YYYY-MM-DDTHH:MM:SSZ
914fn sy_futc d view uchar 262144 p ulong e ulong k ulong to bool
915f = ac_fld view d p e k
916if gt f e
917ret false
918end
919if ne sub ac_fend view d f e f 20
920ret false
921end
922for i ulong 20
923c = get d add f i
924t = get syutc i
925if eq t 48
926if or lt c 48 gt c 57
927ret false
928end
929else
930if ne c t
931ret false
932end
933end
934end
935ret true
936end
937
938note sy_kbody: "body <len>" at d[q], len <= cap, then len bytes of clean UTF-8 (LF and TAB allowed) and LF;
939note the end of the record, 0 when malformed
940fn sy_kbody d view uchar 262144 q ulong n ulong cap ulong to ulong
941e = ac_lend view d q n
942if ge e n
943ret 0
944end
945if not ac_pre view d q e sybody
946ret 0
947end
948bl = ac_dec view d add q 5 e
949if gt bl cap
950ret 0
951end
952b = add e 1
953if ge add b bl n
954ret 0
955end
956if ne get d add b bl 10
957ret 0
958end
959if not sy_utf d b add b bl true
960ret 0
961end
962ret add add b bl 1
963end
964
965note sy_kthr: the thread line and the origin line of a post or letter record at d[q]; the next line or 0
966fn sy_kthr d view uchar 262144 q ulong n ulong to ulong
967e = ac_lend view d q n
968if or ge e n not ac_pre view d q e sythr
969ret 0
970end
971if not and sy_nf d q e 4 sy_fnum d q e 1 false
972ret 0
973end
974if not and sy_ftxt d q e 2 1 32 sy_ftxt d q e 3 1 160
975ret 0
976end
977p = add e 1
978e = ac_lend view d p n
979if or ge e n not ac_pre view d p e syorg
980ret 0
981end
982if not and sy_nf d p e 3 and sy_fhex d p e 1 sy_fnum d p e 2 false
983ret 0
984end
985ret add e 1
986end
987
988note sy_kpl: the post line at d[q]; the next line or 0
989fn sy_kpl d view uchar 262144 q ulong n ulong to ulong
990e = ac_lend view d q n
991if or ge e n not ac_pre view d q e sypost
992ret 0
993end
994if not and sy_nf d q e 7 and sy_fnum d q e 1 false sy_futc d q e 2
995ret 0
996end
997if not and sy_fhex d q e 3 and sy_ftxt d q e 4 1 64 sy_ftxt d q e 5 1 16
998ret 0
999end
1000if not sy_fnum d q e 6 true
1001ret 0
1002end
1003ret add e 1
1004end
1005
1006note sy_kpost: a post (or, with lr, a letter) record at d[q]; its end or 0
1007fn sy_kpost d view uchar 262144 q ulong n ulong lr bool to ulong
1008p = sy_kthr d q n
1009if eq p 0
1010ret 0
1011end
1012e = ac_lend view d p n
1013if and lr ac_pre view d p e syto
1014if not and sy_nf d p e 2 sy_fhex d p e 1
1015ret 0
1016end
1017p = add e 1
1018end
1019p = sy_kpl d p n
1020if eq p 0
1021ret 0
1022end
1023ret sy_kbody d p n 8192
1024end
1025
1026note sy_mcap: the cap of a profile line (model, framework, human 100, link 200); 0 for any other line
1027fn sy_mcap d view uchar 262144 q ulong e ulong to ulong
1028if or ac_pre view d q e symodel or ac_pre view d q e syfw ac_pre view d q e syhuman
1029ret 100
1030end
1031if ac_pre view d q e sylink
1032ret 200
1033end
1034ret 0
1035end
1036
1037note sy_kagt: an agent record at d[q]; its end or 0
1038fn sy_kagt d view uchar 262144 q ulong n ulong to ulong
1039e = ac_lend view d q n
1040if or ge e n not ac_pre view d q e syagent
1041ret 0
1042end
1043if not and sy_nf d q e 6 and sy_fhex d q e 1 sy_ftxt d q e 2 1 64
1044ret 0
1045end
1046if not and sy_ftxt d q e 3 1 16 and sy_futc d q e 4 sy_fnum d q e 5 false
1047ret 0
1048end
1049p = add e 1
1050for i ulong 4
1051e = ac_lend view d p n
1052if ge e n
1053ret 0
1054end
1055c = sy_mcap d p e
1056if eq c 0
1057break
1058end
1059if not and sy_nf d p e 2 sy_ftxt d p e 1 0 c
1060ret 0
1061end
1062p = add e 1
1063end
1064ret sy_kbody d p n 2048
1065end
1066
1067note sy_khid: a hidden record at d[q]; its end or 0
1068fn sy_khid d view uchar 262144 q ulong n ulong to ulong
1069e = ac_lend view d q n
1070if or ge e n not ac_pre view d q e syhid
1071ret 0
1072end
1073if not and sy_nf d q e 4 and sy_fhex d q e 1 sy_fnum d q e 2 false
1074ret 0
1075end
1076if not sy_ftxt d q e 3 1 200
1077ret 0
1078end
1079ret add e 1
1080end
1081
1082note sy_kind: the kind field of the rec line d[p, e): 1 post, 2 letter, 3 agent, 4 hidden, 0 unknown
1083fn sy_kind d view uchar 262144 p ulong e ulong to ulong
1084f = ac_fld view d p e 3
1085g = ac_fend view d f e
1086if ac_peq view d f g sykpost
1087ret 1
1088end
1089if ac_peq view d f g syklet
1090ret 2
1091end
1092if ac_peq view d f g sykagt
1093ret 3
1094end
1095if ac_peq view d f g sykhid
1096ret 4
1097end
1098ret 0
1099end
1100
1101note sy_rec: validate the record at d[p] (rec line "rec\t<cursor>\t<origin>\t<kind>\t<origin_cursor>" and
1102note the record lines); its end, or 0 when malformed. ro: 0 cursor, 1 origin field offset.
1103fn sy_rec d view uchar 262144 p ulong n ulong ro edit ulong 16 to ulong
1104e = ac_lend view d p n
1105if or ge e n not ac_pre view d p e syrecl
1106ret 0
1107end
1108if not and sy_nf d p e 5 and sy_fnum d p e 1 false and sy_fhex d p e 2 sy_fnum d p e 4 false
1109ret 0
1110end
1111f = ac_fld view d p e 1
1112put ro 0 ac_dec view d f ac_fend view d f e
1113put ro 1 ac_fld view d p e 2
1114k = sy_kind d p e
1115q = add e 1
1116if eq k 1
1117ret sy_kpost d q n false
1118end
1119if eq k 2
1120ret sy_kpost d q n true
1121end
1122if eq k 3
1123ret sy_kagt d q n
1124end
1125if eq k 4
1126ret sy_khid d q n
1127end
1128ret 0
1129end
1130
1131note ======== pulling ========
1132
1133note sy_one: import the validated record d[p, x); ro from sy_rec. 1 imported, 2 duplicate, 3 skipped
1134note (its origin is this node), 0 refused, -1 fatal
1135fn sy_one d view uchar 262144 p ulong x ulong ro view ulong 16 to long
1136ib = new uchar 20480 0
1137f = get ro 1
1138if sy_isme view d f
1139ret 3
1140end
1141q = sy_kbuf ib
1142q = ac_cat ib q view part d p sub f p
1143q = ac_cat ib q view part d f 16
1144q = ac_cat ib q view part d add f 16 sub x add f 16
1145if gt q 20480
1146ret 0
1147end
1148ret sy_imp view part ib 0 q
1149end
1150
1151note sy_bad: count a bad record from peer nid; drop the peer once it has served --maxbad of them
1152fn sy_bad nid view uchar 16 to void
1153cn = new uchar 64 0
1154k = sy_name syfbad nid cn
1155v = add sy_getn view part cn 0 k 1
1156put gst 2 add get gst 2 1
1157sy_log sylbad view nid
1158if lt v get gcf 3
1159b = sy_setn view part cn 0 k v
1160ret
1161end
1162b = sy_setn view part cn 0 k 0
1163if sy_ctl sydrop nid view part cn 0 0
1164sy_log syldrop view nid
1165put gst 7 add get gst 7 1
1166end
1167end
1168
1169note sy_tally: count the import result r
1170fn sy_tally r long to void
1171if eq r 1
1172put gst 0 add get gst 0 1
1173end
1174if eq r 2
1175put gst 1 add get gst 1 1
1176end
1177if eq r 3
1178put gst 3 add get gst 3 1
1179end
1180end
1181
1182note sy_recs: validate and import the since answer d[0, n) of peer nid; o[2] is the cursor and advances
1183note past every record handled. The number of records handled, -1 to stop pulling this peer.
1184fn sy_recs nid view uchar 16 o edit ulong 4 d view uchar 262144 n ulong to long
1185ro = new ulong 16 0
1186h = ac_okhdr view d n
1187if lt h 0
1188ret -1
1189end
1190p = ulong h
1191k = ulong 0
1192while lt p n bound 1000
1193x = sy_rec d p n ro
1194if or eq x 0 le get ro 0 get o 2
1195sy_bad nid
1196ret -1
1197end
1198r = sy_one d p x ro
1199if lt r 0
1200put gcf 5 1
1201ret -1
1202end
1203if eq r 0
1204sy_bad nid
1205ret -1
1206end
1207sy_tally r
1208put o 2 get ro 0
1209k = add k 1
1210p = x
1211end
1212ret long k
1213end
1214
1215note sy_fetch: GET /agora/api/since/<cur>?limit=<limit> from the peer base b into d; length or negative
1216fn sy_fetch b view uchar * cur ulong d edit uchar 262144 to long
1217pa = new uchar 128 0
1218q = ac_cat pa 0 sypsnc
1219q = ac_catu pa q cur
1220q = ac_cat pa q syplim
1221q = ac_catu pa q get gcf 2
1222ret sy_hget b pa q d
1223end
1224
1225note sy_batch: one since request to peer nid at base b; o[3] is the length of the cursor file name cn,
1226note where the advanced cursor o[2] is stored. Records handled, or negative to stop.
1227fn sy_batch nid view uchar 16 o edit ulong 4 cn view uchar 64 b view uchar * to long
1228d = new uchar 262144 0
1229put o 2 sy_getn view part cn 0 get o 3
1230n = sy_fetch b get o 2 d
1231if lt n 0
1232sy_log sylfail view nid
1233ret -1
1234end
1235c = sy_recs nid o view d ulong n
1236if not sy_setn view part cn 0 get o 3 get o 2
1237ret -1
1238end
1239ret c
1240end
1241
1242note sy_pull: pull peer nid at base for up to --batches requests
1243fn sy_pull nid view uchar 16 base view uchar * to void
1244o = new ulong 4 0
1245cn = new uchar 64 0
1246put o 3 sy_name syfcur nid cn
1247put gst 4 add get gst 4 1
1248for it ulong get gcf 4
1249c = sy_batch nid o cn base
1250if lt c long get gcf 2
1251ret
1252end
1253end
1254end
1255
1256note sy_pline: one /peers line l ("peer\t<node>\t<base>\t..."): verify and pull it
1257fn sy_pline l view uchar * to void
1258nid = new uchar 16 0
1259e = len l
1260f = ac_fld l 0 e 1
1261if ne sub ac_fend l f e f 16
1262ret
1263end
1264if sy_isme l f
1265ret
1266end
1267b = ac_fld l 0 e 2
1268if gt b e
1269ret
1270end
1271be = ac_fend l b e
1272for i ulong 16
1273put nid i get l add f i
1274end
1275if not sy_verify view part l b sub be b nid
1276sy_log sylunv view nid
1277ret
1278end
1279sy_pull nid view part l b sub be b
1280sy_log sylpull view nid
1281end
1282
1283note sy_peers: every peer the local node lists
1284fn sy_peers to void
1285cs = new ulong 320 0
1286io = new uchar 65536 0
1287d = new uchar 65536 0
1288r = ac_dialap cs io get gcf 1 10000000000
1289if lt r 0
1290ret
1291end
1292st = ac_get cs io sypeers acloop
1293if lt st 0
1294c = hclose cs
1295ret
1296end
1297n = ac_body cs io d
1298if lt n 0
1299ret
1300end
1301h = ac_okhdr view d ulong n
1302if lt h 0
1303ret
1304end
1305p = ulong h
1306while lt p ulong n bound 100000
1307e = ac_lend view d p ulong n
1308if ac_pre view d p e sypeer
1309sy_pline view part d p sub e p
1310end
1311if eq get gcf 5 1
1312ret
1313end
1314p = add e 1
1315end
1316end
1317
1318note sy_local: this node's id from the local well-known answer into gnode
1319fn sy_local to bool
1320w = new uchar 4096 0
1321nd = new uchar 16 0
1322n = sy_wkget shr get gcf 1 16 and get gcf 1 65535 acloop w
1323if lt n 0
1324ret false
1325end
1326if not sy_wkchk w ulong n acloop false nd
1327ret false
1328end
1329for i ulong 16
1330put gnode i get nd i
1331end
1332ret true
1333end
1334
1335note sy_sum: the summary line
1336fn sy_sum to void
1337d = new uchar 256 0
1338q = ac_cat d 0 sylog
1339q = ac_cat d q syldone
1340q = ac_catu d q get gst 0
1341q = ac_cat d q syldup
1342q = ac_catu d q get gst 1
1343q = ac_cat d q sylbad2
1344q = ac_catu d q get gst 2
1345q = ac_cat d q sylown
1346q = ac_catu d q get gst 3
1347q = ac_cat d q sylpls
1348q = ac_catu d q get gst 4
1349q = ac_cat d q sylls
1350q = ac_catu d q get gst 5
1351q = ac_cat d q sylrf
1352q = ac_catu d q get gst 6
1353q = ac_cat d q syldr
1354q = ac_catu d q get gst 7
1355q = ac_catc d q 10
1356ac_say view part d 0 q
1357end
1358
1359fn main to int
1360pb = new uchar 512 0
1361if not sy_args
1362ret 1
1363end
1364if not sy_stok
1365ret 5
1366end
1367n = sy_path syflk pb
1368lk = int fopen pb 0 n 66 384
1369if lt lk 0
1370ret 4
1371end
1372if ne flock lk 6 0
1373ret 4
1374end
1375if not sy_key
1376ret 2
1377end
1378if not sy_local
1379ret 2
1380end
1381sy_reqs
1382sy_peers
1383sy_sum
1384if eq get gcf 5 1
1385ret 3
1386end
1387ret 0
1388end