ObjFW  Check-in [8faa16f249]

Overview
Comment:Try inet_addr() before gethostbyname().

gethostbyname() is not required to accept an IP address and does not do
so on the Wii.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 8faa16f249b58fa7e1580fd433230b739101e85373f055702f25ad88cdb4cceb
User & Date: js on 2013-06-13 01:59:45
Other Links: manifest | tags
Context
2013-06-13
02:03
Don't bind to port 0 on the Wii. check-in: ca113e0145 user: js tags: trunk
01:59
Try inet_addr() before gethostbyname(). check-in: 8faa16f249 user: js tags: trunk
2013-06-12
20:12
OFException.m: Define _GNU_SOURCE. check-in: e4cb03d080 user: js tags: trunk
Changes

Modified src/OFTCPSocket.m from [4800955ec9] to [73d0c39dd0].

    93     93   # define connect(sock, addr, addrlen) net_connect(sock, addr, addrlen)
    94     94   # define gethostbyname(name) net_gethostbyname(name)
    95     95   # define getsockname(sock, addr, addrlen) net_getsockname(sock, addr, addrlen)
    96     96   # define listen(sock, backlog) net_listen(sock, backlog)
    97     97   # define setsockopt(sock, level, name, value, len) \
    98     98   	net_setsockopt(sock, level, name, value, len)
    99     99   # define socket(domain, type, proto) net_socket(domain, type, proto)
          100  +typedef u32 in_addr_t;
   100    101   #endif
   101    102   
   102    103   /* References for static linking */
   103    104   void _references_to_categories_of_OFTCPSocket(void)
   104    105   {
   105    106   	_OFTCPSocket_SOCKS5_reference = 1;
   106    107   }
................................................................................
   371    372   #else
   372    373   	bool connected = false;
   373    374   	struct hostent *he;
   374    375   	struct sockaddr_in addr;
   375    376   	char **ip;
   376    377   # ifdef OF_HAVE_THREADS
   377    378   	OFDataArray *addrlist;
          379  +# endif
   378    380   
          381  +	memset(&addr, 0, sizeof(addr));
          382  +	addr.sin_family = AF_INET;
          383  +	addr.sin_port = OF_BSWAP16_IF_LE(port);
          384  +
          385  +	if ((addr.sin_addr.s_addr = inet_addr([host cStringWithEncoding:
          386  +	    OF_STRING_ENCODING_NATIVE])) != (in_addr_t)(-1)) {
          387  +		if ((_socket = socket(AF_INET, SOCK_STREAM,
          388  +		    0)) == INVALID_SOCKET) {
          389  +			@throw [OFConnectionFailedException
          390  +			    exceptionWithClass: [self class]
          391  +					socket: self
          392  +					  host: host
          393  +					  port: port];
          394  +		}
          395  +
          396  +		if (connect(_socket, (struct sockaddr*)&addr,
          397  +		    sizeof(addr)) == -1) {
          398  +			close(_socket);
          399  +			_socket = INVALID_SOCKET;
          400  +			@throw [OFConnectionFailedException
          401  +			    exceptionWithClass: [self class]
          402  +					socket: self
          403  +					  host: host
          404  +					  port: port];
          405  +		}
          406  +
          407  +		if (_SOCKS5Host != nil)
          408  +			[self OF_SOCKS5ConnectToHost: destinationHost
          409  +						port: destinationPort];
          410  +
          411  +		return;
          412  +	}
          413  +
          414  +# ifdef OF_HAVE_THREADS
   379    415   	addrlist = [[OFDataArray alloc] initWithItemSize: sizeof(char**)];
   380    416   	[mutex lock];
   381    417   # endif
   382    418   
   383    419   	if ((he = gethostbyname([host cStringWithEncoding:
   384    420   	    OF_STRING_ENCODING_NATIVE])) == NULL) {
   385    421   # ifdef OF_HAVE_THREADS
................................................................................
   388    424   # endif
   389    425   		@throw [OFAddressTranslationFailedException
   390    426   		    exceptionWithClass: [self class]
   391    427   				socket: self
   392    428   				  host: host];
   393    429   	}
   394    430   
   395         -	memset(&addr, 0, sizeof(addr));
   396         -	addr.sin_family = AF_INET;
   397         -	addr.sin_port = OF_BSWAP16_IF_LE(port);
   398         -
   399    431   	if (he->h_addrtype != AF_INET ||
   400    432   	    (_socket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
   401    433   # ifdef OF_HAVE_THREADS
   402    434   		[addrlist release];
   403    435   		[mutex unlock];
   404    436   # endif
   405    437   		@throw [OFConnectionFailedException
................................................................................
   558    590   							  socket: self
   559    591   							    host: host
   560    592   							    port: port];
   561    593   	}
   562    594   
   563    595   	freeaddrinfo(res);
   564    596   #else
   565         -	struct hostent *he;
   566         -
   567         -# ifdef OF_HAVE_THREADS
   568         -	[mutex lock];
   569         -# endif
   570         -
   571         -	if ((he = gethostbyname([host cStringWithEncoding:
   572         -	    OF_STRING_ENCODING_NATIVE])) == NULL) {
   573         -# ifdef OF_HAVE_THREADS
   574         -		[mutex unlock];
   575         -# endif
   576         -		@throw [OFAddressTranslationFailedException
   577         -		    exceptionWithClass: [self class]
   578         -				socket: self
   579         -				  host: host];
   580         -	}
   581         -
   582    597   	memset(&addr, 0, sizeof(addr));
   583    598   	addr.in.sin_family = AF_INET;
   584    599   	addr.in.sin_port = OF_BSWAP16_IF_LE(port);
   585    600   
   586         -	if (he->h_addrtype != AF_INET || he->h_addr_list[0] == NULL) {
          601  +	if ((addr.in.sin_addr.s_addr = inet_addr([host cStringWithEncoding:
          602  +	    OF_STRING_ENCODING_NATIVE])) == (in_addr_t)(-1)) {
   587    603   # ifdef OF_HAVE_THREADS
   588         -		[mutex unlock];
          604  +		[mutex lock];
          605  +		@try {
   589    606   # endif
   590         -		@throw [OFAddressTranslationFailedException
   591         -		    exceptionWithClass: [self class]
   592         -				socket: self
   593         -				  host: host];
          607  +			struct hostent *he;
          608  +
          609  +			if ((he = gethostbyname([host cStringWithEncoding:
          610  +			    OF_STRING_ENCODING_NATIVE])) == NULL)
          611  +				@throw [OFAddressTranslationFailedException
          612  +				    exceptionWithClass: [self class]
          613  +						socket: self
          614  +						  host: host];
          615  +
          616  +			if (he->h_addrtype != AF_INET ||
          617  +			    he->h_addr_list[0] == NULL) {
          618  +				@throw [OFAddressTranslationFailedException
          619  +				    exceptionWithClass: [self class]
          620  +						socket: self
          621  +						  host: host];
          622  +			}
          623  +
          624  +			memcpy(&addr.in.sin_addr.s_addr, he->h_addr_list[0],
          625  +			    he->h_length);
          626  +# ifdef OF_HAVE_THREADS
          627  +		} @finally {
          628  +			[mutex unlock];
          629  +		}
          630  +# endif
   594    631   	}
   595    632   
   596         -	memcpy(&addr.in.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
   597         -
   598         -# ifdef OF_HAVE_THREADS
   599         -	[mutex unlock];
   600         -# endif
   601    633   	if ((_socket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
   602    634   		@throw [OFBindFailedException exceptionWithClass: [self class]
   603    635   							  socket: self
   604    636   							    host: host
   605    637   							    port: port];
   606    638   
   607    639   	if (setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR, (const char*)&one,

Modified tests/TestsAppDelegate.m from [612bc88548] to [f5e90b7f86].

    87     87   	} @catch (id e) {
    88     88   		TestsAppDelegate *delegate =
    89     89   		    [[OFApplication sharedApplication] delegate];
    90     90   		OFString *string = [OFString stringWithFormat:
    91     91   		    @"\nRuntime error: Unhandled exception:\n%@\n", e];
    92     92   		OFString *backtrace = [OFString stringWithFormat:
    93     93   		    @"\nBacktrace:\n  %@\n\n",
    94         -		    [[e backtrace] componentsJoinedByString: @"\n  "];
           94  +		    [[e backtrace] componentsJoinedByString: @"\n  "]];
    95     95   
    96     96   		[delegate outputString: string
    97     97   			       inColor: RED];
    98     98   		[delegate outputString: backtrace
    99     99   			       inColor: RED];
   100    100   		[delegate outputString: @"Press home button to exit!\n"
   101    101   			       inColor: NO_COLOR];