1 #pragma once
2 #ifndef SRC_INCLUDE_BSDSOCKET_H_INCLUDED
3 #define SRC_INCLUDE_BSDSOCKET_H_INCLUDED 1
4 
5  /*
6   * UAE - The Un*x Amiga Emulator
7   *
8   * bsdsocket.library emulation
9   *
10   * Copyright 1997,98 Mathias Ortmann
11   *
12   */
13 
14 #define BSD_TRACING_ENABLED 0
15 
16 extern int log_bsd;
17 
18 #define ISBSDTRACE (log_bsd || BSD_TRACING_ENABLED)
19 #define BSDTRACE(x) do { if (ISBSDTRACE) { write_log x; } } while(0)
20 
21 int init_socket_layer (void);
22 void deinit_socket_layer (void);
23 
24 /* inital size of per-process descriptor table (currently fixed) */
25 #define DEFAULT_DTABLE_SIZE 64
26 
27 #define SCRATCHBUFSIZE 128
28 
29 #define MAXPENDINGASYNC 512
30 
31 #define MAXADDRLEN 256
32 
33 #define SOCKET_TYPE int
34 
35 /* allocated and maintained on a per-task basis */
36 struct socketbase {
37     struct socketbase *next;
38     struct socketbase *nextsig;	/* queue for tasks to signal */
39 
40     uaecptr sysbase;
41     int dosignal;		/* signal flag */
42     uae_u32 ownertask;		/* task that opened the library */
43     int signal;			/* signal allocated for that task */
44     int sb_errno, sb_herrno;	/* errno and herrno variables */
45     uae_u32 errnoptr, herrnoptr;	/* pointers */
46     uae_u32 errnosize, herrnosize;	/* pinter sizes */
47     int dtablesize;		/* current descriptor/flag etc. table size */
48     SOCKET_TYPE *dtable;	/* socket descriptor table */
49     int *ftable;		/* socket flags */
50     int resultval;
51     uae_u32 hostent;		/* pointer to the current hostent structure (Amiga mem) */
52     uae_u32 hostentsize;
53     uae_u32 protoent;		/* pointer to the current protoent structure (Amiga mem) */
54     uae_u32 protoentsize;
55     uae_u32 servent;		/* pointer to the current servent structure (Amiga mem) */
56     uae_u32 serventsize;
57     uae_u32 sigstosend;
58     uae_u32 eventsigs;		/* EVENT sigmask */
59     uae_u32 eintrsigs;		/* EINTR sigmask */
60     int eintr;			/* interrupted by eintrsigs? */
61     int eventindex;		/* current socket looked at by GetSocketEvents() to prevent starvation */
62 	uae_u32 logstat;
63 	uae_u32 logptr;
64 	uae_u32 logmask;
65 	uae_u32 logfacility;
66 	uae_u32 fdcallback;
67 
68     /* host-specific fields below */
69     uae_sem_t sem;		/* semaphore to notify the socket thread of work */
70     uae_thread_id thread;	/* socket thread */
71     int  sockabort[2];		/* pipe used to tell the thread to abort a select */
72     int action;
73     int s;			/* for accept */
74     uae_u32 name;		/* For gethostbyname */
75     uae_u32 a_addr;		/* gethostbyaddr, accept */
76     uae_u32 a_addrlen;		/* for gethostbyaddr, accept */
77     uae_u32 flags;
78     void *buf;
79     uae_u32 len;
80     uae_u32 to, tolen, from, fromlen;
81     int nfds;
82     uae_u32 sets [3];
83     uae_u32 timeout;
84     uae_u32 sigmp;
85 };
86 
87 #define LIBRARY_SIZEOF 36
88 
89 struct UAEBSDBase {
90     uae_u8 dummy[LIBRARY_SIZEOF];
91     struct socketbase *sb;
92     uae_u8 scratchbuf[SCRATCHBUFSIZE];
93 };
94 
95 /* socket flags */
96 /* socket events to report */
97 #define REP_ACCEPT	 0x01	/* there is a connection to accept() */
98 #define REP_CONNECT	 0x02	/* connect() completed */
99 #define REP_OOB		 0x04	/* socket has out-of-band data */
100 #define REP_READ	 0x08	/* socket is readable */
101 #define REP_WRITE	 0x10	/* socket is writeable */
102 #define REP_ERROR	 0x20	/* asynchronous error on socket */
103 #define REP_CLOSE	 0x40	/* connection closed (graceful or not) */
104 #define REP_ALL      0x7f
105 /* socket events that occurred */
106 #define SET_ACCEPT	 0x0100	/* there is a connection to accept() */
107 #define SET_CONNECT	 0x0200	/* connect() completed */
108 #define SET_OOB		 0x0400	/* socket has out-of-band data */
109 #define SET_READ	 0x0800	/* socket is readable */
110 #define SET_WRITE	 0x1000	/* socket is writeable */
111 #define SET_ERROR	 0x2000	/* asynchronous error on socket */
112 #define SET_CLOSE	 0x4000	/* connection closed (graceful or not) */
113 #define SET_ALL      0x7f00
114 /* socket properties */
115 #define SF_BLOCKING 0x80000000
116 #define SF_BLOCKINGINPROGRESS 0x40000000
117 /* STBC_FDCALLBACK */
118 #define FDCB_FREE  0
119 #define FDCB_ALLOC 1
120 #define FDCB_CHECK 2
121 
122 uae_u32 addstr (uae_u32 *, const char *);
123 uae_u32 addmem (uae_u32 *, const char *, int len);
124 
125 char *strncpyah (char *, uae_u32, int);
126 char *strcpyah (char *, uae_u32);
127 uae_u32 strcpyha (uae_u32, const char *);
128 uae_u32 strncpyha (uae_u32, const char *, int);
129 
130 #define SB struct socketbase *sb
131 
132 typedef int SOCKET;
133 #define INVALID_SOCKET -1
134 
135 void bsdsocklib_seterrno (SB, int);
136 void bsdsocklib_setherrno (SB, int);
137 
138 void sockmsg (unsigned int, unsigned long, unsigned long);
139 void sockabort (SB);
140 
141 void addtosigqueue (SB, int);
142 void removefromsigqueue (SB);
143 void sigsockettasks (void);
144 void locksigqueue (void);
145 void unlocksigqueue (void);
146 
147 bool checksd(SB, int sd);
148 void setsd(SB, int , SOCKET_TYPE);
149 int getsd (SB, SOCKET_TYPE);
150 SOCKET_TYPE getsock (SB, int);
151 void releasesock (SB, int);
152 
153 void waitsig (TrapContext *context, SB);
154 void cancelsig (TrapContext *context, SB);
155 
156 int host_sbinit (TrapContext*, SB);
157 void host_sbcleanup (SB);
158 void host_sbreset (void);
159 void host_closesocketquick (SOCKET_TYPE);
160 
161 int host_dup2socket (SB, int, int);
162 int host_socket (SB, int, int, int);
163 uae_u32 host_bind (SB, uae_u32, uae_u32, uae_u32);
164 uae_u32 host_listen (SB, uae_u32, uae_u32);
165 void host_accept (TrapContext *, SB, uae_u32, uae_u32, uae_u32);
166 void host_sendto (TrapContext *, SB, uae_u32, uae_u32, uae_u32, uae_u32, uae_u32, uae_u32);
167 void host_recvfrom (TrapContext *, SB, uae_u32, uae_u32, uae_u32, uae_u32, uae_u32, uae_u32);
168 uae_u32 host_shutdown (SB, uae_u32, uae_u32);
169 void host_setsockopt (SB, uae_u32, uae_u32, uae_u32, uae_u32, uae_u32);
170 uae_u32 host_getsockopt (SB, uae_u32, uae_u32, uae_u32, uae_u32, uae_u32);
171 uae_u32 host_getsockname (SB, uae_u32, uae_u32, uae_u32);
172 uae_u32 host_getpeername (SB, uae_u32, uae_u32, uae_u32);
173 uae_u32 host_IoctlSocket (TrapContext *, SB, uae_u32, uae_u32, uae_u32);
174 uae_u32 host_shutdown (SB, uae_u32, uae_u32);
175 int host_CloseSocket (TrapContext *, SB, int);
176 void host_connect (TrapContext *, SB, uae_u32, uae_u32, uae_u32);
177 void host_WaitSelect (TrapContext *, SB, uae_u32, uae_u32, uae_u32, uae_u32, uae_u32, uae_u32);
178 uae_u32 host_SetSocketSignals (void);
179 uae_u32 host_getdtablesize (void);
180 uae_u32 host_ObtainSocket (void);
181 uae_u32 host_ReleaseSocket (void);
182 uae_u32 host_ReleaseCopyOfSocket (void);
183 uae_u32 host_Inet_NtoA (TrapContext *context, SB, uae_u32);
184 uae_u32 host_inet_addr (uae_u32);
185 uae_u32 host_Inet_LnaOf (void);
186 uae_u32 host_Inet_NetOf (void);
187 uae_u32 host_Inet_MakeAddr (void);
188 uae_u32 host_inet_network (void);
189 void host_gethostbynameaddr (TrapContext *, SB, uae_u32, uae_u32, long);
190 uae_u32 host_getnetbyname (void);
191 uae_u32 host_getnetbyaddr (void);
192 void host_getservbynameport (TrapContext *, SB, uae_u32, uae_u32, uae_u32);
193 void host_getprotobyname (TrapContext *, SB, uae_u32);
194 void host_getprotobynumber (TrapContext *, SB, uae_u32);
195 uae_u32 host_vsyslog (void);
196 uae_u32 host_Dup2Socket (void);
197 uae_u32 host_gethostname (uae_u32, uae_u32);
198 
199 uaecptr bsdlib_startup (uaecptr);
200 void bsdlib_install (void);
201 void bsdlib_reset (void);
202 
203 void bsdsock_fake_int_handler(void);
204 
205 #endif // SRC_INCLUDE_BSDSOCKET_H_INCLUDED
206