libmodbus icon indicating copy to clipboard operation
libmodbus copied to clipboard

AI_ADDRCONFIG causes getaddrinfo error with purely local connection

Open uecasm opened this issue 4 years ago • 0 comments

libmodbus version

3.0.6 (but the related code appears unchanged in the latest version)

OS and/or distribution

Windows 10

Environment

64-bit OS, compiling library as 32-bit

Description

Creating modbus client or server with modbus_new_tcp_pi + modbus_connect / modbus_tcp_pi_listen.

At least on Windows (not sure if this affects Linux or not), due to the use of the AI_ADDRCONFIG flag the call to getaddrinfo (in both server and client) fails when there is no local network connected, even if the address you're trying to resolve is "localhost".

I'm not sure whether this flag should be removed entirely, or just omitted when the specified address is localhost.

Expected behaviour

It should still be possible to start server+client on localhost when there is otherwise no network connection.

Actual behaviour

getaddrinfo fails, causing server and client to fail to start.

Known workarounds

It can be worked around on both client and server by explicitly passing "127.0.0.1" instead of "localhost" as the node address. (But it would be nicer if the name worked.) Also note that while I've confirmed that this does work on Windows, there are several bug reports I found while looking into this that suggest that this workaround would not work under Linux (and also that this issue in general would affect Linux; but I have not tested that).

Also, on the server side (only), you can pass NULL for the node address, causing it to listen on all adapters (including loopback) and also conveniently bypassing this issue since getaddrinfo does not fail in this case (at least on Windows) even if the flag is set. (Although this does not work in 3.0.6; it requires a fix introduced in 3.1.2.)

uecasm avatar Jan 14 '21 23:01 uecasm