js' blog

Sockets on the Wii
Created: 13.06.2013 01:46 UTC

I already committed initial support for sockets on the Wii yesterday, however, there were still a few bugs as mentioned in the commit.

In the commit message, I wrote that I was undecided whether I want to add workarounds in ObjFW or whether I want to fix the SDK. Today, I decided to do both and went ahead and implemented the workarounds in ObjFW while filing bugs for devkitPPC.

The first bug in the list was that binding to port 0 does not work on the Wii. I worked around this by having a variable start at 65532 which is decremented on each bind. This works as nothing else should be running on the Wii while executing homebrew code. However, it imposes a restriction to not manually bind to very high port numbers. The variable starts at 65532 as 65533 and 65534 are used by OFStreamObserver for the cancel socket and using the last port just didn't feel right.

The second bug was that gethostbyname() does not work for IPs. I looked around in the POSIX specification and found out that this behaviour is indeed not required by the standard, but it seems to be what everybody else does (at least I couldn't find any OS not accepting IPs for gethostbyname()). As this was not 100% standards compliant anyway, I decided to add some extra code that tries to parse the host string using inet_addr() first and only falls back to gethostbyname() if the former failed. If you are scared because you just read gethostbyname(): Yes, the SDK only provides that, even though the IOS seems to provide getaddrinfo(). And no, gethostbyname() is never used if getaddrinfo() is available ;).

For the two remaining bugs, the workarounds were quite simple:
gethostbyname() simply isn't needed anymore, as we don't bind to port 0 anymore on the Wii, but use our variable instead and thus don't need it anymore to find out to which port the socket was actually bound to. And the addition of the missing struct sockaddr_storage isn't even worth mentioning.

So, after these commits, sockets work with the vanilla devkitPPC SDK for the Wii.