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.
#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; /*************************************************************************** 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: 成功 ****************************************************************************/ 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; } }
原文地址:http://blog.csdn.net/usstmes318/article/details/37562063