Skip to content

Commit

Permalink
Add some IPv4 address utility functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielwippermann committed Jan 20, 2024
1 parent 22ad48e commit 206c6b4
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/tcp-data-source-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,30 @@ const DataSourceProvider = require('./data-source-provider');



const ipv4Re = /^\d+\.\d+\.\d+\.\d+$/;

function parseIpV4Address(string) {
if (!ipv4Re.test(string)) {
throw new Error('Invalid IPv4 input');
}
return string.split('.').map(s => +s);
}

function formatIpV4Address(parts) {
return parts.map(p => p.toString()).join('.');
}

function calculateIpV4BroadcastAddress(address, netmask) {
const addressParts = parseIpV4Address(address);
const netmaskParts = parseIpV4Address(netmask);
const bcastParts = [ 0, 0, 0, 0, ];
for (let i = 0; i < 4; i++) {
bcastParts [i] = addressParts [i] | (netmaskParts [i] ^ 255);
}
const broadcastAddress = formatIpV4Address(bcastParts);
return broadcastAddress;
}

class TcpDataSourceProvider extends DataSourceProvider {

/**
Expand Down

0 comments on commit 206c6b4

Please sign in to comment.