Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function validateIPv4Address(address) {
- const arrayedAddress = address.split('.');
- if (arrayedAddress.length !== 4) {
- return false;
- }
- const invalidOctet = arrayedAddress.filter(octet => parseInt(octet, 10) < 0 || parseInt(octet, 10) > 255);
- return invalidOctet.length === 0;
- }
- function clasifyIPv4Address(address) {
- const subnetClasses = {
- A: {
- lowest: 0,
- highest: 127,
- },
- B: {
- lowest: 128,
- highest: 191,
- },
- C: {
- lowest: 192,
- highest: 223,
- },
- D: {
- lowest: 224,
- highest: 239,
- },
- E: {
- lowest: 240,
- highest: 255,
- },
- };
- if (validateIPv4Address(address)) {
- const addressPointer = parseInt(address.split('.')[0]);
- for (let subnetClass in subnetClasses) {
- const lowest = subnetClasses[subnetClass].lowest;
- const highest = subnetClasses[subnetClass].highest;
- if (addressPointer >= lowest && addressPointer <= highest) {
- return subnetClass;
- }
- }
- }
- return new Error('Invalid IPv4 address');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement