标签:article ica null csdn pre reserve 组播 ++ 研究
An IP address is 32-bit long. IP addresses are classified into A, B, C, D, and E classes, as shown in the figure below.
Please determine the class of an input IP address and obtain the corresponding IP address information according to the input 32-bit IP address.?
For IP addresses of A, B, and C classes, the network number and host number are required. For class D addresses, the multicast address is required. Class E addresses are reserved for future use and do not require the network number, host number, or multicast
address.?
1.A类地址:1.0.0.0~127.255.255.255
当中127开头的地址被保留,用作測试仅仅用。10.0.0.0~10.255.255.255被保留为私有地址使用。
这样A类地址就仅仅有1.0.0.0~9.255.255.255和11.0.0.0~126.255.255.255能够正经常使用了。
2.B类地址:128.1.0.0~191.255.255.255
当中172.16.0.0和172.31.255.255保留为私有网络的使用,互联网上不使用。
3.C类地址:192.0.1.0~223.255.255.255
当中192.168.0.0和192.168.255.255保留为私有网络使用,互联网上不适用。
4.D类地址:224.0.0.0~ 239. 255. 255.255
D类地址用于在IP网络中的组播( multicasting。又称为多目广播)。
5.E类地址:240.0.0.0~255.255.255.255
E类地址保留作研究之用。因此Internet上没有可用的E类地址。
/***************************************************************************
Description
IP地址长32bit,可分为A/B/C/D/E 5类地址。不同五类地址格式定义见题目描写叙述中的附图。
请依据输入的32bit IP地址,推断IP地址的类别,并获得相应的IP地址信息。
当中A/B/C类地址。须要获得网络号和主机号。D类地址须要获得多播组号。E类地址不涉及。
Prototype
int GetIpAddressInfo(unsigned int unIpAddr, IpAddressInfoStru* pAddrInfo)
Input Param
unsigned int unIpAddr: Ip 地址
Output Param
IpAddressInfoStru* pAddrInfo: IP地址信息的结果。IpAddressInfoStru參加定义
当中:nType 为地址类别。请按枚举IP_ADDR_TYPE返回。
stCompose 为网络号和主机号的结构定义。在A/B/C类地址的时候须要返回。
nMultiBoardNo 为多播组号。在D类地址的时候须要返回。
Return Value
int
-1: 失败
0: 成功
****************************************************************************/
#include <stdio.h>
#include <bitset>
using std::bitset;
/*IP地址类别*/
typedef enum
{
IP_ADDR_TYPE_A, /* A类IP地址*/
IP_ADDR_TYPE_B, /* B类IP地址*/
IP_ADDR_TYPE_C, /* C类IP地址*/
IP_ADDR_TYPE_D, /* D类IP地址*/
IP_ADDR_TYPE_E /* E类IP地址*/
}IP_ADDR_TYPE;
typedef struct
{
int nNetworkNo; /* 网络号*/
int nHostNo; /* 主机号*/
}IpComposeStru;
typedef struct
{
int nType; /*IP地址类型*/
union
{
IpComposeStru stCompose; /*IP地址子信息*/
int nMultiBoardNo; /*多播组号*/
}IpSubsetInfo;
}IpAddressInfoStru;
// 方法1
int GetIpAddressInfo(unsigned int unIpAddr, IpAddressInfoStru* pAddrInfo)
{
/*在这里实现功能*/
if (pAddrInfo == NULL)
{
return -1;
}
bitset<32> bitIpAddr = 0;
bitIpAddr = unIpAddr;
if (!bitIpAddr.test(31))
{
pAddrInfo->nType = IP_ADDR_TYPE_A;
bitset<7> bitNetworkNo;
for (int i = 0; i < 7; i++)
{
bitNetworkNo[i] = bitIpAddr[24 + i];
}
bitset<24> bitHostNo;
for (int i = 0; i < 24; i++)
{
bitHostNo[i] = bitIpAddr[i];
}
pAddrInfo->IpSubsetInfo.stCompose.nNetworkNo = bitNetworkNo.to_ulong();
pAddrInfo->IpSubsetInfo.stCompose.nHostNo = bitHostNo.to_ulong();
return 0;
}
else if (!bitIpAddr.test(30))
{
pAddrInfo->nType = IP_ADDR_TYPE_B;
bitset<14> bitNetworkNo;
for (int i = 0; i < 14; i++)
{
bitNetworkNo[i] = bitIpAddr[16 + i];
}
bitset<16> bitHostNo;
for (int i = 0; i < 16; i++)
{
bitHostNo[i] = bitIpAddr[i];
}
pAddrInfo->IpSubsetInfo.stCompose.nNetworkNo = bitNetworkNo.to_ulong();
pAddrInfo->IpSubsetInfo.stCompose.nHostNo = bitHostNo.to_ulong();
return 0;
}
else if (!bitIpAddr.test(29))
{
pAddrInfo->nType = IP_ADDR_TYPE_C;
bitset<21> bitNetworkNo;
for (int i = 0; i < 21; i++)
{
bitNetworkNo[i] = bitIpAddr[8 + i];
}
bitset<8> bitHostNo;
for (int i = 0; i < 8; i++)
{
bitHostNo[i] = bitIpAddr[i];
}
pAddrInfo->IpSubsetInfo.stCompose.nNetworkNo = bitNetworkNo.to_ulong();
pAddrInfo->IpSubsetInfo.stCompose.nHostNo = bitHostNo.to_ulong();
return 0;
}
else if (!bitIpAddr.test(28))
{
pAddrInfo->nType = IP_ADDR_TYPE_D;
bitset<28> bitMultiBoardNo;
for (int i = 0; i < 28; i++)
{
bitMultiBoardNo[i] = bitIpAddr[i];
}
pAddrInfo->IpSubsetInfo.nMultiBoardNo = bitMultiBoardNo.to_ulong();
return 0;
}
else if (!bitIpAddr.test(27))
{
pAddrInfo->nType = IP_ADDR_TYPE_E;
return 0;
}
}
#include <stdio.h>
/*IP地址类别*/
typedef enum
{
IP_ADDR_TYPE_A, /* A类IP地址*/
IP_ADDR_TYPE_B, /* B类IP地址*/
IP_ADDR_TYPE_C, /* C类IP地址*/
IP_ADDR_TYPE_D, /* D类IP地址*/
IP_ADDR_TYPE_E /* E类IP地址*/
}IP_ADDR_TYPE;
typedef struct
{
int nNetworkNo; /* 网络号*/
int nHostNo; /* 主机号*/
}IpComposeStru;
typedef struct
{
int nType; /*IP地址类型*/
union
{
IpComposeStru stCompose; /*IP地址子信息*/
int nMultiBoardNo; /*多播组号*/
}IpSubsetInfo;
}IpAddressInfoStru;
//方法2
int GetIpAddressInfo(unsigned int unIpAddr, IpAddressInfoStru* pAddrInfo)
{
/*在这里实现功能*/
if (pAddrInfo == NULL)
{
return -1;
}
if (!((unIpAddr)&(0x80000000)))
{
pAddrInfo->nType = IP_ADDR_TYPE_A;
unsigned int bitNetworkNo;
unsigned int bitHostNo;
unsigned int bittemp = unIpAddr;
bitHostNo = bittemp&(0x00FFFFFF);
bitNetworkNo = bittemp>>24;
pAddrInfo->IpSubsetInfo.stCompose.nNetworkNo = bitNetworkNo;
pAddrInfo->IpSubsetInfo.stCompose.nHostNo = bitHostNo;
return 0;
}
else if (!((unIpAddr)&(0x40000000)))
{
pAddrInfo->nType = IP_ADDR_TYPE_B;
unsigned int bitNetworkNo;
unsigned int bitHostNo;
unsigned int bittemp = unIpAddr;
bitHostNo = bittemp&(0x0000FFFF);
bitNetworkNo = ((bittemp>>16)&(0x00003FFF));
pAddrInfo->IpSubsetInfo.stCompose.nNetworkNo = bitNetworkNo;
pAddrInfo->IpSubsetInfo.stCompose.nHostNo = bitHostNo;
return 0;
}
else if (!((unIpAddr)&(0x20000000)))
{
pAddrInfo->nType = IP_ADDR_TYPE_C;
unsigned int bitNetworkNo;
unsigned int bitHostNo;
unsigned int bittemp = unIpAddr;
bitHostNo = bittemp&(0x000000FF);
bitNetworkNo = ((bittemp>>8)&(0x001FFFFF));
pAddrInfo->IpSubsetInfo.stCompose.nNetworkNo = bitNetworkNo;
pAddrInfo->IpSubsetInfo.stCompose.nHostNo = bitHostNo;
return 0;
}
else if (!((unIpAddr)&(0x10000000)))
{
pAddrInfo->nType = IP_ADDR_TYPE_D;
unsigned int bitMultiBoardNo;
unsigned int bittemp = unIpAddr;
bitMultiBoardNo = bittemp&(0x0FFFFFFF);
pAddrInfo->IpSubsetInfo.nMultiBoardNo = bitMultiBoardNo;
return 0;
}
else if (!((unIpAddr)&(0x08000000)))
{
pAddrInfo->nType = IP_ADDR_TYPE_E;
return 0;
}
return -1;
}
标签:article ica null csdn pre reserve 组播 ++ 研究
原文地址:https://www.cnblogs.com/xfgnongmin/p/10660790.html