Showing posts with label ip-num. Show all posts
Showing posts with label ip-num. Show all posts

Sunday, May 09, 2021

ip-num v1.3.2 has been released

ip-num is A TypeScript/JavaScript library for working with ASN, IPv4, and IPv6 numbers. It provides representations of these internet protocol numbers with the ability to perform various IP related operations like parsing, validating etc, on them.

A new version of ip-num, version 1.3.2 is now available.

This release contains one new feature and a bug fix.

Add method to split range into smaller ranges of certain size

In previous versions of ip-num, it was possible to take IP ranges with prefix less than /32 and split them into two. For example:

import {IPv4CidrRange} from "ip-num/IPRange";
import {IPv4Prefix} from "ip-num/Prefix";

let ipv4CidrRange = IPv4CidrRange.fromCidr("192.168.208.0/24");
let splitRanges: Array<IPv4CidrRange> = ipv4CidrRange.split();

// console logs:
// 192.168.208.0/25
// 192.168.208.128/25

splitRanges.forEach(range => console.log(range.toCidrString()))

But what if one needs to take an IP range and instead of just splitting into two, there is the requirement to split into a number of ranges with a specified prefix? Well, that is now possible with the addition of splitInto method:

import {IPv4CidrRange} from "ip-num/IPRange";
import {IPv4Prefix} from "ip-num/Prefix";

let ipv4CidrRange = IPv4CidrRange.fromCidr("192.168.208.0/24");
let splitRanges: Array<IPv4CidrRange> = 
ipv4CidrRange.splitInto(IPv4Prefix.fromNumber(26));

// console logs:
// 192.168.208.0/26
// 192.168.208.64/26
// 192.168.208.128/26
// 192.168.208.192/26

splitRanges.forEach(range => console.log(range.toCidrString()))

RangedSet.isCidrAble() incorrect results

Previous versions of ip-num failed to properly report that a string representing a single IP can be represented in the CIDR notation. This has now been fixed:

import {RangedSet} from "ip-num/IPRange";

let rangedSet= RangedSet.fromRangeString("1.2.3.4-1.2.3.4");

// now returns true
console.log(rangedSet.isCidrAble())

As always, ip-num is just an npm install or npm upgrade away.

Feel free to open an issue to discuss a feature or to report a bug.


Sunday, June 02, 2019

How to Quickly Calculate Exponents With Base 2

This post is part of the series of posts on Tips to Quickly Perform Binary and CIDR Operations. It is borne out of my experience working on ip-num, a typescript library for working with ASN, IPv4, and IPv6 numbers.

This post is about how to quickly calculate exponents with base 2.

Unfortunately I am yet to find a magical tip to use for this task that does not involve having to memorise the result of some calculations, so the core of being able to quickly perform exponents of base 2, involves some memorisation.

Below are the calculations I have come to memorise:

21 = 2
22 = 4
23 = 8
24 = 16
25 = 32
26 = 64
27 = 128
28 = 256
29 = 512
210 = 1024
216 = 65536

The only tip to know is that with each increment of the exponent, the value of the calculation doubles. For example 26 is 64, increasing the exponent by 1, ie 27 leads to 128 which is a doubling of 64.

With this, I can then easily deduce the value of 217 as the double of 65536 which is 131072.



Previous post: How To Tell If Two IP Ranges Are Adjacent

Saturday, June 01, 2019

How To Tell If Two IP Ranges Are Adjacent

This post is about how to tell if two IP ranges are adjacent. It is part of series of posts on Tips to Quickly Perform Binary and CIDR Operations.

How to tell if two IP ranges are adjacent


Task:
You have two CIDR notations representing two IP ranges, and you want to tell if they are adjacent, or not.

Steps to answer:
  • The prefix value must be the same. CIDR notations with different prefix can never be adjacent
  • Used the steps outlined in How to quickly tell the first and last IP in a CIDR range, to figure out the first and last ip numbers in the two ranges
  • Check if adding 1 to the last IP address of one will lead to the starting IP address of the other. If so, then the two ranges are adjacent

Example:
Determine if 10.0.4.0/24 and 10.0.5.0/24 are adjacent




Previous post: How To Calculate How Many Smaller IP Ranges Can Be Gotten From a Bigger IP Range
Next post: How to quickly calculate exponents of base 2

How To Calculate How Many Smaller IP Ranges Can Be Gotten From a Bigger IP Range

This post is about how to calculate how many smaller IP range are there in a bigger IP range. It is part of series of posts on Tips to Quickly Perform Binary and CIDR Operations.

How many smaller IP ranges can be gotten from a bigger IP range


Task:
You have a big IP range, and you want to break it into smaller IP range, and you want to know how many of those smaller IP range you will end up with. For example, having a /22, how many /25 are in a /22

Steps to answer:
  • Subtract the ranges.
  • Calculate 2 raised to the power of the value above.
  • The number in the previous step will be the amount of the smaller range in the big range

Example:
How much /25 are in /22?
  • 25 - 22 = 3
  • 2 raised to power 3 = 8
  • There are 8 /25 in /22


Previous post: How To Quickly Tell The First And Last IP In a CIDR Range
Next post: How to tell if two IP ranges are adjacent

How To Quickly Tell The First And Last IP In a CIDR Range

This post is part of the series of posts on Tips to Quickly Perform Binary and CIDR Operations. It is borne out of my experience working on ip-num, a typescript library for working with ASN, IPv4, and IPv6 numbers.

In this post I would show the steps I have come to use to be able to determine the start IP address depicted by a CIDR notation.

How to quickly tell the first and last ip in a CIDR range

Task: How to tell the first and last IP number in a CIDR range. Ideally the format of a CIDR range dictates that the portion of the notation that depicts the IP number should be the first number in the range, but often times, you cannot be sure that this is being adhered to when you encounter CIDR notations in the wild, so it is useful to be able to look at a CIDR notation, and tell which are the first and last IP addresses and whether the IP portion of the notation is indeed the first IP in the range.

Step to answer: Specialised to IPv4
  • Have /8, /16 and /24 as prefix bit boundaries 
  • If the prefix is any of the prefix boundary, then the starting octet will be zero and will always be in the 2nd, 3rd and 4th octet for /8, /16, /24 respectively. If the prefix under consideration is not the bit boundary, then continue with the following steps
  • Pick the bit boundary prefix which is least smaller than the prefix of the CIDR notation you are reading. So: 
    • if you have /26, pick /24
    • If you have /23, pick /16
    • If you have /19, pick /16
    • If you have /12, pick /8
    • If you have a prefix less than 8, pick /8 then follow the steps for prefix less than /8 below
  • Subtract the prefix in your CIDR notation, from the bit boundary prefix you picked above. So If you have a 10.3.4.5/19 do 19 - 16 = 3
  • Subtract the number above from 8. In this case that would be 8 - 3 = 5 
  • Find 2 raised to the power of the subtraction. For above that will be 25 = 32
  • This means the starting octet of the first IP number must be a multiple of the value calculated in previous step. For example, in previous step, the calculated value is 32, hence the starting octet must be a multiple of 32 - 32, 64, 96 etc
  • Pick which of the four octets would contain the starting octet based on the bit boundary prefix picked
  • If you pick /24, the 3rd octet will be fixed, starting will be on the 4th octet: fixed.fixed.fixed.start
  • If you pick /16, the 2nd octet will be fixed, starting will be on the 3rd octet:
    fixed.fixed.start.x
  • If you pick /8 the 1st octet will be fixed, starting will be on the 2nd octet:
    fixed.start.x.x

How To Tell Which CIDR Notation is Bigger

This post is about how to quickly tell which range, represented by CIDR notation is bigger: that is contains a higher number of IP addresses. It is part of series of posts on Tips to Quickly Perform Binary and CIDR Operations.

How to tell which CIDR notation is bigger

Task:
Given CIDR notations, be able to tell which is bigger: That is which contains more IP addresses.

Step to answer:
The CIDR notation with the biggest IP prefix, contains the lesser IP address.

This can be deduced in the steps outlined in the post: How To Tell The Count of IP Numbers In a CIDR Notation: the larger the prefix, the less the amount that would be left after it is subtracted from 32 or 128 if IPv6.

Example:
Out of the following CIDR prefix: /17, /25, /30 which is the largest and the smallest?
/17 - Largest
/30 - Smallest

I admit, it needs some getting used to, to be able to infer that the smaller number actually depicts a larger range.

Previous post: How To Tell The Count of IP Numbers In a CIDR Notation
Next post: How to quickly tell the first and last ip in a CIDR range

How To Tell The Count of IP Numbers In a CIDR Notation

This post about how to get the count of IP numbers a CIDR notation depicts, is in the series of posts on Tips to Quickly Perform Binary and CIDR Operations. It is borne out of my experience working on ip-num, a typescript library for working with ASN, IPv4, and IPv6 numbers.

How to tell the count of IP number in a CIDR notation

Task: 
You have a CIDR notation and you want to quickly calculate how many ip numbers present in the range represented by the CIDR notation.

Step to answer:
  • Subtract the CIDR prefix from 32. If it is an IPv6 address, subtract from 128
  • Raise 2 to the power of the result of the above subtraction
Example:
How many IP addresses are in a /17, /25, /30 assuming IPv4?
/17 = 2 (32-17) = 2 15 = 32768 IP addresses
/25 = 2 (32-25) = 2 7 = 128 IP addresses
/30 = 2 (32-30) = 2 2 = 4 IP addresses

Previous post: How to tell the size of IP number in a CIDR notation
Next post: How to tell which CIDR notation is bigger

Tips to Quickly Perform Binary and CIDR Operations

I maintain ip-num, a typescript library for working with ASN, IPv4, and IPv6 numbers. The library provides representations of these internet protocol numbers with the ability to perform various IP related operations like parsing, validating etc. on them

One of the side effects of working on such a library is that I get to become conversant with some bit (yup, intended) of Networking concepts: binary numbers and how that feed into how IP numbering works.

For example, how to read CIDR notations and tell how many IP number is in a range, or given a CIDR notation, how to tell if it is valid or not.

This post is just a quick pen down of some of these operations that has to do with IP numbers and ranges that I have encountered and how I have learnt to perform them quickly in my head, or at-least, with the help of some minimal jotting on a piece of paper.

This post was initially meant as a single post, but it quickly grew too long, so I am only including the first tip in this post and split the rest into various mini posts. Find the links below:

How to quickly convert binary to decimal

At around the time I started ip-num, I stumbled on an algorithm that can be used to quickly convert binary number to decimal mentally. The steps was stated on this link, which unfortunately is no longer up, but the content can still be seen via the wayback machine here

It is a simple step, and instead of recreating it, I copy it as it is stated:

  • Start at the first ‘1’ on the left, and start with the mental number one
  • Move one digit right. If that digit is a zero, multiply your mental number by two. If it is a one, multiply your mental number by two and add one.
  • Repeat step 2 for every digit of the binary number

Applying these steps to convert the binary number 1011010:

  • 1011010 – We start at the first one. Our mental total: 1
  • 1011010 – Next digit is a zero; we double our mental number: 1 x 2 = 2.
  • 1011010 – Next digit is a one; we double our mental number and add one: 2 x 2 + 1 = 5
  • 1011010 – Another one; double and add one: 5 x 2 + 1 = 11
  • 1011010 – Zero; double: 11 x 2 = 22
  • 1011010 – One; double and add one: 22 x 2 + 1 = 45
  • 1011010 – And finally a zero; double: 45 x 2 = 90

Nifty right?



Next post: How to tell the size of IP number in a CIDR notation

Thursday, May 16, 2019

ip-num version 1.2.0 has been released.

NOTE: A cyclic dependency issue was found in the 1.2.0 release and has been fixed with the 1.2.1 patch release. It is thus advised to use version 1.2.1. The content of this post still applies. 
ip-num version 1.2.0 has been released.













This release contains the following enhancements/additions:

Switched JavaScript target to es5 to ease usage in ie11.

Based on this issue, the generated JavaScript for ip-num is now es5. This is to make it easier to use in browsers that do not support es6: like internet explorer 11. Unfortunately things won’t still work right out of the box, and a polyfill for string.prototype.repeat needs to be included. Trying to get things to work in ie11 is already sucking out the fun on working on the library, so I would not be spending any more energy/time on this. Perhaps in the future I will look into getting things to work out of the box with ie11...or perhaps not.

Moved some operations to the IPRange interface.

The following methods were moved to the IPRange interface:
  • isConsecutive - Indicates whether the given IP range is an adjacent range
  • Contains - Indicates if the given IP range is contained within the range
  • Inside - inverse of Contains. It indicates if the range is contained inside the given range
  • isOverlapping - Checks if two IP ranges overlap (which will always be false with CIDR ranges)

Added method to IPRange to return the adjacent ranges.

This enables the ability to check if a range has next or previous range and the ability to return such a range. So for example:

IPv4CidrRange.fromCidr("255.255.254.0/24").hasNextRange() will return true and
IPv4CidrRange.fromCidr("255.255.254.0/24").nextRange().toCidrString() will return 255.255.255.0/24



Added ability to validate if a CIDR notation represents a valid range.

Added isValidIPv4CidrRange/isValidIPv6CidrRange validation methods that returns true or false depending on if the given ip number represents the start of the CIDR range.

For example, validating 10.0.0.0/8 should return true, since 10.0.0.0 is the start of the CIDR range depicted, but validating 10.0.4.23/8 should return false, since 10.0.4.23/8 is not the start of the range.

This new validation is a stricter form of already existing isValidIPv4CidrNotation/isValidIPv6CidrNotation which only validates that the given string is of the format [ip number]/[range].

That is basically the summary of the 1.2.0 release. Work would start soon on the 1.3.0 version, which would focus mainly on adding operations for working with sets of IP numbers to the library.

Saturday, July 14, 2018

ip-num 1.1.1 is Released

ip-num, version 1.1.1 is now available.


I will quickly highlight some of the new features, improvement and some bug fixes contained in this release.

Support for IPv4-Mapped IPv6 Address

It is possible to embed an IPv4 address within an IPv6 address. Doing this involves using the least significant 32 bits to encode the IPv4 address, the next least 16 significant bits is turned On, and the remaining 80 bits turned Off. The format of the "IPv4-mapped IPv6 address" Looks like this:

   |                80 bits               | 16 |      32 bits        |
   +--------------------------------------+--------------------------+
   |0000..............................0000|FFFF|    IPv4 address     |
   +--------------------------------------+----+---------------------+

ip-num now supports creating these kinds of IPv6 addresses. This can be achieved in various ways:

Converting from an existing IPv4
import { IPv4 } from "ip-num/IPv4";

let ipv4 = new IPv4("74.125.43.99")
ipv4.toIPv4MappedIPv6().toString() // produces ::ffff:4a7d:2b63

From an existing IPv4 using convenience method on IPv6
import { IPv6 } from "ip-num/IPv6";

let ipv6 = IPv6.fromIPv4(new IPv4("74.125.43.99"))
ipv6.toString() // produces ::ffff:4a7d:2b63

From dot-decimal notation using convenience method on IPv6
import { IPv6 } from "ip-num/IPv6";

let ipv6 = IPv6.fromIPv4DotDecimalString("74.125.43.99")
ipv6.toString() // produces ::ffff:4a7d:2b63

Convenience methods for creating IPv4, IPv6 and ASN from Binary string

ip-num now has method that allows the creation of IPv4, IPv6, and ASN instances from binary string.

import { Asn } from "ip-num/Asn";
import { IPv4 } from "ip-num/IPv4";
import { IPv6 } from "ip-num/IPv6";

let anAsn = Asn.fromBinaryString('1111');
let anIPv4 = IPv4.fromBinaryString("01001010011111010010101101100011");
let anIPv6 = IPv6.fromBinaryString("01001010011111010010101101100011");

Improvement to various Validators

Big fixes, increased test coverage and improvements were made to Validators. This mainly touches the Validator.isValidIPv4String() and Validator.isValidIPv6String() validators.

For a complete view of the changes included in the v1.1.1 release, please check the change log.

As usual, you can download the source here. Or just run:

npm install ip-num //or npm install ip-num@1.1.1

to add ip-num as a dependency to your next project.

Feel free to open an issue to discuss a feature or to report a bug.

ps: v1.1.0 was unpublished due to inadvertently publishing some dev dependencies as normal dependencies