如果用户输入诸如"239.4.4.4“这样的IP地址,如何使用linux C中可用的任何功能来确定这个IP地址是多播的?
发布于 2015-11-25 15:14:54
/**********************************************************************************************************************
* Checks if specified IP is multicast IP. Multicast IP ranges from 224.0.0.0 to 239.255.255.255.
*
* Returns 0 if specified IP is not multicast IP, else non-zero.
*
* Parameters:
* ip IP to check for multicast IP, stored in network byte-order.
*********************************************************************************************************************/
int net_ip__is_multicast_ip(in_addr_t ip){
char *ip_str = (char *) &ip;
int i = ip_str[0] & 0xFF;
// we will check only first byte of IP
// and if it from 224 to 239, then it can
// represent multicast IP.
if(i >= 224 && i <= 239){
return 1;
}
return 0;
}发布于 2014-02-25 13:19:09
IPv4多播地址是由最重要的比特1110定义的,因此:
>> 28,然后检查结果是否为14 (1110),如isMulti = ((address >> 28) == 14);。发布于 2014-02-25 13:18:52
多播地址范围从224.0.0.0到239.255.255.255.地址
因此,只要检查地址是否在范围内就足够了。
https://stackoverflow.com/questions/22015308
复制相似问题