nabaztag_hack_kit 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. data/.gitignore +7 -0
  2. data/.travis.yml +3 -0
  3. data/CHANGELOG.md +11 -0
  4. data/Gemfile +4 -0
  5. data/README.md +119 -0
  6. data/Rakefile +21 -0
  7. data/bin/mtl_comp +41 -0
  8. data/bin/mtl_merge +22 -0
  9. data/bin/mtl_simu +40 -0
  10. data/bytecode/lib/buffer.mtl +109 -0
  11. data/bytecode/lib/button.mtl +27 -0
  12. data/bytecode/lib/data_helper.mtl +88 -0
  13. data/bytecode/lib/ear.mtl +90 -0
  14. data/bytecode/lib/led.mtl +33 -0
  15. data/bytecode/lib/rfid.mtl +50 -0
  16. data/bytecode/main.mtl +129 -0
  17. data/config.ru +6 -0
  18. data/ext/bytecode/Commands.md +172 -0
  19. data/ext/bytecode/Docs.md +488 -0
  20. data/ext/bytecode/README.md +5 -0
  21. data/ext/bytecode/lib/arp.mtl +159 -0
  22. data/ext/bytecode/lib/cfg.mtl +74 -0
  23. data/ext/bytecode/lib/choreos.mtl +1487 -0
  24. data/ext/bytecode/lib/dhcp.mtl +152 -0
  25. data/ext/bytecode/lib/dns.mtl +89 -0
  26. data/ext/bytecode/lib/http.mtl +84 -0
  27. data/ext/bytecode/lib/tcp.mtl +340 -0
  28. data/ext/bytecode/lib/udp.mtl +49 -0
  29. data/ext/bytecode/lib/util.mtl +74 -0
  30. data/ext/bytecode/lib/var.mtl +15 -0
  31. data/ext/bytecode/lib/wifi.mtl +243 -0
  32. data/ext/bytecode/nominal-ping.mtl +5828 -0
  33. data/ext/mtl/Makefile +42 -0
  34. data/ext/mtl/README.md +13 -0
  35. data/ext/mtl/bc.cpp +1891 -0
  36. data/ext/mtl/conf.bin.sans_password +0 -0
  37. data/ext/mtl/config.txt +5 -0
  38. data/ext/mtl/dumpbc.c +2566 -0
  39. data/ext/mtl/extconf.rb +1 -0
  40. data/ext/mtl/linux_simu.c +271 -0
  41. data/ext/mtl/linux_simuaudio.c +16 -0
  42. data/ext/mtl/linux_simuaudio.h +18 -0
  43. data/ext/mtl/linux_simunet.c +620 -0
  44. data/ext/mtl/linux_simunet.h +7 -0
  45. data/ext/mtl/log.c +297 -0
  46. data/ext/mtl/log.h +20 -0
  47. data/ext/mtl/main_compiler.cpp +104 -0
  48. data/ext/mtl/main_simu.cpp +221 -0
  49. data/ext/mtl/mp3/GTKANAL.H +97 -0
  50. data/ext/mtl/mp3/LAYER3.C +2090 -0
  51. data/ext/mtl/mp3/TABINIT.C +82 -0
  52. data/ext/mtl/mp3/common.c +265 -0
  53. data/ext/mtl/mp3/dct64_i386.c +316 -0
  54. data/ext/mtl/mp3/decode_i386.c +155 -0
  55. data/ext/mtl/mp3/huffman.h +332 -0
  56. data/ext/mtl/mp3/interface.c +258 -0
  57. data/ext/mtl/mp3/mpg123.h +182 -0
  58. data/ext/mtl/mp3/mpglib.h +44 -0
  59. data/ext/mtl/properties.c +293 -0
  60. data/ext/mtl/properties.h +10 -0
  61. data/ext/mtl/simu.c +750 -0
  62. data/ext/mtl/simuaudio.c +662 -0
  63. data/ext/mtl/simuaudio.h +74 -0
  64. data/ext/mtl/simunet.c +400 -0
  65. data/ext/mtl/simunet.h +30 -0
  66. data/ext/mtl/utils/correct_const.sh +34 -0
  67. data/ext/mtl/vaudio.c +677 -0
  68. data/ext/mtl/vaudio.h +46 -0
  69. data/ext/mtl/vbc.h +160 -0
  70. data/ext/mtl/vbc_str.h +166 -0
  71. data/ext/mtl/vcomp/Makefile +29 -0
  72. data/ext/mtl/vcomp/bootstrap.cpp +89 -0
  73. data/ext/mtl/vcomp/compiler.cpp +470 -0
  74. data/ext/mtl/vcomp/compiler.h +200 -0
  75. data/ext/mtl/vcomp/compiler_file.cpp +929 -0
  76. data/ext/mtl/vcomp/compiler_prog.cpp +250 -0
  77. data/ext/mtl/vcomp/compiler_term.cpp +1053 -0
  78. data/ext/mtl/vcomp/compiler_type.cpp +872 -0
  79. data/ext/mtl/vcomp/compiler_var.cpp +289 -0
  80. data/ext/mtl/vcomp/file.cpp +79 -0
  81. data/ext/mtl/vcomp/file.h +39 -0
  82. data/ext/mtl/vcomp/filesystem.h +14 -0
  83. data/ext/mtl/vcomp/interpreter.cpp +85 -0
  84. data/ext/mtl/vcomp/interpreter.h +121 -0
  85. data/ext/mtl/vcomp/memory.cpp +241 -0
  86. data/ext/mtl/vcomp/memory.h +326 -0
  87. data/ext/mtl/vcomp/param.h +95 -0
  88. data/ext/mtl/vcomp/parser.cpp +427 -0
  89. data/ext/mtl/vcomp/parser.h +97 -0
  90. data/ext/mtl/vcomp/parser_xml.cpp +124 -0
  91. data/ext/mtl/vcomp/prodbuffer.cpp +125 -0
  92. data/ext/mtl/vcomp/prodbuffer.h +42 -0
  93. data/ext/mtl/vcomp/resource.h +17 -0
  94. data/ext/mtl/vcomp/stdlib_core.cpp +122 -0
  95. data/ext/mtl/vcomp/terminal.cpp +73 -0
  96. data/ext/mtl/vcomp/terminal.h +30 -0
  97. data/ext/mtl/vcomp/util.cpp +48 -0
  98. data/ext/mtl/vcomp/util.h +31 -0
  99. data/ext/mtl/vinterp.c +1349 -0
  100. data/ext/mtl/vinterp.h +11 -0
  101. data/ext/mtl/vloader.c +127 -0
  102. data/ext/mtl/vloader.h +31 -0
  103. data/ext/mtl/vlog.c +589 -0
  104. data/ext/mtl/vlog.h +69 -0
  105. data/ext/mtl/vmem.c +424 -0
  106. data/ext/mtl/vmem.h +107 -0
  107. data/ext/mtl/vnet.c +255 -0
  108. data/ext/mtl/vnet.h +19 -0
  109. data/lib/nabaztag_hack_kit/message/api.rb +39 -0
  110. data/lib/nabaztag_hack_kit/message/helper.rb +39 -0
  111. data/lib/nabaztag_hack_kit/message.rb +36 -0
  112. data/lib/nabaztag_hack_kit/server.rb +50 -0
  113. data/lib/nabaztag_hack_kit/version.rb +3 -0
  114. data/lib/nabaztag_hack_kit.rb +4 -0
  115. data/nabaztag_hack_kit.gemspec +29 -0
  116. data/public/bytecode.bin +0 -0
  117. data/test/bytecode/helper.mtl +60 -0
  118. data/test/bytecode/native.mtl +28 -0
  119. data/test/bytecode/test.mtl +221 -0
  120. data/test/spec_helper.rb +5 -0
  121. data/test/unit/message_spec.rb +56 -0
  122. metadata +209 -0
@@ -0,0 +1,152 @@
1
+ // --------------- DHCP
2
+
3
+ const DHCP_DISCOVER=1;;
4
+ const DHCP_OFFER=2;;
5
+ const DHCP_REQUEST=3;;
6
+ const DHCP_DECLINE=4;;
7
+ const DHCP_ACK=5;;
8
+
9
+ fun mkdhcp op netip hostip newip =
10
+ let 236+16+14->n in
11
+ let strnew n -> b in
12
+ (
13
+ for i=0;i<n do strset b i 0;
14
+ strcpy b 0 "\1\1\6" 0 3;
15
+ strcpy b 12 netip 0 4;
16
+ strcpy b 12+16 mymac 0 6;
17
+ strcpy b 236 "\99\130\83\99\53\1" 0 6;
18
+ strset b 236+6 op;
19
+ strcpy b 236+7 "\61\7\1" 0 3;
20
+ strcpy b 236+10 mymac 0 6;
21
+ strcpy b 236+16 "\12\7Pabcdef\55\3\1\3\6" 0 14;
22
+ if op==DHCP_REQUEST then strcatlist b::"\54\4"::hostip::"\50\4"::newip::"\255"::nil
23
+ else strcat b "\255"
24
+ );;
25
+
26
+ fun mkdhcpans op tid newip dmac=
27
+ let 236+7->n in
28
+ let strnew n -> b in
29
+ (
30
+ for i=0;i<n do strset b i 0;
31
+ strcpy b 0 "\2\1\6" 0 3;
32
+ strcpy b 4 tid 0 4;
33
+ strcpy b 16 newip 0 4;
34
+ strcpy b 12+16 dmac 0 6;
35
+ strcpy b 236 "\99\130\83\99\53\1" 0 6;
36
+ strset b 236+6 op;
37
+ strcatlist b::"\54\4"::newip::"\51\4\0\1\$51\$80\1\4"::netmask::"\3\4"::netip::"\6\4"::netip::"\15\4home\255"::nil
38
+ );;
39
+
40
+ fun extractdhcp src i type lease submask dns gateway mac=
41
+ if i<strlen src then
42
+ let strget src i -> c in
43
+ if c==255 then [type lease submask dns gateway mac]
44
+ else let strget src i+1 -> len in
45
+ let i+2->i in
46
+ if c==53 then extractdhcp src i+len (strget src i) lease submask dns gateway mac
47
+ else if c==51 then extractdhcp src i+len type (strgetword src i) submask dns gateway mac
48
+ else if c==1 then extractdhcp src i+len type lease (strsub src i 4) dns gateway mac
49
+ else if c==6 then extractdhcp src i+len type lease submask (strsub src i 4) gateway mac
50
+ else if c==3 then extractdhcp src i+len type lease submask dns (strsub src i 4) mac
51
+ else if c==61 then extractdhcp src i+len type lease submask dns gateway (strsub src i+1 6)
52
+ else extractdhcp src i+len type lease submask dns gateway mac;;
53
+
54
+ fun mkdhcpip mac=
55
+ let strnew 4 -> s in
56
+ (
57
+ strcpy s 0 netip 0 4;
58
+ strset s 3 ((strget mac 5)&0x7f)+100;
59
+ s
60
+ );;
61
+
62
+ fun cbnetdhcp src macfrom hostip=
63
+ Secholn "<dhcp"; MACecho macfrom 0 1;
64
+ let strget src 0 -> x in
65
+ let MACecho (strsub src 28 6)0 1 -> mac in
66
+ if x==2 && !strcmp mac mymac then
67
+ (
68
+ let IPecho (strsub src 16 4) 0 1-> newip in
69
+ let extractdhcp src 240 0 nil nil nil nil nil->[type lease submask dns gateway _] in
70
+ if type==DHCP_OFFER then
71
+ (
72
+ Secholn ">>>>>>>>>>>>>>>OFFER";
73
+ udpsend netip 68 ipbroadcast 67 (mkdhcp DHCP_REQUEST netip hostip newip) macbroadcast;
74
+ nil
75
+ )
76
+ else if type==DHCP_ACK then
77
+ (
78
+ Secholn ">>>>>>>>>>>>>>>ACK";
79
+ Secho "server ";IPecho hostip 0 1;
80
+ Secho "ip ";IPecho set netip=newip 0 1;
81
+ Secho "type ";Iecholn type;
82
+ Secho "leasetime ";Iecholn lease;
83
+ Secho "submask ";IPecho set netmask=submask 0 1;
84
+ Secho "dns ";IPecho set netdns=dns 0 1;
85
+ Secho "gateway ";IPecho set netgateway=gateway 0 1;
86
+ nil
87
+ )
88
+ );;
89
+
90
+ fun cbnetdhcp67 src macfrom hostip=
91
+ Secholn "<dhcp"; MACecho macfrom 0 1;
92
+ let strget src 0 -> x in
93
+ let MACecho (strsub src 28 6)0 1 -> mac in
94
+ if x==1 /*&& !strcmp mac mymac*/ then
95
+ (
96
+ let extractdhcp src 240 0 nil nil nil nil nil ->[type _ _ _ _ dmac] in
97
+ let strsub src 4 4 -> tid in
98
+ let mkdhcpip macfrom -> newip in
99
+ if type==DHCP_DISCOVER then
100
+ (
101
+ Secholn ">>>>>>>>>>>>>>>DISCOVER";
102
+ // dump src;
103
+ udpsend netip 67 ipbroadcast 68 (mkdhcpans DHCP_OFFER tid newip dmac) macbroadcast;
104
+ nil
105
+ )
106
+ else if type==DHCP_REQUEST then
107
+ (
108
+ Secholn ">>>>>>>>>>>>>>>REQUEST";
109
+ // dump src;
110
+ udpsend netip 67 ipbroadcast 68 (mkdhcpans DHCP_ACK tid newip dmac) macbroadcast;
111
+ nil
112
+ )
113
+
114
+ );;
115
+
116
+ fun startdhcp=
117
+ udpsend netip 68 ipbroadcast 67 (mkdhcp DHCP_DISCOVER "\0\0\0\0" nil nil) macbroadcast;
118
+ regudp 68 #cbnetdhcp;
119
+ 0;;
120
+
121
+ fun startdhcpserver=
122
+ regudp 67 #cbnetdhcp67;
123
+ 0;;
124
+
125
+ // --------------- net HOOK debut
126
+
127
+ fun net src mac=
128
+ Secho "n ";//MACecho mac 0 1;
129
+ // dump src;
130
+ let strget src 7 -> p in
131
+ (
132
+ if p==6 then cbnetarp src mac // ARP
133
+ else if p==0 then
134
+ let strget src 17 -> ip in
135
+ if ip==6 then cbnettcp src mac
136
+ else if ip==17 then cbnetudp src mac;
137
+ 0
138
+ );
139
+ //buttoncheckevent;
140
+ 0;;
141
+
142
+ fun netstart=
143
+ netCb #net;
144
+ resetarp;
145
+ resettcp;
146
+ resetudp;
147
+ 0;;
148
+
149
+ fun nettime=
150
+ arptime;
151
+ tcptime;
152
+ 0;;
@@ -0,0 +1,89 @@
1
+ // --------------- DNS
2
+ fun parsequ s i= let strfind s i "\0" 0 nil -> j in j+5;;
3
+
4
+ fun parsequs s i n= if n<=0 then i else parsequs s parsequ s i n-1;;
5
+
6
+ fun skipname s i=
7
+ let strgetword s i -> x in
8
+ if (x&0xc000)==0xc000 then i+2
9
+ else (strfind s i "\0" 0 nil)+1;;
10
+
11
+ fun parseans s i n=
12
+ if n<=0 then nil
13
+ else let skipname s i -> j in
14
+ let strgetword s j -> typ in
15
+ if typ==1 then
16
+ strcatlist (itoa strget s j+10)::"."::(itoa strget s j+11)::"."::
17
+ (itoa strget s j+12)::"."::(itoa strget s j+13)::nil
18
+ else parseans s (j+10+strgetword s j+8) n-1;;
19
+
20
+ fun parsemsg s=
21
+ let strgetword s 0 -> id in
22
+ let strgetword s 2 -> code in
23
+ let strgetword s 4 -> nbqu in
24
+ let strgetword s 6 -> nbans in
25
+ if nbans==0 then nil
26
+ else let parsequs s 12 nbqu -> i in
27
+ parseans s i nbans;;
28
+
29
+ fun filterdns src=
30
+ let strfind src 0 "." 0 nil ->i in
31
+ if i!=nil then
32
+ strcat
33
+ strcat ctoa i strsub src 0 i
34
+ filterdns strsub src i+1 nil
35
+ else strcat ctoa strlen src src;;
36
+
37
+ fun question id dns=
38
+ strcatlist (itobin2 id)::"\$01\$00\$00\$01\$00\$00\$00\$00\$00\$00"::(filterdns dns)::"\$00\$00\$01\$00\$01"::nil;;
39
+
40
+ var dnsid=0;;
41
+
42
+ type Dns=[idD domainD reqD timeoutD cbD];;
43
+ var ldnsreq;;
44
+ var ldns;;
45
+
46
+ fun dnsreq domain cb=
47
+ set dnsid=if dnsid==nil then time_ms else dnsid+1;
48
+ let listswitchstr ldns domain -> ip in
49
+ if ip!=nil then call cb[ip]
50
+ else let dump question dnsid domain -> tramedns in
51
+ (
52
+ udpsend netip DNSLOCAL netdns 53 tramedns nil;
53
+ set ldnsreq=[idD:dnsid domainD:domain reqD:tramedns timeoutD:time+5 cbD:cb]::ldnsreq;
54
+ nil
55
+ );
56
+ 0;;
57
+
58
+ fun selectbyid d v= d.idD==v;;
59
+
60
+ fun cbnetdns msg mac ipfrom=
61
+ let strgetword msg 0 -> id in
62
+ let parsemsg msg -> ip in
63
+ let hd select ldnsreq id #selectbyid -> x in
64
+ if x!=nil then
65
+ (
66
+ set ldnsreq=listrem ldnsreq x;
67
+ if ip!=nil then set ldns=[x.domainD ip]::ldns; // ### attention à la taille de la liste
68
+ call x.cbD [ip]
69
+ );
70
+ 0;;
71
+
72
+ fun filterdnsdead l=if l!=nil then let hd l-> d in if d.timeoutD==nil then filterdnsdead tl l else (hd l)::filterdnsdead tl l;;
73
+
74
+ fun dnstime=
75
+ for l=ldnsreq;l!=nil;tl l do let hd l-> d in
76
+ if time-d.timeoutD>=0 then
77
+ (
78
+ set d.timeoutD=nil;
79
+ call d.cbD [nil]
80
+ );
81
+ set ldnsreq=filterdnsdead ldnsreq;
82
+ 0;;
83
+
84
+
85
+ fun startdnsclient=
86
+ regudp DNSLOCAL #cbnetdns;
87
+ set ldnsreq=nil;
88
+ set ldns=nil;
89
+ 0;;
@@ -0,0 +1,84 @@
1
+ //------------------- HTTP
2
+
3
+ //where to put this
4
+ fun isip s i=
5
+ if i>=strlen s then 1
6
+ else let strget s i -> c in
7
+ if (c<'0' || c>'9')&&c!='.' then 0
8
+ else isip s i+1;;
9
+
10
+
11
+ fun cuturl url=
12
+ if !strcmp (strsub url 0 strlen http_prefurl) http_prefurl then cuturl strsub url strlen http_prefurl strlen url
13
+ else let strstr url "/" 0 -> i in
14
+ let if i==nil then url else strsub url 0 i -> addr in
15
+ let strstr addr ":" 0 -> j in
16
+ let if j==nil then [addr 80]
17
+ else [strsub addr 0 j atoi strsub addr j+1 strlen addr] -> [host port] in
18
+ let if i==nil then "/" else strsub url i strlen url -> path in
19
+ [host port path];;
20
+
21
+ fun httpsendreq ip x=
22
+ Secho "found ip>>>>>>>>>>>>>>>>>>>>>>>>>"; Secholn ip;
23
+ let x->[port httpreq] in
24
+ if ip==nil then (call httpreq.cbH [httpreq nil]; nil)
25
+ else
26
+ (
27
+ set httpreq.cnxH=opentcp netip nil useparamip ip port fixarg4 #tcpevent httpreq;
28
+ set httpreq.stateH=HTTP_REACH;
29
+ nil
30
+ );
31
+ 0;;
32
+
33
+ fun httprequest verb url postdata cb type=
34
+ let cuturl url ->[host port path] in
35
+ //let if confGetProxy then strcatlist "http://"::host::":"::(itoa port)::path::nil else path -> path in //Icy-MetaData:1\13\n
36
+ //let if confGetProxy then webip confGetProxyip else host -> host in
37
+ //let if confGetProxy then confGetProxyport else port -> port in
38
+
39
+ let strcatlist verb::" "::path::" HTTP/1.0\13\nUser-Agent: MTL\13\nPragma: no-cache\13\nIcy-MetaData:1\13\nHost: "::host::"\13\n"::
40
+ if postdata==nil then "\13\n"::nil
41
+ else "Content-length: "::(itoa strlen postdata)::"\13\n\13\n"::postdata::nil
42
+ -> request in // création de la chaîne requête
43
+
44
+ let [outputH:request indexH:0 cbH:cb typeH:type stateH:HTTP_SOLVE aliveH:time_ms] -> httpreq in // création de la structure requête
45
+ (
46
+ // Secho "HTTPREQUEST host =";Secholn host;
47
+ if isip host 0 then httpsendreq host [port httpreq]
48
+ else
49
+ (
50
+ dnsreq host fixarg2 #httpsendreq [port httpreq];
51
+ nil
52
+ );
53
+ httpreq // on retourne la structure requête pour pouvoir éventuellement l'interrompre en cours de route
54
+ );;
55
+
56
+
57
+ var http_sep="\13\n\13\n";; // séparateur entre l'en-tête et le corps de la réponse à une requête
58
+
59
+
60
+ //##> retourne le header d'une réponse à une requête
61
+ fun httpgetheader res =
62
+ let strstr res http_sep 0 -> i in
63
+ if i==nil then res
64
+ else strsub res 0 i+strlen http_sep;;
65
+
66
+ //##> retourne le contenu d'une réponse à une requête (sans header)
67
+ fun httpgetcontent res =
68
+ let strstr res http_sep 0 -> i in
69
+ if i==nil then nil
70
+ else strsub res i+strlen http_sep strlen res;;
71
+
72
+ // Cryptage des trames
73
+ fun bintoi3 s x=((strget s x)<<16)+((strget s x+1)<<8)+(strget s x+2);;
74
+
75
+ fun pingextract_ s i res=
76
+ if i<strlen s then
77
+ let (strget s i)-> code in
78
+ if code==255 then res
79
+ else if code>=0 && code<=10 then
80
+ let bintoi3 s i+1 -> len in
81
+ if len>=0 then pingextract_ s i+4+len [code strsub s i+4 len]::res;;
82
+
83
+ fun pingextract s=
84
+ if (strget s 0)==0x7f then pingextract_ s 1 nil;;
@@ -0,0 +1,340 @@
1
+ // ------------- TCP
2
+ const TFIN=0x01;;
3
+ const TSYN=0x02;;
4
+ const TRST=0x04;;
5
+ const TPUSH=0x08;;
6
+ const TACK=0x10;;
7
+ const TURGE=0x20;;
8
+
9
+ const STOFF=-1;;
10
+ const STSYN=0;;
11
+ const STEST=1;;
12
+ const STLISTEN=2;;
13
+ const STFIN=3;;
14
+
15
+ const CLIENT_SEQ_START="\0\0\1\0";;
16
+ const CLIENT_SEQ_NULL="\0\0\0\0";;
17
+
18
+ const TCPWRITE=0;;
19
+ const TCPREAD=1;;
20
+ const TCPCLOSE=-1;;
21
+ const TCPSTART=2;;
22
+
23
+ const TCPMAX=1024;;
24
+
25
+ type Tcp=[stateT locT dstT locpT dstpT seqT ackT cbT macT lastsentT retryT locksendT enableT];;
26
+
27
+ fun mktcp_ ipsrc ipdst portsrc portdst seq ack flag content=
28
+ let strcatlist
29
+ "\$aa\$aa\$03\$00\$00\$00\$08\$00\$45\$00\$00\$00\$00\$00\$00\$00\100\6\0\0"::
30
+ ipsrc::
31
+ ipdst::
32
+ "\0\0\0\0"::
33
+ seq::
34
+ ack::
35
+ "\0\0\$ff\$ff\0\0\0\0"::
36
+ if flag&TSYN then "\2\4\5\$b4"::content::nil // 5.b4 final : taille MSS
37
+ else content::nil
38
+ -> tcp in
39
+ let strlen tcp ->len in
40
+ (
41
+ strputword tcp 8+2 len-8;
42
+ strputword tcp 8+20 portsrc;
43
+ strputword tcp 8+22 portdst;
44
+
45
+ strset tcp 8+32 4*if flag&TSYN then 24 else 20;
46
+ strset tcp 8+33 flag;
47
+
48
+ strputchk tcp 8+10 netChk tcp 8 20 0;
49
+
50
+ let strnew 2 -> s in
51
+ (
52
+ strputword s 0 len-28;
53
+ strputchk tcp 8+36
54
+ netChk tcp 8+20 (len-28) netChk s 0 2 netChk "\0\6" 0 nil netChk tcp 8+12 8 0
55
+ );
56
+ tcp
57
+ );;
58
+
59
+ fun mktcp t flag content=
60
+ // Secholn "mktcp "; Secho "seq "; SEQecho t.seqT 0 1; Secho "ack "; SEQecho t.ackT 0 1;
61
+ mktcp_ t.locT t.dstT t.locpT t.dstpT t.seqT t.ackT flag content;;
62
+
63
+ fun resendtcp t=
64
+ netSend t.lastsentT 0 nil (/*MACecho*/ t.macT /*0 1*/) 0 1;
65
+ 0;;
66
+
67
+ fun headerlen src=((strget src 8+32)>>4)<<2;;
68
+
69
+ fun datalength src=(strgetword src 10)-20-headerlen src;;
70
+
71
+ fun sendtcp t trame=
72
+ // Secholn "tcpSend"; dump trame;
73
+ netSend trame 0 nil (/*MACecho*/ t.macT /*0 1*/) 0 1;
74
+ let strget trame 8+33 -> flag in
75
+ set t.seqT=netSeqAdd t.seqT (datalength trame)+(if flag&(TSYN|TFIN) then 1 else 0);
76
+ 0;;
77
+
78
+ fun sendtcpforretry t trame=
79
+ set t.lastsentT=trame;
80
+ set t.retryT=nil;
81
+ sendtcp t trame;;
82
+
83
+
84
+ fun tcpSend2 mac tcp trame=
85
+ set tcp.macT=mac;
86
+ sendtcpforretry/*sendtcp*/ tcp trame;;
87
+
88
+ // List of all current tcp requests
89
+ var ltcp;;
90
+
91
+
92
+ fun remtcp t=set t.stateT=STOFF; set ltcp=remfromlist ltcp t;;
93
+
94
+ var counttcp;;
95
+
96
+ fun opentcp local localp dst dstp cb=
97
+ let if localp==nil then 1024+set counttcp=((if counttcp==nil then time_ms else counttcp)+1)&16383 else localp -> localp in
98
+ let [stateT:STSYN locT:local dstT:dst locpT:localp dstpT:dstp seqT:CLIENT_SEQ_START ackT:CLIENT_SEQ_NULL cbT:cb enableT:1] -> tcp in
99
+ let mktcp tcp TSYN nil -> trame in
100
+ let dst -> ip in // ajouter le test de passerelle
101
+ (
102
+ set ltcp=tcp::ltcp;
103
+ arpreq ip fixarg2 fixarg3 #tcpSend2 trame tcp;
104
+ tcp
105
+ );;
106
+
107
+ fun listentcp localp cb=
108
+ let [stateT:STLISTEN locpT:localp cbT:cb enableT:1] -> tcp in
109
+ (
110
+ set ltcp=tcp::ltcp
111
+ );;
112
+
113
+ fun findtcp l localp dstp src=
114
+ if l!=nil then let hd l-> t in
115
+ if t.locpT==localp && t.dstpT==dstp && (!vstrcmp src 8+16 t.locT 0 4)&& (!vstrcmp src 8+12 t.dstT 0 4)
116
+ then t
117
+ else if t.stateT==STLISTEN && t.locpT==localp then t
118
+ else findtcp tl l localp dstp src;;
119
+
120
+ fun sendclose t=
121
+ Secholn "## sendclose";
122
+ sendtcp t mktcp t TFIN+TACK nil;
123
+ set t.stateT=STFIN;
124
+ 0;;
125
+
126
+ fun cbnettcp src mac=
127
+ Secho "t";
128
+ let /*Iecholn*/ strgetword src 8+20+2 -> locp in
129
+ let /*Iecholn*/ strgetword src 8+20+0 -> dstp in
130
+ let findtcp ltcp locp dstp src -> t in
131
+ if t!=nil && t.enableT then let t.stateT -> st in
132
+ let /*Iecholn*/ strget src 8+33 -> flag in
133
+ let /*SEQecho*/ (strsub src 8+24 4) /*0 1*/-> rseq in
134
+ let /*SEQecho*/ (strsub src 8+28 4) /*0 1*/-> rack in
135
+ if st==STSYN then
136
+ (
137
+ Secholn "stsyn";
138
+ if (flag==TSYN+TACK) && !vstrcmp (SEQecho(t.seqT)0 1) 0 rack 0 4 then
139
+ (
140
+ set t.ackT=SEQecho (netSeqAdd rseq 1) 0 1;
141
+ sendtcp t mktcp t TACK nil;
142
+ set t.stateT=STEST;
143
+ set t.lastsentT=nil;
144
+ // Secholn "call TCPWRITE";
145
+ call t.cbT [t TCPWRITE nil]
146
+ )
147
+ else
148
+ (
149
+ // Secholn "TSRT+TACK";
150
+ sendtcp t mktcp t TRST+TACK nil;
151
+ remtcp t;
152
+ nil
153
+ )
154
+ )
155
+ else if st==STEST then
156
+ if !vstrcmp t.ackT 0 rseq 0 4 then
157
+ let strgetword src 10 -> iplen in
158
+ let ((strget src 8+32)>>4)<<2 -> tcplen in
159
+ let datalength src -> datalen in
160
+ (
161
+ if datalen then
162
+ (
163
+ // dump src;
164
+ // Iecholn iplen;
165
+ // Iecholn tcplen;
166
+ // Secho "update ackT : add ";
167
+ set t.ackT=netSeqAdd t.ackT datalen
168
+ );
169
+ if flag&TFIN then
170
+ (
171
+ set t.ackT=netSeqAdd t.ackT 1;
172
+ nil
173
+ )
174
+ else if !vstrcmp t.seqT 0 rack 0 4 then
175
+ (
176
+ // Secholn "acquittement de l'envoi";
177
+ set t.lastsentT=nil; // acquittement de l'envoi
178
+ if t.locksendT==1 then
179
+ (
180
+ set t.locksendT=0;
181
+ call t.cbT [t TCPWRITE nil]
182
+ )
183
+ else if t.locksendT==2 then
184
+ (
185
+ sendclose t;
186
+ nil
187
+ )
188
+ )
189
+ else (Secholn "##bad ack"; SEQecho t.seqT 0 1;SEQecho rack 0 1; nil);
190
+ if datalen then
191
+ let 8+20+headerlen src -> start in
192
+ let strsub src start datalen -> data in
193
+ call t.cbT [t TCPREAD data];
194
+ if datalen || flag&TFIN then sendtcp t mktcp t TACK nil;
195
+ if flag&TFIN then
196
+ (
197
+ sendtcp t mktcp t TFIN+TACK nil;
198
+ remtcp t;
199
+ call t.cbT [t TCPCLOSE nil]
200
+ )
201
+ )
202
+ else (/*SEQecho(t.ackT)0 1; SEQecho rseq 0 1;*/Secho "##bs/";sendtcp t mktcp t TACK nil; nil)
203
+ else if st==STFIN then
204
+ (
205
+ Secholn "STFIN";
206
+ set t.ackT=SEQecho (netSeqAdd rseq 1) 0 1;
207
+ sendtcp t mktcp t TACK nil;
208
+ remtcp t;
209
+ nil
210
+ )
211
+ else if st==STLISTEN then
212
+ if flag&TSYN then
213
+ (
214
+ let [stateT:STEST locT:(strsub src 8+16 4) dstT:(strsub src 8+12 4) locpT:locp dstpT:dstp
215
+ seqT:CLIENT_SEQ_START ackT:(netSeqAdd rseq 1) cbT:t.cbT macT:mac enableT:1] -> tcp in
216
+ (
217
+ set ltcp=tcp::ltcp;
218
+ sendtcpforretry tcp mktcp tcp TACK+TSYN nil;
219
+ call tcp.cbT [tcp TCPSTART nil]
220
+ )
221
+ )
222
+ ;;
223
+
224
+
225
+ fun writetcp t msg i=
226
+ if t.stateT!=STEST then nil
227
+ else if t.lastsentT!=nil then
228
+ (
229
+ // Secholn "locksend";
230
+ set t.locksendT=1;
231
+ i
232
+ )
233
+ else let strsub msg i TCPMAX -> content in
234
+ let mktcp t TACK content -> trame in
235
+ (
236
+ sendtcpforretry t trame;
237
+ let i+strlen content -> ni in
238
+ (
239
+ if ni!=strlen msg then set t.locksendT=1;
240
+ ni
241
+ )
242
+ );;
243
+
244
+
245
+ fun closetcp t=
246
+ if t.stateT!=STEST then 0
247
+ else if t.lastsentT!=nil then
248
+ (
249
+ set t.locksendT=2;
250
+ 0
251
+ )
252
+ else sendclose t;
253
+ 0;;
254
+
255
+ fun tcpcb t cb= set t.cbT=cb;;
256
+
257
+ fun enabletcp t v= set t.enableT=v;;
258
+ fun enabledtcpP t= t.enableT;;
259
+
260
+ fun tcptime =
261
+ for l=ltcp;l!=nil;tl l do let hd l-> t in
262
+ if t.lastsentT!=nil then
263
+ (
264
+ if t.retryT!=nil then
265
+ (
266
+ set t.retryT=1+t.retryT;
267
+ if t.retryT>10 then
268
+ (
269
+ remtcp t;
270
+ call t.cbT [t TCPCLOSE nil];
271
+ nil
272
+ )
273
+ else resendtcp t
274
+ )
275
+ else set t.retryT=0
276
+ );
277
+ 0;;
278
+
279
+ fun resettcp=
280
+ set ltcp=nil;
281
+ 0;;
282
+
283
+
284
+ fun tcpread cnx input httpreq=
285
+ if input==nil ||0==strlen input then // erreur ou fin
286
+ ( closetcp cnx; // on ferme la socket
287
+ if httpreq.typeH==HTTP_NORMAL then call httpreq.cbH [httpreq strcatlist rev httpreq.inputH nil] // on retourne ce qui a été reçu
288
+ else call httpreq.cbH [httpreq nil] // HTTP_STREAM or HTTP_DIRECT
289
+ )
290
+ else
291
+ (
292
+ // dump input;
293
+ set lasthttpevent=time;
294
+ set httpreq.aliveH=time_ms;
295
+ if httpreq.typeH==HTTP_NORMAL then set httpreq.inputH=input::httpreq.inputH // on bufferise ce qui a été reçu
296
+ else if httpreq.typeH==HTTP_DIRECT then
297
+ (
298
+ call httpreq.cbH [httpreq input];
299
+ nil
300
+ )
301
+ else let strcat hd httpreq.inputH input -> s in
302
+ let strstr s "\13\10\13\10" 0 -> i in
303
+ if i==nil then
304
+ (
305
+ set httpreq.inputH=s::nil
306
+ )
307
+ else
308
+ (
309
+ set httpreq.inputH=nil;
310
+ set httpreq.typeH=HTTP_DIRECT;
311
+ call httpreq.cbH [httpreq strsub s 0 i];
312
+ if i+4<strlen s then call httpreq.cbH [httpreq strsub s i+4 nil];
313
+ nil
314
+ );
315
+ nil
316
+ );;
317
+
318
+
319
+ fun tcpwrite cnx httpreq=
320
+ set httpreq.stateH=HTTP_CONNECTED;
321
+ set httpreq.aliveH=time_ms;
322
+ if httpreq.outputH!=nil then // s'il y a des choses à envoyer (notamment la première fois)
323
+ (
324
+ set httpreq.indexH=writetcp cnx httpreq.outputH httpreq.indexH; // envoyer ce qui peut l'être
325
+ if httpreq.indexH==nil then // si erreur lors de l'envoi
326
+ ( closetcp cnx; // on ferme la socket
327
+ call httpreq.cbH [httpreq nil] ) // on retourne nil
328
+ else if httpreq.indexH>=strlen httpreq.outputH then // sinon si tout a été envoyé
329
+ ( set httpreq.indexH=nil; // purger les données d'émission
330
+ set httpreq.outputH=nil;
331
+ nil
332
+ )
333
+ );;
334
+
335
+
336
+ fun tcpevent t val msg sock=
337
+ if val==TCPWRITE then tcpwrite t sock
338
+ else if val==TCPCLOSE then tcpread t nil sock
339
+ else tcpread t msg sock;
340
+ 0;;
@@ -0,0 +1,49 @@
1
+ // ------------- UDP
2
+
3
+ fun mkudp ipsrc ipdst portsrc portdst content=
4
+ let strcatlist
5
+ "\$aa\$aa\$03\$00\$00\$00\$08\$00\$45\$00\$00\$00\$00\$00\$00\$00\100\17\0\0"::
6
+ ipsrc::
7
+ ipdst::
8
+ "\0\0\0\0\0\0\0\0"::
9
+ content::
10
+ nil -> udp in
11
+ (
12
+ strputword udp 8+2 28+strlen content;
13
+ strputword udp 8+20 portsrc;
14
+ strputword udp 8+22 portdst;
15
+ strputword udp 8+24 8+strlen content;
16
+ strputchk udp 8+10 netChk udp 8 20 0;
17
+ strputchk udp 8+26 netChk udp 8+20 (8+strlen content) netChk udp 8+24 2 netChk "\0\17" 0 nil netChk udp 8+12 8 0;
18
+ udp
19
+ );;
20
+
21
+ fun udpSend2 mac udp=
22
+ Secho ">u";
23
+ // dump udp;
24
+ netSend udp 0 nil (/*MACecho*/ mac /*0 1*/) 0 1;;
25
+
26
+ fun udpsend local localp dst dstp content mac=
27
+ let mkudp local dst localp dstp content -> udp in
28
+ if mac!=nil then udpSend2 mac udp
29
+ else let dst -> ip in // ajouter le test de passerelle
30
+ arpreq ip fixarg2 #udpSend2 udp;;
31
+
32
+ var ludp;;
33
+
34
+ fun remudp l port=
35
+ if l!=nil then let hd l ->[p _] in if p==port then remudp tl l port else (hd l)::remudp tl l port;;
36
+
37
+ fun regudp port cb=
38
+ set ludp=[port cb]::remudp ludp port;;
39
+
40
+ fun unregudp port=
41
+ set ludp=remudp ludp port;;
42
+
43
+ fun resetudp= set ludp=nil;;
44
+
45
+ fun cbnetudp src mac=
46
+ Secho "<u";
47
+ let Iecholn strgetword src 8+20+2 -> locp in
48
+ let listswitch ludp locp -> cb in
49
+ call cb [strsub src 8+20+8 nil mac strsub src 20 4];;