forked from tonioni/WinUAE
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bsdsocket.cpp
2015 lines (1742 loc) · 57 KB
/
bsdsocket.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
/*
* UAE - The Un*x Amiga Emulator
*
* bsdsocket.library emulation machine-independent part
*
* Copyright 1997, 1998 Mathias Ortmann
*
* Library initialization code (c) Tauno Taipaleenmaki
*/
#include "sysconfig.h"
#include "sysdeps.h"
#include <assert.h>
#include <stddef.h>
#include "options.h"
#include "memory.h"
#include "custom.h"
#include "newcpu.h"
#include "autoconf.h"
#include "traps.h"
#include "bsdsocket.h"
#include "threaddep/thread.h"
#include "native2amiga.h"
#include "debug.h"
#ifdef BSDSOCKET
#define NEWTRAP 1
int log_bsd = 0;
struct socketbase *socketbases;
static uae_u32 SockLibBase;
#define SOCKPOOLSIZE 128
#define UNIQUE_ID (-1)
/* ObtainSocket()/ReleaseSocket() public socket pool */
struct sockd {
long sockpoolids[SOCKPOOLSIZE];
SOCKET_TYPE sockpoolsocks[SOCKPOOLSIZE];
uae_u32 sockpoolflags[SOCKPOOLSIZE];
};
static long curruniqid = 65536;
static struct sockd *sockdata;
uae_u32 strncpyha(TrapContext *ctx, uae_u32 dst, const uae_char *src, int size)
{
uae_u32 res = dst;
if (trap_is_indirect()) {
trap_put_string(ctx, src, dst, size);
} else {
if (!addr_valid(_T("strncpyha"), dst, size))
return res;
while (size--) {
put_byte (dst++, *src);
if (!*src++)
return res;
}
}
return res;
}
uae_u32 addstr(TrapContext *ctx, uae_u32 * dst, const TCHAR *src)
{
uae_u32 res = *dst;
int len;
char *s = ua(src);
len = uaestrlen(s) + 1;
if (trap_is_indirect()) {
trap_put_bytes(ctx, dst, res, len);
} else {
strcpyha_safe (*dst, s);
}
(*dst) += len;
xfree (s);
return res;
}
uae_u32 addstr_ansi(TrapContext *ctx, uae_u32 * dst, const uae_char *src)
{
uae_u32 res = *dst;
int len;
len = uaestrlen (src) + 1;
if (trap_is_indirect()) {
trap_put_bytes(ctx, dst, res, len);
} else {
strcpyha_safe (*dst, src);
}
(*dst) += len;
return res;
}
uae_u32 addmem(TrapContext *ctx, uae_u32 * dst, const uae_char *src, int len)
{
uae_u32 res = *dst;
if (!src)
return 0;
if (trap_is_indirect()) {
trap_put_bytes(ctx, src, res, len);
} else {
memcpyha_safe (*dst, (uae_u8*)src, len);
}
(*dst) += len;
return res;
}
/* Get current task */
static uae_u32 gettask (TrapContext *ctx)
{
uae_u32 currtask, a1 = trap_get_areg(ctx, 1);
TCHAR *tskname;
trap_call_add_areg(ctx, 1, 0);
currtask = trap_call_lib(ctx, trap_get_long(ctx, 4), -0x126); /* FindTask */
trap_set_areg(ctx, 1, a1);
if (ISBSDTRACE) {
uae_char name[256];
trap_get_string(ctx, name, trap_get_long(ctx, currtask + 10), sizeof name);
tskname = au(name);
BSDTRACE ((_T("[%s] "), tskname));
xfree (tskname);
}
return currtask;
}
/* errno/herrno setting */
void bsdsocklib_seterrno (TrapContext *ctx, SB, int sb_errno)
{
sb->sb_errno = sb_errno;
if (sb->sb_errno >= 1001 && sb->sb_errno <= 1005)
bsdsocklib_setherrno(ctx, sb,sb->sb_errno-1000);
if (sb->errnoptr) {
switch (sb->errnosize) {
case 1:
trap_put_byte(ctx, sb->errnoptr, sb_errno);
break;
case 2:
trap_put_word(ctx, sb->errnoptr, sb_errno);
break;
case 4:
trap_put_long(ctx, sb->errnoptr, sb_errno);
break;
}
}
}
void bsdsocklib_setherrno(TrapContext *ctx, SB, int sb_herrno)
{
sb->sb_herrno = sb_herrno;
if (sb->herrnoptr) {
switch (sb->herrnosize) {
case 1:
trap_put_byte(ctx, sb->herrnoptr, sb_herrno);
break;
case 2:
trap_put_word(ctx, sb->herrnoptr, sb_herrno);
break;
case 4:
trap_put_long(ctx, sb->herrnoptr, sb_herrno);
break;
}
}
}
uae_u32 callfdcallback (TrapContext *ctx, SB, uae_u32 fd, uae_u32 action)
{
uae_u32 v;
if (!sb->fdcallback)
return 0;
BSDTRACE((_T("FD_CALLBACK(%d,%d) "), fd, action));
trap_call_add_dreg(ctx, 0, fd);
trap_call_add_dreg(ctx, 1, action);
v = trap_call_func(ctx, sb->fdcallback);
BSDTRACE((_T("-> %d\n"), v));
return v;
}
bool checksd(TrapContext *ctx, SB, int sd)
{
int iCounter;
SOCKET s;
s = getsock(ctx, sb, sd);
if (s != INVALID_SOCKET) {
for (iCounter = 1; iCounter <= sb->dtablesize; iCounter++) {
if (iCounter != sd) {
if (getsock(ctx, sb, iCounter) == s) {
releasesock(ctx, sb, sd);
return true;
}
}
}
for (iCounter = 0; iCounter < SOCKPOOLSIZE; iCounter++) {
if (s == sockdata->sockpoolsocks[iCounter])
return true;
}
}
BSDTRACE((_T("checksd FALSE s 0x%x sd %d\n"),s,sd));
return false;
}
void setsd(TrapContext *ctx, SB, int sd, SOCKET_TYPE s)
{
callfdcallback(ctx, sb, sd - 1, FDCB_ALLOC);
sb->dtable[sd - 1] = s;
}
/* Socket descriptor/opaque socket handle management */
int getsd (TrapContext *ctx, SB, SOCKET_TYPE s)
{
int i, fdcb;
SOCKET_TYPE *dt = sb->dtable;
/* return socket descriptor if already exists */
for (i = sb->dtablesize; i--;) {
if (dt[i] == s)
return i + 1;
}
/* create new table entry */
fdcb = 0;
for (i = 0; i < sb->dtablesize; i++) {
if (dt[i] == -1) {
if (callfdcallback(ctx, sb, i, FDCB_CHECK)) {
/* fd was allocated by link lib */
dt[i] = -2;
continue;
}
dt[i] = s;
sb->ftable[i] = SF_BLOCKING;
return i + 1;
} else if (dt[i] == -2) {
fdcb = 1;
}
}
/* recheck callback allocated FDs */
if (fdcb) {
for (i = 0; i < sb->dtablesize; i++) {
if (dt[i] == -2) {
if (!callfdcallback(ctx, sb, i, FDCB_CHECK)) {
dt[i] = s;
sb->ftable[i] = SF_BLOCKING;
return i + 1;
}
}
}
}
/* descriptor table full. */
bsdsocklib_seterrno(ctx, sb, 24); /* EMFILE */
return -1;
}
SOCKET_TYPE getsock (TrapContext *ctx, SB, int sd)
{
if ((unsigned int) (sd - 1) >= (unsigned int) sb->dtablesize) {
BSDTRACE ((_T("Invalid Socket Descriptor (%d)\n"), sd));
bsdsocklib_seterrno(ctx, sb, 38); /* ENOTSOCK */
return -1;
}
if (sb->dtable[sd - 1] == INVALID_SOCKET) {
struct socketbase *sb1, *nsb;
uaecptr ot;
uae_char name[256];
if (!trap_valid_address(ctx, sb->ownertask + 10, 4))
return -1;
ot = trap_get_long(ctx, sb->ownertask + 10);
if (!trap_valid_address(ctx, ot, 1))
return -1;
trap_get_string(ctx, name, ot, sizeof name);
// Fix for Newsrog (All Tasks of Newsrog using the same dtable)
for (sb1 = socketbases; sb1; sb1 = nsb) {
uaecptr ot1;
uae_char name1[256];
if (!trap_valid_address(ctx, sb1->ownertask + 10, 4))
break;
ot1 = trap_get_long(ctx, sb1->ownertask + 10);
if (!trap_valid_address(ctx, ot1, 1))
break;
trap_get_string(ctx, name1, ot1, sizeof name1);
if (strcmp(name, name1) == 0) {
// Task with same name already exists -> use same dtable
if (sb1->dtable[sd - 1] != INVALID_SOCKET)
return sb1->dtable[sd - 1];
}
nsb = sb1->next;
}
}
return sb->dtable[sd - 1];
}
void releasesock (TrapContext *ctx, SB, int sd)
{
if ((unsigned int) (sd - 1) < (unsigned int) sb->dtablesize) {
sb->dtable[sd - 1] = -1;
callfdcallback(ctx, sb, sd - 1, FDCB_FREE);
}
}
/* Signal queue */
/* @@@ TODO: ensure proper interlocking */
#if 1
struct socketbase *sbsigqueue;
volatile int bsd_int_requested;
#endif
void addtosigqueue (SB, int events)
{
locksigqueue ();
if (events)
sb->sigstosend |= sb->eventsigs;
else
sb->sigstosend |= ((uae_u32) 1) << sb->signal;
if (!sb->dosignal) {
sb->nextsig = sbsigqueue;
sbsigqueue = sb;
}
sb->dosignal = 1;
bsd_int_requested |= 1;
unlocksigqueue ();
}
void bsdsock_fake_int_handler(void)
{
locksigqueue ();
bsd_int_requested = 0;
if (sbsigqueue != NULL) {
SB;
for (sb = sbsigqueue; sb; sb = sb->nextsig) {
if (sb->dosignal == 1) {
uae_Signal (sb->ownertask, sb->sigstosend);
sb->sigstosend = 0;
}
sb->dosignal = 0;
}
sbsigqueue = NULL;
}
unlocksigqueue ();
}
void waitsig (TrapContext *ctx, SB)
{
long sigs;
trap_call_add_dreg(ctx, 0, (((uae_u32) 1) << sb->signal) | sb->eintrsigs);
if ((sigs = trap_call_lib(ctx, sb->sysbase, -0x13e)) & sb->eintrsigs) { /* Wait */
sockabort (sb);
bsdsocklib_seterrno(ctx, sb, 4); /* EINTR */
// Set signal
trap_call_add_dreg(ctx, 0, sigs);
trap_call_add_dreg(ctx, 1, sb->eintrsigs);
sigs = trap_call_lib(ctx, sb->sysbase, -0x132); /* SetSignal() */
sb->eintr = 1;
} else
sb->eintr = 0;
}
void cancelsig (TrapContext *ctx, SB)
{
locksigqueue ();
if (sb->dosignal)
sb->dosignal = 2;
unlocksigqueue ();
trap_call_add_dreg(ctx, 0, 0);
trap_call_add_dreg(ctx, 1, ((uae_u32) 1) << sb->signal);
trap_call_lib(ctx, sb->sysbase, -0x132); /* SetSignal() */
}
/* Allocate and initialize per-task state structure */
static struct socketbase *alloc_socketbase (TrapContext *ctx)
{
SB;
int i;
if ((sb = xcalloc (struct socketbase, 1)) != NULL) {
sb->ownertask = gettask(ctx);
sb->sysbase = trap_get_long(ctx, 4);
trap_call_add_dreg(ctx, 0, -1);
sb->signal = trap_call_lib(ctx, sb->sysbase, -0x14A); /* AllocSignal */
if (sb->signal == -1) {
write_log (_T("bsdsocket: ERROR: Couldn't allocate signal for task 0x%08x.\n"), sb->ownertask);
free (sb);
return NULL;
}
// trap_get_dreg(ctx, 0) = SCRATCHBUFSIZE;
// trap_get_dreg(ctx, 1) = 0;
sb->dtablesize = DEFAULT_DTABLE_SIZE;
/* @@@ check malloc() result */
sb->dtable = xmalloc(SOCKET, sb->dtablesize);
sb->ftable = xcalloc(int, sb->dtablesize);
for (i = sb->dtablesize; i--;)
sb->dtable[i] = INVALID_SOCKET;
sb->eintrsigs = 0x1000; /* SIGBREAKF_CTRL_C */
sb->logfacility = 1 << 3; /* LOG_USER */
sb->logmask = 0xff;
if (!host_sbinit(ctx, sb)) {
/* @@@ free everything */
}
locksigqueue();
if (socketbases)
sb->next = socketbases;
socketbases = sb;
unlocksigqueue();
return sb;
}
return NULL;
}
STATIC_INLINE struct socketbase *get_socketbase (TrapContext *ctx)
{
return (struct socketbase*)get_pointer (trap_get_areg(ctx, 6) + offsetof (struct UAEBSDBase, sb));
}
static void free_socketbase (TrapContext *ctx)
{
struct socketbase *sb, *nsb;
if ((sb = get_socketbase (ctx)) != NULL) {
trap_call_add_dreg(ctx, 0, sb->signal);
trap_call_lib(ctx, sb->sysbase, -0x150); /* FreeSignal */
if (sb->hostent) {
trap_call_add_areg(ctx, 1, sb->hostent);
trap_call_add_dreg(ctx, 0, sb->hostentsize);
trap_call_lib(ctx, sb->sysbase, -0xD2); /* FreeMem */
}
if (sb->protoent) {
trap_call_add_areg(ctx, 1, sb->protoent);
trap_call_add_dreg(ctx, 0, sb->protoentsize);
trap_call_lib(ctx, sb->sysbase, -0xD2); /* FreeMem */
}
if (sb->servent) {
trap_call_add_areg(ctx, 1, sb->servent);
trap_call_add_dreg(ctx, 0, sb->serventsize);
trap_call_lib(ctx, sb->sysbase, -0xD2); /* FreeMem */
}
host_sbcleanup (sb);
free (sb->dtable);
free (sb->ftable);
locksigqueue ();
if (sb == socketbases)
socketbases = sb->next;
else {
for (nsb = socketbases; nsb; nsb = nsb->next) {
if (sb == nsb->next) {
nsb->next = sb->next;
break;
}
}
}
#if 1
if (sb == sbsigqueue)
sbsigqueue = sb->next;
else {
for (nsb = sbsigqueue; nsb; nsb = nsb->next) {
if (sb == nsb->next) {
nsb->next = sb->next;
break;
}
}
}
#endif
unlocksigqueue ();
free (sb);
}
}
static uae_u32 REGPARAM2 bsdsocklib_Expunge (TrapContext *ctx)
{
BSDTRACE ((_T("Expunge() -> [ignored]\n")));
return 0;
}
static uae_u32 functable, datatable, inittable;
static uae_u32 REGPARAM2 bsdsocklib_Open (TrapContext *ctx)
{
uae_u32 result = 0;
int opencount;
SB;
BSDTRACE ((_T("OpenLibrary() -> ")));
if ((sb = alloc_socketbase(ctx)) != NULL) {
trap_put_word(ctx, SockLibBase + 32, opencount = trap_get_word(ctx, SockLibBase + 32) + 1);
trap_call_add_areg(ctx, 0, functable);
trap_call_add_areg(ctx, 1, datatable);
trap_call_add_areg(ctx, 2, 0);
trap_call_add_dreg(ctx, 0, sizeof (struct UAEBSDBase));
trap_call_add_dreg(ctx, 1, 0);
result = trap_call_lib(ctx, sb->sysbase, -0x54); /* MakeLibrary */
put_pointer(result + offsetof(struct UAEBSDBase, sb), sb);
BSDTRACE ((_T("%0x [%d]\n"), result, opencount));
} else
BSDTRACE ((_T("failed (out of memory)\n")));
return result;
}
static uae_u32 REGPARAM2 bsdsocklib_Close (TrapContext *ctx)
{
int opencount;
uae_u32 base = trap_get_areg(ctx, 6);
uae_u32 negsize = get_word (base + 16);
free_socketbase(ctx);
trap_put_word(ctx, SockLibBase + 32, opencount = trap_get_word(ctx, SockLibBase + 32) - 1);
trap_call_add_areg(ctx, 1, base - negsize);
trap_call_add_dreg(ctx, 0, negsize + trap_get_word(ctx, base + 18));
trap_call_lib(ctx, trap_get_long(ctx, 4), -0xD2); /* FreeMem */
BSDTRACE ((_T("CloseLibrary() -> [%d]\n"), opencount));
return 0;
}
/* socket(domain, type, protocol)(d0/d1/d2) */
static uae_u32 REGPARAM2 bsdsocklib_socket (TrapContext *ctx)
{
struct socketbase *sb = get_socketbase (ctx);
return host_socket(ctx, sb, trap_get_dreg(ctx, 0), trap_get_dreg(ctx, 1),
trap_get_dreg(ctx, 2));
}
/* bind(s, name, namelen)(d0/a0/d1) */
static uae_u32 REGPARAM2 bsdsocklib_bind (TrapContext *ctx)
{
struct socketbase *sb = get_socketbase (ctx);
return host_bind(ctx, sb, trap_get_dreg(ctx, 0), trap_get_areg(ctx, 0),
trap_get_dreg(ctx, 1));
}
/* listen(s, backlog)(d0/d1) */
static uae_u32 REGPARAM2 bsdsocklib_listen (TrapContext *ctx)
{
struct socketbase *sb = get_socketbase (ctx);
return host_listen(ctx, sb, trap_get_dreg(ctx, 0), trap_get_dreg(ctx, 1));
}
/* accept(s, addr, addrlen)(d0/a0/a1) */
static uae_u32 REGPARAM2 bsdsocklib_accept (TrapContext *ctx)
{
struct socketbase *sb = get_socketbase (ctx);
host_accept(ctx, sb, trap_get_dreg(ctx, 0), trap_get_areg(ctx, 0), trap_get_areg(ctx, 1));
return sb->resultval;
}
/* connect(s, name, namelen)(d0/a0/d1) */
static uae_u32 REGPARAM2 bsdsocklib_connect (TrapContext *ctx)
{
struct socketbase *sb = get_socketbase (ctx);
host_connect(ctx, sb, trap_get_dreg(ctx, 0), trap_get_areg(ctx, 0), trap_get_dreg(ctx, 1));
return sb->sb_errno ? -1 : 0;
}
/* sendto(s, msg, len, flags, to, tolen)(d0/a0/d1/d2/a1/d3) */
static uae_u32 REGPARAM2 bsdsocklib_sendto (TrapContext *ctx)
{
struct socketbase *sb = get_socketbase (ctx);
host_sendto(ctx, sb, trap_get_dreg(ctx, 0), trap_get_areg(ctx, 0), NULL, trap_get_dreg(ctx, 1),
trap_get_dreg(ctx, 2), trap_get_areg(ctx, 1), trap_get_dreg(ctx, 3));
return sb->resultval;
}
/* send(s, msg, len, flags)(d0/a0/d1/d2) */
static uae_u32 REGPARAM2 bsdsocklib_send (TrapContext *ctx)
{
struct socketbase *sb = get_socketbase (ctx);
host_sendto(ctx, sb, trap_get_dreg(ctx, 0), trap_get_areg(ctx, 0), NULL, trap_get_dreg(ctx, 1),
trap_get_dreg(ctx, 2), 0, 0);
return sb->resultval;
}
/* recvfrom(s, buf, len, flags, from, fromlen)(d0/a0/d1/d2/a1/a2) */
static uae_u32 REGPARAM2 bsdsocklib_recvfrom (TrapContext *ctx)
{
struct socketbase *sb = get_socketbase (ctx);
host_recvfrom(ctx, sb, trap_get_dreg(ctx, 0), trap_get_areg(ctx, 0), NULL, trap_get_dreg(ctx, 1),
trap_get_dreg(ctx, 2), trap_get_areg(ctx, 1), trap_get_areg(ctx, 2));
return sb->resultval;
}
/* recv(s, buf, len, flags)(d0/a0/d1/d2) */
static uae_u32 REGPARAM2 bsdsocklib_recv (TrapContext *ctx)
{
struct socketbase *sb = get_socketbase (ctx);
host_recvfrom(ctx, sb, trap_get_dreg(ctx, 0), trap_get_areg(ctx, 0), NULL, trap_get_dreg(ctx, 1),
trap_get_dreg(ctx, 2), 0, 0);
return sb->resultval;
}
/* shutdown(s, how)(d0/d1) */
static uae_u32 REGPARAM2 bsdsocklib_shutdown (TrapContext *ctx)
{
struct socketbase *sb = get_socketbase (ctx);
return host_shutdown (sb, trap_get_dreg(ctx, 0), trap_get_dreg(ctx, 1));
}
/* setsockopt(s, level, optname, optval, optlen)(d0/d1/d2/a0/d3) */
static uae_u32 REGPARAM2 bsdsocklib_setsockopt (TrapContext *ctx)
{
struct socketbase *sb = get_socketbase (ctx);
host_setsockopt (sb, trap_get_dreg(ctx, 0), trap_get_dreg(ctx, 1), trap_get_dreg(ctx, 2),
trap_get_areg(ctx, 0), trap_get_dreg(ctx, 3));
return sb->resultval;
}
/* getsockopt(s, level, optname, optval, optlen)(d0/d1/d2/a0/a1) */
static uae_u32 REGPARAM2 bsdsocklib_getsockopt (TrapContext *ctx)
{
struct socketbase *sb = get_socketbase (ctx);
return host_getsockopt (ctx, sb, trap_get_dreg(ctx, 0), trap_get_dreg(ctx, 1), trap_get_dreg(ctx, 2),
trap_get_areg(ctx, 0), trap_get_areg(ctx, 1));
}
/* getsockname(s, hostname, namelen)(d0/a0/a1) */
static uae_u32 REGPARAM2 bsdsocklib_getsockname (TrapContext *ctx)
{
struct socketbase *sb = get_socketbase (ctx);
return host_getsockname (ctx, sb, trap_get_dreg(ctx, 0), trap_get_areg(ctx, 0), trap_get_areg(ctx, 1));
}
/* getpeername(s, hostname, namelen)(d0/a0/a1) */
static uae_u32 REGPARAM2 bsdsocklib_getpeername (TrapContext *ctx)
{
struct socketbase *sb = get_socketbase (ctx);
return host_getpeername (ctx, sb, trap_get_dreg(ctx, 0), trap_get_areg(ctx, 0), trap_get_areg(ctx, 1));
}
/* *------ generic system calls related to sockets */
/* IoctlSocket(d, request, argp)(d0/d1/a0) */
static uae_u32 REGPARAM2 bsdsocklib_IoctlSocket (TrapContext *ctx)
{
struct socketbase *sb = get_socketbase (ctx);
return host_IoctlSocket(ctx, sb, trap_get_dreg(ctx, 0), trap_get_dreg(ctx, 1), trap_get_areg(ctx, 0));
}
/* *------ AmiTCP/IP specific stuff */
/* CloseSocket(d)(d0) */
static uae_u32 REGPARAM2 bsdsocklib_CloseSocket (TrapContext *ctx)
{
struct socketbase *sb = get_socketbase (ctx);
return host_CloseSocket(ctx, sb, trap_get_dreg(ctx, 0));
}
/* WaitSelect(nfds, readfds, writefds, execptfds, timeout, maskp)(d0/a0/a1/a2/a3/d1) */
static uae_u32 REGPARAM2 bsdsocklib_WaitSelect (TrapContext *ctx)
{
struct socketbase *sb = get_socketbase (ctx);
host_WaitSelect(ctx, sb, trap_get_dreg(ctx, 0), trap_get_areg(ctx, 0), trap_get_areg(ctx, 1),
trap_get_areg(ctx, 2), trap_get_areg(ctx, 3), trap_get_dreg(ctx, 1));
return sb->resultval;
}
/* SetSocketSignals(SIGINTR, SIGIO, SIGURG)(d0/d1/d2) */
static uae_u32 REGPARAM2 bsdsocklib_SetSocketSignals (TrapContext *ctx)
{
struct socketbase *sb = get_socketbase (ctx);
BSDTRACE ((_T("SetSocketSignals(0x%08x,0x%08x,0x%08x) -> "), trap_get_dreg(ctx, 0), trap_get_dreg(ctx, 1), trap_get_dreg(ctx, 2)));
sb->eintrsigs = trap_get_dreg(ctx, 0);
sb->eventsigs = trap_get_dreg(ctx, 1);
return 0;
}
/* SetDTableSize(size)(d0) */
static uae_u32 bsdsocklib_SetDTableSize (SB, int newSize)
{
TrapContext *ctx = NULL;
int *newdtable;
int *newftable;
unsigned int *newmtable;
int i;
if (newSize < sb->dtablesize) {
/* I don't support lowering the size */
return 0;
}
newdtable = xcalloc (int, newSize);
newftable = xcalloc (int, newSize);
newmtable = xcalloc (unsigned int, newSize);
if (newdtable == NULL || newftable == NULL || newmtable == NULL) {
sb->resultval = -1;
bsdsocklib_seterrno(ctx, sb, ENOMEM);
xfree(newdtable);
xfree(newftable);
xfree(newmtable);
return -1;
}
memcpy (newdtable, sb->dtable, sb->dtablesize * sizeof(*sb->dtable));
memcpy (newftable, sb->ftable, sb->dtablesize * sizeof(*sb->ftable));
memcpy (newmtable, sb->mtable, sb->dtablesize * sizeof(*sb->mtable));
for (i = sb->dtablesize + 1; i < newSize; i++)
newdtable[i] = -1;
sb->dtablesize = newSize;
xfree(sb->dtable);
xfree(sb->ftable);
xfree(sb->mtable);
sb->dtable = (SOCKET*)newdtable;
sb->ftable = newftable;
sb->mtable = newmtable;
sb->resultval = 0;
return 0;
}
static int sockpoolindex (long id)
{
int i;
for (i = 0; i < SOCKPOOLSIZE; i++)
if (sockdata->sockpoolids[i] == id)
return i;
return -1;
}
/* ObtainSocket(id, domain, type, protocol)(d0/d1/d2/d3) */
static uae_u32 REGPARAM2 bsdsocklib_ObtainSocket (TrapContext *ctx)
{
struct socketbase *sb = get_socketbase (ctx);
int sd;
int id;
SOCKET_TYPE s;
int i;
id = trap_get_dreg(ctx, 0);
BSDTRACE ((_T("ObtainSocket(%d,%d,%d,%d) -> "), id, trap_get_dreg(ctx, 1), trap_get_dreg(ctx, 2), trap_get_dreg(ctx, 3)));
i = sockpoolindex (id);
if (i == -1) {
BSDTRACE ((_T("[invalid key]\n")));
return -1;
}
s = sockdata->sockpoolsocks[i];
sd = getsd(ctx, sb, s);
BSDTRACE ((_T(" -> Socket=%d\n"), sd));
if (sd != -1) {
sb->ftable[sd - 1] = sockdata->sockpoolflags[i];
callfdcallback(ctx, sb, sd - 1, FDCB_ALLOC);
sockdata->sockpoolids[i] = UNIQUE_ID;
return sd - 1;
}
return -1;
}
/* ReleaseSocket(fd, id)(d0/d1) */
static uae_u32 REGPARAM2 bsdsocklib_ReleaseSocket (TrapContext *ctx)
{
struct socketbase *sb = get_socketbase (ctx);
int sd;
int id;
SOCKET_TYPE s;
int i;
uae_u32 flags;
sd = trap_get_dreg(ctx, 0);
id = trap_get_dreg(ctx, 1);
sd++;
BSDTRACE ((_T("ReleaseSocket(%d,%d) -> "), sd, id));
s = getsock (ctx, sb, sd);
if (s != -1) {
flags = sb->ftable[sd - 1];
if (flags & REP_ALL) {
write_log (_T("bsdsocket: ERROR: ReleaseSocket() is not supported for sockets with async event notification enabled!\n"));
return -1;
}
releasesock(ctx, sb, sd);
if (id == UNIQUE_ID) {
for (;;) {
if (sockpoolindex (curruniqid) == -1)
break;
curruniqid += 129;
if ((unsigned long) (curruniqid + 1) < 65536)
curruniqid += 65537;
}
id = curruniqid;
} else if (id < 0 && id > 65535) {
if (sockpoolindex (id) != -1) {
BSDTRACE ((_T("[unique ID already exists]\n")));
return -1;
}
}
i = sockpoolindex (-1);
if (i == -1) {
BSDTRACE ((_T("-1\n")));
write_log (_T("bsdsocket: ERROR: Global socket pool overflow\n"));
return -1;
}
sockdata->sockpoolids[i] = id;
sockdata->sockpoolsocks[i] = s;
sockdata->sockpoolflags[i] = flags;
BSDTRACE ((_T("id %d s 0x%x\n"), id,s));
} else {
BSDTRACE ((_T("[invalid socket descriptor]\n")));
return -1;
}
return id;
}
/* ReleaseCopyOfSocket(fd, id)(d0/d1) */
static uae_u32 REGPARAM2 bsdsocklib_ReleaseCopyOfSocket (TrapContext *ctx)
{
struct socketbase *sb = get_socketbase (ctx);
int sd;
int id;
SOCKET_TYPE s;
int i;
uae_u32 flags;
sd = trap_get_dreg(ctx, 0);
id = trap_get_dreg(ctx, 1);
sd++;
BSDTRACE ((_T("ReleaseSocket(%d,%d) -> "), sd, id));
s = getsock (ctx, sb, sd);
if (s != -1) {
flags = sb->ftable[sd - 1];
if (flags & REP_ALL) {
write_log (_T("bsdsocket: ERROR: ReleaseCopyOfSocket() is not supported for sockets with async event notification enabled!\n"));
return -1;
}
if (id == UNIQUE_ID) {
for (;;) {
if (sockpoolindex (curruniqid) == -1)
break;
curruniqid += 129;
if ((unsigned long) (curruniqid + 1) < 65536)
curruniqid += 65537;
}
id = curruniqid;
} else if (id < 0 && id > 65535) {
if (sockpoolindex (id) != -1) {
BSDTRACE ((_T("[unique ID already exists]\n")));
return -1;
}
}
i = sockpoolindex (-1);
if (i == -1) {
BSDTRACE ((_T("-1\n")));
write_log (_T("bsdsocket: ERROR: Global socket pool overflow\n"));
return -1;
}
sockdata->sockpoolids[i] = id;
sockdata->sockpoolsocks[i] = s;
sockdata->sockpoolflags[i] = flags;
BSDTRACE ((_T("id %d s 0x%x\n"), id,s));
} else {
BSDTRACE ((_T("[invalid socket descriptor]\n")));
return -1;
}
return id;
}
/* Errno()() */
static uae_u32 REGPARAM2 bsdsocklib_Errno (TrapContext *ctx)
{
struct socketbase *sb = get_socketbase (ctx);
BSDTRACE ((_T("Errno() -> %d\n"), sb->sb_errno));
return sb->sb_errno;
}
/* SetErrnoPtr(errno_p, size)(a0/d0) */
static uae_u32 REGPARAM2 bsdsocklib_SetErrnoPtr (TrapContext *ctx)
{
struct socketbase *sb = get_socketbase (ctx);
uae_u32 errnoptr = trap_get_areg(ctx, 0);
uae_u32 size = trap_get_dreg(ctx, 0);
BSDTRACE ((_T("SetErrnoPtr(0x%08x,%d) -> "), errnoptr, size));
if (size == 1 || size == 2 || size == 4) {
sb->errnoptr = errnoptr;
sb->errnosize = size;
BSDTRACE ((_T("OK\n")));
return 0;
}
bsdsocklib_seterrno(ctx, sb, 22); /* EINVAL */
return -1;
}
/* *------ inet library calls related to inet address manipulation */
/* Inet_NtoA(in)(d0) */
static uae_u32 REGPARAM2 bsdsocklib_Inet_NtoA (TrapContext *ctx)
{
struct socketbase *sb = get_socketbase (ctx);
return host_Inet_NtoA(ctx, sb, trap_get_dreg(ctx, 0));
}
/* inet_addr(cp)(a0) */
static uae_u32 REGPARAM2 bsdsocklib_inet_addr (TrapContext *ctx)
{
return host_inet_addr(ctx, trap_get_areg(ctx, 0));
}
/* Inet_LnaOf(in)(d0) */
static uae_u32 REGPARAM2 bsdsocklib_Inet_LnaOf (TrapContext *ctx)
{
write_log (_T("bsdsocket: UNSUPPORTED: Inet_LnaOf()\n"));
return 0;
}
/* Inet_NetOf(in)(d0) */
static uae_u32 REGPARAM2 bsdsocklib_Inet_NetOf (TrapContext *ctx)
{
write_log (_T("bsdsocket: UNSUPPORTED: Inet_NetOf()\n"));
return 0;
}
/* Inet_MakeAddr(net, host)(d0/d1) */
static uae_u32 REGPARAM2 bsdsocklib_Inet_MakeAddr (TrapContext *ctx)
{
write_log (_T("bsdsocket: UNSUPPORTED: Inet_MakeAddr()\n"));
return 0;
}
/* inet_network(cp)(a0) */
static uae_u32 REGPARAM2 bsdsocklib_inet_network (TrapContext *ctx)
{
return host_inet_addr(ctx, trap_get_areg(ctx, 0));
}
/* *------ gethostbyname etc */
/* gethostbyname(name)(a0) */