通過ping 命令原始碼獲取網路狀態

2021-10-02 22:16:18 字數 2016 閱讀 5072

通過裁剪ping命令的原始碼,實現獲取網路狀態;ping通,則表示網路狀態ok,ping不通,則表示網路狀態error:

/*

使用ping 命令原始碼判斷網路狀態是否ok

*/#include #include #include #include #include #include #include #include typedef struct pingm_pakcetpingm_pakcet;

static pingm_pakcet pingpacket[128];

static struct sockaddr_in dest;

static int32_t rawsock = 0;

#define huip_address "192.168.2.99"

static unsigned short network_icmp_cksum(unsigned char *data, int32_t len)

if(odd)

sum = (sum >> 16) + (sum & 0xffff);

sum += (sum >> 16);

return ~sum;

}static void network_icmp_pack(struct icmp *icmph, int32_t seq, struct timeval *tv, int32_t length)

static int32_t network_icmp_send()

} if(packet)

network_icmp_pack((struct icmp *)send_buff,packet_send,&tv, 64);

size = sendto(rawsock, send_buff,64,0,(struct sockaddr *)&dest, sizeof(dest));

if(0 <=size)

else

usleep(1000 * 200);

return packet_send;

}static int32_t network_icmp_recv()

logdbg("call recv()failed\n");}}

break;

} return status;

}

int main()

memset(dest_str, 0x0, sizeof(dest_str));

memcpy(dest_str, huip_address,strlen(huip_address)+1);

memset(pingpacket, 0, sizeof(pingm_pakcet) * 128);

rawsock = socket(af_inet, sock_raw, protocol->p_proto);

if(rawsock < 0)

setsockopt(rawsock, sol_socket, so_rcvbuf, &size, sizeof(size));

bzero(&dest, sizeof(dest));

dest.sin_family = af_inet;

inaddr = inet_addr(huip_address);

memcpy((char *)&dest.sin_addr, &inaddr,sizeof(inaddr));

inaddr = dest.sin_addr.s_addr;

logdbg("ping %s (%ld.%ld.%ld.%ld) 56(84) bytes of data\n",

dest_str,(inaddr&0x000000ff)>>0,(inaddr&0x0000ff00)>>8,(inaddr&0x00ff0000)>>16,(inaddr&0xff000000)>>24);

network_icmp_send();

retnetworkstatus = network_icmp_recv();

//retnetworkstatus 為網路狀態情況

return 0;

}

ubuntu命令(一) 獲取原始碼

本作業系統為ubuntu kylin 14.01 32位作業系統。以下使用ubuntu代替ubuntu kylin 14.01 32位作業系統。ubuntu查詢命令所在原始碼包 以ls命令來看,搜尋命令所在原始碼包。which ls bin ls用命令搜尋該軟體所在包 dpkg s bin ls c...

獲取下命令原始碼包

以ping命令為例 1 shell下執行which ping 輸出 bin ping 2 dpkg s bin ping 輸出 iputils ping bin ping 3 apt get source iputils ping 即可看到當前目錄下存在 iputils 20121221 4ubun...

通過鏈結獲取Html原始碼內容

通過鏈結獲取網頁原始碼 public static string getcontenfrommurl string url 獲取指定div的內容 被篩選的字串 id名 public static string getdivfromstr string strhtml 替換指定 html 返回替換後的...