1 | /* $Id: tcpip.h 3809 2014-02-16 20:20:59Z bird $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * Semi Private TCP/IP header.
|
---|
5 | *
|
---|
6 | * Copyright (c) 2003-2004 knut st. osmundsen <bird-srcspam@anduin.net>
|
---|
7 | *
|
---|
8 | *
|
---|
9 | * This file is part of Innotek LIBC.
|
---|
10 | *
|
---|
11 | * Innotek LIBC is free software; you can redistribute it and/or modify
|
---|
12 | * it under the terms of the GNU General Public License as published by
|
---|
13 | * the Free Software Foundation; either version 2 of the License, or
|
---|
14 | * (at your option) any later version.
|
---|
15 | *
|
---|
16 | * Innotek LIBC is distributed in the hope that it will be useful,
|
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | * GNU General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with Innotek LIBC; if not, write to the Free Software
|
---|
23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
24 | *
|
---|
25 | */
|
---|
26 |
|
---|
27 | #ifndef __InnoTekLIBC_tcpip_h__
|
---|
28 | #define __InnoTekLIBC_tcpip_h__
|
---|
29 |
|
---|
30 | #include <errno.h>
|
---|
31 | #include <sys/socket.h>
|
---|
32 | #include <emx/io.h>
|
---|
33 |
|
---|
34 |
|
---|
35 | /** @group grp_libc_tcpip LIBC TCP/IP Backend
|
---|
36 | * @{
|
---|
37 | */
|
---|
38 |
|
---|
39 | /*******************************************************************************
|
---|
40 | * Defined Constants And Macros *
|
---|
41 | *******************************************************************************/
|
---|
42 | /** @def TCPNAME44
|
---|
43 | * Name construction macro for BSD 4.4 specific function.
|
---|
44 | */
|
---|
45 | #define TCPNAME44(a) __libc_tcpip##a##44
|
---|
46 | /** @def TCPNAME43
|
---|
47 | * Name construction macro for BSD 4.3 specific function.
|
---|
48 | */
|
---|
49 | #define TCPNAME43(a) __libc_tcpip##a##43
|
---|
50 | /** @def TCPNAME
|
---|
51 | * Name constructure macro for the current BSD mode.
|
---|
52 | * TCPV40HDRS is used to figure out which mode we're in.
|
---|
53 | */
|
---|
54 | #ifdef TCPV40HDRS
|
---|
55 | # define TCPNAME(a) TCPNAME43(a)
|
---|
56 | #else
|
---|
57 | # define TCPNAME(a) TCPNAME44(a)
|
---|
58 | #endif
|
---|
59 |
|
---|
60 | /** @def TCPNAMEG44
|
---|
61 | * Name construction macro for BSD 4.4 specific function.
|
---|
62 | */
|
---|
63 | #define TCPNAMEG44(a) __libc_Tcpip##a##44
|
---|
64 | /** @def TCPNAMEG43
|
---|
65 | * Name construction macro for BSD 4.3 specific function.
|
---|
66 | */
|
---|
67 | #define TCPNAMEG43(a) __libc_Tcpip##a##43
|
---|
68 | /** @def TCPNAMEG
|
---|
69 | * Name constructure macro for the current BSD mode.
|
---|
70 | * TCPV40HDRS is used to figure out which mode we're in.
|
---|
71 | */
|
---|
72 | #ifdef TCPV40HDRS
|
---|
73 | # define TCPNAMEG(a) TCPNAMEG43(a)
|
---|
74 | #else
|
---|
75 | # define TCPNAMEG(a) TCPNAMEG44(a)
|
---|
76 | #endif
|
---|
77 |
|
---|
78 |
|
---|
79 | /** The offset the OS/2 TCP/IP errno values are skewed compared to the
|
---|
80 | * LIBC errno values. */
|
---|
81 | #define EOS2_TCPIP_OFFSET 10000
|
---|
82 |
|
---|
83 | /** Get the low word of the ioctl request number.
|
---|
84 | * Used to support ioctl request numbers from old and new _IOC macros.
|
---|
85 | */
|
---|
86 | #define __IOCLW(a) ((unsigned short)(a))
|
---|
87 |
|
---|
88 |
|
---|
89 | #ifndef TCPV40HDRS
|
---|
90 | #define V5_FD_SETSIZE 64
|
---|
91 | #define V5_FD_SET(fd, set) do { \
|
---|
92 | /* if (((v5_fd_set *)(set))->fd_count < V5_FD_SETSIZE) - calcsize fixes this */ \
|
---|
93 | ((v5_fd_set *)(set))->fd_array[((v5_fd_set *)(set))->fd_count++]=fd;\
|
---|
94 | } while(0)
|
---|
95 | #define V5_FD_ISSET(fd, set) v5_isset(fd, set)
|
---|
96 | #endif
|
---|
97 |
|
---|
98 |
|
---|
99 |
|
---|
100 | /*******************************************************************************
|
---|
101 | * Structures and Typedefs *
|
---|
102 | *******************************************************************************/
|
---|
103 | /** Socket handle.*/
|
---|
104 | typedef struct __libc_SocketHandle
|
---|
105 | {
|
---|
106 | /** the common fh core. */
|
---|
107 | LIBCFH core;
|
---|
108 |
|
---|
109 | /** OS/2 socket number. */
|
---|
110 | int iSocket;
|
---|
111 | /** Pointer to next socket handle in the chain. */
|
---|
112 | struct __libc_SocketHandle * volatile pNext;
|
---|
113 | /** Pointer to previous socket handle in the chain. */
|
---|
114 | struct __libc_SocketHandle * volatile pPrev;
|
---|
115 | } LIBCSOCKETFH, *PLIBCSOCKETFH;
|
---|
116 |
|
---|
117 |
|
---|
118 | #ifndef TCPV40HDRS
|
---|
119 |
|
---|
120 | #pragma pack(4)
|
---|
121 | /** OS/2 oddities from the BSD 4.4 stack. */
|
---|
122 | typedef struct v5_fd_set {
|
---|
123 | u_short fd_count; /* how many are SET? */
|
---|
124 | int fd_array[V5_FD_SETSIZE];/* an array of SOCKETs */
|
---|
125 | } v5_fd_set;
|
---|
126 | #pragma pack()
|
---|
127 |
|
---|
128 | /** internal helper */
|
---|
129 | static inline int v5_isset(int fd, const struct v5_fd_set *set)
|
---|
130 | {
|
---|
131 | const int *pfd = &set->fd_array[0];
|
---|
132 | int c = set->fd_count;
|
---|
133 | while (c > 0)
|
---|
134 | {
|
---|
135 | if (*pfd == fd)
|
---|
136 | return 1;
|
---|
137 | pfd++;
|
---|
138 | c--;
|
---|
139 | }
|
---|
140 | return 0;
|
---|
141 | }
|
---|
142 |
|
---|
143 | #endif
|
---|
144 |
|
---|
145 |
|
---|
146 | /*******************************************************************************
|
---|
147 | * Internal Functions *
|
---|
148 | *******************************************************************************/
|
---|
149 | /** @defgroup libsocket_renamed Renamed Imports.
|
---|
150 | * @{ */
|
---|
151 | int _System __libsocket_accept(int, struct sockaddr *, int *);
|
---|
152 | int _System __libsocket_bind(int, const struct sockaddr *, int);
|
---|
153 | int _System __libsocket_connect(int, const struct sockaddr *, int);
|
---|
154 | int _System __libsocket_getpeername(int, struct sockaddr *, int *);
|
---|
155 | int _System __libsocket_getsockname(int, struct sockaddr *, int *);
|
---|
156 | int _System __libsocket_getsockopt(int, int, int, void *, int *);
|
---|
157 | int _System __libsocket_ioctl(int, int, char *);
|
---|
158 | int _System __libsocket_listen(int, int);
|
---|
159 | int _System __libsocket_os2_ioctl(int, unsigned long, char *, int);
|
---|
160 | int _System __libsocket_os2_select(int *, int, int, int, long);
|
---|
161 | int _System __libsocket_recv(int, void *, int, int);
|
---|
162 | int _System __libsocket_recvfrom(int, void *, int, int, struct sockaddr *, int *);
|
---|
163 | int _System __libsocket_recvmsg(int, struct msghdr *, int);
|
---|
164 | int _System __libsocket_send(int, const void *, int, int);
|
---|
165 | int _System __libsocket_sendmsg(int, const struct msghdr *, int);
|
---|
166 | int _System __libsocket_sendto(int, const void *, int, int, const struct sockaddr *, int);
|
---|
167 | int _System __libsocket_setsockopt(int, int, int, const void *, int);
|
---|
168 | int _System __libsocket_shutdown(int, int);
|
---|
169 | int _System __libsocket_sock_errno(void);
|
---|
170 | int _System __libsocket_socket(int, int, int);
|
---|
171 | int _System __libsocket_socketpair(int, int, int, int *);
|
---|
172 | int _System __libsocket_soclose(int);
|
---|
173 | int _System __libsocket_accept_and_recv(long, long*, struct sockaddr *, long*, struct sockaddr*, long*, caddr_t, size_t);
|
---|
174 | void _System __libsocket_addsockettolist(int);
|
---|
175 | int _System __libsocket_removesocketfromlist(int);
|
---|
176 | ssize_t _System __libsocket_so_readv(int, struct iovec *, int);
|
---|
177 | ssize_t _System __libsocket_so_writev(int, struct iovec *, int);
|
---|
178 | int _System __libsocket_so_cancel(int);
|
---|
179 | int _System __libsocket_soabort(int);
|
---|
180 | int _System __libsocket_Raccept(int, struct sockaddr *, int *);
|
---|
181 | struct sockaddr_in;
|
---|
182 | int _System __libsocket_tcpip4_Rbind(int, struct sockaddr_in *, int, struct sockaddr_in *);
|
---|
183 | int _System __libsocket_Rbind(int, struct sockaddr *, int, struct sockaddr *);
|
---|
184 | int _System __libsocket_Rconnect(int, const struct sockaddr *, int);
|
---|
185 | int _System __libsocket_Rgetsockname(int, struct sockaddr *, int *);
|
---|
186 | int _System __libsocket_Rlisten(int, int);
|
---|
187 | #ifndef TCPV40HDRS
|
---|
188 | ssize_t _System __libsocket_send_file(int *, struct sf_parms *, int );
|
---|
189 | int _System __libsocket_socketpair(int af, int type, int flags, int *osfd);
|
---|
190 | #endif
|
---|
191 | char * _System __libsocket_sock_strerror(int);
|
---|
192 | #ifdef TCPV40HDRS
|
---|
193 | int _System __libsocket_bsdselect(int, fd_set *, fd_set *, fd_set *, struct timeval *);
|
---|
194 | #else
|
---|
195 | int _System __libsocket_bsdselect(int, v5_fd_set *, v5_fd_set *, v5_fd_set *, struct timeval *);
|
---|
196 | #endif
|
---|
197 | void _System __libsocket_set_errno(int);
|
---|
198 | /** @} */
|
---|
199 |
|
---|
200 |
|
---|
201 | /** @defgroup grp_libc_tcpip_internal Internal helpers.
|
---|
202 | * @{ */
|
---|
203 |
|
---|
204 | /**
|
---|
205 | * BSD Select worker.
|
---|
206 | */
|
---|
207 | int TCPNAMEG(bsdselect)(int nfds, struct fd_set *readfds, struct fd_set *writefds, struct fd_set *exceptfds, struct timeval *tv);
|
---|
208 |
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * Allocates file handle for a BSD 4.4 socket.
|
---|
212 | *
|
---|
213 | * @returns Pointer to socket handle on success.
|
---|
214 | * @returns NULL and errno on failure.
|
---|
215 | * @param iSocket The socket to allocate handle for.
|
---|
216 | * @param pfh Where to store the filename.
|
---|
217 | */
|
---|
218 | PLIBCSOCKETFH TCPNAMEG44(AllocFH)(int iSocket, int *pfh);
|
---|
219 | /**
|
---|
220 | * Allocates file handle for a BSD 4.3 socket.
|
---|
221 | *
|
---|
222 | * @returns Pointer to socket handle on success.
|
---|
223 | * @returns NULL and errno on failure.
|
---|
224 | * @param iSocket The socket to allocate handle for.
|
---|
225 | * @param pfh Where to store the filename.
|
---|
226 | */
|
---|
227 | PLIBCSOCKETFH TCPNAMEG43(AllocFH)(int iSocket, int *pfh);
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * Allocates file handle for a socket - extended version.
|
---|
231 | *
|
---|
232 | * The socket is not removed from the socket list when calling this
|
---|
233 | * allocation function. This is because it's used by the inherit
|
---|
234 | * processor where such actions are unnecessary.
|
---|
235 | *
|
---|
236 | * @returns 0 on success.
|
---|
237 | * @returns Negative error code (errno.h) on failure.
|
---|
238 | * @param fh The requested file handle.
|
---|
239 | * If -1 any file handle can be used.
|
---|
240 | * @param iSocket The socket to allocate handle for.
|
---|
241 | * @param fFlags LIBC file handle flags.
|
---|
242 | * @param fNew If set we handle the socket as brand new setting the global
|
---|
243 | * reference counter to 1 and remove it from the TCPIP cleanup
|
---|
244 | * list for the current process.
|
---|
245 | * If clear we only increment the global reference counter.
|
---|
246 | * @param pfh Where to store the filename.
|
---|
247 | * @param ppFHSocket Where to store the allocated socket handle. Optional.
|
---|
248 | */
|
---|
249 | int TCPNAMEG44(AllocFHEx)(int fh, int iSocket, unsigned fFlags, int fNew, int *pfh, PLIBCSOCKETFH *ppFHSocket);
|
---|
250 | /**
|
---|
251 | * Allocates file handle for a socket - extended version.
|
---|
252 | *
|
---|
253 | * The socket is not removed from the socket list when calling this
|
---|
254 | * allocation function. This is because it's used by the inherit
|
---|
255 | * processor where such actions are unnecessary.
|
---|
256 | *
|
---|
257 | * @returns 0 on success.
|
---|
258 | * @returns Negative error code (errno.h) on failure.
|
---|
259 | * @param fh The requested file handle.
|
---|
260 | * If -1 any file handle can be used.
|
---|
261 | * @param iSocket The socket to allocate handle for.
|
---|
262 | * @param fFlags LIBC file handle flags.
|
---|
263 | * @param fNew If set we handle the socket as brand new setting the global
|
---|
264 | * reference counter to 1 and remove it from the TCPIP cleanup
|
---|
265 | * list for the current process.
|
---|
266 | * If clear we only increment the global reference counter.
|
---|
267 | * @param pfh Where to store the filename.
|
---|
268 | * @param ppFHSocket Where to store the allocated socket handle. Optional.
|
---|
269 | */
|
---|
270 | int TCPNAMEG43(AllocFHEx)(int fh, int iSocket, unsigned fFlags, int fNew, int *pfh, PLIBCSOCKETFH *ppFHSocket);
|
---|
271 |
|
---|
272 |
|
---|
273 | #if defined(OS2_INCLUDED) || defined(_OS2EMX_H)
|
---|
274 | /**
|
---|
275 | * Loads a given TCPIP dll during fork.
|
---|
276 | * @returns 0 on success.
|
---|
277 | * @returns negative errno on failure.
|
---|
278 | * @param hmodLoad The handle of the module we're loading.
|
---|
279 | * @param pszDll The DLL name.
|
---|
280 | */
|
---|
281 | int __libc_tcpipForkLoadModule(HMODULE hmodLoad, const char *pszDll);
|
---|
282 | #endif
|
---|
283 |
|
---|
284 | #ifndef NO_TCPIP_INLINE
|
---|
285 |
|
---|
286 | #ifdef IN_INNOTEK_LIBC
|
---|
287 | int TCPNAME(imp_sock_errno)(void);
|
---|
288 | void TCPNAME(imp_set_errno)(int);
|
---|
289 | #endif
|
---|
290 |
|
---|
291 | /**
|
---|
292 | * Sets the LIBC and socket errno variables to a given error number.
|
---|
293 | */
|
---|
294 | static inline void __libc_TcpipSetErrno(int err)
|
---|
295 | {
|
---|
296 | errno = err;
|
---|
297 | #ifdef IN_INNOTEK_LIBC
|
---|
298 | TCPNAME(imp_set_errno)(err + EOS2_TCPIP_OFFSET);
|
---|
299 | #else
|
---|
300 | __libsocket_set_errno(err + EOS2_TCPIP_OFFSET);
|
---|
301 | #endif
|
---|
302 | }
|
---|
303 |
|
---|
304 | /**
|
---|
305 | * Updates the LIBC errno with the latest socket error.
|
---|
306 | */
|
---|
307 | static inline void __libc_TcpipUpdateErrno(void)
|
---|
308 | {
|
---|
309 | #ifdef IN_INNOTEK_LIBC
|
---|
310 | int err = TCPNAME(imp_sock_errno)();
|
---|
311 | #else
|
---|
312 | int err = __libsocket_sock_errno();
|
---|
313 | #endif
|
---|
314 | if (err >= EOS2_TCPIP_OFFSET && err < EOS2_TCPIP_OFFSET + 1000)
|
---|
315 | errno = err - EOS2_TCPIP_OFFSET;
|
---|
316 | }
|
---|
317 |
|
---|
318 | /**
|
---|
319 | * Updates the socket errno with the latest LIBC error.
|
---|
320 | */
|
---|
321 | static inline void __libc_TcpipSetSocketErrno(void)
|
---|
322 | {
|
---|
323 | #ifdef IN_INNOTEK_LIBC
|
---|
324 | TCPNAME(imp_set_errno)(errno + EOS2_TCPIP_OFFSET);
|
---|
325 | #else
|
---|
326 | __libsocket_set_errno(errno + EOS2_TCPIP_OFFSET);
|
---|
327 | #endif
|
---|
328 | }
|
---|
329 |
|
---|
330 | /**
|
---|
331 | * Gets the socket errno translating it to a LIBC errno.
|
---|
332 | */
|
---|
333 | static inline int __libc_TcpipGetSocketErrno(void)
|
---|
334 | {
|
---|
335 | #ifdef IN_INNOTEK_LIBC
|
---|
336 | int err = TCPNAME(imp_sock_errno)();
|
---|
337 | #else
|
---|
338 | int err = __libsocket_sock_errno();
|
---|
339 | #endif
|
---|
340 | if (err >= EOS2_TCPIP_OFFSET && err < EOS2_TCPIP_OFFSET + 1000)
|
---|
341 | return err - EOS2_TCPIP_OFFSET;
|
---|
342 | return EDOOFUS;
|
---|
343 | }
|
---|
344 |
|
---|
345 |
|
---|
346 | /**
|
---|
347 | * Retrieve the socket handle structure for a given handle.
|
---|
348 | *
|
---|
349 | * @returns Pointer to socket handle structure on success.
|
---|
350 | * @returns NULL on failure with errno set to the appropriate value.
|
---|
351 | * @param iSocket Socket handle number.
|
---|
352 | */
|
---|
353 | static inline PLIBCSOCKETFH __libc_TcpipFH(int iSocket)
|
---|
354 | {
|
---|
355 | PLIBCSOCKETFH pFHSocket = (PLIBCSOCKETFH)__libc_FH(iSocket);
|
---|
356 | if (pFHSocket)
|
---|
357 | {
|
---|
358 | if ((pFHSocket->core.fFlags & __LIBC_FH_TYPEMASK) == F_SOCKET)
|
---|
359 | return pFHSocket;
|
---|
360 | __libc_TcpipSetErrno(ENOTSOCK);
|
---|
361 | }
|
---|
362 | else
|
---|
363 | __libc_TcpipSetErrno(EBADF);
|
---|
364 | return NULL;
|
---|
365 | }
|
---|
366 |
|
---|
367 | #endif /* !NO_TCPIP_INLINE */
|
---|
368 |
|
---|
369 | /** @} */
|
---|
370 |
|
---|
371 |
|
---|
372 | /** @} */
|
---|
373 |
|
---|
374 | #endif
|
---|