Back to index

lightning-sunbird  0.9+nobinonly
writev.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 "nspr.h"
00039 
00040 #include "plgetopt.h"
00041 
00042 #include <stdlib.h>
00043 #include <string.h>
00044 
00045 #ifdef XP_MAC
00046 #include "prlog.h"
00047 #define printf PR_LogPrint
00048 #endif
00049 
00050 
00051 #ifndef IOV_MAX
00052 #define IOV_MAX 16
00053 #endif
00054 
00055 #define BASE_PORT 9867
00056 
00057 int PR_CALLBACK Writev(int argc, char **argv)
00058 {
00059 
00060     PRStatus rv;
00061     PRNetAddr serverAddr;
00062     PRFileDesc *clientSock, *debug = NULL;
00063 
00064     char *buffer = NULL;
00065     PRIOVec *iov = NULL;
00066     PRBool passed = PR_TRUE;
00067     PRIntervalTime timein, elapsed, timeout;
00068     PRIntervalTime tmo_min = 0x7fffffff, tmo_max = 0, tmo_elapsed = 0;
00069     PRInt32 tmo_counted = 0, iov_index, loop, bytes, number_fragments;
00070     PRInt32 message_length = 100, fragment_length = 100, messages = 100;
00071     struct Descriptor { PRInt32 length; PRUint32 checksum; } descriptor;
00072 
00073     /*
00074      * USAGE
00075      * -h       dns name of host serving the connection (default = self)
00076      * -m       number of messages to send              (default = 100)
00077      * -s       size of each message                    (default = 100)
00078      * -f       size of each message fragment           (default = 100)
00079      */
00080 
00081        PLOptStatus os;
00082        PLOptState *opt = PL_CreateOptState(argc, argv, "dh:m:s:f:");
00083 
00084     PR_STDIO_INIT();
00085     rv = PR_InitializeNetAddr(PR_IpAddrLoopback, BASE_PORT, &serverAddr);
00086     PR_ASSERT(PR_SUCCESS == rv);
00087 
00088        while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
00089     {
00090         if (PL_OPT_BAD == os) continue;
00091         switch (opt->option)
00092         {
00093         case 'h':  /* the remote host */
00094             {
00095                 PRIntn es = 0;
00096                 PRHostEnt host;
00097                 char buffer[1024];
00098                 (void)PR_GetHostByName(opt->value, buffer, sizeof(buffer), &host);
00099                 es = PR_EnumerateHostEnt(es, &host, BASE_PORT, &serverAddr);
00100                 PR_ASSERT(es > 0);
00101             }
00102             break;
00103         case 'd':  /* debug mode */
00104             debug = PR_GetSpecialFD(PR_StandardError);
00105             break;
00106         case 'm':  /* number of messages to send */
00107             messages = atoi(opt->value);
00108             break;
00109         case 's':  /* total size of each message */
00110             message_length = atoi(opt->value);
00111             break;
00112         case 'f':  /* size of each message fragment */
00113             fragment_length = atoi(opt->value);
00114             break;
00115         default:
00116             break;
00117         }
00118     }
00119        PL_DestroyOptState(opt);
00120 
00121     buffer = (char*)malloc(message_length);
00122 
00123     number_fragments = (message_length + fragment_length - 1) / fragment_length + 1;
00124     while (IOV_MAX < number_fragments)
00125     {
00126         fragment_length = message_length / (IOV_MAX - 2);
00127         number_fragments = (message_length + fragment_length - 1) /
00128             fragment_length + 1;
00129         if (NULL != debug) PR_fprintf(debug, 
00130             "Too many fragments - reset fragment length to %ld\n", fragment_length);
00131     }
00132     iov = (PRIOVec*)malloc(number_fragments * sizeof(PRIOVec));
00133 
00134     iov[0].iov_base = (char*)&descriptor;
00135     iov[0].iov_len = sizeof(descriptor);
00136     for (iov_index = 1; iov_index < number_fragments; ++iov_index)
00137     {
00138         iov[iov_index].iov_base = buffer + (iov_index - 1) * fragment_length;
00139         iov[iov_index].iov_len = fragment_length;
00140     }
00141 
00142     for (bytes = 0; bytes < message_length; ++bytes)
00143         buffer[bytes] = (char)bytes;
00144 
00145     timeout = PR_SecondsToInterval(1);
00146 
00147     for (loop = 0; loop < messages; ++loop)
00148     {
00149         if (NULL != debug)
00150             PR_fprintf(debug, "[%d]socket ... ", loop);
00151         clientSock = PR_NewTCPSocket();
00152         if (clientSock)
00153         {
00154             timein = PR_IntervalNow();
00155             if (NULL != debug)
00156                 PR_fprintf(debug, "connecting ... ");
00157             rv = PR_Connect(clientSock, &serverAddr, timeout);
00158             if (PR_SUCCESS == rv)
00159             {
00160                 descriptor.checksum = 0;
00161                 descriptor.length = (loop < (messages - 1)) ? message_length : 0;
00162                 if (0 == descriptor.length) number_fragments = 1;
00163                 else
00164                     for (iov_index = 0; iov_index < descriptor.length; ++iov_index)
00165                     {
00166                         PRUint32 overflow = descriptor.checksum & 0x80000000;
00167                         descriptor.checksum = (descriptor.checksum << 1);
00168                         if (0x00000000 != overflow) descriptor.checksum += 1;
00169                         descriptor.checksum += buffer[iov_index];
00170                     }
00171                 if (NULL != debug) PR_fprintf(
00172                     debug, "sending %d bytes ... ", descriptor.length);
00173 
00174                 /* then, at the last moment ... */
00175                 descriptor.length = PR_ntohl(descriptor.length);
00176                 descriptor.checksum = PR_ntohl(descriptor.checksum);
00177 
00178                 bytes = PR_Writev(clientSock, iov, number_fragments, timeout);
00179                 if (NULL != debug)
00180                     PR_fprintf(debug, "closing ... ");
00181                 rv = PR_Shutdown(clientSock, PR_SHUTDOWN_BOTH);
00182                 rv = PR_Close(clientSock);
00183                 if (NULL != debug) PR_fprintf(
00184                     debug, "%s\n", ((PR_SUCCESS == rv) ? "good" : "bad"));
00185                 elapsed = PR_IntervalNow() - timein;
00186                 if (elapsed < tmo_min) tmo_min = elapsed;
00187                 else if (elapsed > tmo_max) tmo_max = elapsed;
00188                 tmo_elapsed += elapsed;
00189                 tmo_counted += 1;
00190             }
00191             else
00192             {
00193                 if (NULL != debug) PR_fprintf(
00194                     debug, "failed - retrying (%d, %d)\n",
00195                     PR_GetError(), PR_GetOSError());
00196                 PR_Close(clientSock);
00197             }
00198         }
00199         else if (NULL != debug)
00200         {
00201             PR_fprintf(debug, "unable to create client socket\n");
00202             passed = PR_FALSE;
00203         }
00204     }
00205     if (NULL != debug) {
00206         if (0 == tmo_counted) {
00207             PR_fprintf(debug, "No connection made\n");
00208         } else {
00209         PR_fprintf(
00210             debug, "\nTimings: %d [%d] %d (microseconds)\n",
00211             PR_IntervalToMicroseconds(tmo_min),
00212             PR_IntervalToMicroseconds(tmo_elapsed / tmo_counted),
00213             PR_IntervalToMicroseconds(tmo_max));
00214        }
00215     }
00216 
00217     PR_DELETE(buffer);
00218     PR_DELETE(iov);
00219 
00220     PR_fprintf(
00221         PR_GetSpecialFD(PR_StandardError),
00222         "%s\n", (passed) ? "PASSED" : "FAILED");
00223     return (passed) ? 0 : 1;
00224 }
00225 
00226 int main(int argc, char **argv)
00227 {
00228     return (PR_VersionCheck(PR_VERSION)) ?
00229         PR_Initialize(Writev, argc, argv, 4) : -1;
00230 }  /* main */
00231 
00232 /* writev.c */
00233 
00234