/// <summary> /// Read a named IPv4 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 IPv4 address.</returns> public static IPv4 LookupIPv4(ProtocolParams parameters, string parameterName, IPv4 defaultValue) { if (parameters == null) { return(defaultValue); } else if (parameterName == null) { throw new ArgumentNullException(); } string sValue = parameters[parameterName]; if (sValue == null) { return(defaultValue); } try { return(IPv4.Parse(sValue)); } catch (FormatException) { Core.Log("Failed on parameter \"{0}\" value \"{1}\"\n", parameterName, sValue); } return(defaultValue); }
internal static int Delete(DeleteConfig !config) { if (config.servers == null) { Console.WriteLine("no servers given"); return(-1); } DNSContract.Imp dnsConn = ((!)config.dnsRef).Acquire(); if (dnsConn == null) { Console.WriteLine("Could not initialize DNS endpoint."); return(1); } dnsConn.RecvReady(); for (int i = 0; i < config.servers.Length; i++) { IPv4 resolver; if (IPv4.Parse(config.servers[i], out resolver) == false) { Console.WriteLine("Invalid IP address: {0}", config.servers[i]); } dnsConn.SendRemoveNameServer((uint)resolver); dnsConn.RecvAck(); } delete dnsConn; return(0); }
/// <summary> /// Resolves a DNS host name or IP address to an IPv4HostEntry instance. /// </summary> public StatusCode Resolve(string hostName, out IPv4HostEntry hostEntry) { IPv4 hostAddress; if (IPv4.Parse(hostName, out hostAddress) == true) { StatusCode status = GetHostByAddress(hostAddress, out hostEntry); if (status != StatusCode.Success) { // As a convenience to the caller, instead of failing // outright, at least give back the parsed version of // the net address. hostEntry = new IPv4HostEntry(new string [] { hostName }, new IPv4 [] { hostAddress }); return(StatusCode.Success); } } return(GetHostByName(hostName, out hostEntry)); }
public virtual void Listen(NetifConfiguration configuration) { if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if (configuration.Address == null) { throw new ArgumentNullException("The network interface device address is not allowed to be null"); } if (configuration.DnsAddress1 == null) { throw new ArgumentNullException("The network interface device dns address 1 is not allowed to be null"); } if (configuration.DnsAddress2 == null) { throw new ArgumentNullException("The network interface device dns address 2 is not allowed to be null"); } if (configuration.GatewayAddress == null) { throw new ArgumentNullException("The network interface device gateway address is not allowed to be null"); } if (configuration.SubnetMask == null) { throw new ArgumentNullException("The network interface device subnet mask is not allowed to be null"); } StartNetifInformation(configuration); ListenNetifLevel3Input((buffer, length) => { IPFrame ip = IPv4.Parse(new BufferSegment(new BufferSegment(buffer, length).ToArray())); if (ip != null) { IPv4.Input(ip); } }); }
static void Main() { StaticConfiguration.Initialize(); StaticConfiguration.Start(); LoopbackAdapter loopback = new LoopbackAdapter(); Console.WriteLine("Created Loopback Adapter {0}", loopback.HardwareAddress); Core.Instance().RegisterAdapter(loopback, 64); IPModule ip = Core.Instance().GetProtocolByName("IP") as IPModule; Console.WriteLine("Binding address to adapter"); IPv4 ifAddress = IPv4.Parse("192.168.0.100"); IPv4 ifNetmask = IPv4.Parse("255.255.255.0"); IPv4 gwAddress = IPv4.Parse("192.168.0.1"); ip.HostConfiguration.Bindings.Add( loopback, new InterfaceIPConfiguration(ifAddress, ifNetmask, gwAddress) ); const int nInstances = 8; const int maxPackets = 10000; ushort basePort = 10000; Receiver[] receivers = new Receiver[nInstances]; Sender[] senders = new Sender[nInstances]; for (int i = 0; i < nInstances; i++) { ushort rPort = (ushort)(basePort + 2 * i); ushort sPort = (ushort)(basePort + 2 * i + 1); receivers[i] = new Receiver(ifAddress, rPort, maxPackets); senders[i] = new Sender(ifAddress, sPort, ifAddress, rPort, maxPackets); } bool done = false; while (done == false) { Thread.CurrentThread.Join(TimeSpan.FromSeconds(1)); done = true; for (int i = 0; i < nInstances; i++) { if (receivers[i].ThreadState != ThreadState.Stopped || senders[i].ThreadState != ThreadState.Stopped) { done = false; break; } } } Console.WriteLine("Removing Adapter."); Core.Instance().DeregisterAdapter(loopback); StaticConfiguration.Stop(); }
public static IPAddress Parse(string ipString) { return(new IPAddress(IPv4.Parse(ipString))); }
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); }