ObjFW
OFSocket+Private.h
1 /*
2  * Copyright (c) 2008-2026 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 # ifdef OF_HAVE_THREADS
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 ioctlsocket(...) net_ioctl(__VA_ARGS__)
83 # define listen(sock, backlog) net_listen(sock, backlog)
84 # define poll(fds, nfds, timeout) net_poll(fds, nfds, timeout)
85 # define recv(sock, buf, len, flags) net_recv(sock, buf, len, flags)
86 # define recvfrom(sock, buf, len, flags, addr, addrlen) \
87  net_recvfrom(sock, buf, len, flags, addr, addrlen)
88 # define select(nfds, readfds, writefds, errorfds, timeout) \
89  net_select(nfds, readfds, writefds, errorfds, timeout)
90 # define send(sock, buf, len, flags) net_send(sock, buf, len, flags)
91 # define sendto(sock, buf, len, flags, addr, addrlen) \
92  net_sendto(sock, buf, len, flags, (struct sockaddr *)(addr), addrlen)
93 # define setsockopt(sock, level, name, value, len) \
94  net_setsockopt(sock, level, name, value, len)
95 # define socket(domain, type, proto) net_socket(domain, type, proto)
96 typedef u32 in_addr_t;
97 typedef u32 nfds_t;
98 #endif
99 
100 OF_ASSUME_NONNULL_BEGIN
101 
102 #ifdef __cplusplus
103 extern "C" {
104 #endif
105 extern bool _OFSocketInit(void) OF_VISIBILITY_INTERNAL;
106 #if defined(OF_HAVE_THREADS) && defined(OF_AMIGAOS)
107 extern void _OFSocketDeinit(void) OF_VISIBILITY_INTERNAL;
108 #endif
109 extern int _OFSocketErrNo(void) OF_VISIBILITY_INTERNAL;
110 #if !defined(OF_WII) && !defined(OF_NINTENDO_3DS)
111 extern int _OFGetSockName(OFSocketHandle sock, struct sockaddr *restrict addr,
112  socklen_t *restrict addrLen) OF_VISIBILITY_INTERNAL;
113 #endif
114 
115 #if defined(OF_HAVE_THREADS) && defined(OF_AMIGAOS)
116 extern OFTLSKey _OFSocketBaseKey OF_VISIBILITY_INTERNAL;
117 # ifdef OF_AMIGAOS4
118 extern OFTLSKey _OFSocketInterfaceKey OF_VISIBILITY_INTERNAL;
119 # endif
120 #endif
121 #ifdef __cplusplus
122 }
123 #endif
124 
125 OF_ASSUME_NONNULL_END