- 1#include <arpa/inet.h> // for inet_pton
- 2
- 3static int is_ipv4_prefix(const char *host, size_t *prefix_len)
- 4{
- 5 size_t i = 0;
- 6 while (host[i] && ((host[i] >= '0' && host[i] <= '9') || host[i] == '.'))
- 7 i++;
- 8 if (i == 0)
- 9 return 0;
- 10 char buf[32];
- 11 if (i >= sizeof(buf)) // too long to be valid IPv4
- 12 return 0;
- 13 memcpy(buf, host, i);
- 14 buf[i] = '\0';
- 15 struct in_addr addr;
- 16 if (inet_pton(AF_INET, buf, &addr) == 1) {
- 17 *prefix_len = i;
- 18 return 1;
- 19 }
- 20 return 0;
- 21}
Raw Paste