-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcomelang-net.h
463 lines (347 loc) · 11.5 KB
/
comelang-net.h
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#ifndef __COMELANG_NET_H__
#define __COMELANG_NET_H__
#include <comelang.h>
using c
{
#include <unistd.h>
#include <arpa/inet.h>
#ifdef __LINUX__
#include <endian.h>
#endif
#ifdef __MAC__
#include <libkern/OSByteOrder.h>
#endif
#include <openssl/ssl.h>
#include <openssl/err.h>
#ifdef __ANDROID__
#include <mysql.h>
#else
#include <mysql/mysql.h>
#endif
}
using comelang-net;
typedef int socket_fd;
uniq int socket_fd::write(socket_fd self, string str)
{
return write(self, str, str.length());
}
uniq int server_socket(int port=8080, int socket_family=AF_INET, int socket_type=SOCK_STREAM, int protocol=0, bool reuse=false, void* parent, void (*block)(void* parent, socket_fd it, bool* break_, bool* reconnection))
{
socket_fd sock = socket(socket_family, socket_type, protocol);
if(sock <0) die("socket failed");
#ifndef __ANDROID__
if(reuse) {
int opt = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
close(sock);
die(s"setsockopt");
}
}
#endif
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(port);
int addrlen = sizeof(address);
if (bind(sock, (struct sockaddr*)&address, sizeof(address)) < 0) {
close(sock);
die(s"Unable to bind");
}
if (listen(sock, 3) < 0) {
close(sock);
die(s"Unable to listen");
}
socket_fd new_socket = accept(sock, (struct sockaddr *)&address, (socklen_t*)&addrlen);
while (1) {
bool reconnection = false;
bool break_ = false;
block(parent, new_socket, &break_, &reconnection);
if(reconnection) {
close(new_socket);
new_socket = accept(sock, (struct sockaddr *)&address, (socklen_t*)&addrlen);
}
if(break_) {
break;
}
}
close(new_socket);
close(sock);
return 0;
}
uniq int client_socket(int port=8080, char* address="127.0.0.1", void* parent, void (*block)(void* parent, socket_fd it, bool* break_))
{
int sock = 0;
struct sockaddr_in serv_addr;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
die(s"socket");
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
if (inet_pton(AF_INET, address, &serv_addr.sin_addr) <= 0) {
close(sock);
die(s"Invalid address/ Address not supported");
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
close(sock);
die(s"Connection Failed");
}
bool break_ = false;
while(1) {
bool break_ = false;
block(parent, sock, &break_);
if(break_) {
break;
}
}
close(sock);
return 0;
}
uniq string client_socket2(int port, char* data, char* address="127.0.0.1")
{
int sock = 0;
struct sockaddr_in serv_addr;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
die(s"Socket creation error");
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
if (inet_pton(AF_INET, address, &serv_addr.sin_addr) <= 0) {
close(sock);
die(s"Invalid address/ Address not supported");
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
close(sock);
die(s"Connection Failed");
}
if(write(sock, data, strlen(data)) < 0) {
close(sock);
die(s"Write Failed");
}
buffer*% buf = new buffer();
char buf2[1024] = { '\0' };
int size = read(sock, buf2, 1024);
if(size < 0) {
close(sock);
die(s"Read Failed");
}
buf.append(buf2, size);
close(sock);
return buf.to_string();
}
uniq int httpd_socket(int port=8080, int socket_family=AF_INET, int socket_type=SOCK_STREAM, int protocol=0, bool reuse=false, void* parent, void (*block)(void* parent, socket_fd it, bool* break_))
{
socket_fd sock = socket(socket_family, socket_type, protocol);
if(sock < 0) die("socket failed");
#ifndef __ANDROID__
if(reuse) {
int opt = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
close(sock);
die(s"setsockpt failed");
}
}
#endif
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(port);
int addrlen = sizeof(address);
if (bind(sock, (struct sockaddr*)&address, sizeof(address)) < 0) {
close(sock);
die(s"Unable to bind");
}
if (listen(sock, 3) < 0) {
close(sock);
die(s"Unable to listen");
}
while (1) {
socket_fd new_socket = accept(sock, (struct sockaddr *)&address, (socklen_t*)&addrlen);
bool break_ = false;
block(parent, new_socket, &break_);
close(new_socket);
if(break_) {
break;
}
}
close(sock);
return 0;
}
uniq void ERR_print_errors_fp(FILE* f)
{
/*
unsigned long err = ERR_get_error(); // エラーコードを取得
char err_str[256]; // エラーメッセージを格納するためのバッファ
ERR_error_string_n(err, err_str, sizeof(err_str)); // エラーメッセージを文字列に変換
fprintf(f, "OpenSSL Error: %s\n", err_str); // エラーメッセージを標準エラーに出力
*/
}
uniq int httpsd_socket(int port=443, bool reuse=false, void* parent, void (*block)(void* parent, SSL* it, bool* break_))
{
int sock;
SSL_CTX *ctx;
SSL_load_error_strings();
OpenSSL_add_ssl_algorithms();
const SSL_METHOD *method;
method = SSLv23_server_method();
ctx = SSL_CTX_new(method);
if (!ctx) {
//ERR_print_errors_fp(stdout);
die(s"Unable to create SSL context");
}
if (SSL_CTX_use_certificate_file(ctx, "cert.pem", SSL_FILETYPE_PEM) <= 0) {
//ERR_print_errors_fp(stdout);
die(s"SSL_CTX_use_certificate_file");
}
if (SSL_CTX_use_PrivateKey_file(ctx, "key.pem", SSL_FILETYPE_PEM) <= 0 ) {
//ERR_print_errors_fp(stdout);
die(s"SSL_CTX_use_PrivateKey_file");
}
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
die(s"Unable to create socket");
}
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY;
if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
die(s"Unable to bind");
}
if (listen(sock, 1) < 0) {
die(s"Unable to listen");
}
while (1) {
struct sockaddr_in addr;
uint len = sizeof(addr);
int client = accept(sock, (struct sockaddr*)&addr, &len);
if (client < 0) {
die(s"Unable to accept");
}
SSL *ssl = SSL_new(ctx);
SSL_set_fd(ssl, client);
if (SSL_accept(ssl) <= 0) {
ERR_print_errors_fp(stdout);
} else {
bool break_ = false;
block(parent, ssl, &break_);
if(break_) {
break;
}
}
SSL_shutdown(ssl);
SSL_free(ssl);
close(client);
}
close(sock);
SSL_CTX_free(ctx);
EVP_cleanup();
return 0;
}
uniq MYSQL* gComeMySQL = NULL;
uniq int come_mysql_init()
{
if(gComeMySQL == NULL) {
gComeMySQL = mysql_init(NULL);
if(gComeMySQL == NULL) {
die(s"mysql_init failed");
}
}
return 0;
}
uniq void come_mysql_final()
{
if(gComeMySQL) {
mysql_close(gComeMySQL);
gComeMySQL = NULL;
}
}
uniq int finish_with_error()
{
mysql_close(gComeMySQL);
die(s"\{mysql_error(gComeMySQL)}");
}
uniq int create_user_if_not_exists(char* user, char* password, char* root_password="", char* host_name="localhost")
{
come_mysql_init();
if(mysql_real_connect(gComeMySQL, host_name, "root", root_password, NULL, 0, NULL, 0) == NULL) {
finish_with_error();
}
string check_user_query =
s"SELECT COUNT(*) FROM mysql.user WHERE user = '\{user}' AND host = '\{host_name}'";
if(mysql_query(gComeMySQL, check_user_query) == NULL) {
finish_with_error();
}
MYSQL_RES *result = mysql_store_result(gComeMySQL);
if(result == null) finish_with_error();
MYSQL_ROW row = mysql_fetch_row(result);
int user_exists = atoi(row[0]);
mysql_free_result(result);
if (user_exists == 0) {
if(mysql_query(gComeMySQL, s"CREATE USER '\{user}'@'\{host_name}' IDENTIFIED BY '\{password}'") == NULL) {
finish_with_error();
}
if(mysql_query(gComeMySQL, s"GRANT ALL PRIVILEGES ON *.* TO '\{user}'@'\{host_name}'") == NULL) {
finish_with_error();
}
if(mysql_query(gComeMySQL, "FLUSH PRIVILEGES") == NULL) {
finish_with_error();
}
}
come_mysql_final();
return 0;
}
uniq int create_database_if_not_exists(char* database_name, char* user_name, char* password, char* host_name="localhost")
{
come_mysql_init();
if(mysql_real_connect(gComeMySQL, host_name, user_name, password, NULL, 0, NULL, 0) == NULL) { finish_with_error(); }
string create_db_query = s"CREATE DATABASE IF NOT EXISTS \{database_name}";
if(mysql_query(gComeMySQL, create_db_query) == NULL) { finish_with_error(); }
come_mysql_final();
return 0;
}
uniq int xmysql_query(char* query, char* user="user", char* password="user", bool create_user=true, bool create_database=true, char* root_password="", char* database_name="testdb", char* host_name="localhost")
{
if(create_user) {
create_user_if_not_exists(user, password, root_password);
}
if(create_database) {
create_database_if_not_exists(database_name, user, password, host_name);
}
come_mysql_init();
if(mysql_select_db(gComeMySQL, database_name) == NULL) { finish_with_error(); }
if(mysql_real_connect(gComeMySQL, host_name, user, password, database_name, 0, NULL, 0) == NULL) { finish_with_error(); }
if(mysql_query(gComeMySQL, query) == NULL) { finish_with_error(); }
come_mysql_final();
return 0;
}
uniq list<list<string>*%>*% xmysql_query_and_fetch_row(char* query, char* user="user", char* password="user", bool create_user=false, bool create_database=false, char* root_password="", char* database_name="testdb", char* host_name="localhost")
{
if(create_user) {
create_user_if_not_exists(user, password, root_password);
}
if(create_database) {
create_database_if_not_exists(database_name, user, password, host_name);
}
come_mysql_init();
if(mysql_select_db(gComeMySQL, database_name) == NULL) { finish_with_error(); }
if(mysql_real_connect(gComeMySQL, host_name, user, password, database_name, 0, NULL, 0) == NULL) { finish_with_error(); }
if(mysql_query(gComeMySQL, query) == NULL) { finish_with_error(); }
MYSQL_RES* res = mysql_store_result(gComeMySQL);
if(res) finish_with_error();
int num_fields = mysql_num_fields(res);
list<list<string>*%>*% result = new list<list<string>*%>();
MYSQL_ROW row;
while ((row = mysql_fetch_row(res)) != NULL) {
list<string>*% li = new list<string>();
for(int i=0; i<num_fields; i++) {
if(row[i]) {
li.add(string(row[i]));
}
}
result.add(li);
}
mysql_free_result(res);
come_mysql_final();
return result;
}
#endif