This repository has been archived by the owner on Oct 29, 2022. It is now read-only.
/
luaNetwork.cpp
719 lines (640 loc) · 24.1 KB
/
luaNetwork.cpp
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
/*----------------------------------------------------------------------------------------------------------------------#
#-----------------------------------------------------------------------------------------------------------------------#
#------ This File is Part Of : ----------------------------------------------------------------------------------------#
#------- _ ------------------- ______ _ --------------------------------------------------------------------------#
#------ | | ------------------- (_____ \ | | --------------------------------------------------------------------------#
#------ | | --- _ _ ____ _____) )| | ____ _ _ ____ ____ ----------------------------------------------#
#------ | | --- | | | | / _ | | ____/ | | / _ || | | | / _ ) / ___) ----------------------------------------------#
#------ | |_____| |_| |( ( | | | | | |( ( | || |_| |( (/ / | | --------------------------------------------------#
#------ |_______)\____| \_||_| |_| |_| \_||_| \__ | \____)|_| --------------------------------------------------#
#------------------------------------------------- (____/ -------------------------------------------------------------#
#------------------------ ______ _ -------------------------------------------------------------------------------#
#------------------------ (_____ \ | | -------------------------------------------------------------------------------#
#------------------------ _____) )| | _ _ ___ ------------------------------------------------------------------#
#------------------------ | ____/ | || | | | /___) ------------------------------------------------------------------#
#------------------------ | | | || |_| ||___ | ------------------------------------------------------------------#
#------------------------ |_| |_| \____|(___/ ------------------------------------------------------------------#
#-----------------------------------------------------------------------------------------------------------------------#
#-----------------------------------------------------------------------------------------------------------------------#
#- Licensed under the GPL License --------------------------------------------------------------------------------------#
#-----------------------------------------------------------------------------------------------------------------------#
#- Copyright (c) Nanni <lpp.nanni@gmail.com> ---------------------------------------------------------------------------#
#- Copyright (c) Rinnegatamante <rinnegatamante@gmail.com> -------------------------------------------------------------#
#-----------------------------------------------------------------------------------------------------------------------#
#-----------------------------------------------------------------------------------------------------------------------#
#- Credits : -----------------------------------------------------------------------------------------------------------#
#-----------------------------------------------------------------------------------------------------------------------#
#- Smealum for ctrulib and ftpony src ----------------------------------------------------------------------------------#
#- StapleButter for debug font -----------------------------------------------------------------------------------------#
#- Lode Vandevenne for lodepng -----------------------------------------------------------------------------------------#
#- Jean-loup Gailly and Mark Adler for zlib ----------------------------------------------------------------------------#
#- xerpi for sf2dlib ---------------------------------------------------------------------------------------------------#
#- Special thanks to Aurelio for testing, bug-fixing and various help with codes and implementations -------------------#
#-----------------------------------------------------------------------------------------------------------------------*/
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <3ds.h>
#include <fcntl.h>
#include "include/luaplayer.h"
#include "include/ftp/ftp.h"
#include "include/utils.h"
#define stringify(str) #str
#define VariableRegister(lua, value) do { lua_pushinteger(lua, value); lua_setglobal (lua, stringify(value)); } while(0)
extern bool ftp_state;
static int connfd;
typedef struct{
u32 magic;
int sock;
bool serverSocket;
sslcContext sslc_context;
bool isSSL;
} Socket;
u32 RootCertChain_contexthandle=0;
static int lua_checkFTPcommand(lua_State *L){
int argc = lua_gettop(L);
#ifndef SKIP_ERROR_HANDLING
if (argc != 0) return luaL_error(L, "wrong number of arguments");
#endif
if(connfd<0)connfd=ftp_getConnection();
else{
int ret=ftp_frame(connfd);
if(ret==1)
{
sprintf(shared_ftp,"Client has disconnected. Wait for next client...");
connfd=-1;
}
}
lua_pushstring(L, shared_ftp);
return 1;
}
static int lua_wifistat(lua_State *L){
int argc = lua_gettop(L);
#ifndef SKIP_ERROR_HANDLING
if (argc != 0) return luaL_error(L, "wrong number of arguments");
#endif
u32 wifiStatus;
if (ACU_GetWifiStatus( &wifiStatus) == 0xE0A09D2E) lua_pushboolean(L,false);
else lua_pushboolean(L,wifiStatus);
return 1;
}
static int lua_macaddr(lua_State *L){
int argc = lua_gettop(L);
#ifndef SKIP_ERROR_HANDLING
if (argc != 0) return luaL_error(L, "wrong number of arguments");
#endif
u8* mac_byte = (u8*)0x1FF81060;
char mac_address[18];
sprintf(mac_address,"%02X:%02X:%02X:%02X:%02X:%02X",*mac_byte,*(mac_byte+1),*(mac_byte+2),*(mac_byte+3),*(mac_byte+4),*(mac_byte+5));
mac_address[17] = 0;
lua_pushstring(L,mac_address);
return 1;
}
static int lua_ipaddr(lua_State *L){
int argc = lua_gettop(L);
if (argc != 0) return luaL_error(L, "wrong number of arguments");
u32 ip=(u32)gethostid();
char ip_address[64];
sprintf(ip_address,"%lu.%lu.%lu.%lu", ip & 0xFF, (ip>>8)&0xFF, (ip>>16)&0xFF, (ip>>24)&0xFF);
lua_pushstring(L,ip_address);
return 1;
}
static int lua_download(lua_State *L){
int argc = lua_gettop(L);
#ifndef SKIP_ERROR_HANDLING
if (argc < 2 || argc > 5) return luaL_error(L, "wrong number of arguments");
#endif
const char* url = luaL_checkstring(L,1);
const char* file = luaL_checkstring(L,2);
const char* headers = (argc >= 3) ? luaL_checkstring(L,3) : NULL;
u8 method = (argc >= 4) ? luaL_checkinteger(L,4) : 0;
const char* postdata = (argc >= 5) ? luaL_checkstring(L,5) : NULL;
httpcContext context;
u32 statuscode=0;
HTTPC_RequestMethod useMethod = HTTPC_METHOD_GET;
if(method <= 3 && method >= 1) useMethod = (HTTPC_RequestMethod)method;
do {
if (statuscode >= 301 && statuscode <= 308) {
char newurl[4096];
httpcGetResponseHeader(&context, (char*)"Location", &newurl[0], 4096);
url = &newurl[0];
httpcCloseContext(&context);
}
Result ret = httpcOpenContext(&context, useMethod, (char*)url, 0);
// Just disable SSL verification instead of loading default certs.
httpcSetSSLOpt(&context, SSLCOPT_DisableVerify);
if(headers != NULL){
char *tokenheader = (char*)malloc(strlen(headers)+1);
strcpy(tokenheader, headers);
char *toker = tokenheader;
char *headername = NULL;
char *headervalue = NULL;
do {
headername = strtok(toker, ":");
if (headername == NULL) break;
headervalue = strtok(NULL, "\n");
if (headervalue == NULL) break;
if (headervalue[0] == ' ') headervalue++;
httpcAddRequestHeaderField(&context, headername, headervalue);
toker = NULL;
} while (headername != NULL && headervalue != NULL);
free(tokenheader);
}
if (useMethod == HTTPC_METHOD_POST && postdata != NULL) {
httpcAddPostDataRaw(&context, (u32*)postdata, strlen(postdata));
}
#ifndef SKIP_ERROR_HANDLING
if(ret==0){
#endif
httpcBeginRequest(&context);
u32 contentsize=0;
httpcGetResponseStatusCode(&context, &statuscode, 0);
if (statuscode == 200){
u32 readSize = 0;
long int bytesWritten = 0;
u8* buf = (u8*)malloc(0x1000);
memset(buf, 0, 0x1000);
Handle fileHandle;
FS_Archive sdmcArchive=(FS_Archive){ARCHIVE_SDMC, (FS_Path){PATH_EMPTY, 1, (u8*)""}};
FS_Path filePath=fsMakePath(PATH_ASCII, file);
FSUSER_OpenFileDirectly( &fileHandle, sdmcArchive, filePath, FS_OPEN_CREATE|FS_OPEN_WRITE, 0x00000000);
do {
ret = httpcDownloadData(&context, buf, 0x1000, &readSize);
FSFILE_Write(fileHandle, NULL, bytesWritten, buf, readSize, 0x10001);
bytesWritten += readSize;
} while (ret == (s32)HTTPC_RESULTCODE_DOWNLOADPENDING);
FSFILE_Close(fileHandle);
svcCloseHandle(fileHandle);
free(buf);
}
#ifndef SKIP_ERROR_HANDLING
}
#endif
} while ((statuscode >= 301 && statuscode <= 303) || (statuscode >= 307 && statuscode <= 308));
#ifndef SKIP_ERROR_HANDLING
if ((statuscode < 200 && statuscode > 226) && statuscode != 304) luaL_error(L, "error opening url");
#endif
httpcCloseContext(&context);
lua_pushinteger(L, statuscode);
return 1;
}
static int lua_downstring(lua_State *L){
int argc = lua_gettop(L);
#ifndef SKIP_ERROR_HANDLING
if (argc < 1 || argc > 4) return luaL_error(L, "wrong number of arguments");
#endif
const char* url = luaL_checkstring(L,1);
const char* headers = (argc >= 2) ? luaL_checkstring(L,2) : NULL;
u8 method = (argc >= 3) ? luaL_checkinteger(L,3) : 0;
const char* postdata = (argc >= 4) ? luaL_checkstring(L,4) : NULL;
httpcContext context;
HTTPC_RequestMethod useMethod = HTTPC_METHOD_GET;
if(method <= 3 && method >= 1) useMethod = (HTTPC_RequestMethod)method;
u32 statuscode=0;
do {
if (statuscode >= 301 && statuscode <= 308) {
char newurl[4096];
httpcGetResponseHeader(&context, (char*)"Location", &newurl[0], 4096);
url = &newurl[0];
httpcCloseContext(&context);
}
Result ret = httpcOpenContext(&context, useMethod, (char*)url , 0);
// Lets just disable SSL verification instead of loading default certs.
httpcSetSSLOpt(&context, SSLCOPT_DisableVerify);
if(headers != NULL){
char *tokenheader = (char*)malloc(strlen(headers)+1);
strcpy(tokenheader, headers);
char *toker = tokenheader;
char *headername = NULL;
char *headervalue = NULL;
do {
headername = strtok(toker, ":");
if (headername == NULL) break;
headervalue = strtok(NULL, "\n");
if (headervalue == NULL) break;
if (headervalue[0] == ' ') headervalue++;
httpcAddRequestHeaderField(&context, headername, headervalue);
toker = NULL;
} while (headername != NULL && headervalue != NULL);
free(tokenheader);
}
if (useMethod == HTTPC_METHOD_POST && postdata != NULL) {
httpcAddPostDataRaw(&context, (u32*)postdata, strlen(postdata));
}
#ifndef SKIP_ERROR_HANDLING
if(ret==0){
#endif
httpcBeginRequest(&context);
long int contentsize=0; // Crash on the += if u32. WTF?
u32 readSize=0;
httpcGetResponseStatusCode(&context, &statuscode, 0);
if (statuscode == 200) {
unsigned char *buffer = (unsigned char*)malloc(0x1000);
do {
ret = httpcDownloadData(&context, buffer+contentsize, 0x1000, &readSize);
contentsize += readSize;
if (ret == (s32)HTTPC_RESULTCODE_DOWNLOADPENDING)
buffer = (unsigned char*)realloc(buffer, contentsize + 0x1000);
} while (ret == (s32)HTTPC_RESULTCODE_DOWNLOADPENDING);
buffer = (unsigned char*)realloc(buffer, contentsize + 1);
buffer[contentsize] = 0;
lua_pushlstring(L,(const char*)buffer,contentsize);
free(buffer);
}
#ifndef SKIP_ERROR_HANDLING
}
#endif
} while ((statuscode >= 301 && statuscode <= 303) || (statuscode >= 307 && statuscode <= 308));
#ifndef SKIP_ERROR_HANDLING
if ((statuscode < 200 && statuscode > 226) && statuscode != 304) luaL_error(L, "error opening url");
#endif
httpcCloseContext(&context);
return 1;
}
static int SpaceCounter(char* string){
int res=0;
while (*string){
if (string[0] == 0x20) res++;
string++;
}
return res;
}
static int lua_sendmail(lua_State *L){ //BETA func
int argc = lua_gettop(L);
#ifndef SKIP_ERROR_HANDLING
if (argc != 3) return luaL_error(L, "wrong number of arguments");
#endif
char* to = (char*)luaL_checkstring(L,1);
char* subj = (char*)luaL_checkstring(L,2);
char* mex = (char*)luaL_checkstring(L,3);
int req_size = 70;
int nss = SpaceCounter(subj);
int nsm = SpaceCounter(mex);
req_size = req_size + (nss<<1) + (nsm<<1) + strlen(subj) + strlen(mex) + strlen(to);
char* url = (char*)malloc(req_size);
strcpy(url,"http://rinnegatamante.netsons.org/tmp_mail_lpp_beta.php?t=");
strcat(url,to);
strcat(url,"&s=");
char* subj_p = subj;
char* url_p = &url[strlen(url)];
while (*subj_p){
if (subj_p[0] == 0x20){
url_p[0] = '%';
url_p++;
url_p[0] = '2';
url_p++;
url_p[0] = '0';
}else url_p[0] = subj_p[0];
url_p++;
subj_p++;
}
strcat(url,"&b=");
char* mex_p = mex;
url_p = &url[strlen(url)];
while (*mex_p){
if (mex_p[0] == 0x20){
url_p[0] = '%';
url_p++;
url_p[0] = '2';
url_p++;
url_p[0] = '0';
}else url_p[0] = mex_p[0];
url_p++;
mex_p++;
}
url_p[0] = 0;
httpcContext context;
Result ret = httpcOpenContext(&context, HTTPC_METHOD_GET, (char*)url , 0);
#ifndef SKIP_ERROR_HANDLING
if(ret==0){
#endif
httpcBeginRequest(&context);
HTTPC_RequestStatus loading;
httpcGetRequestState(&context, &loading);
while (loading == HTTPC_STATUS_REQUEST_IN_PROGRESS){
httpcGetRequestState(&context, &loading);
}
u32 statuscode=0;
u32 contentsize=0;
httpcGetResponseStatusCode(&context, &statuscode, 0);
if (statuscode != 200) luaL_error(L, "request error");
httpcGetDownloadSizeState(&context, NULL, &contentsize);
u8 response;
httpcDownloadData(&context, &response, contentsize, NULL);
lua_pushboolean(L,response);
free(url);
#ifndef SKIP_ERROR_HANDLING
}else luaL_error(L, "error opening url");
#endif
httpcCloseContext(&context);
return 1;
}
static int lua_wifilevel(lua_State *L){
int argc = lua_gettop(L);
#ifndef SKIP_ERROR_HANDLING
if (argc != 0) return luaL_error(L, "wrong number of arguments");
#endif
u8* wifi_link = (u8*)0x1FF81066;
lua_pushinteger(L,*wifi_link);
return 1;
}
static int lua_initSock(lua_State *L)
{
int argc = lua_gettop(L);
#ifndef SKIP_ERROR_HANDLING
if (argc != 0) return luaL_error(L, "wrong number of arguments");
#endif
// Init default socketing
ftp_init();
ftp_state = true;
sprintf(shared_ftp,"Waiting for connection...");
connfd = -1;
// Init SSL support
sslcCreateRootCertChain(&RootCertChain_contexthandle);
// Add default certificates
for (int i=0x7;i<0xC;i++){
sslcRootCertChainAddDefaultCert(RootCertChain_contexthandle, (SSLC_DefaultRootCert)i, NULL);
}
return 0;
}
static int lua_createServerSocket(lua_State *L)
{
int argc = lua_gettop(L);
#ifndef SKIP_ERROR_HANDLING
if (argc != 1 && argc != 2) return luaL_error(L, "Socket.createServerSocket(port) takes one argument.");
#endif
int port = luaL_checkinteger(L, 1);
int type = IPPROTO_TCP;
if (argc == 2) type = luaL_checkinteger(L, 2);
Socket* my_socket = (Socket*) malloc(sizeof(Socket));
my_socket->serverSocket = true;
if (type == IPPROTO_TCP) my_socket->sock = socket(AF_INET, SOCK_STREAM, 0);
else my_socket->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
#ifndef SKIP_ERROR_HANDLING
if (my_socket->sock <= 0) return luaL_error(L, "invalid socket.");
#endif
int rcvbuf = 32768;
setsockopt(my_socket->sock, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf));
struct sockaddr_in addrTo;
addrTo.sin_family = AF_INET;
addrTo.sin_port = htons(port);
if (type == IPPROTO_TCP) addrTo.sin_addr.s_addr = 0;
else addrTo.sin_addr.s_addr = gethostid();
int err = bind(my_socket->sock, (struct sockaddr*)&addrTo, sizeof(addrTo));
#ifndef SKIP_ERROR_HANDLING
if (err != 0) return luaL_error(L, "bind error.");
#endif
fcntl(my_socket->sock, F_SETFL, O_NONBLOCK);
if (type == IPPROTO_TCP){
err = listen(my_socket->sock, 1);
#ifndef SKIP_ERROR_HANDLING
if (err != 0) return luaL_error(L, "listen error.");
#endif
}
my_socket->magic = 0xDEADDEAD;
lua_pushinteger(L,(u32)my_socket);
return 1;
}
static int lua_shutSock(lua_State *L){
int argc = lua_gettop(L);
#ifndef SKIP_ERROR_HANDLING
if (argc != 0) return luaL_error(L, "wrong number of arguments");
#endif
// Closing default socketing
ftp_exit();
ftp_state = false;
// Closing SSL
sslcDestroyRootCertChain(RootCertChain_contexthandle);
return 0;
}
static int lua_recv(lua_State *L){
int argc = lua_gettop(L);
#ifndef SKIP_ERROR_HANDLING
if (argc != 2) return luaL_error(L, "wrong number of arguments");
#endif
Socket* my_socket = (Socket*)luaL_checkinteger(L, 1);
u32 size = luaL_checkinteger(L, 2);
#ifndef SKIP_ERROR_HANDLING
if (my_socket->magic != 0xDEADDEAD) return luaL_error(L, "attempt to access wrong memory block type");
if (my_socket->serverSocket) return luaL_error(L, "recv not allowed for server sockets.");
#endif
char* data = (char*)malloc(size);
int count = 0;
if (my_socket->isSSL) count = sslcRead(&my_socket->sslc_context, data, size, false);
else count = recv(my_socket->sock, data, size, 0);
if (count > 0) lua_pushlstring(L, data, count);
else lua_pushstring(L, "");
free(data);
return 1;
}
static int lua_send(lua_State *L){
int argc = lua_gettop(L);
#ifndef SKIP_ERROR_HANDLING
if (argc != 2) return luaL_error(L, "wrong number of arguments");
#endif
Socket* my_socket = (Socket*)luaL_checkinteger(L, 1);
size_t size;
char* text = (char*)luaL_checklstring(L, 2, &size);
#ifndef SKIP_ERROR_HANDLING
if (my_socket->magic != 0xDEADDEAD) return luaL_error(L, "attempt to access wrong memory block type");
if (my_socket->serverSocket) return luaL_error(L, "send not allowed for server sockets.");
if (!text) return luaL_error(L, "Socket.send() expected a string.");
#endif
int result = 0;
if (my_socket->isSSL) result = sslcWrite(&my_socket->sslc_context, text, size);
else result = sendto(my_socket->sock, text, size, 0, NULL, 0);
lua_pushinteger(L, result);
return 1;
}
static int lua_connect(lua_State *L){
int argc = lua_gettop(L);
#ifndef SKIP_ERROR_HANDLING
if (argc != 2 && argc != 4 && argc != 3) return luaL_error(L, "wrong number of arguments");
#endif
// Getting arguments
char *host = (char*)luaL_checkstring(L, 1);
int port = luaL_checkinteger(L, 2);
bool isSSL = false;
if (argc >= 3) isSSL = lua_toboolean(L,3);
int type = IPPROTO_TCP;
if (argc == 4) type = luaL_checkinteger(L, 4);
char port_str[8];
sprintf(port_str,"%i",port);
// Allocating Socket memblock
Socket* my_socket = (Socket*) malloc(sizeof(Socket));
my_socket->serverSocket = false;
my_socket->magic = 0xDEADDEAD;
my_socket->isSSL = isSSL;
// Creating socket
if (type == IPPROTO_TCP) my_socket->sock = socket(AF_INET, SOCK_STREAM, 0);
else my_socket->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
#ifndef SKIP_ERROR_HANDLING
if (my_socket->sock < 0){
free(my_socket);
return luaL_error(L, "Failed creating socket.");
}
#endif
// Resolving host
struct addrinfo hints;
struct addrinfo *resaddr = NULL, *resaddr_cur;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
getaddrinfo(host, port_str, &hints, &resaddr);
// Connecting to the server
for(resaddr_cur = resaddr; resaddr_cur!=NULL; resaddr_cur = resaddr_cur->ai_next){
if(connect(my_socket->sock, resaddr_cur->ai_addr, resaddr_cur->ai_addrlen)==0)break;
}
freeaddrinfo(resaddr);
#ifndef SKIP_ERROR_HANDLING
if(resaddr_cur==NULL){
closesocket(my_socket->sock);
free(my_socket);
return luaL_error(L, "Failed connecting to the server.");
}
#endif
// Opening SSL connection
if (isSSL){
Result ret = sslcCreateContext(&my_socket->sslc_context, my_socket->sock, SSLCOPT_DisableVerify, host);
#ifndef SKIP_ERROR_HANDLING
if(R_FAILED(ret)){
closesocket(my_socket->sock);
free(my_socket);
return luaL_error(L, "Failed creating SSL context.");
}
#endif
sslcContextSetRootCertChain(&my_socket->sslc_context, RootCertChain_contexthandle);
ret = sslcStartConnection(&my_socket->sslc_context, NULL, NULL);
#ifndef SKIP_ERROR_HANDLING
if(R_FAILED(ret)){
closesocket(my_socket->sock);
free(my_socket);
return luaL_error(L, "SSL connection failed.");
}
#endif
}
// Setting socket options
int sock_buffersize = 32768;
setsockopt(my_socket->sock, SOL_SOCKET, SO_RCVBUF, &sock_buffersize, sizeof(sock_buffersize));
int flags = fcntl(my_socket->sock, F_GETFL, 0);
fcntl(my_socket->sock, F_SETFL, flags | O_NONBLOCK);
lua_pushinteger(L, (u32)my_socket);
return 1;
}
static int lua_addCert(lua_State *L)
{
int argc = lua_gettop(L);
#ifndef SKIP_ERROR_HANDLING
if (argc != 1) return luaL_error(L, "wrong number of arguments");
#endif
const char *text = luaL_checkstring(L, 1);
fileStream fileHandle;
if (strncmp("romfs:/",text,7) == 0){
fileHandle.isRomfs = true;
FILE* handle = fopen(text,"r");
#ifndef SKIP_ERROR_HANDLING
if (handle == NULL) return luaL_error(L, "file doesn't exist.");
#endif
fileHandle.handle = (u32)handle;
}else{
fileHandle.isRomfs = false;
FS_Path filePath = fsMakePath(PATH_ASCII, text);
FS_Archive script=(FS_Archive){ARCHIVE_SDMC, (FS_Path){PATH_EMPTY, 1, (u8*)""}};
Result ret = FSUSER_OpenFileDirectly( &fileHandle.handle, script, filePath, FS_OPEN_READ, 0x00000000);
#ifndef SKIP_ERROR_HANDLING
if (ret) return luaL_error(L, "file doesn't exist.");
#endif
}
u64 cert_size;
u32 bytesRead;
FS_GetSize(&fileHandle, &cert_size);
u8* cert = (u8*)malloc(cert_size);
FS_Read(&fileHandle, &bytesRead, 0, cert, cert_size);
sslcAddTrustedRootCA(RootCertChain_contexthandle, cert, cert_size, NULL);
free(cert);
FS_Close(&fileHandle);
return 0;
}
static int lua_accept(lua_State *L){
int argc = lua_gettop(L);
#ifndef SKIP_ERROR_HANDLING
if (argc != 1) return luaL_error(L, "wrong number of arguments");
#endif
Socket* my_socket = (Socket*)luaL_checkinteger(L, 1);
#ifndef SKIP_ERROR_HANDLING
if (my_socket->magic != 0xDEADDEAD) return luaL_error(L, "attempt to access wrong memory block type");
if (!my_socket->serverSocket) return luaL_error(L, "accept allowed for server sockets only.");
#endif
struct sockaddr_in addrAccept;
socklen_t cbAddrAccept = sizeof(addrAccept);
int sockClient = accept(my_socket->sock, (struct sockaddr*)&addrAccept, &cbAddrAccept);
if (sockClient <= 0) return 0;
Socket* incomingSocket = (Socket*) malloc(sizeof(Socket));
incomingSocket->serverSocket = 0;
incomingSocket->sock = sockClient;
incomingSocket->magic = 0xDEADDEAD;
int rcvbuf = 32768;
setsockopt(incomingSocket->sock, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf));
lua_pushinteger(L, (u32)incomingSocket);
return 1;
}
static int lua_closeSock(lua_State *L){
int argc = lua_gettop(L);
#ifndef SKIP_ERROR_HANDLING
if (argc != 1) return luaL_error(L, "Socket.close() takes one argument.");
#endif
Socket* my_socket = (Socket*)luaL_checkinteger(L, 1);
#ifndef SKIP_ERROR_HANDLING
if (my_socket->magic != 0xDEADDEAD) return luaL_error(L, "attempt to access wrong memory block type");
#endif
if (my_socket->isSSL) sslcDestroyContext(&my_socket->sslc_context);
closesocket(my_socket->sock);
free(my_socket);
return 0;
}
//Register our Network Functions
static const luaL_Reg Network_functions[] = {
{"updateFTP", lua_checkFTPcommand},
{"isWifiEnabled", lua_wifistat},
{"getWifiLevel", lua_wifilevel},
{"getMacAddress", lua_macaddr},
{"getIPAddress", lua_ipaddr},
{"downloadFile", lua_download},
{"requestString", lua_downstring},
{"sendMail", lua_sendmail},
{"addCertificate",lua_addCert},
{0, 0}
};
//Register our Socket Functions
static const luaL_Reg Socket_functions[] = {
{"init", lua_initSock},
{"term", lua_shutSock},
{"createServerSocket",lua_createServerSocket},
{"connect", lua_connect},
{"receive", lua_recv},
{"send", lua_send},
{"accept", lua_accept},
{"close", lua_closeSock},
{0, 0}
};
void luaNetwork_init(lua_State *L) {
lua_newtable(L);
luaL_setfuncs(L, Network_functions, 0);
lua_setglobal(L, "Network");
lua_newtable(L);
luaL_setfuncs(L, Socket_functions, 0);
lua_setglobal(L, "Socket");
u8 GET_METHOD = 1;
u8 POST_METHOD = 2;
u8 HEAD_METHOD = 3;
VariableRegister(L,IPPROTO_UDP);
VariableRegister(L,IPPROTO_TCP);
VariableRegister(L,POST_METHOD);
VariableRegister(L,GET_METHOD);
VariableRegister(L,HEAD_METHOD);
}