beaugunderson / ip-address

💻 a library for parsing and manipulating IPv4 and IPv6 addresses in JavaScript
http://ip-address.js.org/
MIT License
525 stars 71 forks source link

Wrong Link-Local detection and others (getScope method) #122

Open didix16 opened 3 years ago

didix16 commented 3 years ago

Hi,

I'm trying to detect some scopes from IPv6 but it seems does not work, specially with link local

let ip = "2002::"
let address = new ipAddress.Addess6(ip);

// console.log(address.getScope()) -> prints 'Link local'. Should be 'Reserved'
// This is false since fe80::/10 are currently those designed for Link-Local
// 2002::/16 are reserved for 6to4

let ip = "fe80::2ff:33ff:feaa:bbcc"
let address = new ipAddress.Addess6(ip);

// console.log(address.getScope()) -> prints 'Reserved'. Should be 'Link Local'

However .isLinkLocal() seems to work fine. Maybe only getScope() method is failling.

beaugunderson commented 3 years ago

@didix16 ah, I think you want getType(), though I do think there are some improvements that could be made, including the use of enums:

/**
 * Represents IPv6 address scopes
 * @memberof Address6
 * @static
 */
export const SCOPES: { [key: number]: string | undefined } = {
  0: 'Reserved',
  1: 'Interface local',
  2: 'Link local',
  4: 'Admin local',
  5: 'Site local',
  8: 'Organization local',
  14: 'Global',
  15: 'Reserved',
} as const;

/**
 * Represents IPv6 address types
 * @memberof Address6
 * @static
 */
export const TYPES: { [key: string]: string | undefined } = {
  'ff01::1/128': 'Multicast (All nodes on this interface)',
  'ff01::2/128': 'Multicast (All routers on this interface)',
  'ff02::1/128': 'Multicast (All nodes on this link)',
  'ff02::2/128': 'Multicast (All routers on this link)',
  'ff05::2/128': 'Multicast (All routers in this site)',
  'ff02::5/128': 'Multicast (OSPFv3 AllSPF routers)',
  'ff02::6/128': 'Multicast (OSPFv3 AllDR routers)',
  'ff02::9/128': 'Multicast (RIP routers)',
  'ff02::a/128': 'Multicast (EIGRP routers)',
  'ff02::d/128': 'Multicast (PIM routers)',
  'ff02::16/128': 'Multicast (MLDv2 reports)',
  'ff01::fb/128': 'Multicast (mDNSv6)',
  'ff02::fb/128': 'Multicast (mDNSv6)',
  'ff05::fb/128': 'Multicast (mDNSv6)',
  'ff02::1:2/128': 'Multicast (All DHCP servers and relay agents on this link)',
  'ff05::1:2/128': 'Multicast (All DHCP servers and relay agents in this site)',
  'ff02::1:3/128': 'Multicast (All DHCP servers on this link)',
  'ff05::1:3/128': 'Multicast (All DHCP servers in this site)',
  '::/128': 'Unspecified',
  '::1/128': 'Loopback',
  'ff00::/8': 'Multicast',
  'fe80::/10': 'Link-local unicast',
} as const;

re: 6to4 addresses, they don't necessarily map to a "scope" or a "type", so perhaps a new function that returns a well-known allocation would be useful?

beaugunderson commented 3 years ago

some more background here

didix16 commented 3 years ago

Yeah @beaugunderson. That would be nice! Also I'm missing the ULA range identification (those which starts with fd00::/8) https://en.wikipedia.org/wiki/Unique_local_address

Thanks for your effort :+1: