ObjFW
Loading...
Searching...
No Matches
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_SYS_UN_H
43# include <sys/un.h>
44#endif
45#ifdef OF_HAVE_AFUNIX_H
46# include <afunix.h>
47#endif
48#ifdef OF_HAVE_NETIPX_IPX_H
49# include <netipx/ipx.h>
50#endif
51#if defined(OF_HAVE_NETAT_APPLETALK_H)
52# include <netat/appletalk.h>
53#elif defined(OF_HAVE_NETATALK_AT_H)
54# include <netatalk/at.h>
55#endif
56
57#ifdef OF_WINDOWS
58# include <windows.h>
59# include <ws2tcpip.h>
60# ifdef OF_HAVE_IPX
61# include <wsipx.h>
62# endif
63# ifdef OF_HAVE_APPLETALK
64# include <atalkwsh.h>
65# endif
66#endif
67
68#ifdef OF_WII
69# include <network.h>
70#endif
71
72#ifdef OF_PSP
73# include <stdint.h>
74#endif
75
76#import "macros.h"
77
78OF_ASSUME_NONNULL_BEGIN
79
82#ifndef OF_WINDOWS
83typedef int OFSocketHandle;
84static const OFSocketHandle OFInvalidSocketHandle = -1;
85#else
86typedef SOCKET OFSocketHandle;
87static const OFSocketHandle OFInvalidSocketHandle = INVALID_SOCKET;
88#endif
89
90#ifdef OF_WINDOWS
91typedef short sa_family_t;
92#endif
93
94#ifdef OF_WII
95typedef u8 sa_family_t;
96#endif
97
98#ifdef OF_MORPHOS
99typedef long socklen_t;
100typedef u_char sa_family_t;
101typedef u_short in_port_t;
102#endif
103
123
124#ifndef OF_HAVE_IPV6
125struct sockaddr_in6 {
126 sa_family_t sin6_family;
127 in_port_t sin6_port;
128 uint32_t sin6_flowinfo;
129 struct in6_addr {
130 uint8_t s6_addr[16];
131 } sin6_addr;
132 uint32_t sin6_scope_id;
133};
134#endif
135
136#if !defined(OF_HAVE_UNIX_SOCKETS) && !defined(OF_MORPHOS) && !defined(OF_MINT)
137struct sockaddr_un {
138 sa_family_t sun_family;
139 char sun_path[108];
140};
141#endif
142
143#ifndef IPX_NODE_LEN
144# define IPX_NODE_LEN 6
145#endif
146#if !defined(OF_HAVE_IPX)
147struct sockaddr_ipx {
148 sa_family_t sipx_family;
149 uint32_t sipx_network;
150 unsigned char sipx_node[IPX_NODE_LEN];
151 uint16_t sipx_port;
152 uint8_t sipx_type;
153};
154#elif defined(OF_WINDOWS)
155# define IPX_NODE_LEN 6
156# define sipx_family sa_family
157# define sipx_network sa_netnum
158# define sipx_node sa_nodenum
159# define sipx_port sa_socket
160#elif defined(OF_FREEBSD)
161# define sipx_network sipx_addr.x_net.c_net
162# define sipx_node sipx_addr.x_host.c_host
163#endif
164
165#ifndef OF_HAVE_APPLETALK
166struct sockaddr_at {
167 sa_family_t sat_family;
168 uint8_t sat_port;
169 uint16_t sat_net;
170 uint8_t sat_node;
171};
172#else
173# ifdef OF_WINDOWS
174# define sat_port sat_socket
175# else
176# define sat_net sat_addr.s_net
177# define sat_node sat_addr.s_node
178# endif
179#endif
180
186typedef struct OF_BOXABLE OFSocketAddress {
188 /*
189 * We can't use struct sockaddr as it can contain variable length
190 * arrays.
191 */
192 union {
193 struct sockaddr_in in;
194 struct sockaddr_in6 in6;
195 struct sockaddr_un un;
196 struct sockaddr_ipx ipx;
197 struct sockaddr_at at;
198#ifdef OF_HAVE_SOCKADDR_STORAGE
199 /*
200 * Required to make the ABI stable in case we want to add more
201 * address types later.
202 */
203 struct sockaddr_storage storage;
204#endif
205 } sockaddr;
206 socklen_t length;
208
209#ifdef __cplusplus
210extern "C" {
211#endif
221extern OFSocketAddress OFSocketAddressParseIP(OFString *IP, uint16_t port);
222
231extern OFSocketAddress OFSocketAddressParseIPv4(OFString *IP, uint16_t port);
232
241extern OFSocketAddress OFSocketAddressParseIPv6(OFString *IP, uint16_t port);
242
250
259extern OFSocketAddress OFSocketAddressMakeIPX(uint32_t network,
260 const unsigned char node[_Nonnull IPX_NODE_LEN], uint16_t port);
261
271extern OFSocketAddress OFSocketAddressMakeAppleTalk(uint16_t network,
272 uint8_t node, uint8_t port);
273
281extern bool OFSocketAddressEqual(const OFSocketAddress *_Nonnull address1,
282 const OFSocketAddress *_Nonnull address2);
283
290extern unsigned long OFSocketAddressHash(
291 const OFSocketAddress *_Nonnull address);
292
299extern OFString *_Nonnull OFSocketAddressString(
300 const OFSocketAddress *_Nonnull address);
301
310extern OFString *_Nonnull OFSocketAddressDescription(
311 const OFSocketAddress *_Nonnull address);
312
319extern void OFSocketAddressSetIPPort(OFSocketAddress *_Nonnull address,
320 uint16_t port);
321
328extern uint16_t OFSocketAddressIPPort(const OFSocketAddress *_Nonnull address);
329
337 const OFSocketAddress *_Nonnull address);
338
345extern void OFSocketAddressSetIPXNetwork(OFSocketAddress *_Nonnull address,
346 uint32_t network);
347
354extern uint32_t OFSocketAddressIPXNetwork(
355 const OFSocketAddress *_Nonnull address);
356
363extern void OFSocketAddressSetIPXNode(OFSocketAddress *_Nonnull address,
364 const unsigned char node[_Nonnull IPX_NODE_LEN]);
365
372extern void OFSocketAddressGetIPXNode(const OFSocketAddress *_Nonnull address,
373 unsigned char node[_Nonnull IPX_NODE_LEN]);
374
381extern void OFSocketAddressSetIPXPort(OFSocketAddress *_Nonnull address,
382 uint16_t port);
383
390extern uint16_t OFSocketAddressIPXPort(const OFSocketAddress *_Nonnull address);
391
399 OFSocketAddress *_Nonnull address, uint16_t network);
400
407extern uint16_t OFSocketAddressAppleTalkNetwork(
408 const OFSocketAddress *_Nonnull address);
409
416extern void OFSocketAddressSetAppleTalkNode(OFSocketAddress *_Nonnull address,
417 uint8_t node);
418
425extern uint8_t OFSocketAddressAppleTalkNode(
426 const OFSocketAddress *_Nonnull address);
427
434extern void OFSocketAddressSetAppleTalkPort(OFSocketAddress *_Nonnull address,
435 uint8_t port);
436
443extern uint8_t OFSocketAddressAppleTalkPort(
444 const OFSocketAddress *_Nonnull address);
445
446extern bool OFSocketInit(void);
447#if defined(OF_HAVE_THREADS) && defined(OF_AMIGAOS) && !defined(OF_MORPHOS)
448extern void OFSocketDeinit(void);
449#endif
450extern int OFSocketErrNo(void);
451#if !defined(OF_WII) && !defined(OF_NINTENDO_3DS)
452extern int OFGetSockName(OFSocketHandle sock, struct sockaddr *restrict addr,
453 socklen_t *restrict addrLen);
454#endif
455
456#if defined(OF_HAVE_THREADS) && defined(OF_AMIGAOS) && !defined(OF_MORPHOS)
457extern OFTLSKey OFSocketBaseKey;
458# ifdef OF_AMIGAOS4
459extern OFTLSKey OFSocketInterfaceKey;
460# endif
461#endif
462#ifdef __cplusplus
463}
464#endif
465
466OF_ASSUME_NONNULL_END
void OFSocketAddressSetAppleTalkPort(OFSocketAddress *address, uint8_t port)
Sets the AppleTalk port of the specified OFSocketAddress.
Definition OFSocket.m:1180
void OFSocketAddressSetAppleTalkNetwork(OFSocketAddress *address, uint16_t network)
Sets the AppleTalk network of the specified OFSocketAddress.
Definition OFSocket.m:1136
uint8_t OFSocketAddressAppleTalkNode(const OFSocketAddress *address)
Gets the AppleTalk node of the specified OFSocketAddress.
Definition OFSocket.m:1171
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
OFString * OFSocketAddressUNIXPath(const OFSocketAddress *address)
Gets the UNIX socket path of the specified OFSocketAddress.
Definition OFSocket.m:1055
OFSocketAddress OFSocketAddressParseIPv4(OFString *IP, uint16_t port)
Parses the specified IPv4 and port into an OFSocketAddress.
Definition OFSocket.m:367
void OFSocketAddressSetIPXPort(OFSocketAddress *address, uint16_t port)
Sets the IPX port of the specified OFSocketAddress.
Definition OFSocket.m:1118
uint32_t OFSocketAddressIPXNetwork(const OFSocketAddress *address)
Returns the IPX network of the specified OFSocketAddress.
Definition OFSocket.m:1085
void OFSocketAddressSetAppleTalkNode(OFSocketAddress *address, uint8_t node)
Sets the AppleTalk node of the specified OFSocketAddress.
Definition OFSocket.m:1162
uint16_t OFSocketAddressAppleTalkNetwork(const OFSocketAddress *address)
Returns the AppleTalk network of the specified OFSocketAddress.
Definition OFSocket.m:1149
void OFSocketAddressSetIPXNetwork(OFSocketAddress *address, uint32_t network)
Sets the IPX network of the specified OFSocketAddress.
Definition OFSocket.m:1074
void OFSocketAddressSetIPPort(OFSocketAddress *address, uint16_t port)
Sets the IP port of the specified OFSocketAddress.
Definition OFSocket.m:1027
uint16_t OFSocketAddressIPPort(const OFSocketAddress *address)
Returns the IP port of the specified OFSocketAddress.
Definition OFSocket.m:1042
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
uint16_t OFSocketAddressIPXPort(const OFSocketAddress *address)
Returns the IPX port of the specified OFSocketAddress.
Definition OFSocket.m:1127
OFSocketAddress OFSocketAddressParseIPv6(OFString *IP, uint16_t port)
Parses the specified IPv6 and port into an OFSocketAddress.
Definition OFSocket.m:469
bool OFSocketAddressEqual(const OFSocketAddress *address1, const OFSocketAddress *address2)
Compares two OFSocketAddress for equality.
Definition OFSocket.m:663
unsigned long OFSocketAddressHash(const OFSocketAddress *address)
Returns the hash for the specified OFSocketAddress.
Definition OFSocket.m:770
OFString * OFSocketAddressDescription(const OFSocketAddress *address)
Returns a description for the specified OFSocketAddress.
Definition OFSocket.m:998
OFSocketAddress OFSocketAddressParseIP(OFString *IP, uint16_t port)
Parses the specified IP (either v4 or v6) and port into an OFSocketAddress.
Definition OFSocket.m:568
OFString * OFSocketAddressString(const OFSocketAddress *address)
Converts the specified OFSocketAddress to a string.
Definition OFSocket.m:979
OFSocketAddressFamily
A socket address family.
Definition OFSocket.h:107
@ OFSocketAddressFamilyAny
Definition OFSocket.h:121
@ OFSocketAddressFamilyIPX
Definition OFSocket.h:117
@ OFSocketAddressFamilyUNIX
Definition OFSocket.h:115
@ OFSocketAddressFamilyAppleTalk
Definition OFSocket.h:119
@ OFSocketAddressFamilyIPv6
Definition OFSocket.h:113
@ OFSocketAddressFamilyIPv4
Definition OFSocket.h:111
@ OFSocketAddressFamilyUnknown
Definition OFSocket.h:109
uint8_t OFSocketAddressAppleTalkPort(const OFSocketAddress *address)
Returns the AppleTalk port of the specified OFSocketAddress.
Definition OFSocket.m:1189
OFSocketAddress OFSocketAddressMakeUNIX(OFString *path)
Creates a UNIX socket address from the specified path.
Definition OFSocket.m:582
void OFSocketAddressGetIPXNode(const OFSocketAddress *address, unsigned char node[IPX_NODE_LEN])
Gets the IPX node of the specified OFSocketAddress.
Definition OFSocket.m:1108
void OFSocketAddressSetIPXNode(OFSocketAddress *address, const unsigned char node[IPX_NODE_LEN])
Sets the IPX node of the specified OFSocketAddress.
Definition OFSocket.m:1098
A class for handling strings.
Definition OFString.h:139
A struct which represents a host / port pair for a socket.
Definition OFSocket.h:186