Lmhosts.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // This code is derived from jcifs smb client library <jcifs at samba dot org>
  2. // Ported by J. Arturo <webmaster at komodosoft dot net>
  3. //
  4. // This library is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU Lesser General Public
  6. // License as published by the Free Software Foundation; either
  7. // version 2.1 of the License, or (at your option) any later version.
  8. //
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public
  15. // License along with this library; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. using System.IO;
  18. using SharpCifs.Smb;
  19. using SharpCifs.Util;
  20. using SharpCifs.Util.Sharpen;
  21. namespace SharpCifs.Netbios
  22. {
  23. public class Lmhosts
  24. {
  25. private static readonly string Filename = Config.GetProperty("jcifs.netbios.lmhosts"
  26. );
  27. private static readonly Hashtable Tab = new Hashtable();
  28. private static long _lastModified = 1L;
  29. private static int _alt;
  30. private static LogStream _log = LogStream.GetInstance();
  31. /// <summary>
  32. /// This is really just for
  33. /// <see cref="SharpCifs.UniAddress">Jcifs.UniAddress</see>
  34. /// . It does
  35. /// not throw an
  36. /// <see cref="UnknownHostException">Sharpen.UnknownHostException</see>
  37. /// because this
  38. /// is queried frequently and exceptions would be rather costly to
  39. /// throw on a regular basis here.
  40. /// </summary>
  41. public static NbtAddress GetByName(string host)
  42. {
  43. lock (typeof(Lmhosts))
  44. {
  45. return GetByName(new Name(host, 0x20, null));
  46. }
  47. }
  48. internal static NbtAddress GetByName(Name name)
  49. {
  50. lock (typeof(Lmhosts))
  51. {
  52. NbtAddress result = null;
  53. try
  54. {
  55. if (Filename != null)
  56. {
  57. FilePath f = new FilePath(Filename);
  58. long lm;
  59. if ((lm = f.LastModified()) > _lastModified)
  60. {
  61. _lastModified = lm;
  62. Tab.Clear();
  63. _alt = 0;
  64. //path -> fileStream
  65. //Populate(new FileReader(f));
  66. Populate(new FileReader(new FileStream(f, FileMode.Open)));
  67. }
  68. result = (NbtAddress)Tab[name];
  69. }
  70. }
  71. catch (FileNotFoundException fnfe)
  72. {
  73. if (_log.Level > 1)
  74. {
  75. _log.WriteLine("lmhosts file: " + Filename);
  76. Runtime.PrintStackTrace(fnfe, _log);
  77. }
  78. }
  79. catch (IOException ioe)
  80. {
  81. if (_log.Level > 0)
  82. {
  83. Runtime.PrintStackTrace(ioe, _log);
  84. }
  85. }
  86. return result;
  87. }
  88. }
  89. /// <exception cref="System.IO.IOException"></exception>
  90. internal static void Populate(StreamReader r)
  91. {
  92. string line;
  93. BufferedReader br = new BufferedReader((InputStreamReader)r);
  94. while ((line = br.ReadLine()) != null)
  95. {
  96. line = line.ToUpper().Trim();
  97. if (line.Length == 0)
  98. {
  99. }
  100. else
  101. {
  102. if (line[0] == '#')
  103. {
  104. if (line.StartsWith("#INCLUDE "))
  105. {
  106. line = Runtime.Substring(line, line.IndexOf('\\'));
  107. string url = "smb:" + line.Replace('\\', '/');
  108. if (_alt > 0)
  109. {
  110. try
  111. {
  112. Populate(new InputStreamReader(new SmbFileInputStream(url)));
  113. }
  114. catch (IOException ioe)
  115. {
  116. _log.WriteLine("lmhosts URL: " + url);
  117. Runtime.PrintStackTrace(ioe, _log);
  118. continue;
  119. }
  120. _alt--;
  121. while ((line = br.ReadLine()) != null)
  122. {
  123. line = line.ToUpper().Trim();
  124. if (line.StartsWith("#END_ALTERNATE"))
  125. {
  126. break;
  127. }
  128. }
  129. }
  130. else
  131. {
  132. Populate(new InputStreamReader(new SmbFileInputStream(url)));
  133. }
  134. }
  135. else
  136. {
  137. if (line.StartsWith("#BEGIN_ALTERNATE"))
  138. {
  139. _alt++;
  140. }
  141. else
  142. {
  143. if (line.StartsWith("#END_ALTERNATE") && _alt > 0)
  144. {
  145. _alt--;
  146. throw new IOException("no lmhosts alternate includes loaded");
  147. }
  148. }
  149. }
  150. }
  151. else
  152. {
  153. if (char.IsDigit(line[0]))
  154. {
  155. char[] data = line.ToCharArray();
  156. int ip;
  157. int i;
  158. int j;
  159. Name name;
  160. NbtAddress addr;
  161. char c;
  162. c = '.';
  163. ip = i = 0;
  164. for (; i < data.Length && c == '.'; i++)
  165. {
  166. int b = unchecked(0x00);
  167. for (; i < data.Length && (c = data[i]) >= 48 && c <= 57; i++)
  168. {
  169. b = b * 10 + c - '0';
  170. }
  171. ip = (ip << 8) + b;
  172. }
  173. while (i < data.Length && char.IsWhiteSpace(data[i]))
  174. {
  175. i++;
  176. }
  177. j = i;
  178. while (j < data.Length && char.IsWhiteSpace(data[j]) == false)
  179. {
  180. j++;
  181. }
  182. name = new Name(Runtime.Substring(line, i, j), unchecked(0x20), null
  183. );
  184. addr = new NbtAddress(name, ip, false, NbtAddress.BNode, false, false, true, true
  185. , NbtAddress.UnknownMacAddress);
  186. Tab.Put(name, addr);
  187. }
  188. }
  189. }
  190. }
  191. }
  192. }
  193. }