Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

equivalent of INET_ATON() in mongodb

What is the equivalent of INET_ATON() in mongodb? I am using nodejs with mongodb so if a equivalent in nodejs is avaliable than it is good enough.

like image 251
peter Avatar asked Dec 26 '22 05:12

peter


1 Answers

// ip example: 192.168.2.1
function inet_aton(ip){
    // split into octets
    var a = ip.split('.');
    var buffer = new ArrayBuffer(4);
    var dv = new DataView(buffer);
    for(var i = 0; i < 4; i++){
        dv.setUint8(i, a[i]);
    }
    return(dv.getUint32(0));
}

// num example: 3232236033
function inet_ntoa(num){
    var nbuffer = new ArrayBuffer(4);
    var ndv = new DataView(nbuffer);
    ndv.setUint32(0, num);

    var a = new Array();
    for(var i = 0; i < 4; i++){
        a[i] = ndv.getUint8(i);
    }
    return a.join('.');
}

http://rolfrost.de/ipjs.html

like image 114
Rolf Rost Avatar answered Jan 14 '23 21:01

Rolf Rost