Advertisement
sindibadthesailor

determining the class of an IPv4 address

Mar 10th, 2023
605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.20 KB | Cybersecurity | 0 0
  1. package com.isga.isi.reseaux;
  2.  
  3.     import java.net.InetAddress;
  4.     import java.net.UnknownHostException;
  5.  
  6. public class IPClasses {
  7.    
  8.     public static String findClass(InetAddress ia) {
  9.         byte[] ip=ia.getAddress();
  10.         String res=null;
  11.            
  12.         if ((ip[0]& 0x80)==0) res="A";
  13.         if ((ip[0]& 0xC0)==0x80) res="B";
  14.         if ((ip[0]& 0xE0)==0xC0) res="C";
  15.         if ((ip[0]& 0xF0)==0xE0) res="D";
  16.         if ((ip[0]& 0xF0)==0xF0) res="E";
  17.  
  18.         return res;
  19.     }
  20.  
  21.     public static String printSquareBrackets(InetAddress ia) {
  22.        
  23.         byte[] ip=ia.getAddress();
  24.         if(ip.length==4) { //adresse IPv4
  25.            
  26.         }else { //adresse IPv6
  27.            
  28.         }
  29.        
  30.        
  31.         return null;
  32.     }
  33.    
  34.    
  35. public static void main(String[] args) {
  36.  
  37.             try {
  38.                 InetAddress myLocalHost = InetAddress.getLocalHost();
  39.                 System.out.println("My own \"local\" address is : " + myLocalHost);
  40.                 String name=null;
  41.                 int i;
  42.                 for (i = 0; i < args.length; i++) {
  43.                     name=args[i];
  44.                     InetAddress ia = InetAddress.getByName(name);
  45.                     System.out.println("l'adresse "+ia + " est de classe "+findClass(ia));
  46.                 }
  47.             } catch (UnknownHostException uhe) {
  48.                 System.out.println("Une exception a eu lieu:");
  49.                 System.out.println(uhe.getMessage());
  50.             }
  51.         }
  52.     }
  53.  
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement