Back to index

lightning-sunbird  0.9+nobinonly
acceptread.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 #include <prio.h>
00039 #include <prprf.h>
00040 #include <prinit.h>
00041 #include <prnetdb.h>
00042 #include <prinrval.h>
00043 #include <prthread.h>
00044 
00045 #include <plerror.h>
00046 
00047 #include <stdlib.h>
00048 
00049 #define DEFAULT_PORT 12273
00050 #define GET "GET / HTTP/1.0\n\n"
00051 static PRFileDesc *std_out, *err_out;
00052 static PRIntervalTime write_dally, accept_timeout;
00053 
00054 static PRStatus PrintAddress(const PRNetAddr* address)
00055 {
00056     char buffer[100];
00057     PRStatus rv = PR_NetAddrToString(address, buffer, sizeof(buffer));
00058     if (PR_FAILURE == rv) PL_FPrintError(err_out, "PR_NetAddrToString");
00059     else PR_fprintf(
00060         std_out, "Accepted connection from (0x%p)%s:%d\n",
00061         address, buffer, address->inet.port);
00062     return rv;
00063 }  /* PrintAddress */
00064 
00065 static void ConnectingThread(void *arg)
00066 {
00067     PRInt32 nbytes;
00068     char buf[1024];
00069     PRFileDesc *sock;
00070     PRNetAddr peer_addr, *addr;
00071 
00072     addr = (PRNetAddr*)arg;
00073 
00074     sock = PR_NewTCPSocket();
00075     if (sock == NULL)
00076     {
00077         PL_FPrintError(err_out, "PR_NewTCPSocket (client) failed");
00078         PR_ProcessExit(1);
00079     }
00080 
00081     if (PR_Connect(sock, addr, PR_INTERVAL_NO_TIMEOUT) == PR_FAILURE)
00082     {
00083         PL_FPrintError(err_out, "PR_Connect (client) failed");
00084         PR_ProcessExit(1);
00085     }
00086     if (PR_GetPeerName(sock, &peer_addr) == PR_FAILURE)
00087     {
00088         PL_FPrintError(err_out, "PR_GetPeerName (client) failed");
00089         PR_ProcessExit(1);
00090     }
00091 
00092     /*
00093     ** Then wait between the connection coming up and sending the expected
00094     ** data. At some point in time, the server should fail due to a timeou
00095     ** on the AcceptRead() operation, which according to the document is
00096     ** only due to the read() portion.
00097     */
00098     PR_Sleep(write_dally);
00099 
00100     nbytes = PR_Send(sock, GET, sizeof(GET), 0, PR_INTERVAL_NO_TIMEOUT);
00101     if (nbytes == -1) PL_FPrintError(err_out, "PR_Send (client) failed");
00102 
00103     nbytes = PR_Recv(sock, buf, sizeof(buf), 0, PR_INTERVAL_NO_TIMEOUT);
00104     if (nbytes == -1) PL_FPrintError(err_out, "PR_Recv (client) failed");
00105     else
00106     {
00107         PR_fprintf(std_out, "PR_Recv (client) succeeded: %d bytes\n", nbytes);
00108         buf[sizeof(buf) - 1] = '\0';
00109         PR_fprintf(std_out, "%s\n", buf);
00110     }
00111 
00112     if (PR_FAILURE == PR_Shutdown(sock, PR_SHUTDOWN_BOTH))
00113         PL_FPrintError(err_out, "PR_Shutdown (client) failed");
00114 
00115     if (PR_FAILURE == PR_Close(sock))
00116         PL_FPrintError(err_out, "PR_Close (client) failed");
00117 
00118     return;
00119 }  /* ConnectingThread */
00120 
00121 #define BUF_SIZE 117
00122 static void AcceptingThread(void *arg)
00123 {
00124     PRStatus rv;
00125     PRInt32 bytes;
00126     PRSize buf_size = BUF_SIZE;
00127     PRUint8 buf[BUF_SIZE + (2 * sizeof(PRNetAddr)) + 32];
00128     PRNetAddr *accept_addr, *listen_addr = (PRNetAddr*)arg;
00129     PRFileDesc *accept_sock, *listen_sock = PR_NewTCPSocket();
00130     PRSocketOptionData sock_opt;
00131 
00132     if (NULL == listen_sock)
00133     {
00134         PL_FPrintError(err_out, "PR_NewTCPSocket (server) failed");
00135         PR_ProcessExit(1);        
00136     }
00137     sock_opt.option = PR_SockOpt_Reuseaddr;
00138     sock_opt.value.reuse_addr = PR_TRUE;
00139     rv = PR_SetSocketOption(listen_sock, &sock_opt);
00140     if (PR_FAILURE == rv)
00141     {
00142         PL_FPrintError(err_out, "PR_SetSocketOption (server) failed");
00143         PR_ProcessExit(1);        
00144     }
00145     rv = PR_Bind(listen_sock, listen_addr);
00146     if (PR_FAILURE == rv)
00147     {
00148         PL_FPrintError(err_out, "PR_Bind (server) failed");
00149         PR_ProcessExit(1);        
00150     }
00151     rv = PR_Listen(listen_sock, 10);
00152     if (PR_FAILURE == rv)
00153     {
00154         PL_FPrintError(err_out, "PR_Listen (server) failed");
00155         PR_ProcessExit(1);        
00156     }
00157     bytes = PR_AcceptRead(
00158         listen_sock, &accept_sock, &accept_addr, buf, buf_size, accept_timeout);
00159 
00160     if (-1 == bytes) PL_FPrintError(err_out, "PR_AcceptRead (server) failed");
00161     else
00162     {
00163         PrintAddress(accept_addr);
00164         PR_fprintf(
00165             std_out, "(Server) read [0x%p..0x%p) %s\n",
00166             buf, &buf[BUF_SIZE], buf);
00167         bytes = PR_Write(accept_sock, buf, bytes);
00168         rv = PR_Shutdown(accept_sock, PR_SHUTDOWN_BOTH);
00169         if (PR_FAILURE == rv)
00170             PL_FPrintError(err_out, "PR_Shutdown (server) failed");
00171     }
00172 
00173     if (-1 != bytes)
00174     {
00175         rv = PR_Close(accept_sock);
00176         if (PR_FAILURE == rv)
00177             PL_FPrintError(err_out, "PR_Close (server) failed");
00178     }
00179 
00180     rv = PR_Close(listen_sock);
00181     if (PR_FAILURE == rv)
00182         PL_FPrintError(err_out, "PR_Close (server) failed");
00183 }  /* AcceptingThread */
00184 
00185 PRIntn main(PRIntn argc, char **argv)
00186 {
00187     PRHostEnt he;
00188     PRStatus status;
00189     PRIntn next_index;
00190     PRUint16 port_number;
00191     char netdb_buf[PR_NETDB_BUF_SIZE];
00192     PRNetAddr client_addr, server_addr;
00193     PRThread *client_thread, *server_thread;
00194     PRIntervalTime delta = PR_MillisecondsToInterval(500);
00195 
00196     err_out = PR_STDERR;
00197     std_out = PR_STDOUT;
00198     accept_timeout = PR_SecondsToInterval(2);
00199 
00200     if (argc != 2 && argc != 3) port_number = DEFAULT_PORT;
00201     else port_number = (PRUint16)atoi(argv[(argc == 2) ? 1 : 2]);
00202 
00203     status = PR_InitializeNetAddr(PR_IpAddrAny, port_number, &server_addr);
00204     if (PR_SUCCESS != status)
00205     {
00206         PL_FPrintError(err_out, "PR_InitializeNetAddr failed");
00207         PR_ProcessExit(1);
00208     }
00209     if (argc < 3)
00210     {
00211         status = PR_InitializeNetAddr(
00212             PR_IpAddrLoopback, port_number, &client_addr);
00213         if (PR_SUCCESS != status)
00214         {
00215             PL_FPrintError(err_out, "PR_InitializeNetAddr failed");
00216             PR_ProcessExit(1);
00217         }
00218     }
00219     else
00220     {
00221         status = PR_GetHostByName(
00222             argv[1], netdb_buf, sizeof(netdb_buf), &he);
00223         if (status == PR_FAILURE)
00224         {
00225             PL_FPrintError(err_out, "PR_GetHostByName failed");
00226             PR_ProcessExit(1);
00227         }
00228         next_index = PR_EnumerateHostEnt(0, &he, port_number, &client_addr);
00229         if (next_index == -1)
00230         {
00231             PL_FPrintError(err_out, "PR_EnumerateHostEnt failed");
00232             PR_ProcessExit(1);
00233         }
00234     }
00235 
00236     for (
00237         write_dally = 0;
00238         write_dally < accept_timeout + (2 * delta);
00239         write_dally += delta)
00240     {
00241         PR_fprintf(
00242             std_out, "Testing w/ write_dally = %d msec\n",
00243             PR_IntervalToMilliseconds(write_dally));
00244         server_thread = PR_CreateThread(
00245             PR_USER_THREAD, AcceptingThread, &server_addr,
00246             PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
00247         if (server_thread == NULL)
00248         {
00249             PL_FPrintError(err_out, "PR_CreateThread (server) failed");
00250             PR_ProcessExit(1);
00251         }
00252 
00253         PR_Sleep(delta);  /* let the server pot thicken */
00254 
00255         client_thread = PR_CreateThread(
00256             PR_USER_THREAD, ConnectingThread, &client_addr,
00257             PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
00258         if (client_thread == NULL)
00259         {
00260             PL_FPrintError(err_out, "PR_CreateThread (client) failed");
00261             PR_ProcessExit(1);
00262         }
00263 
00264         if (PR_JoinThread(client_thread) == PR_FAILURE)
00265             PL_FPrintError(err_out, "PR_JoinThread (client) failed");
00266 
00267         if (PR_JoinThread(server_thread) == PR_FAILURE)
00268             PL_FPrintError(err_out, "PR_JoinThread (server) failed");
00269     }
00270 
00271     return 0;
00272 }