Increment individual IPs from IPv6 string (php) -



Increment individual IPs from IPv6 string (php) -

what simple, elegant way list first x number of ipv6 ips ipv6 string.

for example,

listips("2600:f333:10:c000::0", 4)

echos

2600:f333:10:c000::1 2600:f333:10:c000::2 2600:f333:10:c000::3 2600:f333:10:c000::4

here's sample of code may have worked ipv4, converted int:

$input = "2600:f333:10:c000::/51"; $max = 4; list($block, $cidr) = explode("/", $input); $first = inet_pton( $block ); echo inet_ntop($first) . "\n"; ($i = 1; $i < $max; $i++) { //todo: die if has exceeded block size based on $cidr echo inet_ntop($first + $i) . "\n"; //doesn't work, packed binary? }

here's illustration programme written in c (since don't know c++). it's fast, i'm not happy it. maybe can help me improve it.

edit: obviously, wrote before turned php-only question. turning php left exercise reader (ew).

class="lang-c prettyprint-override">#include <arpa/inet.h> #include <string.h> #include <stdlib.h> #include <errno.h> #include <stdio.h> #include <ctype.h> /* * syntax: ./ipv6_list <ip>/<cidr-prefix> */ int main(int argc, char **argv) { uint8_t start[16]; uint8_t address[16]; uint8_t mask[16] = { 0 }; uint8_t prefix = 128; char *prefix_location; int i; /* octet that, when changed, result in <ip> & <mask> != <start ip> */ int mask_check_octet = 0; if(argc != 2) homecoming 1; /* find prefix */ if((prefix_location = strstr(argv[1], "/")) != null) { char *prefix_search = prefix_location + 1; char *prefix_remaining; long prefix_test; if(!isdigit(*prefix_search)) homecoming 2; errno = 0; prefix_test = strtol(prefix_search, &prefix_remaining, 10); if(errno == erange || prefix_test < 0 || prefix_test > 128 || strcmp(prefix_remaining, "") != 0) homecoming 2; prefix = (uint8_t)prefix_test; *prefix_location = '\0'; /* can pass argv[1] inet_pton(3) */ } /* convert prefix mask */ for(i = 0; < 16; i++) { if(prefix == 0) break; mask_check_octet = i; if(prefix < 8) { mask[i] = ~((1 << (8 - prefix)) - 1); break; } else mask[i] = uint8_max; prefix -= 8; } /* find address */ if(inet_pton(af_inet6, argv[1], start) != 1) homecoming 3; /* start @ origin of network */ for(i = 0; < 16; i++) { start[i] &= mask[i]; address[i] = start[i]; } /* iterate */ while((address[mask_check_octet] & mask[mask_check_octet]) == start[mask_check_octet]) { char address_str[inet6_addrstrlen]; inet_ntop(af_inet6, address, address_str, sizeof(address_str)); printf("%s\n", address_str); /* add together 1 address */ for(i = 15; >= 0; i--) { if(address[i] != uint8_max) break; } address[i]++; for(i++; < 16; i++) address[i] = 0; }; homecoming 0; }

you can utilize standard shell commands limit output (or modify while loop):

class="lang-none prettyprint-override">nfontes@brioche:~$ ./ipv6_list '2607:fc50:0:d00::0/106' | head -n 200 2607:fc50:0:d00:: 2607:fc50:0:d00::1 2607:fc50:0:d00::2 2607:fc50:0:d00::3 2607:fc50:0:d00::4 2607:fc50:0:d00::5 2607:fc50:0:d00::6 2607:fc50:0:d00::7 2607:fc50:0:d00::8 2607:fc50:0:d00::9 2607:fc50:0:d00::a 2607:fc50:0:d00::b 2607:fc50:0:d00::c 2607:fc50:0:d00::d 2607:fc50:0:d00::e [...] 2607:fc50:0:d00::c0 2607:fc50:0:d00::c1 2607:fc50:0:d00::c2 2607:fc50:0:d00::c3 2607:fc50:0:d00::c4 2607:fc50:0:d00::c5 2607:fc50:0:d00::c6 2607:fc50:0:d00::c7

php ipv6

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -