Exemplo n.º 1
0
        public CServerHello(MemoryStream buffer)
        {
            DonkeyHeader header;
            BinaryWriter writer=new BinaryWriter(buffer);
            header=new DonkeyHeader((byte)Protocol.ServerCommand.LoginRequest,writer);
            writer.Write(CKernel.Preferences.GetByteArray("UserHash"));
            writer.Write(CKernel.Preferences.GetUInt("ID"));
            writer.Write(CKernel.Preferences.GetUShort("TCPPort"));
            uint nParameters=5;
            writer.Write(nParameters);
            // user
            new ParameterWriter((byte)Protocol.ClientParameter.Name,CKernel.Preferences.GetString("UserName"),writer);
            // version
            new ParameterWriter((byte)Protocol.ClientParameter.Version,Protocol.EDONKEYVERSION,writer);
            // port
            new ParameterWriter((byte)Protocol.ClientParameter.Port,(uint)CKernel.Preferences.GetUShort("TCPPort"),writer);
            // compression
            new ParameterWriter((byte)Protocol.ClientParameter.Compression,5,writer); //1 (supports compression) + 5 (supports alternative server ports)
            // emule version
            new ParameterWriter((byte)Protocol.ClientParameter.EmuleVersion,Protocol.EMULE_VERSION_COMPLEX,writer);

            header.Packetlength=(uint)writer.BaseStream.Length-header.Packetlength+1;
            writer.Seek(0,SeekOrigin.Begin);
            header.Serialize(writer);
        }
Exemplo n.º 2
0
        public CMuleHello(MemoryStream buffer,bool isResponse)
        {
            BinaryWriter writer=new BinaryWriter(buffer);
            DonkeyHeader header;
            if (isResponse)
                header=new DonkeyHeader((byte)Protocol.ClientCommandExt.eMuleInfoAnswer,writer,Protocol.ProtocolType.eMule);
            else
                header=new DonkeyHeader((byte)Protocol.ClientCommandExt.eMuleInfo,writer,Protocol.ProtocolType.eMule);
            writer.Write(Protocol.EMULE_VERSION);
            writer.Write(Protocol.EMULE_PROTOCOL_VERSION);
            uint nParameters=8;
            writer.Write(nParameters);
            // compression
            new ParameterWriter(Protocol.ET_COMPRESSION,Protocol.EMULE_VERSION_COMPRESION,writer);
            // udp port
            new ParameterWriter(Protocol.ET_UDPPORT,(uint)CKernel.Preferences.GetUShort("UDPPort"),writer);
            // version udp
            new ParameterWriter(Protocol.ET_UDPVER,(uint)Protocol.EMULE_VERSION_UDP,writer);
            // version source exchange
            new ParameterWriter(Protocol.ET_SOURCEEXCHANGE,(uint)Protocol.EMULE_VERSION_SOURCEEXCHANGE,writer);
            // version comments
            new ParameterWriter(Protocol.ET_COMMENTS,(uint)Protocol.EMULE_VERSION_COMMENTS,writer);
            // version extended requests
            new ParameterWriter(Protocol.ET_EXTENDEDREQUEST,(uint)Protocol.EMULE_VERSION_EXTENDEDREQUEST,writer);
            // client compatible
            new ParameterWriter(Protocol.ET_COMPATIBLECLIENT,(byte)Protocol.Client.eLePhant,writer);
            // version lphant
            new ParameterWriter(Protocol.ET_ELEPHANTVERSION,Protocol.ELEPHANT_VERSION,writer);

            header.Packetlength=(uint)writer.BaseStream.Length-header.Packetlength+1;
            writer.Seek(0,SeekOrigin.Begin);
            header.Serialize(writer);
        }
Exemplo n.º 3
0
 public CServerAskSources(byte[] FileHash,MemoryStream buffer)
 {
     DonkeyHeader header;
     BinaryWriter writer=new BinaryWriter(buffer);
     writer.Seek(0,SeekOrigin.End);
     header=new DonkeyHeader((byte)Protocol.ServerCommand.GetSources/*,writer*/);
     header.Packetlength=17;
     header.Serialize(writer);
     writer.Write(FileHash);
     //			header.Packetlength=(uint)writer.BaseStream.Length-header.Packetlength+1;
     //			writer.Seek(0,SeekOrigin.Begin);
     //			header.Serialize(writer);
 }
Exemplo n.º 4
0
 public CServerRequestCallback(uint ID,MemoryStream buffer)
 {
     DonkeyHeader header;
     BinaryWriter writer=new BinaryWriter(buffer);
     header=new DonkeyHeader((byte)Protocol.ServerCommand.CallBackRequest,writer);
     writer.Write(ID);
     header.Packetlength=(uint)writer.BaseStream.Length-header.Packetlength+1;
     writer.Seek(0,SeekOrigin.Begin);
     header.Serialize(writer);
 }
Exemplo n.º 5
0
 public CFileInfo(byte[] in_FileHash,string ClientFileName,MemoryStream buffer)
 {
     BinaryWriter writer=new BinaryWriter(buffer);
     DonkeyHeader header=new DonkeyHeader((byte)Protocol.ClientCommand.FileRequestAnswer,writer);
     writer.Write(in_FileHash,0,16);
     writer.Write((ushort)ClientFileName.ToCharArray().Length);
     writer.Write(ClientFileName.ToCharArray());
     header.Packetlength=(uint)writer.BaseStream.Length-header.Packetlength+1;
     writer.Seek(0,SeekOrigin.Begin);
     header.Serialize(writer);
 }
Exemplo n.º 6
0
        public CFileStatus(byte[] in_FileHash,byte[] in_Partes,MemoryStream buffer)
        {
            BinaryWriter writer=new BinaryWriter(buffer);
            DonkeyHeader header=new DonkeyHeader((byte)Protocol.ClientCommand.FileState,writer);
            writer.Write(in_FileHash,0,16);

            WriteFileStatus(writer,in_Partes);

            header.Packetlength=(uint)writer.BaseStream.Length-header.Packetlength+1;
            writer.Seek(0,SeekOrigin.Begin);
            header.Serialize(writer);
        }
Exemplo n.º 7
0
 public CSharedFiles(MemoryStream buffer,CFilesList fileList)
 {
     BinaryWriter writer=new BinaryWriter(buffer);
     DonkeyHeader header=new DonkeyHeader((byte)Protocol.ClientCommand.AskSharedFilesAnswer,writer);
     writer.Write((uint)fileList.Count);
     lock(fileList)
     {
         foreach (CElement Elemento in fileList.Values)
         {
             writer.Write(Elemento.File.FileHash);
             writer.Write((uint)0);
             writer.Write((ushort)0);
             writer.Write(Elemento.File.FileType);
             // file name
             new ParameterWriter((byte)Protocol.FileTag.Name,Elemento.File.FileName,writer);
             // file size
             new ParameterWriter((byte)Protocol.FileTag.Size,Elemento.File.FileSize,writer);
         }
     }
     header.Packetlength = (uint)writer.BaseStream.Length-header.Packetlength+1;
     writer.Seek(0,SeekOrigin.Begin);
     header.Serialize(writer);
 }
Exemplo n.º 8
0
 public CCrumbSetResponse(MemoryStream buffer,CFile File)
 {
     BinaryWriter writer=new BinaryWriter(buffer);
     DonkeyHeader header=new DonkeyHeader((byte)Protocol.ClientCommand.HashSetAnswer,writer);
     writer.Write(File.FileHash);
     writer.Write((byte)1);
     foreach (byte[] PartialHash in File.HashSet)
     {
         writer.Write(PartialHash);
     }
     writer.Write((byte)1);
     foreach (byte[] CrumbHash in File.CrumbsHashSet)
     {
         writer.Write(CrumbHash);
     }
     header.Packetlength=(uint)writer.BaseStream.Length-header.Packetlength+1;
     writer.Seek(0,SeekOrigin.Begin);
     header.Serialize(writer);
 }
Exemplo n.º 9
0
        public CSendBlock(MemoryStream data,uint start,uint end,byte[] FileHash,ref ArrayList UploadDataPackets)
        {
            MemoryStream buffer;
            uint size;
            BinaryReader reader=new BinaryReader(data);
            BinaryWriter writer;
            //byte[] aux_buffer;
            while (start!=end)
            {
                buffer=new MemoryStream();
                writer=new BinaryWriter(buffer);
                DonkeyHeader header=new DonkeyHeader((byte)Protocol.ClientCommand.SendingPart,writer);
                writer.Write(FileHash);
                if (end-start>10240) size=10240;
                else size=end-start;
                writer.Write(start);
                writer.Write(start+size);
                //aux_buffer=reader.ReadBytes((int)size);
                //writer.Write(aux_buffer);
                writer.Write(reader.ReadBytes((int)size));

                start+=size;
                header.Packetlength=(uint)writer.BaseStream.Length-header.Packetlength+1;
                writer.Seek(0,SeekOrigin.Begin);
                header.Serialize(writer);
                UploadDataPackets.Add(buffer);
                writer=null;
                buffer=null;
            }
            reader.Close();
            data.Close();
            data=null;
            reader=null;
        }
Exemplo n.º 10
0
        //private byte opcode;
        public CSendHello(bool response,MemoryStream buffer,CServer server, bool sendCompatOptions, bool allowPartner)
        {
            DonkeyHeader header;
            BinaryWriter writer=new BinaryWriter(buffer);
            if (response)
                header=new DonkeyHeader((byte)Protocol.ClientCommand.HelloAnswer,writer);
            else
            {
                header=new DonkeyHeader((byte)Protocol.ClientCommand.Hello,writer);
                writer.Write((byte)CKernel.Preferences.GetByteArray("UserHash").Length);
            }
            writer.Write(CKernel.Preferences.GetByteArray("UserHash"));
            writer.Write(CKernel.Preferences.GetUInt("ID"));
            writer.Write(CKernel.Preferences.GetUShort("TCPPort"));
            uint nParametros=5;
            if (sendCompatOptions) nParametros++;
            writer.Write(nParametros);
            // username
            new ParameterWriter((byte)Protocol.ClientParameter.Name,CKernel.Preferences.GetString("UserName"),writer);
            // version
            new ParameterWriter((byte)Protocol.ClientParameter.Version,Protocol.EDONKEYVERSION,writer);
            //ParameterNumericNumeric port=new ParameterWriter((byte)Protocol.ClientParameter.Port,(uint)CKernel.Preferences.GetUShort("TCPPort"),writer);
            // emule version
            new ParameterWriter((byte)Protocol.ClientParameter.EmuleVersion,Protocol.EMULE_VERSION_COMPLEX,writer);
            // upd port
            new ParameterWriter((byte)Protocol.ClientParameter.EmuleUDPPort,(uint)CKernel.Preferences.GetUShort("UDPPort"),writer);
            // emule flags
            new ParameterWriter((byte)Protocol.ClientParameter.Emule_MiscOptions1,
            //				(						<< 4*7) |
                (Protocol.EMULE_VERSION_UDP				<< 4*6) |
                (Protocol.EMULE_VERSION_COMPRESION		<< 4*5) |
                (0 /*secureID */						<< 4*4) |
                (Protocol.EMULE_VERSION_SOURCEEXCHANGE	<< 4*3) |
                (Protocol.EMULE_VERSION_EXTENDEDREQUEST	<< 4*2) |
                (Protocol.EMULE_VERSION_COMMENTS		<< 4*1) |
            //				(										<< 1*3) |
                ((((Types.Constants.AllowViewShared)CKernel.Preferences.GetEnum("AllowViewShared",Types.Constants.AllowViewShared.Nobody)==Types.Constants.AllowViewShared.Nobody) ? (uint)1 : (uint)0 )<< 1*2) |
                (0 /*uMultiPacket*/						<< 1*1) |
                (0 /*uSupportPreview*/					<< 1*0)
                ,writer);
            uint compatValue=1;

            if (sendCompatOptions)
            {
                if (allowPartner) compatValue=3;
                //CLog.Log(Types.Constants.Log.Verbose,"Sent partner flag "+compatValue.ToString());
                new ParameterWriter((byte)Protocol.ClientParameter.EmuleCompatOptions,compatValue,writer);
            }

            if (server==null)
            {
                writer.Write((uint)0);
                writer.Write((ushort)0);
            }
            else
            {
                writer.Write(server.IP);
                writer.Write(server.MainPort);
            }
            header.Packetlength=(uint)writer.BaseStream.Length-header.Packetlength+1;
            writer.Seek(0,SeekOrigin.Begin);
            header.Serialize(writer);
        }
Exemplo n.º 11
0
 public CRequestSourceExchange(MemoryStream buffer,byte[] FileHash)
 {
     BinaryWriter writer = new BinaryWriter(buffer);
     DonkeyHeader header = new DonkeyHeader((byte)Protocol.ClientCommandExt.SourcesRequest,Protocol.ProtocolType.eMule);
     header.Packetlength=17;
     header.Serialize(writer);
     writer.Write(FileHash);
     //			header.Packetlength=(uint)writer.BaseStream.Length-header.Packetlength+1;
     //			writer.Seek(0,SeekOrigin.Begin);
     //			header.Serialize(writer);
 }
Exemplo n.º 12
0
 public CNoFile(MemoryStream buffer,byte[] FileHash)
 {
     BinaryWriter writer=new BinaryWriter(buffer);
     DonkeyHeader header=new DonkeyHeader((byte)Protocol.ClientCommand.FileRequestAnswerNoFile,writer);
     writer.Write(FileHash);
     header.Packetlength=(uint)writer.BaseStream.Length-header.Packetlength+1;
     writer.Seek(0,SeekOrigin.Begin);
     header.Serialize(writer);
 }
Exemplo n.º 13
0
 public CMuleQR(MemoryStream buffer,ushort QR)
 {
     BinaryWriter writer=new BinaryWriter(buffer);
     DonkeyHeader header;
     header=new DonkeyHeader((byte)Protocol.ClientCommandExt.QueueRanking,writer,Protocol.ProtocolType.eMule);
     writer.Write(QR);
     byte[] empty=new byte[10];
     writer.Write(empty);
     header.Packetlength=(uint)writer.BaseStream.Length-header.Packetlength+1;
     writer.Seek(0,SeekOrigin.Begin);
     header.Serialize(writer);
 }
Exemplo n.º 14
0
        public CCompressedPacket(ref MemoryStream packet)
        {
            byte[] compressedBuffer=null;

            BinaryReader reader=new BinaryReader(packet);
            MemoryStream compressedPacket=new MemoryStream();
            packet.Seek(0,SeekOrigin.Begin);
            BinaryWriter writer=new BinaryWriter(compressedPacket);

            writer.Write(reader.ReadBytes(5));
            byte opcode=reader.ReadByte();
            writer.Write(opcode);
            byte[] uncompressedBuffer=reader.ReadBytes((int)packet.Length-6);
            int compressedsize=CCompressedBlockSend.ComprimirBuffer(uncompressedBuffer,ref compressedBuffer);
            if (compressedsize+6>=packet.Length)
                return;
            writer.Write(compressedBuffer);
            compressedPacket.Seek(0,SeekOrigin.Begin);

            DonkeyHeader header=new DonkeyHeader(opcode,writer,Protocol.ProtocolType.Packet);
            header.Packetlength=(uint)writer.BaseStream.Length-header.Packetlength+1;
            writer.Seek(0,SeekOrigin.Begin);
            header.Serialize(writer);
            packet.Close();
            packet=compressedPacket;
        }
Exemplo n.º 15
0
        public CSourceExchangeResponse(MemoryStream buffer,byte[] FileHash,uint RequesterID,ushort RequesterPort, int version)
        {
            //Hashtable IPList=new Hashtable();
            ArrayList clientsList=new ArrayList();
            BinaryWriter writer=new BinaryWriter(buffer);
            DonkeyHeader header=new DonkeyHeader((byte)Protocol.ClientCommandExt.SourcesResult,writer,Protocol.ProtocolType.eMule);
            CElement Element=(CElement)CKernel.FilesList[FileHash];
            nSources=0;
            writer.Write(FileHash);
            writer.Write(nSources);
            uint myID=CKernel.Preferences.GetUInt("ID");
            if ((Element!=null)&&(Element.SourcesList!=null))
            {
                lock(Element.SourcesList.SyncRoot())
                {
                    foreach (CClient Client in Element.SourcesList)
                    {
                        if ((Client.DownloadState != Protocol.DownloadState.None)&&
                            (Client.UserID>Protocol.LowIDLimit)&&
                            (Client.UserHash!=null)&&
                            (Client.UserID!=RequesterID)&&
                            (Client.UserID!=myID))
                        {
                            //IPList[((ulong)Client.UserID << 16) + Client.Port]=((ulong)Client.ServerIP<<16) + Client.ServerPort;
                            clientsList.Add(Client);
                            nSources++;
                        }
                        if (nSources>=200) break;
                    }
                }
                if (nSources<200)
                {
                    int i=0;
                    CClient QueueClient;
                    while (i<CKernel.Queue.Count)
                    {
                        QueueClient=(CClient)CKernel.Queue[i];
                        if ((QueueClient.UploadElement==Element)&&
                            (QueueClient.UserID > Protocol.LowIDLimit)&&
                            (QueueClient.DownloadState==Protocol.DownloadState.OnQueue)&&
                            (QueueClient.UserHash!=null)&&
                            (QueueClient.UserID!=RequesterID)&&
                            (QueueClient.UserID!=myID))
                        {
                            //IPList[((ulong)QueueClient.UserID<<16)+QueueClient.Port]=((ulong)QueueClient.ServerIP<<16)+QueueClient.ServerPort;
                            if (!clientsList.Contains(QueueClient)) clientsList.Add(QueueClient);
                            nSources++;
                        }
                        if (nSources>=200) break;
                        i++;
                    }
                }

            }
            /*			//do not send oursef
            IPList.Remove(((ulong)CKernel.Preferences.GetUInt("ID")<<16)+CKernel.Preferences.GetUShort("TCPPort"));
            //do not send himself
            IPList.Remove(((ulong)RequesterID<<16)+RequesterPort);
            foreach (ulong ipPort in IPList.Keys)
            {
                uint IP=(uint)(ipPort>>16);
                ushort Port=(ushort)(ipPort&0x00000000FFFF);
                //ushort Port=(ushort)(ipPort-((ulong)IP<<16));

                ulong ipServerPort=(ulong)IPList[ipPort];
                uint ServerIP=(uint)(ipServerPort>>16);
                ushort ServerPort=(ushort)(ipServerPort&0x00000000FFFF);

                writer.Write(IP);
                writer.Write(Port);
                writer.Write(ServerIP);
                writer.Write(ServerPort);
            }
            */
            foreach (CClient client in clientsList)
            {
                writer.Write(client.UserID);
                writer.Write(client.Port);
                writer.Write(client.ServerIP);
                writer.Write(client.ServerPort);
                if (version>1)
                    writer.Write(client.UserHash);
            }
            header.Packetlength=(uint)writer.BaseStream.Length-header.Packetlength+1;
            writer.Seek(0,SeekOrigin.Begin);
            header.Serialize(writer);
            writer.Seek(16,SeekOrigin.Current);
            writer.Write((ushort)clientsList.Count);
        }
Exemplo n.º 16
0
 public CCrumbSetRequest(byte[] Hash,MemoryStream buffer)
 {
     BinaryWriter writer=new BinaryWriter(buffer);
     DonkeyHeader header=new DonkeyHeader((byte)Protocol.ClientCommand.CrumbSetRequest,writer);
     writer.Write(Hash,0,16);
     header.Packetlength=(uint)writer.BaseStream.Length-header.Packetlength+1;
     writer.Seek(0,SeekOrigin.Begin);
     header.Serialize(writer);
 }
Exemplo n.º 17
0
 public CStartDownload(MemoryStream buffer, byte[] in_FileHash)
 {
     BinaryWriter writer=new BinaryWriter(buffer);
     DonkeyHeader header=new DonkeyHeader((byte)Protocol.ClientCommand.StartUploadRequest,writer);
     if (in_FileHash!=null) writer.Write(in_FileHash,0,16);
     header.Packetlength=(uint)writer.BaseStream.Length-header.Packetlength+1;
     writer.Seek(0,SeekOrigin.Begin);
     header.Serialize(writer);
 }
Exemplo n.º 18
0
 public CFileBlockRequest(MemoryStream buffer,byte[] FileHash,ref CFileBlock block1,ref CFileBlock block2,ref CFileBlock block3)
 {
     BinaryWriter writer=new BinaryWriter(buffer);
     DonkeyHeader header=new DonkeyHeader((byte)Protocol.ClientCommand.RequestParts,writer);
     writer.Write(FileHash);
     writer.Write(block1.start);
     writer.Write(block2.start);
     writer.Write(block3.start);
     writer.Write(block1.end+1);
     writer.Write(block2.end+1);
     writer.Write(block3.end+1);
     header.Packetlength=(uint)writer.BaseStream.Length-header.Packetlength+1;
     writer.Seek(0,SeekOrigin.Begin);
     header.Serialize(writer);
 }
Exemplo n.º 19
0
 public CStartUpload(MemoryStream buffer)
 {
     BinaryWriter writer=new BinaryWriter(buffer);
     DonkeyHeader header=new DonkeyHeader((byte)Protocol.ClientCommand.AcceptUploadRequest,writer);
     header.Packetlength=(uint)writer.BaseStream.Length-header.Packetlength+1;
     writer.Seek(0,SeekOrigin.Begin);
     header.Serialize(writer);
 }
Exemplo n.º 20
0
        public CFileRequest(byte[] Hash,byte[] Partes,MemoryStream buffer)
        {
            uint startpos=(uint)buffer.Position;
            BinaryWriter writer=new BinaryWriter(buffer);
            DonkeyHeader header=new DonkeyHeader((byte)Protocol.ClientCommand.FileRequest,writer);
            writer.Write(Hash,0,16);

            WriteFileStatus(Partes,writer);

            header.Packetlength=(uint)buffer.Position-startpos-header.Packetlength+1;
            writer.Seek(0,SeekOrigin.Begin);
            header.Serialize(writer);
        }
Exemplo n.º 21
0
 public CCancelTransfer(MemoryStream buffer)
 {
     BinaryWriter writer=new BinaryWriter(buffer);
     DonkeyHeader header=new DonkeyHeader((byte)Protocol.ClientCommand.CancelTransfer,writer);
     header.Packetlength=(uint)writer.BaseStream.Length-header.Packetlength+1;
     writer.Seek(0,SeekOrigin.Begin);
     header.Serialize(writer);
 }
Exemplo n.º 22
0
 //constructor to publish one file
 public CServerPublishFiles(MemoryStream buffer,CElement element)
 {
     DonkeyHeader header;
     BinaryWriter writer=new BinaryWriter(buffer);
     header=new DonkeyHeader((byte)Protocol.ServerCommand.OfferFiles,writer);
     writer.Write(1);
     m_AddFileToPacket(writer,element);
     header.Packetlength=(uint)writer.BaseStream.Length-header.Packetlength+1;
     writer.Seek(0,SeekOrigin.Begin);
     header.Serialize(writer);
     writer.Write(1);
     CLog.Log(Types.Constants.Log.Info,"FIL_PUBLISHED",1);
 }
Exemplo n.º 23
0
 public CClientMessage(MemoryStream buffer,string message)
 {
     BinaryWriter writer=new BinaryWriter(buffer);
     DonkeyHeader header=new DonkeyHeader((byte)Protocol.ClientCommand.Message,writer,Protocol.ProtocolType.eDonkey);
     if (message.Length>128) Message=message.Substring(0,128);
     byte[] byteStringValue=Encoding.Default.GetBytes(message);
     writer.Write((ushort)byteStringValue.Length);
     writer.Write(byteStringValue);
     header.Packetlength=(uint)writer.BaseStream.Length-header.Packetlength+1;
     writer.Seek(0,SeekOrigin.Begin);
     header.Serialize(writer);
 }
Exemplo n.º 24
0
 public CServerQueryMoreResults(MemoryStream buffer)
 {
     DonkeyHeader header;
     BinaryWriter writer=new BinaryWriter(buffer);
     header=new DonkeyHeader((byte)Protocol.ServerCommand.QueryMoreResults,writer);
     header.Packetlength=(uint)writer.BaseStream.Length-header.Packetlength+1;
     writer.Seek(0,SeekOrigin.Begin);
     header.Serialize(writer);
 }
Exemplo n.º 25
0
 public CComment(MemoryStream buffer,byte rating,string comment)
 {
     BinaryWriter writer=new BinaryWriter(buffer);
     DonkeyHeader header=new DonkeyHeader((byte)Protocol.ClientCommandExt.FileDescription,writer,Protocol.ProtocolType.eMule);
     writer.Write(rating);
     if (comment.Length>128) comment=comment.Substring(0,128);
     byte[] byteStringValue=Encoding.Default.GetBytes(comment);
     writer.Write((uint)byteStringValue.Length);
     writer.Write(byteStringValue);
     header.Packetlength=(uint)writer.BaseStream.Length-header.Packetlength+1;
     writer.Seek(0,SeekOrigin.Begin);
     header.Serialize(writer);
 }
Exemplo n.º 26
0
        public CServerRequestSearch(MemoryStream buffer,string searchString,bool matchAnyWords,string type,uint maxSize,uint minSize,uint avaibility, string exclude, bool esUDP)
        {
            DonkeyHeader header=new DonkeyHeader((byte)Protocol.ServerCommand.SearchRequest);
            BinaryWriter writer=new BinaryWriter(buffer);
            if (esUDP)
            {
                writer.Write((byte)Protocol.Header.eDonkey);
                //writer.Write((byte)Protocol.ServerCommandsUDP.GlobalSearchRequest);
                writer.Write((byte)Protocol.ServerCommandUDP.GlobalSearchRequest2);
            }
            else
                header.Serialize(writer);

            if (type=="Any") type="";
            //header
            int parametercount=0; //must be written parametercount-1 parmeter headers

            if (exclude.Length>0)
                writer.Write(notParameter);

            if ( searchString.Length>0)
            {
                parametercount++;
                if (parametercount>1)
                    writer.Write(andParameter);
            }

            if (type.Length>0)
            {
                parametercount++;
                if (parametercount>1)
                    writer.Write(andParameter);
            }

            if (minSize>0)
            {
                parametercount++;
                if (parametercount>1)
                    writer.Write(andParameter);
            }

            if (maxSize>0)
            {
                parametercount++;
                if (parametercount>1)
                    writer.Write(andParameter);
            }

            if (avaibility>0)
            {
                parametercount++;
                if (parametercount>1)
                    writer.Write(andParameter);
            }

            //				if (extension.GetLength()>0)
            //				{
            //				parametercount++;
            //				if (parametercount>1)
            //					writer.Write(andParameter);
            //				}
            //body
            if (searchString.Length>0) //search a string
            {
                //writer.Write(notParameter);

                string[] list=searchString.Split(" ".ToCharArray());
                byte[] searchbyte;
                if ((matchAnyWords)&&(list.Length>1))
                {
                    for (int i=0;i<list.Length;i++)
                    {
                        if (i!=list.Length-1) writer.Write(orParameter);
                        writer.Write(stringParameter); //write the parameter type
                        searchbyte=Encoding.Default.GetBytes(list[i]);
                        writer.Write((ushort)searchbyte.Length);
                        writer.Write(searchbyte);
                    }
                }
                else
                {
                        writer.Write(stringParameter); //write the parameter type
                        searchbyte=Encoding.Default.GetBytes(searchString);
                        writer.Write((ushort)searchbyte.Length);
                        writer.Write(searchbyte);
                }
            }

            if (type.Length>0)
            {
                writer.Write(typeParameter); //write the parameter type
                byte[] searchbyte=Encoding.Default.GetBytes(type);
                writer.Write((ushort)searchbyte.Length);
                writer.Write(searchbyte);
                writer.Write(typeNemonic2);
                writer.Write(typeNemonic1);
            }

            if (minSize>0)
            {
                writer.Write(numericParameter); //write the parameter type
                writer.Write(minSize);		   //write the parameter
                writer.Write(minNemonic);    //nemonic for this kind of parameter
            }

            if (maxSize>0)
            {
                writer.Write(numericParameter); //write the parameter type
                writer.Write(maxSize);		   //write the parameter
                writer.Write(maxNemonic);    //nemonic for this kind of parameter
            }

            if (avaibility>0)
            {
                writer.Write(numericParameter); //write the parameter type
                writer.Write(avaibility);    //write the parameter
                writer.Write(avaibilityNemonic);  //nemonic for this kind of parameter
            }

            //				if (extension.GetLength()>0)
            //				{
            //					data.Write(&stringParameter,1); //write the parameter type
            //					nSize=extension.GetLength();
            //					data.Write(&nSize,2);   //write the length
            //					formatC=extension.GetBuffer();
            //					data.Write(formatC,nSize); //write parameter
            //					data.Write(&extensionNemonic,3); //nemonic for this kind of parameter (only 3 bytes!!)
            //				}
            if (exclude.Length>0)
            {
                writer.Write(stringParameter); //write the parameter type
                byte[] searchbyte2=Encoding.Default.GetBytes(exclude);
                writer.Write((ushort)searchbyte2.Length);
                writer.Write(searchbyte2);
            }

            if (!esUDP)
            {
                header.Packetlength=(uint)writer.BaseStream.Length-header.Packetlength+1;
                writer.Seek(0,SeekOrigin.Begin);
                header.Serialize(writer);
            }
        }
Exemplo n.º 27
0
        public CCompressedBlockSend(byte[] data,uint start,uint end,byte[] FileHash,ref ArrayList UploadDataPackets)
        {
            //			byte[] buffercomp=new byte[final-inicio+300];
            //			Deflater descompresor=new Deflater();
            //			descompresor.SetInput(datos);
            //			descompresor.Flush();
            //			int compressedsize=descompresor.Deflate(buffercomp,0,(int)(final-inicio)+300);
            //			descompresor.Finish();
            byte[] buffercomp=null;
            int compressedsize=ComprimirBuffer(data,ref buffercomp);
            if (compressedsize>=end-start)
            {
                buffercomp=null;
                MemoryStream strmdatos=new MemoryStream(data);
                CSendBlock EnvioBloque=new CSendBlock(strmdatos,start,end,FileHash,ref UploadDataPackets);
                return;
            }
            Debug.Write("Compressed comp:"+compressedsize.ToString()+" win: "+Convert.ToString(end-start-compressedsize)+"\n");
            MemoryStream datosComp=new MemoryStream(buffercomp);
            end=start+(uint)compressedsize;
            MemoryStream buffer;
            uint size;
            BinaryReader reader=new BinaryReader(datosComp);
            BinaryWriter writer;
            byte[] aux_buffer;
            while (start!=end)
            {
                buffer=new MemoryStream();
                writer=new BinaryWriter(buffer);
                DonkeyHeader header=new DonkeyHeader((byte)Protocol.ClientCommandExt.CompressedPart,writer,Protocol.ProtocolType.eMule);
                writer.Write(FileHash);
                if (end-start>10240) size=10240;
                else size=end-start;
                writer.Write(start);
                writer.Write(compressedsize);
                aux_buffer=reader.ReadBytes((int)size);
                writer.Write(aux_buffer);

                start+=size;
                header.Packetlength=(uint)writer.BaseStream.Length-header.Packetlength+1;
                writer.Seek(0,SeekOrigin.Begin);
                header.Serialize(writer);
                UploadDataPackets.Add(buffer);
                writer=null;
                buffer=null;
            }
            reader.Close();
            datosComp.Close();
            datosComp=null;
            reader=null;
        }
Exemplo n.º 28
0
 //constructor to publish a list of files
 public CServerPublishFiles(MemoryStream buffer,CFilesList FilesList,uint startIndex)
 {
     DonkeyHeader header;
     LastFilePublished=startIndex;
     BinaryWriter writer=new BinaryWriter(buffer);
     header=new DonkeyHeader((byte)Protocol.ServerCommand.OfferFiles,writer);
     writer.Write((uint)FilesList.Count);
     uint nfiles=0;
     int fileIndex=0;
     foreach (CElement element in FilesList.Values)
     {
         fileIndex++;
         if (fileIndex<startIndex) continue;
         if (m_AddFileToPacket(writer,element)) nfiles++;
         LastFilePublished++;
         if (nfiles>=200) break; //TODO: check server soft limits
     }
     header.Packetlength=(uint)writer.BaseStream.Length-header.Packetlength+1;
     writer.Seek(0,SeekOrigin.Begin);
     header.Serialize(writer);
     writer.Write(nfiles);
     CLog.Log(Types.Constants.Log.Info,"FIL_PUBLISHED",nfiles);
 }
Exemplo n.º 29
0
 private bool m_ProcessReceptionStream()
 {
     if (m_HeaderPending)
     {
         if (m_ReceptionStream.Length-m_StreamPosition>=6)
         {
             m_ReceptionStream.Seek(m_StreamPosition,SeekOrigin.Begin);
             m_DonkeyHeader=new DonkeyHeader(m_ReceptionStream);
             if ((m_DonkeyHeader.Packetlength>200000)||((m_DonkeyHeader.eDonkeyID!=Protocol.ProtocolType.eDonkey)
                 &&(m_DonkeyHeader.eDonkeyID!=Protocol.ProtocolType.eMule)
                 &&(m_DonkeyHeader.eDonkeyID!=Protocol.ProtocolType.Packet)))
             {
                 OnConnectionFail((byte)Protocol.ConnectionReason.InvalidHeader);
                 return false;
             }
             m_HeaderPending=false;
             m_ReceptionStream.Seek(0,SeekOrigin.End);
         }
     }
     if ((!m_HeaderPending)&&
         ((m_ReceptionStream.Length-m_StreamPosition)>=m_DonkeyHeader.Packetlength+5))
     {
         m_ReceptionStream.Seek(m_StreamPosition+6,SeekOrigin.Begin);
         byte[] paquete=new byte[m_DonkeyHeader.Packetlength-1];
         m_ReceptionStream.Read(paquete,0,(int)m_DonkeyHeader.Packetlength-1);
         m_HeaderPending=true;
         if (m_DonkeyHeader.eDonkeyID==Protocol.ProtocolType.eDonkey)
             ProcessDonkeyPacket(ref paquete);
         else if (m_DonkeyHeader.eDonkeyID==Protocol.ProtocolType.eMule) ProcessMulePacket(ref paquete);
         else if (m_DonkeyHeader.eDonkeyID==Protocol.ProtocolType.Packet)
         {
             CReceivedCompressedBlock.Uncompress(ref paquete);
             ProcessMulePacket(ref paquete);
         }
         m_DonkeyHeader=null;
         if (m_ReceptionStream.Position==m_ReceptionStream.Length)
         {
             //m_ReceptionStream=new MemoryStream(buffSize);
             m_ReceptionStream.SetLength(0);
             m_StreamPosition=0;
         }
         else
         {
             if (m_ReceptionStream.Length>10240)
             {
                 byte[] tmpbuffer=new byte[m_ReceptionStream.Length-m_ReceptionStream.Position];
                 m_ReceptionStream.Read(tmpbuffer,0,tmpbuffer.Length);
                 //						m_ReceptionStream.Close();
                 //						MemoryStream nuevoStream=new MemoryStream();
                 //						nuevoStream.Write(tmpbuffer,0,tmpbuffer.Length);
                 //						m_ReceptionStream=nuevoStream;
                 m_ReceptionStream.SetLength(0);
                 m_ReceptionStream.Write(tmpbuffer,0,tmpbuffer.Length);
                 m_StreamPosition=0;
                 return true;
             }
             else
             {
                 m_StreamPosition=m_ReceptionStream.Position;
                 //m_ReceptionStream.Seek(0,SeekOrigin.End);
                 return true;
             }
         }
     }
     if (m_ReceptionStream.Position!=m_ReceptionStream.Length)
     {
         m_ReceptionStream.Seek(0,SeekOrigin.End);
     }
     return false;
 }