internal void UninstallDhcpOptions() { DhcpIPv4Option address = (DhcpIPv4Option) activeDhcpOptions[DhcpRequestedIPAddress.OptionCode]; IPv4 ifAddress = address.Value; // // Remove routes associated with interface address // HostConfiguration hostConfiguration = IP.GetHostConfiguration(); hostConfiguration.RoutingTable.DeleteInterfaceRoutes(ifAddress); // // Remove name server // DhcpMultiIPv4Option dnsServers = activeDhcpOptions[DhcpDomainNameServer.OptionCode] as DhcpMultiIPv4Option; if (dnsServers != null) { foreach (IPv4 server in dnsServers.Values) { hostConfiguration.AddNameServer(server); } } // // Leave domain name in place // // If we wanted to remove it... DhcpStringOption domain = activeDhcpOptions[DhcpDomainName.OptionCode] as DhcpStringOption; string domainName = hostConfiguration.GetDomainName(); if (domain != null && domainName == domain.Value) { hostConfiguration.SetDomainName(""); } // // Remove interface address bindings // hostConfiguration.Bindings.Flush(adapter, ifAddress); activeDhcpOptions = null; }
//There are several different flavors of implementation for WriteTo //BSD Unix assigns an ephemeral port for every packet, while Linux //picks an ephemeral port and assigns it permanantly. I'm unsure of the //behavior of windows. public bool WriteTo(IPv4 remoteAddr, ushort remotePort, Bytes buffer) { //allocate a local port if (localPort == 0) { bool rc; DebugPrint("Assigning local port to process for WriteTo\n"); rc = AssignNextAvailablePort(); VTable.Assert(rc); } if (localIPAddress == IPv4.Any) { HostConfiguration hostConfiguration = IP.GetHostConfiguration(); RouteEntry e = hostConfiguration.RoutingTable.Lookup(remoteAddr); if (e != null) { localIPAddress = e.InterfaceAddress; DebugPrint("local address now {0}\n", localIPAddress); } else { DebugPrint("No route for {0}\n", remoteAddr); } } int headerSize = EthernetHeader.Size + IpHeader.Size + UDPHeader.Size; Bytes header = new Bytes(new byte[headerSize]); WriteCompleteUDPHeader(header, buffer, buffer.Length); DebugPrint("Sending UDP packet\n"); IP.SendOutgoingPacket(header, buffer, remoteAddr); return(true); }
internal bool InstallDhcpOptions(SortedList dhcpOptions) { if (activeDhcpOptions != null) { UninstallDhcpOptions(); } // // 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); } HostConfiguration hostConfiguration = IP.GetHostConfiguration(); 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) { hostConfiguration.SetDomainName(domain.Value); } // // Add DNS servers // DhcpMultiIPv4Option dnsServers = dhcpOptions[DhcpDomainNameServer.OptionCode] as DhcpMultiIPv4Option; if (dnsServers != null) { foreach (IPv4 server in dnsServers.Values) { 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; hostConfiguration.RoutingTable.AddRoute( new RouteEntry(destination, gateway, ifAddress, RouteEntry.DefaultRouteMetric, 0) ); } } activeDhcpOptions = dhcpOptions; return(true); }