コード例 #1
0
        private IPAddress GetBroadcastPoint()
        {
            if (IP != null && IPv4Mask != null)
            {
                byte[] a = IP.GetAddressBytes();
                byte[] b = IPv4Mask.GetAddressBytes();

                byte[] c = new byte[a.Length];
                for (int i = 0; i < a.Length; i++)
                {
                    int d = a[i] & b[i];
                    c[i] = (byte)d;
                }
                for (int i = 0; i < c.Length; i++)
                {
                    if (c[i] == 0)
                    {
                        c[i] = 255;
                    }
                }

                string e = c[0] + "." + c[1] + "." + c[2] + "." + c[3];

                IPAddress iPAddress = IPAddress.Parse(e);
                return(iPAddress);
            }
            else
            {
                return(null);
            }
        }
コード例 #2
0
        public static string GetIPAddressLike(String target)
        {
            Console.WriteLine(target);
            IPHostEntry Host     = default(IPHostEntry);
            string      Hostname = Environment.MachineName;

            Host = Dns.GetHostEntry(Hostname);
            IPAddress targetIPA  = IPAddress.Parse(target.Trim());
            String    IPAddresss = "";
            int       best       = 0;

            foreach (IPAddress IP in Host.AddressList)
            {
                Console.WriteLine(IP);
                if (IP.AddressFamily == AddressFamily.InterNetwork)
                {
                    int matches = 0;
                    for (int i = 0; i < 4; i++)
                    {
                        if (IP.GetAddressBytes()[i] == targetIPA.GetAddressBytes()[i])
                        {
                            matches++;
                        }
                    }
                    if (matches > best)
                    {
                        best       = matches;
                        IPAddresss = Convert.ToString(IP);
                    }
                }
            }
            return(IPAddresss);
        }
コード例 #3
0
        private byte[] ToAPayload()
        {
            using (var ms = new MemoryStream())
                using (var writer = new BinaryWriter(ms))
                {
                    // Write the service name
                    var modName      = new DNS.DomainName(Name.Split('.')[0] + ".local");
                    var modNameBytes = modName.ToBytes();
                    writer.Write(modNameBytes, 0, modNameBytes.Length);
                    writer.Write((byte)0);

                    // A Record in Internet Class
                    writer.Write(new byte[4] {
                        0x00, 0x01, 0x80, 0x01
                    }, 0, 4);

                    // We need to write the TTL
                    byte[] ttlBytes = BitConverter.GetBytes(TTL);
                    byte[] newTTL   = new byte[4] {
                        ttlBytes[3], ttlBytes[2], ttlBytes[1], ttlBytes[0]
                    };
                    writer.Write(newTTL, 0, 4);

                    // Write the data length
                    writer.Write(new byte[2] {
                        0x00, 0x04
                    }, 0, 2);

                    // Write the IP
                    writer.Write(IP.GetAddressBytes(), 0, 4);

                    writer.Flush();
                    return(ms.ToArray());
                }
        }
コード例 #4
0
ファイル: TCPAddress.cs プロジェクト: xpA076/Socket
 public byte[] GetBytes()
 {
     byte[] ip_bytes = IP.GetAddressBytes();
     return(new byte[6]
     {
         ip_bytes[0],
         ip_bytes[1],
         ip_bytes[2],
         ip_bytes[3],
         (byte)((Port >> 8) & 0xFF),
         (byte)(Port & 0xFF)
     });
 }
コード例 #5
0
        public static uint IPToInt(this string Host)
        {
            IPAddress IP;

            if (IPAddress.TryParse(Host, out IP))
            {
                byte[] temp = IP.GetAddressBytes();
                Array.Reverse(temp);
                return(BitConverter.ToUInt32(temp, 0));
            }
            else
            {
                return(0);
            }
        }
コード例 #6
0
ファイル: TcpParameters.cs プロジェクト: xiltoos/Anviz
        internal byte[] ToArray()
        {
            var ret = new byte[27];

            IP.GetAddressBytes().CopyTo(ret, 0);
            SubnetMask.GetAddressBytes().CopyTo(ret, 4);
            MacAddress.GetAddressBytes().CopyTo(ret, 8);
            DefaultGateway.GetAddressBytes().CopyTo(ret, 14);
            Server.GetAddressBytes().CopyTo(ret, 18);
            ret[22] = FarLimit;
            Bytes.Write(2, ComPort).CopyTo(ret, 23);
            ret[25] = (byte)TcpMode;
            ret[26] = (byte)(DhcpEnabled ? 1 : 0);
            return(ret);
        }
コード例 #7
0
ファイル: IpHelper.cs プロジェクト: Fortitude444/IP-Changer
        private static uint ReturnFirstOctet(string ipAddress)
        {
            IPAddress IP;
            bool      flag = IPAddress.TryParse(ipAddress, out IP);

            if (flag == true)
            {
                IP = IPAddress.Parse(ipAddress);
                byte[] byteIP   = IP.GetAddressBytes();
                uint   ipInUint = (uint)byteIP[0];

                return(ipInUint);
            }
            else
            {
                return(0);
            }
        }
コード例 #8
0
 public static string GetCountryCodeFromIP(List <GeoIPInfo> geoIPInfoList, string ip)
 {
     if (!string.IsNullOrWhiteSpace(ip))
     {
         IPAddress IP;
         if (IPAddress.TryParse(ip, out IP))
         {
             byte[] bytes = IP.GetAddressBytes();
             Array.Reverse(bytes); // flip big-endian(network order) to little-endian
             uint intAddress = BitConverter.ToUInt32(bytes, 0);
             foreach (var Info in geoIPInfoList)
             {
                 if (intAddress >= Info.IPStart && intAddress <= Info.IPEnd)
                 {
                     return(Info.CountryCode);
                 }
             }
             return("-");
         }
     }
     return("-");
 }
コード例 #9
0
ファイル: DhtBucket.cs プロジェクト: nandub/DeOps
        public new void WritePacket(G2Protocol protocol, G2Frame root, byte name)
        {
            byte[] payload = new byte[PAYLOAD_SIZE];

            BitConverter.GetBytes(UserID).CopyTo(payload, 0);
            BitConverter.GetBytes(ClientID).CopyTo(payload, 8);
            BitConverter.GetBytes(UdpPort).CopyTo(payload, 10);
            BitConverter.GetBytes(TcpPort).CopyTo(payload, 12);

            G2Frame address = protocol.WritePacket(root, name, payload);

            protocol.WritePacket(address, Packet_IP, IP.GetAddressBytes());

            if (TunnelServer != null)
            {
                TunnelServer.WritePacket(protocol, address, Packet_Server);
            }

            if (TunnelClient != null)
            {
                protocol.WritePacket(address, Packet_Client, TunnelClient.ToBytes());
            }
        }
コード例 #10
0
ファイル: DNSServer.cs プロジェクト: DFishSoft/DnsService
        /// <summary>
        /// 域名解析过程
        /// </summary>
        /// <param name="data"></param>
        private void Working(WorkingData data)
        {
            DnsPack  dnsPack = new DnsPack(data.dnsPack);
            EndPoint client  = data.client;


            //本机PTR指针
            if (dnsPack.QR == 0 && dnsPack.Opcode == 0 && dnsPack.QueryRecords.Count >= 1)
            {
                if (dnsPack.QueryRecords[0].QueryType == QueryType.PTR)
                {
                    string      hostName  = Dns.GetHostName();
                    IPAddress[] localhost = Dns.GetHostAddresses(hostName);
                    foreach (IPAddress IP in localhost)
                    {
                        if (IP.AddressFamily == AddressFamily.InterNetwork)
                        {
                            byte[] ip     = IP.GetAddressBytes();
                            string ptrstr = string.Format("{3}.{2}.{1}.{0}.{4}", ip[0], ip[1], ip[2], ip[3], @"in-addr.arpa");

                            if (dnsPack.QueryRecords[0].QueryName == ptrstr ||
                                dnsPack.QueryRecords[0].QueryName == @"1.0.0.127.in-addr.arpa")
                            {
                                dnsPack.QR             = 1; dnsPack.RA = 1; dnsPack.RD = 1;
                                dnsPack.ResouceRecords = new List <ResouceRecord> {
                                    new ResouceRecord {
                                        Point      = IPAddress.HostToNetworkOrder(BitConverter.ToInt16(new byte[] { 192, 12 }, 0)),
                                        Datas      = dnsPack.getNameByts(SeaFish.Settings.Custom.ServerName),
                                        TTL        = 100,
                                        QueryClass = 1,
                                        QueryType  = QueryType.PTR,
                                    }
                                };
                                Response(dnsPack.ToBytes(), client);
                                return;
                            }
                        }
                    }
                }
                else if (dnsPack.QueryRecords[0].QueryType == QueryType.A)
                {
                    if (dnsPack.QueryRecords[0].QueryName == SeaFish.Settings.Custom.ServerName)
                    {
                        dnsPack.QR             = 1; dnsPack.RA = 1; dnsPack.RD = 1;
                        dnsPack.ResouceRecords = new List <ResouceRecord> {
                            new ResouceRecord {
                                Point      = IPAddress.HostToNetworkOrder(BitConverter.ToInt16(new byte[] { 192, 12 }, 0)),
                                Datas      = new byte[] { 127, 0, 0, 1 },
                                TTL        = 100,
                                QueryClass = 1,
                                QueryType  = QueryType.A,
                            }
                        };
                        Response(dnsPack.ToBytes(), client);
                        return;
                    }
                }
            }


            byte[] cache;
            if (dataBase.ReadHost(dnsPack, out cache))
            {
                //如果存在host
                Response(cache, client);
            }
            else if (dataBase.ReadCache(dnsPack, out cache))
            {
                //如果存在缓存数据
                DnsPack dnsCache = new DnsPack(cache)
                {
                    //这里需要修改识别标识
                    Sign = dnsPack.Sign,
                };
                Response(dnsCache.ToBytes(), client);
            }
            else
            {
                //无缓存则直接转发
                RelayDNS(data.dnsPack, client);
            }
        }