示例#1
0
        /// <exception cref="System.IO.IOException"></exception>
        internal virtual void EnsureOpen(int timeout)
        {
            //Log.Out($"NameServiceClient.EnsureOpen");

            _closeTimeout = 0;
            if (SoTimeout != 0)
            {
                _closeTimeout = Math.Max(SoTimeout, timeout);
            }

            var localPort = (SmbConstants.Lport == 0) ? _lport : SmbConstants.Lport;

            // If socket is still good, the new closeTimeout will
            // be ignored; see tryClose comment.
            if (
                _socketSender == null ||
                _socketSender.LocalEndPoint == null ||
                _socketSender.GetLocalPort() != localPort ||
                !IPAddress.Any.Equals(_socketSender.GetLocalInetAddress())
                )
            {
                if (_socketSender != null)
                {
                    _socketSender.Dispose();
                    _socketSender = null;
                }

                _socketSender = new SocketEx(AddressFamily.InterNetwork,
                                             SocketType.Dgram,
                                             ProtocolType.Udp);

                _socketSender.Bind(new IPEndPoint(IPAddress.Any, localPort));


                if (_waitResponse)
                {
                    if (_thread != null)
                    {
                        _thread.Cancel(true);
                        _thread.Dispose();
                    }

                    _thread = new Thread(this);
                    _thread.SetDaemon(true);
                    _thread.Start(true);
                }
            }
        }
示例#2
0
        /// <exception cref="System.IO.IOException"></exception>
        protected internal override void DoDisconnect(bool hard)
        {
            try
            {
                if (Sessions != null)
                {
                    foreach (var ssn in Sessions)
                    {
                        ssn?.Logoff(hard);
                    }
                }

                Out?.Close();
                In?.Close();

                //Socket.`Close` method deleted
                //Socket.Close();
                Socket?.Shutdown(SocketShutdown.Both);
                Socket?.Dispose();
            }
            finally
            {
                Digest       = null;
                Socket       = null;
                TconHostName = null;
            }
        }
示例#3
0
        /// <exception cref="System.IO.IOException"></exception>
        protected internal override void DoDisconnect(bool hard)
        {
            try
            {
                foreach (var ssn in Sessions)
                {
                    ssn.Logoff(hard);
                }

                Out.Close();
                In.Close();
                Socket.Dispose();
            }
            finally
            {
                Digest       = null;
                Socket       = null;
                TconHostName = null;
            }
        }
示例#4
0
        internal virtual void TryClose()
        {
            lock (_lock)
            {
                if (_socket != null)
                {
                    _socket.Dispose();
                    _socket = null;
                }
                _thread = null;

                if (_waitResponse)
                {
                    _responseTable.Clear();
                }
                else
                {
                    _autoResetWaitReceive.Set();
                }
            }
        }
示例#5
0
        /// <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);

                //TCPローカルポートは、毎回空いているものを使う。
                //https://blogs.msdn.microsoft.com/dgorti/2005/09/18/only-one-usage-of-each-socket-address-protocolnetwork-addressport-is-normally-permitted/
                Socket.Bind(new IPEndPoint(LocalAddr, 0));

                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.`Close` method deleted
                        //Socket.Close();
                        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.`Close` method deleted
                        //Socket.Close();
                        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);
        }
示例#6
0
        /// <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(Address.GetHostAddress(), 139);
                //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);
        }