From: <bo...@us...> - 2003-11-26 01:23:00
|
Update of /cvsroot/sharedaemon/core/src In directory sc8-pr-cvs1:/tmp/cvs-serv14868 Modified Files: Ed2kConnection.h ParseClass.cpp ParseClass.h parse-ed2k.c test.cpp Log Message: Pre-support of sending the OfferFile packet to the server Index: Ed2kConnection.h =================================================================== RCS file: /cvsroot/sharedaemon/core/src/Ed2kConnection.h,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- Ed2kConnection.h 20 Nov 2003 16:27:25 -0000 1.3 +++ Ed2kConnection.h 26 Nov 2003 01:22:56 -0000 1.4 @@ -40,6 +40,7 @@ // Accepted connections on tcp serverport: SERVER_TCP_SENDING_LOGIN_REQUEST, SERVER_TCP_WAITING_FOR_LOGIN_ANSWER, + SERVER_TCP_INITIAL_OFFER_FILES, // Our udp serverport listening for incoming connections SERVER_UDP_LISTENING, // we are contacting a server: Index: ParseClass.cpp =================================================================== RCS file: /cvsroot/sharedaemon/core/src/ParseClass.cpp,v retrieving revision 1.9 retrieving revision 1.10 diff -u -d -r1.9 -r1.10 --- ParseClass.cpp 22 Nov 2003 02:41:25 -0000 1.9 +++ ParseClass.cpp 26 Nov 2003 01:22:56 -0000 1.10 @@ -199,7 +199,7 @@ tmp=ps->first_referer+ps->num_referer; numPCs=numPCs>tmp?numPCs:tmp; - size=ps->offset+ps->size+(numPCs?sizeof(ParseClass*):0); + size=ps->offset+ps->size+numPCs*sizeof(ParseClass*); } else { if (IsStruct()) { assert(!IsStruct()); @@ -211,22 +211,23 @@ return; } +ParseClass * * ParseClass::FirstParseClassPointer(unsigned idx) const { + return (ParseClass**)(data+size*(idx+1)-sizeof(ParseClass*)*numPCs); +} + void ParseClass::Resize(size_t n) { unsigned char * tmp; - size_t offset,i,p; + size_t i,p; const struct parsestruct * ps; // printf(">> Resize(n=%i), size=%i\n",n,size); if (numPCs && n<nume) { - offset=size-sizeof(ParseClass*); - for (i=n;i<nume;++i) { - ParseClass * PC=(*(ParseClass**)(data+size*i+offset)); + ParseClass * * PC=FirstParseClassPointer(i); for (p=0;p<numPCs;++p,++PC) { - PC->~ParseClass(); + (*PC)->~ParseClass(); } - free(*(ParseClass**)(data+size*i+offset)); } } @@ -244,27 +245,23 @@ data=tmp; if (numPCs && n>nume) { - offset=size-sizeof(ParseClass*); int * constructed=new int[numPCs]; for (i=nume;i<n;++i) { - class ParseClass * PCp=reinterpret_cast<ParseClass*>( - malloc(sizeof(ParseClass)*numPCs) - ); + class ParseClass * * PC=FirstParseClassPointer(i); for (size_t j=0;j<numPCs;++j) constructed[j]=0; - *(ParseClass**)(data+size*i+offset)=PCp; for (ps=type;ps;ps=ps->next) { if (ps->flags&PF_ARRAY0 || ps->type_id==STRUCT) { assert(ps->referer_id<numPCs); if (ps->type_id==STRUCT) { - new (PCp+ps->referer_id) + (*(PC+ps->referer_id))=new ParseClass(this,reinterpret_cast< struct parsestruct * >(ps->default_value)); } else { - new (PCp+ps->referer_id) + (*(PC+ps->referer_id))=new ParseClass(this,ps); } assert (!constructed[ps->referer_id]); @@ -449,10 +446,6 @@ exit(1); } -ParseClass * ParseClass::FirstParseClassPointer(int idx) { - return *(ParseClass**)(data+size*(idx+1)-sizeof(ParseClass*)*numPCs); -} - size_t ParseClass::IncrementalReadNonArray( const u8 * buffer,size_t buffsize ) { @@ -519,12 +512,12 @@ if (num) { size_t call_done; - ParseClass * PC; + ParseClass * * PC; PC=FirstParseClassPointer(i); PC+=ps->referer_id; - call_done=PC->IncrementalRead( + call_done=(*PC)->IncrementalRead( buffer,buffsize,num ); if (call_done==(size_t)-1) { @@ -543,10 +536,10 @@ */ } } else { - ParseClass * PC; - u32 val; - unsigned j; - bool scalar; + ParseClass * * PC; + u32 val; + unsigned j; + bool scalar; /* printf("."); fflush(stdout); @@ -578,8 +571,9 @@ } PC=FirstParseClassPointer(i); + PC+=ps->first_referer; for (j=0;j<ps->num_referer;++j,++PC) { - PC->Resize(val); + (*PC)->Resize(val); } } Index: ParseClass.h =================================================================== RCS file: /cvsroot/sharedaemon/core/src/ParseClass.h,v retrieving revision 1.9 retrieving revision 1.10 diff -u -d -r1.9 -r1.10 --- ParseClass.h 22 Nov 2003 02:50:17 -0000 1.9 +++ ParseClass.h 26 Nov 2003 01:22:56 -0000 1.10 @@ -220,7 +220,7 @@ /* * Helpers for IncrementalRead. */ - ParseClass * FirstParseClassPointer(int idx); + ParseClass * * FirstParseClassPointer(unsigned idx) const; size_t IncrementalReadNonArray(const u8 * buffer,size_t buffsize); size_t IncrementalReadArray(const u8 * buffer,size_t buffsize); @@ -322,12 +322,11 @@ abort(); } } else { - ParseClass * PC=*(ParseClass**) - (data+size*(index+1)-sizeof(ParseClass*)); + ParseClass * * PC=FirstParseClassPointer(index); if (struc) { - PC[ps->referer_id].Get(var,next_index,subexpr); + PC[ps->referer_id]->Get(var,next_index,subexpr); } else { - var=*(T*)(PC->data+PC->size*next_index); + var=*(T*)(PC[ps->referer_id]->data+PC[ps->referer_id]->size*next_index); } } } Index: parse-ed2k.c =================================================================== RCS file: /cvsroot/sharedaemon/core/src/parse-ed2k.c,v retrieving revision 1.13 retrieving revision 1.14 diff -u -d -r1.13 -r1.14 --- parse-ed2k.c 20 Nov 2003 16:27:25 -0000 1.13 +++ parse-ed2k.c 26 Nov 2003 01:22:56 -0000 1.14 @@ -784,7 +784,9 @@ printf("STRUCT w/o ARRAY!\n"); i->referer_id=max_referer++; } else { +/* i->referer_id=0; +*/ } } } Index: test.cpp =================================================================== RCS file: /cvsroot/sharedaemon/core/src/test.cpp,v retrieving revision 1.27 retrieving revision 1.28 diff -u -d -r1.27 -r1.28 --- test.cpp 22 Nov 2003 02:46:32 -0000 1.27 +++ test.cpp 26 Nov 2003 01:22:56 -0000 1.28 @@ -543,40 +543,6 @@ return true; } -#define RD_1_ARG(function,need,arg1) do { \ - if (need<=buffered) { \ - rc=(function)(buffer+offset,arg1); \ - } else { \ - rc=-1; \ - } \ - if (-1==rc && fill_buffer(file,buffer,BUFFER_SIZE,offset,&buffered)) { \ - offset=0; \ - if (need<=buffered) { \ - rc=(function)(buffer+offset,arg1); \ - } else { \ - rc=-1; \ - } \ - } \ - if (-1==rc) return false; \ - buffered-=rc; \ - offset+=rc; \ -} while (0) - -#define RDLONG(argument) RD_1_ARG(rdlong,4,argument) -#define RDWORD(argument) RD_1_ARG(rdword,2,argument) -#define RDBYTE(argument) RD_1_ARG(rdbyte,1,argument) -#define RDTAG(argument) do { \ - rc=rdtag(buffer+offset,argument,buffered); \ - if (-1==rc && fill_buffer(file,buffer,BUFFER_SIZE,offset,&buffered)) { \ - offset=0; \ - rc=rdtag(buffer+offset,argument,buffered); \ - } \ - if (-1==rc) return false; \ - buffered-=rc; \ - offset+=rc; \ -} while (0) - - #define BUFFER_SIZE 4096 #define load_server_met new_load_server_met @@ -591,10 +557,7 @@ struct tag * tp; u32 nt; - fprintf(stderr,">>ServerMet->Read(server_met)\n"); ServerMet->Read(server_met); - fprintf(stderr,"<<ServerMet->Read(server_met)\n"); - ServerMet->Get(server_met_magic,"magic"); ServerMet->Get(num_servers,"num_servers"); @@ -623,25 +586,137 @@ } struct parsestruct * server_met_ps; +struct parsestruct * part_met_ps; +int num_partfiles=0; +int max_partfiles=0; +std::string IncomingDirectory="/mnt/Fat_C/gesaugt/Internet/eMule/Incoming"; +std::map<std::string,ParseClass *> partfile; + +void printhash(hash h) { + int i; + + for (i=0;i<16;++i) printf("%02X",h.b[i]); +} + +bool load_part_met(std::string part_met) { + u8 part_met_magic; + u32 part_met_date; + hash part_met_filehash; + u32 part_met_chunkcount; + hash part_met_chunkhash; + u32 part_met_tagcount; + + u32 i; + + printf("Loading part file %s ...",part_met.c_str()); + + ParseClass * & PartFile=partfile[part_met]; + + if (!PartFile) { + PartFile=new ParseClass(part_met_ps); + } + + PartFile->Read(part_met.c_str()); + PartFile->Get(part_met_magic ,"magic"); printf("Magic =0x%02x\n",(unsigned)part_met_magic); + PartFile->Get(part_met_date ,"date"); printf("Date =0x%08x\n",(unsigned)part_met_date); + PartFile->Get(part_met_filehash ,"filehash"); printf("File hash ="); printhash(part_met_filehash); putchar('\n'); + PartFile->Get(part_met_chunkcount,"chunkcount"); printf("Chunk count=%u\n",(unsigned)part_met_chunkcount); + for (i=0;i<part_met_chunkcount;++i) { + PartFile->GetF(part_met_chunkhash,"chunkhash[%i]",(unsigned)i); + printf("\tChunk hash[%u]=",(unsigned)i); + printhash(part_met_chunkhash); + putchar('\n'); + } + PartFile->Get(part_met_tagcount ,"tagcount"); printf("Tag count =%u\n",(unsigned)part_met_tagcount); + + printf(" done.\n"); + + return true; +} void parse_something() { server_met_ps=parse_line( "<BYTE>magic<DWORD>num_servers{<IPPORT>ip<DWORD>num_tags<TAG>tag[num_tags]}server[num_servers]" ); + part_met_ps=parse_line( + /* + * According to eDonkey-protocol-0.6.1, the field magic is + * 0xe1 (or 0xe0 for old clients). + */ + "<BYTE>magic<DWORD>date<HASH>filehash" + /* + * According to eDonkey-protocol-0.6.1, there are existing + * broken clients, which doubles the list of chunkhashs. + * So, numchunks may be 2*real_num_chunks. Keep this in mind. + */ + "<WORD>chunkcount<HASH>chunkhash[chunkcount]" + "<DWORD>tagcount<TAG>tag[tagcount]" + ); + /* print_parsestruct(server_met_ps,0); */ + print_parsestruct(part_met_ps,0); ServerMet=new ParseClass(server_met_ps); } -// class Connection Connection::empty; -int num_connected=0; +/* +3.2. part.met +------------------------- +<part.met> ::= 0xe1 <File details> // 0xe0 in older versions +<File details> ::= <Date> <File hash> <Part hash list> <Meta tag list> +<Date> ::= DWORD + +<Meta tag list> ::= DWORD <Meta tag>* +<Meta tag> ::= 0x00 Undefined + ||= 0x01 <Meta tag name> HASH + ||= 0x02 <Meta tag name> <String> + ||= 0x03 <Meta tag name> DWORD + ||= 0x04 <Meta tag name> FLOAT + ||= 0x05 <Meta tag name> BOOL // ?? + ||= 0x06 <Meta tag name> BOOL Array // ?? + ||= 0x07 <Meta tag name> BLOB // ?? +<Meta tag name> ::= WORD <Special tag> // WORD is the number of bytes in <Special tag> + ||= <String> +<Special tag> ::= 0x01 // name + ||= 0x02 // size: size of file + ||= 0x03 // type: Audio, Video, Image, Pro, Doc, Col + ||= 0x04 // format: file extension + ||= 0x05 // Collection (depricated) // ?? + ||= 0x06 // Part Path // ?? + ||= 0x07 // Part Hash // ?? + ||= 0x08 // copied + ||= 0x09 DATA // gap start: DATA keeps number of gap as string + ||= 0x0a DATA // gap end: DATA keeps number of gap as string + ||= 0x0b // description + ||= 0x0c // ping + ||= 0x0d // fail + ||= 0x0e // preference + ||= 0x0f // port + ||= 0x10 // ip + ||= 0x11 // version + ||= 0x12 // tempfile + ||= 0x13 // priority + ||= 0x14 // status + ||= 0x15 // availability + ||= 0x16 // QTime // ?? + ||= 0x17 // Parts // ?? + ||= <eMule special tag> + +<eMule special tag> ::= 0x20 // Compression + ||= 0x21 // UDP client port + ||= 0x22 // UDP version + ||= 0x23 // Source exchange + ||= 0x24 // Comments + ||= 0x25 // Extended request + ||= 0x26 // Compatible client +*/ std::map<std::string,ParseClass *> PClist; -void Log(char * msg) { +void Log(const char * msg) { struct tm * t; time_t tmp; @@ -651,6 +726,24 @@ printf("%04u-%02u-%02u %02u:%02u'%02u: %s\n",1900+t->tm_year,t->tm_mon,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec,msg); } +void LogF(const char * fmt,...) { + char * msg; + va_list ap; + + va_start(ap,fmt); + msg=vmprintf(fmt,ap); + va_end(ap); + + Log(msg); +} + +// class Connection Connection::empty; + +int num_connected=0; +int max_connected=0; +u32 * OurClientID=NULL; +u32 * connection_id=NULL; + void Client_Tcp_Sending_Hello_Packet(u32 id) { bool rc; Ed2kPacket p; @@ -766,18 +859,25 @@ return false; } + char proto_opcode[5]; + + sprintf( + proto_opcode, + "%04x", + (unsigned)((u16)p.GetProtocol()*0x100+p.GetOpcode()) + ); + + if (!PClist[proto_opcode]) PClist[proto_opcode]=new ParseClass(ps); + PC=PClist[proto_opcode]; + PC->Read(p.GetPtr(),p.GetSize()); + switch ((u16)p.GetProtocol()*0x100+p.GetOpcode()) { case 0xe338: // Servermessage u16 msglen,i; 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+1]; @@ -786,19 +886,58 @@ } msg[i]=0; Log(msg); + delete 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); - // ??? + if (num_connected) { + Log("Duh, we are already connected.\n"); + } + if (num_connected==max_connected) { + max_connected=1+2*max_connected; + OurClientID =(u32*)realloc(OurClientID , + max_connected*sizeof(*OurClientID )); + connection_id=(u32*)realloc(connection_id, + max_connected*sizeof(*connection_id)); + if (!OurClientID || !connection_id) { + LogF( + "%s:%s:%i: FATAL: " + "Out of memory.\n" + ,__FILE__,__FUNCTION__,__LINE__ + ); + } + } + connection_id[num_connected]=id; + PC->Get(OurClientID[num_connected],"new_id"); + char * tmp,*t; + if (OurClientID[num_connected]<0x1000000) { + t=strdup("LowID"); + } else { + t=mprintf( + "%u.%u.%u.%u" + ,( OurClientID[num_connected] &255) + ,((OurClientID[num_connected]>> 8)&255) + ,((OurClientID[num_connected]>>16)&255) + ,((OurClientID[num_connected]>>24)&255) + ); + } + tmp=mprintf( + "We are connected now. " + "New client ID is %u (%s)" + ,OurClientID[num_connected],t + ); + free(t); + Log(tmp); + free(tmp); + Connection::SetStatus(id,SERVER_TCP_INITIAL_OFFER_FILES); + ++num_connected; break; case 0xe341: // ServerIdent @@ -873,6 +1012,10 @@ Connection::GetSocket(id).Close(); Connection::SetStatus(id,ILLEGAL); Connection::Free(id); + LogF( + "Connection lost. [ID %u]" + ,id + ); return; } } @@ -1031,6 +1174,8 @@ } // print_server_met(); + load_part_met(IncomingDirectory+"/"+"004.part.met"); + u32 clienttcpportid=Connection::Alloc(); rc=ClientTcpListen(clienttcpportid,DEFAULT_CLIENTPORT); @@ -1059,6 +1204,11 @@ ip=server[serverindex].ip; port=server[serverindex].port; } + LogF( + "Trying to connect to %u.%u.%u.%u:%u ... [ID %u]" + ,(ip>>24)&255,(ip>>16)&255,(ip>>8)&255,ip&255 + ,port,serverid + ); rc=ServerTcpConnect(serverid,ip,port); printf( "ServerTcpConnect(ip=%u.%u.%u.%u,port=%u)=%i\n" @@ -1068,7 +1218,13 @@ ,(unsigned)((ip )&255) ,(unsigned)port,rc ); - if (!rc) Connection::Free(serverid); + if (!rc) { + Connection::Free(serverid); + LogF( + "Connection could not be established. [ID %u]" + ,serverid + ); + } } } |