From 1cdf18747db4fa5e0e20c351896545dff31ea103 Mon Sep 17 00:00:00 2001 From: Tilen Komel Date: Wed, 7 Aug 2024 08:35:16 +0200 Subject: [PATCH] Added ip utils --- src/core/utils/ip.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/core/utils/ip.ts diff --git a/src/core/utils/ip.ts b/src/core/utils/ip.ts new file mode 100644 index 00000000..09f7c935 --- /dev/null +++ b/src/core/utils/ip.ts @@ -0,0 +1,13 @@ +export function convertIntToIpAddress(int: number): string { + return `${int & 0xff}.${(int >> 8) & 0xff}.${(int >> 16) & 0xff}.${(int >> 24) & 0xff}`; +} + +export function convertIpAddressToInt(ip: string): number | null { + const parts = ip.split('.').map(Number).reverse(); // little-endian byte order + + if (parts.some(Number.isNaN)) { + return null; + } + + return parts.reduce((total, part) => (total << 8) | part, 0); +} \ No newline at end of file