I found a bug in network_open_address(), where if the bind() call failed the socket was not invalidated, thus if no connection could be made on any address it was returning a non-Invalid, but non-Connected socket.
Attached patch fixes this, and also adds better logging for a few sections by properly writing out errno results from networking calls.
(I found this by misconfiguring my client with the wrong listen directive address, so it couldn't connect!)
Thanks for figuring this out!
One remark: the GET_LAST_ERROR() macro is there for a reason: windows code is recommended to use windows-internal function rather than errno, which is now hidden behind the macro GET_LAST_ERROR().
I do not know whether it makes much sense to call strerror with socket errors on windows systems.
It does not, since this is a DLL error of which the standard C library know nothing. In the old days there where even several such DLLs before microsoft ended this by providing winsock.dll out of the box.
I'll be sure to use GET_LAST_ERROR() then instead of manually using errno - what about the gai_strerror() method employed by getnameinfo() and similar?
gai_strerror should work according to this page:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms738514%28v=vs.85%29.aspx
In my tries those functions did not gave nice error messages by rather generic "IO error -42" or so. But it may depend on the windows version. I will run some trials.
EDIT: It is even weritten there:
QuoteIf the ecode parameter is not an error code value that getaddrinfo (http://msdn.microsoft.com/en-us/library/windows/desktop/ms738520%28v=vs.85%29.aspx) returns, the gai_strerror function returns a pointer to a string that indicates an unknown error.
Thus the usual ouput would be "unknown error -42", as I observed too.
gai_strerror() should of course only be used with functions which it understands the error codes to (e.g. getaddrinfo, getnameinfo etc.)
Looks like on Windows WSAGetLastError() is the way to go for getting windows sockets errors (it is also threadsafe, whereas gai_strerror is not on Windows). Maybe we could have a define, SOCKLASTERROR or similar which on Windows goes to WSAGetLastError(), and on *nix maps to gai_strerror()?