DhcpOption.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2024 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. ArgumentNullException.ThrowIfNull(hexValue);
  116. _code = code;
  117. if (hexValue.Contains(':'))
  118. _value = hexValue.ParseColonHexString();
  119. else
  120. _value = Convert.FromHexString(hexValue);
  121. }
  122. public DhcpOption(DhcpOptionCode code, byte[] value)
  123. {
  124. ArgumentNullException.ThrowIfNull(value);
  125. _code = code;
  126. _value = value;
  127. }
  128. protected DhcpOption(DhcpOptionCode code, Stream s)
  129. {
  130. _code = code;
  131. int len = s.ReadByte();
  132. if (len < 0)
  133. throw new EndOfStreamException();
  134. _value = s.ReadExactly(len);
  135. }
  136. protected DhcpOption(DhcpOptionCode code)
  137. {
  138. _code = code;
  139. }
  140. #endregion
  141. #region static
  142. public static DhcpOption CreateEndOption()
  143. {
  144. return new DhcpOption(DhcpOptionCode.End);
  145. }
  146. public static DhcpOption Parse(Stream s)
  147. {
  148. int code = s.ReadByte();
  149. if (code < 0)
  150. throw new EndOfStreamException();
  151. DhcpOptionCode optionCode = (DhcpOptionCode)code;
  152. switch (optionCode)
  153. {
  154. case DhcpOptionCode.SubnetMask:
  155. return new SubnetMaskOption(s);
  156. case DhcpOptionCode.Router:
  157. return new RouterOption(s);
  158. case DhcpOptionCode.DomainNameServer:
  159. return new DomainNameServerOption(s);
  160. case DhcpOptionCode.HostName:
  161. return new HostNameOption(s);
  162. case DhcpOptionCode.DomainName:
  163. return new DomainNameOption(s);
  164. case DhcpOptionCode.BroadcastAddress:
  165. return new BroadcastAddressOption(s);
  166. case DhcpOptionCode.VendorSpecificInformation:
  167. return new VendorSpecificInformationOption(s);
  168. case DhcpOptionCode.NetBiosOverTcpIpNameServer:
  169. return new NetBiosNameServerOption(s);
  170. case DhcpOptionCode.RequestedIpAddress:
  171. return new RequestedIpAddressOption(s);
  172. case DhcpOptionCode.IpAddressLeaseTime:
  173. return new IpAddressLeaseTimeOption(s);
  174. case DhcpOptionCode.OptionOverload:
  175. return new OptionOverloadOption(s);
  176. case DhcpOptionCode.DhcpMessageType:
  177. return new DhcpMessageTypeOption(s);
  178. case DhcpOptionCode.ServerIdentifier:
  179. return new ServerIdentifierOption(s);
  180. case DhcpOptionCode.ParameterRequestList:
  181. return new ParameterRequestListOption(s);
  182. case DhcpOptionCode.MaximumDhcpMessageSize:
  183. return new MaximumDhcpMessageSizeOption(s);
  184. case DhcpOptionCode.RenewalTimeValue:
  185. return new RenewalTimeValueOption(s);
  186. case DhcpOptionCode.RebindingTimeValue:
  187. return new RebindingTimeValueOption(s);
  188. case DhcpOptionCode.VendorClassIdentifier:
  189. return new VendorClassIdentifierOption(s);
  190. case DhcpOptionCode.ClientIdentifier:
  191. return new ClientIdentifierOption(s);
  192. case DhcpOptionCode.ClientFullyQualifiedDomainName:
  193. return new ClientFullyQualifiedDomainNameOption(s);
  194. case DhcpOptionCode.DomainSearch:
  195. return new DomainSearchOption(s);
  196. case DhcpOptionCode.ClasslessStaticRoute:
  197. return new ClasslessStaticRouteOption(s);
  198. case DhcpOptionCode.CAPWAPAccessControllerAddresses:
  199. return new CAPWAPAccessControllerOption(s);
  200. case DhcpOptionCode.TftpServerAddress:
  201. return new TftpServerAddressOption(s);
  202. case DhcpOptionCode.Pad:
  203. case DhcpOptionCode.End:
  204. return new DhcpOption(optionCode);
  205. default:
  206. //unknown option
  207. return new DhcpOption(optionCode, s);
  208. }
  209. }
  210. #endregion
  211. #region internal
  212. internal void AppendOptionValue(DhcpOption option)
  213. {
  214. byte[] value = new byte[_value.Length + option._value.Length];
  215. Buffer.BlockCopy(_value, 0, value, 0, _value.Length);
  216. Buffer.BlockCopy(option._value, 0, value, _value.Length, option._value.Length);
  217. _value = value;
  218. }
  219. internal void ParseOptionValue()
  220. {
  221. if (_value != null)
  222. {
  223. using (MemoryStream mS = new MemoryStream(_value))
  224. {
  225. ParseOptionValue(mS);
  226. }
  227. }
  228. }
  229. #endregion
  230. #region protected
  231. protected virtual void ParseOptionValue(Stream s)
  232. { }
  233. protected virtual void WriteOptionValue(Stream s)
  234. {
  235. if (_value == null)
  236. throw new NotImplementedException();
  237. s.Write(_value);
  238. }
  239. #endregion
  240. #region public
  241. public void WriteTo(Stream s)
  242. {
  243. switch (_code)
  244. {
  245. case DhcpOptionCode.Pad:
  246. case DhcpOptionCode.End:
  247. s.WriteByte((byte)_code);
  248. break;
  249. default:
  250. using (MemoryStream mS = new MemoryStream())
  251. {
  252. WriteOptionValue(mS);
  253. int len = 255;
  254. int valueLen = Convert.ToInt32(mS.Position);
  255. mS.Position = 0;
  256. do
  257. {
  258. if (valueLen < len)
  259. len = valueLen;
  260. //write option
  261. s.WriteByte((byte)_code); //code
  262. s.WriteByte((byte)len); //len
  263. mS.CopyTo(s, len, len); //value
  264. valueLen -= len;
  265. }
  266. while (valueLen > 0);
  267. }
  268. break;
  269. }
  270. }
  271. #endregion
  272. #region properties
  273. public DhcpOptionCode Code
  274. { get { return _code; } }
  275. public byte[] RawValue
  276. { get { return _value; } }
  277. #endregion
  278. }
  279. }