-
Notifications
You must be signed in to change notification settings - Fork 17
/
ps2link.c
887 lines (648 loc) · 26.8 KB
/
ps2link.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
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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <time.h>
#include <sys/stat.h>
#include <pthread.h>
#ifndef _WIN32
#include <netinet/in.h>
#else
#include <windows.h>
#define sleep(x) Sleep(x * 1000)
#endif
#include "network.h"
#include "ps2link.h"
#include "utility.h"
int console_socket = -1;
int request_socket = -1;
int command_socket = -1;
pthread_t console_thread_id;
pthread_t request_thread_id;
int ps2link_counter = 0;
// ps2link_dd is now an array of structs
struct {
char *pathname; // remember to free when closing dir
DIR *dir;
} ps2link_dd[10] = {
{ NULL, NULL }, { NULL, NULL }, { NULL, NULL }, { NULL, NULL }, { NULL, NULL },
{ NULL, NULL }, { NULL, NULL }, { NULL, NULL }, { NULL, NULL }, { NULL, NULL }
};
///////////////////////
// PS2LINK FUNCTIONS //
///////////////////////
int ps2link_connect(char *hostname) {
// Connect to the console port.
console_socket = network_listen(0x4712, SOCK_DGRAM);
// Create the console thread.
if (console_socket > 0) { pthread_create(&console_thread_id, NULL, ps2link_thread_console, (void *)&console_thread_id); }
// Connect to the request port.
request_socket = network_connect(hostname, 0x4711, SOCK_STREAM);
// Create the request thread.
if (request_socket > 0) { pthread_create(&request_thread_id, NULL, ps2link_thread_request, (void *)&request_thread_id); }
// Connect to the command port.
command_socket = network_connect(hostname, 0x4712, SOCK_DGRAM);
// Delay for a moment to let ps2link finish setup.
#ifdef _WIN32
Sleep(1);
#else
sleep(1);
#endif
// End function.
return 0;
}
int ps2link_mainloop(int timeout) {
// Disconnect from the command port.
if (network_disconnect(command_socket) < 0) { return -1; }
// If no timeout was given, timeout immediately.
if (timeout == 0) { return 0; }
// If timeout was never, loop forever.
if (timeout < 0) { for (;;) { sleep(600); } }
// Increment the timeout counter until timeout is reached.
while (ps2link_counter++ < timeout) { sleep(1); };
// End function.
return 0;
}
int ps2link_disconnect(void) {
// Disconnect from the command port.
if (network_disconnect(command_socket) < 0) { return -1; }
// Disconnect from the request port.
if (network_disconnect(request_socket) < 0) { return -1; }
// Disconnect from console port.
if (network_disconnect(console_socket) < 0) { return -1; }
// End function.
return 0;
}
///////////////////////////////
// PS2LINK COMMAND FUNCTIONS //
///////////////////////////////
int ps2link_command_reset(void) {
struct { unsigned int number; unsigned short length; } PACKED command;
// Build the command packet.
command.number = htonl(PS2LINK_COMMAND_RESET);
command.length = htons(sizeof(command));
// Send the command packet.
return network_send(command_socket, &command, sizeof(command));
}
int ps2link_command_execiop(int argc, char **argv) {
struct { unsigned int number; unsigned short length; int argc; char argv[256]; } PACKED command;
// Build the command packet.
command.number = htonl(PS2LINK_COMMAND_EXECIOP);
command.length = htons(sizeof(command));
command.argc = htonl(argc);
fix_argv(command.argv, argv);
// Send the command packet.
return network_send(command_socket, &command, sizeof(command));
}
int ps2link_command_execee(int argc, char **argv) {
struct { unsigned int number; unsigned short length; int argc; char argv[256]; } PACKED command;
// Build the command packet.
command.number = htonl(PS2LINK_COMMAND_EXECEE);
command.length = htons(sizeof(command));
command.argc = htonl(argc);
fix_argv(command.argv, argv);
// Send the command packet.
return network_send(command_socket, &command, sizeof(command));
}
int ps2link_command_poweroff(void) {
struct { unsigned int number; unsigned short length; } PACKED command;
// Build the command packet.
command.number = htonl(PS2LINK_COMMAND_POWEROFF);
command.length = htons(sizeof(command));
// Send the command packet.
return network_send(command_socket, &command, sizeof(command));
}
int ps2link_command_scrdump(void) {
struct { unsigned int number; unsigned short length; } PACKED command;
// Build the command packet.
command.number = htonl(PS2LINK_COMMAND_SCRDUMP);
command.length = htons(sizeof(command));
// Send the command packet.
return network_send(command_socket, &command, sizeof(command));
}
int ps2link_command_netdump(void) {
struct { unsigned int number; unsigned short length; } PACKED command;
// Build the command packet.
command.number = htonl(PS2LINK_COMMAND_NETDUMP);
command.length = htons(sizeof(command));
// Send the command packet.
return network_send(command_socket, &command, sizeof(command));
}
int ps2link_command_dumpmem(unsigned int offset, unsigned int size, char *pathname) {
struct { unsigned int number; unsigned short length; unsigned int offset; unsigned int size; char pathname[256]; } PACKED command;
// Build the command packet.
command.number = htonl(PS2LINK_COMMAND_DUMPMEM);
command.length = htons(sizeof(command));
command.offset = htonl(offset);
command.size = htonl(size);
if (pathname) { strncpy(command.pathname, pathname, 256); }
// Send the command packet.
return network_send(command_socket, &command, sizeof(command));
}
int ps2link_command_startvu(int vu) {
struct { unsigned int number; unsigned short length; int vu; } PACKED command;
// Build the command packet.
command.number = htonl(PS2LINK_COMMAND_STARTVU);
command.length = htons(sizeof(command));
command.vu = htonl(vu);
// Send the command packet.
return network_send(command_socket, &command, sizeof(command));
}
int ps2link_command_stopvu(int vu) {
struct { unsigned int number; unsigned short length; int vu; } PACKED command;
// Build the command packet.
command.number = htonl(PS2LINK_COMMAND_STOPVU);
command.length = htons(sizeof(command));
command.vu = htonl(vu);
// Send the command packet.
return network_send(command_socket, &command, sizeof(command));
}
int ps2link_command_dumpreg(int type, char *pathname) {
struct { unsigned int number; unsigned short length; int type; char pathname[256]; } PACKED command;
// Build the command packet.
command.number = htonl(PS2LINK_COMMAND_DUMPREG);
command.length = htons(sizeof(command));
command.type = htonl(type);
if (pathname) { strncpy(command.pathname, pathname, 256); }
// Send the command packet.
return network_send(command_socket, &command, sizeof(command));
}
int ps2link_command_gsexec(unsigned short size, char *pathname) {
struct { unsigned int number; unsigned short length; unsigned short size; char pathname[256]; } PACKED command;
// Build the command packet..
command.number = htonl(PS2LINK_COMMAND_GSEXEC);
command.length = htons(sizeof(command));
command.size = htonl(size);
if (pathname) { strncpy(command.pathname, pathname, 256); }
// Send the command packet.
return network_send(command_socket, &command, sizeof(command));
}
int ps2link_command_writemem(unsigned int offset, unsigned int size, char *pathname) {
struct { unsigned int number; unsigned short length; unsigned int offset; unsigned int size; char pathname[256]; } PACKED command;
// Build the command packet.
command.number = htonl(PS2LINK_COMMAND_WRITEMEM);
command.length = htons(sizeof(command));
command.offset = htonl(offset);
command.size = htonl(size);
if (pathname) { strncpy(command.pathname, pathname, 256); }
// Send the command packet.
return network_send(command_socket, &command, sizeof(command));
}
int ps2link_command_iopexcep(void) {
struct { unsigned int number; unsigned short length; } PACKED command;
// Build the command packet.
command.number = htonl(PS2LINK_COMMAND_IOPEXCEP);
command.length = htons(sizeof(command));
// Send the command packet.
return network_send(command_socket, &command, sizeof(command));
// End function.
return 0;
}
///////////////////////////////
// PS2LINK REQUEST FUNCTIONS //
///////////////////////////////
int ps2link_request_open(void *packet) {
struct { unsigned int number; unsigned short length; int flags; char pathname[256]; } PACKED *request = packet;
int result = -1;
struct stat stats;
// Fix the arguments.
fix_pathname(request->pathname);
request->flags = fix_flags(ntohl(request->flags));
if(((stat(request->pathname, &stats) == 0) && (!S_ISDIR(stats.st_mode))) || (request->flags & O_CREAT))
{
// Perform the request.
#if defined (__CYGWIN__) || defined (__MINGW32__)
result = open(request->pathname, request->flags | O_BINARY, 0644);
#else
result = open(request->pathname, request->flags, 0644);
#endif
}
// Send the response.
return ps2link_response_open(result);
}
int ps2link_request_close(void *packet) {
struct { unsigned int number; unsigned short length; int fd; } PACKED *request = packet;
int result = -1;
// Perform the request.
result = close(ntohl(request->fd));
// Send the response.
return ps2link_response_close(result);
}
int ps2link_request_read(void *packet) {
struct { unsigned int number; unsigned short length; int fd; int size; } PACKED *request = packet;
int result = -1, size = -1; char buffer[65536];
// If a big read is requested...
if (ntohl(request->size) > sizeof(buffer)) {
// Allocate the bigbuffer.
char *bigbuffer = malloc(ntohl(request->size));
// Perform the request.
result = size = read(ntohl(request->fd), bigbuffer, ntohl(request->size));
// Send the response.
ps2link_response_read(result, size);
// Send the response data.
network_send(request_socket, bigbuffer, size);
// Free the bigbuffer.
free(bigbuffer);
// Else, a normal read is requested...
} else {
// Perform the request.
result = size = read(ntohl(request->fd), buffer, ntohl(request->size));
// Send the response.
ps2link_response_read(result, size);
// Send the response data.
network_send(request_socket, buffer, size);
}
// End function.
return 0;
}
int ps2link_request_write(void *packet) {
struct { unsigned int number; unsigned short length; int fd; int size; } PACKED *request = packet;
int result = -1; char buffer[65536];
// If a big write is requested...
if (ntohl(request->size) > sizeof(buffer)) {
// Allocate the bigbuffer.
char *bigbuffer = malloc(ntohl(request->size));
// Read the request data.
network_receive_all(request_socket, bigbuffer, ntohl(request->size));
// Perform the request.
result = write(ntohl(request->fd), bigbuffer, ntohl(request->size));
// Send the response.
ps2link_response_write(result);
// Free the bigbuffer.
free(bigbuffer);
// Else, a normal write is requested...
} else {
// Read the request data.
network_receive_all(request_socket, buffer, ntohl(request->size));
// Perform the request.
result = write(ntohl(request->fd), buffer, ntohl(request->size));
// Send the response.
ps2link_response_write(result);
}
// End function.
return 0;
}
int ps2link_request_lseek(void *packet) {
struct { unsigned int number; unsigned short length; int fd, offset, whence; } PACKED *request = packet;
int result = -1;
// Perform the request.
result = lseek(ntohl(request->fd), ntohl(request->offset), ntohl(request->whence));
// Send the response.
return ps2link_response_lseek(result);
}
int ps2link_request_opendir(void *packet) {
struct { unsigned int command; unsigned short length; int flags; char pathname[256]; } PACKED *request = packet;
int result = -1;
struct stat stats;
// Fix the arguments.
fix_pathname(request->pathname);
if((stat(request->pathname, &stats) == 0) && (S_ISDIR(stats.st_mode)))
{
// Allocate an available directory descriptor.
for (int loop0=0; loop0<10; loop0++) { if (ps2link_dd[loop0].dir == NULL) { result = loop0; break; } }
// Perform the request.
if (result != -1)
{
ps2link_dd[result].pathname = (char *) malloc(strlen(request->pathname) + 1);
strcpy(ps2link_dd[result].pathname, request->pathname);
ps2link_dd[result].dir = opendir(request->pathname);
}
}
// Send the response.
return ps2link_response_opendir(result);
}
int ps2link_request_closedir(void *packet) {
struct { unsigned int number; unsigned short length; int dd; } PACKED *request = packet;
int result = -1;
// Perform the request.
result = closedir(ps2link_dd[ntohl(request->dd)].dir);
if(ps2link_dd[ntohl(request->dd)].pathname)
{
free(ps2link_dd[ntohl(request->dd)].pathname);
ps2link_dd[ntohl(request->dd)].pathname = NULL;
}
// Free the directory descriptor.
ps2link_dd[ntohl(request->dd)].dir = NULL;
// Send the response.
return ps2link_response_closedir(result);
}
int ps2link_request_readdir(void *packet) {
DIR *dir;
struct { unsigned int number; unsigned short length; int dd; } PACKED *request = packet;
struct dirent *dirent; struct stat stats; struct tm *loctime;
unsigned int mode; unsigned char ctime[8]; unsigned char atime[8]; unsigned char mtime[8];
char tname[512];
dir = ps2link_dd[ntohl(request->dd)].dir;
// Perform the request.
dirent = readdir(dir);
// If no more entries were found...
if (dirent == NULL) {
// Tell the user an entry wasn't found.
return ps2link_response_readdir(0, 0, 0, 0, NULL, NULL, NULL, 0, NULL);
}
// need to specify the directory as well as file name otherwise uses CWD!
sprintf(tname, "%s/%s", ps2link_dd[ntohl(request->dd)].pathname, dirent->d_name);
// Fetch the entry's statistics.
stat(tname, &stats);
// Convert the mode.
mode = (stats.st_mode & 0x07);
if (S_ISDIR(stats.st_mode)) { mode |= 0x20; }
#ifndef _WIN32
if (S_ISLNK(stats.st_mode)) { mode |= 0x08; }
#endif
if (S_ISREG(stats.st_mode)) { mode |= 0x10; }
// Convert the creation time.
loctime = localtime(&(stats.st_ctime));
ctime[6] = (unsigned char)loctime->tm_year;
ctime[5] = (unsigned char)loctime->tm_mon + 1;
ctime[4] = (unsigned char)loctime->tm_mday;
ctime[3] = (unsigned char)loctime->tm_hour;
ctime[2] = (unsigned char)loctime->tm_min;
ctime[1] = (unsigned char)loctime->tm_sec;
// Convert the access time.
loctime = localtime(&(stats.st_atime));
atime[6] = (unsigned char)loctime->tm_year;
atime[5] = (unsigned char)loctime->tm_mon + 1;
atime[4] = (unsigned char)loctime->tm_mday;
atime[3] = (unsigned char)loctime->tm_hour;
atime[2] = (unsigned char)loctime->tm_min;
atime[1] = (unsigned char)loctime->tm_sec;
// Convert the last modified time.
loctime = localtime(&(stats.st_mtime));
mtime[6] = (unsigned char)loctime->tm_year;
mtime[5] = (unsigned char)loctime->tm_mon + 1;
mtime[4] = (unsigned char)loctime->tm_mday;
mtime[3] = (unsigned char)loctime->tm_hour;
mtime[2] = (unsigned char)loctime->tm_min;
mtime[1] = (unsigned char)loctime->tm_sec;
// Send the response.
return ps2link_response_readdir(1, mode, 0, stats.st_size, ctime, atime, mtime, 0, dirent->d_name);
}
int ps2link_request_remove(void *packet) {
struct { unsigned int number; unsigned short length; char name[256]; } PACKED *request = packet;
int result = -1;
// Fix the arguments.
fix_pathname(request->name);
// Perform the request.
result = remove(request->name);
// Send the response.
return ps2link_response_remove(result);
}
int ps2link_request_mkdir(void *packet) {
struct { unsigned int number; unsigned short length; int mode; char name[256]; } PACKED *request = packet;
int result = -1;
// Fix the arguments.
fix_pathname(request->name);
// request->flags = fix_flags(ntohl(request->flags));
// Perform the request.
// do we need to use mode in here: request->mode ?
#ifdef _WIN32
result = mkdir(request->name);
#else
result = mkdir(request->name, request->mode);
#endif
// Send the response.
return ps2link_response_mkdir(result);
}
int ps2link_request_rmdir(void *packet) {
struct { unsigned int number; unsigned short length; char name[256]; } PACKED *request = packet;
int result = -1;
// Fix the arguments.
fix_pathname(request->name);
// Perform the request.
result = rmdir(request->name);
// Send the response.
return ps2link_response_rmdir(result);
}
int ps2link_request_getstat(void *packet) {
struct { unsigned int number; unsigned short length; char name[256]; } PACKED *request = packet;
struct stat stats; struct tm *loctime;
int ret;
unsigned int mode = 0;
unsigned char ctime[8]; unsigned char atime[8]; unsigned char mtime[8];
// Fix the arguments.
fix_pathname(request->name);
// Fetch the entry's statistics.
ret = stat(request->name, &stats);
if (ret == 0) {
// Convert the mode.
mode = (stats.st_mode & 0x07);
if (S_ISDIR(stats.st_mode)) { mode |= 0x20; }
#ifndef _WIN32
if (S_ISLNK(stats.st_mode)) { mode |= 0x08; }
#endif
if (S_ISREG(stats.st_mode)) { mode |= 0x10; }
// Convert the creation time.
loctime = localtime(&(stats.st_ctime));
ctime[6] = (unsigned char)loctime->tm_year;
ctime[5] = (unsigned char)loctime->tm_mon + 1;
ctime[4] = (unsigned char)loctime->tm_mday;
ctime[3] = (unsigned char)loctime->tm_hour;
ctime[2] = (unsigned char)loctime->tm_min;
ctime[1] = (unsigned char)loctime->tm_sec;
// Convert the access time.
loctime = localtime(&(stats.st_atime));
atime[6] = (unsigned char)loctime->tm_year;
atime[5] = (unsigned char)loctime->tm_mon + 1;
atime[4] = (unsigned char)loctime->tm_mday;
atime[3] = (unsigned char)loctime->tm_hour;
atime[2] = (unsigned char)loctime->tm_min;
atime[1] = (unsigned char)loctime->tm_sec;
// Convert the last modified time.
loctime = localtime(&(stats.st_mtime));
mtime[6] = (unsigned char)loctime->tm_year;
mtime[5] = (unsigned char)loctime->tm_mon + 1;
mtime[4] = (unsigned char)loctime->tm_mday;
mtime[3] = (unsigned char)loctime->tm_hour;
mtime[2] = (unsigned char)loctime->tm_min;
mtime[1] = (unsigned char)loctime->tm_sec;
}
return ps2link_response_getstat(ret, mode, 0, stats.st_size, ctime, atime, mtime, 0);
}
////////////////////////////////
// PS2LINK RESPONSE FUNCTIONS //
////////////////////////////////
int ps2link_response_open(int result) {
struct { unsigned int number; unsigned short length; int result; } PACKED response;
// Build the response packet.
response.number = htonl(PS2LINK_RESPONSE_OPEN);
response.length = htons(sizeof(response));
response.result = htonl(result);
// Send the response packet.
return network_send(request_socket, &response, sizeof(response));
}
int ps2link_response_close(int result) {
struct { unsigned int number; unsigned short length; int result; } PACKED response;
// Build the response packet.
response.number = htonl(PS2LINK_RESPONSE_CLOSE);
response.length = htons(sizeof(response));
response.result = htonl(result);
// Send the response packet.
return network_send(request_socket, &response, sizeof(response));
}
int ps2link_response_read(int result, int size) {
struct { unsigned int number; unsigned short length; int result; int size; } PACKED response;
// Build the response packet.
response.number = htonl(PS2LINK_RESPONSE_READ);
response.length = htons(sizeof(response));
response.result = htonl(result);
response.size = htonl(size);
// Send the response packet.
return network_send(request_socket, &response, sizeof(response));
}
int ps2link_response_write(int result) {
struct { unsigned int number; unsigned short length; int result; } PACKED response;
// Build the response packet.
response.number = htonl(PS2LINK_RESPONSE_WRITE);
response.length = htons(sizeof(response));
response.result = htonl(result);
// Send the response packet.
return network_send(request_socket, &response, sizeof(response));
}
int ps2link_response_lseek(int result) {
struct { unsigned int number; unsigned short length; int result; } PACKED response;
// Build the response packet.
response.number = htonl(PS2LINK_RESPONSE_LSEEK);
response.length = htons(sizeof(response));
response.result = htonl(result);
// Send the response packet.
return network_send(request_socket, &response, sizeof(response));
}
int ps2link_response_opendir(int result) {
struct { unsigned int number; unsigned short length; int result; } PACKED response;
// Build the response packet.
response.number = htonl(PS2LINK_RESPONSE_OPENDIR);
response.length = htons(sizeof(response));
response.result = htonl(result);
// Send the response packet.
return network_send(request_socket, &response, sizeof(response));
}
int ps2link_response_closedir(int result) {
struct { unsigned int number; unsigned short length; int result; } PACKED response;
// Build the response packet.
response.number = htonl(PS2LINK_RESPONSE_CLOSEDIR);
response.length = htons(sizeof(response));
response.result = htonl(result);
// Send the response packet.
return network_send(request_socket, &response, sizeof(response));
}
int ps2link_response_readdir(int result, unsigned int mode, unsigned int attr, unsigned int size, unsigned char *ctime, unsigned char *atime, unsigned char *mtime, unsigned int hisize, char *name) {
struct { unsigned int number; unsigned short length; int result; unsigned int mode; unsigned int attr; unsigned int size; unsigned char ctime[8]; unsigned char atime[8]; unsigned char mtime[8]; unsigned int hisize; char name[256]; } PACKED response;
// Build the response packet.
response.number = htonl(PS2LINK_RESPONSE_READDIR);
response.length = htons(sizeof(response));
response.result = htonl(result);
response.mode = htonl(mode);
response.attr = htonl(attr);
response.size = htonl(size);
if (ctime) { memcpy(response.ctime, ctime, 8); }
if (atime) { memcpy(response.atime, atime, 8); }
if (mtime) { memcpy(response.mtime, mtime, 8); }
response.hisize = htonl(hisize);
#ifdef _WIN32
if (name) { sprintf(response.name, "%s", name); }
#else
if (name) { snprintf(response.name, 256, "%s", name); }
#endif
// Send the response packet.
return network_send(request_socket, &response, sizeof(response));
}
int ps2link_response_remove(int result) {
struct { unsigned int number; unsigned short length; int result; } PACKED response;
// Build the response packet.
response.number = htonl(PS2LINK_RESPONSE_REMOVE);
response.length = htons(sizeof(response));
response.result = htonl(result);
// Send the response packet.
return network_send(request_socket, &response, sizeof(response));
}
int ps2link_response_mkdir(int result) {
struct { unsigned int number; unsigned short length; int result; } PACKED response;
// Build the response packet.
response.number = htonl(PS2LINK_RESPONSE_MKDIR);
response.length = htons(sizeof(response));
response.result = htonl(result);
// Send the response packet.
return network_send(request_socket, &response, sizeof(response));
}
int ps2link_response_rmdir(int result) {
struct { unsigned int number; unsigned short length; int result; } PACKED response;
// Build the response packet.
response.number = htonl(PS2LINK_RESPONSE_RMDIR);
response.length = htons(sizeof(response));
response.result = htonl(result);
// Send the response packet.
return network_send(request_socket, &response, sizeof(response));
}
int ps2link_response_getstat(int result, unsigned int mode, unsigned int attr, unsigned int size, unsigned char *ctime, unsigned char *atime, unsigned char *mtime, unsigned int hisize) {
struct { unsigned int number; unsigned short length; int result; unsigned int mode; unsigned int attr; unsigned int size; unsigned char ctime[8]; unsigned char atime[8]; unsigned char mtime[8]; unsigned int hisize; } PACKED response;
// Build the response packet.
response.number = htonl(PS2LINK_RESPONSE_GETSTAT);
response.length = htons(sizeof(response));
response.result = htonl(result);
response.mode = htonl(mode);
response.attr = htonl(attr);
response.size = htonl(size);
if (ctime) { memcpy(response.ctime, ctime, 8); }
if (atime) { memcpy(response.atime, atime, 8); }
if (mtime) { memcpy(response.mtime, mtime, 8); }
response.hisize = htonl(hisize);
// Send the response packet.
return network_send(request_socket, &response, sizeof(response));
}
//////////////////////////////
// PS2LINK THREAD FUNCTIONS //
//////////////////////////////
void *ps2link_thread_console(void *thread_id) {
char buffer[1024];
// If the socket isn't open, this thread isn't needed.
if (console_socket < 0) { pthread_exit(thread_id); }
// Loop forever...
for (;;) {
// Wait for network activity.
network_wait(console_socket, -1);
// Receive the console buffer.
network_receive(console_socket, buffer, sizeof(buffer));
// Print out the console buffer.
printf("%s", buffer);
// Clear the console buffer.
memset(buffer, 0, sizeof(buffer));
// Reset the timeout counter.
ps2link_counter = 0;
}
// End function.
return NULL;
}
void *ps2link_thread_request(void *thread_id) {
struct { unsigned int number; unsigned short length; char buffer[512]; } PACKED packet;
// If the socket isn't open, this thread isn't needed.
if (request_socket < 0) { pthread_exit(thread_id); }
// Loop forever...
for (;;) {
// Wait for network activity.
network_wait(request_socket, -1);
// Read in the request packet header.
network_receive_all(request_socket, &packet, 6);
// Read in the rest of the packet.
network_receive_all(request_socket, packet.buffer, ntohs(packet.length) - 6);
// Perform the requested action.
if (ntohl(packet.number) == PS2LINK_REQUEST_OPEN) { ps2link_request_open(&packet); } else
if (ntohl(packet.number) == PS2LINK_REQUEST_CLOSE) { ps2link_request_close(&packet); } else
if (ntohl(packet.number) == PS2LINK_REQUEST_READ) { ps2link_request_read(&packet); } else
if (ntohl(packet.number) == PS2LINK_REQUEST_WRITE) { ps2link_request_write(&packet); } else
if (ntohl(packet.number) == PS2LINK_REQUEST_LSEEK) { ps2link_request_lseek(&packet); } else
if (ntohl(packet.number) == PS2LINK_REQUEST_OPENDIR) { ps2link_request_opendir(&packet); } else
if (ntohl(packet.number) == PS2LINK_REQUEST_CLOSEDIR) { ps2link_request_closedir(&packet); } else
if (ntohl(packet.number) == PS2LINK_REQUEST_READDIR) { ps2link_request_readdir(&packet); } else
if (ntohl(packet.number) == PS2LINK_REQUEST_REMOVE) { ps2link_request_remove(&packet); } else
if (ntohl(packet.number) == PS2LINK_REQUEST_MKDIR) { ps2link_request_mkdir(&packet); } else
if (ntohl(packet.number) == PS2LINK_REQUEST_RMDIR) { ps2link_request_rmdir(&packet); } else
if (ntohl(packet.number) == PS2LINK_REQUEST_GETSTAT) { ps2link_request_getstat(&packet); }
// Reset the timeout counter.
ps2link_counter = 0;
}
// End function.
return NULL;
}