ObjFW
OFSocket+Private.h
1 /*
2  * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
3  *
4  * All rights reserved.
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License version 3.0 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13  * version 3.0 for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * version 3.0 along with this program. If not, see
17  * <https://www.gnu.org/licenses/>.
18  */
19 
20 #include "config.h"
21 
22 #include "unistd_wrapper.h"
23 
24 #ifdef HAVE_ARPA_INET_H
25 # include <arpa/inet.h>
26 #endif
27 #ifdef HAVE_NETDB_H
28 # include <netdb.h>
29 #endif
30 
31 #import "OFSocket.h"
32 
33 #ifndef INADDR_NONE
34 # define INADDR_NONE ((in_addr_t)-1)
35 #endif
36 
37 #ifndef SOMAXCONN
38 /*
39  * Use 16 as everything > 17 fails on Nintendo 3DS and 16 is a less arbitrary
40  * number than 17.
41  */
42 # define SOMAXCONN 16
43 #endif
44 
45 #ifndef SOCK_CLOEXEC
46 # define SOCK_CLOEXEC 0
47 #endif
48 
49 #if defined(OF_AMIGAOS)
50 # ifdef OF_MORPHOS
51 # include <proto/socket.h>
52 # else
53 # include <proto/bsdsocket.h>
54 # endif
55 # include <sys/filio.h>
56 # define closesocket(sock) CloseSocket(sock)
57 # define ioctlsocket(fd, req, arg) IoctlSocket(fd, req, arg)
58 # define hstrerror(err) "unknown (no hstrerror)"
59 # define SOCKET_ERROR -1
60 # if defined(OF_HAVE_THREADS) && !defined(OF_MORPHOS)
61 # define SocketBase ((struct Library *)OFTLSKeyGet(_OFSocketBaseKey))
62 # ifdef OF_AMIGAOS4
63 # define ISocket ((struct SocketIFace *)OFTLSKeyGet(_OFSocketInterfaceKey))
64 # endif
65 # endif
66 # ifdef OF_MORPHOS
67 typedef uint32_t in_addr_t;
68 # endif
69 #elif !defined(OF_WINDOWS) && !defined(OF_WII)
70 # define closesocket(sock) close(sock)
71 #endif
72 
73 #ifdef OF_WII
74 # define accept(sock, addr, addrlen) net_accept(sock, addr, addrlen)
75 # define bind(sock, addr, addrlen) net_bind(sock, addr, addrlen)
76 # define closesocket(sock) net_close(sock)
77 # define connect(sock, addr, addrlen) \
78  net_connect(sock, (struct sockaddr *)addr, addrlen)
79 # define fcntl(fd, cmd, flags) net_fcntl(fd, cmd, flags)
80 # define h_errno 0
81 # define hstrerror(err) "unknown (no hstrerror)"
82 # define listen(sock, backlog) net_listen(sock, backlog)
83 # define poll(fds, nfds, timeout) net_poll(fds, nfds, timeout)
84 # define recv(sock, buf, len, flags) net_recv(sock, buf, len, flags)
85 # define recvfrom(sock, buf, len, flags, addr, addrlen) \
86  net_recvfrom(sock, buf, len, flags, addr, addrlen)
87 # define select(nfds, readfds, writefds, errorfds, timeout) \
88  net_select(nfds, readfds, writefds, errorfds, timeout)
89 # define send(sock, buf, len, flags) net_send(sock, buf, len, flags)
90 # define sendto(sock, buf, len, flags, addr, addrlen) \
91  net_sendto(sock, buf, len, flags, (struct sockaddr *)(addr), addrlen)
92 # define setsockopt(sock, level, name, value, len) \
93  net_setsockopt(sock, level, name, value, len)
94 # define socket(domain, type, proto) net_socket(domain, type, proto)
95 typedef u32 in_addr_t;
96 typedef u32 nfds_t;
97 #endif
98 
99 OF_ASSUME_NONNULL_BEGIN
100 
101 #ifdef __cplusplus
102 extern "C" {
103 #endif
104 extern bool _OFSocketInit(void) OF_VISIBILITY_HIDDEN;
105 #if defined(OF_HAVE_THREADS) && defined(OF_AMIGAOS) && !defined(OF_MORPHOS)
106 extern void _OFSocketDeinit(void) OF_VISIBILITY_HIDDEN;
107 #endif
108 extern int _OFSocketErrNo(void) OF_VISIBILITY_HIDDEN;
109 #if !defined(OF_WII) && !defined(OF_NINTENDO_3DS)
110 extern int _OFGetSockName(OFSocketHandle sock, struct sockaddr *restrict addr,
111  socklen_t *restrict addrLen) OF_VISIBILITY_HIDDEN;
112 #endif
113 
114 #if defined(OF_HAVE_THREADS) && defined(OF_AMIGAOS) && !defined(OF_MORPHOS)
115 extern OFTLSKey _OFSocketBaseKey OF_VISIBILITY_HIDDEN;
116 # ifdef OF_AMIGAOS4
117 extern OFTLSKey _OFSocketInterfaceKey OF_VISIBILITY_HIDDEN;
118 # endif
119 #endif
120 #ifdef __cplusplus
121 }
122 #endif
123 
124 OF_ASSUME_NONNULL_END