ObjFW  Check-in [898f8c7072]

Overview
Comment:resolver.m: Fix locking

A lock for thread-unsafe getaddrinfo was missing and not all locks were
unlocked / all buffers free'd if an error occurred.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 898f8c7072ea3671a2179c766804c56d1b881e3f1de13d3518711b534788841e
User & Date: js on 2014-03-04 23:53:48
Other Links: manifest | tags
Context
2014-03-05
00:00
Include socket.h before socket_helpers.h check-in: f96fd9d551 user: js tags: trunk
2014-03-04
23:53
resolver.m: Fix locking check-in: 898f8c7072 user: js tags: trunk
2014-02-28
00:41
Make MinGW32 and MinGW-w64 happy at the same time check-in: 5ca844dd3b user: js tags: trunk
Changes

Modified src/exceptions/common.h from [8eded04c5d] to [fcb5839156].

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#ifdef OF_HAVE_SOCKETS
# include "socket_helpers.h"
#endif

#ifndef _WIN32
# define GET_ERRNO	errno
# ifdef OF_HAVE_SOCKETS
#  if !defined(HAVE_THREADSAFE_GETADDRINFO) && defined(HAVE_H_ERRNO)
#   define GET_AT_ERRNO	h_errno
#  else
#   define GET_AT_ERRNO	errno
#  endif
# define GET_SOCK_ERRNO	errno
# endif
# define ERRFMT			@"Error string was: %s"
# define ERRPARAM		strerror(_errNo)
# ifdef OF_HAVE_SOCKETS
#  if !defined(HAVE_THREADSAFE_GETADDRINFO) && defined(HAVE_HSTRERROR)
#   define AT_ERRPARAM		hstrerror(_errNo)
#  else
#   define AT_ERRPARAM		strerror(_errNo)
#  endif
# endif
#else
# include <windows.h>







|









|







22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#ifdef OF_HAVE_SOCKETS
# include "socket_helpers.h"
#endif

#ifndef _WIN32
# define GET_ERRNO	errno
# ifdef OF_HAVE_SOCKETS
#  if !defined(HAVE_GETADDRINFO) && defined(HAVE_H_ERRNO)
#   define GET_AT_ERRNO	h_errno
#  else
#   define GET_AT_ERRNO	errno
#  endif
# define GET_SOCK_ERRNO	errno
# endif
# define ERRFMT			@"Error string was: %s"
# define ERRPARAM		strerror(_errNo)
# ifdef OF_HAVE_SOCKETS
#  if !defined(HAVE_GETADDRINFO) && defined(HAVE_HSTRERROR)
#   define AT_ERRPARAM		hstrerror(_errNo)
#  else
#   define AT_ERRPARAM		strerror(_errNo)
#  endif
# endif
#else
# include <windows.h>

Modified src/resolver.m from [f6383b3c0c] to [d1d2c810b6].

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
227
228
229
230
231






232
233
234
235
236
237
238
239
240
241
242
243
244

245
246
247
248

249
250
251






252
253
254
255
256
257
258
259
260
261
262


263
264
265
266
267
268
269
270
271
272
273
274

275
276

277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
	hints.ai_socktype = type;
	hints.ai_flags = AI_NUMERICSERV;
	snprintf(portCString, 7, "%" PRIu16, port);

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


# endif

	if (getaddrinfo([host UTF8String], portCString, &hints, &res0))
		@throw [OFAddressTranslationFailedException
		    exceptionWithHost: host];

	count = 0;
	for (res = res0; res != NULL; res = res->ai_next)
		count++;

	if (count == 0) {
		freeaddrinfo(res0);
		@throw [OFAddressTranslationFailedException
		    exceptionWithHost: host];
	}

	if ((ret = calloc(count + 1, sizeof(*ret))) == NULL)

		@throw [OFOutOfMemoryException
		    exceptionWithRequestedSize: (count + 1) * sizeof(*ret)];



	if ((results = malloc(count * sizeof(*results))) == NULL)


		@throw [OFOutOfMemoryException
		    exceptionWithRequestedSize: count * sizeof(*results)];



	for (retIter = ret, resultsIter = results, res = res0;
	    res != NULL; retIter++, resultsIter++, res = res->ai_next) {
		resultsIter->family = res->ai_family;
		resultsIter->type = res->ai_socktype;
		resultsIter->protocol = res->ai_protocol;
		resultsIter->address = res->ai_addr;
		resultsIter->addressLength = res->ai_addrlen;

		*retIter = resultsIter;
	}
	*retIter = NULL;

	ret[0]->private_ = res0;

# if !defined(HAVE_THREADSAFE_GETADDRINFO) && defined(OF_HAVE_THREADS)

	if (!of_mutex_unlock(&mutex))
		@throw [OFUnlockFailedException exception];

# endif
#else
	struct hostent *he;
	in_addr_t s_addr;
	char **ip;
	struct sockaddr_in *addrs, *addrsIter;

	/*
	 * If the host is an IP address, don't try resolving it.
	 * On the Wii for example, the resolver will return an error if you
	 * specify an IP address.
	 */
	if ((s_addr = inet_addr([host UTF8String])) != INADDR_NONE) {
		of_resolver_result_t *tmp;
		struct sockaddr_in *addr;

		if ((ret = calloc(2, sizeof(*ret))) == NULL)
			@throw [OFOutOfMemoryException
			    exceptionWithRequestedSize: 2 * sizeof(*ret)];

		if ((tmp = malloc(sizeof(*tmp))) == NULL)

			@throw [OFOutOfMemoryException
			    exceptionWithRequestedSize: sizeof(*tmp)];


		if ((addr = calloc(1, sizeof(*addr))) == NULL)


			@throw [OFOutOfMemoryException
			    exceptionWithRequestedSize: sizeof(*addr)];


		addr->sin_family = AF_INET;
		addr->sin_port = OF_BSWAP16_IF_LE(port);
		addr->sin_addr.s_addr = s_addr;

		tmp->family = AF_INET;
		tmp->type = type;
		tmp->protocol = 0;
		tmp->address = (struct sockaddr*)addr;
		tmp->addressLength = sizeof(*addr);

		ret[0] = tmp;
		ret[1] = NULL;

		return ret;
	}

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



	if ((he = gethostbyname([host UTF8String])) == NULL ||
	    he->h_addrtype != AF_INET) {
# ifdef OF_HAVE_THREADS
		if (!of_mutex_unlock(&mutex))
			@throw [OFUnlockFailedException exception];
# endif

		@throw [OFAddressTranslationFailedException
		    exceptionWithHost: host];
	}

	count = 0;
	for (ip = he->h_addr_list; *ip != NULL; ip++)
		count++;

	if (count == 0)
		@throw [OFAddressTranslationFailedException
		    exceptionWithHost: host];

	if ((ret = calloc(count + 1, sizeof(*ret))) == NULL)
		@throw [OFOutOfMemoryException
		    exceptionWithRequestedSize: (count + 1) * sizeof(*ret)];


	if ((results = malloc(count * sizeof(*results))) == NULL)

		@throw [OFOutOfMemoryException
		    exceptionWithRequestedSize: count * sizeof(*results)];



	if ((addrs = calloc(count, sizeof(*addrs))) == NULL)


		@throw [OFOutOfMemoryException
		    exceptionWithRequestedSize: count * sizeof(*addrs)];


	for (retIter = ret, resultsIter = results, addrsIter = addrs,
	    ip = he->h_addr_list; *ip != NULL; retIter++, resultsIter++,
	    addrsIter++, ip++) {
		addrsIter->sin_family = he->h_addrtype;
		addrsIter->sin_port = OF_BSWAP16_IF_LE(port);

		if (he->h_length > sizeof(addrsIter->sin_addr.s_addr))
			@throw [OFOutOfRangeException exception];

		memcpy(&addrsIter->sin_addr.s_addr, *ip, he->h_length);

		resultsIter->family = he->h_addrtype;
		resultsIter->type = type;
		resultsIter->protocol = 0;
		resultsIter->address = (struct sockaddr*)addrsIter;
		resultsIter->addressLength = sizeof(*addrsIter);

		*retIter = resultsIter;
	}

# ifdef OF_HAVE_THREADS

	if (!of_mutex_unlock(&mutex))
		@throw [OFUnlockFailedException exception];

# endif
#endif

	return ret;
}

void
of_address_to_string_and_port(struct sockaddr *address, socklen_t addressLength,
    OFString *__autoreleasing *host, uint16_t *port)
{
#ifdef HAVE_THREADSAFE_GETADDRINFO
	char hostCString[NI_MAXHOST];
	char portCString[NI_MAXSERV];







	/* FIXME: Add NI_DGRAM for UDP? */
	if (getnameinfo(address, addressLength, hostCString, NI_MAXHOST,
	    portCString, NI_MAXSERV, NI_NUMERICHOST | NI_NUMERICSERV))
		@throw [OFAddressTranslationFailedException exception];

	if (host != NULL)
		*host = [OFString stringWithUTF8String: hostCString];

	if (port != NULL) {
		char *endptr;
		long tmp;

		if ((tmp = strtol(portCString, &endptr, 10)) > UINT16_MAX)

			@throw [OFOutOfRangeException exception];

		if (endptr != NULL && *endptr != '\0')
			@throw [OFAddressTranslationFailedException exception];


		*port = (uint16_t)tmp;
	}






#else
	char *hostCString;

	if (address->sa_family != AF_INET)
		@throw [OFInvalidArgumentException exception];

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



	if ((hostCString = inet_ntoa(
	    ((struct sockaddr_in*)(void*)address)->sin_addr)) == NULL)
		@throw [OFAddressTranslationFailedException exception];

	if (host != NULL)
		*host = [OFString stringWithUTF8String: hostCString];

	if (port != NULL)
		*port = OF_BSWAP16_IF_LE(
		    ((struct sockaddr_in*)(void*)address)->sin_port);

# if OF_HAVE_THREADS

	if (!of_mutex_unlock(&mutex))
		@throw [OFUnlockFailedException exception];

# endif
#endif
}

void
of_resolver_free(of_resolver_result_t **results)
{
#ifdef HAVE_THREADSAFE_GETADDRINFO
	freeaddrinfo(results[0]->private_);
#else
	free(results[0]->address);
#endif
	free(results[0]);
	free(results);
}







>
>

<
|
|
|

|
|
|

|
|
|
|
|

|
>
|
|
>
|
>
|
>
>
|
|
>
|
>
|
|
|
|
|
|
|

|
|
|

|
<

>
|
|
>




















|
>


|
>
|
>
>


>




















<

>
>
|
|
<
<
<
<
<
|
|
<

|
|
|

|
|
|

|
|
|
>

|
>
|
|
>
|
>
|
>
>
|
|
|
>
|
|
|
|
|

|
|

|

|
|
|
|
|

|
|
<

>
|
|
>










|



>
>
>
>
>
>
|
|
|
|

|
|

|
|
|

|
>
|

|
|
>

|
|
>
>
>
>
>
>









<

>
>
|
|
|

|
|

|
|
|
<

>
|
|
>







|







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
227
228
229

230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291

292
293
294
295
296
297
298
299
300
301
302
303
304

305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
	hints.ai_socktype = type;
	hints.ai_flags = AI_NUMERICSERV;
	snprintf(portCString, 7, "%" PRIu16, port);

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

	@try {
# endif

		if (getaddrinfo([host UTF8String], portCString, &hints, &res0))
			@throw [OFAddressTranslationFailedException
			    exceptionWithHost: host];

		count = 0;
		for (res = res0; res != NULL; res = res->ai_next)
			count++;

		if (count == 0) {
			freeaddrinfo(res0);
			@throw [OFAddressTranslationFailedException
			    exceptionWithHost: host];
		}

		if ((ret = calloc(count + 1, sizeof(*ret))) == NULL) {
			freeaddrinfo(res0);
			@throw [OFOutOfMemoryException
			    exceptionWithRequestedSize:
			    (count + 1) * sizeof(*ret)];
		}

		if ((results = malloc(count * sizeof(*results))) == NULL) {
			freeaddrinfo(res0);
			free(ret);
			@throw [OFOutOfMemoryException
			    exceptionWithRequestedSize:
			    count * sizeof(*results)];
		}

		for (retIter = ret, resultsIter = results, res = res0;
		    res != NULL; retIter++, resultsIter++, res = res->ai_next) {
			resultsIter->family = res->ai_family;
			resultsIter->type = res->ai_socktype;
			resultsIter->protocol = res->ai_protocol;
			resultsIter->address = res->ai_addr;
			resultsIter->addressLength = res->ai_addrlen;

			*retIter = resultsIter;
		}
		*retIter = NULL;

		ret[0]->private_ = res0;

# if !defined(HAVE_THREADSAFE_GETADDRINFO) && defined(OF_HAVE_THREADS)
	} @finally {
		if (!of_mutex_unlock(&mutex))
			@throw [OFUnlockFailedException exception];
	}
# endif
#else
	struct hostent *he;
	in_addr_t s_addr;
	char **ip;
	struct sockaddr_in *addrs, *addrsIter;

	/*
	 * If the host is an IP address, don't try resolving it.
	 * On the Wii for example, the resolver will return an error if you
	 * specify an IP address.
	 */
	if ((s_addr = inet_addr([host UTF8String])) != INADDR_NONE) {
		of_resolver_result_t *tmp;
		struct sockaddr_in *addr;

		if ((ret = calloc(2, sizeof(*ret))) == NULL)
			@throw [OFOutOfMemoryException
			    exceptionWithRequestedSize: 2 * sizeof(*ret)];

		if ((tmp = malloc(sizeof(*tmp))) == NULL) {
			free(ret);
			@throw [OFOutOfMemoryException
			    exceptionWithRequestedSize: sizeof(*tmp)];
		}

		if ((addr = calloc(1, sizeof(*addr))) == NULL) {
			free(ret);
			free(tmp);
			@throw [OFOutOfMemoryException
			    exceptionWithRequestedSize: sizeof(*addr)];
		}

		addr->sin_family = AF_INET;
		addr->sin_port = OF_BSWAP16_IF_LE(port);
		addr->sin_addr.s_addr = s_addr;

		tmp->family = AF_INET;
		tmp->type = type;
		tmp->protocol = 0;
		tmp->address = (struct sockaddr*)addr;
		tmp->addressLength = sizeof(*addr);

		ret[0] = tmp;
		ret[1] = NULL;

		return ret;
	}

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


	@try {
# endif
		if ((he = gethostbyname([host UTF8String])) == NULL ||
		    he->h_addrtype != AF_INET)





			@throw [OFAddressTranslationFailedException
			    exceptionWithHost: host];


		count = 0;
		for (ip = he->h_addr_list; *ip != NULL; ip++)
			count++;

		if (count == 0)
			@throw [OFAddressTranslationFailedException
			    exceptionWithHost: host];

		if ((ret = calloc(count + 1, sizeof(*ret))) == NULL)
			@throw [OFOutOfMemoryException
			    exceptionWithRequestedSize:
			    (count + 1) * sizeof(*ret)];

		if ((results = malloc(count * sizeof(*results))) == NULL) {
			free(ret);
			@throw [OFOutOfMemoryException
			    exceptionWithRequestedSize:
			    count * sizeof(*results)];
		}

		if ((addrs = calloc(count, sizeof(*addrs))) == NULL) {
			free(ret);
			free(results);
			@throw [OFOutOfMemoryException
			    exceptionWithRequestedSize: count * sizeof(*addrs)];
		}

		for (retIter = ret, resultsIter = results, addrsIter = addrs,
		    ip = he->h_addr_list; *ip != NULL;
		    retIter++, resultsIter++, addrsIter++, ip++) {
			addrsIter->sin_family = he->h_addrtype;
			addrsIter->sin_port = OF_BSWAP16_IF_LE(port);

			if (he->h_length > sizeof(addrsIter->sin_addr.s_addr))
				@throw [OFOutOfRangeException exception];

			memcpy(&addrsIter->sin_addr.s_addr, *ip, he->h_length);

			resultsIter->family = he->h_addrtype;
			resultsIter->type = type;
			resultsIter->protocol = 0;
			resultsIter->address = (struct sockaddr*)addrsIter;
			resultsIter->addressLength = sizeof(*addrsIter);

			*retIter = resultsIter;
		}

# ifdef OF_HAVE_THREADS
	} @finally {
		if (!of_mutex_unlock(&mutex))
			@throw [OFUnlockFailedException exception];
	}
# endif
#endif

	return ret;
}

void
of_address_to_string_and_port(struct sockaddr *address, socklen_t addressLength,
    OFString *__autoreleasing *host, uint16_t *port)
{
#ifdef HAVE_GETADDRINFO
	char hostCString[NI_MAXHOST];
	char portCString[NI_MAXSERV];

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

	@try {
# endif
		/* FIXME: Add NI_DGRAM for UDP? */
		if (getnameinfo(address, addressLength, hostCString, NI_MAXHOST,
		    portCString, NI_MAXSERV, NI_NUMERICHOST | NI_NUMERICSERV))
			@throw [OFAddressTranslationFailedException exception];

		if (host != NULL)
			*host = [OFString stringWithUTF8String: hostCString];

		if (port != NULL) {
			char *endptr;
			long tmp;

			if ((tmp = strtol(portCString, &endptr, 10)) >
			    UINT16_MAX)
				@throw [OFOutOfRangeException exception];

			if (endptr != NULL && *endptr != '\0')
				@throw [OFAddressTranslationFailedException
				    exception];

			*port = (uint16_t)tmp;
		}
# if !defined(HAVE_THREADSAFE_GETADDRINFO) && defined(OF_HAVE_THREADS)
	} @finally {
		if (!of_mutex_unlock(&mutex))
			@throw [OFUnlockFailedException exception];
	}
# endif
#else
	char *hostCString;

	if (address->sa_family != AF_INET)
		@throw [OFInvalidArgumentException exception];

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


	@try {
# endif
		if ((hostCString = inet_ntoa(
		    ((struct sockaddr_in*)(void*)address)->sin_addr)) == NULL)
			@throw [OFAddressTranslationFailedException exception];

		if (host != NULL)
			*host = [OFString stringWithUTF8String: hostCString];

		if (port != NULL)
			*port = OF_BSWAP16_IF_LE(
			    ((struct sockaddr_in*)(void*)address)->sin_port);

# if OF_HAVE_THREADS
	} @finally {
		if (!of_mutex_unlock(&mutex))
			@throw [OFUnlockFailedException exception];
	}
# endif
#endif
}

void
of_resolver_free(of_resolver_result_t **results)
{
#ifdef HAVE_GETADDRINFO
	freeaddrinfo(results[0]->private_);
#else
	free(results[0]->address);
#endif
	free(results[0]);
	free(results);
}

Modified src/socket_helpers.h from [6a92767d43] to [278174d7fd].

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# include <netdb.h>
#endif

#ifndef INVALID_SOCKET
# define INVALID_SOCKET -1
#endif

#ifdef HAVE_THREADSAFE_GETADDRINFO
# ifndef AI_NUMERICSERV
#  define AI_NUMERICSERV 0
# endif
# ifndef AI_NUMERICHOST
#  define AI_NUMERICHOST 0
# endif
#endif







|







26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# include <netdb.h>
#endif

#ifndef INVALID_SOCKET
# define INVALID_SOCKET -1
#endif

#ifdef HAVE_GETADDRINFO
# ifndef AI_NUMERICSERV
#  define AI_NUMERICSERV 0
# endif
# ifndef AI_NUMERICHOST
#  define AI_NUMERICHOST 0
# endif
#endif