inline IpPrefix getSubnet()

in common/ipprefix.h [146:187]


    inline IpPrefix getSubnet() const
    {
        switch (m_ip.getIp().family)
        {
            case AF_INET:
            {
                uint32_t ipaddr = m_ip.getV4Addr();
                uint32_t mask = getMask().getV4Addr();

                ipaddr &= mask;
                return IpPrefix(ipaddr, m_mask);
            }

            case AF_INET6:
            {
                const uint8_t *ipaddr = m_ip.getV6Addr();
                uint8_t subnet[INET6_ADDRSTRLEN] = {0};
                memcpy(subnet, ipaddr, 16);

                IpAddress ip6mask = getMask();
                const uint8_t *mask = ip6mask.getV6Addr();

                for (int i = 0; i < 16; ++i)
                {
                    subnet[i] &= mask[i];
                }

                char buf[INET6_ADDRSTRLEN];
                std::string ipStr(inet_ntop(AF_INET6,
                                            &subnet,
                                            buf,
                                            INET6_ADDRSTRLEN));

                return IpPrefix(ipStr + "/" + std::to_string(m_mask));
            }

            default:
            {
                throw std::logic_error("Invalid family");
            }
        }
    }