OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
[privoxy.git] / errlog.c
1 const char errlog_rcs[] = "$Id: errlog.c,v 1.20 2001/09/16 23:04:34 jongfoster Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/errlog.c,v $
5  *
6  * Purpose     :  Log errors to a designated destination in an elegant,
7  *                printf-like fashion.
8  *
9  * Copyright   :  Written by and Copyright (C) 2001 the SourceForge
10  *                IJBSWA team.  http://ijbswa.sourceforge.net
11  *
12  *                Based on the Internet Junkbuster originally written
13  *                by and Copyright (C) 1997 Anonymous Coders and 
14  *                Junkbusters Corporation.  http://www.junkbusters.com
15  *
16  *                This program is free software; you can redistribute it 
17  *                and/or modify it under the terms of the GNU General
18  *                Public License as published by the Free Software
19  *                Foundation; either version 2 of the License, or (at
20  *                your option) any later version.
21  *
22  *                This program is distributed in the hope that it will
23  *                be useful, but WITHOUT ANY WARRANTY; without even the
24  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
25  *                PARTICULAR PURPOSE.  See the GNU General Public
26  *                License for more details.
27  *
28  *                The GNU General Public License should be included with
29  *                this file.  If not, you can view it at
30  *                http://www.gnu.org/copyleft/gpl.html
31  *                or write to the Free Software Foundation, Inc., 59
32  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
33  *
34  * Revisions   :
35  *    $Log: errlog.c,v $
36  *    Revision 1.20  2001/09/16 23:04:34  jongfoster
37  *    Fixing a warning
38  *
39  *    Revision 1.19  2001/09/13 20:08:06  jongfoster
40  *    Adding support for LOG_LEVEL_CGI
41  *
42  *    Revision 1.18  2001/09/10 11:27:24  oes
43  *    Declaration of w32_socket_strerr now conditional
44  *
45  *    Revision 1.17  2001/09/10 10:17:13  oes
46  *    Removed unused variable; Fixed sprintf format
47  *
48  *    Revision 1.16  2001/07/30 22:08:36  jongfoster
49  *    Tidying up #defines:
50  *    - All feature #defines are now of the form FEATURE_xxx
51  *    - Permanently turned off WIN_GUI_EDIT
52  *    - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
53  *
54  *    Revision 1.15  2001/07/29 17:41:10  jongfoster
55  *    Now prints thread ID for each message (pthreads only)
56  *
57  *    Revision 1.14  2001/07/19 19:03:48  haroon
58  *    - Added case for LOG_LEVEL_POPUPS
59  *
60  *    Revision 1.13  2001/07/13 13:58:58  oes
61  *     - Added case for LOG_LEVEL_DEANIMATE
62  *     - Removed all #ifdef PCRS
63  *
64  *    Revision 1.12  2001/06/09 10:55:28  jongfoster
65  *    Changing BUFSIZ ==> BUFFER_SIZE
66  *
67  *    Revision 1.11  2001/06/01 18:14:49  jongfoster
68  *    Changing the calls to strerr() to check HAVE_STRERR (which is defined
69  *    in config.h if appropriate) rather than the NO_STRERR macro.
70  *
71  *    Revision 1.10  2001/05/29 11:52:21  oes
72  *    Conditional compilation of w32_socket_error
73  *
74  *    Revision 1.9  2001/05/28 16:15:17  jongfoster
75  *    Improved reporting of errors under Win32.
76  *
77  *    Revision 1.8  2001/05/26 17:25:14  jongfoster
78  *    Added support for CLF (Common Log Format) and fixed LOG_LEVEL_LOG
79  *
80  *    Revision 1.7  2001/05/26 15:21:28  jongfoster
81  *    Activity animation in Win32 GUI now works even if debug==0
82  *
83  *    Revision 1.6  2001/05/25 21:55:08  jongfoster
84  *    Now cleans up properly on FATAL (removes taskbar icon etc)
85  *
86  *    Revision 1.5  2001/05/22 18:46:04  oes
87  *
88  *    - Enabled filtering banners by size rather than URL
89  *      by adding patterns that replace all standard banner
90  *      sizes with the "Junkbuster" gif to the re_filterfile
91  *
92  *    - Enabled filtering WebBugs by providing a pattern
93  *      which kills all 1x1 images
94  *
95  *    - Added support for PCRE_UNGREEDY behaviour to pcrs,
96  *      which is selected by the (nonstandard and therefore
97  *      capital) letter 'U' in the option string.
98  *      It causes the quantifiers to be ungreedy by default.
99  *      Appending a ? turns back to greedy (!).
100  *
101  *    - Added a new interceptor ijb-send-banner, which
102  *      sends back the "Junkbuster" gif. Without imagelist or
103  *      MSIE detection support, or if tinygif = 1, or the
104  *      URL isn't recognized as an imageurl, a lame HTML
105  *      explanation is sent instead.
106  *
107  *    - Added new feature, which permits blocking remote
108  *      script redirects and firing back a local redirect
109  *      to the browser.
110  *      The feature is conditionally compiled, i.e. it
111  *      can be disabled with --disable-fast-redirects,
112  *      plus it must be activated by a "fast-redirects"
113  *      line in the config file, has its own log level
114  *      and of course wants to be displayed by show-proxy-args
115  *      Note: Boy, all the #ifdefs in 1001 locations and
116  *      all the fumbling with configure.in and acconfig.h
117  *      were *way* more work than the feature itself :-(
118  *
119  *    - Because a generic redirect template was needed for
120  *      this, tinygif = 3 now uses the same.
121  *
122  *    - Moved GIFs, and other static HTTP response templates
123  *      to project.h
124  *
125  *    - Some minor fixes
126  *
127  *    - Removed some >400 CRs again (Jon, you really worked
128  *      a lot! ;-)
129  *
130  *    Revision 1.4  2001/05/21 19:32:54  jongfoster
131  *    Added another #ifdef _WIN_CONSOLE
132  *
133  *    Revision 1.3  2001/05/20 01:11:40  jongfoster
134  *    Added support for LOG_LEVEL_FATAL
135  *    Renamed LOG_LEVEL_FRC to LOG_LEVEL_FORCE,
136  *    and LOG_LEVEL_REF to LOG_LEVEL_RE_FILTER
137  *
138  *    Revision 1.2  2001/05/17 22:42:01  oes
139  *     - Cleaned CRLF's from the sources and related files
140  *     - Repaired logging for REF and FRC
141  *
142  *    Revision 1.1.1.1  2001/05/15 13:58:51  oes
143  *    Initial import of version 2.9.3 source tree
144  *
145  *
146  *********************************************************************/
147 \f
148
149 #include "config.h"
150
151 #include <stdlib.h>
152 #include <stdio.h>
153 #include <stdarg.h>
154 #include <string.h>
155
156 #if !defined(_WIN32) && !defined(__OS2__)
157 #include <unistd.h>
158 #endif /* !defined(_WIN32) && !defined(__OS2__) */
159
160 #include <errno.h>
161 #include <assert.h>
162 #ifdef FEATURE_PTHREAD
163 #include <pthread.h>
164 #endif /* def FEATURE_PTHREAD */
165
166 #ifdef _WIN32
167 #include <windows.h>
168 #ifndef _WIN_CONSOLE
169 #include "w32log.h"
170 #endif /* ndef _WIN_CONSOLE */
171 #endif /* def _WIN32 */
172
173 #ifdef __OS2__
174 #define INCL_DOS
175 #include <os2.h>
176 #endif
177
178 #include "errlog.h"
179 #include "project.h"
180
181 const char errlog_h_rcs[] = ERRLOG_H_VERSION;
182
183
184 /*
185  * LOG_LEVEL_FATAL cannot be turned off.  (There are
186  * some exceptional situations where we need to get a
187  * message to the user).
188  */
189 #define LOG_LEVEL_MINIMUM  LOG_LEVEL_FATAL
190
191 /* where to log (default: stderr) */
192 static FILE *logfp = NULL;
193
194 /* logging detail level.  */
195 static int debug = (LOG_LEVEL_FATAL | LOG_LEVEL_ERROR | LOG_LEVEL_INFO);  
196
197 /* static functions */
198 static void fatal_error(const char * error_message);
199 #ifdef _WIN32
200 static char *w32_socket_strerr(int errcode, char *tmp_buf);
201 #endif
202
203 /*********************************************************************
204  *
205  * Function    :  fatal_error
206  *
207  * Description :  Displays a fatal error to standard error (or, on 
208  *                a WIN32 GUI, to a dialog box), and exits
209  *                JunkBuster with status code 1.
210  *
211  * Parameters  :
212  *          1  :  error_message = The error message to display.
213  *
214  * Returns     :  Does not return.
215  *
216  *********************************************************************/
217 static void fatal_error(const char * error_message)
218 {
219 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
220    MessageBox(g_hwndLogFrame, error_message, "Internet JunkBuster Error", 
221       MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);  
222
223    /* Cleanup - remove taskbar icon etc. */
224    TermLogWindow();
225
226 #else /* if !defined(_WIN32) || defined(_WIN_CONSOLE) */
227    fputs(error_message, stderr);
228 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
229
230    exit(1);
231 }
232
233
234 /*********************************************************************
235  *
236  * Function    :  init_errlog
237  *
238  * Description :  Initializes the logging module.  Must call before
239  *                calling log_error.
240  *
241  * Parameters  :
242  *          1  :  prog_name  = The program name.
243  *          2  :  logfname   = The logfile name, or NULL for stderr.
244  *          3  :  debuglevel = The debugging level.
245  *
246  * Returns     :  N/A
247  *
248  *********************************************************************/
249 void init_error_log(const char *prog_name, const char *logfname, int debuglevel)
250 {
251    FILE *fp;
252
253    /* FIXME RACE HAZARD: should start critical section error_log_use here */
254
255    /* set the logging detail level */
256    debug = debuglevel | LOG_LEVEL_MINIMUM;
257
258    if ((logfp != NULL) && (logfp != stderr))
259    {
260       fclose(logfp);
261    }
262    logfp = stderr;
263
264    /* set the designated log file */
265    if( logfname )
266    {
267       if( !(fp = fopen(logfname, "a")) )
268       {
269          log_error(LOG_LEVEL_FATAL, "init_errlog(): can't open logfile: %s", logfname);
270       }
271
272       /* set logging to be completely unbuffered */
273       setbuf(fp, NULL);
274
275       logfp = fp;
276    }
277
278    log_error(LOG_LEVEL_INFO, "Internet JunkBuster version " VERSION);
279    if (prog_name != NULL)
280    {
281       log_error(LOG_LEVEL_INFO, "Program name: %s", prog_name);
282    }
283
284    /* FIXME RACE HAZARD: should end critical section error_log_use here */
285
286 } /* init_error_log */
287
288
289 /*********************************************************************
290  *
291  * Function    :  log_error
292  *
293  * Description :  This is the error-reporting and logging function.
294  *
295  * Parameters  :
296  *          1  :  loglevel  = the type of message to be logged
297  *          2  :  fmt       = the main string we want logged, printf-like
298  *          3  :  ...       = arguments to be inserted in fmt (printf-like).
299  *
300  * Returns     :  N/A
301  *
302  *********************************************************************/
303 void log_error(int loglevel, char *fmt, ...)
304 {
305    va_list ap;
306    char *outbuf= NULL;
307    char * src = fmt;
308    int outc = 0;
309    long this_thread = 1;  /* was: pthread_t this_thread;*/
310 #ifdef __OS2__
311    PTIB     ptib;
312    APIRET   ulrc;
313 #endif /* __OS2__ */
314
315 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
316    /*
317     * Irrespective of debug setting, a GET/POST/CONNECT makes
318     * the taskbar icon animate.  (There is an option to disable
319     * this but checking that is handled inside LogShowActivity()).
320     */
321    if (loglevel == LOG_LEVEL_GPC)
322    {
323       LogShowActivity();
324    }
325 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
326
327    /* verify if loglevel applies to current settings and bail out if negative */
328    if ((loglevel & debug) == 0)
329    {
330       return;
331    }
332
333    /* FIXME get current thread id */
334 #ifdef FEATURE_PTHREAD
335    this_thread = (long)pthread_self();
336 #elif __OS2__
337   
338    ulrc = DosGetInfoBlocks(&ptib, NULL);
339    if (ulrc == 0)
340      this_thread = ptib -> tib_ptib2 -> tib2_ultid;
341 #endif /* def FEATURE_PTHREAD */
342
343    outbuf = (char*)malloc(BUFFER_SIZE);
344    assert(outbuf);
345    switch (loglevel)
346    {
347       case LOG_LEVEL_ERROR:
348          outc = sprintf(outbuf, "IJB(%ld) Error: ", this_thread);
349          break;
350       case LOG_LEVEL_FATAL:
351          outc = sprintf(outbuf, "IJB(%ld) Fatal error: ", this_thread);
352          break;
353       case LOG_LEVEL_GPC:
354          outc = sprintf(outbuf, "IJB(%ld) Request: ", this_thread);
355          break;
356       case LOG_LEVEL_CONNECT:
357          outc = sprintf(outbuf, "IJB(%ld) Connect: ", this_thread);
358          break;
359       case LOG_LEVEL_LOG:
360          outc = sprintf(outbuf, "IJB(%ld) Writing: ", this_thread);
361          break;
362       case LOG_LEVEL_HEADER:
363          outc = sprintf(outbuf, "IJB(%ld) Header: ", this_thread);
364          break;
365       case LOG_LEVEL_INFO:
366          outc = sprintf(outbuf, "IJB(%ld) Info: ", this_thread);
367          break;
368       case LOG_LEVEL_RE_FILTER:
369          outc = sprintf(outbuf, "IJB(%ld) Re-Filter: ", this_thread);
370          break;
371 #ifdef FEATURE_FORCE_LOAD
372       case LOG_LEVEL_FORCE:
373          outc = sprintf(outbuf, "IJB(%ld) Force: ", this_thread);
374          break;
375 #endif /* def FEATURE_FORCE_LOAD */
376 #ifdef FEATURE_FAST_REDIRECTS
377       case LOG_LEVEL_REDIRECTS:
378          outc = sprintf(outbuf, "IJB(%ld) Redirect: ", this_thread);
379          break;
380 #endif /* def FEATURE_FAST_REDIRECTS */
381       case LOG_LEVEL_DEANIMATE:
382          outc = sprintf(outbuf, "IJB(%ld) Gif-Deanimate: ", this_thread);
383          break;
384       case LOG_LEVEL_CLF:
385          outc = 0;
386          outbuf[0] = '\0';
387          break;
388 #ifdef FEATURE_KILL_POPUPS
389       case LOG_LEVEL_POPUPS:
390          outc = sprintf(outbuf, "IJB(%ld) Kill-Popups: ", this_thread);
391          break;
392 #endif /* def FEATURE_KILL_POPUPS */
393       case LOG_LEVEL_CGI:
394          outc = sprintf(outbuf, "IJB(%ld) CGI: ", this_thread);
395          break;
396       default:
397          outc = sprintf(outbuf, "IJB(%ld) UNKNOWN LOG TYPE(%d): ", this_thread, loglevel);
398          break;
399    }
400    
401    /* get ready to scan var. args. */
402    va_start( ap, fmt );
403
404    /* build formatted message from fmt and var-args */
405    while ((*src) && (outc < BUFFER_SIZE-2))
406    {
407       char tempbuf[BUFFER_SIZE];
408       char *sval;
409       int ival;
410       unsigned uval;
411       long lval;
412       unsigned long ulval;
413       int oldoutc;
414       char ch;
415       
416       ch = *src++;
417       if( ch != '%' )
418       {
419          outbuf[outc++] = ch;
420          continue;
421       }
422
423       ch = *src++;
424       switch (ch) {
425          case '%':
426             outbuf[outc++] = '%';
427             break;
428          case 'd':
429             ival = va_arg( ap, int );
430             oldoutc = outc;
431             outc += sprintf(tempbuf, "%d", ival);
432             if (outc < BUFFER_SIZE-1) 
433             {
434                strcpy(outbuf + oldoutc, tempbuf);
435             }
436             else
437             {
438                outbuf[oldoutc] = '\0';
439             }
440             break;
441          case 'u':
442             uval = va_arg( ap, unsigned );
443             oldoutc = outc;
444             outc += sprintf(tempbuf, "%u", uval);
445             if (outc < BUFFER_SIZE-1) 
446             {
447                strcpy(outbuf + oldoutc, tempbuf);
448             }
449             else
450             {
451                outbuf[oldoutc] = '\0';
452             }
453             break;
454          case 'l':
455             /* this is a modifier that must be followed by u or d */
456             ch = *src++;
457             if (ch == 'd')
458             {
459                lval = va_arg( ap, long );
460                oldoutc = outc;
461                outc += sprintf(tempbuf, "%ld", lval);
462             }
463             else if (ch == 'u')
464             {
465                ulval = va_arg( ap, unsigned long );
466                oldoutc = outc;
467                outc += sprintf(tempbuf, "%lu", ulval);
468             }
469             else
470             {
471                /* Error */
472                sprintf(outbuf, "IJB(%ld) Error: log_error(): Bad format string:\n"
473                                "Format = \"%s\"\n"
474                                "Exiting.", this_thread, fmt);
475                /* FIXME RACE HAZARD: should start critical section error_log_use here */
476                if( !logfp )
477                {
478                   logfp = stderr;
479                }
480                fputs(outbuf, logfp);
481                /* FIXME RACE HAZARD: should end critical section error_log_use here */
482                fatal_error(outbuf);
483                /* Never get here */
484                break;
485             }
486             if (outc < BUFFER_SIZE-1) 
487             {
488                strcpy(outbuf + oldoutc, tempbuf);
489             }
490             else
491             {
492                outbuf[oldoutc] = '\0';
493             }
494             break;
495          case 'c':
496             /*
497              * Note that char paramaters are converted to int, so we need to
498              * pass "int" to va_arg.  (See K&R, 2nd ed, section A7.3.2, page 202)
499              */
500             outbuf[outc++] = (char) va_arg( ap, int );
501             break;
502          case 's':
503             sval = va_arg( ap, char * );
504             oldoutc = outc;
505             outc += strlen(sval);
506             if (outc < BUFFER_SIZE-1) 
507             {
508                strcpy(outbuf + oldoutc, sval);
509             }
510             else
511             {
512                outbuf[oldoutc] = '\0';
513             }
514             break;
515          case 'N':
516             /* Non-standard: Print a counted string.  Takes 2 parameters:
517              * int length, const char * string
518              */
519             ival = va_arg( ap, int );
520             sval = va_arg( ap, char * );
521             if (ival < 0)
522             {
523                ival = 0;
524             }
525             oldoutc = outc;
526             outc += ival;
527             if (outc < BUFFER_SIZE-1)
528             {
529                memcpy(outbuf + oldoutc, sval, ival);
530             }
531             else
532             {
533                outbuf[oldoutc] = '\0';
534             }
535             break;
536          case 'E':
537             /* Non-standard: Print error code from errno */
538 #ifdef _WIN32
539             ival = WSAGetLastError();
540             sval = w32_socket_strerr(ival, tempbuf);
541 #else /* ifndef _WIN32 */
542             ival = errno; 
543 #ifdef HAVE_STRERROR
544             sval = strerror(ival);
545 #else /* ifndef HAVE_STRERROR */
546             sval = NULL;
547 #endif /* ndef HAVE_STRERROR */
548             if (sval == NULL)
549             {
550                sprintf(tempbuf, "(errno = %d)", ival);
551                sval = tempbuf;
552             }
553 #endif /* ndef _WIN32 */
554             oldoutc = outc;
555             outc += strlen(sval);
556             if (outc < BUFFER_SIZE-1) 
557             {
558                strcpy(outbuf + oldoutc, sval);
559             }
560             else
561             {
562                outbuf[oldoutc] = '\0';
563             }
564             break;
565          case 'T':
566             /* Non-standard: Print a Common Log File timestamp */
567             {
568                /*
569                 * Write timestamp into tempbuf.
570                 *
571                 * Complex because not all OSs have tm_gmtoff or
572                 * the %z field in strftime()
573                 */
574                time_t now; 
575                struct tm *tm_now; 
576                struct tm gmt; 
577                int days, hrs, mins; 
578                time (&now); 
579                gmt = *gmtime (&now); 
580                tm_now = localtime (&now); 
581                days = tm_now->tm_yday - gmt.tm_yday; 
582                hrs = ((days < -1 ? 24 : 1 < days ? -24 : days * 24) + tm_now->tm_hour - gmt.tm_hour); 
583                mins = hrs * 60 + tm_now->tm_min - gmt.tm_min; 
584                strftime (tempbuf, BUFFER_SIZE-6, "%d/%b/%Y:%H:%M:%S ", tm_now); 
585                sprintf (tempbuf + strlen(tempbuf), "%+03d%02d", mins / 60, abs(mins) % 60); 
586             }
587             oldoutc = outc;
588             outc += strlen(tempbuf);
589             if (outc < BUFFER_SIZE-1) 
590             {
591                strcpy(outbuf + oldoutc, tempbuf);
592             }
593             else
594             {
595                outbuf[oldoutc] = '\0';
596             }
597             break;
598          default:
599             sprintf(outbuf, "IJB(%ld) Error: log_error(): Bad format string:\n"
600                             "Format = \"%s\"\n"
601                             "Exiting.", this_thread, fmt);
602             /* FIXME RACE HAZARD: should start critical section error_log_use here */
603             if( !logfp )
604             {
605                logfp = stderr;
606             }
607             fputs(outbuf, logfp);
608             /* FIXME RACE HAZARD: should end critical section error_log_use here */
609             fatal_error(outbuf);
610             /* Never get here */
611             break;
612
613       } /* switch( p ) */
614
615    } /* for( p ... ) */
616    
617    /* done with var. args */
618    va_end( ap );
619    
620    if (outc >= BUFFER_SIZE-2)
621    {
622       /* insufficient room for newline and trailing null. */
623
624       static const char warning[] = "... [too long, truncated]\n";
625
626       if (outc < BUFFER_SIZE)
627       {
628          /* Need to add terminating null in this case. */
629          outbuf[outc] = '\0';
630       }
631
632       /* Truncate output */
633       outbuf[BUFFER_SIZE - sizeof(warning)] = '\0';
634
635       /* Append warning */
636       strcat(outbuf, warning);
637    }
638    else
639    {
640       /* Add terminating newline and null */
641       outbuf[outc++] = '\n';
642       outbuf[outc] = '\0';
643    }
644
645    /* FIXME RACE HAZARD: should start critical section error_log_use here */
646
647    /* deal with glibc stupidity - it won't let you initialize logfp */
648    if( !logfp )
649    {
650       logfp = stderr;
651    }
652
653    fputs(outbuf, logfp);
654
655    if (loglevel == LOG_LEVEL_FATAL)
656    {
657       fatal_error(outbuf);
658       /* Never get here */
659    }
660
661    /* FIXME RACE HAZARD: should end critical section error_log_use here */
662
663 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
664    /* Write to display */
665    LogPutString(outbuf);
666 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
667
668 }
669
670
671 #ifdef _WIN32
672 /*********************************************************************
673  *
674  * Function    :  w32_socket_strerr
675  *
676  * Description :  Translate the return value from WSAGetLastError()
677  *                into a string.
678  *
679  * Parameters  :
680  *          1  :  errcode = The return value from WSAGetLastError().
681  *          2  :  tmp_buf = A temporary buffer that might be used to
682  *                          store the string.
683  *
684  * Returns     :  String representing the error code.  This may be
685  *                a global string constant or a string stored in
686  *                tmp_buf.
687  *
688  *********************************************************************/
689 static char *w32_socket_strerr(int errcode, char *tmp_buf)
690 {
691 #define TEXT_FOR_ERROR(code,text) \
692    if (errcode == code)           \
693    {                              \
694       return #code " - " text;    \
695    }
696
697    TEXT_FOR_ERROR(WSAEACCES, "Permission denied")
698    TEXT_FOR_ERROR(WSAEADDRINUSE, "Address already in use.")
699    TEXT_FOR_ERROR(WSAEADDRNOTAVAIL, "Cannot assign requested address.");
700    TEXT_FOR_ERROR(WSAEAFNOSUPPORT, "Address family not supported by protocol family.");
701    TEXT_FOR_ERROR(WSAEALREADY, "Operation already in progress.");
702    TEXT_FOR_ERROR(WSAECONNABORTED, "Software caused connection abort.");
703    TEXT_FOR_ERROR(WSAECONNREFUSED, "Connection refused.");
704    TEXT_FOR_ERROR(WSAECONNRESET, "Connection reset by peer.");
705    TEXT_FOR_ERROR(WSAEDESTADDRREQ, "Destination address required.");
706    TEXT_FOR_ERROR(WSAEFAULT, "Bad address.");
707    TEXT_FOR_ERROR(WSAEHOSTDOWN, "Host is down.");
708    TEXT_FOR_ERROR(WSAEHOSTUNREACH, "No route to host.");
709    TEXT_FOR_ERROR(WSAEINPROGRESS, "Operation now in progress.");
710    TEXT_FOR_ERROR(WSAEINTR, "Interrupted function call.");
711    TEXT_FOR_ERROR(WSAEINVAL, "Invalid argument.");
712    TEXT_FOR_ERROR(WSAEISCONN, "Socket is already connected.");
713    TEXT_FOR_ERROR(WSAEMFILE, "Too many open sockets.");
714    TEXT_FOR_ERROR(WSAEMSGSIZE, "Message too long.");
715    TEXT_FOR_ERROR(WSAENETDOWN, "Network is down.");
716    TEXT_FOR_ERROR(WSAENETRESET, "Network dropped connection on reset.");
717    TEXT_FOR_ERROR(WSAENETUNREACH, "Network is unreachable.");
718    TEXT_FOR_ERROR(WSAENOBUFS, "No buffer space available.");
719    TEXT_FOR_ERROR(WSAENOPROTOOPT, "Bad protocol option.");
720    TEXT_FOR_ERROR(WSAENOTCONN, "Socket is not connected.");
721    TEXT_FOR_ERROR(WSAENOTSOCK, "Socket operation on non-socket.");
722    TEXT_FOR_ERROR(WSAEOPNOTSUPP, "Operation not supported.");
723    TEXT_FOR_ERROR(WSAEPFNOSUPPORT, "Protocol family not supported.");
724    TEXT_FOR_ERROR(WSAEPROCLIM, "Too many processes.");
725    TEXT_FOR_ERROR(WSAEPROTONOSUPPORT, "Protocol not supported.");
726    TEXT_FOR_ERROR(WSAEPROTOTYPE, "Protocol wrong type for socket.");
727    TEXT_FOR_ERROR(WSAESHUTDOWN, "Cannot send after socket shutdown.");
728    TEXT_FOR_ERROR(WSAESOCKTNOSUPPORT, "Socket type not supported.");
729    TEXT_FOR_ERROR(WSAETIMEDOUT, "Connection timed out.");
730    TEXT_FOR_ERROR(WSAEWOULDBLOCK, "Resource temporarily unavailable.");
731    TEXT_FOR_ERROR(WSAHOST_NOT_FOUND, "Host not found.");
732    TEXT_FOR_ERROR(WSANOTINITIALISED, "Successful WSAStartup not yet performed.");
733    TEXT_FOR_ERROR(WSANO_DATA, "Valid name, no data record of requested type.");
734    TEXT_FOR_ERROR(WSANO_RECOVERY, "This is a non-recoverable error.");
735    TEXT_FOR_ERROR(WSASYSNOTREADY, "Network subsystem is unavailable.");
736    TEXT_FOR_ERROR(WSATRY_AGAIN, "Non-authoritative host not found.");
737    TEXT_FOR_ERROR(WSAVERNOTSUPPORTED, "WINSOCK.DLL version out of range.");
738    TEXT_FOR_ERROR(WSAEDISCON, "Graceful shutdown in progress.");
739    /*
740     * The following error codes are documented in the Microsoft WinSock
741     * reference guide, but don't actually exist.
742     *
743     * TEXT_FOR_ERROR(WSA_INVALID_HANDLE, "Specified event object handle is invalid.");
744     * TEXT_FOR_ERROR(WSA_INVALID_PARAMETER, "One or more parameters are invalid.");
745     * TEXT_FOR_ERROR(WSAINVALIDPROCTABLE, "Invalid procedure table from service provider.");
746     * TEXT_FOR_ERROR(WSAINVALIDPROVIDER, "Invalid service provider version number.");
747     * TEXT_FOR_ERROR(WSA_IO_PENDING, "Overlapped operations will complete later.");
748     * TEXT_FOR_ERROR(WSA_IO_INCOMPLETE, "Overlapped I/O event object not in signaled state.");
749     * TEXT_FOR_ERROR(WSA_NOT_ENOUGH_MEMORY, "Insufficient memory available.");
750     * TEXT_FOR_ERROR(WSAPROVIDERFAILEDINIT, "Unable to initialize a service provider.");
751     * TEXT_FOR_ERROR(WSASYSCALLFAILURE, "System call failure.");
752     * TEXT_FOR_ERROR(WSA_OPERATION_ABORTED, "Overlapped operation aborted.");
753     */
754
755    sprintf(tmp_buf, "(error number %d)", errcode);
756    return tmp_buf;
757 }
758 #endif /* def _WIN32 */
759
760
761 /*
762   Local Variables:
763   tab-width: 3
764   end:
765 */