-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathhandlers.c
executable file
·1216 lines (1073 loc) · 34.3 KB
/
handlers.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
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
/* Copyright (c) 2012 The Chromium Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file. */
#include "handlers.h"
#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include "include/nacl_io/osdirent.h"
#include "include/nacl_io/osinttypes.h"
#include "nacl_io_demo.h"
#define MAX_OPEN_FILES 10
#define MAX_OPEN_DIRS 10
#define MAX_PARAMS 4
#if defined(WIN32)
#define stat _stat
#endif
#ifdef __PNACL
void PostMessage(const char* format, ...);
#define printf PostMessage
#endif
/**
* A mapping from int -> FILE*, so the JavaScript messages can refer to an open
* File.
*/
static FILE* g_OpenFiles[MAX_OPEN_FILES];
/**
* A mapping from int -> DIR*, so the JavaScript messages can refer to an open
* Directory.
*/
static void* g_OpenDirs[MAX_OPEN_DIRS];
/**
* A collection of the most recently allocated parameter strings. This makes
* the Handle* functions below easier to write because they don't have to
* manually deallocate the strings they're using.
*/
static char* g_ParamStrings[MAX_PARAMS];
/**
* Add |object| to |map| and return the index it was added at.
* @param[in] map The map to add the object to.
* @param[in] max_map_size The maximum map size.
* @param[in] object The object to add to the map.
* @return int The index of the added object, or -1 if there is no more space.
*/
static int AddToMap(void** map, int max_map_size, void* object) {
int i;
assert(object != NULL);
for (i = 0; i < max_map_size; ++i) {
if (map[i] == NULL) {
map[i] = object;
return i;
}
}
return -1;
}
/**
* Remove an object at index |i| from |map|.
* @param[in] map The map to remove from.
* @param[in] max_map_size The size of the map.
* @param[in] i The index to remove.
*/
static void RemoveFromMap(void** map, int max_map_size, int i) {
assert(i >= 0 && i < max_map_size);
map[i] = NULL;
}
/**
* Add the file to the g_OpenFiles map.
* @param[in] file The file to add to g_OpenFiles.
* @return int The index of the FILE in g_OpenFiles, or -1 if there are too many
* open files.
*/
static int AddFileToMap(FILE* file) {
return AddToMap((void**)g_OpenFiles, MAX_OPEN_FILES, file);
}
/**
* Remove the file from the g_OpenFiles map.
* @param[in] i The index of the file handle to remove.
*/
static void RemoveFileFromMap(int i) {
RemoveFromMap((void**)g_OpenFiles, MAX_OPEN_FILES, i);
}
/* Win32 doesn't support DIR/opendir/readdir/closedir. */
#if !defined(WIN32)
/**
* Add the dir to the g_OpenDirs map.
* @param[in] dir The dir to add to g_OpenDirs.
* @return int The index of the DIR in g_OpenDirs, or -1 if there are too many
* open dirs.
*/
static int AddDirToMap(DIR* dir) {
return AddToMap((void**)g_OpenDirs, MAX_OPEN_DIRS, dir);
}
/**
* Remove the dir from the g_OpenDirs map.
* @param[in] i The index of the dir handle to remove.
*/
static void RemoveDirFromMap(int i) {
RemoveFromMap((void**)g_OpenDirs, MAX_OPEN_DIRS, i);
}
#endif
/**
* Get the number of parameters.
* @param[in] params The parameter array.
* @return uint32_t The number of parameters in the array.
*/
static uint32_t GetNumParams(struct PP_Var params) {
return g_ppb_var_array->GetLength(params);
}
/**
* Get a parameter at |index| as a string.
* @param[in] params The parameter array.
* @param[in] index The index in |params| to get.
* @param[out] out_string The output string.
* @param[out] out_string_len The length of the output string.
* @param[out] out_error An error message, if this operation failed.
* @return int 0 if successful, otherwise 1.
*/
static int GetParamString(struct PP_Var params,
uint32_t index,
char** out_string,
uint32_t* out_string_len,
const char** out_error) {
if (index >= MAX_PARAMS) {
*out_error = PrintfToNewString("Param index %u >= MAX_PARAMS (%d)",
index, MAX_PARAMS);
return 1;
}
struct PP_Var value = g_ppb_var_array->Get(params, index);
if (value.type != PP_VARTYPE_STRING) {
*out_error =
PrintfToNewString("Expected param at index %d to be a string not.%d", index,value.type);
return 1;
}
uint32_t length;
const char* var_str = g_ppb_var->VarToUtf8(value, &length);
char* string = (char*)malloc(length + 1);
memcpy(string, var_str, length);
string[length] = 0;
/* Put the allocated string in g_ParamStrings. This keeps us from leaking
* each parameter string, without having to do manual cleanup in every
* Handle* function below.
*/
free(g_ParamStrings[index]);
g_ParamStrings[index] = string;
*out_string = string;
*out_string_len = length;
return 0;
}
/**
* Get a parameter at |index| as a FILE*.
* @param[in] params The parameter array.
* @param[in] index The index in |params| to get.
* @param[out] out_file The output FILE*.
* @param[out] out_file_index The index of the output FILE* in g_OpenFiles.
* @param[out] out_error An error message, if this operation failed.
* @return int 0 if successful, otherwise 1.
*/
static int GetParamFile(struct PP_Var params,
uint32_t index,
FILE** out_file,
int32_t* out_file_index,
const char** out_error) {
struct PP_Var value = g_ppb_var_array->Get(params, index);
if (value.type != PP_VARTYPE_INT32) {
*out_error =
PrintfToNewString("Expected param at index %d to be an int32", index);
return 1;
}
int32_t file_index = value.value.as_int;
if (file_index < 0 || file_index >= MAX_OPEN_FILES) {
*out_error = PrintfToNewString("File index %d is out range", file_index);
return 1;
}
if (g_OpenFiles[file_index] == NULL) {
*out_error = PrintfToNewString("File index %d is not open", file_index);
return 1;
}
*out_file = g_OpenFiles[file_index];
*out_file_index = file_index;
return 0;
}
/**
* Get a parameter at |index| as a DIR*.
* @param[in] params The parameter array.
* @param[in] index The index in |params| to get.
* @param[out] out_file The output DIR*.
* @param[out] out_file_index The index of the output DIR* in g_OpenDirs.
* @param[out] out_error An error message, if this operation failed.
* @return int 0 if successful, otherwise 1.
*/
static int GetParamDir(struct PP_Var params,
uint32_t index,
DIR** out_dir,
int32_t* out_dir_index,
const char** out_error) {
struct PP_Var value = g_ppb_var_array->Get(params, index);
if (value.type != PP_VARTYPE_INT32) {
*out_error =
PrintfToNewString("Expected param at index %d to be an int32", index);
return 1;
}
int32_t dir_index = value.value.as_int;
if (dir_index < 0 || dir_index >= MAX_OPEN_DIRS) {
*out_error = PrintfToNewString("Dir at index %d is out range", dir_index);
return 1;
}
if (g_OpenDirs[dir_index] == NULL) {
*out_error = PrintfToNewString("Dir index %d is not open", dir_index);
return 1;
}
*out_dir = g_OpenDirs[dir_index];
*out_dir_index = dir_index;
return 0;
}
/**
* Get a parameter at |index| as an int.
* @param[in] params The parameter array.
* @param[in] index The index in |params| to get.
* @param[out] out_file The output int32_t.
* @param[out] out_error An error message, if this operation failed.
* @return int 0 if successful, otherwise 1.
*/
static int GetParamInt(struct PP_Var params,
uint32_t index,
int32_t* out_int,
const char** out_error) {
struct PP_Var value = g_ppb_var_array->Get(params, index);
if (value.type != PP_VARTYPE_INT32) {
*out_error =
PrintfToNewString("Expected param at index %d to be an int32", index);
return 1;
}
*out_int = value.value.as_int;
return 0;
}
/**
* Create a response PP_Var to send back to JavaScript.
* @param[out] response_var The response PP_Var.
* @param[in] cmd The name of the function that is being executed.
* @param[out] out_error An error message, if this call failed.
*/
static void CreateResponse(struct PP_Var* response_var,
const char* cmd,
const char** out_error) {
PP_Bool result;
struct PP_Var dict_var = g_ppb_var_dictionary->Create();
struct PP_Var cmd_key = CStrToVar("cmd");
struct PP_Var cmd_value = CStrToVar(cmd);
result = g_ppb_var_dictionary->Set(dict_var, cmd_key, cmd_value);
g_ppb_var->Release(cmd_key);
g_ppb_var->Release(cmd_value);
if (!result) {
g_ppb_var->Release(dict_var);
*out_error =
PrintfToNewString("Unable to set \"cmd\" key in result dictionary");
return;
}
struct PP_Var args_key = CStrToVar("args");
struct PP_Var args_value = g_ppb_var_array->Create();
result = g_ppb_var_dictionary->Set(dict_var, args_key, args_value);
g_ppb_var->Release(args_key);
g_ppb_var->Release(args_value);
if (!result) {
g_ppb_var->Release(dict_var);
*out_error =
PrintfToNewString("Unable to set \"args\" key in result dictionary");
return;
}
*response_var = dict_var;
}
/**
* Append a PP_Var to the response dictionary.
* @param[in,out] response_var The response PP_var.
* @param[in] value The value to add to the response args.
* @param[out] out_error An error message, if this call failed.
*/
static void AppendResponseVar(struct PP_Var* response_var,
struct PP_Var value,
const char** out_error) {
struct PP_Var args_value = GetDictVar(*response_var, "args");
uint32_t args_length = g_ppb_var_array->GetLength(args_value);
PP_Bool result = g_ppb_var_array->Set(args_value, args_length, value);
if (!result) {
// Release the dictionary that was there before.
g_ppb_var->Release(*response_var);
// Return an error message instead.
*response_var = PP_MakeUndefined();
*out_error = PrintfToNewString("Unable to append value to result");
return;
}
}
/**
* Append an int to the response dictionary.
* @param[in,out] response_var The response PP_var.
* @param[in] value The value to add to the response args.
* @param[out] out_error An error message, if this call failed.
*/
static void AppendResponseInt(struct PP_Var* response_var,
int32_t value,
const char** out_error) {
AppendResponseVar(response_var, PP_MakeInt32(value), out_error);
}
/**
* Append a string to the response dictionary.
* @param[in,out] response_var The response PP_var.
* @param[in] value The value to add to the response args.
* @param[out] out_error An error message, if this call failed.
*/
static void AppendResponseString(struct PP_Var* response_var,
const char* value,
const char** out_error) {
struct PP_Var value_var = CStrToVar(value);
AppendResponseVar(response_var, value_var, out_error);
g_ppb_var->Release(value_var);
}
#define CHECK_PARAM_COUNT(name, expected) \
if (GetNumParams(params) != expected) { \
*out_error = PrintfToNewString(#name " takes " #expected " parameters." \
" Got %d", GetNumParams(params)); \
return 1; \
}
#define PARAM_STRING(index, var) \
char* var; \
uint32_t var##_len; \
if (GetParamString(params, index, &var, &var##_len, out_error)) { \
return 1; \
}
#define PARAM_FILE(index, var) \
FILE* var; \
int32_t var##_index; \
if (GetParamFile(params, index, &var, &var##_index, out_error)) { \
return 1; \
}
#define PARAM_DIR(index, var) \
DIR* var; \
int32_t var##_index; \
if (GetParamDir(params, index, &var, &var##_index, out_error)) { \
return 1; \
}
#define PARAM_INT(index, var) \
int32_t var; \
if (GetParamInt(params, index, &var, out_error)) { \
return 1; \
}
#define CREATE_RESPONSE(name) CreateResponse(output, #name, out_error)
#define RESPONSE_STRING(var) AppendResponseString(output, var, out_error)
#define RESPONSE_INT(var) AppendResponseInt(output, var, out_error)
/**
* Handle a call to fopen() made by JavaScript.
*
* fopen expects 2 parameters:
* 0: the path of the file to open
* 1: the mode string
* on success, fopen returns a result in |output|:
* 0: "fopen"
* 1: the filename opened
* 2: the file index
* on failure, fopen returns an error string in |out_error|.
*/
int HandleFopen(struct PP_Var params,
struct PP_Var* output,
const char** out_error) {
CHECK_PARAM_COUNT(fopen, 2);
PARAM_STRING(0, filename);
PARAM_STRING(1, mode);
FILE* file = fopen(filename, mode);
if (!file) {
*out_error = PrintfToNewString("fopen returned a NULL FILE*");
return 1;
}
int file_index = AddFileToMap(file);
if (file_index == -1) {
*out_error = PrintfToNewString("Example only allows %d open file handles",
MAX_OPEN_FILES);
return 1;
}
CREATE_RESPONSE(fopen);
RESPONSE_STRING(filename);
RESPONSE_INT(file_index);
return 0;
}
/**
* Handle a call to fwrite() made by JavaScript.
*
* fwrite expects 2 parameters:
* 0: The index of the file (which is mapped to a FILE*)
* 1: A string to write to the file
* on success, fwrite returns a result in |output|:
* 0: "fwrite"
* 1: the file index
* 2: the number of bytes written
* on failure, fwrite returns an error string in |out_error|.
*/
int HandleFwrite(struct PP_Var params,
struct PP_Var* output,
const char** out_error) {
CHECK_PARAM_COUNT(fwrite, 2);
PARAM_FILE(0, file);
PARAM_STRING(1, data);
size_t bytes_written = fwrite(data, 1, data_len, file);
if (ferror(file)) {
*out_error = PrintfToNewString(
"Wrote %" PRIuS " bytes, but ferror() returns true", bytes_written);
return 1;
}
CREATE_RESPONSE(fwrite);
RESPONSE_INT(file_index);
RESPONSE_INT((int32_t)bytes_written);
return 0;
}
/**
* Handle a call to fread() made by JavaScript.
*
* fread expects 2 parameters:
* 0: The index of the file (which is mapped to a FILE*)
* 1: The number of bytes to read from the file.
* on success, fread returns a result in |output|:
* 0: "fread"
* 1: the file index
* 2: the data read from the file
* on failure, fread returns an error string in |out_error|.
*/
int HandleFread(struct PP_Var params,
struct PP_Var* output,
const char** out_error) {
CHECK_PARAM_COUNT(fread, 2);
PARAM_FILE(0, file);
PARAM_INT(1, data_len);
char* buffer = (char*)malloc(data_len + 1);
size_t bytes_read = fread(buffer, 1, data_len, file);
buffer[bytes_read] = 0;
if (ferror(file)) {
*out_error = PrintfToNewString(
"Read %" PRIuS " bytes, but ferror() returns true", bytes_read);
free(buffer);
return 1;
}
CREATE_RESPONSE(fread);
RESPONSE_INT(file_index);
RESPONSE_STRING(buffer);
free(buffer);
return 0;
}
/**
* Handle a call to fseek() made by JavaScript.
*
* fseek expects 3 parameters:
* 0: The index of the file (which is mapped to a FILE*)
* 1: The offset to seek to
* 2: An integer representing the whence parameter of standard fseek.
* whence = 0: seek from the beginning of the file
* whence = 1: seek from the current file position
* whence = 2: seek from the end of the file
* on success, fseek returns a result in |output|:
* 0: "fseek"
* 1: the file index
* 2: The new file position
* on failure, fseek returns an error string in |out_error|.
*/
int HandleFseek(struct PP_Var params,
struct PP_Var* output,
const char** out_error) {
CHECK_PARAM_COUNT(fseek, 3);
PARAM_FILE(0, file);
PARAM_INT(1, offset);
PARAM_INT(2, whence);
int result = fseek(file, offset, whence);
if (result) {
*out_error = PrintfToNewString("fseek returned error %d", result);
return 1;
}
offset = (int32_t)ftell(file);
if (offset < 0) {
*out_error = PrintfToNewString(
"fseek succeeded, but ftell returned error %d", offset);
return 1;
}
CREATE_RESPONSE(fseek);
RESPONSE_INT(file_index);
RESPONSE_INT(offset);
return 0;
}
/**
* Handle a call to fflush() made by JavaScript.
*
* fflush expects 1 parameters:
* 0: The index of the file (which is mapped to a FILE*)
* on success, fflush returns a result in |output|:
* 0: "fflush"
* 1: the file index
* on failure, fflush returns an error string in |out_error|.
*/
int HandleFflush(struct PP_Var params,
struct PP_Var* output,
const char** out_error) {
CHECK_PARAM_COUNT(fflush, 1);
PARAM_FILE(0, file);
fflush(file);
CREATE_RESPONSE(fflush);
RESPONSE_INT(file_index);
return 0;
}
/**
* Handle a call to fclose() made by JavaScript.
*
* fclose expects 1 parameter:
* 0: The index of the file (which is mapped to a FILE*)
* on success, fclose returns a result in |output|:
* 0: "fclose"
* 1: the file index
* on failure, fclose returns an error string in |out_error|.
*/
int HandleFclose(struct PP_Var params,
struct PP_Var* output,
const char** out_error) {
CHECK_PARAM_COUNT(fclose, 1);
PARAM_FILE(0, file);
int result = fclose(file);
if (result) {
*out_error = PrintfToNewString("fclose returned error %d", result);
return 1;
}
RemoveFileFromMap(file_index);
CREATE_RESPONSE(fclose);
RESPONSE_INT(file_index);
return 0;
}
/**
* Handle a call to stat() made by JavaScript.
*
* stat expects 1 parameter:
* 0: The name of the file
* on success, stat returns a result in |output|:
* 0: "stat"
* 1: the file name
* 2: the size of the file
* on failure, stat returns an error string in |out_error|.
*/
int HandleStat(struct PP_Var params,
struct PP_Var* output,
const char** out_error) {
CHECK_PARAM_COUNT(stat, 1);
PARAM_STRING(0, filename);
struct stat buf;
memset(&buf, 0, sizeof(buf));
int result = stat(filename, &buf);
if (result == -1) {
*out_error = PrintfToNewString("stat returned error %d", errno);
return 1;
}
CREATE_RESPONSE(stat);
RESPONSE_STRING(filename);
RESPONSE_INT((int32_t)buf.st_size);
return 0;
}
/**
* Handle a call to opendir() made by JavaScript.
*
* opendir expects 1 parameter:
* 0: The name of the directory
* on success, opendir returns a result in |output|:
* 0: "opendir"
* 1: the directory name
* 2: the index of the directory
* on failure, opendir returns an error string in |out_error|.
*/
int HandleOpendir(struct PP_Var params,
struct PP_Var* output,
const char** out_error) {
#if defined(WIN32)
*out_error = PrintfToNewString("Win32 does not support opendir");
return 1;
#else
CHECK_PARAM_COUNT(opendir, 1);
PARAM_STRING(0, dirname);
DIR* dir = opendir(dirname);
if (!dir) {
*out_error = PrintfToNewString("opendir returned a NULL DIR*");
return 1;
}
int dir_index = AddDirToMap(dir);
if (dir_index == -1) {
*out_error = PrintfToNewString("Example only allows %d open dir handles",
MAX_OPEN_DIRS);
return 1;
}
CREATE_RESPONSE(opendir);
RESPONSE_STRING(dirname);
RESPONSE_INT(dir_index);
return 0;
#endif
}
/**
* Handle a call to readdir() made by JavaScript.
*
* readdir expects 1 parameter:
* 0: The index of the directory (which is mapped to a DIR*)
* on success, opendir returns a result in |output|:
* 0: "readdir"
* 1: the inode number of the entry
* 2: the name of the entry
* if there are no more entries, |output| contains:
* 0: "readdir"
* on failure, readdir returns an error string in |out_error|.
*/
int HandleReaddir(struct PP_Var params,
struct PP_Var* output,
const char** out_error) {
#if defined(WIN32)
*out_error = PrintfToNewString("Win32 does not support readdir");
return 1;
#else
CHECK_PARAM_COUNT(readdir, 1);
PARAM_DIR(0, dir);
struct dirent* entry = readdir(dir);
CREATE_RESPONSE(readdir);
RESPONSE_INT(dir_index);
if (entry != NULL) {
RESPONSE_INT((int32_t)entry->d_ino);
RESPONSE_STRING(entry->d_name);
}
return 0;
#endif
}
/**
* Handle a call to closedir() made by JavaScript.
*
* closedir expects 1 parameter:
* 0: The index of the directory (which is mapped to a DIR*)
* on success, closedir returns a result in |output|:
* 0: "closedir"
* 1: the name of the directory
* on failure, closedir returns an error string in |out_error|.
*/
int HandleClosedir(struct PP_Var params,
struct PP_Var* output,
const char** out_error) {
#if defined(WIN32)
*out_error = PrintfToNewString("Win32 does not support closedir");
return 1;
#else
CHECK_PARAM_COUNT(closedir, 1);
PARAM_DIR(0, dir);
int result = closedir(dir);
if (result) {
*out_error = PrintfToNewString("closedir returned error %d", result);
return 1;
}
RemoveDirFromMap(dir_index);
CREATE_RESPONSE(closedir);
RESPONSE_INT(dir_index);
return 0;
#endif
}
/**
* Handle a call to mkdir() made by JavaScript.
*
* mkdir expects 1 parameter:
* 0: The name of the directory
* 1: The mode to use for the new directory, in octal.
* on success, mkdir returns a result in |output|:
* 0: "mkdir"
* 1: the name of the directory
* on failure, mkdir returns an error string in |out_error|.
*/
int HandleMkdir(struct PP_Var params,
struct PP_Var* output,
const char** out_error) {
CHECK_PARAM_COUNT(mkdir, 2);
PARAM_STRING(0, dirname);
PARAM_INT(1, mode);
int result = mkdir(dirname, mode);
if (result != 0) {
*out_error = PrintfToNewString("mkdir returned error: %d", errno);
return 1;
}
CREATE_RESPONSE(mkdir);
RESPONSE_STRING(dirname);
return 0;
}
/**
* Handle a call to rmdir() made by JavaScript.
*
* rmdir expects 1 parameter:
* 0: The name of the directory to remove
* on success, rmdir returns a result in |output|:
* 0: "rmdir"
* 1: the name of the directory
* on failure, rmdir returns an error string in |out_error|.
*/
int HandleRmdir(struct PP_Var params,
struct PP_Var* output,
const char** out_error) {
CHECK_PARAM_COUNT(rmdir, 1);
PARAM_STRING(0, dirname);
int result = rmdir(dirname);
if (result != 0) {
*out_error = PrintfToNewString("rmdir returned error: %d", errno);
return 1;
}
CREATE_RESPONSE(rmdir);
RESPONSE_STRING(dirname);
return 0;
}
/**
* Handle a call to chdir() made by JavaScript.
*
* chdir expects 1 parameter:
* 0: The name of the directory
* on success, chdir returns a result in |output|:
* 0: "chdir"
* 1: the name of the directory
* on failure, chdir returns an error string in |out_error|.
*/
int HandleChdir(struct PP_Var params,
struct PP_Var* output,
const char** out_error) {
CHECK_PARAM_COUNT(chdir, 1);
PARAM_STRING(0, dirname);
int result = chdir(dirname);
if (result != 0) {
*out_error = PrintfToNewString("chdir returned error: %d", errno);
return 1;
}
CREATE_RESPONSE(chdir);
RESPONSE_STRING(dirname);
return 0;
}
/**
* Handle a call to getcwd() made by JavaScript.
*
* getcwd expects 0 parameters.
* on success, getcwd returns a result in |output|:
* 0: "getcwd"
* 1: the current working directory
* on failure, getcwd returns an error string in |out_error|.
*/
int HandleGetcwd(struct PP_Var params,
struct PP_Var* output,
const char** out_error) {
CHECK_PARAM_COUNT(getcwd, 0);
char cwd[PATH_MAX];
char* result = getcwd(cwd, PATH_MAX);
if (result == NULL) {
*out_error = PrintfToNewString("getcwd returned error: %d", errno);
return 1;
}
CREATE_RESPONSE(getcwd);
RESPONSE_STRING(cwd);
return 0;
}
/**
* Handle a call to getaddrinfo() made by JavaScript.
*
* getaddrinfo expects 1 parameter:
* 0: The name of the host to look up.
* on success, getaddrinfo returns a result in |output|:
* 0: "getaddrinfo"
* 1: The canonical name
* 2*n+2: Host name
* 2*n+3: Address type (either "AF_INET" or "AF_INET6")
* on failure, getaddrinfo returns an error string in |out_error|.
*/
int HandleGetaddrinfo(struct PP_Var params,
struct PP_Var* output,
const char** out_error) {
CHECK_PARAM_COUNT(getaddrinfo, 2);
PARAM_STRING(0, name);
PARAM_STRING(1, family);
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_CANONNAME;
if (!strcmp(family, "AF_INET"))
hints.ai_family = AF_INET;
else if (!strcmp(family, "AF_INET6"))
hints.ai_family = AF_INET6;
else if (!strcmp(family, "AF_UNSPEC"))
hints.ai_family = AF_UNSPEC;
else {
*out_error = PrintfToNewString("getaddrinfo uknown family: %s", family);
return 1;
}
struct addrinfo* ai;
int rtn = getaddrinfo(name, NULL, &hints, &ai);
if (rtn != 0) {
*out_error = PrintfToNewString("getaddrinfo failed, error is \"%s\"",
gai_strerror(rtn));
return 2;
}
CREATE_RESPONSE(getaddrinfo);
RESPONSE_STRING(ai->ai_canonname);
struct addrinfo* current = ai;
while (current) {
char addr_str[INET6_ADDRSTRLEN];
if (ai->ai_family == AF_INET6) {
struct sockaddr_in6* in6 = (struct sockaddr_in6*)current->ai_addr;
inet_ntop(
ai->ai_family, &in6->sin6_addr.s6_addr, addr_str, sizeof(addr_str));
} else if (ai->ai_family == AF_INET) {
struct sockaddr_in* in = (struct sockaddr_in*)current->ai_addr;
inet_ntop(ai->ai_family, &in->sin_addr, addr_str, sizeof(addr_str));
}
RESPONSE_STRING(addr_str);
RESPONSE_STRING(ai->ai_family == AF_INET ? "AF_INET" : "AF_INET6");
current = current->ai_next;
}
freeaddrinfo(ai);
return 0;
}
/**
* Handle a call to gethostbyname() made by JavaScript.
*
* gethostbyname expects 1 parameter:
* 0: The name of the host to look up.
* on success, gethostbyname returns a result in |output|:
* 0: "gethostbyname"
* 1: Host name
* 2: Address type (either "AF_INET" or "AF_INET6")
* 3: The first address.
* 4+ The second, third, etc. addresses.
* on failure, gethostbyname returns an error string in |out_error|.
*/
int HandleGethostbyname(struct PP_Var params,
struct PP_Var* output,
const char** out_error) {
CHECK_PARAM_COUNT(gethostbyname, 1);
PARAM_STRING(0, name);
struct hostent* info = gethostbyname(name);
if (!info) {
*out_error = PrintfToNewString("gethostbyname failed, error is \"%s\"",
hstrerror(h_errno));
return 1;
}
CREATE_RESPONSE(gethostbyname);
RESPONSE_STRING(info->h_name);
RESPONSE_STRING(info->h_addrtype == AF_INET ? "AF_INET" : "AF_INET6");
struct in_addr** addr_list = (struct in_addr**)info->h_addr_list;
int i;
for (i = 0; addr_list[i] != NULL; i++) {
if (info->h_addrtype == AF_INET) {
RESPONSE_STRING(inet_ntoa(*addr_list[i]));
} else { // IPv6
char addr_str[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, addr_list[i], addr_str, sizeof(addr_str));
RESPONSE_STRING(addr_str);
}
}
return 0;
}
/**
* Handle a call to connect() made by JavaScript.
*
* connect expects 2 parameters:
* 0: The hostname to connect to.
* 1: The port number to connect to.
* on success, connect returns a result in |output|:
* 0: "connect"
* 1: The socket file descriptor.
* on failure, connect returns an error string in |out_error|.
*/
int HandleConnect(struct PP_Var params,
struct PP_Var* output,
const char** out_error) {
CHECK_PARAM_COUNT(connect, 2);
PARAM_STRING(0, hostname);
PARAM_INT(1, port);
// Lookup host
struct hostent* hostent = gethostbyname(hostname);
if (hostent == NULL) {
*out_error = PrintfToNewString("gethostbyname() returned error: %d", errno);
return 1;