Check-in [127dfc04ac]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Send first packet to open DHT both to IPv4 and IPv6
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 127dfc04acc88ca39b3b6acf97e158884a89329e
User & Date: bernd 2019-10-11 20:07:52
Context
2019-10-14
12:01
Some refactoring check-in: 3ac838b3fc user: bernd tags: trunk
2019-10-11
20:07
Send first packet to open DHT both to IPv4 and IPv6 check-in: 127dfc04ac user: bernd tags: trunk
2019-10-10
17:45
Bump version number check-in: 9277fd8b03 user: bernd tags: trunk, 0.9.1-20191010
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to cmd.fs.

186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
...
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
: n>cmd ( n -- addr ) cells >r
    o IF  token-table  ELSE  setup-table  THEN
    $@ r@ u<= !!function!! r> + ;

: cmd@ ( -- u ) buf-state 2@ over + >r p@+ r> over - buf-state 2! 64>n ;

standard:field
-5 cells 0 +field net2o.name
drop

: >net2o-name ( addr -- addr' u )
    net2o.name body> name>string ;
: >net2o-sig ( addr -- addr' u )
    net2o.name 3 cells + $@ ;
: .net2o-num ( off -- )  cell/ '<' emit 0 .r '>' emit space ;
................................................................................
    THEN ;

: send-cmd ( addr u dest -- size ) n64-swap { buf# }
    +send-cmd dest-addr 64@ 64>r set-dest
    cmd( <info> ." send: " outflag .dest-addr dup buf# net2o:see <default> cr )
    max-size^2 1+ 0 DO
	buf# min-size I lshift u<= IF
	    I outflag @ stateless# and IF  send-cX ?punch-cmds
	    ELSE
		send-reply >r over buf# r@ 2! r> send-xt
	    THEN
	    min-size I lshift  UNLOOP
	    64r> dest-addr 64! EXIT  THEN
    LOOP  64r> dest-addr 64!  true !!commands!! ;








|







 







|







186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
...
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
: n>cmd ( n -- addr ) cells >r
    o IF  token-table  ELSE  setup-table  THEN
    $@ r@ u<= !!function!! r> + ;

: cmd@ ( -- u ) buf-state 2@ over + >r p@+ r> over - buf-state 2! 64>n ;

standard:field
-6 cells 0 +field net2o.name
drop

: >net2o-name ( addr -- addr' u )
    net2o.name body> name>string ;
: >net2o-sig ( addr -- addr' u )
    net2o.name 3 cells + $@ ;
: .net2o-num ( off -- )  cell/ '<' emit 0 .r '>' emit space ;
................................................................................
    THEN ;

: send-cmd ( addr u dest -- size ) n64-swap { buf# }
    +send-cmd dest-addr 64@ 64>r set-dest
    cmd( <info> ." send: " outflag .dest-addr dup buf# net2o:see <default> cr )
    max-size^2 1+ 0 DO
	buf# min-size I lshift u<= IF
	    I outflag @ stateless# and IF  send0-xt ?punch-cmds
	    ELSE
		send-reply >r over buf# r@ 2! r> send-xt
	    THEN
	    min-size I lshift  UNLOOP
	    64r> dest-addr 64! EXIT  THEN
    LOOP  64r> dest-addr 64!  true !!commands!! ;

Changes to connect.fs.

231
232
233
234
235
236
237

238



239
240
241
242
243
244
245
	ELSE  !!nokey!!  THEN
    update-key all-ivs ;
: reply-key ( -- ) crypt( ." Reply key: " tmpkey@ .nnb forth:cr )
    reply-key, ( cookie+request ) time-offset! context ]tmpnest
    push-cmd ;

+net2o: gen-reply ( -- ) \g generate a key request reply

    own-crypt? IF  ['] reply-key IS expect-reply?  THEN ;



+net2o: gen-punch-reply ( -- ) ( obsolete dummy ) ;

\ one-shot packets

+net2o: invite ( $:nick+sig $:pk -- ) \g invite someone
    $> ?keysize search-key 2drop
    $> tmp-crypt? dup invit:pend# and ulit, <invite-result>







>
|
>
>
>







231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
	ELSE  !!nokey!!  THEN
    update-key all-ivs ;
: reply-key ( -- ) crypt( ." Reply key: " tmpkey@ .nnb forth:cr )
    reply-key, ( cookie+request ) time-offset! context ]tmpnest
    push-cmd ;

+net2o: gen-reply ( -- ) \g generate a key request reply
    own-crypt? IF
	['] reply-key IS expect-reply?
	['] send-cX   IS send0-xt
	return-addr return-address $10 move
    THEN ;
+net2o: gen-punch-reply ( -- ) ( obsolete dummy ) ;

\ one-shot packets

+net2o: invite ( $:nick+sig $:pk -- ) \g invite someone
    $> ?keysize search-key 2drop
    $> tmp-crypt? dup invit:pend# and ulit, <invite-result>

Changes to helper.fs.

53
54
55
56
57
58
59



60
61

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
...
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
    lastaddr# 0<> to online?
    ind-addr off  !0key ;

: dhtroot-off ( --- )
    dhtroot-addr$ $off
    dhtroot-addr @ ?dup-IF  net2o:dispose-addr  THEN ;




: pk:connect ( code data key u -- )
    connect( [: .time ." Connect to: " dup hex. cr ;] $err )

    net2o:new-context >o rdrop o to connection  setup!
    dest-pk \ set our destination key
    +resend-cmd net2o:connect
    +flow-control +resend
    connect( [: .time ." Connected, o=" o hex. cr ;] $err ) ;

: pk-addr:connect ( code data key u addr -- )
    connect( [: .time ." Connect to: " dup hex. cr ;] $err )
    net2o:new-context >o rdrop o to connection  setup!
    ['] dests is send0-xt  dest-addrs >stack
    dest-pk \ set our destination key
    +resend-cmd net2o:connect
    +flow-control +resend
    connect( [: .time ." Connected, o=" o hex. cr ;] $err ) ;

Forward renat-all

event: :>renat ( -- )  renat-all ;
................................................................................
    swap .net2o:dispose-addr ;

: insert-host? ( flag o addr u -- flag' o )
    3 pick IF  2drop  EXIT  THEN
    check-host? IF  insert-host  ELSE  2drop false  THEN
    rot or swap ;

: make-context ( pk u -- )
    ret0 net2o:new-context >o rdrop dest-pk ;

in net2o : pklookup? ( pkaddr u -- flag )
    2dup keysize2 safe/string hostc$ $! key2| 2dup pkc over str= to ?myself
    2dup >d#id { id }
    id .dht-host $[]# 0= IF  2dup pk-lookup  2dup >d#id to id  THEN
    2dup make-context
    false id dup .dht-host ['] insert-host? $[]map drop nip nip ;
in net2o : pklookup ( pkaddr u -- )







>
>
>


>
|
<






|
|
|







 







<
<
<







53
54
55
56
57
58
59
60
61
62
63
64
65
66

67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
...
294
295
296
297
298
299
300



301
302
303
304
305
306
307
    lastaddr# 0<> to online?
    ind-addr off  !0key ;

: dhtroot-off ( --- )
    dhtroot-addr$ $off
    dhtroot-addr @ ?dup-IF  net2o:dispose-addr  THEN ;

: make-context ( pk u -- )
    ret0 net2o:new-context >o rdrop dest-pk ;

: pk:connect ( code data key u -- )
    connect( [: .time ." Connect to: " dup hex. cr ;] $err )
    make-context
    o to connection  setup!

    +resend-cmd net2o:connect
    +flow-control +resend
    connect( [: .time ." Connected, o=" o hex. cr ;] $err ) ;

: pk-addr:connect ( code data key u addr -- )
    connect( [: .time ." Connect to: " dup hex. cr ;] $err )
    >r make-context  r> dest-addrs >stack
    o to connection  setup!
    ['] dests is send0-xt
    +resend-cmd net2o:connect
    +flow-control +resend
    connect( [: .time ." Connected, o=" o hex. cr ;] $err ) ;

Forward renat-all

event: :>renat ( -- )  renat-all ;
................................................................................
    swap .net2o:dispose-addr ;

: insert-host? ( flag o addr u -- flag' o )
    3 pick IF  2drop  EXIT  THEN
    check-host? IF  insert-host  ELSE  2drop false  THEN
    rot or swap ;




in net2o : pklookup? ( pkaddr u -- flag )
    2dup keysize2 safe/string hostc$ $! key2| 2dup pkc over str= to ?myself
    2dup >d#id { id }
    id .dht-host $[]# 0= IF  2dup pk-lookup  2dup >d#id to id  THEN
    2dup make-context
    false id dup .dht-host ['] insert-host? $[]map drop nip nip ;
in net2o : pklookup ( pkaddr u -- )

Changes to net2o.fs.

455
456
457
458
459
460
461













462
463
464
465
466
467
468
469
470
471
472

473
474
475
476
477
478
479
...
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
...
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
....
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
....
1089
1090
1091
1092
1093
1094
1095
1096

1097
1098
1099
1100
1101
1102
1103
: rtdelay! ( time -- )
    timeouts @ IF \ don't update rtdelay if there were timeouts
	rtdelay 64@ init-delay# 64<> IF  64drop  EXIT  THEN
    THEN
    recv-tick 64@ 64swap 64-
    rtd( ." rtdelay: " 64dup 64>f .ns cr ) rtdelay 64! ;














in net2o : new-context ( -- o )
    context-class new >o timeout( ." new context: " o hex. cr )
    my-key-default to my-key \ set default key
    o contexts !@ next-context !
    o to connection \ current connection
    context-table @ token-table ! \ copy pointer
    init-context# @ context# !  1 init-context# +!
    return-addr return-address $10 move
    ['] no-timeout is timeout-xt ['] .iperr is setip-xt
    ['] noop is punch-done-xt ['] noop is sync-done-xt
    ['] noop is sync-none-xt  ['] noop is ack-xt

    -flow-control
    -1 blocksize !
    1 blockalign !
    config:timeouts# @ to max-timeouts
    end-semas start-semas DO  I 0 pthread_mutex_init drop
    1 pthread-mutexes +LOOP
    64#0 context-ticker 64!@ 64dup 64#0 64<> IF
................................................................................

: >dest ( addr -- ) outbuf destination $10 move ;
: set-dest ( target -- )
    64dup dest-addr 64!  outbuf mapaddr le-64! ;
: set-dest# ( resend# -- )
    n>64 dest-addr 64+!  dest-addr 64@ outbuf mapaddr le-64! ;

User outflag  outflag off

: set-flags ( -- )
    0 outflag !@ outbuf hdrtags c!
    outbuf hdrflags le-uw@ dest-flags le-w! ;

#90 Constant EMSGSIZE

: ?msgsize ( ior -- )
    0< IF
	errno EMSGSIZE <> ?ior
	max-size^2 1- to max-size^2  ." pmtu/2" cr
    THEN ;
................................................................................
    THEN   ret-addr >dest packet-to ;

: send-data-packet ( -- ) +sendX
    header( ." send data " outbuf .header )
    data-map  outbuf-encrypt
    ret-addr >dest packet-to ;

: >send ( addr n -- )
    >r  r@ [ 64bit# qos3# or ]L or outbuf c!  set-flags
    outbuf packet-body min-size r> lshift move ;

: bandwidth+ ( -- )
    ns/burst 64@ 1 tick-init 1+ 64*/ bandwidth-tick 64+! ;

: burst-end ( flag -- flag )
    ticker 64@ bandwidth-tick 64@ 64max next-tick 64! drop false ;

: send-cX ( addr n -- ) +sendX2  >send  send-code-packet ;

\ !!FIXME!! use ffz>, branchless with floating point

: 64ffz< ( 64b -- u / -1 )
    \G find first zero from the right, u is bit position
    64 0 DO
	64dup 64>n 1 and 0= IF  64drop I unloop  EXIT  THEN
	64-2/
................................................................................
Forward new-addr

: send-punch ( addr u -- addr u )
    check-addr1 0= IF  2drop  EXIT  THEN
    temp-addr ret-addr $10 move
    insert-address ret-addr ins-dest
    nat( ticks .ticks ."  send punch to: " ret-addr .addr-path cr )
    2dup send-cX ;

in net2o : punch ( addr u o:connection -- )
    o IF
	new-addr punch-addrs >stack
    ELSE  2drop  THEN ;

: punch-wrap ( xt -- )
................................................................................

: punchs ( addr u o:connection -- )
    \G send a reply to all addresses
    punch-addrs ['] send-punch addrs-loop 2drop ;

: dests ( addr u o:connection -- )
    \G send a reply to all addresses
    dest-addrs ['] send-punch addrs-loop 2drop ;


\ send chunk

\ branchless version using floating point

: send-size ( u -- n )
    min-size umax maxdata umin 1-







>
>
>
>
>
>
>
>
>
>
>
>
>











>







 







<
<
<
<
<
<







 







<
<
<
<






<
<







 







|







 







|
>







455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
...
980
981
982
983
984
985
986






987
988
989
990
991
992
993
....
1006
1007
1008
1009
1010
1011
1012




1013
1014
1015
1016
1017
1018


1019
1020
1021
1022
1023
1024
1025
....
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
....
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
: rtdelay! ( time -- )
    timeouts @ IF \ don't update rtdelay if there were timeouts
	rtdelay 64@ init-delay# 64<> IF  64drop  EXIT  THEN
    THEN
    recv-tick 64@ 64swap 64-
    rtd( ." rtdelay: " 64dup 64>f .ns cr ) rtdelay 64! ;

User outflag  outflag off

: set-flags ( -- )
    0 outflag !@ outbuf hdrtags c!
    outbuf hdrflags le-uw@ dest-flags le-w! ;

: >send ( addr n -- )
    >r  r@ [ 64bit# qos3# or ]L or outbuf c!  set-flags
    outbuf packet-body min-size r> lshift move ;

forward send-code-packet
: send-cX ( addr n -- ) +sendX2  >send  send-code-packet ;

in net2o : new-context ( -- o )
    context-class new >o timeout( ." new context: " o hex. cr )
    my-key-default to my-key \ set default key
    o contexts !@ next-context !
    o to connection \ current connection
    context-table @ token-table ! \ copy pointer
    init-context# @ context# !  1 init-context# +!
    return-addr return-address $10 move
    ['] no-timeout is timeout-xt ['] .iperr is setip-xt
    ['] noop is punch-done-xt ['] noop is sync-done-xt
    ['] noop is sync-none-xt  ['] noop is ack-xt
    ['] send-cX is send0-xt
    -flow-control
    -1 blocksize !
    1 blockalign !
    config:timeouts# @ to max-timeouts
    end-semas start-semas DO  I 0 pthread_mutex_init drop
    1 pthread-mutexes +LOOP
    64#0 context-ticker 64!@ 64dup 64#0 64<> IF
................................................................................

: >dest ( addr -- ) outbuf destination $10 move ;
: set-dest ( target -- )
    64dup dest-addr 64!  outbuf mapaddr le-64! ;
: set-dest# ( resend# -- )
    n>64 dest-addr 64+!  dest-addr 64@ outbuf mapaddr le-64! ;







#90 Constant EMSGSIZE

: ?msgsize ( ior -- )
    0< IF
	errno EMSGSIZE <> ?ior
	max-size^2 1- to max-size^2  ." pmtu/2" cr
    THEN ;
................................................................................
    THEN   ret-addr >dest packet-to ;

: send-data-packet ( -- ) +sendX
    header( ." send data " outbuf .header )
    data-map  outbuf-encrypt
    ret-addr >dest packet-to ;





: bandwidth+ ( -- )
    ns/burst 64@ 1 tick-init 1+ 64*/ bandwidth-tick 64+! ;

: burst-end ( flag -- flag )
    ticker 64@ bandwidth-tick 64@ 64max next-tick 64! drop false ;



\ !!FIXME!! use ffz>, branchless with floating point

: 64ffz< ( 64b -- u / -1 )
    \G find first zero from the right, u is bit position
    64 0 DO
	64dup 64>n 1 and 0= IF  64drop I unloop  EXIT  THEN
	64-2/
................................................................................
Forward new-addr

: send-punch ( addr u -- addr u )
    check-addr1 0= IF  2drop  EXIT  THEN
    temp-addr ret-addr $10 move
    insert-address ret-addr ins-dest
    nat( ticks .ticks ."  send punch to: " ret-addr .addr-path cr )
    outflag @ >r  2dup send-cX  r> outflag ! ;

in net2o : punch ( addr u o:connection -- )
    o IF
	new-addr punch-addrs >stack
    ELSE  2drop  THEN ;

: punch-wrap ( xt -- )
................................................................................

: punchs ( addr u o:connection -- )
    \G send a reply to all addresses
    punch-addrs ['] send-punch addrs-loop 2drop ;

: dests ( addr u o:connection -- )
    \G send a reply to all addresses
    dest-addrs ['] send-punch addrs-loop 2drop
    outflag off ;

\ send chunk

\ branchless version using floating point

: send-size ( u -- n )
    min-size umax maxdata umin 1-