public override string ToString() { StringBuilder sb = new StringBuilder(); if (Addr != null) { sb.AppendFormat(" Address: {0}\n", Addr.ToString()); } if (Netmask != null) { sb.AppendFormat(" Subnet Mask: {0}\n", Netmask.ToString()); } if (Broadaddr != null) { sb.AppendFormat(" Broadcast Address: {0}\n", Broadaddr.ToString()); } if (Dstaddr != null) { sb.AppendFormat(" Destination Address: {0}\n", Dstaddr.ToString()); } return(sb.ToString()); }
/// <summary> /// ToString override /// </summary> /// <returns> /// A <see cref="string"/> /// </returns> public override string ToString() { StringBuilder sb = new StringBuilder(); if (Addr != null) { sb.AppendFormat("Addr: {0}\n", Addr.ToString()); } if (Netmask != null) { sb.AppendFormat("Netmask: {0}\n", Netmask.ToString()); } if (Broadaddr != null) { sb.AppendFormat("Broadaddr: {0}\n", Broadaddr.ToString()); } if (Dstaddr != null) { sb.AppendFormat("Dstaddr: {0}\n", Dstaddr.ToString()); } return(sb.ToString()); }
public override int GetHashCode() { unchecked { return((Network.GetHashCode() * 397) ^ Netmask.GetHashCode()); } }
public bool GetMatchingASNAndOrgForIP(string IP, out string ASN, out string Org, out string NetmaskString) { UInt32 IPValue = Netmask.IPAsInteger(IP); foreach (var Pair in ASNToNetmask) { foreach (var CurrentNetmask in Pair.Value) { if (Netmask.IPMatchesNetmask(IPValue, CurrentNetmask)) { NetmaskString = CurrentNetmask.NetmaskString; ASN = Pair.Key.ToString(); if (!ASNToOwner.TryGetValue(Pair.Key, out Org)) { Org = "Unknown"; } return(true); } } } ASN = ""; Org = ""; NetmaskString = ""; return(false); }
public static void DeleteStaticRoute(VyattaConfigObject ConfigRoot, RouterData Data, string Network) { if (Network.Contains("-")) { Network = Netmask.IPRangeToCIDR(Network); } ConfigRoot.Delete(string.Format("protocols:static:route {0}", Network)); }
public FirewallObject GetPayload() { if (!string.IsNullOrEmpty(Netmask) && Netmask.Contains("/")) { return(new SubnetObject( Name, IPAddress.Parse(Netmask.Split('/')[0]), ushort.Parse(Netmask.Split('/')[1]), Description)); } if (!string.IsNullOrEmpty(Range)) { return(new AddressRangeObject( Name, IPAddress.Parse(Range.Split('-')[0]), IPAddress.Parse(Range.Split('-')[1]), Description)); } return(new AddressObject(Name, IPAddress.Parse(Netmask), Description)); }
public static VyattaConfigObject AddStaticRoute(VyattaConfigObject ConfigRoot, RouterData Data, string Network, string Target, string Description) { if (Network.Contains("-")) { Network = Netmask.IPRangeToCIDR(Network); } foreach (var Int in Data.Interfaces) { if ((Int.Description == Target || Int.Interface == Target) && Int.Gateway != null) { Target = Int.Gateway; break; } } string DescriptionOut = string.Format("VCU-Auto: {0}", Description); VyattaConfigObject Route = ConfigRoot.AddObject(string.Format("protocols:static:route {0}:next-hop {1}", Network, Target)); Route.AddAttribute("description").Add(DescriptionOut); return(Route); }
/// <summary> /// 数据包监视分析线程。 /// </summary> private void WatchThread() { try { while (true) { // 从队列中请求一个数据包 RawCapture packet; if ((packet = NextRawCapture) != null) { // 分析数据包中的数据 var ether = new EthernetPacket(new ByteArraySegment(packet.Data)); // 设置抓包过滤器后不再需要此项 // if (ether.Type != EthernetPacketType.IPv4) continue; // 分析IPv4数据包 var ipv4 = (IPv4Packet)ether.PayloadPacket; // 设置抓包过滤器后不再需要此项 // if (ipv4.Protocol != IPProtocolType.TCP) continue; // 分析TCP数据包 var tcp = (TcpPacket)ipv4.PayloadPacket; if (!tcp.Ack) { continue; } // 获取IP数据包内的源地址、目标地址以及设备上的子网掩码 byte[] src = ipv4.SourceAddress.GetAddressBytes(), dst = ipv4.DestinationAddress.GetAddressBytes(), netmask = Netmask.GetAddressBytes(); // 通过子网掩码计算网络号 for (var i = 0; i < 4; i++) { src[i] = (byte)(src[i] & netmask[i]); dst[i] = (byte)(dst[i] & netmask[i]); } // 判断IP数据包内的源网络号和目标网络号哪个在当前子网内,并将其作为源地址。 IPAddress srcNetworkNumber = new IPAddress(src), srcAddress, dstAddress; ushort srcPort, dstPort; if (Equals(srcNetworkNumber, NetworkNumber)) { srcAddress = ipv4.SourceAddress; srcPort = tcp.SourcePort; dstAddress = ipv4.DestinationAddress; dstPort = tcp.DestinationPort; } else { srcAddress = ipv4.DestinationAddress; srcPort = tcp.DestinationPort; dstAddress = ipv4.SourceAddress; dstPort = tcp.SourcePort; } var link = new TcpLink(srcAddress, srcPort, dstAddress, dstPort); // 保存或更新可能的TCP连接信息 lock (_possibleTcpLinks) { if (!_possibleTcpLinks.ContainsKey(link)) { // 此连接不存在于可能的TCP连接列表中 lock (_tcpLinks) { // 查找连接信息 if (_tcpLinks.All(item => !(item.SrcAddress.Equals(srcAddress) && item.SrcPort == srcPort) || !(item.DstAddress.Equals(dstAddress) && item.DstPort == dstPort))) { // 此连接也不存在于TCP连接列表中 if (!tcp.Fin && !tcp.Rst) { // 增加新连接到列表中 _possibleTcpLinks.Add(link, 1); } } else { // 此连接存在于TCP连接列表中 var tcpLink = _tcpLinks.Find(item => item.SrcAddress.Equals(srcAddress) && item.SrcPort == srcPort && item.DstAddress.Equals(dstAddress) && item.DstPort == dstPort); if (!tcp.Fin && !tcp.Rst) { // 连接仍然存活,更新状态 tcpLink.UpdateTime(); tcpLink.LastPacket = ether; } else // 检测到Fin或Rst标志且Ack标志激活,则此连接的终止已被接受,移除连接 { _tcpLinks.Remove(tcpLink); } } } } else { // 此连接存在于可能的TCP连接列表中 // 检测到Fin或Rst标志且Ack标志激活,则此连接的终止已被接受,永久移除此连接 if (tcp.Fin || tcp.Rst) { _possibleTcpLinks.Remove(link); continue; } // 连接仍然存活,更新最后发现时间及发现次数 if (_possibleTcpLinks[link] == 2) { lock (_tcpLinks) { // 已经监测到此连接两次,可以确定连接存在 _possibleTcpLinks.Remove(link); _tcpLinks.Add(link); _tcpLinks[_tcpLinks.Count - 1].LastPacket = ether; } } else { // 连接可信度仍然不高,继续监测 _possibleTcpLinks[link]++; _possibleTcpLinks.Keys.First(item => item.Equals(link)).UpdateTime(); } } } } else // 队列尚未获得数据,挂起等待 { Thread.Sleep(100); } } } catch (ThreadAbortException) { } }
public bool DoWork(Util.UpdateStatusDelegate SetStatus, Util.ShouldCancelDelegate ShouldCancel) { this.SetStatus = SetStatus; this.ShouldCancel = ShouldCancel; SetStatus("Starting downloads...", 0); char[] Separators = new char[] { '\t' }; string NetmaskToASN = AppDataPath + "NetmaskToASN"; string ASNToOwner = AppDataPath + "ASNToOwner"; if (!DownloadFile(@"http://thyme.apnic.net/current/data-raw-table", NetmaskToASN)) { return(false); } SectionsCompleted++; if (!DownloadFile(@"http://thyme.apnic.net/current/data-used-autnums", ASNToOwner)) { return(false); } SectionsCompleted++; SetStatus("Reading ASN data...", ProgressIntForSection(0)); if (File.Exists(NetmaskToASN + ".bin")) { using (MemoryStream NetmaskToASNBinMS = new MemoryStream(File.ReadAllBytes(NetmaskToASN + ".bin"))) using (BinaryReader NetmaskToASNBin = new BinaryReader(NetmaskToASNBinMS)) { while (NetmaskToASNBin.BaseStream.Position != NetmaskToASNBin.BaseStream.Length) { Int32 ASN = NetmaskToASNBin.ReadInt32(); uint MaskedAddress = NetmaskToASNBin.ReadUInt32(); byte MaskedBits = NetmaskToASNBin.ReadByte(); string NetmaskString = NetmaskToASNBin.ReadString(); Netmask Value = new Netmask(); Value.MaskedAddress = MaskedAddress; Value.MaskBits = MaskedBits; Value.MaskValue = MaskedBits == 0 ? 0 : (~(((UInt32)1 << (int)(32 - MaskedBits)) - 1)); Value.NetmaskString = NetmaskString; Value.ASN = (UInt32)ASN; List <Netmask> TargetList; if (!ASNDataOutput.ASNToNetmask.TryGetValue(ASN, out TargetList)) { TargetList = new List <Netmask>(); ASNDataOutput.ASNToNetmask.Add(ASN, TargetList); } TargetList.Add(Value); } } } else { string[] NetmaskToASNLines = File.ReadAllLines(NetmaskToASN); char[] NetmaskSplitSep = new char[] { '/' }; char[] IPSep = new char[] { '.' }; using (FileStream NetmaskToASNBinFS = new FileStream(NetmaskToASN + ".bin", FileMode.CreateNew)) using (BinaryWriter NetmaskToASNBin = new BinaryWriter(NetmaskToASNBinFS)) { foreach (var Line in NetmaskToASNLines) { string[] LineSplit = Line.Split(Separators); if (LineSplit.Length != 2) { throw new Exception(string.Format("Failed to parse netmask to ASN data file on line {0}", Line)); } Int32 ASN; if (!Int32.TryParse(LineSplit[1], out ASN)) { throw new Exception(string.Format("Failed to parse ASN on line {0}", Line)); } List <Netmask> TargetList; if (!ASNDataOutput.ASNToNetmask.TryGetValue(ASN, out TargetList)) { TargetList = new List <Netmask>(); ASNDataOutput.ASNToNetmask.Add(ASN, TargetList); } Netmask?Value = Netmask.GetNetmaskFromString(LineSplit[0]); if (Value.HasValue) { TargetList.Add(Value.Value); NetmaskToASNBin.Write(ASN); NetmaskToASNBin.Write(Value.Value.MaskedAddress); NetmaskToASNBin.Write((byte)Value.Value.MaskBits); NetmaskToASNBin.Write(Value.Value.NetmaskString); } } } } string[] ASNToOwnerLines = File.ReadAllLines(ASNToOwner); Regex ASNToOwnerRegex = new Regex(@"^\s*([0-9]+)\s+(.*)$"); foreach (var Line in ASNToOwnerLines) { Match Match = ASNToOwnerRegex.Match(Line); if (!Match.Success) { throw new Exception(string.Format("Failed to parse ASN to owner data file on line {0}", Line)); } List <int> TargetList; if (!ASNDataOutput.OwnerToASN.TryGetValue(Match.Groups[2].Value, out TargetList)) { TargetList = new List <int>(); ASNDataOutput.OwnerToASN.Add(Match.Groups[2].Value, TargetList); } ASNDataOutput.ASNToOwner[Convert.ToInt32(Match.Groups[1].Value)] = Match.Groups[2].Value; TargetList.Add(Convert.ToInt32(Match.Groups[1].Value)); } SetStatus("Processing ASN data...", ProgressIntForSection(20)); SectionsCompleted++; SetStatus("Downloads complete.", 100); return(true); }