Updated the TCP Handshake

Updated the TCP Handshake to also negotiate a protocol version.
This commit is contained in:
Maschell 2017-04-10 15:39:58 +02:00
parent 73bf9bfb74
commit 74d422a87e
8 changed files with 228 additions and 174 deletions

View File

@ -13,7 +13,7 @@ s32 ControllerPatcherNet::recvwait(s32 sock, void *buffer, s32 len) {
return 0;
}
s32 ControllerPatcherNet::recvbyte(s32 sock) {
u8 ControllerPatcherNet::recvbyte(s32 sock) {
unsigned char buffer[1];
s32 ret;

View File

@ -6,7 +6,7 @@ class ControllerPatcherNet{
friend class UDPServer;
private:
static s32 recvwait(s32 sock, void *buffer, s32 len);
static s32 recvbyte(s32 sock);
static u8 recvbyte(s32 sock);
static s32 checkbyte(s32 sock);
static s32 sendwait(s32 sock, const void *buffer, s32 len);
static s32 sendbyte(s32 sock, unsigned char byte);

View File

@ -21,25 +21,9 @@
#include <string.h>
#include "../ControllerPatcher.hpp"
#include "./ControllerPatcherNet.hpp"
#include "../utils/CPRetainVars.hpp"
#define WIIU_CP_TCP_HANDSHAKE 0x12
#define WIIU_CP_TCP_SAME_CLIENT 0x20
#define WIIU_CP_TCP_NEW_CLIENT 0x21
#define ATTACH 0x01
#define DETACH 0x00
#define WIIU_CP_TCP_ATTACH 0x01
#define WIIU_CP_TCP_DETACH 0x02
#define WIIU_CP_TCP_PING 0xF0
#define WIIU_CP_TCP_PONG 0xF1
#define WIIU_CP_TCP_ATTACH_CONFIG_FOUND 0xE0
#define WIIU_CP_TCP_ATTACH_CONFIG_NOT_FOUND 0xE1
#define WIIU_CP_TCP_ATTACH_USER_DATA_OKAY 0xE8
#define WIIU_CP_TCP_ATTACH_USER_DATA_BAD 0xE9
#define errno (*__gh_errno_ptr())
#define wiiu_errno (*__gh_errno_ptr())
ControllerPatcherThread * TCPServer::pThread = NULL;
TCPServer * TCPServer::instance = NULL;
@ -128,13 +112,14 @@ s32 TCPServer::RunTCP(){
if(exitThread) break;
ret = ControllerPatcherNet::checkbyte(clientfd);
if (ret < 0) {
if(errno != 6) return ret;
if(wiiu_errno != 6) return ret;
usleep(1000);
continue;
}
//log_printf("got byte from tcp! %01X\n",ret);
switch (ret) {
case WIIU_CP_TCP_ATTACH: { /*attach */
if(gUsedProtocolVersion >= WIIU_CP_TCP_HANDSHAKE_VERSION_1){
s32 handle;
ret = ControllerPatcherNet::recvwait(clientfd, &handle, 4);
if(ret < 0){
@ -215,7 +200,10 @@ s32 TCPServer::RunTCP(){
if(HID_DEBUG) log_printf("TCPServer::RunTCP(line %d): handle %d connected! vid: %02X pid: %02X deviceslot %d, padslot %d\n",__LINE__,handle,vid,pid,user->slotdata.deviceslot,user->pad_slot);
break;
}
break;
}
case WIIU_CP_TCP_DETACH: { /*detach */
if(gUsedProtocolVersion >= WIIU_CP_TCP_HANDSHAKE_VERSION_1){
s32 handle;
ret = ControllerPatcherNet::recvwait(clientfd, &handle, 4);
if(ret < 0){
@ -260,13 +248,18 @@ s32 TCPServer::RunTCP(){
if(HID_DEBUG) log_printf("TCPServer::RunTCP(line %d): handle %d disconnected!\n",__LINE__,handle);
break;
}
break;
}
case WIIU_CP_TCP_PING: { /*ping*/
if(gUsedProtocolVersion >= WIIU_CP_TCP_HANDSHAKE_VERSION_1){
if(HID_DEBUG) log_printf("TCPServer::RunTCP(line %d): Got Ping, sending now a Pong\n",__LINE__);
s32 ret = ControllerPatcherNet::sendbyte(clientfd, WIIU_CP_TCP_PONG);
if(ret < 0){ log_printf("TCPServer::RunTCP(line %d): Error in %02X: sendbyte PONG\n",__LINE__); return -1;}
break;
}
break;
}
default:
return -1;
break;
@ -306,15 +299,44 @@ void TCPServer::DoTCPThreadInternal(){
if(exitThread) break;
len = 16;
/**
Handshake
1. At first this server sends his protocol version
2. The network clients answers with his preferred version (which needs to be equals or lower the version this server sent him) or an abort command.
3a. If the client sent a abort, close the connection and wait for another connection
3b. If the client sent his highest supported version, the server confirm that he is able to use this version (by sending the version back) or sending a abort command to disconnect.
**/
clientfd = ret = accept(sockfd, (sockaddr *)&(sock_addr), &len);
if(ret == -1){ ErrorHandling(); break;}
log_printf("TCPServer::DoTCPThreadInternal(line %d): TCP Connection accepted\n",__LINE__);
log_printf("TCPServer::DoTCPThreadInternal(line %d): TCP Connection accepted! Sending my protocol version: %d (0x%02X)\n",__LINE__, (WIIU_CP_TCP_HANDSHAKE - WIIU_CP_TCP_HANDSHAKE_VERSION_1)+1,WIIU_CP_TCP_HANDSHAKE);
s32 ret;
ret = ControllerPatcherNet::sendbyte(clientfd, WIIU_CP_TCP_HANDSHAKE); //Hey I'm a WiiU console!
if(ret < 0){ log_printf("TCPServer::DoTCPThreadInternal(line %d): Error sendbyte: %02X\n",__LINE__,WIIU_CP_TCP_HANDSHAKE); ErrorHandling(); break;}
u8 clientProtocolVersion = ControllerPatcherNet::recvbyte(clientfd);
if(ret < 0){ log_printf("TCPServer::DoTCPThreadInternal(line %d): Error recvbyte: %02X\n",__LINE__,WIIU_CP_TCP_HANDSHAKE); ErrorHandling(); break;}
if(clientProtocolVersion == WIIU_CP_TCP_HANDSHAKE_ABORT){
log_printf("TCPServer::DoTCPThreadInternal(line %d): The network client wants to abort.\n",__LINE__);
ErrorHandling(); break;
}
log_printf("TCPServer::DoTCPThreadInternal(line %d): received protocol version: %d (0x%02X)\n",__LINE__,(clientProtocolVersion - WIIU_CP_TCP_HANDSHAKE_VERSION_1)+1,clientProtocolVersion);
if(clientProtocolVersion >= WIIU_CP_TCP_HANDSHAKE_VERSION_MIN && clientProtocolVersion <= WIIU_CP_TCP_HANDSHAKE_VERSION_MAX){
log_printf("TCPServer::DoTCPThreadInternal(line %d): We support this protocol version. Let's confirm it to the network client.\n",__LINE__);
gUsedProtocolVersion = clientProtocolVersion;
ret = ControllerPatcherNet::sendbyte(clientfd, clientProtocolVersion);
if(ret < 0){ log_printf("TCPServer::DoTCPThreadInternal(line %d): Error sendbyte: %02X\n",__LINE__,clientProtocolVersion); ErrorHandling(); break;}
}else{
log_printf("TCPServer::DoTCPThreadInternal(line %d): We don't support this protocol version. We need to abort =(.\n",__LINE__);
ret = ControllerPatcherNet::sendbyte(clientfd, WIIU_CP_TCP_HANDSHAKE_ABORT);
ErrorHandling(); break;
}
log_printf("TCPServer::DoTCPThreadInternal(line %d): Handshake done! Success!\n",__LINE__);
if(ret < 0){ log_printf("TCPServer::DoTCPThreadInternal(line %d): Error sendbyte %02X/02X\n",__LINE__,WIIU_CP_TCP_NEW_CLIENT,WIIU_CP_TCP_SAME_CLIENT); ErrorHandling(); break;}
TCPServer::DetachAndDelete(); //Clear connected controller
RunTCP();

View File

@ -21,6 +21,29 @@
#include "dynamic_libs/socket_functions.h"
#include "utils/logger.h"
#define WIIU_CP_TCP_HANDSHAKE WIIU_CP_TCP_HANDSHAKE_VERSION_2
#define WIIU_CP_TCP_HANDSHAKE_VERSION_MIN WIIU_CP_TCP_HANDSHAKE_VERSION_1
#define WIIU_CP_TCP_HANDSHAKE_VERSION_MAX WIIU_CP_TCP_HANDSHAKE_VERSION_2
#define WIIU_CP_TCP_HANDSHAKE_VERSION_1 0x12
#define WIIU_CP_TCP_HANDSHAKE_VERSION_2 0x13
#define WIIU_CP_TCP_HANDSHAKE_ABORT 0x30
#define ATTACH 0x01
#define DETACH 0x00
#define WIIU_CP_TCP_ATTACH 0x01
#define WIIU_CP_TCP_DETACH 0x02
#define WIIU_CP_TCP_PING 0xF0
#define WIIU_CP_TCP_PONG 0xF1
#define WIIU_CP_TCP_ATTACH_CONFIG_FOUND 0xE0
#define WIIU_CP_TCP_ATTACH_CONFIG_NOT_FOUND 0xE1
#define WIIU_CP_TCP_ATTACH_USER_DATA_OKAY 0xE8
#define WIIU_CP_TCP_ATTACH_USER_DATA_BAD 0xE9
#define DEFAULT_TCP_PORT 8112
class TCPServer{

View File

@ -20,7 +20,7 @@
#include <string.h>
#include "../ControllerPatcher.hpp"
#define MAX_UDP_SIZE 0x578
#define errno (*__gh_errno_ptr())
#define wiiu_errno (*__gh_errno_ptr())
ControllerPatcherThread * UDPServer::pThread = NULL;
UDPServer * UDPServer::instance = NULL;
@ -92,7 +92,7 @@ void UDPServer::DoUDPThreadInternal(){
memset(buffer,0,MAX_UDP_SIZE);
n = recv(sockfd,buffer,MAX_UDP_SIZE,0);
if (n < 0){
s32 errno_ = errno;
s32 errno_ = wiiu_errno;
usleep(2000);
if(errno_ != 11 && errno_ != 9){
break;
@ -104,7 +104,8 @@ void UDPServer::DoUDPThreadInternal(){
memcpy((void *)&type,buffer,sizeof(type));
bufferoffset += sizeof(type);
switch (buffer[0]) {
case 0x03: {
case WIIU_CP_UDP_CONTROLLER_READ_DATA: {
if(gUsedProtocolVersion >= WIIU_CP_TCP_HANDSHAKE_VERSION_1){
u8 count_commands;
memcpy((void *)&count_commands,buffer+bufferoffset,sizeof(count_commands));
bufferoffset += sizeof(count_commands);
@ -148,6 +149,8 @@ void UDPServer::DoUDPThreadInternal(){
}
break;
}
break;
}
default:{
break;
}

View File

@ -23,6 +23,8 @@
#define DEFAULT_UDP_PORT 8113
#define WIIU_CP_UDP_CONTROLLER_READ_DATA 0x03
class UDPServer{
friend class ControllerPatcher;

View File

@ -16,6 +16,7 @@
****************************************************************************/
#include <gctypes.h>
#include "../patcher/ControllerPatcherDefs.h"
#include "../network/TCPServer.hpp"
#include "../utils/CPRetainVars.hpp"
ControllerMapping gControllerMapping __attribute__((section(".data")));
@ -54,3 +55,4 @@ u8 gOriginalAPDState __attribute__((section(".data"))) = 0;
u16 gNetworkController[gHIDMaxDevices][HID_MAX_PADS_COUNT][4] __attribute__((section(".data")));
s32 gHIDNetworkClientID __attribute__((section(".data"))) = 0;
u8 gUsedProtocolVersion __attribute__((section(".data"))) = WIIU_CP_TCP_HANDSHAKE;

View File

@ -56,4 +56,6 @@ extern u8 gOriginalAPDState;
extern u16 gNetworkController[gHIDMaxDevices][HID_MAX_PADS_COUNT][4];
extern s32 gHIDNetworkClientID;
extern u8 gUsedProtocolVersion;
#endif // CP_RETAINS_VARS_H_