-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip_mask_cheker.c
77 lines (60 loc) · 1.66 KB
/
ip_mask_cheker.c
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
// Function to validate the IP address
bool is_valid_ip(const char *ip) {
unsigned int num;
int parts[4];
int count = sscanf(ip, "%d.%d.%d.%d", &parts[0], &parts[1], &parts[2], &parts[3]);
if (count != 4) {
return false;
}
for (int i = 0; i < 4; i++) {
if (parts[i] < 0 || parts[i] > 255) {
return false;
}
}
return true;
}
// Function to validate the subnet mask
bool is_valid_mask(const char *mask) {
unsigned int num;
int parts[4];
int count = sscanf(mask, "%d.%d.%d.%d", &parts[0], &parts[1], &parts[2], &parts[3]);
if (count != 4) {
return false;
}
for (int i = 0; i < 4; i++) {
if (parts[i] < 0 || parts[i] > 255) {
return false;
}
}
// Check if subnet mask is valid
unsigned long long bitmask = (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | parts[3];
unsigned long long inverted_mask = ~bitmask;
// Check if the inverted mask contains any 1's after the first 0
if ((inverted_mask & (inverted_mask + 1)) != 0) {
return false;
}
return true;
}
int main() {
char ip[16];
char mask[16];
printf("Enter an IP address: ");
scanf("%15s", ip);
if (!is_valid_ip(ip)) {
printf("Invalid IP address format!\n");
return 1;
}
printf("Enter a subnet mask: ");
scanf("%15s", mask);
if (!is_valid_mask(mask)) {
printf("Invalid subnet mask format!\n");
return 1;
}
printf("IP address: %s\n", ip);
printf("Subnet mask: %s\n", mask);
return 0;
}