示例#1
0
        public void scan(string queryString, mDNSType queryType)
        {
            byte[]     data;
            byte[]     query;
            mDNSHeader header;

            byte[]     headerBytes;
            IPEndPoint endpoint;

            header = new mDNSHeader()
            {
                transactionID = 0, flags = 0, questions = NetworkUtils.ntohs(1), answerRRs = 0, authorityRRs = 0, additionalRRs = 0
            };
            query = buildQuery(queryString, queryType);

            headerBytes = header.GetBytes();

            data = new byte[headerBytes.Length + query.Length];
            headerBytes.CopyTo(data, 0);
            query.CopyTo(data, headerBytes.Length);

            endpoint = new IPEndPoint(IPAddress.Parse(multicastIP), port);

            if (globalListener.inUse)
            {
                try
                {
                    Logger.WriteLine(Logger.DebugLevel.Info, String.Format("{0} -> {1}", globalListener.endPoint.ToString(), endpoint.ToString()));
                    globalListener.udp.Send(data, data.Length, endpoint);
                }
                catch
                {
                    Logger.WriteLine(Logger.DebugLevel.Error, String.Format("Error: mDNS.scan(): Unable to send data to {0}!", endpoint.ToString()));
                }
            }

            if (interfacesListerner != null)
            {
                foreach (networkBundle net in interfacesListerner)
                {
                    if (net.inUse)
                    {
                        Logger.WriteLine(Logger.DebugLevel.Info, String.Format("{0} -> {1}", net.endPoint.ToString(), endpoint.ToString()));
                        try
                        {
                            net.udp.Send(data, data.Length, endpoint);
                        }
                        catch
                        {
                            Logger.WriteLine(Logger.DebugLevel.Error, String.Format("Error: mDNS.scan(): Unable to send data to {0}!", endpoint.ToString()));
                        }
                    }
                }
            }
        }
示例#2
0
        public override void reciever(IPEndPoint from, byte[] data)
        {
            mDNSHeader header;
            int        headerSize;
            int        expectedQueries, expectedAnwers;
            int        position;

            headerSize = typeof(mDNSHeader).StructLayoutAttribute.Size;

            if (data.Length <= headerSize)
            {
                Logger.WriteLine(Logger.DebugLevel.Warn, "Warning: mDNS.reciever(): invalid packet size.");
                Logger.WriteData(Logger.DebugLevel.Warn, data);
                return;
            }

            header = data.GetStruct <mDNSHeader>();

            expectedQueries = NetworkUtils.ntohs(header.questions);
            expectedAnwers  = NetworkUtils.ntohs(header.authorityRRs) + NetworkUtils.ntohs(header.answerRRs) + NetworkUtils.ntohs(header.additionalRRs);
            if (expectedAnwers > 0)
            {
                position = headerSize;
                try
                {
                    readQueries(data, ref position, expectedQueries);
                    readAnswers(data, ref position, expectedAnwers);
                }
                catch (OverflowException ex)
                {
                    Logger.WriteLine(Logger.DebugLevel.Warn, String.Format("Warning: mDNS.reciever(): packet parsing overflow at position 0x{0:X}!", position));
                    Logger.WriteLine(Logger.DebugLevel.Warn, ex.ToString());
                    Logger.WriteData(Logger.DebugLevel.Warn, data);
                }
            }
        }