ObjFW  Check-in [a3e03981ea]

Overview
Comment:Use ports instead of services in OFTCPSocket.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: a3e03981eac22ed585e2101cf16e6033a3361f61ac0f17a55916b69dc5a9acbf
User & Date: js on 2011-02-10 17:19:23
Other Links: manifest | tags
Context
2011-02-10
18:40
Fix two unimportant typos. check-in: 5a0049ac30 user: js tags: trunk
17:19
Use ports instead of services in OFTCPSocket. check-in: a3e03981ea user: js tags: trunk
01:20
More work on the PSP port.
Links and runs now, but does not show any output yet.
check-in: 423353cdc1 user: js tags: trunk
Changes

Modified src/OFExceptions.h from [015ac7b5d3] to [8168934e0a].

931
932
933
934
935
936
937
938
939

940
941
942
943
944
945

946
947
948
949
950
951

952
953
954
955
956

957
958
959
960
961
962
963

964
965
966
967
968

969
970
971
972
973
974
975
976
977

978
979

980
981
982
983
984
985
986
987
988
989
990
991
992
993


994
995
996
997
998
999


1000
1001
1002
1003
1004
1005
1006


1007
1008
1009
1010
1011


1012
1013
1014
1015
1016
1017
1018


1019
1020
1021
1022
1023


1024
1025
1026
1027
1028
1029
1030
1031

1032
1033

1034
1035
1036

1037
1038

1039
1040
1041
1042
1043
1044
1045
1046
1047


1048
1049
1050
1051
1052
1053


1054
1055
1056
1057
1058
1059
1060


1061
1062
1063
1064
1065


1066
1067
1068
1069
1070
1071
1072


1073
1074
1075
1076
1077


1078
1079
1080
1081
1082
1083
1084
1085

1086
1087

1088
1089
1090

1091
1092

1093
1094
1095
1096
1097
1098
1099
931
932
933
934
935
936
937


938
939
940
941
942


943
944
945
946
947
948

949

950
951
952

953

954
955
956
957
958

959

960
961
962

963

964
965
966
967
968
969
970

971
972

973





974
975
976
977
978
979
980


981
982
983
984
985
986


987
988
989
990
991
992
993


994
995
996
997
998


999
1000
1001
1002
1003
1004
1005


1006
1007
1008
1009
1010


1011
1012
1013
1014
1015
1016
1017
1018
1019

1020
1021

1022
1023
1024

1025
1026

1027
1028
1029
1030
1031
1032
1033
1034


1035
1036
1037
1038
1039
1040


1041
1042
1043
1044
1045
1046
1047


1048
1049
1050
1051
1052


1053
1054
1055
1056
1057
1058
1059


1060
1061
1062
1063
1064


1065
1066
1067
1068
1069
1070
1071
1072
1073

1074
1075

1076
1077
1078

1079
1080

1081
1082
1083
1084
1085
1086
1087
1088







-
-
+




-
-
+





-
+
-



-
+
-





-
+
-



-
+
-







-
+

-
+
-
-
-
-
-







-
-
+
+




-
-
+
+





-
-
+
+



-
-
+
+





-
-
+
+



-
-
+
+







-
+

-
+


-
+

-
+







-
-
+
+




-
-
+
+





-
-
+
+



-
-
+
+





-
-
+
+



-
-
+
+







-
+

-
+


-
+

-
+







@end

/**
 * \brief An exception indicating the translation of an address failed.
 */
@interface OFAddressTranslationFailedException: OFException
{
	OFString *node;
	OFString *service;
	OFString *host;
	int	 errNo;
}

#ifdef OF_HAVE_PROPERTIES
@property (readonly, nonatomic) OFString *node;
@property (readonly, nonatomic) OFString *service;
@property (readonly, nonatomic) OFString *host;
@property (readonly) int errNo;
#endif

/**
 * \param class_ The class of the object which caused the exception
 * \param node The node for which translation was requested
 * \param host The host for which translation was requested
 * \param service The service of the node for which translation was requested
 * \return A new address translation failed exception
 */
+ newWithClass: (Class)class_
	  node: (OFString*)node
	  host: (OFString*)host;
       service: (OFString*)service;

/**
 * Initializes an already allocated address translation failed exception.
 *
 * \param class_ The class of the object which caused the exception
 * \param node The node for which translation was requested
 * \param host The host for which translation was requested
 * \param service The service of the node for which translation was requested
 * \return An initialized address translation failed exception
 */
- initWithClass: (Class)class_
	   node: (OFString*)node
	   host: (OFString*)host;
	service: (OFString*)service;

/**
 * \return The errno from when the exception was created
 */
- (int)errNo;

/**
 * /return The node for which translation was requested
 * /return The host for which translation was requested
 */
- (OFString*)node;
- (OFString*)host;

/**
 * \return The service of the node for which translation was requested
 */
- (OFString*)service;
@end

/**
 * \brief An exception indicating that a connection could not be established.
 */
@interface OFConnectionFailedException: OFException
{
	OFString *node;
	OFString *service;
	OFString *host;
	uint16_t port;
	int	 errNo;
}

#ifdef OF_HAVE_PROPERTIES
@property (readonly, nonatomic) OFString *node;
@property (readonly, nonatomic) OFString *service;
@property (readonly, nonatomic) OFString *host;
@property (readonly) uint16_t port;
@property (readonly) int errNo;
#endif

/**
 * \param class_ The class of the object which caused the exception
 * \param node The node to which the connection failed
 * \param service The service on the node to which the connection failed
 * \param host The host to which the connection failed
 * \param port The port on the host to which the connection failed
 * \return A new connection failed exception
 */
+ newWithClass: (Class)class_
	  node: (OFString*)node
       service: (OFString*)service;
	  host: (OFString*)host
	  port: (uint16_t)port;

/**
 * Initializes an already allocated connection failed exception.
 *
 * \param class_ The class of the object which caused the exception
 * \param node The node to which the connection failed
 * \param service The service on the node to which the connection failed
 * \param host The host to which the connection failed
 * \param port The port on the host to which the connection failed
 * \return An initialized connection failed exception
 */
- initWithClass: (Class)class_
	   node: (OFString*)node
	service: (OFString*)service;
	   host: (OFString*)host
	   port: (uint16_t)port;

/**
 * \return The errno from when the exception was created
 */
- (int)errNo;

/**
 * \return The node to which the connection failed
 * \return The host to which the connection failed
 */
- (OFString*)node;
- (OFString*)host;

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

/**
 * \brief An exception indicating that binding a socket failed.
 */
@interface OFBindFailedException: OFException
{
	OFString *node;
	OFString *service;
	OFString *host;
	uint16_t port;
	int	 errNo;
}

#ifdef OF_HAVE_PROPERTIES
@property (readonly, nonatomic) OFString *node;
@property (readonly, nonatomic) OFString *service;
@property (readonly, nonatomic) OFString *host;
@property (readonly) uint16_t port;
@property (readonly) int errNo;
#endif

/**
 * \param class_ The class of the object which caused the exception
 * \param node The node on which binding failed
 * \param service The service on which binding failed
 * \param host The host on which binding failed
 * \param port The port on which binding failed
 * \return A new bind failed exception
 */
+ newWithClass: (Class)class_
	  node: (OFString*)node
       service: (OFString*)service;
	  host: (OFString*)host
	  port: (uint16_t)port;

/**
 * Initializes an already allocated bind failed exception.
 *
 * \param class_ The class of the object which caused the exception
 * \param node The node on which binding failed
 * \param service The service on which binding failed
 * \param host The host on which binding failed
 * \param port The port on which binding failed
 * \return An initialized bind failed exception
 */
- initWithClass: (Class)class_
	   node: (OFString*)node
	service: (OFString*)service;
	   host: (OFString*)host
	   port: (uint16_t)port;

/**
 * \return The errno from when the exception was created
 */
- (int)errNo;

/**
 * \return The node on which binding failed
 * \return The host on which binding failed
 */
- (OFString*)node;
- (OFString*)host;

/**
 * \return The service on which binding failed
 * \return The port on which binding failed
 */
- (OFString*)service;
- (uint16_t)port;
@end

/**
 * \brief An exception indicating that listening on the socket failed.
 */
@interface OFListenFailedException: OFException
{

Modified src/OFExceptions.m from [c20d586426] to [9ed6a2db34].

1355
1356
1357
1358
1359
1360
1361
1362

1363
1364
1365
1366

1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380

1381
1382
1383
1384
1385
1386

1387
1388

1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399

1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410

1411
1412
1413


1414
1415
1416
1417
1418




1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432

1433
1434

1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446


1447
1448
1449
1450


1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463


1464
1465
1466
1467
1468
1469
1470



1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481

1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494


1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505

1506
1507

1508
1509
1510

1511
1512

1513
1514
1515
1516
1517
1518
1519


1520
1521
1522
1523


1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536


1537
1538
1539
1540
1541
1542
1543



1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554

1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567


1568
1569
1570
1571
1572
1573
1574
1575
1576
1577

1578
1579

1580
1581
1582

1583
1584

1585
1586
1587
1588
1589
1590
1591
1355
1356
1357
1358
1359
1360
1361

1362

1363
1364

1365

1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377

1378

1379
1380
1381
1382

1383


1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394

1395

1396
1397
1398
1399
1400
1401
1402
1403
1404

1405
1406


1407
1408





1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425

1426
1427

1428





1429
1430
1431
1432
1433


1434
1435
1436
1437


1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450


1451
1452
1453
1454
1455
1456



1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469

1470

1471
1472
1473
1474
1475
1476
1477
1478
1479
1480


1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492

1493
1494

1495
1496
1497

1498
1499

1500
1501
1502
1503
1504
1505


1506
1507
1508
1509


1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522


1523
1524
1525
1526
1527
1528



1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541

1542

1543
1544
1545
1546
1547
1548
1549
1550
1551
1552


1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563

1564
1565

1566
1567
1568

1569
1570

1571
1572
1573
1574
1575
1576
1577
1578







-
+
-


-
+
-












-
+
-




-
+
-
-
+










-
+
-









-
+

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













-
+

-
+
-
-
-
-
-





-
-
+
+


-
-
+
+











-
-
+
+




-
-
-
+
+
+










-
+
-










-
-
+
+










-
+

-
+


-
+

-
+





-
-
+
+


-
-
+
+











-
-
+
+




-
-
-
+
+
+










-
+
-










-
-
+
+









-
+

-
+


-
+

-
+








	return description;
}
@end

@implementation OFAddressTranslationFailedException
+ newWithClass: (Class)class_
	  node: (OFString*)node
	  host: (OFString*)host
       service: (OFString*)service
{
	return [[self alloc] initWithClass: class_
				      node: node
				      host: host];
				   service: service];
}

- initWithClass: (Class)class_
{
	self = [super initWithClass: class_];

	errNo = GET_AT_ERRNO;

	return self;
}

- initWithClass: (Class)class_
	   node: (OFString*)node_
	   host: (OFString*)host_
	service: (OFString*)service_
{
	self = [super initWithClass: class_];

	@try {
		node	= [node_ copy];
		host  = [host_ copy];
		service = [service_ copy];
		errNo	= GET_AT_ERRNO;
		errNo = GET_AT_ERRNO;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[node release];
	[host release];
	[service release];

	[super dealloc];
}

- (OFString*)description
{
	if (description != nil)
		return description;

	if (node != nil && service != nil)
	if (host != nil)
		description = [[OFString alloc] initWithFormat:
		    @"The service %@ on %@ could not be translated to an "
		    @"address in class %s. This means that either the node was "
		    @"The host %@ could not be translated to an address in "
		    @"class %s. This means that either the host was not found, "
		    @"not found, there is no such service on the node, there "
		    @"was a problem with the name server, there was a problem "
		    @"with your network connection or you specified an invalid "
		    @"node or service. " ERRFMT, service, node,
		    class_getName(inClass), AT_ERRPARAM];
		    @"there was a problem with the name server, there was a "
		    @"problem with your network connection or you specified an "
		    @"invalid host. " ERRFMT, host, class_getName(inClass),
		    AT_ERRPARAM];
	else
		description = [[OFString alloc] initWithFormat:
		    @"An address translation failed in class %s! " ERRFMT,
		    class_getName(inClass), AT_ERRPARAM];

	return description;
}

- (int)errNo
{
	return errNo;
}

- (OFString*)node
- (OFString*)host
{
	return node;
	return host;
}

- (OFString*)service
{
	return service;
}
@end

@implementation OFConnectionFailedException
+ newWithClass: (Class)class_
	  node: (OFString*)node
       service: (OFString*)service
	  host: (OFString*)host
	  port: (uint16_t)port
{
	return [[self alloc] initWithClass: class_
				      node: node
				   service: service];
				      host: host
				      port: port];
}

- initWithClass: (Class)class_
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

- initWithClass: (Class)class_
	   node: (OFString*)node_
	service: (OFString*)service_
	   host: (OFString*)host_
	   port: (uint16_t)port_
{
	self = [super initWithClass: class_];

	@try {
		node	= [node_ copy];
		service	= [service_ copy];
		errNo	= GET_SOCK_ERRNO;
		host  = [host_ copy];
		port  = port_;
		errNo = GET_SOCK_ERRNO;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[node release];
	[host release];
	[service release];

	[super dealloc];
}

- (OFString*)description
{
	if (description != nil)
		return description;

	description = [[OFString alloc] initWithFormat:
	    @"A connection to service %@ on node %@ could not be established "
	    @"in class %s! " ERRFMT, service, node, class_getName(inClass),
	    @"A connection to %@ on port %" @PRIu16 @"could not be established "
	    @"in class %s! " ERRFMT, host, port, class_getName(inClass),
	    ERRPARAM];

	return description;
}

- (int)errNo
{
	return errNo;
}

- (OFString*)node
- (OFString*)host
{
	return node;
	return host;
}

- (OFString*)service
- (uint16_t)port
{
	return service;
	return port;
}
@end

@implementation OFBindFailedException
+ newWithClass: (Class)class_
	  node: (OFString*)node
       service: (OFString*)service
	  host: (OFString*)host
	  port: (uint16_t)port
{
	return [[self alloc] initWithClass: class_
				      node: node
				   service: service];
				      host: host
				      port: port];
}

- initWithClass: (Class)class_
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

- initWithClass: (Class)class_
	   node: (OFString*)node_
	service: (OFString*)service_
	   host: (OFString*)host_
	   port: (uint16_t)port_
{
	self = [super initWithClass: class_];

	@try {
		node	= [node_ copy];
		service	= [service_ copy];
		errNo	= GET_SOCK_ERRNO;
		host  = [host_ copy];
		port  = port_;
		errNo = GET_SOCK_ERRNO;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[node release];
	[host release];
	[service release];

	[super dealloc];
}

- (OFString*)description
{
	if (description != nil)
		return description;

	description = [[OFString alloc] initWithFormat:
	    @"Binding service %@ on node %@ failed in class %s! " ERRFMT,
	    service, node, class_getName(inClass), ERRPARAM];
	    @"Binding to port %" @PRIu16 @" on host %@ failed in class %s! "
	    ERRFMT, port, host, class_getName(inClass), ERRPARAM];

	return description;
}

- (int)errNo
{
	return errNo;
}

- (OFString*)node
- (OFString*)host
{
	return node;
	return host;
}

- (OFString*)service
- (uint16_t)port
{
	return service;
	return port;
}
@end

@implementation OFListenFailedException
+ newWithClass: (Class)class_
       backLog: (int)backlog
{

Modified src/OFHTTPRequest.m from [ef4afd4934] to [a21896a236].

113
114
115
116
117
118
119
120
121


122
123
124
125
126
127
128
113
114
115
116
117
118
119


120
121
122
123
124
125
126
127
128







-
-
+
+








	if (![[URL scheme] isEqual: @"http"])
		@throw [OFUnsupportedProtocolException newWithClass: isa
								URL: URL];
	pool = [[OFAutoreleasePool alloc] init];
	sock = [OFTCPSocket socket];

	[sock connectToService: [OFString stringWithFormat: @"%d", [URL port]]
			onNode: [URL host]];
	[sock connectToHost: [URL host]
		     onPort: [URL port]];

	@try {
		OFString *line;
		OFMutableDictionary *s_headers;
		OFDataArray *data;
		OFEnumerator *enumerator;
		OFString *key;

Modified src/OFString.h from [6e3ed07eb5] to [aedcb6f5f6].

12
13
14
15
16
17
18




19
20
21
22
23
24
25
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29







+
+
+
+







 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include <stdio.h>
#include <stdarg.h>

#ifdef HAVE_INTTYPES_H
# include <inttypes.h>
#endif

#import "OFObject.h"

typedef uint32_t of_unichar_t;

/**
 * \brief The encoding of a string.

Modified src/OFTCPSocket.h from [4276a55e09] to [c8cf94209d].

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
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







-
-
+
+

-
-
+
+


-
+

-
-
+
+

-

-
-
+
+







	struct sockaddr	*sockAddr;
	socklen_t	sockAddrLen;
}

/**
 * Connect the OFTCPSocket to the specified destination.
 *
 * \param service The service on the node to connect to
 * \param node The node to connect to
 * \param host The host to connect to
 * \param port The port on the host to connect to
 */
- (void)connectToService: (OFString*)service
		  onNode: (OFString*)node;
- (void)connectToHost: (OFString*)host
	       onPort: (uint16_t)port;

/**
 * Bind socket on the specified node and service.
 * Bind the socket on the specified port and host.
 *
 * \param service The service to bind
 * \param node The node to bind to. Use @"0.0.0.0" for IPv4 or @"::" for IPv6
 * \param port The port to bind to
 * \param host The host to bind to. Use @"0.0.0.0" for IPv4 or @"::" for IPv6
 *	       to bind to all.
 * \param family The family to use (AF_INET for IPv4 or AF_INET6 for IPv6)
 */
- (void)bindService: (OFString*)service
	     onNode: (OFString*)node;
- (void)bindToPort: (uint16_t)port
	    onHost: (OFString*)host;

/**
 * Listen on the socket.
 *
 * \param backlog Maximum length for the queue of pending connections.
 */
- (void)listenWithBackLog: (int)backlog;

Modified src/OFTCPSocket.m from [1565c3535f] to [0ec27b8819].

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
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







-
-
+
+






+




+

-
-
+
+
-
-
+
-



















-
-
-

-








-
+




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

-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-


-
+







-
-
-
+
+
+
-








	sock = INVALID_SOCKET;
	sockAddr = NULL;

	return self;
}

- (void)connectToService: (OFString*)service
		  onNode: (OFString*)node
- (void)connectToHost: (OFString*)host
	       onPort: (uint16_t)port
{
	if (sock != INVALID_SOCKET)
		@throw [OFAlreadyConnectedException newWithClass: isa];

#ifdef HAVE_THREADSAFE_GETADDRINFO
	struct addrinfo hints, *res, *res0;
	char port_s[7];

	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	snprintf(port_s, 7, "%" PRIu16, port);

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

	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);
#else
	BOOL connected = NO;
	struct hostent *he;
# ifndef _PSP
	struct servent *se;
# endif
	struct sockaddr_in addr;
	uint16_t port;
	char **ip;
# ifdef OF_THREADS
	OFDataArray *addrlist;

	addrlist = [[OFDataArray alloc] initWithItemSize: sizeof(char**)];
	[mutex lock];
# endif

	if ((he = gethostbyname([node cString])) == NULL) {
	if ((he = gethostbyname([host cString])) == NULL) {
# ifdef OF_THREADS
		[addrlist release];
		[mutex unlock];
# endif
		@throw [OFAddressTranslationFailedException
		@throw [OFAddressTranslationFailedException newWithClass: isa
		    newWithClass: isa
			    node: node
			 service: service];
	}

								    host: host];
# ifndef _PSP
	if ((se = getservbyname([service cString], "tcp")) != NULL)
		port = se->s_port;
	else {
# endif
		@try {
			intmax_t p = [service decimalValue];

	}
			if (p < 1 || p > 65535)
				@throw [OFOutOfRangeException
				    newWithClass: isa];

			port = of_bswap16_if_le(p);
		} @catch (OFInvalidFormatException *e) {
			[e release];
# ifdef OF_THREADS
			[addrlist release];
			[mutex unlock];
# endif
			@throw [OFAddressTranslationFailedException
			    newWithClass: isa
				    node: node
				 service: service];
		} @catch (id e) {
# ifdef OF_THREADS
			[addrlist release];
			[mutex unlock];
# endif
			@throw e;
		}
# ifndef _PSP
	}
# endif

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

	if (he->h_addrtype != AF_INET ||
	    (sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
# ifdef OF_THREADS
		[addrlist release];
		[mutex unlock];
# endif
		@throw [OFConnectionFailedException
		    newWithClass: isa
			    node: node
		@throw [OFConnectionFailedException newWithClass: isa
							    host: host
							    port: port];
			 service: service];
	}

# ifdef OF_THREADS
	@try {
		for (ip = he->h_addr_list; *ip != NULL; ip++)
			[addrlist addItem: ip];

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
325
326
327
328
329
330
331
332
333
334
335


336
337
338
339
340
341
342


343
344
345
346
347
348
349
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







-
-
+
+


-
-
+
+






+




+

-
-
+
+
-
-
+
-



-
-
+
+






-
-
+
+





-
-
-

-





-
+



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

-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-


-
+





-
+
-
-
+
-









-
-
+
+





-
-
+
+







		close(sock);
		sock = INVALID_SOCKET;
	}
#endif

	if (sock == INVALID_SOCKET)
		@throw [OFConnectionFailedException newWithClass: isa
							    node: node
							 service: service];
							    host: host
							    port: port];
}

- (void)bindService: (OFString*)service
	     onNode: (OFString*)node
- (void)bindToPort: (uint16_t)port
	    onHost: (OFString*)host
{
	if (sock != INVALID_SOCKET)
		@throw [OFAlreadyConnectedException newWithClass: isa];

#ifdef HAVE_THREADSAFE_GETADDRINFO
	struct addrinfo hints, *res;
	char port_s[7];

	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	snprintf(port_s, 7, "%" PRIu16, port);

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

	if ((sock = socket(res->ai_family, SOCK_STREAM, 0)) == INVALID_SOCKET)
		@throw [OFBindFailedException newWithClass: isa
						      node: node
						   service: service];
						      host: host
						      port: port];

	if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) {
		freeaddrinfo(res);
		close(sock);
		sock = INVALID_SOCKET;
		@throw [OFBindFailedException newWithClass: isa
						      node: node
						   service: service];
						      host: host
						      port: port];
	}

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

# ifdef OF_THREADS
	[mutex lock];
# endif

	if ((he = gethostbyname([node cString])) == NULL) {
	if ((he = gethostbyname([host cString])) == NULL) {
# ifdef OF_THREADS
		[mutex unlock];
# endif
		@throw [OFAddressTranslationFailedException
		@throw [OFAddressTranslationFailedException newWithClass: isa
		    newWithClass: isa
			    node: node
			 service: service];
	}

								    host: host];
# ifndef _PSP
	if ((se = getservbyname([service cString], "tcp")) != NULL)
		port = se->s_port;
	else {
# endif
		@try {
			intmax_t p = [service decimalValue];

	}
			if (p < 1 || p > 65535)
				@throw [OFOutOfRangeException
				    newWithClass: isa];

			port = of_bswap16_if_le(p);
		} @catch (OFInvalidFormatException *e) {
			[e release];
# ifdef OF_THREADS
			[mutex unlock];
# endif
			@throw [OFAddressTranslationFailedException
			    newWithClass: isa
				    node: node
				 service: service];
		} @catch (id e) {
# ifdef OF_THREADS
			[mutex unlock];
# endif
			@throw e;
		}
# ifndef _PSP
	}
# endif

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

	if (he->h_addrtype != AF_INET || he->h_addr_list[0] == NULL) {
# ifdef OF_THREADS
		[mutex unlock];
# endif
		@throw [OFAddressTranslationFailedException
		@throw [OFAddressTranslationFailedException newWithClass: isa
		    newWithClass: isa
			    node: node
								    host: host];
			 service: service];
	}

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

# ifdef OF_THREADS
	[mutex unlock];
# endif
	if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
		@throw [OFBindFailedException newWithClass: isa
						      node: node
						   service: service];
						      host: host
						      port: port];

	if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
		close(sock);
		sock = INVALID_SOCKET;
		@throw [OFBindFailedException newWithClass: isa
						      node: node
						   service: service];
						      host: host
						      port: port];
	}
#endif
}

- (void)listenWithBackLog: (int)backlog
{
	if (sock == INVALID_SOCKET)
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
327
328
329
330
331
332
333

334
335
336

337
338
339
340
341

342
343

344
345
346

347
348
349
350
351
352
353

354
355

356
357
358
359

360
361
362
363
364
365
366
367







-
+


-
+




-
+

-
+


-
+






-
+

-
+



-
+







- (OFString*)remoteAddress
{
	if (sockAddr == NULL || sockAddrLen == 0)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

#ifdef HAVE_THREADSAFE_GETADDRINFO
	char *node = [self allocMemoryWithSize: NI_MAXHOST];
	char *host = [self allocMemoryWithSize: NI_MAXHOST];

	@try {
		if (getnameinfo(sockAddr, sockAddrLen, node, NI_MAXHOST, NULL,
		if (getnameinfo(sockAddr, sockAddrLen, host, NI_MAXHOST, NULL,
		    0, NI_NUMERICHOST))
			@throw [OFAddressTranslationFailedException
			    newWithClass: isa];

		return [OFString stringWithCString: node];
		return [OFString stringWithCString: host];
	} @finally {
		[self freeMemory: node];
		[self freeMemory: host];
	}
#else
	char *node;
	char *host;

# ifdef OF_THREADS
	[mutex lock];

	@try {
# endif
		node = inet_ntoa(((struct sockaddr_in*)sockAddr)->sin_addr);
		host = inet_ntoa(((struct sockaddr_in*)sockAddr)->sin_addr);

		if (node == NULL)
		if (host == NULL)
			@throw [OFAddressTranslationFailedException
			    newWithClass: isa];

		return [OFString stringWithCString: node];
		return [OFString stringWithCString: host];
# ifdef OF_THREADS
	} @finally {
		[mutex unlock];
	}
# endif
#endif

Modified tests/OFTCPSocketTests.m from [fca04a710e] to [03ba1d4e12].

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
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







-
+







-





-
-
-
+
+
+



-
-
-
+
+
+







static OFString *module = @"OFTCPSocket";

@implementation TestsAppDelegate (OFTCPSocketTests)
- (void)TCPSocketTests
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFTCPSocket *server, *client = nil, *accepted;
	OFString *service, *msg;
	OFString *msg;
	uint16_t port;
	char buf[6];

	srand(time(NULL));
	port = (uint16_t)rand();
	if (port < 1024)
		port += 1024;
	service = [OFString stringWithFormat: @"%d", port];

	TEST(@"+[socket]", (server = [OFTCPSocket socket]) &&
	    (client = [OFTCPSocket socket]))

	msg = [OFString stringWithFormat:
	    @"-[bindService:onNode:] (port %d)", port];
	TEST(msg, R([server bindService: service
				 onNode: @"127.0.0.1"]))
	    @"-[bindToPort:onHost:] (port " @PRIu16 @")", port];
	TEST(msg, R([server bindToPort: port
				onHost: @"127.0.0.1"]))

	TEST(@"-[listen]", R([server listen]))

	TEST(@"-[connectToService:onNode:]",
	    R([client connectToService: service
				onNode: @"127.0.0.1"]))
	TEST(@"-[connectToHost:onPort:]",
	    R([client connectToHost: @"127.0.0.1"
			     onPort: port]))

	TEST(@"-[accept]", (accepted = [server accept]))

	TEST(@"-[remoteAddress]",
	    [[accepted remoteAddress] isEqual: @"127.0.0.1"])

	TEST(@"-[writeString:]", [client writeString: @"Hello!"])