Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Typedefs | Functions | Variables
nbconn.c File Reference
#include "nspr.h"
#include "plgetopt.h"
#include <stdio.h>
#include <string.h>

Go to the source code of this file.

Classes

struct  Server_Param

Defines

#define SERVER_MAX_BIND_COUNT   100
#define DATA_BUF_SIZE   256
#define TCP_SERVER_PORT   10000
#define TCP_UNUSED_PORT   211
#define DPRINTF(arg)   if (_debug_on) printf arg

Typedefs

typedef struct Server_Param Server_Param

Functions

static void PR_CALLBACK TCP_Server (void *arg)
static PRIntn connection_success_test ()
static PRIntn connection_failure_test ()
int main (int argc, char **argv)
 The Xalan testcases app.

Variables

int _debug_on

Class Documentation

struct Server_Param

Definition at line 75 of file nbconn.c.

Collaboration diagram for Server_Param:
Class Members
PRSemaphore * addr_sem
PRInt32 conn_counter
PRInt32 datalen
PRInt32 * exit_counter
PRMonitor * exit_mon
PRJobIoDesc iod
PRInt32 * job_counterp
PRNetAddr netaddr
PRFileDesc * sp_fd
PRThreadPool * tp

Define Documentation

#define DATA_BUF_SIZE   256

Definition at line 71 of file nbconn.c.

Definition at line 81 of file nbconn.c.

Definition at line 70 of file nbconn.c.

#define TCP_SERVER_PORT   10000

Definition at line 72 of file nbconn.c.

Definition at line 73 of file nbconn.c.


Typedef Documentation

typedef struct Server_Param Server_Param

Function Documentation

static PRIntn connection_failure_test ( ) [static]

Definition at line 489 of file nbconn.c.

{
       PRFileDesc *sockfd = NULL, *conn_fd = NULL;
       PRNetAddr netaddr;
       PRInt32 i, rv;
    PRPollDesc pd;
    PRSocketOptionData optData;
    PRIntn n, failed_already = 0;

       /*
        * Create a tcp socket
        */
       if ((sockfd = PR_NewTCPSocket()) == NULL) {
              fprintf(stderr,"Error - PR_NewTCPSocket failed\n");
              failed_already=1;
              goto def_exit;
       }
       memset(&netaddr, 0 , sizeof(netaddr));
       netaddr.inet.family = PR_AF_INET;
       netaddr.inet.port = PR_htons(TCP_SERVER_PORT);
       netaddr.inet.ip = PR_htonl(PR_INADDR_ANY);
       /*
        * try a few times to bind server's address, if addresses are in
        * use
        */
       i = 0;
       while (PR_Bind(sockfd, &netaddr) < 0) {
              if (PR_GetError() == PR_ADDRESS_IN_USE_ERROR) {
                     netaddr.inet.port += 2;
                     if (i++ < SERVER_MAX_BIND_COUNT)
                            continue;
              }
              fprintf(stderr,"ERROR - PR_Bind failed: (%d,%d)\n",
                                                               PR_GetError(), PR_GetOSError());
              failed_already=1;
              goto def_exit;
       }

       if (PR_GetSockName(sockfd, &netaddr) < 0) {
              fprintf(stderr,"ERROR - PR_GetSockName failed: (%d,%d)\n",
                                                               PR_GetError(), PR_GetOSError());
              failed_already=1;
              goto def_exit;
       }
#ifdef AIX
       /*
        * On AIX, set to unused/reserved port
        */
       netaddr.inet.port = PR_htons(TCP_UNUSED_PORT);
#endif
       if ((conn_fd = PR_NewTCPSocket()) == NULL) {
              fprintf(stderr,"Error - PR_NewTCPSocket failed\n");
              failed_already=1;
              goto def_exit;
       }
       optData.option = PR_SockOpt_Nonblocking;
       optData.value.non_blocking = PR_TRUE;
       PR_SetSocketOption(conn_fd, &optData);
       rv = PR_Connect(conn_fd, &netaddr, PR_INTERVAL_NO_TIMEOUT);
       if (rv == PR_FAILURE) {
              DPRINTF(("PR_Connect to a non-listen port failed: (%d, %d)\n",
                                                               PR_GetError(), PR_GetOSError()));
       } else {
              PR_ASSERT(rv == PR_SUCCESS);
              fprintf(stderr,"Error - PR_Connect succeeded, expected to fail\n");
              failed_already=1;
              goto def_exit;
       }
       pd.fd = conn_fd;
       pd.in_flags = PR_POLL_WRITE | PR_POLL_EXCEPT;
#ifndef XP_MAC
       n = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT);
#else
       n = PR_Poll(&pd, 1, timeout);
#endif
       if (n == -1) {
              fprintf(stderr,"Error - PR_Poll failed: (%d, %d)\n",
                                                               PR_GetError(), PR_GetOSError());
              failed_already=1;
              goto def_exit;
       }
       if (PR_GetConnectStatus(&pd) == PR_SUCCESS) {
              PRInt32 rv;
              fprintf(stderr,"PR_GetConnectStatus succeeded, expected to fail\n");
              failed_already = 1;
              goto def_exit;
       }
       rv = PR_GetError();
       DPRINTF(("Connection failed, successfully with PR_Error %d\n",rv));
def_exit:
       if (sockfd) {
              PR_Close(sockfd);
              sockfd = NULL;
       }
       if (conn_fd) {
              PR_Close(conn_fd);
              conn_fd = NULL;
       }
       if (failed_already)
              return 1;
       else
              return 0;

}

Here is the call graph for this function:

Here is the caller graph for this function:

static PRIntn connection_success_test ( ) [static]

Definition at line 292 of file nbconn.c.

{
       PRFileDesc *sockfd = NULL, *conn_fd = NULL;
       PRNetAddr netaddr;
       PRInt32 i, rv;
    PRPollDesc pd;
    PRSocketOptionData optData;
       PRThread *thr = NULL;
       Server_Param sp;
       char send_buf[DATA_BUF_SIZE], recv_buf[DATA_BUF_SIZE];
    PRIntn default_case, n, bytes_read, bytes_sent;
    PRIntn failed_already = 0;

       /*
        * Create a tcp socket
        */
       if ((sockfd = PR_NewTCPSocket()) == NULL) {
              fprintf(stderr,"Error - PR_NewTCPSocket failed\n");
              failed_already=1;
              goto def_exit;
       }
       memset(&netaddr, 0 , sizeof(netaddr));
       netaddr.inet.family = PR_AF_INET;
       netaddr.inet.port = PR_htons(TCP_SERVER_PORT);
       netaddr.inet.ip = PR_htonl(PR_INADDR_ANY);
       /*
        * try a few times to bind server's address, if addresses are in
        * use
        */
       i = 0;
       while (PR_Bind(sockfd, &netaddr) < 0) {
              if (PR_GetError() == PR_ADDRESS_IN_USE_ERROR) {
                     netaddr.inet.port += 2;
                     if (i++ < SERVER_MAX_BIND_COUNT)
                            continue;
              }
              fprintf(stderr,"ERROR - PR_Bind failed: (%d,%d)\n",
                                                               PR_GetError(), PR_GetOSError());
              failed_already=1;
              goto def_exit;
       }

       if (PR_Listen(sockfd, 32) < 0) {
              fprintf(stderr,"ERROR - PR_Listen failed: (%d,%d)\n",
                                                               PR_GetError(), PR_GetOSError());
              failed_already=1;
              goto def_exit;
       }

       if (PR_GetSockName(sockfd, &netaddr) < 0) {
              fprintf(stderr,"ERROR - PR_GetSockName failed: (%d,%d)\n",
                                                               PR_GetError(), PR_GetOSError());
              failed_already=1;
              goto def_exit;
       }
       if ((conn_fd = PR_NewTCPSocket()) == NULL) {
              fprintf(stderr,"Error - PR_NewTCPSocket failed\n");
              failed_already=1;
              goto def_exit;
       }
       optData.option = PR_SockOpt_Nonblocking;
       optData.value.non_blocking = PR_TRUE;
       PR_SetSocketOption(conn_fd, &optData);
       rv = PR_Connect(conn_fd, &netaddr, PR_INTERVAL_NO_TIMEOUT);
       if (rv == PR_FAILURE) {
              if (PR_GetError() == PR_IN_PROGRESS_ERROR) {
                     DPRINTF(("Connect in progress\n"));
              } else  {
                     fprintf(stderr,"Error - PR_Connect failed: (%d, %d)\n",
                                                               PR_GetError(), PR_GetOSError());
                     failed_already=1;
                     goto def_exit;
              }
       }
       /*
        * Now create a thread to accept a connection
        */
       sp.sp_fd = sockfd;
       thr = PR_CreateThread(PR_USER_THREAD, TCP_Server, (void *)&sp, 
                     PR_PRIORITY_NORMAL, PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
       if (thr == NULL) {
              fprintf(stderr,"Error - PR_CreateThread failed: (%d,%d)\n",
                                                               PR_GetError(), PR_GetOSError());
              failed_already=1;
              goto def_exit;
       }
       DPRINTF(("Created TCP_Server thread [0x%x]\n",thr));
       pd.fd = conn_fd;
       pd.in_flags = PR_POLL_WRITE | PR_POLL_EXCEPT;
#ifndef XP_MAC
       n = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT);
#else
       n = PR_Poll(&pd, 1, timeout);
#endif
       if (n == -1) {
              fprintf(stderr,"Error - PR_Poll failed: (%d, %d)\n",
                                                               PR_GetError(), PR_GetOSError());
              failed_already=1;
              goto def_exit;
       }
       if (PR_GetConnectStatus(&pd) == PR_SUCCESS) {
              PRInt32 rv;

              DPRINTF(("Connection successful\n"));

              /*
               * Write some data, read it back and check data integrity to
               * make sure the connection is good
               */
              pd.in_flags = PR_POLL_WRITE;
              bytes_sent = 0;
              memset(send_buf, 'a', DATA_BUF_SIZE);
              while (bytes_sent != DATA_BUF_SIZE) {
                     rv = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT);
                     if (rv < 0) {
                            fprintf(stderr,"Error - PR_Poll failed: (%d, %d)\n",
                                                        PR_GetError(), PR_GetOSError());
                            failed_already=1;
                            goto def_exit;
                     }
                     PR_ASSERT((rv == 1) && (pd.out_flags == PR_POLL_WRITE));
                     rv = PR_Write(conn_fd, send_buf + bytes_sent,
                                                                      DATA_BUF_SIZE - bytes_sent);
                     if (rv < 0) {
                            fprintf(stderr,"Error - PR_Write failed: (%d, %d)\n",
                                                        PR_GetError(), PR_GetOSError());
                            failed_already=1;
                            goto def_exit;
                     }
                     PR_ASSERT(rv > 0);
                     bytes_sent += rv;
              }
              DPRINTF(("Bytes written to server - %d\n",bytes_sent));
              PR_Shutdown(conn_fd, PR_SHUTDOWN_SEND);
              pd.in_flags = PR_POLL_READ;
              bytes_read = 0;
              memset(recv_buf, 0, DATA_BUF_SIZE);
              while (bytes_read != DATA_BUF_SIZE) {
                     rv = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT);
                     if (rv < 0) {
                            fprintf(stderr,"Error - PR_Poll failed: (%d, %d)\n",
                                                        PR_GetError(), PR_GetOSError());
                            failed_already=1;
                            goto def_exit;
                     }
                     PR_ASSERT((rv == 1) && (pd.out_flags == PR_POLL_READ));
                     rv = PR_Read(conn_fd, recv_buf + bytes_read ,
                                                                      DATA_BUF_SIZE - bytes_read);
                     if (rv < 0) {
                            fprintf(stderr,"Error - PR_Read failed: (%d, %d)\n",
                                                        PR_GetError(), PR_GetOSError());
                            failed_already=1;
                            goto def_exit;
                     }
                     PR_ASSERT(rv != 0);
                     bytes_read += rv;
              }
              DPRINTF(("Bytes read from server - %d\n",bytes_read));
              /*
               * verify the data read
               */
              if (memcmp(send_buf, recv_buf, DATA_BUF_SIZE) != 0) {
                     fprintf(stderr,"ERROR - data corruption\n");
                     failed_already=1;
                     goto def_exit;
              }
              DPRINTF(("Data integrity verified\n"));
       } else {
              fprintf(stderr,"PR_GetConnectStatus: connect failed: (%ld, %ld)\n",
                            PR_GetError(), PR_GetOSError());
              failed_already = 1;
              goto def_exit;
       }
def_exit:
       if (thr) {
              PR_JoinThread(thr);
              thr = NULL;
       }
       if (sockfd) {
              PR_Close(sockfd);
              sockfd = NULL;
       }
       if (conn_fd) {
              PR_Close(conn_fd);
              conn_fd = NULL;
       }
       if (failed_already)
              return 1;
       else
              return 0;

}

Here is the call graph for this function:

Here is the caller graph for this function:

int main ( int  argc,
char **  argv 
)

The Xalan testcases app.

Definition at line 86 of file nbconn.c.

{
    PRHostEnt he;
    char buf[1024];
    PRNetAddr addr;
    PRPollDesc pd;
    PRStatus rv;
    PRSocketOptionData optData;
       const char *hostname = NULL;
    PRIntn default_case, n, bytes_read, bytes_sent;
       PRInt32 failed_already = 0;
#ifdef XP_MAC
       int index;
       PRIntervalTime timeout;
#endif

    /*
     * -d           debug mode
     */

    PLOptStatus os;
    PLOptState *opt = PL_CreateOptState(argc, argv, "d");
    while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
    {
        if (PL_OPT_BAD == os) continue;
        switch (opt->option)
        {
        case 0:  /* debug mode */
            hostname = opt->value;
            break;
        case 'd':  /* debug mode */
            _debug_on = 1;
            break;
        default:
            break;
        }
    }
    PL_DestroyOptState(opt);

#ifdef XP_MAC
       SetupMacPrintfLog("nbconn.log");
       for (index=0; index<4; index++) {
       argv[1] = hosts[index];
       timeout = PR_INTERVAL_NO_TIMEOUT;
       if (index == 3)
              timeout = PR_SecondsToInterval(10UL);
#endif


    PR_STDIO_INIT();
#ifndef XP_MAC
    if (hostname)
              default_case = 0;
       else
              default_case = 1;
#endif

       if (default_case) {

              /*
               * In the default case the following tests are executed:
               *     1. successful connection: a server thread accepts a connection
               *        from the main thread
               *     2. unsuccessful connection: the main thread tries to connect to a
               *        non-existent port and expects to get an error
               */
              rv = connection_success_test();
              if (rv == 0)
                     rv = connection_failure_test();
              return rv;
       } else {
       PRFileDesc *sock;

              if (PR_GetHostByName(argv[1], buf, sizeof(buf), &he) == PR_FAILURE) {
                     printf( "Unknown host: %s\n", argv[1]);
                     exit(1);
              } else {
                     printf( "host: %s\n", buf);
              }
              PR_EnumerateHostEnt(0, &he, 80, &addr);

              sock = PR_NewTCPSocket();
              optData.option = PR_SockOpt_Nonblocking;
              optData.value.non_blocking = PR_TRUE;
              PR_SetSocketOption(sock, &optData);
              rv = PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT);
              if (rv == PR_FAILURE && PR_GetError() == PR_IN_PROGRESS_ERROR) {
                     printf( "Connect in progress\n");
              }

              pd.fd = sock;
              pd.in_flags = PR_POLL_WRITE | PR_POLL_EXCEPT;
#ifndef XP_MAC
              n = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT);
#else
              n = PR_Poll(&pd, 1, timeout);
#endif
              if (n == -1) {
                     printf( "PR_Poll failed\n");
                     exit(1);
              }
              printf( "PR_Poll returns %d\n", n);
              if (pd.out_flags & PR_POLL_READ) {
                     printf( "PR_POLL_READ\n");
              }
              if (pd.out_flags & PR_POLL_WRITE) {
                     printf( "PR_POLL_WRITE\n");
              }
              if (pd.out_flags & PR_POLL_EXCEPT) {
                     printf( "PR_POLL_EXCEPT\n");
              }
              if (pd.out_flags & PR_POLL_ERR) {
                     printf( "PR_POLL_ERR\n");
              }
              if (pd.out_flags & PR_POLL_NVAL) {
                     printf( "PR_POLL_NVAL\n");
              }

              if (PR_GetConnectStatus(&pd) == PR_SUCCESS) {
                     printf("PR_GetConnectStatus: connect succeeded\n");
                     /* Mac and Win16 have trouble printing to the console. */
#if !defined(XP_MAC) && !defined(WIN16)
                     PR_Write(sock, "GET /\r\n\r\n", 9);
                     PR_Shutdown(sock, PR_SHUTDOWN_SEND);
                     pd.in_flags = PR_POLL_READ;
                     while (1) {
                            n = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT);
                            printf( "poll returns %d\n", n);
                            n = PR_Read(sock, buf, sizeof(buf));
                            printf( "read returns %d\n", n);
                            if (n <= 0) {
                                   break;
                            }
                            PR_Write(PR_STDOUT, buf, n);
                     }
#endif
              } else {
                     if (PR_GetError() == PR_IN_PROGRESS_ERROR) {
                            printf( "PR_GetConnectStatus: connect still in progress\n");
                            exit(1);
                     }
                     printf( "PR_GetConnectStatus: connect failed: (%ld, %ld)\n",
                                   PR_GetError(), PR_GetOSError());
              }
              PR_Close(sock);
#ifdef XP_MAC
              } /* end of for loop */
#endif
       printf( "PASS\n");
       return 0;

       }
}

Here is the call graph for this function:

static void PR_CALLBACK TCP_Server ( void arg) [static]

Definition at line 247 of file nbconn.c.

{
    Server_Param *sp = (Server_Param *) arg;
    PRFileDesc *sockfd, *newsockfd;
       char data_buf[DATA_BUF_SIZE];
    PRIntn rv, bytes_read;

       sockfd = sp->sp_fd;
       if ((newsockfd = PR_Accept(sockfd, NULL,
              PR_INTERVAL_NO_TIMEOUT)) == NULL) {
              fprintf(stderr,"ERROR - PR_Accept failed: (%d,%d)\n",
                                                                      PR_GetError(), PR_GetOSError());
              return;
       }
       bytes_read = 0;
       while (bytes_read != DATA_BUF_SIZE) {
              rv = PR_Read(newsockfd, data_buf + bytes_read ,
                                                               DATA_BUF_SIZE - bytes_read);
              if (rv < 0) {
                     fprintf(stderr,"Error - PR_Read failed: (%d, %d)\n",
                                                 PR_GetError(), PR_GetOSError());
                     PR_Close(newsockfd);
                     return;
              }
              PR_ASSERT(rv != 0);
              bytes_read += rv;
       }
       DPRINTF(("Bytes read from client - %d\n",bytes_read));
       rv = PR_Write(newsockfd, data_buf,DATA_BUF_SIZE);
       if (rv < 0) {
              fprintf(stderr,"Error - PR_Write failed: (%d, %d)\n",
                                          PR_GetError(), PR_GetOSError());
              PR_Close(newsockfd);
              return;
       }
       PR_ASSERT(rv == DATA_BUF_SIZE);
       DPRINTF(("Bytes written to client - %d\n",rv));
       PR_Close(newsockfd);
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 80 of file nbconn.c.