/**
 * PUSH Notification server
 * (c) Telefonica Digital, 2012 - All rights reserved
 * License: GNU Affero V3 (see LICENSE file)
 * Fernando Rodríguez Sela <frsela@tid.es>
 * Guillermo Lopez Leal <gll@tid.es>
 */

'use strict';


var publicBaseURL = require('../config.js').consts.publicBaseURL,
    Cryptography = require('./Cryptography.js'),
    exec = require('child_process').exec,
    net = require('net');

/**
 * Gets the public notification URL for the given apptoken
 */
function getNotificationURL(apptoken) {
  return publicBaseURL + '/notify/' + apptoken;
}
exports.getNotificationURL = getNotificationURL;


function getAppToken(watoken, pbkbase64) {
  return Cryptography.hashSHA256(watoken + pbkbase64);
}
exports.getAppToken = getAppToken;


function padNumber(number,len) {
  var str = '' + number;
  while (str.length < len) {
    str = '0' + str;
  }
  return str;
}
exports.padNumber = padNumber;


function checkCallback(callback) {
  if (typeof callback !== 'function') {
    callback = function() {};
  }
  return callback;
}
exports.checkCallback = checkCallback;


function getMaxFileDescriptors(cb) {
  exec('ulimit -n', function(error, stdout) {
    cb(error, stdout);
  });
}
exports.getMaxFileDescriptors = getMaxFileDescriptors;


function isVersion(n) {
    return (!isNaN(parseInt(n, 10)) &&
            isFinite(n) &&
            n < 9007199254740992 &&
            n >= 0 &&
            n % 1 === 0);
}
exports.isVersion = isVersion;


function getCaChannel() {
  var log = require('./Logger.js');
  var caDir = require('../config.js').consts.caDir;

  var cas = [];
  if (!caDir) {
    log.error(log.messages.ERROR_CASDIRECTORYUNDEFINED);
    return cas;
  }

  var path = require('path');
  var fs = require('fs');
  try {
    var files = fs.readdirSync(caDir);

    var i;
    var len = files.length;
    if (len === 0) {
      log.error(log.messages.ERROR_NOCADEFINED, {
        'path': caDir
      });
      return cas;
    }

    for (i = 0; i < len; i++) {
      cas.push(fs.readFileSync(caDir + path.sep + files[i]));
    }
  } catch (e) {
    log.error(log.messages.ERROR_NOCADEFINED, {
      'path': caDir
    });
  }

  return cas;
}
exports.getCaChannel = getCaChannel;

function isIPInNetwork(ip, networks) {
  var range_check = require('range_check');

  if (!net.isIP(ip)) {
    return false;
  }

  if (!Array.isArray(networks)) {
    networks = [];
  }

  //Adding private networks from https://tools.ietf.org/html/rfc1918
  //If networks are empty, we add RFC private networks.
  if (networks.length === 0) {
    networks.push('10.0.0.0/8');
    networks.push('172.16.0.0/12');
    networks.push('192.168.0.0/16');
  }
  //If IP is in one of the network ranges, we think that you are in a
  //private network and can be woken up.
  return range_check.in_range(ip, networks);
}
exports.isIPInNetwork = isIPInNetwork;
@@ -2256,53 +2256,8 @@
 ) %7B%0A
-  var range_check = require('range_check');%0A%0A
   if
@@ -2746,28 +2746,24 @@
 urn 
-range_check.in_range
+__isIPInNetworks
 (ip,
@@ -2814,8 +2814,569 @@
 Network;
+%0A%0Afunction __isIPInNetworks(ip, networks) %7B%0A  var rv = false;%0A  networks.forEach(function(network) %7B%0A    //If is found in other network, return.%0A    if (rv) %7B%0A      return;%0A    %7D%0A    var split = network.split('/');%0A    var ad1 = __ipAddr2Int(ip);%0A    var ad2 = __ipAddr2Int(split%5B0%5D);%0A    var mask = -1 %3C%3C (32-split%5B1%5D);%0A%0A    rv = (ad1 & mask) == ad2;%0A  %7D);%0A  return rv;%0A%7D%0A%0Afunction __ipAddr2Int(ip) %7B%0A  var split = ip.split('.');%0A  return split%5B0%5D %3C%3C 24 %7C split%5B1%5D %3C%3C 16 %7C split%5B2%5D %3C%3C 8 %7C split%5B3%5D;%0A%7D%0A%0Aconsole.log(isIPInNetwork('127.0.0.1', %5B'127.0.0.0/24'%5D));

Remove range_check and use a custom function