ObjFW
socket.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2021 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 
26 #ifdef OF_HAVE_SYS_SOCKET_H
27 # include <sys/socket.h>
28 #endif
29 #ifdef OF_HAVE_NETINET_IN_H
30 # include <netinet/in.h>
31 #endif
32 #ifdef OF_HAVE_NETINET_TCP_H
33 # include <netinet/tcp.h>
34 #endif
35 #ifdef OF_HAVE_NETINET_SCTP_H
36 # include <netinet/sctp.h>
37 #endif
38 #ifdef OF_HAVE_NETIPX_IPX_H
39 # include <netipx/ipx.h>
40 #endif
41 
42 #include "platform.h"
43 
44 #ifdef OF_WINDOWS
45 # include <windows.h>
46 # include <ws2tcpip.h>
47 # ifdef OF_HAVE_IPX
48 # include <wsipx.h>
49 # endif
50 #endif
51 
54 #ifdef OF_WII
55 # include <network.h>
56 #endif
57 
58 #ifdef OF_PSP
59 # include <stdint.h>
60 #endif
61 
62 #import "macros.h"
63 #if defined(OF_HAVE_THREADS) && defined(OF_AMIGAOS)
64 # import "tlskey.h"
65 #endif
66 
67 OF_ASSUME_NONNULL_BEGIN
68 
69 #ifndef OF_WINDOWS
70 typedef int of_socket_t;
71 #else
72 typedef SOCKET of_socket_t;
73 #endif
74 
75 #ifdef OF_WII
76 typedef u8 sa_family_t;
77 #endif
78 
79 #ifdef OF_MORPHOS
80 typedef long socklen_t;
81 typedef u_char sa_family_t;
82 typedef u_short in_port_t;
83 #endif
84 
88 typedef enum {
100 
101 #ifndef OF_HAVE_IPV6
102 struct sockaddr_in6 {
103  sa_family_t sin6_family;
104  in_port_t sin6_port;
105  uint32_t sin6_flowinfo;
106  struct in6_addr {
107  uint8_t s6_addr[16];
108  } sin6_addr;
109  uint32_t sin6_scope_id;
110 };
111 #endif
112 
113 #ifndef OF_HAVE_IPX
114 # define IPX_NODE_LEN 6
115 struct sockaddr_ipx {
116  sa_family_t sipx_family;
117  uint32_t sipx_network;
118  unsigned char sipx_node[IPX_NODE_LEN];
119  uint16_t sipx_port;
120  uint8_t sipx_type;
121 };
122 #endif
123 #ifdef OF_WINDOWS
124 # define IPX_NODE_LEN 6
125 # define sipx_family sa_family
126 # define sipx_network sa_netnum
127 # define sipx_node sa_nodenum
128 # define sipx_port sa_socket
129 #endif
130 
136 struct OF_BOXABLE of_socket_address_t {
137  /*
138  * Even though struct sockaddr contains the family, we need to use our
139  * own family, as we need to support storing an IPv6 address on systems
140  * that don't support IPv6. These may not have AF_INET6 defined and we
141  * can't just define it, as the value is system-dependent and might
142  * clash with an existing value.
143  */
145  union {
146  struct sockaddr sockaddr;
147  struct sockaddr_in in;
148  struct sockaddr_in6 in6;
149  struct sockaddr_ipx ipx;
150  } sockaddr;
151  socklen_t length;
152 };
154 
155 #ifdef __cplusplus
156 extern "C" {
157 #endif
166  OFString *IP, uint16_t port);
167 
176  OFString *IP, uint16_t port);
177 
186  OFString *IP, uint16_t port);
187 
196  const unsigned char node[_Nonnull IPX_NODE_LEN], uint32_t network,
197  uint16_t port);
198 
207  const of_socket_address_t *_Nonnull address1,
208  const of_socket_address_t *_Nonnull address2);
209 
216 extern unsigned long of_socket_address_hash(
217  const of_socket_address_t *_Nonnull address);
218 
228  const of_socket_address_t *_Nonnull address, uint16_t *_Nullable port);
229 
237 extern void of_socket_address_set_port(of_socket_address_t *_Nonnull address,
238  uint16_t port);
239 
248  const of_socket_address_t *_Nonnull address);
249 
257  of_socket_address_t *_Nonnull address, uint32_t network);
258 
266  const of_socket_address_t *_Nonnull address);
267 
275  of_socket_address_t *_Nonnull address,
276  const unsigned char node[_Nonnull IPX_NODE_LEN]);
277 
285  const of_socket_address_t *_Nonnull address,
286  unsigned char node[_Nonnull IPX_NODE_LEN]);
287 
288 extern bool of_socket_init(void);
289 #if defined(OF_HAVE_THREADS) && defined(OF_AMIGAOS) && !defined(OF_MORPHOS)
290 extern void of_socket_deinit(void);
291 #endif
292 extern int of_socket_errno(void);
293 #if !defined(OF_WII) && !defined(OF_NINTENDO_3DS)
294 extern int of_getsockname(of_socket_t sock, struct sockaddr *restrict addr,
295  socklen_t *restrict addrLen);
296 #endif
297 
298 #if defined(OF_HAVE_THREADS) && defined(OF_AMIGAOS) && !defined(OF_MORPHOS)
299 extern of_tlskey_t of_socket_base_key;
300 # ifdef OF_AMIGAOS4
301 extern of_tlskey_t of_socket_interface_key;
302 # endif
303 #endif
304 #ifdef __cplusplus
305 }
306 #endif
307 
308 OF_ASSUME_NONNULL_END
A class for handling strings.
Definition: OFString.h:132
of_socket_address_family_t
A socket address family.
Definition: socket.h:88
@ OF_SOCKET_ADDRESS_FAMILY_UNKNOWN
Definition: socket.h:90
@ OF_SOCKET_ADDRESS_FAMILY_IPV6
Definition: socket.h:94
@ OF_SOCKET_ADDRESS_FAMILY_IPX
Definition: socket.h:96
@ OF_SOCKET_ADDRESS_FAMILY_IPV4
Definition: socket.h:92
@ OF_SOCKET_ADDRESS_FAMILY_ANY
Definition: socket.h:98
of_socket_address_t of_socket_address_parse_ip(OFString *IP, uint16_t port)
Parses the specified IP and port into an of_socket_address_t.
Definition: socket.m:505
unsigned long of_socket_address_hash(const of_socket_address_t *_Nonnull address)
Returns the hash for the specified of_socket_address_t.
void of_socket_address_set_ipx_node(of_socket_address_t *_Nonnull address, const unsigned char node[_Nonnull IPX_NODE_LEN])
Sets the IPX node of the specified of_socket_address_t.
void of_socket_address_set_ipx_network(of_socket_address_t *_Nonnull address, uint32_t network)
Sets the IPX network of the specified of_socket_address_t.
of_socket_address_t of_socket_address_ipx(const unsigned char node[_Nonnull IPX_NODE_LEN], uint32_t network, uint16_t port)
Creates an IPX address for the specified network, node and port.
uint16_t of_socket_address_get_port(const of_socket_address_t *_Nonnull address)
Returns the port of the specified of_socket_address_t, independent of the address family used.
bool of_socket_address_equal(const of_socket_address_t *_Nonnull address1, const of_socket_address_t *_Nonnull address2)
Compares two of_socket_address_t for equality.
void of_socket_address_set_port(of_socket_address_t *_Nonnull address, uint16_t port)
Sets the port of the specified of_socket_address_t, independent of the address family used.
of_socket_address_t of_socket_address_parse_ipv6(OFString *IP, uint16_t port)
Parses the specified IPv6 and port into an of_socket_address_t.
Definition: socket.m:426
void of_socket_address_get_ipx_node(const of_socket_address_t *_Nonnull address, unsigned char node[_Nonnull IPX_NODE_LEN])
Gets the IPX node of the specified of_socket_address_t.
OFString *_Nonnull of_socket_address_ip_string(const of_socket_address_t *_Nonnull address, uint16_t *_Nullable port)
Converts the specified of_socket_address_t to an IP string and port.
of_socket_address_t of_socket_address_parse_ipv4(OFString *IP, uint16_t port)
Parses the specified IPv4 and port into an of_socket_address_t.
Definition: socket.m:352
uint32_t of_socket_address_get_ipx_network(const of_socket_address_t *_Nonnull address)
Returns the IPX network of the specified of_socket_address_t.
A struct which represents a host / port pair for a socket.
Definition: socket.h:136