1 |
ce996bd3
|
Simon Cornelius P. Umacob
|
/*
|
2 |
|
|
NetUtils.js
|
3 |
|
|
part of pfSense (http://www.pfsense.com)
|
4 |
|
|
Various helper functions for IPv6 support.
|
5 |
|
|
|
6 |
|
|
Copyright (C) 2007 Simon Cornelius P. Umacob <simoncpu@gmail.com>
|
7 |
|
|
All rights reserved.
|
8 |
|
|
|
9 |
|
|
Redistribution and use in source and binary forms, with or without
|
10 |
|
|
modification, are permitted provided that the following conditions are met:
|
11 |
|
|
|
12 |
|
|
1. Redistributions of source code must retain the above copyright notice,
|
13 |
|
|
this list of conditions and the following disclaimer.
|
14 |
|
|
|
15 |
|
|
2. Redistributions in binary form must reproduce the above copyright
|
16 |
|
|
notice, this list of conditions and the following disclaimer in the
|
17 |
|
|
documentation and/or other materials provided with the distribution.
|
18 |
|
|
|
19 |
|
|
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
20 |
|
|
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
21 |
|
|
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
22 |
|
|
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
23 |
|
|
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
24 |
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
25 |
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
26 |
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
27 |
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
28 |
|
|
POSSIBILITY OF SUCH DAMAGE.
|
29 |
|
|
|
30 |
|
|
*/
|
31 |
|
|
|
32 |
|
|
function NetUtils_changeIPVersionMask(field, version) {
|
33 |
|
|
switch(version){
|
34 |
|
|
case 'IPv4':
|
35 |
|
|
NetUtils_clearOptions(document.getElementById(field));
|
36 |
|
|
NetUtils_loadMaskIPv4(document.getElementById(field), 32);
|
37 |
|
|
|
38 |
|
|
break;
|
39 |
|
|
case 'IPv6':
|
40 |
|
|
NetUtils_clearOptions(document.getElementById(field));
|
41 |
|
|
NetUtils_loadMaskIPv6(document.getElementById(field), 64);
|
42 |
|
|
|
43 |
|
|
break;
|
44 |
|
|
case 'IPv4_net':
|
45 |
|
|
NetUtils_clearOptions(document.getElementById(field));
|
46 |
|
|
NetUtils_loadMaskIPv4(document.getElementById(field), 32, 1, 31);
|
47 |
|
|
|
48 |
|
|
break;
|
49 |
|
|
case 'IPv6_net':
|
50 |
|
|
NetUtils_clearOptions(document.getElementById(field));
|
51 |
|
|
NetUtils_loadMaskIPv6(document.getElementById(field), 64, 1, 63);
|
52 |
|
|
|
53 |
|
|
break;
|
54 |
|
|
}
|
55 |
|
|
}
|
56 |
|
|
|
57 |
|
|
function NetUtils_clearOptions(obj) {
|
58 |
|
|
var len = obj.length;
|
59 |
|
|
|
60 |
|
|
for (var i = 0; i < len; i++) {
|
61 |
|
|
obj[0] = null;
|
62 |
|
|
}
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
function NetUtils_loadMaskIPv4(obj, sel, min, max) {
|
66 |
|
|
var min,
|
67 |
|
|
max,
|
68 |
|
|
j = 0;
|
69 |
|
|
|
70 |
|
|
min = min == undefined ? 1 : min;
|
71 |
|
|
max = max == undefined ? 32 : max;
|
72 |
|
|
|
73 |
|
|
for (var i = max; i >= min; i--) {
|
74 |
|
|
obj[j] = new Option(i, i);
|
75 |
|
|
if (sel == i) {
|
76 |
|
|
obj[j].selected = true;
|
77 |
|
|
}
|
78 |
|
|
j++;
|
79 |
|
|
}
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
function NetUtils_loadMaskIPv6(obj, sel, min, max) {
|
83 |
|
|
var min,
|
84 |
|
|
max,
|
85 |
|
|
j = 0;
|
86 |
|
|
|
87 |
|
|
min = min == undefined ? 1 : min;
|
88 |
|
|
max = max == undefined ? 64 : max;
|
89 |
|
|
|
90 |
|
|
if ((max % 4) != 0) {
|
91 |
|
|
obj[j++] = new Option(max, max);
|
92 |
|
|
|
93 |
|
|
/**
|
94 |
|
|
* NOTE: This solution is a kludge.
|
95 |
|
|
* If you have a better way, don't hesitate
|
96 |
|
|
* to change this. Please send patches. :)
|
97 |
|
|
*/
|
98 |
|
|
for (var i = 1; i <= 3; i++) {
|
99 |
|
|
if (((max - i) % 4) == 0) {
|
100 |
|
|
max = max - i;
|
101 |
|
|
break;
|
102 |
|
|
}
|
103 |
|
|
}
|
104 |
|
|
}
|
105 |
|
|
|
106 |
|
|
for (var i = max; i >= min; i -= 4) {
|
107 |
|
|
obj[j] = new Option(i, i);
|
108 |
|
|
if (sel == i) {
|
109 |
|
|
obj[j].selected = true;
|
110 |
|
|
}
|
111 |
|
|
j++;
|
112 |
|
|
}
|
113 |
|
|
}
|