Lmhosts.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. private static readonly Hashtable Tab = new Hashtable();
  27. private static long _lastModified = 1L;
  28. private static int _alt;
  29. private static LogStream _log = LogStream.GetInstance();
  30. /// <summary>
  31. /// This is really just for
  32. /// <see cref="SharpCifs.UniAddress">Jcifs.UniAddress</see>
  33. /// . It does
  34. /// not throw an
  35. /// <see cref="UnknownHostException">Sharpen.UnknownHostException</see>
  36. /// because this
  37. /// is queried frequently and exceptions would be rather costly to
  38. /// throw on a regular basis here.
  39. /// </summary>
  40. public static NbtAddress GetByName(string host)
  41. {
  42. lock (typeof(Lmhosts))
  43. {
  44. return GetByName(new Name(host, 0x20, null));
  45. }
  46. }
  47. internal static NbtAddress GetByName(Name name)
  48. {
  49. lock (typeof(Lmhosts))
  50. {
  51. NbtAddress result = null;
  52. try
  53. {
  54. if (Filename != null)
  55. {
  56. FilePath f = new FilePath(Filename);
  57. long lm;
  58. if ((lm = f.LastModified()) > _lastModified)
  59. {
  60. _lastModified = lm;
  61. Tab.Clear();
  62. _alt = 0;
  63. //path -> fileStream
  64. //Populate(new FileReader(f));
  65. Populate(new FileReader(new FileStream(f, FileMode.Open)));
  66. }
  67. result = (NbtAddress)Tab[name];
  68. }
  69. }
  70. catch (FileNotFoundException fnfe)
  71. {
  72. if (_log.Level > 1)
  73. {
  74. _log.WriteLine("lmhosts file: " + Filename);
  75. Runtime.PrintStackTrace(fnfe, _log);
  76. }
  77. }
  78. catch (IOException ioe)
  79. {
  80. if (_log.Level > 0)
  81. {
  82. Runtime.PrintStackTrace(ioe, _log);
  83. }
  84. }
  85. return result;
  86. }
  87. }
  88. /// <exception cref="System.IO.IOException"></exception>
  89. internal static void Populate(StreamReader r)
  90. {
  91. string line;
  92. BufferedReader br = new BufferedReader((InputStreamReader)r);
  93. while ((line = br.ReadLine()) != null)
  94. {
  95. line = line.ToUpper().Trim();
  96. if (line.Length == 0)
  97. {
  98. }
  99. else
  100. {
  101. if (line[0] == '#')
  102. {
  103. if (line.StartsWith("#INCLUDE "))
  104. {
  105. line = Runtime.Substring(line, line.IndexOf('\\'));
  106. string url = "smb:" + line.Replace('\\', '/');
  107. if (_alt > 0)
  108. {
  109. try
  110. {
  111. Populate(new InputStreamReader(new SmbFileInputStream(url)));
  112. }
  113. catch (IOException ioe)
  114. {
  115. _log.WriteLine("lmhosts URL: " + url);
  116. Runtime.PrintStackTrace(ioe, _log);
  117. continue;
  118. }
  119. _alt--;
  120. while ((line = br.ReadLine()) != null)
  121. {
  122. line = line.ToUpper().Trim();
  123. if (line.StartsWith("#END_ALTERNATE"))
  124. {
  125. break;
  126. }
  127. }
  128. }
  129. else
  130. {
  131. Populate(new InputStreamReader(new SmbFileInputStream(url)));
  132. }
  133. }
  134. else
  135. {
  136. if (line.StartsWith("#BEGIN_ALTERNATE"))
  137. {
  138. _alt++;
  139. }
  140. else
  141. {
  142. if (line.StartsWith("#END_ALTERNATE") && _alt > 0)
  143. {
  144. _alt--;
  145. throw new IOException("no lmhosts alternate includes loaded");
  146. }
  147. }
  148. }
  149. }
  150. else
  151. {
  152. if (char.IsDigit(line[0]))
  153. {
  154. char[] data = line.ToCharArray();
  155. int ip;
  156. int i;
  157. int j;
  158. Name name;
  159. NbtAddress addr;
  160. char c;
  161. c = '.';
  162. ip = i = 0;
  163. for (; i < data.Length && c == '.'; i++)
  164. {
  165. int b = unchecked(0x00);
  166. for (; i < data.Length && (c = data[i]) >= 48 && c <= 57; i++)
  167. {
  168. b = b * 10 + c - '0';
  169. }
  170. ip = (ip << 8) + b;
  171. }
  172. while (i < data.Length && char.IsWhiteSpace(data[i]))
  173. {
  174. i++;
  175. }
  176. j = i;
  177. while (j < data.Length && char.IsWhiteSpace(data[j]) == false)
  178. {
  179. j++;
  180. }
  181. name = new Name(Runtime.Substring(line, i, j), unchecked(0x20), null
  182. );
  183. addr = new NbtAddress(name, ip, false, NbtAddress.BNode, false, false, true, true
  184. , NbtAddress.UnknownMacAddress);
  185. Tab.Put(name, addr);
  186. }
  187. }
  188. }
  189. }
  190. }
  191. }
  192. }