forked from wjx823/filagree
/
node.c
305 lines (252 loc) · 9.2 KB
/
node.c
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
// node.c - socket client and server
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <cyassl/ssl.h>
#include <pthread.h>
#include <dirent.h>
#include "vm.h"
#include "serial.h"
#include "sys.h"
#define MAXLINE 1024
#define STR_BUF_SIZE 1024
struct thread_argument {
find_c_var *find;
struct variable *listener;
CYASSL_CTX *cya;
CYASSL* ssl;
int fd;
};
struct listen_arguments {
struct variable *listener;
int serverport;
find_c_var *find;
};
static struct map *server_listeners = NULL; // maps port to listener
static struct map *socket_listeners = NULL; // maps fd to listener
void *incoming_connection(void *arg)
{
char readline[MAXLINE];
struct thread_argument *ta = (struct thread_argument *)arg;
struct context *context = context_new(true, true);
context->find = ta->find;
for (;;)
{
bzero(readline, sizeof(readline));
int n;
if (((n = CyaSSL_read(ta->ssl, readline, MAXLINE)) <= 0))
{
fprintf(stderr, "client closed connection\n");
goto free_ssl;
}
fprintf(stderr, "%d bytes received: %s\n", n, readline);
struct byte_array *raw_message = byte_array_new_size(n);
raw_message->data = (uint8_t*)readline;
int32_t raw_message_length = serial_decode_int(raw_message);
assert_message(raw_message_length < MAXLINE, "todo: handle long messages");
struct variable *message = variable_deserialize(context, raw_message);
struct variable *listener = (struct variable *)map_get(server_listeners, (void*)(VOID_INT)ta->fd);
vm_call(context, listener, message);
}
free_ssl:
CyaSSL_free(ta->ssl); // Free CYASSL object
free(ta);
context_del(context);
return NULL;
}
static bool int_compare(const void *a, const void *b, void *context)
{
int32_t i = (VOID_INT)a;
int32_t j = (VOID_INT)b;
return i == j;
}
static int32_t int_hash(const void* x, void *context) {
return (int32_t)(VOID_INT)x;
}
void *int_copy(const void *x, void *context) { return (void*)x; }
void int_del(const void *x, void *context) {}
void node_init()
{
static bool done = false;
if (!done)
CyaSSL_Init(); // Initialize CyaSSL
done = true;
}
// listens for incoming connections
void *sys_listen2(void *arg)
{
struct listen_arguments *la = (struct listen_arguments*)arg;
struct variable *listener = la->listener;
int serverport = la->serverport;
if (server_listeners == NULL)
server_listeners = map_new_ex(NULL, &int_compare, &int_hash, &int_copy, &int_del);
map_insert(server_listeners, (void*)(VOID_INT)serverport, listener);
node_init();
// Create and initialize CYASSL_CTX structure
CYASSL_CTX* ctx;
if ( (ctx = CyaSSL_CTX_new(CyaTLSv1_server_method())) == NULL)
{
fprintf(stderr, "CyaSSL_CTX_new error.\n");
return NULL;
}
// Load CA certificates into CYASSL_CTX
if (CyaSSL_CTX_load_verify_locations(ctx, "./conf/ca-cert.pem", 0) != SSL_SUCCESS)
{
fprintf(stderr, "Error loading ca-cert.pem, please check the file.\n");
return NULL;
}
// Load server certificate into CYASSL_CTX
if (CyaSSL_CTX_use_certificate_file(ctx, "conf/server-cert.pem", SSL_FILETYPE_PEM) != SSL_SUCCESS)
{
fprintf(stderr, "Error loading server-cert.pem, please check the file.\n");
return NULL;
}
// Load server key into CYASSL_CTX
if (CyaSSL_CTX_use_PrivateKey_file(ctx, "conf/server-key.pem", SSL_FILETYPE_PEM) != SSL_SUCCESS)
{
fprintf(stderr, "Error loading server-key.pem, please check the file.\n");
return NULL;
}
// open the server socket over specified port 8080 to accept client connections
int listenfd = socket(AF_INET, SOCK_STREAM, 0);
// setsockopt: Eliminates "ERROR on binding: Address already in use" error.
int optval = 1;
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval, sizeof(int));
struct sockaddr_in servaddr;
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(serverport);
bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
listen(listenfd, 5);
// create thread for processing each client request
struct sockaddr_in client_addr;
socklen_t sin_size = sizeof (struct sockaddr_in);
for(;;)
{
int connfd = accept(listenfd, (struct sockaddr *) &client_addr, &sin_size );
DEBUGPRINT("\n Got a connection from (%s , %d)\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
// Create CYASSL Object
CYASSL* ssl;
if ((ssl = CyaSSL_new(ctx)) == NULL) {
fprintf(stderr, "CyaSSL_new error");
return NULL;
}
CyaSSL_set_fd(ssl, connfd);
struct thread_argument *ta = (struct thread_argument *)malloc(sizeof(struct thread_argument));
ta->find = la->find;
ta->listener = listener;
ta->ssl = ssl;
ta->fd = connfd;
ta->cya = ctx;
DEBUGPRINT("listenting on %d - %p\n", connfd, ta->ssl);
pthread_t child;
pthread_create(&child, NULL, incoming_connection, &ta);
}
return NULL;
}
struct variable *sys_listen(struct context *context)
{
struct variable *arguments = (struct variable*)stack_pop(context->operand_stack);
struct listen_arguments *la = (struct listen_arguments*)malloc(sizeof(struct listen_arguments));
la->serverport = param_int(arguments, 1);
la->listener = variable_copy(context, (struct variable*)array_get(arguments->list, 2));
pthread_t child;
pthread_create(&child, NULL, sys_listen2, la);
return NULL;
}
struct variable *sys_connect(struct context *context)
{
struct variable *arguments = (struct variable*)stack_pop(context->operand_stack);
const char *serveraddr = param_str(arguments, 1);
const int32_t serverport = param_int(arguments, 2);
struct variable *listener = ((struct variable*)array_get(arguments->list, 2));
int sockfd;
struct sockaddr_in servaddr;
CYASSL_CTX* ctx;
CYASSL* ssl;
node_init();
// Create and initialize CYASSL_CTX structure
if ( (ctx = CyaSSL_CTX_new(CyaTLSv1_client_method())) == NULL)
{
context->vm_exception = variable_new_str(context, byte_array_from_string("SSL_CTX_new error"));
CyaSSL_Cleanup();
return NULL;
}
// Load CA certificates into CYASSL_CTX
if (CyaSSL_CTX_load_verify_locations(ctx, "./conf/ca-cert.pem", 0) != SSL_SUCCESS)
{
context->vm_exception = variable_new_str(context, byte_array_from_string("Error loading ca-cert.pem, please check the file.\n"));
CyaSSL_CTX_free(ctx);
CyaSSL_Cleanup();
return NULL;
}
// Create Socket file descriptor
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(serverport);
inet_pton(AF_INET, serveraddr, &servaddr.sin_addr);
// Blocking Connect to socket file descriptor
connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
// Create CYASSL object
if ((ssl = CyaSSL_new(ctx)) == NULL)
{
context->vm_exception = variable_new_str(context, byte_array_from_string("CyaSSL_new error"));
CyaSSL_CTX_free(ctx);
CyaSSL_Cleanup();
return NULL;
}
CyaSSL_set_fd(ssl, sockfd);
fprintf(stderr, "Connected on %d -- %p\n", sockfd, ssl);
struct thread_argument *ta = (struct thread_argument *)malloc(sizeof(struct thread_argument));
ta->find = context->find;
ta->listener = listener;
ta->ssl = ssl;
ta->fd = sockfd;
ta->cya = ctx;
if (socket_listeners == NULL)
socket_listeners = map_new_ex(NULL, &int_compare, &int_hash, &int_copy, &int_del);
map_insert(socket_listeners, (void*)(VOID_INT)sockfd, (void*)(VOID_INT)ta);
return variable_new_int(context, sockfd);
}
struct variable *sys_send(struct context *context)
{
struct variable *arguments = (struct variable*)stack_pop(context->operand_stack);
struct variable *sender = (struct variable*)array_get(arguments->list, 1);
const char *message = param_str(arguments, 2);
assert_message(sender->type == VAR_INT, "non int fd");
int32_t fd = sender->integer;
struct thread_argument *ta = (struct thread_argument*)map_get(socket_listeners, (void*)(VOID_INT)fd);
printf("send on ssl=%p\n", ta->ssl);
if (CyaSSL_write(ta->ssl, message, strlen(message)) != strlen(message))
context->vm_exception = variable_new_str(context, byte_array_from_string("CyaSSL_write error"));
return NULL;
}
struct variable *sys_disconnect(struct context *context)
{
struct variable *arguments = (struct variable*)stack_pop(context->operand_stack);
if (arguments->list->length < 2)
{
struct variable *sockets = (struct variable *)array_get(arguments->list, 1);
assert_message(sockets->type == VAR_LST, "non list of sockets");
for (int i=0; i<sockets->list->length; i++)
{
struct thread_argument *tc = (struct thread_argument *)array_get(sockets->list, i);
close(tc->fd);
CyaSSL_free(tc->ssl);
}
}
else
{
const int32_t fd = param_int(arguments, 1);
close(fd);
map_remove(socket_listeners, (void*)(VOID_INT)fd);
}
return NULL;
}