ObjFW  Diff

Differences From Artifact [54314146cb]:

To Artifact [b18a1905f9]:


25
26
27
28
29
30
31

32
33
34
35
36
37
38
39

40




41
42
43
44
45
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
125
126

127

128
129
130
131





132























































133
134
135
136
137
138
139
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

42
43
44
45
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

125
126
127
128

129
130
131


132
133
134
135
136
137

138
139
140

141
142
143

144
145
146
147
148
149
150
151
152

153
154
155
156
157
158
159
160
161
162
163
164

165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226







+








+
-
+
+
+
+












-
+


-
-
+
+
+
+
+
+
+
+
+

-
+

+


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



-
+




-
+



-
+





-
-
+
+


-
+









-
+

-
+



-
+


-
-
+
+




-
+


-
+


-
+




+



-
+

+




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








#import "OFArray.h"
#import "OFCharacterSet.h"
#import "OFLocale.h"
#import "OFString.h"

#import "OFException.h"  /* For some E* -> WSAE* defines */
#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
#import "OFLockFailedException.h"
#import "OFUnlockFailedException.h"

#import "socket.h"
#import "socket_helpers.h"
#ifdef OF_HAVE_THREADS
# ifndef OF_AMIGAOS
# include "mutex.h"
#  import "mutex.h"
# else
#  import "tlskey.h"
# endif
#endif
#include "once.h"

#ifdef OF_AMIGAOS
# include <proto/exec.h>
#endif

#ifdef OF_NINTENDO_3DS
# include <3ds/types.h>
# include <3ds/services/soc.h>
#endif

#ifdef OF_HAVE_THREADS
#if defined(OF_HAVE_THREADS) && !defined(OF_AMIGAOS)
static of_mutex_t mutex;
#endif
#ifdef OF_AMIGAOS
/* TODO: Support multiple threads */
#ifndef OF_AMIGAOS
static bool initSuccessful = false;
#else
# ifdef OF_HAVE_THREADS
of_tlskey_t of_socket_base_key;
#  ifdef OF_AMIGAOS4
of_tlskey_t of_socket_interface_key;
#  endif
# else
struct Library *SocketBase;
# ifdef OF_AMIGAOS4
#  ifdef OF_AMIGAOS4
struct SocketIFace *ISocket = NULL;
#  endif
# endif
#endif
static bool initSuccessful = false;


#if defined(OF_HAVE_THREADS) && defined(OF_AMIGAOS)
OF_CONSTRUCTOR()
{
	if (!of_tlskey_new(&of_socket_base_key))
		@throw [OFInitializationFailedException exception];

# ifdef OF_AMIGAOS4
	if (!of_tlskey_new(&of_socket_interface_key))
		@throw [OFInitializationFailedException exception];
# endif
}
#endif

#if !defined(OF_AMIGAOS) || !defined(OF_HAVE_THREADS)
static void
init(void)
{
#if defined(OF_WINDOWS)
# if defined(OF_WINDOWS)
	WSADATA wsa;

	if (WSAStartup(MAKEWORD(2, 0), &wsa))
		return;
#elif defined(OF_AMIGAOS)
# elif defined(OF_AMIGAOS)
	if ((SocketBase = OpenLibrary("bsdsocket.library", 4)) == NULL)
		return;

# ifdef OF_AMIGAOS4
#  ifdef OF_AMIGAOS4
	if ((ISocket = (struct SocketIFace *)
	    GetInterface(SocketBase, "main", 1, NULL)) == NULL) {
		CloseLibrary(SocketBase);
		return;
	}
# endif
#elif defined(OF_WII)
#  endif
# elif defined(OF_WII)
	if (net_init() < 0)
		return;
#elif defined(OF_NINTENDO_3DS)
# elif defined(OF_NINTENDO_3DS)
	void *ctx;

	if ((ctx = memalign(0x1000, 0x100000)) == NULL)
		return;

	if (socInit(ctx, 0x100000) != 0)
		return;

	atexit((void (*)(void))socExit);
#endif
# endif

#ifdef OF_HAVE_THREADS
# if defined(OF_HAVE_THREADS) && !defined(OF_AMIGAOS)
	if (!of_mutex_new(&mutex))
		return;

# ifdef OF_WII
#  ifdef OF_WII
	if (!of_spinlock_new(&spinlock))
		return;
# endif
#endif
#  endif
# endif

	initSuccessful = true;
}

#ifdef OF_AMIGAOS
# ifdef OF_AMIGAOS
OF_DESTRUCTOR()
{
# ifdef OF_AMIGAOS4
#  ifdef OF_AMIGAOS4
	if (ISocket != NULL)
		DropInterface((struct Interface *)ISocket);
# endif
#  endif

	if (SocketBase != NULL)
		CloseLibrary(SocketBase);
}
# endif
#endif

bool
of_socket_init()
of_socket_init(void)
{
#if !defined(OF_AMIGAOS) || !defined(OF_HAVE_THREADS)
	static of_once_t onceControl = OF_ONCE_INIT;
	of_once(&onceControl, init);

	return initSuccessful;
#else
	struct Library *socketBase;
# ifdef OF_AMIGAOS4
	struct SocketIFace *socketInterface;
# endif
}

# ifdef OF_AMIGAOS4
	if ((socketInterface = of_tlskey_get(of_socket_interface_key)) != NULL)
# else
	if ((socketBase = of_tlskey_get(of_socket_base_key)) != NULL)
# endif
		return true;

	if ((socketBase = OpenLibrary("bsdsocket.library", 4)) == NULL)
		return false;

# ifdef OF_AMIGAOS4
	if ((socketInterface = (struct SocketIFace *)
	    GetInterface(socketBase, "main", 1, NULL)) == NULL) {
		CloseLibrary(socketBase);
		return false;
	}
# endif

	if (!of_tlskey_set(of_socket_base_key, socketBase)) {
		CloseLibrary(socketBase);
# ifdef OF_AMIGAOS4
		DropInterface((struct Interface *)socketInterface);
# endif
		return false;
	}

# ifdef OF_AMIGAOS4
	if (!of_tlskey_set(of_socket_interface_key, socketInterface)) {
		CloseLibrary(socketBase);
		DropInterface((struct Interface *)socketInterface);
		return false;
	}
# endif

	return true;
#endif
}

#if defined(OF_HAVE_THREADS) && defined(OF_AMIGAOS)
void
of_socket_deinit(void)
{
	struct Library *socketBase = of_tlskey_get(of_socket_base_key);
# ifdef OF_AMIGAOS4
	struct SocketIFace *socketInterface =
	    of_tlskey_get(of_socket_interface_key);

	if (socketInterface != NULL)
		DropInterface((struct Interface *)socketInterface);
# endif
	if (socketBase != NULL)
		CloseLibrary(socketBase);
}
#endif

int
of_socket_errno()
{
#if defined(OF_WINDOWS)
	switch (WSAGetLastError()) {
	case WSAEACCES:
235
236
237
238
239
240
241
242

243
244
245
246
247
248
249
250

251
252
253
254
255
256
257
322
323
324
325
326
327
328

329
330
331
332
333
334
335
336

337
338
339
340
341
342
343
344







-
+







-
+







#ifndef OF_WII
int
of_getsockname(of_socket_t sock, struct sockaddr *restrict addr,
    socklen_t *restrict addrLen)
{
	int ret;

# ifdef OF_HAVE_THREADS
# if defined(OF_HAVE_THREADS) && !defined(OF_AMIGAOS)
	if (!of_mutex_lock(&mutex))
		@throw [OFLockFailedException exception];

# endif

	ret = getsockname(sock, addr, addrLen);

# ifdef OF_HAVE_THREADS
# if defined(OF_HAVE_THREADS) && !defined(OF_AMIGAOS)
	if (!of_mutex_unlock(&mutex))
		@throw [OFUnlockFailedException exception];
# endif

	return ret;
}
#endif