Guest User

Leandro Costa

a guest
Oct 3rd, 2008
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.67 KB | None | 0 0
  1. //Ipv4.java
  2. package loki.net;
  3.  
  4. public class Ipv4{
  5.     private int bitmask;
  6.     private int oct[]   = new int[4];
  7.     private int mask[]  = new int[4];
  8.     private String boct[]  = new String[4]; // Ip em Binario
  9.     private String bmask[] = new String[4]; // Mask em Binario
  10.     private String ipbin="",maskbin=""; // Ip e Mask em binario sem pontuacao.
  11.     private String host; // Parte binaria do ip referente ao host.
  12.     private String net; // Parte bin�ria do ip referente a rede.
  13.    
  14.     //Array relacionando as mascaras e o bitmask.
  15.     public static final String BITMASK[] = {
  16.         "0.0.0.0","128.0.0.0","192.0.0.0",
  17.         "224.0.0.0","240.0.0.0","248.0.0.0",
  18.         "252.0.0.0","254.0.0.0","255.0.0.0",
  19.         "255.128.0.0","255.192.0.0","255.224.0.0",
  20.         "255.240.0.0","255.248.0.0","255.252.0.0",
  21.         "255.254.0.0","255.255.0.0","255.255.128.0",
  22.         "255.255.192.0","255.255.224.0","255.255.240.0",
  23.         "255.255.248.0","255.255.252.0","255.255.254.0",
  24.         "255.255.255.0","255.255.255.128","255.255.255.192",
  25.         "255.255.255.224","255.255.255.240","255.255.255.248",
  26.         "255.255.255.252","255.255.255.254","255.255.255.255"
  27.     };
  28.    
  29.     public static final String NUM_HOSTS[] = {
  30.         "4294967294","2147483646","1073741822",
  31.         "536870910","268435454","134217726",
  32.         "67108862","33554430","16777214",
  33.         "8388606","4194302","2097150","1048574",
  34.         "524286","262142","131070","65534","32766",
  35.         "16382","8190","4094","2046","1022","510",
  36.         "254","126","62","30","14","6","2","0","1"
  37.     };
  38.    
  39.     //Construtor da classe.
  40.     public Ipv4(String addr, String mask){
  41.         //Checando se o ip est� em formato valido.
  42.         String tmp[] = addr.split("[.]");
  43.         if(tmp.length < 4)
  44.             throw new RuntimeException("Formato de Host Invalido. Ex: w.x.y.z");
  45.            
  46.         //Armazenando os octetos do endere�o.
  47.         for(int i=0;i<4;i++){
  48.             oct[i] = Integer.parseInt(tmp[i]);
  49.             boct[i] = zfill(Integer.toString(oct[i], 2),8);
  50.             ipbin += boct[i];
  51.         }
  52.        
  53.         //Trabalhando com a mascara.
  54.         bitmask = calcBitMask(mask);
  55.        
  56.         //Armazenando os octetos da mascara.
  57.         tmp = BITMASK[bitmask].split("[.]");
  58.         for(int i=0;i<4;i++){
  59.             this.mask[i] = Integer.parseInt(tmp[i]);
  60.             bmask[i] = zfill(Integer.toString(this.mask[i],2),8);
  61.             maskbin += bmask[i];
  62.         }
  63.        
  64.         //Armazena em binario sem os pontos a parte do ip referente a rede
  65.         net  = ipbin.substring(0,bitmask);
  66.         host = ipbin.substring(bitmask);
  67.        
  68.     }
  69.    
  70.     //Completa a string numerica com zeros a esquerda.
  71.     private String zfill(String num, int tam){
  72.         if(num.length()<tam){
  73.             int dif = tam-num.length();
  74.             for(int i=0;i < dif;i++)num="0"+num;
  75.         }
  76.         return num;
  77.     }
  78.    
  79.     //Coloca a pontuacao em um ip binario
  80.     private String binPutDot(String ipbin){
  81.         StringBuffer ip = new StringBuffer(ipbin);
  82.         ip = ip.insert(8,".");
  83.         ip = ip.insert(17,".");
  84.         ip = ip.insert(26,".");
  85.         return ip.toString();
  86.     }
  87.    
  88.     //Converte um ip em binario com pontuacao para decial
  89.     private String ipToDecimal(String ip){
  90.         String o[] = ip.split("[.]");
  91.         ip="";
  92.         for(int i=0;i < 4; i++)
  93.             ip += String.valueOf(Integer.parseInt(o[i],2)) + ".";
  94.         ip = ip.substring(0,ip.length()-1);
  95.         return ip;
  96.     }
  97.    
  98.     //Calcula o bitmask da mascara de rede.
  99.     private int calcBitMask(String m){
  100.         if(m.length()<=2){
  101.             int mask = Integer.parseInt(m);
  102.             if(mask<0> 32)
  103.                 throw new RuntimeException("Bitmask de Tamanho Inv�lido");
  104.             return mask;
  105.         }
  106.        
  107.         for(int i=0;i<BITMASK.length;i++)
  108.             if(BITMASK[i].equals(m))return i;
  109.        
  110.         return 0;
  111.     }
  112.    
  113.     //Retorna o bitmask
  114.     public String getBitMask(){return String.valueOf(bitmask);}
  115.    
  116.     //Retorna a m�scara em decimal
  117.     public String getMask(){
  118.         String tmp = "";
  119.         for(int i=0; i < mask.length;i++)
  120.             tmp += String.valueOf(mask[i]) +".";
  121.         tmp = tmp.substring(0,tmp.length()-1);
  122.         return tmp;
  123.     }
  124.    
  125.     //Retorna a m�scara em bin�rio.  
  126.     public String getBinMask(){
  127.         String tmp = "";
  128.         for(int i=0; i < mask.length;i++)
  129.             tmp += bmask[i];
  130.         return binPutDot(tmp);
  131.     }
  132.    
  133.     //Retorna a rede a qual pertence o ip.
  134.     public String getNetwork(){
  135.         StringBuffer ip = new StringBuffer(net);
  136.         int dif = 32 - ip.length();
  137.         for(int i=0;i < dif; i++)ip.append("0");
  138.         ip = ip.insert(8,".");
  139.         ip = ip.insert(17,".");
  140.         ip = ip.insert(26,".");
  141.         return ipToDecimal(ip.toString());
  142.     }
  143.    
  144.     //Retorna o broadcast da rede a qual pertence o ip.
  145.     public String getBroadcast(){
  146.         StringBuffer ip = new StringBuffer(net);
  147.         int dif = 32 - ip.length();
  148.         for(int i=0;i < dif; i++)ip.append("1");
  149.         return ipToDecimal(binPutDot(ip.toString()));
  150.     }
  151.    
  152.     //Retorna o primeiro ip da rede
  153.     public String getMinIp(){
  154.         String h = "";
  155.         int dif = 32 - net.length();
  156.         for(int i=0;i < dif; i++)h+="0";
  157.        
  158.         //Converte pra decimal e soma 1 ao host
  159.         int s = Integer.parseInt(h,2) + 1;
  160.         //Converte novamente pra binario
  161.         String host = Integer.toString(s,2);
  162.         //Completa os zeros caso necessario
  163.         host = zfill(host,dif);
  164.         //Coloca os pontos e converte pra decimal
  165.         return ipToDecimal(binPutDot(net + host));      
  166.     }
  167.    
  168.     //Retorna o primeiro ip da rede
  169.     public String getMaxIp(){
  170.         String h = "";
  171.         int dif = 32 - net.length();
  172.         for(int i=0;i < dif; i++) h+="1";
  173.        
  174.         //Converte pra decimal e soma 1 ao host
  175.         int s = Integer.parseInt(h,2) - 1;
  176.         //Converte novamente pra binario
  177.         String host = Integer.toString(s,2);
  178.         //Completa os zeros caso necessario
  179.         host = zfill(host,dif);
  180.         //Coloca os pontos e converte pra decimal
  181.         return ipToDecimal(binPutDot(net + host));      
  182.     }
  183.    
  184.     //Retorna true se o ip passado no construtor for rede.
  185.     public boolean isNetwork(){
  186.         String h = "";
  187.         int dif = 32 - net.length();
  188.         for(int i=0;i < dif; i++)h+="0";
  189.         return h.equals(host);
  190.     }
  191.    
  192.     //Retorna true se o ip passado no construtor for broadcast.
  193.     public boolean isBroadcast(){
  194.         String h = "";
  195.         int dif = 32 - net.length();
  196.         for(int i=0;i < dif; i++)h+="1";
  197.         return h.equals(host);
  198.     }
  199.    
  200.     //Retorna o proximo ip.
  201.     public String nextIp(){
  202.         if(isBroadcast())return "0.0.0.0";
  203.         int dif = 32 - net.length();
  204.         //Converte pra decimal e soma 1 ao host
  205.         int s = Integer.parseInt(host,2) + 1;
  206.         //Converte novamente pra binario
  207.         String host = Integer.toString(s,2);
  208.         //Completa os zeros caso necessario
  209.         host = zfill(host,dif);
  210.         //Coloca os pontos e converte pra decimal
  211.         host = ipToDecimal(binPutDot(net + host));
  212.        
  213.         //Se o proximo ip for o broadcast, retorn 0.
  214.         if(host.equals(getBroadcast()))return "0.0.0.0";
  215.        
  216.         return host;
  217.     }
  218.  
  219.     // Retorna a Quantidade de Hosts da Rede.
  220.     public String getNumHosts(){return NUM_HOSTS[bitmask];}
  221.    
  222.     //Ferramenta de linha de comando para testes da lib.
  223.     public static void main(String args[]){
  224.         java.util.Scanner in = new java.util.Scanner(System.in);
  225.         System.out.println("-----------------------------------");
  226.         System.out.println("--- Ipcalc - Leandro Costa 2008 ---");
  227.         System.out.println("-- ");
  228.         System.out.print("-- Host|Net  :");
  229.         String h = in.nextLine();
  230.         System.out.print("-- [Bit]Mask :");
  231.         String m = in.nextLine();
  232.        
  233.         Ipv4 i = new Ipv4(h,m);
  234.         System.out.println("-----------------------------------");
  235.         System.out.println("-- Network  : " + i.getNetwork() + "/" + i.getBitMask());
  236.         System.out.println("-- Broadcast: " + i.getBroadcast());
  237.         System.out.println("-- Mascara  : " + i.getMask());
  238.         System.out.println("-- Ip Min   : " + i.getMinIp());
  239.         System.out.println("-- Ip Max   : " + i.getMaxIp());
  240.         System.out.println("-- Next Ip  : " + i.nextIp());
  241.         System.out.println("-- isNet    : " + i.isNetwork());
  242.         System.out.println("-- isBrd    : " + i.isBroadcast());
  243.         System.out.println("-- N. Hosts : " + i.getNumHosts());
  244.     }
  245. }
Advertisement
Add Comment
Please, Sign In to add comment