Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add conversion to reverse format and back for ipv4 #140

Merged
merged 2 commits into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/common.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Address4 } from './ipv4';
import { Address6 } from './ipv6';

export interface ReverseFormOptions {
omitSuffix?: boolean;
}

export function isInSubnet(this: Address4 | Address6, address: Address4 | Address6) {
if (this.subnetMask < address.subnetMask) {
return false;
Expand Down
48 changes: 48 additions & 0 deletions lib/ipv4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,29 @@ export class Address4 {
return Address4.fromHex(integer.toString(16));
}


/**
* Return an address from in-addr.arpa form
* @memberof Address4
* @static
* @param {string} arpaFormAddress - an 'in-addr.arpa' form ipv4 address
* @returns {Adress4}
* @example
* var address = Address4.fromArpa(42.2.0.192.in-addr.arpa.)
* address.correctForm(); // '192.0.2.42'
*/
static fromArpa(arpaFormAddress: string): Address4 {
// remove ending ".in-addr.arpa." or just "."
const leader = arpaFormAddress.replace(/(\.in-addr\.arpa)?\.$/, '');

const address = leader.split('.')
.reverse()
.join('.');

return new Address4(address);
}


/**
* Converts an IPv4 address object to a hex string
* @memberof Address4
Expand Down Expand Up @@ -275,6 +298,31 @@ export class Address4 {
return this.binaryZeroPad().slice(start, end);
}

/**
* Return the reversed ip6.arpa form of the address
* @memberof Address4
* @param {Object} options
* @param {boolean} options.omitSuffix - omit the "in-addr.arpa" suffix
* @instance
* @returns {String}
*/
reverseForm(options?: common.ReverseFormOptions): string {
if (!options) {
options = {};
}

const reversed = this.correctForm()
.split('.')
.reverse()
.join('.');

if (options.omitSuffix) {
return reversed;
} else {
return sprintf('%s.in-addr.arpa.', reversed);
}
}

/**
* Returns true if the given address is in the subnet of the current address
* @memberof Address4
Expand Down
5 changes: 1 addition & 4 deletions lib/ipv6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ function unsignByte(b: number) {
return b & 0xff;
}

interface ReverseFormOptions {
omitSuffix?: boolean;
}

interface SixToFourProperties {
prefix: string;
Expand Down Expand Up @@ -506,7 +503,7 @@ export class Address6 {
* @instance
* @returns {String}
*/
reverseForm(options?: ReverseFormOptions): string {
reverseForm(options?: common.ReverseFormOptions): string {
if (!options) {
options = {};
}
Expand Down
8 changes: 8 additions & 0 deletions test/address-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ function addressIs(addressString: string, descriptors: string[]) {
address4.subnetMask.should.be.at.least(0);
address4.subnetMask.should.be.at.most(128);
});
it('converts to arpa format and back', () => {
const arpa = address4.reverseForm();
arpa.length.should.be.at.most(29);

const converted = Address4.fromArpa(arpa);

address4.correctForm().should.equal(converted.correctForm());
});
}

if (descriptor === 'valid-ipv6') {
Expand Down