Skip to content

Commit 154f78d

Browse files
committed
Does not register the address in history if it is invalid
1 parent af4be8b commit 154f78d

File tree

2 files changed

+114
-46
lines changed

2 files changed

+114
-46
lines changed

Base.lproj/MainMenu.xib

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1514,7 +1514,7 @@ CA
15141514
<font key="font" metaFont="system"/>
15151515
</buttonCell>
15161516
<connections>
1517-
<action selector="calc:" target="b7w-M5-ti7" id="gsi-fk-GkQ"/>
1517+
<action selector="calc:" target="b7w-M5-ti7" id="H7m-mU-2rm"/>
15181518
</connections>
15191519
</button>
15201520
<textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="615">

SubnetCalcAppDelegate.swift

Lines changed: 113 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,10 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
404404
Check if there is are current IP address and mask otherwise take the default IP and mask.
405405

406406
Check if the IP address and mask are valid.
407+
408+
- Throws: an invalid IP or invalid mask error with a message explaining the reason
407409
*/
408-
private func doIPSubnetCalc()
410+
private func doIPSubnetCalc() throws
409411
{
410412
var ipaddr: String
411413
var ipmask: String?
@@ -440,7 +442,8 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
440442
}
441443
}
442444
}
443-
catch {}
445+
catch {
446+
}
444447
if (ipmask == nil && ipsc != nil) {
445448
ipmask = String(ipsc!.maskBits)
446449
}
@@ -466,15 +469,42 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
466469
}
467470
catch SubnetCalcError.invalidIPv4(let info) {
468471
myAlert(message: "Invalid IPv4 Address", info: info)
469-
return
472+
throw SubnetCalcError.invalidIPv4(info)
470473
}
471474
catch SubnetCalcError.invalidIPv4Mask(let info) {
472475
myAlert(message: "Invalid IPv4 Mask", info: info)
473-
return
476+
throw SubnetCalcError.invalidIPv4Mask(info)
474477
}
475478
catch {
476479
myAlert(message: "Unknown invalid error", info: "\(ipaddr)/\(ipmask ?? "") Error: \(error)")
477-
return
480+
throw error
481+
}
482+
}
483+
484+
/**
485+
Compute IPv4 or IPv6 infos depending of IP address format in IP address field
486+
487+
- Throws: an invalid IP or invalid mask error with a message explaining the reason
488+
*/
489+
private func doCalc() throws
490+
{
491+
if (addrField.stringValue.contains(":")) {
492+
do {
493+
try self.doIPv6SubnetCalc()
494+
tabView.selectTabViewItem(at: 5)
495+
}
496+
catch {
497+
throw error
498+
}
499+
}
500+
else {
501+
do {
502+
try self.doIPSubnetCalc()
503+
tabView.selectTabViewItem(at: 0)
504+
}
505+
catch {
506+
throw error
507+
}
478508
}
479509
}
480510

@@ -544,8 +574,10 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
544574
Genrate infos also for all IPv4 tabs based on the converted IPv4 address
545575

546576
Check if the IPv6 address and mask are valid.
577+
578+
- Throws: an invalid IP or invalid mask error with a message explaining the reason
547579
*/
548-
private func doIPv6SubnetCalc()
580+
private func doIPv6SubnetCalc() throws
549581
{
550582
var ipaddr: String
551583
var ipmask: String?
@@ -589,15 +621,15 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
589621
}
590622
catch SubnetCalcError.invalidIPv6(let info) {
591623
myAlert(message: "Invalid IPv6 Address", info: info)
592-
return
624+
throw SubnetCalcError.invalidIPv6(info)
593625
}
594626
catch SubnetCalcError.invalidIPv6Mask(let info) {
595627
myAlert(message: "Invalid IPv6 Mask", info: info)
596-
return
628+
throw SubnetCalcError.invalidIPv6Mask(info)
597629
}
598630
catch {
599631
myAlert(message: "Unknown invalid IPv6 error", info: "\(ipaddr)/\(ipmask ?? "") Error: \(error)")
600-
return
632+
throw error
601633
}
602634
}
603635

@@ -658,7 +690,10 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
658690
}
659691
if (sender.indexOfSelectedItem != -1) {
660692
ipsc!.maskBits = 31 - sender.indexOfSelectedItem()
661-
self.doIPSubnetCalc()
693+
do {
694+
try self.doIPSubnetCalc()
695+
}
696+
catch {}
662697
}
663698
else {
664699
myAlert(message: "Invalid Max Hosts", info: "Bad selection")
@@ -680,7 +715,10 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
680715
}
681716
if (sender.indexOfSelectedItem != -1) {
682717
ipsc!.maskBits = 8 + sender.indexOfSelectedItem()
683-
self.doIPSubnetCalc()
718+
do {
719+
try self.doIPSubnetCalc()
720+
}
721+
catch {}
684722
}
685723
else {
686724
myAlert(message: "Invalid Max Subnets", info: "Bad selection")
@@ -702,7 +740,10 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
702740
}
703741
if (sender.objectValueOfSelectedItem as? String) != nil {
704742
ipsc!.maskBits = sender.intValue + ipsc!.netBits()
705-
self.doIPSubnetCalc()
743+
do {
744+
try self.doIPSubnetCalc()
745+
}
746+
catch {}
706747
}
707748
else {
708749
myAlert(message: "Invalid Subnet Bits", info: "Bad selection")
@@ -732,7 +773,10 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
732773
ipsc!.maskBits = IPSubnetCalc.maskBits(mask: mask)
733774
//print("changeSubnetMask object value : \(str)")
734775
}
735-
self.doIPSubnetCalc()
776+
do {
777+
try self.doIPSubnetCalc()
778+
}
779+
catch {}
736780
}
737781
else {
738782
myAlert(message: "Invalid Subnet Mask", info: "Bad format \(maskStr)")
@@ -759,7 +803,10 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
759803
}
760804
if (sender.objectValueOfSelectedItem as? String) != nil {
761805
ipsc!.maskBits = sender.intValue
762-
self.doIPSubnetCalc()
806+
do {
807+
try self.doIPSubnetCalc()
808+
}
809+
catch {}
763810
}
764811
else {
765812
myAlert(message: "Invalid Mask Bits", info: "Bad selection")
@@ -794,7 +841,10 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
794841
}
795842
if (result >= 0) {
796843
ipsc!.maskBits = sender.intValue
797-
self.doIPSubnetCalc()
844+
do {
845+
try self.doIPSubnetCalc()
846+
}
847+
catch {}
798848
}
799849
else {
800850
doCIDR(maskbits: sender.intValue)
@@ -835,7 +885,10 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
835885
}
836886
if (result >= 0) {
837887
ipsc!.maskBits = maskbits
838-
self.doIPSubnetCalc()
888+
do {
889+
try self.doIPSubnetCalc()
890+
}
891+
catch {}
839892
}
840893
else {
841894
doCIDR(maskbits: maskbits)
@@ -1129,13 +1182,19 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
11291182
if (sender.intValue as Int >= Constants.NETWORK_BITS_MIN)
11301183
{
11311184
ipsc!.maskBits = sender.intValue as Int
1132-
self.doIPSubnetCalc()
1185+
do {
1186+
try self.doIPSubnetCalc()
1187+
}
1188+
catch {}
11331189
//subnetsHostsView.reloadData()
11341190
}
11351191
else {
11361192
let maskbits = sender.intValue as Int
11371193
ipsc!.maskBits = Constants.NETWORK_BITS_MIN
1138-
self.doIPSubnetCalc()
1194+
do {
1195+
try self.doIPSubnetCalc()
1196+
}
1197+
catch {}
11391198
if (tabViewClassLess.state == NSControl.StateValue.on) {
11401199
ipsc!.maskBits = maskbits
11411200
self.doSubnetHost()
@@ -1276,7 +1335,10 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
12761335
if (ipsc != nil) {
12771336
if (ipsc!.maskBits < Constants.NETWORK_BITS_MIN) {
12781337
ipsc!.maskBits = Constants.NETWORK_BITS_MIN
1279-
self.doIPSubnetCalc()
1338+
do {
1339+
try self.doIPSubnetCalc()
1340+
}
1341+
catch {}
12801342
}
12811343
}
12821344
}
@@ -1357,7 +1419,10 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
13571419
}
13581420
if (sender.objectValueOfSelectedItem as? String) != nil {
13591421
ipsc!.ipv6MaskBits = sender.intValue
1360-
self.doIPv6SubnetCalc()
1422+
do {
1423+
try self.doIPv6SubnetCalc()
1424+
}
1425+
catch {}
13611426
}
13621427
else {
13631428
myAlert(message: "Invalid IPv6 Mask Bits", info: "Bad selection")
@@ -1378,7 +1443,10 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
13781443
ipsc = IPSubnetCalc(Constants.defaultIP)
13791444
}
13801445
ipsc!.ipv6MaskBits -= sender.indexOfSelectedItem()
1381-
self.doIPv6SubnetCalc()
1446+
do {
1447+
try self.doIPv6SubnetCalc()
1448+
}
1449+
catch {}
13821450
}
13831451

13841452
/**
@@ -1396,7 +1464,10 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
13961464
}
13971465
if (sender.indexOfSelectedItem != -1) {
13981466
ipsc!.ipv6MaskBits = 128 - sender.indexOfSelectedItem()
1399-
self.doIPv6SubnetCalc()
1467+
do {
1468+
try self.doIPv6SubnetCalc()
1469+
}
1470+
catch {}
14001471
}
14011472
else {
14021473
myAlert(message: "Invalid Max Hosts", info: "Bad selection")
@@ -1417,43 +1488,40 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
14171488
*/
14181489
@IBAction func calc(_ sender: AnyObject)
14191490
{
1420-
if (addrField.stringValue.contains(":")) {
1421-
self.doIPv6SubnetCalc()
1422-
tabView.selectTabViewItem(at: 5)
1423-
}
1424-
else {
1425-
self.doIPSubnetCalc()
1426-
tabView.selectTabViewItem(at: 0)
1427-
}
1491+
self.ipAddrEdit(addrField)
14281492
}
1429-
1493+
14301494
/**
14311495
Triggered when the user hit Enter key in the IP address field
14321496

1433-
- Parameter sender: sender of the action
1497+
- Parameter sender: selected item of the IP address field
14341498

14351499
*/
14361500
@IBAction func ipAddrEdit(_ sender: AnyObject)
14371501
{
14381502
//print("ipAddrEdit action")
14391503
if ((sender as? NSTextField)?.stringValue) != nil {
14401504
if (sender.stringValue != "") {
1441-
if (addrField.indexOfItem(withObjectValue: sender.stringValue!) == NSNotFound) {
1442-
if (addrField.numberOfItems >= Constants.maxAddrHistory) {
1443-
addrField.removeItem(at: 0)
1444-
if (history.count > 0) {
1445-
container.viewContext.delete(history[0])
1446-
history.remove(at: 0)
1447-
saveHistory()
1505+
do {
1506+
let addr = sender.stringValue!
1507+
try self.doCalc()
1508+
if (addrField.indexOfItem(withObjectValue: addr) == NSNotFound) {
1509+
if (addrField.numberOfItems >= Constants.maxAddrHistory) {
1510+
addrField.removeItem(at: 0)
1511+
if (history.count > 0) {
1512+
container.viewContext.delete(history[0])
1513+
history.remove(at: 0)
1514+
saveHistory()
1515+
}
14481516
}
1517+
addrField.addItem(withObjectValue: addr)
1518+
let historyItem = AddrHistory(context: container.viewContext)
1519+
historyItem.address = addr
1520+
history.append(historyItem)
1521+
saveHistory()
14491522
}
1450-
addrField.addItem(withObjectValue: sender.stringValue!)
1451-
let historyItem = AddrHistory(context: container.viewContext)
1452-
historyItem.address = sender.stringValue!
1453-
history.append(historyItem)
1454-
saveHistory()
14551523
}
1456-
self.calc(sender)
1524+
catch {}
14571525
}
14581526
}
14591527
}

0 commit comments

Comments
 (0)