ObjFW  Check-in [ec3c199d1a]

Overview
Comment:Fix two more FIXMEs in OFTCPSocket; new exception.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: ec3c199d1af7889cb2860bb4846c71673dc2c081785af3ec38e758d8acafef1c
User & Date: js on 2008-12-13 23:11:25
Other Links: manifest | tags
Context
2008-12-14
01:45
Two new exceptions; fix 3 FIXMEs in OFTCPSocket. check-in: af9e349898 user: js tags: trunk
2008-12-13
23:11
Fix two more FIXMEs in OFTCPSocket; new exception. check-in: ec3c199d1a user: js tags: trunk
22:58
Don't compile asprintf.c if not needed. check-in: a6c1870058 user: js tags: trunk
Changes

Modified src/OFExceptions.h from [b6b180dc1e] to [9bfba73e23].

398
399
400
401
402
403
404

























































- (const char*)host;

/**
 * \return The port on the host to which the connection failed
 */
- (uint16_t)port;
@end
































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
- (const char*)host;

/**
 * \return The port on the host to which the connection failed
 */
- (uint16_t)port;
@end

/**
 * An OFException indicating that binding the socket failed.
 */
@interface OFBindFailedException: OFException
{
	char *host;
	uint16_t port;
	int family;
}

/**
 * \param h The host on which binding failed
 * \param p The port on which binding failed
 * \param f The family for which binnding failed
 * \return A new bind failed exception
 */
+ newWithObject: (id)obj
	andHost: (const char*)h
	andPort: (uint16_t)p
      andFamily: (int)f;

/**
 * Initializes an already allocated bind failed exception.
 *
 * \param h The host on which binding failed
 * \param p The port on which binding failed
 * \param f The family for which binnding failed
 * \return An initialized bind failed exception
 */
- initWithObject: (id)obj
	 andHost: (const char*)h
	 andPort: (uint16_t)p
       andFamily: (int)f;

- free;

/**
 * \return An error message for the exception as a C string.
 */
- (const char*)cString;

/**
 * \return The host on which binding failed
 */
- (const char*)host;

/**
 * \return The port on which binding failed
 */
- (uint16_t)port;

/**
 * \return The family for which binding failed
 */
- (int)family;
@end

Modified src/OFExceptions.m from [fe916bd7cf] to [0baa314b8c].

454
455
456
457
458
459
460





























































}

- (uint16_t)port
{
	return port;
}
@end




































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
}

- (uint16_t)port
{
	return port;
}
@end

@implementation OFBindFailedException
+ newWithObject: (id)obj
	andHost: (const char*)h
	andPort: (uint16_t)p
      andFamily: (int)f
{
	return [self newWithObject: obj
			   andHost: h
			   andPort: p
			 andFamily: f];
}

- initWithObject: (id)obj
	 andHost: (const char*)h
	 andPort: (uint16_t)p
       andFamily: (int)f
{
	if ((self = [super initWithObject: obj])) {
		host = (h != NULL ? strdup(h) : NULL);
		port = p;
		family = f;
	}

	return self;
}

- free
{
	if (host != NULL)
		free(host);

	return [super free];
}

- (const char*)cString
{
	if (string != NULL)
		return string;

	asprintf(&string, "Binding to port %d on %s using family %d failed in "
	    "object of type %s!", port, host, family, [object name]);

	return string;
}

- (const char*)host
{
	return host;
}

- (uint16_t)port
{
	return port;
}

- (int)family
{
	return family;
}
@end

Modified src/OFTCPSocket.m from [eaaeb57c56] to [4835d59f11].

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

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

	if (sock >= 0)
		@throw [OFAlreadyConnectedException newWithObject: self];

	if ((sock = socket(family, SOCK_STREAM, 0)) < 0) {
		/* FIXME: Throw exception */


		return nil;
	}

	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
		    newWithObject: self
			  andNode: host
		       andService: portstr];

	if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) {
		/* FIXME: Throw exception */
		freeaddrinfo(res);



		return nil;
	}

	freeaddrinfo(res);

	return self;
}








|
|
>
>
|
<














<

>
>
>
|







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

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

	if (sock >= 0)
		@throw [OFAlreadyConnectedException newWithObject: self];

	if ((sock = socket(family, SOCK_STREAM, 0)) < 0)
		@throw [OFBindFailedException newWithObject: self
						    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
		    newWithObject: self
			  andNode: host
		       andService: portstr];

	if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) {

		freeaddrinfo(res);
		@throw [OFBindFailedException newWithObject: self
						    andHost: host
						    andPort: port
						  andFamily: family];
	}

	freeaddrinfo(res);

	return self;
}