public void TestRoute(HttpContext context)
        {
            string      payload = context.Request.Payload.ReadAll();
            PathWrapper path    = Deserialize <PathWrapper>(payload);

            if (path == null)
            {
                context.Response.Status = HttpStatus.BadRequest;
                return;
            }

            RoutingEntry          re      = Service.RoutingEngine.GetEntry(path.Method + path.Path);
            RoutingEntriesWrapper entries = new RoutingEntriesWrapper();

            if (re != null)
            {
                RoutingEntryWrapper entry = new RoutingEntryWrapper();
                entry.Nested = re;
                entries.RoutingEntries.Add(entry);
            }

            string json = Serialize(entries);

            if (json != null)
            {
                context.Response.Payload.Write(json);
                context.Response.Status = HttpStatus.OK;
            }
            else
            {
                context.Response.Status = HttpStatus.InternalServerError;
            }
        }
예제 #2
0
        private IList <string> GetScopesOfPath(string path)
        {
            IList <string> scopes = new List <string>();

            foreach (DynamicAuthorizationScope das in _oauth2.DynamicScopes)
            {
                if (das.Path.CompareTo(path) == 0)
                {
                    scopes.Add(das.Scope);
                }
            }

            if (scopes.Count == 0)
            {
                RoutingEntry re = _service.RoutingEngine.GetEntry(path);
                if (re != null)
                {
                    foreach (Attribute a in System.Attribute.GetCustomAttributes(re.MethodInfo))
                    {
                        if (a is AuthorizationScope)
                        {
                            scopes.Add(((AuthorizationScope)a).Scope);
                        }
                    }
                }
            }
            return(scopes);
        }
예제 #3
0
        private void AddNewRoutingObject(String descriptor)
        {
            // add new routing table
            Dictionary <string, RoutingEntry> newRoutingTable = new Dictionary <string, RoutingEntry>();

            // add existing targets to this one
            foreach (string key in m_routingMap.Keys)
            {
                newRoutingTable[key] = new RoutingEntry()
                {
                    Distance = 99999999
                }
            }
            ;

            // add this new target to the existing routing tables
            foreach (Dictionary <string, RoutingEntry> table in m_routingMap.Values)
            {
                table[descriptor] = new RoutingEntry()
                {
                    Distance = 99999999
                }
            }
            ;

            m_routingMap[descriptor] = newRoutingTable;
        }
예제 #4
0
        private void RemoveEntry(IPAddress ipaNextHop, IPAddress ipaDestinatoin, Subnetmask smMask, int iMetric)
        {
            RoutingEntry reFound = null;

            foreach (RoutingEntry re in RoutingEntries)
            {
                if (re.NextHop.Equals(ipaNextHop) && re.Destination.Equals(ipaDestinatoin) && re.Subnetmask.Equals(smMask))
                {
                    reFound = re;
                }
            }
            if (reFound != null)
            {
                RemoveEntry(reFound);
            }
        }
예제 #5
0
        protected override void Route(HttpContext task)
        {
            //apply manipulation first
            _manipulators.Manipulate(task);

            //prepare route
            string route = "";

            if (task.Request.Version == HttpVersion.HTTP_2_0)
            {
                route = HttpInternalController.HTTP_VERSION;
            }
            else if (!task.Request.Headers.Has("Host"))
            {
                route = HttpInternalController.BAD_REQUEST;
            }
            else if (task.Request.Method == HttpMethod.TRACE && (_reference.ServiceConfiguration.AllowTracing == TracingMode.DISABLED || _reference.ServiceConfiguration.AllowTracing == TracingMode.AUTO))
            {
                route = HttpInternalController.TRACE;
            }
            else if (task.Request.Method == HttpMethod.OPTIONS && (_reference.ServiceConfiguration.AllowOptions == OptionsMode.DISABLED || _reference.ServiceConfiguration.AllowOptions == OptionsMode.AUTO))
            {
                route = HttpInternalController.OPTIONS;
            }
            else if (task.Request.Method == HttpMethod.HEAD)
            {
                route = @"GET" + task.Request.Path;
            }
            else
            {
                route = task.Request.Method.ToString() + task.Request.Path;
            }

            //determining routing target
            RoutingEntry entry = _reference.RoutingEngine.GetEntry(route);

            //if no such entry --> EXCEPTION
            if (entry == null)
            {
                entry = _reference.RoutingEngine.GetEntry(HttpInternalController.PATH_NOT_FOUND);
            }

            //pack routed context
            RoutedContext routedContext = new RoutedContext(task, entry);

            Forward(entry.ProcessingGroup, routedContext);
        }
예제 #6
0
        /// <summary>
        /// Updates the routing table with a conversion. If the conversion results
        /// into a shorter path, the updates are propagated.
        /// </summary>
        private void UpdateRoutingTable(Pair <string> conversion)
        {
            Dictionary <string, RoutingEntry> tableToUpdate    = m_routingMap[conversion.E0];
            Dictionary <string, RoutingEntry> tableWithNewInfo = m_routingMap[conversion.E1];

            bool updated = false;

            var targetsToOptimize = (
                from target in tableToUpdate.Keys
                where target != conversion.E1
                select target
                ).ToArray();

            foreach (string target in targetsToOptimize)
            {
                int alternativeDistance = tableWithNewInfo[target].Distance + tableToUpdate[conversion.E1].Distance;

                // found a better way to the target
                if (alternativeDistance < tableToUpdate[target].Distance)
                {
                    tableToUpdate[target] = new RoutingEntry()
                    {
                        Distance = alternativeDistance,
                        Next     = tableToUpdate[conversion.E1].Next
                    };
                    updated = true;
                }
            }

            if (updated)
            {
                foreach (Pair <string> conv in m_actionMap.Keys)
                {
                    if (conv.E1 == conversion.E0)
                    {
                        UpdateRoutingTable(conv);
                    }
                }
            }
        }
예제 #7
0
        /// <summary>
        /// Builds the static routing table and stores the shortest distances.
        /// </summary>
        public void BuildRoutingTables()
        {
            var keys = m_routingMap.Keys.ToList();

            // clear all entries
            foreach (var key1 in keys)
            {
                foreach (var key2 in keys)
                {
                    m_routingMap[key1][key2] = new RoutingEntry()
                    {
                        Distance = 99999999
                    }
                }
            }
            ;

            var validConversions = (from conversion in m_actionMap.Keys
                                    where AreAllResourcesAvailable(conversion.E0) &&
                                    AreAllResourcesAvailable(conversion.E1)
                                    select conversion).ToList();

            // first add all direct routes
            foreach (Pair <string> conversion in validConversions)
            {
                AddDirectRoute(conversion);
            }

            // now go and find the indirect routes
            foreach (Pair <string> conversion in validConversions)
            {
                UpdateRoutingTable(conversion);
            }

            if (!m_initialized)
            {
                m_initialized = true;
            }
        }
예제 #8
0
        /// <summary>
        /// Updates an RIP Entry
        /// </summary>
        /// <param name="ipaNextHop"></param>
        /// <param name="ipaDestinatoin"></param>
        /// <param name="smMask"></param>
        /// <param name="iMetric"></param>
        /// <returns>Bool indicating if something changed</returns>
        private bool UpdateEntry(IPAddress ipaNextHop, IPAddress ipaDestinatoin, Subnetmask smMask, int iMetric)
        {
            bool bFound   = false;
            bool bChanged = true;

            foreach (RoutingEntry re in RoutingEntries)
            {
                if (re.Destination.Equals(ipaDestinatoin) && re.Subnetmask.Equals(smMask))
                {
                    if (iMetric != re.Metric || !re.NextHop.Equals(ipaNextHop))
                    {
                        bChanged = true;
                        if (iMetric < 16)
                        {
                            re.Metric = iMetric;
                        }
                        else
                        {
                            re.Metric = 65535;
                            Holddown(ipaDestinatoin);
                        }
                        re.NextHop = ipaNextHop;
                        InvokeEntryUpdated(re);
                    }
                    bFound = true;
                }
            }

            if (!bFound)
            {
                bChanged = true;
                RoutingEntry re = new RoutingEntry(ipaDestinatoin, ipaNextHop, iMetric, smMask, RoutingEntryOwner.RIP);
                AddRoutingEntry(re);
            }

            return(bChanged);
        }
예제 #9
0
        static void Main(string[] args)
        {
            //Query all interfaces
            WinPcapInterface[] arWpc = EthernetInterface.GetAllPcapInterfaces();

            //Create handler classes
            Router          rRouter    = new Router();
            TrafficSplitter tsSplitter = new TrafficSplitter();

            //Start handlers
            rRouter.Start();
            tsSplitter.Start();

            //Let the router forward traffic from the interfaces to the traffic splitter
            rRouter.OutputHandler = tsSplitter;
            //Let the traffic splitter forward received traffic back to the router
            tsSplitter.OutputHandler = rRouter;

            //Create the properties of the routing entry
            IPAddress  ipaDestination = IPAddress.Parse("0.0.0.0");
            IPAddress  ipaGateway     = IPAddress.Parse("192.168.0.1");
            Subnetmask smMask         = Subnetmask.Parse("0.0.0.0");
            int        iMetric        = 10;

            //Create the routing entry
            RoutingEntry rEntry = new RoutingEntry(ipaDestination, ipaGateway, iMetric, smMask, RoutingEntryOwner.UserStatic);

            //Add some event handlers
            rRouter.FrameDropped    += new EventHandler(rRouter_FrameDropped);
            rRouter.FrameForwarded  += new EventHandler(rRouter_FrameForwarded);
            rRouter.FrameReceived   += new EventHandler(rRouter_FrameReceived);
            rRouter.ExceptionThrown += new TrafficHandler.ExceptionEventHandler(rRouter_ExceptionThrown);

            //Create a list for the interfaces
            List <EthernetInterface> wpcInterfaces = new List <EthernetInterface>();

            //Foreach WinPcapInterface of this host
            foreach (WinPcapInterface wpc in arWpc)
            {
                //Create a new interface handler and start it
                EthernetInterface ipInterface = new EthernetInterface(wpc);
                ipInterface.Start();

                //Then add it to the router and to our list
                wpcInterfaces.Add(ipInterface);
                rRouter.AddInterface(ipInterface);
            }

            Console.WriteLine("Loading complete...");

            //Run until 'x' is pressed
            while (Console.ReadKey().Key != ConsoleKey.X)
            {
                ;
            }

            //Start the cleanup process for all handlers
            rRouter.Cleanup();
            tsSplitter.Cleanup();

            //Start the cleanup process for all interfaces
            foreach (EthernetInterface ipInterface in wpcInterfaces)
            {
                ipInterface.Cleanup();
            }

            //Stop all handlers
            rRouter.Stop();
            tsSplitter.Stop();

            //Stop all interfaces
            foreach (EthernetInterface ipInterface in wpcInterfaces)
            {
                ipInterface.Stop();
            }
        }
예제 #10
0
        public void ProcessHttpRequest(RoutedContext context, HttpManipulatorCollection <RoutedContext> internalPreManipulators, HttpManipulatorCollection <RoutedContext> preManipulators, HttpManipulatorCollection <RoutedContext> internalPostManipulators, HttpManipulatorCollection <RoutedContext> postManipulators)
        {
            try
            {
                //execute pre manipulators first
                internalPreManipulators.Manipulate(context);
                preManipulators.Manipulate(context);

                //extract routing information
                RoutingEntry   entry      = context.RoutingEntry;
                MethodInfo     method     = entry.MethodInfo;
                HttpController controller = entry.HttpController;

                //prepare parameter list
                IList <string> variables = ExtractPathVariables(entry.Path, context.Context.Request.Path);

                object[] parameters = new object[1 + variables.Count]; //prepare parameter list for invocation
                parameters[0] = context.Context;                       //the first parameter is always the context object
                                                                       //followed by the extracted URL variables:
                for (int i = 0; i < variables.Count; i++)
                {
                    parameters[i + 1] = variables[i];
                }

                //invoke method
                method.Invoke(controller, parameters);

                //post processing
                internalPostManipulators.Manipulate(context);
                postManipulators.Manipulate(context);
            }
            catch (HttpRequestException hre) //this might never be the case, as HttpRequestException is always an inner exeception
            {
                if (!String.IsNullOrWhiteSpace(hre.ErrorMessage))
                {
                    context.Context.Response.Payload.Write(hre.ErrorMessage);
                    if (!String.IsNullOrWhiteSpace(hre.ContentType))
                    {
                        context.Context.Response.Headers.Set("Content-Type", hre.ContentType);
                    }
                    else
                    {
                        context.Context.Response.Headers.Set("Content-Type", MimeType.TEXT_PLAN);
                    }
                }
                context.Context.Response.Status = hre.Status;
            }
            catch (Exception e)
            {
                if (e.InnerException is HttpRequestException)
                {
                    HttpRequestException hre = (HttpRequestException)e.InnerException;
                    if (!String.IsNullOrWhiteSpace(hre.ErrorMessage))
                    {
                        context.Context.Response.Payload.Write(hre.ErrorMessage);
                        if (!String.IsNullOrWhiteSpace(hre.ContentType))
                        {
                            context.Context.Response.Headers.Set("Content-Type", hre.ContentType);
                        }
                        else
                        {
                            context.Context.Response.Headers.Set("Content-Type", MimeType.TEXT_PLAN);
                        }
                    }
                    context.Context.Response.Status = hre.Status;
                }
                else
                {
                    if (e.InnerException != null && !String.IsNullOrEmpty(e.InnerException.Message))
                    {
                        context.Context.Response.Payload.Write(e.InnerException.Message);
                    }
                    else
                    {
                        context.Context.Response.Payload.Write(e.Message);
                    }

                    context.Context.Response.Headers.Set("Content-Type", MimeType.TEXT_PLAN);
                    context.Context.Response.Status = HttpStatus.InternalServerError;
                }
            }


            /*
             * //invoke method:
             * HttpContext httpContext = (HttpContext)
             *
             * /*
             * if (httpContext == null)
             * {
             *  httpContext = context.Context;
             *  httpContext.Response.Status = HttpStatus.InternalServerError;
             *  //TODO: set Content with error
             * }
             *
             * return httpContext;
             */
        }
예제 #11
0
 /// <summary>
 /// Constructs a RoutedContext consiting of a <see cref="HttpContext"/> and the corresponding <see cref="RoutingEntry"/> providing details for processing the request.
 /// </summary>
 /// <param name="context"></param>
 /// <param name="routingEntry"></param>
 public RoutedContext(HttpContext context, RoutingEntry routingEntry)
 {
     _context      = context;
     _routingEntry = routingEntry;
 }
예제 #12
0
        static void Main(string[] args)
        {
            //Query all interfaces
            WinPcapInterface[] arWpc = EthernetInterface.GetAllPcapInterfaces();

            //Create handler classes
            Router          rRouter     = new Router();
            TrafficSplitter tsSplitter  = new TrafficSplitter();
            LibCapDumper    lcpDumper   = new LibCapDumper();
            NetMap          nmMap       = new NetMap();
            WANEmulator     wanEmulator = new WANEmulator();

            //Start handlers
            rRouter.Start();
            tsSplitter.Start();
            lcpDumper.Start();
            nmMap.Start();
            wanEmulator.Start();

            //Let the router forward traffic from the interfaces to the traffic splitter
            rRouter.OutputHandler = tsSplitter;
            //Let the traffic splitter forward received traffic to the WAN emulator
            tsSplitter.OutputHandler = wanEmulator;
            //Let the WAN emulator forward received traffic back to the router
            wanEmulator.OutputHandler = rRouter;
            //Let the traffic splitter clone each frame and send it to the traffic dumper and the NetMap
            tsSplitter.AddTrafficAnalyzer(nmMap);
            tsSplitter.AddTrafficAnalyzer(lcpDumper);


            //Create the properties of the routing entry
            IPAddress  ipaDestination = IPAddress.Parse("0.0.0.0");
            IPAddress  ipaGateway     = IPAddress.Parse("192.168.0.1");
            Subnetmask smMask         = Subnetmask.Parse("0.0.0.0");
            int        iMetric        = 10;

            //Create the routing entry
            RoutingEntry rEntry = new RoutingEntry(ipaDestination, ipaGateway, iMetric, smMask, RoutingEntryOwner.UserStatic);

            //Set traffic dumper properties
            lcpDumper.StartLogging(Path.Combine(System.Environment.CurrentDirectory, "Dump " + DateTime.Now.ToLongDateString()), false);

            //Add some event handlers
            rRouter.FrameDropped         += new EventHandler(rRouter_FrameDropped);
            rRouter.FrameForwarded       += new EventHandler(rRouter_FrameForwarded);
            rRouter.FrameReceived        += new EventHandler(rRouter_FrameReceived);
            rRouter.ExceptionThrown      += new TrafficHandler.ExceptionEventHandler(rRouter_ExceptionThrown);
            nmMap.HostInformationChanged += new NetMap.HostChangedEventHandler(nmMap_HostInformationChanged);

            //Create a list for the interfaces
            List <EthernetInterface> wpcInterfaces = new List <EthernetInterface>();

            //Foreach WinPcapInterface of this host
            foreach (WinPcapInterface wpc in arWpc)
            {
                //Create a new interface handler and start it
                EthernetInterface ipInterface = new EthernetInterface(wpc);
                ipInterface.Start();

                //Then add it to the router and to our list
                wpcInterfaces.Add(ipInterface);
                rRouter.AddInterface(ipInterface);
            }

            Console.WriteLine("Loading complete...");

            //Run until 'x' is pressed
            while (Console.ReadKey().Key != ConsoleKey.X)
            {
                ;
            }

            //Start the cleanup process for all handlers
            rRouter.Cleanup();
            tsSplitter.Cleanup();
            lcpDumper.Cleanup();
            nmMap.Cleanup();
            wanEmulator.Cleanup();

            //Start the cleanup process for all interfaces
            foreach (EthernetInterface ipInterface in wpcInterfaces)
            {
                ipInterface.Cleanup();
            }

            //Stop all handlers
            rRouter.Stop();
            tsSplitter.Stop();
            lcpDumper.Stop();
            nmMap.Stop();
            wanEmulator.Stop();

            //Stop all interfaces
            foreach (EthernetInterface ipInterface in wpcInterfaces)
            {
                ipInterface.Stop();
            }
        }