Simple async lowlevel ICMP (ping) messaging library built on top of React PHP

Overview

clue/icmp-react Build Status

Simple async lowlevel ICMP (ping) messaging library built on top of react

Usage

Once clue/icmp-react is installed, you can run any of its examples via command line like this:

$ sudo php examples/ping.php www.google.com

Note: Please note the sudo there. Opening raw ICMP sockets requires root access!

The provided examples should give you a good overview of how this library works. As such, the following guide assumes some familiarity with the examples and explains them a bit more in detail.

This library is built on top of reactphp in order to follow an async (non-blocking) paradigm. This is done so that you can send and receive multiple messages in parallel without blocking each other.

For this to work, you will need to pass it an event loop (or create a new one).

Factory

The Clue\React\Icmp\Factory is a convenient wrapper that helps you initialize the correct loop and an ICMP handler.

Simple factory

Passing an existing loop instance to the factory is optional. If you're new to the concept of an event loop and/or only care about handling ICMP messages, then you're recommend to let the factory do the work for you and let it create the recommended loop implementation:

$factory = new Clue\React\Icmp\Factory();
$icmp = $factory->createIcmp4();

You will have to access and later run its loop like this:

$loop = $factory->getLoop();

// place your ICMP calls here, see below...

// important: run the loop in order to actually do anything
$loop->run();

If you do not pass an existing event loop (like above), one will be created for you that is suitable (and reasonably fast) to handle an ICMP socket. However, due to the nature of ICMP's low-level network access, this loop is not suitable to add other stream resources to it. In particular, this means you should expect it to reject just about any normal stream / connection usually found in reactphp.

Using an existing event loop

If you only want to handle ICMP sockets, the above limitations do not really apply to you at all. If however you also want to use the same event loop for other streams, you can also pass in an existing event loop instance to the factory like this:

$loop = React\EventLoop\Factory::create();
$factory = new Clue\React\Icmp\Factory($loop);

While this will allow you to attach any number of streams to the loop, this approach usually does not natively support ICMP's low-level sockets. As such, instead of actually setting up an event listener in the given loop internally, it will have to set up a timer to periodically check the ICMP socket using an hidden inner event loop. This is actually only a small implementation detail you'll likely not have to worry about as this happens completely transparent. However, while relying on a periodic timer (interval of 10ms, i.e. 100 checks per second) will only have a small impact on CPU usage, this will have a noticable effect on ICMP response times. So for example expect a ping() to take an additional 10-20ms.

Ping

Probably the most common use of this library is to send ICMP echo requests (i.e. ping messages), so it provides a convenient promise-based API:

$icmp->ping($remote, $timeout);

Keep in mind that this is an async method, i.e. the ping message will only be queued to be sent. This is done so that you can send multiple ping messages in parallel without blocking each other and also matching the incoming ping replies to their corresponding ping requests.

You will probably use this code somewhat like this:

$icmp->ping('github.com', 3.0)->then(function ($time) {
    echo 'Success after ' . $time . ' seconds';
}, function(Exception $error) {
    echo 'Nope, there was an error';
});

Receive messages

The above code automatically sets up a temporary listener to check each incoming message if it matches the expected ping result. You can also listen to any incoming messages yourself like this:

$icmp->on('message', function (Clue\React\Icmp\Message $message, $peerAddress) {
    echo 'Message type ' . $message->getType() . ' from ' . $peerAddress . PHP_EOL;
});

Send messages

If you want to send arbitrary ICMP messages, you can either construct a Message object yourself like this

$message = new Clue\React\Icmp\Message($type, $code, $checksum, $header, $payload)

or you can use the MessageFactory to create common types of messages like this

$factory = new Clue\React\Icmp\MessageFactory();
$message = $factory->createMessagePing();

Next, you can send the message object like this:

$icmp->sendMessage($message, '127.0.0.1');

This is an async operation, i.e. the message is only queued to be sent, so make sure to actually start the loop ($loop->run()). Also, you may likely (but not necessarily) want to wait for a response message, so take a look at receiving messages.

Install

The recommended way to install this library is through composer. New to composer?

{
    "require": {
        "clue/icmp-react": "~0.2.0"
    }
}

Tests

To run the test suite, you need PHPUnit. Go to the project root and run:

$ phpunit tests

Note: The test suite contains tests for ICMP sockets which require root access on unix/linux systems. Therefor some tests will be skipped unless you run sudo phpunit tests to execute the full test suite.

License

MIT

You might also like...
A fully-managed real-time messaging service that allows you to send and receive messages between independent applications.

A fully-managed real-time messaging service that allows you to send and receive messages between independent applications.

Description: A simple plugin that sets the current admin page to always be at the top of the admin menu.

=== Sticky Admin Menu === Contributors: sc0ttkclark Donate link: https://www.scottkclark.com/ Tags: admin menu, sticky Requires at least: 4.4 Tested u

Here is the top 100 PHP functions: it is the list of the most often used PHP native functions

Here is the top 100 PHP functions: it is the list of the most often used PHP native functions. If you are a PHP developer, you must know the Top 100 PHP Functions deeply.

Envbar allows you to differentiate between environments by adding a custom colored bar above the top navigation.
Envbar allows you to differentiate between environments by adding a custom colored bar above the top navigation.

Envbar Envbar allows you to differentiate between environments by adding a custom colored bar above the top navigation. This should help backend users

WordPlate is a wrapper around WordPress. It makes developers life easier. It is just like building any other WordPress website with themes and plugins. Just with sprinkles on top.
WordPlate is a wrapper around WordPress. It makes developers life easier. It is just like building any other WordPress website with themes and plugins. Just with sprinkles on top.

WordPlate is simply a wrapper around WordPress. It makes developers life easier. It is just like building any other WordPress website with themes and plugins. Just with sprinkles on top.

Buy and sell crypto top 100 crypto with our fake currency. Donate to and use our referal links for discounts

PLEASE ENABLE SQLITE3 AND GD OR GD2 IN XAMPP TO RUN THE APP! (SEE HOW_TO_SETUP_XAMPP.gif) ![alt text](https://github.com/Tby23rd/Project1-Cryptosimul

Adds support for quickly adding a "snow day" banner at the top of a website.

❄️ Snow Day Plugin 🚨 Requires OctoberCMS 2.0 ✨ What does this plugin do? Provides the ability to quickly add a cross-site banner using a component. ❓

Magento React Native Community
Magento React Native Community

Magento React Native Community New version of the https://github.com/troublediehard/magento-react-native based on GraphQL api. Which will be covered w

React Native mobile app for Magento 2.x
React Native mobile app for Magento 2.x

Open source React Native mobile app for Magento 2 Magento React Native is a fully functional eCommerce App for your Magento 2 website. It uses Magento

Comments
  • Changed requirement of socket-react to equal/greater than 0.2.

    Changed requirement of socket-react to equal/greater than 0.2.

    In our project many repositories require igorw/evenement 2., but socket-react 0.2. accepts only igorw/evenement 1.*

    I've updated the requirements to >=0.2 and tested the example 'ping.php' with success.

    opened by blackus3r 4
  • Travis build errors

    Travis build errors

    Current build errors on travis are caused by an issue in travis and ought to be ignored for now:

    https://github.com/travis-ci/travis-ci/issues/2261

    See also clue/socket-raw#4 for a possible workaround. See also clue/socket-react#11 for same issue.

    maintenance help wanted 
    opened by clue 1
  • Support IPv6 (ICMPv6)

    Support IPv6 (ICMPv6)

    Is thie lib support IPv6?

    PING fe80::250:56ff:fe95:d806(fe80::250:56ff:fe95:d806) 56 data bytes
    64 bytes from fe80::250:56ff:fe95:d806%ens192: icmp_seq=1 ttl=64 time=0.129 ms
    64 bytes from fe80::250:56ff:fe95:d806%ens192: icmp_seq=2 ttl=64 time=0.063 ms
    ^C
    --- fe80::250:56ff:fe95:d806 ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 1018ms
    rtt min/avg/max/mdev = 0.063/0.096/0.129/0.033 ms
    root@php-dev-cn:~/php-icmp-react# php examples/ping.php fe80::250:56ff:fe95:d806
    Pinging "fe80::250:56ff:fe95:d806"...
    Error: Timed out after 3 seconds
    

    doesn't seem to work

    new feature help wanted 
    opened by louishot 2
Releases(v0.2.0)
  • v0.2.0(Jul 21, 2014)

  • v0.1.0(Mar 18, 2014)

    • First tagged release
    • Send and receive arbitrary ICMP messages
    • Promise-based Icmp::ping() method (ICMP echo request and echo reply)
    • Event-driven access to incoming ICMP messages
    Source code(tar.gz)
    Source code(zip)
Owner
Christian Lück
Maintainer of @ReactPHP. Creator of Framework X. Head of @clue-engineering. Professional software engineer using open source to empower web-based projects.
Christian Lück
Use php-fpm as a simple built-in async queue

PHP-FPM Async Queue Use php-fpm as a simple built-in async queue. Based on interoperable queue interfaces Queue Interop. Usage composer makasim/php-fp

Max Kotliar 111 Nov 19, 2022
Tiny, fast and simple PHP boilerplate built on top of FlightPHP

BlessPHP Tiny, fast and simple PHP boilerplate built on top of FlightPHP. Good enough to use as skeleton for prototypes and some pet-projects. The nam

Anthony Axenov 2 Sep 20, 2022
Spot v2.x DataMapper built on top of Doctrine's Database Abstraction Layer

Spot DataMapper ORM v2.0 Spot v2.x is built on the Doctrine DBAL, and targets PHP 5.4+. The aim of Spot is to be a lightweight DataMapper alternative

Spot ORM 602 Dec 27, 2022
PHP Library that implements several messaging patterns for RabbitMQ

Thumper Thumper is a PHP library that aims to abstract several messaging patterns that can be implemented over RabbitMQ. Inside the examples folder yo

php-amqplib 276 Nov 20, 2022
Venture allows you to create and manage complex, async workflows in your Laravel apps.

Venture is a package to help you build and manage complex workflows of interdependent jobs using Laravel's queueing system. Installation Note: Venture

Kai Sassnowski 680 Dec 14, 2022
An async process dispatcher for Amp.

process This package provides an asynchronous process dispatcher that works on all major platforms (including Windows). As Windows pipes are file hand

AMPHP 204 Jan 8, 2023
This component, based on the Symfony serializer and async-aws, is a human-readable and quick abstraction to easily store serialized objects in DynamoDB 🚀.

DynamoDB Storable This component, based on the Symfony serializer and async-aws, is a human-readable and quick abstraction to easily store serialized

Matthieu W. 2 Jun 19, 2022
Stupid async implementation using await-generator

libAsync Stupid async implementation using await-generator Usage libAsync::doAsync(Closure $executor); // <-- Returns a promise Example Fetch data fro

null 5 Jan 2, 2023
Integrate reCAPTCHA using async HTTP/2, making your app fast with a few lines.

ReCaptcha Integrate reCAPTCHA using async HTTP/2, making your app fast with a few lines. use Illuminate\Support\Facades\Route; Route::post('login', f

Laragear 14 Dec 6, 2022
Starless Sky is a network protocol for secure identities, providing the use of assymetric identities, public information, end-to-end messaging and smart contracts

Descentralized network protocol providing smart identity over an secure layer. What is the Starless Sky Protocol? Starless Sky is a network protocol f

Starless Sky Protocol 3 Jun 19, 2022