Blame fmtutils.c

Packit 209cc3
/*
Packit 209cc3
 * Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998
Packit 209cc3
 *	The Regents of the University of California.  All rights reserved.
Packit 209cc3
 *
Packit 209cc3
 * Redistribution and use in source and binary forms, with or without
Packit 209cc3
 * modification, are permitted provided that the following conditions
Packit 209cc3
 * are met:
Packit 209cc3
 * 1. Redistributions of source code must retain the above copyright
Packit 209cc3
 *    notice, this list of conditions and the following disclaimer.
Packit 209cc3
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 209cc3
 *    notice, this list of conditions and the following disclaimer in the
Packit 209cc3
 *    documentation and/or other materials provided with the distribution.
Packit 209cc3
 * 3. All advertising materials mentioning features or use of this software
Packit 209cc3
 *    must display the following acknowledgement:
Packit 209cc3
 *	This product includes software developed by the Computer Systems
Packit 209cc3
 *	Engineering Group at Lawrence Berkeley Laboratory.
Packit 209cc3
 * 4. Neither the name of the University nor of the Laboratory may be used
Packit 209cc3
 *    to endorse or promote products derived from this software without
Packit 209cc3
 *    specific prior written permission.
Packit 209cc3
 *
Packit 209cc3
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit 209cc3
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 209cc3
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 209cc3
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit 209cc3
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 209cc3
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit 209cc3
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit 209cc3
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit 209cc3
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit 209cc3
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit 209cc3
 * SUCH DAMAGE.
Packit 209cc3
 */
Packit 209cc3
Packit 209cc3
/*
Packit 209cc3
 * Utilities for message formatting used both by libpcap and rpcapd.
Packit 209cc3
 */
Packit 209cc3
Packit 209cc3
#ifdef HAVE_CONFIG_H
Packit 209cc3
#include <config.h>
Packit 209cc3
#endif
Packit 209cc3
Packit 209cc3
#include "ftmacros.h"
Packit 209cc3
Packit 209cc3
#include <stddef.h>
Packit 209cc3
#include <stdarg.h>
Packit 209cc3
#include <stdio.h>
Packit 209cc3
#include <string.h>
Packit 209cc3
#include <errno.h>
Packit 209cc3
Packit 209cc3
#include <pcap/pcap.h>
Packit 209cc3
Packit 209cc3
#include "portability.h"
Packit 209cc3
Packit 209cc3
#include "fmtutils.h"
Packit 209cc3
Packit 209cc3
/*
Packit 209cc3
 * Generate an error message based on a format, arguments, and an
Packit 209cc3
 * errno, with a message for the errno after the formatted output.
Packit 209cc3
 */
Packit 209cc3
void
Packit 209cc3
pcap_fmt_errmsg_for_errno(char *errbuf, size_t errbuflen, int errnum,
Packit 209cc3
    const char *fmt, ...)
Packit 209cc3
{
Packit 209cc3
	va_list ap;
Packit 209cc3
	size_t msglen;
Packit 209cc3
	char *p;
Packit 209cc3
	size_t errbuflen_remaining;
Packit 209cc3
Packit 209cc3
	va_start(ap, fmt);
Packit 209cc3
	pcap_vsnprintf(errbuf, errbuflen, fmt, ap);
Packit 209cc3
	va_end(ap);
Packit 209cc3
	msglen = strlen(errbuf);
Packit 209cc3
Packit 209cc3
	/*
Packit 209cc3
	 * Do we have enough space to append ": "?
Packit 209cc3
	 * Including the terminating '\0', that's 3 bytes.
Packit 209cc3
	 */
Packit 209cc3
	if (msglen + 3 > errbuflen) {
Packit 209cc3
		/* No - just give them what we've produced. */
Packit 209cc3
		return;
Packit 209cc3
	}
Packit 209cc3
	p = errbuf + msglen;
Packit 209cc3
	errbuflen_remaining = errbuflen - msglen;
Packit 209cc3
	*p++ = ':';
Packit 209cc3
	*p++ = ' ';
Packit 209cc3
	*p = '\0';
Packit 209cc3
	msglen += 2;
Packit 209cc3
	errbuflen_remaining -= 2;
Packit 209cc3
Packit 209cc3
	/*
Packit 209cc3
	 * Now append the string for the error code.
Packit 209cc3
	 */
Packit 209cc3
#if defined(HAVE_STRERROR_S)
Packit 209cc3
	/*
Packit 209cc3
	 * We have a Windows-style strerror_s().
Packit 209cc3
	 */
Packit 209cc3
	errno_t err = strerror_s(p, errbuflen_remaining, errnum);
Packit 209cc3
	if (err != 0) {
Packit 209cc3
		/*
Packit 209cc3
		 * It doesn't appear to be documented anywhere obvious
Packit 209cc3
		 * what the error returns from strerror_s().
Packit 209cc3
		 */
Packit 209cc3
		pcap_snprintf(p, errbuflen_remaining, "Error %d", errnum);
Packit 209cc3
	}
Packit 209cc3
#elif defined(HAVE_GNU_STRERROR_R)
Packit 209cc3
	/*
Packit 209cc3
	 * We have a GNU-style strerror_r(), which is *not* guaranteed to
Packit 209cc3
	 * do anything to the buffer handed to it, and which returns a
Packit 209cc3
	 * pointer to the error string, which may or may not be in
Packit 209cc3
	 * the buffer.
Packit 209cc3
	 *
Packit 209cc3
	 * It is, however, guaranteed to succeed.
Packit 209cc3
	 */
Packit 209cc3
	char strerror_buf[PCAP_ERRBUF_SIZE];
Packit 209cc3
	char *errstring = strerror_r(errnum, strerror_buf, PCAP_ERRBUF_SIZE);
Packit 209cc3
	pcap_snprintf(p, errbuflen_remaining, "%s", errstring);
Packit 209cc3
#elif defined(HAVE_POSIX_STRERROR_R)
Packit 209cc3
	/*
Packit 209cc3
	 * We have a POSIX-style strerror_r(), which is guaranteed to fill
Packit 209cc3
	 * in the buffer, but is not guaranteed to succeed.
Packit 209cc3
	 */
Packit 209cc3
	int err = strerror_r(errnum, p, errbuflen_remaining);
Packit 209cc3
	if (err == EINVAL) {
Packit 209cc3
		/*
Packit 209cc3
		 * UNIX 03 says this isn't guaranteed to produce a
Packit 209cc3
		 * fallback error message.
Packit 209cc3
		 */
Packit 209cc3
		pcap_snprintf(p, errbuflen_remaining, "Unknown error: %d",
Packit 209cc3
		    errnum);
Packit 209cc3
	} else if (err == ERANGE) {
Packit 209cc3
		/*
Packit 209cc3
		 * UNIX 03 says this isn't guaranteed to produce a
Packit 209cc3
		 * fallback error message.
Packit 209cc3
		 */
Packit 209cc3
		pcap_snprintf(p, errbuflen_remaining,
Packit 209cc3
		    "Message for error %d is too long", errnum);
Packit 209cc3
	}
Packit 209cc3
#else
Packit 209cc3
	/*
Packit 209cc3
	 * We have neither strerror_s() nor strerror_r(), so we're
Packit 209cc3
	 * stuck with using pcap_strerror().
Packit 209cc3
	 */
Packit 209cc3
	pcap_snprintf(p, errbuflen_remaining, "%s", pcap_strerror(errnum));
Packit 209cc3
#endif
Packit 209cc3
}
Packit 209cc3
Packit 209cc3
#ifdef _WIN32
Packit 209cc3
/*
Packit 209cc3
 * Generate an error message based on a format, arguments, and a
Packit 209cc3
 * Win32 error, with a message for the Win32 error after the formatted output.
Packit 209cc3
 */
Packit 209cc3
void
Packit 209cc3
pcap_fmt_errmsg_for_win32_err(char *errbuf, size_t errbuflen, DWORD errnum,
Packit 209cc3
    const char *fmt, ...)
Packit 209cc3
{
Packit 209cc3
	va_list ap;
Packit 209cc3
	size_t msglen;
Packit 209cc3
	char *p;
Packit 209cc3
	size_t errbuflen_remaining;
Packit 209cc3
	DWORD retval;
Packit 209cc3
	char win32_errbuf[PCAP_ERRBUF_SIZE+1];
Packit 209cc3
Packit 209cc3
	va_start(ap, fmt);
Packit 209cc3
	pcap_vsnprintf(errbuf, errbuflen, fmt, ap);
Packit 209cc3
	va_end(ap);
Packit 209cc3
	msglen = strlen(errbuf);
Packit 209cc3
Packit 209cc3
	/*
Packit 209cc3
	 * Do we have enough space to append ": "?
Packit 209cc3
	 * Including the terminating '\0', that's 3 bytes.
Packit 209cc3
	 */
Packit 209cc3
	if (msglen + 3 > errbuflen) {
Packit 209cc3
		/* No - just give them what we've produced. */
Packit 209cc3
		return;
Packit 209cc3
	}
Packit 209cc3
	p = errbuf + msglen;
Packit 209cc3
	errbuflen_remaining = errbuflen - msglen;
Packit 209cc3
	*p++ = ':';
Packit 209cc3
	*p++ = ' ';
Packit 209cc3
	*p = '\0';
Packit 209cc3
	msglen += 2;
Packit 209cc3
	errbuflen_remaining -= 2;
Packit 209cc3
Packit 209cc3
	/*
Packit 209cc3
	 * Now append the string for the error code.
Packit 209cc3
	 *
Packit 209cc3
	 * XXX - what language ID to use?
Packit 209cc3
	 *
Packit 209cc3
	 * For UN*Xes, pcap_strerror() may or may not return localized
Packit 209cc3
	 * strings.
Packit 209cc3
	 *
Packit 209cc3
	 * We currently don't have localized messages for libpcap, but
Packit 209cc3
	 * we might want to do so.  On the other hand, if most of these
Packit 209cc3
	 * messages are going to be read by libpcap developers and
Packit 209cc3
	 * perhaps by developers of libpcap-based applications, English
Packit 209cc3
	 * might be a better choice, so the developer doesn't have to
Packit 209cc3
	 * get the message translated if it's in a language they don't
Packit 209cc3
	 * happen to understand.
Packit 209cc3
	 */
Packit 209cc3
	retval = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_MAX_WIDTH_MASK,
Packit 209cc3
	    NULL, errnum, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
Packit 209cc3
	    win32_errbuf, PCAP_ERRBUF_SIZE, NULL);
Packit 209cc3
	if (retval == 0) {
Packit 209cc3
		/*
Packit 209cc3
		 * Failed.
Packit 209cc3
		 */
Packit 209cc3
		pcap_snprintf(p, errbuflen_remaining,
Packit 209cc3
		    "Couldn't get error message for error (%lu)", errnum);
Packit 209cc3
		return;
Packit 209cc3
	}
Packit 209cc3
Packit 209cc3
	pcap_snprintf(p, errbuflen_remaining, "%s (%lu)", win32_errbuf, errnum);
Packit 209cc3
}
Packit 209cc3
#endif