-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReseau.java
74 lines (64 loc) · 2.48 KB
/
Reseau.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//package openjsip;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.lang.Math;
public class Reseau {
private String ip;
private String mask;
//constructor
public Reseau(String ip, String mask) {
this.ip = ip;
this.mask = mask;
}
public String getIP() {
return this.ip;
}
public void setIP(String ip) {
this.ip = ip;
}
public String getMask() {
return this.mask;
}
public void setMask(String mask) {
this.mask = mask;
}
//returns null if you couldn't change ip to network, returns the network part of the ip if successful
public static String ipToNetwork(String ip) throws UnknownHostException {
/*
* byte[] ipNetwork = null; if(ip != null){ System.out.println("ip pas null");
* //converting ip and mask from string to byte byte[] ipAddressByte =
* InetAddress.getByName(ip).getAddress(); System.out.println("IP Address Byte "
* + ipAddressByte); byte[] ipMaskByte =
* InetAddress.getByName(maskIP).getAddress(); System.out.println("Mask Byte " +
* ipMaskByte); //System.out.println("a"); //performing the logical operation &
* between the ip address and the mask for(int j=0; j<ipAddressByte.length;
* j++){ System.out.println("Dans la boucle"); ipNetwork[j] = (byte)
* (ipAddressByte[j] & ipMaskByte[j]); }
*
* System.out.println("avant convert"); //converting from byte to inet address
* InetAddress netInetAddress = InetAddress.getByAddress(ipNetwork);
* System.out.println("failed to convert"); //and from inet address to string
* return netInetAddress.getHostAddress(); } else { return null; }
*/
String networkConcat="";
String ipTab[] = ip.split("\\.");
if(ipTab.length>2) {
networkConcat = ipTab[0]+"."+ipTab[1]+"."+ipTab[2];
}
return networkConcat;
}
// method used to convert numerical mask to subnet mask
/*
* public static String getCIDRToSubnetMask(int cidr) { StringBuilder
* subnetMaskBuilder = new StringBuilder();
*
* int remainingBits = 32 - cidr; int octet = 0; for (int i = 0; i < 4; i++) {
* if (remainingBits >= 8) { octet = 255; remainingBits -= 8; } else { octet =
* (int) (256 - Math.pow(2, remainingBits)); remainingBits = 0; }
*
* subnetMaskBuilder.append(octet);
*
* if (i < 3) { subnetMaskBuilder.append("."); } } return
* subnetMaskBuilder.toString(); }
*/
}