Linux uses ICMP protocol for simple Ping Network Monitoring

Source: Internet
Author: User

ICMP is an Internet Control Message Protocol. It is a sub-Protocol of the TCP/IP protocol family. It is used to transmit control messages between IP hosts and routers. A message control refers to a message of the network itself, such as network connectivity, host accessibility, and routing availability. Although these control messages do not transmit user data, they play an important role in transferring user data. ICMP is a connectionless protocol used to transmit Error Report control information. It is a very important protocol and has an extremely important significance for network security.
After half a day, ICMP is also one of the TCP/IP protocols, so it is similar to TCP protocol to monitor whether the network is pinged. The steps are summarized as follows: 1. Bind a socket; 2. Send data packets; 3. receive data packets; 4. parse data packets.
#include <stdio.h>#include <sys/socket.h>#include <netinet/in.h>#include <netinet/ip.h>#include <netinet/ip_icmp.h>#include <netdb.h>
# Define PACKET_SIZE 4096 # define ERROR 0 # define SUCCESS 1 // validation algorithm (comments are provided under Baidu, but I still cannot understand it) unsigned short cal_chksum (unsigned short * addr, int len) {int nleft = len; int sum = 0; unsigned short * w = addr; unsigned short answer = 0; while (nleft> 1) {sum + = * w ++; nleft-= 2;} if (nleft = 1) {* (unsigned char *) (& answer) = * (unsigned char *) w; sum + = answer ;} sum = (sum> 16) + (sum & 0 xffff); sum + = (sum> 16 ); Answer = ~ Sum; return answer;} // Ping function int ping (char * ips, int timeout) {struct timeval * tval; int maxfds = 0; fd_set readfds; struct sockaddr_in addr; struct sockaddr_in from; // sets Ip information bzero (& addr, sizeof (addr); addr. sin_family = AF_INET; addr. sin_addr.s_addr = inet_addr (ips); int sockfd; // obtain socket sockfd = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP); if (sockfd <0) {printf ("ip: % s, socket error \ n ", ips); r Eturn ERROR;} struct timeval timeo; // set the TimeOut time timeo. TV _sec = timeout/1000; timeo. TV _usec = timeout % 1000; if (setsockopt (sockfd, SOL_SOCKET, SO_SNDTIMEO, & timeo, sizeof (timeo) =-1) {printf ("ip: % s, setsockopt error \ n ", ips); return ERROR;} char sendpacket [PACKET_SIZE]; char recvpacket [PACKET_SIZE]; // set the Ping packet memset (sendpacket, 0, sizeof (sendpacket); pid_t pid; // obtain the PID as the Ping Sequence. ID pid = getpid (); struct ip * iph; struct icmp * icmp; icmp = (struct icmp *) sendpacket; icmp-> icmp_type = ICMP_ECHO; // echo request icmp-> icmp_code = 0; icmp-> icmp_cksum = 0; icmp-> icmp_seq = 0; icmp-> icmp_id = pid; tval = (struct timeval *) icmp-> icmp_data; gettimeofday (tval, NULL); icmp-> icmp_cksum = cal_chksum (unsigned short *) icmp, sizeof (struct icmp); // check int n; // send packets n = sendto (sockfd, (char *) & sendpacket, sizeof (st Ruct icmp), 0, (struct sockaddr *) & addr, sizeof (addr); if (n <1) {printf ("ip: % s, sendto error \ n ", ips); return ERROR;} // accept // The while (1) {// set the TimeOut time. This time, FD_ZERO (& readfds); FD_SET (sockfd, & readfds); maxfds = sockfd + 1; n = select (maxfds, & readfds, NULL, NULL, & timeo); if (n <= 0) {printf ("ip: % s, Time out error \ n", ips ); close (sockfd); return ERROR;} // accept Memset (recvpacket, 0, sizeof (recvpacket); int fromlen = sizeof (from); n = recvfrom (sockfd, recvpacket, sizeof (recvpacket), 0, (struct sockaddr *) & from, (socklen_t *) & fromlen); if (n <1) {break;} char * from_ip = (char *) inet_ntoa (from. sin_addr); // determines if it is a self-Ping reply if (strcmp (from_ip, ips )! = 0) {printf ("NowPingip: % s Fromip: % s \ nNowPingip is not same to Fromip, so ping wrong! \ N ", ips, from_ip); return ERROR;} iph = (struct ip *) recvpacket; icmp = (struct icmp *) (recvpacket + (iph-> ip_hl <2); printf ("ip: % s \ n, icmp-> icmp_type: % d \ n, icmp-> icmp_id: % d \ n ", ips, icmp-> icmp_type, icmp-> icmp_id ); // determine the status of the Ping response packet if (icmp-> icmp_type = ICMP_ECHOREPLY & icmp-> icmp_id = pid) // ICMP_ECHOREPLY echo response {// exit the loop break if it is normal;} else {// otherwise, continue to wait for the continue;} int main () {char cPing [16]; printf ("Pleas E input ping IP: "); scanf (" % s ", cPing); if (ping (cPing, 10000) {printf (" Ping succeed! \ N ");} else {printf (" Ping wrong! \ N ");}}

Test result: root @ an-virtual-machine :~ /Wyz/test #./testping
Please input ping IP: 192.168.1.155
Nowip: 192.168.1.155 Fromip: 192.168.1.20.
Nowip is not same to Fromip, so ping wrong!
Ping wrong!
Root @ an-virtual-machine :~ /Wyz/test #./testping
Please input ping IP: 192.168.1.188
Ip: 192.168.1.188
, Icmp-> icmp_type: 0
, Icmp-> icmp_id: 27865
Ping succeed!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.