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