#include
#include
#include
#include
#include
#include
char* longToIp(char* buf, int IPAddr){//ip2long
sprintf(buf, "%d.%d.%d.%d", (0X000000FF & (IPAddr >> 24)),
(0X000000FF & (IPAddr >> 16)),
(0X000000FF & (IPAddr >> 8)),
(0X000000FF & IPAddr ));
return buf;
}
int h_fillbitsfromleft(unsigned int num) {
if (num >= 8) {
return (255);
}
int bitpat = 0xff00;
while (num > 0) {
bitpat = bitpat >> 1;
num--;
}
return (bitpat & 0xff);
}
int fourNum2Ip(unsigned int ip1, unsigned int ip2, unsigned int ip3,
unsigned int ip4) {
return (ip1 << 24) + (ip2 << 16) + (ip3 << 8) + ip4;
}
int calNBFL(unsigned int ip1, unsigned int ip2, unsigned int ip3, unsigned int ip4, unsigned int mask, __be32 *ips, __be32 *ipe) {
unsigned int mask1, mask2, mask3, mask4, mask_o;
unsigned int ips_1, ips_2, ips_3, ips_4;
unsigned int ipe_1, ipe_2, ipe_3, ipe_4;
if (ip1 > 255 || ip2 > 255 || ip3 > 255 || ip4 > 255)
return 0;
if (mask > 32)
return 0;
mask1 = 0;
mask2 = 0;
mask3 = 0;
mask4 = 0;
mask_o = mask;
if (mask >= 8) {
mask1 = 255;
mask -= 8;
if (mask >= 8) {
mask2 = 255;
mask -= 8;
if (mask >= 8) {
mask3 = 255;
mask -= 8;
mask4 = h_fillbitsfromleft(mask);
} else {
mask3 = h_fillbitsfromleft(mask);
}
} else {
mask2 = h_fillbitsfromleft(mask);
}
} else {
mask1 = h_fillbitsfromleft(mask);
}
//prt(__LINE__, mask1, mask2, mask3, mask4);
mask = mask_o;
if (mask == 31) {
ips_1 = ip1 & mask1;
ips_2 = ip2 & mask2;
ips_3 = ip3 & mask3;
ips_4 = (ip4 & mask4) - 1;
ipe_1 = ip1 | (~mask1 & 0xff);
ipe_2 = ip2 | (~mask2 & 0xff);
ipe_3 = ip3 | (~mask3 & 0xff);
ipe_4 = (ip4 | (~mask4 & 0xff))+1;
*ips = fourNum2Ip(ips_1,ips_2,ips_3,ips_4);
*ipe = fourNum2Ip(ipe_1,ipe_2,ipe_3,ipe_4);
return 1;
}
if (mask == 32) {
ipe_1 = ips_1 = ip1;
ipe_2 = ips_2 = ip2;
ipe_3 = ips_3 = ip3;
ips_4 = ip4-1;
ipe_4 = ip4+1;
*ips = fourNum2Ip(ips_1,ips_2,ips_3,ips_4);
*ipe = fourNum2Ip(ipe_1,ipe_2,ipe_3,ipe_4);
return 1;
}
//cform.numofaddr.value = Math.pow(2, 32 - mask) - 2;
//prt(__LINE__, mask1, mask2, mask3, mask4);
ips_1 = ip1 & mask1;
ips_2 = ip2 & mask2;
ips_3 = ip3 & mask3;
ips_4 = ip4 & mask4;
ipe_1 = ip1 | (~mask1 & 0xff);
ipe_2 = ip2 | (~mask2 & 0xff);
ipe_3 = ip3 | (~mask3 & 0xff);
ipe_4 = ip4 | (~mask4 & 0xff);
*ips = fourNum2Ip(ips_1,ips_2,ips_3,ips_4);
*ipe = fourNum2Ip(ipe_1,ipe_2,ipe_3,ipe_4);
return 1;
}
int calcNWmask(int tmpvar, int* out1, int* out2, int* out3, int* out4)
{
*out1=0;
*out2=0;
*out3=0;
*out4=0;
if (tmpvar > 32 || tmpvar < 0){
return(1);
}
if (tmpvar >= 8){
*out1 = 255;
tmpvar-=8;
}else{
*out1 = h_fillbitsfromleft(tmpvar);
return(0);
}
if (tmpvar >= 8){
*out2 = 255;
tmpvar-=8;
}else{
*out2 = h_fillbitsfromleft(tmpvar);
return(0);
}
if (tmpvar >= 8){
*out3 = 255;
tmpvar-=8;
}else{
*out3 = h_fillbitsfromleft(tmpvar);
return(0);
}
*out4 = h_fillbitsfromleft(tmpvar);
return(0);
}
int calNBFL1(unsigned int ip1, unsigned int ip2, unsigned int ip3, unsigned int ip4, unsigned int mask, __be32 *ips, __be32 *ipe) {
unsigned int mask1, mask2, mask3, mask4, mask_o;
unsigned int ips_1, ips_2, ips_3, ips_4;
unsigned int ipe_1, ipe_2, ipe_3, ipe_4;
mask_o = mask;
int rt=calcNWmask(mask, &mask1, &mask2, &mask3, &mask4);
if (rt !=0 ){
// error
return 0;
}
if (mask == 31){
//cform.numofaddr.value = "two hosts";
ips_1 = ip1 & mask1;
ips_2 = ip2 & mask2;
ips_3 = ip3 & mask3;
ips_4 = ip4 & mask4;
//
ipe_1 = ip1 | (~ mask1 & 0xff);
ipe_2 = ip2 | (~ mask2 & 0xff);
ipe_3 = ip3 | (~ mask3 & 0xff);
ipe_4 = ip4 | (~ mask4 & 0xff);
*ips = fourNum2Ip(ips_1,ips_2,ips_3,ips_4);
*ipe = fourNum2Ip(ipe_1,ipe_2,ipe_3,ipe_4);
return 1;
}
if (mask == 32){
//cform.numofaddr.value = "one host";
ipe_1 = ips_1 = ip1;
ipe_2 = ips_2 = ip2;
ipe_3 = ips_3 = ip3;
ipe_4 = ips_4 = ip4;
*ips = fourNum2Ip(ips_1,ips_2,ips_3,ips_4);
*ipe = fourNum2Ip(ipe_1,ipe_2,ipe_3,ipe_4);
return 1;
}
//cform.numofaddr.value = Math.pow(2,32 - tmpvar) - 2;
//
ips_1 = ip1 & mask1;
ips_2 = ip2 & mask2;
ips_3 = ip3 & mask3;
ips_4 = ip4 & mask4;
//
ipe_1 = ip1 | (~ mask1 & 0xff);
ipe_2 = ip2 | (~ mask2 & 0xff);
ipe_3 = ip3 | (~ mask3 & 0xff);
ipe_4 = ip4 | (~ mask4 & 0xff);
*ips = fourNum2Ip(ips_1,ips_2,ips_3,ips_4);
*ipe = fourNum2Ip(ipe_1,ipe_2,ipe_3,ipe_4);
return 1;
}
void main(int argc, char* argv[]){
if(argc<2){
printf("error, usage %s ip/mask\n", argv[0]);
exit(0);
}
printf("> Input %s\n", argv[1]);
unsigned int ip1=0, ip2=0, ip3=0, ip4 = 0, cover=0;
sscanf(argv[1],"%u.%u.%u.%u/%2d", &ip1, &ip2, &ip3, &ip4, &cover);
__be32 ips=0,ipe=0;
char ipa[16],ipb[16];
calNBFL1(ip1, ip2, ip3, ip4, cover, &ips, &ipe);
longToIp(ipa, ips);
longToIp(ipb, ipe);
printf("%s-%s\n", ipa, ipb);
}
拿 https://tool.520101.com/wangluo/ipjisuan/ 的js改的