@@ -17,7 +17,7 @@ function detectIpVersion(ip: string): IPVersion {
1717 */
1818function ipToBigInt ( ip : string ) : bigint {
1919 const version = detectIpVersion ( ip ) ;
20-
20+
2121 if ( version === 4 ) {
2222 return ip . split ( '.' )
2323 . reduce ( ( acc , octet ) => {
@@ -105,7 +105,7 @@ export function cidrToRange(cidr: string): IPRange {
105105 const version = detectIpVersion ( ip ) ;
106106 const prefixBits = parseInt ( prefix ) ;
107107 const ipBigInt = ipToBigInt ( ip ) ;
108-
108+
109109 // Validate prefix length
110110 const maxPrefix = version === 4 ? 32 : 128 ;
111111 if ( prefixBits < 0 || prefixBits > maxPrefix ) {
@@ -116,7 +116,7 @@ export function cidrToRange(cidr: string): IPRange {
116116 const mask = BigInt . asUintN ( version === 4 ? 64 : 128 , ( BigInt ( 1 ) << shiftBits ) - BigInt ( 1 ) ) ;
117117 const start = ipBigInt & ~ mask ;
118118 const end = start | mask ;
119-
119+
120120 return { start, end } ;
121121}
122122
@@ -136,17 +136,17 @@ export function findNextAvailableCidr(
136136 if ( ! startCidr && existingCidrs . length === 0 ) {
137137 return null ;
138138 }
139-
139+
140140 // If no existing CIDRs, use the IP version from startCidr
141- const version = startCidr
141+ const version = startCidr
142142 ? detectIpVersion ( startCidr . split ( '/' ) [ 0 ] )
143143 : 4 ; // Default to IPv4 if no startCidr provided
144-
144+
145145 // Use appropriate default startCidr if none provided
146146 startCidr = startCidr || ( version === 4 ? "0.0.0.0/0" : "::/0" ) ;
147-
147+
148148 // If there are existing CIDRs, ensure all are same version
149- if ( existingCidrs . length > 0 &&
149+ if ( existingCidrs . length > 0 &&
150150 existingCidrs . some ( cidr => detectIpVersion ( cidr . split ( '/' ) [ 0 ] ) !== version ) ) {
151151 throw new Error ( 'All CIDRs must be of the same IP version' ) ;
152152 }
@@ -196,12 +196,14 @@ export function findNextAvailableCidr(
196196export function isIpInCidr ( ip : string , cidr : string ) : boolean {
197197 const ipVersion = detectIpVersion ( ip ) ;
198198 const cidrVersion = detectIpVersion ( cidr . split ( '/' ) [ 0 ] ) ;
199-
199+
200+ // If IP versions don't match, the IP cannot be in the CIDR range
200201 if ( ipVersion !== cidrVersion ) {
201- throw new Error ( 'IP address and CIDR must be of the same version' ) ;
202+ // throw new Erorr
203+ return false ;
202204 }
203205
204206 const ipBigInt = ipToBigInt ( ip ) ;
205207 const range = cidrToRange ( cidr ) ;
206208 return ipBigInt >= range . start && ipBigInt <= range . end ;
207- }
209+ }
0 commit comments