1 /* w32sock.h --- internal auxiliary functions for Windows socket functions
2 
3    Copyright (C) 2008-2021 Free Software Foundation, Inc.
4 
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
17 
18 /* Written by Paolo Bonzini */
19 
20 #include <errno.h>
21 
22 /* Get O_RDWR and O_BINARY.  */
23 #include <fcntl.h>
24 
25 /* Get _open_osfhandle().  */
26 #include <io.h>
27 
28 /* Get _get_osfhandle().  */
29 #if GNULIB_MSVC_NOTHROW
30 # include "msvc-nothrow.h"
31 #else
32 # include <io.h>
33 #endif
34 
35 #define FD_TO_SOCKET(fd)   ((SOCKET) _get_osfhandle ((fd)))
36 #define SOCKET_TO_FD(fh)   (_open_osfhandle ((intptr_t) (fh), O_RDWR | O_BINARY))
37 
38 static inline void
set_winsock_errno(void)39 set_winsock_errno (void)
40 {
41   int err = WSAGetLastError ();
42 
43   /* Map some WSAE* errors to the runtime library's error codes.  */
44   switch (err)
45     {
46     case WSA_INVALID_HANDLE:
47       errno = EBADF;
48       break;
49     case WSA_NOT_ENOUGH_MEMORY:
50       errno = ENOMEM;
51       break;
52     case WSA_INVALID_PARAMETER:
53       errno = EINVAL;
54       break;
55     case WSAENAMETOOLONG:
56       errno = ENAMETOOLONG;
57       break;
58     case WSAENOTEMPTY:
59       errno = ENOTEMPTY;
60       break;
61     case WSAEWOULDBLOCK:
62       errno = EWOULDBLOCK;
63       break;
64     case WSAEINPROGRESS:
65       errno = EINPROGRESS;
66       break;
67     case WSAEALREADY:
68       errno = EALREADY;
69       break;
70     case WSAENOTSOCK:
71       errno = ENOTSOCK;
72       break;
73     case WSAEDESTADDRREQ:
74       errno = EDESTADDRREQ;
75       break;
76     case WSAEMSGSIZE:
77       errno = EMSGSIZE;
78       break;
79     case WSAEPROTOTYPE:
80       errno = EPROTOTYPE;
81       break;
82     case WSAENOPROTOOPT:
83       errno = ENOPROTOOPT;
84       break;
85     case WSAEPROTONOSUPPORT:
86       errno = EPROTONOSUPPORT;
87       break;
88     case WSAEOPNOTSUPP:
89       errno = EOPNOTSUPP;
90       break;
91     case WSAEAFNOSUPPORT:
92       errno = EAFNOSUPPORT;
93       break;
94     case WSAEADDRINUSE:
95       errno = EADDRINUSE;
96       break;
97     case WSAEADDRNOTAVAIL:
98       errno = EADDRNOTAVAIL;
99       break;
100     case WSAENETDOWN:
101       errno = ENETDOWN;
102       break;
103     case WSAENETUNREACH:
104       errno = ENETUNREACH;
105       break;
106     case WSAENETRESET:
107       errno = ENETRESET;
108       break;
109     case WSAECONNABORTED:
110       errno = ECONNABORTED;
111       break;
112     case WSAECONNRESET:
113       errno = ECONNRESET;
114       break;
115     case WSAENOBUFS:
116       errno = ENOBUFS;
117       break;
118     case WSAEISCONN:
119       errno = EISCONN;
120       break;
121     case WSAENOTCONN:
122       errno = ENOTCONN;
123       break;
124     case WSAETIMEDOUT:
125       errno = ETIMEDOUT;
126       break;
127     case WSAECONNREFUSED:
128       errno = ECONNREFUSED;
129       break;
130     case WSAELOOP:
131       errno = ELOOP;
132       break;
133     case WSAEHOSTUNREACH:
134       errno = EHOSTUNREACH;
135       break;
136     default:
137       errno = (err > 10000 && err < 10025) ? err - 10000 : err;
138       break;
139     }
140 }
141