ObjFW
OFSocket.h
Go to the documentation of this file.
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 "objfw-defs.h"
21 
22 #ifndef OF_HAVE_SOCKETS
23 # error No sockets available!
24 #endif
25 
26 #include <stdbool.h>
27 
28 #import "OFString.h"
29 #if defined(OF_HAVE_THREADS) && defined(OF_AMIGAOS)
30 # import "OFTLSKey.h"
31 #endif
32 
33 #ifdef OF_HAVE_SYS_SOCKET_H
34 # include <sys/socket.h>
35 #endif
36 #ifdef OF_HAVE_NETINET_IN_H
37 # include <netinet/in.h>
38 #endif
39 #ifdef OF_HAVE_NETINET_TCP_H
40 # include <netinet/tcp.h>
41 #endif
42 #ifdef OF_HAVE_NETINET_SCTP_H
43 # include <netinet/sctp.h>
44 #endif
45 #ifdef OF_HAVE_SYS_UN_H
46 # include <sys/un.h>
47 #endif
48 #ifdef OF_HAVE_AFUNIX_H
49 # include <afunix.h>
50 #endif
51 #ifdef OF_HAVE_NETIPX_IPX_H
52 # include <netipx/ipx.h>
53 #endif
54 #if defined(OF_HAVE_NETAT_APPLETALK_H)
55 # include <netat/appletalk.h>
56 #elif defined(OF_HAVE_NETATALK_AT_H)
57 # include <netatalk/at.h>
58 #endif
59 
60 #ifdef OF_WINDOWS
61 # include <windows.h>
62 # include <ws2tcpip.h>
63 # ifdef OF_HAVE_IPX
64 # include <wsipx.h>
65 # endif
66 # ifdef OF_HAVE_APPLETALK
67 # include <atalkwsh.h>
68 # endif
69 #endif
70 
71 #ifdef OF_WII
72 # include <network.h>
73 #endif
74 
75 #ifdef OF_PSP
76 # include <stdint.h>
77 #endif
78 
79 #import "macros.h"
80 
81 OF_ASSUME_NONNULL_BEGIN
82 
85 #ifndef OF_WINDOWS
86 typedef int OFSocketHandle;
87 static const OFSocketHandle OFInvalidSocketHandle = -1;
88 #else
89 typedef SOCKET OFSocketHandle;
90 static const OFSocketHandle OFInvalidSocketHandle = INVALID_SOCKET;
91 #endif
92 
93 #ifdef OF_WINDOWS
94 typedef short sa_family_t;
95 #endif
96 
97 #ifdef OF_WII
98 typedef u8 sa_family_t;
99 #endif
100 
101 #ifdef OF_MORPHOS
102 typedef long socklen_t;
103 typedef u_char sa_family_t;
104 typedef u_short in_port_t;
105 #endif
106 
110 typedef enum {
126 
127 #ifndef OF_HAVE_IPV6
128 struct sockaddr_in6 {
129  sa_family_t sin6_family;
130  in_port_t sin6_port;
131  uint32_t sin6_flowinfo;
132  struct in6_addr {
133  uint8_t s6_addr[16];
134  } sin6_addr;
135  uint32_t sin6_scope_id;
136 };
137 #endif
138 
139 #if !defined(OF_HAVE_UNIX_SOCKETS) && !defined(OF_MORPHOS) && !defined(OF_MINT)
140 struct sockaddr_un {
141  sa_family_t sun_family;
142  char sun_path[108];
143 };
144 #endif
145 
146 #ifndef IPX_NODE_LEN
147 # define IPX_NODE_LEN 6
148 #endif
149 #if !defined(OF_HAVE_IPX)
150 struct sockaddr_ipx {
151  sa_family_t sipx_family;
152  uint32_t sipx_network;
153  unsigned char sipx_node[IPX_NODE_LEN];
154  uint16_t sipx_port;
155  uint8_t sipx_type;
156 };
157 #elif defined(OF_WINDOWS)
158 # define IPX_NODE_LEN 6
159 # define sipx_family sa_family
160 # define sipx_network sa_netnum
161 # define sipx_node sa_nodenum
162 # define sipx_port sa_socket
163 #elif defined(OF_FREEBSD)
164 # define sipx_network sipx_addr.x_net.c_net
165 # define sipx_node sipx_addr.x_host.c_host
166 #endif
167 
168 #ifndef OF_HAVE_APPLETALK
169 struct sockaddr_at {
170  sa_family_t sat_family;
171  uint8_t sat_port;
172  uint16_t sat_net;
173  uint8_t sat_node;
174 };
175 #else
176 # ifdef OF_WINDOWS
177 # define sat_port sat_socket
178 # else
179 # define sat_net sat_addr.s_net
180 # define sat_node sat_addr.s_node
181 # endif
182 #endif
183 
189 typedef struct OF_BOXABLE OFSocketAddress {
190  OFSocketAddressFamily family;
191  /*
192  * We can't use struct sockaddr as it can contain variable length
193  * arrays.
194  */
195  union {
196  struct sockaddr_in in;
197  struct sockaddr_in6 in6;
198  struct sockaddr_un un;
199  struct sockaddr_ipx ipx;
200  struct sockaddr_at at;
201 #ifdef OF_HAVE_SOCKADDR_STORAGE
202  /*
203  * Required to make the ABI stable in case we want to add more
204  * address types later.
205  */
206  struct sockaddr_storage storage;
207 #endif
208  } sockaddr;
209  socklen_t length;
211 
212 #ifdef __cplusplus
213 extern "C" {
214 #endif
215 
224 extern OFSocketAddress OFSocketAddressParseIP(OFString *IP, uint16_t port);
225 
234 extern OFSocketAddress OFSocketAddressParseIPv4(OFString *IP, uint16_t port);
235 
244 extern OFSocketAddress OFSocketAddressParseIPv6(OFString *IP, uint16_t port);
245 
253 
262 extern OFSocketAddress OFSocketAddressMakeIPX(uint32_t network,
263  const unsigned char node[_Nonnull IPX_NODE_LEN], uint16_t port);
264 
274 extern OFSocketAddress OFSocketAddressMakeAppleTalk(uint16_t network,
275  uint8_t node, uint8_t port);
276 
284 extern bool OFSocketAddressEqual(const OFSocketAddress *_Nonnull address1,
285  const OFSocketAddress *_Nonnull address2);
286 
293 extern unsigned long OFSocketAddressHash(
294  const OFSocketAddress *_Nonnull address);
295 
302 extern OFString *_Nonnull OFSocketAddressString(
303  const OFSocketAddress *_Nonnull address);
304 
313 extern OFString *_Nonnull OFSocketAddressDescription(
314  const OFSocketAddress *_Nonnull address);
315 
322 extern void OFSocketAddressSetIPPort(OFSocketAddress *_Nonnull address,
323  uint16_t port);
324 
331 extern uint16_t OFSocketAddressIPPort(const OFSocketAddress *_Nonnull address);
332 
340  const OFSocketAddress *_Nonnull address);
341 
348 extern void OFSocketAddressSetIPXNetwork(OFSocketAddress *_Nonnull address,
349  uint32_t network);
350 
357 extern uint32_t OFSocketAddressIPXNetwork(
358  const OFSocketAddress *_Nonnull address);
359 
366 extern void OFSocketAddressSetIPXNode(OFSocketAddress *_Nonnull address,
367  const unsigned char node[_Nonnull IPX_NODE_LEN]);
368 
375 extern void OFSocketAddressGetIPXNode(const OFSocketAddress *_Nonnull address,
376  unsigned char node[_Nonnull IPX_NODE_LEN]);
377 
384 extern void OFSocketAddressSetIPXPort(OFSocketAddress *_Nonnull address,
385  uint16_t port);
386 
393 extern uint16_t OFSocketAddressIPXPort(const OFSocketAddress *_Nonnull address);
394 
402  OFSocketAddress *_Nonnull address, uint16_t network);
403 
410 extern uint16_t OFSocketAddressAppleTalkNetwork(
411  const OFSocketAddress *_Nonnull address);
412 
419 extern void OFSocketAddressSetAppleTalkNode(OFSocketAddress *_Nonnull address,
420  uint8_t node);
421 
428 extern uint8_t OFSocketAddressAppleTalkNode(
429  const OFSocketAddress *_Nonnull address);
430 
437 extern void OFSocketAddressSetAppleTalkPort(OFSocketAddress *_Nonnull address,
438  uint8_t port);
439 
446 extern uint8_t OFSocketAddressAppleTalkPort(
447  const OFSocketAddress *_Nonnull address);
448 #ifdef __cplusplus
449 }
450 #endif
451 
452 OF_ASSUME_NONNULL_END
void OFSocketAddressSetIPXPort(OFSocketAddress *address, uint16_t port)
Sets the IPX port of the specified OFSocketAddress.
Definition: OFSocket.m:1119
void OFSocketAddressSetAppleTalkNode(OFSocketAddress *address, uint8_t node)
Sets the AppleTalk node of the specified OFSocketAddress.
Definition: OFSocket.m:1163
OFString * OFSocketAddressDescription(const OFSocketAddress *address)
Returns a description for the specified OFSocketAddress.
Definition: OFSocket.m:998
Definition: OFSocket.h:118
Definition: OFSocket.h:112
Definition: OFSocket.h:114
OFSocketAddress OFSocketAddressParseIP(OFString *IP, uint16_t port)
Parses the specified IP (either v4 or v6) and port into an OFSocketAddress.
Definition: OFSocket.m:568
OFSocketAddress OFSocketAddressMakeIPX(uint32_t network, const unsigned char node[IPX_NODE_LEN], uint16_t port)
Creates an IPX address for the specified network, node and port.
Definition: OFSocket.m:614
void OFSocketAddressSetIPXNetwork(OFSocketAddress *address, uint32_t network)
Sets the IPX network of the specified OFSocketAddress.
Definition: OFSocket.m:1075
OFSocketAddress OFSocketAddressMakeUNIX(OFString *path)
Creates a UNIX socket address from the specified path.
Definition: OFSocket.m:582
uint16_t OFSocketAddressAppleTalkNetwork(const OFSocketAddress *address)
Returns the AppleTalk network of the specified OFSocketAddress.
Definition: OFSocket.m:1150
Definition: OFSocket.h:116
A struct which represents a host / port pair for a socket.
Definition: OFSocket.h:189
Definition: OFSocket.h:120
A class for handling strings.
Definition: OFString.h:142
uint8_t OFSocketAddressAppleTalkPort(const OFSocketAddress *address)
Returns the AppleTalk port of the specified OFSocketAddress.
Definition: OFSocket.m:1190
OFSocketAddress OFSocketAddressMakeAppleTalk(uint16_t network, uint8_t node, uint8_t port)
Creates an AppleTalk address for the specified network, node and port.
Definition: OFSocket.m:638
void OFSocketAddressSetIPPort(OFSocketAddress *address, uint16_t port)
Sets the IP port of the specified OFSocketAddress.
Definition: OFSocket.m:1027
void OFSocketAddressSetAppleTalkNetwork(OFSocketAddress *address, uint16_t network)
Sets the AppleTalk network of the specified OFSocketAddress.
Definition: OFSocket.m:1137
void OFSocketAddressSetIPXNode(OFSocketAddress *address, const unsigned char node[IPX_NODE_LEN])
Sets the IPX node of the specified OFSocketAddress.
Definition: OFSocket.m:1099
bool OFSocketAddressEqual(const OFSocketAddress *address1, const OFSocketAddress *address2)
Compares two OFSocketAddress for equality.
Definition: OFSocket.m:663
void OFSocketAddressSetAppleTalkPort(OFSocketAddress *address, uint8_t port)
Sets the AppleTalk port of the specified OFSocketAddress.
Definition: OFSocket.m:1181
OFSocketAddress OFSocketAddressParseIPv6(OFString *IP, uint16_t port)
Parses the specified IPv6 and port into an OFSocketAddress.
Definition: OFSocket.m:469
OFSocketAddress OFSocketAddressParseIPv4(OFString *IP, uint16_t port)
Parses the specified IPv4 and port into an OFSocketAddress.
Definition: OFSocket.m:368
uint16_t OFSocketAddressIPXPort(const OFSocketAddress *address)
Returns the IPX port of the specified OFSocketAddress.
Definition: OFSocket.m:1128
OFString * OFSocketAddressUNIXPath(const OFSocketAddress *address)
Gets the UNIX socket path of the specified OFSocketAddress.
Definition: OFSocket.m:1055
Definition: OFSocket.h:124
uint8_t OFSocketAddressAppleTalkNode(const OFSocketAddress *address)
Gets the AppleTalk node of the specified OFSocketAddress.
Definition: OFSocket.m:1172
uint32_t OFSocketAddressIPXNetwork(const OFSocketAddress *address)
Returns the IPX network of the specified OFSocketAddress.
Definition: OFSocket.m:1086
OFSocketAddressFamily
A socket address family.
Definition: OFSocket.h:110
OFString * OFSocketAddressString(const OFSocketAddress *address)
Converts the specified OFSocketAddress to a string.
Definition: OFSocket.m:979
void OFSocketAddressGetIPXNode(const OFSocketAddress *address, unsigned char node[IPX_NODE_LEN])
Gets the IPX node of the specified OFSocketAddress.
Definition: OFSocket.m:1109
uint16_t OFSocketAddressIPPort(const OFSocketAddress *address)
Returns the IP port of the specified OFSocketAddress.
Definition: OFSocket.m:1042
Definition: OFSocket.h:122
unsigned long OFSocketAddressHash(const OFSocketAddress *address)
Returns the hash for the specified OFSocketAddress.
Definition: OFSocket.m:770