ObjFW  Diff

Differences From Artifact [aef6c3578a]:

To Artifact [804f167c59]:


10
11
12
13
14
15
16





17
18
19
20
21
22
23
24






25








26
27
28
29
30
31
32
 */

#include "config.h"

#include <stdio.h>
#include <string.h>
#include <unistd.h>






#import "OFTCPSocket.h"
#import "OFExceptions.h"

#ifndef INVALID_SOCKET
#define INVALID_SOCKET -1
#endif







@implementation OFTCPSocket








+ socket
{
	return [[[OFTCPSocket alloc] init] autorelease];
}

- init
{







>
>
>
>
>








>
>
>
>
>
>

>
>
>
>
>
>
>
>







10
11
12
13
14
15
16
17
18
19
20
21
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
47
48
49
50
51
 */

#include "config.h"

#include <stdio.h>
#include <string.h>
#include <unistd.h>

#ifndef HAVE_GETADDRINFO
#include <stdlib.h>
#include <arpa/inet.h>
#endif

#import "OFTCPSocket.h"
#import "OFExceptions.h"

#ifndef INVALID_SOCKET
#define INVALID_SOCKET -1
#endif

#ifndef HAVE_GETADDRINFO
#import "OFThread.h"

static OFObject *lock = nil;
#endif

@implementation OFTCPSocket
#ifndef HAVE_GETADDRINFO
+ (void)initialize
{
	if (self == [OFTCPSocket class])
		lock = [[OFObject alloc] init];
}
#endif

+ socket
{
	return [[[OFTCPSocket alloc] init] autorelease];
}

- init
{
45
46
47
48
49
50
51
52
53
54
55
56



57
58
59
60
61
62
63

	[super dealloc];
}

- connectToService: (OFString*)service
	    onNode: (OFString*)node
{
	struct addrinfo hints, *res, *res0;

	if (sock != INVALID_SOCKET)
		@throw [OFAlreadyConnectedException newWithClass: isa];




	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;

	if (getaddrinfo([node cString], [service cString], &hints, &res0))
		@throw [OFAddressTranslationFailedException
		    newWithClass: isa







<
<



>
>
>







64
65
66
67
68
69
70


71
72
73
74
75
76
77
78
79
80
81
82
83

	[super dealloc];
}

- connectToService: (OFString*)service
	    onNode: (OFString*)node
{


	if (sock != INVALID_SOCKET)
		@throw [OFAlreadyConnectedException newWithClass: isa];

#ifdef HAVE_GETADDRINFO
	struct addrinfo hints, *res, *res0;

	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;

	if (getaddrinfo([node cString], [service cString], &hints, &res0))
		@throw [OFAddressTranslationFailedException
		    newWithClass: isa
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
			continue;
		}

		break;
	}

	freeaddrinfo(res0);





















































	if (sock == INVALID_SOCKET)
		@throw [OFConnectionFailedException newWithClass: isa
							 andNode: node
						      andService: service];

	return self;
}

- bindService: (OFString*)service
       onNode: (OFString*)node
   withFamily: (int)family
{
	struct addrinfo hints, *res;

	if (sock != INVALID_SOCKET)
		@throw [OFAlreadyConnectedException newWithClass: isa];

	if ((sock = socket(family, SOCK_STREAM, 0)) == INVALID_SOCKET)
		@throw [OFBindFailedException newWithClass: isa
						   andNode: node
						andService: service
						 andFamily: family];




	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = family;
	hints.ai_socktype = SOCK_STREAM;

	if (getaddrinfo([node cString], [service cString], &hints, &res))
		@throw [OFAddressTranslationFailedException
		    newWithClass: isa
			 andNode: node
		      andService: service];

	if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
		freeaddrinfo(res);
		@throw [OFBindFailedException newWithClass: isa
						   andNode: node
						andService: service
						 andFamily: family];
	}

	freeaddrinfo(res);















































	return self;
}

- listenWithBackLog: (int)backlog
{
	if (sock == INVALID_SOCKET)







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>













<
<









>
>
>



















>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
			continue;
		}

		break;
	}

	freeaddrinfo(res0);
#else
	BOOL connected = NO;
	struct hostent *he;
	struct servent *se;
	struct sockaddr_in addr;
	uint16_t port;
	char **ip;

	@synchronized (lock) {
		if ((he = gethostbyname([node cString])) == NULL)
			@throw [OFAddressTranslationFailedException
			    newWithClass: isa
				 andNode: node
			      andService: service];

		if ((se = getservbyname([service cString], "TCP")) != NULL)
			port = se->s_port;
		else if ((port = htons(atoi([service cString]))) == 0)
			@throw [OFAddressTranslationFailedException
			    newWithClass: isa
				 andNode: node
			      andService: service];

		memset(&addr, 0, sizeof(addr));
		addr.sin_family = AF_INET;
		addr.sin_port = port;

		if (he->h_addrtype != AF_INET ||
		    (sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
			@throw [OFConnectionFailedException
			    newWithClass: isa
				 andNode: node
			      andService: service];

		for (ip = he->h_addr_list; *ip != NULL; ip++) {
			memcpy(&addr.sin_addr.s_addr, *ip, he->h_length);

			if (connect(sock, (struct sockaddr*)&addr,
			    sizeof(addr)) == -1)
				continue;

			connected = YES;

			break;
		}

		if (!connected) {
			close(sock);
			sock = INVALID_SOCKET;
		}
	}
#endif

	if (sock == INVALID_SOCKET)
		@throw [OFConnectionFailedException newWithClass: isa
							 andNode: node
						      andService: service];

	return self;
}

- bindService: (OFString*)service
       onNode: (OFString*)node
   withFamily: (int)family
{


	if (sock != INVALID_SOCKET)
		@throw [OFAlreadyConnectedException newWithClass: isa];

	if ((sock = socket(family, SOCK_STREAM, 0)) == INVALID_SOCKET)
		@throw [OFBindFailedException newWithClass: isa
						   andNode: node
						andService: service
						 andFamily: family];

#ifdef HAVE_GETADDRINFO
	struct addrinfo hints, *res;

	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = family;
	hints.ai_socktype = SOCK_STREAM;

	if (getaddrinfo([node cString], [service cString], &hints, &res))
		@throw [OFAddressTranslationFailedException
		    newWithClass: isa
			 andNode: node
		      andService: service];

	if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
		freeaddrinfo(res);
		@throw [OFBindFailedException newWithClass: isa
						   andNode: node
						andService: service
						 andFamily: family];
	}

	freeaddrinfo(res);
#else
	struct hostent *he;
	struct servent *se;
	struct sockaddr_in addr;
	uint16_t port;

	if (family != AF_INET)
		@throw [OFBindFailedException newWithClass: isa
						   andNode: node
						andService: service
						 andFamily: family];

	@synchronized (lock) {
		if ((he = gethostbyname([node cString])) == NULL)
			@throw [OFAddressTranslationFailedException
			    newWithClass: isa
				 andNode: node
			      andService: service];

		if ((se = getservbyname([service cString], "TCP")) != NULL)
			port = se->s_port;
		else if ((port = htons(atoi([service cString]))) == 0)
			@throw [OFAddressTranslationFailedException
			    newWithClass: isa
				 andNode: node
			      andService: service];

		memset(&addr, 0, sizeof(addr));
		addr.sin_family = AF_INET;
		addr.sin_port = port;

		if (he->h_addrtype != AF_INET || he->h_addr_list[0] == NULL)
			@throw [OFAddressTranslationFailedException
			    newWithClass: isa
				 andNode: node
			      andService: service];

		memcpy(&addr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);

		if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1)
			@throw [OFBindFailedException newWithClass: isa
							   andNode: node
							andService: service
							 andFamily: family];
	}
#endif

	return self;
}

- listenWithBackLog: (int)backlog
{
	if (sock == INVALID_SOCKET)