1 /*
2 * iperf, Copyright (c) 2014-2018, The Regents of the University of
3 * California, through Lawrence Berkeley National Laboratory (subject
4 * to receipt of any required approvals from the U.S. Dept. of
5 * Energy). All rights reserved.
6 *
7 * If you have questions about your rights to use or distribute this
8 * software, please contact Berkeley Lab's Technology Transfer
9 * Department at TTD@lbl.gov.
10 *
11 * NOTICE. This software is owned by the U.S. Department of Energy.
12 * As such, the U.S. Government has been granted for itself and others
13 * acting on its behalf a paid-up, nonexclusive, irrevocable,
14 * worldwide license in the Software to reproduce, prepare derivative
15 * works, and perform publicly and display publicly. Beginning five
16 * (5) years after the date permission to assert copyright is obtained
17 * from the U.S. Department of Energy, and subject to any subsequent
18 * five (5) year renewals, the U.S. Government is granted for itself
19 * and others acting on its behalf a paid-up, nonexclusive,
20 * irrevocable, worldwide license in the Software to reproduce,
21 * prepare derivative works, distribute copies to the public, perform
22 * publicly and display publicly, and to permit others to do so.
23 *
24 * This code is distributed under a BSD style license, see the LICENSE
25 * file for complete information.
26 */
27 #include <stdio.h>
28 #include <errno.h>
29 #include <netdb.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <stdarg.h>
33 #include "iperf.h"
34 #include "iperf_api.h"
35
36 /* Do a printf to stderr. */
37 void
iperf_err(struct iperf_test * test,const char * format,...)38 iperf_err(struct iperf_test *test, const char *format, ...)
39 {
40 va_list argp;
41 char str[1000];
42
43 va_start(argp, format);
44 vsnprintf(str, sizeof(str), format, argp);
45 if (test != NULL && test->json_output && test->json_top != NULL)
46 cJSON_AddStringToObject(test->json_top, "error", str);
47 else
48 if (test && test->outfile && test->outfile != stdout) {
49 fprintf(test->outfile, "iperf3: %s\n", str);
50 }
51 else {
52 fprintf(stderr, "iperf3: %s\n", str);
53 }
54 va_end(argp);
55 }
56
57 /* Do a printf to stderr or log file as appropriate, then exit. */
58 void
iperf_errexit(struct iperf_test * test,const char * format,...)59 iperf_errexit(struct iperf_test *test, const char *format, ...)
60 {
61 va_list argp;
62 char str[1000];
63
64 va_start(argp, format);
65 vsnprintf(str, sizeof(str), format, argp);
66 if (test != NULL && test->json_output && test->json_top != NULL) {
67 cJSON_AddStringToObject(test->json_top, "error", str);
68 iperf_json_finish(test);
69 } else
70 if (test && test->outfile && test->outfile != stdout) {
71 fprintf(test->outfile, "iperf3: %s\n", str);
72 }
73 else {
74 fprintf(stderr, "iperf3: %s\n", str);
75 }
76 va_end(argp);
77 if (test)
78 iperf_delete_pidfile(test);
79 exit(1);
80 }
81
82 int i_errno;
83
84 char *
iperf_strerror(int int_errno)85 iperf_strerror(int int_errno)
86 {
87 static char errstr[256];
88 int len, perr, herr;
89 perr = herr = 0;
90
91 len = sizeof(errstr);
92 memset(errstr, 0, len);
93
94 switch (int_errno) {
95 case IENONE:
96 snprintf(errstr, len, "no error");
97 break;
98 case IESERVCLIENT:
99 snprintf(errstr, len, "cannot be both server and client");
100 break;
101 case IENOROLE:
102 snprintf(errstr, len, "must either be a client (-c) or server (-s)");
103 break;
104 case IESERVERONLY:
105 snprintf(errstr, len, "some option you are trying to set is server only");
106 break;
107 case IECLIENTONLY:
108 snprintf(errstr, len, "some option you are trying to set is client only");
109 break;
110 case IEDURATION:
111 snprintf(errstr, len, "test duration too long (maximum = %d seconds)", MAX_TIME);
112 break;
113 case IENUMSTREAMS:
114 snprintf(errstr, len, "number of parallel streams too large (maximum = %d)", MAX_STREAMS);
115 break;
116 case IEBLOCKSIZE:
117 snprintf(errstr, len, "block size too large (maximum = %d bytes)", MAX_BLOCKSIZE);
118 break;
119 case IEBUFSIZE:
120 snprintf(errstr, len, "socket buffer size too large (maximum = %d bytes)", MAX_TCP_BUFFER);
121 break;
122 case IEINTERVAL:
123 snprintf(errstr, len, "invalid report interval (min = %g, max = %g seconds)", MIN_INTERVAL, MAX_INTERVAL);
124 break;
125 case IEBIND: /* UNUSED */
126 snprintf(errstr, len, "--bind must be specified to use --cport");
127 break;
128 case IEUDPBLOCKSIZE:
129 snprintf(errstr, len, "block size invalid (minimum = %d bytes, maximum = %d bytes)", MIN_UDP_BLOCKSIZE, MAX_UDP_BLOCKSIZE);
130 break;
131 case IEBADTOS:
132 snprintf(errstr, len, "bad TOS value (must be between 0 and 255 inclusive)");
133 break;
134 case IESETCLIENTAUTH:
135 snprintf(errstr, len, "you must specify username (max 20 chars), password (max 20 chars) and a path to a valid public rsa client to be used");
136 break;
137 case IESETSERVERAUTH:
138 snprintf(errstr, len, "you must specify path to a valid private rsa server to be used and a user credential file");
139 break;
140 case IEBADFORMAT:
141 snprintf(errstr, len, "bad format specifier (valid formats are in the set [kmgtKMGT])");
142 break;
143 case IEMSS:
144 snprintf(errstr, len, "TCP MSS too large (maximum = %d bytes)", MAX_MSS);
145 break;
146 case IENOSENDFILE:
147 snprintf(errstr, len, "this OS does not support sendfile");
148 break;
149 case IEOMIT:
150 snprintf(errstr, len, "bogus value for --omit");
151 break;
152 case IEUNIMP:
153 snprintf(errstr, len, "an option you are trying to set is not implemented yet");
154 break;
155 case IEFILE:
156 snprintf(errstr, len, "unable to open -F file");
157 perr = 1;
158 break;
159 case IEBURST:
160 snprintf(errstr, len, "invalid burst count (maximum = %d)", MAX_BURST);
161 break;
162 case IEENDCONDITIONS:
163 snprintf(errstr, len, "only one test end condition (-t, -n, -k) may be specified");
164 break;
165 case IELOGFILE:
166 snprintf(errstr, len, "unable to open log file");
167 perr = 1;
168 break;
169 case IENOSCTP:
170 snprintf(errstr, len, "no SCTP support available");
171 break;
172 case IENEWTEST:
173 snprintf(errstr, len, "unable to create a new test");
174 perr = 1;
175 break;
176 case IEINITTEST:
177 snprintf(errstr, len, "test initialization failed");
178 perr = 1;
179 break;
180 case IEAUTHTEST:
181 snprintf(errstr, len, "test authorization failed");
182 break;
183 case IELISTEN:
184 snprintf(errstr, len, "unable to start listener for connections");
185 perr = 1;
186 break;
187 case IECONNECT:
188 snprintf(errstr, len, "unable to connect to server");
189 perr = 1;
190 break;
191 case IEACCEPT:
192 snprintf(errstr, len, "unable to accept connection from client");
193 herr = 1;
194 perr = 1;
195 break;
196 case IESENDCOOKIE:
197 snprintf(errstr, len, "unable to send cookie to server");
198 perr = 1;
199 break;
200 case IERECVCOOKIE:
201 snprintf(errstr, len, "unable to receive cookie at server");
202 perr = 1;
203 break;
204 case IECTRLWRITE:
205 snprintf(errstr, len, "unable to write to the control socket");
206 perr = 1;
207 break;
208 case IECTRLREAD:
209 snprintf(errstr, len, "unable to read from the control socket");
210 perr = 1;
211 break;
212 case IECTRLCLOSE:
213 snprintf(errstr, len, "control socket has closed unexpectedly");
214 break;
215 case IEMESSAGE:
216 snprintf(errstr, len, "received an unknown control message");
217 break;
218 case IESENDMESSAGE:
219 snprintf(errstr, len, "unable to send control message");
220 perr = 1;
221 break;
222 case IERECVMESSAGE:
223 snprintf(errstr, len, "unable to receive control message");
224 perr = 1;
225 break;
226 case IESENDPARAMS:
227 snprintf(errstr, len, "unable to send parameters to server");
228 perr = 1;
229 break;
230 case IERECVPARAMS:
231 snprintf(errstr, len, "unable to receive parameters from client");
232 perr = 1;
233 break;
234 case IEPACKAGERESULTS:
235 snprintf(errstr, len, "unable to package results");
236 perr = 1;
237 break;
238 case IESENDRESULTS:
239 snprintf(errstr, len, "unable to send results");
240 perr = 1;
241 break;
242 case IERECVRESULTS:
243 snprintf(errstr, len, "unable to receive results");
244 perr = 1;
245 break;
246 case IESELECT:
247 snprintf(errstr, len, "select failed");
248 perr = 1;
249 break;
250 case IECLIENTTERM:
251 snprintf(errstr, len, "the client has terminated");
252 break;
253 case IESERVERTERM:
254 snprintf(errstr, len, "the server has terminated");
255 break;
256 case IEACCESSDENIED:
257 snprintf(errstr, len, "the server is busy running a test. try again later");
258 break;
259 case IESETNODELAY:
260 snprintf(errstr, len, "unable to set TCP/SCTP NODELAY");
261 perr = 1;
262 break;
263 case IESETMSS:
264 snprintf(errstr, len, "unable to set TCP/SCTP MSS");
265 perr = 1;
266 break;
267 case IESETBUF:
268 snprintf(errstr, len, "unable to set socket buffer size");
269 perr = 1;
270 break;
271 case IESETTOS:
272 snprintf(errstr, len, "unable to set IP TOS");
273 perr = 1;
274 break;
275 case IESETCOS:
276 snprintf(errstr, len, "unable to set IPv6 traffic class");
277 perr = 1;
278 break;
279 case IESETFLOW:
280 snprintf(errstr, len, "unable to set IPv6 flow label");
281 break;
282 case IEREUSEADDR:
283 snprintf(errstr, len, "unable to reuse address on socket");
284 perr = 1;
285 break;
286 case IENONBLOCKING:
287 snprintf(errstr, len, "unable to set socket to non-blocking");
288 perr = 1;
289 break;
290 case IESETWINDOWSIZE:
291 snprintf(errstr, len, "unable to set socket window size");
292 perr = 1;
293 break;
294 case IEPROTOCOL:
295 snprintf(errstr, len, "protocol does not exist");
296 break;
297 case IEAFFINITY:
298 snprintf(errstr, len, "unable to set CPU affinity");
299 perr = 1;
300 break;
301 case IEDAEMON:
302 snprintf(errstr, len, "unable to become a daemon");
303 perr = 1;
304 break;
305 case IECREATESTREAM:
306 snprintf(errstr, len, "unable to create a new stream");
307 herr = 1;
308 perr = 1;
309 break;
310 case IEINITSTREAM:
311 snprintf(errstr, len, "unable to initialize stream");
312 herr = 1;
313 perr = 1;
314 break;
315 case IESTREAMLISTEN:
316 snprintf(errstr, len, "unable to start stream listener");
317 perr = 1;
318 break;
319 case IESTREAMCONNECT:
320 snprintf(errstr, len, "unable to connect stream");
321 herr = 1;
322 perr = 1;
323 break;
324 case IESTREAMACCEPT:
325 snprintf(errstr, len, "unable to accept stream connection");
326 perr = 1;
327 break;
328 case IESTREAMWRITE:
329 snprintf(errstr, len, "unable to write to stream socket");
330 perr = 1;
331 break;
332 case IESTREAMREAD:
333 snprintf(errstr, len, "unable to read from stream socket");
334 perr = 1;
335 break;
336 case IESTREAMCLOSE:
337 snprintf(errstr, len, "stream socket has closed unexpectedly");
338 break;
339 case IESTREAMID:
340 snprintf(errstr, len, "stream has an invalid id");
341 break;
342 case IENEWTIMER:
343 snprintf(errstr, len, "unable to create new timer");
344 perr = 1;
345 break;
346 case IEUPDATETIMER:
347 snprintf(errstr, len, "unable to update timer");
348 perr = 1;
349 break;
350 case IESETCONGESTION:
351 snprintf(errstr, len, "unable to set TCP_CONGESTION: "
352 "Supplied congestion control algorithm not supported on this host");
353 break;
354 case IEPIDFILE:
355 snprintf(errstr, len, "unable to write PID file");
356 perr = 1;
357 break;
358 case IEV6ONLY:
359 snprintf(errstr, len, "Unable to set/reset IPV6_V6ONLY");
360 perr = 1;
361 break;
362 case IESETSCTPDISABLEFRAG:
363 snprintf(errstr, len, "unable to set SCTP_DISABLE_FRAGMENTS");
364 perr = 1;
365 break;
366 case IESETSCTPNSTREAM:
367 snprintf(errstr, len, "unable to set SCTP_INIT num of SCTP streams\n");
368 perr = 1;
369 break;
370 case IESETPACING:
371 snprintf(errstr, len, "unable to set socket pacing");
372 perr = 1;
373 break;
374 case IESETBUF2:
375 snprintf(errstr, len, "socket buffer size not set correctly");
376 break;
377 case IEREVERSEBIDIR:
378 snprintf(errstr, len, "cannot be both reverse and bidirectional");
379 break;
380
381 }
382
383 if (herr || perr)
384 strncat(errstr, ": ", len - strlen(errstr) - 1);
385 if (errno && perr)
386 strncat(errstr, strerror(errno), len - strlen(errstr) - 1);
387
388 return errstr;
389 }
390