Esempio n. 1
0
 public static bool IsSubnet(IpAddress _ip, int _maskBitsNum)
 {
     if (_ip.GetSubnetAddress(IpAddress.BuildMaskFromBits(_maskBitsNum)).CompareTo(_ip) != 0)
     {
         return(false);
     }
     return(true);
 }
Esempio n. 2
0
 public static bool IsSubnet(IpAddress _ip, IpAddress _mask)
 {
     if (_ip.GetSubnetAddress(_mask).CompareTo(_ip) != 0)
     {
         return(false);
     }
     return(true);
 }
Esempio n. 3
0
 /// <summary>
 /// 根据子网地址和掩码位数初始化子网类
 /// </summary>
 /// <param name="_ip">子网地址</param>
 /// <param name="_maskBitsNum">掩码位数</param>
 public Subnet(IpAddress _ip, int _maskBitsNum)
 {
     mask = IpAddress.BuildMaskFromBits(_maskBitsNum);
     if (_ip.GetSubnetAddress(mask).CompareTo(_ip) != 0)
     {
         throw new SubnetErrorException("子网地址和掩码不搭配");
     }
     ip = _ip;
 }
Esempio n. 4
0
 /// <summary>
 /// 根据子网地址和掩码地址初始化子网类
 /// </summary>
 /// <param name="_ip">子网地址</param>
 /// <param name="_mask">掩码地址</param>
 public Subnet(IpAddress _ip, IpAddress _mask)
 {
     if (!_mask.IsValidMask())
     {
         throw new SubnetErrorException("不是正确的掩码格式");
     }
     if (_ip.GetSubnetAddress(_mask).CompareTo(_ip) != 0)
     {
         throw new SubnetErrorException("子网地址和掩码不搭配");
     }
     ip          = _ip;
     mask        = _mask;
     maskBitsNum = _mask.GetMaskBits();
 }
Esempio n. 5
0
        private void btnAddSubnet_Click(object sender, RoutedEventArgs e)
        {
            if (tbIP.IP == null && tbMask.IP == null)
            {
                MessageBox.Show("子网地址或掩码不是正确的IP格式");
                return;
            }
            IpAddress ip   = new IpAddress(tbIP.IP);
            IpAddress mask = new IpAddress(tbMask.IP);

            if (!mask.IsValidMask())
            {
                MessageBox.Show("掩码的格式不正确");
                return;
            }
            IpAddress subnet = ip.GetSubnetAddress(mask);

            subnetList.Add(new Subnet(subnet, mask));
            tbIP.Clear();
            tbMask.Clear();
        }
Esempio n. 6
0
        //Dictionary
        private void DrawLine(Equipment equip, Equipment lastEquip)
        {
            Line l = null;


            #region  废弃,拓扑发现时不获取设备连接相关信息,trap来的时候获取
            LineInfo lInfo = new LineInfo();
            lInfo.UCEquipA = equip.UCEquipIcon;
            lInfo.UCEquipB = lastEquip.UCEquipIcon;
            try
            {
                if (equip.IpFirstGet.ToString() == "3.245.1.1" && lastEquip.IpFirstGet.ToString() == "3.247.1.1")
                {
                }
                //随便获取一个对方设备一个地址所属的子网,这样才可以从路由表中得到路由信息和其中的ifIndex
                IpAddress subnetA = equip.IpFirstGet.GetSubnetAddress(equip.IPAndInfoList[equip.IpFirstGet].IpMask);
                if (lastEquip.IpDstAndRouteInfoLIst.ContainsKey(subnetA))
                {
                    lInfo.IfIDB = lastEquip.IpDstAndRouteInfoLIst[subnetA].IfIndex;
                }
                else
                {
                    //考虑默认路由聚合路由等情况,比如到3.11.1.0/24的路由,它用3.0.0.0/8囊括了
                    //还需要考虑一种情况,使用IpFirstGet所在子网作为目的地址获取路由,万一这个是一个互联子网或者不重要的
                    //且路由协议不是动态的,它不把此子网加入到路由表怎么办?
                    bool isContainSubnet = false;
                    foreach (KeyValuePair <IpAddress, RouteInfomation> pair in lastEquip.IpDstAndRouteInfoLIst)
                    {
                        if (pair.Value.RouteType != 4)
                        {
                            continue;   //既然是默认路由,肯定是indirect(4)
                        }
                        //寻找是否有路由包含目的子网
                        if (pair.Key.CompareTo(subnetA.GetSubnetAddress(pair.Value.DstMask)) == 0)
                        {
                            isContainSubnet = true;
                            lInfo.IfIDB     = pair.Value.IfIndex;
                            break;
                        }
                    }
                    if (!isContainSubnet)
                    {
                        lInfo.IfIDB = -1; //标注为-1 代表未获取到合适的端口id,在后面的链路判断中,需要进行区分
                    }
                }
                IpAddress subnetB = lastEquip.IpFirstGet.GetSubnetAddress(lastEquip.IPAndInfoList[lastEquip.IpFirstGet].IpMask);
                if (equip.IpDstAndRouteInfoLIst.ContainsKey(subnetB))
                {
                    lInfo.IfIDA = equip.IpDstAndRouteInfoLIst[subnetB].IfIndex;
                }
                else
                {
                    bool isContainSubnet = false;
                    foreach (KeyValuePair <IpAddress, RouteInfomation> pair in equip.IpDstAndRouteInfoLIst)
                    {
                        if (pair.Value.RouteType != 4)
                        {
                            continue;   //既然是默认路由,肯定是indirect(4)
                        }
                        //寻找是否有路由包含目的子网
                        if (pair.Key.CompareTo(subnetB.GetSubnetAddress(pair.Value.DstMask)) == 0)
                        {
                            isContainSubnet = true;
                            lInfo.IfIDA     = pair.Value.IfIndex;
                            break;
                        }
                    }
                    if (!isContainSubnet)
                    {
                        lInfo.IfIDA = -1;
                    }
                }
                //将接口ID和连接线的信息加入到两边设备
                equip.IfIDandLineInfoList.Add(lInfo.IfIDA, lInfo);
                lastEquip.IfIDandLineInfoList.Add(lInfo.IfIDB, lInfo);
            }
            catch (System.Exception ex)
            {
                //lInfo = null;
                AddMessage("为连接线设置附加信息出现问题 " + ex.Message);
            }
            #endregion

            this.Dispatcher.Invoke(new Action(() =>
            {
                l = new Line();
                l.SetBinding(Line.X1Property, new Binding()
                {
                    Path = new PropertyPath("CenterPointX"), Source = equip.UCEquipIcon
                });
                l.SetBinding(Line.Y1Property, new Binding()
                {
                    Path = new PropertyPath("CenterPointY"), Source = equip.UCEquipIcon
                });
                l.SetBinding(Line.X2Property, new Binding()
                {
                    Path = new PropertyPath("CenterPointX"), Source = lastEquip.UCEquipIcon
                });
                l.SetBinding(Line.Y2Property, new Binding()
                {
                    Path = new PropertyPath("CenterPointY"), Source = lastEquip.UCEquipIcon
                });
                l.Tag         = lInfo;
                l.ContextMenu = this.FindResource("lineContextMenu") as ContextMenu;
                canvas.Children.Add(l);
            }));
            if (lInfo == null)
            {
                //出现null引用错误,调试用
            }
            lInfo.L = l;
            l.MouseLeftButtonDown += new MouseButtonEventHandler(l_MouseLeftButtonDown);
        }
Esempio n. 7
0
 /// <summary>
 /// 判断指定ip地址是否在此子网范围内
 /// </summary>
 /// <param name="_ip">指定ip地址</param>
 public bool Contains(IpAddress _ip)
 {
     return(ip.CompareTo(_ip.GetSubnetAddress(mask)) == 0 ? true : false);
 }