POE::C...::Ping(3pm)

πŸ“¦
version 1.177 Β· 3 versions
, in debian bookworm, debian trixie, ubuntu jammy, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– POE::Component::Client::Ping β€” the manual, but make it cute πŸ’–

okay bestie, welcome to the POE::C...::Ping(3pm) page β€” that's POE::Component::Client::Ping, from the User Contributed Perl Documentation section of your local man pages. Pull up a chair, grab a boba, we're doing ICMP today. πŸ§‹βœ¨


🌸 NAME 🌸

POE::Component::Client::Ping β€” a non-blocking ICMP ping client πŸ’…

No cap, that's the whole name section. Short. Iconic. Moving on. πŸ¦‹


✨ SYNOPSIS ✨

Here's the whole vibe, verbatim, because I would never do you dirty by paraphrasing code:

use POE qw(Component::Client::Ping);

POE::Component::Client::Ping->spawn(
  Alias               => "pingthing",  # defaults to "pinger"
  Timeout             => 10,           # defaults to 1 second
  Retry               => 3,            # defaults to 1 attempt
  OneReply            => 1,            # defaults to disabled
  Parallelism         => 64,           # defaults to autodetect
  BufferSize          => 65536,        # defaults to undef
  AlwaysDecodeAddress => 1,            # defaults to 0
);

sub some_event_handler {
  $kernel->post(
    "pingthing", # Post the request to the "pingthing" component.
    "ping",      # Ask it to "ping" an address.
    "pong",      # Have it post an answer as a "pong" event.
    $address,    # This is the address we want to ping.
    $timeout,    # Optional timeout.  It overrides the default.
    $retry,      # Optional retries. It overrides the default.
  );
}

# This is the sub which is called when the session receives a "pong"
# event.  It handles responses from the Ping component.
sub got_pong {
  my ($request, $response) = @_[ARG0, ARG1];

  my ($req_address, $req_timeout, $req_time)      = @$request;
  my ($resp_address, $roundtrip_time, $resp_time, $resp_ttl) = @$response;

  # The response address is defined if this is a response.
  if (defined $resp_address) {
    printf(
      "ping to %-15.15s at %10d. pong from %-15.15s in %6.3f s\n",
      $req_address, $req_time,
      $resp_address, $roundtrip_time,
    );
    return;
  }

  # Otherwise the timeout period has ended.
  printf(
    "ping to %-15.15s is done.\n", $req_address,
  );
}

or

use POE::Component::Client::Ping ":const";

# Post an array ref as the callback to get data back to you
$kernel->post("pinger", "ping", [ "pong", $user_data ]);

# use the REQ_USER_ARGS constant to get to your data
sub got_pong {
    my ($request, $response) = @_[ARG0, ARG1];
    my $user_data = $request->[REQ_USER_ARGS];
    ...;
}

okay so let's break that down, because there is so much happening here and it's all lowkey slay. 🌈

First move: you use POE qw(Component::Client::Ping); β€” boom, the component is in the building. 🫢

Second move: you spawn the thing. That spawn call takes a whole little roster of options, each one with a comment right there telling you what it defaults to β€” Alias defaults to "pinger", Timeout defaults to 1 second, Retry defaults to 1 attempt, OneReply defaults to disabled, Parallelism defaults to autodetect, BufferSize defaults to undef, and AlwaysDecodeAddress defaults to 0. (In the example they're all cranked to non-default values, because why be basic. πŸ’…)

Third move: you post to it. You send the alias of the component ("pingthing" in the example), the event name "ping", the event you want back as an answer ("pong"), the $address you're pinging, plus an optional $timeout and an optional $retry β€” and heads up, those two optional args override the default when you pass them. πŸ‘‘

Fourth move: you write the sub that catches the "pong". It gets @_[ARG0, ARG1] as ($request, $response).

  • The $request unpacks into ($req_address, $req_timeout, $req_time).
  • The $response unpacks into ($resp_address, $roundtrip_time, $resp_time, $resp_ttl).
  • The response address is defined if this is a response β€” so if defined $resp_address hits, you got a real pong and you can print the address, the time, and the roundtrip time. πŸ”₯
  • Otherwise? the timeout period has ended. That's it, that's the whole mood. 😭

The alt route: instead of the plain use, you can do use POE::Component::Client::Ping ":const";. Then you post an array ref as the callback to get data back to you β€” $kernel->post("pinger", "ping", [ "pong", $user_data ]); β€” and inside got_pong you reach into your data with the REQ_USER_ARGS constant: my $user_data = $request->[REQ_USER_ARGS];. πŸŽ€

ngl, note that in that second snippet the alias is "pinger" β€” that's the default alias, not "pingthing". Both are in the original, both stay. Don't come for me. πŸ™ˆ

and that's the synopsis, bestie β€” more parts are coming so don't go anywhere, we're just getting started. πŸ’œ

πŸ’– DESCRIPTION πŸ’–

POE::Component::Client::Ping is a non-blocking ICMP ping client ✨ It lets a whole bunch of other sessions ping through it in parallel, and it lets them keep doing their own lil thing while they wait for responses. Multitasking queen behavior honestly πŸ‘‘

Okay so important vibe check first: ping client components are not proper objects. Like, most objects get created, but these ones get spawned as separate sessions instead. To avoid confusion (and hopefully not cause other confusion 😭), you have to spawn them with a spawn method β€” NOT created anew with a new one. Do not come in here trying new, bestie, it will not go how you want πŸ’€

PoCo::Client::Ping's spawn method takes a few named parameters:

  • Alias => $session_alias β€” sets the component's alias. It's the target of post() calls (peep the synopsis πŸ‘€). The alias defaults to pinger.
  • Socket => $raw_socket β€” lets developers open an existing raw socket instead of letting the component try to open one itself. If you omit it, the component creates its own raw socket.

This is super useful for people who'd rather not do a whole security audit on POE, because it lets them make a raw socket in their own code and then run POE at reduced privileges. iykyk 🫢

  • Timeout => $ping_timeout β€” sets the default amount of time (in seconds) a Ping component will wait for a single ICMP echo reply before retrying. Default is 1. And yes, it is both possible and meaningful to set it to a fractional number of seconds ✨

This default timeout only kicks in for ping requests that don't bring their own timeouts. No gatekeeping, just defaults πŸ’…

  • Retry => $ping_attempts β€” sets the default number of attempts a ping gets sent before it should be considered failed. Default is 1.
  • OneReply => 0|1 β€” set OneReply to stop the Ping component from waiting the full timeout period for replies. Normally the ICMP protocol allows for multiple replies to a single request, so it's proper to wait for the late ones. This option disables that wait, ending the ping transaction at the first response. Any subsequent responses will be silently ignored πŸ«₯

OneReply is disabled by default, and a single successful request will generate at least two responses. The first response is a successful ICMP ECHO REPLY event. The second is an undefined response event, signifying that the timeout period has ended.

A ping request will generate exactly one reply when OneReply is enabled. That reply represents either the first ICMP ECHO REPLY to arrive, or that the timeout period has ended. Either way, one (1) reply, periodt πŸ’…

  • Parallelism => $limit β€” sets POE::Component::Client::Ping's maximum number of simultaneous ICMP requests. Higher numbers speed up the processing of big host lists, up to the point where the OS or the network gets oversaturated and starts dropping packets. It's giving... capacity planning πŸ“ˆ

And the difference can be dramatic. A tuned Parallelism can enable responses down to 1ms, depending on the network, although it will take longer to get through the hosts list.

Pinging 762 hosts at Parallelism=64
Starting to ping hosts.
Pinged 10.0.0.25       - Response from 10.0.0.25       in  0.002s
Pinged 10.0.0.200      - Response from 10.0.0.200      in  0.003s
Pinged 10.0.0.201      - Response from 10.0.0.201      in  0.001s

real  1m1.923s
user  0m2.584s
sys   0m0.207s

Responses will take significantly longer with an untuned Parallelism, but the total run time will be quicker.

Pinging 762 hosts at Parallelism=500
Starting to ping hosts.
Pinged 10.0.0.25       - Response from 10.0.0.25       in  3.375s
Pinged 10.0.0.200      - Response from 10.0.0.200      in  1.258s
Pinged 10.0.0.201      - Response from 10.0.0.201      in  2.040s

real  0m13.410s
user  0m6.390s
sys   0m0.290s

Excessively high parallelism values may saturate the OS or network, resulting in few or no responses 😭

Pinging 762 hosts at Parallelism=1000
Starting to ping hosts.

real  0m20.520s
user  0m7.896s
sys   0m0.297s

By default, POE::Component::Client::Ping will guess at an optimal Parallelism value based on the raw socket receive buffer size and the operating system's nominal ICMP packet size. That latter figure is 3000 octets for Linux and 100 octets for other systems. ICMP packets are generally under 90 bytes, but operating systems may use alternative numbers when calculating buffer capacities. The component tries to mimic calculations observed in the wild 🧚

When in doubt: experiment with different Parallelism values and use the one that works best. Science, bestie πŸ”¬

  • BufferSize => $bytes β€” if set, the size of the receive buffer of the raw socket gets modified to the given value. The default size of the receive buffer is operating system dependent. If the buffer cannot be set to the given value, a warning will be generated but the system will continue working.
Heads up: if the buffer is set too small and too many ping replies arrive at the same time, the operating system may discard the ping replies and mistakenly cause this component to believe the ping has timed out. In this case, you'll typically see discards noted in the counters displayed by netstat -s. πŸ’€

Increased BufferSize values can expand the practical limit for Parallelism ✨

  • AlwaysDecodeAddress => 0|1 β€” if set, any input addresses will always be looked up, even if the hostname happens to be only 4 characters in size. Ideally you should be passing addresses in to the system to avoid slow hostname lookups, but if you must use hostnames and there's a possibility you might have short hostnames, then you should set this. Short king energy πŸ‘‘
  • Payload => $bytes β€” sets the ICMP payload (data bytes). Otherwise the component generates 56 data bytes internally. Note that some firewalls will discard ICMP packets with nonstandard payload sizes, so watch out πŸ™ˆ

Sessions communicate asynchronously with the Client::Ping component. They post ping requests to it, and they receive pong events back. It's literally a whole conversation πŸ’¬

Requests are posted to the component's ping handler. They include the name of an event to post back, an address to ping, and an optional amount of time to wait for responses. The address may be a:

  • numeric dotted quad
  • packed inet_aton address
  • host name
Host names are not recommended: they must be looked up for every ping request, and DNS lookups can be very slow. 😭

The optional timeout overrides the one set when spawn was called.

Ping responses come with two array references:

my ($request, $response) = @_[ARG0, ARG1];

$request contains information about the original request:

my (
  $req_address, $req_timeout, $req_time, $req_user_args,
) = @$request;
  • $req_address β€” the original request address. It matches the address posted along with the original ping request.

It's useful along with $req_user_args for pairing requests with their corresponding responses. Matching, we love to see it 🫢

  • $req_timeout β€” the original request timeout. It's either the one passed with the ping request or the default timeout set with spawn.
  • $req_time β€” the time the ping event was received by the Ping component. It's a real number based on the current system's time() epoch.
  • $req_user_args β€” a scalar containing arbitrary data that can be sent along with a request. It's often used to provide continuity between requests and their responses. $req_user_args may contain a reference to some larger data structure.

To use it, replace the response event with an array reference in the original request. The array reference should contain two items: the actual response event, and a scalar with the context data the program needs back. Peep the SYNOPSIS for an example ✨

$response contains information about the ICMP ping response. There may be multiple responses for a single request.

my ($response_address, $roundtrip_time, $reply_time, $reply_ttl) =
@$response;
  • $response_address β€” the address that responded to the ICMP echo request. It may be different than $request_address, especially if the request was sent to a broadcast address.

$response_address will be undefined if $request_timeout seconds have elapsed. This marks the end of responses for a given request. Programs can assume that no more responses will be sent for the request address. They may use this marker to initiate another ping request. It's giving "closing the loop" πŸ’…

  • $roundtrip_time β€” the number of seconds that elapsed between the ICMP echo request's transmission and its corresponding response's receipt. It's a real number. This is purely the trip time and does not include any time spent queueing if the system's parallelism limit caused the ping transmission to be delayed. No sneaky queue time in here, nope βœ‹
  • $reply_time β€” the time when the ICMP echo response was received. It's a real number based on the current system's time() epoch.
  • $reply_ttl β€” the ttl for the echo response packet we received.

If the :const tagset is imported the following constants will be exported:

REQ_ADDRESS, REQ_TIMEOUT, REQ_TIME REQ_USER_ARGS, RES_ADDRESS, RES_ROUNDTRIP, RES_TIME, RES_TTL


🌈 SEE ALSO 🌈

This component's ICMP ping code was lifted from Net::Ping, which is an excellent module when you only need to ping one host at a time. Sometimes that's the whole vibe πŸŽ€

See POE, of course, which includes a lot of documentation about how POE works. Obviously. Periodt πŸ’…

Also see the test program, t/01_ping.t, in the component's distribution πŸ‘€


πŸ› BUG TRACKER πŸ›

https://rt.cpan.org/Dist/Display.html?Queue=POE-Component-Client-Ping


πŸ“¦ REPOSITORY πŸ“¦

http://github.com/rcaputo/poe-component-client-ping/


🌐 OTHER RESOURCES 🌐

http://search.cpan.org/dist/POE-Component-Client-Ping/


πŸ‘‘ AUTHOR & COPYRIGHTS πŸ‘‘

POE::Component::Client::Ping is Copyright 1999-2020 by Rocco Caputo. All rights are reserved. POE::Component::Client::Ping is free software; you may redistribute it and/or modify it under the same terms as Perl itself.

You can learn more about POE at http://poe.perl.org/ ✨


okay bestie that's the whole page 🫢 this was perl v5.32.1, dated 2021-02-15, from the POE::C...::Ping(3pm) page β€” go forth and ping responsibly πŸ’–βœ¨

πŸ“¦ where this page comes from