Beispiel #1
0
            // Wiresharkの表示を参考にした
            public static XmlDocument Query(IPEndPoint ip, int millisecs)
            {
                using (UdpClient client = new UdpClient(ip.AddressFamily)) {
                    client.Connect(ip);

                    Random rand = new Random();
                    ushort seed = (ushort)rand.Next();
                    {
                        MemoryStream os = new MemoryStream();
                        BEW          wr = new BEW(os);
                        wr.Write(seed);
                        wr.Write((ushort)0); //Flags: 0x0000 (Name query)
                        wr.Write((ushort)1); //Questions: 1
                        wr.Write((ushort)0); //Answer RRs: 0
                        wr.Write((ushort)0); //Authority RRs: 0
                        wr.Write((ushort)0); //Additional RRs: 0

                        wr.Write((byte)32);
                        wr.Write(Encoding.ASCII.GetBytes("CKAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
                        wr.Write((byte)0);

                        wr.Write((ushort)0x21); //Type: NBSTAT
                        wr.Write((ushort)0x01); //Class: IN

                        byte[] bin = os.ToArray();
                        if (client.Send(bin, bin.Length) != bin.Length)
                        {
                            throw new EndOfStreamException();
                        }
                    }

                    {
                        XmlDocument xmlo   = new XmlDocument();
                        XmlElement  elroot = xmlo.CreateElement("wins");
                        xmlo.AppendChild(elroot);
                        XmlElement elres = xmlo.CreateElement("response");
                        elroot.AppendChild(elres);

                        byte[] bin;
                        {
                            IPEndPoint   ipr = new IPEndPoint(0, 0);
                            IAsyncResult ar  = client.BeginReceive(CbReceived, null);
                            if (false == ar.AsyncWaitHandle.WaitOne(millisecs, false))
                            {
                                client.Close();
                            }
                            bin = client.EndReceive(ar, ref ipr);
                        }
                        MemoryStream si    = new MemoryStream(bin, false);
                        BER          br    = new BER(si);
                        ushort       rseed = br.ReadUInt16();
                        if (rseed != seed)
                        {
                            throw new InvalidDataException();
                        }
                        ushort fl = br.ReadUInt16();
                        if (0 == (fl & 0x8000) || 0 != (fl & 15))
                        {
                            throw new InvalidDataException();
                        }

                        {
                            int cnt = br.ReadUInt16();
                            for (int x = 0; x < cnt;)
                            {
                                throw new NotSupportedException();
                            }
                        }

                        int[] acnt = new int[3];
                        acnt[0] = br.ReadUInt16();
                        acnt[1] = br.ReadUInt16();
                        acnt[2] = br.ReadUInt16();

                        for (int t = 0; t < 3; t++)
                        {
                            XmlElement elrrs = null;
                            if (t == 0)
                            {
                                elrrs = xmlo.CreateElement("answer-rrs");
                            }
                            if (t == 1)
                            {
                                elrrs = xmlo.CreateElement("authority-rrs");
                            }
                            if (t == 2)
                            {
                                elrrs = xmlo.CreateElement("additional-rrs");
                            }
                            elres.AppendChild(elrrs);

                            for (int x = 0; x < acnt[t]; x++)
                            {
                                XmlElement ela = xmlo.CreateElement("answer");
                                elrrs.AppendChild(ela);

                                byte   cb   = br.ReadByte();
                                byte[] name = br.ReadBytes(cb);
                                ela.SetAttribute("raw-name", Encoding.ASCII.GetString(name));
                                byte nodeType = br.ReadByte();
                                ela.SetAttribute("node-type", nodeType.ToString("x2"));

                                int atype = br.ReadUInt16();
                                if (atype != 0x21)
                                {
                                    throw new NotSupportedException();
                                }

                                int aclass = br.ReadUInt16();
                                if (aclass != 1)
                                {
                                    throw new NotSupportedException();
                                }

                                uint attl = br.ReadUInt32();
                                ela.SetAttribute("ttl", attl.ToString());

                                br.ReadUInt16();

                                byte aNamecnt = br.ReadByte();
                                for (int a = 0; a < aNamecnt; a++)
                                {
                                    String aname = Encoding.Default.GetString(br.ReadBytes(15));
                                    byte   anty  = br.ReadByte();
                                    int    afl   = br.ReadUInt16();

                                    XmlElement elan = xmlo.CreateElement("name");
                                    ela.AppendChild(elan);
                                    elan.SetAttribute("name", aname);
                                    elan.SetAttribute("name-type", anty.ToString("x2"));
                                    elan.SetAttribute("flags", afl.ToString("x4"));
                                }

                                byte[] mac = br.ReadBytes(6);
                                ela.SetAttribute("unit-id", String.Format("{0:x2}:{1:x2}:{2:x2}:{3:x3}:{4:x2}:{5:x2}"
                                                                          , mac[0]
                                                                          , mac[1]
                                                                          , mac[2]
                                                                          , mac[3]
                                                                          , mac[4]
                                                                          , mac[5]
                                                                          ));
                            }
                        }

                        return(xmlo);
                    }
                }
            }
Beispiel #2
0
            // Wiresharkの表示を参考にした
            public static XmlDocument Query(IPEndPoint ip, int millisecs) {
                using (UdpClient client = new UdpClient(ip.AddressFamily)) {
                    client.Connect(ip);

                    Random rand = new Random();
                    ushort seed = (ushort)rand.Next();
                    {
                        MemoryStream os = new MemoryStream();
                        BEW wr = new BEW(os);
                        wr.Write(seed);
                        wr.Write((ushort)0);//Flags: 0x0000 (Name query)
                        wr.Write((ushort)1);//Questions: 1
                        wr.Write((ushort)0);//Answer RRs: 0
                        wr.Write((ushort)0);//Authority RRs: 0
                        wr.Write((ushort)0);//Additional RRs: 0

                        wr.Write((byte)32);
                        wr.Write(Encoding.ASCII.GetBytes("CKAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
                        wr.Write((byte)0);

                        wr.Write((ushort)0x21);//Type: NBSTAT
                        wr.Write((ushort)0x01);//Class: IN

                        byte[] bin = os.ToArray();
                        if (client.Send(bin, bin.Length) != bin.Length) throw new EndOfStreamException();
                    }

                    {
                        XmlDocument xmlo = new XmlDocument();
                        XmlElement elroot = xmlo.CreateElement("wins");
                        xmlo.AppendChild(elroot);
                        XmlElement elres = xmlo.CreateElement("response");
                        elroot.AppendChild(elres);

                        byte[] bin;
                        {
                            IPEndPoint ipr = new IPEndPoint(0, 0);
                            IAsyncResult ar = client.BeginReceive(CbReceived, null);
                            if (false == ar.AsyncWaitHandle.WaitOne(millisecs, false)) {
                                client.Close();
                            }
                            bin = client.EndReceive(ar, ref ipr);
                        }
                        MemoryStream si = new MemoryStream(bin, false);
                        BER br = new BER(si);
                        ushort rseed = br.ReadUInt16();
                        if (rseed != seed) throw new InvalidDataException();
                        ushort fl = br.ReadUInt16();
                        if (0 == (fl & 0x8000) || 0 != (fl & 15)) throw new InvalidDataException();

                        {
                            int cnt = br.ReadUInt16();
                            for (int x = 0; x < cnt; ) {
                                throw new NotSupportedException();
                            }
                        }

                        int[] acnt = new int[3];
                        acnt[0] = br.ReadUInt16();
                        acnt[1] = br.ReadUInt16();
                        acnt[2] = br.ReadUInt16();

                        for (int t = 0; t < 3; t++) {
                            XmlElement elrrs = null;
                            if (t == 0) elrrs = xmlo.CreateElement("answer-rrs");
                            if (t == 1) elrrs = xmlo.CreateElement("authority-rrs");
                            if (t == 2) elrrs = xmlo.CreateElement("additional-rrs");
                            elres.AppendChild(elrrs);

                            for (int x = 0; x < acnt[t]; x++) {
                                XmlElement ela = xmlo.CreateElement("answer");
                                elrrs.AppendChild(ela);

                                byte cb = br.ReadByte();
                                byte[] name = br.ReadBytes(cb);
                                ela.SetAttribute("raw-name", Encoding.ASCII.GetString(name));
                                byte nodeType = br.ReadByte();
                                ela.SetAttribute("node-type", nodeType.ToString("x2"));

                                int atype = br.ReadUInt16();
                                if (atype != 0x21) throw new NotSupportedException();

                                int aclass = br.ReadUInt16();
                                if (aclass != 1) throw new NotSupportedException();

                                uint attl = br.ReadUInt32();
                                ela.SetAttribute("ttl", attl.ToString());

                                br.ReadUInt16();

                                byte aNamecnt = br.ReadByte();
                                for (int a = 0; a < aNamecnt; a++) {
                                    String aname = Encoding.Default.GetString(br.ReadBytes(15));
                                    byte anty = br.ReadByte();
                                    int afl = br.ReadUInt16();

                                    XmlElement elan = xmlo.CreateElement("name");
                                    ela.AppendChild(elan);
                                    elan.SetAttribute("name", aname);
                                    elan.SetAttribute("name-type", anty.ToString("x2"));
                                    elan.SetAttribute("flags", afl.ToString("x4"));
                                }

                                byte[] mac = br.ReadBytes(6);
                                ela.SetAttribute("unit-id", String.Format("{0:x2}:{1:x2}:{2:x2}:{3:x3}:{4:x2}:{5:x2}"
                                    , mac[0]
                                    , mac[1]
                                    , mac[2]
                                    , mac[3]
                                    , mac[4]
                                    , mac[5]
                                    ));
                            }
                        }

                        return xmlo;
                    }
                }
            }