-
Notifications
You must be signed in to change notification settings - Fork 0
/
bsdsocket-posix-new.c
executable file
·1671 lines (1416 loc) · 38.2 KB
/
bsdsocket-posix-new.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
/*
* UAE - The Un*x Amiga Emulator
*
* bsdsocket.library emulation - Unix
*
* Copyright 2000-2001 Carl Drougge <[email protected]> <[email protected]>
* Copyright 2003-2005 Richard Drummond
* Copyright 2004 Jeff Shepherd
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "sysconfig.h"
#include "sysdeps.h"
#include "options.h"
#include "memory.h"
#include "custom.h"
#include "newcpu.h"
#include "autoconf.h"
#include "traps.h"
#include "threaddep/thread.h"
#include "native2amiga.h"
#include "bsdsocket.h"
#define S_GL_result(res) sb->resultval = (res)
uae_u32 bsdthr_Accept_2 (SB);
uae_u32 bsdthr_Recv_2 (SB);
uae_u32 bsdthr_blockingstuff (uae_u32 (*tryfunc)(SB), SB);
uae_u32 bsdthr_SendRecvAcceptConnect (uae_u32 (*tryfunc)(SB), SB);
uae_u32 bsdthr_Send_2 (SB);
uae_u32 bsdthr_Connect_2 (SB);
uae_u32 bsdthr_WaitSelect (SB);
uae_u32 bsdthr_Wait (SB);
void clearsockabort (SB);
static uae_sem_t sem_queue;
/**
** Helper functions
**/
/*
* Map host errno to amiga errno
*/
static int mapErrno (int e)
{
switch (e) {
case EINTR: e = 4; break;
case EDEADLK: e = 11; break;
case EAGAIN: e = 35; break;
case EINPROGRESS: e = 36; break;
case EALREADY: e = 37; break;
case ENOTSOCK: e = 38; break;
case EDESTADDRREQ: e = 39; break;
case EMSGSIZE: e = 40; break;
case EPROTOTYPE: e = 41; break;
case ENOPROTOOPT: e = 42; break;
case EPROTONOSUPPORT: e = 43; break;
case ESOCKTNOSUPPORT: e = 44; break;
case EOPNOTSUPP: e = 45; break;
case EPFNOSUPPORT: e = 46; break;
case EAFNOSUPPORT: e = 47; break;
case EADDRINUSE: e = 48; break;
case EADDRNOTAVAIL: e = 49; break;
case ENETDOWN: e = 50; break;
case ENETUNREACH: e = 51; break;
case ENETRESET: e = 52; break;
case ECONNABORTED: e = 53; break;
case ECONNRESET: e = 54; break;
case ENOBUFS: e = 55; break;
case EISCONN: e = 56; break;
case ENOTCONN: e = 57; break;
case ESHUTDOWN: e = 58; break;
case ETOOMANYREFS: e = 59; break;
case ETIMEDOUT: e = 60; break;
case ECONNREFUSED: e = 61; break;
case ELOOP: e = 62; break;
case ENAMETOOLONG: e = 63; break;
default: break;
}
return e;
}
/*
* Map amiga (s|g)etsockopt level into native one
*/
static int mapsockoptlevel (int level)
{
switch (level) {
case 0xffff:
return SOL_SOCKET;
case 0:
return IPPROTO_IP;
case 1:
return IPPROTO_ICMP;
case 2:
return IPPROTO_IGMP;
#ifdef IPPROTO_IPIP
case 4:
return IPPROTO_IPIP;
#endif
case 6:
return IPPROTO_TCP;
case 8:
return IPPROTO_EGP;
case 12:
return IPPROTO_PUP;
case 17:
return IPPROTO_UDP;
case 22:
return IPPROTO_IDP;
#ifdef IPPROTO_TP
case 29:
return IPPROTO_TP;
#endif
case 98:
return IPPROTO_ENCAP;
default:
DEBUG_LOG ("Unknown sockopt level %d\n", level);
return level;
}
}
/*
* Map amiga (s|g)etsockopt optname into native one
*/
static int mapsockoptname (int level, int optname)
{
switch (level) {
case SOL_SOCKET:
switch (optname) {
case 0x0001:
return SO_DEBUG;
case 0x0002:
return SO_ACCEPTCONN;
case 0x0004:
return SO_REUSEADDR;
case 0x0008:
return SO_KEEPALIVE;
case 0x0010:
return SO_DONTROUTE;
case 0x0020:
return SO_BROADCAST;
#ifdef SO_USELOOPBACK
case 0x0040:
return SO_USELOOPBACK;
#endif
case 0x0080:
return SO_LINGER;
case 0x0100:
return SO_OOBINLINE;
#ifdef SO_REUSEPORT
case 0x0200:
return SO_REUSEPORT;
#endif
case 0x1001:
return SO_SNDBUF;
case 0x1002:
return SO_RCVBUF;
case 0x1003:
return SO_SNDLOWAT;
case 0x1004:
return SO_RCVLOWAT;
case 0x1005:
return SO_SNDTIMEO;
case 0x1006:
return SO_RCVTIMEO;
case 0x1007:
return SO_ERROR;
case 0x1008:
return SO_TYPE;
default:
DEBUG_LOG ("Invalid setsockopt option %x for level %d\n",
optname, level);
return -1;
}
break;
case IPPROTO_IP:
switch (optname) {
case 1:
return IP_OPTIONS;
case 2:
return IP_HDRINCL;
case 3:
return IP_TOS;
case 4:
return IP_TTL;
case 5:
return IP_RECVOPTS;
case 6:
return IP_RECVRETOPTS;
case 8:
return IP_RETOPTS;
case 9:
return IP_MULTICAST_IF;
case 10:
return IP_MULTICAST_TTL;
case 11:
return IP_MULTICAST_LOOP;
case 12:
return IP_ADD_MEMBERSHIP;
default:
DEBUG_LOG ("Invalid setsockopt option %x for level %d\n",
optname, level);
return -1;
}
break;
case IPPROTO_TCP:
switch (optname) {
case 1:
return TCP_NODELAY;
case 2:
return TCP_MAXSEG;
default:
DEBUG_LOG ("Invalid setsockopt option %x for level %d\n",
optname, level);
return -1;
}
break;
default:
DEBUG_LOG ("Unknown level %d\n", level);
return -1;
}
}
/*
* Map amiga (s|g)etsockopt return value into the correct form
*/
static void mapsockoptreturn(int level, int optname, uae_u32 optval, void *buf)
{
switch (level) {
case SOL_SOCKET:
switch (optname) {
case SO_DEBUG:
case SO_ACCEPTCONN:
case SO_REUSEADDR:
case SO_KEEPALIVE:
case SO_DONTROUTE:
case SO_BROADCAST:
#ifdef SO_USELOOPBACK
case SO_USELOOPBACK:
#endif
case SO_LINGER:
case SO_OOBINLINE:
#ifdef SO_REUSEPORT
case SO_REUSEPORT:
#endif
case SO_SNDBUF:
case SO_RCVBUF:
case SO_SNDLOWAT:
case SO_RCVLOWAT:
case SO_SNDTIMEO:
case SO_RCVTIMEO:
case SO_TYPE:
put_long(optval, *(int *)buf);
break;
case SO_ERROR:
DEBUG_LOG("New errno is %d\n", mapErrno(*(int *)buf));
put_long(optval, mapErrno(*(int *)buf));
break;
default:
break;
}
break;
case IPPROTO_IP:
switch (optname) {
case IP_OPTIONS:
case IP_HDRINCL:
case IP_TOS:
case IP_TTL:
case IP_RECVOPTS:
//case IP_RECVRETOPTS:
//case IP_RETOPTS:
case IP_MULTICAST_IF:
case IP_MULTICAST_TTL:
case IP_MULTICAST_LOOP:
case IP_ADD_MEMBERSHIP:
put_long(optval, *(int *)buf);
break;
default:
break;
}
break;
case IPPROTO_TCP:
switch (optname) {
case TCP_NODELAY:
case TCP_MAXSEG:
put_long(optval,*(int *)buf);
break;
default:
break;
}
break;
default:
break;
}
}
/*
* Map amiga (s|g)etsockopt value from amiga to the appropriate value
*/
static void mapsockoptvalue(int level, int optname, uae_u32 optval, void *buf)
{
switch (level) {
case SOL_SOCKET:
switch (optname) {
case SO_DEBUG:
case SO_ACCEPTCONN:
case SO_REUSEADDR:
case SO_KEEPALIVE:
case SO_DONTROUTE:
case SO_BROADCAST:
#ifdef SO_USELOOPBACK
case SO_USELOOPBACK:
#endif
case SO_LINGER:
case SO_OOBINLINE:
#ifdef SO_REUSEPORT
case SO_REUSEPORT:
#endif
case SO_SNDBUF:
case SO_RCVBUF:
case SO_SNDLOWAT:
case SO_RCVLOWAT:
case SO_SNDTIMEO:
case SO_RCVTIMEO:
case SO_TYPE:
case SO_ERROR:
*((int *)buf) = get_long(optval);
break;
default:
break;
}
break;
case IPPROTO_IP:
switch (optname) {
case IP_OPTIONS:
case IP_HDRINCL:
case IP_TOS:
case IP_TTL:
case IP_RECVOPTS:
//case IP_RECVRETOPTS:
//case IP_RETOPTS:
case IP_MULTICAST_IF:
case IP_MULTICAST_TTL:
case IP_MULTICAST_LOOP:
case IP_ADD_MEMBERSHIP:
*((int *)buf) = get_long(optval);
break;
default:
break;
}
break;
case IPPROTO_TCP:
switch (optname) {
case TCP_NODELAY:
case TCP_MAXSEG:
*((int *)buf) = get_long(optval);
break;
default:
break;
}
break;
default:
break;
}
}
STATIC_INLINE void fd_zero (uae_u32 fdset, uae_u32 nfds)
{
unsigned int i;
for (i = 0; i < nfds; i += 32, fdset += 4)
put_long (fdset, 0);
}
STATIC_INLINE int bsd_amigaside_FD_ISSET (int n, uae_u32 set)
{
uae_u32 foo = get_long (set + (n / 32));
if (foo & (1 << (n % 32)))
return 1;
return 0;
}
STATIC_INLINE void bsd_amigaside_FD_ZERO (uae_u32 set)
{
put_long (set, 0);
put_long (set + 4, 0);
}
STATIC_INLINE void bsd_amigaside_FD_SET (int n, uae_u32 set)
{
set = set + (n / 32);
put_long (set, get_long (set) | (1 << (n % 32)));
}
#ifdef DEBUG_BSDSOCKET
static void printSockAddr (struct sockaddr_in *in)
{
DEBUG_LOG ("Family %d, ", in->sin_family);
DEBUG_LOG ("Port %d,", ntohs (in->sin_port));
DEBUG_LOG ("Address %s,", inet_ntoa (in->sin_addr));
}
#else
#define printSockAddr(sockAddr)
#endif
/*
* Copy a sockaddr object from amiga space to native space
*/
static int copysockaddr_a2n (struct sockaddr_in *addr, uae_u32 a_addr, unsigned int len)
{
if ((len > sizeof (struct sockaddr_in)) || (len < 8))
return 1;
if (a_addr == 0)
return 0;
addr->sin_family = get_byte (a_addr + 1);
addr->sin_port = htons (get_word (a_addr + 2));
addr->sin_addr.s_addr = htonl (get_long (a_addr + 4));
if (len > 8)
memcpy (&addr->sin_zero, get_real_address (a_addr + 8), len - 8); /* Pointless? */
return 0;
}
/*
* Copy a sockaddr object from native space to amiga space
*/
static int copysockaddr_n2a (uae_u32 a_addr, const struct sockaddr_in *addr, unsigned int len)
{
if (len < 8)
return 1;
if (a_addr == 0)
return 0;
put_byte (a_addr, 0); /* Anyone use this field? */
put_byte (a_addr + 1, addr->sin_family);
put_word (a_addr + 2, ntohs (addr->sin_port));
put_long (a_addr + 4, ntohl (addr->sin_addr.s_addr));
if (len > 8)
memset (get_real_address (a_addr + 8), 0, len - 8);
return 0;
}
/*
* Copy a hostent object from native space to amiga space
*/
static void copyHostent (const struct hostent *hostent, SB)
{
int size = 28;
int i;
int numaddr = 0;
int numaliases = 0;
uae_u32 aptr;
if (hostent->h_name != NULL)
size += strlen(hostent->h_name)+1;
if (hostent->h_aliases != NULL)
while (hostent->h_aliases[numaliases])
size += strlen(hostent->h_aliases[numaliases++]) + 5;
if (hostent->h_addr_list != NULL) {
while (hostent->h_addr_list[numaddr])
numaddr++;
size += numaddr*(hostent->h_length+4);
}
aptr = sb->hostent + 28 + numaliases * 4 + numaddr * 4;
// transfer hostent to Amiga memory
put_long (sb->hostent + 4, sb->hostent + 20);
put_long (sb->hostent + 8, hostent->h_addrtype);
put_long (sb->hostent + 12, hostent->h_length);
put_long (sb->hostent + 16, sb->hostent + 24 + numaliases*4);
for (i = 0; i < numaliases; i++)
put_long (sb->hostent + 20 + i * 4, addstr (&aptr, hostent->h_aliases[i]));
put_long (sb->hostent + 20 + numaliases * 4, 0);
for (i = 0; i < numaddr; i++) {
put_long (sb->hostent + 24 + (numaliases + i) * 4,
addmem (&aptr, hostent->h_addr_list[i], hostent->h_length));
}
put_long (sb->hostent + 24 + numaliases * 4 + numaddr * 4, 0);
put_long (sb->hostent, aptr);
addstr (&aptr, hostent->h_name);
TRACE (("OK (%s)\n",hostent->h_name));
bsdsocklib_seterrno (sb,0);
}
/*
* Copy a protoent object from native space to Amiga space
*/
static void copyProtoent (TrapContext *context, SB, const struct protoent *p)
{
size_t size = 16;
int numaliases = 0;
int i;
uae_u32 aptr;
// compute total size of protoent
if (p->p_name != NULL)
size += strlen (p->p_name) + 1;
if (p->p_aliases != NULL)
while (p->p_aliases[numaliases])
size += strlen (p->p_aliases[numaliases++]) + 5;
if (sb->protoent) {
uae_FreeMem (context, sb->protoent, sb->protoentsize);
}
sb->protoent = uae_AllocMem (context, size, 0);
if (!sb->protoent) {
write_log ("BSDSOCK: WARNING - copyProtoent() ran out of Amiga memory (couldn't allocate %d bytes)\n", size);
bsdsocklib_seterrno (sb, 12); // ENOMEM
return;
}
sb->protoentsize = size;
aptr = sb->protoent + 16 + numaliases * 4;
// transfer protoent to Amiga memory
put_long (sb->protoent + 4, sb->protoent + 12);
put_long (sb->protoent + 8, p->p_proto);
for (i = 0; i < numaliases; i++)
put_long (sb->protoent + 12 + i * 4, addstr (&aptr, p->p_aliases[i]));
put_long (sb->protoent + 12 + numaliases * 4, 0);
put_long (sb->protoent, aptr);
addstr (&aptr, p->p_name);
bsdsocklib_seterrno(sb, 0);
}
uae_u32 bsdthr_WaitSelect (SB)
{
fd_set sets [3];
int i, s, set, a_s, max;
uae_u32 a_set;
struct timeval tv;
int r;
DEBUG_LOG ("WaitSelect: %d 0x%x 0x%x 0x%x 0x%x 0x%x\n",
sb->nfds, sb->sets [0], sb->sets [1], sb->sets [2], sb->timeout, sb->sigmp);
if (sb->timeout)
DEBUG_LOG ("WaitSelect: timeout %d %d\n", get_long (sb->timeout),
get_long (sb->timeout + 4));
FD_ZERO (&sets [0]);
FD_ZERO (&sets [1]);
FD_ZERO (&sets [2]);
/* Set up the abort socket */
FD_SET (sb->sockabort[0], &sets[0]);
FD_SET (sb->sockabort[0], &sets[2]);
max = sb->sockabort[0];
for (set = 0; set < 3; set++) {
if (sb->sets [set] != 0) {
a_set = sb->sets [set];
for (i = 0; i < sb->nfds; i++) {
if (bsd_amigaside_FD_ISSET (i, a_set)) {
s = getsock (sb, i + 1);
DEBUG_LOG ("WaitSelect: AmigaSide %d set. NativeSide %d.\n", i, s);
if (s == -1) {
write_log ("BSDSOCK: WaitSelect() called with invalid descriptor %d in set %d.\n", i, set);
} else {
FD_SET (s, &sets [set]);
if (max < s) max = s;
}
}
}
}
}
max++;
if (sb->timeout) {
tv.tv_sec = get_long (sb->timeout);
tv.tv_usec = get_long (sb->timeout + 4);
}
DEBUG_LOG("Select going to select\n");
r = select (max, &sets [0], &sets [1], &sets [2], (sb->timeout == 0) ? NULL : &tv);
DEBUG_LOG("Select returns %d, errno is %d\n", r, errno);
if( r > 0 ) {
/* Socket told us to abort */
if (FD_ISSET (sb->sockabort[0], &sets[0])) {
/* read from the pipe to reset it */
DEBUG_LOG ("WaitSelect aborted from signal\n");
r = 0;
for (set = 0; set < 3; set++)
if (sb->sets [set] != 0)
bsd_amigaside_FD_ZERO (sb->sets [set]);
clearsockabort (sb);
}
else
/* This is perhaps slightly inefficient, but I don't care.. */
for (set = 0; set < 3; set++) {
a_set = sb->sets [set];
if (a_set != 0) {
bsd_amigaside_FD_ZERO (a_set);
for (i = 0; i < sb->nfds; i++) {
a_s = getsock (sb, i + 1);
if (a_s != -1) {
if (FD_ISSET (a_s, &sets [set])) {
DEBUG_LOG ("WaitSelect: NativeSide %d set. AmigaSide %d.\n", a_s, i);
bsd_amigaside_FD_SET (i, a_set);
}
}
}
}
}
} else if (r == 0) { /* Timeout. I think we're supposed to clear the sets.. */
for (set = 0; set < 3; set++)
if (sb->sets [set] != 0)
bsd_amigaside_FD_ZERO (sb->sets [set]);
}
DEBUG_LOG ("WaitSelect: %d(%d)\n", r, errno);
return r;
}
uae_u32 bsdthr_Accept_2 (SB)
{
int foo, s, s2;
long flags;
struct sockaddr_in addr;
socklen_t hlen = sizeof (struct sockaddr_in);
if ((s = accept (sb->s, (struct sockaddr *)&addr, &hlen)) >= 0) {
if ((flags = fcntl (s, F_GETFL)) == -1)
flags = 0;
fcntl (s, F_SETFL, flags & ~O_NONBLOCK); /* @@@ Don't do this if it's supposed to stay nonblocking... */
s2 = getsd (sb, s);
sb->ftable[s2-1] = sb->ftable[sb->len]; /* new socket inherits the old socket's properties */
DEBUG_LOG ("Accept: AmigaSide %d, NativeSide %d, len %d(%d)",
sb->resultval, s, &hlen, get_long(sb->a_addrlen));
printSockAddr (&addr);
foo = get_long (sb->a_addrlen);
if (foo > 16)
put_long (sb->a_addrlen, 16);
copysockaddr_n2a (sb->a_addr, &addr, foo);
return s2 - 1;
} else {
return -1;
}
}
uae_u32 bsdthr_Recv_2 (SB)
{
int foo;
int l, i;
if (sb->from == 0) {
foo = recv (sb->s, sb->buf, sb->len, sb->flags /*| MSG_NOSIGNAL*/);
DEBUG_LOG ("recv2, recv returns %d, errno is %d\n", foo, errno);
} else {
struct sockaddr_in addr;
socklen_t l = sizeof (struct sockaddr_in);
i = get_long (sb->fromlen);
copysockaddr_a2n (&addr, sb->from, i);
foo = recvfrom (sb->s, sb->buf, sb->len, sb->flags | MSG_NOSIGNAL, (struct sockaddr *)&addr, &l);
DEBUG_LOG ("recv2, recvfrom returns %d, errno is %d\n", foo, errno);
if (foo >= 0) {
copysockaddr_n2a (sb->from, &addr, l);
put_long (sb->fromlen, l);
}
}
return foo;
}
uae_u32 bsdthr_Send_2 (SB)
{
if (sb->to == 0) {
return send (sb->s, sb->buf, sb->len, sb->flags | MSG_NOSIGNAL);
} else {
struct sockaddr_in addr;
int l = sizeof (struct sockaddr_in);
copysockaddr_a2n (&addr, sb->to, sb->tolen);
return sendto (sb->s, sb->buf, sb->len, sb->flags | MSG_NOSIGNAL,
(struct sockaddr *)&addr, l);
}
}
uae_u32 bsdthr_Connect_2 (SB)
{
if (sb->action == 1) {
struct sockaddr_in addr;
int len = sizeof (struct sockaddr_in);
int retval;
copysockaddr_a2n (&addr, sb->a_addr, sb->a_addrlen);
retval = connect (sb->s, (struct sockaddr *)&addr, len);
DEBUG_LOG ("Connect returns %d, errno is %d\n", retval, errno);
/* Hack: I need to set the action to something other than
* 1 but I know action == 2 does the correct thing
*/
sb->action = 2;
if (retval == 0) {
errno = 0;
}
return retval;
} else {
int foo;
socklen_t bar;
bar = sizeof (foo);
if (getsockopt (sb->s, SOL_SOCKET, SO_ERROR, &foo, &bar) == 0) {
errno = foo;
DEBUG_LOG("Connect status is %d\n", foo);
return (foo == 0) ? 0 : -1;
}
return -1;
}
}
uae_u32 bsdthr_SendRecvAcceptConnect (uae_u32 (*tryfunc)(SB), SB)
{
return bsdthr_blockingstuff (tryfunc, sb);
}
uae_u32 bsdthr_blockingstuff (uae_u32 (*tryfunc)(SB), SB)
{
int done = 0, foo;
long flags;
int nonblock;
if ((flags = fcntl (sb->s, F_GETFL)) == -1)
flags = 0;
nonblock = (flags & O_NONBLOCK);
fcntl (sb->s, F_SETFL, flags | O_NONBLOCK);
while (!done) {
done = 1;
foo = tryfunc (sb);
if (foo < 0 && !nonblock) {
if ((errno == EAGAIN) || (errno == EWOULDBLOCK) || (errno == EINPROGRESS)) {
fd_set readset, writeset, exceptset;
int maxfd = (sb->s > sb->sockabort[0]) ? sb->s : sb->sockabort[0];
int num;
FD_ZERO (&readset);
FD_ZERO (&writeset);
FD_ZERO (&exceptset);
if (sb->action == 3 || sb->action == 6)
FD_SET (sb->s, &readset);
if (sb->action == 2 || sb->action == 1 || sb->action == 4)
FD_SET (sb->s, &writeset);
FD_SET (sb->sockabort[0], &readset);
num = select (maxfd + 1, &readset, &writeset, &exceptset, NULL);
if (num == -1) {
DEBUG_LOG ("Blocking select(%d) returns -1,errno is %d\n", sb->sockabort[0],errno);
fcntl (sb->s, F_SETFL, flags);
return -1;
}
if (FD_ISSET (sb->sockabort[0], &readset) || FD_ISSET (sb->sockabort[0], &writeset)) {
/* reset sock abort pipe */
/* read from the pipe to reset it */
DEBUG_LOG ("select aborted from signal\n");
clearsockabort (sb);
DEBUG_LOG ("Done read\n");
errno = EINTR;
done = 1;
} else {
done = 0;
}
} else if (errno == EINTR)
done = 1;
}
}
fcntl (sb->s, F_SETFL, flags);
return foo;
}
static void *bsdlib_threadfunc (void *arg)
{
struct socketbase *sb = (struct socketbase *) arg;
DEBUG_LOG ("THREAD_START\n");
while (1) {
uae_sem_wait (&sb->sem);
DEBUG_LOG ("Socket thread got action %d\n", sb->action);
switch (sb->action) {
case 0: /* kill thread (CloseLibrary) */
DEBUG_LOG ("THREAD_END\n");
uae_sem_destroy (&sb->sem);
return NULL;
case 1: /* Connect */
sb->resultval = bsdthr_SendRecvAcceptConnect (bsdthr_Connect_2, sb);
break;
/* @@@ Should check (from|to)len so it's 16.. */
case 2: /* Send[to] */
sb->resultval = bsdthr_SendRecvAcceptConnect (bsdthr_Send_2, sb);
break;
case 3: /* Recv[from] */
sb->resultval = bsdthr_SendRecvAcceptConnect (bsdthr_Recv_2, sb);
break;
case 4: { /* Gethostbyname */
struct hostent *tmphostent = gethostbyname ((char *)get_real_address (sb->name));
if (tmphostent) {
copyHostent (tmphostent, sb);
bsdsocklib_setherrno (sb, 0);
} else
SETHERRNO;
break;
}
case 5: /* WaitSelect */
sb->resultval = bsdthr_WaitSelect (sb);
break;
case 6: /* Accept */
sb->resultval = bsdthr_SendRecvAcceptConnect (bsdthr_Accept_2, sb);
break;
case 7: {
struct hostent *tmphostent = gethostbyaddr (get_real_address (sb->name), sb->a_addrlen, sb->flags);
if (tmphostent) {
copyHostent (tmphostent, sb);
bsdsocklib_setherrno (sb, 0);
} else
SETHERRNO;
break;
}
}
SETERRNO;
SETSIGNAL;
}
return NULL; /* Just to keep GCC happy.. */
}
void host_connect (TrapContext *context, SB, uae_u32 sd, uae_u32 name, uae_u32 namelen)
{
sb->s = getsock (sb, sd + 1);
if (sb->s == -1) {
sb->resultval = -1;
bsdsocklib_seterrno (sb, 9); /* EBADF */
return;
}
sb->a_addr = name;
sb->a_addrlen = namelen;
sb->action = 1;
uae_sem_post (&sb->sem);
WAITSIGNAL;
}
void host_sendto (TrapContext *context, SB, uae_u32 sd, uae_u32 msg, uae_u32 len, uae_u32 flags, uae_u32 to, uae_u32 tolen)
{
sb->s = getsock (sb, sd + 1);
if (sb->s == -1) {
sb->resultval = -1;
bsdsocklib_seterrno (sb, 9); /* EBADF */
return;
}
sb->buf = get_real_address (msg);
sb->len = len;
sb->flags = flags;
sb->to = to;
sb->tolen = tolen;
sb->action = 2;
uae_sem_post (&sb->sem);
WAITSIGNAL;
}
void host_recvfrom (TrapContext *context, SB, uae_u32 sd, uae_u32 msg, uae_u32 len, uae_u32 flags, uae_u32 addr, uae_u32 addrlen)
{
int s = getsock (sb, sd + 1);
DEBUG_LOG ("Recv[from](%lx, %d, %lx, %ld, %lx, %lx, %d)\n",
sb, sd, msg, len, flags, addr, addrlen);
if (s == -1) {
sb->resultval = -1;
bsdsocklib_seterrno (sb, 9); /* EBADF */;
return;
}
sb->s = s;
sb->buf = get_real_address (msg);
sb->len = len;
sb->flags = flags;
sb->from = addr;
sb->fromlen= addrlen;
sb->action = 3;
uae_sem_post (&sb->sem);
WAITSIGNAL;
}
void host_setsockopt (SB, uae_u32 sd, uae_u32 level, uae_u32 optname, uae_u32 optval, uae_u32 optlen)
{
int s = getsock (sb, sd + 1);
int nativelevel = mapsockoptlevel (level);
int nativeoptname = mapsockoptname(nativelevel, optname);
void *buf;
if (s == -1) {
sb->resultval = -1;
bsdsocklib_seterrno (sb, 9); /* EBADF */;
return;
}
if (optval) {
buf = malloc(optlen);
mapsockoptvalue(nativelevel, nativeoptname, optval, buf);
} else {
buf = NULL;
}
sb->resultval = setsockopt (s, nativelevel, nativeoptname, buf, optlen);
if (buf)
free(buf);
SETERRNO;
DEBUG_LOG ("setsockopt: sock %d, level %d, 'name' %d(%d), len %d -> %d, %d\n",
s, level, optname, nativeoptname, optlen,
sb->resultval, errno);
}
uae_u32 host_getsockname (SB, uae_u32 sd, uae_u32 name, uae_u32 namelen)
{
int s;
socklen_t len = sizeof (struct sockaddr_in);
struct sockaddr_in addr;
DEBUG_LOG ("getsockname(%d,0x%lx,%d) -> ", sd, name, len);
s = getsock (sb, sd + 1);