Beispiel #1
0
        internal void UninstallDhcpOptions()
        {
            // Back-out options
            IPModule ip = (IPModule !)Core.Instance().GetProtocolByName("IP");

            DhcpIPv4Option address = (DhcpIPv4Option !)
                                     activeDhcpOptions[DhcpRequestedIPAddress.OptionCode];

            IPv4 ifAddress = address.Value;

            //
            // Remove routes associated with interface address
            //
            ip.HostConfiguration.RoutingTable.DeleteInterfaceRoutes(ifAddress);

            //
            // Remove name server
            //
            DhcpMultiIPv4Option dnsServers =
                activeDhcpOptions[DhcpDomainNameServer.OptionCode]
                as DhcpMultiIPv4Option;

            if (dnsServers != null)
            {
                foreach (IPv4 server in dnsServers.Values)
                {
                    ip.HostConfiguration.AddNameServer(server);
                }
            }

            //
            // Leave domain name in place
            //

            // If we wanted to remove it...
            DhcpStringOption domain =
                activeDhcpOptions[DhcpDomainName.OptionCode]
                as DhcpStringOption;

            string domainName = ip.HostConfiguration.GetDomainName();

            if (domain != null && domainName == domain.Value)
            {
                ip.HostConfiguration.SetDomainName("");
            }

            //
            // Remove interface address bindings
            //
            ip.HostConfiguration.Bindings.Flush(adapter, ifAddress);

            activeDhcpOptions = null;
        }
Beispiel #2
0
        // called by the runtime when
        // the protocol should be started
        public bool StartModule()
        {
            DebugPrint("Starting ARP.\n");

            // we have the request...
            // Notice: can be ARP_REQUEST or ARP_RESPONSE
            ipModule = Core.Instance().GetProtocolByName("IP") as IPModule;
            if (ipModule == null)
            {
                return(false);
            }

            // ask for an aging[msec] timer
            DateTime then = DateTime.UtcNow + ArpTable.AgePeriod;

            Core.Instance().TheDispatcher.AddCallback(
                new Dispatcher.Callback(OnAgingTimer), null, (ulong)then.Ticks
                );
            return(true);
        }
        // loads an adapter's driver and add the relevant data structures
        // for managing it.
        protected bool LoadAdapter(ProtocolParams args, string driversPath)
        {
            // the user can ignore an adapter
            // so ignore it...
            string ignore = args["ignore"];

            if (ignore != null && ignore == "true")
            {
                return(true);
            }

            string name =
                ProtocolParams.LookupString(args, "name", "unknown");
            string typeName =
                ProtocolParams.LookupString(args, "type", "unknown");
            string id =
                ProtocolParams.LookupString(args, "id", "unknown");

            int mtu = ProtocolParams.LookupInt32(args, "mtu",
                                                 EthernetFormat.MaxFrameSize);
            int txRing  = ProtocolParams.LookupInt32(args, "txRing", 64);
            int rxRing  = ProtocolParams.LookupInt32(args, "rxRing", 64);
            int fwQueue = ProtocolParams.LookupInt32(args, "fwQueue", 64);

            IAdapterFactory factory = null;
            IAdapter        adapter = null;

            try {
                string   factoryTypeName = typeName + "Factory";
                Assembly assembly        = Assembly.LoadFrom(driversPath);
                Type[]   types           = assembly.GetTypes();
                foreach (Type t in types)
                {
                    if (t.IsClass && t.Name.Equals(typeName))
                    {
                        factory = (IAdapterFactory)Activator.CreateInstance(t, null);
                        break;
                    }
                    if (factory == null)
                    {
                        throw new Exception("Can't find Adapter's Factory.");
                    }

                    adapter = factory.CreateAdapter(name, id, txRing, rxRing);
                    Core.Instance().RegisterAdapter(adapter, fwQueue);
                }
            }
            catch (Exception e) {
                adapter = null;
                Console.Out.WriteLine(e.Message);
                Environment.Exit(1);
            }

            IPModule          ipModule   = Core.Instance().GetProtocolByName("IP") as IPModule;
            HostConfiguration hostConfig = ipModule.HostConfiguration;

            for (int i = 0;; i++)
            {
                string ipTag      = String.Format("ip{0}", i);
                string maskTag    = String.Format("mask{0}", i);
                string gatewayTag = String.Format("gateway{0}", i);

                // XXX No point-to-point support here.
                IPv4 address = ProtocolParams.LookupIPv4(args, ipTag,
                                                         IPv4.AllOnes);
                IPv4 netmask = ProtocolParams.LookupIPv4(args, maskTag,
                                                         IPv4.AllOnes);
                IPv4 gateway = ProtocolParams.LookupIPv4(args, gatewayTag,
                                                         IPv4.Zero);
                if (address == IPv4.AllOnes || netmask == IPv4.AllOnes)
                {
                    break;
                }
                hostConfig.Bindings.Add(adapter,
                                        new InterfaceIPConfiguration(address, netmask, gateway, 128)
                                        );
            }

#if DEBUG
            System.Console.Out.WriteLine("[Interface {0}]", args["name"]);
            System.Console.Out.WriteLine("-----------------------------");
#endif
            return(true);
        }
Beispiel #4
0
        internal bool InstallDhcpOptions(SortedList !dhcpOptions)
        {
            if (activeDhcpOptions != null)
            {
                UninstallDhcpOptions();
            }

            IPModule ip = (IPModule)Core.Instance().GetProtocolByName("IP");

            if (ip == null)
            {
                DebugPrint("Failed to get IP Module\n");
                return(false);
            }

            //
            // Add interface address binding
            //
            DhcpIPv4Option address =
                dhcpOptions[DhcpRequestedIPAddress.OptionCode]
                as DhcpIPv4Option;

            DhcpIPv4Option netmask =
                dhcpOptions[DhcpSubnetMask.OptionCode] as DhcpIPv4Option;

            DhcpMultiIPv4Option routers =
                dhcpOptions[DhcpRouter.OptionCode] as DhcpMultiIPv4Option;

            if (address == null || netmask == null || routers == null ||
                routers.Values.Length == 0)
            {
                return(false);
            }

            ip.HostConfiguration.Bindings.Add(
                adapter, new InterfaceIPConfiguration(address.Value,
                                                      netmask.Value,
                                                      routers.Values[0])
                );

            //
            // Register Domain name
            //
            DhcpStringOption domain =
                dhcpOptions[DhcpDomainName.OptionCode] as DhcpStringOption;

            //            string domainName = ip.HostConfiguration.GetDomainName();
            // never used
            if (domain != null)
            {
                ip.HostConfiguration.SetDomainName(domain.Value);
            }

            //
            // Add DNS servers
            //
            DhcpMultiIPv4Option dnsServers =
                dhcpOptions[DhcpDomainNameServer.OptionCode]
                as DhcpMultiIPv4Option;

            if (dnsServers != null)
            {
                foreach (IPv4 server in dnsServers.Values)
                {
                    ip.HostConfiguration.AddNameServer(server);
                }
            }

            //
            // Install static routes
            //
            DhcpMultiIPv4Option staticRoutes =
                dhcpOptions[DhcpStaticRoutes.OptionCode]
                as DhcpMultiIPv4Option;

            if (staticRoutes != null)
            {
                int routeCount = staticRoutes.Values.Length & ~1; // pairs
                for (int i = 0; i < routeCount; i += 2)
                {
                    IPv4 destination = staticRoutes.Values[i];
                    IPv4 gateway     = staticRoutes.Values[i + 1];
                    IPv4 ifAddress   = address.Value;
                    ip.HostConfiguration.RoutingTable.AddRoute(
                        new RouteEntry(destination, gateway, ifAddress,
                                       RouteEntry.DefaultRouteMetric, 0)
                        );
                }
            }

            activeDhcpOptions = dhcpOptions;
            return(true);
        }