Skip to content

Commit e739e12

Browse files
committed
Fix VLSM required hosts too large for maskbits when no entry
Rename exportCSV method to exportSubnetsHosts Add export FSLM (working) Add export VSLM (working)
1 parent ce976e1 commit e739e12

File tree

2 files changed

+125
-8
lines changed

2 files changed

+125
-8
lines changed

Base.lproj/MainMenu.xib

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,19 @@
6767
<menuItem title="File" id="656">
6868
<menu key="submenu" title="File" id="657">
6969
<items>
70-
<menuItem title="Export CSV" keyEquivalent="e" id="668">
70+
<menuItem title="Export Subnets/Hosts" keyEquivalent="e" id="668">
7171
<connections>
72-
<action selector="exportCSV:" target="b7w-M5-ti7" id="Ks9-gQ-HYU"/>
72+
<action selector="exportSubnetsHosts:" target="b7w-M5-ti7" id="EHT-lN-ksr"/>
73+
</connections>
74+
</menuItem>
75+
<menuItem title="Export FLSM" keyEquivalent="e" id="xmj-b6-ggW">
76+
<connections>
77+
<action selector="exportFLSM:" target="b7w-M5-ti7" id="uJ1-dH-jxD"/>
78+
</connections>
79+
</menuItem>
80+
<menuItem title="Export VLSM" keyEquivalent="e" id="XVa-5e-1dG">
81+
<connections>
82+
<action selector="exportVLSM:" target="b7w-M5-ti7" id="oYt-CR-eL0"/>
7383
</connections>
7484
</menuItem>
7585
<menuItem title="Export Clipboard" keyEquivalent="b" id="ceZ-iQ-E9j">
@@ -1458,9 +1468,19 @@
14581468
<menu key="menu" title="OtherViews" id="739">
14591469
<items>
14601470
<menuItem hidden="YES" id="740"/>
1461-
<menuItem title="Export CSV" id="742">
1471+
<menuItem title="Export Subnets/Hosts" id="742">
1472+
<connections>
1473+
<action selector="exportSubnetsHosts:" target="b7w-M5-ti7" id="207-fy-PIx"/>
1474+
</connections>
1475+
</menuItem>
1476+
<menuItem title="Export FLSM" id="pbN-aM-Sl1">
1477+
<connections>
1478+
<action selector="exportFLSM:" target="b7w-M5-ti7" id="X2n-6Q-aS4"/>
1479+
</connections>
1480+
</menuItem>
1481+
<menuItem title="Export VLSM" id="Tui-fq-nG6">
14621482
<connections>
1463-
<action selector="exportCSV:" target="b7w-M5-ti7" id="4Td-Nv-X6s"/>
1483+
<action selector="exportVLSM:" target="b7w-M5-ti7" id="74Q-4L-4K8"/>
14641484
</connections>
14651485
</menuItem>
14661486
<menuItem title="Export Clipboard" id="mB2-aR-3r0">

SubnetCalcAppDelegate.swift

Lines changed: 101 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
933933
(maskbits, hosts) = IPSubnetCalc.fittingSubnet(hosts: UInt(requiredHostsVLSM.integerValue))
934934
if (maskbits != 0) {
935935
used = (requiredHostsVLSM.integerValue * 100) / Int(hosts)
936-
//print("VLSM fitting subnet mask: \(maskbits) with \(hosts) max hosts")
936+
print("VLSM fitting subnet mask: \(maskbits) with \(hosts) max hosts")
937937
if (subnetsVLSM.count != 0) {
938938
print("VLSM subnets NOT empty")
939939
print("Mask VLSM: \(IPSubnetCalc.digitize(ipAddress: globalMaskVLSM)) Maskbits: \(IPSubnetCalc.digitize(ipAddress: ~IPSubnetCalc.numerize(maskbits: maskbits)))")
@@ -955,10 +955,16 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
955955
else {
956956
print("VLSM subnets empty")
957957
globalMaskVLSM = ~IPSubnetCalc.numerize(maskbits: ipsc!.maskBits) + 1
958-
print("Mask VLSM: \(IPSubnetCalc.digitize(ipAddress: globalMaskVLSM))")
958+
print("Mask VLSM: \(IPSubnetCalc.digitize(ipAddress: globalMaskVLSM)) Maskbits: \(IPSubnetCalc.digitize(ipAddress: ~IPSubnetCalc.numerize(maskbits: maskbits)))")
959+
if (globalMaskVLSM > ~IPSubnetCalc.numerize(maskbits: maskbits)) {
959960
globalMaskVLSM = globalMaskVLSM - (~IPSubnetCalc.numerize(maskbits: maskbits) + 1)
960961
print("Mask AFTER VLSM: \(IPSubnetCalc.digitize(ipAddress: globalMaskVLSM))")
961-
subnetsVLSM.append((maskbits, subnetNameVLSM.stringValue, "\(requiredHostsVLSM.stringValue)/\(hosts) (\(used)%)"))
962+
subnetsVLSM.append((maskbits, subnetNameVLSM.stringValue, "\(requiredHostsVLSM.stringValue)/\(hosts) (\(used)%)"))
963+
}
964+
else {
965+
myAlert(message: "No space for Hosts requirement", info: "\(requiredHostsVLSM.integerValue) hosts require /\(maskbits) Mask bits")
966+
}
967+
962968
}
963969
viewVLSM.reloadData()
964970
}
@@ -1080,7 +1086,7 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
10801086
self.calc(sender)
10811087
}
10821088

1083-
@IBAction func exportCSV(_ sender: AnyObject)
1089+
@IBAction func exportSubnetsHosts(_ sender: AnyObject)
10841090
{
10851091
if (ipsc != nil) {
10861092
let panel = NSSavePanel()
@@ -1119,6 +1125,97 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
11191125
}
11201126
}
11211127

1128+
@IBAction func exportFLSM(_ sender: AnyObject)
1129+
{
1130+
if (ipsc != nil) {
1131+
if (ipsc!.maskBits <= 29) {
1132+
let panel = NSSavePanel()
1133+
panel.allowedFileTypes = ["csv"]
1134+
panel.begin(completionHandler: { (result) in
1135+
if (result == NSApplication.ModalResponse.OK && panel.url != nil) {
1136+
var fileMgt: FileManager
1137+
if #available(OSX 10.14, *) {
1138+
fileMgt = FileManager(authorization: NSWorkspace.Authorization())
1139+
} else {
1140+
// Fallback on earlier versions
1141+
fileMgt = FileManager.default
1142+
}
1143+
fileMgt.createFile(atPath: panel.url!.path, contents: nil, attributes: nil)
1144+
//var cvsData = NSMutableData.init(capacity: Constants.BUFFER_LINES)
1145+
var cvsData = Data(capacity: Constants.BUFFER_LINES)
1146+
let cvsFile = FileHandle(forWritingAtPath: panel.url!.path)
1147+
if (cvsFile != nil) {
1148+
var cvsStr = "#;Subnet ID;Mask bits;Range;Broadcast\n"
1149+
let subnetid: UInt32 = ((IPSubnetCalc.numerize(ipAddress: self.ipsc!.ipv4Address) & self.ipsc!.classMask()) >> (32 - self.ipsc!.maskBits)) << (32 - self.ipsc!.maskBits)
1150+
for index in (0...(Int(truncating: NSDecimalNumber(decimal: pow(2, self.slideFLSM.integerValue))) - 1)) {
1151+
let ipaddr = (subnetid >> (32 - (self.ipsc!.maskBits + self.slideFLSM.integerValue)) + UInt32(index)) << (32 - (self.ipsc!.maskBits + self.slideFLSM.integerValue))
1152+
let ipsc_tmp = IPSubnetCalc(ipAddress: IPSubnetCalc.digitize(ipAddress: ipaddr), maskbits: (self.ipsc!.maskBits + self.slideFLSM.integerValue))
1153+
if (ipsc_tmp != nil) {
1154+
cvsStr.append("\(index + 1);\(ipsc_tmp!.subnetId());\(self.ipsc!.maskBits + self.slideFLSM.integerValue);\(ipsc_tmp!.subnetRange());\(ipsc_tmp!.subnetBroadcast())\n")
1155+
}
1156+
}
1157+
cvsData.append(cvsStr.data(using: String.Encoding.ascii)!)
1158+
cvsFile!.write(cvsData)
1159+
cvsFile!.synchronizeFile()
1160+
cvsFile!.closeFile()
1161+
}
1162+
}
1163+
}
1164+
)
1165+
}
1166+
else {
1167+
myAlert(message: "Cannot not export FLSM Info", info: "Mask bits \(ipsc!.maskBits) > 29")
1168+
}
1169+
}
1170+
}
1171+
1172+
@IBAction func exportVLSM(_ sender: AnyObject)
1173+
{
1174+
if (ipsc != nil) {
1175+
if (subnetsVLSM.count != 0) {
1176+
let panel = NSSavePanel()
1177+
panel.allowedFileTypes = ["csv"]
1178+
panel.begin(completionHandler: { (result) in
1179+
if (result == NSApplication.ModalResponse.OK && panel.url != nil) {
1180+
var fileMgt: FileManager
1181+
if #available(OSX 10.14, *) {
1182+
fileMgt = FileManager(authorization: NSWorkspace.Authorization())
1183+
} else {
1184+
// Fallback on earlier versions
1185+
fileMgt = FileManager.default
1186+
}
1187+
fileMgt.createFile(atPath: panel.url!.path, contents: nil, attributes: nil)
1188+
//var cvsData = NSMutableData.init(capacity: Constants.BUFFER_LINES)
1189+
var cvsData = Data(capacity: Constants.BUFFER_LINES)
1190+
let cvsFile = FileHandle(forWritingAtPath: panel.url!.path)
1191+
if (cvsFile != nil) {
1192+
var cvsStr = "#;Subnet ID;Mask bits;Subnet Name;Used\n"
1193+
let subnetid = IPSubnetCalc.numerize(ipAddress: self.ipsc!.subnetId())
1194+
for index in (0...(self.subnetsVLSM.count - 1)) {
1195+
var subnet = subnetid
1196+
if (index > 0) {
1197+
for index2 in (0...(index - 1)) {
1198+
subnet = subnet + ~IPSubnetCalc.numerize(maskbits: self.subnetsVLSM[index2].0) + 1
1199+
}
1200+
}
1201+
//print("VLSM: \(index + 1);\(IPSubnetCalc.digitize(ipAddress: subnet));\(self.subnetsVLSM[index].0);\(self.subnetsVLSM[index].1);\(self.subnetsVLSM[index].2)\n")
1202+
cvsStr.append("\(index + 1);\(IPSubnetCalc.digitize(ipAddress: subnet));\(self.subnetsVLSM[index].0);\(self.subnetsVLSM[index].1);\(self.subnetsVLSM[index].2)\n")
1203+
}
1204+
cvsData.append(cvsStr.data(using: String.Encoding.ascii)!)
1205+
cvsFile!.write(cvsData)
1206+
cvsFile!.synchronizeFile()
1207+
cvsFile!.closeFile()
1208+
}
1209+
}
1210+
}
1211+
)
1212+
}
1213+
else {
1214+
myAlert(message: "Cannot not export VLSM Info", info: "No subnet")
1215+
}
1216+
}
1217+
}
1218+
11221219
@IBAction func exportClipboard(_ sender: AnyObject)
11231220
{
11241221
if (ipsc != nil) {

0 commit comments

Comments
 (0)