C 检查网络连通性

/* check interface connected to internet or not
*  if connected return 1, else return 0;
*/
/********************************************************************************************
@fn device_check_if_conn

@brief check the interface connected to internet or not

@author taoxp

@param interface name

@return 0 or 1, 1:interface connect to internet success 0:interface connect to internet fail

********************************************************************************************/
static uint8_t device_get_net_mode(){
        pid_t status;
        uint8_t ret=0, i=0;
        char cmd[256];
        char serverip[3][32]={"114.114.114.114","8.8.8.8","223.5.5.5"};
        for(i=0;i<3;i++){
	        sprintf(cmd,"ping -I %s -w 1 -c 1 %s 1>/dev/null 2>&1", ifname, serverip[i]);
	        status=system(cmd);
	        if(status==-1){
	                GLOG_ERROR("run system command error");
	        }else{
	                if(WIFEXITED(status)){
	                        if(0==WEXITSTATUS(status)){
	                                GLOG_DEBUG("%s connect to internet ok", ifname);
	                                ret=1;
	                                break;
	                        }else{
	                                //GLOG_DEBUG("%s connect to internet fail", ifname);
	                        }
	                }else{
	                        GLOG_ERROR("exit Code status %d", WEXITSTATUS(status));
	                }
	        }
        }
        return ret;
#endif
}

/***************************************************************************************************
* @fn	   device_get_net_mode
*
* @brief   get interface connected to internet 
*
* @author  taoxp
*
* @param   none
*
* @return  0/1/3/7 0:eth, 1:wifi, 3:4g
****************************************************************************************************/
uint8_t device_get_net_mode(){
    uint8_t netmode=ETH_MODE, status=0;
    status=device_check_if_conn(GW_DEFAULT_ETH_NAME);
    if(status==1){
		netmode=ETH_MODE;
    }
    else{
    	status=device_check_if_conn(GW_DEFAULT_WIFI_NAME);
        if(status==1){
			netmode=WIFI_MODE;
        }else{
            status=device_check_if_conn(GW_DEFAULT_PPP_NAME);
            if(status==1){
				netmode=PPP_MODE;
            }
        }
    }
	return netmode;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值