Overview:

The Nsm authentication process is enabled when the R-CODE system 
has an owner.txt file on AIBO's Memory Stick media.

NsmAuthorize() is a windows function to establish a connection to the
R-CODE wireless command line when the authentication process is enabled.

About OWNER.TXT
Put OWNER.TXT in the following path to enable Nsm authentication.

PATH : \OPEN-R\APP\DATA\P\OWNER.TXT

Setup example
--------------------------------
OWNER=aibo
OWNER_PASSWORD=aibo
--------------------------------

------------------------------------------------------------------------

   int NsmAuthorize ( char *HostName, char *OwnerID, char *Password )

   Parameters  HostName    AIBO's host name (IP address separated by period '.')
               OwnerID     Owner ID (Maximum 8 letters)
               Password    Password (Maximum 8 letters)

   Return value
          0           Success
          1           Authentication Failed (wrong password)
          2           Illegal IP address
          3           Illegal Owner ID 
          4           Illegal response data
          11          General Error for the service
          12          The service is not implemented
          13          The service is activated
          -1          Failure to create a socket
          -2          Failure to connect
          -3          recv() Error
          -4          send() Error

------------------------------------------------------------------------
[+] How to use

When connecting to RComm, the authentication process may be disabled.
So, you should first try to connect to port 21002.

If authentication is not required, ACK ('\006') will be returned.
(Header strings will be return before ACK.)

If you need authentication, NAK ('\025') will be returned.
In this case, disconnect and try reconnection after executing NsmAuthorize();

------------------------------------------------------------------------

Example

static char *HostName = "192.168.0.10";
static char *UserID   = "aibo";
static char *Password = "aibo";

int OpenWLAN () // Function to WLAN connection
{
    struct sockaddr_in inet_addr;
    int    fd;
    int    nsm;

Top:

    // try connecting to port 21002 
    memset( (char*)&inet_addr, 0, sizeof(inet_addr) );
    inet_addr.sin_family = AF_INET;
    sscanf( HostName, "%d.%d.%d.%d",
            &(inet_addr.sin_addr.S_un.S_un_b.s_b1),
            &(inet_addr.sin_addr.S_un.S_un_b.s_b2),
            &(inet_addr.sin_addr.S_un.S_un_b.s_b3),
            &(inet_addr.sin_addr.S_un.S_un_b.s_b4) );
    inet_addr.sin_port   = htons((u_short)21002);
    if( (fd = socket( AF_INET, SOCK_STREAM, 0 )) < 0 )
    {
        return 0;
    }
    if( connect( fd, (struct sockaddr *)&inet_addr, sizeof(inet_addr) ) < 0 )
    {
        closesocket( fd );
        return 0;
    }

    // wait until ACK or NAK return
    nsm = 0;
    while(1)
    {
        char ch;
        int  cc = recv( fd, &ch, 1, 0 );
        if( cc < 0 )
        {
            break;
        }
        if( cc > 0 )
        {
            if( ch == '\006' ) // ACK
            {
            	nsm = 1;
                break;
            }
            if( ch == '\025' ) // NAK
            {
                closesocket( fd );
                fd  = 0;
                nsm = 2;
                break;
            }
        }
    }

    if( nsm == 2 ) // Authentication process is enabled.
    {
        int rc;
        Sleep( 300L );
        rc = NsmAuthorize( HostName, UserID, Password );
        if( rc != 0 )
        {
            // Fail
            return 0;
        }
        Sleep( 300L );
        goto Top; // re-connect
    }

    return fd;
}

