Skip to content

Commit

Permalink
Rename isValidIPv6 function to validateIPv6
Browse files Browse the repository at this point in the history
Handle custom errors in validateIPv6 function
  • Loading branch information
mulot committed Dec 7, 2021
1 parent f8b3915 commit c9ff303
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 35 deletions.
24 changes: 13 additions & 11 deletions IPSubnetcalc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -831,14 +831,14 @@ class IPSubnetCalc: NSObject {
Boolean if the given IPv6 address is valid or not

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

if mask != nil {
if (mask! < 1 || mask! > 128) {
print("mask \(mask!) invalid")
return false
throw SubnetCalcError.invalidIPv6Mask("mask \(mask!) must be between 1 and 128")
}
}
else {
Expand All @@ -848,43 +848,43 @@ class IPSubnetCalc: NSObject {
ip4Hex = ipAddress.components(separatedBy: ":")
if (ip4Hex == nil) {
//print("\(ipAddress) invalid")
return false
throw SubnetCalcError.invalidIPv6("IPv6 address must contain :")
}
if (ip4Hex!.count != 8) {
//print("no 8 hex")
if (ipAddress.contains("::"))
{
if (ipAddress.components(separatedBy: "::").count > 2) {
//print("too many '::'")
return false
throw SubnetCalcError.invalidIPv6("too many ::")
}
}
else {
//print("IPv6 \(ipAddress) bad format")
return false
throw SubnetCalcError.invalidIPv6("short IPv6 address must contain ::")
}
}
for index in 0...(ip4Hex!.count - 1) {
//print("Index : \(index) IPHex : \(ip4Hex[index]) Dec : \(String(UInt16(ip4Hex[index], radix: 16)!, radix: 16))")
if (ip4Hex![index].count > 4 && ip4Hex![index].count != 0) {
//print("\(ip4Hex![index]) too large")
return false
throw SubnetCalcError.invalidIPv6("\(ip4Hex![index]) segment is too large")
}
hex = UInt16(ip4Hex![index], radix: 16)
if hex != nil {
if (hex! < 0 || hex! > 0xFFFF) {
//print("\(hex!) is invalid")
return false
throw SubnetCalcError.invalidIPv6("\(hex!) segment must be between 0 and 0xFFFF")
}
}
else {
if (ip4Hex![index] != "") {
//print("\(ip4Hex![index]) not an integer")
return false
throw SubnetCalcError.invalidIPv6("\(ip4Hex![index]) segment is not an integer")
}
}
}
return true
//return true
}

/**
Expand Down Expand Up @@ -1419,7 +1419,8 @@ class IPSubnetCalc: NSObject {

*/
init?(ipv6: String, maskbits: Int) {
if (IPSubnetCalc.isValidIPv6(ipAddress: ipv6, mask: maskbits)) {
do {
try IPSubnetCalc.validateIPv6(ipAddress: ipv6, mask: maskbits)
(self.ipv4Address, _) = IPSubnetCalc.convertIPv6toIPv4(ipAddress: ipv6)
if (maskbits >= (Constants.defaultIPv6to4Mask + Constants.classAbits)) {
self.maskBits = maskbits - Constants.defaultIPv6to4Mask
Expand All @@ -1433,7 +1434,8 @@ class IPSubnetCalc: NSObject {
self.ipv6MaskBits = maskbits
//print("init IPv6 ipv6 addr: \(self.ipv6Address) ipv4 addr: \(self.ipv4Address)")
}
else {
catch {
print("Init error: \(error)")
return nil
}
}
Expand Down
59 changes: 35 additions & 24 deletions SubnetCalcAppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
return (String(ipInfo[0]), String(ipInfo[1]))
}
else if ipInfo.count > 2 {
print("Bad IP format: \(ipInfo)")
print("Invalid IP format: \(ipInfo)")
return ("", nil)
}
return (address, nil)
Expand Down Expand Up @@ -422,7 +422,8 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
else {
(ipaddr, ipmask) = splitAddrMask(address: addrField.stringValue)
addrField.stringValue = ipaddr
if (IPSubnetCalc.isValidIPv6(ipAddress: ipaddr, mask: Int(ipmask ?? Constants.defaultIPv6Mask)) == true) {
do {
try IPSubnetCalc.validateIPv6(ipAddress: ipaddr, mask: Int(ipmask ?? Constants.defaultIPv6Mask))
if (ipsc != nil) {
ipaddr = ipsc!.ipv4Address
}
Expand All @@ -435,6 +436,7 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
}
}
}
catch {}
if (ipmask == nil && ipsc != nil) {
ipmask = String(ipsc!.maskBits)
}
Expand Down Expand Up @@ -564,10 +566,11 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
}
}
else if (Int(ipmask!) == nil) {
myAlert(message: "Bad IPv6 mask", info: "Bad format: \(ipmask!)")
myAlert(message: "Invalid IPv6 mask", info: "\(ipmask!) is not an integer")
return
}
if (IPSubnetCalc.isValidIPv6(ipAddress: ipaddr, mask: Int(ipmask!)) == true) {
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) {
Expand All @@ -580,8 +583,16 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
self.doIPv6()
}
}
else {
myAlert(message: "Bad IPv6 Address", info: "Bad format: \(ipaddr)/\(ipmask ?? "")")
catch SubnetCalcError.invalidIPv6(let info) {
myAlert(message: "Invalid IPv6 Address", info: info)
return
}
catch SubnetCalcError.invalidIPv6Mask(let info) {
myAlert(message: "Invalid IPv6 Mask", info: info)
return
}
catch {
myAlert(message: "Unknown invalid IPv6 error", info: "\(ipaddr)/\(ipmask ?? "") Error: \(error)")
return
}
}
Expand Down Expand Up @@ -646,7 +657,7 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
self.doIPSubnetCalc()
}
else {
myAlert(message: "Bad Max Hosts", info: "Bad selection")
myAlert(message: "Invalid Max Hosts", info: "Bad selection")
return
}
}
Expand All @@ -668,7 +679,7 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
self.doIPSubnetCalc()
}
else {
myAlert(message: "Bad Max Subnets", info: "Bad selection")
myAlert(message: "Invalid Max Subnets", info: "Bad selection")
return
}
}
Expand All @@ -690,7 +701,7 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
self.doIPSubnetCalc()
}
else {
myAlert(message: "Bad Subnet Bits", info: "Bad selection")
myAlert(message: "Invalid Subnet Bits", info: "Bad selection")
return
}
}
Expand Down Expand Up @@ -720,12 +731,12 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
self.doIPSubnetCalc()
}
else {
myAlert(message: "Bad Subnet Mask", info: "Bad format \(maskStr)")
myAlert(message: "Invalid Subnet Mask", info: "Bad format \(maskStr)")
return
}
}
else {
myAlert(message: "Bad Subnet Mask", info: "Bad selection")
myAlert(message: "Invalid Subnet Mask", info: "Bad selection")
return
}
}
Expand All @@ -747,7 +758,7 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
self.doIPSubnetCalc()
}
else {
myAlert(message: "Bad Mask Bits", info: "Bad selection")
myAlert(message: "Invalid Mask Bits", info: "Bad selection")
return
}
}
Expand Down Expand Up @@ -786,7 +797,7 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
}
}
else {
myAlert(message: "Bad CIDR Mask Bits", info: "Bad selection")
myAlert(message: "Invalid CIDR Mask Bits", info: "Bad selection")
return
}
}
Expand Down Expand Up @@ -827,12 +838,12 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
}
}
else {
myAlert(message: "Bad CIDR Mask", info: "Bad format \(maskStr)")
myAlert(message: "Invalid CIDR Mask", info: "Bad format \(maskStr)")
return
}
}
else {
myAlert(message: "Bad CIDR Mask", info: "Bad selection")
myAlert(message: "Invalid CIDR Mask", info: "Bad selection")
return
}
}
Expand Down Expand Up @@ -866,12 +877,12 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
doCIDR(maskbits: result)
}
else {
myAlert(message: "Bad Max Supernets", info: "Value too high")
myAlert(message: "Invalid Max Supernets", info: "Value too high")
return
}
}
else {
myAlert(message: "Bad Max Supernets", info: "Bad selection")
myAlert(message: "Invalid Max Supernets", info: "Bad selection")
return
}
}
Expand All @@ -893,12 +904,12 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
doCIDR(maskbits: (32 - sender.indexOfSelectedItem - 1))
}
else {
myAlert(message: "Bad Max Adresses", info: "Bad value")
myAlert(message: "Invalid Max Adresses", info: "Bad value")
return
}
}
else {
myAlert(message: "Bad Max Adresses", info: "Bad selection")
myAlert(message: "Invalid Max Adresses", info: "Bad selection")
return
}
}
Expand All @@ -920,12 +931,12 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
doCIDR(maskbits: (32 - sender.indexOfSelectedItem))
}
else {
myAlert(message: "Bad Max Subnets", info: "Bad value")
myAlert(message: "Invalid Max Subnets", info: "Bad value")
return
}
}
else {
myAlert(message: "Bad Max Subnets", info: "Bad selection")
myAlert(message: "Invalid Max Subnets", info: "Bad selection")
return
}
}
Expand Down Expand Up @@ -1209,7 +1220,7 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
}
}
else {
myAlert(message: "Bad VLSM required Hosts number", info: "\(requiredHostsVLSM.integerValue) is not a number")
myAlert(message: "Invalid VLSM required Hosts number", info: "\(requiredHostsVLSM.integerValue) is not a number")
}
requiredHostsVLSM.stringValue = ""
subnetNameVLSM.stringValue = ""
Expand Down Expand Up @@ -1345,7 +1356,7 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
self.doIPv6SubnetCalc()
}
else {
myAlert(message: "Bad IPv6 Mask Bits", info: "Bad selection")
myAlert(message: "Invalid IPv6 Mask Bits", info: "Bad selection")
return
}
}
Expand Down Expand Up @@ -1384,7 +1395,7 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
self.doIPv6SubnetCalc()
}
else {
myAlert(message: "Bad Max Hosts", info: "Bad selection")
myAlert(message: "Invalid Max Hosts", info: "Bad selection")
return
}
}
Expand Down

0 comments on commit c9ff303

Please sign in to comment.