C函数确定IP地址是否为多播地址 [英] C function to determine if IP address is multicast address

查看:49
本文介绍了C函数确定IP地址是否为多播地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户输入的IP地址为"239.4.4.4",那么如何在Linux C中使用任何功能确定该IP地址是否是多播的?

If user enters some IP address like "239.4.4.4", how can I determine this IP address is multicast using any function if available in linux C?

推荐答案

/**********************************************************************************************************************
 * 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;
}

这篇关于C函数确定IP地址是否为多播地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆