コード例 #1
0
ファイル: UDPSocketImpl.cs プロジェクト: mheydt/mpddotnet
        public void SendPacket(Packet packet,
                               Mule.ED2K.ED2KServer pServer,
                               ushort nSpecialPort, byte[] pInRawPacket, uint nRawLen)
        {
            // Create raw UDP packet
            byte[] pRawPacket;
            uint   uRawPacketSize;
            ushort nPort = 0;

            if (packet != null)
            {
                pRawPacket = new byte[packet.Size + 2];
                Array.Copy(packet.UDPHeader, pRawPacket, 2);
                Array.Copy(packet.Buffer, 0, pRawPacket, 2, packet.Size);
                uRawPacketSize = packet.Size + 2;
                if (MuleApplication.Instance.Preference.IsServerCryptLayerUDPEnabled &&
                    pServer.ServerKeyUDP != 0 &&
                    pServer.DoesSupportsObfuscationUDP)
                {
                    uRawPacketSize = (uint)EncryptSendServer(ref pRawPacket, (int)uRawPacketSize, pServer.ServerKeyUDP);
                    nPort          = pServer.ObfuscationPortUDP;
                }
                else
                {
                    nPort = (ushort)(pServer.Port + 4);
                }
            }
            else if (pInRawPacket != null)
            {
                // we don't encrypt rawpackets (!)
                pRawPacket = new byte[nRawLen];
                Array.Copy(pInRawPacket, pRawPacket, nRawLen);
                uRawPacketSize = nRawLen;
                nPort          = (ushort)(pServer.Port + 4);
            }
            else
            {
                Debug.Assert(false);
                return;
            }
            nPort = (nSpecialPort == 0) ? nPort : nSpecialPort;
            Debug.Assert(nPort != 0);

            // Do we need to resolve the DN of this server?
            IPAddress ipAddr = IPAddress.None;

            IPAddress.TryParse(pServer.Address, out ipAddr);
            if (ipAddr == IPAddress.None)
            {
                IPHostEntry hostEntry = Dns.GetHostEntry(pServer.Address);
                // Get the IP value
                uint nIP = 0;
                if (hostEntry != null && hostEntry.AddressList.Length > 0)
                {
                    nIP = BitConverter.ToUInt32(hostEntry.AddressList[0].GetAddressBytes(), 0);
                }

                if (nIP != 0)
                {
                    bool bRemoveServer = false;
                    if (!MpdUtilities.IsGoodIP(nIP))
                    {
                        // However, if we are currently connected to a "not-good-ip", that IP can't
                        // be that bad -- may only happen when debugging in a LAN.
                        ED2KServer pConnectedServer = MuleApplication.Instance.ServerConnect.CurrentServer;
                        if (pConnectedServer != null || pConnectedServer.IP != nIP)
                        {
                            bRemoveServer = true;
                        }
                    }
                    if (!bRemoveServer && MuleApplication.Instance.Preference.IsFilterServerByIP &&
                        MuleApplication.Instance.IPFilter.IsFiltered(nIP))
                    {
                        bRemoveServer = true;
                    }

                    ED2KServer pTempServer =
                        MuleApplication.Instance.ServerList.GetServerByAddress(pServer.Address, pServer.Port);
                    if (pTempServer != null)
                    {
                        pServer.IP = nIP;
                        // If we already have entries in the server list (dynIP-servers without a DN)
                        // with the same IP as this dynIP-server, remove the duplicates.
                        MuleApplication.Instance.ServerList.RemoveDuplicatesByIP(pServer);
                    }

                    if (bRemoveServer)
                    {
                        return;
                    }

                    // Send all of the queued packets for this server.
                    SendBuffer(nIP, nPort, pRawPacket, uRawPacketSize);
                }
            }
            else
            {
                // No DNS query needed for this server. Just send the packet.
                SendBuffer(BitConverter.ToUInt32(ipAddr.GetAddressBytes(), 0),
                           nPort, pRawPacket, uRawPacketSize);
            }
        }