libiio: utils-windows.h Source File

libiio 1.0 Library for interfacing with IIO devices

Loading...
Searching...
No Matches
utils-windows.h
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2/*
3 * libiio - Library for interfacing industrial I/O (IIO) devices
4 *
5 * Copyright (C) 2014-2025 Analog Devices, Inc.
6 * Author: Dan Nechita <dan.nechita@analog.com>
7 */
8
9#ifndef __IIO_UTILS_WINDOWS_H
10#define __IIO_UTILS_WINDOWS_H
11
12#include <winsock2.h>
13#include <errno.h>
14
30static inline int translate_wsa_error_to_posix(int wsa_err)
31{
32 switch (wsa_err) {
33 /* Generic Windows/WSA errors */
34 case WSA_INVALID_HANDLE:
35 return -EBADF; // Invalid handle, closest equivalent
36 case WSA_NOT_ENOUGH_MEMORY:
37 return -ENOMEM;
38 case WSA_INVALID_PARAMETER:
39 return -EINVAL;
40 case WSA_OPERATION_ABORTED:
41#ifdef ECANCELED
42 return -ECANCELED; // Operation was aborted
43#else
44 return -EINTR; // Fallback to interrupted system call
45#endif
46 case WSA_IO_PENDING:
47 return -EINPROGRESS; // Operation will complete later
48 /* Standard socket errors */
49 case WSAEACCES:
50 return -EACCES;
51 case WSAEADDRINUSE:
52 return -EADDRINUSE;
53 case WSAEADDRNOTAVAIL:
54 return -EADDRNOTAVAIL;
55 case WSAEAFNOSUPPORT:
56 return -EAFNOSUPPORT;
57 case WSAEALREADY:
58 return -EALREADY;
59 case WSAEBADF:
60 return -EBADF;
61 case WSAECONNREFUSED:
62 return -ECONNREFUSED;
63 case WSAECONNRESET:
64 return -ECONNRESET;
65 case WSAECONNABORTED:
66 return -ECONNABORTED;
67 case WSAEDESTADDRREQ:
68 return -EDESTADDRREQ;
69 case WSAEDISCON:
70 return -ECONNRESET; // Graceful shutdown, closest equivalent
71#ifdef EDQUOT
72 case WSAEDQUOT:
73 return -EDQUOT;
74#else
75 case WSAEDQUOT:
76 return -ENOSPC; // Fallback to "no space left on device"
77#endif
78 case WSAEFAULT:
79 return -EFAULT;
80#ifdef EHOSTDOWN
81 case WSAEHOSTDOWN:
82 return -EHOSTDOWN;
83#else
84 case WSAEHOSTDOWN:
85 return -ENETUNREACH; // Fallback to network unreachable
86#endif
87 case WSAEHOSTUNREACH:
88 return -EHOSTUNREACH;
89 case WSAEINPROGRESS:
90 return -EINPROGRESS;
91 case WSAEINTR:
92 return -EINTR;
93 case WSAEINVAL:
94 return -EINVAL;
95 case WSAEISCONN:
96 return -EISCONN;
97 case WSAELOOP:
98 return -ELOOP;
99 case WSAEMFILE:
100 return -EMFILE;
101 case WSAEMSGSIZE:
102 return -EMSGSIZE;
103 case WSAENAMETOOLONG:
104 return -ENAMETOOLONG;
105 case WSAENETDOWN:
106 return -ENETDOWN;
107 case WSAENETRESET:
108 return -ENETRESET;
109 case WSAENETUNREACH:
110 return -ENETUNREACH;
111 case WSAENOBUFS:
112 return -ENOBUFS;
113 case WSAENOPROTOOPT:
114 return -ENOPROTOOPT;
115 case WSAENOTEMPTY:
116 return -ENOTEMPTY;
117 case WSAENOTSOCK:
118 return -ENOTSOCK;
119 case WSAENOTCONN:
120 return -ENOTCONN;
121 case WSAEOPNOTSUPP:
122 return -EOPNOTSUPP;
123#ifdef EPFNOSUPPORT
124 case WSAEPFNOSUPPORT:
125 return -EPFNOSUPPORT;
126#else
127 case WSAEPFNOSUPPORT:
128 return -EAFNOSUPPORT; // Fallback to address family not supported
129#endif
130 case WSAEPROCLIM:
131 return -EAGAIN; // Too many processes, closest equivalent
132 case WSAEPROTONOSUPPORT:
133 return -EPROTONOSUPPORT;
134 case WSAEPROTOTYPE:
135 return -EPROTOTYPE;
136#ifdef EREMOTE
137 case WSAEREMOTE:
138 return -EREMOTE;
139#else
140 case WSAEREMOTE:
141 return -EIO; // Fallback to I/O error
142#endif
143#ifdef ESHUTDOWN
144 case WSAESHUTDOWN:
145 return -ESHUTDOWN;
146#else
147 case WSAESHUTDOWN:
148 return -ECONNABORTED;
149#endif
150#ifdef ESOCKTNOSUPPORT
151 case WSAESOCKTNOSUPPORT:
152 return -ESOCKTNOSUPPORT;
153#else
154 case WSAESOCKTNOSUPPORT:
155 return -EPROTONOSUPPORT; // Fallback to protocol not supported
156#endif
157#ifdef ESTALE
158 case WSAESTALE:
159 return -ESTALE;
160#else
161 case WSAESTALE:
162 return -EIO; // Fallback to I/O error
163#endif
164 case WSAETIMEDOUT:
165 return -ETIMEDOUT;
166#ifdef ETOOMANYREFS
167 case WSAETOOMANYREFS:
168 return -ETOOMANYREFS;
169#else
170 case WSAETOOMANYREFS:
171 return -ENOBUFS; // Fallback to closest equivalent
172#endif
173#ifdef EUSERS
174 case WSAEUSERS:
175 return -EUSERS;
176#else
177 case WSAEUSERS:
178 return -EAGAIN; // Fallback for user quota exceeded
179#endif
180 case WSAEWOULDBLOCK:
181 return -EAGAIN;
182 /* Winsock initialization and system errors */
183 case WSASYSNOTREADY:
184 return -ENODEV; // Network subsystem unavailable
185 case WSAVERNOTSUPPORTED:
186 return -ENOSYS; // Version not supported
187 case WSANOTINITIALISED:
188 return -ENODEV; // WSAStartup not performed
189 /* DNS resolution errors */
190 case WSAHOST_NOT_FOUND:
191 return -ENOENT; // Host not found
192 case WSATRY_AGAIN:
193 return -EAGAIN; // Temporary DNS failure
194 case WSANO_RECOVERY:
195 return -EIO; // Non-recoverable DNS error
196 case WSANO_DATA:
197#ifdef ENODATA
198 return -ENODATA; // Valid name but no data record
199#else
200 return -ENOENT; // Fallback to "no such file or directory"
201#endif
202 default:
203 if (wsa_err > -4096 && wsa_err < 0) // pass through for POSIX errors
204 return wsa_err;
205
206 return -EIO; // generic fallback
207 }
208}
209
210#endif /* __IIO_UTILS_WINDOWS_H */