Untitled

Cpp Detected boxlabs 87 Views Size: 544 bytes Posted on: Sep 15, 25 @ 6:08 PM
  1. #include <arpa/inet.h>   // for inet_pton
  2.  
  3. static 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

Comments 0
Login to post a comment.
  • No comments yet. Be the first.
Login to post a comment. Login or Register
We use cookies. To comply with GDPR in the EU and the UK we have to show you these.

We use cookies and similar technologies to keep this website functional (including spam protection via Google reCAPTCHA or Cloudflare Turnstile), and — with your consent — to measure usage and show ads. See Privacy.