xref: /netbsd/external/bsd/libpcap/dist/fmtutils.c (revision 9a3414cb)
10036d835Schristos /*
20036d835Schristos  * Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998
30036d835Schristos  *	The Regents of the University of California.  All rights reserved.
40036d835Schristos  *
50036d835Schristos  * Redistribution and use in source and binary forms, with or without
60036d835Schristos  * modification, are permitted provided that the following conditions
70036d835Schristos  * are met:
80036d835Schristos  * 1. Redistributions of source code must retain the above copyright
90036d835Schristos  *    notice, this list of conditions and the following disclaimer.
100036d835Schristos  * 2. Redistributions in binary form must reproduce the above copyright
110036d835Schristos  *    notice, this list of conditions and the following disclaimer in the
120036d835Schristos  *    documentation and/or other materials provided with the distribution.
130036d835Schristos  * 3. All advertising materials mentioning features or use of this software
140036d835Schristos  *    must display the following acknowledgement:
150036d835Schristos  *	This product includes software developed by the Computer Systems
160036d835Schristos  *	Engineering Group at Lawrence Berkeley Laboratory.
170036d835Schristos  * 4. Neither the name of the University nor of the Laboratory may be used
180036d835Schristos  *    to endorse or promote products derived from this software without
190036d835Schristos  *    specific prior written permission.
200036d835Schristos  *
210036d835Schristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
220036d835Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
230036d835Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
240036d835Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
250036d835Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
260036d835Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
270036d835Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
280036d835Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
290036d835Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
300036d835Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
310036d835Schristos  * SUCH DAMAGE.
320036d835Schristos  */
330036d835Schristos 
340036d835Schristos /*
350036d835Schristos  * Utilities for message formatting used both by libpcap and rpcapd.
360036d835Schristos  */
370036d835Schristos 
380036d835Schristos #ifdef HAVE_CONFIG_H
390036d835Schristos #include <config.h>
400036d835Schristos #endif
410036d835Schristos 
420036d835Schristos #include "ftmacros.h"
430036d835Schristos 
440036d835Schristos #include <stddef.h>
450036d835Schristos #include <stdarg.h>
460036d835Schristos #include <stdio.h>
470036d835Schristos #include <string.h>
480036d835Schristos #include <errno.h>
490036d835Schristos 
500036d835Schristos #include <pcap/pcap.h>
510036d835Schristos 
520036d835Schristos #include "portability.h"
530036d835Schristos 
540036d835Schristos #include "fmtutils.h"
550036d835Schristos 
560036d835Schristos /*
570036d835Schristos  * Generate an error message based on a format, arguments, and an
580036d835Schristos  * errno, with a message for the errno after the formatted output.
590036d835Schristos  */
600036d835Schristos void
pcap_fmt_errmsg_for_errno(char * errbuf,size_t errbuflen,int errnum,const char * fmt,...)610036d835Schristos pcap_fmt_errmsg_for_errno(char *errbuf, size_t errbuflen, int errnum,
620036d835Schristos     const char *fmt, ...)
630036d835Schristos {
640036d835Schristos 	va_list ap;
650036d835Schristos 	size_t msglen;
660036d835Schristos 	char *p;
670036d835Schristos 	size_t errbuflen_remaining;
680036d835Schristos 
690036d835Schristos 	va_start(ap, fmt);
700036d835Schristos 	pcap_vsnprintf(errbuf, errbuflen, fmt, ap);
710036d835Schristos 	va_end(ap);
720036d835Schristos 	msglen = strlen(errbuf);
730036d835Schristos 
740036d835Schristos 	/*
750036d835Schristos 	 * Do we have enough space to append ": "?
760036d835Schristos 	 * Including the terminating '\0', that's 3 bytes.
770036d835Schristos 	 */
780036d835Schristos 	if (msglen + 3 > errbuflen) {
790036d835Schristos 		/* No - just give them what we've produced. */
800036d835Schristos 		return;
810036d835Schristos 	}
820036d835Schristos 	p = errbuf + msglen;
830036d835Schristos 	errbuflen_remaining = errbuflen - msglen;
840036d835Schristos 	*p++ = ':';
850036d835Schristos 	*p++ = ' ';
860036d835Schristos 	*p = '\0';
870036d835Schristos 	msglen += 2;
880036d835Schristos 	errbuflen_remaining -= 2;
890036d835Schristos 
900036d835Schristos 	/*
910036d835Schristos 	 * Now append the string for the error code.
920036d835Schristos 	 */
930036d835Schristos #if defined(HAVE_STRERROR_S)
94*9a3414cbSchristos 	/*
95*9a3414cbSchristos 	 * We have a Windows-style strerror_s().
96*9a3414cbSchristos 	 */
97*9a3414cbSchristos 	errno_t err = strerror_s(p, errbuflen_remaining, errnum);
980036d835Schristos 	if (err != 0) {
990036d835Schristos 		/*
1000036d835Schristos 		 * It doesn't appear to be documented anywhere obvious
1010036d835Schristos 		 * what the error returns from strerror_s().
1020036d835Schristos 		 */
1030036d835Schristos 		pcap_snprintf(p, errbuflen_remaining, "Error %d", errnum);
1040036d835Schristos 	}
105*9a3414cbSchristos #elif defined(HAVE_GNU_STRERROR_R)
106*9a3414cbSchristos 	/*
107*9a3414cbSchristos 	 * We have a GNU-style strerror_r(), which is *not* guaranteed to
108*9a3414cbSchristos 	 * do anything to the buffer handed to it, and which returns a
109*9a3414cbSchristos 	 * pointer to the error string, which may or may not be in
110*9a3414cbSchristos 	 * the buffer.
111*9a3414cbSchristos 	 *
112*9a3414cbSchristos 	 * It is, however, guaranteed to succeed.
113*9a3414cbSchristos 	 */
114*9a3414cbSchristos 	char strerror_buf[PCAP_ERRBUF_SIZE];
115*9a3414cbSchristos 	char *errstring = strerror_r(errnum, strerror_buf, PCAP_ERRBUF_SIZE);
116*9a3414cbSchristos 	pcap_snprintf(p, errbuflen_remaining, "%s", errstring);
117*9a3414cbSchristos #elif defined(HAVE_POSIX_STRERROR_R)
118*9a3414cbSchristos 	/*
119*9a3414cbSchristos 	 * We have a POSIX-style strerror_r(), which is guaranteed to fill
120*9a3414cbSchristos 	 * in the buffer, but is not guaranteed to succeed.
121*9a3414cbSchristos 	 */
122*9a3414cbSchristos 	int err = strerror_r(errnum, p, errbuflen_remaining);
1230036d835Schristos 	if (err == EINVAL) {
1240036d835Schristos 		/*
1250036d835Schristos 		 * UNIX 03 says this isn't guaranteed to produce a
1260036d835Schristos 		 * fallback error message.
1270036d835Schristos 		 */
1280036d835Schristos 		pcap_snprintf(p, errbuflen_remaining, "Unknown error: %d",
1290036d835Schristos 		    errnum);
1300036d835Schristos 	} else if (err == ERANGE) {
1310036d835Schristos 		/*
1320036d835Schristos 		 * UNIX 03 says this isn't guaranteed to produce a
1330036d835Schristos 		 * fallback error message.
1340036d835Schristos 		 */
1350036d835Schristos 		pcap_snprintf(p, errbuflen_remaining,
1360036d835Schristos 		    "Message for error %d is too long", errnum);
1370036d835Schristos 	}
1380036d835Schristos #else
1390036d835Schristos 	/*
1400036d835Schristos 	 * We have neither strerror_s() nor strerror_r(), so we're
1410036d835Schristos 	 * stuck with using pcap_strerror().
1420036d835Schristos 	 */
1430036d835Schristos 	pcap_snprintf(p, errbuflen_remaining, "%s", pcap_strerror(errnum));
1440036d835Schristos #endif
1450036d835Schristos }
146*9a3414cbSchristos 
147*9a3414cbSchristos #ifdef _WIN32
148*9a3414cbSchristos /*
149*9a3414cbSchristos  * Generate an error message based on a format, arguments, and a
150*9a3414cbSchristos  * Win32 error, with a message for the Win32 error after the formatted output.
151*9a3414cbSchristos  */
152*9a3414cbSchristos void
pcap_fmt_errmsg_for_win32_err(char * errbuf,size_t errbuflen,DWORD errnum,const char * fmt,...)153*9a3414cbSchristos pcap_fmt_errmsg_for_win32_err(char *errbuf, size_t errbuflen, DWORD errnum,
154*9a3414cbSchristos     const char *fmt, ...)
155*9a3414cbSchristos {
156*9a3414cbSchristos 	va_list ap;
157*9a3414cbSchristos 	size_t msglen;
158*9a3414cbSchristos 	char *p;
159*9a3414cbSchristos 	size_t errbuflen_remaining;
160*9a3414cbSchristos 	DWORD retval;
161*9a3414cbSchristos 	char win32_errbuf[PCAP_ERRBUF_SIZE+1];
162*9a3414cbSchristos 
163*9a3414cbSchristos 	va_start(ap, fmt);
164*9a3414cbSchristos 	pcap_vsnprintf(errbuf, errbuflen, fmt, ap);
165*9a3414cbSchristos 	va_end(ap);
166*9a3414cbSchristos 	msglen = strlen(errbuf);
167*9a3414cbSchristos 
168*9a3414cbSchristos 	/*
169*9a3414cbSchristos 	 * Do we have enough space to append ": "?
170*9a3414cbSchristos 	 * Including the terminating '\0', that's 3 bytes.
171*9a3414cbSchristos 	 */
172*9a3414cbSchristos 	if (msglen + 3 > errbuflen) {
173*9a3414cbSchristos 		/* No - just give them what we've produced. */
174*9a3414cbSchristos 		return;
175*9a3414cbSchristos 	}
176*9a3414cbSchristos 	p = errbuf + msglen;
177*9a3414cbSchristos 	errbuflen_remaining = errbuflen - msglen;
178*9a3414cbSchristos 	*p++ = ':';
179*9a3414cbSchristos 	*p++ = ' ';
180*9a3414cbSchristos 	*p = '\0';
181*9a3414cbSchristos 	msglen += 2;
182*9a3414cbSchristos 	errbuflen_remaining -= 2;
183*9a3414cbSchristos 
184*9a3414cbSchristos 	/*
185*9a3414cbSchristos 	 * Now append the string for the error code.
186*9a3414cbSchristos 	 *
187*9a3414cbSchristos 	 * XXX - what language ID to use?
188*9a3414cbSchristos 	 *
189*9a3414cbSchristos 	 * For UN*Xes, pcap_strerror() may or may not return localized
190*9a3414cbSchristos 	 * strings.
191*9a3414cbSchristos 	 *
192*9a3414cbSchristos 	 * We currently don't have localized messages for libpcap, but
193*9a3414cbSchristos 	 * we might want to do so.  On the other hand, if most of these
194*9a3414cbSchristos 	 * messages are going to be read by libpcap developers and
195*9a3414cbSchristos 	 * perhaps by developers of libpcap-based applications, English
196*9a3414cbSchristos 	 * might be a better choice, so the developer doesn't have to
197*9a3414cbSchristos 	 * get the message translated if it's in a language they don't
198*9a3414cbSchristos 	 * happen to understand.
199*9a3414cbSchristos 	 */
200*9a3414cbSchristos 	retval = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_MAX_WIDTH_MASK,
201*9a3414cbSchristos 	    NULL, errnum, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
202*9a3414cbSchristos 	    win32_errbuf, PCAP_ERRBUF_SIZE, NULL);
203*9a3414cbSchristos 	if (retval == 0) {
204*9a3414cbSchristos 		/*
205*9a3414cbSchristos 		 * Failed.
206*9a3414cbSchristos 		 */
207*9a3414cbSchristos 		pcap_snprintf(p, errbuflen_remaining,
208*9a3414cbSchristos 		    "Couldn't get error message for error (%lu)", errnum);
209*9a3414cbSchristos 		return;
210*9a3414cbSchristos 	}
211*9a3414cbSchristos 
212*9a3414cbSchristos 	pcap_snprintf(p, errbuflen_remaining, "%s (%lu)", win32_errbuf, errnum);
213*9a3414cbSchristos }
214*9a3414cbSchristos #endif
215