ipとint間の変換関数

1119 ワード

/*
 * purpose : transfer ip to u_int32_t
 * @Param IPdotdec : ip
 * return u_int_32 : the result u_int32_t 
 */
u_int32_t ip2int(char IPdotdec[20]){
        struct in_addr s; // IP to int
        inet_pton(AF_INET, IPdotdec, (void *)&s);
        return s.s_addr;
}

/*
 * purpose : transfer u_int32_t to ip
 * @Param u_int32_t : ip
 * return char * : IPdotdec
 */
void int2ip(u_int32_t ip , char IPdotdec[20]){
        struct in_addr s; // int to IP
        s.s_addr = ip;
        // int2ip
        inet_ntop(AF_INET, (void *)&s, IPdotdec, 16);
}