-
Notifications
You must be signed in to change notification settings - Fork 50
/
cutils.js
1451 lines (1346 loc) · 48 KB
/
cutils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
*/
//Common utility functions
cutils = module.exports;
g = require('./global')
function getCookie(name) {
if(isSet(name) && isString(name)) {
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var x = cookies[i].substr(0, cookies[i].indexOf("="));
var y = cookies[i].substr(cookies[i].indexOf("=") + 1);
x = x.replace(/^s+|s+$/g, "").trim();
if (x == name)
return unescape(y);
}
}
return false;
}
function setCookie(name, value) {
var oldCookie = getCookie(name);
document.cookie = name + "=" + escape(value) +
"; expires=Sun, 17 Jan 2038 00:00:00 UTC; path=/";
}
var class_A = 1;
var class_B = 2;
var class_C = 3;
function isSet(o) {
if (typeof o == "undefined" || null == o)
return false;
else if (isString(o)) {
return ("" !== o.trim())
}
else if (isNumber(o))
return true;
else if (isObject(o)) {
return true;
}
}
function isObject(o) {
return (typeof o === "object");
}
function isNumber(o) {
return !isNaN(o - 0);
}
function isString(o) {
return (typeof o === "string");
}
function pad(num, size) {
var s = g.UNIQUE_ID_PADDING + num;
return s.substr(s.length - size);
}
function clone(obj) {
// Handle the 3 simple types, and null or undefined
if (null == obj || "object" != typeof obj) return obj;
// Handle Date
if (obj instanceof Date) {
var copy = new Date();
copy.setTime(obj.getTime());
return copy;
}
// Handle Array
if (obj instanceof Array) {
var copy = [];
for (var i = 0, len = obj.length; i < len; i++) {
copy[i] = clone(obj[i]);
}
return copy;
}
// Handle Object
if (obj instanceof Object) {
var copy = {};
for (var attr in obj) {
if (obj.hasOwnProperty(attr)) copy[attr] = clone(obj[attr]);
}
return copy;
}
throw new Error("Unable to copy obj! Its type isnt supported.");
}
function getID(divid){
if(divid === undefined){
return -1;
}
var split = divid.split("_");
if(split.length > 1){
return(split[1])
} else {
return -1;
}
}
function getInnerID(divid){
var split = divid.split("_");
if(split.length > 3){
return(split[3])
} else {
return -1;
}
}
function doAjaxCall(targetUrl, methodType, postData, successHandler, failureHandler, cacheEnabled, callbackParams, timeOut, hideErrMsg,abortCall,projectUUID) {
var url = targetUrl, type = methodType, cache = cacheEnabled,
success = successHandler, failure = failureHandler, data = postData,
cbParams = callbackParams, headers = {}, timeout = timeOut, hideErrorMsg = hideErrMsg;
headers["X-Tenant-Name"] = "default-project";
var contentType = null, dataType = null;
if (isSet(url)) {
if (isSet(type)) {
var type = type.trim();
if (type !== "GET" && type !== "POST" && type !== "DELETE" &&
type !== "PUT") {
type = "GET";
}
if (type === "POST" || type === "PUT" || type === "DELETE") {
headers["X-Tenant-Name"] = "default-project";
if (!isSet(data)) {
data = "{}";
}
contentType = "application/json; charset=utf-8";
dataType = "json";
}
} else {
type = "GET";
}
if (isSet(cache)) {
if (cache === "true" || cache === true)
cache = true;
else if (cache === "false" || cache === false)
cache = false;
}
else {
cache = false;
}
if (!isSet(success)) {
success = "";
}
if (!isSet(failure)) {
failure = "";
}
var config = {
type:type,
cache:cache,
url:url,
data:data,
headers:headers,
abortOnNavigate:abortCall
};
if (isSet(contentType))
config.contentType = contentType;
if (isSet(dataType))
config.dataType = dataType;
if(isSet(timeout) && isNumber(timeout) && timeout > 0) {
config.timeout = timeout;
}
if(methodType === "POST") {
var jsonPostData = JSON.parse(data);
var projectUUId = "";
var parent_type = jsonPath(jsonPostData, "$..parent_type");
if(null !== parent_type && false !== parent_type &&
parent_type.length == 1) {
parent_type = parent_type[0];
if(parent_type === "project") {
var fqn = jsonPath(jsonPostData, "$.*..fq_name")[0];
projectUUId =
jsonPath(configObj, "$.projects[?(@.fq_name[0]=='" + fqn[0] + "' && @.fq_name[1]=='" + fqn[1] + "')]")[0].uuid;
var dataType = jsonPath(jsonPostData, "$.*")[0];
dataType["parent_uuid"] = projectUUId;
} else if(parent_type === "domain") {
var fqn = jsonPath(jsonPostData, "$.*.fq_name")[0];
var domainUUId =
jsonPath(configObj, "$.domains[?(@.fq_name[0]=='" + fqn[0] + "')]")[0].uuid;
var dataType = jsonPath(jsonPostData, "$.*")[0];
dataType["parent_uuid"] = domainUUId;
} else if(parent_type === "floating-ip-pool") {
var fqn = jsonPath(jsonPostData, "$.*.fq_name")[0];
var fipoolUUId =
jsonPath(configObj, "$.floating-ip-pools[?(@.to[0]=='" + fqn[0] + "' && @.to[1]=='" + fqn[1] +
"' && @.to[2]=='" + fqn[2] + "')]")[0].uuid;
var dataType = jsonPath(jsonPostData, "$.*")[0];
dataType["parent_uuid"] = fipoolUUId;
}
data = JSON.stringify(jsonPostData);
config.data = data;
var getProject = {
type:"GET",
abortOnNavigate:true
};
if(parent_type === "project" || parent_type === "floating-ip-pool") {
getProject["url"] = "/api/tenants/config/project/" + projectUUId +
"?exclude_children=True&exclude_back_refs=True";
}
$.ajax(getProject)
.success(function (res) {
callAjax(config, success, failure, hideErrorMsg, cbParams);
})
.fail(function (res) {
callAjax(config, success, failure, hideErrorMsg, cbParams);
});
} else {
callAjax(config, success, failure, hideErrorMsg, cbParams);
}
} else {
callAjax(config, success, failure, hideErrorMsg, cbParams);
}
}
else {
return false;
}
}
function callAjax(config, success, failure, hideErrorMsg, cbParams) {
$.ajax(config)
.success(function (res) {
configObj = $.extend({}, configObj, res);
if (typeof window[success] === "function")
window[success](res, cbParams);
})
.fail(function (res) {
if(res.statusText === 'abort') {
return;
}
if(hideErrorMsg !== "true" && hideErrorMsg !== true) {
if(res.responseText && res.responseText != "") {
showInfoWindow(res.responseText, res.statusText);
}
}
if (typeof window[failure] === "function")
window[failure](res, cbParams);
});
}
function toggleButtonStateByID(id, enable) {
var btn = $("#" + id);
if (isSet(btn) && btn.length > 0) {
if (enable === true) {
$("#" + id)[0].disabled = false;
$("#" + id).removeClass("disabled");
} else {
$("#" + id)[0].disabled = true;
$("#" + id).addClass("disabled");
}
}
}
function removeRTString(rt) {
if (isSet(rt) && rt.indexOf("target:") != -1) {
return rt.split("target:")[1];
}
return rt;
}
function formatPolicyRule(rule, domain, project) {
var rule_display = "";
if (isSet(rule) && !rule.hasOwnProperty("length")) {
if (isSet(rule["action_list"]) && isSet(rule["action_list"]["simple_action"]))
rule_display += policyRuleFormat(rule["action_list"]["simple_action"]);
if (isSet(rule["action_list"]) && isSet(rule["action_list"]["log"]) &&
true === rule["action_list"]["log"])
rule_display += policyRuleFormat(", log ");
if (isSet(rule["application"]) && rule["application"].length > 0) {
rule_display += " application " + policyRuleFormat(rule["application"].toString());
var src_addr = formatSrcDestAddresses(rule["src_addresses"], domain, project);
rule_display += src_addr;
if(isSet(rule["direction"]))
rule_display += policyRuleFormat(rule["direction"]);
var dest_addr = policy_net_display(rule["dst_addresses"], domain, project);
var dest_addr = formatSrcDestAddresses(rule["dst_addresses"], domain, project);
rule_display += dest_addr;
if (isSet(rule["action_list"]))
rule_display += " action" + policyRuleFormat(rule["action_list"].toString());
} else {
if(null !== rule["simple_action"] && typeof rule["simple_action"] !== "undefined")
rule_display += policyRuleFormat(rule["simple_action"]);
if (isSet(rule["protocol"]))
//rule_display += " protocol " + rule["protocol"].toString();
rule_display += ' protocol ' + policyRuleFormat(rule["protocol"].toString());
var src_addr = formatSrcDestAddresses(rule["src_addresses"], domain, project);
rule_display += src_addr;
var src_ports = policy_ports_display(rule["src_ports"]);
if(isSet(src_ports))
rule_display += " ports " + policyRuleFormat(src_ports);
if(isSet(rule["direction"]))
rule_display += ' ' + policyRuleFormat(rule["direction"]);
var dest_addr = formatSrcDestAddresses(rule["dst_addresses"], domain, project);
rule_display += dest_addr;
var dst_ports = policy_ports_display(rule["dst_ports"]);
if(isSet(dst_ports))
rule_display += ' ports ' + policyRuleFormat(dst_ports);
var action_list = policy_services_display(rule["action_list"], domain, project);
if(isSet(action_list))
rule_display += action_list;
}
}
return rule_display;
}
function formatSrcDestAddresses (rule, domain, project) {
var rule_display = '';
var addrSrcDest = policy_net_display(rule, domain, project);
if(isSet(addrSrcDest.value)) {
rule_display = addrSrcDest.label + addrSrcDest.value;
}
return rule_display;
}
function policy_net_display(nets, domain, project) {
var net_disp_all = "";
var labelName = ' network ';
if (isSet(nets) && nets.length > 0) {
for (var i = 0; i < nets.length; i++) {
var net_disp = "";
var net = nets[i];
if (isSet(net)) {
if (isSet(net["security_group"])) {
net_disp += net["security_group"].toString();
}
if (isSet(net["subnet"]) && isSet(net["subnet"]["ip_prefix"]) &&
isSet(net["subnet"]["ip_prefix_len"])) {
labelName = ' ';
net_disp +=
policyRuleFormat(net["subnet"]["ip_prefix"] + "/" +
net["subnet"]["ip_prefix_len"]);
}
if (isSet(net["virtual_network"])) {
labelName = ' network ';
net_disp += prepareFQN(domain, project, net["virtual_network"]);
}
if(isSet(net["network_policy"])) {
labelName = ' policy ';
net_disp += prepareFQN(domain, project, net["network_policy"]);
}
}
net_disp_all += net_disp;
}
}
return {value : net_disp_all, label : labelName} ;
}
function prepareFQN(domain, project, net) {
var net_disp = '';
if(isSet(domain) && isSet(project) && isString(domain) &&
isString(project)) {
var splits = net.split(":");
if(domain === splits[0] && project === splits[1]) {
if(splits.length === 3) {
if(splits[2].toLowerCase() === "any"
|| splits[2].toLowerCase() === "local"){
net_disp = policyRuleFormat(net.toString());
} else {
net_disp = policyRuleFormat(splits[2]);
}
} else {
net_disp = policyRuleFormat(splits[0]);
}
} else {
//prepare network display format
var netArry = net.toString().split(':');
if(netArry.length === 3) {
if(netArry[0].toLowerCase() != 'any' && netArry[0].toLowerCase() != 'local') {
netArry = policyRuleFormat(netArry[2]) + ' (' + netArry[0] + ':' + netArry[1] + ')';
net_disp = netArry;
} else {
net_disp = policyRuleFormat(netArry[0]);
}
} else {
net_disp = policyRuleFormat(netArry[0]);
}
}
} else {
net_disp = policyRuleFormat(net.toString());
}
return net_disp;
}
function policy_ports_display(ports) {
var ports_str = "";
if (isSet(ports) && ports.length > 0) {
if (ports.length == 1 && ports[0]["start_port"] == -1) {
ports_str += " any";
}
else {
ports_str += " [";
for (var i = 0; i < ports.length; i++) {
var p = ports[i];
if (isSet(p["start_port"])) {
ports_str += " " + p["start_port"].toString();
if (isSet(p["end_port"])) {
if (p["start_port"] !== p["end_port"]) {
ports_str += "-" + p["end_port"].toString();
}
}
}
if (i != (ports.length - 1)) {
ports_str += ",";
}
}
ports_str = ports_str + " ]";
}
}
return ports_str;
}
function policy_services_display(action_list, domain, project) {
var service_str = "";
if (isSet(action_list)) {
var as = action_list.apply_service;
var mt = action_list.mirror_to;
if (isSet(as) && as.length > 0) {
var services_value = "";
for (var i = 0; i < as.length; i++) {
var item = as[i].split(':');
if(item.length === 3) {
if(item[0] === domain &&
item[1] === project) {
item = item[2];
} else {
item = item[2] + ' (' + item[0] + ':' + item[1] + ')';
}
} else {
item = item[0];
}
services_value += item;
if (i != (as.length - 1)) {
services_value += ",";
}
}
service_str += ' services ' + policyRuleFormat(services_value);
}
if (isSet(mt) && isSet(mt.analyzer_name)) {
mt.analyzer_name = mt.analyzer_name.split(':');
if(mt.analyzer_name.length === 3) {
if(mt.analyzer_name[0] === domain &&
mt.analyzer_name[1] === project) {
mt.analyzer_name = mt.analyzer_name[2];
} else {
mt.analyzer_name = mt.analyzer_name[2] + ' (' + mt.analyzer_name[0] + ':' + mt.analyzer_name[1] + ')';
}
} else {
mt.analyzer_name = mt.analyzer_name[0];
}
service_str += ' mirror ' + policyRuleFormat(mt.analyzer_name);
}
}
return service_str;
}
function launchVNCcb(result, cbParams) {
var href = jsonPath(result, "$.console.url")[0];
if (cbParams.sameWindow) {
$("#vnc-console-widget").show();
if(cbParams.title) {
$("#vnc-console-title").text(cbParams.title);
}
$("#vnc-console-frame").attr("src", href);
} else {
window.open(href);
}
}
function failureLaunchVNCcb(error) {
$("#vnc-console-widget").hide();
$("#vnc-console-frame").attr("src", "");
showInfoWindow("Error in getting url of VNC console: " + error.statusText, "Error");
}
function getFormatVNName(vn) {
if (isSet(vn) && vn.length > 0) {
if (vn.trim().toLowerCase() === "automatic") {
return "";
}
return vn.trim();
}
return "";
}
function getApplyServices(arr) {
if (isSet(arr) && arr.length > 0) {
for (var i = 0; i < arr.length; i++) {
arr[i] = arr[i].trim();
var sis = jsonPath(configObj, "$..service_templates[*].service-template.service_instance_back_refs[*]");
for (var j = 0; j < sis.length; j++) {
var si = sis[j];
if (si.to[2] == arr[i]) {
arr[i] = si.to[0] + ":" + si.to[1] + ":" + si.to[2];
break;
}
}
}
return arr;
}
return null;
}
function checkValidSourceNetwork(vn) {
if (!isSet(vn)) {
//showInfoWindow("Select valid source network", "Input required");
return "any";
}
return vn;
}
function checkValidDestinationNetwork(vn) {
if (!isSet(vn)) {
//showInfoWindow("Select valid destination network", "Input required");
return "any";
}
return vn;
}
function getProtocol(protocol) {
return (isSet(protocol)) ? protocol.toLowerCase() : "any";
}
function getFQNofVN(domain, project, vn) {
if(!isSet(domain) || !isSet(project)) {
if(isSet(vn)) {
return vn;
} else {
return null;
}
}
var fqn = jsonPath(configObj,
"$..virtual-networks[?(@.fq_name[0]=='" + domain +
"' && @.fq_name[1]=='" + project +
"' && @.fq_name[2]=='" + vn + "')]");
if (fqn && fqn.length == 1) {
fqn = fqn[0].fq_name;
fqn = (fqn.toString()).replace(/,/g, ":");
return fqn;
} else if (isSet(vn)) {
return vn
}
return null;
}
function getFQNofPolicy(domain, project, policy) {
if(!isSet(domain) || !isSet(project)) {
if(isSet(policy)) {
return policy;
} else {
return null;
}
}
var fqn = jsonPath(configObj,
"$..policys-input[?(@.fq_name[0]=='" + domain +
"' && @.fq_name[1]=='" + project +
"' && @.fq_name[2]=='" + policy + "')]");
if (fqn && fqn.length == 1) {
fqn = fqn[0].fq_name;
fqn = (fqn.toString()).replace(/,/g, ":");
return fqn;
} else if (isSet(policy)) {
return policy
}
return null;
}
function getEndPort(port) {
if (isSet(port)) {
port = port.toString();
if (port.trim().toLowerCase() == "any")
return -1;
else {
if (port.indexOf(",") != -1) {
var startports = [];
var parts = port.split(",");
for (var i = 0; i < parts.length; i++) {
var part = parts[i];
if (isSet(part.split("-")[1]))
startports[startports.length] = part.split("-")[1];
else {
if (isSet(part.split("-")[0])) {
startports[startports.length] =
part.split("-")[0];
}
}
}
return startports.toString();
} else {
var part = port.split("-")[1];
if (isSet(part)) {
part = part.trim().toLowerCase();
return part;
} else if (isSet(port.split("-")[0])) {
port = port.split("-")[0];
return port;
}
}
}
}
return -1;
}
function getStartPort(port) {
if (isSet(port)) {
if (port.trim().toLowerCase() == "any")
return -1;
else {
if (port.indexOf(",") != -1) {
var startports = [];
var parts = port.split(",");
for (var i = 0; i < parts.length; i++) {
var part = parts[i];
if (isSet(part.split("-")[0]))
startports[startports.length] = part.split("-")[0];
else {
if (isSet(part.split("-")[1])) {
startports[startports.length] =
part.split("-")[1];
}
}
}
return startports.toString();
} else {
port = port.split("-")[0];
if (isSet(port)) {
port = port.trim().toLowerCase();
return port;
}
}
}
}
return -1;
}
function genarateGateway(cidr,from){
var ciderValue = new v4.Address(cidr);
var gateway;
if(ciderValue.isValid() === true){
var ipcreated;
var bigInt;
if(from == "end"){
ipcreated = ciderValue.endAddress();
var bg1 = ipcreated.bigInteger();
bigInt = bg1.subtract(new BigInteger("1",10));
} else if(from == "start" || from == "" ){
ipcreated = ciderValue.startAddress();
var bg1 = ipcreated.bigInteger();
bigInt = bg1.add(new BigInteger("1",10));
}
gateway = v4.Address.fromBigInteger(bigInt).address;
} else {
ciderValue = new v6.Address(cidr);
if(ciderValue.isValid() === true){
var ipcreated;
var bigInt;
if(from == "end"){
ipcreated = ciderValue.endAddress();
var bg1 = ipcreated.bigInteger();
bigInt = bg1.subtract(new BigInteger("1",10));
} else if(from == "start" || from == "" ){
ipcreated = ciderValue.startAddress();
var bg1 = ipcreated.bigInteger();
bigInt = bg1.add(new BigInteger("1",10));
}
gateway = new v6.Address.fromBigInteger(bigInt).correctForm()
} else {
return false;
}
}
return gateway;
}
function isIPBoundToRange(range,ipAddress){
var IP = new v4.Address(ipAddress);
var IPRange = new v4.Address(range);
if(IP.isValid() === true && IPRange.isValid() === true){
return IP.isInSubnet(IPRange);
} else {
IP = new v6.Address(ipAddress);
IPRange = new v6.Address(range);
if(IP.isValid() === true && IPRange.isValid() === true){
return IP.isInSubnet(IPRange);
}
}
}
function isIPBoundToIPRange(rangeStart, rangeEnd, ipAddress){
var code = 2;//Out of Range
var IP = new v4.Address(ipAddress);
var IPRangeStart = new v4.Address(rangeStart);
var IPRangeEnd = new v4.Address(rangeEnd);
if(IP.isValid() === true && IPRangeStart.isValid() === true && IPRangeEnd.isValid()){
var IPInt = IP.bigInteger();
var startIPInt = IPRangeStart.bigInteger();
var endIPInt = IPRangeEnd.bigInteger();
if(startIPInt.compareTo(IPInt) <= 0 && endIPInt.compareTo(IPInt) >= 0){
code = 0;// In Range
}
} else {
IP = new v6.Address(ipAddress);
IPRangeStart = new v6.Address(rangeStart);
IPRangeEnd = new v6.Address(rangeEnd);
if(IP.isValid() === true && IPRangeStart.isValid() === true && IPRangeEnd.isValid()){
var IPInt = IP.bigInteger();
var startIPInt = IPRangeStart.bigInteger();
var endIPInt = IPRangeEnd.bigInteger();
if(startIPInt.compareTo(IPInt) <= 0 && endIPInt.compareTo(IPInt) >= 0){
code = 0;// In Range
}
} else {
code = 1;// Invalid IP
}
}
//code 0-> In Range.
//code 1-> Invalid IP.
//code 2-> Not in Range.
return code;
}
function isStartAddress(cidr, ipAddress){
var cidrAddress = new v4.Address(cidr);
if(cidrAddress.isValid() == true){
if(isIPv4(ipAddress)){
var cidrBigInt = new v4.Address(ipAddress).bigInteger();
var IPBigInt = new v4.Address(cidrAddress.startAddress().address).bigInteger();
if(cidrBigInt.compareTo(IPBigInt) == 0){
return true;
}
}
} else {
cidrAddress = new v6.Address(cidr);
if(cidrAddress.isValid() == true){
if(isIPv6(ipAddress)){
var cidrBigInt = new v6.Address(cidrAddress.startAddress().address).bigInteger();
var IPBigInt = new v6.Address(ipAddress).bigInteger();
if(cidrBigInt.compareTo(IPBigInt) == 0){
return true;
}
}
}
}
return false;
}
function isEndAddress(cidr, ipAddress){
var cidrAddress = new v4.Address(cidr);
if(cidrAddress.isValid() == true){
if(isIPv4(ipAddress)){
var cidrBigInt = new v4.Address(ipAddress).bigInteger();
var IPBigInt = new v4.Address(cidrAddress.endAddress().address).bigInteger();
if(cidrBigInt.compareTo(IPBigInt) == 0){
return true;
}
}
} else {
cidrAddress = new v6.Address(cidr);
if(cidrAddress.isValid() == true){
if(isIPv6(ipAddress)){
var cidrBigInt = new v6.Address(cidrAddress.endAddress().address).bigInteger();
var IPBigInt = new v6.Address(ipAddress).bigInteger();
if(cidrBigInt.compareTo(IPBigInt) == 0){
return true;
}
}
}
}
return false;
}
function isValidIP(ipAddress){
if(ipAddress == null)
return false;
var IP = new v4.Address(ipAddress);
if(IP.isValid() === true){
return true;
}
IP = new v6.Address(ipAddress);
if(IP.isValid() === true){
return true;
}
return false;
}
function isIPv4(ipAddress){
if(ipAddress == null)
return false;
var IP = new v4.Address(ipAddress);
if(IP.isValid() === true){
return true;
}
return false;
}
function isIPv6(ipAddress){
var IP;
if(ipAddress == null ) {
return false;
} else {
try {
IP = new v6.Address(ipAddress);
} catch (error) {
return false;
}
if(IP.isValid() === true){
return true;
}
}
return false;
}
function validip(ip) {
if (null != ip && "" != ip) {
ipsplit = ip.split("/");
if (null != ipsplit && "" != ipsplit) {
var regex = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/
if (regex.test(ipsplit[0]) == true) {
if (null != ipsplit[1] && "" != ipsplit[1]) {
try {
var subnet = parseInt(ipsplit[1]);
if (subnet >= 0 && subnet <= 32)
return true;
else
return false;
} catch (e) {
return false;
}
}
else {
return true;
}
}
else {
//invalid ip
return false;
}
}
else {
return false;
}
}
else {
//blank input - throw error
return false;
}
return false;
}
function convert_str_to_ip(ip_str) {
var temp_ip_str = ip_str.trim();
var ip_octet = "";
var ip_int = 0;
var ip_int = 0;
var max_bits = 32;
pos = temp_ip_str.indexOf('.');
while (-1 != pos) {
ip_octet = temp_ip_str.substr(0, pos);
ip_int += (parseInt(ip_octet) << (max_bits - 8));
max_bits -= 8;
temp_ip_str = temp_ip_str.slice(pos + 1);
pos = temp_ip_str.indexOf('.');
}
ip_int += (parseInt(temp_ip_str) << (max_bits - 8));
return ip_int;
}
function get_subnet_mask(ip_str) {
var pos = ip_str.indexOf('/');
var subnet_mask = 0;
if (-1 == pos) {
return;
}
var mask = parseInt(ip_str.slice(pos + 1));
for (var i = 1; i <= mask; i++) {
subnet_mask += (1 << (32 - i));
}
return convert_ip_to_str(subnet_mask);
}
function convert_ip_to_str(ip_addr) {
ip_addr = ip_addr;
var ip_str = "";
var max_bits = 32;
while (max_bits > 0) {
ip_str += ((ip_addr >> (max_bits - 8)) & 0xff);
max_bits -= 8;
if (max_bits) {
ip_str += ".";
}
}
return ip_str;
}
function ip_range_add(ip_str, ip_list) {
var pos = ip_str.indexOf('-');
var ip_str1 = ip_str.substr(0, pos);
var ip_str2 = ip_str.slice(pos + 1);
var ip_addr1 = convert_str_to_ip(ip_str1);
var ip_addr2 = convert_str_to_ip(ip_str2);
if (ip_addr2 < ip_addr1) {
ip_list = "";
alert("ip2 < ip1")
return 0;
}
while (ip_addr1 <= ip_addr2) {
/* Skip FF and 0 as 4th Octet */
ip_fourth_octet = ip_addr1 & 0xFF;
if (!(ip_fourth_octet) || (0xFF == ip_fourth_octet)) {
ip_addr1++;
continue;
}
ip_list.push(convert_ip_to_str(ip_addr1));
ip_addr1++;
}
return 1;
}
function ip_range_with_mask(ip_str, ip_list) {
var i;
var ip_addr = 0;
var subnet_mask = 0;
var ip_addr_octet = [];
var host_all_1s = 0;
var subnet_octet = [];
var pos = ip_str.indexOf('/');
var mask = parseInt(ip_str.slice(pos + 1));
var str = ip_str.substr(0, pos);
var max_no_subnet = 0;
var no_host = 0;
var subnet_mask = 0;
for (i = 1; i <= mask; i++) {
subnet_mask += (1 << (32 - i));
}
ip_addr = convert_str_to_ip(str);
ip_addr_octet[0] = (ip_addr >> 24) & 0xFF;
ip_addr_octet[1] = (ip_addr >> 16) & 0xFF;
ip_addr_octet[2] = (ip_addr >> 8) & 0xFF;
ip_addr_octet[3] = (ip_addr >> 0) & 0xFF;
var first_ip = subnet_mask && ip_addr;
//console.log("first :", convert_ip_to_str(first_ip));
for (i = 0; i < 32 - mask; i++) {
host_all_1s += (1 << i);
}
var last_ip = ip_addr | host_all_1s;
for (i = first_ip + 1; i <= last_ip - 1; i++) {
str = convert_ip_to_str(i);
ip_list.push(str);
}
//console.log("Last:", convert_ip_to_str(last_ip));
return ip_list;
}
function ip_range_add_raw(ip_str, ip_list) {
/* Check for - or / character */
var pos = ip_str.indexOf('/');
if (-1 == pos) {
pos = ip_str.indexOf('-');
if (-1 == pos) {
ip_list.push(ip_str);
} else {
ip_range_add(ip_str, ip_list);
}
} else {
ip_range_with_mask(ip_str, ip_list);
}
}
function ip_range(ip_str, ip_list) {
/* First divide the string into comma seperated strings and store all those
* in some list
*/
var i = 0;
var pos = -1;
var raw_ip_list = [];
var ip_str_slice = "";
ip_str = ip_str.trim();
if (!ip_str.length) {