Back to index

lightning-sunbird  0.9+nobinonly
Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes
nsSocket Class Reference

#include <nsSocket.h>

Collaboration diagram for nsSocket:
Collaboration graph
[legend]

List of all members.

Public Types

enum  {
  OK = 0, E_USER_CANCEL = -813, E_PARAM = -1001, E_MEM = -1002,
  E_INVALID_HOST = -1003, E_SOCK_OPEN = -1004, E_SOCK_CLOSE = -1005, E_TIMEOUT = -1006,
  E_WRITE = -1007, E_READ_MORE = -1008, E_READ = -1009, E_SMALL_BUF = -1010,
  E_EOF_FOUND = -1011, E_BIND = -1012, E_LISTEN = -1014, E_ACCEPT = -1015,
  E_GETSOCKNAME = -1016, E_WINSOCK = -1017, E_INVALID_ADDR = -1018
}

Public Member Functions

 nsSocket (char *aHost, int aPort)
 nsSocket (char *aHost, int aPort, int(*aEventPumpCB)(void))
 ~nsSocket ()
int Open ()
int SrvOpen ()
int SrvAccept ()
int Send (unsigned char *aBuf, int *aBufSize)
int Recv (unsigned char *aBuf, int *aBufSize)
int Recv (unsigned char *aBuf, int *aBufSize, int aTimeoutThresholdUsecs)
int Close ()
int GetHostPortString (char **aHostPort)

Static Public Member Functions

static float CalcRate (struct timeval *aPre, struct timeval *aPost, int aBytes)

Private Member Functions

int IsIPAddress (char *aAddress)

Private Attributes

int(* mEventPumpCB )(void)
char * mHost
int mPort
int mFd
int mListenFd

Detailed Description

Definition at line 47 of file nsSocket.h.


Member Enumeration Documentation

anonymous enum
Enumerator:
OK 
E_USER_CANCEL 
E_PARAM 
E_MEM 
E_INVALID_HOST 
E_SOCK_OPEN 
E_SOCK_CLOSE 
E_TIMEOUT 
E_WRITE 
E_READ_MORE 
E_READ 
E_SMALL_BUF 
E_EOF_FOUND 
E_BIND 
E_LISTEN 
E_ACCEPT 
E_GETSOCKNAME 
E_WINSOCK 
E_INVALID_ADDR 

Definition at line 57 of file nsSocket.h.

    {
        OK              = 0,
        E_USER_CANCEL   = -813, /* user canceled the download */
        E_PARAM         = -1001,
        E_MEM           = -1002,
        E_INVALID_HOST  = -1003,
        E_SOCK_OPEN     = -1004,
        E_SOCK_CLOSE    = -1005,
        E_TIMEOUT       = -1006,
        E_WRITE         = -1007,
        E_READ_MORE     = -1008,
        E_READ          = -1009,
        E_SMALL_BUF     = -1010,
        E_EOF_FOUND     = -1011,
        E_BIND          = -1012,
        E_LISTEN        = -1014,
        E_ACCEPT        = -1015,
        E_GETSOCKNAME   = -1016,
        E_WINSOCK       = -1017,
        E_INVALID_ADDR  = -1018
    };

Constructor & Destructor Documentation

nsSocket::nsSocket ( char *  aHost,
int  aPort 
)

Definition at line 117 of file nsSocket.cpp.

                                         :
    mEventPumpCB( NULL ),
    mHost(aHost),
    mPort(aPort),
    mFd(-1),
    mListenFd(-1)
{
}
nsSocket::nsSocket ( char *  aHost,
int  aPort,
int(*)(void aEventPumpCB 
)

Definition at line 108 of file nsSocket.cpp.

                                                                    :
    mEventPumpCB( aEventPumpCB ),
    mHost(aHost),
    mPort(aPort),
    mFd(-1),
    mListenFd(-1)
{
}

Definition at line 126 of file nsSocket.cpp.

{
    // don't release mHost cause we don't own it
}

Member Function Documentation

float nsSocket::CalcRate ( struct timeval aPre,
struct timeval aPost,
int  aBytes 
) [static]

Definition at line 542 of file nsSocket.cpp.

{
    float diffUsecs, rate;

    /* param check */
    if (!aPre || !aPost || aBytes <= 0)
        return 0;
    
    diffUsecs = (float)(aPost->tv_sec - aPre->tv_sec) * kUsecsPerSec;
    diffUsecs += (float)aPost->tv_usec - (float)aPre->tv_usec;
    rate = ((float)aBytes)/((float)diffUsecs) * kUsecsPerKBFactor;

    return rate;
}

Here is the caller graph for this function:

Definition at line 446 of file nsSocket.cpp.

{
    int rv = OK, rv1 = OK, rv2 = OK;

/* funky windows shutdown of winsock */
#ifdef _WINDOWS
    closesocket(mFd);
    if (mListenFd > 0)
        closesocket(mListenFd);
    
    if (rv1 != 0 || rv2 != 0)
        rv = E_SOCK_CLOSE; 

    int wsaErr = WSACleanup();
    if (wsaErr != 0)
        rv = wsaErr;
#else /* unix or mac */
    rv1 = close(mFd);
    if (mListenFd > 0)
        rv2 = close(mListenFd);
    if (rv1 != 0 || rv2 != 0)
        rv = E_SOCK_CLOSE; 
#endif

    return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int nsSocket::GetHostPortString ( char **  aHostPort)

Definition at line 474 of file nsSocket.cpp.

{
    int rv = OK;
    socklen_t salen;
    struct sockaddr_in servaddr;
    int hpsLen; // host-port string length

    if (!aHostPort)
        return E_PARAM;

    salen = MAXSOCKADDR;
    if ((getsockname(mListenFd, (struct sockaddr *) &servaddr, &salen)) < 0)
    {
        *aHostPort = NULL;
        return E_GETSOCKNAME;
    }

    hpsLen = strlen("AA1,AA2,AA3,AA4,PP1,PP2");
    *aHostPort = (char *) malloc(hpsLen);
    if (!*aHostPort)
       return E_MEM;
 
    memset(*aHostPort, 0, hpsLen);
    sprintf(*aHostPort, "%d,%d,%d,%d,%d,%d",
      (int)((char*)&servaddr.sin_addr)[0] & 0xFF,
      (int)((char*)&servaddr.sin_addr)[1] & 0xFF,
      (int)((char*)&servaddr.sin_addr)[2] & 0xFF,
      (int)((char*)&servaddr.sin_addr)[3] & 0xFF,
      (int)((char*)&servaddr.sin_port)[0] & 0xFF,
      (int)((char*)&servaddr.sin_port)[1] & 0xFF);

    return rv;    
}

Here is the call graph for this function:

int nsSocket::IsIPAddress ( char *  aAddress) [private]

Definition at line 509 of file nsSocket.cpp.

{
    int addr[4];
    int numDots = 0;
    
    for (unsigned int i=0; i < strlen(aAddress); ++i)
    {
        if (isspace(aAddress[i]))
            return E_INVALID_ADDR;
        if (aAddress[i] == '.')
        {
            ++numDots;
            if (numDots > 3)
                return E_INVALID_ADDR;
        }
        else if (!isdigit(aAddress[i]))
            return E_INVALID_ADDR;
    }

    if (sscanf(aAddress, "%d.%d.%d.%d", 
        &addr[0], &addr[1], &addr[2], &addr[3]) != 4)
        return E_INVALID_ADDR;

    if ((addr[0] > 255) || 
        (addr[1] > 255) || 
        (addr[2] > 255) || 
        (addr[3] > 255))
        return E_INVALID_ADDR;

    return OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 132 of file nsSocket.cpp.

{
#ifdef _WINDOWS
    /* funky windows initialization of winsock */

    int err;
    WSADATA wsaData;
    WORD wVersionRequested;
    
    /* We don't care which version we get because we're not
     * doing any specific to a particular winsock version. */
    /* Request for version 2.2 */
    wVersionRequested = MAKEWORD(2, 2);
    err = WSAStartup(wVersionRequested, &wsaData);
    if (err == WSAVERNOTSUPPORTED)
    {
        /* Request for version 1.1 */
        wVersionRequested = MAKEWORD(1, 1);
        err = WSAStartup(wVersionRequested, &wsaData);
        if (err == WSAVERNOTSUPPORTED)
        {
            /* Request for version 1.0 */
            wVersionRequested = MAKEWORD(0, 1);
            err = WSAStartup(wVersionRequested, &wsaData);
            if (err == WSAVERNOTSUPPORTED)
            {
                /* Request for version 0.4 */
                wVersionRequested = MAKEWORD(4, 0);
                err = WSAStartup(wVersionRequested, &wsaData);
            }
        }
    }

    if (err != 0)
    {
        return E_WINSOCK;
    }
#endif

    int rv = OK;
    struct sockaddr_in servaddr;
    struct hostent *hptr = NULL;

    mFd = socket(AF_INET, SOCK_STREAM, 0);
#ifdef _WINDOWS
       if (mFd == INVALID_SOCKET) {
                     printf("Last error: %d\n", WSAGetLastError());
    }
    if ( mFd != INVALID_SOCKET ) {
#else
    if ( mFd != -1 ) {
#endif

        int windowSize = 32768;   // we could tune this, but for now...
        socklen_t newTCPWin, len;
       
        len = sizeof( newTCPWin );
 
        setsockopt( mFd, SOL_SOCKET, SO_RCVBUF, (char*) &windowSize, sizeof( windowSize ));
#ifdef DEBUG
        getsockopt( mFd, SOL_SOCKET, SO_RCVBUF, (char*) &newTCPWin, &len );
#endif
 
        setsockopt( mFd, SOL_SOCKET, SO_SNDBUF, (char*) &windowSize, sizeof( windowSize ));
#ifdef DEBUG
        getsockopt( mFd, SOL_SOCKET, SO_RCVBUF, (char*) &newTCPWin, &len );
#endif

        memset(&servaddr, 0, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_port = htons(mPort);

        if ((hptr = gethostbyname(mHost)) == NULL )
        {
            if (IsIPAddress(mHost) == OK)
            {
                unsigned long netAddr;

                netAddr = inet_addr(mHost);
                if ((hptr = gethostbyaddr((char *)&netAddr, sizeof(unsigned long), AF_INET)) == NULL )
                    return E_INVALID_HOST;
            }
            else
            {
                return E_INVALID_HOST;
            }
        }

        memcpy(&servaddr.sin_addr, (struct in_addr **) hptr->h_addr_list[0], sizeof(struct in_addr));

        rv = connect(mFd, (struct sockaddr *) &servaddr, sizeof(servaddr));
        if (rv < 0)
        {
#if defined(DEBUG) && (defined(__unix) || defined(__unix__))
            printf("ETIMEDOUT: %d\n", ETIMEDOUT);
            printf("ECONNREFUSED: %d\n", ECONNREFUSED);
            printf("EHOSTUNREACH: %d\n", EHOSTUNREACH);
            printf("ENETUNREACH: %d\n", ENETUNREACH);

            printf("connect error: %d\n", errno);
#endif /* DEBUG && (__unix || __unix__) */
            mFd = -1;
            rv = E_SOCK_OPEN;
        }
        else
            rv = OK;
    } else
        rv = E_SOCK_OPEN;

    return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int nsSocket::Recv ( unsigned char *  aBuf,
int aBufSize 
)

Definition at line 342 of file nsSocket.cpp.

{
  return(Recv(aBuf, aBufSize, kRecvTimeoutThresholdUsecs));
}

Here is the caller graph for this function:

int nsSocket::Recv ( unsigned char *  aBuf,
int aBufSize,
int  aTimeoutThresholdUsecs 
)

Definition at line 348 of file nsSocket.cpp.

{
    int  rv = OK;
    unsigned char lbuf[kReadBufSize]; /* function local buffer */
    int bytesrd = 0;
    struct timeval seltime;
    fd_set selset;
    int bufsize;
    int timeout;

    if (!aBuf || (aBufSize && (*aBufSize <= 0)) || mFd < 0)
        return E_PARAM;
    memset(aBuf, 0, *aBufSize);

    timeout = 0;
    while (timeout < aTimeoutThresholdUsecs)
    {
        /* return if we anticipate overflowing caller's buffer */
        if (bytesrd >= *aBufSize)
            return E_READ_MORE;

        memset(&lbuf, 0, kReadBufSize);

        FD_ZERO(&selset);
        FD_SET(mFd, &selset);
        seltime.tv_sec = 0;
        seltime.tv_usec = kTimeoutSelectUsecs;

        if ( mEventPumpCB != NULL )
            if (mEventPumpCB() == E_USER_CANCEL)
                return E_USER_CANCEL;

        rv = select(mFd+1, &selset, NULL, NULL, &seltime);
        switch (rv)
        {
            case -1:            /* error occured! */
                return errno;
            case 0:             /* timeout; retry */
                timeout += kTimeoutSelectUsecs;
                continue;
            default:            /* ready to read */
                timeout = 0;    /* reset the time out counter */
                break;
        }

        if (!FD_ISSET(mFd, &selset))
        {
            timeout += kTimeoutSelectUsecs;
            continue;           /* not ready to read; retry */
        }

        bufsize = *aBufSize - bytesrd;
        rv = read(mFd, lbuf, bufsize);
        if (rv == 0) /* EOF encountered */
        {
            rv = E_EOF_FOUND;
            break;
        }
        if (rv < 0)
        {
            rv = E_READ;
            break;
        }

        if (*aBufSize >= bytesrd + rv)
        {
            memcpy(aBuf + bytesrd, lbuf, rv);
            bytesrd += rv;
            if (rv <= bufsize)
            {
                FD_ZERO(&selset);
                FD_SET(mFd, &selset);
                seltime.tv_sec = 0;
                seltime.tv_usec = kTimeoutSelectUsecs;

                /* check if we still need to read from this socket */
                rv = select(mFd+1, &selset, NULL, NULL, &seltime);
                if (rv == 1)
                    rv = E_READ_MORE;
                else
                    rv = OK;
                break;
            }
        }
        else
        {
            rv = E_SMALL_BUF;
            break;
        }
    }
    if (timeout >= aTimeoutThresholdUsecs)
        return E_TIMEOUT;

    *aBufSize = bytesrd;
    return rv;
}

Here is the call graph for this function:

int nsSocket::Send ( unsigned char *  aBuf,
int aBufSize 
)

Definition at line 284 of file nsSocket.cpp.

{
    int rv = OK;
    struct timeval seltime;
    int timeout = 0;
    fd_set selset;

    if (!aBuf || (aBufSize && (*aBufSize <= 0)) || mFd < 0)
        return E_PARAM;

    while (timeout < kTimeoutThresholdUsecs)
    {
        FD_ZERO(&selset);
        FD_SET(mFd, &selset);
        seltime.tv_sec = 0;
        seltime.tv_usec = kTimeoutSelectUsecs;

        if ( mEventPumpCB != NULL )
            if (mEventPumpCB() == E_USER_CANCEL)
                return E_USER_CANCEL;

        rv = select(mFd+1, NULL, &selset, NULL, &seltime);
        switch (rv)
        {
            case -1:            /* error occured! */
                return errno;
            case 0:             /* timeout; retry */
                timeout += kTimeoutSelectUsecs;
                continue;
            default:            /* ready to write */
                timeout = 0;    /* reset the time out counter */
                break;
        }

        if (!FD_ISSET(mFd, &selset))
        {
            timeout += kTimeoutSelectUsecs;
            continue;           /* not ready to write; retry */
        }
        else
            break;
    }
    if (rv == 0)
        return E_TIMEOUT;

    rv = write(mFd, aBuf, *aBufSize);
    if (rv <= 0)
        rv = E_WRITE;
    else
    {
        *aBufSize = rv;
        rv = OK;
    }

    return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 266 of file nsSocket.cpp.

{
    int rv = OK;
    struct sockaddr cliaddr;
    socklen_t clilen;

    if (mListenFd < 0)
        return E_PARAM;

    clilen = sizeof(cliaddr);
    mFd = accept(mListenFd, (struct sockaddr *) &cliaddr, &clilen);
    if (mFd < 0)
        rv = E_ACCEPT;

    return rv;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 245 of file nsSocket.cpp.

{
    int rv = OK;
    struct sockaddr_in servaddr;

    /* init data socket making it listen */
    mListenFd = socket(AF_INET, SOCK_STREAM, 0);

    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY); /* wildcard */
    servaddr.sin_port = 0; /* let kernel bind an ephemeral port */
    
    if ((bind(mListenFd, (struct sockaddr *) &servaddr, sizeof(servaddr))) != 0)
        return E_BIND;

    if ((listen(mListenFd, SOMAXCONN)) != 0)
        return E_LISTEN;
    return rv;
}

Here is the call graph for this function:


Member Data Documentation

Definition at line 97 of file nsSocket.h.

int nsSocket::mFd [private]

Definition at line 100 of file nsSocket.h.

char* nsSocket::mHost [private]

Definition at line 98 of file nsSocket.h.

Definition at line 101 of file nsSocket.h.

int nsSocket::mPort [private]

Definition at line 99 of file nsSocket.h.


The documentation for this class was generated from the following files: