Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        static UniAddress()
        {
            string    ro   = Config.GetProperty("jcifs.resolveOrder");
            IPAddress nbns = NbtAddress.GetWinsAddress();

            try
            {
                _baddr = Config.GetInetAddress("jcifs.netbios.baddr",
                                               Extensions.GetAddressByName("255.255.255.255"));
            }
            catch (UnknownHostException)
            {
            }
            if (string.IsNullOrEmpty(ro))
            {
                if (nbns == null)
                {
                    _resolveOrder    = new int[3];
                    _resolveOrder[0] = ResolverLmhosts;
                    _resolveOrder[1] = ResolverDns;
                    _resolveOrder[2] = ResolverBcast;
                }
                else
                {
                    _resolveOrder    = new int[4];
                    _resolveOrder[0] = ResolverLmhosts;
                    _resolveOrder[1] = ResolverWins;
                    _resolveOrder[2] = ResolverDns;
                    _resolveOrder[3] = ResolverBcast;
                }
            }
            else
            {
                int[]           tmp = new int[4];
                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 (nbns == null)
                            {
                                if (_log.Level > 1)
                                {
                                    _log.WriteLine(
                                        "UniAddress 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"))
                                {
                                    tmp[i++] = ResolverDns;
                                }
                                else
                                {
                                    if (_log.Level > 1)
                                    {
                                        _log.WriteLine("unknown resolver method: " + s);
                                    }
                                }
                            }
                        }
                    }
                }
                _resolveOrder = new int[i];
                Array.Copy(tmp, 0, _resolveOrder, 0, i);
            }
        }
Ejemplo n.º 3
0
        /// <exception cref="UnknownHostException"></exception>
        public static UniAddress[] GetAllByName(string hostname, bool possibleNtDomainOrWorkgroup)
        {
            object addr;
            int    i;

            if (string.IsNullOrEmpty(hostname))
            {
                throw new UnknownHostException();
            }
            if (IsDotQuadIp(hostname))
            {
                UniAddress[] addrs = new UniAddress[1];
                addrs[0] = new UniAddress(NbtAddress.GetByName(hostname));
                return(addrs);
            }
            for (i = 0; i < _resolveOrder.Length; i++)
            {
                try
                {
                    switch (_resolveOrder[i])
                    {
                    case ResolverLmhosts:
                    {
                        if ((addr = Lmhosts.GetByName(hostname)) == null)
                        {
                            continue;
                        }
                        break;
                    }

                    case ResolverWins:
                    {
                        if (hostname == NbtAddress.MasterBrowserName ||
                            hostname.Length > 15)
                        {
                            // invalid netbios name
                            continue;
                        }
                        if (possibleNtDomainOrWorkgroup)
                        {
                            addr = LookupServerOrWorkgroup(hostname,
                                                           NbtAddress.GetWinsAddress());
                        }
                        else
                        {
                            addr = NbtAddress.GetByName(hostname,
                                                        unchecked (0x20),
                                                        null,
                                                        NbtAddress.GetWinsAddress());
                        }
                        break;
                    }

                    case ResolverBcast:
                    {
                        if (hostname.Length > 15)
                        {
                            // invalid netbios name
                            continue;
                        }

                        try
                        {
                            if (possibleNtDomainOrWorkgroup)
                            {
                                NbtAddress[] iaddrs = LookupServerOrWorkgroup(hostname,
                                                                              _baddr);

                                UniAddress[] addrs = new UniAddress[iaddrs.Length];
                                for (int ii = 0; ii < iaddrs.Length; ii++)
                                {
                                    addrs[ii] = new UniAddress(iaddrs[ii]);
                                }
                                return(addrs);
                            }
                            else
                            {
                                addr = NbtAddress.GetByName(hostname,
                                                            unchecked (0x20),
                                                            null,
                                                            _baddr);
                            }
                        }
                        catch (Exception ex)
                        {
                            if (i == _resolveOrder.Length - 1)
                            {
                                throw ex;
                            }
                            else
                            {
                                continue;
                            }
                        }
                        break;
                    }

                    case ResolverDns:
                    {
                        if (IsAllDigits(hostname))
                        {
                            throw new UnknownHostException(hostname);
                        }

                        IPAddress[] iaddrs = Extensions.GetAddressesByName(hostname);

                        if (iaddrs == null || iaddrs.Length == 0)
                        {
                            continue;
                        }

                        return(iaddrs.Select(iaddr => new UniAddress(iaddr)).ToArray());
                    }

                    default:
                    {
                        // Success
                        throw new UnknownHostException(hostname);
                    }
                    }
                    UniAddress[] addrs1 = new UniAddress[1];
                    addrs1[0] = new UniAddress(addr);
                    return(addrs1);
                }
                catch (IOException)
                {
                }
            }
            // Success
            // Failure
            throw new UnknownHostException(hostname);
        }
Ejemplo n.º 4
0
 /// <summary>To convert this address to an <code>InetAddress</code>.</summary>
 /// <remarks>To convert this address to an <code>InetAddress</code>.</remarks>
 /// <returns>
 /// the
 /// <see cref="System.Net.IPAddress">System.Net.IPAddress</see>
 /// representation of this address.
 /// </returns>
 /// <exception cref="UnknownHostException"></exception>
 public IPAddress GetInetAddress()
 {
     return(Extensions.GetAddressByName(GetHostAddress()));
 }