1
2 /***************************************************************************
3 * output.h -- Some simple error and regular message output functions. *
4 * *
5 ***********************IMPORTANT NMAP LICENSE TERMS************************
6 * *
7 * The Nmap Security Scanner is (C) 1996-2020 Insecure.Com LLC ("The Nmap *
8 * Project"). Nmap is also a registered trademark of the Nmap Project. *
9 * *
10 * This program is distributed under the terms of the Nmap Public Source *
11 * License (NPSL). The exact license text applying to a particular Nmap *
12 * release or source code control revision is contained in the LICENSE *
13 * file distributed with that version of Nmap or source code control *
14 * revision. More Nmap copyright/legal information is available from *
15 * https://nmap.org/book/man-legal.html, and further information on the *
16 * NPSL license itself can be found at https://nmap.org/npsl. This header *
17 * summarizes some key points from the Nmap license, but is no substitute *
18 * for the actual license text. *
19 * *
20 * Nmap is generally free for end users to download and use themselves, *
21 * including commercial use. It is available from https://nmap.org. *
22 * *
23 * The Nmap license generally prohibits companies from using and *
24 * redistributing Nmap in commercial products, but we sell a special Nmap *
25 * OEM Edition with a more permissive license and special features for *
26 * this purpose. See https://nmap.org/oem *
27 * *
28 * If you have received a written Nmap license agreement or contract *
29 * stating terms other than these (such as an Nmap OEM license), you may *
30 * choose to use and redistribute Nmap under those terms instead. *
31 * *
32 * The official Nmap Windows builds include the Npcap software *
33 * (https://npcap.org) for packet capture and transmission. It is under *
34 * separate license terms which forbid redistribution without special *
35 * permission. So the official Nmap Windows builds may not be *
36 * redistributed without special permission (such as an Nmap OEM *
37 * license). *
38 * *
39 * Source is provided to this software because we believe users have a *
40 * right to know exactly what a program is going to do before they run it. *
41 * This also allows you to audit the software for security holes. *
42 * *
43 * Source code also allows you to port Nmap to new platforms, fix bugs, *
44 * and add new features. You are highly encouraged to submit your *
45 * changes as a Github PR or by email to the dev@nmap.org mailing list *
46 * for possible incorporation into the main distribution. Unless you *
47 * specify otherwise, it is understood that you are offering us very *
48 * broad rights to use your submissions as described in the Nmap Public *
49 * Source License Contributor Agreement. This is important because we *
50 * fund the project by selling licenses with various terms, and also *
51 * because the inability to relicense code has caused devastating *
52 * problems for other Free Software projects (such as KDE and NASM). *
53 * *
54 * The free version of Nmap is distributed in the hope that it will be *
55 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of *
56 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Warranties, *
57 * indemnification and commercial support are all available through the *
58 * Npcap OEM program--see https://nmap.org/oem. *
59 * *
60 ***************************************************************************/
61
62 /* This file contains the functions that are used to print stuff to stout or
63 * stderr in Nping. All of them take a "level" and then a variable list of
64 * argument. The "level" parameter states which is the minimum level of
65 * verbosity that should NpingOps::vb contain to print the message to stdout.
66 * For a more detailed explanation, check documentation of verbosity and
67 * debugging levels in nping.h */
68
69 #include "NpingOps.h"
70 #include "output.h"
71
72 extern NpingOps o;
73
74 #ifdef WIN32
75 #include <windows.h>
76 #endif /* WIN32 */
77
78
79 /** Print fatal error messages to stderr and then exits.
80 * @warning This function does not return because it calls exit() */
nping_fatal(int level,const char * str,...)81 int nping_fatal(int level, const char *str, ...) {
82 va_list list;
83 char errstr[MAX_ERR_STR_LEN];
84 memset(errstr,0, MAX_ERR_STR_LEN);
85
86 fflush(stdout);
87 fflush(stderr);
88
89 int current_vb_level= o.getVerbosity();
90 int current_dbg_level= o.getDebugging();
91
92 /* If supplied level is more than current level, do nothing */
93 if( level>=QT_4 && level<=VB_4 && level>current_vb_level )
94 return OP_SUCCESS;
95 if( level>=DBG_0 && level<=DBG_9 && level>current_dbg_level )
96 return OP_SUCCESS;
97
98
99 if ( (level>=QT_3 && level<=VB_4) || (level>=DBG_1 && level<=DBG_9) ){
100 va_start(list, str);
101 vfprintf(stderr, str, list);
102 va_end(list);
103 fprintf(stderr,"\n"); /* Print to stderr */
104 }
105
106 exit(EXIT_FAILURE);
107 return OP_SUCCESS;
108 } /* End of nping_fatal() */
109
110
111 /** Prints recoverable error message to stderr and returns. This function
112 * inserts one \n newline automatically in every call. To avoid that
113 * behaviour it is possible to OR the supplied level with the constant
114 * NO_NEWLINE like this:
115 *
116 * nping_warning(QT_2|NO_NEWLINE, "I don't want newlines in this string");
117 * */
nping_warning(int level,const char * str,...)118 int nping_warning(int level, const char *str, ...) {
119 va_list list;
120 char errstr[MAX_ERR_STR_LEN];
121 bool skipnewline=false;
122 memset(errstr,0, MAX_ERR_STR_LEN);
123
124 fflush(stdout);
125 fflush(stderr);
126
127 int current_vb_level= o.getVerbosity();
128 int current_dbg_level= o.getDebugging();
129
130 /* Determine if caller requested that we don't print a newline character */
131 if ( level & NO_NEWLINE ){
132 level ^= NO_NEWLINE; /* Unset the flag restoring the original level */
133 skipnewline=true;
134 }
135
136 /* If supplied level is more than current level, do nothing */
137 if( level>=QT_4 && level<=VB_4 && level>current_vb_level )
138 return OP_SUCCESS;
139 if( level>=DBG_0 && level<=DBG_9 && level>current_dbg_level )
140 return OP_SUCCESS;
141
142 /* Otherwise, print the info to stderr*/
143 if ( (level>=QT_3 && level<=VB_4) || (level>=DBG_1 && level<=DBG_9) ){
144 va_start(list, str);
145 vfprintf(stderr, str, list); /* Print to stderr */
146 va_end(list);
147 if( !skipnewline )
148 fprintf(stderr,"\n");
149 }
150 return OP_SUCCESS;
151 } /* End of nping_warning() */
152
153
154 /** Print regular messages to stdout. This function inserts one \n newline
155 * automatically in every call. To avoid that behaviour it is possible to
156 * OR the supplied level with constant NO_NEWLINE like this:
157 *
158 * nping_print(VB_2|NO_NEWLINE, "I don't want newlines in this string");
159 * */
nping_print(int level,const char * str,...)160 int nping_print(int level, const char *str, ...){
161 va_list list;
162 char errstr[MAX_ERR_STR_LEN];
163 bool skipnewline=false;
164 memset(errstr,0, MAX_ERR_STR_LEN);
165
166 fflush(stdout);
167
168 int current_vb_level= o.getVerbosity();
169 int current_dbg_level= o.getDebugging();
170
171 /* Determine if caller requested that we don't print a newline character */
172 if ( level & NO_NEWLINE ){
173 level ^= NO_NEWLINE; /* Unset the flag restoring the original level */
174 skipnewline=true;
175 }
176
177 /* If supplied level is more than current level, do nothing */
178 if( level>=QT_4 && level<=VB_4 && level>current_vb_level )
179 return OP_SUCCESS;
180 if( level>=DBG_0 && level<=DBG_9 && level>current_dbg_level )
181 return OP_SUCCESS;
182
183 /* Otherwise, print the info to stderr*/
184 if ( (level>=QT_3 && level<=VB_4) || (level>=DBG_1 && level<=DBG_9) ){
185 va_start(list, str);
186 vfprintf(stdout, str, list); /* Print to stderr */
187 va_end(list);
188 if( !skipnewline )
189 fprintf(stdout,"\n");
190 }
191 return OP_SUCCESS;
192 } /* End of nping_print() */
193
194
195 /*****************************************************************************/
196 /* The following functions are provided only for compatibility with some */
197 /* code from Nmap. They should NOT be used in any new piece of code unless */
198 /* the code you are writing needs to be shared with nmap. */
199 /*****************************************************************************/
200 /** Print fatal error messages to stderr and then exits.
201 * @warning This function does not return because it calls exit() */
fatal(const char * str,...)202 int fatal(const char *str, ...) {
203 va_list list;
204 char errstr[MAX_ERR_STR_LEN];
205 memset(errstr,0, MAX_ERR_STR_LEN);
206 va_start(list, str);
207 fflush(stdout);
208 /* Print error msg to strerr */
209 vfprintf(stderr, str, list);
210 fprintf(stderr,"\n");
211 va_end(list);
212 exit(EXIT_FAILURE);
213 return OP_SUCCESS;
214 } /* End of fatal() */
215
216
217 /** Print error messages to stderr and then return. */
error(const char * str,...)218 int error(const char *str, ...) {
219 va_list list;
220 char errstr[MAX_ERR_STR_LEN];
221 memset(errstr,0, MAX_ERR_STR_LEN);
222 va_start(list, str);
223 fflush(stdout);
224 /* Print error msg to strerr */
225 vfprintf(stderr, str, list);
226 fprintf(stderr,"\n");
227 va_end(list);
228 return OP_SUCCESS;
229 } /* End of error() */
230
231
232 /** Needed by struct interface_info *getinterfaces(int *howmany) in common.h
233 * (taken originally from nmap tcpip.cc */
pfatal(const char * str,...)234 int pfatal(const char *str, ...) {
235 va_list list;
236 char errstr[MAX_ERR_STR_LEN];
237 memset(errstr,0, MAX_ERR_STR_LEN);
238 va_start(list, str);
239 fflush(stdout);
240 /* Print error msg to strerr */
241 vfprintf(stderr, str, list);
242 fprintf(stderr,"\n");
243 va_end(list);
244 exit(EXIT_FAILURE);
245 return OP_SUCCESS;
246 } /* End of fatal() */
247