From: <bo...@us...> - 2003-11-20 14:44:49
|
Update of /cvsroot/sharedaemon/core/src In directory sc8-pr-cvs1:/tmp/cvs-serv20594 Modified Files: test.cpp Log Message: Splitted down the big switch in function Serve to many small functions, one for each case. Index: test.cpp =================================================================== RCS file: /cvsroot/sharedaemon/core/src/test.cpp,v retrieving revision 1.22 retrieving revision 1.23 diff -u -d -r1.22 -r1.23 --- test.cpp 31 Oct 2003 21:00:13 -0000 1.22 +++ test.cpp 20 Nov 2003 14:44:06 -0000 1.23 @@ -440,6 +440,7 @@ #include <signal.h> #define _GLIBCPP_DEPRECATED // We want std::vector::push_back(void)! #include <vector> +#include <map> hash userhash={{'x','M','u','l','e',0x0e,'2','0','0','3','0','8','2','6',0x6f,'2'}}; /* @@ -638,9 +639,215 @@ // class Connection Connection::empty; int num_connected=0; +std::map<std::string,ParseClass *> PClist; + +void Log(char * msg) { + struct tm * t; + time_t tmp; + + tmp=time(NULL); + t=localtime(&tmp); + + printf("%04u-%02u-%02u %02u:%02u'%02u: %s",1900+t->tm_year,t->tm_mon,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec,msg); +} + +void Client_Tcp_Sending_Hello_Packet(u32 id) { + p.BuildHello3Packet( + 0x10,userhash,ipport(MY_IP,DEFAULT_CLIENTPORT), + 0x01,"bothie tests his xMule v2 core", + 0x11,60, + 0x0f,DEFAULT_CLIENTPORT, + ipport(MY_IP,DEFAULT_SERVERPORT) + ); + printf( + "Sending packet (id=%lu; ip:port=%lu:%u. Protocol=0x%02x, Opcode=0x%02x, Size=0x%08x\n", + (long unsigned)id,(long unsigned)Connection::GetSocket(id).GetIP(), + (unsigned)Connection::GetSocket(id).GetPort(),p.GetProtocol(), + p.GetOpcode(),(int)p.GetSize() + ); + printf("%s:%i: \n",__FILE__,__LINE__); HexDump(p.GetPtr(),p.GetSize(),true); + rc=Connection::GetSocket(id).EnqueuePacket(p); + if (!rc) { + if (get_errno()!=SOCKET_EWOULDBLOCK) { + Connection::GetSocket(id).Close(); + Connection::SetStatus(id,ILLEGAL); + Connection::Free(id); + return; + } + } + Connection::SetStatus(id,CLIENT_TCP_WAITING_FOR_HELLO_ANSWER); + + p.BuildHashsetRequestPacket(filehash); + printf( + "Sending packet (id=%lu; ip:port=%lu:%u. Protocol=0x%02x, Opcode=0x%02x, Size=0x%08x\n", + (long unsigned)id,(long unsigned)Connection::GetSocket(id).GetIP(), + (unsigned)Connection::GetSocket(id).GetPort(),p.GetProtocol(), + p.GetOpcode(),(int)p.GetSize() + ); + printf("%s:%i: \n",__FILE__,__LINE__); HexDump(p.GetPtr(),p.GetSize(),true); + rc=Connection::GetSocket(id).EnqueuePacket(p); + if (!rc) { + printf("error sending hashset request.\n"); + } + rc=true; +} + +void Server_Tcp_Sending_Login_Request(u32 id) { + p.BuildLoginRequest3Packet( + userhash,ipport(0,DEFAULT_CLIENTPORT), // <HASH>user<IPPORT>client<DWORD>tagcount + 0x01,"bothie tests his xMule v2 core", // <TAG>non-unique user name + 0x11,60, // <TAG>version + 0x0f,DEFAULT_CLIENTPORT, // <TAG>port + ipport(0,DEFAULT_SERVERPORT) // <IPPORT>server_address + ); + // i+=mkdword (r+i,'MLDK'); // <DWORD>*software_id + printf( + "Sending packet (id=%lu; ip:port=%lu:%u. Protocol=0x%02x, Opcode=0x%02x, Size=0x%08x\n", + (long unsigned)id,(long unsigned)Connection::GetSocket(id).GetIP(), + (unsigned)Connection::GetSocket(id).GetPort(),p.GetProtocol(), + p.GetOpcode(),(int)p.GetSize() + ); + printf("%s:%i: \n",__FILE__,__LINE__); HexDump(p.GetPtr(),p.GetSize(),true); + rc=Connection::GetSocket(id).EnqueuePacket(p); + if (!rc) { + if (get_errno()!=SOCKET_EWOULDBLOCK) { + Connection::GetSocket(id).Close(); + Connection::SetStatus(id,ILLEGAL); + Connection::Free(id); + return; + } + } + Connection::SetStatus(id,SERVER_TCP_WAITING_FOR_LOGIN_ANSWER); + rc=true; +} + +bool Server_Tcp_Waiting_For_Login_Answer(u32 id) { + bool rc; + Ed2kPacket p; + const parsestruct * ps; + ParseClass * PC; + + // We need 0x40 (IDCHANGE) here. + // Additionally we can already accept here: + // * 0x34 (SERVERSTATUS) + // * 0x41 (SERVERIDENT) + // * 0x38 (SERVERMESSAGE) + rc=Connection::GetSocket(id).ReceivePacket(p); + if (!rc) { + if (get_errno()!=SOCKET_EWOULDBLOCK) { + Connection::GetSocket(id).Close(); + Connection::SetStatus(id,ILLEGAL); + Connection::Free(id); + } + return false; + } + + printf( + "Received packet (id=%lu; ip:port=%lu:%u. Protocol=0x%02x, Opcode=0x%02x, Size=0x%08x\n", + (long unsigned)id,(long unsigned)p.GetIP(),(unsigned)p.GetPort(),p.GetProtocol(),p.GetOpcode(),(int)p.GetSize() + ); + printf("%s:%i: \n",__FILE__,__LINE__); HexDump(p.GetPtr(),p.GetSize(),false); + + if (!packet_description(p.GetProtocol(),p.GetOpcode(),&ps,NULL)) { + fprintf( + stderr + ,"%s:%i: FATAL: Couldn't find ps for packet 0x%02x,0x%02x.\n" + ,__FILE__,__LINE__,p.GetProtocol(),p.GetOpcode() + ); + Connection::GetSocket(id).Close(); + Connection::SetStatus(id,ILLEGAL); + Connection::Free(id); + return false; + } + + switch ((u16)p.GetProtocol()*0x100+p.GetOpcode()) { + case 0xe338: // Servermessage + u16 msglen; + char * msg; + + if (!PClist["e338"]) { + PClist["e338"]=new ParseClass(ps); + } + PC=PClist["e338"]; + printf("Servermessage:\n"); + // Log Message. + PC->Read(p.GetPtr(),p.GetSize()); + PC->Get(msglen,"message_len"); + printf("msglen=%u\n",msglen); + msg=new char[msglen]; + for (u16 i=0;i<msglen;++i) { + PC->GetF(msg[i],"message[%u]",i); + } + Log(msg); + break; + + case 0xe334: // Serverstatus + printf("Serverstatus:\n"); + printf("%s:%i: \n",__FILE__,__LINE__); HexDump(p.GetPtr(),p.GetSize(),false); + // Update ServerMet + // Schedule ServerMet to write back to disk + break; + + case 0xe340: // IDchange + printf("IDchange:\n"); + printf("%s:%i: \n",__FILE__,__LINE__); HexDump(p.GetPtr(),p.GetSize(),false); + // ??? + break; + + case 0xe341: // ServerIdent + printf("ServerIdent:\n"); + printf("%s:%i: \n",__FILE__,__LINE__); HexDump(p.GetPtr(),p.GetSize(),false); + // Update ServerMet + // Schedule ServerMet to write back to disk + break; + } + + return true; +} + +bool Client_Tcp_Waiting_For_Hello_Answer(u32 id) { + rc=Connection::GetSocket(id).ReceivePacket(p); + if (!rc) { + if (get_errno()!=SOCKET_EWOULDBLOCK) { + Connection::GetSocket(id).Close(); + Connection::SetStatus(id,ILLEGAL); + Connection::Free(id); + } + return false; + } + printf( + "Received packet (id=%lu; ip:port=%lu:%u. Protocol=0x%02x, Opcode=0x%02x, Size=0x%08x\n", + (long unsigned)id,(long unsigned)p.GetIP(),(unsigned)p.GetPort(),p.GetProtocol(),p.GetOpcode(),(int)p.GetSize() + ); + printf("%s:%i: \n",__FILE__,__LINE__); HexDump(p.GetPtr(),p.GetSize(),false); + + return true; +} + +bool Client_Tcp_Waiting_For_Hello_Packet(u32 id) { + rc=Connection::GetSocket(id).ReceivePacket(p); + if (!rc) { + if (get_errno()!=SOCKET_EWOULDBLOCK) { + Connection::GetSocket(id).Close(); + Connection::SetStatus(id,ILLEGAL); + Connection::Free(id); + } + return false; + } + printf( + "Received packet (id=%lu; ip:port=%lu:%u. Protocol=0x%02x, Opcode=0x%02x, Size=0x%08x\n", + (long unsigned)id,(long unsigned)p.GetIP(),(unsigned)p.GetPort(),p.GetProtocol(),p.GetOpcode(),(int)p.GetSize() + ); + printf("%s:%i: \n",__FILE__,__LINE__); HexDump(p.GetPtr(),p.GetSize(),false); + + return true; +} + void Serve(u32 id) { - bool rc,again; + bool again; static u32 nc=0xffffffff; + + bool rc; Ed2kPacket p; if (id==nc || Connection::IsFree(id)) { @@ -649,7 +856,8 @@ rc=Connection::GetSocket(id).SendPackets(1024); if (!rc) { - if (get_errno()!=SOCKET_EWOULDBLOCK && get_errno()!=SOCKET_EINPROGRESS) { + if (get_errno()!=SOCKET_EWOULDBLOCK + && get_errno()!=SOCKET_EINPROGRESS) { Connection::GetSocket(id).Close(); Connection::SetStatus(id,ILLEGAL); Connection::Free(id); @@ -684,138 +892,33 @@ rc=true; break; case CLIENT_TCP_SENDING_HELLO_PACKET: { - p.BuildHello3Packet( - 0x10,userhash,ipport(MY_IP,DEFAULT_CLIENTPORT), - 0x01,"bothie tests his xMule v2 core", - 0x11,60, - 0x0f,DEFAULT_CLIENTPORT, - ipport(MY_IP,DEFAULT_SERVERPORT) - ); - printf( - "Sending packet (id=%lu; ip:port=%lu:%u. Protocol=0x%02x, Opcode=0x%02x, Size=0x%08x\n", - (long unsigned)id,(long unsigned)Connection::GetSocket(id).GetIP(), - (unsigned)Connection::GetSocket(id).GetPort(),p.GetProtocol(), - p.GetOpcode(),(int)p.GetSize() - ); - HexDump(p.GetPtr(),p.GetSize(),true); - rc=Connection::GetSocket(id).EnqueuePacket(p); - if (!rc) { - if (get_errno()!=SOCKET_EWOULDBLOCK) { - Connection::GetSocket(id).Close(); - Connection::SetStatus(id,ILLEGAL); - Connection::Free(id); - break; - } - } - Connection::SetStatus(id,CLIENT_TCP_WAITING_FOR_HELLO_ANSWER); + Client_Tcp_Sending_Hello_Packet(id); - p.BuildHashsetRequestPacket(filehash); - printf( - "Sending packet (id=%lu; ip:port=%lu:%u. Protocol=0x%02x, Opcode=0x%02x, Size=0x%08x\n", - (long unsigned)id,(long unsigned)Connection::GetSocket(id).GetIP(), - (unsigned)Connection::GetSocket(id).GetPort(),p.GetProtocol(), - p.GetOpcode(),(int)p.GetSize() - ); - HexDump(p.GetPtr(),p.GetSize(),true); - rc=Connection::GetSocket(id).EnqueuePacket(p); - if (!rc) { - printf("error sending hashset request.\n"); - } - rc=true; break; } case SERVER_TCP_SENDING_LOGIN_REQUEST: { - p.BuildLoginRequest3Packet( - userhash,ipport(0,DEFAULT_CLIENTPORT), // <HASH>user<IPPORT>client<DWORD>tagcount - 0x01,"bothie tests his xMule v2 core", // <TAG>non-unique user name - 0x11,60, // <TAG>version - 0x0f,DEFAULT_CLIENTPORT, // <TAG>port - ipport(0,DEFAULT_SERVERPORT) // <IPPORT>server_address - ); - // i+=mkdword (r+i,'MLDK'); // <DWORD>*software_id - printf( - "Sending packet (id=%lu; ip:port=%lu:%u. Protocol=0x%02x, Opcode=0x%02x, Size=0x%08x\n", - (long unsigned)id,(long unsigned)Connection::GetSocket(id).GetIP(), - (unsigned)Connection::GetSocket(id).GetPort(),p.GetProtocol(), - p.GetOpcode(),(int)p.GetSize() - ); - HexDump(p.GetPtr(),p.GetSize(),true); - rc=Connection::GetSocket(id).EnqueuePacket(p); - if (!rc) { - if (get_errno()!=SOCKET_EWOULDBLOCK) { - Connection::GetSocket(id).Close(); - Connection::SetStatus(id,ILLEGAL); - Connection::Free(id); - break; - } - } - Connection::SetStatus(id,SERVER_TCP_WAITING_FOR_LOGIN_ANSWER); - rc=true; + Server_Tcp_Sending_Login_Request(id); + break; } case SERVER_TCP_WAITING_FOR_LOGIN_ANSWER: { - // We need 0x41 (SERVERIDENT) and 0x40 (IDCHANGE) - // Additionally we can accept here 0x34 (SERVERSTATUS) - // and 0x38 (SERVERMESSAGE) already - rc=Connection::GetSocket(id).ReceivePacket(p); - if (!rc) { - if (get_errno()!=SOCKET_EWOULDBLOCK) { - Connection::GetSocket(id).Close(); - Connection::SetStatus(id,ILLEGAL); - Connection::Free(id); - } - break; - } - printf( - "Received packet (id=%lu; ip:port=%lu:%u. Protocol=0x%02x, Opcode=0x%02x, Size=0x%08x\n", - (long unsigned)id,(long unsigned)p.GetIP(),(unsigned)p.GetPort(),p.GetProtocol(),p.GetOpcode(),(int)p.GetSize() - ); - HexDump(p.GetPtr(),p.GetSize(),false); - again=true; + again=Server_Tcp_Waiting_For_Login_Answer(id); break; } case CLIENT_TCP_WAITING_FOR_HELLO_ANSWER: { - rc=Connection::GetSocket(id).ReceivePacket(p); - if (!rc) { - if (get_errno()!=SOCKET_EWOULDBLOCK) { - Connection::GetSocket(id).Close(); - Connection::SetStatus(id,ILLEGAL); - Connection::Free(id); - } - break; - } - printf( - "Received packet (id=%lu; ip:port=%lu:%u. Protocol=0x%02x, Opcode=0x%02x, Size=0x%08x\n", - (long unsigned)id,(long unsigned)p.GetIP(),(unsigned)p.GetPort(),p.GetProtocol(),p.GetOpcode(),(int)p.GetSize() - ); - HexDump(p.GetPtr(),p.GetSize(),false); - again=true; + again=Client_Tcp_Waiting_For_Hello_Answer(id); break; } case CLIENT_TCP_WAITING_FOR_HELLO_PACKET: - rc=Connection::GetSocket(id).ReceivePacket(p); - if (!rc) { - if (get_errno()!=SOCKET_EWOULDBLOCK) { - Connection::GetSocket(id).Close(); - Connection::SetStatus(id,ILLEGAL); - Connection::Free(id); - } - break; - } - printf( - "Received packet (id=%lu; ip:port=%lu:%u. Protocol=0x%02x, Opcode=0x%02x, Size=0x%08x\n", - (long unsigned)id,(long unsigned)p.GetIP(),(unsigned)p.GetPort(),p.GetProtocol(),p.GetOpcode(),(int)p.GetSize() - ); - HexDump(p.GetPtr(),p.GetSize(),false); - again=true; + again=Client_Tcp_Waiting_For_Hello_Packet(id); break; default: rc=false; break; - }; // for (again=true;!(again=!again);), switch (Connection::GetStatus(id)) + }; // for (again=true;!(again=!again);), switch (Conn.::GetStatus(id)) } /* // Anything went wrong: @@ -838,6 +941,43 @@ return 1; } +/* +Received packet (id=2; ip:port=0:0. Protocol=0xe3, Opcode=0x41, Size=0x0000005f +00000000 2a2a 2a2a 6998 7348 51dc ff5c 4a94 ec58 ****isHQÜ \JX +00000010 0afb 960f 3512 0200 0000 0201 0001 0f00 ..5........... +00000020 626f 7468 6965 2773 2053 6572 7665 7202 bothie's.Server. +00000030 0100 0b2a 0045 7870 6572 696d 656e 7461 ...*.Experimenta +00000040 6c20 7365 7276 6572 2062 6173 6564 2075 l.server.based.u +00000050 706f 6e20 6546 6172 6d20 7630 2e32 62 pon.eFarm.v0.2b + +Received packet (id=2; ip:port=0:0. Protocol=0xe3, Opcode=0x34, Size=0x00000008 +00000000 0100 0000 0000 0000 ........ + +Received packet (id=2; ip:port=0:0. Protocol=0xe3, Opcode=0x40, Size=0x00000004 +00000000 7f00 0001 .... + +Received packet (id=2; ip:port=0:0. Protocol=0xe3, Opcode=0x38, Size=0x0000004d +00000000 4b00 5465 7374 7365 7276 6572 2e20 506f K.Testserver..Po +00000010 7765 7265 6420 6279 2068 7474 703a 2f2f wered.by.http:// +00000020 7777 772e 6566 6172 6d2d 7072 6f6a 6563 www.efarm-projec +00000030 742e 6e65 742c 2076 6572 7369 6f6e 2030 t.net,.version.0 +00000040 2e32 6220 2870 6174 6368 6564 29 .2b.(patched) + +Received packet (id=2; ip:port=0:0. Protocol=0xe3, Opcode=0x38, Size=0x000000b9 +00000000 b700 5468 6973 2073 6572 7665 7220 7365 .This.server.se +00000010 656d 7320 746f 2068 6176 6520 7072 6f62 ems.to.have.prob +00000020 6c65 6d73 2073 6572 7669 6e67 2073 6561 lems.serving.sea +00000030 7263 6820 7265 7175 6573 7473 2e20 4927 rch.requests..I' +00000040 6d20 776f 726b 696e 6720 6f6e 2069 7421 m.working.on.it! +00000050 2050 6c65 6173 6520 636f 6e6e 6563 7420 .Please.connect. +00000060 746f 2070 6f72 7420 3436 3631 2061 6e64 to.port.4661.and +00000070 206e 6f74 2035 3636 312e 2054 6865 2073 .not.5661..The.s +00000080 6572 7665 7220 7275 6e6e 696e 6720 6174 erver.running.at +00000090 2035 3636 3120 7769 6c6c 2062 6520 7265 .5661.will.be.re +000000a0 7374 6174 6564 2065 7665 7279 2066 6577 stated.every.few +000000b0 206d 696e 7574 6573 2e .minutes. +*/ + int main(int argc,char * argv[]) { u32 serverindex; u32 ip; @@ -891,6 +1031,9 @@ rc=ClientTcpConnect(Connection::Alloc(),MY_IP,6662); printf("ClientTcpConnect(Alloc(),MY_IP,6662)=%i\n",(int)rc); + + rc=ServerTcpConnect(Connection::Alloc(),MY_IP,4661); + printf("ServerTcpConnect(Connection::Alloc(),MY_IP,4661)=%i\n",(int)rc); for (;;) { if (!--serverconnectwait) { |