Ejemplo n.º 1
0
        internal static int AppMain(Parameters !config)
        {
            IPContract.Imp ipConn = ((!)config.ipRef).Acquire();
            if (ipConn == null)
            {
                Console.WriteLine("Could not initialize IP endpoint.");
                return(1);
            }
            ipConn.RecvReady();

            try {
                if (config.name == null)
                {
                    char[] !in ExHeap repHost, repDomain;
                    ipConn.SendGetHostName();
                    ipConn.RecvHostName(out repHost);

                    ipConn.SendGetDomainName();
                    ipConn.RecvDomainName(out repDomain);

                    Console.WriteLine("{0}.{1}", Bitter.ToString(repHost), Bitter.ToString(repDomain));
                    delete repHost;
                    delete repDomain;
                    return(0); // success
                }
                else
                {
                    ipConn.SendSetHostName(Bitter.FromString2(config.name));

                    switch receive {
                    case ipConn.Err():
                        Console.WriteLine("Failure setting host name \"{0}\"", config.name);
                        return(1);    // failure;

                    case ipConn.OK():
                        Console.WriteLine("Success setting host name");
                        break;

                    case ipConn.ChannelClosed():
                        Console.WriteLine("Failure setting host name \"{0}\" (channel closed)", config.name);
                        return(1);    // failure;
                    }
                }
            }
            finally {
                delete ipConn;
            }

            return(0); // success
        }
Ejemplo n.º 2
0
        internal static int DefaultMain(DefaultConfig !config)
        {
            IPContract.Imp ipConn = ((!)config.ipRef).Acquire();
            if (ipConn == null)
            {
                throw new Exception("Unable to acquire handle to the IP network");
            }
            ipConn.RecvReady();


            char[][] !in ExHeap ifNames;
            ipConn.SendGetInterfaces();
            ipConn.RecvInterfaceList(out ifNames);

            for (int i = 0; i < ifNames.Length; ++i)
            {
                CustomVector.Expose(ifNames, i);
                char[] in ExHeap ifName = ifNames[i];
                if (ifName == null)
                {
                    throw new Exception("ifName is null");
                }
                string !deviceName = Bitter.ToString2(ifName);
                CustomVector.UnExpose(ifNames, i);
                ipConn.SendGetInterfaceState(Bitter.FromString2(deviceName));
                switch receive {
                case ipConn.InterfaceNotFound():
                    Console.WriteLine("Unexpected error");
                    break;

                case ipConn.InterfaceState(InterfaceInfo ifInfo):
                    WriteInterfaceLine(ifInfo, deviceName);
                    break;

                case ipConn.ChannelClosed():
                    throw new Exception("ipConn channel closed");
                }
            }

            delete ifNames;
            delete ipConn;

            return(0);
        }
Ejemplo n.º 3
0
 // Marked unsafe for now since checker
 // has problems with custom vector access.
 public static void ShowConfig(IPContract.Imp : ReadyState !ipConn, DNSContract.Imp : ReadyState !dnsConn)
Ejemplo n.º 4
0
        internal static int Add(AddConfig !config)
        {
            IPv4 gateway;

            RoutingContract.Imp routeConn = ((!)config.routingRef).Acquire();
            if (routeConn == null)
            {
                Console.WriteLine("Could not initialize routing endpoint.");
                return(1);
            }
            routeConn.RecvReady();

            if (IPv4.Parse(config.gateway, out gateway) == false)
            {
                Console.WriteLine("Could not parse gateway address.");
                delete routeConn;
                return(-1);
            }

            try {
                // NOTE: for some reason the compiler doesn't
                // realize that ifaddr will definitely be assigned if
                // we survive the if/switch-receive sequence. Work
                // around that by initializing.
                IPv4 ifaddr = new IPv4(0);

                if (config.ifAddress == null)
                {
                    routeConn.SendFindHostRoute((uint)gateway);

                    switch receive {
                    case routeConn.Route(RouteEntry r):
                        ifaddr = new IPv4(r.ifaddr);
                        break;

                    case routeConn.NoRouteFound():
                        Console.WriteLine("No route to gateway.");
                        delete routeConn;

                        return(-1);

                        break;

                    case routeConn.ChannelClosed():
                        Console.WriteLine("routeConn channel closed.");
                        throw new Exception("routeConn channel closed.");
                    }
                }
                else if (IPv4.Parse(config.ifAddress, out ifaddr) == false)
                {
                    Console.WriteLine("Could not parse interface address.");
                    delete routeConn;
                    return(-1);
                }

                IPContract.Imp ipConn = ((!)config.ipRef).Acquire();
                if (ipConn == null)
                {
                    Console.WriteLine("Could not initialize IP endpoint.");
                    delete routeConn;
                    return(-1);
                }

                try {
                    bool isLocal;
                    ipConn.SendIsLocalAddress((uint)ifaddr);
                    ipConn.RecvIsLocal(out isLocal);

                    if (!isLocal)
                    {
                        Console.WriteLine("Proposed interface address is not bound to an interface.");
                        delete routeConn;
                        return(-1);
                    }
                }
                finally {
                    delete ipConn;
                    ipConn = null;
                }

                IPv4Network destination;
                if (IPv4Network.Parse(config.destination, out destination) == false)
                {
                    Console.WriteLine("Could not parse destination address.");
                    delete routeConn;
                    return(-1);
                }

                NetStack.Contracts.Network nwrk = new NetStack.Contracts.Network();
                nwrk.network = (uint)destination.Network;
                nwrk.netmask = (uint)destination.NetMask;
                routeConn.SendAddRoute(nwrk, (uint)gateway, (uint)ifaddr);

                switch receive {
                case routeConn.Err():
                    Console.WriteLine("Route could not be added -- it may already exist.");
                    break;

                case routeConn.OK():
                    Console.WriteLine("Route added successfully");
                    break;

                case routeConn.ChannelClosed():
                    throw new Exception("routeConn channel closed");
                }
            }
            catch (Exception e) {
                Console.WriteLine("Exception: {0}", e);
            }
            delete routeConn;

            return(0);
        }