Skip to content

Commit

Permalink
coap_io.c: Map Windows WSA errors into errno for CoAP layer simplific…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
mrdeep1 committed Mar 2, 2023
1 parent aa34b5f commit 4dfbcb6
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions src/coap_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,22 @@ coap_update_epoll_timer(coap_context_t *context, coap_tick_t delay)

#endif /* COAP_EPOLL_SUPPORT */

#ifdef _WIN32
static void
coap_win_error_to_errno(void) {
int error = WSAGetLastError();
switch (error) {
case WSAEWOULDBLOCK: errno = EWOULDBLOCK; break;
case WSAECONNRESET: errno = ECONNRESET; break;
case WSAECONNREFUSED: errno = ECONNREFUSED; break;
case WSAEINVAL: errno = EINVAL; break;
default:
coap_log_debug("WSAGetLastError: %d mapping to errno undefined\n", error);
break;
}
}
#endif /* _WIN32 */

ssize_t
coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len) {
ssize_t r;
Expand All @@ -490,12 +506,14 @@ coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len) {
#endif
if (r == COAP_SOCKET_ERROR) {
#ifdef _WIN32
if (WSAGetLastError() == WSAEWOULDBLOCK) {
coap_win_error_to_errno();
if (WSAGetLastError() == WSAEWOULDBLOCK)
#elif EAGAIN != EWOULDBLOCK
if (errno==EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
if (errno==EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
#else
if (errno==EAGAIN || errno == EINTR) {
if (errno==EAGAIN || errno == EINTR)
#endif
{
sock->flags |= COAP_SOCKET_WANT_WRITE;
#ifdef COAP_EPOLL_SUPPORT
coap_epoll_ctl_mod(sock,
Expand Down Expand Up @@ -544,17 +562,20 @@ coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len) {
if (r == 0) {
/* graceful shutdown */
sock->flags &= ~COAP_SOCKET_CAN_READ;
errno = ECONNRESET;
return -1;
} else if (r == COAP_SOCKET_ERROR) {
sock->flags &= ~COAP_SOCKET_CAN_READ;
#ifdef _WIN32
coap_win_error_to_errno();
error = WSAGetLastError();
if (error == WSAEWOULDBLOCK) {
if (error == WSAEWOULDBLOCK)
#elif EAGAIN != EWOULDBLOCK
if (errno==EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
if (errno==EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
#else
if (errno==EAGAIN || errno == EINTR) {
if (errno==EAGAIN || errno == EINTR)
#endif
{
return 0;
}
#ifdef _WIN32
Expand Down Expand Up @@ -829,6 +850,7 @@ coap_socket_recv(coap_socket_t *sock, coap_packet_t *packet) {
#endif
if (len < 0) {
#ifdef _WIN32
coap_win_error_to_errno();
if (WSAGetLastError() == WSAECONNRESET ||
WSAGetLastError() == WSAECONNREFUSED) {
#else
Expand Down Expand Up @@ -905,6 +927,7 @@ coap_socket_recv(coap_socket_t *sock, coap_packet_t *packet) {

if (len < 0) {
#ifdef _WIN32
coap_win_error_to_errno();
if (WSAGetLastError() == WSAECONNRESET) {
#else
if (errno == ECONNREFUSED) {
Expand Down Expand Up @@ -1375,6 +1398,7 @@ coap_io_process_with_fds(coap_context_t *ctx, uint32_t timeout_ms,

if (result < 0) { /* error */
#ifdef _WIN32
coap_win_error_to_errno();
if (WSAGetLastError() != WSAEINVAL) /* May happen because of ICMP */
#else
if (errno != EINTR)
Expand Down

0 comments on commit 4dfbcb6

Please sign in to comment.