-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathps2link.c
More file actions
699 lines (476 loc) · 18.5 KB
/
ps2link.c
File metadata and controls
699 lines (476 loc) · 18.5 KB
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
#include <time.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include "network.h"
#include "ps2link.h"
char buffer[65536];
int command_sock, request_sock, textlog_sock;
////////////////////////////
// PS2LINK MAIN FUNCTIONS //
////////////////////////////
int ps2link_connect(char *hostname) {
// Open the connections.
request_sock = network_connect(hostname, 0x4711, SOCKET_TCP);
textlog_sock = network_listen(0x4712, SOCKET_UDP);
// Give them some time to connect.
usleep(1);
// End function.
return 0;
}
int ps2link_mainloop(int timeout) {
struct { int number; short length; char data[65544]; } __attribute__((packed)) request;
// Main loop.
while (1) {
// Wait until something happens.
if (network_wait(timeout) == 0) { return -1; };
// Read and perform any requests as needed.
if (network_recv(request_sock, &request, sizeof(request)) > 0) {
if (ntohl(request.number) == 0xBABE0111) { ps2link_request_open ((void *)&request); } else
if (ntohl(request.number) == 0xBABE0121) { ps2link_request_close ((void *)&request); } else
if (ntohl(request.number) == 0xBABE0131) { ps2link_request_read ((void *)&request); } else
if (ntohl(request.number) == 0xBABE0141) { ps2link_request_write ((void *)&request); } else
if (ntohl(request.number) == 0xBABE0151) { ps2link_request_lseek ((void *)&request); } else
if (ntohl(request.number) == 0xBABE0161) { ps2link_request_dopen ((void *)&request); } else
if (ntohl(request.number) == 0xBABE0171) { ps2link_request_dclose ((void *)&request); } else
if (ntohl(request.number) == 0xBABE0181) { ps2link_request_dread ((void *)&request); }
}
// Read and display any textlog information.
while (network_recvfrom(textlog_sock, buffer, sizeof(buffer)) > 0) { fprintf(stdout, buffer); }
}
// End function.
return 0;
}
int ps2link_disconnect(void) {
// Close the connections.
network_disconnect(request_sock);
network_disconnect(textlog_sock);
// End function.
return 0;
}
///////////////////////////////
// PS2LINK TOOLBOX FUNCTIONS //
///////////////////////////////
int ps2link_fixflags(int flags) { int retval = 0;
// Convert the read and write flags.
if ((flags & 0x03) == 0x0001) { retval |= O_RDONLY; }
if ((flags & 0x03) == 0x0002) { retval |= O_WRONLY; }
if ((flags & 0x03) == 0x0003) { retval |= O_RDWR; }
// Convert the other flags.
if (flags & 0x0010) { retval |= O_NONBLOCK; }
if (flags & 0x0100) { retval |= O_APPEND; }
if (flags & 0x0200) { retval |= O_CREAT; }
if (flags & 0x0400) { retval |= O_TRUNC; }
// End function.
return retval;
}
int ps2link_fixpathname(char *pathname) { int loop0;
// If empty, set a pathname default.
if (pathname[0] == 0) { strcpy(pathname, "."); }
// Convert \ to / for unix compatibility.
for(loop0=0;loop0<strlen(pathname);loop0++) { if (pathname[loop0] == '\\') { pathname[loop0] = '/'; } }
// Check for a leading slash.
if (pathname[0] == '/') {
// Kill off the leading slash.
for(loop0=0;loop0<strlen(pathname)-1;loop0++) { pathname[loop0] = pathname[loop0+1]; }
// Terminate the pathname with a null.
pathname[loop0] = 0;
}
// End function.
return 0;
}
///////////////////////////////
// PS2LINK COMMAND FUNCTIONS //
///////////////////////////////
int ps2link_send_command(char *hostname, void *packet, int size) {
// Open the connection.
command_sock = network_connect(hostname, 0x4712, SOCKET_UDP);
// Send the command packet.
network_send(command_sock, packet, size);
// Close the connection.
network_disconnect(command_sock);
// End function.
return 0;
}
int ps2link_command_reset(char *hostname) {
struct { int number; short length; } __attribute__((packed)) command;
#ifndef __QUIET__
// Tell the user what we're doing.
printf("[***] Sending reset() command...\n");
#endif
// Build the command packet.
command.number = htonl(0xBABE0201);
command.length = htons(sizeof(command));
// Send the command packet.
ps2link_send_command(hostname, &command, sizeof(command));
// End function.
return 0;
}
int ps2link_command_execiop(char *hostname, int timeout, char *pathname) {
struct { int number; short length; int argc; char argv[256]; } __attribute__((packed)) command;
#ifndef __QUIET__
// Tell the user what we're doing.
printf("[***] Sending execiop(\"%s\") command...\n", pathname);
#endif
// Build the command packet.
command.number = htonl(0xBABE0202);
command.length = htons(sizeof(command));
command.argc = htonl(1);
if (pathname) { strncpy(command.argv, pathname, 256); }
// Connect to the host.
ps2link_connect(hostname);
// Send the command packet.
ps2link_send_command(hostname, &command, sizeof(command));
// Enter the main loop.
ps2link_mainloop(timeout);
// Close the connection.
ps2link_disconnect();
// End function.
return 0;
}
int ps2link_command_execee(char *hostname, int timeout, char *pathname) {
struct { int number; short length; int argc; char argv[256]; } __attribute__((packed)) command;
#ifndef __QUIET__
// Tell the user what we're doing.
printf("[***] Sending execee(\"%s\") command...\n", pathname);
#endif
// Build the command packet.
command.number = htonl(0xBABE0203);
command.length = htons(sizeof(command));
command.argc = htonl(1);
if (pathname) { strncpy(command.argv, pathname, 256); }
// Connect to the host.
ps2link_connect(hostname);
// Send the command packet.
ps2link_send_command(hostname, &command, sizeof(command));
// Enter the main loop.
ps2link_mainloop(timeout);
// Close the connection.
ps2link_disconnect();
// End function.
return 0;
}
int ps2link_command_poweroff(char *hostname) {
struct { int number; short length; } __attribute__((packed)) command;
#ifndef __QUIET__
// Tell the user what we're doing.
printf("[***] Sending poweroff() command...\n");
#endif
// Build the command packet.
command.number = htonl(0xBABE0204);
command.length = htons(sizeof(command));
// Send the command packet.
ps2link_send_command(hostname, &command, sizeof(command));
// End function.
return 0;
}
int ps2link_command_dumpmem(char *hostname, int timeout, char *pathname, int offset, int size) {
struct { int number; short length; int offset, size; char pathname[256]; } __attribute__((packed)) command;
#ifndef __QUIET__
// Tell the user what we're doing.
printf("[***] Sending dumpmem(\"%s\", %d, %d) command...\n", pathname, offset, size);
#endif
// Build the command packet.
command.number = htonl(0xBABE0207);
command.length = htons(sizeof(command));
command.offset = offset;
command.size = htonl(size);
if (pathname) { strncpy(command.pathname, pathname, 256); }
// Connect to the host.
ps2link_connect(hostname);
// Send the command packet.
ps2link_send_command(hostname, &command, sizeof(command));
// Enter the main loop.
ps2link_mainloop(timeout);
// Close the connection.
ps2link_disconnect();
// End function.
return 0;
}
int ps2link_command_startvu(char *hostname, int vu) {
struct { int number; short length; int vu; } __attribute__((packed)) command;
#ifndef __QUIET__
// Tell the user what we're doing.
printf("[***] Sending startvu(%d) command...\n", vu);
#endif
// Build the command packet.
command.number = htonl(0xBABE0208);
command.length = htons(sizeof(command));
command.vu = htonl(vu);
// Send the command packet.
ps2link_send_command(hostname, &command, sizeof(command));
// End function.
return 0;
}
int ps2link_command_stopvu(char *hostname, int vu) {
struct { int number; short length; int vu; } __attribute__((packed)) command;
#ifndef __QUIET__
// Tell the user what we're doing.
printf("[***] Sending stopvu(%d) command...\n", vu);
#endif
// Build the command packet.
command.number = htonl(0xBABE0209);
command.length = htons(sizeof(command));
command.vu = htonl(vu);
// Send the command packet.
ps2link_send_command(hostname, &command, sizeof(command));
// End function.
return 0;
}
int ps2link_command_dumpreg(char *hostname, int timeout, char *pathname, int type) {
struct { int number; short length; int type; char pathname[256]; } command;
#ifndef __QUIET__
// Tell the user what we're doing.
printf("[***] Sending dumpreg(\"%s\", %d) command...\n", pathname, type);
#endif
// Build the command packet.
command.number = htonl(0xBABE020A);
command.length = htons(sizeof(command));
command.type = htonl(type);
if (pathname) { strncpy(command.pathname, pathname, 256); }
// Connect to the host.
ps2link_connect(hostname);
// Send the command packet.
ps2link_send_command(hostname, &command, sizeof(command));
// Enter the main loop.
ps2link_mainloop(timeout);
// Close the connection.
ps2link_disconnect();
// End function.
return 0;
}
int ps2link_command_gsexec(char *hostname, int timeout, char *pathname) {
struct { int number; short length; int size; char pathname[256]; } command;
int fd, size;
#ifndef __QUIET__
// Tell the user what we're doing.
printf("[***] Sending gsexec(\"%s\", %d) command...\n", pathname, size);
#endif
// Get the file size.
fd = open(pathname, O_RDONLY); size = lseek(fd, 0, SEEK_END); close(fd);
// Build the command packet.
command.number = htonl(0xBABE020B);
command.length = htons(sizeof(command));
command.size = htonl(size);
if (pathname) { strncpy(command.pathname, pathname, 256); }
// Connect to the host.
ps2link_connect(hostname);
// Send the command packet.
ps2link_send_command(hostname, &command, sizeof(command));
// Enter the main loop.
ps2link_mainloop(timeout);
// Close the connection.
ps2link_disconnect();
// End function.
return 0;
}
int ps2link_command_listen(char *hostname, int timeout) {
#ifndef __QUIET__
// Tell the user what we're doing.
printf("[***] Entering listen mode...\n");
#endif
// Connect to the host.
ps2link_connect(hostname);
// Enter the main loop.
ps2link_mainloop(timeout);
// Close the connection.
ps2link_disconnect();
// End function.
return 0;
}
///////////////////////////////
// PS2LINK REQUEST FUNCTIONS //
///////////////////////////////
int ps2link_request_open(void *packet) {
struct { int number; short length; int flags; char pathname[256]; } __attribute__((packed)) *request = packet;
struct { int number; short length; int result; } __attribute__((packed)) response;
// Fix the requested pathname.
ps2link_fixpathname(request->pathname);
// Build the response packet.
response.number = htonl(0xBABE0112);
response.length = htons(sizeof(response));
response.result = htonl(open(request->pathname, ps2link_fixflags(ntohl(request->flags)), 0644));
// Send the response packet.
network_send(request_sock, &response, sizeof(response));
#ifdef __DEBUG__
// Tell the user about the request and its result.
printf("[***] open(\"host0:%s\", %d) = %d\n", request->pathname, ps2link_fixflags(ntohl(request->flags)), ntohl(response.result));
#endif
// End function.
return 0;
}
int ps2link_request_close(void *packet) {
struct { int number; short length; int fd; } __attribute__((packed)) *request = packet;
struct { int number; short length; int result; } __attribute__((packed)) response;
// Build the response packet.
response.number = htonl(0xBABE0122);
response.length = htons(sizeof(response));
response.result = htonl(close(ntohl(request->fd)));
// Send the response packet.
network_send(request_sock, &response, sizeof(response));
#ifdef __DEBUG__
// Tell the user about the request and its result.
printf("[***] close(%d) = %d\n", ntohl(request->fd), ntohl(response.result));
#endif
// End function.
return 0;
}
int ps2link_request_read(void *packet) {
struct { int number; short length; int fd, size; } __attribute__((packed)) *request = packet;
struct { int number; short length; int result, size; } __attribute__((packed)) response;
// Build the response packet.
response.number = htonl(0xBABE0132);
response.length = htons(sizeof(response));
response.result = htonl(read(ntohl(request->fd), buffer, ntohl(request->size)));
response.size = response.result;
// Send the response packet and read data.
network_send(request_sock, &response, sizeof(response));
network_send(request_sock, buffer, ntohl(response.result));
#ifdef __DEBUG__
// Tell the user about the request and its result.
printf("[***] read(%d, buffer, %d) = %d\n", ntohl(request->fd), ntohl(request->size), ntohl(response.result));
#endif
// End function.
return 0;
}
int ps2link_request_write(void *packet) {
struct { int number; short length; int fd, size; char data[65536]; } __attribute__((packed)) *request = packet;
struct { int number; short length; int result; } __attribute__((packed)) response;
// Build the response packet.
response.number = htonl(0xBABE0142);
response.length = htons(sizeof(response));
response.result = htonl(write(ntohl(request->fd), request->data, ntohl(request->size)));
// Send the response packet.
network_send(request_sock, &response, sizeof(response));
#ifdef __DEBUG__
// Tell the user about the request and its result.
printf("[***] write(%d, buffer, %d) = %d\n", ntohl(request->fd), ntohl(request->size), ntohl(response.result));
#endif
// End function.
return 0;
}
int ps2link_request_lseek(void *packet) {
struct { int number; short length; int fd, offset, whence; } __attribute__((packed)) *request = packet;
struct { int number; short length; int result; } __attribute__((packed)) response;
// Build the response packet.
response.number = htonl(0xBABE0152);
response.length = htons(sizeof(response));
response.result = htonl(lseek(ntohl(request->fd), ntohl(request->offset), ntohl(request->whence)));
// Send the response packet.
network_send(request_sock, &response, sizeof(response));
#ifdef __DEBUG__
// Tell the user about the request and its result.
printf("[***] lseek(%d, %d, %d) = %d\n", ntohl(request->fd), ntohl(request->offset), ntohl(request->whence), ntohl(response.result));
#endif
// End function.
return 0;
}
int ps2link_request_dopen(void *packet) {
struct { int number; short length; int flags; char pathname[256]; } __attribute__((packed)) *request = packet;
struct { int number; short length; int dd; } __attribute__((packed)) response;
// Fix the requested pathname.
ps2link_fixpathname(request->pathname);
// Build the response packet.
response.number = htonl(0xBABE0162);
response.length = htons(sizeof(response));
response.dd = htonl((int)opendir(request->pathname));
// Send the response packet.
network_send(request_sock, &response, sizeof(response));
#ifdef __DEBUG__
// Tell the user about the request and its result.
printf("[***] dopen(\"host0:%s\") = %d\n", request->pathname, ntohl(response.dd));
#endif
// End function.
return 0;
}
int ps2link_request_dread(void *packet) {
struct { int number; short length; int dd; } __attribute__((packed)) *request = packet;
struct { int number; short length; int result, mode, attr, size; char ctime[8], atime[8], mtime[8]; int hisize; char name[256]; } __attribute__((packed)) response;
struct dirent *direptr; struct stat stats; struct tm loctime;
// Build the response packet.
response.number = htonl(0xBABE0182);
response.length = htons(sizeof(response));
response.result = htonl((int)direptr = readdir((DIR *)ntohl(request->dd)));
// If an entry was returned, add the entry information.
if (direptr != 0) {
// Fetch the stats for the entry.
stat(direptr->d_name, &stats);
// Convert and add the mode.
response.mode = (stats.st_mode & 0x07);
if (S_ISDIR(stats.st_mode)) { response.mode |= 0x20; }
if (S_ISLNK(stats.st_mode)) { response.mode |= 0x08; }
if (S_ISREG(stats.st_mode)) { response.mode |= 0x10; }
response.mode = htonl(response.mode);
// Add the attributes.
response.attr = htonl(0);
// Add the size.
response.size = htonl(stats.st_size);
// Convert and add the creation time.
if (localtime_r(&(stats.st_ctime), &loctime)) {
response.ctime[6] = (char)loctime.tm_year;
response.ctime[5] = (char)loctime.tm_mon + 1;
response.ctime[4] = (char)loctime.tm_mday;
response.ctime[3] = (char)loctime.tm_hour;
response.ctime[2] = (char)loctime.tm_min;
response.ctime[1] = (char)loctime.tm_sec;
}
// Convert and add the access time.
if (localtime_r(&(stats.st_atime), &loctime)) {
response.atime[6] = (char)loctime.tm_year;
response.atime[5] = (char)loctime.tm_mon + 1;
response.atime[4] = (char)loctime.tm_mday;
response.atime[3] = (char)loctime.tm_hour;
response.atime[2] = (char)loctime.tm_min;
response.atime[1] = (char)loctime.tm_sec;
}
// Convert and add the modification time.
if (localtime_r(&(stats.st_mtime), &loctime)) {
response.mtime[6] = (char)loctime.tm_year;
response.mtime[5] = (char)loctime.tm_mon + 1;
response.mtime[4] = (char)loctime.tm_mday;
response.mtime[3] = (char)loctime.tm_hour;
response.mtime[2] = (char)loctime.tm_min;
response.mtime[1] = (char)loctime.tm_sec;
}
// Add the hsize. (what is this?)
response.hisize = htonl(0);
// Add the name.
strncpy(response.name, direptr->d_name, 256);
}
// Send the response packet.
network_send(request_sock, &response, sizeof(response));
#ifdef __DEBUG__
// Tell the user about the request and its result.
printf("[***] dread(%d) = %d\n", ntohl(request->dd), ntohl(response.result));
#endif
// End function.
return 0;
}
int ps2link_request_dclose(void *packet) {
struct { int number; short length; int dd; } __attribute__((packed)) *request = packet;
struct { int number; short length; int result; } __attribute__((packed)) response;
// Build the response packet.
response.number = htonl(0xBABE0172);
response.length = htons(sizeof(response));
response.result = htonl(closedir((DIR *)ntohl(request->dd)));
// Send the response packet.
network_send(request_sock, &response, sizeof(response));
#ifdef __DEBUG__
// Tell the user about the request and its result.
printf("[***] dclose(%d) = %d\n", ntohl(request->dd), ntohl(response.result));
#endif
// End function.
return 0;
}
///////////////////////////////
// PS2LINK TEXTLOG FUNCTIONS //
///////////////////////////////
int ps2link_read_textlog(void *packet, int size) {
// Read the textlog packet.
return network_recvfrom(textlog_sock, packet, size);
}