nabaztag_hack_kit 0.1.0.beta3 → 0.1.0.beta8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (173) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +7 -2
  3. data/.gitmodules +3 -0
  4. data/.travis.yml +2 -2
  5. data/CHANGELOG.md +17 -2
  6. data/LICENSE +21 -0
  7. data/README.md +115 -44
  8. data/Rakefile +2 -6
  9. data/bytecode/{Commands.md → _docs/commands.md} +0 -0
  10. data/bytecode/{Docs.md → _docs/grammar.md} +0 -0
  11. data/bytecode/_docs/grammar.pdf +0 -0
  12. data/bytecode/{src → _original}/README.md +0 -0
  13. data/bytecode/{src → _original}/lib/arp.mtl +0 -0
  14. data/bytecode/{src → _original}/lib/cfg.mtl +0 -0
  15. data/bytecode/{src → _original}/lib/choreos.mtl +0 -0
  16. data/bytecode/{src → _original}/lib/dhcp.mtl +0 -0
  17. data/bytecode/{src → _original}/lib/dns.mtl +0 -0
  18. data/bytecode/{src → _original}/lib/http.mtl +0 -0
  19. data/bytecode/{src → _original}/lib/tcp.mtl +0 -0
  20. data/bytecode/{src → _original}/lib/udp.mtl +0 -0
  21. data/bytecode/{src → _original}/lib/util.mtl +0 -0
  22. data/bytecode/{src → _original}/lib/var.mtl +0 -0
  23. data/bytecode/{src → _original}/lib/wav.mtl +0 -0
  24. data/bytecode/{src → _original}/lib/wifi.mtl +0 -0
  25. data/bytecode/{src → _original}/nominal-ping.mtl +0 -0
  26. data/bytecode/main.mtl +11 -11
  27. data/bytecode/test/test.mtl +8 -8
  28. data/examples/basic/Gemfile +1 -3
  29. data/examples/basic/config.ru +14 -4
  30. data/examples/record/Gemfile +0 -5
  31. data/examples/record/{config.ru.example → config.ru} +2 -2
  32. data/examples/record/server.rb +0 -2
  33. data/ext/Makefile +10 -0
  34. data/ext/bin/mtl_comp +29 -0
  35. data/{bytecode → ext}/bin/mtl_merge +0 -0
  36. data/ext/bin/mtl_simu +29 -0
  37. data/{bytecode/src/mtl → ext}/extconf.rb +0 -0
  38. data/ext/mtl_linux/.gitignore +7 -0
  39. data/ext/mtl_linux/LICENSE +21 -0
  40. data/ext/mtl_linux/Makefile +116 -0
  41. data/ext/mtl_linux/MetalC.project +196 -0
  42. data/ext/mtl_linux/conf.bin +0 -0
  43. data/{bytecode/src/mtl → ext/mtl_linux}/conf.bin.sans_password +0 -0
  44. data/{bytecode/src/mtl → ext/mtl_linux}/config.txt +0 -0
  45. data/ext/mtl_linux/inc/log.h +21 -0
  46. data/{bytecode/src/mtl → ext/mtl_linux/inc}/properties.h +0 -0
  47. data/{bytecode/src/mtl/linux_simuaudio.h → ext/mtl_linux/inc/simu/linux/simuaudio.h} +0 -0
  48. data/{bytecode/src/mtl/linux_simunet.h → ext/mtl_linux/inc/simu/linux/simunet.h} +0 -0
  49. data/{bytecode/src/mtl → ext/mtl_linux/inc/simu/win}/simuaudio.h +18 -18
  50. data/{bytecode/src/mtl → ext/mtl_linux/inc/simu/win}/simunet.h +0 -0
  51. data/ext/mtl_linux/inc/vcomp/compiler.h +200 -0
  52. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/file.h +11 -11
  53. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/filesystem.h +0 -0
  54. data/ext/mtl_linux/inc/vcomp/interpreter.h +121 -0
  55. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/memory.h +120 -92
  56. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/param.h +0 -0
  57. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/parser.h +27 -27
  58. data/ext/mtl_linux/inc/vcomp/prodbuffer.h +42 -0
  59. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/resource.h +1 -1
  60. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/terminal.h +8 -8
  61. data/{bytecode/src/mtl → ext/mtl_linux/inc}/vcomp/util.h +7 -7
  62. data/{bytecode/src/mtl → ext/mtl_linux/inc/vm}/vaudio.h +0 -0
  63. data/{bytecode/src/mtl → ext/mtl_linux/inc/vm}/vbc.h +1 -1
  64. data/ext/mtl_linux/inc/vm/vbc_str.h +167 -0
  65. data/{bytecode/src/mtl → ext/mtl_linux/inc/vm}/vinterp.h +0 -0
  66. data/{bytecode/src/mtl → ext/mtl_linux/inc/vm}/vloader.h +0 -0
  67. data/{bytecode/src/mtl → ext/mtl_linux/inc/vm}/vlog.h +0 -0
  68. data/{bytecode/src/mtl → ext/mtl_linux/inc/vm}/vmem.h +0 -0
  69. data/{bytecode/src/mtl → ext/mtl_linux/inc/vm}/vnet.h +0 -0
  70. data/ext/mtl_linux/nominal.mtl +3959 -0
  71. data/ext/mtl_linux/src/comp/main.cpp +123 -0
  72. data/ext/mtl_linux/src/simu/dumpbc.c +2566 -0
  73. data/ext/mtl_linux/src/simu/linux/main.cpp +288 -0
  74. data/ext/mtl_linux/src/simu/linux/simu.c +309 -0
  75. data/ext/mtl_linux/src/simu/linux/simuaudio.c +44 -0
  76. data/ext/mtl_linux/src/simu/linux/simunet.c +707 -0
  77. data/ext/mtl_linux/src/simu/log.c +340 -0
  78. data/ext/mtl_linux/src/simu/properties.c +368 -0
  79. data/{bytecode/src/mtl → ext/mtl_linux/src/simu/win}/mp3/GTKANAL.H +0 -0
  80. data/{bytecode/src/mtl → ext/mtl_linux/src/simu/win}/mp3/LAYER3.C +0 -0
  81. data/{bytecode/src/mtl → ext/mtl_linux/src/simu/win}/mp3/TABINIT.C +0 -0
  82. data/ext/mtl_linux/src/simu/win/mp3/common.c +302 -0
  83. data/ext/mtl_linux/src/simu/win/mp3/dct64_i386.c +316 -0
  84. data/ext/mtl_linux/src/simu/win/mp3/decode_i386.c +160 -0
  85. data/ext/mtl_linux/src/simu/win/mp3/huffman.h +332 -0
  86. data/ext/mtl_linux/src/simu/win/mp3/interface.c +276 -0
  87. data/{bytecode/src/mtl → ext/mtl_linux/src/simu/win}/mp3/mpg123.h +34 -30
  88. data/ext/mtl_linux/src/simu/win/mp3/mpglib.h +47 -0
  89. data/ext/mtl_linux/src/simu/win/simu.c +819 -0
  90. data/ext/mtl_linux/src/simu/win/simuaudio.c +787 -0
  91. data/ext/mtl_linux/src/simu/win/simunet.c +489 -0
  92. data/ext/mtl_linux/src/vcomp/bootstrap.cpp +95 -0
  93. data/ext/mtl_linux/src/vcomp/compiler.cpp +587 -0
  94. data/ext/mtl_linux/src/vcomp/compiler_file.cpp +1253 -0
  95. data/ext/mtl_linux/src/vcomp/compiler_prog.cpp +437 -0
  96. data/ext/mtl_linux/src/vcomp/compiler_term.cpp +1500 -0
  97. data/ext/mtl_linux/src/vcomp/compiler_type.cpp +1238 -0
  98. data/ext/mtl_linux/src/vcomp/compiler_var.cpp +502 -0
  99. data/ext/mtl_linux/src/vcomp/file.cpp +100 -0
  100. data/ext/mtl_linux/src/vcomp/interpreter.cpp +93 -0
  101. data/ext/mtl_linux/src/vcomp/memory.cpp +313 -0
  102. data/ext/mtl_linux/src/vcomp/parser.cpp +584 -0
  103. data/ext/mtl_linux/src/vcomp/parser_xml.cpp +137 -0
  104. data/{bytecode/src/mtl → ext/mtl_linux/src}/vcomp/prodbuffer.cpp +57 -36
  105. data/ext/mtl_linux/src/vcomp/stdlib_core.cpp +130 -0
  106. data/ext/mtl_linux/src/vcomp/terminal.cpp +80 -0
  107. data/{bytecode/src/mtl → ext/mtl_linux/src}/vcomp/util.cpp +18 -11
  108. data/ext/mtl_linux/src/vm/vaudio.c +830 -0
  109. data/ext/mtl_linux/src/vm/vinterp.c +1780 -0
  110. data/ext/mtl_linux/src/vm/vloader.c +139 -0
  111. data/ext/mtl_linux/src/vm/vlog.c +782 -0
  112. data/ext/mtl_linux/src/vm/vmem.c +461 -0
  113. data/ext/mtl_linux/src/vm/vnet.c +298 -0
  114. data/{bytecode/src/mtl → ext/mtl_linux}/utils/correct_const.sh +0 -0
  115. data/lib/nabaztag_hack_kit/server.rb +4 -7
  116. data/lib/nabaztag_hack_kit/version.rb +1 -1
  117. data/lib/nabaztag_hack_kit.rb +2 -2
  118. data/nabaztag_hack_kit.gemspec +10 -11
  119. data/spec/integration/server_spec.rb +3 -3
  120. data/spec/spec_helper.rb +2 -0
  121. data/spec/unit/message_spec.rb +12 -18
  122. data/spec/unit/server_spec.rb +13 -10
  123. metadata +145 -137
  124. data/bytecode/bin/mtl_comp +0 -50
  125. data/bytecode/bin/mtl_simu +0 -49
  126. data/bytecode/src/mtl/Makefile +0 -42
  127. data/bytecode/src/mtl/README.md +0 -13
  128. data/bytecode/src/mtl/bc.cpp +0 -1891
  129. data/bytecode/src/mtl/dumpbc.c +0 -2566
  130. data/bytecode/src/mtl/linux_simu.c +0 -271
  131. data/bytecode/src/mtl/linux_simuaudio.c +0 -16
  132. data/bytecode/src/mtl/linux_simunet.c +0 -620
  133. data/bytecode/src/mtl/log.c +0 -297
  134. data/bytecode/src/mtl/log.h +0 -20
  135. data/bytecode/src/mtl/main_compiler.cpp +0 -104
  136. data/bytecode/src/mtl/main_simu.cpp +0 -221
  137. data/bytecode/src/mtl/mp3/common.c +0 -265
  138. data/bytecode/src/mtl/mp3/dct64_i386.c +0 -316
  139. data/bytecode/src/mtl/mp3/decode_i386.c +0 -155
  140. data/bytecode/src/mtl/mp3/huffman.h +0 -332
  141. data/bytecode/src/mtl/mp3/interface.c +0 -258
  142. data/bytecode/src/mtl/mp3/mpglib.h +0 -44
  143. data/bytecode/src/mtl/properties.c +0 -293
  144. data/bytecode/src/mtl/simu.c +0 -750
  145. data/bytecode/src/mtl/simuaudio.c +0 -662
  146. data/bytecode/src/mtl/simunet.c +0 -400
  147. data/bytecode/src/mtl/vaudio.c +0 -677
  148. data/bytecode/src/mtl/vbc_str.h +0 -166
  149. data/bytecode/src/mtl/vcomp/Makefile +0 -29
  150. data/bytecode/src/mtl/vcomp/bootstrap.cpp +0 -89
  151. data/bytecode/src/mtl/vcomp/compiler.cpp +0 -470
  152. data/bytecode/src/mtl/vcomp/compiler.h +0 -200
  153. data/bytecode/src/mtl/vcomp/compiler_file.cpp +0 -929
  154. data/bytecode/src/mtl/vcomp/compiler_prog.cpp +0 -250
  155. data/bytecode/src/mtl/vcomp/compiler_term.cpp +0 -1053
  156. data/bytecode/src/mtl/vcomp/compiler_type.cpp +0 -872
  157. data/bytecode/src/mtl/vcomp/compiler_var.cpp +0 -289
  158. data/bytecode/src/mtl/vcomp/file.cpp +0 -79
  159. data/bytecode/src/mtl/vcomp/interpreter.cpp +0 -85
  160. data/bytecode/src/mtl/vcomp/interpreter.h +0 -121
  161. data/bytecode/src/mtl/vcomp/memory.cpp +0 -241
  162. data/bytecode/src/mtl/vcomp/parser.cpp +0 -427
  163. data/bytecode/src/mtl/vcomp/parser_xml.cpp +0 -124
  164. data/bytecode/src/mtl/vcomp/prodbuffer.h +0 -42
  165. data/bytecode/src/mtl/vcomp/stdlib_core.cpp +0 -122
  166. data/bytecode/src/mtl/vcomp/terminal.cpp +0 -73
  167. data/bytecode/src/mtl/vinterp.c +0 -1349
  168. data/bytecode/src/mtl/vloader.c +0 -127
  169. data/bytecode/src/mtl/vlog.c +0 -589
  170. data/bytecode/src/mtl/vmem.c +0 -424
  171. data/bytecode/src/mtl/vnet.c +0 -255
  172. data/examples/basic/Procfile +0 -2
  173. data/examples/basic/server.rb +0 -14
@@ -0,0 +1,707 @@
1
+ #include <string.h>
2
+ #include <stdio.h>
3
+
4
+ #include <resolv.h>
5
+ #include <arpa/inet.h>
6
+ #include <sys/types.h>
7
+ #include <sys/socket.h>
8
+ #include <sys/select.h>
9
+ #include <netinet/in.h>
10
+ #include <unistd.h>
11
+ #include <fcntl.h>
12
+
13
+ #include <errno.h>
14
+
15
+ #include "vmem.h"
16
+ #include "vloader.h"
17
+ #include "vinterp.h"
18
+
19
+ #include "log.h"
20
+
21
+ #define TCPMAX 128 // nombre max de sockets tcp pouvant être ouvertes en même temps
22
+ #define UDPMAX 128 // nombre max de sockets udp pouvant être ouvertes en même temps
23
+
24
+ int tcp_sock[TCPMAX]; // =-1 -> disponible
25
+ int tcp_enable[TCPMAX];
26
+ int tcp_listen[TCPMAX]; // si 1, la socket est en listen (et donc une action sur la socket est une connection)
27
+ int tcp_writeEventToNotify[TCPMAX]; // si 1, on a envoyé un write event qui n'a pas encore été suivi d'un read ou write
28
+
29
+ int udp_sock[UDPMAX];
30
+ int udp_port[UDPMAX]; // port=0 -> disponible
31
+
32
+
33
+ // déclarations
34
+ int inet_addr_bin(char *ip);
35
+ int simunetinit(void);
36
+ int checkNetworkEvents(void);
37
+ int checkTcpEvents(void);
38
+ int tcpEventRead(int fd);
39
+ int tcpEventWrite(int fd);
40
+ int tcpbysock(int s);
41
+ int tcpgetfree(void);
42
+ int tcpopen(char* dstip,int dstport);
43
+ int tcpclose(int i);
44
+ void tcpenable(int i,int enable);
45
+ int tcpsend(int i,char* msg, int len);
46
+ int tcpservercreate(int port);
47
+ int udpbyport(int p);
48
+ int udpgetfree(void);
49
+ int udpbysock(int s);
50
+ int udpcreate(int port);
51
+ int udpclose(int port);
52
+ int udpsend(int localport,char* dstip,int dstport,char* msg, int len);
53
+ int checkUdpEvents();
54
+ int udpEventRead(int fd);
55
+
56
+
57
+ void helper_write_buffer(char *buf, int res)
58
+ {
59
+ if (res <= 0)
60
+ {
61
+ my_printf(LOG_SIMUNET, "BUF empty\n");
62
+ return ;
63
+ }
64
+
65
+ my_printf(LOG_SIMUNET, "buffer content (len: %d) >>>>>>>>\n", res);
66
+
67
+ // rendre la chaine imprimable
68
+ char printable[2048];
69
+ if (res>2048)
70
+ {
71
+ res = 2048;
72
+ }
73
+ memcpy(printable, buf, 2048);
74
+ int i;
75
+ for (i=0; i<res; ++i)
76
+ if (!isprint(printable[i]) && ('\n' != printable[i]))
77
+ {
78
+ printable[i]='.';
79
+ }
80
+
81
+ my_printf(LOG_SIMUNET, "%.2048s\n", printable);
82
+ my_printf(LOG_SIMUNET, "<<<<<<< end of buffer content\n");
83
+ }
84
+
85
+
86
+
87
+ /**
88
+ Cast parce qu'on nous donne les ip dans un format bizarre
89
+ */
90
+ int inet_addr_bin(char *ip)
91
+ {
92
+ return *(int*)ip;
93
+ }
94
+
95
+
96
+ /**
97
+ Initialiise les structures qui serviront à gérer les fonctions réseau
98
+ */
99
+ int simunetinit(void)
100
+ {
101
+ ;
102
+ memset(&tcp_sock, -1, sizeof(tcp_sock));
103
+ memset(&tcp_enable, 0, sizeof(tcp_enable));
104
+ memset(&tcp_listen, 0, sizeof(tcp_listen));
105
+ memset(&tcp_writeEventToNotify, 0, sizeof(tcp_writeEventToNotify));
106
+ memset(&udp_sock, 0, sizeof(udp_sock));
107
+ memset(&udp_port, 0, sizeof(udp_port));
108
+
109
+ return 0;
110
+ }
111
+
112
+ int checkTcpEvents(void);
113
+
114
+ /**
115
+ Vérifie les évènements qui se sont produits sur les socket ouvertes et
116
+ fait les actions appropriées.
117
+
118
+ TODO peut être possible de retravailler les autres fonctions pour
119
+ avoir les tableaux et valeurs prêtes sans avoir à passer par les
120
+ boucles ?
121
+ */
122
+ int checkNetworkEvents(void)
123
+ {
124
+ checkTcpEvents();
125
+ checkUdpEvents();
126
+ return 0;
127
+ }
128
+
129
+ /////////////// TCP /////////////////
130
+
131
+ int tcpEventRead(int fd);
132
+ int tcpEventWrite(int fd);
133
+ int tcpbysock(int s);
134
+
135
+ int checkTcpEvents(void)
136
+ {
137
+ fd_set fdset_r, fdset_w, fdset_err;
138
+ int nfds = 0;
139
+ int maxval = 0; // doit pouvoir être dispo directement
140
+ struct timeval timeout = {0, 0} ;
141
+ int i;
142
+
143
+ FD_ZERO(&fdset_r);
144
+ FD_ZERO(&fdset_w);
145
+ FD_ZERO(&fdset_err);
146
+
147
+ for (i=0; i<TCPMAX; ++i)
148
+ {
149
+ if (tcp_sock[i] != -1)
150
+ {
151
+ FD_SET(tcp_sock[i], &fdset_r);
152
+ FD_SET(tcp_sock[i], &fdset_w);
153
+ FD_SET(tcp_sock[i], &fdset_err);
154
+ if (tcp_sock[i] > maxval)
155
+ {
156
+ maxval = tcp_sock[i];
157
+ }
158
+ }
159
+ }
160
+
161
+ int nbevts = select(maxval+1, &fdset_r, NULL /* &fdset_w */, &fdset_err, &timeout);
162
+ if (nbevts < 0)
163
+ {
164
+ my_printf(LOG_SIMUNET, "Sockets : Tcp select failed (%s)\n", strerror(errno));
165
+ return -1;
166
+ }
167
+
168
+ /*** write events ***/
169
+ for (i=0; i<TCPMAX; ++i)
170
+ if (tcp_writeEventToNotify[i])
171
+ {
172
+ tcpEventWrite(tcp_sock[i]);
173
+ }
174
+
175
+ /*** read events ***/
176
+ if (nbevts > 0)
177
+ {
178
+ for (i=0; i<=maxval; ++i)
179
+ {
180
+ if (FD_ISSET(i, &fdset_w))
181
+ {
182
+ tcpEventWrite(i);
183
+ }
184
+ if (FD_ISSET(i, &fdset_r))
185
+ {
186
+ tcpEventRead(i);
187
+ }
188
+ if (FD_ISSET(i, &fdset_err))
189
+ {
190
+ // TODO
191
+ }
192
+ }
193
+ }
194
+
195
+ return 0;
196
+ }
197
+
198
+ /**
199
+ Gère un evt read sur une socket
200
+ */
201
+ int tcpEventRead(int fd)
202
+ {
203
+ /* soit une donnée à lire, soit un accept, soit un close */
204
+ /* accept si on l'attend, close si on lit 0 data, read sinon */
205
+ int idx = tcpbysock(fd);
206
+
207
+ if (idx < 0)
208
+ {
209
+ // TODO gérer l'erreur
210
+ return 0;
211
+ }
212
+
213
+ if (tcp_listen[idx])
214
+ {
215
+ // accept
216
+ struct sockaddr_in cor;
217
+ int ns;
218
+ int sizecor;
219
+ int ni,ip,port;
220
+ char buf[16];
221
+
222
+ tcp_listen[idx] = 0;
223
+
224
+ sizecor=sizeof(cor);
225
+ ns=accept(fd,(struct sockaddr*)&cor,&sizecor);
226
+ if (ns==-1)
227
+ {
228
+ return 1;
229
+ }
230
+
231
+ ni=tcpgetfree();
232
+ if (ni<0)
233
+ {
234
+ close(ns);
235
+ return 1;
236
+ }
237
+
238
+ ip=cor.sin_addr.s_addr;
239
+ port=ntohs(cor.sin_port);
240
+
241
+ tcp_sock[ni]=ns;
242
+
243
+ my_printf(LOG_SIMUNET, "Sockets : accept Tcp from %x:%d (socket=%d)\n",ip,port,ns);
244
+ VPUSH(INTTOVAL(ni));
245
+ VPUSH(INTTOVAL(2));
246
+ sprintf(buf,"%d",idx);
247
+ VPUSH(PNTTOVAL(VMALLOCSTR(buf,strlen(buf))));
248
+ VPUSH(VCALLSTACKGET(sys_start,SYS_CBTCP));
249
+ if (VSTACKGET(0)!=NIL)
250
+ {
251
+ interpGo();
252
+ }
253
+ else
254
+ {
255
+ VPULL();
256
+ VPULL();
257
+ VPULL();
258
+ }
259
+ VPULL();
260
+ return 1;
261
+ }
262
+ // donnée ou close
263
+ else
264
+ {
265
+ char buf[2048];
266
+ int res;
267
+ if (!tcp_enable[idx])
268
+ {
269
+ // Sleep(10);
270
+ // recv(sock,buf,0,0);
271
+ // printf("disabled\n");
272
+ return 1;
273
+ }
274
+
275
+ my_printf(LOG_SIMUNET, "Sockets : Read event on %d\n",fd);
276
+ res=recv(fd,buf,2048,0);
277
+
278
+ helper_write_buffer(buf, res);
279
+
280
+ VPUSH(INTTOVAL(idx));
281
+ if (res>0)
282
+ {
283
+ VPUSH(INTTOVAL(1));
284
+ VPUSH(PNTTOVAL(VMALLOCSTR(buf,res)));
285
+ }
286
+ else
287
+ {
288
+ VPUSH(INTTOVAL(-1));
289
+ VPUSH(NIL);
290
+ }
291
+
292
+ VPUSH(VCALLSTACKGET(sys_start,SYS_CBTCP));
293
+ if (VSTACKGET(0)!=NIL)
294
+ {
295
+ interpGo();
296
+ }
297
+ else
298
+ {
299
+ VPULL();
300
+ VPULL();
301
+ VPULL();
302
+ }
303
+ VPULL();
304
+ return 1;
305
+ }
306
+ }
307
+
308
+ /**
309
+ Gère un evt write sur une socket
310
+ */
311
+ int tcpEventWrite(int fd)
312
+ {
313
+ int idx = tcpbysock(fd);
314
+ if (idx<0)
315
+ {
316
+ my_printf(LOG_SIMUNET, "Sockets : idx < 0\n");
317
+ return 0;
318
+ }
319
+
320
+ tcp_writeEventToNotify[idx] = 0;
321
+
322
+ my_printf(LOG_SIMUNET, "Sockets : Write event on %d\n",idx);
323
+ VPUSH(INTTOVAL(idx));
324
+ VPUSH(INTTOVAL(0));
325
+ VPUSH(NIL);
326
+ VPUSH(VCALLSTACKGET(sys_start,SYS_CBTCP));
327
+ if (VSTACKGET(0)!=NIL)
328
+ {
329
+ interpGo();
330
+ }
331
+ else
332
+ {
333
+ VPULL();
334
+ VPULL();
335
+ VPULL();
336
+ }
337
+ VPULL();
338
+ return 1;
339
+ }
340
+
341
+ /**
342
+ Retrouve l'indice d'une socket dans le tableau tcp_sock
343
+ */
344
+ int tcpbysock(int s)
345
+ {
346
+ int i;
347
+ for(i=0; i<TCPMAX; i++) if (tcp_sock[i]==s)
348
+ {
349
+ return i;
350
+ }
351
+ return -1;
352
+ }
353
+
354
+ /**
355
+ Renvoie l'indice du premier emplacement libre (socket pas en cours
356
+ d'utilisation) du tableau tcp_sock
357
+ */
358
+ int tcpgetfree(void)
359
+ {
360
+ int i;
361
+ for(i=0; i<TCPMAX; i++) if (tcp_sock[i]==-1)
362
+ {
363
+ tcp_enable[i]=1;
364
+ tcp_listen[i]=0;
365
+ return i;
366
+ }
367
+ return -1;
368
+ }
369
+
370
+ /**
371
+ Ouvre une connection tcp
372
+
373
+ dstip:
374
+ */
375
+ int tcpopen(char* dstip,int dstport)
376
+ {
377
+ int socktcp;
378
+ struct sockaddr_in ina;
379
+ int opt=1;
380
+
381
+ int i=tcpgetfree();
382
+ if (i<0)
383
+ {
384
+ return i;
385
+ }
386
+
387
+ socktcp=socket(AF_INET,SOCK_STREAM,0);
388
+ if (socktcp==-1)
389
+ {
390
+ return -1;
391
+ }
392
+
393
+ ina.sin_family = AF_INET;
394
+ ina.sin_port = htons((unsigned short)dstport);
395
+ ina.sin_addr.s_addr = inet_addr_bin(dstip);
396
+
397
+ fcntl(socktcp, F_SETFD, O_NONBLOCK);
398
+
399
+ if (connect(socktcp,(struct sockaddr *)&ina,sizeof(ina))!=0)
400
+ // ici dans le code windows il y a une condition si SOCKETWOULDBLOCK
401
+ {
402
+ my_printf(LOG_SIMUNET, "Sockets : Tcp connect failed (%s)\n", strerror(errno));
403
+ close(socktcp);
404
+ return -1;
405
+ }
406
+ my_printf(LOG_SIMUNET, "Sockets : create Tcp %s:%d (socket=%d)\n",inet_ntoa(ina.sin_addr),dstport,socktcp);
407
+
408
+ tcp_sock[i]=socktcp;
409
+ tcp_writeEventToNotify[i] = 1;
410
+
411
+ return i;
412
+ }
413
+
414
+ /**
415
+ Ferme une connection tcp
416
+ */
417
+ int tcpclose(int i)
418
+ {
419
+ if ((i>=0)&&(i<TCPMAX)&&(tcp_sock[i]!=-1))
420
+ {
421
+ my_printf(LOG_SIMUNET, "Sockets : Tcp close %d\n",tcp_sock[i]);
422
+ close(tcp_sock[i]);
423
+ tcp_sock[i]=-1;
424
+ }
425
+ return i;
426
+ }
427
+
428
+ /**
429
+ ?
430
+ */
431
+ void tcpenable(int i,int enable)
432
+ {
433
+ if ((i>=0)&&(i<TCPMAX)&&(tcp_sock[i]!=-1))
434
+ {
435
+ char buf[16];
436
+ if (enable!=tcp_enable[i])
437
+ {
438
+ tcp_enable[i]=enable;
439
+ if (enable)
440
+ {
441
+ recv(tcp_sock[i],buf,0,0);
442
+ }
443
+ }
444
+ }
445
+ }
446
+
447
+
448
+ /**
449
+ Envoie des données sur une connection tcp déjà ouverte
450
+ */
451
+ int tcpsend(int i,char* msg, int len)
452
+ {
453
+ if ((i>=0)&&(i<TCPMAX)&&(tcp_sock[i]!=-1))
454
+ {
455
+ int res=send(tcp_sock[i],msg,len,0);
456
+ helper_write_buffer(msg, len);
457
+ if (res<0)
458
+ {
459
+ my_printf(LOG_SIMUNET, "Sockets: Tcp could not send data (%s)\n", strerror(errno));
460
+ // ici dans le code windows il y a une condition si SOCKETWOULDBLOCK
461
+ res=-1;
462
+ }
463
+ return res;
464
+ }
465
+ return -1;
466
+ }
467
+
468
+ /**
469
+ Ouvre le port spécifié et attend des connections dessus
470
+ */
471
+ int tcpservercreate(int port)
472
+ {
473
+ int opt=1;
474
+ struct sockaddr_in ina;
475
+ int socksrv;
476
+ int i=tcpgetfree();
477
+ if (i<0)
478
+ {
479
+ return i;
480
+ }
481
+
482
+ socksrv=socket(AF_INET,SOCK_STREAM,0);
483
+ if (socksrv==-1)
484
+ {
485
+ return -1;
486
+ }
487
+ setsockopt (socksrv, SOL_SOCKET, SO_REUSEADDR,(char*) &opt, sizeof (opt));
488
+ ina.sin_family = AF_INET;
489
+ ina.sin_port = htons((unsigned short)port);
490
+ ina.sin_addr.s_addr = INADDR_ANY;
491
+
492
+ if (bind(socksrv,(struct sockaddr*)&ina,sizeof(ina))!=0)
493
+ {
494
+ my_printf(LOG_SIMUNET, "Sockets : Tcp port %d busy (%s)\n",port, strerror(errno));
495
+ close(socksrv);
496
+ return -1;
497
+ }
498
+ if (listen(socksrv,3)!=0)
499
+ {
500
+ my_printf(LOG_SIMUNET, "Sockets : Tcp port %d listen error (%s)\n",port, strerror(errno));
501
+ close(socksrv);
502
+ return -1;
503
+ }
504
+ my_printf(LOG_SIMUNET, "Sockets : create Tcp server :%d (socket=%d) (%s)\n",port,socksrv, strerror(errno));
505
+ tcp_sock[i]=socksrv;
506
+ tcp_listen[i]=1;
507
+
508
+ return 0;
509
+ }
510
+
511
+
512
+ /////////////// UDP /////////////////
513
+
514
+ int udpbyport(int p)
515
+ {
516
+ int i;
517
+ for(i=0; i<UDPMAX; i++) if (udp_port[i]==p)
518
+ {
519
+ return i;
520
+ }
521
+ return -1;
522
+ }
523
+
524
+ int udpgetfree(void)
525
+ {
526
+ return udpbyport(0);
527
+ }
528
+
529
+ int udpbysock(int s)
530
+ {
531
+ int i;
532
+ for(i=0; i<UDPMAX; i++) if ((udp_port[i])&&(udp_sock[i]==s))
533
+ {
534
+ return i;
535
+ }
536
+ return -1;
537
+ }
538
+
539
+ int udpcreate(int port)
540
+ {
541
+ int sockudp;
542
+ struct sockaddr_in ina;
543
+ int opt=1;
544
+ int i;
545
+
546
+ udpclose(port);
547
+
548
+ i=udpgetfree();
549
+ if (i<0)
550
+ {
551
+ return i;
552
+ }
553
+
554
+ sockudp=socket(AF_INET,SOCK_DGRAM,0);
555
+ if (sockudp==-1)
556
+ {
557
+ return -1;
558
+ }
559
+
560
+ ina.sin_family = AF_INET;
561
+ ina.sin_port = htons((unsigned short)port);
562
+ ina.sin_addr.s_addr = INADDR_ANY;
563
+ if (bind(sockudp,(struct sockaddr*)&ina,sizeof(ina))!=0)
564
+ {
565
+ my_printf(LOG_SIMUNET, "Sockets : Udp port %d busy\n",port);
566
+ close(sockudp);
567
+ return -1;
568
+ }
569
+ setsockopt(sockudp, SOL_SOCKET, SO_REUSEADDR, (char*)&opt, sizeof (opt));
570
+ setsockopt(sockudp, SOL_SOCKET, SO_BROADCAST, (char*)&opt, sizeof (opt));
571
+ my_printf(LOG_SIMUNET, "Sockets : create Udp :%d (socket=%d)\n",port,sockudp);
572
+
573
+ udp_port[i]=port;
574
+ udp_sock[i]=sockudp;
575
+ return i;
576
+ }
577
+
578
+ int udpclose(int port)
579
+ {
580
+ int i=udpbyport(port);
581
+ if (i>=0)
582
+ {
583
+ close(udp_sock[i]);
584
+ udp_port[i]=0;
585
+ }
586
+ return i;
587
+ }
588
+
589
+ int udpsend(int localport,char* dstip,int dstport,char* msg, int len)
590
+ {
591
+ struct sockaddr_in ina;
592
+ int i;
593
+
594
+ i=udpbyport(localport);
595
+ if (i<0)
596
+ {
597
+ i=udpcreate(localport);
598
+ }
599
+ if (i>=0)
600
+ {
601
+ ina.sin_family = AF_INET;
602
+ ina.sin_port = htons((unsigned short)dstport);
603
+ ina.sin_addr.s_addr = inet_addr_bin(dstip);
604
+
605
+ // hack pour dns. dans le bytecode de simu, on considère que
606
+ // 192.168.1.1 est le dns. Si on essaie d'envoyer un paquet à
607
+ // cette adresse, il faut translater vers le "vrai" dns.
608
+ in_addr_t ip_192_168_1_1 = inet_addr("192.168.1.1");
609
+ if (!memcmp(&(ina.sin_addr),&ip_192_168_1_1, sizeof(ip_192_168_1_1)))
610
+ {
611
+ my_printf(LOG_SIMUNET, "Sockets : (hack) converting 192.168.1.1 to real dns ip\n");
612
+ res_init();
613
+ if (_res.nscount <= 0)
614
+ {
615
+ my_printf(LOG_SIMUNET, "Fatal error: no DNS available. Should abort\n");
616
+ return 0;
617
+ };
618
+ ina.sin_addr = _res.nsaddr_list[0].sin_addr;
619
+ }
620
+
621
+ my_printf(LOG_SIMUNET, "Sockets : send %d bytes on chn %d\n",len,i);
622
+ sendto(udp_sock[i],msg,len,0,(struct sockaddr *)&ina,sizeof(ina));
623
+ return 0;
624
+ }
625
+ return -1;
626
+ }
627
+
628
+
629
+
630
+ int checkUdpEvents(void)
631
+ {
632
+ fd_set fdset_r, fdset_w, fdset_err;
633
+ int nfds = 0;
634
+ int maxval = 0; // doit pouvoir être dispo directement
635
+ struct timeval timeout = {0, 0} ;
636
+ int i;
637
+
638
+ FD_ZERO(&fdset_r);
639
+ FD_ZERO(&fdset_w);
640
+ FD_ZERO(&fdset_err);
641
+
642
+ for (i=0; i<UDPMAX; ++i)
643
+ {
644
+ if (udp_sock[i] != -1)
645
+ {
646
+ FD_SET(udp_sock[i], &fdset_r);
647
+ /* FD_SET(udp_sock[i], &fdset_w); */
648
+ /* FD_SET(udp_sock[i], &fdset_err); */
649
+ if (udp_sock[i] > maxval)
650
+ {
651
+ maxval = udp_sock[i];
652
+ }
653
+ }
654
+ }
655
+
656
+ int nbevts = select(maxval+1, &fdset_r, &fdset_w, &fdset_err, &timeout);
657
+
658
+ if (nbevts < 0)
659
+ {
660
+ my_printf(LOG_SIMUNET, "Sockets : Udp select failed (%s)\n", strerror(errno));
661
+ return -1;
662
+ }
663
+
664
+ if (nbevts > 0)
665
+ {
666
+ for (i=0; i<=maxval; ++i)
667
+ {
668
+ if (FD_ISSET(i, &fdset_r))
669
+ {
670
+ udpEventRead(i);
671
+ }
672
+ }
673
+ }
674
+
675
+ return 0;
676
+ }
677
+
678
+ int udpEventRead(int fd)
679
+ {
680
+ char buf[4096];
681
+ struct sockaddr_in add;
682
+ int i=udpbysock(fd);
683
+ int l=sizeof(add);
684
+ int res=recvfrom(fd,buf,4096,0,(struct sockaddr *)&add,&l);
685
+ if (res<0)
686
+ {
687
+ return 1;
688
+ }
689
+
690
+ my_printf(LOG_SIMUNET, "Sockets : UDP Read %d bytes on :%d from %s:%d\n",res,udp_port[i],inet_ntoa(add.sin_addr),ntohs(add.sin_port));
691
+ VPUSH(INTTOVAL(i));
692
+ VPUSH(PNTTOVAL(VMALLOCSTR(buf,res)));
693
+ VPUSH(PNTTOVAL(VMALLOCSTR((char*)&add.sin_addr.s_addr,4)));
694
+ VPUSH(VCALLSTACKGET(sys_start,SYS_CBUDP));
695
+ if (VSTACKGET(0)!=NIL)
696
+ {
697
+ interpGo();
698
+ }
699
+ else
700
+ {
701
+ VPULL();
702
+ VPULL();
703
+ VPULL();
704
+ }
705
+ VPULL();
706
+ return 1;
707
+ }