Merge remote-tracking branch 'GliniakRepo/xamNetSockNameAndErrorHandling' into canary_pr

This commit is contained in:
Gliniak 2022-05-19 10:17:29 +02:00
commit ef281c69c3
3 changed files with 82 additions and 51 deletions

View File

@ -211,13 +211,13 @@ dword_result_t NetDll_XNetGetOpt_entry(dword_t one, dword_t option_id,
case 1:
if (*buffer_size < sizeof(XNetStartupParams)) {
*buffer_size = sizeof(XNetStartupParams);
return 0x2738; // WSAEMSGSIZE
return uint32_t(X_WSAError::X_WSAEMSGSIZE);
}
std::memcpy(buffer_ptr, &xnet_startup_params, sizeof(XNetStartupParams));
return 0;
default:
XELOGE("NetDll_XNetGetOpt: option {} unimplemented", option_id);
return 0x2726; // WSAEINVAL
return uint32_t(X_WSAError::X_WSAEINVAL);
}
}
DECLARE_XAM_EXPORT1(NetDll_XNetGetOpt, kNetworking, kSketchy);
@ -288,6 +288,9 @@ dword_result_t NetDll_WSACleanup_entry(dword_t caller) {
}
DECLARE_XAM_EXPORT1(NetDll_WSACleanup, kNetworking, kImplemented);
// Instead of using dedicated storage for WSA error like on OS.
// Xbox shares space between normal error codes and WSA errors.
// This under the hood returns directly value received from RtlGetLastError.
dword_result_t NetDll_WSAGetLastError_entry() {
return XThread::GetLastError();
}
@ -326,8 +329,7 @@ dword_result_t NetDll_WSASendTo_entry(
auto socket =
kernel_state()->object_table()->LookupObject<XSocket>(socket_handle);
if (!socket) {
// WSAENOTSOCK
XThread::SetLastError(0x2736);
XThread::SetLastError(uint32_t(X_WSAError::X_WSAENOTSOCK));
return -1;
}
@ -363,7 +365,7 @@ dword_result_t NetDll_WSAWaitForMultipleEvents_entry(dword_t num_events,
dword_t timeout,
dword_t alertable) {
if (num_events > 64) {
XThread::SetLastError(87); // ERROR_INVALID_PARAMETER
XThread::SetLastError(uint32_t(X_WSAError::X_WSA_INVALID_PARAMETER));
return ~0u;
}
@ -633,8 +635,7 @@ dword_result_t NetDll_socket_entry(dword_t caller, dword_t af, dword_t type,
if (XFAILED(result)) {
socket->Release();
uint32_t error = xboxkrnl::xeRtlNtStatusToDosError(result);
XThread::SetLastError(error);
XThread::SetLastError(socket->GetLastWSAError());
return -1;
}
@ -646,8 +647,7 @@ dword_result_t NetDll_closesocket_entry(dword_t caller, dword_t socket_handle) {
auto socket =
kernel_state()->object_table()->LookupObject<XSocket>(socket_handle);
if (!socket) {
// WSAENOTSOCK
XThread::SetLastError(0x2736);
XThread::SetLastError(uint32_t(X_WSAError::X_WSAENOTSOCK));
return -1;
}
@ -664,19 +664,13 @@ int_result_t NetDll_shutdown_entry(dword_t caller, dword_t socket_handle,
auto socket =
kernel_state()->object_table()->LookupObject<XSocket>(socket_handle);
if (!socket) {
// WSAENOTSOCK
XThread::SetLastError(0x2736);
XThread::SetLastError(uint32_t(X_WSAError::X_WSAENOTSOCK));
return -1;
}
auto ret = socket->Shutdown(how);
if (ret == -1) {
#ifdef XE_PLATFORM_WIN32
uint32_t error_code = WSAGetLastError();
XThread::SetLastError(error_code);
#else
XThread::SetLastError(0x0);
#endif
XThread::SetLastError(socket->GetLastWSAError());
}
return ret;
}
@ -688,8 +682,7 @@ dword_result_t NetDll_setsockopt_entry(dword_t caller, dword_t socket_handle,
auto socket =
kernel_state()->object_table()->LookupObject<XSocket>(socket_handle);
if (!socket) {
// WSAENOTSOCK
XThread::SetLastError(0x2736);
XThread::SetLastError(uint32_t(X_WSAError::X_WSAENOTSOCK));
return -1;
}
@ -703,14 +696,13 @@ dword_result_t NetDll_ioctlsocket_entry(dword_t caller, dword_t socket_handle,
auto socket =
kernel_state()->object_table()->LookupObject<XSocket>(socket_handle);
if (!socket) {
// WSAENOTSOCK
XThread::SetLastError(0x2736);
XThread::SetLastError(uint32_t(X_WSAError::X_WSAENOTSOCK));
return -1;
}
X_STATUS status = socket->IOControl(cmd, arg_ptr);
if (XFAILED(status)) {
XThread::SetLastError(xboxkrnl::xeRtlNtStatusToDosError(status));
XThread::SetLastError(socket->GetLastWSAError());
return -1;
}
@ -725,15 +717,14 @@ dword_result_t NetDll_bind_entry(dword_t caller, dword_t socket_handle,
auto socket =
kernel_state()->object_table()->LookupObject<XSocket>(socket_handle);
if (!socket) {
// WSAENOTSOCK
XThread::SetLastError(0x2736);
XThread::SetLastError(uint32_t(X_WSAError::X_WSAENOTSOCK));
return -1;
}
N_XSOCKADDR_IN native_name(name);
X_STATUS status = socket->Bind(&native_name, namelen);
if (XFAILED(status)) {
XThread::SetLastError(xboxkrnl::xeRtlNtStatusToDosError(status));
XThread::SetLastError(socket->GetLastWSAError());
return -1;
}
@ -747,15 +738,14 @@ dword_result_t NetDll_connect_entry(dword_t caller, dword_t socket_handle,
auto socket =
kernel_state()->object_table()->LookupObject<XSocket>(socket_handle);
if (!socket) {
// WSAENOTSOCK
XThread::SetLastError(0x2736);
XThread::SetLastError(uint32_t(X_WSAError::X_WSAENOTSOCK));
return -1;
}
N_XSOCKADDR native_name(name);
X_STATUS status = socket->Connect(&native_name, namelen);
if (XFAILED(status)) {
XThread::SetLastError(xboxkrnl::xeRtlNtStatusToDosError(status));
XThread::SetLastError(socket->GetLastWSAError());
return -1;
}
@ -768,14 +758,13 @@ dword_result_t NetDll_listen_entry(dword_t caller, dword_t socket_handle,
auto socket =
kernel_state()->object_table()->LookupObject<XSocket>(socket_handle);
if (!socket) {
// WSAENOTSOCK
XThread::SetLastError(0x2736);
XThread::SetLastError(uint32_t(X_WSAError::X_WSAENOTSOCK));
return -1;
}
X_STATUS status = socket->Listen(backlog);
if (XFAILED(status)) {
XThread::SetLastError(xboxkrnl::xeRtlNtStatusToDosError(status));
XThread::SetLastError(socket->GetLastWSAError());
return -1;
}
@ -787,16 +776,14 @@ dword_result_t NetDll_accept_entry(dword_t caller, dword_t socket_handle,
pointer_t<XSOCKADDR> addr_ptr,
lpdword_t addrlen_ptr) {
if (!addr_ptr) {
// WSAEFAULT
XThread::SetLastError(0x271E);
XThread::SetLastError(uint32_t(X_WSAError::X_WSAEFAULT));
return -1;
}
auto socket =
kernel_state()->object_table()->LookupObject<XSocket>(socket_handle);
if (!socket) {
// WSAENOTSOCK
XThread::SetLastError(0x2736);
XThread::SetLastError(uint32_t(X_WSAError::X_WSAENOTSOCK));
return -1;
}
@ -868,7 +855,7 @@ struct host_set {
}
};
int_result_t NetDll_select_entry(int_t caller, int_t nfds,
int_result_t NetDll_select_entry(dword_t caller, dword_t nfds,
pointer_t<x_fd_set> readfds,
pointer_t<x_fd_set> writefds,
pointer_t<x_fd_set> exceptfds,
@ -928,8 +915,7 @@ dword_result_t NetDll_recv_entry(dword_t caller, dword_t socket_handle,
auto socket =
kernel_state()->object_table()->LookupObject<XSocket>(socket_handle);
if (!socket) {
// WSAENOTSOCK
XThread::SetLastError(0x2736);
XThread::SetLastError(uint32_t(X_WSAError::X_WSAENOTSOCK));
return -1;
}
@ -945,8 +931,7 @@ dword_result_t NetDll_recvfrom_entry(dword_t caller, dword_t socket_handle,
auto socket =
kernel_state()->object_table()->LookupObject<XSocket>(socket_handle);
if (!socket) {
// WSAENOTSOCK
XThread::SetLastError(0x2736);
XThread::SetLastError(uint32_t(X_WSAError::X_WSAENOTSOCK));
return -1;
}
@ -969,13 +954,7 @@ dword_result_t NetDll_recvfrom_entry(dword_t caller, dword_t socket_handle,
}
if (ret == -1) {
// TODO: Better way of getting the error code
#ifdef XE_PLATFORM_WIN32
uint32_t error_code = WSAGetLastError();
XThread::SetLastError(error_code);
#else
XThread::SetLastError(0x0);
#endif
XThread::SetLastError(socket->GetLastWSAError());
}
return ret;
@ -988,8 +967,7 @@ dword_result_t NetDll_send_entry(dword_t caller, dword_t socket_handle,
auto socket =
kernel_state()->object_table()->LookupObject<XSocket>(socket_handle);
if (!socket) {
// WSAENOTSOCK
XThread::SetLastError(0x2736);
XThread::SetLastError(uint32_t(X_WSAError::X_WSAENOTSOCK));
return -1;
}
@ -1005,8 +983,7 @@ dword_result_t NetDll_sendto_entry(dword_t caller, dword_t socket_handle,
auto socket =
kernel_state()->object_table()->LookupObject<XSocket>(socket_handle);
if (!socket) {
// WSAENOTSOCK
XThread::SetLastError(0x2736);
XThread::SetLastError(uint32_t(X_WSAError::X_WSAENOTSOCK));
return -1;
}
@ -1031,6 +1008,28 @@ void NetDll_WSASetLastError_entry(dword_t error_code) {
}
DECLARE_XAM_EXPORT1(NetDll_WSASetLastError, kNetworking, kImplemented);
dword_result_t NetDll_getsockname_entry(dword_t caller, dword_t socket_handle,
lpvoid_t buf_ptr, lpdword_t len_ptr) {
auto socket =
kernel_state()->object_table()->LookupObject<XSocket>(socket_handle);
if (!socket) {
XThread::SetLastError(uint32_t(X_WSAError::X_WSAENOTSOCK));
return -1;
}
int buffer_len = *len_ptr;
X_STATUS status = socket->GetSockName(buf_ptr, &buffer_len);
if (XFAILED(status)) {
XThread::SetLastError(socket->GetLastWSAError());
return -1;
}
*len_ptr = buffer_len;
return 0;
}
DECLARE_XAM_EXPORT1(NetDll_getsockname, kNetworking, kImplemented);
} // namespace xam
} // namespace kernel
} // namespace xe

View File

@ -257,5 +257,26 @@ bool XSocket::QueuePacket(uint32_t src_ip, uint16_t src_port,
return true;
}
X_STATUS XSocket::GetSockName(uint8_t* buf, int* buf_len) {
struct sockaddr sa = {};
int ret = getsockname(native_handle_, &sa, (socklen_t*)buf_len);
if (ret < 0) {
return X_STATUS_UNSUCCESSFUL;
}
std::memcpy(buf, &sa, *buf_len);
return X_STATUS_SUCCESS;
}
uint32_t XSocket::GetLastWSAError() const {
// Todo(Gliniak): Provide error mapping table
// Xbox error codes might not match with what we receive from OS
#ifdef XE_PLATFORM_WIN32
return WSAGetLastError();
#endif
return errno;
}
} // namespace kernel
} // namespace xe

View File

@ -19,6 +19,14 @@
namespace xe {
namespace kernel {
enum class X_WSAError : uint32_t {
X_WSA_INVALID_PARAMETER = 0x0057,
X_WSAEFAULT = 0x271E,
X_WSAEINVAL = 0x2726,
X_WSAENOTSOCK = 0x2736,
X_WSAEMSGSIZE = 0x2738,
};
struct XSOCKADDR {
xe::be<uint16_t> address_family;
char sa_data[14];
@ -106,6 +114,7 @@ class XSocket : public XObject {
X_STATUS Connect(N_XSOCKADDR* name, int name_len);
X_STATUS Bind(N_XSOCKADDR_IN* name, int name_len);
X_STATUS Listen(int backlog);
X_STATUS GetSockName(uint8_t* buf, int* buf_len);
object_ref<XSocket> Accept(N_XSOCKADDR* name, int* name_len);
int Shutdown(int how);
@ -117,6 +126,8 @@ class XSocket : public XObject {
int SendTo(uint8_t* buf, uint32_t buf_len, uint32_t flags, N_XSOCKADDR_IN* to,
uint32_t to_len);
uint32_t GetLastWSAError() const;
struct packet {
// These values are in network byte order.
xe::be<uint16_t> src_port;