Skip to content

Commit

Permalink
[Net] Explicitly handle buffer errors in send/recv
Browse files Browse the repository at this point in the history
  • Loading branch information
Faless committed Jul 19, 2023
1 parent 851bc64 commit 28001b9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
20 changes: 20 additions & 0 deletions drivers/unix/net_socket_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ NetSocketPosix::NetError NetSocketPosix::_get_socket_error() const {
if (err == WSAEACCES) {
return ERR_NET_UNAUTHORIZED;
}
if (err == WSAEMSGSIZE || err == WSAENOBUFS) {
return ERR_NET_BUFFER_TOO_SMALL;
}
print_verbose("Socket error: " + itos(err));
return ERR_NET_OTHER;
#else
Expand All @@ -222,6 +225,9 @@ NetSocketPosix::NetError NetSocketPosix::_get_socket_error() const {
if (errno == EACCES) {
return ERR_NET_UNAUTHORIZED;
}
if (errno == ENOBUFS) {
return ERR_NET_BUFFER_TOO_SMALL;
}
print_verbose("Socket error: " + itos(errno));
return ERR_NET_OTHER;
#endif
Expand Down Expand Up @@ -550,6 +556,10 @@ Error NetSocketPosix::recv(uint8_t *p_buffer, int p_len, int &r_read) {
return ERR_BUSY;
}

if (err == ERR_NET_BUFFER_TOO_SMALL) {
return ERR_OUT_OF_MEMORY;
}

return FAILED;
}

Expand All @@ -571,6 +581,10 @@ Error NetSocketPosix::recvfrom(uint8_t *p_buffer, int p_len, int &r_read, IPAddr
return ERR_BUSY;
}

if (err == ERR_NET_BUFFER_TOO_SMALL) {
return ERR_OUT_OF_MEMORY;
}

return FAILED;
}

Expand Down Expand Up @@ -606,6 +620,9 @@ Error NetSocketPosix::send(const uint8_t *p_buffer, int p_len, int &r_sent) {
if (err == ERR_NET_WOULD_BLOCK) {
return ERR_BUSY;
}
if (err == ERR_NET_BUFFER_TOO_SMALL) {
return ERR_OUT_OF_MEMORY;
}

return FAILED;
}
Expand All @@ -625,6 +642,9 @@ Error NetSocketPosix::sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IP
if (err == ERR_NET_WOULD_BLOCK) {
return ERR_BUSY;
}
if (err == ERR_NET_BUFFER_TOO_SMALL) {
return ERR_OUT_OF_MEMORY;
}

return FAILED;
}
Expand Down
1 change: 1 addition & 0 deletions drivers/unix/net_socket_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class NetSocketPosix : public NetSocket {
ERR_NET_IN_PROGRESS,
ERR_NET_ADDRESS_INVALID_OR_UNAVAILABLE,
ERR_NET_UNAUTHORIZED,
ERR_NET_BUFFER_TOO_SMALL,
ERR_NET_OTHER,
};

Expand Down

0 comments on commit 28001b9

Please sign in to comment.