Home > Article > Backend Development > Detailed explanation of PHP implementing ping (raw socket) through ICMP protocol
Recommended study: "PHP Video Tutorial"
PHP implements ping (raw socket) through ICMP protocol
Recently I want to implement a function to detect whether the target host is online. I checked it on Baidu. Most of them use a connection opened to a certain port to determine whether the target host is online. Such as port 3389 (RDP) of Windows systems and port 22 (SSH) of *nix systems.
But this will cause a problem. If the target host does not open these ports, it will lead to errors in judgment. Just because a certain port is not open does not mean that the target host is offline.
Since most devices will respond to ping, I thought of using ping to implement this function. Querying Baidu again, I found that most tutorials use the exec() function to call the system ping command, which is obviously very unsafe.
So I finally decided to use the raw socket provided by PHP and build the ICMP package myself to implement ping.
To build an ICMP packet, we first need to understand the structure of the ICMP packet.
As you can see, a standard ICMP packet consists of 8-bit type, 8-bit code, 16-bit checksum, 16-bit ID, 16-bit sequence number and data . Next, we build such a data package through PHP.
$package = chr(8).chr(0);//模式 8 0 $package .= chr(0).chr(0);//置零校验和 $package .= "R"."C";//ID 这里是我随便填的 $package .= chr(0).chr(1);//序列号 一样 随便填的 for($i=strlen($package);$i<64;$i++){//填充满64位 $package .= chr(0);//数据 }
Next calculate the checksum.
$tmp = unpack("n*",$package);//把数据16位一组放进数组里 $sum = array_sum($tmp);//求和 $sum = ($sum >> 16) + ($sum & 0xFFFF);//结果右移十六位 加上结果与0xFFFF做AND运算 $sum = $sum + ($sum >> 16);//结果加上结果右移十六位 $sum = ~ $sum;//做NOT运算 $checksum = pack("n*", $sum);//打包成2字节
Fill the checksum into the data packet.
$package[2] = $checksum[0]; $package[3] = $checksum[1];//填充校验和
In this way, a standard ICMP data packet is constructed and can be sent directly to the target host. Ready to go~
$host = "192.168.1.1";//设置目标主机 $socket=socket_create(AF_INET, SOCK_RAW, getprotobyname('icmp'));//创建原始套接字 $start = microtime();//记录开始时间 socket_sendto($socket, $package, strlen($package), 0, $host, 0);//发送数据包 $read = array($socket);//初始化socket $select = socket_select($read, $write, $except, 5); if ($select === FALSE){ $icmpError = "socket_select()方法发生错误,原因:".socket_strerror(socket_last_error()); socket_close($socket); }else if($select === 0){ $icmpError = "请求超时"; socket_close($socket); } if($icmpError !== NULL){ echo $icmpError; exit(); } socket_recvfrom($socket, $recv, 65535, 0, $host, $port);//接受回传数据 /*回传数据处理*/ $end = microtime();//记录结束时间 $recv = unpack("C*", $recv); $length = count($recv) - 20;//包长度 减去20字节IP报头 $ttl = $recv[9];//ttl $seq = $recv[28];//序列号 $duration = round(($end - $start) * 1000,3);//计算耗费的时间 echo "{$length} bytes from {$host}: icmp_seq={$seq} ttl={$ttl} time={$duration}ms".PHP_EOL;//输出结果
Tap to run, and a ping request is completed. If nothing else, the result should be as shown below.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=128 time=0.589ms
Finally, I packaged these codes into a function. Add it to your code and use ping(string $host, int $retry) when you need to call it.
<?php function ping($host, $retry = 1){ $g_icmp_error = NULL; $write = NULL; $except = NULL;//初始化所需变量 $package = chr(8).chr(0);//模式 8 0 $package .= chr(0).chr(0);//置零校验和 $package .= "R"."C";//ID $package .= chr(0).chr(1);//序列号 for($i=strlen($package);$i<64;$i++){ $package .= chr(0); } $tmp = unpack("n*",$package);//把数据16位一组放进数组里 $sum = array_sum($tmp);//求和 $sum = ($sum >> 16) + ($sum & 0xFFFF);//结果右移十六位 加上结果与0xFFFF做AND运算 $sum = $sum + ($sum >> 16);//结果加上结果右移十六位 $sum = ~ $sum;//做NOT运算 $checksum = pack("n*", $sum);//打包成2字节 $package[2] = $checksum[0]; $package[3] = $checksum[1];//填充校验和 $socket=socket_create(AF_INET, SOCK_RAW, getprotobyname('icmp'));//创建原始套接字 $start = microtime();//记录开始时间 socket_sendto($socket, $package, strlen($package), 0, $host, 0);//发送数据包 $read = array($socket);//初始化socket $select = socket_select($read, $write, $except, 5); if ($select === FALSE){ $icmpError = "socket_select()方法发生错误,原因:".socket_strerror(socket_last_error()); socket_close($socket); }else if($select === 0){ $icmpError = "请求超时"; socket_close($socket); } if($icmpError !== NULL){ echo $icmpError; exit(); } socket_recvfrom($socket, $recv, 65535, 0, $host, $port);//接受回传数据 /*回传数据处理*/ $end = microtime();//记录结束时间 $recv = unpack("C*", $recv); $length = count($recv) - 20;//包长度 减去20字节IP报头 $ttl = $recv[9];//ttl $seq = $recv[28];//序列号 $duration = round(($end - $start) * 1000,3);//计算耗费的时间 echo "{$length} bytes from {$host}: icmp_seq={$seq} ttl={$ttl} time={$duration}ms".PHP_EOL;//输出结果 socket_close($socket);//关闭socket } ?>
If there are any errors or incomplete details in the article, please feel free to point them out and discuss them in the comment area.
The above is the detailed content of Detailed explanation of PHP implementing ping (raw socket) through ICMP protocol. For more information, please follow other related articles on the PHP Chinese website!