Ejemplo n.º 1
0
        /// <summary>
        /// Read a named unsigned integer parameter from a
        /// <c>ProtocolParams</c> instance.
        /// </summary>
        /// <returns><paramref name="defaultValue"/> if <paramref name="parameters"/>
        /// is <c>null</c> or <paramref name="parameterName"/> cannot be found.
        /// Otherwise it returns the named parameter as an unsigned
        /// integer.</returns>
        public static uint LookupUInt32(ProtocolParams parameters,
                                        string parameterName,
                                        uint defaultValue)
        {
            if (parameters == null)
            {
                return(defaultValue);
            }
            else if (parameterName == null)
            {
                throw new ArgumentNullException();
            }

            string sValue = parameters[parameterName];

            if (sValue == null)
            {
                return(defaultValue);
            }

            try {
                return(UInt32.Parse(sValue));
            }
            catch {
                Core.Log("Failed on parameter \"{0}\" value \"{1}\"\n",
                         parameterName, sValue);
                return(defaultValue);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Read a named boolean parameter from a <c>ProtocolParams</c>
        /// instance.
        /// </summary>
        /// <returns><paramref name="defaultValue"/> if <paramref name="parameters"/>
        /// is <c>null</c> or <paramref name="parameterName"/> cannot be found.
        /// Otherwise it returns the named parameter as a boolean.</returns>
        public static bool LookupBoolean(ProtocolParams parameters,
                                         string parameterName,
                                         bool defaultValue)
        {
            if (parameters == null)
            {
                return(defaultValue);
            }
            else if (parameterName == null)
            {
                throw new ArgumentNullException();
            }

            string sValue = parameters[parameterName];

            if (sValue == null)
            {
                return(defaultValue);
            }

            try {
                return(Boolean.Parse(sValue));
            }
            catch (FormatException) {
                Core.Log("Failed on parameter \"{0}\" value \"{1}\"\n",
                         parameterName, sValue);
                return(defaultValue);
            }
        }
Ejemplo n.º 3
0
        // IProtocol interfaces
        // ------------------------
        public bool Initialize(ProtocolParams parameters)
        {
            Debug.Assert(parameters == null ||
                         parameters["name"] == moduleName);

            hostConfig = new HostConfiguration();
            version    = ProtocolParams.LookupInt32(parameters, "version", 4);

            bool fragment = ProtocolParams.LookupBoolean(parameters,
                                                         "fragment",
                                                         false);

            // save the routing protocol name, if exists
            if (version != 4 || fragment == true)
            {
                DebugPrint("Support only exists for V4 w/o fragments.\n");
                return(false);
            }

            Core core = Core.Instance();

            core.RegisterProtocol(this);
            if (!core.packetTypes.RegisterTypeHandler(PacketTypes.IP, this))
            {
                core.DeregisterProtocol(this);
                return(false);
            }
            return(true);
        }
Ejemplo n.º 4
0
 // IProtocol interfaces
 // ------------------------
 public bool Initialize(ProtocolParams parameters)
 {
     Debug.Assert(parameters == null || parameters["name"] == "TCP");
     Core.Instance().RegisterProtocol(this);
     TcpSessionPool.SetTcpModule(this);
     return(true);
 }
Ejemplo n.º 5
0
        // IProtocol interfaces
        // ------------------------
        public bool Initialize(ProtocolParams parameters)
        {
            Debug.Assert(parameters == null || parameters["name"] == "ICMP");

            Core.Instance().RegisterProtocol(this);
            // no need to register at the packet level!
            return(true);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Read a named string parameter from a <c>ProtocolParams</c>
        /// instance.
        /// </summary>
        /// <returns><paramref name="defaultValue"/> if <paramref name="parameters"/>
        /// is <c>null</c> or <paramref name="parameterName"/> cannot be found.
        /// Otherwise it returns the named parameter as a string.</returns>
        public static string LookupString(ProtocolParams parameters,
                                          string parameterName,
                                          string defaultValue)
        {
            if (parameters == null)
            {
                return(defaultValue);
            }
            else if (parameterName == null)
            {
                throw new ArgumentNullException();
            }

            string sValue = parameters[parameterName];

            if (sValue == null)
            {
                return(defaultValue);
            }
            return(sValue);
        }
Ejemplo n.º 7
0
        // This method dynamically loads a protocol and
        // initializes it.
        protected bool LoadProtocolModule(ProtocolParams args)
        {
            // the user can mark a protocol ignore field so
            // we just ignore it...
            string ignore = args["ignore"];

            if (ignore != null && ignore == "true")
            {
                return(true);
            }
#if DEBUG
            System.Console.Out.WriteLine("[Protocol {0}]", args["name"]);
            foreach (string s in args.Keys)
            {
                System.Console.Out.WriteLine("{0}={1}", s, args[s]);
            }
            System.Console.Out.WriteLine("-----------------------------");
#endif
            // dynamic load the protocol
            // by default it is in the Runtime assembly
            string protocolType = args["type"];

            try {
                IProtocol protocol = (IProtocol)Activator.CreateInstance(Type.GetType(protocolType));

                if (protocol == null)
                {
                    throw new Exception("Can't find Protocol's Class.");
                }

                // initialize the protocol
                protocol.Initialize(args);
            }
            catch (Exception e) {
                Console.Out.WriteLine(String.Format("Can't load protocol class: {0}", protocolType));
                Console.Out.WriteLine(e.Message);
                return(false);
            }
            return(true);
        }
Ejemplo n.º 8
0
        // IProtocol interfaces
        // ------------------------
        public bool Initialize(ProtocolParams parameters)
        {
            Debug.Assert(parameters == null || parameters["name"] == "ARP");

            int size = ProtocolParams.LookupInt32(parameters, "cacheSize",
                                                  128);
            int age = ProtocolParams.LookupInt32(parameters, "max-age",
                                                 ArpTable.MaxAge);

            arpTable        = new ArpTable(size, age, this);
            pendingRequests = new ArrayList();

            Core core = Core.Instance();

            core.RegisterProtocol(this);
            if (!core.packetTypes.RegisterTypeHandler(PacketTypes.ARP, this))
            {
                core.DeregisterProtocol(this);
                return(false);
            }
            return(true);
        }
Ejemplo n.º 9
0
        // 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);
        }
Ejemplo n.º 10
0
        //handle host config
        protected void HandleConfiguration()
        {
            Core core = Core.Instance();

            // the path of the driver assembly
            ConfigElement config = ParseConfiguration(false);

            // we start with the configuration tag...
            // we assume that the XML is valid!

            // if we have children, and we have...
            if (config.ChildElements.Count > 0)
            {
                foreach (ConfigElement element in config.ChildElements)
                {
                    switch (element.TagName)
                    {
                    case "creator":
                        break;

                    case "date":
                        break;

                    case "hostname":
                        break;

                    case "forwarding":
                        break;

                    case "OutQHandlerTimeout":
                        core.SetOutQueueHandlerInterval(Convert.ToInt32(element.Text, 10));
                        break;

                    case "protocol-path":
                        protocolsPath = element.Text;
                        break;

                    case "drivers-path":
                        driversPath = element.Text;
                        break;

                    case "backlog-size":
                        int freeListSize = Convert.ToInt32(element.Text, 10);
                        core.ProvisionDemux(
                            freeListSize,
                            EthernetFormat.MaxFrameSize
                            );
                        break;

                    case "protocols":
                        // handle the protocols
                        // use a generic scheme and apply
                        // it to the adapters in the future ;-)

                        // the args include the name and type!
                        ProtocolParams args = new ProtocolParams();;
                        foreach (ConfigElement prot in element.ChildElements)
                        {
                            args.Clear();
                            foreach (string attr in prot.Attributes.Keys)
                            {
                                args.Add(attr, prot.Attribute(attr));
                            }
                            foreach (ConfigElement protInfo in prot.ChildElements)
                            {
                                args.Add(protInfo.TagName, protInfo.Text);
                            }
                            // done with one...
                            if (!LoadProtocolModule(args))
                            {
                                Core.Panic("Error while loading Protocol");
                            }
                        }
                        break;

                    case "adapters":
                        // handle the adapters
                        ProtocolParams adArgs = new ProtocolParams();;
                        foreach (ConfigElement ad in element.ChildElements)
                        {
                            adArgs.Clear();
                            foreach (string attr in ad.Attributes.Keys)
                            {
                                adArgs.Add(attr, ad.Attribute(attr));
                            }
                            foreach (ConfigElement adInfo in ad.ChildElements)
                            {
                                adArgs.Add(adInfo.TagName, adInfo.Text);
                            }
                            // done with one...
                            if (!LoadAdapter(adArgs, driversPath))
                            {
                                Core.Panic("Error while loading an Adapter");
                            }
                        }
                        break;
                    }
                }
            }
        }
Ejemplo n.º 11
0
 public bool Initialize(ProtocolParams parameters)
 {
     Core.Instance().RegisterProtocol(this);
     return(true);
 }