jellyfin-server/Emby.Server.Implementations/IO/SharpCifs/Netbios/Lmhosts.cs

203 lines
5.0 KiB
C#
Raw Normal View History

2017-04-02 00:36:06 +00:00
// This code is derived from jcifs smb client library <jcifs at samba dot org>
// Ported by J. Arturo <webmaster at komodosoft dot net>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System.IO;
using SharpCifs.Smb;
using SharpCifs.Util;
using SharpCifs.Util.Sharpen;
namespace SharpCifs.Netbios
{
2017-07-08 03:12:21 +00:00
public class Lmhosts
{
private static readonly string Filename = Config.GetProperty("jcifs.netbios.lmhosts"
);
2017-04-02 00:36:06 +00:00
2017-07-08 03:12:21 +00:00
private static readonly Hashtable Tab = new Hashtable();
2017-04-02 00:36:06 +00:00
2017-07-08 03:12:21 +00:00
private static long _lastModified = 1L;
2017-04-02 00:36:06 +00:00
2017-07-08 03:12:21 +00:00
private static int _alt;
2017-04-02 00:36:06 +00:00
2017-07-08 03:12:21 +00:00
private static LogStream _log = LogStream.GetInstance();
2017-04-02 00:36:06 +00:00
2017-07-08 03:12:21 +00:00
/// <summary>
/// This is really just for
/// <see cref="SharpCifs.UniAddress">Jcifs.UniAddress</see>
/// . It does
/// not throw an
/// <see cref="UnknownHostException">Sharpen.UnknownHostException</see>
/// because this
/// is queried frequently and exceptions would be rather costly to
/// throw on a regular basis here.
/// </summary>
public static NbtAddress GetByName(string host)
{
lock (typeof(Lmhosts))
{
return GetByName(new Name(host, 0x20, null));
}
}
2017-04-02 00:36:06 +00:00
2017-07-08 03:12:21 +00:00
internal static NbtAddress GetByName(Name name)
{
lock (typeof(Lmhosts))
{
NbtAddress result = null;
try
{
if (Filename != null)
{
FilePath f = new FilePath(Filename);
long lm;
if ((lm = f.LastModified()) > _lastModified)
{
_lastModified = lm;
Tab.Clear();
_alt = 0;
//path -> fileStream
//Populate(new FileReader(f));
2017-04-02 00:36:06 +00:00
Populate(new FileReader(new FileStream(f, FileMode.Open)));
2017-07-08 03:12:21 +00:00
}
result = (NbtAddress)Tab[name];
}
}
catch (FileNotFoundException fnfe)
{
if (_log.Level > 1)
{
_log.WriteLine("lmhosts file: " + Filename);
Runtime.PrintStackTrace(fnfe, _log);
}
}
catch (IOException ioe)
{
if (_log.Level > 0)
{
Runtime.PrintStackTrace(ioe, _log);
}
}
return result;
}
}
2017-04-02 00:36:06 +00:00
2017-07-08 03:12:21 +00:00
/// <exception cref="System.IO.IOException"></exception>
internal static void Populate(StreamReader r)
{
string line;
2017-04-02 00:36:06 +00:00
BufferedReader br = new BufferedReader((InputStreamReader)r);
2017-07-08 03:12:21 +00:00
while ((line = br.ReadLine()) != null)
{
line = line.ToUpper().Trim();
if (line.Length == 0)
{
}
else
{
if (line[0] == '#')
{
if (line.StartsWith("#INCLUDE "))
{
line = Runtime.Substring(line, line.IndexOf('\\'));
string url = "smb:" + line.Replace('\\', '/');
if (_alt > 0)
{
try
{
Populate(new InputStreamReader(new SmbFileInputStream(url)));
}
catch (IOException ioe)
{
_log.WriteLine("lmhosts URL: " + url);
Runtime.PrintStackTrace(ioe, _log);
continue;
}
_alt--;
while ((line = br.ReadLine()) != null)
{
line = line.ToUpper().Trim();
if (line.StartsWith("#END_ALTERNATE"))
{
break;
}
}
}
else
{
Populate(new InputStreamReader(new SmbFileInputStream(url)));
}
}
else
{
if (line.StartsWith("#BEGIN_ALTERNATE"))
{
_alt++;
}
else
{
if (line.StartsWith("#END_ALTERNATE") && _alt > 0)
{
_alt--;
throw new IOException("no lmhosts alternate includes loaded");
}
}
}
}
else
{
if (char.IsDigit(line[0]))
{
char[] data = line.ToCharArray();
int ip;
int i;
int j;
Name name;
NbtAddress addr;
char c;
c = '.';
ip = i = 0;
for (; i < data.Length && c == '.'; i++)
{
int b = unchecked(0x00);
for (; i < data.Length && (c = data[i]) >= 48 && c <= 57; i++)
{
b = b * 10 + c - '0';
}
ip = (ip << 8) + b;
}
while (i < data.Length && char.IsWhiteSpace(data[i]))
{
i++;
}
j = i;
while (j < data.Length && char.IsWhiteSpace(data[j]) == false)
{
j++;
}
name = new Name(Runtime.Substring(line, i, j), unchecked(0x20), null
);
addr = new NbtAddress(name, ip, false, NbtAddress.BNode, false, false, true, true
, NbtAddress.UnknownMacAddress);
Tab.Put(name, addr);
}
}
}
}
}
}
2017-04-02 00:36:06 +00:00
}