IP falls in CIDR range

后端 未结 2 610
忘了有多久
忘了有多久 2021-02-10 12:16

I have an IP like this: 12.12.12.12
I\'m looping through different IP ranges (in 12.12.12.0/24 (example)) format, and trying to see if the IP is in the range.
I have t

2条回答
  •  别跟我提以往
    2021-02-10 13:03

    Just test whether:

    (ip & netmask) == (range & netmask)
    

    You can determine the netmask from the CIDR parameters range/netbits as follows:

    uint32_t netmask = ~(~uint32_t(0) >> netbits);
    

提交回复
热议问题