Multitude of fixes for networking.

This commit is contained in:
Matthew Parlane 2012-01-26 23:34:56 +13:00
parent de11c592f2
commit 6b4c5c4685
2 changed files with 134 additions and 24 deletions

View File

@ -464,7 +464,7 @@ char *DecodeError(int ErrorCode)
}
#endif
static int getNetErrorCode(int ret, std::string caller)
static int getNetErrorCode(int ret, std::string caller, bool isRW)
{
#ifdef _WIN32
int errorCode = WSAGetLastError();
@ -484,7 +484,11 @@ static int getNetErrorCode(int ret, std::string caller)
case 10056:
return -30;
case 10035:
if(isRW){
return -6;
}else{
return -26;
}
case 6:
return -26;
default:
@ -581,7 +585,7 @@ u32 CWII_IPC_HLE_Device_net_ip_top::ExecuteCommand(u32 _Command,
serverAddr.sin_family = serverAddr.sin_family >> 8;
int ret = connect(Common::swap32(params.socket), (struct sockaddr *) &serverAddr, sizeof(serverAddr));
ret = getNetErrorCode(ret, "SO_CONNECT");
ret = getNetErrorCode(ret, "SO_CONNECT", false);
INFO_LOG(WII_IPC_NET,"/dev/net/ip/top::IOCtl request IOCTL_SO_CONNECT (%08x, %s:%d)",
Common::swap32(params.socket), inet_ntoa(serverAddr.sin_addr), Common::swap16(serverAddr.sin_port));
@ -598,7 +602,7 @@ u32 CWII_IPC_HLE_Device_net_ip_top::ExecuteCommand(u32 _Command,
u32 sock = Memory::Read_U32(_BufferIn);
u32 how = Memory::Read_U32(_BufferIn+4);
int ret = shutdown(sock, how);
return getNetErrorCode(ret, "SO_SHUTDOWN");
return getNetErrorCode(ret, "SO_SHUTDOWN", false);
break;
}
@ -610,7 +614,7 @@ u32 CWII_IPC_HLE_Device_net_ip_top::ExecuteCommand(u32 _Command,
#ifdef _WIN32
u32 ret = closesocket(sock);
return getNetErrorCode(ret, "IOCTL_SO_CLOSE");
return getNetErrorCode(ret, "IOCTL_SO_CLOSE", false);
#else
return close(sock);
#endif
@ -626,7 +630,7 @@ u32 CWII_IPC_HLE_Device_net_ip_top::ExecuteCommand(u32 _Command,
INFO_LOG(WII_IPC_NET, "/dev/net/ip/top::IOCtl request IOCTL_SO_SOCKET "
"Socket: %08x (%d,%d,%d), BufferIn: (%08x, %i), BufferOut: (%08x, %i)",
s, AF, TYPE, PROT, _BufferIn, BufferInSize, _BufferOut, BufferOutSize);
return getNetErrorCode(s, "SO_SOCKET");
return getNetErrorCode(s, "SO_SOCKET", false);
break;
}
@ -646,7 +650,7 @@ u32 CWII_IPC_HLE_Device_net_ip_top::ExecuteCommand(u32 _Command,
inet_ntoa(address.sin_addr), Common::swap16(address.sin_port),
Common::swap32(addr->socket), _BufferIn, BufferInSize, _BufferOut, BufferOutSize);
return getNetErrorCode(ret, "SO_BIND");
return getNetErrorCode(ret, "SO_BIND", false);
break;
}
@ -659,7 +663,7 @@ u32 CWII_IPC_HLE_Device_net_ip_top::ExecuteCommand(u32 _Command,
u32 S = Memory::Read_U32(_BufferIn);
u32 BACKLOG = Memory::Read_U32(_BufferIn + 0x04);
u32 ret = listen(S, BACKLOG);
return getNetErrorCode(ret, "SO_LISTEN");
return getNetErrorCode(ret, "SO_LISTEN", false);
break;
}
@ -674,7 +678,7 @@ u32 CWII_IPC_HLE_Device_net_ip_top::ExecuteCommand(u32 _Command,
socklen_t* addrlen = (socklen_t*) Memory::GetPointer(BufferOutSize);
*addrlen = sizeof(struct sockaddr);
int ret = accept(S, addr, addrlen);
return getNetErrorCode(ret, "SO_ACCEPT");
return getNetErrorCode(ret, "SO_ACCEPT", false);
}
case IOCTL_SO_GETSOCKOPT:
@ -699,7 +703,7 @@ u32 CWII_IPC_HLE_Device_net_ip_top::ExecuteCommand(u32 _Command,
int ret = getsockopt (sock, level, optname, (char *) &optval, (socklen_t*)&optlen);
ret = getNetErrorCode(ret, "SO_GETSOCKOPT");
ret = getNetErrorCode(ret, "SO_GETSOCKOPT", false);
Memory::Write_U32(optlen, _BufferOut + 0xC);
@ -732,7 +736,7 @@ u32 CWII_IPC_HLE_Device_net_ip_top::ExecuteCommand(u32 _Command,
int ret = setsockopt(S, level, optname, (char*)optval, optlen);
ret = getNetErrorCode(ret, "SO_SETSOCKOPT");
ret = getNetErrorCode(ret, "SO_SETSOCKOPT", false);
return ret;
}
@ -761,7 +765,7 @@ u32 CWII_IPC_HLE_Device_net_ip_top::ExecuteCommand(u32 _Command,
if (arg & F_NONBLOCK)
iMode = 1;
int ioctlret = ioctlsocket(sock, FIONBIO, &iMode);
return getNetErrorCode(ioctlret, "SO_FCNTL");
return getNetErrorCode(ioctlret, "SO_FCNTL", false);
}
else
{
@ -839,7 +843,7 @@ u32 CWII_IPC_HLE_Device_net_ip_top::ExecuteCommand(u32 _Command,
}
int ret = poll(ufds, nfds, timeout);
ret = getNetErrorCode(ret, "SO_SETSOCKOPT");
ret = getNetErrorCode(ret, "SO_SETSOCKOPT", false);
for (int i = 0; i<nfds; i++)
{
@ -1107,13 +1111,13 @@ u32 CWII_IPC_HLE_Device_net_ip_top::ExecuteCommandV(u32 _Parameter, SIOCtlVBuffe
addr->sin_family = addr->sin_family >> 8;
int ret = sendto(Common::swap32(params.socket), (char*)Memory::GetPointer(_BufferIn),
BufferInSize, Common::swap32(params.flags), (struct sockaddr*)addr, len);
return getNetErrorCode(ret, "SO_SENDTO");
return getNetErrorCode(ret, "SO_SENDTO", true);
}
else
{
int ret = send(Common::swap32(params.socket), (char*)Memory::GetPointer(_BufferIn),
BufferInSize, Common::swap32(params.flags));
return getNetErrorCode(ret, "SO_SEND");
return getNetErrorCode(ret, "SO_SEND", true);
}
break;
}
@ -1162,7 +1166,7 @@ u32 CWII_IPC_HLE_Device_net_ip_top::ExecuteCommandV(u32 _Parameter, SIOCtlVBuffe
addr.sin_family = (addr.sin_family << 8) | (BufferOutSize2&0xFF);
Memory::WriteBigEData((u8*)&addr, _BufferOut2, BufferOutSize2);
}
return getNetErrorCode(ret, "SO_RECVFROM");
return getNetErrorCode(ret, "SO_RECVFROM", true);
break;
}

View File

@ -23,6 +23,7 @@
#include <openssl/err.h>
#include "FileUtil.h"
#include "WII_IPC_HLE_Device_net_ssl.h"
#include "../Debugger/Debugger_SymbolMap.h"
CWII_IPC_HLE_Device_net_ssl::CWII_IPC_HLE_Device_net_ssl(u32 _DeviceID, const std::string& _rDeviceName)
: IWII_IPC_HLE_Device(_DeviceID, _rDeviceName)
@ -189,6 +190,63 @@ u32 CWII_IPC_HLE_Device_net_ssl::ExecuteCommandV(u32 _Parameter, SIOCtlVBuffer C
_BufferOut2, BufferOutSize2, _BufferOut3, BufferOutSize3);
break;
}
// a BufferIn: (007ad6a0, 32), BufferIn2: (00000000, 0), BufferIn3: (00000000, 0), BufferOut: (007ad680, 32), BufferOut2: (00765920, 924), BufferOut3: (00000000, 0)
case IOCTLV_NET_SSL_SETROOTCA:
{
INFO_LOG(WII_IPC_NET, "/dev/net/ssl::IOCtlV request IOCTLV_NET_SSL_SETROOTCA "
"BufferIn: (%08x, %i), BufferIn2: (%08x, %i), "
"BufferIn3: (%08x, %i), BufferOut: (%08x, %i), "
"BufferOut2: (%08x, %i), BufferOut3: (%08x, %i)",
_BufferIn, BufferInSize, _BufferIn2, BufferInSize2,
_BufferIn3, BufferInSize3, _BufferOut, BufferOutSize,
_BufferOut2, BufferOutSize2, _BufferOut3, BufferOutSize3);
//Dolphin_Debugger::PrintCallstack(LogTypes::WII_IPC_NET, LogTypes::LINFO);
int sslID = Memory::Read_U32(_BufferOut) - 1;
void* certca = malloc(BufferOutSize2);
if (sslID >= 0 && sslID < NET_SSL_MAXINSTANCES && sslfds[sslID] != NULL)
{
std::string cert_base_path(File::GetUserPath(D_WIIUSER_IDX));
SSL* ssl = sslfds[sslID];
FILE *wiiclientca = fopen((cert_base_path + "clientca.cer").c_str(), "rb");
if (wiiclientca == NULL)
break;
X509 *cert = d2i_X509_fp(wiiclientca, NULL);
fclose(wiiclientca);
if (SSL_use_certificate(ssl,cert) <= 0)
break;
if (cert)
X509_free(cert);
FILE * clientcakey = fopen((cert_base_path + "clientcakey.der").c_str(), "rb");
if (clientcakey == NULL)
break;
EVP_PKEY * key = d2i_PrivateKey_fp(clientcakey, NULL);
if (SSL_use_PrivateKey(ssl,key) <= 0)
break;
if (!SSL_check_private_key(ssl))
break;
if (key)
EVP_PKEY_free(key);
Memory::Write_U32(0, _BufferIn);
}
free(certca);
break;
}
case IOCTLV_NET_SSL_SETBUILTINCLIENTCERT:
{
@ -331,6 +389,16 @@ u32 CWII_IPC_HLE_Device_net_ssl::ExecuteCommandV(u32 _Parameter, SIOCtlVBuffer C
int sock = Memory::Read_U32(_BufferOut2);
SSL* ssl = sslfds[sslID];
SSL_set_fd(ssl,sock);
FILE *ssl_write = fopen("ssl_write.txt", "ab");
fprintf(ssl_write, "%s", "###############\n");
fclose(ssl_write);
FILE *ssl_read = fopen("ssl_read.txt", "ab");
fprintf(ssl_read, "%s", "###############\n");
fclose(ssl_read);
returnValue = SSL_connect(ssl);
Memory::Write_U32(0, _BufferIn);
}
@ -355,8 +423,13 @@ u32 CWII_IPC_HLE_Device_net_ssl::ExecuteCommandV(u32 _Parameter, SIOCtlVBuffer C
if (sslID >= 0 && sslID < NET_SSL_MAXINSTANCES && sslfds[sslID] != NULL)
{
SSL* ssl = sslfds[sslID];
SSL_set_verify(ssl, SSL_VERIFY_NONE, NULL);
returnValue = SSL_do_handshake(ssl);
if (returnValue == 1)
SSL_load_error_strings();
FILE *quickDump = fopen("quickdump.txt", "wb");
ERR_print_errors_fp(quickDump);
fclose(quickDump);
// if (returnValue == 1)
Memory::Write_U32(0, _BufferIn);
}
INFO_LOG(WII_IPC_NET, "/dev/net/ssl::IOCtlV request IOCTLV_NET_SSL_DOHANDSHAKE "
@ -375,15 +448,25 @@ u32 CWII_IPC_HLE_Device_net_ssl::ExecuteCommandV(u32 _Parameter, SIOCtlVBuffer C
char out1[32];
char out2[256];
//Dolphin_Debugger::PrintCallstack(LogTypes::WII_IPC_NET, LogTypes::LINFO);
Memory::ReadBigEData((u8*)in1, _BufferIn, 32);
Memory::ReadBigEData((u8*)out1, _BufferOut, 32);
Memory::ReadBigEData((u8*)out2, _BufferOut2, 256);
Memory::ReadBigEData((u8*)out2, _BufferOut2, BufferOutSize2);
int sslID = Memory::Read_U32(_BufferOut) - 1;
if (sslID >= 0 && sslID < NET_SSL_MAXINSTANCES && sslfds[sslID] != NULL)
{
SSL* ssl = sslfds[sslID];
/*FILE *ssl_write = fopen("ssl_write.txt", "ab");
fwrite(Memory::GetPointer(_BufferOut2), 1, BufferOutSize2, ssl_write);
fprintf(ssl_write, "----(%d)----\n", BufferOutSize2);
fclose(ssl_write);
*/
returnValue = SSL_write(ssl, Memory::GetPointer(_BufferOut2), BufferOutSize2);
if (returnValue == -1)
returnValue = -SSL_get_error(ssl, returnValue);
Memory::Write_U32(returnValue, _BufferIn);
returnValue = 0;
}
@ -400,23 +483,46 @@ u32 CWII_IPC_HLE_Device_net_ssl::ExecuteCommandV(u32 _Parameter, SIOCtlVBuffer C
case IOCTLV_NET_SSL_READ:
{
char in1[32];
char in2[256];
char out1[32];
u32 in1[32];
u32 in2[256];
u32 out1[32];
memset(in2, 0, 256);
Memory::ReadBigEData((u8*)in1, _BufferIn, 32);
//Memory::ReadBigEData((u8*)in2, _BufferIn2, BufferInSize2);
Memory::ReadBigEData((u8*)out1, _BufferOut, 32);
/*DumpCommands(_BufferIn,4,LogTypes::WII_IPC_NET,LogTypes::LDEBUG);
DumpCommands(_BufferIn2,2,LogTypes::WII_IPC_NET,LogTypes::LDEBUG);
DumpCommands(_BufferOut,4,LogTypes::WII_IPC_NET,LogTypes::LDEBUG);
*/
int sslID = Memory::Read_U32(_BufferOut) - 1;
if (sslID >= 0 && sslID < NET_SSL_MAXINSTANCES && sslfds[sslID] != NULL)
{
SSL* ssl = sslfds[sslID];
returnValue = SSL_read(ssl, Memory::GetPointer(_BufferIn2), BufferInSize2);
if (returnValue == -1)
returnValue = -SSL_get_error(ssl, returnValue);
SSL_load_error_strings();
FILE *quickDump = fopen("quickdump.txt", "ab");
ERR_print_errors_fp(quickDump);
fclose(quickDump);
Memory::Write_U32(returnValue, _BufferIn);
//returnValue = 0;
}
strncpy(in2, (char*)Memory::GetPointer(_BufferIn2), BufferInSize2);
/*memcpy(in2, (char*)Memory::GetPointer(_BufferIn2), BufferInSize2);
FILE *ssl_read = fopen("ssl_read.txt", "ab");
if((s32)returnValue >0)
fwrite(in2, 1, returnValue, ssl_read);
fprintf(ssl_read, "%s", "--------\n");
fclose(ssl_read);
*/
INFO_LOG(WII_IPC_NET, "/dev/net/ssl::IOCtlV request IOCTLV_NET_SSL_READ(%d) %s "
"BufferIn: (%08x, %i), BufferIn2: (%08x, %i), "
"BufferIn3: (%08x, %i), BufferOut: (%08x, %i), "
@ -430,7 +536,7 @@ u32 CWII_IPC_HLE_Device_net_ssl::ExecuteCommandV(u32 _Parameter, SIOCtlVBuffer C
default:
{
INFO_LOG(WII_IPC_NET, "/dev/net/ssl::IOCtlV request %x "
ERROR_LOG(WII_IPC_NET, "/dev/net/ssl::IOCtlV request %i "
"BufferIn: (%08x, %i), BufferIn2: (%08x, %i), "
"BufferIn3: (%08x, %i), BufferOut: (%08x, %i), "
"BufferOut2: (%08x, %i), BufferOut3: (%08x, %i)",
@ -452,7 +558,7 @@ u32 CWII_IPC_HLE_Device_net_ssl::ExecuteCommand(u32 _Command,
{
default:
{
INFO_LOG(WII_IPC_NET, "/dev/net/ssl::IOCtl request %x "
INFO_LOG(WII_IPC_NET, "/dev/net/ssl::IOCtl request unknown %i "
"(BufferIn: (%08x, %i), BufferOut: (%08x, %i)",
_Command,
_BufferIn, BufferInSize, _BufferOut, BufferOutSize);