/*---------------------------------------------------------------------------*
  Project:  tcp client test program
  File:     sotcp_client.c

  Copyright 2002 Nintendo.  All rights reserved.

  These coded instructions, statements, and computer programs contain
  proprietary information of Nintendo of America Inc. and/or Nintendo
  Company Ltd., and are protected by Federal copyright law.  They may
  not be disclosed to third parties or copied or duplicated in any form,
  in whole or in part, without the prior written consent of Nintendo.

 *---------------------------------------------------------------------------*/

#include <ios.h>
#include <iostypes.h>
#include <ioslibc.h>
#include <socket.h>

#define STACK_SIZE          (4 * 1024)
const u8 _initStack[STACK_SIZE] __attribute__ ((aligned (4)));
const u32 _initStackSize = sizeof(_initStack);
const u32 _initPriority = 10;

//const u8 Host[4]        = {  10,   0,   0,  33 };
const u8 Server[4]      = {  10,   0,   0, 177 };
const u8 Netmask[4]     = { 255, 255, 255,   0 };
const u8 Gateway[4]     = {  10,   0,   0,   6 };

SOConfig Config =
{
    SO_VENDOR_NINTENDO,
    SO_VERSION,

    0,
    0,

    SO_FLAG_DHCP,
    { SO_INADDR_ANY },
    { SO_INADDR_ANY },
    { SO_INADDR_ANY },
    { SO_INADDR_ANY },
    { SO_INADDR_ANY },

    4096,
    4096
};

int main(void)
{
    s32      rc;
    int      fd = -1;
    int      i;

    //memcpy(&Config.addr,    Host,     IP_ALEN);
    memcpy(&Config.netmask, Netmask,    IP_ALEN);
    memcpy(&Config.router,  Gateway,    IP_ALEN);

    rc = SOStartup(&Config);
    if (rc < 0) {
        printf("SOStartup failed, return = %d\n", rc);
        goto done;
    }

    while (SOGetHostID() == 0) {
        usleep(10000);
    }

    fd = SOSocket(SO_PF_INET, SO_SOCK_STREAM, 0);
    if (fd < 0) {
        printf("SOSocket failed, return = %d\n", fd);
        goto cleanup;
    }

    struct SOHostEnt* h;
    char host[] = "www.yahoo.com";
    u16 port = 80;
    
    printf("calling SOGetHostByName, host = %s\n", host);
    h = SOGetHostByName(host);

    if (!h) {
        printf("SOGetHostByName failed, return = %d\n", (u32) h);
        goto close;
    }

    printf("%s: SOGetHostByName results --\n", __FUNCTION__);
//    printf("%s: h=0x%x\n", __FUNCTION__, (u32)h);
//    printf("%s: &(h->name) = 0x%x\n", __FUNCTION__, (u32) h->name);
    printf("%s: h->name = %s\n", __FUNCTION__, h->name);
    printf("%s: h->length = %d\n", __FUNCTION__, h->length);
    printf("%s: h->addrType = %d\n", __FUNCTION__, h->addrType);
    printf("%s: h->addrList = 0x%x\n", __FUNCTION__, (u32) h->addrList);

    for (i=0; h->addrList[i]; i++) {
        char *a = (char *) h->addrList[i];
        printf("%s: h->addrList[%d] = %d:%d:%d:%d (0x%x:%x:%x:%x)\n",
               __FUNCTION__, i,
               a[0], a[1], a[2], a[3],
               a[0], a[1], a[2], a[3]);
    }
    
    IPSocket socket;
    
    socket.len = IP_SOCKLEN;
    socket.family = IP_INET;
    socket.port = SOHtoNs(port);
    
    for (i=0; h->addrList[i]; i++) {
        char *a = (char *) h->addrList[i];
        printf("%s: trying to connect to %d:%d:%d:%d (0x%x:%x:%x:%x)\n",
                 __FUNCTION__,
                 a[0], a[1], a[2], a[3],
                 a[0], a[1], a[2], a[3]);
        memcpy(socket.addr, h->addrList[i], IP_ALEN);
        
        rc = SOConnect(fd, &socket);
        if (rc == 0) {
            printf("success!\n");
            goto close;
        } else {
            printf("SOConnect failed, return = %d\n", rc);
        }
    }

    printf("FAIL!\n");

close:
    if ( fd >= 0 ) {
        rc = SOClose(fd);
        if ( rc < 0 )
            printf("SOClose failed, return = %d\n", rc);
    }

cleanup:
    SOCleanup();

done:
    printf("Done.");

    return (rc);
}
