Skip to content

Conversation

@dNechita
Copy link
Contributor

@dNechita dNechita commented Oct 9, 2025

Add some more WSA error codes to the translation function what are likely to occur.

PR Description

Add more WSA errors to the translation function that are likely to be encountered.

PR Type

  • Bug fix (a change that fixes an issue)
  • New feature (a change that adds new functionality)
  • Breaking change (a change that affects other repos or cause CIs to fail)

PR Checklist

  • I have conducted a self-review of my own code changes
  • I have commented new code, particularly complex or unclear areas
  • I have checked that I did not introduce new warnings or errors (CI output)
  • I have checked that components that use libiio did not get broken
  • I have updated the documentation accordingly (GitHub Pages, READMEs, etc)

@rgetz
Copy link
Contributor

rgetz commented Oct 14, 2025

I don't know if you want to fix the rest of the network.c code at the same time?

ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, ...);
if (ret < 0)
    ret = -errno;

On Windows, setsockopt() - if no error occurs, setsockopt returns zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.

Here, it just reads errno, which is not updated for Winsock calls.
So this path may yield bogus or zero error codes on Windows.

same as in : network_get_description():

ptr2 = inet_ntop(AF_INET6, &in->sin6_addr, description, INET6_ADDRSTRLEN);
if (!ptr2) {
    err = -errno;
    prm_perror(params, err, "Unable to look up IPv6 address");
}

On Windows, inet_ntop() is a Winsock call, so it sets WSAGetLastError() — not errno.
But this code reads errno directly (leaking bogus data)

and in network-windows.c there is alot of missing error checking in these WinSock functions:

  • getnameinfo() Any nonzero return value indicates failure and a specific error code can be retrieved by calling WSAGetLastError.
  • ioctlsocket() the ioctlsocket returns zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.
  • select() SOCKET_ERROR if an error occurred. If the return value is SOCKET_ERROR, WSAGetLastError can be used to retrieve a specific error code.

And this Win32 function:
GetAdaptersAddresses() If the function fails, the return value is one of the following error codes (which are outside the POSIX range : https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--1000-1299-)

Add some more WSA error codes to the translation function what are
likely to occur.

Signed-off-by: Dan Nechita <dan.nechita@analog.com>
If setsockopt() and inet_ntop() fail when executed on Windows, the
WSAGetLastError needs to be called to retrieve the specific error code
and not use errno.

Signed-off-by: Dan Nechita <dan.nechita@analog.com>
Signed-off-by: Dan Nechita <dan.nechita@analog.com>
@dNechita dNechita force-pushed the add-more-wsa-errors-codes branch 2 times, most recently from 15e408d to 37f3466 Compare November 6, 2025 09:59
@dNechita
Copy link
Contributor Author

dNechita commented Nov 6, 2025

I don't know if you want to fix the rest of the network.c code at the same time?

It would make sense to do so.

ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, ...);
ptr2 = inet_ntop(AF_INET6, &in->sin6_addr, description, INET6_ADDRSTRLEN);

I have fixed both of the above.

and in network-windows.c there is alot of missing error checking in these WinSock functions:

  • getnameinfo() Any nonzero return value indicates failure and a specific error code can be retrieved by calling [WSAGetLastError]

The errors from getnameinfo() are ignored so no WSA error codes can leak from here.

  • ioctlsocket() the ioctlsocket returns zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling [WSAGetLastError]

In dns_sd_windows the return value is ignored and in network-windows.c the return value is already handled.

  • select() SOCKET_ERROR if an error occurred. If the return value is SOCKET_ERROR, [WSAGetLastError]

Fixed.

And this Win32 function: GetAdaptersAddresses() If the function fails, the return value is one of the following error codes (which are outside the POSIX range : https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--1000-1299-)

The return values are never returned as the function calling GetAdaptersAdresses() is only interested in returning the number of sockets.

Additionaly, I found that getaddrinfo() returns errors with naming convention EAI_. While these have the same values as WSA errors (they are simply aliases) on Windows, on Linux the EAI_* don't always map to the errno values. So, I had to create another mapping function that converts EAI to errno codes.
While doing so, I also made sure the return values of getaddrinfo are not fed directly into iio_ptr().

With these changes, no errors outside of [-4095, -1] should emerge.

Unify and normalize getaddrinfo() return codes into libiio errno range.

The issue:
On Windows, getaddrinfo() may return large positive WSA codes
(e.g. 11001, 11002) that were previously passed directly to iio_ptr(),
leaking non-errno values.
On Unix, EAI_* codes live in a separate namespace distinct from errno;
feeding them directly would yield “Unknown error” messages.

Solution:
Add network_normalize_gai_error() (platform-specific) to convert every
non-zero getaddrinfo() result before pointer encoding.
 - On Windows: WSA codes (≥10000) translated via
 translate_wsa_error_to_posix(); EAI_* aliases also covered.
 - On Unix: EAI_* mapped to meaningful errno equivalents
(EAI_AGAIN→-EAGAIN, EAI_NONAME→-ENOENT, etc.) through a shared
network_map_eai_code().

Signed-off-by: Dan Nechita <dan.nechita@analog.com>
@dNechita dNechita force-pushed the add-more-wsa-errors-codes branch from 37f3466 to 21eb318 Compare November 6, 2025 10:44
Copy link
Contributor

@rgetz rgetz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me - thanks

@dNechita dNechita merged commit de2676e into main Nov 17, 2025
32 checks passed
@dNechita dNechita deleted the add-more-wsa-errors-codes branch November 17, 2025 10:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants