Repository navigation
Expand file tree
/
Copy pathIP.java
More file actions
179 lines (155 loc) · 6.02 KB
/
Copy pathIP.java
File metadata and controls
179 lines (155 loc) · 6.02 KB
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
import java.util.regex.Pattern;
import java.util.Scanner;
import java.util.regex.Matcher;
class ipformating {
// Regular expression for IPv4 addresses
private static final String IPv4_PATTERN =
"^((25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\\.){3}" +
"(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)$";
private static final Pattern pattern = Pattern.compile(IPv4_PATTERN);
// Method to validate IPv4 address
public static boolean isValidIPv4(String ip) {
if (ip == null) {
return false;
}
Matcher matcher = pattern.matcher(ip);
return matcher.matches();
}
// Method to check if the IP is private
public static boolean isPrivateIPv4(String ip) {
if (!isValidIPv4(ip)) {
return false;
}
int[] octets = new int[4];
String[] parts = ip.split("\\.");
for (int i = 0; i < 4; i++) {
octets[i] = Integer.parseInt(parts[i]);
}
// Private IP ranges
return (octets[0] == 10) ||
(octets[0] == 172 && octets[1] >= 16 && octets[1] <= 31) ||
(octets[0] == 192 && octets[1] == 168);
}
// Method to calculate the network address
public static String getNetworkAddress(String ip, int prefix) {
int ipAddr = ipToInt(ip);
int mask = 0xFFFFFFFF << (32 - prefix);
int network = ipAddr & mask;
return intToIp(network);
}
// Method to calculate the broadcast address
public static String getBroadcastAddress(String ip, int prefix) {
int ipAddr = ipToInt(ip);
int mask = 0xFFFFFFFF << (32 - prefix);
int broadcast = ipAddr | ~mask;
return intToIp(broadcast);
}
// Convert IP string to integer
private static int ipToInt(String ip) {
String[] parts = ip.split("\\.");
int result = 0;
for (String part : parts) {
result = result << 8;
result |= Integer.parseInt(part);
}
return result;
}
// Convert integer to IP string
private static String intToIp(int ip) {
return ((ip >> 24) & 0xFF) + "." + ((ip >> 16) & 0xFF) + "." +
((ip >> 8) & 0xFF) + "." + (ip & 0xFF);
}
// Method to classify the IP address
public static String classifyIPv4(String ip) {
if (!isValidIPv4(ip)) {
return "Invalid IP";
}
int firstOctet = Integer.parseInt(ip.split("\\.")[0]);
if (firstOctet >= 1 && firstOctet <= 126) {
return "Class A";
} else if (firstOctet >= 128 && firstOctet <= 191) {
return "Class B";
} else if (firstOctet >= 192 && firstOctet <= 223) {
return "Class C";
} else if (firstOctet >= 224 && firstOctet <= 239) {
return "Class D";
} else if (firstOctet >= 240 && firstOctet <= 255) {
return "Class E";
} else {
return "Invalid IP";
}
}
// Method to calculate the number of subnets
public static int getNumberOfSubnets(int prefix, String ipClass) {
int defaultPrefix;
switch (ipClass) {
case "Class A":
defaultPrefix = 8;
break;
case "Class B":
defaultPrefix = 16;
break;
case "Class C":
defaultPrefix = 24;
break;
default:
return -1; // Not applicable for Class D and E
}
return (int) Math.pow(2, prefix - defaultPrefix);
}
}
class ipchk extends ipformating {
void check() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Enter an IP address (or type 'exit' to quit): ");
String ip = scanner.nextLine();
if (ip.equalsIgnoreCase("exit")) {
break;
}
System.out.print("Enter subnet (in prefix format): ");
String subnetInput = scanner.nextLine();
int subnet;
try {
subnet = Integer.parseInt(subnetInput);
if (subnet < 0 || subnet > 32) {
System.out.println("Invalid subnet value. Please enter a value between 0 and 32.");
continue;
}
} catch (NumberFormatException e) {
System.out.println("Invalid subnet format. Please enter a numeric value.");
continue;
}
double possibleAddresses = Math.pow(2, 32 - subnet);
String networkAddress = getNetworkAddress(ip, subnet);
String broadcastAddress = getBroadcastAddress(ip, subnet);
if (isValidIPv4(ip)) {
System.out.println("Valid IPv4: " + ip);
String ipClass = classifyIPv4(ip);
System.out.println("IP Class: " + ipClass);
if (isPrivateIPv4(ip)) {
System.out.println("The IP is private.");
} else {
System.out.println("The IP is public.");
}
System.out.println("Number of possible hosts: " + (int) possibleAddresses);
System.out.println("Network Address: " + networkAddress);
System.out.println("Broadcast Address: " + broadcastAddress);
if (ipClass.equals("Class A") || ipClass.equals("Class B") || ipClass.equals("Class C")) {
int numberOfSubnets = getNumberOfSubnets(subnet, ipClass);
System.out.println("Number of possible subnets: " + numberOfSubnets);
}
} else {
System.out.println("Invalid IPv4: " + ip);
}
System.out.println();
}
scanner.close();
}
}
public class IP {
public static void main(String[] args) {
ipchk chk = new ipchk();
chk.check();
}
}