Socket (beta version)


Introduction

Note: This software is a beta version and must not be used in game products.

The socket library provides the access to the internet via Nintendo GameCube Broadband Adapter and Nintendo GameCube Modem 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.

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

Berkeley socket API GameCube Socket API
accept() SOAccept()
bind() SOBind()
close() SOClose()
connect() SOConnect()
getpeername() SOGetPeerName()
getsockname() SOGetSockName()
getsockopt()
setsockopt()
SOGetSockOpt()
SOSetSockOpt()
htonl()
htons()
ntohl()
ntohs()
SOHtoNl()
SOHtoNs()

SONtoHl()
SONtoHs()
inet_aton() SOInetAtoN()
inet_ntoa() SOInetNtoA()
inet_ntop() SOInetNtoP()
inet_pton() SOInetPtoN()
listen() SOListen()
poll() SOPoll()
recv()
recvfrom()
read()
SORecv()
SORecvFrom()
SORead()
send()
sendto()
write()
SOSend()
SOSendTo()
SOWrite()
shutdown() SOShutdown()
socket() SOSocket()
gethostbyaddr() SOGetHostByAddr()
gethostbyname() SOGetHostByName()
freeaddrinfo() SOFreeAddrInfo()
getaddrinfo() SOGetAddrInfo()
getnameinfo() SOGetNameInfo()

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 the len field of the structure. The GCN socket functions do not take the length of the socket address structure via a function argument.

Library layers

Internally, the socket library is implemented in the following four layers:

Socket API
TCP API UDP API
IP API
Network interface API

Low level access methods to the network interface and to the IP routing table have not been well standardized. Therefore, the GameCube socket library lets the program directly access to the following IP APIs to get the current network interface configuration and to modify the IP routing table.

IP API Description
IPGetLinkState Determines whether the network interface is up.
IPGetMacAddr Gets the MAC address of the interface.
IPGetMtu Gets the MTU of the interface.
IPGetAddr Gets the primary internet address of the interface.
IPGetAlias Gets the alias internet address of the interface.
IPGetBroadcastAddr Gets the broadcast address of the interface.
IPGetGateway Gets the default gateway address of the interface.
IPGetNetmask Gets the subnet mask of the interface.
IPClearConfigError Clears the host configuration error code.
IPGetConfigError Gets the host configuration error code.
IPSetConfigError Sets the host configuration error code.
IPInitRoute Initializes the IP routing table.
IPAddRoute Adds the specified data to the routing table.
IPRemoveRoute Removes the specified route from the routing table.
IPRefreshRoute Refreshes the routing table using the base network interface configuration.
IPClearInterfaceStat Clears the interface statistics.
IPGetInterfaceStat Gets the interface statistics.
PPPGetMessage Gets the PAP/CHAP response message.

The socket library currently reserves the following prefixes and an application program should not define functions that begin with theses names:

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

Using the socket API

The socket library supports IPv4 (Internet Protocol version 4) and IPv6 (Internet Protocol version 6) experimentally. The socket library also supports two kinds of network adapter: the Nintendo GameCube Broadband Adapter (broadband adapter) and the Nintendo GameCube Modem Adapter (modem adapter).

Three combinations of the network adapter and IPv4/IPv6 are shown below:

To use one of the combination, a program must be linked with each library shown in the following table.

Library

IPv4 IPv6

Description

broadband adapter modem adapter broadband adapter
ip[D].a • • the internet protocol version 4 stack.
ip6[D].a • the internet protocol version 4 and 6 stacks (dual stack).
eth[D].a • • • the low-level broadband adapter interface.
ipmdm[D].a • the library for modem adapter and socket.
mdm[D].a • the low-level modem adapter interface.

To use IPv4 (Internet Protocol version 4) functions of the socket library with the broadband adapter, 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.

The socket library experimentally supports IPv6 (Internet Protocol version 6) for the broadband adapter only. To use IPv6 functions, a program must be linked with ip6[D].a, instead of ip[D].a. ip6[D].a implements the internet protocol version 4 and 6 stacks (dual stack). In addition to ip6[D].a, a program must be linked with eth[D].a.

The socket library also supports the Nintendo GameCube modem adapter. To access to the internet via a modem adapter, a program must be linked with ip[D].a, eth[D].a, ipmdm[D].a and mdm[D].a. ipmdm[D].a implements the socket library for the modem adapter. mdm[D].a implements the low-level modem adapter interface.

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

#include <dolphin/socket.h>

Configure the network interface

The program must first initialize the socket library with SOInit(). And then the program can configure the network interface with SOStartup().

Note: It is strongly recommended that the program checks the link status of the broadband adapter with IPGetLinkState() before configuring the network interface.

There are four ways to configure the network interface, when the broadband adapter is attached.

  1. Configure the network interface manually.
  2. Configure the network interface via DHCP.
  3. Configure the network interface via PPPoE.
  4. Configure the network interface via IPv6 stateless address autoconfiguration.

There is only one way to configure the network interface, when the modem adapter is attached.

  1. Configure the network interface via PPP.

To configure the network interface via DHCP, initialize the SOConfig{} structure as shown below. The SO_FLAG_DHCP flag set in the flag field sets the library to configure the interface using DHCP.

    
SOConfig Config =
{
    SO_VENDOR_NINTENDO,     // vendor
    SO_VERSION,             // version

    Alloc,                  // alloc
    Free,                   // free

    SO_FLAG_DHCP,           // flag
    SOHtoNl(SO_INADDR_ANY), // addr
    SOHtoNl(SO_INADDR_ANY), // netmask
    SOHtoNl(SO_INADDR_ANY), // router
    SOHtoNl(SO_INADDR_ANY), // dns1
    SOHtoNl(SO_INADDR_ANY), // dns1

    4096,                   // timeWaitBuffer
    4096                    // reassemblyBuffer
};

To configure the network interface via PPPoE, initialize the SOConfig{} structure as shown below. The SO_FLAG_PPPoE flag set in the flag field sets the library to configure the interface using PPPoE. The peerid and passwd fields are used if the access concentrator requires user authentication.

Note: The current implementation supports only PAP and CHAP for user authentication method.

    
SOConfig Config =
{
    SO_VENDOR_NINTENDO,     // vendor
    SO_VERSION,             // version

    Alloc,                  // alloc
    Free,                   // free

    SO_FLAG_PPPoE,          // flag
    SOHtoNl(SO_INADDR_ANY), // addr
    SOHtoNl(SO_INADDR_ANY), // netmask
    SOHtoNl(SO_INADDR_ANY), // router
    SOHtoNl(SO_INADDR_ANY), // dns1
    SOHtoNl(SO_INADDR_ANY), // dns1

    4096,                   // timeWaitBuffer
    4096,                   // reassemblyBuffer

    0,                      // mtu (default)
    0,                      // rwin (default)
    0,                      // r2 (default)
    
    "user-id",              // peerid
    "password",             // passwd
};

To configure the network interface via IPv6, initialize the SOConfig{} structure as shown below. The SO_FLAG_IPv6 flag set in the flag field sets the library to configure the interface using IPv6 stateless autoconfiguration.

    
SOConfig Config =
{
    SO_VENDOR_NINTENDO,     // vendor
    SO_VERSION,             // version

    Alloc,                  // alloc
    Free,                   // free

    SO_FLAG_IPv6,           // flag
    SOHtoNl(SO_INADDR_ANY), // addr
    SOHtoNl(SO_INADDR_ANY), // netmask
    SOHtoNl(SO_INADDR_ANY), // router
    SOHtoNl(SO_INADDR_ANY), // dns1
    SOHtoNl(SO_INADDR_ANY), // dns1

    4096,                   // timeWaitBuffer
    4096                    // reassemblyBuffer
};

To configure the network interface via PPP, initialize the SOConfig{} structure as shown below. The SO_FLAG_PPP flag set in the flag field sets the library to configure the interface using PPP. A telephone number must be specified to dial field. A modem country code chosen from $DOLPHIN_ROOT/include/dolphin/mdm_country.h must be set to countryCode field. Dial mode (tone or pulse) is specified to dialMode field according to communication environment, if required. The peerid and passwd fields are used if the access concentrator requires user authentication.

Note: The current implementation supports only PAP and CHAP for user authentication method.

    
SOConfig Config =
{
    SO_VENDOR_NINTENDO,     // vendor
    SO_VERSION,             // version

    Alloc,                  // alloc
    Free,                   // free

    SO_FLAG_PPP,            // flag
    SOHtoNl(SO_INADDR_ANY), // addr
    SOHtoNl(SO_INADDR_ANY), // netmask
    SOHtoNl(SO_INADDR_ANY), // router
    SOHtoNl(SO_INADDR_ANY), // dns1
    SOHtoNl(SO_INADDR_ANY), // dns1

    4096,                   // timeWaitBuffer
    4096,                   // reassemblyBuffer

    0,                      // mtu (default)
    0,                      // rwin (default)
    0,                      // r2 (default)
    
    "user-id",              // peerid
    "password",             // passwd
    0,                      // PPPoE service name
    
    0,                      // DHCP host name
    0,                      // TCP total retransmit times (default 4)
    
    0,                      // default UDP send buffer size (default 1472)
    0,                      // defualt UDP receive buffer size (default 4416)
    
    "0123456789",           // telephone number
    SO_DIAL_MODE_TONE,      // dial mode
    MDM_CC_US               // modem country code
};

If no flag is set in the flag field (i.e., SOConfig.flag member is zero), the interface is configured manually using addr, netmask and router fields.

The host configuration via DHCP or PPPoE can fail from various reasons (DHCP server does not exist, given peerid and passwd is not correct, etc.). If the configuration is failed, a configuration error code is set which can be get with IPGetConfigError(). Once the program calls SOStartup(), it should call IPGetConfigError() repeatedly to verify the network configuration is in normal operation. Once the host configuration is completed, SOGetHostID() returns a non-zero IP address. It can take up to a minute until the host is configured via DHCP or PPPoE.

Dynamically configured host settings can be queried with IPGetAddr() and other IP interface functions.

Note: Even if the network interface is not configured by any means, it can still broadcast and receive UDP datagrams.

Note: Without a broadband adapter, sockets bound to the loopback address can be used.

Using domain name system (IPv4)

Up to two DNS server addresses can be specified with SOStartup() or SOSetResolver(). If DNS server addresses are obtained via DHCP or PPPoE, then the assigned server addresses can be retrieved with SOGetResolver(). To look up the host entry, use SOGetHostByAddr() or SOGetHostByName() functions.

Interface and library termination

Once the program no longer needs access to the network, it must shutdown the network interface with SOCleanup(). SOCleanup() releases the internet resources including the IP address assigned from the DHCP server, the PPP session opened with the access concentrator, etc.

A host network connection can be compulsory terminated if the lease time of the IP address assigned from the DHCP server expires, the remote PPPoE access concentrator terminates the current PPP session or the network address collision is detected. The program may check the cause of the error with IPGetConfigError(). If a compulsory termination of the network connection is detected, the program should immediately shutdown the network interface with SOCleanup().

Using transport protocols

Currently the socket library supports TCP and UDP. To use TCP, specify SO_SOCK_STREAM with SOSocket(). To use UDP, specify SO_SOCK_DGRAM with SOSocket().

The programming using transport protocols is basically same as the standard socket network programming. Please refer the popular text books for the socket network programming in general.

Demo programs

dhcp.elf (DHCP host configuration demo )

This program shows how to configure the host with SOStartup() using DHCP.

pppoe.elf (PPPoE host configuration demo )

This program shows how to configure the host with SOStartup() using PPPoE.

host.elf (DNS lookup demo)

This program shows how to look up domain name system (DNS) to get the host name from the IP address and vice versa.

httpd.elf (a trivial HTTP server)

This program is a trivial web server implementation. The contents under the 'www' folder in the disc can be accessed with the web browser.

netcfg.elf (Network Setup Screen demo)

This program shows how to load a network configuration file from a memory card and how to call Network Setup Screen.

ipv6.elf (IPv6 host configuration demo)

This program shows how to configure the host with SOStartup() using IPv6 stateless address autoconfiguration.

RFC Compliance

The current socket library implementation supports the following RFCs:

RFC Protocol Description
768 UDP User Datagram Protocol
777 ICMP Internet Control Message Protocol
791 IP Internet Protocol
793 TCP Transmission Control Protocol
826 ARP An Ethernet Address Resolution Protocol
1034, 1035 DNS Domain Name System (stub only)
1191 TCP Path MTU Discovery
1332 IPCP The PPP Internet Protocol Control Protocol
1334 PAP PPP Password Authentication Protocol
1661 PPP The Point-to-Point Protocol
1994 CHAP PPP Challenge Handshake Authentication Protocol
2018 TCP Selective Acknowledgment (SACK)
2131, 2132 DHCP Dynamic Host Configuration Protocol
2236 IGMPv2 Internet Group Management Protocol, Version 2
2460, 2461,
2462, 2463
IPv6 Internet Protocol, Version 6 (IPv6)
2516 PPPoE PPP Over Ethernet
2581 TCP Slow start, Congestion Avoidance, Fast Retransmit, Fast Recovery, Delayed ACK
2988 TCP Computing TCP's Retransmission Timer
3021 IP Using 31-Bit Prefixes on IPv4 Point-to-Point Links

Note: The GameCube socket library employs Berkeley interpretation for the TCP urgent pointer: The TCP urgent pointer points to the byte next to the urgent byte.

Note: The fast retransmit algorithm is activated only when the SACK option is used between two end points in the GameCube socket implementation. Also the GameCube socket implementation uses the FACK congestion control algorithm instead of the fast recovery algorithm.

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. Practically these items are not really important for most network applications of these days as packets with source route options are often simply discarded by routers for security reasons.

Layer Feature Description
IP Source Route Option To be available in the future release
UDP Pass received IP options to application layer To be available in the future release
TCP Source Route Option To be available in the future release

RFC 2525 catalogs a number of known TCP implementation problems. The GameCube socket implementation is adhering to this document for the stability of the global Internet. "SHOULD" items in RFC 1122 that are pointed in RFC 2525 are also implemented in addition to the "MUST" items.