Socket Library (beta version)


Introduction

The socket library provides the access to the internet via the Nintendo GameCube broadband adapter. The socket library API has many resemblance to Berkeley sockets which are the de facto standard programming interface for networking. Programs written to Berkeley sockets interface can be ported to GameCube very easily.

Library structure

The socket library is implemented in four layers as illustrated in the following figure:

Socket API
TCP API UDP API
IP API
Network interface API

Though it is not strongly recommended, the advanced network programmers can directly access the TCP, UDP and other APIs in lower levels bypassing the socket API layer. Low level APIs are introduced later in this page.

The socket library currently reserves the following prefixes:

Prefix Description
ARP address resolution protocol
DHCP dynamic host configuration protocol
DNS domain name system
ICMP internet control message protocol
IF interface
IP internet protocol
SO socket
TCP transmission control protocol
UDP user datagram protocol

The following table illustrates the correspondence of the GCN socket APIs and Berkeley sockets.

Berkeley socket API GameCube Socket API GameCube UDP API GameCube TCP API
accept() SOAccept()   TCPAccept[Async]()
bind() SOBind() UDPBind() TCPBind()
close() SOClose() UDPClose() TCPClose[Async]()
TCPCancel()
connect() SOConnect() UDPConnect() TCPConnect[Async]()
getpeername() SOGetPeerName() UDPGetRemoteSocket() TCPGetRemoteSocket()
getsockname() SOGetSockName() UDPGetLocalSocket() TCPGetLocalSocket()
getsockopt()
setsockopt()
SOGetSockOpt()
SOSetSockOpt()
UDPGetSockOpt()
UDPSetSockOpt()
TCPGetSockOpt()
TCPSetSockOpt()
listen() SOListen()   TCPListen()
recv()
recvfrom()
read()
SORecv()
SORecvFrom()
SORead()
UDPReceive[Async]()
UDPPeek()
TCPReceive[Urg][Async]()
TCPPeek()
send()
sendto()
write()
SOSend()
SOSendTo()
SOWrite()
UDPSend[Async]() TCPSend[Urg][Async]()
shutdown() SOShutdown()   TCPShutdown()
socket() SOSocket() UDPOpen() TCPOpen()
misc. SOStartup()
SOCleanup()
UDPCancel()
 
TCPGetStatus()
TCPSetTimeout()

TCPGetUrgOffset()
       
Berkeley socket API GameCube Socket API GameCube IP API GameCube DNS API
inet_aton() SOInetAtoN() IPAtoN()  
inet_ntoa() SOInetNtoA() IPNtoA()  
gethostbyaddr() SOGetHostByAddr()   DNSGetName()
gethostbyname() SOGetHostByName()   DNSGetAddr()

The GCN socket API is primarily different from Berkeley sockets in the following points:

  1. Functions, structures and define names take "SO" prefix. (e.g.,  SOClose(), SOHostEnt, SO_PF_INET)
  2. Functions return an error code rather than -1 if an error occurs for better thread support.
  3. The length of the socket address structure (e.g., SOSockAddrIn) must be set in len field of the structure. The GCN socket functions do not take the length of the socket address structure via a function argument.

Using the socket API

To use the socket library, a program must be linked with ip[D].a and eth[D].a. ip[D].a implements the internet protocol stack. eth[D].a implements the low-level broadband adapter interface.

In a network program source code, the following header file must be included:

    #include <dolphin/socket.h>

The program must initialize the network interface and the library with SOStartup() before using any other socket functions. Then the following programming step is basically same as the standard socket network programming. Please refer the network programming text books based on sockets for the socket network programming in general.

Using the low level APIs

Using the low level APIs, programmers can manually allocate the main memory block for the library specific data structures like the other GCN SDK libraries including:

The 2 MSL time-wait buffer is unique in GCN protocol stack implementation. Please refer to the function description of TCPSetTimeWaitBuffer().

Programming Steps

Interface and library initialization

The network interface and the library must be initialized with IFInit() at first. IFInit() initializes the broadband adapter and the library data structures including the IP routing table. If the adapter is functioning correctly, IFInit() returns TRUE. 

Caution: The low level API and the socket API can be used together. In this case, the host configuration must be performed by SOStartup() and the low level IP and IF functions that configure the host must not be called. Once host is configured by SOStartup(), then a program can call low level TCP, UDP, DNS, IP and IF functions unless they do not modify the host configuration until SOCleanup() is called.

Configure the network interface

The network interface is not configured and is not assigned any IP addresses after the call to IFInit(). Note that even if the network interface is not configured by any means, it can still broadcast and receive UDP datagrams. 

There are two ways to configure the network interface.

  1. Configure the network interface manually.
  2. Configure the network interface via DHCP.

To manually configure the network interface, use IPInitRoute() as shown below.


    u8 Host[4]    = {  10, 119,   1,  75 };  // Host's IP address
    u8 Netmask[4] = { 255, 255, 255,   0 };  // Netmask of the host's network
    u8 Gateway[4] = {  10, 119,   1,   1 };  // Default gateway address of the host's network  

    IPInitRoute(Host, Netmask, Gateway);

To configure the network interface via DHCP, call DHCPStartup() and then check the DHCP state machine via DHCPGetStatus() until the host is configured as shown below. It will take up to a minute until the host is configured. If the DHCP configuration is failed, probably no DHCP server is running in the connected network.

    
    for (;;)
    {
        state = DHCPGetStatus(&Dhcp);
        if (state == DHCP_STATE_INIT)
        {
            // Host configuration failed
            break;
        }
        if (state == DHCP_STATE_BOUND ||
            state == DHCP_STATE_RENEWING ||
            state == DHCP_STATE_REBINDING)
        {
            // An IP address has been assigned
            break;
        }
    }

Using transport protocols

Currently UDP and TCP are supported by the socket library. Like the other GCN SDK libraries, the socket library lets the game programmers allocate the main memory for the library specific data structures including:

The 2 MSL time-wait buffer is unique in GCN protocol stack implementation. Please refer to the function description of TCPSetTimeWaitBuffer().

Using domain name system

Currently a domain name lookup system using UDP has been implemented. Please refer to DNS section in the table of content.

Note: The socket API provides hi-level SOGetHostByAddr() and SOGetHostByName() functions.

Test programs

http1991.elf (an HTTP 0.9 server)

For an artist station, running HTTP server in GameCube would be one easy way for an artist to perform various tasks. The http1991.elf is a very trivial HTTP server implementation based on HTTP 0.9 specification as defined in early 90's. A CGI capable HTTP server implementation will be released in future release of the socket library.

host.elf (a DNS lookup demo)

host.elf illustrates the use of DNS API.

RFC 1122 Compliance

RFC 1122 defines the requirements for Internet host software. The current implementation does not implement the following required ("MUST") items and thus it is not RFC 1122 compliant.

Layer Feature Description
IP Source Route Option To be available in the beta release
UDP Pass rcv'd IP options to applic layer To be available in the beta release
TCP Source Route Option To be available in the beta release

The current goal is conditionally compliant implementation where not all the "SHOULD" requirements are implemented.