ObjFW  Check-in [ffb91daffe]

Overview
Comment:Initial sockets support for the Wii.

Not functional yet due to bugs in the SDK.
Bugs found so far:

* Binding to port 0 fails instead of choosing a free port.
* gethostbyname() does not work for IPs.
* getsockname() is missing.
* struct sockaddr_storage is missing.

I have not decided yet whether I fix those bugs in the SDK (I already
implemented getsockname() and added struct sockaddr_stroage and it seems
to work) or if I work around them in ObjFW. This will mainly depend on
how cooperative the developers of the Wii SDK are.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: ffb91daffe96f4784a60da77c2bad3b8ab007b0d611e134fb804ed9ad55db86d
User & Date: js on 2013-06-11 23:33:16
Other Links: manifest | tags
Context
2013-06-12
20:12
OFException.m: Define _GNU_SOURCE. check-in: e4cb03d080 user: js tags: trunk
2013-06-11
23:33
Initial sockets support for the Wii. check-in: ffb91daffe user: js tags: trunk
22:55
Use _Unwind_Backtrace() to get a backtrace. check-in: 259f593244 user: js tags: trunk
Changes

Modified .gitignore from [7fbc43bc98] to [3a8df09a67].

     6      6   *.o
     7      7   *.orig
     8      8   *.so
     9      9   *~
    10     10   .deps
    11     11   aclocal.m4
    12     12   autom4te.cache
           13  +boot.dol
    13     14   build
    14     15   buildsys.mk
    15     16   config.h
    16     17   config.h.in
    17     18   config.log
    18     19   config.status
    19     20   configure

Modified configure.ac from [538d135f42] to [1fee552aa8].

   633    633   
   634    634   	case "$host_os" in
   635    635   		mingw*)
   636    636   			AC_SUBST(OFSTREAMOBSERVER_SELECT_M,
   637    637   				"OFStreamObserver_select.m")
   638    638   			;;
   639    639   	esac
          640  +
          641  +	AS_IF([test x"$with_wii" = x"yes"], [
          642  +	       AC_SUBST(OFSTREAMOBSERVER_POLL_M, "OFStreamObserver_poll.m")
          643  +	])
   640    644   
   641    645   	AC_MSG_CHECKING(for getaddrinfo)
   642    646   	AC_TRY_COMPILE([
   643    647   		#include <stddef.h>
   644    648   		#ifndef _WIN32
   645    649   		# include <sys/types.h>
   646    650   		# include <sys/socket.h>

Modified src/OFStreamObserver.h from [9d097b3bbf] to [1ad6295688].

    23     23   #ifdef OF_HAVE_SYS_SOCKET_H
    24     24   # include <sys/socket.h>
    25     25   #endif
    26     26   
    27     27   #ifdef _WIN32
    28     28   # include <winsock2.h>
    29     29   #endif
           30  +
           31  +#ifdef __wii__
           32  +# define BOOL OGC_BOOL
           33  +# include <network.h>
           34  +# undef BOOL
           35  +#endif
    30     36   
    31     37   @class OFStream;
    32     38   @class OFMutableArray;
    33     39   @class OFMutableDictionary;
    34     40   @class OFDataArray;
    35     41   #ifdef OF_HAVE_THREADS
    36     42   @class OFMutex;

Modified src/OFStreamObserver.m from [765c53f8f8] to [ec4b8afafc].

    36     36   # import "OFMutex.h"
    37     37   #endif
    38     38   #import "OFDate.h"
    39     39   
    40     40   #ifdef HAVE_KQUEUE
    41     41   # import "OFStreamObserver_kqueue.h"
    42     42   #endif
    43         -#ifdef HAVE_POLL_H
           43  +#if defined(HAVE_POLL_H) || defined(__wii__)
    44     44   # import "OFStreamObserver_poll.h"
    45     45   #endif
    46     46   #if defined(HAVE_SYS_SELECT_H) || defined(_WIN32)
    47     47   # import "OFStreamObserver_select.h"
    48     48   #endif
    49     49   
    50     50   #ifdef _WIN32
................................................................................
    52     52   #endif
    53     53   
    54     54   #import "OFInitializationFailedException.h"
    55     55   #import "OFOutOfRangeException.h"
    56     56   
    57     57   #import "autorelease.h"
    58     58   #import "macros.h"
           59  +
           60  +#ifdef __wii__
           61  +# define BOOL OGC_BOOL
           62  +# include <network.h>
           63  +# undef BOOL
           64  +# define bind(sock, addr, addrlen) net_bind(sock, addr, addrlen)
           65  +# define getsockname(sock, addr, addrlen) net_getsockname(sock, addr, addrlen)
           66  +# define sendto(sock, buf, len, flags, addr, addrlen) \
           67  +	net_sendto(sock, buf, len, flags, addr, addrlen)
           68  +# define socket(domain, type, proto) net_socket(domain, type, proto)
           69  +#endif
    59     70   
    60     71   enum {
    61     72   	QUEUE_ADD = 0,
    62     73   	QUEUE_REMOVE = 1,
    63     74   	QUEUE_READ = 0,
    64     75   	QUEUE_WRITE = 2
    65     76   };
................................................................................
    75     86   + alloc
    76     87   {
    77     88   	if (self == [OFStreamObserver class])
    78     89   		return [OFStreamObserver_kqueue alloc];
    79     90   
    80     91   	return [super alloc];
    81     92   }
    82         -#elif defined(HAVE_POLL_H)
           93  +#elif defined(HAVE_POLL_H) || defined(__wii__)
    83     94   + alloc
    84     95   {
    85     96   	if (self == [OFStreamObserver class])
    86     97   		return [OFStreamObserver_poll alloc];
    87     98   
    88     99   	return [super alloc];
    89    100   }

Modified src/OFStreamObserver_poll.m from [047427a85c] to [a45ade6207].

    27     27   #import "OFStreamObserver_poll.h"
    28     28   #import "OFDataArray.h"
    29     29   
    30     30   #import "OFOutOfRangeException.h"
    31     31   
    32     32   #import "autorelease.h"
    33     33   #import "macros.h"
           34  +
           35  +#ifdef __wii__
           36  +# define BOOL OGC_BOOL
           37  +# include <network.h>
           38  +# undef BOOL
           39  +# define poll(fds, nfds, timeout) net_poll(fds, nfds, timeout)
           40  +# define pollfd pollsd
           41  +# define fd socket
           42  +typedef u32 nfds_t;
           43  +#endif
    34     44   
    35     45   @implementation OFStreamObserver_poll
    36     46   - init
    37     47   {
    38     48   	self = [super init];
    39     49   
    40     50   	@try {

Modified src/OFStreamSocket.m from [05982c597a] to [10e23bfd72].

    32     32   #import "OFStreamSocket.h"
    33     33   
    34     34   #import "OFInitializationFailedException.h"
    35     35   #import "OFNotConnectedException.h"
    36     36   #import "OFReadFailedException.h"
    37     37   #import "OFSetOptionFailedException.h"
    38     38   #import "OFWriteFailedException.h"
           39  +
           40  +#ifdef __wii__
           41  +# define BOOL OGC_BOOL
           42  +# include <network.h>
           43  +# undef BOOL
           44  +# define recv(sock, buf, len, flags) net_recv(sock, buf, len, flags)
           45  +# define send(sock, buf, len, flags) net_send(sock, buf, len, flags)
           46  +#endif
    39     47   
    40     48   #ifndef INVALID_SOCKET
    41     49   # define INVALID_SOCKET -1
    42     50   #endif
    43     51   
    44     52   #ifdef _WIN32
    45     53   # define close(sock) closesocket(sock)
    46     54   #endif
    47     55   
    48     56   @implementation OFStreamSocket
    49         -#ifdef _WIN32
    50     57   + (void)initialize
    51     58   {
           59  +#ifdef _WIN32
    52     60   	WSADATA wsa;
           61  +#endif
    53     62   
    54     63   	if (self != [OFStreamSocket class])
    55     64   		return;
    56     65   
           66  +#if defined(_WIN32)
    57     67   	if (WSAStartup(MAKEWORD(2, 0), &wsa))
    58     68   		@throw [OFInitializationFailedException
    59     69   		    exceptionWithClass: self];
    60         -}
           70  +#elif defined(__wii__)
           71  +	if (net_init() < 0)
           72  +		@throw [OFInitializationFailedException
           73  +		    exceptionWithClass: self];
    61     74   #endif
           75  +}
    62     76   
    63     77   + (instancetype)socket
    64     78   {
    65     79   	return [[[self alloc] init] autorelease];
    66     80   }
    67     81   
    68     82   - (bool)lowlevelIsAtEndOfStream

Modified src/OFTCPSocket.h from [4ec65efa11] to [2b3fb6a25a].

    28     28   #endif
    29     29   
    30     30   #import "OFStreamSocket.h"
    31     31   
    32     32   #ifdef _WIN32
    33     33   # include <ws2tcpip.h>
    34     34   #endif
           35  +
           36  +#ifdef __wii__
           37  +# define BOOL OGC_BOOL
           38  +# include <network.h>
           39  +# undef BOOL
           40  +#endif
    35     41   
    36     42   /*! @file */
    37     43   
    38     44   @class OFTCPSocket;
    39     45   @class OFString;
    40     46   
    41     47   #ifdef OF_HAVE_BLOCKS

Modified src/OFTCPSocket.m from [4199e0da2a] to [4800955ec9].

    81     81   
    82     82   static OFMutex *mutex = nil;
    83     83   #endif
    84     84   
    85     85   #ifdef _WIN32
    86     86   # define close(sock) closesocket(sock)
    87     87   #endif
           88  +
           89  +#ifdef __wii__
           90  +# define accept(sock, addr, addrlen) net_accept(sock, addr, addrlen)
           91  +# define bind(sock, addr, addrlen) net_bind(sock, addr, addrlen)
           92  +# define close(sock) net_close(sock)
           93  +# define connect(sock, addr, addrlen) net_connect(sock, addr, addrlen)
           94  +# define gethostbyname(name) net_gethostbyname(name)
           95  +# define getsockname(sock, addr, addrlen) net_getsockname(sock, addr, addrlen)
           96  +# define listen(sock, backlog) net_listen(sock, backlog)
           97  +# define setsockopt(sock, level, name, value, len) \
           98  +	net_setsockopt(sock, level, name, value, len)
           99  +# define socket(domain, type, proto) net_socket(domain, type, proto)
          100  +#endif
    88    101   
    89    102   /* References for static linking */
    90    103   void _references_to_categories_of_OFTCPSocket(void)
    91    104   {
    92    105   	_OFTCPSocket_SOCKS5_reference = 1;
    93    106   }
    94    107   
................................................................................
   487    500   - (uint16_t)bindToHost: (OFString*)host
   488    501   		  port: (uint16_t)port
   489    502   {
   490    503   	const int one = 1;
   491    504   	union {
   492    505   		struct sockaddr_storage storage;
   493    506   		struct sockaddr_in in;
          507  +#ifdef AF_INET6
   494    508   		struct sockaddr_in6 in6;
          509  +#endif
   495    510   	} addr;
   496    511   	socklen_t addrLen;
   497    512   
   498    513   	if (_socket != INVALID_SOCKET)
   499    514   		@throw [OFAlreadyConnectedException
   500    515   		    exceptionWithClass: [self class]
   501    516   				socket: self];
................................................................................
   616    631   							  socket: self
   617    632   							    host: host
   618    633   							    port: port];
   619    634   	}
   620    635   
   621    636   	if (addr.storage.ss_family == AF_INET)
   622    637   		return OF_BSWAP16_IF_LE(addr.in.sin_port);
          638  +#ifdef AF_INET6
   623    639   	if (addr.storage.ss_family == AF_INET6)
   624    640   		return OF_BSWAP16_IF_LE(addr.in6.sin6_port);
          641  +#endif
   625    642   
   626    643   	close(_socket);
   627    644   	_socket = INVALID_SOCKET;
   628    645   	@throw [OFBindFailedException exceptionWithClass: [self class]
   629    646   						  socket: self
   630    647   						    host: host
   631    648   						    port: port];

Modified tests/Makefile from [a5eab7fd1a] to [2ffb33610c].

    89     89   
    90     90   EBOOT.PBP: ${PROG_NOINST}
    91     91   	psp-fixup-imports ${PROG_NOINST}
    92     92   	mksfo "ObjFW Tests" PARAM.SFO
    93     93   	psp-strip ${PROG_NOINST}
    94     94   	pack-pbp $@ PARAM.SFO NULL NULL NULL NULL NULL ${PROG_NOINST} NULL
    95     95   
           96  +boot.dol: ${PROG_NOINST}
           97  +	elf2dol ${PROG_NOINST} $@
           98  +
    96     99   include ../buildsys.mk
    97    100   
    98    101   CPPFLAGS += -I../src -I../src/exceptions -I../src/runtime -I.. -DSTDOUT
    99    102   LIBS := -L../src -lobjfw ${TESTS_LIBS} ${LIBS}
   100    103   LDFLAGS += ${MAP_LDFLAGS}
   101    104   LD = ${OBJC}

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

    85     85   		return of_application_main(&argc, &argv,
    86     86   		    [TestsAppDelegate class]);
    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         -		OFString *backtrace = [[e backtrace]
    93         -		    componentsJoinedByString: @"\n  "];
           92  +		OFString *backtrace = [OFString stringWithFormat:
           93  +		    @"\nBacktrace:\n  %@\n\n",
           94  +		    [[e backtrace] componentsJoinedByString: @"\n  "];
    94     95   
    95     96   		[delegate outputString: string
    96     97   			       inColor: RED];
    97         -		if (backtrace != nil) {
    98         -			backtrace = [OFString stringWithFormat:
    99         -			    @"\nBacktrace:\n  %@\n\n", backtrace];
   100         -			[delegate outputString: backtrace
   101         -				       inColor: RED];
   102         -		}
           98  +		[delegate outputString: backtrace
           99  +			       inColor: RED];
   103    100   		[delegate outputString: @"Press home button to exit!\n"
   104    101   			       inColor: NO_COLOR];
   105    102   		for (;;) {
   106    103   			WPAD_ScanPads();
   107    104   
   108    105   			if (WPAD_ButtonsDown(0) & WPAD_BUTTON_HOME)
   109    106   				[OFApplication terminateWithStatus: 1];