private void Button_Click(object sender, RoutedEventArgs e)
 {
     NetworkInfo Info = new NetworkInfo();
     try
     {
         Info.NumberOfHosts = int.Parse(NumberOfHostsTextBox.Text);
         Info.RequiredSubnets = int.Parse(RequiredSubnetsTextBox.Text);
         Info.SampleAddress = SampleAddressTextBox.Text;
         NetworkBuilder Builder = new NetworkBuilder(Info);
         SubnettingOutput Window = new SubnettingOutput(Builder.BuiltNetwork);
         Window.Show();
     }
     catch (Exception)
     {
         Helpers.MessageBox MessageBox = new Helpers.MessageBox("There was an error parsing your information");
         MessageBox.Show();
     }
 }
示例#2
0
        public Server(IPAddress externalAddress, NetworkInfo network, NodeConnectionParameters nodeConnectionParameters)
        {
            _Server = new NodeServer(network, internalPort: network.DefaultPort);
            OwnResource(_Server);

            if (externalAddress != null)
            {
                var            externalEndpoint = new IPEndPoint(externalAddress, network.DefaultPort);
                AddressManager addressManager   = AddressManagerBehavior.GetAddrman(nodeConnectionParameters);
                addressManager.Add(new NetworkAddress(externalEndpoint));
                addressManager.Connected(new NetworkAddress(externalEndpoint));
                _Server.ExternalEndpoint = externalEndpoint;
            }

            _Server.InboundNodeConnectionParameters = nodeConnectionParameters;
            _Server.AllowLocalPeers = false;             //TODO

            NodeServerTrace.Information($"Server setup at {externalAddress}");
        }
        public NetworkBuilder(NetworkInfo Info)
        {
            //// Get the nessecary data to build the subnetting information

            //int NumberOfHostsNeeded = Info.NumberOfHosts;

            //// The nessecary address class for the number of subnets and the required hosts per subnet
            //AddressClass InetClass = DetermineNessecaryAddressClass(NumberOfHostsNeeded);

            //// Uses 2^x to determine how many bits need to be borrowed
            //int BitsToBorrow = DetermineBitsToBorrow(NumberOfHostsNeeded, InetClass);

            //int BitsForAddressSpace = DetermineBitsForAddressSpace(BitsToBorrow, InetClass);

            //int RequiredSubnets = DetermineNumberOfSubnets(BitsToBorrow, InetClass);

            int BitsToBorrow = DetermineBitsToBorrow(Info.RequiredSubnets);

            AddressClass InetClass = DetermineNessecaryAddressClass(Info.NumberOfHosts);

            int AddressSpaceBits = DetermineBitsForAddressSpace(BitsToBorrow, InetClass);

            int RequiredSubnets = IntPow(2, BitsToBorrow);

            // Gets the subnet mask using data resources basedo n the required bits to borrow
            SubnetMask NetMask = GetSubnetAddress(InetClass, BitsToBorrow, AddressSpaceBits);
            IpAddress SampleAddress = new IpAddress(Info.SampleAddress);

            // The program has different functionallity based on different address class
            // For Class C it will be able to build the entire subnet out
            // For the other classes it will display generic subnetting information but not build a subnet
            switch (InetClass)
            {
                case AddressClass.A:
                    BuiltNetwork = new FullNetwork();
                    // Gather network information for Class A
                    //ClassAandBBuilder ClassABuilder = new ClassAandBBuilder();
                    //ClassABuilder.NetMask = NetMask;
                    //ClassABuilder.HostsPerSubnet = IntPow(2, (AddressSpaceBits - 2) );
                    //ClassABuilder.NumberOfSubents = IntPow(2, BitsToBorrow);
                    //BuiltNetwork.ClassAorBBuilder = ClassABuilder;
                    BuiltNetwork = new FullNetwork();
                    BuiltNetwork.BitsBorrowed = BitsToBorrow;
                    BuiltNetwork.Class = InetClass;
                    BuiltNetwork.NetMask = NetMask;
                    BuiltNetwork.NumberOfSubnets = IntPow(2, BitsToBorrow);
                    BuiltNetwork.AddressSpace = IntPow(2, AddressSpaceBits);
                    BuiltNetwork.UsableHosts = IntPow(2, AddressSpaceBits) - 2;
                    break;
                case AddressClass.B:
                    //BuiltNetwork = new FullNetwork();
                    //ClassAandBBuilder ClassBBuilder = new ClassAandBBuilder();
                    //ClassBBuilder.NetMask = NetMask;
                    //ClassBBuilder.HostsPerSubnet = IntPow(2, AddressSpaceBits);
                    //ClassBBuilder.NumberOfSubents = IntPow(2, BitsToBorrow);
                    //BuiltNetwork.ClassAorBBuilder = ClassBBuilder;
                    BuiltNetwork = new FullNetwork();
                    BuiltNetwork.BitsBorrowed = BitsToBorrow;
                    BuiltNetwork.Class = InetClass;
                    BuiltNetwork.NetMask = NetMask;
                    BuiltNetwork.NumberOfSubnets = IntPow(2, BitsToBorrow);
                    BuiltNetwork.AddressSpace = IntPow(2, AddressSpaceBits);
                    BuiltNetwork.UsableHosts = IntPow(2, AddressSpaceBits) - 2;
                    break;
                case AddressClass.C:
                    // Gather network information for Class C
                    int SubnetCount = 0;
                    BuiltNetwork = new FullNetwork();
                    // Begin the subnet building process
                    ClassCSubnetBuilder ClassCBuilder = new ClassCSubnetBuilder(NetMask, SampleAddress);
                    // Subtraction by one because it is a based of zero
                    while (SubnetCount < RequiredSubnets)
                    {
                        SubnetCount += 1;
                        Subnetwork Subnet = ClassCBuilder.NextSubnet();
                        BuiltNetwork.Subnets.Enqueue(Subnet);
                    }

                    BuiltNetwork.BitsBorrowed = BitsToBorrow;
                    BuiltNetwork.Class = InetClass;
                    BuiltNetwork.NetMask = NetMask;
                    BuiltNetwork.NumberOfSubnets = IntPow(2, BitsToBorrow);
                    BuiltNetwork.AddressSpace = (IntPow(2, AddressSpaceBits) * BuiltNetwork.NumberOfSubnets);
                    BuiltNetwork.UsableHosts = IntPow(2, AddressSpaceBits) - 2;

                    break;
                default:
                    break;
            }
        }
示例#4
0
        public async Task Connect(NetworkInfo networkInfo)
        {
            IPAddress ipAddress = null;

            WireSerialization.Instance.Magic = networkInfo.Magic;
            NodeServerTrace.Information($"Magic is {networkInfo.Magic}");

            var natManager = new NATManager(networkInfo.DefaultPort);

#if DEBUG
            if (networkInfo.IsLANHost)
            {
                ipAddress = natManager.InternalIPAddress ?? IPAddress.Loopback;
                networkInfo.PeersToFind = 0;
            }
            else if (networkInfo.IsLANClient)
            {
                var ipAddressStr = (natManager.InternalIPAddress ?? IPAddress.Loopback).ToString();
                networkInfo.PeersToFind = 1;

                if (networkInfo.Seeds.Count == 0 && !networkInfo.Seeds.Contains(ipAddressStr))
                {
                    networkInfo.Seeds.Add(ipAddressStr);
                }
            }
            else
#endif
            if (!string.IsNullOrEmpty(networkInfo.ExternalIPAddress))
            {
                ipAddress = IPAddress.Parse(networkInfo.ExternalIPAddress);
            }
            else if (networkInfo.DisableUPnP)
            {
                StatusMessageProducer.OutboundStatus = OutboundStatusEnum.Disabled;
            }
            else
            {
                StatusMessageProducer.OutboundStatus = OutboundStatusEnum.Initializing;
                await natManager.Init();

                if (natManager.DeviceFound &&
                    natManager.Mapped.Value &&
                    natManager.ExternalIPVerified.HasValue &&
                    natManager.ExternalIPVerified.Value)
                {
                    StatusMessageProducer.OutboundStatus = OutboundStatusEnum.HasValidAddress;
                    ipAddress = natManager.ExternalIPAddress;
                }
                else
                {
                    StatusMessageProducer.OutboundStatus = OutboundStatusEnum.HasInvalidAddress;
                }
            }

            _BroadcastHubBehavior = new BroadcastHubBehavior();
            _MinerBehavior        = new MinerBehavior();

            _NodeConnectionParameters.TemplateBehaviors.Add(_BroadcastHubBehavior);
            _NodeConnectionParameters.TemplateBehaviors.Add(_MinerBehavior);
            _NodeConnectionParameters.TemplateBehaviors.Add(new SPVBehavior(_BlockChain, _BroadcastHubBehavior.BroadcastHub));
            _NodeConnectionParameters.TemplateBehaviors.Add(new ChainBehavior(_BlockChain));

            AddressManagerBehavior.GetAddrman(_NodeConnectionParameters).PeersToFind = networkInfo.PeersToFind;

            if (ipAddress != null)
            {
                _NodeConnectionParameters.TemplateBehaviors.Find <AddressManagerBehavior>().Mode = AddressManagerBehaviorMode.AdvertizeDiscover;                //parameters.Advertize = true;
                _NodeConnectionParameters.AddressFrom = new System.Net.IPEndPoint(ipAddress, networkInfo.DefaultPort);
            }
            else
            {
                _NodeConnectionParameters.TemplateBehaviors.Find <AddressManagerBehavior>().Mode = AddressManagerBehaviorMode.Discover;
            }

            if (ipAddress != null)
            {
                _Server = new Server(ipAddress, networkInfo, _NodeConnectionParameters);
                OwnResource(_Server);

                if (_Server.Start())
                {
                    NodeServerTrace.Information($"Server started at {ipAddress}:{networkInfo.DefaultPort}");
                    StatusMessageProducer.OutboundStatus = OutboundStatusEnum.Accepting;
                }
                else
                {
                    NodeServerTrace.Information($"Could not start server at {ipAddress}:{networkInfo.DefaultPort}");
                }
            }

            if (networkInfo.Seeds.Count == 0)
            {
                NodeServerTrace.Information("No seeds defined");
            }
            else
            {
                _NodesGroup = new NodesGroup(networkInfo, _NodeConnectionParameters);
                OwnResource(_NodesGroup);

                _NodesGroup.AllowSameGroup        = true;          //TODO
                _NodesGroup.MaximumNodeConnection = networkInfo.MaximumNodeConnection;

#if TRACE
                _NodesGroup.ConnectedNodes.Added += (object sender, NodeEventArgs e) =>
                {
                    NodeServerTrace.Information("Peer found: " + e.Node.RemoteSocketAddress + ":" + e.Node.RemoteSocketPort);
                };
#endif

                _NodesGroup.Connect();
            }
        }
示例#5
0
        public NetworkBuilder(NetworkInfo Info)
        {
            //// Get the nessecary data to build the subnetting information

            //int NumberOfHostsNeeded = Info.NumberOfHosts;

            //// The nessecary address class for the number of subnets and the required hosts per subnet
            //AddressClass InetClass = DetermineNessecaryAddressClass(NumberOfHostsNeeded);

            //// Uses 2^x to determine how many bits need to be borrowed
            //int BitsToBorrow = DetermineBitsToBorrow(NumberOfHostsNeeded, InetClass);

            //int BitsForAddressSpace = DetermineBitsForAddressSpace(BitsToBorrow, InetClass);

            //int RequiredSubnets = DetermineNumberOfSubnets(BitsToBorrow, InetClass);

            int BitsToBorrow = DetermineBitsToBorrow(Info.RequiredSubnets);

            AddressClass InetClass = DetermineNessecaryAddressClass(Info.NumberOfHosts);

            int AddressSpaceBits = DetermineBitsForAddressSpace(BitsToBorrow, InetClass);

            int RequiredSubnets = IntPow(2, BitsToBorrow);

            // Gets the subnet mask using data resources basedo n the required bits to borrow
            SubnetMask NetMask       = GetSubnetAddress(InetClass, BitsToBorrow, AddressSpaceBits);
            IpAddress  SampleAddress = new IpAddress(Info.SampleAddress);


            // The program has different functionallity based on different address class
            // For Class C it will be able to build the entire subnet out
            // For the other classes it will display generic subnetting information but not build a subnet
            switch (InetClass)
            {
            case AddressClass.A:
                BuiltNetwork = new FullNetwork();
                // Gather network information for Class A
                //ClassAandBBuilder ClassABuilder = new ClassAandBBuilder();
                //ClassABuilder.NetMask = NetMask;
                //ClassABuilder.HostsPerSubnet = IntPow(2, (AddressSpaceBits - 2) );
                //ClassABuilder.NumberOfSubents = IntPow(2, BitsToBorrow);
                //BuiltNetwork.ClassAorBBuilder = ClassABuilder;
                BuiltNetwork = new FullNetwork();
                BuiltNetwork.BitsBorrowed    = BitsToBorrow;
                BuiltNetwork.Class           = InetClass;
                BuiltNetwork.NetMask         = NetMask;
                BuiltNetwork.NumberOfSubnets = IntPow(2, BitsToBorrow);
                BuiltNetwork.AddressSpace    = IntPow(2, AddressSpaceBits);
                BuiltNetwork.UsableHosts     = IntPow(2, AddressSpaceBits) - 2;
                break;

            case AddressClass.B:
                //BuiltNetwork = new FullNetwork();
                //ClassAandBBuilder ClassBBuilder = new ClassAandBBuilder();
                //ClassBBuilder.NetMask = NetMask;
                //ClassBBuilder.HostsPerSubnet = IntPow(2, AddressSpaceBits);
                //ClassBBuilder.NumberOfSubents = IntPow(2, BitsToBorrow);
                //BuiltNetwork.ClassAorBBuilder = ClassBBuilder;
                BuiltNetwork = new FullNetwork();
                BuiltNetwork.BitsBorrowed    = BitsToBorrow;
                BuiltNetwork.Class           = InetClass;
                BuiltNetwork.NetMask         = NetMask;
                BuiltNetwork.NumberOfSubnets = IntPow(2, BitsToBorrow);
                BuiltNetwork.AddressSpace    = IntPow(2, AddressSpaceBits);
                BuiltNetwork.UsableHosts     = IntPow(2, AddressSpaceBits) - 2;
                break;

            case AddressClass.C:
                // Gather network information for Class C
                int SubnetCount = 0;
                BuiltNetwork = new FullNetwork();
                // Begin the subnet building process
                ClassCSubnetBuilder ClassCBuilder = new ClassCSubnetBuilder(NetMask, SampleAddress);
                // Subtraction by one because it is a based of zero
                while (SubnetCount < RequiredSubnets)
                {
                    SubnetCount += 1;
                    Subnetwork Subnet = ClassCBuilder.NextSubnet();
                    BuiltNetwork.Subnets.Enqueue(Subnet);
                }

                BuiltNetwork.BitsBorrowed    = BitsToBorrow;
                BuiltNetwork.Class           = InetClass;
                BuiltNetwork.NetMask         = NetMask;
                BuiltNetwork.NumberOfSubnets = IntPow(2, BitsToBorrow);
                BuiltNetwork.AddressSpace    = (IntPow(2, AddressSpaceBits) * BuiltNetwork.NumberOfSubnets);
                BuiltNetwork.UsableHosts     = IntPow(2, AddressSpaceBits) - 2;

                break;

            default:
                break;
            }
        }
        static void Main(string[] args)
        {
            FullNetwork Network = new FullNetwork();

            NetworkInfo Info;
            NetworkBuilder Builder;

            Info = new NetworkInfo();
            Info.NumberOfHosts = 2046;
            Info.RequiredSubnets = 32;
            Info.SampleAddress = "172.16.2.5";
            Builder = new NetworkBuilder(Info);

            SubnetDataWriter Writer = new SubnetDataWriter();
            Writer.WriteOutExcelDocument(Builder.BuiltNetwork, "Test");
            //// Deprecated
            //Info.Class = AddressClass.B; // Deprecated
            //Info.NumberOfHosts = 14;
            //Info.RequiredSubnets = 2;
            //Info.SampleAddress = "192.168.5.23";
            //Builder = new NetworkBuilder(Info);

            //SubnetDataWriter Datawriter = new SubnetDataWriter();
            //Datawriter.WriteSubnetData(Builder.BuiltNetwork);
            //Datawriter.WriteOutExcelDocument(Builder.BuiltNetwork, "14-2");

            //Info = new NetworkInfo();
            //// Deprecated
            //Info.Class = AddressClass.B; // Deprecated
            //Info.NumberOfHosts = 14;
            //Info.RequiredSubnets = 16;
            //Info.SampleAddress = "192.168.5.23";
            //Builder = new NetworkBuilder(Info);

            //Datawriter.WriteOutExcelDocument(Builder.BuiltNetwork, "14-16");

            //Info = new NetworkInfo();
            //Info.NumberOfHosts = 126;
            //Info.RequiredSubnets = 2;
            //Info.SampleAddress = "192.168.5.23";
            //Builder = new NetworkBuilder(Info);

            //Datawriter.WriteOutExcelDocument(Builder.BuiltNetwork, "126-2");

            //Info = new NetworkInfo();
            //Info.NumberOfHosts = 62;
            //Info.RequiredSubnets = 4;
            //Info.SampleAddress = "192.168.5.23";
            //Builder = new NetworkBuilder(Info);

            //Datawriter.WriteOutExcelDocument(Builder.BuiltNetwork, "62-2");

            //Info = new NetworkInfo();
            //Info.NumberOfHosts = 30;
            //Info.RequiredSubnets = 8;
            //Info.SampleAddress = "192.168.5.23";
            //Builder = new NetworkBuilder(Info);

            //Datawriter.WriteOutExcelDocument(Builder.BuiltNetwork, "30-8");

            //Info = new NetworkInfo();
            //Info.NumberOfHosts = 6;
            //Info.RequiredSubnets = 32;
            //Info.SampleAddress = "192.168.5.23";
            //Builder = new NetworkBuilder(Info);

            //Datawriter.WriteOutExcelDocument(Builder.BuiltNetwork, "6-32");

            //Info = new NetworkInfo();
            //Info.NumberOfHosts = 2;
            //Info.RequiredSubnets = 64;
            //Info.SampleAddress = "192.168.5.23";
            //Builder = new NetworkBuilder(Info);

            //Datawriter.WriteOutExcelDocument(Builder.BuiltNetwork, "2-64");

            //Info = new NetworkInfo();
            //Info.NumberOfHosts = 15;
            //Info.RequiredSubnets = 12;
            //Info.SampleAddress = "192.168.3.2";
            //Builder = new NetworkBuilder(Info);

            //Datawriter.WriteOutExcelDocument(Builder.BuiltNetwork, "Test");
        }