-
Notifications
You must be signed in to change notification settings - Fork 12
/
lampshade_bindings.go
413 lines (328 loc) · 10.2 KB
/
lampshade_bindings.go
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package main
import (
"context"
"net"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
lampshade "github.com/getlantern/lampshade"
)
// #cgo CFLAGS: -DIN_GO=1 -ggdb -I ${SRCDIR}/../../include
// #cgo android LDFLAGS: -Wl,--unresolved-symbols=ignore-all
//#include <stdlib.h>
//#include <stddef.h>
//#include <stdint.h>
//#include <string.h>
//
//// Don't export these functions into C or we'll get "unused function" warnings
//// (Or errors saying functions are defined more than once if the're not static).
//
//#if IN_GO
//static void execute_cb_void(void* func, void* arg, int err)
//{
// ((void(*)(void*, int)) func)(arg, err);
//}
//static void execute_cb_int(void* func, void* arg, int err, uint64_t data)
//{
// ((void(*)(void*, int, uint64_t)) func)(arg, err, data);
//}
//static void execute_cb_buffers(void* func, void* arg, int err, void* data1, size_t size1, void* data2, size_t size2)
//{
// ((void(*)(void*, int, void*, size_t, void*, size_t)) func)(arg, err, data1, size1, data2, size2);
//}
//#endif // if IN_GO
import "C"
import "unsafe"
func main() {
}
var g_buffer_pool lampshade.BufferPool = lampshade.NewBufferPool(128)
// This binding contains a bunch of object tables identified by ID, and
// assorted ID counters. All are only ever accessed by the binding API
// functions, and need external synchronization.
var g_cancellation_next_id uint64 = 0
var g_cancellations = make(map[uint64]func())
//export go_lampshade_cancellation_allocate
func go_lampshade_cancellation_allocate() uint64 {
id := g_cancellation_next_id
g_cancellation_next_id += 1
return id
}
//export go_lampshade_cancellation_free
func go_lampshade_cancellation_free(cancellation_id uint64) {
delete(g_cancellations, cancellation_id)
}
//export go_lampshade_cancellation_cancel
func go_lampshade_cancellation_cancel(cancellation_id uint64) {
cancel, ok := g_cancellations[cancellation_id]
if !ok { return }
cancel()
}
type lampshade_Connection struct {
connection net.Conn
}
var g_connection_next_id uint64 = 0
var g_connections = make(map[uint64]*lampshade_Connection)
//export go_lampshade_connection_allocate
func go_lampshade_connection_allocate() uint64 {
var connection lampshade_Connection
connection_id := g_connection_next_id
g_connection_next_id += 1
g_connections[connection_id] = &connection
return connection_id
}
//export go_lampshade_connection_free
func go_lampshade_connection_free(connection_id uint64) {
delete(g_connections, connection_id)
}
//export go_lampshade_connection_send
func go_lampshade_connection_send(connection_id uint64, buffer unsafe.Pointer, buffer_size C.int, callback unsafe.Pointer, callback_arg unsafe.Pointer) {
connection, ok := g_connections[connection_id]
if !ok {
C.execute_cb_int(callback, callback_arg, 1, 0)
return
}
if connection.connection == nil {
C.execute_cb_int(callback, callback_arg, 1, 0)
return
}
data := C.GoBytes(buffer, buffer_size)
go func() {
written, err := connection.connection.Write(data)
if err != nil {
C.execute_cb_int(callback, callback_arg, 1, 0)
return
}
C.execute_cb_int(callback, callback_arg, 0, C.uint64_t(written))
}()
}
//export go_lampshade_connection_receive
func go_lampshade_connection_receive(connection_id uint64, buffer unsafe.Pointer, buffer_size C.int, callback unsafe.Pointer, callback_arg unsafe.Pointer) {
connection, ok := g_connections[connection_id]
if !ok {
C.execute_cb_int(callback, callback_arg, 1, 0)
return
}
if connection.connection == nil {
C.execute_cb_int(callback, callback_arg, 1, 0)
return
}
go func() {
read_buffer := make([]byte, buffer_size)
read, err := connection.connection.Read(read_buffer)
if err != nil {
C.execute_cb_int(callback, callback_arg, 1, 0)
return
}
C.memcpy(buffer, unsafe.Pointer(&read_buffer[0]), C.size_t(read))
C.execute_cb_int(callback, callback_arg, 0, C.uint64_t(read))
}()
}
//export go_lampshade_connection_close
func go_lampshade_connection_close(connection_id uint64, callback unsafe.Pointer, callback_arg unsafe.Pointer) {
connection, ok := g_connections[connection_id]
if !ok {
C.execute_cb_void(callback, callback_arg, 1)
return
}
if connection.connection == nil {
C.execute_cb_void(callback, callback_arg, 1)
return
}
go func() {
err := connection.connection.Close()
if err != nil {
C.execute_cb_void(callback, callback_arg, 1)
return
}
C.execute_cb_void(callback, callback_arg, 0)
}()
}
type lampshade_Dialer struct {
dialer lampshade.BoundDialer
}
var g_dialer_next_id uint64 = 0
var g_dialers = make(map[uint64]*lampshade_Dialer)
//export go_lampshade_dialer_allocate
func go_lampshade_dialer_allocate() uint64 {
var dialer lampshade_Dialer
dialer_id := g_dialer_next_id
g_dialer_next_id += 1
g_dialers[dialer_id] = &dialer
return dialer_id
}
//export go_lampshade_dialer_free
func go_lampshade_dialer_free(dialer_id uint64) {
delete(g_dialers, dialer_id)
}
//export go_lampshade_dialer_init
func go_lampshade_dialer_init(dialer_id uint64, c_endpoint *C.char, public_key_buffer unsafe.Pointer, public_key_size C.int, callback unsafe.Pointer, callback_arg unsafe.Pointer) {
endpoint := C.GoString(c_endpoint)
encoded_public_key := C.GoBytes(public_key_buffer, public_key_size)
public_key, err := x509.ParsePKCS1PublicKey(encoded_public_key)
if err != nil {
C.execute_cb_void(callback, callback_arg, 1)
return
}
dialer, ok := g_dialers[dialer_id]
if !ok {
C.execute_cb_void(callback, callback_arg, 1)
return
}
if dialer.dialer != nil {
C.execute_cb_void(callback, callback_arg, 1)
return
}
go func() {
dialer.dialer = lampshade.NewDialer(&lampshade.DialerOpts{
Pool: g_buffer_pool,
Cipher: lampshade.AES128GCM,
ServerPublicKey: public_key,
}).BoundTo(func() (net.Conn, error) {
return net.Dial("tcp", endpoint)
})
C.execute_cb_void(callback, callback_arg, 0)
}()
}
//export go_lampshade_dialer_dial
func go_lampshade_dialer_dial(dialer_id uint64, connection_id uint64, cancellation_id uint64, callback unsafe.Pointer, callback_arg unsafe.Pointer) {
dialer, ok := g_dialers[dialer_id]
if !ok {
C.execute_cb_void(callback, callback_arg, 1)
return
}
if dialer.dialer == nil {
C.execute_cb_void(callback, callback_arg, 1)
return
}
connection, ok := g_connections[connection_id]
if !ok {
C.execute_cb_void(callback, callback_arg, 1)
return
}
if connection.connection != nil {
C.execute_cb_void(callback, callback_arg, 1)
return
}
cancel_ctx, cancel_func := context.WithCancel(context.Background())
g_cancellations[cancellation_id] = cancel_func
go func() {
conn, err := dialer.dialer.DialContext(cancel_ctx)
if err != nil {
C.execute_cb_void(callback, callback_arg, 1)
return
}
connection.connection = conn
C.execute_cb_void(callback, callback_arg, 0)
}()
}
type lampshade_Listener struct {
listener net.Listener
}
var g_listener_next_id uint64 = 0
var g_listeners = make(map[uint64]*lampshade_Listener)
//export go_lampshade_listener_allocate
func go_lampshade_listener_allocate() uint64 {
var listener lampshade_Listener
listener_id := g_listener_next_id
g_listener_next_id += 1
g_listeners[listener_id] = &listener
return listener_id
}
//export go_lampshade_listener_free
func go_lampshade_listener_free(listener_id uint64) {
delete(g_listeners, listener_id)
}
//export go_lampshade_listener_create
func go_lampshade_listener_create(listener_id uint64, c_endpoint *C.char, private_key_buffer unsafe.Pointer, private_key_size C.int, callback unsafe.Pointer, callback_arg unsafe.Pointer) {
endpoint := C.GoString(c_endpoint)
encoded_private_key := C.GoBytes(private_key_buffer, private_key_size)
private_key, err := x509.ParsePKCS1PrivateKey(encoded_private_key)
if err != nil {
C.execute_cb_void(callback, callback_arg, 1)
}
listener, ok := g_listeners[listener_id]
if !ok {
C.execute_cb_void(callback, callback_arg, 1)
return
}
if listener.listener != nil {
C.execute_cb_void(callback, callback_arg, 1)
return
}
go func() {
tcp_listener, err := net.Listen("tcp", endpoint)
if err != nil {
C.execute_cb_void(callback, callback_arg, 1)
return
}
listener.listener = lampshade.WrapListener(tcp_listener, g_buffer_pool, private_key, true)
C.execute_cb_void(callback, callback_arg, 0)
}()
}
//export go_lampshade_listener_accept
func go_lampshade_listener_accept(listener_id uint64, connection_id uint64, callback unsafe.Pointer, callback_arg unsafe.Pointer) {
listener, ok := g_listeners[listener_id]
if !ok {
C.execute_cb_void(callback, callback_arg, 1)
return
}
if listener.listener == nil {
C.execute_cb_void(callback, callback_arg, 1)
return
}
connection, ok := g_connections[connection_id]
if !ok {
C.execute_cb_void(callback, callback_arg, 1)
return
}
if connection.connection != nil {
C.execute_cb_void(callback, callback_arg, 1)
return
}
go func() {
conn, err := listener.listener.Accept()
if err != nil {
C.execute_cb_void(callback, callback_arg, 1)
return
}
connection.connection = conn
C.execute_cb_void(callback, callback_arg, 0)
}()
}
//export go_lampshade_listener_close
func go_lampshade_listener_close(listener_id uint64, callback unsafe.Pointer, callback_arg unsafe.Pointer) {
listener, ok := g_listeners[listener_id]
if !ok {
C.execute_cb_void(callback, callback_arg, 1)
return
}
if listener.listener == nil {
C.execute_cb_void(callback, callback_arg, 1)
return
}
go func() {
err := listener.listener.Close()
if err != nil {
C.execute_cb_void(callback, callback_arg, 1)
return
}
C.execute_cb_void(callback, callback_arg, 0)
}()
}
//export go_lampshade_generate_key
func go_lampshade_generate_key(bits int, callback unsafe.Pointer, callback_arg unsafe.Pointer) {
private_key, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
C.execute_cb_buffers(callback, callback_arg, 1, nil, 0, nil, 0)
return
}
encoded_private_key := x509.MarshalPKCS1PrivateKey(private_key)
encoded_public_key := x509.MarshalPKCS1PublicKey(&private_key.PublicKey)
c_private_key := C.CBytes(encoded_private_key)
c_private_key_size := C.size_t(len(encoded_private_key))
defer C.free(c_private_key)
c_public_key := C.CBytes(encoded_public_key)
c_public_key_size := C.size_t(len(encoded_public_key))
defer C.free(c_public_key)
C.execute_cb_buffers(callback, callback_arg, 0, c_private_key, c_private_key_size, c_public_key, c_public_key_size)
}