WebServiceDhcpApi.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2022 Shreyas Zare (shreyas@technitium.com)
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. using DnsServerCore.Dhcp;
  16. using DnsServerCore.Dhcp.Options;
  17. using Newtonsoft.Json;
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Net;
  21. using System.Threading.Tasks;
  22. namespace DnsServerCore
  23. {
  24. class WebServiceDhcpApi
  25. {
  26. #region variables
  27. readonly DnsWebService _dnsWebService;
  28. #endregion
  29. #region constructor
  30. public WebServiceDhcpApi(DnsWebService dnsWebService)
  31. {
  32. _dnsWebService = dnsWebService;
  33. }
  34. #endregion
  35. #region public
  36. public void ListDhcpLeases(JsonTextWriter jsonWriter)
  37. {
  38. IReadOnlyDictionary<string, Scope> scopes = _dnsWebService.DhcpServer.Scopes;
  39. //sort by name
  40. List<Scope> sortedScopes = new List<Scope>(scopes.Count);
  41. foreach (KeyValuePair<string, Scope> entry in scopes)
  42. sortedScopes.Add(entry.Value);
  43. sortedScopes.Sort();
  44. jsonWriter.WritePropertyName("leases");
  45. jsonWriter.WriteStartArray();
  46. foreach (Scope scope in sortedScopes)
  47. {
  48. IReadOnlyDictionary<ClientIdentifierOption, Lease> leases = scope.Leases;
  49. //sort by address
  50. List<Lease> sortedLeases = new List<Lease>(leases.Count);
  51. foreach (KeyValuePair<ClientIdentifierOption, Lease> entry in leases)
  52. sortedLeases.Add(entry.Value);
  53. sortedLeases.Sort();
  54. foreach (Lease lease in sortedLeases)
  55. {
  56. jsonWriter.WriteStartObject();
  57. jsonWriter.WritePropertyName("scope");
  58. jsonWriter.WriteValue(scope.Name);
  59. jsonWriter.WritePropertyName("type");
  60. jsonWriter.WriteValue(lease.Type.ToString());
  61. jsonWriter.WritePropertyName("hardwareAddress");
  62. jsonWriter.WriteValue(BitConverter.ToString(lease.HardwareAddress));
  63. jsonWriter.WritePropertyName("address");
  64. jsonWriter.WriteValue(lease.Address.ToString());
  65. jsonWriter.WritePropertyName("hostName");
  66. jsonWriter.WriteValue(lease.HostName);
  67. jsonWriter.WritePropertyName("leaseObtained");
  68. jsonWriter.WriteValue(lease.LeaseObtained);
  69. jsonWriter.WritePropertyName("leaseExpires");
  70. jsonWriter.WriteValue(lease.LeaseExpires);
  71. jsonWriter.WriteEndObject();
  72. }
  73. }
  74. jsonWriter.WriteEndArray();
  75. }
  76. public void ListDhcpScopes(JsonTextWriter jsonWriter)
  77. {
  78. IReadOnlyDictionary<string, Scope> scopes = _dnsWebService.DhcpServer.Scopes;
  79. //sort by name
  80. List<Scope> sortedScopes = new List<Scope>(scopes.Count);
  81. foreach (KeyValuePair<string, Scope> entry in scopes)
  82. sortedScopes.Add(entry.Value);
  83. sortedScopes.Sort();
  84. jsonWriter.WritePropertyName("scopes");
  85. jsonWriter.WriteStartArray();
  86. foreach (Scope scope in sortedScopes)
  87. {
  88. jsonWriter.WriteStartObject();
  89. jsonWriter.WritePropertyName("name");
  90. jsonWriter.WriteValue(scope.Name);
  91. jsonWriter.WritePropertyName("enabled");
  92. jsonWriter.WriteValue(scope.Enabled);
  93. jsonWriter.WritePropertyName("startingAddress");
  94. jsonWriter.WriteValue(scope.StartingAddress.ToString());
  95. jsonWriter.WritePropertyName("endingAddress");
  96. jsonWriter.WriteValue(scope.EndingAddress.ToString());
  97. jsonWriter.WritePropertyName("subnetMask");
  98. jsonWriter.WriteValue(scope.SubnetMask.ToString());
  99. jsonWriter.WritePropertyName("networkAddress");
  100. jsonWriter.WriteValue(scope.NetworkAddress.ToString());
  101. jsonWriter.WritePropertyName("broadcastAddress");
  102. jsonWriter.WriteValue(scope.BroadcastAddress.ToString());
  103. if (scope.InterfaceAddress != null)
  104. {
  105. jsonWriter.WritePropertyName("interfaceAddress");
  106. jsonWriter.WriteValue(scope.InterfaceAddress.ToString());
  107. }
  108. jsonWriter.WriteEndObject();
  109. }
  110. jsonWriter.WriteEndArray();
  111. }
  112. public void GetDhcpScope(HttpListenerRequest request, JsonTextWriter jsonWriter)
  113. {
  114. string scopeName = request.QueryString["name"];
  115. if (string.IsNullOrEmpty(scopeName))
  116. throw new DnsWebServiceException("Parameter 'name' missing.");
  117. Scope scope = _dnsWebService.DhcpServer.GetScope(scopeName);
  118. if (scope == null)
  119. throw new DnsWebServiceException("DHCP scope was not found: " + scopeName);
  120. jsonWriter.WritePropertyName("name");
  121. jsonWriter.WriteValue(scope.Name);
  122. jsonWriter.WritePropertyName("startingAddress");
  123. jsonWriter.WriteValue(scope.StartingAddress.ToString());
  124. jsonWriter.WritePropertyName("endingAddress");
  125. jsonWriter.WriteValue(scope.EndingAddress.ToString());
  126. jsonWriter.WritePropertyName("subnetMask");
  127. jsonWriter.WriteValue(scope.SubnetMask.ToString());
  128. jsonWriter.WritePropertyName("leaseTimeDays");
  129. jsonWriter.WriteValue(scope.LeaseTimeDays);
  130. jsonWriter.WritePropertyName("leaseTimeHours");
  131. jsonWriter.WriteValue(scope.LeaseTimeHours);
  132. jsonWriter.WritePropertyName("leaseTimeMinutes");
  133. jsonWriter.WriteValue(scope.LeaseTimeMinutes);
  134. jsonWriter.WritePropertyName("offerDelayTime");
  135. jsonWriter.WriteValue(scope.OfferDelayTime);
  136. jsonWriter.WritePropertyName("pingCheckEnabled");
  137. jsonWriter.WriteValue(scope.PingCheckEnabled);
  138. jsonWriter.WritePropertyName("pingCheckTimeout");
  139. jsonWriter.WriteValue(scope.PingCheckTimeout);
  140. jsonWriter.WritePropertyName("pingCheckRetries");
  141. jsonWriter.WriteValue(scope.PingCheckRetries);
  142. if (!string.IsNullOrEmpty(scope.DomainName))
  143. {
  144. jsonWriter.WritePropertyName("domainName");
  145. jsonWriter.WriteValue(scope.DomainName);
  146. }
  147. jsonWriter.WritePropertyName("dnsTtl");
  148. jsonWriter.WriteValue(scope.DnsTtl);
  149. if (scope.ServerAddress != null)
  150. {
  151. jsonWriter.WritePropertyName("serverAddress");
  152. jsonWriter.WriteValue(scope.ServerAddress.ToString());
  153. }
  154. if (scope.ServerHostName != null)
  155. {
  156. jsonWriter.WritePropertyName("serverHostName");
  157. jsonWriter.WriteValue(scope.ServerHostName);
  158. }
  159. if (scope.BootFileName != null)
  160. {
  161. jsonWriter.WritePropertyName("bootFileName");
  162. jsonWriter.WriteValue(scope.BootFileName);
  163. }
  164. if (scope.RouterAddress != null)
  165. {
  166. jsonWriter.WritePropertyName("routerAddress");
  167. jsonWriter.WriteValue(scope.RouterAddress.ToString());
  168. }
  169. jsonWriter.WritePropertyName("useThisDnsServer");
  170. jsonWriter.WriteValue(scope.UseThisDnsServer);
  171. if (scope.DnsServers != null)
  172. {
  173. jsonWriter.WritePropertyName("dnsServers");
  174. jsonWriter.WriteStartArray();
  175. foreach (IPAddress dnsServer in scope.DnsServers)
  176. jsonWriter.WriteValue(dnsServer.ToString());
  177. jsonWriter.WriteEndArray();
  178. }
  179. if (scope.WinsServers != null)
  180. {
  181. jsonWriter.WritePropertyName("winsServers");
  182. jsonWriter.WriteStartArray();
  183. foreach (IPAddress winsServer in scope.WinsServers)
  184. jsonWriter.WriteValue(winsServer.ToString());
  185. jsonWriter.WriteEndArray();
  186. }
  187. if (scope.NtpServers != null)
  188. {
  189. jsonWriter.WritePropertyName("ntpServers");
  190. jsonWriter.WriteStartArray();
  191. foreach (IPAddress ntpServer in scope.NtpServers)
  192. jsonWriter.WriteValue(ntpServer.ToString());
  193. jsonWriter.WriteEndArray();
  194. }
  195. if (scope.StaticRoutes != null)
  196. {
  197. jsonWriter.WritePropertyName("staticRoutes");
  198. jsonWriter.WriteStartArray();
  199. foreach (ClasslessStaticRouteOption.Route route in scope.StaticRoutes)
  200. {
  201. jsonWriter.WriteStartObject();
  202. jsonWriter.WritePropertyName("destination");
  203. jsonWriter.WriteValue(route.Destination.ToString());
  204. jsonWriter.WritePropertyName("subnetMask");
  205. jsonWriter.WriteValue(route.SubnetMask.ToString());
  206. jsonWriter.WritePropertyName("router");
  207. jsonWriter.WriteValue(route.Router.ToString());
  208. jsonWriter.WriteEndObject();
  209. }
  210. jsonWriter.WriteEndArray();
  211. }
  212. if (scope.VendorInfo != null)
  213. {
  214. jsonWriter.WritePropertyName("vendorInfo");
  215. jsonWriter.WriteStartArray();
  216. foreach (KeyValuePair<string, VendorSpecificInformationOption> entry in scope.VendorInfo)
  217. {
  218. jsonWriter.WriteStartObject();
  219. jsonWriter.WritePropertyName("identifier");
  220. jsonWriter.WriteValue(entry.Key);
  221. jsonWriter.WritePropertyName("information");
  222. jsonWriter.WriteValue(entry.Value.ToString());
  223. jsonWriter.WriteEndObject();
  224. }
  225. jsonWriter.WriteEndArray();
  226. }
  227. if (scope.Exclusions != null)
  228. {
  229. jsonWriter.WritePropertyName("exclusions");
  230. jsonWriter.WriteStartArray();
  231. foreach (Exclusion exclusion in scope.Exclusions)
  232. {
  233. jsonWriter.WriteStartObject();
  234. jsonWriter.WritePropertyName("startingAddress");
  235. jsonWriter.WriteValue(exclusion.StartingAddress.ToString());
  236. jsonWriter.WritePropertyName("endingAddress");
  237. jsonWriter.WriteValue(exclusion.EndingAddress.ToString());
  238. jsonWriter.WriteEndObject();
  239. }
  240. jsonWriter.WriteEndArray();
  241. }
  242. jsonWriter.WritePropertyName("reservedLeases");
  243. jsonWriter.WriteStartArray();
  244. foreach (Lease reservedLease in scope.ReservedLeases)
  245. {
  246. jsonWriter.WriteStartObject();
  247. jsonWriter.WritePropertyName("hostName");
  248. jsonWriter.WriteValue(reservedLease.HostName);
  249. jsonWriter.WritePropertyName("hardwareAddress");
  250. jsonWriter.WriteValue(BitConverter.ToString(reservedLease.HardwareAddress));
  251. jsonWriter.WritePropertyName("address");
  252. jsonWriter.WriteValue(reservedLease.Address.ToString());
  253. jsonWriter.WritePropertyName("comments");
  254. jsonWriter.WriteValue(reservedLease.Comments);
  255. jsonWriter.WriteEndObject();
  256. }
  257. jsonWriter.WriteEndArray();
  258. jsonWriter.WritePropertyName("allowOnlyReservedLeases");
  259. jsonWriter.WriteValue(scope.AllowOnlyReservedLeases);
  260. }
  261. public async Task SetDhcpScopeAsync(HttpListenerRequest request)
  262. {
  263. string scopeName = request.QueryString["name"];
  264. if (string.IsNullOrEmpty(scopeName))
  265. throw new DnsWebServiceException("Parameter 'name' missing.");
  266. string strStartingAddress = request.QueryString["startingAddress"];
  267. string strEndingAddress = request.QueryString["endingAddress"];
  268. string strSubnetMask = request.QueryString["subnetMask"];
  269. bool scopeExists;
  270. Scope scope = _dnsWebService.DhcpServer.GetScope(scopeName);
  271. if (scope is null)
  272. {
  273. //scope does not exists; create new scope
  274. if (string.IsNullOrEmpty(strStartingAddress))
  275. throw new DnsWebServiceException("Parameter 'startingAddress' missing.");
  276. if (string.IsNullOrEmpty(strEndingAddress))
  277. throw new DnsWebServiceException("Parameter 'endingAddress' missing.");
  278. if (string.IsNullOrEmpty(strSubnetMask))
  279. throw new DnsWebServiceException("Parameter 'subnetMask' missing.");
  280. scopeExists = false;
  281. scope = new Scope(scopeName, true, IPAddress.Parse(strStartingAddress), IPAddress.Parse(strEndingAddress), IPAddress.Parse(strSubnetMask));
  282. }
  283. else
  284. {
  285. scopeExists = true;
  286. IPAddress startingAddress;
  287. if (string.IsNullOrEmpty(strStartingAddress))
  288. startingAddress = scope.StartingAddress;
  289. else
  290. startingAddress = IPAddress.Parse(strStartingAddress);
  291. IPAddress endingAddress;
  292. if (string.IsNullOrEmpty(strEndingAddress))
  293. endingAddress = scope.EndingAddress;
  294. else
  295. endingAddress = IPAddress.Parse(strEndingAddress);
  296. IPAddress subnetMask;
  297. if (string.IsNullOrEmpty(strSubnetMask))
  298. subnetMask = scope.SubnetMask;
  299. else
  300. subnetMask = IPAddress.Parse(strSubnetMask);
  301. //validate scope address
  302. foreach (KeyValuePair<string, Scope> entry in _dnsWebService.DhcpServer.Scopes)
  303. {
  304. Scope existingScope = entry.Value;
  305. if (existingScope.Equals(scope))
  306. continue;
  307. if (existingScope.IsAddressInRange(startingAddress) || existingScope.IsAddressInRange(endingAddress))
  308. throw new DhcpServerException("Scope with overlapping range already exists: " + existingScope.StartingAddress.ToString() + "-" + existingScope.EndingAddress.ToString());
  309. }
  310. scope.ChangeNetwork(startingAddress, endingAddress, subnetMask);
  311. }
  312. string strLeaseTimeDays = request.QueryString["leaseTimeDays"];
  313. if (!string.IsNullOrEmpty(strLeaseTimeDays))
  314. scope.LeaseTimeDays = ushort.Parse(strLeaseTimeDays);
  315. string strLeaseTimeHours = request.QueryString["leaseTimeHours"];
  316. if (!string.IsNullOrEmpty(strLeaseTimeHours))
  317. scope.LeaseTimeHours = byte.Parse(strLeaseTimeHours);
  318. string strLeaseTimeMinutes = request.QueryString["leaseTimeMinutes"];
  319. if (!string.IsNullOrEmpty(strLeaseTimeMinutes))
  320. scope.LeaseTimeMinutes = byte.Parse(strLeaseTimeMinutes);
  321. string strOfferDelayTime = request.QueryString["offerDelayTime"];
  322. if (!string.IsNullOrEmpty(strOfferDelayTime))
  323. scope.OfferDelayTime = ushort.Parse(strOfferDelayTime);
  324. string strPingCheckEnabled = request.QueryString["pingCheckEnabled"];
  325. if (!string.IsNullOrEmpty(strPingCheckEnabled))
  326. scope.PingCheckEnabled = bool.Parse(strPingCheckEnabled);
  327. string strPingCheckTimeout = request.QueryString["pingCheckTimeout"];
  328. if (!string.IsNullOrEmpty(strPingCheckTimeout))
  329. scope.PingCheckTimeout = ushort.Parse(strPingCheckTimeout);
  330. string strPingCheckRetries = request.QueryString["pingCheckRetries"];
  331. if (!string.IsNullOrEmpty(strPingCheckRetries))
  332. scope.PingCheckRetries = byte.Parse(strPingCheckRetries);
  333. string strDomainName = request.QueryString["domainName"];
  334. if (strDomainName != null)
  335. scope.DomainName = strDomainName.Length == 0 ? null : strDomainName;
  336. string strDnsTtl = request.QueryString["dnsTtl"];
  337. if (!string.IsNullOrEmpty(strDnsTtl))
  338. scope.DnsTtl = uint.Parse(strDnsTtl);
  339. string strServerAddress = request.QueryString["serverAddress"];
  340. if (strServerAddress != null)
  341. scope.ServerAddress = strServerAddress.Length == 0 ? null : IPAddress.Parse(strServerAddress);
  342. string strServerHostName = request.QueryString["serverHostName"];
  343. if (strServerHostName != null)
  344. scope.ServerHostName = strServerHostName.Length == 0 ? null : strServerHostName;
  345. string strBootFileName = request.QueryString["bootFileName"];
  346. if (strBootFileName != null)
  347. scope.BootFileName = strBootFileName.Length == 0 ? null : strBootFileName;
  348. string strRouterAddress = request.QueryString["routerAddress"];
  349. if (strRouterAddress != null)
  350. scope.RouterAddress = strRouterAddress.Length == 0 ? null : IPAddress.Parse(strRouterAddress);
  351. string strUseThisDnsServer = request.QueryString["useThisDnsServer"];
  352. if (!string.IsNullOrEmpty(strUseThisDnsServer))
  353. scope.UseThisDnsServer = bool.Parse(strUseThisDnsServer);
  354. if (!scope.UseThisDnsServer)
  355. {
  356. string strDnsServers = request.QueryString["dnsServers"];
  357. if (strDnsServers != null)
  358. {
  359. if (strDnsServers.Length == 0)
  360. {
  361. scope.DnsServers = null;
  362. }
  363. else
  364. {
  365. string[] strDnsServerParts = strDnsServers.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  366. IPAddress[] dnsServers = new IPAddress[strDnsServerParts.Length];
  367. for (int i = 0; i < strDnsServerParts.Length; i++)
  368. dnsServers[i] = IPAddress.Parse(strDnsServerParts[i]);
  369. scope.DnsServers = dnsServers;
  370. }
  371. }
  372. }
  373. string strWinsServers = request.QueryString["winsServers"];
  374. if (strWinsServers != null)
  375. {
  376. if (strWinsServers.Length == 0)
  377. {
  378. scope.WinsServers = null;
  379. }
  380. else
  381. {
  382. string[] strWinsServerParts = strWinsServers.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  383. IPAddress[] winsServers = new IPAddress[strWinsServerParts.Length];
  384. for (int i = 0; i < strWinsServerParts.Length; i++)
  385. winsServers[i] = IPAddress.Parse(strWinsServerParts[i]);
  386. scope.WinsServers = winsServers;
  387. }
  388. }
  389. string strNtpServers = request.QueryString["ntpServers"];
  390. if (strNtpServers != null)
  391. {
  392. if (strNtpServers.Length == 0)
  393. {
  394. scope.NtpServers = null;
  395. }
  396. else
  397. {
  398. string[] strNtpServerParts = strNtpServers.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  399. IPAddress[] ntpServers = new IPAddress[strNtpServerParts.Length];
  400. for (int i = 0; i < strNtpServerParts.Length; i++)
  401. ntpServers[i] = IPAddress.Parse(strNtpServerParts[i]);
  402. scope.NtpServers = ntpServers;
  403. }
  404. }
  405. string strStaticRoutes = request.QueryString["staticRoutes"];
  406. if (strStaticRoutes != null)
  407. {
  408. if (strStaticRoutes.Length == 0)
  409. {
  410. scope.StaticRoutes = null;
  411. }
  412. else
  413. {
  414. string[] strStaticRoutesParts = strStaticRoutes.Split('|');
  415. List<ClasslessStaticRouteOption.Route> staticRoutes = new List<ClasslessStaticRouteOption.Route>();
  416. for (int i = 0; i < strStaticRoutesParts.Length; i += 3)
  417. {
  418. staticRoutes.Add(new ClasslessStaticRouteOption.Route(IPAddress.Parse(strStaticRoutesParts[i + 0]), IPAddress.Parse(strStaticRoutesParts[i + 1]), IPAddress.Parse(strStaticRoutesParts[i + 2])));
  419. }
  420. scope.StaticRoutes = staticRoutes;
  421. }
  422. }
  423. string strVendorInfo = request.QueryString["vendorInfo"];
  424. if (strVendorInfo != null)
  425. {
  426. if (strVendorInfo.Length == 0)
  427. {
  428. scope.VendorInfo = null;
  429. }
  430. else
  431. {
  432. string[] strVendorInfoParts = strVendorInfo.Split('|');
  433. Dictionary<string, VendorSpecificInformationOption> vendorInfo = new Dictionary<string, VendorSpecificInformationOption>();
  434. for (int i = 0; i < strVendorInfoParts.Length; i += 2)
  435. {
  436. vendorInfo.Add(strVendorInfoParts[i + 0], new VendorSpecificInformationOption(strVendorInfoParts[i + 1]));
  437. }
  438. scope.VendorInfo = vendorInfo;
  439. }
  440. }
  441. string strExclusions = request.QueryString["exclusions"];
  442. if (strExclusions != null)
  443. {
  444. if (strExclusions.Length == 0)
  445. {
  446. scope.Exclusions = null;
  447. }
  448. else
  449. {
  450. string[] strExclusionsParts = strExclusions.Split('|');
  451. List<Exclusion> exclusions = new List<Exclusion>();
  452. for (int i = 0; i < strExclusionsParts.Length; i += 2)
  453. {
  454. exclusions.Add(new Exclusion(IPAddress.Parse(strExclusionsParts[i + 0]), IPAddress.Parse(strExclusionsParts[i + 1])));
  455. }
  456. scope.Exclusions = exclusions;
  457. }
  458. }
  459. string strReservedLeases = request.QueryString["reservedLeases"];
  460. if (strReservedLeases != null)
  461. {
  462. if (strReservedLeases.Length == 0)
  463. {
  464. scope.ReservedLeases = null;
  465. }
  466. else
  467. {
  468. string[] strReservedLeaseParts = strReservedLeases.Split('|');
  469. List<Lease> reservedLeases = new List<Lease>();
  470. for (int i = 0; i < strReservedLeaseParts.Length; i += 4)
  471. {
  472. reservedLeases.Add(new Lease(LeaseType.Reserved, strReservedLeaseParts[i + 0], DhcpMessageHardwareAddressType.Ethernet, strReservedLeaseParts[i + 1], IPAddress.Parse(strReservedLeaseParts[i + 2]), strReservedLeaseParts[i + 3]));
  473. }
  474. scope.ReservedLeases = reservedLeases;
  475. }
  476. }
  477. string strAllowOnlyReservedLeases = request.QueryString["allowOnlyReservedLeases"];
  478. if (!string.IsNullOrEmpty(strAllowOnlyReservedLeases))
  479. scope.AllowOnlyReservedLeases = bool.Parse(strAllowOnlyReservedLeases);
  480. if (scopeExists)
  481. {
  482. _dnsWebService.DhcpServer.SaveScope(scopeName);
  483. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] DHCP scope was updated successfully: " + scopeName);
  484. }
  485. else
  486. {
  487. await _dnsWebService.DhcpServer.AddScopeAsync(scope);
  488. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] DHCP scope was added successfully: " + scopeName);
  489. }
  490. string newName = request.QueryString["newName"];
  491. if (!string.IsNullOrEmpty(newName) && !newName.Equals(scopeName))
  492. {
  493. _dnsWebService.DhcpServer.RenameScope(scopeName, newName);
  494. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] DHCP scope was renamed successfully: '" + scopeName + "' to '" + newName + "'");
  495. }
  496. }
  497. public async Task EnableDhcpScopeAsync(HttpListenerRequest request)
  498. {
  499. string scopeName = request.QueryString["name"];
  500. if (string.IsNullOrEmpty(scopeName))
  501. throw new DnsWebServiceException("Parameter 'name' missing.");
  502. if (!await _dnsWebService.DhcpServer.EnableScopeAsync(scopeName))
  503. throw new DnsWebServiceException("Failed to enable DHCP scope, please check logs for details: " + scopeName);
  504. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] DHCP scope was enabled successfully: " + scopeName);
  505. }
  506. public void DisableDhcpScope(HttpListenerRequest request)
  507. {
  508. string scopeName = request.QueryString["name"];
  509. if (string.IsNullOrEmpty(scopeName))
  510. throw new DnsWebServiceException("Parameter 'name' missing.");
  511. if (!_dnsWebService.DhcpServer.DisableScope(scopeName))
  512. throw new DnsWebServiceException("Failed to disable DHCP scope, please check logs for details: " + scopeName);
  513. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] DHCP scope was disabled successfully: " + scopeName);
  514. }
  515. public void DeleteDhcpScope(HttpListenerRequest request)
  516. {
  517. string scopeName = request.QueryString["name"];
  518. if (string.IsNullOrEmpty(scopeName))
  519. throw new DnsWebServiceException("Parameter 'name' missing.");
  520. _dnsWebService.DhcpServer.DeleteScope(scopeName);
  521. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] DHCP scope was deleted successfully: " + scopeName);
  522. }
  523. public void RemoveDhcpLease(HttpListenerRequest request)
  524. {
  525. string scopeName = request.QueryString["name"];
  526. if (string.IsNullOrEmpty(scopeName))
  527. throw new DnsWebServiceException("Parameter 'name' missing.");
  528. string strHardwareAddress = request.QueryString["hardwareAddress"];
  529. if (string.IsNullOrEmpty(strHardwareAddress))
  530. throw new DnsWebServiceException("Parameter 'hardwareAddress' missing.");
  531. _dnsWebService.DhcpServer.RemoveLease(scopeName, strHardwareAddress);
  532. _dnsWebService.DhcpServer.SaveScope(scopeName);
  533. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] DHCP scope's lease was removed successfully: " + scopeName);
  534. }
  535. public void ConvertToReservedLease(HttpListenerRequest request)
  536. {
  537. string scopeName = request.QueryString["name"];
  538. if (string.IsNullOrEmpty(scopeName))
  539. throw new DnsWebServiceException("Parameter 'name' missing.");
  540. Scope scope = _dnsWebService.DhcpServer.GetScope(scopeName);
  541. if (scope == null)
  542. throw new DnsWebServiceException("DHCP scope does not exists: " + scopeName);
  543. string strHardwareAddress = request.QueryString["hardwareAddress"];
  544. if (string.IsNullOrEmpty(strHardwareAddress))
  545. throw new DnsWebServiceException("Parameter 'hardwareAddress' missing.");
  546. scope.ConvertToReservedLease(strHardwareAddress);
  547. _dnsWebService.DhcpServer.SaveScope(scopeName);
  548. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] DHCP scope's lease was reserved successfully: " + scopeName);
  549. }
  550. public void ConvertToDynamicLease(HttpListenerRequest request)
  551. {
  552. string scopeName = request.QueryString["name"];
  553. if (string.IsNullOrEmpty(scopeName))
  554. throw new DnsWebServiceException("Parameter 'name' missing.");
  555. Scope scope = _dnsWebService.DhcpServer.GetScope(scopeName);
  556. if (scope == null)
  557. throw new DnsWebServiceException("DHCP scope does not exists: " + scopeName);
  558. string strHardwareAddress = request.QueryString["hardwareAddress"];
  559. if (string.IsNullOrEmpty(strHardwareAddress))
  560. throw new DnsWebServiceException("Parameter 'hardwareAddress' missing.");
  561. scope.ConvertToDynamicLease(strHardwareAddress);
  562. _dnsWebService.DhcpServer.SaveScope(scopeName);
  563. _dnsWebService.Log.Write(DnsWebService.GetRequestRemoteEndPoint(request), "[" + _dnsWebService.GetSession(request).Username + "] DHCP scope's lease was unreserved successfully: " + scopeName);
  564. }
  565. #endregion
  566. }
  567. }