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; }
static IDhcpOption ParseDhcpOption( byte optionCode, byte length, byte[] srcBuffer, int offset) { byte type = optionCode; switch (optionCode) { case DhcpSubnetMask.OptionCode: return(DhcpIPv4Option.Parse(type, length, srcBuffer, offset)); case DhcpTimeOffset.OptionCode: return(DhcpDWordOption.Parse(type, length, srcBuffer, offset)); case DhcpRouter.OptionCode: return(DhcpMultiIPv4Option.Parse(type, length, srcBuffer, offset)); case DhcpTimeServer.OptionCode: return(DhcpMultiIPv4Option.Parse(type, length, srcBuffer, offset)); case DhcpNameServer.OptionCode: return(DhcpMultiIPv4Option.Parse(type, length, srcBuffer, offset)); case DhcpDomainNameServer.OptionCode: return(DhcpMultiIPv4Option.Parse(type, length, srcBuffer, offset)); case DhcpLogServer.OptionCode: return(DhcpMultiIPv4Option.Parse(type, length, srcBuffer, offset)); case DhcpCookieServer.OptionCode: return(DhcpMultiIPv4Option.Parse(type, length, srcBuffer, offset)); case DhcpLprServer.OptionCode: return(DhcpMultiIPv4Option.Parse(type, length, srcBuffer, offset)); case DhcpImpressServer.OptionCode: return(DhcpMultiIPv4Option.Parse(type, length, srcBuffer, offset)); case DhcpResourceLocationServer.OptionCode: return(DhcpMultiIPv4Option.Parse(type, length, srcBuffer, offset)); case DhcpHostName.OptionCode: return(DhcpStringOption.Parse(type, length, srcBuffer, offset)); case DhcpBootFileSize.OptionCode: return(DhcpWordOption.Parse(type, length, srcBuffer, offset)); case DhcpMeritDumpFile.OptionCode: return(DhcpStringOption.Parse(type, length, srcBuffer, offset)); case DhcpDomainName.OptionCode: return(DhcpStringOption.Parse(type, length, srcBuffer, offset)); case DhcpSwapServer.OptionCode: return(DhcpIPv4Option.Parse(type, length, srcBuffer, offset)); case DhcpRootPath.OptionCode: return(DhcpStringOption.Parse(type, length, srcBuffer, offset)); case DhcpExtensionsPath.OptionCode: return(DhcpStringOption.Parse(type, length, srcBuffer, offset)); case DhcpIPForwarding.OptionCode: return(DhcpByteOption.Parse(type, length, srcBuffer, offset)); case DhcpForwardRemoteSourceRoute.OptionCode: return(DhcpByteOption.Parse(type, length, srcBuffer, offset)); case DhcpPolicyFilter.OptionCode: return(DhcpMultiIPv4Option.Parse(type, length, srcBuffer, offset)); case DhcpMaximumDatagramReassembly.OptionCode: return(DhcpWordOption.Parse(type, length, srcBuffer, offset)); case DhcpDefaultTtl.OptionCode: return(DhcpByteOption.Parse(type, length, srcBuffer, offset)); case DhcpPathMtuAgingTimeout.OptionCode: return(DhcpDWordOption.Parse(type, length, srcBuffer, offset)); case DhcpPathMtuPlateauTable.OptionCode: return(DhcpMultiWordOption.Parse(type, length, srcBuffer, offset)); case DhcpInterfaceMtu.OptionCode: return(DhcpWordOption.Parse(type, length, srcBuffer, offset)); case DhcpAllSubnetsLocal.OptionCode: return(DhcpByteOption.Parse(type, length, srcBuffer, offset)); case DhcpBroadcastAddress.OptionCode: return(DhcpIPv4Option.Parse(type, length, srcBuffer, offset)); case DhcpMaskDiscovery.OptionCode: return(DhcpByteOption.Parse(type, length, srcBuffer, offset)); case DhcpMaskSupplier.OptionCode: return(DhcpByteOption.Parse(type, length, srcBuffer, offset)); case DhcpRouterDiscovery.OptionCode: return(DhcpByteOption.Parse(type, length, srcBuffer, offset)); case DhcpRouterSolicitationAddress.OptionCode: return(DhcpIPv4Option.Parse(type, length, srcBuffer, offset)); case DhcpStaticRoutes.OptionCode: return(DhcpMultiIPv4Option.Parse(type, length, srcBuffer, offset)); case DhcpTrailerEncapsulate.OptionCode: return(DhcpByteOption.Parse(type, length, srcBuffer, offset)); case DhcpArpCacheTimeout.OptionCode: return(DhcpDWordOption.Parse(type, length, srcBuffer, offset)); case DhcpEthernetEncapsulation.OptionCode: return(DhcpByteOption.Parse(type, length, srcBuffer, offset)); case DhcpTcpDefaultTtl.OptionCode: return(DhcpByteOption.Parse(type, length, srcBuffer, offset)); case DhcpTcpKeepalive.OptionCode: return(DhcpByteOption.Parse(type, length, srcBuffer, offset)); case DhcpTcpKeepaliveGarbage.OptionCode: return(DhcpByteOption.Parse(type, length, srcBuffer, offset)); case DhcpNisDomain.OptionCode: return(DhcpStringOption.Parse(type, length, srcBuffer, offset)); case DhcpNisServers.OptionCode: return(DhcpMultiIPv4Option.Parse(type, length, srcBuffer, offset)); case DhcpNtpServers.OptionCode: return(DhcpMultiIPv4Option.Parse(type, length, srcBuffer, offset)); case DhcpVendorSpecific.OptionCode: return(DhcpMultiByteOption.Parse(type, length, srcBuffer, offset)); case DhcpNetBiosNameServer.OptionCode: return(DhcpMultiIPv4Option.Parse(type, length, srcBuffer, offset)); case DhcpNetBiosDistributionServer.OptionCode: return(DhcpMultiIPv4Option.Parse(type, length, srcBuffer, offset)); case DhcpNetBiosType.OptionCode: return(DhcpByteOption.Parse(type, length, srcBuffer, offset)); case DhcpNetBiosScope.OptionCode: return(DhcpMultiDWordOption.Parse(type, length, srcBuffer, offset)); case DhcpXWindowsFontServer.OptionCode: return(DhcpMultiIPv4Option.Parse(type, length, srcBuffer, offset)); case DhcpXWindowsDisplayManager.OptionCode: return(DhcpMultiIPv4Option.Parse(type, length, srcBuffer, offset)); case DhcpRequestedIPAddress.OptionCode: return(DhcpIPv4Option.Parse(type, length, srcBuffer, offset)); case DhcpIPAddressLeaseTime.OptionCode: return(DhcpDWordOption.Parse(type, length, srcBuffer, offset)); case DhcpOverloadIndicator.OptionCode: return(DhcpByteOption.Parse(type, length, srcBuffer, offset)); case DhcpMessageType.OptionCode: return(DhcpByteOption.Parse(type, length, srcBuffer, offset)); case DhcpServerID.OptionCode: return(DhcpIPv4Option.Parse(type, length, srcBuffer, offset)); case DhcpParameterRequest.OptionCode: return(DhcpMultiByteOption.Parse(type, length, srcBuffer, offset)); case DhcpMessage.OptionCode: return(DhcpStringOption.Parse(type, length, srcBuffer, offset)); case DhcpMaximumMessageSize.OptionCode: return(DhcpWordOption.Parse(type, length, srcBuffer, offset)); case DhcpRenewalTime.OptionCode: return(DhcpDWordOption.Parse(type, length, srcBuffer, offset)); case DhcpRebindingTime.OptionCode: return(DhcpDWordOption.Parse(type, length, srcBuffer, offset)); case DhcpClassID.OptionCode: return(DhcpStringOption.Parse(type, length, srcBuffer, offset)); case DhcpClientID.OptionCode: return(DhcpMultiByteOption.Parse(type, length, srcBuffer, offset)); case DhcpAutoConfigure.OptionCode: return(DhcpByteOption.Parse(type, length, srcBuffer, offset)); default: return(null); } }
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); }