xref: /iperf/src/iperf_error.c (revision c50b5ea8)
1 /*
2  * iperf, Copyright (c) 2014, 2015, 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 [email protected].
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
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) {
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
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) {
71 	    fprintf(test->outfile, "iperf3: %s\n", str);
72 	}
73 	else {
74 	    fprintf(stderr, "iperf3: %s\n", str);
75 	}
76     va_end(argp);
77     iperf_delete_pidfile(test);
78     exit(1);
79 }
80 
81 int i_errno;
82 
83 char *
84 iperf_strerror(int i_errno)
85 {
86     static char errstr[256];
87     int len, perr, herr;
88     perr = herr = 0;
89 
90     len = sizeof(errstr);
91     memset(errstr, 0, len);
92 
93     switch (i_errno) {
94         case IENONE:
95             snprintf(errstr, len, "no error");
96             break;
97         case IESERVCLIENT:
98             snprintf(errstr, len, "cannot be both server and client");
99             break;
100         case IENOROLE:
101             snprintf(errstr, len, "must either be a client (-c) or server (-s)");
102             break;
103         case IESERVERONLY:
104             snprintf(errstr, len, "some option you are trying to set is server only");
105             break;
106         case IECLIENTONLY:
107             snprintf(errstr, len, "some option you are trying to set is client only");
108             break;
109         case IEDURATION:
110             snprintf(errstr, len, "test duration too long (maximum = %d seconds)", MAX_TIME);
111             break;
112         case IENUMSTREAMS:
113             snprintf(errstr, len, "number of parallel streams too large (maximum = %d)", MAX_STREAMS);
114             break;
115         case IEBLOCKSIZE:
116             snprintf(errstr, len, "block size too large (maximum = %d bytes)", MAX_BLOCKSIZE);
117             break;
118         case IEBUFSIZE:
119             snprintf(errstr, len, "socket buffer size too large (maximum = %d bytes)", MAX_TCP_BUFFER);
120             break;
121         case IEINTERVAL:
122             snprintf(errstr, len, "invalid report interval (min = %g, max = %g seconds)", MIN_INTERVAL, MAX_INTERVAL);
123             break;
124         case IEBIND:
125             snprintf(errstr, len, "--bind must be specified to use --cport");
126             break;
127         case IEUDPBLOCKSIZE:
128             snprintf(errstr, len, "block size too large (maximum = %d bytes)", MAX_UDP_BLOCKSIZE);
129             break;
130 	case IEBADTOS:
131 	    snprintf(errstr, len, "bad TOS value (must be between 0 and 255 inclusive)");
132 	    break;
133         case IEMSS:
134             snprintf(errstr, len, "TCP MSS too large (maximum = %d bytes)", MAX_MSS);
135             break;
136         case IENOSENDFILE:
137             snprintf(errstr, len, "this OS does not support sendfile");
138             break;
139         case IEOMIT:
140             snprintf(errstr, len, "bogus value for --omit");
141             break;
142         case IEUNIMP:
143             snprintf(errstr, len, "an option you are trying to set is not implemented yet");
144             break;
145         case IEFILE:
146             snprintf(errstr, len, "unable to open -F file");
147             perr = 1;
148             break;
149         case IEBURST:
150             snprintf(errstr, len, "invalid burst count (maximum = %d)", MAX_BURST);
151             break;
152         case IEENDCONDITIONS:
153             snprintf(errstr, len, "only one test end condition (-t, -n, -k) may be specified");
154             break;
155 	case IELOGFILE:
156 	    snprintf(errstr, len, "unable to open log file");
157 	    perr = 1;
158 	    break;
159 	case IENOSCTP:
160 	    snprintf(errstr, len, "no SCTP support available");
161 	    break;
162         case IENEWTEST:
163             snprintf(errstr, len, "unable to create a new test");
164             perr = 1;
165             break;
166         case IEINITTEST:
167             snprintf(errstr, len, "test initialization failed");
168             perr = 1;
169             break;
170         case IELISTEN:
171             snprintf(errstr, len, "unable to start listener for connections");
172             perr = 1;
173             break;
174         case IECONNECT:
175             snprintf(errstr, len, "unable to connect to server");
176             perr = 1;
177             break;
178         case IEACCEPT:
179             snprintf(errstr, len, "unable to accept connection from client");
180             herr = 1;
181             perr = 1;
182             break;
183         case IESENDCOOKIE:
184             snprintf(errstr, len, "unable to send cookie to server");
185             perr = 1;
186             break;
187         case IERECVCOOKIE:
188             snprintf(errstr, len, "unable to receive cookie at server");
189             perr = 1;
190             break;
191         case IECTRLWRITE:
192             snprintf(errstr, len, "unable to write to the control socket");
193             perr = 1;
194             break;
195         case IECTRLREAD:
196             snprintf(errstr, len, "unable to read from the control socket");
197             perr = 1;
198             break;
199         case IECTRLCLOSE:
200             snprintf(errstr, len, "control socket has closed unexpectedly");
201             break;
202         case IEMESSAGE:
203             snprintf(errstr, len, "received an unknown control message");
204             break;
205         case IESENDMESSAGE:
206             snprintf(errstr, len, "unable to send control message");
207             perr = 1;
208             break;
209         case IERECVMESSAGE:
210             snprintf(errstr, len, "unable to receive control message");
211             perr = 1;
212             break;
213         case IESENDPARAMS:
214             snprintf(errstr, len, "unable to send parameters to server");
215             perr = 1;
216             break;
217         case IERECVPARAMS:
218             snprintf(errstr, len, "unable to receive parameters from client");
219             perr = 1;
220             break;
221         case IEPACKAGERESULTS:
222             snprintf(errstr, len, "unable to package results");
223             perr = 1;
224             break;
225         case IESENDRESULTS:
226             snprintf(errstr, len, "unable to send results");
227             perr = 1;
228             break;
229         case IERECVRESULTS:
230             snprintf(errstr, len, "unable to receive results");
231             perr = 1;
232             break;
233         case IESELECT:
234             snprintf(errstr, len, "select failed");
235             perr = 1;
236             break;
237         case IECLIENTTERM:
238             snprintf(errstr, len, "the client has terminated");
239             break;
240         case IESERVERTERM:
241             snprintf(errstr, len, "the server has terminated");
242             break;
243         case IEACCESSDENIED:
244             snprintf(errstr, len, "the server is busy running a test. try again later");
245             break;
246         case IESETNODELAY:
247             snprintf(errstr, len, "unable to set TCP/SCTP NODELAY");
248             perr = 1;
249             break;
250         case IESETMSS:
251             snprintf(errstr, len, "unable to set TCP/SCTP MSS");
252             perr = 1;
253             break;
254         case IESETBUF:
255             snprintf(errstr, len, "unable to set socket buffer size");
256             perr = 1;
257             break;
258         case IESETTOS:
259             snprintf(errstr, len, "unable to set IP TOS");
260             perr = 1;
261             break;
262         case IESETCOS:
263             snprintf(errstr, len, "unable to set IPv6 traffic class");
264             perr = 1;
265             break;
266         case IESETFLOW:
267             snprintf(errstr, len, "unable to set IPv6 flow label");
268             break;
269         case IEREUSEADDR:
270             snprintf(errstr, len, "unable to reuse address on socket");
271             perr = 1;
272             break;
273         case IENONBLOCKING:
274             snprintf(errstr, len, "unable to set socket to non-blocking");
275             perr = 1;
276             break;
277         case IESETWINDOWSIZE:
278             snprintf(errstr, len, "unable to set socket window size");
279             perr = 1;
280             break;
281         case IEPROTOCOL:
282             snprintf(errstr, len, "protocol does not exist");
283             break;
284         case IEAFFINITY:
285             snprintf(errstr, len, "unable to set CPU affinity");
286             perr = 1;
287             break;
288 	case IEDAEMON:
289 	    snprintf(errstr, len, "unable to become a daemon");
290 	    perr = 1;
291 	    break;
292         case IECREATESTREAM:
293             snprintf(errstr, len, "unable to create a new stream");
294             herr = 1;
295             perr = 1;
296             break;
297         case IEINITSTREAM:
298             snprintf(errstr, len, "unable to initialize stream");
299             herr = 1;
300             perr = 1;
301             break;
302         case IESTREAMLISTEN:
303             snprintf(errstr, len, "unable to start stream listener");
304             perr = 1;
305             break;
306         case IESTREAMCONNECT:
307             snprintf(errstr, len, "unable to connect stream");
308             herr = 1;
309             perr = 1;
310             break;
311         case IESTREAMACCEPT:
312             snprintf(errstr, len, "unable to accept stream connection");
313             perr = 1;
314             break;
315         case IESTREAMWRITE:
316             snprintf(errstr, len, "unable to write to stream socket");
317             perr = 1;
318             break;
319         case IESTREAMREAD:
320             snprintf(errstr, len, "unable to read from stream socket");
321             perr = 1;
322             break;
323         case IESTREAMCLOSE:
324             snprintf(errstr, len, "stream socket has closed unexpectedly");
325             break;
326         case IESTREAMID:
327             snprintf(errstr, len, "stream has an invalid id");
328             break;
329         case IENEWTIMER:
330             snprintf(errstr, len, "unable to create new timer");
331             perr = 1;
332             break;
333         case IEUPDATETIMER:
334             snprintf(errstr, len, "unable to update timer");
335             perr = 1;
336             break;
337         case IESETCONGESTION:
338             snprintf(errstr, len, "unable to set TCP_CONGESTION: "
339                                   "Supplied congestion control algorithm not supported on this host");
340             break;
341 	case IEPIDFILE:
342             snprintf(errstr, len, "unable to write PID file");
343             perr = 1;
344             break;
345 	case IEV6ONLY:
346 	    snprintf(errstr, len, "Unable to set/reset IPV6_V6ONLY");
347 	    perr = 1;
348 	    break;
349         case IESETSCTPDISABLEFRAG:
350             snprintf(errstr, len, "unable to set SCTP_DISABLE_FRAGMENTS");
351             perr = 1;
352             break;
353         case IESETSCTPNSTREAM:
354             snprintf(errstr, len, "unable to set SCTP_INIT num of SCTP streams\n");
355             perr = 1;
356             break;
357     }
358 
359     if (herr || perr)
360         strncat(errstr, ": ", len - strlen(errstr) - 1);
361     if (h_errno && herr) {
362         strncat(errstr, hstrerror(h_errno), len - strlen(errstr) - 1);
363     } else if (errno && perr) {
364         strncat(errstr, strerror(errno), len - strlen(errstr) - 1);
365     }
366 
367     return errstr;
368 }
369