1238106Sdes/*
2238106Sdes * util/log.c - implementation of the log code
3238106Sdes *
4238106Sdes * Copyright (c) 2007, NLnet Labs. All rights reserved.
5238106Sdes *
6238106Sdes * This software is open source.
7238106Sdes *
8238106Sdes * Redistribution and use in source and binary forms, with or without
9238106Sdes * modification, are permitted provided that the following conditions
10238106Sdes * are met:
11238106Sdes *
12238106Sdes * Redistributions of source code must retain the above copyright notice,
13238106Sdes * this list of conditions and the following disclaimer.
14238106Sdes *
15238106Sdes * Redistributions in binary form must reproduce the above copyright notice,
16238106Sdes * this list of conditions and the following disclaimer in the documentation
17238106Sdes * and/or other materials provided with the distribution.
18238106Sdes *
19238106Sdes * Neither the name of the NLNET LABS nor the names of its contributors may
20238106Sdes * be used to endorse or promote products derived from this software without
21238106Sdes * specific prior written permission.
22238106Sdes *
23238106Sdes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24269257Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25269257Sdes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26269257Sdes * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27269257Sdes * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28269257Sdes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29269257Sdes * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30269257Sdes * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31269257Sdes * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32269257Sdes * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33269257Sdes * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34238106Sdes */
35238106Sdes/**
36238106Sdes * \file
37238106Sdes * Implementation of log.h.
38238106Sdes */
39238106Sdes
40238106Sdes#include "config.h"
41238106Sdes#include "util/log.h"
42238106Sdes#include "util/locks.h"
43291767Sdes#include "sldns/sbuffer.h"
44269257Sdes#include <stdarg.h>
45238106Sdes#ifdef HAVE_TIME_H
46238106Sdes#include <time.h>
47238106Sdes#endif
48238106Sdes#ifdef HAVE_SYSLOG_H
49238106Sdes#  include <syslog.h>
50238106Sdes#else
51238106Sdes/**define LOG_ constants */
52238106Sdes#  define LOG_CRIT 2
53238106Sdes#  define LOG_ERR 3
54238106Sdes#  define LOG_WARNING 4
55238106Sdes#  define LOG_NOTICE 5
56238106Sdes#  define LOG_INFO 6
57238106Sdes#  define LOG_DEBUG 7
58238106Sdes#endif
59238106Sdes#ifdef UB_ON_WINDOWS
60238106Sdes#  include "winrc/win_svc.h"
61238106Sdes#endif
62238106Sdes
63238106Sdes/* default verbosity */
64238106Sdesenum verbosity_value verbosity = 0;
65238106Sdes/** the file logged to. */
66238106Sdesstatic FILE* logfile = 0;
67238106Sdes/** if key has been created */
68238106Sdesstatic int key_created = 0;
69238106Sdes/** pthread key for thread ids in logfile */
70238106Sdesstatic ub_thread_key_t logkey;
71285206Sdes#ifndef THREADS_DISABLED
72285206Sdes/** pthread mutex to protect FILE* */
73285206Sdesstatic lock_quick_t log_lock;
74285206Sdes#endif
75238106Sdes/** the identity of this executable/process */
76238106Sdesstatic const char* ident="unbound";
77238106Sdes#if defined(HAVE_SYSLOG_H) || defined(UB_ON_WINDOWS)
78238106Sdes/** are we using syslog(3) to log to */
79238106Sdesstatic int logging_to_syslog = 0;
80238106Sdes#endif /* HAVE_SYSLOG_H */
81238106Sdes/** time to print in log, if NULL, use time(2) */
82269257Sdesstatic time_t* log_now = NULL;
83238106Sdes/** print time in UTC or in secondsfrom1970 */
84238106Sdesstatic int log_time_asc = 0;
85238106Sdes
86238106Sdesvoid
87238106Sdeslog_init(const char* filename, int use_syslog, const char* chrootdir)
88238106Sdes{
89238106Sdes	FILE *f;
90238106Sdes	if(!key_created) {
91238106Sdes		key_created = 1;
92238106Sdes		ub_thread_key_create(&logkey, NULL);
93285206Sdes		lock_quick_init(&log_lock);
94238106Sdes	}
95285206Sdes	lock_quick_lock(&log_lock);
96238106Sdes	if(logfile
97238106Sdes#if defined(HAVE_SYSLOG_H) || defined(UB_ON_WINDOWS)
98238106Sdes	|| logging_to_syslog
99238106Sdes#endif
100285206Sdes	) {
101285206Sdes		lock_quick_unlock(&log_lock); /* verbose() needs the lock */
102285206Sdes		verbose(VERB_QUERY, "switching log to %s",
103285206Sdes			use_syslog?"syslog":(filename&&filename[0]?filename:"stderr"));
104285206Sdes		lock_quick_lock(&log_lock);
105285206Sdes	}
106238106Sdes	if(logfile && logfile != stderr)
107238106Sdes		fclose(logfile);
108238106Sdes#ifdef HAVE_SYSLOG_H
109238106Sdes	if(logging_to_syslog) {
110238106Sdes		closelog();
111238106Sdes		logging_to_syslog = 0;
112238106Sdes	}
113238106Sdes	if(use_syslog) {
114238106Sdes		/* do not delay opening until first write, because we may
115238106Sdes		 * chroot and no longer be able to access dev/log and so on */
116238106Sdes		openlog(ident, LOG_NDELAY, LOG_DAEMON);
117238106Sdes		logging_to_syslog = 1;
118285206Sdes		lock_quick_unlock(&log_lock);
119238106Sdes		return;
120238106Sdes	}
121238106Sdes#elif defined(UB_ON_WINDOWS)
122238106Sdes	if(logging_to_syslog) {
123238106Sdes		logging_to_syslog = 0;
124238106Sdes	}
125238106Sdes	if(use_syslog) {
126238106Sdes		logging_to_syslog = 1;
127285206Sdes		lock_quick_unlock(&log_lock);
128238106Sdes		return;
129238106Sdes	}
130238106Sdes#endif /* HAVE_SYSLOG_H */
131238106Sdes	if(!filename || !filename[0]) {
132238106Sdes		logfile = stderr;
133285206Sdes		lock_quick_unlock(&log_lock);
134238106Sdes		return;
135238106Sdes	}
136238106Sdes	/* open the file for logging */
137238106Sdes	if(chrootdir && chrootdir[0] && strncmp(filename, chrootdir,
138238106Sdes		strlen(chrootdir)) == 0)
139238106Sdes		filename += strlen(chrootdir);
140238106Sdes	f = fopen(filename, "a");
141238106Sdes	if(!f) {
142285206Sdes		lock_quick_unlock(&log_lock);
143238106Sdes		log_err("Could not open logfile %s: %s", filename,
144238106Sdes			strerror(errno));
145238106Sdes		return;
146238106Sdes	}
147238106Sdes#ifndef UB_ON_WINDOWS
148238106Sdes	/* line buffering does not work on windows */
149238106Sdes	setvbuf(f, NULL, (int)_IOLBF, 0);
150238106Sdes#endif
151238106Sdes	logfile = f;
152285206Sdes	lock_quick_unlock(&log_lock);
153238106Sdes}
154238106Sdes
155238106Sdesvoid log_file(FILE *f)
156238106Sdes{
157285206Sdes	lock_quick_lock(&log_lock);
158238106Sdes	logfile = f;
159285206Sdes	lock_quick_unlock(&log_lock);
160238106Sdes}
161238106Sdes
162238106Sdesvoid log_thread_set(int* num)
163238106Sdes{
164238106Sdes	ub_thread_key_set(logkey, num);
165238106Sdes}
166238106Sdes
167291767Sdesint log_thread_get(void)
168291767Sdes{
169291767Sdes	unsigned int* tid;
170291767Sdes	if(!key_created) return 0;
171291767Sdes	tid = (unsigned int*)ub_thread_key_get(logkey);
172291767Sdes	return (int)(tid?*tid:0);
173291767Sdes}
174291767Sdes
175238106Sdesvoid log_ident_set(const char* id)
176238106Sdes{
177238106Sdes	ident = id;
178238106Sdes}
179238106Sdes
180269257Sdesvoid log_set_time(time_t* t)
181238106Sdes{
182238106Sdes	log_now = t;
183238106Sdes}
184238106Sdes
185238106Sdesvoid log_set_time_asc(int use_asc)
186238106Sdes{
187238106Sdes	log_time_asc = use_asc;
188238106Sdes}
189238106Sdes
190238106Sdesvoid
191238106Sdeslog_vmsg(int pri, const char* type,
192238106Sdes	const char *format, va_list args)
193238106Sdes{
194238106Sdes	char message[MAXSYSLOGMSGLEN];
195238106Sdes	unsigned int* tid = (unsigned int*)ub_thread_key_get(logkey);
196238106Sdes	time_t now;
197238106Sdes#if defined(HAVE_STRFTIME) && defined(HAVE_LOCALTIME_R)
198238106Sdes	char tmbuf[32];
199238106Sdes	struct tm tm;
200249141Sdes#elif defined(UB_ON_WINDOWS)
201249141Sdes	char tmbuf[128], dtbuf[128];
202238106Sdes#endif
203238106Sdes	(void)pri;
204238106Sdes	vsnprintf(message, sizeof(message), format, args);
205238106Sdes#ifdef HAVE_SYSLOG_H
206238106Sdes	if(logging_to_syslog) {
207238106Sdes		syslog(pri, "[%d:%x] %s: %s",
208238106Sdes			(int)getpid(), tid?*tid:0, type, message);
209238106Sdes		return;
210238106Sdes	}
211238106Sdes#elif defined(UB_ON_WINDOWS)
212238106Sdes	if(logging_to_syslog) {
213238106Sdes		char m[32768];
214238106Sdes		HANDLE* s;
215238106Sdes		LPCTSTR str = m;
216238106Sdes		DWORD tp = MSG_GENERIC_ERR;
217238106Sdes		WORD wt = EVENTLOG_ERROR_TYPE;
218238106Sdes		if(strcmp(type, "info") == 0) {
219238106Sdes			tp=MSG_GENERIC_INFO;
220238106Sdes			wt=EVENTLOG_INFORMATION_TYPE;
221238106Sdes		} else if(strcmp(type, "warning") == 0) {
222238106Sdes			tp=MSG_GENERIC_WARN;
223238106Sdes			wt=EVENTLOG_WARNING_TYPE;
224238106Sdes		} else if(strcmp(type, "notice") == 0
225238106Sdes			|| strcmp(type, "debug") == 0) {
226238106Sdes			tp=MSG_GENERIC_SUCCESS;
227238106Sdes			wt=EVENTLOG_SUCCESS;
228238106Sdes		}
229238106Sdes		snprintf(m, sizeof(m), "[%s:%x] %s: %s",
230238106Sdes			ident, tid?*tid:0, type, message);
231238106Sdes		s = RegisterEventSource(NULL, SERVICE_NAME);
232238106Sdes		if(!s) return;
233238106Sdes		ReportEvent(s, wt, 0, tp, NULL, 1, 0, &str, NULL);
234238106Sdes		DeregisterEventSource(s);
235238106Sdes		return;
236238106Sdes	}
237238106Sdes#endif /* HAVE_SYSLOG_H */
238285206Sdes	lock_quick_lock(&log_lock);
239285206Sdes	if(!logfile) {
240285206Sdes		lock_quick_unlock(&log_lock);
241285206Sdes		return;
242285206Sdes	}
243238106Sdes	if(log_now)
244238106Sdes		now = (time_t)*log_now;
245238106Sdes	else	now = (time_t)time(NULL);
246238106Sdes#if defined(HAVE_STRFTIME) && defined(HAVE_LOCALTIME_R)
247238106Sdes	if(log_time_asc && strftime(tmbuf, sizeof(tmbuf), "%b %d %H:%M:%S",
248238106Sdes		localtime_r(&now, &tm))%(sizeof(tmbuf)) != 0) {
249238106Sdes		/* %sizeof buf!=0 because old strftime returned max on error */
250238106Sdes		fprintf(logfile, "%s %s[%d:%x] %s: %s\n", tmbuf,
251238106Sdes			ident, (int)getpid(), tid?*tid:0, type, message);
252238106Sdes	} else
253249141Sdes#elif defined(UB_ON_WINDOWS)
254249141Sdes	if(log_time_asc && GetTimeFormat(LOCALE_USER_DEFAULT, 0, NULL, NULL,
255249141Sdes		tmbuf, sizeof(tmbuf)) && GetDateFormat(LOCALE_USER_DEFAULT, 0,
256249141Sdes		NULL, NULL, dtbuf, sizeof(dtbuf))) {
257249141Sdes		fprintf(logfile, "%s %s %s[%d:%x] %s: %s\n", dtbuf, tmbuf,
258249141Sdes			ident, (int)getpid(), tid?*tid:0, type, message);
259249141Sdes	} else
260238106Sdes#endif
261269257Sdes	fprintf(logfile, "[" ARG_LL "d] %s[%d:%x] %s: %s\n", (long long)now,
262238106Sdes		ident, (int)getpid(), tid?*tid:0, type, message);
263238106Sdes#ifdef UB_ON_WINDOWS
264238106Sdes	/* line buffering does not work on windows */
265238106Sdes	fflush(logfile);
266238106Sdes#endif
267285206Sdes	lock_quick_unlock(&log_lock);
268238106Sdes}
269238106Sdes
270238106Sdes/**
271238106Sdes * implementation of log_info
272238106Sdes * @param format: format string printf-style.
273238106Sdes */
274238106Sdesvoid
275238106Sdeslog_info(const char *format, ...)
276238106Sdes{
277238106Sdes        va_list args;
278238106Sdes	va_start(args, format);
279238106Sdes	log_vmsg(LOG_INFO, "info", format, args);
280238106Sdes	va_end(args);
281238106Sdes}
282238106Sdes
283238106Sdes/**
284238106Sdes * implementation of log_err
285238106Sdes * @param format: format string printf-style.
286238106Sdes */
287238106Sdesvoid
288238106Sdeslog_err(const char *format, ...)
289238106Sdes{
290238106Sdes        va_list args;
291238106Sdes	va_start(args, format);
292238106Sdes	log_vmsg(LOG_ERR, "error", format, args);
293238106Sdes	va_end(args);
294238106Sdes}
295238106Sdes
296238106Sdes/**
297238106Sdes * implementation of log_warn
298238106Sdes * @param format: format string printf-style.
299238106Sdes */
300238106Sdesvoid
301238106Sdeslog_warn(const char *format, ...)
302238106Sdes{
303238106Sdes        va_list args;
304238106Sdes	va_start(args, format);
305238106Sdes	log_vmsg(LOG_WARNING, "warning", format, args);
306238106Sdes	va_end(args);
307238106Sdes}
308238106Sdes
309238106Sdes/**
310238106Sdes * implementation of fatal_exit
311238106Sdes * @param format: format string printf-style.
312238106Sdes */
313238106Sdesvoid
314238106Sdesfatal_exit(const char *format, ...)
315238106Sdes{
316238106Sdes        va_list args;
317238106Sdes	va_start(args, format);
318238106Sdes	log_vmsg(LOG_CRIT, "fatal error", format, args);
319238106Sdes	va_end(args);
320238106Sdes	exit(1);
321238106Sdes}
322238106Sdes
323238106Sdes/**
324238106Sdes * implementation of verbose
325238106Sdes * @param level: verbose level for the message.
326238106Sdes * @param format: format string printf-style.
327238106Sdes */
328238106Sdesvoid
329238106Sdesverbose(enum verbosity_value level, const char* format, ...)
330238106Sdes{
331238106Sdes        va_list args;
332238106Sdes	va_start(args, format);
333238106Sdes	if(verbosity >= level) {
334238106Sdes		if(level == VERB_OPS)
335238106Sdes			log_vmsg(LOG_NOTICE, "notice", format, args);
336238106Sdes		else if(level == VERB_DETAIL)
337238106Sdes			log_vmsg(LOG_INFO, "info", format, args);
338238106Sdes		else	log_vmsg(LOG_DEBUG, "debug", format, args);
339238106Sdes	}
340238106Sdes	va_end(args);
341238106Sdes}
342238106Sdes
343238106Sdes/** log hex data */
344238106Sdesstatic void
345238106Sdeslog_hex_f(enum verbosity_value v, const char* msg, void* data, size_t length)
346238106Sdes{
347238106Sdes	size_t i, j;
348238106Sdes	uint8_t* data8 = (uint8_t*)data;
349238106Sdes	const char* hexchar = "0123456789ABCDEF";
350238106Sdes	char buf[1024+1]; /* alloc blocksize hex chars + \0 */
351238106Sdes	const size_t blocksize = 512;
352238106Sdes	size_t len;
353238106Sdes
354238106Sdes	if(length == 0) {
355238106Sdes		verbose(v, "%s[%u]", msg, (unsigned)length);
356238106Sdes		return;
357238106Sdes	}
358238106Sdes
359238106Sdes	for(i=0; i<length; i+=blocksize/2) {
360238106Sdes		len = blocksize/2;
361238106Sdes		if(length - i < blocksize/2)
362238106Sdes			len = length - i;
363238106Sdes		for(j=0; j<len; j++) {
364238106Sdes			buf[j*2] = hexchar[ data8[i+j] >> 4 ];
365238106Sdes			buf[j*2 + 1] = hexchar[ data8[i+j] & 0xF ];
366238106Sdes		}
367238106Sdes		buf[len*2] = 0;
368238106Sdes		verbose(v, "%s[%u:%u] %.*s", msg, (unsigned)length,
369238106Sdes			(unsigned)i, (int)len*2, buf);
370238106Sdes	}
371238106Sdes}
372238106Sdes
373238106Sdesvoid
374238106Sdeslog_hex(const char* msg, void* data, size_t length)
375238106Sdes{
376238106Sdes	log_hex_f(verbosity, msg, data, length);
377238106Sdes}
378238106Sdes
379269257Sdesvoid log_buf(enum verbosity_value level, const char* msg, sldns_buffer* buf)
380238106Sdes{
381238106Sdes	if(verbosity < level)
382238106Sdes		return;
383269257Sdes	log_hex_f(level, msg, sldns_buffer_begin(buf), sldns_buffer_limit(buf));
384238106Sdes}
385238106Sdes
386238106Sdes#ifdef USE_WINSOCK
387238106Sdeschar* wsa_strerror(DWORD err)
388238106Sdes{
389238106Sdes	static char unknown[32];
390238106Sdes
391238106Sdes	switch(err) {
392238106Sdes	case WSA_INVALID_HANDLE: return "Specified event object handle is invalid.";
393238106Sdes	case WSA_NOT_ENOUGH_MEMORY: return "Insufficient memory available.";
394238106Sdes	case WSA_INVALID_PARAMETER: return "One or more parameters are invalid.";
395238106Sdes	case WSA_OPERATION_ABORTED: return "Overlapped operation aborted.";
396238106Sdes	case WSA_IO_INCOMPLETE: return "Overlapped I/O event object not in signaled state.";
397238106Sdes	case WSA_IO_PENDING: return "Overlapped operations will complete later.";
398238106Sdes	case WSAEINTR: return "Interrupted function call.";
399238106Sdes	case WSAEBADF: return "File handle is not valid.";
400238106Sdes 	case WSAEACCES: return "Permission denied.";
401238106Sdes	case WSAEFAULT: return "Bad address.";
402238106Sdes	case WSAEINVAL: return "Invalid argument.";
403238106Sdes	case WSAEMFILE: return "Too many open files.";
404238106Sdes	case WSAEWOULDBLOCK: return "Resource temporarily unavailable.";
405238106Sdes	case WSAEINPROGRESS: return "Operation now in progress.";
406238106Sdes	case WSAEALREADY: return "Operation already in progress.";
407238106Sdes	case WSAENOTSOCK: return "Socket operation on nonsocket.";
408238106Sdes	case WSAEDESTADDRREQ: return "Destination address required.";
409238106Sdes	case WSAEMSGSIZE: return "Message too long.";
410238106Sdes	case WSAEPROTOTYPE: return "Protocol wrong type for socket.";
411238106Sdes	case WSAENOPROTOOPT: return "Bad protocol option.";
412238106Sdes	case WSAEPROTONOSUPPORT: return "Protocol not supported.";
413238106Sdes	case WSAESOCKTNOSUPPORT: return "Socket type not supported.";
414238106Sdes	case WSAEOPNOTSUPP: return "Operation not supported.";
415238106Sdes	case WSAEPFNOSUPPORT: return "Protocol family not supported.";
416238106Sdes	case WSAEAFNOSUPPORT: return "Address family not supported by protocol family.";
417238106Sdes	case WSAEADDRINUSE: return "Address already in use.";
418238106Sdes	case WSAEADDRNOTAVAIL: return "Cannot assign requested address.";
419238106Sdes	case WSAENETDOWN: return "Network is down.";
420238106Sdes	case WSAENETUNREACH: return "Network is unreachable.";
421238106Sdes	case WSAENETRESET: return "Network dropped connection on reset.";
422238106Sdes	case WSAECONNABORTED: return "Software caused connection abort.";
423238106Sdes	case WSAECONNRESET: return "Connection reset by peer.";
424238106Sdes	case WSAENOBUFS: return "No buffer space available.";
425238106Sdes	case WSAEISCONN: return "Socket is already connected.";
426238106Sdes	case WSAENOTCONN: return "Socket is not connected.";
427238106Sdes	case WSAESHUTDOWN: return "Cannot send after socket shutdown.";
428238106Sdes	case WSAETOOMANYREFS: return "Too many references.";
429238106Sdes	case WSAETIMEDOUT: return "Connection timed out.";
430238106Sdes	case WSAECONNREFUSED: return "Connection refused.";
431238106Sdes	case WSAELOOP: return "Cannot translate name.";
432238106Sdes	case WSAENAMETOOLONG: return "Name too long.";
433238106Sdes	case WSAEHOSTDOWN: return "Host is down.";
434238106Sdes	case WSAEHOSTUNREACH: return "No route to host.";
435238106Sdes	case WSAENOTEMPTY: return "Directory not empty.";
436238106Sdes	case WSAEPROCLIM: return "Too many processes.";
437238106Sdes	case WSAEUSERS: return "User quota exceeded.";
438238106Sdes	case WSAEDQUOT: return "Disk quota exceeded.";
439238106Sdes	case WSAESTALE: return "Stale file handle reference.";
440238106Sdes	case WSAEREMOTE: return "Item is remote.";
441238106Sdes	case WSASYSNOTREADY: return "Network subsystem is unavailable.";
442238106Sdes	case WSAVERNOTSUPPORTED: return "Winsock.dll version out of range.";
443238106Sdes	case WSANOTINITIALISED: return "Successful WSAStartup not yet performed.";
444238106Sdes	case WSAEDISCON: return "Graceful shutdown in progress.";
445238106Sdes	case WSAENOMORE: return "No more results.";
446238106Sdes	case WSAECANCELLED: return "Call has been canceled.";
447238106Sdes	case WSAEINVALIDPROCTABLE: return "Procedure call table is invalid.";
448238106Sdes	case WSAEINVALIDPROVIDER: return "Service provider is invalid.";
449238106Sdes	case WSAEPROVIDERFAILEDINIT: return "Service provider failed to initialize.";
450238106Sdes	case WSASYSCALLFAILURE: return "System call failure.";
451238106Sdes	case WSASERVICE_NOT_FOUND: return "Service not found.";
452238106Sdes	case WSATYPE_NOT_FOUND: return "Class type not found.";
453238106Sdes	case WSA_E_NO_MORE: return "No more results.";
454238106Sdes	case WSA_E_CANCELLED: return "Call was canceled.";
455238106Sdes	case WSAEREFUSED: return "Database query was refused.";
456238106Sdes	case WSAHOST_NOT_FOUND: return "Host not found.";
457238106Sdes	case WSATRY_AGAIN: return "Nonauthoritative host not found.";
458238106Sdes	case WSANO_RECOVERY: return "This is a nonrecoverable error.";
459238106Sdes	case WSANO_DATA: return "Valid name, no data record of requested type.";
460238106Sdes	case WSA_QOS_RECEIVERS: return "QOS receivers.";
461238106Sdes	case WSA_QOS_SENDERS: return "QOS senders.";
462238106Sdes	case WSA_QOS_NO_SENDERS: return "No QOS senders.";
463238106Sdes	case WSA_QOS_NO_RECEIVERS: return "QOS no receivers.";
464238106Sdes	case WSA_QOS_REQUEST_CONFIRMED: return "QOS request confirmed.";
465238106Sdes	case WSA_QOS_ADMISSION_FAILURE: return "QOS admission error.";
466238106Sdes	case WSA_QOS_POLICY_FAILURE: return "QOS policy failure.";
467238106Sdes	case WSA_QOS_BAD_STYLE: return "QOS bad style.";
468238106Sdes	case WSA_QOS_BAD_OBJECT: return "QOS bad object.";
469238106Sdes	case WSA_QOS_TRAFFIC_CTRL_ERROR: return "QOS traffic control error.";
470238106Sdes	case WSA_QOS_GENERIC_ERROR: return "QOS generic error.";
471238106Sdes	case WSA_QOS_ESERVICETYPE: return "QOS service type error.";
472238106Sdes	case WSA_QOS_EFLOWSPEC: return "QOS flowspec error.";
473238106Sdes	case WSA_QOS_EPROVSPECBUF: return "Invalid QOS provider buffer.";
474238106Sdes	case WSA_QOS_EFILTERSTYLE: return "Invalid QOS filter style.";
475238106Sdes	case WSA_QOS_EFILTERTYPE: return "Invalid QOS filter type.";
476238106Sdes	case WSA_QOS_EFILTERCOUNT: return "Incorrect QOS filter count.";
477238106Sdes	case WSA_QOS_EOBJLENGTH: return "Invalid QOS object length.";
478238106Sdes	case WSA_QOS_EFLOWCOUNT: return "Incorrect QOS flow count.";
479238106Sdes	/*case WSA_QOS_EUNKOWNPSOBJ: return "Unrecognized QOS object.";*/
480238106Sdes	case WSA_QOS_EPOLICYOBJ: return "Invalid QOS policy object.";
481238106Sdes	case WSA_QOS_EFLOWDESC: return "Invalid QOS flow descriptor.";
482238106Sdes	case WSA_QOS_EPSFLOWSPEC: return "Invalid QOS provider-specific flowspec.";
483238106Sdes	case WSA_QOS_EPSFILTERSPEC: return "Invalid QOS provider-specific filterspec.";
484238106Sdes	case WSA_QOS_ESDMODEOBJ: return "Invalid QOS shape discard mode object.";
485238106Sdes	case WSA_QOS_ESHAPERATEOBJ: return "Invalid QOS shaping rate object.";
486238106Sdes	case WSA_QOS_RESERVED_PETYPE: return "Reserved policy QOS element type.";
487238106Sdes	default:
488238106Sdes		snprintf(unknown, sizeof(unknown),
489238106Sdes			"unknown WSA error code %d", (int)err);
490238106Sdes		return unknown;
491238106Sdes	}
492238106Sdes}
493238106Sdes#endif /* USE_WINSOCK */
494