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 file is part of ObjFW. It may be distributed under the terms of the
7 * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
8 * the packaging of this file.
9 *
10 * Alternatively, it may be distributed under the terms of the GNU General
11 * Public License, either version 2 or 3, which can be found in the file
12 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
13 * file.
14 */
15
16#include "objfw-defs.h"
17
18#ifndef OF_HAVE_SOCKETS
19# error No sockets available!
20#endif
21
22#include <stdbool.h>
23
24#import "OFString.h"
25#if defined(OF_HAVE_THREADS) && defined(OF_AMIGAOS)
26# import "OFTLSKey.h"
27#endif
28
29#ifdef OF_HAVE_SYS_SOCKET_H
30# include <sys/socket.h>
31#endif
32#ifdef OF_HAVE_NETINET_IN_H
33# include <netinet/in.h>
34#endif
35#ifdef OF_HAVE_NETINET_TCP_H
36# include <netinet/tcp.h>
37#endif
38#ifdef OF_HAVE_SYS_UN_H
39# include <sys/un.h>
40#endif
41#ifdef OF_HAVE_AFUNIX_H
42# include <afunix.h>
43#endif
44#ifdef OF_HAVE_NETIPX_IPX_H
45# include <netipx/ipx.h>
46#endif
47#if defined(OF_HAVE_NETAT_APPLETALK_H)
48# include <netat/appletalk.h>
49#elif defined(OF_HAVE_NETATALK_AT_H)
50# include <netatalk/at.h>
51#endif
52
53#ifdef OF_WINDOWS
54# include <windows.h>
55# include <ws2tcpip.h>
56# ifdef OF_HAVE_IPX
57# include <wsipx.h>
58# endif
59# ifdef OF_HAVE_APPLETALK
60# include <atalkwsh.h>
61# endif
62#endif
63
64#ifdef OF_WII
65# include <network.h>
66#endif
67
68#ifdef OF_PSP
69# include <stdint.h>
70#endif
71
72#import "macros.h"
73
74OF_ASSUME_NONNULL_BEGIN
75
78#ifndef OF_WINDOWS
79typedef int OFSocketHandle;
80static const OFSocketHandle OFInvalidSocketHandle = -1;
81#else
82typedef SOCKET OFSocketHandle;
83static const OFSocketHandle OFInvalidSocketHandle = INVALID_SOCKET;
84#endif
85
86#ifdef OF_WINDOWS
87typedef short sa_family_t;
88#endif
89
90#ifdef OF_WII
91typedef u8 sa_family_t;
92#endif
93
94#ifdef OF_MORPHOS
95typedef long socklen_t;
96typedef u_char sa_family_t;
97typedef u_short in_port_t;
98#endif
99
119
120#ifndef OF_HAVE_IPV6
121struct sockaddr_in6 {
122 sa_family_t sin6_family;
123 in_port_t sin6_port;
124 uint32_t sin6_flowinfo;
125 struct in6_addr {
126 uint8_t s6_addr[16];
127 } sin6_addr;
128 uint32_t sin6_scope_id;
129};
130#endif
131
132#if !defined(OF_HAVE_UNIX_SOCKETS) && !defined(OF_MORPHOS) && !defined(OF_MINT)
133struct sockaddr_un {
134 sa_family_t sun_family;
135 char sun_path[108];
136};
137#endif
138
139#ifndef IPX_NODE_LEN
140# define IPX_NODE_LEN 6
141#endif
142#if !defined(OF_HAVE_IPX)
143struct sockaddr_ipx {
144 sa_family_t sipx_family;
145 uint32_t sipx_network;
146 unsigned char sipx_node[IPX_NODE_LEN];
147 uint16_t sipx_port;
148 uint8_t sipx_type;
149};
150#elif defined(OF_WINDOWS)
151# define IPX_NODE_LEN 6
152# define sipx_family sa_family
153# define sipx_network sa_netnum
154# define sipx_node sa_nodenum
155# define sipx_port sa_socket
156#elif defined(OF_FREEBSD)
157# define sipx_network sipx_addr.x_net.c_net
158# define sipx_node sipx_addr.x_host.c_host
159#endif
160
161#ifndef OF_HAVE_APPLETALK
162struct sockaddr_at {
163 sa_family_t sat_family;
164 uint8_t sat_port;
165 uint16_t sat_net;
166 uint8_t sat_node;
167};
168#else
169# ifdef OF_WINDOWS
170# define sat_port sat_socket
171# else
172# define sat_net sat_addr.s_net
173# define sat_node sat_addr.s_node
174# endif
175#endif
176
182typedef struct OF_BOXABLE OFSocketAddress {
184 /*
185 * We can't use struct sockaddr as it can contain variable length
186 * arrays.
187 */
188 union {
189 struct sockaddr_in in;
190 struct sockaddr_in6 in6;
191 struct sockaddr_un un;
192 struct sockaddr_ipx ipx;
193 struct sockaddr_at at;
194#ifdef OF_HAVE_SOCKADDR_STORAGE
195 /*
196 * Required to make the ABI stable in case we want to add more
197 * address types later.
198 */
199 struct sockaddr_storage storage;
200#endif
201 } sockaddr;
202 socklen_t length;
204
205#ifdef __cplusplus
206extern "C" {
207#endif
217extern OFSocketAddress OFSocketAddressParseIP(OFString *IP, uint16_t port);
218
227extern OFSocketAddress OFSocketAddressParseIPv4(OFString *IP, uint16_t port);
228
237extern OFSocketAddress OFSocketAddressParseIPv6(OFString *IP, uint16_t port);
238
246
255extern OFSocketAddress OFSocketAddressMakeIPX(uint32_t network,
256 const unsigned char node[_Nonnull IPX_NODE_LEN], uint16_t port);
257
267extern OFSocketAddress OFSocketAddressMakeAppleTalk(uint16_t network,
268 uint8_t node, uint8_t port);
269
277extern bool OFSocketAddressEqual(const OFSocketAddress *_Nonnull address1,
278 const OFSocketAddress *_Nonnull address2);
279
286extern unsigned long OFSocketAddressHash(
287 const OFSocketAddress *_Nonnull address);
288
295extern OFString *_Nonnull OFSocketAddressString(
296 const OFSocketAddress *_Nonnull address);
297
304extern void OFSocketAddressSetIPPort(OFSocketAddress *_Nonnull address,
305 uint16_t port);
306
313extern uint16_t OFSocketAddressIPPort(const OFSocketAddress *_Nonnull address);
314
322 const OFSocketAddress *_Nonnull address);
323
330extern void OFSocketAddressSetIPXNetwork(OFSocketAddress *_Nonnull address,
331 uint32_t network);
332
339extern uint32_t OFSocketAddressIPXNetwork(
340 const OFSocketAddress *_Nonnull address);
341
348extern void OFSocketAddressSetIPXNode(OFSocketAddress *_Nonnull address,
349 const unsigned char node[_Nonnull IPX_NODE_LEN]);
350
357extern void OFSocketAddressGetIPXNode(const OFSocketAddress *_Nonnull address,
358 unsigned char node[_Nonnull IPX_NODE_LEN]);
359
366extern void OFSocketAddressSetIPXPort(OFSocketAddress *_Nonnull address,
367 uint16_t port);
368
375extern uint16_t OFSocketAddressIPXPort(const OFSocketAddress *_Nonnull address);
376
384 OFSocketAddress *_Nonnull address, uint16_t network);
385
392extern uint16_t OFSocketAddressAppleTalkNetwork(
393 const OFSocketAddress *_Nonnull address);
394
401extern void OFSocketAddressSetAppleTalkNode(OFSocketAddress *_Nonnull address,
402 uint8_t node);
403
410extern uint8_t OFSocketAddressAppleTalkNode(
411 const OFSocketAddress *_Nonnull address);
412
419extern void OFSocketAddressSetAppleTalkPort(OFSocketAddress *_Nonnull address,
420 uint8_t port);
421
428extern uint8_t OFSocketAddressAppleTalkPort(
429 const OFSocketAddress *_Nonnull address);
430
431extern bool OFSocketInit(void);
432#if defined(OF_HAVE_THREADS) && defined(OF_AMIGAOS) && !defined(OF_MORPHOS)
433extern void OFSocketDeinit(void);
434#endif
435extern int OFSocketErrNo(void);
436#if !defined(OF_WII) && !defined(OF_NINTENDO_3DS)
437extern int OFGetSockName(OFSocketHandle sock, struct sockaddr *restrict addr,
438 socklen_t *restrict addrLen);
439#endif
440
441#if defined(OF_HAVE_THREADS) && defined(OF_AMIGAOS) && !defined(OF_MORPHOS)
442extern OFTLSKey OFSocketBaseKey;
443# ifdef OF_AMIGAOS4
444extern OFTLSKey OFSocketInterfaceKey;
445# endif
446#endif
447#ifdef __cplusplus
448}
449#endif
450
451OF_ASSUME_NONNULL_END
void OFSocketAddressSetAppleTalkPort(OFSocketAddress *address, uint8_t port)
Sets the AppleTalk port of the specified OFSocketAddress.
Definition OFSocket.m:1147
void OFSocketAddressSetAppleTalkNetwork(OFSocketAddress *address, uint16_t network)
Sets the AppleTalk network of the specified OFSocketAddress.
Definition OFSocket.m:1103
uint8_t OFSocketAddressAppleTalkNode(const OFSocketAddress *address)
Gets the AppleTalk node of the specified OFSocketAddress.
Definition OFSocket.m:1138
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:610
OFString * OFSocketAddressUNIXPath(const OFSocketAddress *address)
Gets the UNIX socket path of the specified OFSocketAddress.
Definition OFSocket.m:1022
OFSocketAddress OFSocketAddressParseIPv4(OFString *IP, uint16_t port)
Parses the specified IPv4 and port into an OFSocketAddress.
Definition OFSocket.m:363
void OFSocketAddressSetIPXPort(OFSocketAddress *address, uint16_t port)
Sets the IPX port of the specified OFSocketAddress.
Definition OFSocket.m:1085
uint32_t OFSocketAddressIPXNetwork(const OFSocketAddress *address)
Returns the IPX network of the specified OFSocketAddress.
Definition OFSocket.m:1052
void OFSocketAddressSetAppleTalkNode(OFSocketAddress *address, uint8_t node)
Sets the AppleTalk node of the specified OFSocketAddress.
Definition OFSocket.m:1129
uint16_t OFSocketAddressAppleTalkNetwork(const OFSocketAddress *address)
Returns the AppleTalk network of the specified OFSocketAddress.
Definition OFSocket.m:1116
void OFSocketAddressSetIPXNetwork(OFSocketAddress *address, uint32_t network)
Sets the IPX network of the specified OFSocketAddress.
Definition OFSocket.m:1041
void OFSocketAddressSetIPPort(OFSocketAddress *address, uint16_t port)
Sets the IP port of the specified OFSocketAddress.
Definition OFSocket.m:994
uint16_t OFSocketAddressIPPort(const OFSocketAddress *address)
Returns the IP port of the specified OFSocketAddress.
Definition OFSocket.m:1009
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:634
uint16_t OFSocketAddressIPXPort(const OFSocketAddress *address)
Returns the IPX port of the specified OFSocketAddress.
Definition OFSocket.m:1094
OFSocketAddress OFSocketAddressParseIPv6(OFString *IP, uint16_t port)
Parses the specified IPv6 and port into an OFSocketAddress.
Definition OFSocket.m:465
bool OFSocketAddressEqual(const OFSocketAddress *address1, const OFSocketAddress *address2)
Compares two OFSocketAddress for equality.
Definition OFSocket.m:659
unsigned long OFSocketAddressHash(const OFSocketAddress *address)
Returns the hash for the specified OFSocketAddress.
Definition OFSocket.m:766
OFSocketAddress OFSocketAddressParseIP(OFString *IP, uint16_t port)
Parses the specified IP (either v4 or v6) and port into an OFSocketAddress.
Definition OFSocket.m:564
OFString * OFSocketAddressString(const OFSocketAddress *address)
Converts the specified OFSocketAddress to a string.
Definition OFSocket.m:975
OFSocketAddressFamily
A socket address family.
Definition OFSocket.h:103
@ OFSocketAddressFamilyAny
Definition OFSocket.h:117
@ OFSocketAddressFamilyIPX
Definition OFSocket.h:113
@ OFSocketAddressFamilyUNIX
Definition OFSocket.h:111
@ OFSocketAddressFamilyAppleTalk
Definition OFSocket.h:115
@ OFSocketAddressFamilyIPv6
Definition OFSocket.h:109
@ OFSocketAddressFamilyIPv4
Definition OFSocket.h:107
@ OFSocketAddressFamilyUnknown
Definition OFSocket.h:105
uint8_t OFSocketAddressAppleTalkPort(const OFSocketAddress *address)
Returns the AppleTalk port of the specified OFSocketAddress.
Definition OFSocket.m:1156
OFSocketAddress OFSocketAddressMakeUNIX(OFString *path)
Creates a UNIX socket address from the specified path.
Definition OFSocket.m:578
void OFSocketAddressGetIPXNode(const OFSocketAddress *address, unsigned char node[IPX_NODE_LEN])
Gets the IPX node of the specified OFSocketAddress.
Definition OFSocket.m:1075
void OFSocketAddressSetIPXNode(OFSocketAddress *address, const unsigned char node[IPX_NODE_LEN])
Sets the IPX node of the specified OFSocketAddress.
Definition OFSocket.m:1065
A class for handling strings.
Definition OFString.h:135
A struct which represents a host / port pair for a socket.
Definition OFSocket.h:182