ObjFW  Diff

Differences From Artifact [232e64699e]:

To Artifact [d1393543d0]:

  • File src/socket.h — part of check-in [8f810ecb7e] at 2018-08-11 14:00:46 on branch trunk — Change the layout of of_socket_address_t

    Instead of containing a struct sockaddr_storage - which does not exist
    on all supported platforms - it now contains a union of all struct
    sockaddr_* types.

    Additionally, if struct sockaddr_in6 does not exist, it is defined so
    that an IPv6 can be stored even if the system does not support IPv6. (user: js, size: 5102) [annotate] [blame] [check-ins using]


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99





















100
101
102
103
104
105










106



107
108
109
110
111
112
113
46
47
48
49
50
51
52








53
54
55
56
57
58
59
60
61






62
63
64
65








66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

115
116
117
118
119
120
121
122
123
124







-
-
-
-
-
-
-
-









-
-
-
-
-
-




-
-
-
-
-
-
-
-












+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+






+
+
+
+
+
+
+
+
+
+
-
+
+
+







# endif
# include <windows.h>
# include <ws2tcpip.h>
#endif

/*! @file */

#if defined(OF_AMIGAOS) && defined(OF_MORPHOS_IXEMUL)
struct sockaddr_storage {
	uint8_t ss_len;
	sa_family_t ss_family;
	char ss_data[2 + sizeof(struct in_addr) + 8];
};
#endif

#ifdef OF_MORPHOS
typedef long socklen_t;
#endif
#ifdef OF_MORPHOS_IXEMUL
typedef int socklen_t;
#endif

#ifdef OF_WII
# include <network.h>

struct sockaddr_storage {
	u8 ss_len;
	sa_family_t ss_family;
	u8 ss_data[14];
};
#endif

#ifdef OF_PSP
# include <stdint.h>

struct sockaddr_storage {
	uint8_t	       ss_len;
	sa_family_t    ss_family;
	in_port_t      ss_data1;
	struct in_addr ss_data2;
	int8_t	       ss_data3[8];
};
#endif

#import "macros.h"

OF_ASSUME_NONNULL_BEGIN

#ifndef OF_WINDOWS
typedef int of_socket_t;
#else
typedef SOCKET of_socket_t;
#endif

/*!
 * @enum of_socket_address_type_t socket.h ObjFW/socket.h
 */
typedef enum {
	OF_SOCKET_ADDRESS_FAMILY_UNKNOWN,
	OF_SOCKET_ADDRESS_FAMILY_IPV4,
	OF_SOCKET_ADDRESS_FAMILY_IPV6,
} of_socket_address_family_t;

#ifndef OF_HAVE_IPV6
struct sockaddr_in6 {
	sa_family_t sin6_family;
	in_port_t sin6_port;
	uint32_t sin6_flowinfo;
	struct in6_addr {
		uint8_t s6_addr[16];
	} sin6_addr;
	uint32_t sin6_scope_id;
};
#endif

/*!
 * @struct of_socket_address_t socket.h ObjFW/socket.h
 *
 * @brief A struct which represents a host / port pair for a socket.
 */
typedef struct OF_BOXABLE {
	/*
	 * Even though struct sockaddr contains the family, we need to use our
	 * own family, as we need to support storing an IPv6 address on systems
	 * that don't support IPv6. These may not have AF_INET6 defined and we
	 * can't just define it, as the value is system-dependent and might
	 * clash with an existing value.
	 */
	of_socket_address_family_t family;
	union {
		struct sockaddr sockaddr;
	struct sockaddr_storage address;
		struct sockaddr_in in;
		struct sockaddr_in6 in6;
	} sockaddr;
	socklen_t length;
} of_socket_address_t;

#ifdef __cplusplus
extern "C" {
#endif
extern bool of_socket_init(void);
173
174
175
176
177
178
179










180
181
182
183
184
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205







+
+
+
+
+
+
+
+
+
+





 * @param address The address to convert to a string
 * @param port A pointer to an uint16_t which should be set to the port of the
 *	       address or NULL if the port is not needed
 * @return The address as an IP string
 */
extern OFString *_Nonnull of_socket_address_ip_string(
    const of_socket_address_t *_Nonnull address, uint16_t *_Nullable port);

/*!
 * @brief Sets the port of the specified of_socket_address_t, independent of
 *	  the address family used.
 *
 * @param address The address on which to set the port
 * @param port The port to set on the address
 */
extern void of_socket_address_set_port(of_socket_address_t *_Nonnull address,
    uint16_t port);
#ifdef __cplusplus
}
#endif

OF_ASSUME_NONNULL_END