1POE::Component::Client:U:sPeirngC(o3n)tributed Perl DocuPmOeEn:t:aCtoimopnonent::Client::Ping(3)
2
3
4
6 POE::Component::Client::Ping - a non-blocking ICMP ping client
7
9 use POE qw(Component::Client::Ping);
10
11 POE::Component::Client::Ping->spawn(
12 Alias => "pingthing", # defaults to "pinger"
13 Timeout => 10, # defaults to 1 second
14 Retry => 3, # defaults to 1 attempt
15 OneReply => 1, # defaults to disabled
16 Parallelism => 64, # defaults to autodetect
17 BufferSize => 65536, # defaults to undef
18 AlwaysDecodeAddress => 1, # defaults to 0
19 );
20
21 sub some_event_handler {
22 $kernel->post(
23 "pingthing", # Post the request to the "pingthing" component.
24 "ping", # Ask it to "ping" an address.
25 "pong", # Have it post an answer as a "pong" event.
26 $address, # This is the address we want to ping.
27 $timeout, # Optional timeout. It overrides the default.
28 $retry, # Optional retries. It overrides the default.
29 );
30 }
31
32 # This is the sub which is called when the session receives a "pong"
33 # event. It handles responses from the Ping component.
34 sub got_pong {
35 my ($request, $response) = @_[ARG0, ARG1];
36
37 my ($req_address, $req_timeout, $req_time) = @$request;
38 my ($resp_address, $roundtrip_time, $resp_time, $resp_ttl) = @$response;
39
40 # The response address is defined if this is a response.
41 if (defined $resp_address) {
42 printf(
43 "ping to %-15.15s at %10d. pong from %-15.15s in %6.3f s\n",
44 $req_address, $req_time,
45 $resp_address, $roundtrip_time,
46 );
47 return;
48 }
49
50 # Otherwise the timeout period has ended.
51 printf(
52 "ping to %-15.15s is done.\n", $req_address,
53 );
54 }
55
56 or
57
58 use POE::Component::Client::Ping ":const";
59
60 # Post an array ref as the callback to get data back to you
61 $kernel->post("pinger", "ping", [ "pong", $user_data ]);
62
63 # use the REQ_USER_ARGS constant to get to your data
64 sub got_pong {
65 my ($request, $response) = @_[ARG0, ARG1];
66 my $user_data = $request->[REQ_USER_ARGS];
67 ...;
68 }
69
71 POE::Component::Client::Ping is non-blocking ICMP ping client. It lets
72 several other sessions ping through it in parallel, and it lets them
73 continue doing other things while they wait for responses.
74
75 Ping client components are not proper objects. Instead of being
76 created, as most objects are, they are "spawned" as separate sessions.
77 To avoid confusion (and hopefully not cause other confusion), they must
78 be spawned with a "spawn" method, not created anew with a "new" one.
79
80 PoCo::Client::Ping's "spawn" method takes a few named parameters:
81
82 Alias => $session_alias
83 "Alias" sets the component's alias. It is the target of post()
84 calls. See the synopsis. The alias defaults to "pinger".
85
86 Socket => $raw_socket
87 "Socket" allows developers to open an existing raw socket rather than
88 letting the component attempt opening one itself. If omitted, the
89 component will create its own raw socket.
90
91 This is useful for people who would rather not perform a security
92 audit on POE, since it allows them to create a raw socket in their
93 own code and then run POE at reduced privileges.
94
95 Timeout => $ping_timeout
96 "Timeout" sets the default amount of time (in seconds) a Ping
97 component will wait for a single ICMP echo reply before retrying. It
98 is 1 by default. It is possible and meaningful to set the timeout to
99 a fractional number of seconds.
100
101 This default timeout is only used for ping requests that don't
102 include their own timeouts.
103
104 Retry => $ping_attempts
105 "Retry" sets the default number of attempts a ping will be sent
106 before it should be considered failed. It is 1 by default.
107
108 OneReply => 0|1
109 Set "OneReply" to prevent the Ping component from waiting the full
110 timeout period for replies. Normally the ICMP protocol allows for
111 multiple replies to a single request, so it's proper to wait for late
112 responses. This option disables the wait, ending the ping
113 transaction at the first response. Any subsequent responses will be
114 silently ignored.
115
116 "OneReply" is disabled by default, and a single successful request
117 will generate at least two responses. The first response is a
118 successful ICMP ECHO REPLY event. The second is an undefined
119 response event, signifying that the timeout period has ended.
120
121 A ping request will generate exactly one reply when "OneReply" is
122 enabled. This reply will represent either the first ICMP ECHO REPLY
123 to arrive or that the timeout period has ended.
124
125 Parallelism => $limit
126 Parallelism sets POE::Component::Client::Ping's maximum number of
127 simultaneous ICMP requests. Higher numbers speed up the processing
128 of large host lists, up to the point where the operating system or
129 network becomes oversaturated and begin to drop packets.
130
131 The difference can be dramatic. A tuned Parallelism can enable
132 responses down to 1ms, depending on the network, although it will
133 take longer to get through the hosts list.
134
135 Pinging 762 hosts at Parallelism=64
136 Starting to ping hosts.
137 Pinged 10.0.0.25 - Response from 10.0.0.25 in 0.002s
138 Pinged 10.0.0.200 - Response from 10.0.0.200 in 0.003s
139 Pinged 10.0.0.201 - Response from 10.0.0.201 in 0.001s
140
141 real 1m1.923s
142 user 0m2.584s
143 sys 0m0.207s
144
145 Responses will take significantly longer with an untuned Parallelism,
146 but the total run time will be quicker.
147
148 Pinging 762 hosts at Parallelism=500
149 Starting to ping hosts.
150 Pinged 10.0.0.25 - Response from 10.0.0.25 in 3.375s
151 Pinged 10.0.0.200 - Response from 10.0.0.200 in 1.258s
152 Pinged 10.0.0.201 - Response from 10.0.0.201 in 2.040s
153
154 real 0m13.410s
155 user 0m6.390s
156 sys 0m0.290s
157
158 Excessively high parallelism values may saturate the OS or network,
159 resulting in few or no responses.
160
161 Pinging 762 hosts at Parallelism=1000
162 Starting to ping hosts.
163
164 real 0m20.520s
165 user 0m7.896s
166 sys 0m0.297s
167
168 By default, POE::Component::Client::Ping will guess at an optimal
169 Parallelism value based on the raw socket receive buffer size and the
170 operating system's nominal ICMP packet size. The latter figure is
171 3000 octets for Linux and 100 octets for other systems. ICMP packets
172 are generally under 90 bytes, but operating systems may use
173 alternative numbers when calculating buffer capacities. The
174 component tries to mimic calculations observed in the wild.
175
176 When in doubt, experiment with different Parallelism values and use
177 the one that works best.
178
179 BufferSize => $bytes
180 If set, then the size of the receive buffer of the raw socket will be
181 modified to the given value. The default size of the receive buffer
182 is operating system dependent. If the buffer cannot be set to the
183 given value, a warning will be generated but the system will continue
184 working. Note that if the buffer is set too small and too many ping
185 replies arrive at the same time, then the operating system may
186 discard the ping replies and mistakenly cause this component to
187 believe the ping to have timed out. In this case, you will typically
188 see discards being noted in the counters displayed by 'netstat -s'.
189
190 Increased BufferSize values can expand the practical limit for
191 Parallelism.
192
193 AlwaysDecodeAddress => 0|1
194 If set, then any input addresses will always be looked up, even if
195 the hostname happens to be only 4 characters in size. Ideally, you
196 should be passing addresses in to the system to avoid slow hostname
197 lookups, but if you must use hostnames and there is a possibility
198 that you might have short hostnames, then you should set this.
199
200 Payload => $bytes
201 Sets the ICMP payload (data bytes). Otherwise the component
202 generates 56 data bytes internally. Note that some firewalls will
203 discard ICMP packets with nonstandard payload sizes.
204
205 Sessions communicate asynchronously with the Client::Ping component.
206 They post ping requests to it, and they receive pong events back.
207
208 Requests are posted to the component's "ping" handler. They include
209 the name of an event to post back, an address to ping, and an optional
210 amount of time to wait for responses. The address may be a numeric
211 dotted quad, a packed inet_aton address, or a host name. Host names
212 are not recommended: they must be looked up for every ping request, and
213 DNS lookups can be very slow. The optional timeout overrides the one
214 set when "spawn" is called.
215
216 Ping responses come with two array references:
217
218 my ($request, $response) = @_[ARG0, ARG1];
219
220 $request contains information about the original request:
221
222 my (
223 $req_address, $req_timeout, $req_time, $req_user_args,
224 ) = @$request;
225
226 $req_address
227 This is the original request address. It matches the address posted
228 along with the original "ping" request.
229
230 It is useful along with $req_user_args for pairing requests with
231 their corresponding responses.
232
233 $req_timeout
234 This is the original request timeout. It's either the one passed
235 with the "ping" request or the default timeout set with "spawn".
236
237 $req_time
238 This is the time that the "ping" event was received by the Ping
239 component. It is a real number based on the current system's time()
240 epoch.
241
242 $req_user_args
243 This is a scalar containing arbitrary data that can be sent along
244 with a request. It's often used to provide continuity between
245 requests and their responses. $req_user_args may contain a reference
246 to some larger data structure.
247
248 To use it, replace the response event with an array reference in the
249 original request. The array reference should contain two items: the
250 actual response event and a scalar with the context data the program
251 needs back. See the SYNOPSIS for an example.
252
253 $response contains information about the ICMP ping response. There may
254 be multiple responses for a single request.
255
256 my ($response_address, $roundtrip_time, $reply_time, $reply_ttl) =
257 @$response;
258
259 $response_address
260 This is the address that responded to the ICMP echo request. It may
261 be different than $request_address, especially if the request was
262 sent to a broadcast address.
263
264 $response_address will be undefined if $request_timeout seconds have
265 elapsed. This marks the end of responses for a given request.
266 Programs can assume that no more responses will be sent for the
267 request address. They may use this marker to initiate another ping
268 request.
269
270 $roundtrip_time
271 This is the number of seconds that elapsed between the ICMP echo
272 request's transmission and its corresponding response's receipt.
273 It's a real number. This is purely the trip time and does *not*
274 include any time spent queueing if the system's parallelism limit
275 caused the ping transmission to be delayed.
276
277 $reply_time
278 This is the time when the ICMP echo response was received. It is a
279 real number based on the current system's time() epoch.
280
281 $reply_ttl
282 This is the ttl for the echo response packet we received.
283
284 If the ":const" tagset is imported the following constants will be
285 exported:
286
287 REQ_ADDRESS, REQ_TIMEOUT, REQ_TIME REQ_USER_ARGS, RES_ADDRESS,
288 RES_ROUNDTRIP, RES_TIME, RES_TTL
289
291 This component's ICMP ping code was lifted from Net::Ping, which is an
292 excellent module when you only need to ping one host at a time.
293
294 See POE, of course, which includes a lot of documentation about how POE
295 works.
296
297 Also see the test program, t/01_ping.t, in the component's
298 distribution.
299
301 https://rt.cpan.org/Dist/Display.html?Queue=POE-Component-Client-Ping
302
304 http://github.com/rcaputo/poe-component-client-ping/
305
307 http://search.cpan.org/dist/POE-Component-Client-Ping/
308
310 POE::Component::Client::Ping is Copyright 1999-2020 by Rocco Caputo.
311 All rights are reserved. POE::Component::Client::Ping is free
312 software; you may redistribute it and/or modify it under the same terms
313 as Perl itself.
314
315 You can learn more about POE at http://poe.perl.org/
316
317
318
319perl v5.38.0 2023-07-21 POE::Component::Client::Ping(3)