Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function for subnet mask

I have a subnet mask as a value in an object. It comes in the long form, ie. 255.255.255.0 (for /24).

I haven't come across some sort of JavaScript function for calculating this. So before I write a whole lot of if and else if statements, I want to quickly double check to make sure that I didn't miss out on some JavaScript goody that does this already.

Thanks!

Edit: Clarification

I was wondering if there is a JavaScript function that I don't know of, that will translate the long form and return a short form, slash notation. For Example:

If I pass var obj_mask = "255.255.255.0"; to a existing JavaScript (API?), it will return a /24 value.

If such function doesn't exist in JavaScript, it's fine, I have already written half the if statements, and I'll be happy to share it after so no one else has to write it out. But seeing that I'm new to JS, I wanted to know if such function/API existed natively to the language.

like image 482
misterbear Avatar asked Dec 02 '14 23:12

misterbear


2 Answers

I use the following functions for netmask to CIDR and CIDR to netmask conversions:

var netmask2CIDR = (netmask) => (netmask.split('.').map(Number)
      .map(part => (part >>> 0).toString(2))
      .join('')).split('1').length -1;

var CIDR2netmask = (bitCount) => {
  var mask=[];
  for(var i=0;i<4;i++) {
    var n = Math.min(bitCount, 8);
    mask.push(256 - Math.pow(2, 8-n));
    bitCount -= n;
  }
  return mask.join('.');
}

Hope this helps!

like image 125
Xavier Egea Avatar answered Oct 21 '22 07:10

Xavier Egea


Just finished writing it, and then realized that it would look cleaner with switch case:

For anyone's reference. If there is in fact some JS function or API that will make this a lot cleaner and simpler, please answer and I'll mark it.

    switch(ret_mask = path.mask) {
        case "0.0.0.0":
            return ret_mask = "/0";
        case "128.0.0.0":
            return ret_mask = "/1";
        case "192.0.0.0":
            return ret_mask = "/2";
        case "224.0.0.0":
            return ret_mask = "/3";
        case "240.0.0.0":
            return ret_mask = "/4";
        case "248.0.0.0":
            return ret_mask = "/5";
        case "252.0.0.0":
            return ret_mask = "/6";
        case "254.0.0.0":
            return ret_mask = "/7";
        case "255.0.0.0":
            return ret_mask = "/8";
        case "255.128.0.0":
            return ret_mask = "/9";
        case "255.192.0.0":
            return ret_mask = "/10";
        case "255.224.0.0":
            return ret_mask = "/11";
        case "255.240.0.0":
            return ret_mask = "/12";
        case "255.248.0.0":
            return ret_mask = "/13";
        case "255.252.0.0":
            return ret_mask = "/14";
        case "255.254.0.0":
            return ret_mask = "/15";
        case "255.255.0.0":
            return ret_mask = "/16";
        case "255.255.128.0":
            return ret_mask = "/17";
        case "255.255.192.0":
            return ret_mask = "/18";
        case "255.255.224.0":
            return ret_mask = "/19";
        case "255.255.240.0":
            return ret_mask = "/20";
        case "255.255.248.0":
            return ret_mask = "/21";
        case "255.255.252.0":
            return ret_mask = "/22";
        case "255.255.254.0":
            return ret_mask = "/23";
        case "255.255.255.0":
            return ret_mask = "/24";
        case "255.255.255.128":
            return ret_mask = "/25";
        case "255.255.255.192":
            return ret_mask = "/26";
        case "255.255.255.224":
            return ret_mask = "/27";
        case "255.255.255.240":
            return ret_mask = "/28";
        case "255.255.255.248":
            return ret_mask = "/29";
        case "255.255.255.252":
            return ret_mask = "/30";
        case "255.255.255.254":
            return ret_mask = "/31";
        case "255.255.255.255":
            return ret_mask = ""
    }
like image 3
misterbear Avatar answered Oct 21 '22 07:10

misterbear