示例#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.NetworkToHostOrder16(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.getInstance().WriteNet(Logger.DebugLevel.Debug, globalListener.endPoint, endpoint, ProtocolType.Udp, data);
                    globalListener.udp.Send(data, data.Length, endpoint);
                }
                catch
                {
                    Logger.getInstance().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)
                    {
                        try
                        {
                            Logger.getInstance().WriteNet(Logger.DebugLevel.Debug, net.endPoint, endpoint, ProtocolType.Udp, data);
                            net.udp.Send(data, data.Length, endpoint);
                        }
                        catch
                        {
                            Logger.getInstance().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.getInstance().WriteLine(Logger.DebugLevel.Warn, "Warning: mDNS.reciever(): invalid packet size.");
                Logger.getInstance().WriteData(Logger.DebugLevel.Warn, data);
                return;
            }

            header = data.GetStruct <mDNSHeader>();

            expectedQueries = NetworkUtils.NetworkToHostOrder16(header.questions);
            expectedAnwers  = NetworkUtils.NetworkToHostOrder16(header.authorityRRs) + NetworkUtils.NetworkToHostOrder16(header.answerRRs) + NetworkUtils.NetworkToHostOrder16(header.additionalRRs);
            if (expectedAnwers > 0)
            {
                position = headerSize;
                try
                {
                    readQueries(data, ref position, expectedQueries);
                    readAnswers(data, ref position, expectedAnwers);
                }
                catch (OverflowException ex)
                {
                    Logger.getInstance().WriteLine(Logger.DebugLevel.Warn, String.Format("Warning: mDNS.reciever(): packet parsing overflow at position 0x{0:X}!", position));
                    Logger.getInstance().WriteLine(Logger.DebugLevel.Warn, ex.ToString());
                    Logger.getInstance().WriteData(Logger.DebugLevel.Warn, data);
                }
            }
        }
示例#3
0
        public override void reciever(IPEndPoint from, byte[] data)
        {
            GigEVisionAckn answer;
            UInt32         version;
            string         macAddress;
            IPAddress      ipv4;

            string model;
            string vendor;
            string serial;

            if (data.Length != typeof(GigEVisionAckn).StructLayoutAttribute.Size)
            {
                Logger.getInstance().WriteLine(Logger.DebugLevel.Warn, String.Format("Warning: GigEVision.reciever(): Invalid size packet recieved from {0}", from.ToString()));
                return;
            }

            answer = data.GetStruct <GigEVisionAckn>();

            if (NetworkUtils.NetworkToHostOrder16(answer.payloadLen) != data.Length - 8)
            {
                Logger.getInstance().WriteLine(Logger.DebugLevel.Warn, String.Format("Warning: GigEVision.reciever(): Invalid size payload length value (got value {0} while value {1} was expected) recieved from {2}",
                                                                                     NetworkUtils.NetworkToHostOrder16(answer.payloadLen), data.Length - 8, from.ToString()));
                return;
            }

            version = ((UInt32)NetworkUtils.NetworkToHostOrder16(answer.plMajorVersion)) << 16 |
                      ((UInt32)NetworkUtils.NetworkToHostOrder16(answer.plMinorVersion));

            if (version != 0x00010002)
            {
                Logger.getInstance().WriteLine(Logger.DebugLevel.Warn, String.Format("Warning: GigEVision.reciever(): Invalid packet version (got value 0x{0:X8} while value 0x{1:X8} was expected) recieved from {2}",
                                                                                     version, 0x00010002, from.ToString()));
            }


            if (answer.plIPCurrentAddr != 0)
            {
                ipv4 = new IPAddress((long)answer.plIPCurrentAddr);
            }
            else
            {
                ipv4 = from.Address;
            }

            macAddress = String.Format("{0:X2}:{1:X2}:{2:X2}:{3:X2}:{4:X2}:{5:X2}",
                                       answer.plMacAddress.byte00, answer.plMacAddress.byte01, answer.plMacAddress.byte02,
                                       answer.plMacAddress.byte03, answer.plMacAddress.byte04, answer.plMacAddress.byte05);
            serial = Encoding.UTF8.GetString(answer.plSerialNumber);
            if (serial == "")
            {
                serial = macAddress;
            }

            vendor = Encoding.UTF8.GetString(answer.plManufacturer);
            if (vendor == "")
            {
                vendor = name;
            }

            model = Encoding.UTF8.GetString(answer.plModel);
            if (model == "")
            {
                model = Encoding.UTF8.GetString(answer.plUsername);
            }

            viewer.deviceFound(vendor, 0, ipv4, model, serial);
        }