-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathrupicore.scm
301 lines (270 loc) · 10.8 KB
/
rupicore.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#|
LambdaNative - a cross-platform Scheme framework
Copyright (c) 2009-2013, University of British Columbia
All rights reserved.
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the
following conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the following
disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials
provided with the distribution.
* Neither the name of the University of British Columbia nor
the names of its contributors may be used to endorse or
promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|#
;; RUPI - Remote Universal Programming Interface
;; This uses compression+encryption to communicate over tcp/ip
(define rupi:debuglevel 1)
(define rupi:thread-num 0)
(define rupi:error #f)
(define (rupi:log level . x) (if (fx>= rupi:debuglevel level) (apply log-system x)))
(define (rupi:safecall x . y)
(with-exception-catcher
(lambda (e) (rupi:log 1 "rupi:safecall : exception: " (exception->string e)) #f)
(lambda () (apply x y))))
;; We use FastLZ for compression of data
(define rupi:compress u8vector-compress)
(define rupi:decompress u8vector-decompress)
;; Declarations of available encryption algorithms
(define rupi:magic (list (string->u8vector "RupiB"))) ;; 5 characters [Rupi+Encrytiontype]
(define rupi:encode (list u8vector-encrypt-blowfish))
(define rupi:decode (list u8vector-decrypt-blowfish))
(define rupi:setkey (list u8vector-setkey-blowfish))
;; The default uses Blowfish for encryption
(define rupi:magicidx 0)
;; Length conversion functions
(define (rupi:length->data l)
;; We only support length < 16,777,215 so the fourth byte is left for future use
(if (fx>= l 16777215)
(begin (rupi:log 1 "rupi:cmd: Data too long " l) 0)
(u8vector (bitwise-and l #xff)
(bitwise-and (arithmetic-shift l -8) #xff)
(bitwise-and (arithmetic-shift l -16) #xff)
0)))
(define (rupi:data-length sl)
;; Prevent heap overflows with bad packages (length > 16,777,215)
(if (and (fx= (u8vector-length sl) 4) (fx= (u8vector-ref sl 3) 0))
(+ (u8vector-ref sl 0)
(arithmetic-shift (u8vector-ref sl 1) 8)
(arithmetic-shift (u8vector-ref sl 2) 16))
0))
(define (rupi:write keyidx data0 port magicidx)
(let* ((data ((list-ref rupi:encode rupi:magicidx) keyidx (rupi:compress data0)))
(l (u8vector-length data))
(lv (rupi:length->data l)))
(write-subu8vector (list-ref rupi:magic magicidx) 0 5 port)
(write-subu8vector lv 0 4 port)
(write-subu8vector data 0 l port)))
(define (rupi:read keyidx port)
(let ((magic (u8vector 0 0 0 0 0)))
(read-subu8vector magic 0 5 port)
(let ((ridx (list-pos rupi:magic magic)))
(if ridx
(let ((lv (u8vector 0 0 0 0)))
(read-subu8vector lv 0 4 port)
(let* ((l (rupi:data-length lv))
(data (make-u8vector l)))
(if (fx>= l 2)
(begin
(read-subu8vector data 0 l port)
(list ridx (rupi:decompress ((list-ref rupi:decode ridx) keyidx data)))
)
#f
)
)
)
#f
)
)))
(define (rupi:writeobj port obj keyidx magicidx)
(rupi:write keyidx (object->u8vector obj) port magicidx))
(define (rupi:readobj port keyidx)
(let ((rr (rupi:read keyidx port)))
(if (and rr (not (null? rr)) (u8vector? (cadr rr)))
(list (car rr) (u8vector->object (cadr rr)))
#f
)
)
)
;; %%%%%%%%%%%%%%%%%%%%%%%%%%%
;; client
(define rupi:mutex (make-mutex 'rupi:mutex))
(define (rupi:grab!) (mutex-lock! rupi:mutex))
(define (rupi:release!) (mutex-unlock! rupi:mutex))
(define (rupi-client keyidx key addr port)
(let ((t (make-table)))
(table-set! t "ClientAddr" addr)
(table-set! t "ClientPort" port)
(table-set! t "ClientIdx" keyidx)
((list-ref rupi:setkey rupi:magicidx) keyidx key)
t))
(define (rupi-valid? t) (and (table? t) (table-ref t "CmdPort" #f)))
(define (rupi:cmd timeout t cmd . args)
(rupi:grab!)
(set! rupi:error #f)
(let ((res (with-exception-catcher
(lambda (e)
(rupi:log 1 "rupi:cmd: exception (1): " (exception->string e))
(let ((p (table-ref t "CmdPort" #f)))
(if p (rupi:safecall close-port p)))
(table-set! t "CmdPort" #f)
(set! rupi:error #t)
#f
)
(lambda ()
(let* ((clientaddr (table-ref t "ClientAddr"))
(clientport (table-ref t "ClientPort"))
(clientidx (table-ref t "ClientIdx"))
(cmdport (table-ref t "CmdPort" #f))
(p (if cmdport cmdport (open-tcp-client (list server-address: clientaddr port-number: clientport)))))
(if (port? p) (begin
(table-set! t "CmdPort" p)
;; horrible hack to get rid of any stale data on input
(input-port-timeout-set! p 0.001 (lambda () #f))
(let loop () (if (rupi:safecall rupi:readobj p clientidx) (loop)))
(input-port-timeout-set! p timeout (lambda () (set! rupi:error #t) #f))
(output-port-timeout-set! p timeout (lambda () (set! rupi:error #t) #f))
(rupi:writeobj p (append (list cmd) args) clientidx rupi:magicidx)
(force-output p)
(with-exception-catcher
(lambda (e) (rupi:log 1 "rupi:cmd: exception (2): " (exception->string e))
(rupi:safecall close-port p)
(table-set! t "CmdPort" #f)
(set! rupi:error #t)
#f
)
(lambda () (let* ((data2 (rupi:readobj p clientidx))
(data1 (if data2 (cadr data2) #f))
(data (if data1 (cdr data1) #f))
(retcmd (if data1 (car data1) #f)))
(if (equal? retcmd cmd) data #f)
)))
)))))))
(rupi:release!)
res))
(define (rupi-cmd-wait . x) (apply rupi:cmd (append (list 1.) x)))
(define (rupi-cmd-nowait . x) (apply rupi:cmd (append (list 0.001) x)) #t)
(define (rupi-cmd-shortwait . x) (apply rupi:cmd (append (list 0.05) x)))
(define rupi-cmd rupi-cmd-wait)
;; %%%%%%%%%%%%%%%%%%%%%%%%%%%
;; server
(define (rupi-server store keyidx key addr port upiproc)
(for-each (lambda (keyset) (keyset keyidx key)) rupi:setkey)
(thread-start! (make-safe-thread (rupi:server store keyidx addr port upiproc) 'rupi-server)))
(define (rupi:serve port keyidx store upiproc)
(with-exception-catcher
(lambda (e)
(rupi:log 1 "rupi:server: exception: " (exception->string e))
(rupi:safecall close-port port) #f)
(lambda ()
(let loop ()
(let* ((req0 (rupi:readobj port keyidx))
(req (if (list? req0) (cadr req0) #f))
(ridx (if (list? req0) (car req0) 0)))
(if req (begin
;; Reply with the client's encryption algorithm.
(rupi:writeobj port (append (list (car req)) (apply upiproc (append (list store) req))) keyidx ridx)
(force-output port)
(loop)))))
(rupi:safecall close-port port)
)
))
(define (rupi:server store keyidx addr port upiproc)
(lambda ()
(let ((accept-port (open-tcp-server
(list server-address: addr port-number: port reuse-address: #t
))))
(rupi:log 0 "rupi:server starting at " addr ":" port)
(let loop ()
(let ((connection (read accept-port)))
(if (not (eof-object? connection)) (begin
(rupi:log 1 "rupi:server thread number " rupi:thread-num)
(set! rupi:thread-num (fx+ rupi:thread-num 1))
(thread-start! (make-safe-thread (lambda () (rupi:serve connection keyidx store upiproc)) 'rupi:connection))
(loop))
)))
)))
;; unit tests
;; -----------------
(unit-test "rupi-u8vector" "1000 random u8vectors sent/received by rupi"
(lambda ()
(let* ((store "rupi-test")
(rupi:key (random-u8vector 8))
(rupi:addr (u8vector 127 0 0 1))
(rupi:port (+ 8000 (random-integer 100)))
(upi-cmd (lambda (st l) l))
(rc (rupi-client 0 rupi:key rupi:addr rupi:port)))
(rupi-server store 0 rupi:key rupi:addr rupi:port upi-cmd)
(thread-sleep! 0.5)
(let loop ((n 1000))
(if (fx= n 0)
#t
(if (let* ((datalen (random-integer 100000))
(data (random-u8vector datalen)))
(not (equal? data (rupi-cmd rc data))))
#f
(loop (fx- n 1)))
))
)
))
(unit-test "rupi-list" "1000 random number lists sent/received by rupi"
(lambda ()
(let* ((store "rupi-test")
(rupi:key (random-u8vector 8))
(rupi:addr (u8vector 127 0 0 1))
(rupi:port (+ 8000 (random-integer 100)))
(upi-cmd (lambda (st l) l))
(rc (rupi-client 0 rupi:key rupi:addr rupi:port)))
(rupi-server store 0 rupi:key rupi:addr rupi:port upi-cmd)
(thread-sleep! 0.5)
(let loop ((n 1000))
(if (fx= n 0)
#t
(if (let* ((datalen (random-integer 10000))
(data (make-list datalen (random-integer 32000))))
(not (equal? data (rupi-cmd rc data))))
#f
(loop (fx- n 1)))
))
)
))
(unit-test "rupi-bool" "1000 random booleans sent/received by rupi"
(lambda ()
(let* ((store "rupi-test")
(rupi:key (random-u8vector 8))
(rupi:addr (u8vector 127 0 0 1))
(rupi:port (+ 8000 (random-integer 100)))
(upi-cmd (lambda (st l) l))
(rc (rupi-client 0 rupi:key rupi:addr rupi:port)))
(rupi-server store 0 rupi:key rupi:addr rupi:port upi-cmd)
(thread-sleep! 0.5)
(let loop ((n 1000))
(if (fx= n 0)
#t
(if (let ((data (list "Booleans" (fx= (random-integer 2) 1)
(fx= (random-integer 2) 1) (fx= (random-integer 2) 1))))
(not (equal? data (rupi-cmd rc data))))
#f
(loop (fx- n 1)))
))
)
))
;; eof