DhcpOption.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2023 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.IO;
  18. using TechnitiumLibrary;
  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 = hexValue.ParseColonHexString();
  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. #endregion
  213. #region internal
  214. internal void AppendOptionValue(DhcpOption option)
  215. {
  216. byte[] value = new byte[_value.Length + option._value.Length];
  217. Buffer.BlockCopy(_value, 0, value, 0, _value.Length);
  218. Buffer.BlockCopy(option._value, 0, value, _value.Length, option._value.Length);
  219. _value = value;
  220. }
  221. internal void ParseOptionValue()
  222. {
  223. if (_value != null)
  224. {
  225. using (MemoryStream mS = new MemoryStream(_value))
  226. {
  227. ParseOptionValue(mS);
  228. }
  229. }
  230. }
  231. #endregion
  232. #region protected
  233. protected virtual void ParseOptionValue(Stream s)
  234. { }
  235. protected virtual void WriteOptionValue(Stream s)
  236. {
  237. if (_value == null)
  238. throw new NotImplementedException();
  239. s.Write(_value);
  240. }
  241. #endregion
  242. #region public
  243. public void WriteTo(Stream s)
  244. {
  245. switch (_code)
  246. {
  247. case DhcpOptionCode.Pad:
  248. case DhcpOptionCode.End:
  249. s.WriteByte((byte)_code);
  250. break;
  251. default:
  252. using (MemoryStream mS = new MemoryStream())
  253. {
  254. WriteOptionValue(mS);
  255. int len = 255;
  256. int valueLen = Convert.ToInt32(mS.Position);
  257. mS.Position = 0;
  258. do
  259. {
  260. if (valueLen < len)
  261. len = valueLen;
  262. //write option
  263. s.WriteByte((byte)_code); //code
  264. s.WriteByte((byte)len); //len
  265. mS.CopyTo(s, len, len); //value
  266. valueLen -= len;
  267. }
  268. while (valueLen > 0);
  269. }
  270. break;
  271. }
  272. }
  273. #endregion
  274. #region properties
  275. public DhcpOptionCode Code
  276. { get { return _code; } }
  277. public byte[] RawValue
  278. { get { return _value; } }
  279. #endregion
  280. }
  281. }