Advertisement
cyla

IPv6 indicator

Oct 8th, 2011
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.21 KB | None | 0 0
  1. package hu.jmk.ipv6;
  2.  
  3. import java.applet.Applet;
  4. import java.awt.Color;
  5. import java.awt.Graphics;
  6. import java.net.InetAddress;
  7. import java.net.InterfaceAddress;
  8. import java.net.NetworkInterface;
  9. import java.net.SocketException;
  10. import java.net.UnknownHostException;
  11. import java.util.ArrayList;
  12. import java.util.Collections;
  13. import java.util.Enumeration;
  14. import java.util.List;
  15.  
  16. import org.xbill.DNS.*;
  17.  
  18. public class IPv6Indicator extends Applet {
  19.     static final long serialVersionUID = 0x1;
  20.  
  21.     public void init() {
  22.         getLocalHostName();
  23.         getLocalIPv4Addresses();
  24.         getLocalIPv6Addresses();
  25.         getRemoteIPv4Addresses( "google.hu" );
  26.         getRemoteIPv6Addresses( "origo.hu" );
  27.     }
  28.  
  29.     public void paint( Graphics g ) {
  30.         setBackground( Color.BLUE );
  31.     }
  32.  
  33.     public String getLocalHostName() {
  34.         String localHostnameString = null;
  35.         try {
  36.             java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
  37.             localHostnameString = localMachine.getHostName();
  38.             System.out.printf( "Local machine hostname: %s\n", localHostnameString );
  39.         } catch ( UnknownHostException e ) {
  40.         }
  41.  
  42.         return localHostnameString;
  43.     }
  44.  
  45.     public String[] getLocalIPv4Addresses() {
  46.         System.out.printf( "Local IPv4 addresses:" );
  47.         return getLocalAddresses( 4 );
  48.     }
  49.  
  50.     public String[] getLocalIPv6Addresses() {
  51.         System.out.printf( "Local IPv6 adresses:" );
  52.         return getLocalAddresses( 16 );
  53.     }
  54.  
  55.     public String[] getLocalAddresses( int segments ) {
  56.         ArrayList<String> localAddresses = new ArrayList<String>();
  57.  
  58.         try {
  59.             Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
  60.             for ( NetworkInterface networkInterface : Collections.list( networkInterfaces ) ) {
  61.                 if ( networkInterface.isUp() && !networkInterface.isLoopback() ) {
  62.                     List<InterfaceAddress> interfaceAddresses = networkInterface.getInterfaceAddresses();
  63.                     for ( InterfaceAddress interfaceAddress : interfaceAddresses ) {
  64.                         InetAddress inetAddress = interfaceAddress.getAddress();
  65.                         if ( ( inetAddress.getAddress().length == segments ) && !inetAddress.isAnyLocalAddress() && !inetAddress.isLinkLocalAddress() && !inetAddress.isLoopbackAddress() && !inetAddress.isMCGlobal() && !inetAddress.isMCLinkLocal() && !inetAddress.isMCNodeLocal() && !inetAddress.isMCOrgLocal() && !inetAddress.isMCSiteLocal() && !inetAddress.isMulticastAddress() && !inetAddress.isSiteLocalAddress() ) {
  66.                             boolean isTeredo = false;
  67.                             if ( ( interfaceAddress.getNetworkPrefixLength() == 32 ) && ( inetAddress.getAddress()[0] == 32 ) && ( inetAddress.getAddress()[1] == 1 ) && ( inetAddress.getAddress()[2] == 0 ) && ( inetAddress.getAddress()[3] == 0 ) ) {
  68.                                 isTeredo = true;
  69.                             }
  70.                             if ( !isTeredo ) {
  71.                                 localAddresses.add( inetAddress.getHostAddress() );
  72.                                 System.out.printf( " %s", inetAddress.getHostAddress() );
  73.                             }
  74.                         }
  75.                     }
  76.                 }
  77.             }
  78.             System.out.printf( "\n" );
  79.         } catch ( SocketException e ) {
  80.             System.out.printf( "\n" );
  81.             System.out.println( e.getMessage() );
  82.         }
  83.  
  84.         String[] localAddressesArray = new String[localAddresses.size()];
  85.  
  86.         return localAddresses.toArray( localAddressesArray );
  87.     }
  88.  
  89.     public String[] getRemoteIPv4Addresses( String hostname ) {
  90.         System.out.printf( "Remote IPv4 addresses:" );
  91.         return getRemoteAddresses( hostname, Type.A );
  92.     }
  93.  
  94.     public String[] getRemoteIPv6Addresses( String hostname ) {
  95.         System.out.printf( "Remote IPv6 addresses:" );
  96.         return getRemoteAddresses( hostname, Type.AAAA );
  97.     }
  98.  
  99.     public String[] getRemoteAddresses( String hostName, int type ) {
  100.         ArrayList<String> remoteAddresses = new ArrayList<String>();
  101.  
  102.         try {
  103.             Lookup lookup = new Lookup( hostName, type );
  104.             lookup.run();
  105.             if ( lookup.getResult() == Lookup.SUCCESSFUL ) {
  106.                 for ( int i = 0; i < lookup.getAnswers().length; i++ ) {
  107.                     remoteAddresses.add( lookup.getAnswers()[i].rdataToString() );
  108.                     System.out.printf( " %s", lookup.getAnswers()[i].rdataToString() );
  109.                 }
  110.             }
  111.             System.out.printf( "\n" );
  112.         } catch ( TextParseException e ) {
  113.             System.out.printf( "\n" );
  114.             System.out.println( e.getMessage() );
  115.         }
  116.  
  117.         String[] remoteAddressesArray = new String[remoteAddresses.size()];
  118.  
  119.         return remoteAddresses.toArray( remoteAddressesArray );
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement