ObjFW  Diff

Differences From Artifact [b0a40b2d84]:

To Artifact [9dddc5248f]:


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
- connectTo: (const char*)host
     onPort: (uint16_t)port
{
	struct addrinfo hints, *res, *res0;
	char portstr[6];

	if (!port)
		@throw [OFInvalidPortException newWithClass: [self class]];

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

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

	snprintf(portstr, 6, "%d", port);

	if (getaddrinfo(host, portstr, &hints, &res0))
		@throw [OFAddressTranslationFailedException
		    newWithClass: [self class]
			 andNode: host
		      andService: portstr];

	for (res = res0; res != NULL; res = res->ai_next) {
		if ((sock = socket(res->ai_family, res->ai_socktype,
		    res->ai_protocol)) == INVALID_SOCKET)
			continue;

		if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
			close(sock);
			sock = INVALID_SOCKET;
			continue;
		}

		break;
	}

	freeaddrinfo(res0);

	if (sock == INVALID_SOCKET)
		@throw [OFConnectionFailedException newWithClass: [self class]
							 andHost: host
							 andPort: port];

	return self;
}

-    bindOn: (const char*)host
   withPort: (uint16_t)port
  andFamily: (int)family
{
	struct addrinfo hints, *res;
	char portstr[6];

	if (!port)
		@throw [OFInvalidPortException newWithClass: [self class]];

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

	if ((sock = socket(family, SOCK_STREAM, 0)) == INVALID_SOCKET)
		@throw [OFBindFailedException newWithClass: [self class]
						   andHost: host
						   andPort: port
						 andFamily: family];

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

	snprintf(portstr, 6, "%d", port);

	if (getaddrinfo(host, portstr, &hints, &res))
		@throw [OFAddressTranslationFailedException
		    newWithClass: [self class]
			 andNode: host
		      andService: portstr];

	if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
		freeaddrinfo(res);
		@throw [OFBindFailedException newWithClass: [self class]
						   andHost: host
						   andPort: port
						 andFamily: family];
	}

	freeaddrinfo(res);

	return self;
}

- listenWithBackLog: (int)backlog
{
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: [self class]];

	if (listen(sock, backlog) == -1)
		@throw [OFListenFailedException newWithClass: [self class]
						  andBackLog: backlog];

	return self;
}

- listen
{
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: [self class]];

	if (listen(sock, 5) == -1)
		@throw [OFListenFailedException newWithClass: [self class]
						  andBackLog: 5];

	return self;
}

- (OFTCPSocket*)accept
{







|


|









|




















|














|


|


|












|





|













|


|








|


|







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
- connectTo: (const char*)host
     onPort: (uint16_t)port
{
	struct addrinfo hints, *res, *res0;
	char portstr[6];

	if (!port)
		@throw [OFInvalidPortException newWithClass: isa];

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

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

	snprintf(portstr, 6, "%d", port);

	if (getaddrinfo(host, portstr, &hints, &res0))
		@throw [OFAddressTranslationFailedException
		    newWithClass: isa
			 andNode: host
		      andService: portstr];

	for (res = res0; res != NULL; res = res->ai_next) {
		if ((sock = socket(res->ai_family, res->ai_socktype,
		    res->ai_protocol)) == INVALID_SOCKET)
			continue;

		if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
			close(sock);
			sock = INVALID_SOCKET;
			continue;
		}

		break;
	}

	freeaddrinfo(res0);

	if (sock == INVALID_SOCKET)
		@throw [OFConnectionFailedException newWithClass: isa
							 andHost: host
							 andPort: port];

	return self;
}

-    bindOn: (const char*)host
   withPort: (uint16_t)port
  andFamily: (int)family
{
	struct addrinfo hints, *res;
	char portstr[6];

	if (!port)
		@throw [OFInvalidPortException newWithClass: isa];

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

	if ((sock = socket(family, SOCK_STREAM, 0)) == INVALID_SOCKET)
		@throw [OFBindFailedException newWithClass: isa
						   andHost: host
						   andPort: port
						 andFamily: family];

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

	snprintf(portstr, 6, "%d", port);

	if (getaddrinfo(host, portstr, &hints, &res))
		@throw [OFAddressTranslationFailedException
		    newWithClass: isa
			 andNode: host
		      andService: portstr];

	if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
		freeaddrinfo(res);
		@throw [OFBindFailedException newWithClass: isa
						   andHost: host
						   andPort: port
						 andFamily: family];
	}

	freeaddrinfo(res);

	return self;
}

- listenWithBackLog: (int)backlog
{
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: isa];

	if (listen(sock, backlog) == -1)
		@throw [OFListenFailedException newWithClass: isa
						  andBackLog: backlog];

	return self;
}

- listen
{
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: isa];

	if (listen(sock, 5) == -1)
		@throw [OFListenFailedException newWithClass: isa
						  andBackLog: 5];

	return self;
}

- (OFTCPSocket*)accept
{
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
	} @catch(id e) {
		[newsock free];
		@throw e;
	}

	if ((s = accept(sock, addr, &addrlen)) == INVALID_SOCKET) {
		[newsock free];
		@throw [OFAcceptFailedException newWithClass: [self class]];
	}

	[newsock setSocket: s];
	[newsock setSocketAddress: addr
		       withLength: addrlen];

	return newsock;
}

- (size_t)readNBytes: (size_t)size
	  intoBuffer: (char*)buf
{
	ssize_t ret;

	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: [self class]];

	switch ((ret = recv(sock, buf, size, 0))) {
	case 0:
		@throw [OFNotConnectedException newWithClass: [self class]];
	case -1:
		@throw [OFReadFailedException newWithClass: [self class]
						   andSize: size];
	}

	/* This is safe, as we already checked < 1 */
	return ret;
}

- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (const char*)buf
{
	ssize_t ret;

	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: [self class]];

	if ((ret = send(sock, buf, size, 0)) == -1)
		@throw [OFWriteFailedException newWithClass: [self class]
						    andSize: size];

	/* This is safe, as we already checked for -1 */
	return ret;
}

- (size_t)writeCString: (const char*)str
{
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: [self class]];

	return [self writeNBytes: strlen(str)
		      fromBuffer: str];
}

- setBlocking: (BOOL)enable
{
#ifndef _WIN32
	int flags;

	if ((flags = fcntl(sock, F_GETFL)) == -1)
		@throw [OFSetOptionFailedException newWithClass: [self class]];

	if (enable)
		flags &= ~O_NONBLOCK;
	else
		flags |= O_NONBLOCK;

	if (fcntl(sock, F_SETFL, flags) == -1)
		@throw [OFSetOptionFailedException newWithClass: [self class]];
#else
	u_long v = enable;

	if (ioctlsocket(sock, FIONBIO, &v) == SOCKET_ERROR)
		@throw [OFSetOptionFailedException newWithClass: [self class]];
#endif

	return self;
}

- enableKeepAlives: (BOOL)enable
{
	int v = enable;

	if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char*)&v, sizeof(v)))
		@throw [OFSetOptionFailedException newWithClass: [self class]];

	return self;
}

- close
{
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: [self class]];

	sock = INVALID_SOCKET;

	if (saddr != NULL)
		[self freeMem: saddr];
	saddr_len = 0;

	return self;
}
@end







|















|



|

|













|


|









|











|







|




|










|







|










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
	} @catch(id e) {
		[newsock free];
		@throw e;
	}

	if ((s = accept(sock, addr, &addrlen)) == INVALID_SOCKET) {
		[newsock free];
		@throw [OFAcceptFailedException newWithClass: isa];
	}

	[newsock setSocket: s];
	[newsock setSocketAddress: addr
		       withLength: addrlen];

	return newsock;
}

- (size_t)readNBytes: (size_t)size
	  intoBuffer: (char*)buf
{
	ssize_t ret;

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

	switch ((ret = recv(sock, buf, size, 0))) {
	case 0:
		@throw [OFNotConnectedException newWithClass: isa];
	case -1:
		@throw [OFReadFailedException newWithClass: isa
						   andSize: size];
	}

	/* This is safe, as we already checked < 1 */
	return ret;
}

- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (const char*)buf
{
	ssize_t ret;

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

	if ((ret = send(sock, buf, size, 0)) == -1)
		@throw [OFWriteFailedException newWithClass: isa
						    andSize: size];

	/* This is safe, as we already checked for -1 */
	return ret;
}

- (size_t)writeCString: (const char*)str
{
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: isa];

	return [self writeNBytes: strlen(str)
		      fromBuffer: str];
}

- setBlocking: (BOOL)enable
{
#ifndef _WIN32
	int flags;

	if ((flags = fcntl(sock, F_GETFL)) == -1)
		@throw [OFSetOptionFailedException newWithClass: isa];

	if (enable)
		flags &= ~O_NONBLOCK;
	else
		flags |= O_NONBLOCK;

	if (fcntl(sock, F_SETFL, flags) == -1)
		@throw [OFSetOptionFailedException newWithClass: isa];
#else
	u_long v = enable;

	if (ioctlsocket(sock, FIONBIO, &v) == SOCKET_ERROR)
		@throw [OFSetOptionFailedException newWithClass: isa];
#endif

	return self;
}

- enableKeepAlives: (BOOL)enable
{
	int v = enable;

	if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char*)&v, sizeof(v)))
		@throw [OFSetOptionFailedException newWithClass: isa];

	return self;
}

- close
{
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: isa];

	sock = INVALID_SOCKET;

	if (saddr != NULL)
		[self freeMem: saddr];
	saddr_len = 0;

	return self;
}
@end