Ejemplo n.º 1
0
 private void WriteCreditStruct(SafeMemFile memfile, CreditStruct creditStruct)
 {
     memfile.WriteUInt32(creditStruct.nUploadedLo);      // uploaded TO him
     memfile.WriteUInt32(creditStruct.nDownloadedLo);    // downloaded from him
     memfile.WriteUInt32(creditStruct.nLastSeen);
     memfile.WriteUInt32(creditStruct.nUploadedHi);      // upload high 32
     memfile.WriteUInt32(creditStruct.nDownloadedHi);    // download high 32
     memfile.WriteUInt16(creditStruct.nReserved3);
     memfile.WriteUInt8(creditStruct.nKeySize);
     memfile.Write(creditStruct.abySecureIdent);
 }
Ejemplo n.º 2
0
        public void ConnectionEstablished(Mule.Network.ServerSocket sender)
        {
            if (!IsConnecting)
            {
                // we are already IsConnected to another server
                DestroySocket(sender);
                return;
            }

            InitLocalIP();
            if (sender.ConnectionState == ConnectionStateEnum.CS_WAITFORLOGIN)
            {
                ED2KServer pServer =
                    MuleApplication.Instance.ServerList.GetServerByAddress(sender.CurrentServer.Address,
                                                                           sender.CurrentServer.Port);
                if (pServer != null)
                {
                    pServer.ResetFailedCount();
                }

                // Send login packet
                SafeMemFile data = MpdObjectManager.CreateSafeMemFile(256);
                data.WriteHash16(MuleApplication.Instance.Preference.UserHash);
                data.WriteUInt32(ClientID);
                data.WriteUInt16(MuleApplication.Instance.Preference.Port);

                uint tagcount = 4;
                data.WriteUInt32(tagcount);

                Tag tagName = MpdObjectManager.CreateTag(TagTypeEnum.CT_NAME,
                                                         MuleApplication.Instance.Preference.UserNick);
                tagName.WriteTagToFile(data);

                Tag tagVersion = MpdObjectManager.CreateTag(TagTypeEnum.CT_VERSION, VersionsEnum.EDONKEYVERSION);
                tagVersion.WriteTagToFile(data);

                ServerFlagsEnum dwCryptFlags = 0;
                if (MuleApplication.Instance.Preference.IsClientCryptLayerSupported)
                {
                    dwCryptFlags |= ServerFlagsEnum.SRVCAP_SUPPORTCRYPT;
                }
                if (MuleApplication.Instance.Preference.IsClientCryptLayerRequested)
                {
                    dwCryptFlags |= ServerFlagsEnum.SRVCAP_REQUESTCRYPT;
                }
                if (MuleApplication.Instance.Preference.IsClientCryptLayerRequired)
                {
                    dwCryptFlags |= ServerFlagsEnum.SRVCAP_REQUIRECRYPT;
                }

                Tag tagFlags = MpdObjectManager.CreateTag(TagTypeEnum.CT_SERVER_FLAGS,
                                                          ServerFlagsEnum.SRVCAP_ZLIB | ServerFlagsEnum.SRVCAP_NEWTAGS |
                                                          ServerFlagsEnum.SRVCAP_LARGEFILES |
                                                          ServerFlagsEnum.SRVCAP_UNICODE | dwCryptFlags);

                tagFlags.WriteTagToFile(data);

                // eMule Version (14-Mar-2004: requested by lugdunummaster (need for LowID clients which have no chance
                // to send an Hello packet to the server during the callback test))
                Tag tagMuleVersion = MpdObjectManager.CreateTag(TagTypeEnum.CT_EMULE_VERSION,
                                                                (MuleApplication.Instance.Version.Major << 17) |
                                                                (MuleApplication.Instance.Version.Minor << 10) |
                                                                (MuleApplication.Instance.Version.Build << 7));
                tagMuleVersion.WriteTagToFile(data);

                Packet packet = MuleApplication.Instance.NetworkObjectManager.CreatePacket(data);
                packet.OperationCode = OperationCodeEnum.OP_LOGINREQUEST;
                MuleApplication.Instance.Statistics.AddUpDataOverheadServer(packet.Size);
                SendPacket(packet, true, sender);
            }
            else if (sender.ConnectionState == ConnectionStateEnum.CS_CONNECTED)
            {
                MuleApplication.Instance.Statistics.Reconnects++;
                MuleApplication.Instance.Statistics.ServerConnectTime = MpdUtilities.GetTickCount();
                IsConnected      = true;
                connectedSocket_ = sender;
                StopConnectionTry();
                MuleApplication.Instance.SharedFiles.ClearED2KPublishInfo();
                MuleApplication.Instance.SharedFiles.SendListToServer();

                if (MuleApplication.Instance.Preference.DoesAddServersFromServer)
                {
                    Packet packet =
                        MuleApplication.Instance.NetworkObjectManager.CreatePacket(
                            OperationCodeEnum.OP_GETSERVERLIST, 0);
                    MuleApplication.Instance.Statistics.AddUpDataOverheadServer(packet.Size);
                    SendPacket(packet, true);
                }
            }
        }
Ejemplo n.º 3
0
        private bool ProcessPacket(byte[] packet, uint offset, uint size,
                                   byte opcode, uint ip, ushort port)
        {
            switch ((OperationCodeEnum)opcode)
            {
            case OperationCodeEnum.OP_REASKCALLBACKUDP:
            {
                MuleApplication.Instance.Statistics.AddDownDataOverheadOther(size);
                UpDownClient buddy = MuleApplication.Instance.ClientList.Buddy;
                if (buddy != null)
                {
                    if (size < 17 || buddy.ClientSocket == null)
                    {
                        break;
                    }
                    if (MpdUtilities.Md4Cmp(packet, (int)offset, buddy.BuddyID, 0) == 0)
                    {
                        Array.Copy(BitConverter.GetBytes(ip), 0,
                                   packet, offset + 10, 4);
                        Array.Copy(BitConverter.GetBytes(port), 0,
                                   packet, offset + 14, 2);
                        Packet response =
                            MuleApplication.Instance.NetworkObjectManager.CreatePacket(MuleConstants.PROTOCOL_EMULEPROT);
                        response.OperationCode = OperationCodeEnum.OP_REASKCALLBACKTCP;
                        response.Buffer        = new byte[size];
                        Array.Copy(packet, offset + 10, response.Buffer, 0, size - 10);
                        response.Size = size - 10;
                        MuleApplication.Instance.Statistics.AddUpDataOverheadFileRequest(response.Size);
                        buddy.SendPacket(response, true);
                    }
                }
                break;
            }

            case OperationCodeEnum.OP_REASKFILEPING:
            {
                MuleApplication.Instance.Statistics.AddDownDataOverheadFileRequest(size);
                SafeMemFile data_in     = MpdObjectManager.CreateSafeMemFile(packet, offset, size);
                byte[]      reqfilehash = new byte[16];
                data_in.ReadHash16(reqfilehash);
                KnownFile reqfile = MuleApplication.Instance.SharedFiles.GetFileByID(reqfilehash);

                bool         bSenderMultipleIpUnknown = false;
                UpDownClient sender =
                    MuleApplication.Instance.UploadQueue.GetWaitingClientByIP_UDP(ip, port,
                                                                                  true, ref bSenderMultipleIpUnknown);

                if (reqfile == null)
                {
                    Packet response =
                        MuleApplication.Instance.NetworkObjectManager.CreatePacket(OperationCodeEnum.OP_FILENOTFOUND,
                                                                                   0, MuleConstants.PROTOCOL_EMULEPROT);
                    MuleApplication.Instance.Statistics.AddUpDataOverheadFileRequest(response.Size);
                    if (sender != null)
                    {
                        SendPacket(response, ip, port, sender.ShouldReceiveCryptUDPPackets,
                                   sender.UserHash, false, 0);
                    }
                    else
                    {
                        SendPacket(response, ip, port, false, null, false, 0);
                    }
                    break;
                }

                if (sender != null)
                {
                    //Make sure we are still thinking about the same file
                    if (MpdUtilities.Md4Cmp(reqfilehash, sender.UploadFileID) == 0)
                    {
                        sender.AddAskedCount();
                        sender.SetLastUpRequest();
                        //I messed up when I first added extended info to UDP
                        //I should have originally used the entire ProcessExtenedInfo the first time.
                        //So now I am forced to check UDPVersion to see if we are sending all the extended info.
                        //For now on, we should not have to change anything here if we change
                        //anything to the extended info data as this will be taken care of in ProcessExtendedInfo()
                        //Update extended info.
                        if (sender.UDPVersion > 3)
                        {
                            sender.ProcessExtendedInfo(data_in, reqfile);
                        }
                        //Update our complete source counts.
                        else if (sender.UDPVersion > 2)
                        {
                            ushort nCompleteCountLast = sender.UpCompleteSourcesCount;
                            ushort nCompleteCountNew  = data_in.ReadUInt16();
                            sender.UpCompleteSourcesCount = nCompleteCountNew;
                            if (nCompleteCountLast != nCompleteCountNew)
                            {
                                reqfile.UpdatePartsInfo();
                            }
                        }
                        SafeMemFile data_out = MpdObjectManager.CreateSafeMemFile(128);
                        if (sender.UDPVersion > 3)
                        {
                            if (reqfile.IsPartFile)
                            {
                                ((PartFile)reqfile).WritePartStatus(data_out);
                            }
                            else
                            {
                                data_out.WriteUInt16(0);
                            }
                        }
                        data_out.WriteUInt16((ushort)(MuleApplication.Instance.UploadQueue.GetWaitingPosition(sender)));
                        Packet response =
                            MuleApplication.Instance.NetworkObjectManager.CreatePacket(data_out,
                                                                                       MuleConstants.PROTOCOL_EMULEPROT);
                        response.OperationCode = OperationCodeEnum.OP_REASKACK;
                        MuleApplication.Instance.Statistics.AddUpDataOverheadFileRequest(response.Size);
                        SendPacket(response, ip, port, sender.ShouldReceiveCryptUDPPackets,
                                   sender.UserHash, false, 0);
                    }
                }
                else
                {
                    // Don't answer him. We probably have him on our queue already, but can't locate him. Force him to establish a TCP connection
                    if (!bSenderMultipleIpUnknown)
                    {
                        if (((uint)MuleApplication.Instance.UploadQueue.WaitingUserCount + 50) >
                            MuleApplication.Instance.Preference.QueueSize)
                        {
                            Packet response =
                                MuleApplication.Instance.NetworkObjectManager.CreatePacket(OperationCodeEnum.OP_QUEUEFULL,
                                                                                           0, MuleConstants.PROTOCOL_EMULEPROT);
                            MuleApplication.Instance.Statistics.AddUpDataOverheadFileRequest(response.Size);
                            SendPacket(response, ip, port, false, null, false, 0);         // we cannot answer this one encrypted since we dont know this client
                        }
                    }
                }
                break;
            }

            case OperationCodeEnum.OP_QUEUEFULL:
            {
                MuleApplication.Instance.Statistics.AddDownDataOverheadFileRequest(size);
                UpDownClient sender = MuleApplication.Instance.DownloadQueue.GetDownloadClientByIP_UDP(ip, port, true);
                if (sender != null && sender.UDPPacketPending)
                {
                    sender.IsRemoteQueueFull = true;
                    sender.UDPReaskACK(0);
                }
                break;
            }

            case OperationCodeEnum.OP_REASKACK:
            {
                MuleApplication.Instance.Statistics.AddDownDataOverheadFileRequest(size);
                UpDownClient sender =
                    MuleApplication.Instance.DownloadQueue.GetDownloadClientByIP_UDP(ip, port, true);
                if (sender != null && sender.UDPPacketPending)
                {
                    SafeMemFile data_in = MpdObjectManager.CreateSafeMemFile(packet, size);
                    if (sender.UDPVersion > 3)
                    {
                        sender.ProcessFileStatus(true, data_in, sender.RequestFile);
                    }
                    ushort nRank = data_in.ReadUInt16();
                    sender.IsRemoteQueueFull = false;
                    sender.UDPReaskACK(nRank);
                    sender.AddAskedCountDown();
                }

                break;
            }

            case OperationCodeEnum.OP_FILENOTFOUND:
            {
                MuleApplication.Instance.Statistics.AddDownDataOverheadFileRequest(size);
                UpDownClient sender =
                    MuleApplication.Instance.DownloadQueue.GetDownloadClientByIP_UDP(ip, port, true);
                if (sender != null && sender.UDPPacketPending)
                {
                    sender.UDPReaskFNF();         // may delete 'sender'!
                    sender = null;
                }

                break;
            }

            case OperationCodeEnum.OP_PORTTEST:
            {
                MuleApplication.Instance.Statistics.AddDownDataOverheadOther(size);
                if (size == 1)
                {
                    if (packet[0] == 0x12)
                    {
                        bool ret = MuleApplication.Instance.ListenSocket.SendPortTestReply('1', true);
                    }
                }
                break;
            }

            case OperationCodeEnum.OP_DIRECTCALLBACKREQ:
            {
                if (!MuleApplication.Instance.ClientList.AllowCalbackRequest(ip))
                {
                    break;
                }
                // do we accept callbackrequests at all?
                if (MuleApplication.Instance.KadEngine.IsRunning &&
                    MuleApplication.Instance.KadEngine.IsFirewalled)
                {
                    MuleApplication.Instance.ClientList.AddTrackCallbackRequests(ip);
                    SafeMemFile data           = MpdObjectManager.CreateSafeMemFile(packet, size);
                    ushort      nRemoteTCPPort = data.ReadUInt16();
                    byte[]      uchUserHash    = new byte[16];
                    data.ReadHash16(uchUserHash);
                    byte         byConnectOptions = data.ReadUInt8();
                    UpDownClient pRequester       =
                        MuleApplication.Instance.ClientList.FindClientByUserHash(uchUserHash, ip,
                                                                                 nRemoteTCPPort);
                    if (pRequester == null)
                    {
                        pRequester =
                            MuleApplication.Instance.CoreObjectManager.CreateUpDownClient(null,
                                                                                          nRemoteTCPPort, ip, 0, 0, true);
                        pRequester.UserHash = uchUserHash;
                        MuleApplication.Instance.ClientList.AddClient(pRequester);
                    }
                    pRequester.SetConnectOptions(byConnectOptions, true, false);
                    pRequester.DoesDirectUDPCallbackSupport = false;
                    pRequester.IP       = ip;
                    pRequester.UserPort = nRemoteTCPPort;
                    pRequester.TryToConnect();
                }

                break;
            }

            default:
                MuleApplication.Instance.Statistics.AddDownDataOverheadOther(size);
                return(false);
            }
            return(true);
        }