Skip to content

Commit 35248c4

Browse files
committed
Fix a bug in networkRangeIPv6 func with the IPv6 address is short
Integrate fullAddressIPv6 normalization in digitizeIPv6 func Change fullAddressIPv6 and compactAddressIPv6 to static functions
1 parent bce1b83 commit 35248c4

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

IPSubnetcalc.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ class IPSubnetCalc: NSObject {
927927
/**
928928
Convert an IPv6 address in hexadecimal format to its digital format
929929

930-
- Parameter ipAddress: an IPv6 address in hexadecimal format
930+
- Parameter ipAddress: an IPv6 address in hexadecimal format. Must be in full format.
931931

932932
- Returns:
933933
UInt16 array of each digitized IPv6 address hexa segments
@@ -937,7 +937,7 @@ class IPSubnetCalc: NSObject {
937937
var ipAddressNum: [UInt16] = Array(repeating: 0, count: 8)
938938
var ip4Hex = [String]()
939939

940-
ip4Hex = ipAddress.components(separatedBy: ":")
940+
ip4Hex = IPSubnetCalc.fullAddressIPv6(ipAddress: ipAddress).components(separatedBy: ":")
941941
for index in 0...(ip4Hex.count - 1) {
942942
if (ip4Hex[index] == "") {
943943
ip4Hex[index] = "0"
@@ -1044,7 +1044,7 @@ class IPSubnetCalc: NSObject {
10441044

10451045
*/
10461046
func hexaIDIPv6() -> String {
1047-
var hexID: String = fullAddressIPv6(ipAddress: self.ipv6Address)
1047+
var hexID: String = IPSubnetCalc.fullAddressIPv6(ipAddress: self.ipv6Address)
10481048
let delimiter: Set<Character> = [":"]
10491049
hexID.removeAll(where: { delimiter.contains($0) })
10501050
return("0x\(hexID)")
@@ -1070,7 +1070,7 @@ class IPSubnetCalc: NSObject {
10701070
the long notation (non compact) of the given IPv6 address
10711071

10721072
*/
1073-
func fullAddressIPv6(ipAddress: String) -> String {
1073+
static func fullAddressIPv6(ipAddress: String) -> String {
10741074
var fullAddr = String()
10751075
var ip4Hex = [String]()
10761076
var prevIsZero = false
@@ -1109,7 +1109,7 @@ class IPSubnetCalc: NSObject {
11091109
the compact notation of the given IPv6 address
11101110

11111111
*/
1112-
func compactAddressIPv6(ipAddress: String) -> String {
1112+
static func compactAddressIPv6(ipAddress: String) -> String {
11131113
var shortAddr = String()
11141114
var ip4Hex = [String]()
11151115
var prevIsZero = false
@@ -1118,7 +1118,7 @@ class IPSubnetCalc: NSObject {
11181118
var prevNonZero = false
11191119

11201120
//print("IP Address: \(ipAddress)")
1121-
ip4Hex = self.fullAddressIPv6(ipAddress: ipAddress).components(separatedBy: ":")
1121+
ip4Hex = IPSubnetCalc.fullAddressIPv6(ipAddress: ipAddress).components(separatedBy: ":")
11221122
for index in 0...(ip4Hex.count - 1) {
11231123
if (UInt16(ip4Hex[index], radix: 16)! == 0) {
11241124
if (!prevIsZero || prevCompactZero) {
@@ -1176,7 +1176,7 @@ class IPSubnetCalc: NSObject {
11761176
func networkIPv6() -> String {
11771177
var netID = [UInt16]()
11781178
let numMask = IPSubnetCalc.digitizeMaskIPv6(maskbits: self.ipv6MaskBits)
1179-
let numIP = IPSubnetCalc.digitizeIPv6(ipAddress: fullAddressIPv6(ipAddress: self.ipv6Address))
1179+
let numIP = IPSubnetCalc.digitizeIPv6(ipAddress: self.ipv6Address)
11801180

11811181
for index in 0...7 {
11821182
//print("Index: \(index) IP: \(numIP[index]) Mask : \(numMask[index]) Result : \(numIP[index] & (numMask[index])) ")
@@ -1196,7 +1196,7 @@ class IPSubnetCalc: NSObject {
11961196
var netID = [UInt16]()
11971197
var netID2 = [UInt16]()
11981198
let numMask = IPSubnetCalc.digitizeMaskIPv6(maskbits: self.ipv6MaskBits)
1199-
let numIP = IPSubnetCalc.digitizeIPv6(ipAddress: fullAddressIPv6(ipAddress: self.ipv6Address))
1199+
let numIP = IPSubnetCalc.digitizeIPv6(ipAddress: self.ipv6Address)
12001200

12011201
for index in 0...7 {
12021202
//print("Index: \(index) IP: \(numIP[index]) Mask : \(numMask[index]) Result : \(numIP[index] & (numMask[index])) ")
@@ -1236,7 +1236,7 @@ class IPSubnetCalc: NSObject {
12361236
func dottedDecimalIPv6() -> String {
12371237
var ipv4str = String()
12381238

1239-
let ip4Hex = fullAddressIPv6(ipAddress: self.ipv6Address).components(separatedBy: ":")
1239+
let ip4Hex = IPSubnetCalc.fullAddressIPv6(ipAddress: self.ipv6Address).components(separatedBy: ":")
12401240
for index in (0...(ip4Hex.count - 1)) {
12411241
if (index != 0) {
12421242
ipv4str.append(".")
@@ -1261,7 +1261,7 @@ class IPSubnetCalc: NSObject {
12611261

12621262
*/
12631263
func ip6ARPA () -> String {
1264-
var ipARPA = fullAddressIPv6(ipAddress: self.ipv6Address)
1264+
var ipARPA = IPSubnetCalc.fullAddressIPv6(ipAddress: self.ipv6Address)
12651265
let delimiter: Set<Character> = [":"]
12661266

12671267
ipARPA.removeAll(where: { delimiter.contains($0) })
@@ -1286,9 +1286,9 @@ class IPSubnetCalc: NSObject {
12861286
func resBlockIPv6() -> String? {
12871287
var netID = networkIPv6()
12881288

1289-
netID = fullAddressIPv6(ipAddress: netID)
1289+
netID = IPSubnetCalc.fullAddressIPv6(ipAddress: netID)
12901290
//print("NetID BEFORE compact : \(netID)")
1291-
netID = compactAddressIPv6(ipAddress: netID)
1291+
netID = IPSubnetCalc.compactAddressIPv6(ipAddress: netID)
12921292
//print("NetID AFTER compact : \(netID)")
12931293
netID.append("/\(self.ipv6MaskBits)")
12941294

SubnetCalcAppDelegate.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -493,12 +493,12 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
493493
var typeConv: String
494494

495495
if (ipv6Compact.state == NSControl.StateValue.on) {
496-
ipv6Address.stringValue = ipsc!.compactAddressIPv6(ipAddress: ipsc!.ipv6Address)
497-
ipv6Network.stringValue = ipsc!.compactAddressIPv6(ipAddress: ipsc!.networkIPv6())
496+
ipv6Address.stringValue = IPSubnetCalc.compactAddressIPv6(ipAddress: ipsc!.ipv6Address)
497+
ipv6Network.stringValue = IPSubnetCalc.compactAddressIPv6(ipAddress: ipsc!.networkIPv6())
498498
}
499499
else {
500-
ipv6Address.stringValue = ipsc!.fullAddressIPv6(ipAddress: ipsc!.ipv6Address)
501-
ipv6Network.stringValue = ipsc!.fullAddressIPv6(ipAddress: ipsc!.networkIPv6())
500+
ipv6Address.stringValue = IPSubnetCalc.fullAddressIPv6(ipAddress: ipsc!.ipv6Address)
501+
ipv6Network.stringValue = IPSubnetCalc.fullAddressIPv6(ipAddress: ipsc!.networkIPv6())
502502
}
503503
(ipv6to4Address.stringValue, typeConv) = IPSubnetCalc.convertIPv6toIPv4(ipAddress: ipsc!.ipv6Address)
504504
ipv6to4Box.title = "IPv4 conversion" + " (\(typeConv))"
@@ -1572,7 +1572,7 @@ class SubnetCalcAppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate,
15721572
let pb: NSPasteboard = NSPasteboard.general
15731573
pb.clearContents()
15741574
let ipv4Info = "IPv4 Address Class Type: \(ipsc!.netClass())\nIPv4 Address: \(ipsc!.ipv4Address)\nIPv4 Subnet ID: \(ipsc!.subnetId())\nIPv4 Subnet Mask: \(ipsc!.subnetMask())\nIPv4 Broadcast: \(ipsc!.subnetBroadcast())\nIPv4 Address Range: \(ipsc!.subnetRange())\nIPv4 Mask Bits: \(ipsc!.maskBits)\nIPv4 Subnet Bits: \(ipsc!.subnetBits())\nMax IPv4 Subnets: \(ipsc!.maxSubnets())\nIPv4 Max Hosts / Subnet: \(ipsc!.maxHosts())\nIPv4 Address Hexa: \(ipsc!.hexaMap())\nIPv4 Bit Map: \(ipsc!.bitMap())\nIPv4 Binary Map: \(ipsc!.binaryMap())\n"
1575-
let ipv6Info = "\nIPv6 Address: \(ipsc!.ipv6Address)\nLong IPv6 Address: \(ipsc!.fullAddressIPv6(ipAddress: ipsc!.ipv6Address))\nShort IPv6 Address: \(ipsc!.compactAddressIPv6(ipAddress: ipsc!.ipv6Address))\nIPv6-to-IPv4: \(IPSubnetCalc.convertIPv6toIPv4(ipAddress: ipsc!.ipv6Address))\nIPv6 Mask Bits: \(ipsc!.ipv6MaskBits)\nIPv6 Max Hosts / Subnet: \(ipsc!.totalIPAddrIPv6())\nNetwork: \(ipsc!.compactAddressIPv6(ipAddress: ipsc!.networkIPv6()))\nIPv6 Address Range: \(ipsc!.networkRangeIPv6())\nIPv6 Address Type: \(ipsc!.resBlockIPv6() ?? "None")\nIPv6 Address Hexa: \(ipsc!.hexaIDIPv6())\nIPv6 Address Dotted Decimal: \(ipsc!.dottedDecimalIPv6())\nIP6.ARPA: \(ipsc!.ip6ARPA())\n"
1575+
let ipv6Info = "\nIPv6 Address: \(ipsc!.ipv6Address)\nLong IPv6 Address: \(IPSubnetCalc.fullAddressIPv6(ipAddress: ipsc!.ipv6Address))\nShort IPv6 Address: \(IPSubnetCalc.compactAddressIPv6(ipAddress: ipsc!.ipv6Address))\nIPv6-to-IPv4: \(IPSubnetCalc.convertIPv6toIPv4(ipAddress: ipsc!.ipv6Address))\nIPv6 Mask Bits: \(ipsc!.ipv6MaskBits)\nIPv6 Max Hosts / Subnet: \(ipsc!.totalIPAddrIPv6())\nNetwork: \(IPSubnetCalc.compactAddressIPv6(ipAddress: ipsc!.networkIPv6()))\nIPv6 Address Range: \(ipsc!.networkRangeIPv6())\nIPv6 Address Type: \(ipsc!.resBlockIPv6() ?? "None")\nIPv6 Address Hexa: \(ipsc!.hexaIDIPv6())\nIPv6 Address Dotted Decimal: \(ipsc!.dottedDecimalIPv6())\nIP6.ARPA: \(ipsc!.ip6ARPA())\n"
15761576
pb.setString(ipv4Info + ipv6Info, forType: NSPasteboard.PasteboardType.string)
15771577
}
15781578
}

0 commit comments

Comments
 (0)