Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set Neon to return meaningful socket errors on Windows during socket.recv() calls. #445

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions lib/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@

// https://wiki.openssl.org/index.php/SSL/TLS_Client

static std::string socket_error_message(int err)
{
#ifdef _WIN32
char msgbuf[256];
DWORD r = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
msgbuf,
sizeof(msgbuf),
NULL);
if (r == 0) {
snprintf(msgbuf, sizeof(msgbuf), "(No Error Text) - %d", err);
}
return msgbuf;
#endif
return strerror(err);
}

class SocketObject: public Object {
public:
SocketObject() {}
Expand Down Expand Up @@ -133,9 +152,8 @@ class RawSocketObject: public SocketObject {
std::vector<unsigned char> buffer(n);
int r = ::recv(handle, reinterpret_cast<char *>(const_cast<unsigned char *>(buffer.data())), n, 0);
if (r < 0) {
int err = errno;
perror("recv");
return Cell(std::vector<Cell> {Cell(number_from_uint32(CHOICE_RecvResult_error)), Cell(utf8string(strerror(err)))});
int err = socket_error();
return Cell(std::vector<Cell> {Cell(number_from_uint32(CHOICE_RecvResult_error)), Cell(utf8string(socket_error_message(err)))});
}
if (r == 0) {
return Cell(std::vector<Cell> {Cell(number_from_uint32(CHOICE_RecvResult_eof))});
Expand All @@ -150,9 +168,8 @@ class RawSocketObject: public SocketObject {
socklen_t sin_len = sizeof(sin);
int r = ::recvfrom(handle, reinterpret_cast<char *>(const_cast<unsigned char *>(buffer.data())), n, 0, reinterpret_cast<sockaddr *>(&sin), &sin_len);
if (r < 0) {
int err = errno;
perror("recvfrom");
return Cell(std::vector<Cell> {Cell(number_from_uint32(CHOICE_RecvFromResult_error)), Cell(utf8string(strerror(err)))});
int err = socket_error();
return Cell(std::vector<Cell> {Cell(number_from_uint32(CHOICE_RecvFromResult_error)), Cell(utf8string(socket_error_message(err)))});
}
if (r == 0) {
return Cell(std::vector<Cell> {Cell(number_from_uint32(CHOICE_RecvFromResult_eof))});
Expand Down
2 changes: 2 additions & 0 deletions src/socketx.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <winsock.h>
typedef int socklen_t;
#pragma warning(disable: 4127) // incompatible with FD_SET()
#define socket_error() WSAGetLastError()

#else

Expand All @@ -15,5 +16,6 @@ typedef int socklen_t;
typedef int SOCKET;
const int INVALID_SOCKET = -1;
inline void closesocket(int x) { close(x); }
#define socket_error() errno

#endif
Loading