内网ip

#include <stdio.h>

#include <stdint.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>/*
    "10.*"              => 0xA
    "172.16.*~172.31.*" => 0xAC1
    "192.168.*"         => 0xC0A8
*/
bool is_inner_ip(const char *ip)
{
    struct in_addr addr;
    if (inet_pton(AF_INET, ip, &addr) <= 0)
    {
        fprintf(stderr, "invalid ip:%s\n", ip);
        return false;
    }
    uint32_t hostip = ntohl(addr.s_addr);
    fprintf(stderr, "%s %08X\n", ip, hostip);
    return (hostip >> 24 == 0xA || hostip >> 20 == 0xAC1 || hostip >> 16 == 0xC0A8);
}
 
int main(int argc, char **argv)
{
    if(argc < 2)
    {
        fprintf(stderr, "is_inner_ip IP\n");
        return 0;
    }
    bool ret = is_inner_ip(argv[1]);
    printf("%s %d\n", argv[1], ret);
    return 0;
}

 

posted @ 2015-08-02 22:01  寒霜剑气  阅读(61)  评论(0编辑  收藏  举报