0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

IPv4 サブネット計算機(バイナリ可視化 + /31 対応)

0
Posted at

作ったもの

IPv4 Subnethttps://sen.ltd/portfolio/ipv4-subnet/

スクリーンショット

  • IP + CIDR 入力
  • ネットワーク / ブロードキャスト / 第一ホスト / 最終ホスト / 総ホスト数 / 利用可能ホスト数
  • サブネットマスク、ワイルドカードマスク、クラス判定
  • RFC 1918 プライベート判定
  • バイナリ可視化
  • サブネット分割
  • CIDR 対応表

vanilla JS、ゼロ依存、ビルド不要node --test で 63 ケース。

IP は 32 ビット符号なし整数

return (p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3]) >>> 0;

>>> 0 で符号なしに変換。これを忘れると 192.x.x.x が負の数になってマスク演算が壊れる。

サブネット演算はビット演算一発

const network   = (ip & mask) >>> 0;
const broadcast = (network | (~mask >>> 0)) >>> 0;
const firstHost = (network + 1) >>> 0;
const lastHost  = (broadcast - 1) >>> 0;

AND でネットワーク、OR でブロードキャスト。15 行のビット演算で全部出る。

/31 と /32 は特別扱い

  • /32: ホストルート 1 個。ネットワーク/ブロードキャスト概念なし
  • /31: RFC 3021、ポイントツーポイントリンク。ネットワークもブロードキャストもなく、2 個とも使える

2^(32-cidr) - 2 の公式だと -1 や 0 になるので特別扱いが必要。

サブネット分割

const count = 2 ** (newCidr - oldCidr);
const step = 2 ** (32 - newCidr);
for (let i = 0; i < count; i++) {
  subnets.push({ network: networkIp + i * step, cidr: newCidr });
}

/16 を /24 に分割すると 256 個、/30 なら 16384 個。UI は表示を 1024 にキャップ。

シリーズ

100+ 公開ポートフォリオ シリーズの #96 です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?