There is already ip_in_subnet() that does the same, including IPv6