235 }
236
237 /**
238 * Specifies the address family type, for instance, '1' for IPv4
239 * addresses, and '2' for IPv6 addresses.
240 */
241 int family;
242
243 int getFamily() {
244 return family;
245 }
246 }
247
248 /* Used to store the serializable fields of InetAddress */
249 final transient InetAddressHolder holder;
250
251 InetAddressHolder holder() {
252 return holder;
253 }
254
255 /* Used to store the name service provider */
256 private static List<NameService> nameServices = null;
257
258 /* Used to store the best available hostname */
259 private transient String canonicalHostName = null;
260
261 /** use serialVersionUID from JDK 1.0.2 for interoperability */
262 private static final long serialVersionUID = 3286316764910316507L;
263
264 /*
265 * Load net library into runtime, and perform initializations.
266 */
267 static {
268 preferIPv6Address = java.security.AccessController.doPrivileged(
269 new GetBooleanAction("java.net.preferIPv6Addresses")).booleanValue();
270 AccessController.doPrivileged(
271 new java.security.PrivilegedAction<Void>() {
272 public Void run() {
273 System.loadLibrary("net");
274 return null;
275 }
276 });
573 * Returns the hostname for this address.
574 *
575 * <p>If there is a security manager, this method first
576 * calls its {@code checkConnect} method
577 * with the hostname and {@code -1}
578 * as its arguments to see if the calling code is allowed to know
579 * the hostname for this IP address, i.e., to connect to the host.
580 * If the operation is not allowed, it will return
581 * the textual representation of the IP address.
582 *
583 * @return the host name for this IP address, or if the operation
584 * is not allowed by the security check, the textual
585 * representation of the IP address.
586 *
587 * @param check make security check if true
588 *
589 * @see SecurityManager#checkConnect
590 */
591 private static String getHostFromNameService(InetAddress addr, boolean check) {
592 String host = null;
593 for (NameService nameService : nameServices) {
594 try {
595 // first lookup the hostname
596 host = nameService.getHostByAddr(addr.getAddress());
597
598 /* check to see if calling code is allowed to know
599 * the hostname for this IP address, ie, connect to the host
600 */
601 if (check) {
602 SecurityManager sec = System.getSecurityManager();
603 if (sec != null) {
604 sec.checkConnect(host, -1);
605 }
606 }
607
608 /* now get all the IP addresses for this hostname,
609 * and make sure one of them matches the original IP
610 * address. We do this to try and prevent spoofing.
611 */
612
613 InetAddress[] arr = InetAddress.getAllByName0(host, check);
877
878 // search both positive & negative caches
879
880 synchronized (addressCache) {
881 cacheInitIfNeeded();
882
883 CacheEntry entry = addressCache.get(hostname);
884 if (entry == null) {
885 entry = negativeCache.get(hostname);
886 }
887
888 if (entry != null) {
889 return entry.addresses;
890 }
891 }
892
893 // not found
894 return null;
895 }
896
897 private static NameService createNSProvider(String provider) {
898 if (provider == null)
899 return null;
900
901 NameService nameService = null;
902 if (provider.equals("default")) {
903 // initialize the default name service
904 nameService = new NameService() {
905 public InetAddress[] lookupAllHostAddr(String host)
906 throws UnknownHostException {
907 return impl.lookupAllHostAddr(host);
908 }
909 public String getHostByAddr(byte[] addr)
910 throws UnknownHostException {
911 return impl.getHostByAddr(addr);
912 }
913 };
914 } else {
915 final String providerName = provider;
916 try {
917 nameService = java.security.AccessController.doPrivileged(
918 new java.security.PrivilegedExceptionAction<NameService>() {
919 public NameService run() {
920 Iterator<NameServiceDescriptor> itr =
921 ServiceLoader.load(NameServiceDescriptor.class)
922 .iterator();
923 while (itr.hasNext()) {
924 NameServiceDescriptor nsd = itr.next();
925 if (providerName.
926 equalsIgnoreCase(nsd.getType()+","
927 +nsd.getProviderName())) {
928 try {
929 return nsd.createNameService();
930 } catch (Exception e) {
931 e.printStackTrace();
932 System.err.println(
933 "Cannot create name service:"
934 +providerName+": " + e);
935 }
936 }
937 }
938
939 return null;
940 }
941 }
942 );
943 } catch (java.security.PrivilegedActionException e) {
944 }
945 }
946
947 return nameService;
948 }
949
950 static {
951 // create the impl
952 impl = InetAddressImplFactory.create();
953
954 // get name service if provided and requested
955 String provider = null;;
956 String propPrefix = "sun.net.spi.nameservice.provider.";
957 int n = 1;
958 nameServices = new ArrayList<NameService>();
959 provider = AccessController.doPrivileged(
960 new GetPropertyAction(propPrefix + n));
961 while (provider != null) {
962 NameService ns = createNSProvider(provider);
963 if (ns != null)
964 nameServices.add(ns);
965
966 n++;
967 provider = AccessController.doPrivileged(
968 new GetPropertyAction(propPrefix + n));
969 }
970
971 // if not designate any name services provider,
972 // create a default one
973 if (nameServices.size() == 0) {
974 NameService ns = createNSProvider("default");
975 nameServices.add(ns);
976 }
977 }
978
979 /**
980 * Creates an InetAddress based on the provided host name and IP address.
981 * No name service is checked for the validity of the address.
982 *
983 * <p> The host name can either be a machine name, such as
984 * "{@code java.sun.com}", or a textual representation of its IP
985 * address.
986 * <p> No validity checking is done on the host name either.
987 *
988 * <p> If addr specifies an IPv4 address an instance of Inet4Address
989 * will be returned; otherwise, an instance of Inet6Address
990 * will be returned.
991 *
992 * <p> IPv4 address byte array must be 4 bytes long and IPv6 byte array
993 * must be 16 bytes long
994 *
995 * @param host the specified host
996 * @param addr the raw IP address in network byte order
1274 // would add the host in the lookupTable and
1275 // return null. So we will do the lookup.
1276 // 2) If the host is in the lookupTable when
1277 // checkLookupTable() is called, the current thread
1278 // would be blocked until the host is removed
1279 // from the lookupTable. Then this thread
1280 // should try to look up the addressCache.
1281 // i) if it found the addresses in the
1282 // addressCache, checkLookupTable() would
1283 // return the addresses.
1284 // ii) if it didn't find the addresses in the
1285 // addressCache for any reason,
1286 // it should add the host in the
1287 // lookupTable and return null so the
1288 // following code would do a lookup itself.
1289 if ((addresses = checkLookupTable(host)) == null) {
1290 try {
1291 // This is the first thread which looks up the addresses
1292 // this host or the cache entry for this host has been
1293 // expired so this thread should do the lookup.
1294 for (NameService nameService : nameServices) {
1295 try {
1296 /*
1297 * Do not put the call to lookup() inside the
1298 * constructor. if you do you will still be
1299 * allocating space when the lookup fails.
1300 */
1301
1302 addresses = nameService.lookupAllHostAddr(host);
1303 success = true;
1304 break;
1305 } catch (UnknownHostException uhe) {
1306 if (host.equalsIgnoreCase("localhost")) {
1307 InetAddress[] local = new InetAddress[] { impl.loopbackAddress() };
1308 addresses = local;
1309 success = true;
1310 break;
1311 }
1312 else {
1313 addresses = unknown_array;
1314 success = false;
|
235 }
236
237 /**
238 * Specifies the address family type, for instance, '1' for IPv4
239 * addresses, and '2' for IPv6 addresses.
240 */
241 int family;
242
243 int getFamily() {
244 return family;
245 }
246 }
247
248 /* Used to store the serializable fields of InetAddress */
249 final transient InetAddressHolder holder;
250
251 InetAddressHolder holder() {
252 return holder;
253 }
254
255 /* Used to store the name service providers */
256 private static final class NameServices {
257 private static final List<NameService> nameServices = new ArrayList<>();
258
259 static {
260 // get name service if provided and requested
261 String propPrefix = "sun.net.spi.nameservice.provider.";
262 int n = 1;
263 String provider = AccessController.doPrivileged(
264 new GetPropertyAction(propPrefix + n));
265 while (provider != null) {
266 NameService ns = createNSProvider(provider);
267 if (ns != null)
268 nameServices.add(ns);
269
270 n++;
271 provider = AccessController.doPrivileged(
272 new GetPropertyAction(propPrefix + n));
273 }
274
275 // if not designate any name services provider,
276 // create a default one
277 if (nameServices.size() == 0) {
278 NameService ns = createNSProvider("default");
279 nameServices.add(ns);
280 }
281 }
282
283 private static NameService createNSProvider(String provider) {
284 if (provider == null)
285 return null;
286
287 NameService nameService = null;
288 if (provider.equals("default")) {
289 // initialize the default name service
290 nameService = new NameService() {
291 public InetAddress[] lookupAllHostAddr(String host)
292 throws UnknownHostException {
293 return impl.lookupAllHostAddr(host);
294 }
295 public String getHostByAddr(byte[] addr)
296 throws UnknownHostException {
297 return impl.getHostByAddr(addr);
298 }
299 };
300 } else {
301 final String providerName = provider;
302 try {
303 nameService = java.security.AccessController.doPrivileged(
304 new java.security.PrivilegedExceptionAction<NameService>() {
305 public NameService run() {
306 Iterator<NameServiceDescriptor> itr =
307 ServiceLoader.load(NameServiceDescriptor.class)
308 .iterator();
309 while (itr.hasNext()) {
310 NameServiceDescriptor nsd = itr.next();
311 if (providerName.
312 equalsIgnoreCase(nsd.getType()+","
313 +nsd.getProviderName())) {
314 try {
315 return nsd.createNameService();
316 } catch (Exception e) {
317 e.printStackTrace();
318 System.err.println(
319 "Cannot create name service:"
320 +providerName+": " + e);
321 }
322 }
323 }
324
325 return null;
326 }
327 }
328 );
329 } catch (java.security.PrivilegedActionException e) {
330 }
331 }
332
333 return nameService;
334 }
335
336 static List<NameService> get() {
337 return nameServices;
338 }
339 }
340
341 /* Used to store the best available hostname */
342 private transient String canonicalHostName = null;
343
344 /** use serialVersionUID from JDK 1.0.2 for interoperability */
345 private static final long serialVersionUID = 3286316764910316507L;
346
347 /*
348 * Load net library into runtime, and perform initializations.
349 */
350 static {
351 preferIPv6Address = java.security.AccessController.doPrivileged(
352 new GetBooleanAction("java.net.preferIPv6Addresses")).booleanValue();
353 AccessController.doPrivileged(
354 new java.security.PrivilegedAction<Void>() {
355 public Void run() {
356 System.loadLibrary("net");
357 return null;
358 }
359 });
656 * Returns the hostname for this address.
657 *
658 * <p>If there is a security manager, this method first
659 * calls its {@code checkConnect} method
660 * with the hostname and {@code -1}
661 * as its arguments to see if the calling code is allowed to know
662 * the hostname for this IP address, i.e., to connect to the host.
663 * If the operation is not allowed, it will return
664 * the textual representation of the IP address.
665 *
666 * @return the host name for this IP address, or if the operation
667 * is not allowed by the security check, the textual
668 * representation of the IP address.
669 *
670 * @param check make security check if true
671 *
672 * @see SecurityManager#checkConnect
673 */
674 private static String getHostFromNameService(InetAddress addr, boolean check) {
675 String host = null;
676 for (NameService nameService : NameServices.get()) {
677 try {
678 // first lookup the hostname
679 host = nameService.getHostByAddr(addr.getAddress());
680
681 /* check to see if calling code is allowed to know
682 * the hostname for this IP address, ie, connect to the host
683 */
684 if (check) {
685 SecurityManager sec = System.getSecurityManager();
686 if (sec != null) {
687 sec.checkConnect(host, -1);
688 }
689 }
690
691 /* now get all the IP addresses for this hostname,
692 * and make sure one of them matches the original IP
693 * address. We do this to try and prevent spoofing.
694 */
695
696 InetAddress[] arr = InetAddress.getAllByName0(host, check);
960
961 // search both positive & negative caches
962
963 synchronized (addressCache) {
964 cacheInitIfNeeded();
965
966 CacheEntry entry = addressCache.get(hostname);
967 if (entry == null) {
968 entry = negativeCache.get(hostname);
969 }
970
971 if (entry != null) {
972 return entry.addresses;
973 }
974 }
975
976 // not found
977 return null;
978 }
979
980 static {
981 // create the impl
982 impl = InetAddressImplFactory.create();
983 }
984
985 /**
986 * Creates an InetAddress based on the provided host name and IP address.
987 * No name service is checked for the validity of the address.
988 *
989 * <p> The host name can either be a machine name, such as
990 * "{@code java.sun.com}", or a textual representation of its IP
991 * address.
992 * <p> No validity checking is done on the host name either.
993 *
994 * <p> If addr specifies an IPv4 address an instance of Inet4Address
995 * will be returned; otherwise, an instance of Inet6Address
996 * will be returned.
997 *
998 * <p> IPv4 address byte array must be 4 bytes long and IPv6 byte array
999 * must be 16 bytes long
1000 *
1001 * @param host the specified host
1002 * @param addr the raw IP address in network byte order
1280 // would add the host in the lookupTable and
1281 // return null. So we will do the lookup.
1282 // 2) If the host is in the lookupTable when
1283 // checkLookupTable() is called, the current thread
1284 // would be blocked until the host is removed
1285 // from the lookupTable. Then this thread
1286 // should try to look up the addressCache.
1287 // i) if it found the addresses in the
1288 // addressCache, checkLookupTable() would
1289 // return the addresses.
1290 // ii) if it didn't find the addresses in the
1291 // addressCache for any reason,
1292 // it should add the host in the
1293 // lookupTable and return null so the
1294 // following code would do a lookup itself.
1295 if ((addresses = checkLookupTable(host)) == null) {
1296 try {
1297 // This is the first thread which looks up the addresses
1298 // this host or the cache entry for this host has been
1299 // expired so this thread should do the lookup.
1300 for (NameService nameService : NameServices.get()) {
1301 try {
1302 /*
1303 * Do not put the call to lookup() inside the
1304 * constructor. if you do you will still be
1305 * allocating space when the lookup fails.
1306 */
1307
1308 addresses = nameService.lookupAllHostAddr(host);
1309 success = true;
1310 break;
1311 } catch (UnknownHostException uhe) {
1312 if (host.equalsIgnoreCase("localhost")) {
1313 InetAddress[] local = new InetAddress[] { impl.loopbackAddress() };
1314 addresses = local;
1315 success = true;
1316 break;
1317 }
1318 else {
1319 addresses = unknown_array;
1320 success = false;
|