/* Authors: * Petr Vobornik * * Copyright (C) 2010 Red Hat * see file 'COPYING' for use and warranty information * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ define(['./ipa', './jquery'], function(IPA, $) { var NET = {}; NET.ip_address = function(spec) { spec = spec || {}; if (typeof spec === 'string') { spec = { address: spec }; } var that = IPA.object(); that.input = spec.address; that.type = spec.type; that.parts = spec.parts; that.reverse_address = ''; that.only_decimal = spec.only_decimal !== undefined? spec.only_decimal : false; //for parsing IPv4 address that.parse = function() { if (!that.input && !that.parts) { that.set_error('no input'); return false; } if (!that.type) { that.type = that.detect_type(); } if (that.type === 'v4-quads') { return that.parse_v4_quads(); } else if (that.type === 'v4-int') { return that.parse_v4_int(); } else if (that.type === 'v6') { return that.parse_v6(); } that.set_error('not an ip address'); return false; }; that.detect_type = function() { var type; if (!that.input) return null; if (that.input.indexOf(':') > -1) type = 'v6'; else if (that.input.indexOf('.') > -1) type = 'v4-quads'; else type = 'v4-int'; return type; }; that.parse_v4_int = function() { var part = { value: that.input }; if(!that.is_part_valid_v4(part, 32, that.only_decimal)) return false; that.parts = []; that.make_quads(part.decimal_value, that.parts); that.valid = true; return true; }; that.parse_v4_quads = function() { if (!that.parts) { that.parts = that.input.split('.'); } if (that.parts.length !== 4) { return that.set_error('invalid number of parts'); } for (var i=0; i<4; i++) { var part = { value: that.parts[i] }; if (!that.is_part_valid_v4(part, 8, that.only_decimal)) { return false; } that.parts[i] = part.decimal_value.toString(10); } that.valid = true; return true; }; that.parse_v6 = function() { if (!that.parts) { that.parts = that.input.split(':'); } var total_parts = that.parts.length; var ipv4_present = false; var double_colon = false; var double_colon_position; var i; //usecases like ':' if (that.parts.length <= 2) { return that.set_error('invalid format'); } for (i=0; i -1) { ipv4_present = true; total_parts++; //ipv4 part consists of 4 octects (two parts) } //checking for :: if (part.length === 0) { if (!double_colon || //first occurance (double_colon && i === 1) || //still at the beginning (double_colon && i === that.parts.length - 1 && double_colon_position === i -1)) { //still at the end part = '0000'; that.parts[i] = part; double_colon = true; double_colon_position = i; } else { //second occurance of :: return that.set_error('invalid format: mupltiple ::'); } } //add missing zeros for not empty parts if (part.length !== 0 && part.length < 4) { part = add_leading_zeros(part, 4 - part.length); that.parts[i] = part; } } //add missing empty parts if (double_colon) { var parts_to_add = 8 - total_parts; for (i=0; i=0; i--) { that.reverse_parts.push(that.parts[i]); } that.reverse_parts.push('in-addr'); that.reverse_parts.push('arpa'); return that.reverse_parts.join('.'); }; that.get_v6_reverse = function() { that.reverse_parts = []; var address = that.parts.join(''); for (var i=31; i>=0; i--) { that.reverse_parts.push(address[i]); } that.reverse_parts.push('ip6'); that.reverse_parts.push('arpa'); return that.reverse_parts.join('.'); }; that.set_error = function(msg) { that.valid = false; that.error = msg; return false; }; that.is_part_valid_v6 = function(str) { if (str.length === 0) { return that.set_error('not a number'); } if (str.length > 4) { return that.set_error('wrong format - too long'); } for (var i=0; i 1) { return that.set_error('invalid format: leading zeros'); } } var max_value = Math.pow(2, bits) - 1; part.decimal_value = parseInt(part.value, radix); if (part.decimal_value > max_value) { return that.set_error('value out of range'); } return true; }; that.get_radix = function(str) { var normalized = str.toLowerCase(); if (normalized.length > 2 && normalized[0] === '0' && normalized[1] === 'x') { return 16; } else if (normalized.length > 1 && normalized[0] === '0') { return 8; } return 10; }; that.make_quads = function(integer, quads) { var hex_str = integer.toString(16); if (hex_str.length < 8) { hex_str = add_leading_zeros(hex_str, 8 - hex_str.length); } for (var i=0; i 2 && normalized[0] === '0' && normalized[1] === 'x') { return 16; } else if (normalized.length > 1 && normalized[0] === '0') { return 8; } return 10; }; that.parse(); return that; }; return NET; });