コード例 #1
0
ファイル: Route.cs プロジェクト: muqiuq/BeASwitch
 public Route(Subnet subnet, RouterPort routerPort, IPv4Address gateway = null, IPv4Address src = null)
 {
     this.gateway    = gateway;
     this.Subnet     = subnet;
     this.RouterPort = routerPort;
     this.src        = src;
     if (src != null && gateway != null)
     {
         throw new ArgumentException("Can't have source and gateway simultaneously");
     }
 }
コード例 #2
0
ファイル: GameEngine.cs プロジェクト: muqiuq/BeASwitch
        public GameEngine(Dispatcher dispatcher, int numberOfInterfaces = NUMBER_OF_INTERFACES)
        {
            var randomAddresses = GenerateRandomInterfaceAddresses(numberOfInterfaces);

            for (int a = 0; a < numberOfInterfaces; a++)
            {
                var rp = new RouterPort(a);
                rp.AddIPv4Address(randomAddresses[a]);
                Ports.Add(a, rp);
            }

            RoutingTable            = GenerateRandomRoutingTableAndAddConnectedRoutes(NUMBER_OF_ROUTES, Ports);
            this.dispatcher         = dispatcher;
            this.numberOfInterfaces = numberOfInterfaces;
            this.ownMAC             = Helper.GenerateRandomMAC();

            foreach (var route in RoutingTable)
            {
                if (route.IsDefaultRoute())
                {
                    continue;
                }
                var hostAddr       = route.Subnet.GetHostMin().GetAddress();
                var skip           = 1;
                var upperRandValue = 30 - route.Subnet.Mask;
                var lowerRandValue = 1;
                if (route.Subnet.Mask < 24)
                {
                    lowerRandValue = 4 + (24 - route.Subnet.Mask);
                }
                Random r = new Random();
                while (hostAddr.CompareTo(route.Subnet.GetBroadcast()) > 0)
                {
                    listOfAddrInNearbySubnetsAndAFewRandomPublicIPs.Add(hostAddr);
                    hostAddr = hostAddr.IncrementBy(skip);
                    skip    += r.Next(lowerRandValue, upperRandValue);
                }
            }

            var numberOfPublicIPAddressToAdd = listOfAddrInNearbySubnetsAndAFewRandomPublicIPs.Count / 10 + 10;

            for (int a = 0; a < numberOfPublicIPAddressToAdd; a++)
            {
                var randAddr = GetRandomPublicIPv4Subnet().GetAddress();
                if (!listOfAddrInNearbySubnetsAndAFewRandomPublicIPs.Contains(randAddr))
                {
                    listOfAddrInNearbySubnetsAndAFewRandomPublicIPs.Add(randAddr);
                }
            }

            Random rand          = new Random();
            var    numberOfMixes = listOfAddrInNearbySubnetsAndAFewRandomPublicIPs.Count * 30;

            for (int a = 0; a < numberOfMixes; a++)
            {
                var index = rand.Next(0, listOfAddrInNearbySubnetsAndAFewRandomPublicIPs.Count - 1);
                var value = listOfAddrInNearbySubnetsAndAFewRandomPublicIPs[index];
                listOfAddrInNearbySubnetsAndAFewRandomPublicIPs.RemoveAt(index);
                listOfAddrInNearbySubnetsAndAFewRandomPublicIPs.Add(value);
            }
        }