Back to index

lightning-sunbird  0.9+nobinonly
acceptreademu.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039  * This test is the same as acceptread.c except that it uses the
00040  * emulated acceptread method instead of the regular acceptread.
00041  */
00042 
00043 #include <prio.h>
00044 #include <prprf.h>
00045 #include <prinit.h>
00046 #include <prnetdb.h>
00047 #include <prinrval.h>
00048 #include <prthread.h>
00049 #include <pprio.h>
00050 
00051 #include <plerror.h>
00052 
00053 #include <stdlib.h>
00054 
00055 #define DEFAULT_PORT 12273
00056 #define GET "GET / HTTP/1.0\n\n"
00057 static PRFileDesc *std_out, *err_out;
00058 static PRIntervalTime write_dally, accept_timeout;
00059 static PRDescIdentity emu_layer_ident;
00060 static PRIOMethods emu_layer_methods;
00061 
00062 /* the acceptread method in emu_layer_methods */
00063 static PRInt32 PR_CALLBACK emu_AcceptRead(PRFileDesc *sd, PRFileDesc **nd,
00064     PRNetAddr **raddr, void *buf, PRInt32 amount, PRIntervalTime timeout)
00065 {
00066     return PR_EmulateAcceptRead(sd, nd, raddr, buf, amount, timeout);
00067 }
00068 
00069 static PRStatus PrintAddress(const PRNetAddr* address)
00070 {
00071     char buffer[100];
00072     PRStatus rv = PR_NetAddrToString(address, buffer, sizeof(buffer));
00073     if (PR_FAILURE == rv) PL_FPrintError(err_out, "PR_NetAddrToString");
00074     else PR_fprintf(
00075         std_out, "Accepted connection from (0x%p)%s:%d\n",
00076         address, buffer, address->inet.port);
00077     return rv;
00078 }  /* PrintAddress */
00079 
00080 static void ConnectingThread(void *arg)
00081 {
00082     PRInt32 nbytes;
00083     char buf[1024];
00084     PRFileDesc *sock;
00085     PRNetAddr peer_addr, *addr;
00086 
00087     addr = (PRNetAddr*)arg;
00088 
00089     sock = PR_NewTCPSocket();
00090     if (sock == NULL)
00091     {
00092         PL_FPrintError(err_out, "PR_NewTCPSocket (client) failed");
00093         PR_ProcessExit(1);
00094     }
00095 
00096     if (PR_Connect(sock, addr, PR_INTERVAL_NO_TIMEOUT) == PR_FAILURE)
00097     {
00098         PL_FPrintError(err_out, "PR_Connect (client) failed");
00099         PR_ProcessExit(1);
00100     }
00101     if (PR_GetPeerName(sock, &peer_addr) == PR_FAILURE)
00102     {
00103         PL_FPrintError(err_out, "PR_GetPeerName (client) failed");
00104         PR_ProcessExit(1);
00105     }
00106 
00107     /*
00108     ** Then wait between the connection coming up and sending the expected
00109     ** data. At some point in time, the server should fail due to a timeou
00110     ** on the AcceptRead() operation, which according to the document is
00111     ** only due to the read() portion.
00112     */
00113     PR_Sleep(write_dally);
00114 
00115     nbytes = PR_Send(sock, GET, sizeof(GET), 0, PR_INTERVAL_NO_TIMEOUT);
00116     if (nbytes == -1) PL_FPrintError(err_out, "PR_Send (client) failed");
00117 
00118     nbytes = PR_Recv(sock, buf, sizeof(buf), 0, PR_INTERVAL_NO_TIMEOUT);
00119     if (nbytes == -1) PL_FPrintError(err_out, "PR_Recv (client) failed");
00120     else
00121     {
00122         PR_fprintf(std_out, "PR_Recv (client) succeeded: %d bytes\n", nbytes);
00123         buf[sizeof(buf) - 1] = '\0';
00124         PR_fprintf(std_out, "%s\n", buf);
00125     }
00126 
00127     if (PR_FAILURE == PR_Shutdown(sock, PR_SHUTDOWN_BOTH))
00128         PL_FPrintError(err_out, "PR_Shutdown (client) failed");
00129 
00130     if (PR_FAILURE == PR_Close(sock))
00131         PL_FPrintError(err_out, "PR_Close (client) failed");
00132 
00133     return;
00134 }  /* ConnectingThread */
00135 
00136 #define BUF_SIZE 117
00137 static void AcceptingThread(void *arg)
00138 {
00139     PRStatus rv;
00140     PRInt32 bytes;
00141     PRSize buf_size = BUF_SIZE;
00142     PRUint8 buf[BUF_SIZE + (2 * sizeof(PRNetAddr)) + 32];
00143     PRNetAddr *accept_addr, *listen_addr = (PRNetAddr*)arg;
00144     PRFileDesc *accept_sock, *listen_sock = PR_NewTCPSocket();
00145     PRFileDesc *layer;
00146     PRSocketOptionData sock_opt;
00147 
00148     if (NULL == listen_sock)
00149     {
00150         PL_FPrintError(err_out, "PR_NewTCPSocket (server) failed");
00151         PR_ProcessExit(1);        
00152     }
00153     layer = PR_CreateIOLayerStub(emu_layer_ident, &emu_layer_methods);
00154     if (NULL == layer)
00155     {
00156         PL_FPrintError(err_out, "PR_CreateIOLayerStub (server) failed");
00157         PR_ProcessExit(1);        
00158     }
00159     if (PR_PushIOLayer(listen_sock, PR_TOP_IO_LAYER, layer) == PR_FAILURE)
00160     {
00161         PL_FPrintError(err_out, "PR_PushIOLayer (server) failed");
00162         PR_ProcessExit(1);        
00163     }
00164     sock_opt.option = PR_SockOpt_Reuseaddr;
00165     sock_opt.value.reuse_addr = PR_TRUE;
00166     rv = PR_SetSocketOption(listen_sock, &sock_opt);
00167     if (PR_FAILURE == rv)
00168     {
00169         PL_FPrintError(err_out, "PR_SetSocketOption (server) failed");
00170         PR_ProcessExit(1);        
00171     }
00172     rv = PR_Bind(listen_sock, listen_addr);
00173     if (PR_FAILURE == rv)
00174     {
00175         PL_FPrintError(err_out, "PR_Bind (server) failed");
00176         PR_ProcessExit(1);        
00177     }
00178     rv = PR_Listen(listen_sock, 10);
00179     if (PR_FAILURE == rv)
00180     {
00181         PL_FPrintError(err_out, "PR_Listen (server) failed");
00182         PR_ProcessExit(1);        
00183     }
00184     bytes = PR_AcceptRead(
00185         listen_sock, &accept_sock, &accept_addr, buf, buf_size, accept_timeout);
00186 
00187     if (-1 == bytes) PL_FPrintError(err_out, "PR_AcceptRead (server) failed");
00188     else
00189     {
00190         PrintAddress(accept_addr);
00191         PR_fprintf(
00192             std_out, "(Server) read [0x%p..0x%p) %s\n",
00193             buf, &buf[BUF_SIZE], buf);
00194         bytes = PR_Write(accept_sock, buf, bytes);
00195         rv = PR_Shutdown(accept_sock, PR_SHUTDOWN_BOTH);
00196         if (PR_FAILURE == rv)
00197             PL_FPrintError(err_out, "PR_Shutdown (server) failed");
00198     }
00199 
00200     if (-1 != bytes)
00201     {
00202         rv = PR_Close(accept_sock);
00203         if (PR_FAILURE == rv)
00204             PL_FPrintError(err_out, "PR_Close (server) failed");
00205     }
00206 
00207     rv = PR_Close(listen_sock);
00208     if (PR_FAILURE == rv)
00209         PL_FPrintError(err_out, "PR_Close (server) failed");
00210 }  /* AcceptingThread */
00211 
00212 PRIntn main(PRIntn argc, char **argv)
00213 {
00214     PRHostEnt he;
00215     PRStatus status;
00216     PRIntn next_index;
00217     PRUint16 port_number;
00218     char netdb_buf[PR_NETDB_BUF_SIZE];
00219     PRNetAddr client_addr, server_addr;
00220     PRThread *client_thread, *server_thread;
00221     PRIntervalTime delta = PR_MillisecondsToInterval(500);
00222 
00223     err_out = PR_STDERR;
00224     std_out = PR_STDOUT;
00225     accept_timeout = PR_SecondsToInterval(2);
00226     emu_layer_ident = PR_GetUniqueIdentity("Emulated AcceptRead");
00227     emu_layer_methods = *PR_GetDefaultIOMethods();
00228     emu_layer_methods.acceptread = emu_AcceptRead;
00229 
00230     if (argc != 2 && argc != 3) port_number = DEFAULT_PORT;
00231     else port_number = (PRUint16)atoi(argv[(argc == 2) ? 1 : 2]);
00232 
00233     status = PR_InitializeNetAddr(PR_IpAddrAny, port_number, &server_addr);
00234     if (PR_SUCCESS != status)
00235     {
00236         PL_FPrintError(err_out, "PR_InitializeNetAddr failed");
00237         PR_ProcessExit(1);
00238     }
00239     if (argc < 3)
00240     {
00241         status = PR_InitializeNetAddr(
00242             PR_IpAddrLoopback, port_number, &client_addr);
00243         if (PR_SUCCESS != status)
00244         {
00245             PL_FPrintError(err_out, "PR_InitializeNetAddr failed");
00246             PR_ProcessExit(1);
00247         }
00248     }
00249     else
00250     {
00251         status = PR_GetHostByName(
00252             argv[1], netdb_buf, sizeof(netdb_buf), &he);
00253         if (status == PR_FAILURE)
00254         {
00255             PL_FPrintError(err_out, "PR_GetHostByName failed");
00256             PR_ProcessExit(1);
00257         }
00258         next_index = PR_EnumerateHostEnt(0, &he, port_number, &client_addr);
00259         if (next_index == -1)
00260         {
00261             PL_FPrintError(err_out, "PR_EnumerateHostEnt failed");
00262             PR_ProcessExit(1);
00263         }
00264     }
00265 
00266     for (
00267         write_dally = 0;
00268         write_dally < accept_timeout + (2 * delta);
00269         write_dally += delta)
00270     {
00271         PR_fprintf(
00272             std_out, "Testing w/ write_dally = %d msec\n",
00273             PR_IntervalToMilliseconds(write_dally));
00274         server_thread = PR_CreateThread(
00275             PR_USER_THREAD, AcceptingThread, &server_addr,
00276             PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
00277         if (server_thread == NULL)
00278         {
00279             PL_FPrintError(err_out, "PR_CreateThread (server) failed");
00280             PR_ProcessExit(1);
00281         }
00282 
00283         PR_Sleep(delta);  /* let the server pot thicken */
00284 
00285         client_thread = PR_CreateThread(
00286             PR_USER_THREAD, ConnectingThread, &client_addr,
00287             PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
00288         if (client_thread == NULL)
00289         {
00290             PL_FPrintError(err_out, "PR_CreateThread (client) failed");
00291             PR_ProcessExit(1);
00292         }
00293 
00294         if (PR_JoinThread(client_thread) == PR_FAILURE)
00295             PL_FPrintError(err_out, "PR_JoinThread (client) failed");
00296 
00297         if (PR_JoinThread(server_thread) == PR_FAILURE)
00298             PL_FPrintError(err_out, "PR_JoinThread (server) failed");
00299     }
00300 
00301     return 0;
00302 }