-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.cpp
1558 lines (1291 loc) · 30.5 KB
/
main.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
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
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "common.h"
#include "compat.h"
#include "netiso.h"
#include "File.h"
#include "VIsoFile.h"
#define BUFFER_SIZE (3*1048576)
#define MAX_CLIENTS 5
enum
{
VISO_NONE,
VISO_PS3,
VISO_ISO
};
typedef struct
{
int s;
AbstractFile *ro_file;
AbstractFile *wo_file;
DIR *dir;
char *dirpath;
uint8_t *buf;
int connected;
int restarted;
struct in_addr ip_addr;
thread_t thread;
uint32_t CD_SECTOR_SIZE;
} client_t;
static char root_directory[4096];
static client_t clients[MAX_CLIENTS];
static int initialize_socket(uint16_t port)
{
int s;
struct sockaddr_in addr;
#ifdef WIN32
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2), &wsaData);
#endif
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0)
{
DPRINTF("Socket creation error: %d\n", get_network_error());
return s;
}
int flag = 1;
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&flag, sizeof(flag)) < 0)
{
DPRINTF("Error in setsockopt(REUSEADDR): %d\n", get_network_error());
closesocket(s);
return -1;
}
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY;
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
DPRINTF("Error in bind: %d\n", get_network_error());
return -1;
}
if (listen(s, 1) < 0)
{
DPRINTF("Error in listen: %d\n", get_network_error());
return -1;
}
return s;
}
#ifndef WIN32
static int recv_all(int s, void *buf, int size)
{
return recv(s, buf, size, MSG_WAITALL);
}
#else
// stupid windows...
static int recv_all(int s, void *buf, int size)
{
int total_read = 0;
char *buf_b = (char *)buf;
while (size > 0)
{
int r = recv(s, buf_b, size, 0);
if (r <= 0)
return r;
total_read += r;
buf_b += r;
size -= r;
}
return total_read;
}
#endif
static int initialize_client(client_t *client)
{
memset(client, 0, sizeof(client_t));
client->buf = (uint8_t *)malloc(BUFFER_SIZE);
if (!client->buf)
{
DPRINTF("Memory allocation error!\n");
return -1;
}
client->ro_file = NULL;
client->wo_file = NULL;
client->dir = NULL;
client->dirpath = NULL;
client->connected = 1;
client->CD_SECTOR_SIZE = 2352;
return 0;
}
static void finalize_client(client_t *client)
{
shutdown(client->s, SHUT_RDWR);
closesocket(client->s);
if (client->ro_file)
{
delete client->ro_file;
client->ro_file = NULL;
}
if (client->wo_file)
{
delete client->wo_file;
client->wo_file = NULL;
}
if (client->dir)
{
closedir(client->dir);
}
if (client->dirpath)
{
free(client->dirpath);
}
free(client->buf);
memset(client, 0, sizeof(client_t));
}
static char *translate_path(char *path, int del, int always_del, int *viso)
{
char *p;
if (path[0] != '/')
{
DPRINTF("path must start by '/'. Path received: %s\n", path);
if (del && always_del)
{
free(path);
}
return NULL;
}
p = path;
while ((p = strstr(p, "..")))
{
if (strlen(p) >= 2)
{
if (*(p-1) == '/' || *(p-1) == '\\')
{
if (p[2] == 0 || p[2] == '/' || p[2] == '\\')
{
DPRINTF("The path \"%s\" is unsecure!\n", path);
if (del && always_del)
{
free(path);
}
return NULL;
}
}
}
p += 2;
}
p = (char *)malloc(strlen(root_directory)+strlen(path)+1);
if (!p)
{
printf("Memory allocation error\n");
exit(-1);
}
sprintf(p, "%s%s", root_directory, path);
if (del)
{
free(path);
}
if (viso)
{
char *q = p + strlen(root_directory);
if (strstr(q, "/***PS3***/") == q)
{
memmove(q, q+10, strlen(q+10)+1);
DPRINTF("p -> %s\n", p);
*viso = VISO_PS3;
}
else if (strstr(q, "/***DVD***/") == q)
{
memmove(q, q+10, strlen(q+10)+1);
DPRINTF("p -> %s\n", p);
*viso = VISO_ISO;
}
else
{
*viso = 0;
}
}
return p;
}
static int64_t calculate_directory_size(char *path)
{
int64_t result = 0;
DIR *d;
struct dirent *entry;
//DPRINTF("Calculate %s\n", path);
d = opendir(path);
if (!d)
return -1;
uint16_t name_len, path_len;
path_len = strlen(path);
while ((entry = readdir(d)))
{
if(entry->d_name[0] == '.' && (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)) continue;
name_len = strlen(entry->d_name);
if (name_len == 0) continue;
//DPRINTF("name: %s\n", entry->d_name);
file_stat_t st;
char newpath[path_len + name_len + 2];
sprintf(newpath, "%s/%s", path, entry->d_name);
if (stat_file(newpath, &st) < 0)
{
DPRINTF("calculate_directory_size: stat failed on %s\n", newpath);
result = -1;
break;
}
if ((st.mode & S_IFDIR) == S_IFDIR)
{
int64_t temp = calculate_directory_size(newpath);
if (temp < 0)
{
result = temp;
break;
}
result += temp;
}
else if ((st.mode & S_IFREG) == S_IFREG)
{
result += st.file_size;
}
}
closedir(d);
return result;
}
// NOTE: All process_XXX function return an error ONLY if connection must be aborted. If only a not critical error must be returned to the client, that error will be
// sent using network, but the function must return 0
static int process_open_cmd(client_t *client, netiso_open_cmd *cmd)
{
file_stat_t st;
netiso_open_result result;
char *filepath;
uint16_t fp_len;
int ret, viso;
int error = 0;
client->CD_SECTOR_SIZE = 2352;
fp_len = BE16(cmd->fp_len);
//DPRINTF("fp_len = %d\n", fp_len);
filepath = (char *)malloc(fp_len+1);
if (!filepath)
{
DPRINTF("CRITICAL: memory allocation error\n");
return -1;
}
filepath[fp_len] = 0;
ret = recv_all(client->s, (void *)filepath, fp_len);
if (ret != fp_len)
{
DPRINTF("recv failed, getting filename for open: %d %d\n", ret, get_network_error());
free(filepath);
return -1;
}
filepath = translate_path(filepath, 1, 1, &viso);
if (!filepath)
{
DPRINTF("Path cannot be translated. Connection with this client will be aboreted.\n");
return -1;
}
DPRINTF("open %s\n", filepath);
if (client->ro_file)
{
delete client->ro_file;
}
if (viso == VISO_NONE)
{
client->ro_file = new File();
}
else
{
client->ro_file = new VIsoFile((viso == VISO_PS3));
}
if (client->ro_file->open(filepath, O_RDONLY) < 0)
{
DPRINTF("open error on \"%s\" (viso=%d)\n", filepath, viso);
error = 1;
delete client->ro_file;
client->ro_file = NULL;
}
else
{
if (client->ro_file->fstat(&st) < 0)
{
DPRINTF("Error in fstat\n");
error = 1;
}
else
{
result.file_size = BE64(st.file_size);
result.mtime = BE64(st.mtime);
// detect sector size
char buffer[0x10]; buffer[0xD] = 0;
client->ro_file->seek(0x9320, SEEK_SET); client->ro_file->read(buffer, 0xC); if(memcmp(buffer, "PLAYSTATION ", 0xC)==0) client->CD_SECTOR_SIZE = 2352; else {
client->ro_file->seek(0x8020, SEEK_SET); client->ro_file->read(buffer, 0xC); if(memcmp(buffer, "PLAYSTATION ", 0xC)==0) client->CD_SECTOR_SIZE = 2048; else {
client->ro_file->seek(0x9920, SEEK_SET); client->ro_file->read(buffer, 0xC); if(memcmp(buffer, "PLAYSTATION ", 0xC)==0) client->CD_SECTOR_SIZE = 2448; else {
client->ro_file->seek(0x9220, SEEK_SET); client->ro_file->read(buffer, 0xC); if(memcmp(buffer, "PLAYSTATION ", 0xC)==0) client->CD_SECTOR_SIZE = 2336; }}}
}
}
#ifdef WIN32
DPRINTF("File size: %I64x\n", st.file_size);
#else
DPRINTF("File size: %llx\n", (long long unsigned int)st.file_size);
#endif
if (error)
{
result.file_size = BE64(-1);
result.mtime = BE64(0);
}
free(filepath);
ret = send(client->s, (char *)&result, sizeof(result), 0);
if (ret != sizeof(result))
{
DPRINTF("open, send result error: %d %d\n", ret, get_network_error());
return -1;
}
return 0;
}
static int process_read_file_critical(client_t *client, netiso_read_file_critical_cmd *cmd)
{
uint64_t offset;
uint32_t remaining;
offset = BE64(cmd->offset);
remaining = BE32(cmd->num_bytes);
if (!client->ro_file)
return -1;
#ifdef WIN32
DPRINTF("Read %I64x %x\n", (long long unsigned int)offset, remaining);
#else
DPRINTF("Read %llx %x\n", (long long unsigned int)offset, remaining);
#endif
if (client->ro_file->seek(offset, SEEK_SET) < 0)
{
DPRINTF("seek_file failed!\n");
return -1;
}
while (remaining > 0)
{
uint32_t read_size;
if (remaining < BUFFER_SIZE)
{
read_size = remaining;
}
else
{
read_size = BUFFER_SIZE;
}
if (client->ro_file->read(client->buf, read_size) != read_size)
{
DPRINTF("read_file failed on read file critical command!\n");
return -1;
}
if (send(client->s, (char *)client->buf, read_size, 0) != read_size)
{
DPRINTF("send failed on read file critical command!\n");
return -1;
}
remaining -= read_size;
}
return 0;
}
static int process_read_cd_2048_critical_cmd(client_t *client, netiso_read_cd_2048_critical_cmd *cmd)
{
uint64_t offset;
uint32_t sector_count;
uint8_t *buf;
offset = BE32(cmd->start_sector)*(client->CD_SECTOR_SIZE);
sector_count = BE32(cmd->sector_count);
DPRINTF("Read CD 2048 %x %x\n", BE32(cmd->start_sector), sector_count);
if (!client->ro_file)
return -1;
if ((sector_count*2048) > BUFFER_SIZE)
{
// This is just to save some uneeded code. PS3 will never request such a high number of sectors
DPRINTF("This situation wasn't expected, too many sectors read!\n");
return -1;
}
buf = client->buf;
for (uint32_t i = 0; i < sector_count; i++)
{
client->ro_file->seek(offset+24, SEEK_SET);
if (client->ro_file->read(buf, 2048) != 2048)
{
DPRINTF("read_file failed on read cd 2048 critical command!\n");
return -1;
}
buf += 2048;
offset += client->CD_SECTOR_SIZE;
}
if (send(client->s, (char *)client->buf, sector_count*2048, 0) != (sector_count*2048))
{
DPRINTF("send failed on read cd 2048 critical command!\n");
return -1;
}
return 0;
}
static int process_read_file_cmd(client_t *client, netiso_read_file_cmd *cmd)
{
uint64_t offset;
uint32_t remaining;
int32_t bytes_read;
netiso_read_file_result result;
offset = BE64(cmd->offset);
remaining = BE32(cmd->num_bytes);
if (!client->ro_file)
{
bytes_read = -1;
goto send_result_read_file;
}
if (remaining > BUFFER_SIZE)
{
bytes_read = -1;
goto send_result_read_file;
}
if (client->ro_file->seek(offset, SEEK_SET) < 0)
{
bytes_read = -1;
goto send_result_read_file;
}
bytes_read = client->ro_file->read(client->buf, remaining);
if (bytes_read < 0)
{
bytes_read = -1;
}
send_result_read_file:
result.bytes_read = (int32_t)BE32(bytes_read);
if (send(client->s, (char *)&result, sizeof(result), 0) != 4)
{
DPRINTF("send failed on send result (read file)\n");
return -1;
}
if (bytes_read > 0 && send(client->s, (char *)client->buf, bytes_read, 0) != bytes_read)
{
DPRINTF("send failed on read file!\n");
return -1;
}
return 0;
}
static int process_create_cmd(client_t *client, netiso_create_cmd *cmd)
{
netiso_create_result result;
char *filepath;
uint16_t fp_len;
int ret;
fp_len = BE16(cmd->fp_len);
//DPRINTF("fp_len = %d\n", fp_len);
filepath = (char *)malloc(fp_len+1);
if (!filepath)
{
DPRINTF("CRITICAL: memory allocation error\n");
return -1;
}
filepath[fp_len] = 0;
ret = recv_all(client->s, (void *)filepath, fp_len);
if (ret != fp_len)
{
DPRINTF("recv failed, getting filename for create: %d %d\n", ret, get_network_error());
free(filepath);
return -1;
}
filepath = translate_path(filepath, 1, 1, NULL);
if (!filepath)
{
DPRINTF("Path cannot be translated. Connection with this client will be aborted.\n");
return -1;
}
DPRINTF("create %s\n", filepath);
if (client->wo_file)
{
delete client->wo_file;
}
client->wo_file = new File();
if (client->wo_file->open(filepath, O_WRONLY|O_CREAT|O_TRUNC) < 0)
{
DPRINTF("create error on \"%s\"\n", filepath);
result.create_result = BE32(-1);
delete client->wo_file;
client->wo_file = NULL;
}
else
{
result.create_result = BE32(0);
}
free(filepath);
ret = send(client->s, (char *)&result, sizeof(result), 0);
if (ret != sizeof(result))
{
DPRINTF("create, send result error: %d %d\n", ret, get_network_error());
return -1;
}
return 0;
}
static int process_write_file_cmd(client_t *client, netiso_write_file_cmd *cmd)
{
uint32_t remaining;
int32_t bytes_written;
netiso_write_file_result result;
remaining = BE32(cmd->num_bytes);
if (!client->wo_file)
{
bytes_written = -1;
goto send_result_write_file;
}
if (remaining > BUFFER_SIZE)
{
return -1;
}
//DPRINTF("Remaining: %d\n", remaining);
if (remaining > 0)
{
int ret = recv_all(client->s, (void *)client->buf, remaining);
if (ret != remaining)
{
DPRINTF("recv failed on write file: %d %d\n", ret, get_network_error());
return -1;
}
}
bytes_written = client->wo_file->write(client->buf, remaining);
if (bytes_written < 0)
{
bytes_written = -1;
}
send_result_write_file:
result.bytes_written = (int32_t)BE32(bytes_written);
if (send(client->s, (char *)&result, sizeof(result), 0) != 4)
{
DPRINTF("send failed on send result (read file)\n");
return -1;
}
return 0;
}
static int process_open_dir_cmd(client_t *client, netiso_open_dir_cmd *cmd)
{
netiso_open_dir_result result;
char *dirpath;
uint16_t dp_len;
int ret;
dp_len = BE16(cmd->dp_len);
//DPRINTF("fp_len = %d\n", fp_len);
dirpath = (char *)malloc(dp_len+1);
if (!dirpath)
{
DPRINTF("CRITICAL: memory allocation error\n");
return -1;
}
dirpath[dp_len] = 0;
ret = recv_all(client->s, (void *)dirpath, dp_len);
if (ret != dp_len)
{
DPRINTF("recv failed, getting dirname for open dir: %d %d\n", ret, get_network_error());
free(dirpath);
return -1;
}
dirpath = translate_path(dirpath, 1, 1, NULL);
if (!dirpath)
{
DPRINTF("Path cannot be translated. Connection with this client will be aborted.\n");
return -1;
}
DPRINTF("open dir %s\n", dirpath);
if (client->dir)
{
closedir(client->dir);
client->dir = NULL;
}
if (client->dirpath)
{
free(client->dirpath);
}
client->dirpath = NULL;
client->dir = opendir(dirpath);
if (!client->dir)
{
DPRINTF("open dir error on \"%s\"\n", dirpath);
result.open_result = BE32(-1);
}
else
{
client->dirpath = dirpath;
result.open_result = BE32(0);
}
if (!client->dirpath)
free(dirpath);
ret = send(client->s, (char *)&result, sizeof(result), 0);
if (ret != sizeof(result))
{
DPRINTF("open dir, send result error: %d %d\n", ret, get_network_error());
return -1;
}
return 0;
}
static int process_read_dir_entry_cmd(client_t *client, netiso_read_dir_entry_cmd *cmd, int version)
{
netiso_read_dir_entry_result result_v1;
netiso_read_dir_entry_result_v2 result_v2;
file_stat_t st;
struct dirent *entry;
char *path;
if (version == 1)
{
memset(&result_v1, 0, sizeof(result_v1));
}
else
{
memset(&result_v2, 0, sizeof(result_v2));
}
if (!client->dir || !client->dirpath)
{
if (version == 1)
{
result_v1.file_size = BE64(-1);
}
else
{
result_v2.file_size = BE64(-1);
}
goto send_result_read_dir;
}
while ((entry = readdir(client->dir)))
{
if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0 && strlen(entry->d_name) <= 65535)
break;
}
if (!entry)
{
closedir(client->dir);
free(client->dirpath);
client->dir = NULL;
client->dirpath = NULL;
if (version == 1)
{
result_v1.file_size = BE64(-1);
}
else
{
result_v2.file_size = BE64(-1);
}
goto send_result_read_dir;
}
path = (char *)malloc(strlen(client->dirpath)+strlen(entry->d_name)+2);
sprintf(path, "%s/%s", client->dirpath, entry->d_name);
DPRINTF("Read dir entry: %s\n", path);
if (stat_file(path, &st) < 0)
{
closedir(client->dir);
free(client->dirpath);
client->dir = NULL;
client->dirpath = NULL;
if (version == 1)
{
result_v1.file_size = BE64(-1);
}
else
{
result_v2.file_size = BE64(-1);
}
DPRINTF("Stat failed on read dir entry: %s\n", path);
free(path);
goto send_result_read_dir;
}
free(path);
if ((st.mode & S_IFDIR) == S_IFDIR)
{
if (version == 1)
{
result_v1.file_size = BE64(0);
result_v1.is_directory = 1;
}
else
{
result_v2.file_size = BE64(0);
result_v2.is_directory = 1;
}
}
else
{
if (version == 1)
{
result_v1.file_size = BE64(st.file_size);
result_v1.is_directory = 0;
}
else
{
result_v2.file_size = BE64(st.file_size);
result_v2.is_directory = 0;
}
}
if (version == 1)
{
result_v1.fn_len = BE16(strlen(entry->d_name));
}
else
{
result_v2.fn_len = BE16(strlen(entry->d_name));
result_v2.atime = BE64(st.atime);
result_v2.ctime = BE64(st.ctime);
result_v2.mtime = BE64(st.mtime);
}
send_result_read_dir:
if (version == 1)
{
if (send(client->s, (char *)&result_v1, sizeof(result_v1), 0) != sizeof(result_v1))
{
DPRINTF("send error on read dir entry (%d)\n", get_network_error());
return -1;
}
}
else
{
if (send(client->s, (char *)&result_v2, sizeof(result_v2), 0) != sizeof(result_v2))
{
DPRINTF("send error on read dir entry (%d)\n", get_network_error());
return -1;
}
}
if ((version == 1 && result_v1.file_size != BE64(-1)) || (version == 2 && result_v2.file_size != BE64(-1)))
{
if (send(client->s, (char *)entry->d_name, strlen(entry->d_name), 0) != strlen(entry->d_name))
{
DPRINTF("send file name error on read dir entry (%d)\n", get_network_error());
return -1;
}
}
return 0;
}
static int process_delete_file_cmd(client_t *client, netiso_delete_file_cmd *cmd)
{
netiso_delete_file_result result;
char *filepath;
uint16_t fp_len;
int ret;
fp_len = BE16(cmd->fp_len);
filepath = (char *)malloc(fp_len+1);
if (!filepath)
{
DPRINTF("CRITICAL: memory allocation error\n");
return -1;
}
filepath[fp_len] = 0;
ret = recv_all(client->s, (void *)filepath, fp_len);
if (ret != fp_len)
{
DPRINTF("recv failed, getting filename for delete file: %d %d\n", ret, get_network_error());
free(filepath);
return -1;
}
filepath = translate_path(filepath, 1, 1, NULL);
if (!filepath)
{
DPRINTF("Path cannot be translated. Connection with this client will be aborted.\n");
return -1;
}
DPRINTF("delete %s\n", filepath);
result.delete_result = BE32(unlink(filepath));
free(filepath);
ret = send(client->s, (char *)&result, sizeof(result), 0);
if (ret != sizeof(result))
{
DPRINTF("delete, send result error: %d %d\n", ret, get_network_error());
return -1;
}
return 0;
}
static int process_mkdir_cmd(client_t *client, netiso_mkdir_cmd *cmd)
{
netiso_mkdir_result result;
char *dirpath;
uint16_t dp_len;
int ret;
dp_len = BE16(cmd->dp_len);
dirpath = (char *)malloc(dp_len+1);
if (!dirpath)
{
DPRINTF("CRITICAL: memory allocation error\n");
return -1;
}
dirpath[dp_len] = 0;
ret = recv_all(client->s, (void *)dirpath, dp_len);
if (ret != dp_len)
{
DPRINTF("recv failed, getting dirname for mkdir: %d %d\n", ret, get_network_error());
free(dirpath);
return -1;
}
dirpath = translate_path(dirpath, 1, 1, NULL);
if (!dirpath)
{
DPRINTF("Path cannot be translated. Connection with this client will be aborted.\n");
return -1;
}
DPRINTF("mkdir %s\n", dirpath);
#ifdef WIN32
result.mkdir_result = BE32(mkdir(dirpath));
#else
result.mkdir_result = BE32(mkdir(dirpath, 0777));
#endif
free(dirpath);
ret = send(client->s, (char *)&result, sizeof(result), 0);
if (ret != sizeof(result))
{
DPRINTF("open dir, send result error: %d %d\n", ret, get_network_error());
return -1;
}
return 0;
}
static int process_rmdir_cmd(client_t *client, netiso_rmdir_cmd *cmd)
{
netiso_rmdir_result result;
char *dirpath;
uint16_t dp_len;
int ret;