include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <linux/types.h> 
#include <sys/socket.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>


#define IP_STR "10.0.41.0" 


typedef     unsigned char           u8; 


typedef struct {
    u8      version;            // 4 for IPv4, 6 for IPv6
    u8      address[16];        // pad zero for IPv4
} ipAddress_t;


void dec_to_ip(int32_t snmp_ip, char *ipv4)
{
    int ip3;
    int ip2;
    int ip1;
    int ip0;
    ip3 = (snmp_ip >> 24) & 255;
    ip2 = (snmp_ip >> 16) & 255;
    ip1 = (snmp_ip >>  8) & 255;
    ip0 = (snmp_ip      ) & 255;


    sprintf(ipv4,"%d.%d.%d.%d", ip0, ip1, ip2, ip3);

}


int main(char *args, int argc){
    ipAddress_t    r_ip;
    ipAddress_t    router_ip;
    char ipv4_str[32];


    struct in_addr    router_addr;
    memset(&r_ip, 0x00, sizeof(ipAddress_t));
    memset(&router_ip, 0x00, sizeof(ipAddress_t));
    r_ip.version                     = 4;
    inet_pton(AF_INET, IP_STR, r_ip.address);
    memcpy(&router_addr, r_ip.address, 4);
    /* dec format*/
     printf("ip %d\n",router_addr.s_addr);

    /* string format*/
    dec_to_ip(router_addr.s_addr,ipv4_str);
    printf("ip %s\n",ipv4_str);

    return 0;
}

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 = = 的頭像
    = =

    逗點大的雨滴

    = = 發表在 痞客邦 留言(0) 人氣()