コード例 #1
0
        public bool AddServer(ED2KServer pServer, bool bAddTail)
        {
            if (!IsGoodServerIP(pServer))
            {
                // check for 0-IP, localhost and optionally for LAN addresses
                return(false);
            }

            if (MuleApplication.Instance.Preference.IsFilterServerByIP)
            {
                // IP-Filter: We don't need to reject dynIP-servers here. After the DN was
                // resolved, the IP will get filtered and the server will get removed. This applies
                // for TCP-connections as well as for outgoing UDP-packets because for both protocols
                // we resolve the DN and filter the received IP.
                //if (pServer.HasDynIP())
                //	return false;
                if (MuleApplication.Instance.IPFilter.IsFiltered(pServer.IP))
                {
                    return(false);
                }
            }

            ED2KServer pFoundServer = GetServerByAddress(pServer.Address, pServer.Port);

            // Avoid duplicate (dynIP) servers: If the server which is to be added, is a dynIP-server
            // but we don't know yet it's DN, we need to search for an already available server with
            // that IP.
            if (pFoundServer == null && pServer.IP != 0)
            {
                pFoundServer = GetServerByIPTCP(pServer.IP, pServer.Port);
            }

            if (pFoundServer != null)
            {
                pFoundServer.ResetFailedCount();
                return(false);
            }

            if (bAddTail)
            {
                servers_.Add(pServer);
            }
            else
            {
                servers_.Insert(0, pServer);
            }
            return(true);
        }
コード例 #2
0
ファイル: UDPSocketImpl.cs プロジェクト: mheydt/mpddotnet
        private bool ProcessPacket(byte[] buffer, int offset, int size, uint opcode, uint nIP, ushort nUDPPort)
        {
            try
            {
                MuleApplication.Instance.Statistics.AddDownDataOverheadServer((uint)size);
                ED2KServer pServer = MuleApplication.Instance.ServerList.GetServerByIPUDP(nIP, nUDPPort, true);
                if (pServer != null)
                {
                    pServer.ResetFailedCount();
                }

                switch ((OperationCodeEnum)opcode)
                {
                case OperationCodeEnum.OP_GLOBSEARCHRES:
                {
                    SafeMemFile data = MpdObjectManager.CreateSafeMemFile(buffer, size);
                    // process all search result packets
                    int iLeft;
                    do
                    {
                        uint uResultCount = MuleApplication.Instance.SearchList.ProcessUDPSearchAnswer(data, true /*pServer.GetUnicodeSupport()*/, nIP, nUDPPort - 4);

                        // check if there is another source packet
                        iLeft = (int)(data.Length - data.Position);
                        if (iLeft >= 2)
                        {
                            byte protocol = data.ReadUInt8();
                            iLeft--;
                            if (protocol != MuleConstants.PROTOCOL_EDONKEYPROT)
                            {
                                data.Seek(-1, System.IO.SeekOrigin.Current);
                                iLeft += 1;
                                break;
                            }

                            byte opcode1 = data.ReadUInt8();
                            iLeft--;
                            if (opcode1 != (byte)OperationCodeEnum.OP_GLOBSEARCHRES)
                            {
                                data.Seek(-2, System.IO.SeekOrigin.Current);
                                iLeft += 2;
                                break;
                            }
                        }
                    }while (iLeft > 0);
                    break;
                }

                case OperationCodeEnum.OP_GLOBFOUNDSOURCES:
                {
                    SafeMemFile data = MpdObjectManager.CreateSafeMemFile(buffer, size);
                    // process all source packets
                    int iLeft;
                    do
                    {
                        byte[] fileid = new byte[16];
                        data.ReadHash16(fileid);
                        PartFile file = MuleApplication.Instance.DownloadQueue.GetFileByID(fileid);
                        if (file != null)
                        {
                            file.AddSources(data, nIP, (ushort)(nUDPPort - 4), false);
                        }
                        else
                        {
                            // skip sources for that file
                            uint count = data.ReadUInt8();
                            data.Seek(count * (4 + 2), System.IO.SeekOrigin.Current);
                        }

                        // check if there is another source packet
                        iLeft = (int)(data.Length - data.Position);
                        if (iLeft >= 2)
                        {
                            byte protocol = data.ReadUInt8();
                            iLeft--;
                            if (protocol != MuleConstants.PROTOCOL_EDONKEYPROT)
                            {
                                data.Seek(-1, System.IO.SeekOrigin.Current);
                                iLeft += 1;
                                break;
                            }

                            byte opcode1 = data.ReadUInt8();
                            iLeft--;
                            if (opcode1 != (byte)OperationCodeEnum.OP_GLOBFOUNDSOURCES)
                            {
                                data.Seek(-2, System.IO.SeekOrigin.Current);
                                iLeft += 2;
                                break;
                            }
                        }
                    }while (iLeft > 0);

                    break;
                }

                case OperationCodeEnum.OP_GLOBSERVSTATRES:
                {
                    if (size < 12 || pServer == null)
                    {
                        return(true);
                    }
                    uint challenge = BitConverter.ToUInt32(buffer, 0);
                    if (challenge != pServer.Challenge)
                    {
                        return(true);
                    }
                    if (pServer != null)
                    {
                        pServer.Challenge             = 0;
                        pServer.CryptPingReplyPending = false;
                        uint   tNow = MpdUtilities.Time();
                        Random rand = new Random();
                        // if we used Obfuscated ping, we still need to reset the time properly
                        pServer.LastPingedTime =
                            Convert.ToUInt32(tNow - (rand.Next() % MuleConstants.ONE_HOUR_SEC));
                    }
                    uint   cur_user            = BitConverter.ToUInt32(buffer, 4);
                    uint   cur_files           = BitConverter.ToUInt32(buffer, 8);
                    uint   cur_maxusers        = 0;
                    uint   cur_softfiles       = 0;
                    uint   cur_hardfiles       = 0;
                    uint   uUDPFlags           = 0;
                    uint   uLowIDUsers         = 0;
                    uint   dwServerUDPKey      = 0;
                    ushort nTCPObfuscationPort = 0;
                    ushort nUDPObfuscationPort = 0;

                    if (size >= 16)
                    {
                        cur_maxusers = BitConverter.ToUInt32(buffer, 12);
                    }
                    if (size >= 24)
                    {
                        cur_softfiles = BitConverter.ToUInt32(buffer, 16);
                        cur_hardfiles = BitConverter.ToUInt32(buffer, 20);
                    }
                    if (size >= 28)
                    {
                        uUDPFlags = BitConverter.ToUInt32(buffer, 24);
                    }
                    if (size >= 32)
                    {
                        uLowIDUsers = BitConverter.ToUInt32(buffer, 28);
                    }
                    if (size >= 40)
                    {
                        // TODO debug check if this packet was encrypted if it has a key
                        nUDPObfuscationPort = BitConverter.ToUInt16(buffer, 32);
                        nTCPObfuscationPort = BitConverter.ToUInt16(buffer, 34);;
                        dwServerUDPKey      = BitConverter.ToUInt32(buffer, 36);
                    }
                    if (pServer != null)
                    {
                        pServer.Ping               = MpdUtilities.GetTickCount() - pServer.LastPinged;
                        pServer.UserCount          = cur_user;
                        pServer.FileCount          = cur_files;
                        pServer.MaxUsers           = cur_maxusers;
                        pServer.SoftFiles          = cur_softfiles;
                        pServer.HardFiles          = cur_hardfiles;
                        pServer.ServerKeyUDP       = dwServerUDPKey;
                        pServer.ObfuscationPortTCP = nTCPObfuscationPort;
                        pServer.ObfuscationPortUDP = nUDPObfuscationPort;
                        // if the received UDP flags do not match any already stored UDP flags,
                        // reset the server version string because the version (which was determined by last connecting to
                        // that server) is most likely not accurat any longer.
                        // this may also give 'false' results because we don't know the UDP flags when connecting to a server
                        // with TCP.
                        //if (pServer.GetUDPFlags() != uUDPFlags)
                        //	pServer.Version(_T = "");
                        pServer.UDPFlags   = (ED2KServerUdpFlagsEnum)uUDPFlags;
                        pServer.LowIDUsers = uLowIDUsers;

                        pServer.SetLastDescPingedCount(false);
                        if (pServer.LastDescPingedCount < 2)
                        {
                            // eserver 16.45+ supports a new OP_SERVER_DESC_RES answer, if the OP_SERVER_DESC_REQ contains a uint
                            // challenge, the server returns additional info with OP_SERVER_DESC_RES. To properly distinguish the
                            // old and new OP_SERVER_DESC_RES answer, the challenge has to be selected carefully. The first 2 bytes
                            // of the challenge (in network byte order) MUST NOT be a valid string-len-int16!
                            Packet packet1 =
                                MuleApplication.Instance.NetworkObjectManager.CreatePacket(OperationCodeEnum.OP_SERVER_DESC_REQ, 4);
                            uint uDescReqChallenge =
                                ((uint)MpdUtilities.GetRandomUInt16() << 16) +
                                MuleConstants.INV_SERV_DESC_LEN;         // 0xF0FF = an 'invalid' string length.
                            pServer.DescReqChallenge = uDescReqChallenge;
                            Array.Copy(BitConverter.GetBytes(uDescReqChallenge), packet1.Buffer, 4);
                            MuleApplication.Instance.Statistics.AddUpDataOverheadServer(packet1.Size);
                            MuleApplication.Instance.ServerConnect.SendUDPPacket(packet1, pServer, true);
                        }
                        else
                        {
                            pServer.SetLastDescPingedCount(true);
                        }
                    }
                    break;
                }

                case OperationCodeEnum.OP_SERVER_DESC_RES:
                {
                    if (pServer == null)
                    {
                        return(true);
                    }

                    // old packet: <name_len 2><name name_len><desc_len 2 desc_en>
                    // new packet: <challenge 4><taglist>
                    //
                    // NOTE: To properly distinguish between the two packets which are both useing the same opcode...
                    // the first two bytes of <challenge> (in network byte order) have to be an invalid <name_len> at least.

                    SafeMemFile srvinfo = MpdObjectManager.CreateSafeMemFile(buffer, size);
                    if (size >= 8 && BitConverter.ToUInt16(buffer, 0) == MuleConstants.INV_SERV_DESC_LEN)
                    {
                        if (pServer.DescReqChallenge != 0 && BitConverter.ToUInt32(buffer, 0) == pServer.DescReqChallenge)
                        {
                            pServer.DescReqChallenge = 0;
                            srvinfo.ReadUInt32();         // skip challenge
                            uint uTags = srvinfo.ReadUInt32();
                            for (uint i = 0; i < uTags; i++)
                            {
                                Tag tag = MpdObjectManager.CreateTag(srvinfo, true /*pServer.GetUnicodeSupport()*/);
                                if (tag.NameID == MuleConstants.ST_SERVERNAME && tag.IsStr)
                                {
                                    pServer.ServerName = tag.Str;
                                }
                                else if (tag.NameID == MuleConstants.ST_DESCRIPTION && tag.IsStr)
                                {
                                    pServer.Description = tag.Str;
                                }
                                else if (tag.NameID == MuleConstants.ST_DYNIP && tag.IsStr)
                                {
                                    // Verify that we really received a DN.
                                    IPAddress address;


                                    if (!IPAddress.TryParse(tag.Str, out address) ||
                                        address == IPAddress.None)
                                    {
                                        string strOldDynIP = pServer.DynIP;
                                        pServer.DynIP = tag.Str;
                                        // If a dynIP-server changed its address or, if this is the
                                        // first time we get the dynIP-address for a server which we
                                        // already have as non-dynIP in our list, we need to remove
                                        // an already available server with the same 'dynIP:port'.
                                        if (string.Compare(strOldDynIP, pServer.DynIP, true) != 0)
                                        {
                                            MuleApplication.Instance.ServerList.RemoveDuplicatesByAddress(pServer);
                                        }
                                    }
                                }
                                else if (tag.NameID == MuleConstants.ST_VERSION && tag.IsStr)
                                {
                                    pServer.Version = tag.Str;
                                }
                                else if (tag.NameID == MuleConstants.ST_VERSION && tag.IsInt)
                                {
                                    pServer.Version =
                                        string.Format("{0}.{1}", tag.Int >> 16, tag.Int & 0xFFFF);
                                }
                            }
                        }
                        else
                        {
                            // A server sent us a new server description packet (including a challenge) although we did not
                            // ask for it. This may happen, if there are multiple servers running on the same machine with
                            // multiple IPs. If such a server is asked for a description, the server will answer 2 times,
                            // but with the same IP.
                        }
                    }
                    else
                    {
                        string strName = srvinfo.ReadString(true /*pServer.GetUnicodeSupport()*/);
                        string strDesc = srvinfo.ReadString(true /*pServer.GetUnicodeSupport()*/);
                        pServer.Description = strDesc;
                        pServer.ServerName  = strName;
                    }

                    break;
                }

                default:
                    return(false);
                }

                return(true);
            }
            catch (Exception error)
            {
                ProcessPacketError((uint)size, (uint)opcode, nIP, nUDPPort, error);
                if (opcode == (byte)OperationCodeEnum.OP_GLOBSEARCHRES ||
                    opcode == (byte)OperationCodeEnum.OP_GLOBFOUNDSOURCES)
                {
                    return(true);
                }
            }
            return(false);
        }
コード例 #3
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);
                }
            }
        }