-
Notifications
You must be signed in to change notification settings - Fork 0
/
SubnetCalc.java
182 lines (158 loc) · 5.08 KB
/
SubnetCalc.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import java.util.Scanner;
/**
*
* @author esdrasevt
*/
public class SubnetCalc {
int prefix;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
boolean finish = false;
char c;
SubnetCalc brain = new SubnetCalc();
Scanner scanner = new Scanner(System.in);
while(!finish){
System.out.print(
"Enter the slash notation (e.g. 1.1.1.1/23) of your network: ");
String ip = scanner.nextLine();
try{
byte[] ipBinArr = brain.ipStrToBinArr(ip);
int prefix = brain.prefix;
String numIps = String.valueOf(brain.networkTotalIps(prefix));
String firstIp = String.valueOf(brain.firstIpAddress(ipBinArr, prefix));
String lastIp = String.valueOf(brain.lastIpAddress(ipBinArr, prefix));
String subnet = String.valueOf(brain.subNetMask(prefix));
System.out.println("============================\n"
+ " RESULT \n"
+ "============================");
System.out.println("Num of accesses: " + numIps);
System.out.println("Beginning IP(Network ID): " + firstIp);
System.out.println("Ending IP(Broadcast): " + lastIp);
System.out.println("Subnet Mask: " + subnet + "\n");
System.out.print("Calculate again? y for YES or n for NO: ");
c = scanner.next().charAt(0);
scanner.nextLine();
switch(c){
case 'n':
finish = true;
break;
case 'N':
finish = true;
break;
case 'y':
break;
case 'Y':
break;
default:
finish = true;
}
}
catch (Exception e){
System.out.println("=========================================================\n"
+ "|| Error: Please use slash notation:e.g 192.168.0.1/23 ||\n"
+ "=========================================================");
}
}
}
String firstIpAddress(byte[] ip, int prefix){
String res = "";
int count = 0;
byte[] arr = new byte[32];
String temp = "";
for(int i=0; i<prefix; i++){
arr[i] = ip[i];
}
for(int i=prefix; i<32; i++){
arr[i] = 0;
}
for(int i=0;i<4;i++){
for(int j=0;j<8;j++){
temp+=arr[count];
count++;
}
int tempIp = Integer.parseInt(temp, 2);
temp = "";
res+=tempIp;
if(i < 3)res+=".";
}
return res;
}
public String lastIpAddress(byte[] ip, int prefix){
String res = "";
int count = 0;
byte[] arr = new byte[32];
String temp = "";
for(int i=0; i<prefix; i++){
arr[i] = ip[i];
}
for(int i=prefix; i<32; i++){
arr[i] = 1;
}
for(int i=0;i<4;i++){
for(int j=0;j<8;j++){
temp+=arr[count];
count++;
}
int tempIp = Integer.parseInt(temp, 2);
temp = "";
res+=tempIp;
if(i < 3)res+=".";
}
return res;
}
//TODO: Implement this correctly
public String subNetMask(int prefix){
String res = "";
int count = 0;
byte[] arr = new byte[32];
String temp = "";
for(int i=0; i<prefix; i++){
arr[i] = 1;
}
for(int i=prefix; i<32; i++){
arr[i] = 0;
}
for(int i=0;i<4;i++){
for(int j=0;j<8;j++){
temp+=arr[count];
count++;
}
int tempIp = Integer.parseInt(temp, 2);
temp = "";
res+=tempIp;
if(i < 3)res+=".";
}
return res;
}
public int networkTotalIps(int prefix){
int res;
double temp;
temp = Math.pow(2, 32-prefix);
res = (int) temp;
return res;
}
public byte[] ipStrToBinArr(String ip){
String[] ipArr;
String[] temp;
String[] ipBinStr = new String[4];
byte[] ipBinArr = new byte[32];
int count = 0;
ipArr = ip.split("\\.");
temp = ipArr[3].split("\\/");
ipArr[3] = temp[0];
this.prefix = Integer.valueOf(temp[1]);
for(int i = 0;i<ipArr.length;i++){
int x = Integer.valueOf(ipArr[i]);
String s = Integer.toBinaryString(0x100 | x).substring(1);
ipBinStr[i] = s;
for(int j = 0;j<s.length();j++){
char c = s.charAt(j);
ipBinArr[count] = Byte.valueOf(String.valueOf(c));
count++;
}
}
return ipBinArr;
}
}