Ejemplo n.º 1
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (!ValidateRoute())
            {
                return;
            }
            Iphlpapi.DeleteRoute(destination, prefix, gateway, interfaceIndex);
            Iphlpapi.MIB_IPFORWARD_TYPE type = Iphlpapi.MIB_IPFORWARD_TYPE.MIB_IPROUTE_TYPE_INDIRECT;
            int ifIndex = int.Parse(Regex.Replace(routeInterface.Text, @"^(\d+) .*$", "$1"));

            // if on-link set type to direct for XP
            if (Environment.OSVersion.Version.CompareTo(new Version("6.0")) < 0)
            {
                NetworkInterface nic;
                if (ifIndex == 1)
                {
                    nic = NetworkInterface.Loopback;
                }
                else
                {
                    nic = Global.NetworkInterfaces.Values.Where((i) => i.Index == ifIndex).First();
                }
                if (ipVersion == 4)
                {
                    if (nic.IPv4Address.Where((i) => i.Address == routeGateway.Text).Count() > 0)
                    {
                        type = Iphlpapi.MIB_IPFORWARD_TYPE.MIB_IPROUTE_TYPE_DIRECT;
                    }
                    //if (nic.Index == 1 && IP.CheckIfSameNetwork("127.0.0.1", routeGateway.Text, "255.0.0.0"))
                    //    type = Iphlpapi.MIB_IPFORWARD_TYPE.MIB_IPROUTE_TYPE_DIRECT;
                }
                else
                {
                    if (nic.IPv6Address.All.Where((i) => i.Address == routeGateway.Text).Count() > 0)
                    {
                        type = Iphlpapi.MIB_IPFORWARD_TYPE.MIB_IPROUTE_TYPE_DIRECT;
                    }
                    //if (nic.Index == 1 && IP.CheckIfSameNetwork("::1", routeGateway.Text, "128"))
                    //    type = Iphlpapi.MIB_IPFORWARD_TYPE.MIB_IPROUTE_TYPE_DIRECT;
                }
                if (routeGateway.Text == "0.0.0.0" || routeGateway.Text == "::")
                {
                    type = Iphlpapi.MIB_IPFORWARD_TYPE.MIB_IPROUTE_TYPE_DIRECT;
                }
            }
            Iphlpapi.AddRoute(routeDestination.Text, routePrefix.Text, routeGateway.Text, ifIndex.ToString(), routeMetric.Text, type);
            Close();
        }
Ejemplo n.º 2
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (!ValidateRoute())
            {
                return;
            }
            Iphlpapi.MIB_IPFORWARD_TYPE type = Iphlpapi.MIB_IPFORWARD_TYPE.MIB_IPROUTE_TYPE_INDIRECT;
            int ifIndex = int.Parse(Regex.Replace(routeInterface.Text, @"^(\d+) .*$", "$1"));

            // if on-link set type to direct for XP
            if (Environment.OSVersion.Version.CompareTo(new Version("6.0")) < 0)
            {
                NetworkInterface nic;
                if (ifIndex == 1)
                {
                    nic = NetworkInterface.Loopback;
                }
                else
                {
                    nic = Global.NetworkInterfaces.Values.Where((i) => i.Index == ifIndex).First();
                }
                if (ipVersion == 4)
                {
                    if (nic.IPv4Address.Where((i) => i.Address == routeGateway.Text).Count() > 0)
                    {
                        type = Iphlpapi.MIB_IPFORWARD_TYPE.MIB_IPROUTE_TYPE_DIRECT;
                    }
                    //if (nic.Index == 1 && IP.CheckIfSameNetwork("127.0.0.1", routeGateway.Text, "255.0.0.0"))
                    //    type = Iphlpapi.MIB_IPFORWARD_TYPE.MIB_IPROUTE_TYPE_DIRECT;
                }
                else
                {
                    if (nic.IPv6Address.All.Where((i) => i.Address == routeGateway.Text).Count() > 0)
                    {
                        type = Iphlpapi.MIB_IPFORWARD_TYPE.MIB_IPROUTE_TYPE_DIRECT;
                    }
                    //if (nic.Index == 1 && IP.CheckIfSameNetwork("::1", routeGateway.Text, "128"))
                    //    type = Iphlpapi.MIB_IPFORWARD_TYPE.MIB_IPROUTE_TYPE_DIRECT;
                }
                if (routeGateway.Text == "0.0.0.0" || routeGateway.Text == "::")
                {
                    type = Iphlpapi.MIB_IPFORWARD_TYPE.MIB_IPROUTE_TYPE_DIRECT;
                }
            }
            Iphlpapi.AddRoute(routeDestination.Text, routePrefix.Text, routeGateway.Text, ifIndex.ToString(), routeMetric.Text, type);
            // save route
            if (checkBox1.Checked)
            {
                Config.SavedRouteItem savedRoute = new Config.SavedRouteItem();
                savedRoute.Name        = savedRouteName.Text;
                savedRoute.Destination = routeDestination.Text;
                savedRoute.Prefix      = routePrefix.Text;
                savedRoute.Gateway     = routeGateway.Text;
                if (ifIndex == 1)
                {
                    savedRoute.InterfaceGuid = NetworkInterface.Loopback.Guid;
                }
                else
                {
                    savedRoute.InterfaceGuid = Global.NetworkInterfaces.Values.Where((i) => i.Index == ifIndex).FirstOrDefault().Guid;
                }
                savedRoute.Metric    = ushort.Parse(routeMetric.Text);
                savedRoute.IPVersion = ipVersion;
                int result = Global.Config.SavedRoutes.AddNode(treeView1, savedRoute);
                if (result == 1)
                {
                    new BalloonTip("Error", "Invalid destination", button1, BalloonTip.ICON.ERROR);
                    return;
                }
                if (result == 2)
                {
                    DialogResult dialogResult = MessageBox.Show("Route name already used at current destination.\nDo you want to overwrite?", "Overwrite confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
                    if (dialogResult == DialogResult.No)
                    {
                        return;
                    }
                    if (treeView1.SelectedNode.ImageIndex == (int)Config.SavedRouteNode.ImageIndex.Group)
                    {
                        treeView1.SelectedNode = treeView1.SelectedNode.Nodes.Find(savedRouteName.Text, false).First();
                    }
                    else if (treeView1.SelectedNode.Name != savedRouteName.Text)
                    {
                        treeView1.SelectedNode = treeView1.SelectedNode.Parent.Nodes.Find(savedRouteName.Text, false).First();
                    }
                    Global.Config.SavedRoutes.DeleteNode(treeView1);
                    Global.Config.SavedRoutes.AddNode(treeView1, savedRoute);
                }
            }
            Close();
        }
Ejemplo n.º 3
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (!ValidateConfigs())
            {
                return;
            }
            IPAddress ipAddress = new IPAddress(0);

            for (int j = 0; j < loadRoutes.Count; j++)
            {
                if (listView1.Items[j].Checked &&
                    ValidateRoute(ref loadRoutes[j].Destination, ref loadRoutes[j].Prefix, ref loadRoutes[j].Gateway, loadRoutes[j].IPVersion))
                {
                    Config.SavedRouteItem savedRoute = loadRoutes[j];
                    int ifIndex = 0;
                    if (Global.NetworkInterfaces.ContainsKey(savedRoute.InterfaceGuid))
                    {
                        ifIndex = Global.NetworkInterfaces[savedRoute.InterfaceGuid].Index;
                    }
                    else if (Environment.OSVersion.Version.CompareTo(new Version("6.0")) > -1 &&
                             NetworkInterface.Loopback.Guid == savedRoute.InterfaceGuid)
                    {
                        ifIndex = 1;
                    }
                    // load defaults
                    if (defaultInterfaceMode.SelectedIndex == 0 && ifIndex == 0 ||
                        defaultInterfaceMode.SelectedIndex == 1)
                    {
                        if (loadRoutes[j].IPVersion == 4)
                        {
                            ifIndex = int.Parse(Regex.Replace(defaultIPv4Interface.Text, @"^(\d+) .*$", "$1"));
                            if (defaultIPv4GatewayMode.SelectedIndex != 3)
                            {
                                loadRoutes[j].Gateway = defaultIPv4Gateway.Text;
                            }
                        }
                        else
                        {
                            ifIndex = int.Parse(Regex.Replace(defaultIPv6Interface.Text, @"^(\d+) .*$", "$1"));
                            if (defaultIPv6GatewayMode.SelectedIndex != 3)
                            {
                                loadRoutes[j].Gateway = defaultIPv6Gateway.Text;
                            }
                        }
                    }
                    // if on-link set type to direct for XP
                    Iphlpapi.MIB_IPFORWARD_TYPE type = Iphlpapi.MIB_IPFORWARD_TYPE.MIB_IPROUTE_TYPE_INDIRECT;
                    NetworkInterface            nic;
                    if (ifIndex == 1)
                    {
                        nic = NetworkInterface.Loopback;
                    }
                    else
                    {
                        nic = Global.NetworkInterfaces.Values.Where((i) => i.Index == ifIndex).First();
                    }
                    if (Environment.OSVersion.Version.CompareTo(new Version("6.0")) < 0)
                    {
                        if (savedRoute.IPVersion == 4)
                        {
                            if (nic.IPv4Address.Where((i) => i.Address == savedRoute.Gateway).Count() > 0)
                            {
                                type = Iphlpapi.MIB_IPFORWARD_TYPE.MIB_IPROUTE_TYPE_DIRECT;
                            }
                        }
                        else
                        {
                            if (nic.IPv6Address.All.Where((i) => i.Address == savedRoute.Gateway).Count() > 0)
                            {
                                type = Iphlpapi.MIB_IPFORWARD_TYPE.MIB_IPROUTE_TYPE_DIRECT;
                            }
                        }
                        if (savedRoute.Gateway == "0.0.0.0" || savedRoute.Gateway == "::")
                        {
                            type = Iphlpapi.MIB_IPFORWARD_TYPE.MIB_IPROUTE_TYPE_DIRECT;
                        }
                    }
                    // correction for Vista->XP transitioned saved route
                    if (Environment.OSVersion.Version.CompareTo(new Version("6.0")) < 0)
                    {
                        if (IPAddress.TryParse(savedRoute.Gateway, out ipAddress))
                        {
                            if (IPAddress.Parse(savedRoute.Gateway).GetAddressBytes().Max() == 0)
                            {
                                if (savedRoute.IPVersion == 4)
                                {
                                    savedRoute.Gateway = nic.IPv4Address.First().Address;
                                }
                                else
                                {
                                    savedRoute.Gateway = nic.IPv6Address.All.First().Address;
                                }
                            }
                        }
                        if (savedRoute.Metric == 0)
                        {
                            savedRoute.Metric = 1;
                        }
                    }
                    Iphlpapi.DeleteRoute(savedRoute.Destination, savedRoute.Prefix, savedRoute.Gateway, ifIndex.ToString());
                    Iphlpapi.AddRoute(savedRoute.Destination, savedRoute.Prefix, savedRoute.Gateway, ifIndex.ToString(), savedRoute.Metric.ToString(), type);
                    if (updateSavedRoutesCheckBox.Checked == true)
                    {
                        // TODO: update saved route with new interface + gw
                    }
                }
                else
                {
                    MessageBox.Show("Could not load route:\nDestination: " + loadRoutes[j].Destination + "\nPrefix: " + loadRoutes[j].Prefix + "\nGateway: " + loadRoutes[j].Gateway + "\nIP version: " + loadRoutes[j].IPVersion, "Invalid route", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            Close();
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Load in FIB
 /// </summary>
 /// <param name="nic">Override saved egress interface</param>
 /// <param name="gateway">Override saved gateway</param>
 /// <param name="metric">Override saved metric</param>
 public void Load(NetworkInterface nic = null, string gateway = null, int metric = -1)
 {
     if (nic == null)
     {
         if (Global.NetworkInterfaces.ContainsKey(this.InterfaceGuid))
         {
             nic = Global.NetworkInterfaces[this.InterfaceGuid];
         }
         else if (Environment.OSVersion.Version.CompareTo(new Version("6.0")) > -1 &&
                  NetworkInterface.Loopback.Guid == this.InterfaceGuid)
         {
             nic = NetworkInterface.Loopback;
         }
     }
     if (gateway == null)
     {
         gateway = Gateway;
     }
     if (metric < 0)
     {
         metric = Metric;
     }
     // if on-link set type to direct for XP
     Iphlpapi.MIB_IPFORWARD_TYPE type = Iphlpapi.MIB_IPFORWARD_TYPE.MIB_IPROUTE_TYPE_INDIRECT;
     if (Environment.OSVersion.Version.CompareTo(new Version("6.0")) < 0)
     {
         if (this.IPVersion == 4)
         {
             if (nic.IPv4Address.Where((i) => i.Address == gateway).Count() > 0)
             {
                 type = Iphlpapi.MIB_IPFORWARD_TYPE.MIB_IPROUTE_TYPE_DIRECT;
             }
         }
         else
         {
             if (nic.IPv6Address.All.Where((i) => i.Address == gateway).Count() > 0)
             {
                 type = Iphlpapi.MIB_IPFORWARD_TYPE.MIB_IPROUTE_TYPE_DIRECT;
             }
         }
         if (gateway == "0.0.0.0" || gateway == "::")
         {
             type = Iphlpapi.MIB_IPFORWARD_TYPE.MIB_IPROUTE_TYPE_DIRECT;
         }
     }
     // correction for Vista->XP transitioned saved route
     if (Environment.OSVersion.Version.CompareTo(new Version("6.0")) < 0)
     {
         if (IPAddress.TryParse(gateway, out IPAddress ipAddress))
         {
             if (IPAddress.Parse(gateway).GetAddressBytes().Max() == 0)
             {
                 if (this.IPVersion == 4)
                 {
                     gateway = nic.IPv4Address.First().Address;
                 }
                 else
                 {
                     gateway = nic.IPv6Address.All.First().Address;
                 }
             }
         }
         if (metric == 0)
         {
             metric = 1;
         }
     }
     Iphlpapi.DeleteRoute(this.Destination, this.Prefix, gateway, nic.Index.ToString());
     Iphlpapi.AddRoute(this.Destination, this.Prefix, gateway, nic.Index.ToString(), metric.ToString(), type);
 }