interface_status_t detect_beat_auto(int fd, char *iface) {
    interface_status_t status;

    if (cached_detect_beat_func && (status = cached_detect_beat_func(fd, iface)) != IFSTATUS_ERR)
        return status;
    
    if ((status = interface_detect_beat_ethtool(fd, iface)) != IFSTATUS_ERR) {
        cached_detect_beat_func = interface_detect_beat_ethtool;
        daemon_log(LOG_INFO, "Using detection mode: SIOCETHTOOL");
        return status;
    }
    
    if ((status = interface_detect_beat_mii(fd, iface)) != IFSTATUS_ERR) {
        cached_detect_beat_func = interface_detect_beat_mii;
        daemon_log(LOG_INFO, "Using detection mode: SIOCGMIIPHY");
        return status;
    }

    if ((status = interface_detect_beat_wlan(fd, iface)) != IFSTATUS_ERR) {
        cached_detect_beat_func = interface_detect_beat_wlan;
        daemon_log(LOG_INFO, "Using detection mode: wireless extension");
        return status;
    }

    if ((status = interface_detect_beat_iff(fd, iface)) != IFSTATUS_ERR) {
        cached_detect_beat_func = interface_detect_beat_iff;
        daemon_log(LOG_INFO, "Using detection mode: IFF_RUNNING");
        return status;
    }

    return IFSTATUS_ERR;
}
Beispiel #2
0
int report_link_state(const char* iface) {
    int fd, r = 0;
    interface_status_t s;

    if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
	printf("Socket create failed\n");
	return -1;
    }

    /* here just to make it more convenient for testing:if we define DEBUG,so a plugin-out and plugin-in event
     * will be detected if we do "ifconfig interface-name down" and ifconfig interface-name up".Otherwise,
     * if we do not define DEBUG, a plugin-in and plugin-out event can only be detected when the local link
     * plugin in and out. Skip this comment if you did not catch it.
     */

    /* #define DEBUG*/
    #if 0
    if ((s = interface_detect_beat_ethtool(fd, iface)) == IFSTATUS_ERR) {
	printf("    SIOCETHTOOL failed (%s)\n", strerror(errno));
	close(fd);
	return -1;
    }
    #else
    if ((s = interface_detect_beat_iff(fd,iface)) == IFSTATUS_ERR) {
	printf("   IFF_RUNNING failed (%s)\n", strerror(errno));
	close(fd);
	return -1;
    }
    #endif

    switch(s) {
	case IFSTATUS_UP:
	    /* link beat detected */
	    r = 1;
	    break;

	case IFSTATUS_DOWN:
	    /* unplugged */
	    r = 2;
	    break;

	default:
	    /* not supported (Retry as root?)*/
	    r = -1;
	    break;
    }

   close(fd);
   return r;
}
int handle(char *iface) {
    int fd, r = 0;
    interface_status_t s;
    
    if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
        return -1;
    
    if (verbose > 0) {
        printf("%s:\n", iface);
        
	if ((s = interface_detect_beat_ethtool(fd, iface)) == IFSTATUS_ERR)
            printf("    SIOCETHTOOL failed (%s)\n", strerror(errno));
        else
            printf("    SIOCETHTOOL: %s\n", s == IFSTATUS_UP ? "link beat detected" : "unplugged");
        
        if ((s = interface_detect_beat_mii(fd, iface)) == IFSTATUS_ERR)
            printf("    SIOCGMIIPHY failed (%s)\n", strerror(errno));
        else
            printf("    SIOCGMIIPHY: %s\n", s == IFSTATUS_UP ? "link beat detected" : "unplugged");

        if ((s = interface_detect_beat_wlan(fd, iface)) == IFSTATUS_ERR)
            printf("    Wireless failed.\n");
        else
            printf("    Wireless: %s\n", s == IFSTATUS_UP ? "link beat detected" : "unplugged");

        if ((s = interface_detect_beat_iff(fd, iface)) == IFSTATUS_ERR)
            printf("    IFF_RUNNING failed.\n");
        else
            printf("    IFF_RUNNING: %s\n", s == IFSTATUS_UP ? "link beat detected" : "unplugged");
        
        if ((s = interface_detect_beat_priv(fd, iface)) == IFSTATUS_ERR)
            printf("    SIOCDEVPRIVATE failed (%s)\n", strerror(errno));
        else
            printf("    SIOCDEVPRIVATE: %s\n", s == IFSTATUS_UP ? "link beat detected" : "unplugged");

    } else {
        
        if ((s = interface_detect_beat_ethtool(fd, iface)) == IFSTATUS_ERR)
            if ((s = interface_detect_beat_mii(fd, iface)) == IFSTATUS_ERR)
                if ((s = interface_detect_beat_wlan(fd, iface)) == IFSTATUS_ERR)
                    s = interface_detect_beat_iff(fd, iface);

        switch(s) {
            case IFSTATUS_UP:
                if (!verbose)
                    printf("%s: link beat detected\n", iface);
                r = 1;
                break;
                
            case IFSTATUS_DOWN:
                if (!verbose)
                    printf("%s: unplugged\n", iface);
                r = 2;
                break;

            default:
                if (!verbose)
                    printf("%s: not supported%s\n", iface, getuid() != 0 ? " (Retry as root?)" : "");
                
                r = -1;
                break;
        }
    }
    
            
    close(fd);
    return r;
}
//检测网卡的连接状态,rLinkStatus = 1,连接正常,rLinkStatus = 0,连接不正常 
int Test_NetCard::Check_EthLinkStatus(const char* eth_name , int& rLinkStatus )
{
	FILE *fp = NULL;
	interface_status_t status;
	char buf[512] = {'\0'};
	char hw_name[10] = {'\0'};
	//char *token = NULL;

	if ( eth_name == NULL )
	{
		return -1;
	}
	
	strcpy( hw_name , eth_name );

	//方法一:查看一个文件,相对来说比较简单
#if 0
	char carrier_path[512] = {'\0'};

	memset(buf, 0, sizeof(buf)); 
	snprintf(carrier_path, sizeof(carrier_path), "/sys/class/net/%s/carrier", hw_name);
	if ((fp = fopen(carrier_path, "r")) != NULL)
	{
		while (fgets(buf, sizeof(buf), fp) != NULL)
		{
			if (buf[0] == '0')
			{
				status = IFSTATUS_DOWN;
			}
			else
			{
				status = IFSTATUS_UP;
			}
		}
	}
	else
	{
		perror("Open carrier ");
		return -1;
	}
	fclose(fp);
#endif

	//方法二:用函数吧!有点复杂,但是也是一种有效的办法
#if 1
	int fd = -1;

	if((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
	{
		perror("socket ");
		return -70;
		//exit(0);
	}
	status = interface_detect_beat_ethtool(fd, hw_name);
	close(fd);
#endif

	switch ( status )
	{
	case IFSTATUS_UP:
		rLinkStatus = 1;
		//printf("%s : link up\n", hw_name);
		break;

	case IFSTATUS_DOWN:
		rLinkStatus = 0;
		//printf("%s : link down\n", hw_name);
		break;

	default:
		rLinkStatus = 0;
		//printf("Detect Error\n");
		
		return -1;
		break;
	}

	return 0;
}