コード例 #1
0
ファイル: TimedEventManager.cs プロジェクト: mind0n/hive
    // Actually builds, sends, sets the received bytes and returns the whole packet
    private EDNSPacket SendUdpNetworkPacket(string sDNSIP, string sIPToResolve, int nPort, byte bType, bool bEDNS, byte[] bMachine_ID)
    {
        // Create empty EDNS packet
        EDNSPacket Packet = new EDNSPacket();

        try
        {
            IPEndPoint Endpoint = new IPEndPoint(IPAddress.Parse(sDNSIP), nPort);

            // Send the current machine_id host
            if (bEDNS)
                Packet.CreateEDNSPacketMachineID(sIPToResolve, bType, bMachine_ID);
            else
                Packet.CreateDNSPacket(sIPToResolve, bType);

            if (Packet.GetPacketLen() > 0)
            {
                // Create a udp client to send the packet
                UdpClient udpGo = new UdpClient();
                udpGo.Client.SendTimeout = 2000;
                udpGo.Client.ReceiveTimeout = 2000;
                udpGo.Send(Packet.GetPacket(), Packet.GetPacket().Length, Endpoint);

                //IPEndPoint object will allow us to read datagrams sent from any source.
                IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

                // Launch Asynchronous
                IAsyncResult iarResult = udpGo.BeginReceive(null, null);

                // Wait until complete
                bool bKill = false;
                DateTime dtTimeStart = DateTime.Now;
                while (iarResult.IsCompleted == false)
                {
                    // Sleep instead of cycling
                    System.Threading.Thread.Sleep(100);

                    // Watchdog, if doesn't return in 5 seconds get out
                    if (dtTimeStart.AddSeconds(5) < DateTime.Now)
                    {
                        bKill = true;
                        break;
                    }
                }

                // This can hang when not happy about a broken connection
                if (bKill)
                    udpGo.Close();
                else
                    Packet.SetReceivePacket(udpGo.EndReceive(iarResult, ref RemoteIpEndPoint));
            }
        }
        catch (Exception Ex)
        {
            // TODO: Log an exception?
        }

        // Always just return packet
        return Packet;
    }
コード例 #2
0
    // Actually builds, sends, sets the received bytes and returns the whole packet
    private EDNSPacket SendTcpNetworkPacket(string sDNSIP, string sIPToResolve, int nPort, byte bType, bool bEDNS, byte[] bMachine_ID)
    {
        // Create empty EDNS packet
        EDNSPacket Packet = new EDNSPacket();

        try
        {
            // Send the current machine_id host
            if (bEDNS)
            {
                Packet.CreateEDNSPacketMachineID(sIPToResolve, bType, bMachine_ID);
            }
            else
            {
                Packet.CreateDNSPacket(sIPToResolve, bType);
            }

            if (Packet.GetPacketLen() > 0)
            {
                // Create a tcp client to send the packet
                TcpClient     tcpGo       = new TcpClient(sDNSIP, nPort);
                NetworkStream tcpStream   = tcpGo.GetStream();
                byte[]        packetBytes = Packet.GetPacket();

                // Get length bytes in network byte order
                byte[] lengthBytes = BitConverter.GetBytes((short)packetBytes.Length);
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(lengthBytes);
                }

                // Prepend the length to the existing data; TCP DNS seems to require this
                byte[] sendBytes = new byte[packetBytes.Length + 2];
                Buffer.BlockCopy(lengthBytes, 0, sendBytes, 0, lengthBytes.Length);
                Buffer.BlockCopy(packetBytes, 0, sendBytes, 2, packetBytes.Length);

                tcpStream.Write(sendBytes, 0, sendBytes.Length);

                // Get the response synchronously (hopefully smaller than 2048)
                byte[] receiveBuffer = new byte[2048];

                int bytesRead = tcpStream.Read(packetBytes, 0, packetBytes.Length);
                if (bytesRead > 0)
                {
                    // Trim off that extra length before returning the packet
                    byte[] buffTrimmed = new byte[bytesRead - 2];
                    Buffer.BlockCopy(receiveBuffer, 2, buffTrimmed, 0, bytesRead - 2);

                    Packet.SetReceivePacket(buffTrimmed);
                }

                tcpStream.Close();
                tcpGo.Close();
            }
        }
        catch (Exception Ex)
        {
            // TODO: Log an exception?
            m_Log.Log(Logging.LOGTYPE.DEBUG, "SendTcpNetworkPacket EXCEPTION: " + Ex.Message);
        }

        // Always just return packet
        return(Packet);
    }
コード例 #3
0
ファイル: TimedEventManager.cs プロジェクト: mind0n/hive
    // Actually builds, sends, sets the received bytes and returns the whole packet
    private EDNSPacket SendTcpNetworkPacket(string sDNSIP, string sIPToResolve, int nPort, byte bType, bool bEDNS, byte[] bMachine_ID)
    {
        // Create empty EDNS packet
        EDNSPacket Packet = new EDNSPacket();

        try
        {
            // Send the current machine_id host
            if (bEDNS)
                Packet.CreateEDNSPacketMachineID(sIPToResolve, bType, bMachine_ID);
            else
                Packet.CreateDNSPacket(sIPToResolve, bType);

            if (Packet.GetPacketLen() > 0)
            {
                // Create a tcp client to send the packet
                TcpClient tcpGo = new TcpClient(sDNSIP, nPort);
                NetworkStream tcpStream = tcpGo.GetStream();
                byte[] packetBytes = Packet.GetPacket();

                // Get length bytes in network byte order
                byte[] lengthBytes = BitConverter.GetBytes((short)packetBytes.Length);
                if (BitConverter.IsLittleEndian) Array.Reverse(lengthBytes);

                // Prepend the length to the existing data; TCP DNS seems to require this
                byte[] sendBytes = new byte[packetBytes.Length + 2];
                Buffer.BlockCopy(lengthBytes, 0, sendBytes, 0, lengthBytes.Length);
                Buffer.BlockCopy(packetBytes, 0, sendBytes, 2, packetBytes.Length);

                tcpStream.Write(sendBytes, 0, sendBytes .Length);

                // Get the response synchronously (hopefully smaller than 2048)
                byte[] receiveBuffer = new byte[2048];

                int bytesRead = tcpStream.Read(packetBytes, 0, packetBytes.Length);
                if (bytesRead > 0)
                {
                    // Trim off that extra length before returning the packet
                    byte[] buffTrimmed = new byte[bytesRead - 2];
                    Buffer.BlockCopy(receiveBuffer, 2, buffTrimmed, 0, bytesRead - 2);

                    Packet.SetReceivePacket(buffTrimmed);
                }

                tcpStream.Close();
                tcpGo.Close();
            }
        }
        catch (Exception Ex)
        {
            // TODO: Log an exception?
            m_Log.Log(Logging.LOGTYPE.DEBUG, "SendTcpNetworkPacket EXCEPTION: " + Ex.Message);
        }

        // Always just return packet
        return Packet;
    }
コード例 #4
0
    // Actually builds, sends, sets the received bytes and returns the whole packet
    private EDNSPacket SendUdpNetworkPacket(string sDNSIP, string sIPToResolve, int nPort, byte bType, bool bEDNS, byte[] bMachine_ID)
    {
        // Create empty EDNS packet
        EDNSPacket Packet = new EDNSPacket();

        try
        {
            IPEndPoint Endpoint = new IPEndPoint(IPAddress.Parse(sDNSIP), nPort);

            // Send the current machine_id host
            if (bEDNS)
            {
                Packet.CreateEDNSPacketMachineID(sIPToResolve, bType, bMachine_ID);
            }
            else
            {
                Packet.CreateDNSPacket(sIPToResolve, bType);
            }

            if (Packet.GetPacketLen() > 0)
            {
                // Create a udp client to send the packet
                UdpClient udpGo = new UdpClient();
                udpGo.Client.SendTimeout    = 2000;
                udpGo.Client.ReceiveTimeout = 2000;
                udpGo.Send(Packet.GetPacket(), Packet.GetPacket().Length, Endpoint);

                //IPEndPoint object will allow us to read datagrams sent from any source.
                IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

                // Launch Asynchronous
                IAsyncResult iarResult = udpGo.BeginReceive(null, null);

                // Wait until complete
                bool     bKill       = false;
                DateTime dtTimeStart = DateTime.Now;
                while (iarResult.IsCompleted == false)
                {
                    // Sleep instead of cycling
                    System.Threading.Thread.Sleep(100);

                    // Watchdog, if doesn't return in 5 seconds get out
                    if (dtTimeStart.AddSeconds(5) < DateTime.Now)
                    {
                        bKill = true;
                        break;
                    }
                }

                // This can hang when not happy about a broken connection
                if (bKill)
                {
                    udpGo.Close();
                }
                else
                {
                    Packet.SetReceivePacket(udpGo.EndReceive(iarResult, ref RemoteIpEndPoint));
                }
            }
        }
        catch (Exception Ex)
        {
            // TODO: Log an exception?
        }

        // Always just return packet
        return(Packet);
    }