Skip to content

Commit

Permalink
- Added support for ip neighbour get.
Browse files Browse the repository at this point in the history
- Updated badges in README.
  • Loading branch information
diosney committed Jun 23, 2024
1 parent 154c4c3 commit 54ab80c
Show file tree
Hide file tree
Showing 16 changed files with 186 additions and 33 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
### Release Notes

### 2.3.0

- Added support for `ip neighbour get`.
- Updated badges in README.

### 2.2.2

- Merged dependabot PR.
Expand Down
11 changes: 11 additions & 0 deletions EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,17 @@ The `labels` output is an array of address labels with the matching [AddrlabelIn

The `entries` output is an array of ARP entries with the matching [NeighbourInfo[]](https://diosney.github.io/node-iproute/interfaces/NeighbourInfo.html) interface.

#### `neighbour.get(options, globalOptions?)`

**Example:**

const entries = await neighbour.get({
to: '10.0.1.10',
dev: 'eth0'
});

The `entries` output is an array of ARP entries with the matching [NeighbourInfo[]](https://diosney.github.io/node-iproute/interfaces/NeighbourInfo.html) interface.

### `ip ntable` Neighbour table configuration / [Man Page](https://man7.org/linux/man-pages/man8/ip-ntable.8.html)

import { ntable } from 'iproute';
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
# node-iproute

<a href="https://www.buymeacoffee.com/diosney" target="_blank"><img align="right" src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" width="117" style="width: 117px !important;"></a>

Show and manipulate network devices, addresses, routing, policy routing, tunnels, IP forwarding, address labels and other `iproute` objects.

Wrapper around native **iproute** suite to allow its functionality to be used in Node.js space.

[![donate](https://shields.io/badge/ko--fi-donate-ff5f5f?logo=ko-fi&style=for-the-badgeKo-fi)](https://ko-fi.com/diosney)
[![docs](https://github.com/diosney/node-iproute/actions/workflows/docs.yml/badge.svg?branch=master)](https://github.com/diosney/node-iproute/actions/workflows/docs.yml)
[![tests](https://github.com/diosney/node-iproute/actions/workflows/tests.yml/badge.svg?branch=master)](https://github.com/diosney/node-iproute/actions/workflows/tests.yml)
[![npm](https://img.shields.io/npm/v/iproute)](https://www.npmjs.com/package/iproute)
[![GitHub master version](https://img.shields.io/github/package-json/v/diosney/node-iproute/master)](https://github.com/diosney/node-iproute)

## Installation

Expand All @@ -26,7 +24,7 @@ Wrapper around native **iproute** suite to allow its functionality to be used in
| [rule](https://diosney.github.io/node-iproute/modules/rule.html) | Routing policy database (RPDB) management. | `add`, `del`, `save`, `restore`, `flush`, `show`, `list` |
| [monitor](https://diosney.github.io/node-iproute/modules/monitor.html) | State monitoring. | `on`, `close` |
| [addrlabel](https://diosney.github.io/node-iproute/modules/addrlabel.html) | Protocol address label management. | `add`, `del`, `list`, `flush` |
| [neighbour](https://diosney.github.io/node-iproute/modules/neighbour.html) | Neighbour/ARP tables management. | `add`, `del`, `change`, `replace`, `flush`, `show` |
| [neighbour](https://diosney.github.io/node-iproute/modules/neighbour.html) | Neighbour/ARP tables management. | `add`, `del`, `change`, `replace`, `flush`, `show`, `get` |
| [ntable](https://diosney.github.io/node-iproute/modules/ntable.html) | Neighbour table configuration. | `change`, `show` |
| [tunnel](https://diosney.github.io/node-iproute/modules/tunnel.html) | Tunnel configuration. | `add`, `change`, `del`, `show`, `prl`, `6rd` |
| [tuntap](https://diosney.github.io/node-iproute/modules/tuntap.html) | TunTap tunnel configuration. | `add`, `del`, `show`, `list`, `lst` |
Expand Down Expand Up @@ -76,7 +74,10 @@ On Debian-based OSes, if it's not already installed, you can do it by issuing:

Regarding the **iproute** version, the `-json` option is used for display operations and was introduced in `v4.10.0`.
<br>
Ensure that your system has at least this **iproute** version installed.
Ensure that your system has at least this version installed.

**Note:** JSON support for the `ip neighbour get` command was added at a later version, if you need this specific
command check the output in case it is not supported in your version.

### Permission Level

Expand Down
7 changes: 0 additions & 7 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
Those interfaces are: `RuleInfo`, `RouteInfo`, `LinkInfo`, `LinkWithAddressInfo`.

- `neighbour`
- Add `get` support after `json` support gets added.
- Add exec tests.

- `tunnel`
Expand All @@ -38,12 +37,6 @@

- Document every property with schema `format` as a `@see {@link}` in the interfaces?.

### Contact `ip` Developers

- Add `-json` support for:
- `monitor`
- `ip neigh get`

### Tests

- Add Docker to automatize `test:all`.
Expand Down
9 changes: 8 additions & 1 deletion __tests__/safe/cmd-generation/fixtures/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ import {
change as neighbourChange,
replace as neighbourReplace,
flush as neighbourFlush,
show as neighbourShow
show as neighbourShow,
get as neighbourGet
} from '../../../../src/commands/neighbour';

import {
Expand Down Expand Up @@ -139,6 +140,7 @@ import { Tests as neighbourChangeTests } from './neighbour-change';
import { Tests as neighbourReplaceTests } from './neighbour-replace';
import { Tests as neighbourFlushTests } from './neighbour-flush';
import { Tests as neighbourShowTests } from './neighbour-show';
import { Tests as neighbourGetTests } from './neighbour-get';

import { Tests as ntableChangeTests } from './ntable-change';
import { Tests as ntableShowTests } from './ntable-show';
Expand Down Expand Up @@ -393,6 +395,11 @@ export const fixtures: TestDefinition = {
operator : 'show',
testBattery: neighbourShowTests,
method : neighbourShow
},
{
operator : 'get',
testBattery: neighbourGetTests,
method : neighbourGet
}
],
ntable : [
Expand Down
51 changes: 51 additions & 0 deletions __tests__/safe/cmd-generation/fixtures/neighbour-get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {TestFixture} from '../../../../src/common/interfaces/tests';
import {NeighbourGetOptions} from '../../../../src';

export const Tests: TestFixture<NeighbourGetOptions>[] = [
{
description : 'with no params',
options : {
to : '10.0.1.10',
dev: 'eth0'
},
expectedCmd : [
'',
'ip',
'-details',
'-statistics',
'-json',
'neighbour',
'get',
'to',
'10.0.1.10',
'dev',
'eth0'
],
expectedCmdToExec: ` ip -details -statistics -json neighbour get to 10.0.1.10 dev eth0`
},
{
description : 'with proxy enabled',
options : {
proxy: true,
to : '10.0.1.10',
dev : 'eth0'
},
expectedCmd : [
'',
'ip',
'-details',
'-statistics',
'-json',
'neighbour',
'get',
'proxy',
'to',
'10.0.1.10',
'dev',
'eth0'
],
expectedCmdToExec: ` ip -details -statistics -json neighbour get proxy to 10.0.1.10 dev eth0`
}
];

export default Tests;
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iproute",
"version": "2.2.2",
"version": "2.3.0",
"description": "Show and manipulate network devices, addresses, routing, policy routing, tunnels, IP forwarding, address labels and other `iproute` objects.",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down Expand Up @@ -40,6 +40,7 @@
"addrlabel",
"neighbour",
"neigh",
"neighbour",
"ntable",
"tuntap",
"maddress",
Expand Down Expand Up @@ -68,8 +69,8 @@
"url": "https://github.com/diosney/node-iproute.git"
},
"funding": {
"type": "buymeacoffee",
"url": "https://www.buymeacoffee.com/diosney"
"type": "ko-fi",
"url": "https://ko-fi.com/diosney"
},
"files": [
"lib/"
Expand Down
2 changes: 1 addition & 1 deletion src/commands/link/set.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export interface LinkSetCommonOptions {
alias?: string;
/**
* Specify a Virtual Function device to be configured.
* The associated PF device must be specified using the {@link LinkSetDevOptions.dev_ | dev_} parameter.
* The associated PF device must be specified using the {@link LinkSetDevOptions.dev | dev} parameter.
*/
vf?: number;
vf_args?: {
Expand Down
50 changes: 49 additions & 1 deletion src/commands/neighbour.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { NeighbourDelOptions } from './neighbour/del.interfaces';
import { NeighbourDelSchema } from './neighbour/del.schema';
import { NeighbourShowSchema } from './neighbour/show.schema';
import { NeighbourInfo, NeighbourShowOptions } from './neighbour/show.interfaces';
import { NeighbourGetOptions } from './neighbour/get.interfaces';
import { NeighbourGetSchema } from './neighbour/get.schema';

/**
* Add a new neighbour entry.
Expand Down Expand Up @@ -227,11 +229,57 @@ export async function show(options: NeighbourShowOptions = {},
return await ipCmd.exec<NeighbourInfo[]>();
}

/**
* Lookup a neighbour entry to a destination given a device.
*
* @param options - Parameters options to be passed down to `ip`.
* @param globalOptions - Global parameters options that affects the command execution.
*
* @throws {@link ParametersError} - Throws when passed parameters are invalid.
* @throws {@link CommandError} - Throws when the executed command fails.
*
* @example
*
* Import module
* ```
* import { neighbour } from 'iproute';
* ```
*
* Performs a neighbour lookup in the kernel and returns a neighbour entry.
* ```
* const entries = await neighbour.get({
* to: '10.0.1.10',
* dev: 'eth0'
* });
* ```
*/
export async function get(options: NeighbourGetOptions,
globalOptions: GlobalOptions = {}): Promise<Command<NeighbourGetOptions> | NeighbourInfo[]> {

const cmd = ['ip', 'neighbour', 'get'];

const ipCmd = new CommandWithReturnedData<NeighbourGetOptions>(
SchemaIds.NeighbourGet,
NeighbourGetSchema,
options,
{
...globalOptions,
// Overrides for a better show.
'-details' : true,
'-statistics': true,
'-json' : true
},
cmd);

return await ipCmd.exec<NeighbourInfo[]>();
}

export default {
add,
del,
change,
replace,
flush,
show
show,
get
};
12 changes: 12 additions & 0 deletions src/commands/neighbour/get.interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Neighbour get options.
* @category Interfaces
*/
export interface NeighbourGetOptions {
/** Gets the proxy requests too. */
proxy?: true;
/** The prefix selecting the neighbours to list. */
to: string;
/** Only list the neighbours attached to this device. */
dev: string;
}
26 changes: 26 additions & 0 deletions src/commands/neighbour/get.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { JSONSchemaType } from 'ajv';

import { SchemaIds } from '../../common/constants/schemas';
import { NeighbourGetOptions } from './get.interfaces';

export const NeighbourGetSchema: JSONSchemaType<NeighbourGetOptions> = {
$id: SchemaIds.RouteGet,
type: 'object',
required: ['to', 'dev'],
properties: {
proxy: {
type: 'boolean',
enum: [true],
nullable: true
},
to: {
type: 'string',
minLength: 1,
format: 'ip'
},
dev: {
type: 'string',
minLength: 1
}
}
};
10 changes: 5 additions & 5 deletions src/commands/neighbour/show.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ export interface NeighbourInfo {
dst: string;
dev: string;
lladdr: string;
refcnt: number;
used: number;
confirmed: number;
updated: number;
probes: number;
state: string[];
refcnt?: number;
used?: number;
confirmed?: number;
updated?: number;
probes?: number;
}
7 changes: 2 additions & 5 deletions src/common/constants/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { JSONSchemaType } from 'ajv';

import {
FilePathRequiredGlobalOption,
GlobalOptions, StdinRequiredGlobalOption
} from '../interfaces/common';
import { Empty, FilePathRequiredGlobalOption, GlobalOptions, StdinRequiredGlobalOption } from '../interfaces/common';

import { ComplexIpCommandTestOptions } from '../interfaces/tests';
import { TestEnum } from './tests';
import { Empty } from '../interfaces/common';

/**
* Schema ids.
Expand Down Expand Up @@ -50,6 +46,7 @@ export enum SchemaIds {
NeighbourAdd = '#neighbour-add',
NeighbourDel = '#neighbour-del',
NeighbourShow = '#neighbour-show',
NeighbourGet = '#neighbour-get',

NtableShow = '#ntable-show',
NtableChange = '#ntable-change',
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ export { NudStates } from './commands/neighbour.constants';
export { NeighbourAddOptions } from './commands/neighbour/add.interfaces';
export { NeighbourDelOptions } from './commands/neighbour/del.interfaces';
export { NeighbourShowOptions, NeighbourInfo } from './commands/neighbour/show.interfaces';
export { NeighbourGetOptions } from './commands/neighbour/get.interfaces';

/**
* Neighbour table configuration.
Expand Down
Loading

0 comments on commit 54ab80c

Please sign in to comment.