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; Populate(new FileReader(f)); } 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; } }
internal virtual int ReadResourceRecordWireFormat(byte[] src, int srcIndex) { int start = srcIndex; int end; if ((src[srcIndex] & unchecked(0xC0)) == unchecked(0xC0)) { RecordName = QuestionName; // label string pointer to questionName srcIndex += 2; } else { srcIndex += RecordName.ReadWireFormat(src, srcIndex); } RecordType = ReadInt2(src, srcIndex); srcIndex += 2; RecordClass = ReadInt2(src, srcIndex); srcIndex += 2; Ttl = ReadInt4(src, srcIndex); srcIndex += 4; RDataLength = ReadInt2(src, srcIndex); srcIndex += 2; AddrEntry = new NbtAddress[RDataLength / 6]; end = srcIndex + RDataLength; for (AddrIndex = 0; srcIndex < end; AddrIndex++) { srcIndex += ReadRDataWireFormat(src, srcIndex); } return srcIndex - start; }
internal NodeStatusResponse(NbtAddress queryAddress) { this._queryAddress = queryAddress; RecordName = new Name(); _macAddress = new byte[6]; }
/// <exception cref="System.IO.IOException"></exception> internal virtual void Ssn139() { Name calledName = new Name(Address.FirstCalledName(), 0x20, null ); do { Socket = new SocketEx(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); if (LocalAddr != null) { Socket.Bind2(new IPEndPoint(LocalAddr, LocalPort)); } Socket.Connect(new IPEndPoint(IPAddress.Parse(Address.GetHostAddress()), 139), SmbConstants.ConnTimeout); Socket.SoTimeOut = SmbConstants.SoTimeout; Out = Socket.GetOutputStream(); In = Socket.GetInputStream(); SessionServicePacket ssp = new SessionRequestPacket(calledName, NbtAddress.GetLocalName ()); Out.Write(Sbuf, 0, ssp.WriteWireFormat(Sbuf, 0)); if (Readn(In, Sbuf, 0, 4) < 4) { try { Socket.Dispose(); } catch (IOException) { } throw new SmbException("EOF during NetBIOS session request"); } switch (Sbuf[0] & 0xFF) { case SessionServicePacket.PositiveSessionResponse: { if (Log.Level >= 4) { Log.WriteLine("session established ok with " + Address); } return; } case SessionServicePacket.NegativeSessionResponse: { int errorCode = In.Read() & 0xFF; switch (errorCode) { case NbtException.CalledNotPresent: case NbtException.NotListeningCalled: { Socket.Dispose(); break; } default: { Disconnect(true); throw new NbtException(NbtException.ErrSsnSrvc, errorCode); } } break; } case -1: { Disconnect(true); throw new NbtException(NbtException.ErrSsnSrvc, NbtException.ConnectionRefused ); } default: { Disconnect(true); throw new NbtException(NbtException.ErrSsnSrvc, 0); } } } while ((calledName.name = Address.NextCalledName()) != null); throw new IOException("Failed to establish session with " + Address); }
public SessionRequestPacket(Name calledName, Name callingName) { Type = SessionRequest; this._calledName = calledName; this._callingName = callingName; }
public SessionRequestPacket() { _calledName = new Name(); _callingName = new Name(); }
static NbtAddress() { IPAddress localInetAddress; string localHostname; Name localName; AddressCache.Put(UnknownName, new CacheEntry(UnknownName, UnknownAddress , Forever)); localInetAddress = Client.laddr; if (localInetAddress == null) { try { localInetAddress = Extensions.GetAddressByName("127.0.0.1"); } catch (UnknownHostException) { } } localHostname = Config.GetProperty("jcifs.netbios.hostname", null); if (string.IsNullOrEmpty(localHostname)) { byte[] addr = localInetAddress.GetAddressBytes(); /*localHostname = "JCIFS" + (addr[2] & unchecked((int)(0xFF))) + "_" + (addr[3] & unchecked( (int)(0xFF))) + "_" + Hexdump.ToHexString((int)(new Random().NextDouble() * (double)unchecked( (int)(0xFF))), 2);*/ localHostname = "JCIFS_127_0_0_1"; } localName = new Name(localHostname, unchecked(0x00), Config.GetProperty("jcifs.netbios.scope" , null)); Localhost = new NbtAddress(localName, localInetAddress.GetHashCode(), false, BNode , false, false, true, false, UnknownMacAddress); CacheAddress(localName, Localhost, Forever); }
private static void UpdateLookupTable(Name name) { lock (LookupTable) { //Sharpen.Collections.Remove(LOOKUP_TABLE, name); LookupTable.Remove(name); Runtime.NotifyAll(LookupTable); } }
/// <exception cref="UnknownHostException"></exception> internal static NbtAddress DoNameQuery(Name name, IPAddress svr) { NbtAddress addr; if (name.HexCode == unchecked(0x1d) && svr == null) { svr = Client.Baddr; } // bit of a hack but saves a lookup name.SrcHashCode = svr != null ? svr.GetHashCode() : 0; addr = GetCachedAddress(name); if (addr == null) { if ((addr = (NbtAddress)CheckLookupTable(name)) == null) { try { addr = Client.GetByName(name, svr); } catch (UnknownHostException) { addr = UnknownAddress; } finally { CacheAddress(name, addr); UpdateLookupTable(name); } } } if (addr == UnknownAddress) { throw new UnknownHostException(name.ToString()); } return addr; }
private static object CheckLookupTable(Name name) { object obj; lock (LookupTable) { if (LookupTable.ContainsKey(name) == false) { LookupTable.Put(name, name); return null; } while (LookupTable.ContainsKey(name)) { try { Runtime.Wait(LookupTable); } catch (Exception) { } } } obj = GetCachedAddress(name); if (obj == null) { lock (LookupTable) { LookupTable.Put(name, name); } } return obj; }
internal static NbtAddress GetCachedAddress(Name hostName) { if (CachePolicy == 0) { return null; } lock (AddressCache) { CacheEntry entry = (CacheEntry)AddressCache.Get(hostName); if (entry != null && entry.Expiration < Runtime.CurrentTimeMillis() && entry.Expiration >= 0) { entry = null; } return entry != null ? entry.Address : null; } }
internal static void CacheAddress(Name hostName, NbtAddress addr, long expiration ) { if (CachePolicy == 0) { return; } lock (AddressCache) { CacheEntry entry = (CacheEntry)AddressCache.Get(hostName); if (entry == null) { entry = new CacheEntry(hostName, addr, expiration); AddressCache.Put(hostName, entry); } else { entry.Address = addr; entry.Expiration = expiration; } } }
internal static void CacheAddress(Name hostName, NbtAddress addr) { if (CachePolicy == 0) { return; } long expiration = -1; if (CachePolicy != Forever) { expiration = Runtime.CurrentTimeMillis() + CachePolicy * 1000; } CacheAddress(hostName, addr, expiration); }
/// <exception cref="UnknownHostException"></exception> internal virtual NbtAddress[] GetAllByName(Name name, IPAddress addr) { int n; NameQueryRequest request = new NameQueryRequest(name); NameQueryResponse response = new NameQueryResponse(); request.Addr = addr ?? NbtAddress.GetWinsAddress(); request.IsBroadcast = request.Addr == null; if (request.IsBroadcast) { request.Addr = Baddr; n = RetryCount; } else { request.IsBroadcast = false; n = 1; } do { try { Send(request, response, RetryTimeout); } catch (IOException ioe) { if (_log.Level > 1) { Runtime.PrintStackTrace(ioe, _log); } throw new UnknownHostException(ioe); } if (response.Received && response.ResultCode == 0) { return response.AddrEntry; } } while (--n > 0 && request.IsBroadcast); throw new UnknownHostException(); }
internal NbtAddress(Name hostName, int address, bool groupName, int nodeType) { this.HostName = hostName; this.Address = address; this.GroupName = groupName; this.NodeType = nodeType; }
/// <exception cref="UnknownHostException"></exception> internal virtual NbtAddress GetByName(Name name, IPAddress addr) { int n; NameQueryRequest request = new NameQueryRequest(name); NameQueryResponse response = new NameQueryResponse(); if (addr != null) { request.Addr = addr; request.IsBroadcast = (addr.GetAddressBytes()[3] == unchecked(unchecked(0xFF))); n = RetryCount; do { try { Send(request, response, RetryTimeout); } catch (IOException ioe) { if (_log.Level > 1) { Runtime.PrintStackTrace(ioe, _log); } throw new UnknownHostException(ioe); } if (response.Received && response.ResultCode == 0 && response.IsResponse) { int last = response.AddrEntry.Length - 1; response.AddrEntry[last].HostName.SrcHashCode = addr.GetHashCode(); return response.AddrEntry[last]; } } while (--n > 0 && request.IsBroadcast); throw new UnknownHostException(); } for (int i = 0; i < _resolveOrder.Length; i++) { try { switch (_resolveOrder[i]) { case ResolverLmhosts: { NbtAddress ans = Lmhosts.GetByName(name); if (ans != null) { ans.HostName.SrcHashCode = 0; // just has to be different // from other methods return ans; } break; } case ResolverWins: case ResolverBcast: { if (_resolveOrder[i] == ResolverWins && name.name != NbtAddress.MasterBrowserName && name.HexCode != unchecked(0x1d)) { request.Addr = NbtAddress.GetWinsAddress(); request.IsBroadcast = false; } else { request.Addr = Baddr; request.IsBroadcast = true; } n = RetryCount; while (n-- > 0) { try { Send(request, response, RetryTimeout); } catch (IOException ioe) { if (_log.Level > 1) { Runtime.PrintStackTrace(ioe, _log); } throw new UnknownHostException(ioe); } if (response.Received && response.ResultCode == 0 && response.IsResponse) { response.AddrEntry[0].HostName.SrcHashCode = request.Addr.GetHashCode(); return response.AddrEntry[0]; } if (_resolveOrder[i] == ResolverWins) { break; } } break; } } } catch (IOException) { } } throw new UnknownHostException(); }
internal NbtAddress(Name hostName, int address, bool groupName, int nodeType, bool isBeingDeleted, bool isInConflict, bool isActive, bool isPermanent, byte[] macAddress ) { this.HostName = hostName; this.Address = address; this.GroupName = groupName; this.NodeType = nodeType; this.isBeingDeleted = isBeingDeleted; this.isInConflict = isInConflict; this.isActive = isActive; this.isPermanent = isPermanent; this.MacAddress = macAddress; IsDataFromNodeStatus = true; }
/// <exception cref="System.IO.IOException"></exception> internal static void Populate(StreamReader r) { string line; BufferedReader br = new BufferedReader((InputStreamReader)r); 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); } } } } }
internal CacheEntry(Name hostName, NbtAddress address, long expiration) { this.HostName = hostName; this.Address = address; this.Expiration = expiration; }