Пример #1
0
        private void Request(Name name, HeaderOpcode opcode)
        {
            bool recursionDesired = true;

            switch (opcode)
            {
            case HeaderOpcode.Update:
                opcode           = HeaderOpcode.Registration;
                recursionDesired = false;
                break;

            case HeaderOpcode.Release:
            case HeaderOpcode.WACK:
            case HeaderOpcode.Refresh:
                recursionDesired = false;
                break;
            }

            Packet packet = new Packet();

            packet.Header.NameTrnId = NAME_TRN_ID;
            packet.Header.Opcode    = opcode;
            packet.Header.Flags     = HeaderFlags.Broadcast;
            if (recursionDesired)
            {
                packet.Header.Flags |= HeaderFlags.RecursionDesired;
            }
            packet.Header.Rcode   = 0;
            packet.Header.QdCount = 1;
            packet.Header.ArCount = 1;

            packet.QuestionEntries = new QuestionName[1];
            packet.QuestionEntries[0].UncompressedName = name.UncompressedName;
            packet.QuestionEntries[0].Type             = QuestionType.NB;
            packet.QuestionEntries[0].Class            = QuestionClass.IN;

            packet.AdditionalResourceRecords = new ResourceRecord[1];
            packet.AdditionalResourceRecords[0].UncompressedName = name.UncompressedName;   // TODO: Should use a pointer here
            packet.AdditionalResourceRecords[0].Type             = ResourceRecordType.NB;
            packet.AdditionalResourceRecords[0].Class            = ResourceRecordClass.IN;
            packet.AdditionalResourceRecords[0].TTL = 0;

            byte[] data = new byte[6];
            data[0] = (byte)(name.Type == NameType.Group ? 0x80 : 0x00); // NB_FLAGS / bit 15: Group name flag, bit 16-15: 00 B node, 01 P node, 10 M node, 11 reserved, bit 14-8: reserved
            data[1] = 0x00;                                              // NB_FLAGS
            // NB_ADDRESS
            for (int i = 0; i < 4; i++)
            {
                data[i + 2] = localIP[i];
            }

            packet.AdditionalResourceRecords[0].Data = data;

            data = packet.ToArray();

            lock (socket)
                socket.SendTo(data, broadcastEndPoint);
        }
Пример #2
0
        private void Response(Name name, HeaderOpcode opcode, byte rcode, ushort nameTrnId, EndPoint remoteEndPoint)
        {
            Packet packet = new Packet();

            packet.Header.Response  = true;
            packet.Header.NameTrnId = nameTrnId;
            packet.Header.Opcode    = opcode;
            packet.Header.Flags     = HeaderFlags.AuthoritativeAnswer | HeaderFlags.RecursionDesired;
            packet.Header.Rcode     = rcode;
            packet.Header.AnCount   = 1;

            packet.AnswerResourceRecords = new ResourceRecord[1];
            packet.AnswerResourceRecords[0].UncompressedName = name.UncompressedName;
            packet.AnswerResourceRecords[0].Type             = ResourceRecordType.NB;
            packet.AnswerResourceRecords[0].Class            = ResourceRecordClass.IN;
            packet.AnswerResourceRecords[0].TTL = 0;

            byte[] data = new byte[6];
            data[0] = (byte)(name.Type == NameType.Group ? 0x80 : 0x00); // NB_FLAGS / bit 15: Group name flag, bit 16-15: 00 B node, 01 P node, 10 M node, 11 reserved, bit 14-8: reserved
            data[1] = 0x00;                                              // NB_FLAGS
            // NB_ADDRESS
            for (int i = 0; i < 4; i++)
            {
                data[i + 2] = localIP[i];
            }

            packet.AnswerResourceRecords[0].Data = data;

            data = packet.ToArray();

            try
            {
                lock (socket)
                    socket.SendTo(data, remoteEndPoint);
            }
            catch
            {
                // Handles situations where the remote host is not accessable through the network.
            }
        }
Пример #3
0
        private void Response(Name name, HeaderOpcode opcode, byte rcode, ushort nameTrnId, EndPoint remoteEndPoint)
        {
            Packet packet = new Packet();
            packet.Header.Response = true;
            packet.Header.NameTrnId = nameTrnId;
            packet.Header.Opcode = opcode;
            packet.Header.Flags = HeaderFlags.AuthoritativeAnswer | HeaderFlags.RecursionDesired;
            packet.Header.Rcode = rcode;
            packet.Header.AnCount = 1;

            packet.AnswerResourceRecords = new ResourceRecord[1];
            packet.AnswerResourceRecords[0].UncompressedName = name.UncompressedName;
            packet.AnswerResourceRecords[0].Type = ResourceRecordType.NB;
            packet.AnswerResourceRecords[0].Class = ResourceRecordClass.IN;
            packet.AnswerResourceRecords[0].TTL = 0;

            byte[] data = new byte[6];
            data[0] = (byte)(name.Type == NameType.Group ? 0x80 : 0x00);    // NB_FLAGS / bit 15: Group name flag, bit 16-15: 00 B node, 01 P node, 10 M node, 11 reserved, bit 14-8: reserved
            data[1] = 0x00; // NB_FLAGS
            // NB_ADDRESS
            for (int i = 0; i < 4; i++)
                data[i + 2] = localIP[i];

            packet.AnswerResourceRecords[0].Data = data;

            data = packet.ToArray();

            try
            {
                lock (socket)
                    socket.SendTo(data, remoteEndPoint);
            }
            catch
            {
                // Handles situations where the remote host is not accessable through the network.
            }
        }
Пример #4
0
        private void Request(Name name, HeaderOpcode opcode)
        {
            bool recursionDesired = true;

            switch (opcode)
            {
                case HeaderOpcode.Update:
                    opcode = HeaderOpcode.Registration;
                    recursionDesired = false;
                    break;
                case HeaderOpcode.Release:
                case HeaderOpcode.WACK:
                case HeaderOpcode.Refresh:
                    recursionDesired = false;
                    break;
            }

            Packet packet = new Packet();
            packet.Header.NameTrnId = NAME_TRN_ID;
            packet.Header.Opcode = opcode;
            packet.Header.Flags = HeaderFlags.Broadcast;
            if (recursionDesired) packet.Header.Flags |= HeaderFlags.RecursionDesired;
            packet.Header.Rcode = 0;
            packet.Header.QdCount = 1;
            packet.Header.ArCount = 1;

            packet.QuestionEntries = new QuestionName[1];
            packet.QuestionEntries[0].UncompressedName = name.UncompressedName;
            packet.QuestionEntries[0].Type = QuestionType.NB;
            packet.QuestionEntries[0].Class = QuestionClass.IN;

            packet.AdditionalResourceRecords = new ResourceRecord[1];
            packet.AdditionalResourceRecords[0].UncompressedName = name.UncompressedName;   // TODO: Should use a pointer here
            packet.AdditionalResourceRecords[0].Type = ResourceRecordType.NB;
            packet.AdditionalResourceRecords[0].Class = ResourceRecordClass.IN;
            packet.AdditionalResourceRecords[0].TTL = 0;

            byte[] data = new byte[6];
            data[0] = (byte)(name.Type == NameType.Group ? 0x80 : 0x00);    // NB_FLAGS / bit 15: Group name flag, bit 16-15: 00 B node, 01 P node, 10 M node, 11 reserved, bit 14-8: reserved
            data[1] = 0x00; // NB_FLAGS
            // NB_ADDRESS
            for (int i = 0; i < 4; i++)
                data[i + 2] = localIP[i];

            packet.AdditionalResourceRecords[0].Data = data;

            data = packet.ToArray();

            lock (socket)
                socket.SendTo(data, broadcastEndPoint);
            //try
            //{
            //}
            //catch (Exception ex) { }
        }