-
Notifications
You must be signed in to change notification settings - Fork 1
/
SubnetCalcAppDelegate.swift
1817 lines (1639 loc) · 66.7 KB
/
SubnetCalcAppDelegate.swift
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
//
// SubnetCalcAppDelegate.swift
// SubnetCalc
//
// Created by Julien Mulot on 22/11/2020.
//
import Foundation
import Cocoa
import CoreData
@main
class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate, NSTableViewDataSource {
//*******************
//Private Constants
//*******************
private enum Constants {
static let defaultIP: String = "10.0.0.0"
static let defaultIPv6Mask: String = "64"
static let defaultIPv6to4Mask: Int = 96
static let maxAddrHistory: Int = 30
static let BUFFER_LINES:Int = 200000000
static let NETWORK_BITS_MIN_CLASSLESS:Int = 1
static let NETWORK_BITS_MIN:Int = 8
static let NETWORK_BITS_MAX:Int = 32
}
//*******************
//General UI elements
//*******************
@IBOutlet var window: NSWindow!
@IBOutlet var addrField: NSComboBox!
@IBOutlet var exportButton: NSPopUpButton!
@IBOutlet var tabView: NSTabView!
@IBOutlet var darkModeMenu: NSMenuItem!
@IBOutlet var NSApp: NSApplication!
//****************
//IPv4 UI elements
//****************
@IBOutlet var classBinaryMap: NSTextField!
@IBOutlet var classBitMap: NSTextField!
@IBOutlet var classHexaMap: NSTextField!
@IBOutlet var classType: NSPopUpButton!
@IBOutlet var maskBitsCombo: NSComboBox!
@IBOutlet var maxHostsBySubnetCombo: NSComboBox!
@IBOutlet var maxSubnetsCombo: NSComboBox!
@IBOutlet var subnetBitsCombo: NSComboBox!
@IBOutlet var subnetBroadcast: NSTextField!
@IBOutlet var subnetHostAddrRange: NSTextField!
@IBOutlet var subnetId: NSTextField!
@IBOutlet var subnetMaskCombo: NSComboBox!
@IBOutlet var subnetsHostsView: NSTableView!
@IBOutlet var supernetMaskBitsCombo: NSComboBox!
@IBOutlet var supernetMaskCombo: NSComboBox!
@IBOutlet var supernetMaxCombo: NSComboBox!
@IBOutlet var supernetMaxAddr: NSComboBox!
@IBOutlet var supernetMaxSubnetsCombo: NSComboBox!
@IBOutlet var supernetRoute: NSTextField!
@IBOutlet var supernetAddrRange: NSTextField!
@IBOutlet var subnetBitsSlide: NSSlider!
@IBOutlet var bitsOnSlide: NSTextField!
@IBOutlet var tabViewClassLess: NSButton!
@IBOutlet var wildcard: NSButton!
@IBOutlet var dotted: NSButton!
@IBOutlet var maskBitsFLSMCombo: NSComboBox!
@IBOutlet var viewFLSM: NSTableView!
@IBOutlet var slideFLSM: NSSlider!
@IBOutlet var maxSubnetsFLSM: NSTextField!
@IBOutlet var maxHostsBySubnetFLSM: NSTextField!
@IBOutlet var maxHostsFLSM: NSTextField!
@IBOutlet var maskBitsVLSMCombo: NSComboBox!
@IBOutlet var requiredHostsVLSM: NSTextField!
@IBOutlet var subnetNameVLSM: NSTextField!
@IBOutlet var viewVLSM: NSTableView!
//****************
//IPv6 UI elements
//****************
@IBOutlet var ipv6Address: NSTextField!
@IBOutlet var ipv6to4Address: NSTextField!
@IBOutlet var ipv6maskBitsCombo: NSComboBox!
@IBOutlet var ipv6SubnetsCombo: NSComboBox!
@IBOutlet var ipv6maxHostsCombo: NSComboBox!
@IBOutlet var ipv6Network: NSTextField!
@IBOutlet var ipv6Range: NSTextField!
@IBOutlet var ipv6Type: NSTextField!
@IBOutlet var ipv6HexaID: NSTextField!
@IBOutlet var ipv6Decimal: NSTextField!
@IBOutlet var ipv6Arpa: NSTextField!
@IBOutlet var ipv6Compact: NSButton!
@IBOutlet var ipv6to4Box: NSBox!
//*******************
//Private global vars
//*******************
private var ipsc: IPSubnetCalc?
private var subnetsVLSM = [(Int, String, String)]()
private var globalMaskVLSM: UInt32!
private var container: NSPersistentContainer!
private var history = [AddrHistory]()
//**********************
//Private IPv4 functions
//**********************
/**
Init IPv4 CIDR Tab combo lists
*/
private func initCIDRTab() {
for bits in (1...32) {
supernetMaskBitsCombo.addItem(withObjectValue: String(bits))
}
for index in (0...31).reversed() {
supernetMaskCombo.addItem(withObjectValue: IPSubnetCalc.dottedDecimal(ipAddress: (IPSubnetCalc.Constants.addr32Full << index)))
}
for index in (0...31) {
supernetMaxCombo.addItem(withObjectValue: NSDecimalNumber(decimal: pow(2, index)).stringValue)
}
for index in (1...31) {
supernetMaxAddr.addItem(withObjectValue: NSDecimalNumber(decimal: (pow(2, index) - 2)).stringValue)
}
for index in (0...32) {
supernetMaxSubnetsCombo.addItem(withObjectValue: NSDecimalNumber(decimal: pow(2, index)).stringValue)
}
}
/**
Init IPv4 Tab combo lists
*/
private func initSubnetsTab() {
for index in (0...24).reversed() {
if (wildcard.state == NSControl.StateValue.on) {
subnetMaskCombo.addItem(withObjectValue: IPSubnetCalc.dottedDecimal(ipAddress: ~(IPSubnetCalc.Constants.addr32Full << index)))
}
else {
subnetMaskCombo.addItem(withObjectValue: IPSubnetCalc.dottedDecimal(ipAddress: (IPSubnetCalc.Constants.addr32Full << index)))
}
}
for bits in (8...32) {
maskBitsCombo.addItem(withObjectValue: String(bits))
}
for bits in (0...24) {
subnetBitsCombo.addItem(withObjectValue: String(bits))
}
for index in (1...24) {
maxHostsBySubnetCombo.addItem(withObjectValue: NSDecimalNumber(decimal: (pow(2, index) - 2)).stringValue)
}
for index in (0...24) {
maxSubnetsCombo.addItem(withObjectValue: NSDecimalNumber(decimal: pow(2, index)).stringValue)
}
classBitMap.stringValue = "nnnnnnnn.hhhhhhhh.hhhhhhhh.hhhhhhhh"
classBinaryMap.stringValue = "00000001.00000000.00000000.00000000"
classHexaMap.stringValue = "01.00.00.00"
}
/**
Init FLSM Tab
*/
private func initFLSMTab() {
for bits in (8...32) {
maskBitsFLSMCombo.addItem(withObjectValue: String(bits))
}
slideFLSM.integerValue = 1
}
/**
Init VLSM Tab
*/
private func initVLSMTab() {
for bits in (8...32) {
maskBitsVLSMCombo.addItem(withObjectValue: String(bits))
}
}
/**
Init mask bits number of the Mask Bits slide on Subnets/Hosts Tab
*/
private func bitsOnSlidePos()
{
var coordLabel = bitsOnSlide.frame
let coordSlider = subnetBitsSlide.frame
coordLabel.origin.x = coordSlider.origin.x - (coordLabel.size.width / 2) + (subnetBitsSlide.knobThickness / 2) + (((coordSlider.size.width - (subnetBitsSlide.knobThickness / 2)) / CGFloat(subnetBitsSlide.numberOfTickMarks)) * CGFloat(subnetBitsSlide.floatValue - 1.0))
bitsOnSlide.frame = coordLabel
}
/**
Select Address Class Type on IPv4 Tab
- Parameter c: Class type of the IPv4 address: A, B, C, D or E
*/
private func initClassInfos(_ c: String)
{
if (c == "A")
{
classType.selectItem(at: 0)
}
else if (c == "B")
{
classType.selectItem(at: 1)
}
else if (c == "C")
{
classType.selectItem(at: 2)
}
else if (c == "D")
{
classType.selectItem(at: 3)
}
else if (c == "E")
{
classType.selectItem(at: 4)
}
}
/**
Split the given Address to its IP and mask
- Parameters address: IP Address with or without its mask as /XX notation
- Returns:
First String: IP Address. Second String: mask bits number
*/
private func splitAddrMask(address: String) -> (String, String?) {
let ipInfo = address.split(separator: "/")
if ipInfo.count == 2 {
return (String(ipInfo[0]), String(ipInfo[1]))
}
else if ipInfo.count > 2 {
print("Invalid IP format: \(ipInfo)")
return ("", nil)
}
return (address, nil)
}
/**
Generate the Binary Map, Bits Maps and Hexa Map of the current IP
*/
private func doAddressMap() {
if (ipsc != nil) {
self.initClassInfos(ipsc!.netClass())
if (dotted.state == NSControl.StateValue.on) {
classBitMap.stringValue = ipsc!.bitMap(dotted: true)
classBinaryMap.stringValue = ipsc!.binaryMap(dotted: true)
classHexaMap.stringValue = ipsc!.hexaMap(dotted: true)
}
else {
classBitMap.stringValue = ipsc!.bitMap(dotted: false)
classBinaryMap.stringValue = ipsc!.binaryMap(dotted: false)
classHexaMap.stringValue = ipsc!.hexaMap(dotted: false)
}
}
}
/**
Generate infos of the IPv4 Tab for the current IP and mask
*/
private func doSubnet()
{
if (ipsc != nil) {
subnetBitsCombo.selectItem(withObjectValue: String(ipsc!.subnetBits()))
maskBitsCombo.selectItem(withObjectValue: String(ipsc!.maskBits))
maxSubnetsCombo.selectItem(withObjectValue: String(ipsc!.maxSubnets()))
maxHostsBySubnetCombo.selectItem(withObjectValue: String(ipsc!.maxHosts()))
subnetId.stringValue = ipsc!.subnetId()
subnetBroadcast.stringValue = ipsc!.subnetBroadcast()
subnetHostAddrRange.stringValue = ipsc!.subnetRange()
if (wildcard.state == NSControl.StateValue.on) {
subnetMaskCombo.selectItem(withObjectValue: ipsc!.wildcardMask())
}
else {
subnetMaskCombo.selectItem(withObjectValue: ipsc!.subnetMask())
}
}
}
/**
Generate infos of the Subnets/Hosts Tab for the current IP and mask
*/
private func doSubnetHost()
{
if (ipsc != nil) {
bitsOnSlide.stringValue = String(ipsc!.maskBits)
subnetBitsSlide.intValue = Int32(ipsc!.maskBits)
self.bitsOnSlidePos()
subnetsHostsView.reloadData()
}
}
/**
Generate infos of the FLSM Tab for the current IP and mask
*/
private func doFLSM()
{
if (ipsc != nil) {
maskBitsFLSMCombo.selectItem(withObjectValue: String(ipsc!.maskBits))
if (ipsc!.maskBits <= 29) {
slideFLSM.numberOfTickMarks = (30 - ipsc!.maskBits)
slideFLSM.maxValue = Double(30 - ipsc!.maskBits)
self.maxSubnetsFLSM.stringValue = NSDecimalNumber(decimal: (pow(2, slideFLSM.integerValue))).stringValue
self.maxHostsBySubnetFLSM.stringValue = NSDecimalNumber(decimal: (pow(2, (32 - (ipsc!.maskBits + slideFLSM.integerValue)))) - 2).stringValue
self.maxHostsFLSM.stringValue = NSDecimalNumber(decimal: ((pow(2, (32 - (ipsc!.maskBits + slideFLSM.integerValue)))) - 2) * (pow(2, slideFLSM.integerValue))).stringValue
}
else {
self.maxSubnetsFLSM.stringValue = ""
self.maxHostsBySubnetFLSM.stringValue = ""
self.maxHostsFLSM.stringValue = ""
}
viewFLSM.reloadData()
}
}
/**
Generate infos of the VLSM Tab for the current IP and mask if there are some Hosts requirements
*/
private func doVLSM()
{
if (ipsc != nil) {
//print("doVLSM")
maskBitsVLSMCombo.selectItem(withObjectValue: String(ipsc!.maskBits))
var maskVLSM = ~IPSubnetCalc.digitize(maskbits: ipsc!.maskBits)! + 1
if (subnetsVLSM.count != 0) {
var fitsRequirements = true
for index in (0...(subnetsVLSM.count - 1)) {
let maskbits = subnetsVLSM[index].0
//print("Mask VLSM: \(IPSubnetCalc.digitize(ipAddress: maskVLSM)) Maskbits: \(IPSubnetCalc.digitize(ipAddress: ~IPSubnetCalc.numerize(maskbits: maskbits)))")
if (maskVLSM > ~IPSubnetCalc.digitize(maskbits: maskbits)!) {
maskVLSM = maskVLSM - (~IPSubnetCalc.digitize(maskbits: maskbits)! + 1)
//print("Mask AFTER VLSM: \(IPSubnetCalc.digitize(ipAddress: maskVLSM))")
}
else {
fitsRequirements = false
}
}
if (fitsRequirements) {
globalMaskVLSM = maskVLSM
}
else {
myAlert(message: "Mask bits too small", info: "Mask bits doest not suit all VLSM hosts requirements")
}
}
else {
globalMaskVLSM = maskVLSM
}
viewVLSM.reloadData()
}
}
/**
Generate infos of the CIDR Tab
- Parameter maskbits: mask bits number
*/
private func doCIDR(maskbits: Int? = nil)
{
if (ipsc != nil) {
if (maskbits == nil) {
supernetMaskBitsCombo.selectItem(withObjectValue: String(ipsc!.maskBits))
supernetMaskCombo.selectItem(withObjectValue: ipsc!.subnetMask())
supernetMaxSubnetsCombo.selectItem(withObjectValue: String(ipsc!.maxCIDRSubnets()))
supernetMaxAddr.selectItem(withObjectValue: String(ipsc!.maxHosts()))
supernetMaxCombo.selectItem(withObjectValue: String(ipsc!.maxCIDRSupernet()))
supernetRoute.stringValue = ipsc!.subnetId() + "/" + String(ipsc!.maskBits)
supernetAddrRange.stringValue = ipsc!.subnetCIDRRange()
}
else {
let ipsc_tmp = IPSubnetCalc(ipAddress: ipsc!.ipv4Address, maskbits: maskbits!)
if (ipsc_tmp != nil) {
supernetMaskBitsCombo.selectItem(withObjectValue: String(ipsc_tmp!.maskBits))
supernetMaskCombo.selectItem(withObjectValue: ipsc_tmp!.subnetMask())
supernetMaxSubnetsCombo.selectItem(withObjectValue: String(ipsc_tmp!.maxCIDRSubnets()))
supernetMaxAddr.selectItem(withObjectValue: String(ipsc_tmp!.maxHosts()))
supernetMaxCombo.selectItem(withObjectValue: String(ipsc_tmp!.maxCIDRSupernet()))
supernetRoute.stringValue = ipsc_tmp!.subnetId() + "/" + String(ipsc_tmp!.maskBits)
supernetAddrRange.stringValue = ipsc_tmp!.subnetCIDRRange()
}
}
}
}
/**
Display an alert window pop-up with customs message and info
- Parameters:
- message: Main displayed message
- info: message info
*/
private func myAlert(message: String, info: String)
{
let alert = NSAlert()
alert.addButton(withTitle: "OK")
alert.messageText = message
alert.alertStyle = NSAlert.Style.warning
alert.informativeText = info
alert.runModal()
}
/**
Generate infos for all IPv4 tabs.
Check if there is are current IP address and mask otherwise take the default IP and mask.
Check if the IP address and mask are valid.
- Throws: an invalid IP or invalid mask error with a message explaining the reason
*/
private func doIPSubnetCalc() throws
{
var ipaddr: String
var ipmask: String?
if (addrField.stringValue.isEmpty) {
if (ipsc == nil)
{
addrField.stringValue = Constants.defaultIP
ipaddr = Constants.defaultIP
ipmask = nil
}
else {
addrField.stringValue = ipsc!.ipv4Address
ipaddr = ipsc!.ipv4Address
ipmask = String(ipsc!.maskBits)
}
}
else {
(ipaddr, ipmask) = splitAddrMask(address: addrField.stringValue)
addrField.stringValue = ipaddr
do {
try IPSubnetCalc.validateIPv6(ipAddress: ipaddr, mask: Int(ipmask ?? Constants.defaultIPv6Mask))
if (ipsc != nil) {
ipaddr = ipsc!.ipv4Address
}
if (ipmask != nil) {
if (Int(ipmask!)! >= (Constants.defaultIPv6to4Mask + 8)) {
ipmask = String(Int(ipmask!)! - Constants.defaultIPv6to4Mask)
}
else {
ipmask = "8"
}
}
}
catch {
}
if (ipmask == nil && ipsc != nil) {
ipmask = String(ipsc!.maskBits)
}
}
do {
try IPSubnetCalc.validateIPv4(ipAddress: ipaddr, mask: ipmask)
//print("IP Address: \(ipaddr) mask: \(ipmask)")
if (ipmask == nil) {
ipsc = IPSubnetCalc(ipaddr)
}
else {
ipsc = IPSubnetCalc(ipAddress: ipaddr, maskbits: Int(ipmask!)!)
}
if (ipsc != nil) {
self.doAddressMap()
self.doSubnet()
self.doSubnetHost()
self.doCIDR()
self.doIPv6()
self.doFLSM()
self.doVLSM()
}
}
catch SubnetCalcError.invalidIPv4(let info) {
myAlert(message: "Invalid IPv4 Address", info: info)
throw SubnetCalcError.invalidIPv4(info)
}
catch SubnetCalcError.invalidIPv4Mask(let info) {
myAlert(message: "Invalid IPv4 Mask", info: info)
throw SubnetCalcError.invalidIPv4Mask(info)
}
catch {
myAlert(message: "Unknown invalid error", info: "\(ipaddr)/\(ipmask ?? "") Error: \(error)")
throw error
}
}
/**
Compute IPv4 or IPv6 infos depending of IP address format in IP address field
- Throws: an invalid IP or invalid mask error with a message explaining the reason
*/
private func doCalc() throws
{
if (addrField.stringValue.contains(":")) {
do {
try self.doIPv6SubnetCalc()
tabView.selectTabViewItem(at: 5)
}
catch {
throw error
}
}
else {
do {
try self.doIPSubnetCalc()
tabView.selectTabViewItem(at: 0)
}
catch {
throw error
}
}
}
/**
Save the address IP field history for future App sessions
*/
private func saveHistory() {
if container.viewContext.hasChanges {
do {
try container.viewContext.save()
} catch {
print("An error occurred while saving: \(error)")
}
}
}
/**
Load in the address IP field the history of previous App sessions
*/
private func loadHistory() {
container = NSPersistentContainer(name: "SubnetCalc")
container.loadPersistentStores { storeDescription, error in
if let error = error {
print("Unresolved error \(error)")
}
}
do {
history = try container.viewContext.fetch(AddrHistory.fetchRequest())
//print("Got \(history.count) items")
for item in history {
//print(item.address)
addrField.addItem(withObjectValue: item.address)
}
} catch {
print("Fetch history failed")
}
}
//**********************
//Private IPv6 functions
//**********************
/**
Init IPv6 Tab combo lists
*/
private func initIPv6Tab() {
var total = Decimal()
var number: Decimal = 2
for index in (1...128) {
ipv6maskBitsCombo.addItem(withObjectValue: String(index))
}
for index in (0...127) {
NSDecimalPower(&total, &number , index, NSDecimalNumber.RoundingMode.plain)
ipv6maxHostsCombo.addItem(withObjectValue: total)
}
}
/**
Generate info for IPv6 tab
*/
private func doIPv6()
{
if (ipsc != nil) {
var total = Decimal()
var number: Decimal = 2
var typeConv: String
if (ipv6Compact.state == NSControl.StateValue.on) {
ipv6Address.stringValue = IPSubnetCalc.compactAddressIPv6(ipAddress: ipsc!.ipv6Address)
ipv6Network.stringValue = IPSubnetCalc.compactAddressIPv6(ipAddress: ipsc!.networkIPv6())
}
else {
ipv6Address.stringValue = IPSubnetCalc.fullAddressIPv6(ipAddress: ipsc!.ipv6Address)
ipv6Network.stringValue = IPSubnetCalc.fullAddressIPv6(ipAddress: ipsc!.networkIPv6())
}
(ipv6to4Address.stringValue, typeConv) = IPSubnetCalc.convertIPv6toIPv4(ipAddress: ipsc!.ipv6Address)
ipv6to4Box.title = "IPv4 conversion" + " (\(typeConv))"
ipv6maskBitsCombo.selectItem(withObjectValue: String(ipsc!.ipv6MaskBits))
ipv6maxHostsCombo.selectItem(withObjectValue: ipsc!.totalIPAddrIPv6())
ipv6Range.stringValue = ipsc!.networkRangeIPv6()
ipv6Type.stringValue = ipsc!.resBlockIPv6() ?? "None"
ipv6HexaID.stringValue = ipsc!.hexaIDIPv6()
ipv6Decimal.stringValue = ipsc!.dottedDecimalIPv6()
ipv6Arpa.stringValue = ipsc!.ip6ARPA()
ipv6SubnetsCombo.removeAllItems()
for index in (1...ipsc!.ipv6MaskBits).reversed() {
NSDecimalPower(&total, &number , ipsc!.ipv6MaskBits - index, NSDecimalNumber.RoundingMode.plain)
if (total == 1) {
ipv6SubnetsCombo.addItem(withObjectValue: "/\(index)\t\(total) network")
}
else {
ipv6SubnetsCombo.addItem(withObjectValue: "/\(index)\t\(total) networks")
}
}
ipv6SubnetsCombo.selectItem(at: 0)
}
}
/**
Generate infos for IPv6 tab
Genrate infos also for all IPv4 tabs based on the converted IPv4 address
Check if the IPv6 address and mask are valid.
- Throws: an invalid IP or invalid mask error with a message explaining the reason
*/
private func doIPv6SubnetCalc() throws
{
var ipaddr: String
var ipmask: String?
(ipaddr, ipmask) = splitAddrMask(address: addrField.stringValue)
addrField.stringValue = ipaddr
do {
try IPSubnetCalc.validateIPv4(ipAddress: ipaddr, mask: ipmask)
if (ipsc != nil) {
ipaddr = ipsc!.ipv6Address
//print("doIPv6SubnetCalc ipaddr to ipv6 : \(ipsc!.ipv6Address)")
}
}
catch { }
if (ipmask == nil) {
if (ipsc != nil) {
ipmask = String(ipsc!.ipv6MaskBits)
//print("doIPv6SubnetCalc ipmask ipv6 : \(ipsc!.ipv6MaskBits)")
}
else {
ipmask = Constants.defaultIPv6Mask
}
}
else if (Int(ipmask!) == nil) {
myAlert(message: "Invalid IPv6 mask", info: "\(ipmask!) is not an integer")
return
}
do {
try IPSubnetCalc.validateIPv6(ipAddress: ipaddr, mask: Int(ipmask!))
//print("IP Address: \(ipaddr) mask: \(ipmask)")
ipsc = IPSubnetCalc(ipv6: ipaddr, maskbits: Int(ipmask!)!)
if (ipsc != nil) {
self.doAddressMap()
self.doSubnet()
self.doSubnetHost()
self.doCIDR()
self.doFLSM()
self.doVLSM()
self.doIPv6()
}
}
catch SubnetCalcError.invalidIPv6(let info) {
myAlert(message: "Invalid IPv6 Address", info: info)
throw SubnetCalcError.invalidIPv6(info)
}
catch SubnetCalcError.invalidIPv6Mask(let info) {
myAlert(message: "Invalid IPv6 Mask", info: info)
throw SubnetCalcError.invalidIPv6Mask(info)
}
catch {
myAlert(message: "Unknown invalid IPv6 error", info: "\(ipaddr)/\(ipmask ?? "") Error: \(error)")
throw error
}
}
//***************
//IPv4 UI actions
//***************
/**
Triggered when the user has changed the Network Class type in the IPv4 tab
- Parameter sender: Class type selected by the user
*/
@IBAction func changeAddrClassType(_ sender: AnyObject)
{
if (sender.indexOfSelectedItem() == 0)
{
classBitMap.stringValue = "nnnnnnnn.hhhhhhhh.hhhhhhhh.hhhhhhhh"
classBinaryMap.stringValue = "00000001.00000000.00000000.00000000"
classHexaMap.stringValue = "01.00.00.00"
}
else if (sender.indexOfSelectedItem() == 1)
{
classBitMap.stringValue = "nnnnnnnn.nnnnnnnn.hhhhhhhh.hhhhhhhh"
classBinaryMap.stringValue = "10000000.00000000.00000000.00000000"
classHexaMap.stringValue = "80.00.00.00"
}
else if (sender.indexOfSelectedItem() == 2)
{
classBitMap.stringValue = "nnnnnnnn.nnnnnnnn.nnnnnnnn.hhhhhhhh"
classBinaryMap.stringValue = "11000000.00000000.00000000.00000000"
classHexaMap.stringValue = "C0.00.00.00"
}
else if (sender.indexOfSelectedItem() == 3)
{
classBitMap.stringValue = "hhhhhhhh.hhhhhhhh.hhhhhhhh.hhhhhhhh"
classBinaryMap.stringValue = "11100000.00000000.00000000.00000000"
classHexaMap.stringValue = "E0.00.00.00"
}
else if (sender.indexOfSelectedItem() == 4)
{
classBitMap.stringValue = "hhhhhhhh.hhhhhhhh.hhhhhhhh.hhhhhhhh"
classBinaryMap.stringValue = "11110000.00000000.00000000.00000000"
classHexaMap.stringValue = "F0.00.00.00"
}
}
/**
Triggered when the user has changed the Max Hosts / Subnet item in the IPv4 tab
- Parameter sender: selected item of the Max Hosts / Subnet list
*/
@IBAction func changeMaxHosts(_ sender: AnyObject)
{
if (ipsc == nil)
{
ipsc = IPSubnetCalc(Constants.defaultIP)
}
if (sender.indexOfSelectedItem != -1) {
ipsc!.maskBits = 31 - sender.indexOfSelectedItem()
do {
try self.doIPSubnetCalc()
}
catch {}
}
else {
myAlert(message: "Invalid Max Hosts", info: "Bad selection")
return
}
}
/**
Triggered when the user has changed the Max Subnets item in the IPv4 tab
- Parameter sender: selected item of the Max Subnets list
*/
@IBAction func changeMaxSubnets(_ sender: AnyObject)
{
if (ipsc == nil)
{
ipsc = IPSubnetCalc(Constants.defaultIP)
}
if (sender.indexOfSelectedItem != -1) {
ipsc!.maskBits = 8 + sender.indexOfSelectedItem()
do {
try self.doIPSubnetCalc()
}
catch {}
}
else {
myAlert(message: "Invalid Max Subnets", info: "Bad selection")
return
}
}
/**
Triggered when the user has changed the Subnets Bits item in the IPv4 tab
- Parameter sender: selected item of the Subnets Bits list
*/
@IBAction func changeSubnetBits(_ sender: AnyObject)
{
if (ipsc == nil)
{
ipsc = IPSubnetCalc(Constants.defaultIP)
}
if (sender.objectValueOfSelectedItem as? String) != nil {
ipsc!.maskBits = sender.intValue + ipsc!.netBits()
do {
try self.doIPSubnetCalc()
}
catch {}
}
else {
myAlert(message: "Invalid Subnet Bits", info: "Bad selection")
return
}
}
/**
Triggered when the user has changed the Subnet Mask item in the IPv4 tab
- Parameter sender: selected item of the Subnet Mask list
*/
@IBAction func changeSubnetMask(_ sender: AnyObject)
{
if (ipsc == nil)
{
ipsc = IPSubnetCalc(Constants.defaultIP)
}
// Check cast as String needed ?
if let maskStr = sender.objectValueOfSelectedItem as? String {
if let mask:UInt32 = IPSubnetCalc.digitize(ipAddress: maskStr) {
if (wildcard.state == NSControl.StateValue.on) {
ipsc!.maskBits = IPSubnetCalc.maskBits(mask: ~mask)
}
else {
ipsc!.maskBits = IPSubnetCalc.maskBits(mask: mask)
//print("changeSubnetMask object value : \(str)")
}
do {
try self.doIPSubnetCalc()
}
catch {}
}
else {
myAlert(message: "Invalid Subnet Mask", info: "Bad format \(maskStr)")
return
}
}
else {
myAlert(message: "Invalid Subnet Mask", info: "Bad selection")
return
}
}
/**
Triggered when the user has changed the Mask Bits item in the IPv4 tab
- Parameter sender: selected item of the Mask Bits list
*/
@IBAction func changeMaskBits(_ sender: AnyObject)
{
if (ipsc == nil)
{
ipsc = IPSubnetCalc(Constants.defaultIP)
}
if (sender.objectValueOfSelectedItem as? String) != nil {
ipsc!.maskBits = sender.intValue
do {
try self.doIPSubnetCalc()
}
catch {}
}
else {
myAlert(message: "Invalid Mask Bits", info: "Bad selection")
return
}
}
/**
Triggered when the user has changed the Mask Bits item in the CIDR tab
- Parameter sender: selected item of the Mask Bits list
*/
@IBAction func changeSupernetMaskBits(_ sender: AnyObject)
{
if (ipsc == nil)
{
ipsc = IPSubnetCalc(Constants.defaultIP)
}
if (sender.objectValueOfSelectedItem as? String) != nil {
let classType = ipsc!.netClass()
var result: Int = -1
if (classType == "A") {
result = sender.intValue - 8
}
else if (classType == "B") {
result = sender.intValue - 16
}
else if (classType == "C") {
result = sender.intValue - 24
}
if (result >= 0) {
ipsc!.maskBits = sender.intValue
do {
try self.doIPSubnetCalc()
}
catch {}
}
else {
doCIDR(maskbits: sender.intValue)
}
}
else {
myAlert(message: "Invalid CIDR Mask Bits", info: "Bad selection")
return
}
}
/**
Triggered when the user has changed the Mask item in the CIDR tab
- Parameter sender: selected item of the Mask list
*/
@IBAction func changeSupernetMask(_ sender: AnyObject)
{
if (ipsc == nil)
{
ipsc = IPSubnetCalc(Constants.defaultIP)
}
if let maskStr = sender.objectValueOfSelectedItem as? String {
if let mask:UInt32 = IPSubnetCalc.digitize(ipAddress: maskStr) {
let maskbits:Int = IPSubnetCalc.maskBits(mask: mask)
let classType = ipsc!.netClass()
var result: Int = -1
if (classType == "A") {
result = maskbits - 8
}
else if (classType == "B") {
result = maskbits - 16
}
else if (classType == "C") {
result = maskbits - 24
}
if (result >= 0) {
ipsc!.maskBits = maskbits
do {
try self.doIPSubnetCalc()
}
catch {}
}
else {
doCIDR(maskbits: maskbits)
}
}
else {
myAlert(message: "Invalid CIDR Mask", info: "Bad format \(maskStr)")
return
}
}
else {
myAlert(message: "Invalid CIDR Mask", info: "Bad selection")
return
}
}
/**
Triggered when the user has changed the Max Supernets item in the CIDR tab
- Parameter sender: selected item of the Max Supernets list
*/
@IBAction func changeSupernetMax(_ sender: AnyObject)
{
if (ipsc == nil)
{
ipsc = IPSubnetCalc(Constants.defaultIP)
}
if (sender.indexOfSelectedItem != -1) {
let classType = ipsc!.netClass()
var result: Int = -1
if (classType == "A") {
result = 8 - sender.indexOfSelectedItem
}
else if (classType == "B") {
result = 16 - sender.indexOfSelectedItem
}
else if (classType == "C") {
result = 24 - sender.indexOfSelectedItem
}
if (result > 0) {
doCIDR(maskbits: result)
}
else {
myAlert(message: "Invalid Max Supernets", info: "Value too high")
return
}
}
else {
myAlert(message: "Invalid Max Supernets", info: "Bad selection")
return
}
}
/**
Triggered when the user has changed the Max Addresses item in the CIDR tab
- Parameter sender: selected item of the Max Addresses list
*/
@IBAction func changeSupernetMaxAddr(_ sender: AnyObject)
{
if (ipsc == nil)
{
ipsc = IPSubnetCalc(Constants.defaultIP)
}
if (sender.indexOfSelectedItem != -1) {
if ((32 - sender.indexOfSelectedItem - 1) < 32) {
doCIDR(maskbits: (32 - sender.indexOfSelectedItem - 1))
}
else {
myAlert(message: "Invalid Max Adresses", info: "Bad value")
return