xref: /openbsd-src/gnu/usr.bin/cvs/windows-NT/sockerror.c (revision b6c0222275d7627ee4b3daf0c986f90dee05daa5)
1*b6c02222Stholo /* sockerror.c --- convert WinSock error number to string
2*b6c02222Stholo    Vince Del Vecchio <vdelvecc@spd.analog.com>
3*b6c02222Stholo 
4*b6c02222Stholo    This file is part of GNU CVS.
5*b6c02222Stholo 
6*b6c02222Stholo    GNU CVS is free software; you can redistribute it and/or modify it
7*b6c02222Stholo    under the terms of the GNU General Public License as published by the
8*b6c02222Stholo    Free Software Foundation; either version 2, or (at your option) any
9*b6c02222Stholo    later version.
10*b6c02222Stholo 
11*b6c02222Stholo    This program is distributed in the hope that it will be useful,
12*b6c02222Stholo    but WITHOUT ANY WARRANTY; without even the implied warranty of
13*b6c02222Stholo    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*b6c02222Stholo    GNU General Public License for more details.  */
15*b6c02222Stholo 
16*b6c02222Stholo #include <stdio.h>
17*b6c02222Stholo #include <winsock.h>
18*b6c02222Stholo 
19*b6c02222Stholo struct err_strs {
20*b6c02222Stholo     char **strs;
21*b6c02222Stholo     int first;
22*b6c02222Stholo     int last;
23*b6c02222Stholo };
24*b6c02222Stholo 
25*b6c02222Stholo static char *errs1[] = {
26*b6c02222Stholo     /* EINTR		*/ "Interrupted system call"
27*b6c02222Stholo };
28*b6c02222Stholo 
29*b6c02222Stholo static char *errs2[] = {
30*b6c02222Stholo     /* EBADF		*/ "Bad file descriptor"
31*b6c02222Stholo };
32*b6c02222Stholo 
33*b6c02222Stholo static char *errs3[] = {
34*b6c02222Stholo     /* EACCES		*/ "Permission denied",
35*b6c02222Stholo     /* EFAULT		*/ "Bad address"
36*b6c02222Stholo };
37*b6c02222Stholo 
38*b6c02222Stholo static char *errs4[] = {
39*b6c02222Stholo     /* EINVAL		*/ "Invalid argument"
40*b6c02222Stholo };
41*b6c02222Stholo 
42*b6c02222Stholo static char *errs5[] = {
43*b6c02222Stholo     /* EMFILE		*/ "Too many open files",
44*b6c02222Stholo };
45*b6c02222Stholo 
46*b6c02222Stholo static char *errs6[] = {
47*b6c02222Stholo     /* EWOULDBLOCK	*/ "Resource temporarily unavailable",
48*b6c02222Stholo     /* EINPROGRESS	*/ "Operation now in progress",
49*b6c02222Stholo     /* EALREADY		*/ "Operation already in progress",
50*b6c02222Stholo     /* ENOTSOCK		*/ "Socket operation on non-socket",
51*b6c02222Stholo     /* EDESTADDRREQ	*/ "Destination address required",
52*b6c02222Stholo     /* EMSGSIZE		*/ "Message too long",
53*b6c02222Stholo     /* EPROTOTYPE	*/ "Protocol wrong type for socket",
54*b6c02222Stholo     /* ENOPROTOOPT	*/ "Protocol not available",
55*b6c02222Stholo     /* EPROTONOSUPPORT	*/ "Protocol not supported",
56*b6c02222Stholo     /* ESOCKTNOSUPPORT	*/ "Socket type not supported",
57*b6c02222Stholo     /* EOPNOTSUPP	*/ "Operation not supported on socket",
58*b6c02222Stholo     /* EPFNOSUPPORT	*/ "Protocol family not supported",
59*b6c02222Stholo     /* EAFNOSUPPORT	*/ "Address family not supported by protocol",
60*b6c02222Stholo     /* EADDRINUSE	*/ "Address already in use",
61*b6c02222Stholo     /* EADDRNOTAVAIL	*/ "Can't assign requested address",
62*b6c02222Stholo     /* ENETDOWN		*/ "Network is down",
63*b6c02222Stholo     /* ENETUNREACH	*/ "Network is unreachable",
64*b6c02222Stholo     /* ENETRESET	*/ "Network connection dropped on reset",
65*b6c02222Stholo     /* ECONNABORTED	*/ "Software caused connection abort",
66*b6c02222Stholo     /* ECONNRESET	*/ "Connection reset by peer",
67*b6c02222Stholo     /* ENOBUFS		*/ "No buffer space available",
68*b6c02222Stholo     /* EISCONN		*/ "Socket is already connected",
69*b6c02222Stholo     /* ENOTCONN		*/ "Socket is not connected",
70*b6c02222Stholo     /* ESHUTDOWN	*/ "Can't send after socket shutdown",
71*b6c02222Stholo     /* ETOOMANYREFS	*/ "Too many references: can't splice",
72*b6c02222Stholo     /* ETIMEDOUT	*/ "Connection timed out",
73*b6c02222Stholo     /* ECONNREFUSED	*/ "Connection refused",
74*b6c02222Stholo     /* ELOOP		*/ "Too many levels of symbolic links",
75*b6c02222Stholo     /* ENAMETOOLONG	*/ "File name too long",
76*b6c02222Stholo     /* EHOSTDOWN	*/ "Host is down",
77*b6c02222Stholo     /* EHOSTUNREACH	*/ "No route to host",
78*b6c02222Stholo     /* ENOTEMPTY	*/ "Directory not empty",
79*b6c02222Stholo     /* EPROCLIM		*/ "Too many processes",
80*b6c02222Stholo     /* EUSERS		*/ "Too many users",
81*b6c02222Stholo     /* EDQUOT		*/ "Disc quota exceeded",
82*b6c02222Stholo     /* ESTALE		*/ "Stale NFS file handle",
83*b6c02222Stholo     /* EREMOTE		*/ "Object is remote"
84*b6c02222Stholo };
85*b6c02222Stholo 
86*b6c02222Stholo static char *errs7[] = {
87*b6c02222Stholo     /* SYSNOTREADY	*/ "Network subsystem unavailable",
88*b6c02222Stholo     /* VERNOTSUPPORTED	*/ "Requested WinSock version not supported",
89*b6c02222Stholo     /* NOTINITIALISED	*/ "WinSock was not initialized"
90*b6c02222Stholo };
91*b6c02222Stholo 
92*b6c02222Stholo #ifdef WSAEDISCON
93*b6c02222Stholo static char *errs8[] = {
94*b6c02222Stholo     /* EDISCON		*/ "Graceful shutdown in progress"
95*b6c02222Stholo };
96*b6c02222Stholo #endif
97*b6c02222Stholo 
98*b6c02222Stholo static char *errs9[] = {
99*b6c02222Stholo     /* HOST_NOT_FOUND	*/ "Unknown host",
100*b6c02222Stholo     /* TRY_AGAIN	*/ "Host name lookup failure",
101*b6c02222Stholo     /* NO_RECOVERY	*/ "Unknown server error",
102*b6c02222Stholo     /* NO_DATA		*/ "No address associated with name",
103*b6c02222Stholo };
104*b6c02222Stholo 
105*b6c02222Stholo /* Some of these errors are defined in the winsock.h header file I have,
106*b6c02222Stholo    but not in the Winsock 1.1 spec.  I include them some of them anyway,
107*b6c02222Stholo    where it is not too hard to avoid referencing the symbolic constant. */
108*b6c02222Stholo 
109*b6c02222Stholo static struct err_strs sock_errlist[] = {
110*b6c02222Stholo     { errs1,	WSAEINTR,	WSAEINTR },
111*b6c02222Stholo     { errs2,	WSAEBADF,	WSAEBADF },
112*b6c02222Stholo     { errs3,	WSAEACCES,	WSAEFAULT },
113*b6c02222Stholo     { errs4,	WSAEINVAL,	WSAEINVAL },
114*b6c02222Stholo     { errs5,	WSAEMFILE,	WSAEMFILE },
115*b6c02222Stholo     { errs6,	WSAEWOULDBLOCK, WSAEHOSTUNREACH + 6 },
116*b6c02222Stholo     { errs7,	WSASYSNOTREADY,	WSANOTINITIALISED },
117*b6c02222Stholo #ifdef WSAEDISCON
118*b6c02222Stholo     { errs8,	WSAEDISCON,	WSAEDISCON },
119*b6c02222Stholo #endif
120*b6c02222Stholo     { errs9,	WSAHOST_NOT_FOUND, WSANO_DATA }
121*b6c02222Stholo };
122*b6c02222Stholo 
123*b6c02222Stholo char *
sock_strerror(int errnum)124*b6c02222Stholo sock_strerror (int errnum)
125*b6c02222Stholo {
126*b6c02222Stholo     static char buf[40];
127*b6c02222Stholo     int i;
128*b6c02222Stholo 
129*b6c02222Stholo     for (i = 0; i < (sizeof sock_errlist / sizeof *sock_errlist); i++)
130*b6c02222Stholo     {
131*b6c02222Stholo 	if (errnum >= sock_errlist[i].first && errnum <= sock_errlist[i].last)
132*b6c02222Stholo 	    return sock_errlist[i].strs[errnum - sock_errlist[i].first];
133*b6c02222Stholo     }
134*b6c02222Stholo     sprintf(buf, "Unknown socket error: %d", errnum);
135*b6c02222Stholo     return buf;
136*b6c02222Stholo }
137