Пример #1
0
        private void m_ListenerProc()
        {
            Socket edonkeySocket;

            while ((m_ServerThread.IsAlive) && (base.Active))
            {
                edonkeySocket = null;
                try
                {
                    edonkeySocket          = base.AcceptSocket();
                    edonkeySocket.Blocking = false;
                }
                catch
                {
                }
                if (edonkeySocket != null)
                {
                    CConnection connection = null;
                    if ((CKernel.IPFilter == null) || (!CKernel.IPFilter.BlockIP(BitConverter.ToUInt32(((IPEndPoint)edonkeySocket.RemoteEndPoint).Address.GetAddressBytes(), 0))))
                    {
                        connection = new CConnection(edonkeySocket);
                    }
                    else
                    {
                        CLog.Log(Types.Constants.Log.Verbose, "IP_BLOCKED", ((IPEndPoint)edonkeySocket.RemoteEndPoint).Address.ToString(), CKernel.IPFilter.BlockedIPDescription);
                        edonkeySocket.Shutdown(SocketShutdown.Both);
                        edonkeySocket.Close();
                    }
                }
            }
        }
Пример #2
0
 public void Add(CConnection cn)
 {
     if (m_Closing)
     {
         cn.CloseSocket();
         return;
     }
     m_ArrayList.Add(cn);
 }
Пример #3
0
 public void Remove(CConnection cn)
 {
     if (m_Closing)
     {
         cn.CloseSocket();
         return;
     }
     m_ArrayList.Remove(cn);
 }
Пример #4
0
 public void Add(CConnection cn)
 {
     if (m_Closing)
     {
         cn.CloseSocket();
         return;
     }
     m_ArrayList.Add(cn);
 }
Пример #5
0
        /// <summary>
        /// Process incomming eDonkey packet.
        /// </summary>
        /// <param name="packet">Packet which need to be processed.</param>
        protected virtual void ProcessDonkeyPacket(ref byte[] packet)
        {
            try
            {
                switch ((Protocol.ClientCommand)m_DonkeyHeader.Command)
                {
                case Protocol.ClientCommand.Hello:
                    if (m_Client == null)
                    {
                        CReceiveHello HelloPacket = new CReceiveHello(false, new MemoryStream(packet));
                        m_Client = CKernel.ClientsList.GetClient(HelloPacket.UserID, HelloPacket.UserPort, HelloPacket.ServerIP, HelloPacket.Hash);
                        if (m_Client == null)
                        {
                            m_Client = new CClient(this);
                            CKernel.ClientsList.AddClient(m_Client);
                        }
                        else
                        {
                            if ((m_Client.connection != null) && (m_Client.connection.Connected) && (m_Client.connection != this))
                            {
                                m_Client.connection.CloseConnection();
                                Debug.WriteLine("Client conexion!=null");
                            }
                            m_Client.connection = this;
                        }
                    }
                    CClient     cant              = m_Client;
                    CConnection conant            = m_Client.connection;
                    bool        NeedExtendRequest = !m_Client.ProcessHello(false, packet);
                    if ((NeedExtendRequest) && (m_Client.EmuleProtocol) && (CKernel.Preferences.GetBool("UseEmuleProtocol")))
                    {
                        m_Client.SendEmuleHello(false);
                    }
                    m_Client.ResponseHello();
                    break;

                case Protocol.ClientCommand.FileRequest:
                    m_Client.ProcessFileRequest(packet);
                    break;

                case Protocol.ClientCommand.HelloAnswer:
                    m_Client.ProcessHello(true, packet);
                    break;

                case Protocol.ClientCommand.SetRequestFileID:
                    m_Client.ProcessRequestFileID(packet);
                    break;

                case Protocol.ClientCommand.FileRequestAnswer:
                    m_Client.ProcessFileInfo(packet);
                    break;

                case Protocol.ClientCommand.FileState:
                    m_Client.ProcessChunksStatus(packet, false /*tcp*/);
                    break;

                case Protocol.ClientCommand.HashSetAnswer:
                    m_Client.ProcessHashSet(packet);
                    break;

                case Protocol.ClientCommand.HashSetRequest:
                    m_Client.SendHashSet();
                    break;

                case Protocol.ClientCommand.AcceptUploadRequest:
                    if (m_Client.DownloadState == Protocol.DownloadState.OnQueue)
                    {
                        m_Client.RequestBlocks();
                    }
                    else
                    {
                        if (m_Client.DownFileHash != null)
                        {
//								CLog.Log(Constants.Log.Verbose,"Trying to resume download {2}: {0},hashdescarga=null? {1}",m_Client.DownloadState,(m_Client.DownFileHash==null),m_Client.UserName);
                            m_Client.RequestBlocks();
                        }
                        else
                        {
                            if (m_Client.UpFileHash != null)
                            {
#if DEBUG
                                CLog.Log(Constants.Log.Verbose, "Recovering source to download " + m_Client.UserName);
#endif
                                m_Client.DownFileHash = m_Client.UpFileHash;
                                m_Client.RequestBlocks();
                            }

                            else
                            {
                                CSourceOld sourceRecovered = CKernel.SourcesOld.GetSourceOld(m_Client.UserHash);
                                if (sourceRecovered != null)
                                {
                                    m_Client.DownFileHash   = sourceRecovered.FileHash;
                                    m_Client.DownFileChunks = sourceRecovered.FileChunks;
                                    m_Client.RequestBlocks();
#if DEBUG
                                    CLog.Log(Constants.Log.Verbose, "RECOVERED SOURCE from sources cache " + m_Client.UserName);
#endif
                                }
#if DEBUG
                                else
                                {
                                    CLog.Log(Constants.Log.Verbose, "download lost: {0}", m_Client.UserName);
                                }
#endif
                            }
                        }
                    }
                    break;

                case Protocol.ClientCommand.OutOfPartRequest:
                    m_Client.ProcessOutOfParts();
                    break;

                case Protocol.ClientCommand.StartUploadRequest:
                    m_Client.ProcessStartUploadRequest(packet);
                    break;

                case Protocol.ClientCommand.RequestParts:
                    m_Client.ProcessRequestParts(packet);
                    break;

                case Protocol.ClientCommand.EndOfDownload:
                case Protocol.ClientCommand.CancelTransfer:
                    m_Client.CancelUploadTransfer(false);
                    break;

                case Protocol.ClientCommand.SendingPart:
                    m_Client.ReceiveBlock(packet);
                    break;

                case Protocol.ClientCommand.QueueRank:
                    m_Client.ProcessQR(packet);
                    break;

                case Protocol.ClientCommand.FileRequestAnswerNoFile:
                    m_Client.FileNotFound(packet);
                    break;

                case Protocol.ClientCommand.ChangeClientID:
                    m_Client.ProcessIDChange(packet);
                    break;

                case Protocol.ClientCommand.ChangeSlot:
                    break;

                case Protocol.ClientCommand.AskSharedFiles:
                    Constants.AllowViewShared allow = (Constants.AllowViewShared)CKernel.Preferences.GetEnum("AllowViewShared", Constants.AllowViewShared.Nobody);
                    if (allow == Constants.AllowViewShared.All)
                    {
                        m_Client.ProcessSharedListRequest();
                    }
                    break;

                case Protocol.ClientCommand.AskSharedFilesAnswer:
                    m_Client.ProcessSharedListResponse(packet);
                    break;

                case Protocol.ClientCommand.Message:
                    m_Client.ProcessChatMessage(packet);
                    break;

                case Protocol.ClientCommand.CrumbSetResponse:
                    m_Client.ProcessCrumbSetResponse(packet);
                    break;

                default:
                    CLog.Log(Constants.Log.Verbose, "CLI_UNK_DONK_TCP_PACKET", m_DonkeyHeader.Command);
                    break;
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
#if DEBUG
                if (m_DonkeyHeader != null)
                {
                    CLog.Log(Constants.Log.Verbose, "CLI_PAQ_DONK_ERROR", m_DonkeyHeader.Command);
                }
#endif
            }
            packet = null;
            return;
        }
Пример #6
0
        public void OnDisconnect(byte reason)
        {
            connection=null;
            if (m_SharedFilesRequested) CLog.Log(Constants.Log.Notify,"CLI_DONT_SEND_SHARED",m_UserName);
            m_SharedFilesRequested=false;
            #if VERBOSE
            CKernel.LogClient.AddLog("OnDisconnect-Client",m_ID);
            #endif
            #if DEBUG
            string strReason=CKernel.Globalization["UNK_REASON"];

            switch((Protocol.ConnectionReason)reason)
            {
                case Protocol.ConnectionReason.CannotConnect:
                    strReason=CKernel.Globalization["CANT_CONNECT"];
                    break;
                case Protocol.ConnectionReason.ClosedConnection:
                    strReason=CKernel.Globalization["CONNECTION_CLOSED"];
                    break;
                case Protocol.ConnectionReason.NullPacket:
                    strReason=CKernel.Globalization["NULL_PACKET"];
                    break;
                case Protocol.ConnectionReason.NullID:
                    strReason=CKernel.Globalization["NULL_ID"];
                    break;
                case Protocol.ConnectionReason.InvalidHeader:
                    strReason=CKernel.Globalization["INVALID_HEADER"];
                    break;
                case Protocol.ConnectionReason.TimeOut:
                    strReason=CKernel.Globalization["TIMEOUT"];
                    break;
            }
            #endif
            #if VERBOSE
            CKernel.LogClient.AddLog("OnDisconnect-"+strReason,m_ID);
            #endif
            if (m_ChatMessages!=null)
            {
                m_ChatMessages.Clear();
                m_ChatMessages=null;
            }

            if (m_UploadState==Protocol.UploadState.Uploading)
            {
                CancelUploadTransfer(true);
            }

            if ((m_UploadState==Protocol.UploadState.Connecting)
                || (m_UploadState==Protocol.UploadState.WaitCallBack))
            {
                CancelUploadTransfer(false);
            }

            if ((m_DownloadState==Protocol.DownloadState.Connecting)
                ||(m_DownloadState==Protocol.DownloadState.WaitCallBack))
            {
                if ((m_ConexionTries<Protocol.ConnectionRetries)
                    &&(reason==(byte)Protocol.ConnectionReason.CannotConnect))
                {
            #if DEBUG
                    m_UserName=strReason;
            #endif
                    Connect();
                }
                else
                {
                    //quitar la fuente
                    m_ConexionTries=0;
                    m_DownloadState=Protocol.DownloadState.None;

                    if (DownFileHash!=null)
                    {
                        if (m_OtherDownFiles!=null)
                        {
                            m_OtherDownFiles.Clear();
                        }

                        m_OtherDownFiles=null;

                        if (m_DownloadElement==null)
                        {
                            m_DownloadElement=(CElement)CKernel.FilesList[DownFileHash];
                        }
                        if ((m_DownloadElement!=null)&&(m_DownloadElement.SourcesList!=null)) m_DownloadElement.SourcesList.RemoveSource(this);
                    }
                    else
                    {
                        Debug.WriteLine(m_UserName+"Never should enter here-> null DownFileHash");
                    }
            #if DEBUG
                    m_UserName=strReason;
            #endif
                }
            }

            if (m_DownloadState==Protocol.DownloadState.Connected)
            {
                m_DownloadState=Protocol.DownloadState.None;

                if (m_DownloadElement==null)
                {
                    m_DownloadElement=(CElement)CKernel.FilesList[DownFileHash];
                }

                m_DownloadElement.SourcesList.RemoveSource(this);
            }

            //free all pending blocks
            if (m_DownloadState==Protocol.DownloadState.Downloading)
            {
                //CLog.Log(Constants.Log.Verbose,"Transfer interrrupted,reason: {0}",strReason);
                m_CleanDownloadBlocks();

                m_LastDownloadRequest=DateTime.Now;
                m_DownloadSpeeds.Clear();
                m_DownloadSpeed=0;
                m_QRDownload=0;
                m_DownloadState=Protocol.DownloadState.OnQueue;
            }
            //for eny reason some clients (edonkeys mainly) have pending blocks but their downloadstate!=Downloading
            if (m_DownloadBlocks.Count>0)
            {
                m_CleanDownloadBlocks();
            //					Debug.WriteLine(m_DownloadState.ToString());
            //					Debug.WriteLine("Disconnected leaving pending blocks!!!");
            }
            CKernel.ClientsList.IsClientNeeded(this);
        }
Пример #7
0
        public void Connect()
        {
            #if VERBOSE
            CKernel.LogClient.AddLog("Connect",m_ID);
            #endif
            m_ConexionTries++;

            if (m_ID>Protocol.LowIDLimit)
            {
                if (connection==null)
                {
                    connection=new CConnection(m_ID,m_Port,this);
                }
                else
                {
                    connection.ReOpenConnection();
                }

                MemoryStream packet=new MemoryStream();
            #if VERBOSE
                CKernel.LogClient.AddLog("Connect-send hello",m_ID);
            #endif
                bool allowPartner=((this.m_DownloadState!=Protocol.DownloadState.NoNeededParts)&&
                    (CKernel.Queue.AllowNewParner()));
                CSendHello sendHello=new CSendHello(false,packet,CKernel.ServersList.ActiveServer,true,allowPartner);

                if (connection!=null)
                {
                    connection.SendPacket(packet);
                    connection.Connect();
                }
            }
            else
            {
                m_CallBackTime=DateTime.Now;

                if (m_DownloadState==Protocol.DownloadState.Connecting)
                {
                    m_DownloadState=Protocol.DownloadState.WaitCallBack;
                }

                if (m_UploadState==Protocol.UploadState.Connecting)
                {
                    m_UploadState=Protocol.UploadState.WaitCallBack;
                }

                if (CKernel.ServersList.IsTheActiveServer(m_ServerIP,m_ServerPort))
                {
                    CKernel.ServersList.ActiveServer.RequestCallBack(m_ID);
            #if VERBOSE
                    CKernel.LogClient.AddLog("Connect-RequestCallBack",m_ID);
            #endif
                }
                else
                {
            #if VERBOSE
                    CKernel.LogClient.AddLog("LowID other server-Connect-RequestCallBack",m_ID);
            #endif
                    //don't request udp callbacks to save servers bandwidth
                    //this source will be deleted later
                    //CKernel.ServersList.RequestCallBackUDP(m_ServerIP,m_ServerPort,m_ID);
                }
            }
            #if VERBOSE
            CKernel.LogClient.AddLog("Connect-connected ",m_ID);
            #endif
        }
Пример #8
0
 public CClient(CConnection in_connection)
 {
     connection=in_connection;
     m_UploadBlocks=new ArrayList();
     m_UploadDataPackets=new ArrayList();
     m_DownloadBlocks=new ArrayList();
     m_DownloadState=Protocol.DownloadState.None;
     m_UploadState=Protocol.UploadState.None;
     m_UploadElement=null;
     m_LastSourcesRequest=DateTime.MinValue;
     m_DownloadTries=0;
     m_ClientFileName="";
     m_Software=(byte)Protocol.Client.Unknown;
     m_DownloadSpeeds=new ArrayList();
     m_UploadSpeeds=new ArrayList();
     m_SentComment=false;
     m_LastChunkRequested=-1;
     m_AskingUDP=true;
     m_SharedFilesRequested=false;
     m_CallBackTime=DateTime.MinValue;
     m_FriendLevel=Types.Constants.FriendLevel.NoFriend;
     m_FirstMessage=true;
     m_IsSpammer=false;
     m_IsPartner=false;
     m_PartnerAllowed=false;
     m_SupportsPartner=false;
     m_BadPartnerTime=DateTime.MinValue;
     m_PartnerStartedTime=DateTime.MinValue;
     m_supportsHorde=false;
     m_TimesPartnerSlow=0;
 }
Пример #9
0
        private void m_ListenerProc()
        {
            Socket edonkeySocket;

            while ((m_ServerThread.IsAlive)&&(base.Active))
            {
                edonkeySocket=null;
                try
                {
                    edonkeySocket=base.AcceptSocket();
                    edonkeySocket.Blocking=false;
                }
                catch
                {
                }
                if (edonkeySocket!=null)
                {
                    CConnection connection=null;
                    if ((CKernel.IPFilter==null)||(!CKernel.IPFilter.BlockIP(BitConverter.ToUInt32(((IPEndPoint)edonkeySocket.RemoteEndPoint).Address.GetAddressBytes(),0))))
                        connection=new CConnection(edonkeySocket);
                    else
                    {
                        CLog.Log(Types.Constants.Log.Verbose,"IP_BLOCKED",((IPEndPoint)edonkeySocket.RemoteEndPoint).Address.ToString(),CKernel.IPFilter.BlockedIPDescription);
                        edonkeySocket.Shutdown(SocketShutdown.Both);
                        edonkeySocket.Close();
                    }
                }
            }
        }
Пример #10
0
 public void Remove(CConnection cn)
 {
     if (m_Closing)
     {
         cn.CloseSocket();
         return;
     }
     m_ArrayList.Remove(cn);
 }