duangang2825
2016-03-22 22:56
浏览 77
已采纳

如何获取(IPv4)net.IPNet的广播地址?

Is there a built-in function to calculate the broadcast address of a net.IPNet struct?

图片转代码服务由CSDN问答提供 功能建议

是否有内置函数来计算 net.IPNet 的广播地址

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dougaimian1143 2016-03-23 00:15
    已采纳

    The link @Dsafds has the answer. Here for history's sake, and so you don't have to read through the thread yourself. Code is Mikio Hara's with minor modifications made by me.

    func lastAddr(n *net.IPNet) (net.IP, error) { // works when the n is a prefix, otherwise...
        if n.IP.To4() == nil {
            return net.IP{}, errors.New("does not support IPv6 addresses.")
        }
        ip := make(net.IP, len(n.IP.To4()))
        binary.BigEndian.PutUint32(ip, binary.BigEndian.Uint32(n.IP.To4())|^binary.BigEndian.Uint32(net.IP(n.Mask).To4()))
        return ip, nil
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题