Exemplo n.º 1
0
        public bool UnPackPacket(uint uMaxDecompressedSize)
        {
            Debug.Assert(Protocol == MuleConstants.PROTOCOL_PACKEDPROT ||
                         Protocol == MuleConstants.PROTOCOL_KADEMLIAPACKEDPROT);

            byte[] unpack = null;

            bool result = MpdUtilities.Decompress(Buffer, Size, out unpack);

            if (result && unpack.Length < uMaxDecompressedSize)
            {
                Debug.Assert(Buffer != null);
                Size   = (uint)unpack.Length;
                Buffer = unpack;
                if (Protocol == MuleConstants.PROTOCOL_KADEMLIAPACKEDPROT)
                {
                    Protocol = MuleConstants.PROTOCOL_KADEMLIAHEADER;
                }
                else
                {
                    Protocol = MuleConstants.PROTOCOL_EMULEPROT;
                }

                m_bPacked = false;

                return(true);
            }
            return(false);
        }
Exemplo n.º 2
0
        protected override void OnReceive(int nErrorCode)
        {
            byte[]   buffer   = new byte[5000];
            EndPoint endPoint = null;

            int        nRealLen   = ReceiveFrom(buffer, ref endPoint);
            IPEndPoint ipEndPoint = endPoint as IPEndPoint;

            uint dwIP = 0;

            if (ipEndPoint != null)
            {
                dwIP = BitConverter.ToUInt32(ipEndPoint.Address.GetAddressBytes(), 0);
            }
            if (ipEndPoint != null &&
                !(MuleApplication.Instance.IPFilter.IsFiltered(dwIP) ||
                  MuleApplication.Instance.ClientList.IsBannedClient(dwIP)))
            {
                byte[] pBuffer;
                uint   nReceiverVerifyKey;
                uint   nSenderVerifyKey;

                int nPacketLen = DecryptReceivedClient(buffer, nRealLen,
                                                       out pBuffer, dwIP,
                                                       out nReceiverVerifyKey, out nSenderVerifyKey);

                if (nPacketLen >= 1)
                {
                    try
                    {
                        switch (pBuffer[0])
                        {
                        case MuleConstants.PROTOCOL_EMULEPROT:
                        {
                            if (nPacketLen >= 2)
                            {
                                ProcessPacket(pBuffer, 2, (uint)nPacketLen - 2,
                                              pBuffer[1], dwIP,
                                              (ushort)IPAddress.NetworkToHostOrder(ipEndPoint.Port));
                            }
                            else
                            {
                                throw new MuleException("eMule packet too short");
                            }
                            break;
                        }

                        case MuleConstants.PROTOCOL_KADEMLIAPACKEDPROT:
                        {
                            MuleApplication.Instance.Statistics.AddDownDataOverheadKad((uint)nPacketLen);
                            if (nPacketLen >= 2)
                            {
                                byte[] unpack    = null;
                                byte[] unpackTmp = null;

                                if (MpdUtilities.Decompress(pBuffer, 2, (uint)nPacketLen - 2, out unpackTmp))
                                {
                                    unpack = new byte[unpackTmp.Length + 2];
                                    Array.Copy(unpackTmp, 0, unpack, 2, unpackTmp.Length);

                                    unpack[0] = MuleConstants.PROTOCOL_KADEMLIAHEADER;
                                    unpack[1] = pBuffer[1];
                                    try
                                    {
                                        MuleApplication.Instance.KadEngine.ProcessPacket(unpack,
                                                                                         (uint)unpack.Length,
                                                                                         dwIP, (ushort)IPAddress.NetworkToHostOrder(ipEndPoint.Port),
                                                                                         MuleApplication.Instance.KadEngine.Preference.GetUDPVerifyKey(dwIP) == nReceiverVerifyKey,
                                                                                         MuleApplication.Instance.KadObjectManager.CreateKadUDPKey(nSenderVerifyKey,
                                                                                                                                                   (uint)MuleApplication.Instance.GetPublicIP(false)));
                                    }
                                    catch
                                    {
                                        throw;
                                    }
                                }
                                else
                                {
                                    throw new MuleException("Failed to uncompress Kad packet!");
                                }
                            }
                            else
                            {
                                throw new MuleException("Kad packet (compressed) too short");
                            }
                            break;
                        }

                        case MuleConstants.PROTOCOL_KADEMLIAHEADER:
                        {
                            MuleApplication.Instance.Statistics.AddDownDataOverheadKad((uint)nPacketLen);
                            if (nPacketLen >= 2)
                            {
                                MuleApplication.Instance.KadEngine.ProcessPacket(pBuffer, (uint)nPacketLen,
                                                                                 dwIP, (ushort)ipEndPoint.Port,
                                                                                 MuleApplication.Instance.KadEngine.Preference.GetUDPVerifyKey(dwIP) == nReceiverVerifyKey,
                                                                                 MuleApplication.Instance.KadObjectManager.CreateKadUDPKey(nSenderVerifyKey,
                                                                                                                                           (uint)MuleApplication.Instance.GetPublicIP(false)));
                            }
                            else
                            {
                                throw new MuleException("Kad packet too short");
                            }
                            break;
                        }

                        default:
                        {
                            throw new MuleException(string.Format("Unknown protocol 0x{0}", pBuffer[0]));
                        }
                        }
                    }
                    catch (Exception error)
                    {
                        MpdUtilities.DebugLogError(error);
                    }
                }
            }
        }