DhcpOption.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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.Options;
  16. using System;
  17. using System.Globalization;
  18. using System.IO;
  19. using TechnitiumLibrary.IO;
  20. namespace DnsServerCore.Dhcp
  21. {
  22. public enum DhcpOptionCode : byte
  23. {
  24. Pad = 0,
  25. SubnetMask = 1,
  26. TimeOffset = 2,
  27. Router = 3,
  28. TimeServer = 4,
  29. NameServer = 5,
  30. DomainNameServer = 6,
  31. LogServer = 7,
  32. CookieServer = 8,
  33. LprServer = 9,
  34. ImpressServer = 10,
  35. ResourceLocationServer = 11,
  36. HostName = 12,
  37. BootFileSize = 13,
  38. MeritDump = 14,
  39. DomainName = 15,
  40. SwapServer = 16,
  41. RootPath = 17,
  42. ExtensionPath = 18,
  43. IpForwarding = 19,
  44. NonLocalSourceRouting = 20,
  45. PolicyFilter = 21,
  46. MaximumDatagramReassemblySize = 22,
  47. DefaultIpTtl = 23,
  48. PathMtuAgingTimeout = 24,
  49. PathMtuPlateauTable = 25,
  50. InterfaceMtu = 26,
  51. AllSubnetAreLocal = 27,
  52. BroadcastAddress = 28,
  53. PerformMaskDiscovery = 29,
  54. MaskSupplier = 30,
  55. PerformRouterDiscovery = 31,
  56. RouterSolicitationAddress = 32,
  57. StaticRoute = 33,
  58. TrailerEncapsulation = 34,
  59. ArpCacheTimeout = 35,
  60. EthernetEncapsulation = 36,
  61. TcpDefaultTtl = 37,
  62. TcpKeepAliveInterval = 38,
  63. TcpKeepAliveGarbage = 39,
  64. NetworkInformationServiceDomain = 40,
  65. NetworkInformationServers = 41,
  66. NetworkTimeProtocolServers = 42,
  67. VendorSpecificInformation = 43,
  68. NetBiosOverTcpIpNameServer = 44,
  69. NetBiosOverTcpIpDatagramDistributionServer = 45,
  70. NetBiosOverTcpIpNodeType = 46,
  71. NetBiosOverTcpIpScope = 47,
  72. XWindowSystemFontServer = 48,
  73. XWindowSystemDisplayManager = 49,
  74. RequestedIpAddress = 50,
  75. IpAddressLeaseTime = 51,
  76. OptionOverload = 52,
  77. DhcpMessageType = 53,
  78. ServerIdentifier = 54,
  79. ParameterRequestList = 55,
  80. Message = 56,
  81. MaximumDhcpMessageSize = 57,
  82. RenewalTimeValue = 58,
  83. RebindingTimeValue = 59,
  84. VendorClassIdentifier = 60,
  85. ClientIdentifier = 61,
  86. NetworkInformationServicePlusDomain = 64,
  87. NetworkInformationServicePlusServers = 65,
  88. TftpServerName = 66,
  89. BootfileName = 67,
  90. MobileIpHomeAgent = 68,
  91. SmtpServer = 69,
  92. Pop3Server = 70,
  93. NntpServer = 71,
  94. DefaultWwwServer = 72,
  95. DefaultFingerServer = 73,
  96. DefaultIrc = 74,
  97. StreetTalkServer = 75,
  98. StreetTalkDirectoryAssistance = 76,
  99. ClientFullyQualifiedDomainName = 81,
  100. DomainSearch = 119,
  101. ClasslessStaticRoute = 121,
  102. CAPWAPAccessControllerAddresses = 138,
  103. TftpServerAddress = 150,
  104. End = 255
  105. }
  106. public class DhcpOption
  107. {
  108. #region variables
  109. readonly DhcpOptionCode _code;
  110. byte[] _value;
  111. #endregion
  112. #region constructor
  113. public DhcpOption(DhcpOptionCode code, string hexValue)
  114. {
  115. if (hexValue is null)
  116. throw new ArgumentNullException(nameof(hexValue));
  117. _code = code;
  118. if (hexValue.Contains(':'))
  119. _value = ParseColonHexString(hexValue);
  120. else
  121. _value = Convert.FromHexString(hexValue);
  122. }
  123. public DhcpOption(DhcpOptionCode code, byte[] value)
  124. {
  125. if (value is null)
  126. throw new ArgumentNullException(nameof(value));
  127. _code = code;
  128. _value = value;
  129. }
  130. protected DhcpOption(DhcpOptionCode code, Stream s)
  131. {
  132. _code = code;
  133. int len = s.ReadByte();
  134. if (len < 0)
  135. throw new EndOfStreamException();
  136. _value = s.ReadBytes(len);
  137. }
  138. protected DhcpOption(DhcpOptionCode code)
  139. {
  140. _code = code;
  141. }
  142. #endregion
  143. #region static
  144. public static DhcpOption CreateEndOption()
  145. {
  146. return new DhcpOption(DhcpOptionCode.End);
  147. }
  148. public static DhcpOption Parse(Stream s)
  149. {
  150. int code = s.ReadByte();
  151. if (code < 0)
  152. throw new EndOfStreamException();
  153. DhcpOptionCode optionCode = (DhcpOptionCode)code;
  154. switch (optionCode)
  155. {
  156. case DhcpOptionCode.SubnetMask:
  157. return new SubnetMaskOption(s);
  158. case DhcpOptionCode.Router:
  159. return new RouterOption(s);
  160. case DhcpOptionCode.DomainNameServer:
  161. return new DomainNameServerOption(s);
  162. case DhcpOptionCode.HostName:
  163. return new HostNameOption(s);
  164. case DhcpOptionCode.DomainName:
  165. return new DomainNameOption(s);
  166. case DhcpOptionCode.BroadcastAddress:
  167. return new BroadcastAddressOption(s);
  168. case DhcpOptionCode.VendorSpecificInformation:
  169. return new VendorSpecificInformationOption(s);
  170. case DhcpOptionCode.NetBiosOverTcpIpNameServer:
  171. return new NetBiosNameServerOption(s);
  172. case DhcpOptionCode.RequestedIpAddress:
  173. return new RequestedIpAddressOption(s);
  174. case DhcpOptionCode.IpAddressLeaseTime:
  175. return new IpAddressLeaseTimeOption(s);
  176. case DhcpOptionCode.OptionOverload:
  177. return new OptionOverloadOption(s);
  178. case DhcpOptionCode.DhcpMessageType:
  179. return new DhcpMessageTypeOption(s);
  180. case DhcpOptionCode.ServerIdentifier:
  181. return new ServerIdentifierOption(s);
  182. case DhcpOptionCode.ParameterRequestList:
  183. return new ParameterRequestListOption(s);
  184. case DhcpOptionCode.MaximumDhcpMessageSize:
  185. return new MaximumDhcpMessageSizeOption(s);
  186. case DhcpOptionCode.RenewalTimeValue:
  187. return new RenewalTimeValueOption(s);
  188. case DhcpOptionCode.RebindingTimeValue:
  189. return new RebindingTimeValueOption(s);
  190. case DhcpOptionCode.VendorClassIdentifier:
  191. return new VendorClassIdentifierOption(s);
  192. case DhcpOptionCode.ClientIdentifier:
  193. return new ClientIdentifierOption(s);
  194. case DhcpOptionCode.ClientFullyQualifiedDomainName:
  195. return new ClientFullyQualifiedDomainNameOption(s);
  196. case DhcpOptionCode.DomainSearch:
  197. return new DomainSearchOption(s);
  198. case DhcpOptionCode.ClasslessStaticRoute:
  199. return new ClasslessStaticRouteOption(s);
  200. case DhcpOptionCode.CAPWAPAccessControllerAddresses:
  201. return new CAPWAPAccessControllerOption(s);
  202. case DhcpOptionCode.TftpServerAddress:
  203. return new TftpServerAddressOption(s);
  204. case DhcpOptionCode.Pad:
  205. case DhcpOptionCode.End:
  206. return new DhcpOption(optionCode);
  207. default:
  208. //unknown option
  209. return new DhcpOption(optionCode, s);
  210. }
  211. }
  212. protected static byte[] ParseColonHexString(string value)
  213. {
  214. int i;
  215. int j = -1;
  216. string strHex;
  217. int b;
  218. using (MemoryStream mS = new MemoryStream())
  219. {
  220. while (true)
  221. {
  222. i = value.IndexOf(':', j + 1);
  223. if (i < 0)
  224. i = value.Length;
  225. strHex = value.Substring(j + 1, i - j - 1);
  226. if (!int.TryParse(strHex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out b) || (b < byte.MinValue) || (b > byte.MaxValue))
  227. throw new InvalidDataException("VendorSpecificInformation option data must be a colon (:) separated hex string.");
  228. mS.WriteByte((byte)b);
  229. if (i == value.Length)
  230. break;
  231. j = i;
  232. }
  233. return mS.ToArray();
  234. }
  235. }
  236. #endregion
  237. #region internal
  238. internal void AppendOptionValue(DhcpOption option)
  239. {
  240. byte[] value = new byte[_value.Length + option._value.Length];
  241. Buffer.BlockCopy(_value, 0, value, 0, _value.Length);
  242. Buffer.BlockCopy(option._value, 0, value, _value.Length, option._value.Length);
  243. _value = value;
  244. }
  245. internal void ParseOptionValue()
  246. {
  247. if (_value != null)
  248. {
  249. using (MemoryStream mS = new MemoryStream(_value))
  250. {
  251. ParseOptionValue(mS);
  252. }
  253. }
  254. }
  255. #endregion
  256. #region protected
  257. protected virtual void ParseOptionValue(Stream s)
  258. { }
  259. protected virtual void WriteOptionValue(Stream s)
  260. {
  261. if (_value == null)
  262. throw new NotImplementedException();
  263. s.Write(_value);
  264. }
  265. #endregion
  266. #region public
  267. public void WriteTo(Stream s)
  268. {
  269. switch (_code)
  270. {
  271. case DhcpOptionCode.Pad:
  272. case DhcpOptionCode.End:
  273. s.WriteByte((byte)_code);
  274. break;
  275. default:
  276. using (MemoryStream mS = new MemoryStream())
  277. {
  278. WriteOptionValue(mS);
  279. int len = 255;
  280. int valueLen = Convert.ToInt32(mS.Position);
  281. mS.Position = 0;
  282. do
  283. {
  284. if (valueLen < len)
  285. len = valueLen;
  286. //write option
  287. s.WriteByte((byte)_code); //code
  288. s.WriteByte((byte)len); //len
  289. mS.CopyTo(s, len, len); //value
  290. valueLen -= len;
  291. }
  292. while (valueLen > 0);
  293. }
  294. break;
  295. }
  296. }
  297. #endregion
  298. #region properties
  299. public DhcpOptionCode Code
  300. { get { return _code; } }
  301. public byte[] RawValue
  302. { get { return _value; } }
  303. #endregion
  304. }
  305. }