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