vcpe/srcs/libs/ipaddr/ipaddr.c

37 lines
1.3 KiB
C

//
// Created by xajhuang on 2023/3/21.
//
#include <string.h>
#include "ipaddr.h"
#define BIT_TEST(c, i) ((c[(i) / 8] & (1 << ((i) % 8))) ? 1 : 0)
U32 ipv4_get_network_addr(U32 ipAddr, U32 netmask) {
return (ipAddr & netmask);
}
U32 ipv4_get_boardcast_addr(U32 ipAddr, U32 netmask) {
return (ipAddr | netmask);
}
U32 ipv4_network_total_addr(U32 netmask) {
return (~netmask) + 1;
}
U32 ipv4_mask_to_cidr(const char *netmask) {
const char *cidr[] = {"128.0.0.0", "192.0.0.0", "224.0.0.0", "240.0.0.0", "248.0.0.0",
"252.0.0.0", "254.0.0.0", "255.0.0.0", "255.128.0.0", "255.192.0.0",
"255.224.0.0", "255.240.0.0", "255.248.0.0", "255.252.0.0", "255.254.0.0",
"255.255.0.0", "255.255.128.0", "255.255.192.0", "255.255.224.0", "255.255.240.0",
"255.255.248.0", "255.255.252.0", "255.255.254.0", "255.255.255.0", "255.255.255.128",
"255.255.255.192", "255.255.255.224", "255.255.255.240", "255.255.255.248", "255.255.255.252",
"255.255.255.254", "255.255.255.255"};
for(int i = 0; i < ARRAY_SIZE(cidr); i++) {
if(strcmp(cidr[i], netmask) == 0) {
return i + 1;
}
}
return 0xFF;
}