ipv6地址累加函数

 

#include <stdio.h>
#include <arpa/inet.h>

int main()
{
    int i;
    int ret;
    struct in6_addr addr6;
    unsigned long ul1, ul2, ul3, ul0;
    char str[INET6_ADDRSTRLEN] = {0};

    ret = inet_pton(AF_INET6, "ff02::12", &addr6);
    if (ret != 1) {
        perror("inet_pton()");
        return -1;
    }

    for (i = 0; i < 1000000; i++) {
        ul3 = addr6.s6_addr32[3];
        ul2 = addr6.s6_addr32[2];
        ul1 = addr6.s6_addr32[1];
        ul0 = addr6.s6_addr32[0];
        
        ul3 = ntohl(ul3)+1;
        if (ul3) {
            goto u3;
        } 
        ul2 = ntohl(ul2)+1;
        if (ul2) {
            goto u2;
        } 
        ul1 = ntohl(ul1)+1;
        if (ul1) {
            goto u1;
        } 
        ul0 = ntohl(ul0)+1;

        addr6.s6_addr32[0] = htonl(ul0);
u1:
        addr6.s6_addr32[1] = htonl(ul1);
u2:
        addr6.s6_addr32[2] = htonl(ul2);
u3:
        addr6.s6_addr32[3] = htonl(ul3);
    
        if ((inet_ntop(AF_INET6, &addr6, str, INET6_ADDRSTRLEN)) != NULL) {
            printf("%s\n", str);
        } else {
            fprintf(stderr, "inet_ntop() error\n");
            return -1;
        }
    }


    return 0;
}

 

posted on 2019-06-05 14:33  rivsidn  阅读(278)  评论(0编辑  收藏  举报

导航