Detailed explanation of PHP implementing ping (raw socket) through ICMP protocol

藏色散人
Release: 2023-04-09 20:38:02
forward
3031 people have browsed it

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.

Detailed explanation of PHP implementing ping (raw socket) through ICMP protocol

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);//数据
}
Copy after login

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字节
Copy after login

Fill the checksum into the data packet.

$package[2] = $checksum[0];
$package[3] = $checksum[1];//填充校验和
Copy after login

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(&#39;icmp&#39;));//创建原始套接字
$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;//输出结果
Copy after login

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
Copy after login

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(&#39;icmp&#39;));//创建原始套接字
    $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
}
?>
Copy after login

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!

Related labels:
php
source:joyrunc
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template