Skip to content

Commit c9ff303

Browse files
committed
Rename isValidIPv6 function to validateIPv6
Handle custom errors in validateIPv6 function
1 parent f8b3915 commit c9ff303

File tree

2 files changed

+48
-35
lines changed

2 files changed

+48
-35
lines changed

IPSubnetcalc.swift

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -831,14 +831,14 @@ class IPSubnetCalc: NSObject {
831831
Boolean if the given IPv6 address is valid or not
832832

833833
*/
834-
static func isValidIPv6(ipAddress: String, mask: Int?) -> Bool {
834+
static func validateIPv6(ipAddress: String, mask: Int?) throws {
835835
var ip4Hex: [String]?
836836
var hex: UInt16?
837837

838838
if mask != nil {
839839
if (mask! < 1 || mask! > 128) {
840840
print("mask \(mask!) invalid")
841-
return false
841+
throw SubnetCalcError.invalidIPv6Mask("mask \(mask!) must be between 1 and 128")
842842
}
843843
}
844844
else {
@@ -848,43 +848,43 @@ class IPSubnetCalc: NSObject {
848848
ip4Hex = ipAddress.components(separatedBy: ":")
849849
if (ip4Hex == nil) {
850850
//print("\(ipAddress) invalid")
851-
return false
851+
throw SubnetCalcError.invalidIPv6("IPv6 address must contain :")
852852
}
853853
if (ip4Hex!.count != 8) {
854854
//print("no 8 hex")
855855
if (ipAddress.contains("::"))
856856
{
857857
if (ipAddress.components(separatedBy: "::").count > 2) {
858858
//print("too many '::'")
859-
return false
859+
throw SubnetCalcError.invalidIPv6("too many ::")
860860
}
861861
}
862862
else {
863863
//print("IPv6 \(ipAddress) bad format")
864-
return false
864+
throw SubnetCalcError.invalidIPv6("short IPv6 address must contain ::")
865865
}
866866
}
867867
for index in 0...(ip4Hex!.count - 1) {
868868
//print("Index : \(index) IPHex : \(ip4Hex[index]) Dec : \(String(UInt16(ip4Hex[index], radix: 16)!, radix: 16))")
869869
if (ip4Hex![index].count > 4 && ip4Hex![index].count != 0) {
870870
//print("\(ip4Hex![index]) too large")
871-
return false
871+
throw SubnetCalcError.invalidIPv6("\(ip4Hex![index]) segment is too large")
872872
}
873873
hex = UInt16(ip4Hex![index], radix: 16)
874874
if hex != nil {
875875
if (hex! < 0 || hex! > 0xFFFF) {
876876
//print("\(hex!) is invalid")
877-
return false
877+
throw SubnetCalcError.invalidIPv6("\(hex!) segment must be between 0 and 0xFFFF")
878878
}
879879
}
880880
else {
881881
if (ip4Hex![index] != "") {
882882
//print("\(ip4Hex![index]) not an integer")
883-
return false
883+
throw SubnetCalcError.invalidIPv6("\(ip4Hex![index]) segment is not an integer")
884884
}
885885
}
886886
}
887-
return true
887+
//return true
888888
}
889889

890890
/**
@@ -1419,7 +1419,8 @@ class IPSubnetCalc: NSObject {
14191419

14201420
*/
14211421
init?(ipv6: String, maskbits: Int) {
1422-
if (IPSubnetCalc.isValidIPv6(ipAddress: ipv6, mask: maskbits)) {
1422+
do {
1423+
try IPSubnetCalc.validateIPv6(ipAddress: ipv6, mask: maskbits)
14231424
(self.ipv4Address, _) = IPSubnetCalc.convertIPv6toIPv4(ipAddress: ipv6)
14241425
if (maskbits >= (Constants.defaultIPv6to4Mask + Constants.classAbits)) {
14251426
self.maskBits = maskbits - Constants.defaultIPv6to4Mask
@@ -1433,7 +1434,8 @@ class IPSubnetCalc: NSObject {
14331434
self.ipv6MaskBits = maskbits
14341435
//print("init IPv6 ipv6 addr: \(self.ipv6Address) ipv4 addr: \(self.ipv4Address)")
14351436
}
1436-
else {
1437+
catch {
1438+
print("Init error: \(error)")
14371439
return nil
14381440
}
14391441
}

SubnetCalcAppDelegate.swift

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
224224
return (String(ipInfo[0]), String(ipInfo[1]))
225225
}
226226
else if ipInfo.count > 2 {
227-
print("Bad IP format: \(ipInfo)")
227+
print("Invalid IP format: \(ipInfo)")
228228
return ("", nil)
229229
}
230230
return (address, nil)
@@ -422,7 +422,8 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
422422
else {
423423
(ipaddr, ipmask) = splitAddrMask(address: addrField.stringValue)
424424
addrField.stringValue = ipaddr
425-
if (IPSubnetCalc.isValidIPv6(ipAddress: ipaddr, mask: Int(ipmask ?? Constants.defaultIPv6Mask)) == true) {
425+
do {
426+
try IPSubnetCalc.validateIPv6(ipAddress: ipaddr, mask: Int(ipmask ?? Constants.defaultIPv6Mask))
426427
if (ipsc != nil) {
427428
ipaddr = ipsc!.ipv4Address
428429
}
@@ -435,6 +436,7 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
435436
}
436437
}
437438
}
439+
catch {}
438440
if (ipmask == nil && ipsc != nil) {
439441
ipmask = String(ipsc!.maskBits)
440442
}
@@ -564,10 +566,11 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
564566
}
565567
}
566568
else if (Int(ipmask!) == nil) {
567-
myAlert(message: "Bad IPv6 mask", info: "Bad format: \(ipmask!)")
569+
myAlert(message: "Invalid IPv6 mask", info: "\(ipmask!) is not an integer")
568570
return
569571
}
570-
if (IPSubnetCalc.isValidIPv6(ipAddress: ipaddr, mask: Int(ipmask!)) == true) {
572+
do {
573+
try IPSubnetCalc.validateIPv6(ipAddress: ipaddr, mask: Int(ipmask!))
571574
//print("IP Address: \(ipaddr) mask: \(ipmask)")
572575
ipsc = IPSubnetCalc(ipv6: ipaddr, maskbits: Int(ipmask!)!)
573576
if (ipsc != nil) {
@@ -580,8 +583,16 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
580583
self.doIPv6()
581584
}
582585
}
583-
else {
584-
myAlert(message: "Bad IPv6 Address", info: "Bad format: \(ipaddr)/\(ipmask ?? "")")
586+
catch SubnetCalcError.invalidIPv6(let info) {
587+
myAlert(message: "Invalid IPv6 Address", info: info)
588+
return
589+
}
590+
catch SubnetCalcError.invalidIPv6Mask(let info) {
591+
myAlert(message: "Invalid IPv6 Mask", info: info)
592+
return
593+
}
594+
catch {
595+
myAlert(message: "Unknown invalid IPv6 error", info: "\(ipaddr)/\(ipmask ?? "") Error: \(error)")
585596
return
586597
}
587598
}
@@ -646,7 +657,7 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
646657
self.doIPSubnetCalc()
647658
}
648659
else {
649-
myAlert(message: "Bad Max Hosts", info: "Bad selection")
660+
myAlert(message: "Invalid Max Hosts", info: "Bad selection")
650661
return
651662
}
652663
}
@@ -668,7 +679,7 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
668679
self.doIPSubnetCalc()
669680
}
670681
else {
671-
myAlert(message: "Bad Max Subnets", info: "Bad selection")
682+
myAlert(message: "Invalid Max Subnets", info: "Bad selection")
672683
return
673684
}
674685
}
@@ -690,7 +701,7 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
690701
self.doIPSubnetCalc()
691702
}
692703
else {
693-
myAlert(message: "Bad Subnet Bits", info: "Bad selection")
704+
myAlert(message: "Invalid Subnet Bits", info: "Bad selection")
694705
return
695706
}
696707
}
@@ -720,12 +731,12 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
720731
self.doIPSubnetCalc()
721732
}
722733
else {
723-
myAlert(message: "Bad Subnet Mask", info: "Bad format \(maskStr)")
734+
myAlert(message: "Invalid Subnet Mask", info: "Bad format \(maskStr)")
724735
return
725736
}
726737
}
727738
else {
728-
myAlert(message: "Bad Subnet Mask", info: "Bad selection")
739+
myAlert(message: "Invalid Subnet Mask", info: "Bad selection")
729740
return
730741
}
731742
}
@@ -747,7 +758,7 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
747758
self.doIPSubnetCalc()
748759
}
749760
else {
750-
myAlert(message: "Bad Mask Bits", info: "Bad selection")
761+
myAlert(message: "Invalid Mask Bits", info: "Bad selection")
751762
return
752763
}
753764
}
@@ -786,7 +797,7 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
786797
}
787798
}
788799
else {
789-
myAlert(message: "Bad CIDR Mask Bits", info: "Bad selection")
800+
myAlert(message: "Invalid CIDR Mask Bits", info: "Bad selection")
790801
return
791802
}
792803
}
@@ -827,12 +838,12 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
827838
}
828839
}
829840
else {
830-
myAlert(message: "Bad CIDR Mask", info: "Bad format \(maskStr)")
841+
myAlert(message: "Invalid CIDR Mask", info: "Bad format \(maskStr)")
831842
return
832843
}
833844
}
834845
else {
835-
myAlert(message: "Bad CIDR Mask", info: "Bad selection")
846+
myAlert(message: "Invalid CIDR Mask", info: "Bad selection")
836847
return
837848
}
838849
}
@@ -866,12 +877,12 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
866877
doCIDR(maskbits: result)
867878
}
868879
else {
869-
myAlert(message: "Bad Max Supernets", info: "Value too high")
880+
myAlert(message: "Invalid Max Supernets", info: "Value too high")
870881
return
871882
}
872883
}
873884
else {
874-
myAlert(message: "Bad Max Supernets", info: "Bad selection")
885+
myAlert(message: "Invalid Max Supernets", info: "Bad selection")
875886
return
876887
}
877888
}
@@ -893,12 +904,12 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
893904
doCIDR(maskbits: (32 - sender.indexOfSelectedItem - 1))
894905
}
895906
else {
896-
myAlert(message: "Bad Max Adresses", info: "Bad value")
907+
myAlert(message: "Invalid Max Adresses", info: "Bad value")
897908
return
898909
}
899910
}
900911
else {
901-
myAlert(message: "Bad Max Adresses", info: "Bad selection")
912+
myAlert(message: "Invalid Max Adresses", info: "Bad selection")
902913
return
903914
}
904915
}
@@ -920,12 +931,12 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
920931
doCIDR(maskbits: (32 - sender.indexOfSelectedItem))
921932
}
922933
else {
923-
myAlert(message: "Bad Max Subnets", info: "Bad value")
934+
myAlert(message: "Invalid Max Subnets", info: "Bad value")
924935
return
925936
}
926937
}
927938
else {
928-
myAlert(message: "Bad Max Subnets", info: "Bad selection")
939+
myAlert(message: "Invalid Max Subnets", info: "Bad selection")
929940
return
930941
}
931942
}
@@ -1209,7 +1220,7 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
12091220
}
12101221
}
12111222
else {
1212-
myAlert(message: "Bad VLSM required Hosts number", info: "\(requiredHostsVLSM.integerValue) is not a number")
1223+
myAlert(message: "Invalid VLSM required Hosts number", info: "\(requiredHostsVLSM.integerValue) is not a number")
12131224
}
12141225
requiredHostsVLSM.stringValue = ""
12151226
subnetNameVLSM.stringValue = ""
@@ -1345,7 +1356,7 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
13451356
self.doIPv6SubnetCalc()
13461357
}
13471358
else {
1348-
myAlert(message: "Bad IPv6 Mask Bits", info: "Bad selection")
1359+
myAlert(message: "Invalid IPv6 Mask Bits", info: "Bad selection")
13491360
return
13501361
}
13511362
}
@@ -1384,7 +1395,7 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
13841395
self.doIPv6SubnetCalc()
13851396
}
13861397
else {
1387-
myAlert(message: "Bad Max Hosts", info: "Bad selection")
1398+
myAlert(message: "Invalid Max Hosts", info: "Bad selection")
13881399
return
13891400
}
13901401
}

0 commit comments

Comments
 (0)