Enhancement: add constants for IPv4 and IPv6 subnet masks

未關閉
#83 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
3/5
預估耗時
1-2 天
新手友好度
64/100
Issue 類型
功能
描述清晰度
基本清楚
活躍度
冷清
技術堆疊
ruby
領域
networking

研究方向

從 IPAddr 類別開始,檢查現有的 IPv4、IPv6 和網路遮罩處理。為提議的子網路遮罩常數新增涵蓋測試,如果包含可選的述詞,也為其 IPv4 和 IPv6 情況新增測試;完成的條件是 CIDR 索引會回傳文件所述的遮罩,並拒絕非連續遮罩。

由索引模型根據 Issue 內容生成。

描述

Rationale: This SO question demonstrates there is at least some need to manipulate netmasks into CIDR integers. The accepted answer contains a bug which can be easily eliminated by hard coding the 2 sets of netmasks into the IPAddr class - see my alternative answer.

Proposed solution: Define IPAddr::IPV4_SUBNET_MASKS and IPAddr::IPV6_SUBNET_MASKS as follows:

class IPAddr
  IPV4_SUBNET_MASKS = (0..32).map { |n| IPAddr.new("0.0.0.0/#{n}").netmask }
  IPV6_SUBNET_MASKS = (0..128).map { |n| IPAddr.new("0::/#{n}").netmask }
end

# Now a user can do the following to get a subnet mask from a CIDR integer:
IPAddr::IPV4_SUBNET_MASKS[24]
# => "255.255.255.0"

IPAddr::IPV6_SUBNET_MASKS[64]
# => "ffff:ffff:ffff:ffff:0000:0000:0000:0000"

Optional: Add a predicate to check if an instance of IPAddr is a netmask:

class IPAddr
  def netmask?
    if ipv4?
      IPV4_SUBNET_MASKS.include? to_s
    else
      IPV6_SUBNET_MASKS.include? to_s
    end
  end
end

ipaddr1 = IPAddr.new '255.254.255.0'
ipaddr1.netmask? # => false

ipaddr2 = IPAddr.new '255.255.255.0/24'
ipaddr2.netmask? # => true

ipaddr3 = IPAddr.new '255.255.255.0'
ipaddr3.netmask? # => true

# And so on for IPv6 addresses
主要語言
Ruby
星號
82
分支
42
平均合併
2 小時 24 分鐘
30 天內合併 PR
3

貢獻指南

這個儲存庫沒有索引到貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

ruby/ipaddr 的其他 Issue

查看 ruby/ipaddr 的全部 Issue

相似的 Issue

更多 Ruby Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。