Debian Codegraph @

nsprpub/pr/tests/writev.c

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 #include "nspr.h"
7
8 #include "plgetopt.h"
9
10 #include <stdlib.h>
11 #include <string.h>
12
13 #ifndef IOV_MAX
14 # define IOV_MAX 16
15 #endif
16
17 #ifdef DEBUG
18 # define PORT_INC_DO +100
19 #else
20 # define PORT_INC_DO
21 #endif
22 #ifdef IS_64
23 # define PORT_INC_3264 +200
24 #else
25 # define PORT_INC_3264
26 #endif
27
28 #define BASE_PORT 9867 PORT_INC_DO PORT_INC_3264
29
30 int PR_CALLBACK Writev(int argc, char** argv) {
31 PRStatus rv;
32 PRNetAddr serverAddr;
33 PRFileDesc *clientSock, *debug = NULL;
34
35 char* buffer = NULL;
36 PRIOVec* iov = NULL;
37 PRBool passed = PR_TRUE;
38 PRIntervalTime timein, elapsed, timeout;
39 PRIntervalTime tmo_min = 0x7fffffff, tmo_max = 0, tmo_elapsed = 0;
40 PRInt32 tmo_counted = 0, iov_index, loop, bytes, number_fragments;
41 PRInt32 message_length = 100, fragment_length = 100, messages = 100;
42 struct Descriptor {
43 PRInt32 length;
44 PRUint32 checksum;
45 } descriptor;
46
47 /*
48 * USAGE
49 * -h dns name of host serving the connection (default = self)
50 * -m number of messages to send (default = 100)
51 * -s size of each message (default = 100)
52 * -f size of each message fragment (default = 100)
53 */
54
55 PLOptStatus os;
56 PLOptState* opt = PL_CreateOptState(argc, argv, "dh:m:s:f:");
57
58 PR_STDIO_INIT();
59 rv = PR_InitializeNetAddr(PR_IpAddrLoopback, BASE_PORT, &serverAddr);
60 PR_ASSERT(PR_SUCCESS == rv);
61
62 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
63 if (PL_OPT_BAD == os) {
64 continue;
65 }
66 switch (opt->option) {
67 case 'h': /* the remote host */
68 {
69 PRIntn es = 0;
70 PRHostEnt host;
71 char buffer[1024];
72 (void)PR_GetHostByName(opt->value, buffer, sizeof(buffer), &host);
73 es = PR_EnumerateHostEnt(es, &host, BASE_PORT, &serverAddr);
74 PR_ASSERT(es > 0);
75 } break;
76 case 'd': /* debug mode */
77 debug = PR_GetSpecialFD(PR_StandardError);
78 break;
79 case 'm': /* number of messages to send */
80 messages = atoi(opt->value);
81 break;
82 case 's': /* total size of each message */
83 message_length = atoi(opt->value);
84 break;
85 case 'f': /* size of each message fragment */
86 fragment_length = atoi(opt->value);
87 break;
88 default:
89 break;
90 }
91 }
92 PL_DestroyOptState(opt);
93
94 buffer = (char*)malloc(message_length);
95
96 number_fragments =
97 (message_length + fragment_length - 1) / fragment_length + 1;
98 while (IOV_MAX < number_fragments) {
99 fragment_length = message_length / (IOV_MAX - 2);
100 number_fragments =
101 (message_length + fragment_length - 1) / fragment_length + 1;
102 if (NULL != debug)
103 PR_fprintf(debug, "Too many fragments - reset fragment length to %ld\n",
104 fragment_length);
105 }
106 iov = (PRIOVec*)malloc(number_fragments * sizeof(PRIOVec));
107
108 iov[0].iov_base = (char*)&descriptor;
109 iov[0].iov_len = sizeof(descriptor);
110 for (iov_index = 1; iov_index < number_fragments; ++iov_index) {
111 iov[iov_index].iov_base = buffer + (iov_index - 1) * fragment_length;
112 iov[iov_index].iov_len = fragment_length;
113 }
114
115 for (bytes = 0; bytes < message_length; ++bytes) {
116 buffer[bytes] = (char)bytes;
117 }
118
119 timeout = PR_SecondsToInterval(1);
120
121 for (loop = 0; loop < messages; ++loop) {
122 if (NULL != debug) {
123 PR_fprintf(debug, "[%d]socket ... ", loop);
124 }
125 clientSock = PR_NewTCPSocket();
126 if (clientSock) {
127 timein = PR_IntervalNow();
128 if (NULL != debug) {
129 PR_fprintf(debug, "connecting ... ");
130 }
131 rv = PR_Connect(clientSock, &serverAddr, timeout);
132 if (PR_SUCCESS == rv) {
133 descriptor.checksum = 0;
134 descriptor.length = (loop < (messages - 1)) ? message_length : 0;
135 if (0 == descriptor.length) {
136 number_fragments = 1;
137 } else
138 for (iov_index = 0; iov_index < descriptor.length; ++iov_index) {
139 PRUint32 overflow = descriptor.checksum & 0x80000000;
140 descriptor.checksum = (descriptor.checksum << 1);
141 if (0x00000000 != overflow) {
142 descriptor.checksum += 1;
143 }
144 descriptor.checksum += buffer[iov_index];
145 }
146 if (NULL != debug)
147 PR_fprintf(debug, "sending %d bytes ... ", descriptor.length);
148
149 /* then, at the last moment ... */
150 descriptor.length = PR_ntohl(descriptor.length);
151 descriptor.checksum = PR_ntohl(descriptor.checksum);
152
153 bytes = PR_Writev(clientSock, iov, number_fragments, timeout);
154 if (NULL != debug) {
155 PR_fprintf(debug, "closing ... ");
156 }
157 rv = PR_Shutdown(clientSock, PR_SHUTDOWN_BOTH);
158 rv = PR_Close(clientSock);
159 if (NULL != debug)
160 PR_fprintf(debug, "%s\n", ((PR_SUCCESS == rv) ? "good" : "bad"));
161 elapsed = PR_IntervalNow() - timein;
162 if (elapsed < tmo_min) {
163 tmo_min = elapsed;
164 } else if (elapsed > tmo_max) {
165 tmo_max = elapsed;
166 }
167 tmo_elapsed += elapsed;
168 tmo_counted += 1;
169 } else {
170 if (NULL != debug)
171 PR_fprintf(debug, "failed - retrying (%d, %d)\n", PR_GetError(),
172 PR_GetOSError());
173 PR_Close(clientSock);
174 }
175 } else if (NULL != debug) {
176 PR_fprintf(debug, "unable to create client socket\n");
177 passed = PR_FALSE;
178 }
179 }
180 if (NULL != debug) {
181 if (0 == tmo_counted) {
182 PR_fprintf(debug, "No connection made\n");
183 } else {
184 PR_fprintf(debug, "\nTimings: %d [%d] %d (microseconds)\n",
185 PR_IntervalToMicroseconds(tmo_min),
186 PR_IntervalToMicroseconds(tmo_elapsed / tmo_counted),
187 PR_IntervalToMicroseconds(tmo_max));
188 }
189 }
190
191 PR_DELETE(buffer);
192 PR_DELETE(iov);
193
194 PR_fprintf(PR_GetSpecialFD(PR_StandardError), "%s\n",
195 (passed) ? "PASSED" : "FAILED");
196 return (passed) ? 0 : 1;
197 }
198
199 int main(int argc, char** argv) {
200 return (PR_VersionCheck(PR_VERSION)) ? PR_Initialize(Writev, argc, argv, 4)
201 : -1;
202 } /* main */
203
204 /* writev.c */