Debian Codegraph @

js/parsers/parser-network.js

raw symbols SCIP
1 /*! Parser: network - updated 2018-01-10 (v2.29.3) */
2 /* IPv4, IPv6 and MAC Addresses */
3 /*global jQuery: false */
4 ;(function($) {
5 'use strict';
6
7 var ts = $.tablesorter,
8 ipv4Format,
9 ipv4Is;
10
11 /*! IPv6 Address parser (WIP) *//*
12 * IPv6 Address (ffff:0000:0000:0000:0000:0000:0000:0000)
13 * needs to support short versions like '::8' or '1:2::7:8'
14 * and '::00:192.168.10.184' (embedded IPv4 address)
15 * see http://www.intermapper.com/support/tools/IPV6-Validator.aspx
16 */
17 $.extend( ts.regex, {}, {
18 ipv4Validate : /((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})/,
19 ipv4Extract : /([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/,
20
21 // simplified regex from http://www.intermapper.com/support/tools/IPV6-Validator.aspx
22 // (specifically from http://download.dartware.com/thirdparty/ipv6validator.js)
23 ipv6Validate : /^\s*((([0-9a-f]{1,4}:){7}([0-9a-f]{1,4}|:))|(([0-9a-f]{1,4}:){6}(:[0-9a-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-f]{1,4}:){5}(((:[0-9a-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-f]{1,4}:){4}(((:[0-9a-f]{1,4}){1,3})|((:[0-9a-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){3}(((:[0-9a-f]{1,4}){1,4})|((:[0-9a-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){2}(((:[0-9a-f]{1,4}){1,5})|((:[0-9a-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){1}(((:[0-9a-f]{1,4}){1,6})|((:[0-9a-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9a-f]{1,4}){1,7})|((:[0-9a-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/i
24 });
25
26 // used for internal testing; it's not useful to set this to true because the natural sort algorithm
27 // is set up to only sort solitary hex values ("ffff") vs separated hex values ("ffff.ffff")
28 ts.defaults.ipv6HexFormat = false;
29
30 ts.addParser({
31 id: 'ipv6Address',
32 is: function(s) {
33 return ts.regex.ipv6Validate.test(s);
34 },
35 format: function(address, table) {
36 // code modified from http://zurb.com/forrst/posts/JS_Expand_Abbreviated_IPv6_Addresses-1OR
37 // Saved to https://gist.github.com/Mottie/7018157
38 var i, t, sides, groups, groupsPresent,
39 hex = table ? (typeof table === 'boolean' ? table : table && table.config.ipv6HexFormat || false) : false,
40 fullAddress = '',
41 expandedAddress = '',
42 validGroupCount = 8;
43 // validGroupSize = 4; <- removed while loop
44 // remove any extra spaces
45 address = address.replace(/\s*/g, '');
46 // look for embedded ipv4
47 if (ts.regex.ipv4Validate.test(address)) {
48 groups = address.match(ts.regex.ipv4Extract);
49 t = '';
50 for (i = 1; i < groups.length; i++) {
51 t += ('00' + (parseInt(groups[i], 10).toString(16)) ).slice(-2) + ( i === 2 ? ':' : '' );
52 }
53 address = address.replace( ts.regex.ipv4Extract, t );
54 }
55
56 if (address.indexOf('::') === -1) {
57 // All eight groups are present
58 fullAddress = address;
59 } else {
60 // Consecutive groups of zeroes have been collapsed with '::'.
61 sides = address.split('::');
62 groupsPresent = 0;
63 for (i = 0; i < sides.length; i++) {
64 groupsPresent += sides[i].split(':').length;
65 }
66 fullAddress += sides[0] + ':';
67 for (i = 0; i < validGroupCount - groupsPresent; i++) {
68 fullAddress += '0000:';
69 }
70 fullAddress += sides[1];
71 }
72 groups = fullAddress.split(':');
73 for (i = 0; i < validGroupCount; i++) {
74 // it's fastest & easiest for tablesorter to sort decimal values (vs hex)
75 groups[i] = hex ? ('0000' + groups[i]).slice(-4) :
76 ('00000' + (parseInt(groups[i], 16) || 0)).slice(-5);
77 expandedAddress += ( i !== validGroupCount - 1) ? groups[i] + ':' : groups[i];
78 }
79 return expandedAddress;
80 },
81 // uses natural sort hex compare
82 type: 'text'
83 });
84
85 // ipv4 address
86 // moved here from jquery.tablesorter.js (core file)
87 ipv4Is = function(s) {
88 return (/^\d{1,3}[\.]\d{1,3}[\.]\d{1,3}[\.]\d{1,3}$/).test(s);
89 };
90 ipv4Format = function(s) {
91 var i,
92 a = s ? s.split('.') : '',
93 r = [],
94 l = a.length;
95 for (i = 0; i < l; i++) {
96 r.push(('000' + a[i]).slice(-3));
97 }
98 return s ? r.join('.') : s;
99 };
100
101 /*! Parser: ipv4Address (a.k.a. ipAddress) */
102 // duplicate 'ipAddress' as 'ipv4Address' (to maintain backwards compatility)
103 ts.addParser({
104 id: 'ipAddress',
105 is: ipv4Is,
106 format: ipv4Format,
107 type: 'text'
108 });
109 ts.addParser({
110 id: 'ipv4Address',
111 is: ipv4Is,
112 format: ipv4Format,
113 type: 'text'
114 });
115
116 /*! Parser: MAC address */
117 /* MAC examples: 12:34:56:78:9A:BC, 1234.5678.9ABC, 12-34-56-78-9A-BC, and 123456789ABC
118 */
119 ts.addParser({
120 id : 'MAC',
121 is : function() {
122 return false;
123 },
124 format : function( str ) {
125 var indx, len,
126 mac = [],
127 val = ( str || '' ).replace( /[:.-]/g, '' ).match( /\w{2}/g );
128 if ( val ) {
129 // not assuming all mac addresses in the column will end up with six
130 // groups of two to process, so it's not actually validating the address
131 len = val.length;
132 for ( indx = 0; indx < len; indx++ ) {
133 mac.push(( '000' + parseInt( val[ indx ], 16 ) ).slice( -3 ));
134 }
135 return mac.join('.');
136 }
137 return str;
138 },
139 // uses natural sort hex compare
140 type : 'text'
141 });
142
143 })( jQuery );