ObjFW  Check-in [ca113e0145]

Overview
Comment:Don't bind to port 0 on the Wii.

Instead, a decreasing variable is used now. This should be safe, as
nothing else should be binding ports while homebrew code is running. The
only problem that could arise is when code manually binds to very high
port numbers.

This also solves the problem that getsockname() is not available on the
Wii, as we know the port now anyway.

And while we're at it, this also adds struct sockaddr_storage.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: ca113e0145659828611d77eb7f588400352b86dadcf46f0f4c9a5b5607b74c56
User & Date: js on 2013-06-13 02:03:13
Other Links: manifest | tags
Context
2013-06-14
22:59
Clean up OFXMLParser a little. check-in: 568ec65e80 user: js tags: trunk
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
Changes

Modified src/OFStreamObserver.m from [ec4b8afafc] to [2bcf8ed1eb].

    58     58   #import "macros.h"
    59     59   
    60     60   #ifdef __wii__
    61     61   # define BOOL OGC_BOOL
    62     62   # include <network.h>
    63     63   # undef BOOL
    64     64   # define bind(sock, addr, addrlen) net_bind(sock, addr, addrlen)
    65         -# define getsockname(sock, addr, addrlen) net_getsockname(sock, addr, addrlen)
    66     65   # define sendto(sock, buf, len, flags, addr, addrlen) \
    67     66   	net_sendto(sock, buf, len, flags, addr, addrlen)
    68     67   # define socket(domain, type, proto) net_socket(domain, type, proto)
    69     68   #endif
    70     69   
    71     70   enum {
    72     71   	QUEUE_ADD = 0,
................................................................................
   111    110   - init
   112    111   {
   113    112   	self = [super init];
   114    113   
   115    114   	@try {
   116    115   #ifndef OF_HAVE_PIPE
   117    116   		struct sockaddr_in cancelAddr2;
          117  +# ifndef __wii__
   118    118   		socklen_t cancelAddrLen;
          119  +# endif
   119    120   #endif
   120    121   
   121    122   		_readStreams = [[OFMutableArray alloc] init];
   122    123   		_writeStreams = [[OFMutableArray alloc] init];
   123    124   		_queue = [[OFMutableArray alloc] init];
   124    125   		_queueInfo = [[OFDataArray alloc]
   125    126   		    initWithItemSize: sizeof(int)];
................................................................................
   141    142   			@throw [OFInitializationFailedException
   142    143   			    exceptionWithClass: [self class]];
   143    144   
   144    145   		_cancelAddr.sin_family = AF_INET;
   145    146   		_cancelAddr.sin_port = 0;
   146    147   		_cancelAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
   147    148   		cancelAddr2 = _cancelAddr;
          149  +
          150  +# ifdef __wii__
          151  +		/* The Wii does not accept port 0 as "choose any free port" */
          152  +		_cancelAddr.sin_port = 65533;
          153  +		cancelAddr2.sin_port = 65534;
          154  +# endif
   148    155   
   149    156   		if (bind(_cancelFD[0], (struct sockaddr*)&_cancelAddr,
   150    157   		    sizeof(_cancelAddr)) || bind(_cancelFD[1],
   151    158   		    (struct sockaddr*)&cancelAddr2, sizeof(cancelAddr2)))
   152    159   			@throw [OFInitializationFailedException
   153    160   			    exceptionWithClass: [self class]];
   154    161   
          162  +# ifndef __wii__
   155    163   		cancelAddrLen = sizeof(_cancelAddr);
   156    164   		if (getsockname(_cancelFD[0], (struct sockaddr*)&_cancelAddr,
   157    165   		    &cancelAddrLen))
   158    166   			@throw [OFInitializationFailedException
   159    167   			    exceptionWithClass: [self class]];
          168  +# endif
   160    169   #endif
   161    170   
   162    171   		_maxFD = _cancelFD[0];
   163    172   		_FDToStream = [self allocMemoryWithSize: sizeof(OFStream*)
   164    173   						  count: _maxFD + 1];
   165    174   		_FDToStream[_cancelFD[0]] = nil;
   166    175   

Modified src/OFTCPSocket.m from [73d0c39dd0] to [17c2cc5542].

    88     88   
    89     89   #ifdef __wii__
    90     90   # define accept(sock, addr, addrlen) net_accept(sock, addr, addrlen)
    91     91   # define bind(sock, addr, addrlen) net_bind(sock, addr, addrlen)
    92     92   # define close(sock) net_close(sock)
    93     93   # define connect(sock, addr, addrlen) net_connect(sock, addr, addrlen)
    94     94   # define gethostbyname(name) net_gethostbyname(name)
    95         -# define getsockname(sock, addr, addrlen) net_getsockname(sock, addr, addrlen)
    96     95   # define listen(sock, backlog) net_listen(sock, backlog)
    97     96   # define setsockopt(sock, level, name, value, len) \
    98     97   	net_setsockopt(sock, level, name, value, len)
    99     98   # define socket(domain, type, proto) net_socket(domain, type, proto)
   100     99   typedef u32 in_addr_t;
          100  +
          101  +struct sockaddr_storage {
          102  +	u8 ss_len;
          103  +	u8 ss_family;
          104  +	u8 ss_data[14];
          105  +};
   101    106   #endif
   102    107   
   103    108   /* References for static linking */
   104    109   void _references_to_categories_of_OFTCPSocket(void)
   105    110   {
   106    111   	_OFTCPSocket_SOCKS5_reference = 1;
   107    112   }
   108    113   
   109    114   Class of_tls_socket_class = Nil;
   110    115   
   111    116   static OFString *defaultSOCKS5Host = nil;
   112    117   static uint16_t defaultSOCKS5Port = 1080;
          118  +
          119  +#ifdef __wii__
          120  +static uint16_t freePort = 65532;
          121  +#endif
   113    122   
   114    123   #ifdef OF_HAVE_THREADS
   115    124   @interface OFTCPSocket_ConnectThread: OFThread
   116    125   {
   117    126   	OFThread *_sourceThread;
   118    127   	OFTCPSocket *_socket;
   119    128   	OFString *_host;
................................................................................
   536    545   	union {
   537    546   		struct sockaddr_storage storage;
   538    547   		struct sockaddr_in in;
   539    548   #ifdef AF_INET6
   540    549   		struct sockaddr_in6 in6;
   541    550   #endif
   542    551   	} addr;
          552  +#ifndef __wii__
   543    553   	socklen_t addrLen;
          554  +#endif
   544    555   
   545    556   	if (_socket != INVALID_SOCKET)
   546    557   		@throw [OFAlreadyConnectedException
   547    558   		    exceptionWithClass: [self class]
   548    559   				socket: self];
   549    560   
   550    561   	if (_SOCKS5Host != nil)
   551    562   		@throw [OFNotImplementedException
   552    563   		    exceptionWithClass: [self class]
   553    564   			      selector: _cmd];
          565  +
          566  +#ifdef __wii__
          567  +	if (port == 0)
          568  +		port = freePort--;
          569  +#endif
   554    570   
   555    571   #ifdef HAVE_THREADSAFE_GETADDRINFO
   556    572   	struct addrinfo hints, *res;
   557    573   	char portCString[7];
   558    574   
   559    575   	memset(&hints, 0, sizeof(struct addrinfo));
   560    576   	hints.ai_family = AF_UNSPEC;
................................................................................
   651    667   							    port: port];
   652    668   	}
   653    669   #endif
   654    670   
   655    671   	if (port > 0)
   656    672   		return port;
   657    673   
          674  +#ifndef __wii__
   658    675   	addrLen = sizeof(addr.storage);
   659    676   	if (getsockname(_socket, (struct sockaddr*)&addr, &addrLen)) {
   660    677   		close(_socket);
   661    678   		_socket = INVALID_SOCKET;
   662    679   		@throw [OFBindFailedException exceptionWithClass: [self class]
   663    680   							  socket: self
   664    681   							    host: host
   665    682   							    port: port];
   666    683   	}
   667    684   
   668    685   	if (addr.storage.ss_family == AF_INET)
   669    686   		return OF_BSWAP16_IF_LE(addr.in.sin_port);
   670         -#ifdef AF_INET6
          687  +# ifdef AF_INET6
   671    688   	if (addr.storage.ss_family == AF_INET6)
   672    689   		return OF_BSWAP16_IF_LE(addr.in6.sin6_port);
          690  +# endif
   673    691   #endif
   674    692   
   675    693   	close(_socket);
   676    694   	_socket = INVALID_SOCKET;
   677    695   	@throw [OFBindFailedException exceptionWithClass: [self class]
   678    696   						  socket: self
   679    697   						    host: host