Skip to content

Commit

Permalink
feat: add isLinkLocal function
Browse files Browse the repository at this point in the history
Add function for detecting link-local multiaddrs
  • Loading branch information
achingbrain committed Nov 28, 2024
1 parent ce2f45e commit 0366d0a
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@
"types": "./dist/src/moving-average.d.ts",
"import": "./dist/src/moving-average.js"
},
"./multiaddr/is-link-local": {
"types": "./dist/src/multiaddr/is-link-local.d.ts",
"import": "./dist/src/multiaddr/is-link-local.js"
},
"./multiaddr/is-loopback": {
"types": "./dist/src/multiaddr/is-loopback.d.ts",
"import": "./dist/src/multiaddr/is-loopback.js"
Expand Down
29 changes: 29 additions & 0 deletions packages/utils/src/multiaddr/is-link-local.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { Multiaddr } from '@multiformats/multiaddr'

const CODEC_IP4 = 0x04
const CODEC_IP6 = 0x29

/**
* Check if a given multiaddr is a link-local address
*/
export function isLinkLocal (ma: Multiaddr): boolean {
try {
const [[codec, value]] = ma.stringTuples()

if (value == null) {
return false
}

Check warning on line 15 in packages/utils/src/multiaddr/is-link-local.ts

View check run for this annotation

Codecov / codecov/patch

packages/utils/src/multiaddr/is-link-local.ts#L14-L15

Added lines #L14 - L15 were not covered by tests

if (codec === CODEC_IP4) {
return value.startsWith('169.254.')
}

if (codec === CODEC_IP6) {
return value.toLowerCase().startsWith('fe80')
}
} catch {

Check warning on line 25 in packages/utils/src/multiaddr/is-link-local.ts

View check run for this annotation

Codecov / codecov/patch

packages/utils/src/multiaddr/is-link-local.ts#L25

Added line #L25 was not covered by tests
}

return false
}
59 changes: 59 additions & 0 deletions packages/utils/test/multiaddr/is-link-local.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* eslint-env mocha */

import { multiaddr } from '@multiformats/multiaddr'
import { expect } from 'aegir/chai'
import { isLinkLocal } from '../../src/multiaddr/is-link-local.js'

describe('multiaddr isLinkLocal', () => {
it('identifies link-local ip4 multiaddrs', () => {
[
multiaddr('/ip4/169.254.35.4'),
multiaddr('/ip4/169.254.35.4/tcp/1000'),
multiaddr('/ip4/169.254.0.0/tcp/1000'),
multiaddr('/ip4/169.254.255.255/tcp/1000')
].forEach(ma => {
expect(isLinkLocal(ma)).to.be.true()
})
})

it('identifies non link-local ip4 multiaddrs', () => {
[
multiaddr('/ip4/101.0.26.90/tcp/1000'),
multiaddr('/ip4/10.0.0.1/tcp/1000'),
multiaddr('/ip4/192.168.0.1/tcp/1000'),
multiaddr('/ip4/172.16.0.1/tcp/1000')
].forEach(ma => {
expect(isLinkLocal(ma)).to.be.false()
})
})

it('identifies link-local ip6 multiaddrs', () => {
[
multiaddr('/ip6/fe80::1%lo0'),
multiaddr('/ip6/fe80::1%lo0/tcp/1000'),
multiaddr('/ip6/fe80::1893:def4:af04:635a%en'),
multiaddr('/ip6/fe80::1893:def4:af04:635a'),
multiaddr('/ip6/fe80::1893:def4:af04:635a/udp/2183')
].forEach(ma => {
expect(isLinkLocal(ma)).to.be.true()
})
})

it('identifies non link-local ip6 multiaddrs', () => {
[
multiaddr('/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/1000'),
multiaddr('/ip6/::/tcp/1000')
].forEach(ma => {
expect(isLinkLocal(ma)).to.be.false()
})
})

it('identifies other multiaddrs as not link-local addresses', () => {
[
multiaddr('/dns4/wss0.bootstrap.libp2p.io/tcp/443'),
multiaddr('/dns6/wss0.bootstrap.libp2p.io/tcp/443')
].forEach(ma => {
expect(isLinkLocal(ma)).to.be.false()
})
})
})

0 comments on commit 0366d0a

Please sign in to comment.