1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | AnyEvent::Porttracker - Porttracker/PortIQ API client interface. |
3 | AnyEvent::Porttracker - Porttracker API client interface. |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use AnyEvent::Porttracker; |
7 | use AnyEvent::Porttracker; |
|
|
8 | |
|
|
9 | my $api = new AnyEvent::Porttracker |
|
|
10 | host => "10.0.0.1", |
|
|
11 | user => "admin", |
|
|
12 | pass => "31331", |
|
|
13 | tls => 1, |
|
|
14 | on_error => sub { |
|
|
15 | die $_[1]; |
|
|
16 | }, |
|
|
17 | ; |
|
|
18 | |
|
|
19 | # Example 1 |
|
|
20 | # a simple request: ping the server synchronously |
|
|
21 | |
|
|
22 | my ($timestamp, $pid) = $api->req_sync ("ping"); |
|
|
23 | |
|
|
24 | # Example 2 |
|
|
25 | # find all realms, start a discovery on all of them |
|
|
26 | # and wait until all discovery processes have finished |
|
|
27 | # but execute individual discoveries in parallel, |
|
|
28 | # asynchronously |
|
|
29 | |
|
|
30 | my $cv = AE::cv; |
|
|
31 | |
|
|
32 | $cv->begin; |
|
|
33 | # find all realms |
|
|
34 | $api->req (realm_info => ["gid", "name"], sub { |
|
|
35 | my ($api, @realms) = @_; |
|
|
36 | |
|
|
37 | # start discovery on all realms |
|
|
38 | for my $realm (@realms) { |
|
|
39 | my ($gid, $name) = @$realm; |
|
|
40 | |
|
|
41 | $cv->begin; |
|
|
42 | $api->req (realm_discover => $gid, sub { |
|
|
43 | warn "discovery for realm '$name' finished\n"; |
|
|
44 | $cv->end; |
|
|
45 | }); |
|
|
46 | } |
|
|
47 | |
|
|
48 | $cv->end; |
|
|
49 | }); |
|
|
50 | |
|
|
51 | $cv->recv; |
|
|
52 | |
|
|
53 | # Example 3 |
|
|
54 | # subscribe to realm_poll_stop events and report each occurance |
|
|
55 | |
|
|
56 | $api->req (subscribe => "realm_poll_stop", sub {}); |
|
|
57 | $api->on (realm_poll_stop_event => sub { |
|
|
58 | my ($api, $gid) = @_; |
|
|
59 | warn "this just in: poll for realm <$gid> finished.\n"; |
|
|
60 | }); |
|
|
61 | |
|
|
62 | AE::cv->recv; # wait forever |
8 | |
63 | |
9 | =head1 DESCRIPTION |
64 | =head1 DESCRIPTION |
10 | |
65 | |
11 | Porttracker (L<http://www.porttracker.com/>) is a product that (among |
66 | Porttracker (L<http://www.porttracker.com/>) is a product that (among |
12 | other things) scans switches and routers in a network and gives a coherent |
67 | other things) scans switches and routers in a network and gives a coherent |
13 | view of which end devices are connected to which switch ports on which |
68 | view of which end devices are connected to which switch ports on which |
14 | switches and routers. It also offers a JSON-based client API, for which |
69 | switches and routers. It also offers a JSON-based client API, for which |
15 | this module is an implementation. |
70 | this module is an implementation. |
16 | |
71 | |
17 | In addition to Porttracker, the PortIQ product is also supported, as it |
|
|
18 | uses the same protocol. |
|
|
19 | |
|
|
20 | If you do not have access to either a Porttracker or PortIQ box then this |
72 | If you do not have access to a Porttracker box then this module will be of |
21 | module will be of little value to you. |
73 | little value to you. |
22 | |
74 | |
23 | This module is an L<AnyEvent> user, you need to make sure that you use and |
75 | This module is an L<AnyEvent> user, you need to make sure that you use and |
24 | run a supported event loop. |
76 | run a supported event loop. |
25 | |
77 | |
26 | To quickly understand how this module works you should read how to |
78 | To quickly understand how this module works you should read how to |
… | |
… | |
42 | |
94 | |
43 | package AnyEvent::Porttracker; |
95 | package AnyEvent::Porttracker; |
44 | |
96 | |
45 | use common::sense; |
97 | use common::sense; |
46 | |
98 | |
|
|
99 | use Carp (); |
47 | use Scalar::Util (); |
100 | use Scalar::Util (); |
48 | |
101 | |
49 | use AnyEvent (); |
102 | use AnyEvent (); |
50 | use AnyEvent::Handle (); |
103 | use AnyEvent::Handle (); |
51 | |
104 | |
52 | use MIME::Base64 (); |
105 | use MIME::Base64 (); |
53 | use Digest::HMAC_MD6 (); |
106 | use Digest::HMAC_MD6 (); |
54 | use JSON (); |
107 | use JSON (); |
55 | |
108 | |
56 | our $VERSION = '0.0'; |
109 | our $VERSION = '1.01'; |
57 | |
110 | |
58 | sub call { |
111 | sub call { |
59 | my ($self, $type, @args) = @_; |
112 | my ($self, $type, @args) = @_; |
60 | |
113 | |
61 | $self->{$type} |
114 | $self->{$type} |
… | |
… | |
99 | |
152 | |
100 | Enables or disables TLS (default: disables). When enabled, then the |
153 | Enables or disables TLS (default: disables). When enabled, then the |
101 | connection will try to handshake a TLS connection before logging in. If |
154 | connection will try to handshake a TLS connection before logging in. If |
102 | unsuccessful a fatal error will be raised. |
155 | unsuccessful a fatal error will be raised. |
103 | |
156 | |
104 | Since most Porttracker/PortIQ boxes will not have a sensible/verifiable |
157 | Since most Porttracker boxes will not have a sensible/verifiable |
105 | certificate, no attempt at verifying it will be done (which means |
158 | certificate, no attempt at verifying it will be done (which means |
106 | man-in-the-middle-attacks will be trivial). If you want some form of |
159 | man-in-the-middle-attacks will be trivial). If you want some form of |
107 | verification you need to provide your own C<tls_ctx> object with C<< |
160 | verification you need to provide your own C<tls_ctx> object with C<< |
108 | verify => 1, verify_peername => [1, 1, 1] >> or whatever verification mode |
161 | verify => 1, verify_peername => [1, 1, 1] >> or whatever verification mode |
109 | you wish to use. |
162 | you wish to use. |
110 | |
163 | |
111 | =item tls_ctx => $tls_ctx |
164 | =item tls_ctx => $tls_ctx |
112 | |
165 | |
113 | The L<AnyEvent::TLS> object to use. |
166 | The L<AnyEvent::TLS> object to use. See C<tls>, above. |
114 | |
|
|
115 | #TODO# |
|
|
116 | |
167 | |
117 | =item on_XYZ => $coderef |
168 | =item on_XYZ => $coderef |
118 | |
169 | |
119 | You can specify event callbacks either by subclassing and overriding the |
170 | You can specify event callbacks either by sub-classing and overriding the |
120 | respective methods or by specifying coderefs as key-value pairs when |
171 | respective methods or by specifying code-refs as key-value pairs when |
121 | constructing the object. |
172 | constructing the object. You add or remove event handlers at any time with |
|
|
173 | the C<event> method. |
122 | |
174 | |
123 | =back |
175 | =back |
124 | |
176 | |
125 | =cut |
177 | =cut |
126 | |
178 | |
… | |
… | |
128 | my $class = shift; |
180 | my $class = shift; |
129 | |
181 | |
130 | my $self = bless { |
182 | my $self = bless { |
131 | id => "a", |
183 | id => "a", |
132 | ids => [], |
184 | ids => [], |
133 | queue => [], # ininitially queue everything |
185 | queue => [], # initially queue everything |
134 | @_, |
186 | @_, |
135 | }, $class; |
187 | }, $class; |
136 | |
188 | |
137 | { |
189 | { |
138 | Scalar::Util::weaken (my $self = $self); |
190 | Scalar::Util::weaken (my $self = $self); |
… | |
… | |
260 | $_[0]{queue} |
312 | $_[0]{queue} |
261 | ? push @{ $_[0]{queue} }, [@_] |
313 | ? push @{ $_[0]{queue} }, [@_] |
262 | : &_req |
314 | : &_req |
263 | } |
315 | } |
264 | |
316 | |
|
|
317 | =item @res = $api->req_sync ($type => @args) |
|
|
318 | |
|
|
319 | Similar to C<< ->req >>, but waits for the results of the request and on |
|
|
320 | success, returns the values instead (without the success flag, and only |
|
|
321 | the first value in scalar context). On failure, the method will C<croak> |
|
|
322 | with the error message. |
|
|
323 | |
|
|
324 | =cut |
|
|
325 | |
|
|
326 | sub req_sync { |
|
|
327 | push @_, my $cv = AE::cv; |
|
|
328 | &req; |
|
|
329 | my ($ok, @res) = $cv->recv; |
|
|
330 | |
|
|
331 | $ok |
|
|
332 | or Carp::croak $res[0]; |
|
|
333 | |
|
|
334 | wantarray ? @res : $res[0] |
|
|
335 | } |
|
|
336 | |
265 | =item $api->req_failok ($type => @args, $callback->($api, $success, @reply)) |
337 | =item $api->req_failok ($type => @args, $callback->($api, $success, @reply)) |
266 | |
338 | |
267 | Just like C<< ->req >>, with two differences: first, a failure will not |
339 | Just like C<< ->req >>, with two differences: first, a failure will not |
268 | raise an error, second, the initial status reply which indicates success |
340 | raise an error, second, the initial status reply which indicates success |
269 | or failure is not removed before calling the callback. |
341 | or failure is not removed before calling the callback. |
… | |
… | |
272 | |
344 | |
273 | sub req_failok { |
345 | sub req_failok { |
274 | $_[0]{queue} |
346 | $_[0]{queue} |
275 | ? push @{ $_[0]{queue} }, [@_] |
347 | ? push @{ $_[0]{queue} }, [@_] |
276 | : &_req |
348 | : &_req |
|
|
349 | } |
|
|
350 | |
|
|
351 | =item $api->on (XYZ => $callback) |
|
|
352 | |
|
|
353 | Overwrites any currently registered handler for C<on_XYZ> or |
|
|
354 | installs a new one. Or, when C<$callback> is undef, unregisters any |
|
|
355 | currently-registered handler. |
|
|
356 | |
|
|
357 | Example: replace/set the handler for C<on_discover_stop_event>. |
|
|
358 | |
|
|
359 | $api->on (discover_stop_event => sub { |
|
|
360 | my ($api, $gid) = @_; |
|
|
361 | ... |
|
|
362 | }); |
|
|
363 | |
|
|
364 | =cut |
|
|
365 | |
|
|
366 | sub on { |
|
|
367 | my $self = shift; |
|
|
368 | |
|
|
369 | while (@_) { |
|
|
370 | my ($event, $cb) = splice @_, 0, 2; |
|
|
371 | $event =~ s/^on_//; |
|
|
372 | |
|
|
373 | $self->{"on_$event"} = $cb; |
|
|
374 | } |
277 | } |
375 | } |
278 | |
376 | |
279 | sub on_start_tls_notify { |
377 | sub on_start_tls_notify { |
280 | my ($self) = @_; |
378 | my ($self) = @_; |
281 | |
379 | |
… | |
… | |
379 | |
477 | |
380 | $msg =~ s/\n$//; |
478 | $msg =~ s/\n$//; |
381 | $self->error ("login failed: $msg"); |
479 | $self->error ("login failed: $msg"); |
382 | } |
480 | } |
383 | |
481 | |
|
|
482 | sub on_event_notify { |
|
|
483 | my ($self, $event, @args) = @_; |
|
|
484 | |
|
|
485 | call $self, "on_${event}_event", @args; |
|
|
486 | } |
|
|
487 | |
384 | =back |
488 | =back |
385 | |
489 | |
386 | =head2 EVENTS |
490 | =head1 EVENTS/CALLBACKS |
387 | |
491 | |
388 | AnyEvent::Porttracker conenctions are fully event-driven, and naturally |
492 | AnyEvent::Porttracker connections are fully event-driven, and naturally |
389 | there are a number of events that can occur. All these events have a name |
493 | there are a number of events that can occur. All these events have a name |
390 | starting with C<on_> (example: C<on_login_failure>). |
494 | starting with C<on_> (example: C<on_login_failure>). |
391 | |
495 | |
392 | Programs can catch these events in two ways: either by providing |
496 | Programs can catch these events in two ways: either by providing |
393 | constructor arguments with the event name as key and a coderef as value: |
497 | constructor arguments with the event name as key and a code-ref as value: |
394 | |
498 | |
395 | my $api = new AnyEvent::Porttracker |
499 | my $api = new AnyEvent::Porttracker |
396 | host => ..., |
500 | host => ..., |
397 | user => ..., pass => ..., |
501 | user => ..., pass => ..., |
398 | on_error => sub { |
502 | on_error => sub { |
… | |
… | |
400 | warn $msg; |
504 | warn $msg; |
401 | exit 1; |
505 | exit 1; |
402 | }, |
506 | }, |
403 | ; |
507 | ; |
404 | |
508 | |
405 | Or by subclassing C<AnyEvent::Porttracker> and overriding methods of the |
509 | Or by sub-classing C<AnyEvent::Porttracker> and overriding methods of the |
406 | same name: |
510 | same name: |
407 | |
511 | |
408 | package MyClass; |
512 | package MyClass; |
409 | |
513 | |
410 | use base AnyEvent::Porttracker; |
514 | use base AnyEvent::Porttracker; |
… | |
… | |
458 | |
562 | |
459 | =item on_start_tls_notify $api |
563 | =item on_start_tls_notify $api |
460 | |
564 | |
461 | Called when the server wants to start TLS negotiation. This is used |
565 | Called when the server wants to start TLS negotiation. This is used |
462 | internally and - while it is possible to override it - should not be |
566 | internally and - while it is possible to override it - should not be |
463 | overriden. |
567 | overridden. |
|
|
568 | |
|
|
569 | =item on_event_notify $api, $eventname, @args |
|
|
570 | |
|
|
571 | Called when the server broadcasts an event the API object is subscribed |
|
|
572 | to. The default implementation (which should not be overridden) simply |
|
|
573 | re-issues an "on_eventname_event" event with the @args. |
464 | |
574 | |
465 | =item on_XYZ_notify $api, ... |
575 | =item on_XYZ_notify $api, ... |
466 | |
576 | |
467 | In general, any protocol notification will result in an event of the form |
577 | In general, any protocol notification will result in an event of the form |
468 | C<on_NOTIFICATION_notify>. |
578 | C<on_NOTIFICATION_notify>. |
469 | |
579 | |
|
|
580 | =item on_XYZ_event $api, ... |
|
|
581 | |
|
|
582 | Called when the server broadcasts the named (XYZ) event. |
|
|
583 | |
470 | =back |
584 | =back |
471 | |
585 | |
472 | =head1 SEE ALSO |
586 | =head1 SEE ALSO |
473 | |
587 | |
474 | L<AnyEvent>, L<http://www.porttracker.com/>, L<http://www.infoblox.com/en/products/portiq.html>. |
588 | L<AnyEvent>, L<http://www.porttracker.com/>. |
475 | |
589 | |
476 | =head1 AUTHOR |
590 | =head1 AUTHOR |
477 | |
591 | |
478 | Marc Lehmann <marc@porttracker.net> |
592 | Marc Lehmann <marc@nethype.de> |
479 | |
593 | |
480 | =cut |
594 | =cut |
481 | |
595 | |
482 | 1 |
596 | 1 |