-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path468.cpp
More file actions
105 lines (104 loc) · 2.66 KB
/
Copy path468.cpp
File metadata and controls
105 lines (104 loc) · 2.66 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
class Solution {
public:
bool isNum(string s, bool isHex){
string ns;
int ita;
if(isHex){
ns = "0123456789abcdef";
ita = 16;
}else{
ns = "0123456789";
ita = 10;
}
for(int i = 0; i < s.length(); i++){
bool good = false;
for(int j = 0; j < ita; j++){
if(tolower(s[i]) == ns[j]){
good = true;
break;
}
}
if(good == false){
return false;
}
}
return true;
}
bool isIPv4(vector<string> ipstuff){
if(ipstuff.size() != 4){
return false;
}
try{
for(int i = 0; i < 4; i++){
if(!isNum(ipstuff[i], false)){
throw 0;
}
int x = stoi(ipstuff[i]);
if(x > 255 || x < 0){
return false;
}
}
}catch(...){
return false;
}
return true;
}
bool isIPv6(vector<string> ip6stuff){
if(ip6stuff.size() != 8){
return false;
}
try{
for(int i = 0; i < 8; i++){
if(!isNum(ip6stuff[i], true) || ip6stuff[i].length() > 4){
throw 0;
}
int x = stoi(ip6stuff[i], nullptr, 16);
}
}catch(...){
return false;
}
return true;
}
string validIPAddress(string IP) {
stringstream ipdelim(IP);
string tmp;
vector<string> ipstuff;
bool possible = true;
int c1 = 1;
size_t f = IP.find(".");
while(f != string::npos){
f = IP.find(".", f + 1);
c1++;
}
if(c1 == 4){
while(getline(ipdelim, tmp, '.')){
if(tmp[0] == '0' && tmp.length() > 1){
possible = false;
break;
}
ipstuff.push_back(tmp);
}
if(possible && isIPv4(ipstuff)){
return "IPv4";
}
}
stringstream ipdelim6(IP);
string tmp6;
vector<string> ip6stuff;
int c2 = 1;
size_t f2 = IP.find(":");
while(f2 != string::npos){
f2 = IP.find(":", f2 + 1);
c2++;
}
if(c2 == 8){
while(getline(ipdelim6, tmp6, ':')){
ip6stuff.push_back(tmp6);
}
if(isIPv6(ip6stuff)){
return "IPv6";
}
}
return "Neither";
}
};