示例#1
0
        /// <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();
        }
示例#2
0
        /// <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();
        }
示例#3
0
        /// <exception cref="System.IO.IOException"></exception>
        internal virtual void Send(NameServicePacket request, NameServicePacket response,
                                   int timeout)
        {
            int nid = 0;
            int max = NbtAddress.Nbns.Length;

            if (max == 0)
            {
                max = 1;
            }

            lock (response)
            {
                while (max-- > 0)
                {
                    try
                    {
                        lock (_lock)
                        {
                            request.NameTrnId = GetNextNameTrnId();
                            nid = request.NameTrnId;
                            response.Received = false;
                            _responseTable.Put(nid, response);
                            EnsureOpen(timeout + 1000);

                            int requestLenght = request.WriteWireFormat(_sndBuf, 0);
                            //NEW SOCKET
                            _socket.Send(_sndBuf, 0, requestLenght, SocketFlags.None);
                            //_socket.Send(_sndBuf, 0, requestLenght, new IPEndPoint(request.Addr, _lport));
                            if (_log.Level > 3)
                            {
                                _log.WriteLine(request);
                                Hexdump.ToHexdump(_log, _sndBuf, 0, requestLenght);
                            }
                        }
                        if (_waitResponse)
                        {
                            long start = Runtime.CurrentTimeMillis();
                            while (timeout > 0)
                            {
                                Runtime.Wait(response, timeout);
                                if (response.Received && request.QuestionType == response.RecordType)
                                {
                                    return;
                                }
                                response.Received = false;
                                timeout          -= (int)(Runtime.CurrentTimeMillis() - start);
                            }
                        }
                    }
                    catch (Exception ie)
                    {
                        throw new IOException(ie.Message);
                    }
                    finally
                    {
                        //Sharpen.Collections.Remove(responseTable, nid);
                        if (_waitResponse)
                        {
                            _responseTable.Remove(nid);
                        }
                    }
                    if (_waitResponse)
                    {
                        lock (_lock)
                        {
                            if (NbtAddress.IsWins(request.Addr) == false)
                            {
                                break;
                            }
                            if (request.Addr == NbtAddress.GetWinsAddress())
                            {
                                NbtAddress.SwitchWins();
                            }
                            request.Addr = NbtAddress.GetWinsAddress();
                        }
                    }
                }
            }
        }
示例#4
0
        internal NameServiceClient(int lport, IPAddress laddr)
        {
            this._lport = lport;
            this.laddr  = laddr;

            try
            {
                Baddr = Config.GetInetAddress("jcifs.netbios.baddr", Extensions.GetAddressByName("255.255.255.255"));
            }
            catch (Exception e)
            {
                LogStream.GetInstance().WriteLine(e);
            }

            _sndBuf = new byte[SndBufSize];
            _rcvBuf = new byte[RcvBufSize];


            if (string.IsNullOrEmpty(Ro))
            {
                if (NbtAddress.GetWinsAddress() == null)
                {
                    _resolveOrder    = new int[2];
                    _resolveOrder[0] = ResolverLmhosts;
                    _resolveOrder[1] = ResolverBcast;
                }
                else
                {
                    _resolveOrder    = new int[3];
                    _resolveOrder[0] = ResolverLmhosts;
                    _resolveOrder[1] = ResolverWins;
                    _resolveOrder[2] = ResolverBcast;
                }
            }
            else
            {
                int[]           tmp = new int[3];
                StringTokenizer st  = new StringTokenizer(Ro, ",");
                int             i   = 0;
                while (st.HasMoreTokens())
                {
                    string s = st.NextToken().Trim();
                    if (Runtime.EqualsIgnoreCase(s, "LMHOSTS"))
                    {
                        tmp[i++] = ResolverLmhosts;
                    }
                    else
                    {
                        if (Runtime.EqualsIgnoreCase(s, "WINS"))
                        {
                            if (NbtAddress.GetWinsAddress() == null)
                            {
                                if (_log.Level > 1)
                                {
                                    _log.WriteLine("NetBIOS resolveOrder specifies WINS however the " + "jcifs.netbios.wins property has not been set"
                                                   );
                                }
                                continue;
                            }
                            tmp[i++] = ResolverWins;
                        }
                        else
                        {
                            if (Runtime.EqualsIgnoreCase(s, "BCAST"))
                            {
                                tmp[i++] = ResolverBcast;
                            }
                            else
                            {
                                if (Runtime.EqualsIgnoreCase(s, "DNS"))
                                {
                                }
                                else
                                {
                                    // skip
                                    if (_log.Level > 1)
                                    {
                                        _log.WriteLine("unknown resolver method: " + s);
                                    }
                                }
                            }
                        }
                    }
                }
                _resolveOrder = new int[i];
                Array.Copy(tmp, 0, _resolveOrder, 0, i);
            }
        }