blib/lib/COPS/Client.pm untested

filter: Statementstmt 0.0% 0 / 1307 Branchbran 0.0% 0 / 410 Conditioncond 0.0% 0 / 33 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 102 Podpod 0.0% 0 / 97 totaltotal 0.0% 0 / 1949 SCAR 106.6
CC
206
Cov
0%
check_data_available
2550.0
get_listener_connect
342.0
encode_sub_traffic
182.0
CRAP 42642.0
1 package COPS::Client;
2  
3 use warnings;
4 use strict;
5 use IO::Select;
6 use IO::Socket;
7 use IO::Handle;
8  
9 =head1 NAME
10  
11 COPS::Client - COPS Protocol - Packet Cable Client
12  
13 =head1 VERSION
14  
15 Version 0.05
16  
17 =cut
18  
19 our $VERSION = '0.05';
20  
21 =head1 SYNOPSIS
22  
23 This module provides a simple COPS client for managing Packet Cable Multi
24 Media sessions for CMTS. It should provide all the neccessary functionality to
25 enable a service provider to deploy, manage and control service flows within
26 their network. This does not maintain a connection to the CMTS but issue the
27 configured command, get the response and then close the TCP connection. I am
28 working on a stateful Client however this is not yet available.
29  
30 As basic initial use of the module is as follows
31  
32 my $cops_client = new COPS::Client (
33 [
34 ServerIP => '192.168.0.1',
35 ServerPort => '3918',
36 Timeout => 2,
37 DataHandler => \&display_data
38 ]
39 );
40  
41 $cops_client->set_command("set");
42 $cops_client->subscriber_set("ipv4","172.20.1.1");
43  
44 $cops_client->gate_specification_add(
45 [
46 Direction => 'Downstream',
47 DSCPToSMark => 0,
48 Priority => 0,
49 PreEmption => 0,
50 Gate_Flags => 0,
51 Gate_TOSField => 0,
52 Gate_TOSMask => 0,
53 Gate_Class => 0,
54 Gate_T1 => 0,
55 Gate_T2 => 0,
56 Gate_T3 => 0,
57 Gate_T4 => 0
58 ]
59 );
60  
61 $cops_client->classifier_add(
62 [
63 Classifier_Type => 'Classifier',
64 Classifier_Priority => 64,
65 Classifier_SourceIP => "172.20.1.1",
66 ]
67 );
68  
69 $cops_client->envelope_add (
70 [
71 Envelope_Type => "authorize,reserve,commit",
72 Service_Type => 'DOCSIS Service Class Name',
73 ServiceClassName => 'S_down'
74 ]
75 );
76  
77 $cops_client->connect();
78 $cops_client->check_data_available();
79  
80 This will connect to a CMTS on IP 192.168.0.1 and apply a PCMM gate to
81 the subscriber with IP address 172.20.1.1 and apply the service class S_down.
82  
83 It should be noted not all CMTS support all the functions available, so
84 if the COPS request is failing for you remove opaque_set, timebase_set or
85 volume_set and try again.
86  
87 You may also get a very cryptic error if an envelope or classifier is
88 incorrectly configured.
89  
90 =head1 EXPORT
91  
92 There are no exports.
93  
94 =head1 FUNCTIONS
95  
96 new
97 set_command
98 subscriber_set
99 gate_specification_add
100 classifier_add
101 envelope_add
102 rks_set
103 decode_radius_attribute
104 volume_set
105 timebase_set
106 opaque_set
107  
108 =head2 new
109  
110 The new function initialises the module and sets the CMTS IP, Port and
111 the data handling function which gets called if a RTP message is received.
112  
113 The parameters are
114  
115 ServerIP - The IP address of the CMTS to connect to
116  
117 ServerPort - The port that the Packet Cable service is running
118 on. There is no default value however most server
119 implementations use port 3918, so this should be
120 set to that
121  
122 Timeout - This is a timeout parameter for the connection to the
123 CMTS. It has a default of 5 so can be omitted.
124  
125 DataHandler - This should be set to point to a local function to
126 handle any data returned by a COPS message sent.
127  
128 The function will accept 2 variables as input the
129 first is the module point, the second is a point to
130 a hash containing any data returned.
131  
132 An example of use would be.
133  
134 my $cops_client = new COPS::Client (
135 [
136 ServerIP => '192.168.0.1',
137 ServerPort => '3918',
138 Timeout => 2,
139 DataHandler => \&display_data
140 ]
141 );
142  
143 sub display_data
144 {
145 my ( $self ) = shift;
146 my ( $data ) = shift;
147 print "Report Datagram sent.\n\n";
148 foreach my $name ( sort { $a cmp $b } keys %{$data} )
149 {
150 print "Attribute Name is '$name' value is '${$data}{$name}'\n";
151 }
152 }
153  
154 =head2 set_command
155  
156 This command sets the type of command to be sent in the connection. It
157 can be one of 4 types as follows
158  
159 set - Meaning GateSet
160 delete - Meaning GateDelete
161 info - Meaning GateInfo
162 synch - Meaning Synch Request
163  
164 An example of use is
165  
166 $cops_client->set_command ( "set" );
167  
168 The command specified must match *Exactly* otherwise it will be ignored. It
169 appears Cisco CMTS do NOT respond to Synch requests. Cisco have been asked
170 to respond to this query but no information has been forthcoming.
171  
172 =head2 subscriber_set
173  
174 This function sets the subscriber ID to be used. The subcriber ID can be
175 either an IPV4 or IPV6 address. If you use an IPV6 address it *MUST* be
176 fully qualified.
177  
178 The function takes two parameters the first specifies which IPVx to use,
179 the second is the IPVx value.
180  
181 An example of use is
182  
183 $cops_client->subscriber_set("ipv4","172.20.1.1");
184  
185 The subscriber ID is required for 99% of all COPS messages.
186  
187 =head2 gate_specification_add
188  
189 This function builds a gate with the attributes specified. Possible
190 attributes are
191  
192 Direction - This can be 'Upstream' or 'Downstream' only.
193 If specified this overrides Gate_Flags as
194 direction is one bit of the Gate_Flags
195 parameter.
196  
197 Priority - This is a value of 0 to 7. If specified this
198 overrides Gate_Class as Priority is 3 bits
199 of that parameter.
200  
201 PreEmption - This has a value of 0 or 1. This allows this
202 gate to take bandwidth from any other gates
203 already set against this subscriber. If
204 specified this overrides Gate_Class as this is
205 1 bit of that parameter.
206  
207 DSCPToSMark - This has a value of 0 or 1
208  
209 Priority - This has a value between 0 and 255 and should
210 determine the priority of the gate.
211  
212 Gate_Flags - This field is broken down into 2 used bits and
213 6 unused bits.
214  
215 Bit 0 - Direction.
216 0 is Downstream
217 1 is Upstream
218
219 If you use the Direction parameter
220 this is set for you.
221  
222 Bit 1 - DSCP/TOS Field
223 0 is enable
224 1 is overwrite
225  
226 GateTOSField - IP TOS and Precedence value.
227  
228 GateTOSMask - IP TOS Mask settings
229  
230 GateClass - This field is broken down into 8 bits as follows
231
232 Bit 0-2 - Priority of 0-7
233 Bit 3 - PreEmption bit
234 Bit 4-7 - Configurable but should default 0
235  
236 Gate_T1 - Gate T1 timer
237  
238 Gate_T2 - Gate T2 timer
239  
240 Gate_T3 - Gate T3 timer
241  
242 Gate_T4 - Gate T4 timer
243  
244 An example of use would be
245  
246 $cops_client->gate_specification_add(
247 [
248 Direction => 'Downstream',
249 DSCPToSMark => 0,
250 Priority => 0,
251 PreEmption => 0,
252 Gate_Flags => 0,
253 Gate_TOSField => 0,
254 Gate_TOSMask => 0,
255 Gate_Class => 0,
256 Gate_T1 => 0,
257 Gate_T2 => 0,
258 Gate_T3 => 0,
259 Gate_T4 => 0
260 ]
261 );
262  
263 =head2 classifier_add
264  
265 This function adds a classifier to the COPS request being sent and
266 supports normal and extended classifiers.
267  
268 The function requires two types of parameters depending on the type
269 of classifier specified.
270  
271 To specify the correct classifier the attribute Classifier_Type
272 can be used as follows
273  
274 Classifier_Type - This should be 'Classifier' or 'Extended'
275  
276 Classifier_Type 'Classifier' attributes are as follows
277  
278 Classifier_IPProtocolId - This is a standard IP protocol
279 number. You can set this to 0
280 or omit this and a default of 0
281 will be used.
282  
283 Classifier_TOSField - The TOSField of the IP packets
284 to match. You can set this to 0
285 or omit this and a default of 0
286 will be used.
287  
288 Classifier_TOSMask - The TOSMask of the IP packets
289 to match. you can set this to 0
290 or omit this and a default of 0
291 will be used.
292  
293 Classifier_SourceIP - This should be set to the source
294 IP address of the associated flow.
295 If you have a device attached to
296 the cable modem such as a PC, then
297 you should use the IP of that
298 device, not that of the cable modem.
299  
300 Classifier_DestinationIP - This is the destination IP of the
301 flow. It can be a wildcard of 0.
302 If you omit this then a default 0
303 will be used.
304  
305 Classifier_SourcePort - The source port of the flow. If
306 you omit this then a default of 0
307 will be used.
308  
309 Classifier_DestinationPort - This is the destination port of the
310 flow. If you omit this then a default
311 if 0 will be used.
312  
313 Classifier_Priority - The priority of this Classifier. If
314 you have multiple Classifiers then
315 this determines the order they are
316 checked.
317  
318 An example of use would be
319  
320 $cops_client->classifier_add(
321 [
322 Classifier_Type => 'Classifier',
323 Classifier_Priority => 64,
324 Classifier_SourceIP => "172.20.1.1",
325 ]
326 );
327  
328 This sets up a Standard classifier with a priority of 64, Source IP of
329 172.20.1.1,any port and a wildcard destination address any port.
330  
331 Classifier_Type 'Extended' attributes are as follows
332  
333 EClassifier_IPProtocolId - This is a standard IP protocol
334 number. You can set this to 0
335 or omit this and a default of 0
336 will be used.
337  
338 EClassifier_TOSField - The TOSField of the IP packets
339 to match. You can set this to 0
340 or omit this and a default of 0
341 will be used.
342  
343 EClassifier_TOSMask - The TOSMask of the IP packets
344 to match. you can set this to 0
345 or omit this and a default of 0
346 will be used.
347  
348 EClassifier_SourceIP - This should be set to the source
349 IP address of the associated flow.
350 If you have a device attached to
351 the cable modem such as a PC, then
352 you should use the IP of that
353 device, not that of the cable modem.
354 With an extended classifier you can
355 also specify a network address.
356  
357 EClassifier_SourceMask - This is the associated netmask for
358 the SourceIP specified.
359  
360 EClassifier_DestinationIP - This is the destination IP of the
361 flow. It can be a wildcard of 0.
362 If you omit this then a default 0
363 will be used.
364 With an extended classifier you can
365 also specify a network address.
366  
367 EClassifier_DestinationMask - This is the associated netmask for
368 the DestinationIP specified.
369  
370 EClassifier_SourcePortStart - The start source port of the flow.
371 If you omit this then a default of 0
372 will be used.
373  
374 EClassifier_SourcePortEnd - The end source port of the flow. If
375 both the start and end ports are 0
376 then all ports are matched.
377  
378 EClassifier_DestinationPortStart - The start destination port of the
379 flow. If you omit this then a default
380 of 0 will be used.
381  
382 EClassifier_DestinationPortEnd - The end destination port of the flow.
383 If both the start and end ports are 0
384 then all ports are matched.
385  
386 EClassifier_ClassifierID - An extended classifier must have numerical
387 ID and it should unique per classifier per
388 gate. It can range from 1 to 65535 (16bit)
389  
390 EClassifier_Priority - The priority of this Classifier. If
391 you have multiple Classifiers then
392 this determines the order they are
393 checked.
394  
395 EClassifier_State - This determines if this classifier is
396 Inactive or Active, values 0 and 1
397 respectively.
398  
399 EClassifier_Action - This has 4 possible values
400  
401 0 - Means Add - This is the default if not
402 specified.
403 1 - Replace
404 2 - Delete
405 3 - No Change
406  
407 An example of use would be
408  
409 $cops_client->classifier_add(
410 [
411 Classifier_Type => 'Extended',
412 EClassifier_Priority => 64,
413 EClassifier_SourceIP => "172.20.1.1",
414 EClassifier_ClassifierID => 100,
415 EClassifier_State => 1
416 ]
417 );
418  
419 This sets up an Extended classifier with a priority of 64, Source IP of
420 172.20.1.1,any port and a wildcard destination address any port. The ID is set
421 to 100 and it is set to State 1 which is Active.
422  
423 =head2 envelope_add
424  
425 This function adds the correct envelope type to the gate. All the possible parameters
426 can not be detailed here as it would this man page *VERY* long. I may add them in the
427 future.
428  
429 The Attributes that are *ALWAYS* required at
430  
431 Envelope_Type - This specifies the type of request and is managed
432 by three bits (LSB first), lowest value 1 highest
433 value 7
434  
435 0 - Authorize - Value 1
436 1 - Reserve - Value 2
437 2 - Commit - Value 4
438  
439 This is a string and should be one/more of the following
440  
441 authorize reserve commit
442  
443 Service_Type - This determines the type of service that the gate
444 will apply. By specifying the Service_Type and
445 Envelope_Type this determines the additional
446 parameters required.
447  
448 The following values are valid for Service_Type
449  
450 Flow Spec
451 DOCSIS Service Class Name
452 Best Effort Service
453 Non-Real-Time Polling Service
454 Real-Time Polling Service
455 Unsolicited Grant Service
456 Unsolicited Grant Service with Activity Detection
457 Downstream
458  
459 There is an example of each one in the examples
460 directory examples/profiles/
461
462 An example of use would be
463  
464 $cops_client->envelope_add (
465 [
466 Envelope_Type => "authorize reserve commit",
467 Service_Type => 'DOCSIS Service Class Name',
468 ServiceClassName => 'S_down'
469 ]
470 );
471  
472 This sets up the Envelope to be authorized, reserved and committed. It contains a Service
473 Class Name (this should be configured on the CMTS already) and it has been named as
474 S_down. If the specified ServiceClassName is incorrect or does not correspond to the
475 direction specified an error will be returned.
476  
477 =head2 rks_set
478  
479 This function add a Reporting server to the COPS request. You can have a primary and
480 secondary Reporting server and events, such as volume quota reached, time reached should
481 be report to the Reporting server configured. All Reporting server messages are via
482 the RADIUS protocol. This rks_set only supports IPV4 addressing.
483  
484 As part of a RKS request you can also specify unique indentifiers that will be sent in
485 the Reporting request for each specific gate created. The Gate ID is not sent in the
486 reporting request so some external management system will need to track these.
487  
488 The variables you can set in an RKS configuration are as follows
489  
490 PRKS_IPAddress - This is the PRIMARY (PRKS) reporting server IP address.
491 It should be specified as an IP, hostnames are not
492 supported and only IPV4 is available.
493  
494 PRKS_Port - This is the Port that reporting messages are sent to.
495 The protocol used is RADIUS so the standard 1813 port
496 should be used if a default RADIUS server configuration
497 is to be used.
498  
499 PRKS_Flags - Ignore, further work is required, however if you
500 understand this usage it is available to be set.
501  
502 SRKS_IPAddress - This is the SECONDARY (SRKS) reporting server IP address.
503 This is ONLY used if the primary is considered down.
504 It should be specified as an IP, hostnames are not
505 supported and only IPV4 is available.
506  
507 SRKS_Port - This is the Port that reporting messages are sent to
508 for the SECONDARY reporting server.
509  
510 SRKS_Flags - Ignore, further work is required, however if you
511 understand this usage it is available to be set.
512  
513
514 Billing Correlation Identification
515  
516 BCID_TimeStamp - This is a 32bit number and EPOCH is a good use here.
517
518 BCID_ElementID - This is an eight (8) character entry and should be
519 alphanumeric only to be supported by all vendors.
520  
521 BCID_TimeZone - This is an eight(8) character entry and specifies
522 the timezone of the entry.
523  
524 BCID_EventCounter - This is a 32bit number and can be anything within that
525 range. This could be an auto-increment in a table, so
526 allowing GateID to be linked back later.
527  
528 An example of use would be
529  
530 my $timer=time();
531  
532 $cops_client->rks_set (
533 [
534 PRKS_IPAddress => '192.168.50.2',
535 PRKS_Port => 2000,
536 PRKS_Flags => 0,
537 SRKS_IPAddress => 0,
538 SRKS_Port => 0,
539 SRKS_Flags => 0,
540 BCID_TimeStamp => $timer,
541 BCID_ElementID => '99999999',
542 BCID_TimeZone => '00000000',
543 BCID_EventCounter => 12347890
544 ]
545 );
546  
547 You can omit fields which are not used and they will default to 0, but for completeness
548 are included above.
549
550 =head2 decode_radius_attribute
551  
552 This function takes the output from FreeRadius 2.1.9 and expands it where possible. The
553 supported attributes are
554  
555 CableLabs-Event-Message
556 CableLabs-QoS-Descriptor
557  
558 When called this function returns the converted attribute into a hash of the attributes
559 found and decoded.
560  
561 An example of use would be
562  
563 my %return_data;
564  
565 $cops_client->decode_radius_attribute("CableLabs-Event-Message",
566 "
567 0x00034c163b873939393939393939303030303030303000bc69f2000700022020203232323200312b3030303030300000002b32303130303631343135313233382e3032330000000080000400",
568 \%return_data);
569  
570 Note the 0x is required at the beginning so validity checking will pass.
571  
572 The %return_data has should then contain the following keys with values.
573  
574 EventMessageVersionID - 3
575 TimeZone - 1+000000
576 Status - 0
577 AttributeCount - 4
578 SequenceNumber - 43
579 BCID_TimeZone - 00000000
580 EventObject - 0
581 ElementType - 2
582 EventMessageType - 7
583 BCID_Timestamp - 1276525447
584 BCID_ElementID - 99999999
585 BCID_EventCounter - 12347890
586 EventMessageTypeName - QoS_Reserve
587 Priority - 128
588 ElementID - ' 2222'
589 EventTime - 20121019163303.51
590  
591 =head2 volume_set
592  
593 This functions adds a volume limit to the gate being sent. You should be aware the CMTS
594 may not stop traffic flowing through the gate when the limit is reached, implementation
595 dependent, however should send a RKS notification.
596  
597 This function just takes the Volume in the number of bytes, 64 bit number.
598  
599 An example of use would be
600  
601 $cops_client->volume_set( 3000000000 );
602
603 This would set the volume to 3Gigabytes.
604  
605 =head2 timebase_set
606  
607 This function add a time limit to the gate being sent. You should be aware the CMTS may
608 not stop traffifc flowing through the gate when the limit is reached, implementation
609 dependent, however should sent a RKS notification.
610  
611 This function just takes the time in seconds , 32bit number.
612
613 An example of use would be
614  
615 $cops_client->timebase_set( 60 );
616  
617 This would set the time limit to 60 seconds.
618  
619 =head2 opaque_set
620  
621 This function allows you to add arbitary data to the COPS message sent which *may* be
622 recorded against the gate by the remote CMTS.
623  
624 The only attribute for this function is
625  
626 OpaqueData - This be any data, although keeping it to something
627 humanly readable is probably a good idea.
628  
629 An example of use would be
630  
631 $cops_client->opaque_set(
632 [
633 OpaqueData => 'a test string'
634 ]
635 );
636  
637 This would add 'a test string' as Opaque data to the gate.
638  
639 =head2 Summary
640  
641 This is very much a 'work in progress'.
642  
643 =cut
644  
645 sub new {
646  
647 my $self = {};
648 bless $self;
649  
650 my ( $class , $attr ) =@_;
651  
652 my ( %template );
653 my ( %current_data );
654 my ( %complete_decoded_data );
655 my ( %handles );
656  
657 $self->{_GLOBAL}{'DEBUG'}=0;
658  
659 while (my($field, $val) = splice(@{$attr}, 0, 2))
660 { $self->{_GLOBAL}{$field}=$val; }
661  
662 $self->{_GLOBAL}{'STATUS'}="OK";
663  
664 if ( !$self->{_GLOBAL}{'VendorID'} )
665 { $self->{_GLOBAL}{'VendorID'}="Generic Client"; }
666  
667 if ( !$self->{_GLOBAL}{'ServerIP'} )
668 { die "ServerIP Required"; }
669  
670 if ( !$self->{_GLOBAL}{'ServerPort'} )
671 { die "ServerPort Required"; }
672  
673 if ( !$self->{_GLOBAL}{'KeepAlive'} )
674 { $self->{_GLOBAL}{'KeepAlive'}=60; }
675  
676 if ( !$self->{_GLOBAL}{'Timeout'} )
677 { $self->{_GLOBAL}{'Timeout'}=5; }
678  
679 if ( !$self->{_GLOBAL}{'ListenIP'} )
680 { $self->{_GLOBAL}{'ListenIP'}=""; }
681  
682 if ( !$self->{_GLOBAL}{'ListenPort'} )
683 { $self->{_GLOBAL}{'ListenPort'}=""; }
684  
685 if ( !$self->{_GLOBAL}{'ListenServer'} )
686 { $self->{_GLOBAL}{'ListenServer'}=0; }
687  
688 if ( !$self->{_GLOBAL}{'RemotePassword'} )
689 { $self->{_GLOBAL}{'RemotePassword'}=""; }
690  
691 if ( !$self->{_GLOBAL}{'RemoteSpeed'} )
692 { $self->{_GLOBAL}{'RemoteSpeed'}=10; }
693  
694 if ( !$self->{_GLOBAL}{'TMPDirectory'} )
695 { $self->{_GLOBAL}{'TMPDirectory'}="/tmp/"; }
696  
697 $self->{_GLOBAL}{'data_ack'}=0;
698 $self->{_GLOBAL}{'TRANSACTION_COUNT'}=1;
699 $self->{_GLOBAL}{'ERROR'}="" ;
700 $self->{_GLOBAL}{'data_processing'}=0;
701 $self->{_GLOBAL}{'current_command'}="";
702 $self->{_GLOBAL}{'Classifier_Encoded'}="";
703 $self->{_GLOBAL}{'Envelope_Encoded'}="";
704 $self->{_GLOBAL}{'TimeLimit'}="";
705 $self->{_GLOBAL}{'VolumeLimit'}="";
706 $self->{_GLOBAL}{'OpaqueData'}="";
707 $self->{_GLOBAL}{'RKS_Encoded'}="";
708  
709 $self->{_GLOBAL}{'template'}= \%template;
710 $self->{_GLOBAL}{'current_data'}= \%current_data;
711 $self->{_GLOBAL}{'complete_decoded_data'} = \%complete_decoded_data;
712  
713 $self->{_GLOBAL}{'Listener_HandlesP'}= \%handles;
714  
715 return $self;
716 }
717  
718 sub disconnect
719 {
720 my ( $self ) = shift;
721 if ( $self->{_GLOBAL}{'Handle'} )
722 {
723 $self->{_GLOBAL}{'Handle'}->close();
724 }
725 return 1;
726 }
727  
728 sub connect
729 {
730 my ( $self ) = shift;
731  
732 my $lsn = IO::Socket::INET->new
733 (
734 PeerAddr => $self->{_GLOBAL}{'ServerIP'},
735 PeerPort => $self->{_GLOBAL}{'ServerPort'},
736 ReuseAddr => 1,
737 Proto => 'tcp',
738 Timeout => $self->{_GLOBAL}{'Timeout'}
739 );
740  
741 if (!$lsn)
742 {
743 $self->{_GLOBAL}{'STATUS'}="Failed to bind to address '".$self->{_GLOBAL}{'ServerIP'}."' ";;
744 $self->{_GLOBAL}{'STATUS'}.="and port '".$self->{_GLOBAL}{'ServerPort'};
745 $self->{_GLOBAL}{'ERROR'}=$!;
746 return 0;
747 }
748  
749 $self->{_GLOBAL}{'LocalIP'}=$lsn->sockhost();
750 $self->{_GLOBAL}{'LocalPort'}=$lsn->sockport();
751 $self->{_GLOBAL}{'Handle'} = $lsn;
752 $self->{_GLOBAL}{'Selector'}=new IO::Select( $lsn );
753 $self->{_GLOBAL}{'STATUS'}="Success Connected";
754  
755 if ( $self->{_GLOBAL}{'ListenServer'} )
756 {
757 # we should do a fork here so the listener can wait for commands
758 # how we signal when data is ready not sure.
759 my $child;
760 if ($child=fork)
761 {}
762 elsif (defined $child)
763 {
764 my $lsn2 = IO::Socket::INET->new
765 (
766 Listen => 1024,
767 LocalAddr => $self->{_GLOBAL}{'ListenIP'},
768 LocalPort => $self->{_GLOBAL}{'ListenPort'},
769 ReuseAddr => 1,
770 Proto => 'tcp',
771 Timeout => $self->{_GLOBAL}{'Timeout'}
772 );
773 if ( !$lsn2)
774 {
775 $self->{_GLOBAL}{'STATUS'}="Failed to bind to address '".$self->{_GLOBAL}{'ListenIP'}."' ";
776 $self->{_GLOBAL}{'STATUS'}.="and port '".$self->{_GLOBAL}{'ListenPort'};
777 $self->{_GLOBAL}{'ERROR'}=$!;
778 exit(0);
779 }
780  
781 $self->{_GLOBAL}{'Listen_LocalIP'}=$lsn2->sockhost();
782 $self->{_GLOBAL}{'Listen_LocalPort'}=$lsn2->sockport();
783 $self->{_GLOBAL}{'Listen_Handle'} = $lsn2;
784 $self->{_GLOBAL}{'Listen_Selector'}=new IO::Select( $lsn2 );
785 $self->{_GLOBAL}{'Listen_STATUS'}="Success Connected";
786 $self->check_listeners_available();
787 exit(0);
788 }
789 }
790  
791 return 1;
792 }
793  
794 sub connect_flush
795 {
796 my ( $self ) = shift;
797 undef $self->{_GLOBAL}{'LocalIP'};
798 undef $self->{_GLOBAL}{'LocalPort'};
799 undef $self->{_GLOBAL}{'Handle'};
800 undef $self->{_GLOBAL}{'Selector'};
801 undef $self->{_GLOBAL}{'STATUS'};
802 return 1;
803 }
804  
805 sub connected
806 {
807 my ( $self ) = shift;
808 return $self->{_GLOBAL}{'Selector'};
809 }
810  
811  
812 sub check_data_handles
813 {
814 my ( $self ) = shift;
815 my ( @handle ) = $self->{_GLOBAL}{'Selector'}->can_read;
816 if ( !@handle ) { $self->{_GLOBAL}{'ERROR'}="Not Connected"; }
817 $self->{_GLOBAL}{'ready_handles'}=\@handle;
818 }
819  
820 sub get_data_segment
821 {
822 my ( $self ) = shift;
823 my ( $header );
824 my ( $buffer ) = "";
825 my ( $dataset ) ;
826  
827 my $link;
828 my ( $version, $type, $session, $flags, $length );
829 my ( $handles ) = $self->{_GLOBAL}{'ready_handles'};
830  
831 foreach my $handle ( @{$handles} )
832 {
833 $link = sysread($handle,$buffer,1024);
834 if ( !$buffer )
835 {
836 $handle->close(); return 1;
837 }
838 print "Read buffer size of '".length($buffer)."'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
839 $self->{_GLOBAL}{'data_received'} .=$buffer;
840 $self->{_GLOBAL}{'last_handle'}=$handle;
841 }
842 $self->{_GLOBAL}{'data_processing'}=1;
843 }
844  
845 sub check_listener_handles
846 {
847 my ( $self ) = shift;
848 my ( @handle ) = $self->{_GLOBAL}{'Listen_Selector'}->can_read;
849 if ( !@handle ) { $self->{_GLOBAL}{'ERROR'}="Not Connected"; return 0; }
850 print "Handles available as '".@handle."'\n";
851 $self->{_GLOBAL}{'Listener_Handles'}=\@handle;
852 return 1;
853 }
854  
855 sub check_listeners_available
856 {
857 my ( $self ) = shift;
858  
859 while ( $self->check_listener_handles )
860 {
861 print "Checking handles.\n";
862 $self->get_listener_connect();
863 }
864  
865 $self->{_GLOBAL}{'STATUS'}="Socket Closed";
866 $self->{_GLOBAL}{'ERROR'}="Socket Closed";
867 }
868  
869 sub get_listener_connect
870 {
871 my ( $self ) = shift;
872 my ( $dataset ) = "";
873 my ( $handles ) = $self->{_GLOBAL}{'Listener_HandlesP'};
874 my ( $current_handles ) = $self->{_GLOBAL}{'Listener_Handles'};
875  
876 foreach my $handle ( @{$current_handles} )
877 {
878 print "Handle is '$handle'\n" if $self->{_GLOBAL}{'DEBUG'}>5;
879 if ( $handle==$self->{_GLOBAL}{'Listen_Handle'} )
880 {
881 my $new = $self->{_GLOBAL}{'Listen_Handle'}->accept;
882 $self->{_GLOBAL}{'Listen_Selector'}->add($new);
883 }
884 else
885 {
886 my $link = 0;
887 $dataset="";
888 $link = sysread($handle,$dataset,1024);
889 if ( !$link )
890 {
891 my $child;
892 ${$handles}{$handle}{'data'}.=$dataset;
893 if ($child=fork)
894 { } elsif (defined $child)
895 {
896 print "rmote address is '".${$handles}{$handle}{'addr'}."'\n" if $self->{_GLOBAL}{'DEBUG'}>5;
897 print "rmote port is '".${$handles}{$handle}{'port'}."'\n" if $self->{_GLOBAL}{'DEBUG'}>5;
898 if ( !${$handles}{$handle}{'data'} )
899 {} else {
900 my $tmp_filename="COPS_".time().rand(5000);
901 print "Writing data to '".$self->{_GLOBAL}{'TMPDirectory'}."/".$tmp_filename."-data'\n";
902 if ( open (__FILE,">".$self->{_GLOBAL}{'TMPDirectory'}."/".$tmp_filename."-data") )
903 {
904 print __FILE ${$handles}{$handle}{'data'};
905 close __FILE;
906 }
907 if ( open (__FILE,">".$self->{_GLOBAL}{'TMPDirectory'}."/".$tmp_filename."-lock") )
908 {
909 close (__FILE);
910 }
911 }
912 foreach my $handler ( keys %{$handles} )
913 { if ( $handler ne $handle ) { delete ${$handles}{$handler}; } }
914 if ( !${$handles}{$handle} ) { waitpid($child,0); exit(0); }
915 waitpid($child,0);
916 exit(0);
917 }
918 if ( ${$handles}{$handle}{'addr'} )
919 {
920 if ( $self->{_GLOBAL}{'complete_decoded_data'}{ ${$handles}{$handle}{'addr'} } )
921 { undef $self->{_GLOBAL}{'complete_decoded_data'}{ ${$handles}{$handle}{'addr'} }; }
922 }
923 delete ${$handles}{$handle};
924 $self->{_GLOBAL}{'Listen_Selector'}->remove($handle);
925 $handle->close();
926 }
927  
928 if ( $link )
929 {
930 print "Got data set as '$dataset'\n";
931 ${$handles}{$handle}{'data'}.=$dataset;
932 ${$handles}{$handle}{'addr'}=$handle->peerhost() if !${$handles}{$handle}{'addr'};
933 ${$handles}{$handle}{'port'}=$handle->peerport() if !${$handles}{$handle}{'port'};
934 }
935 }
936 }
937 return 1;
938 }
939  
940  
941  
942 sub check_data_available
943 {
944 my ( $self ) = shift;
945  
946 $self->{_GLOBAL}{'data_sync'}=0;
947  
948 while ( $self->check_data_handles && $self->{_GLOBAL}{'ERROR'}!~/not connected/i )
949 {
950  
951 $self->get_data_segment();
952  
953 while ( $self->{_GLOBAL}{'data_processing'}==1 )
954 {
955  
956 my $message = $self->{_GLOBAL}{'data_received'};
957
958 $self->decode_message_type();
959  
960
961  
962 if ( length($message)==0 || $self->{_GLOBAL}{'message_opcode'}=~/^null$/i )
963 {
964 $self->{_GLOBAL}{'data_processing'}=0;
965 }
966  
967 if ( $self->{_GLOBAL}{'DEBUG'}>0 )
968 {
969 if ( $self->{_GLOBAL}{'message_client_id'} )
970 {
971 print "Client is is '".$self->{_GLOBAL}{'message_client_id'}."'\n\n\n\n";
972 }
973  
974 for($a=0;$a<length($message);$a++)
975 {
976 printf("%02x-", ord(substr($message,$a,1)));
977 }
978 print "\n";
979 }
980  
981 if ( $self->{_GLOBAL}{'message_opcode'}=~/^opn$/i )
982 {
983 print "OPN Message received, sending CAT message.\n" if $self->{_GLOBAL}{'DEBUG'}>0;
984 my ( $response ) = $self->encode_cops_object(10,1,
985 pack("N",30) );
986 $response.=$self->encode_cops_object(15,1,
987 pack("N",30) );
988 my ( $cops_message ) = $self->encode_cops_message(
989 1,0,7,$self->{_GLOBAL}{'message_client_id'}, $response);
990  
991 $self->{_GLOBAL}{'major_client_id'} = $self->{_GLOBAL}{'message_client_id'};
992
993 $self->send_message($cops_message);
994 }
995  
996  
997 if ( $self->{_GLOBAL}{'message_opcode'}=~/^req$/i )
998 {
999 print "REQ message received.\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1000  
1001 $self->{_GLOBAL}{'message_client_handle'} = $self->{_GLOBAL}{'message'}{'Handle'}{'Handle'};
1002  
1003 if ( $self->get_command()=~/^set$/ )
1004 {
1005  
1006 my $subscriber_ip=$self->subscriber_get();
1007  
1008 my $handle_object = $self->encode_handle_object($self->{_GLOBAL}{'message_client_handle'});
1009 my $context_object = $self->encode_context_object( 8, 0 );
1010  
1011 my $temp = pack("nn",1,1);
1012  
1013 my $decision_object = $self->encode_decision_object ( 1, $temp);
1014  
1015 my $gate_command = $self->encode_sub_transaction_id(4,rand(4095));
1016 my $amid_command = $self->encode_sub_amid(1,1);
1017 my $subscriber_command = $self->encode_sub_subscriber_id(
1018 $self->subscriber_type(),$subscriber_ip);
1019  
1020
1021 my $gate_id = "";
1022 if ( $self->get_gate_id() )
1023 {
1024 $gate_id = $self->encode_gate_id ();
1025 }
1026  
1027 my $total_object;
1028  
1029 $total_object = $self->gate_specification_get();
1030 $total_object.= $self->envelope_get();
1031 $total_object.= $self->classifier_get();
1032 $total_object.= $self->rks_get();
1033  
1034 if ( length($self->volume_get())>0 )
1035 {
1036 if ( $self->{_GLOBAL}{'DEBUG'}> 5 )
1037 {
1038 print "Volume set adding in object.\n";
1039 print "Object size before is '".length($total_object)."'\n";
1040 }
1041 $total_object.= $self->volume_get();
1042 if ( $self->{_GLOBAL}{'DEBUG'}> 5 )
1043 {
1044 print "Object size after is '".length($total_object)."'\n";
1045 }
1046 }
1047  
1048 if ( length($self->timebase_get())>0 )
1049 {
1050 if ( $self->{_GLOBAL}{'DEBUG'}> 5 )
1051 {
1052 print "Timebase set adding in object.\n";
1053 print "Object size before is '".length($total_object)."'\n";
1054 }
1055 $total_object.= $self->timebase_get();
1056 if ( $self->{_GLOBAL}{'DEBUG'}> 5 )
1057 {
1058 print "Object size after is '".length($total_object)."'\n";
1059 }
1060 }
1061  
1062 if ( $self->opaque_get() )
1063 {
1064 $total_object.= $self->opaque_get();
1065 }
1066  
1067 my $decision2_object = $self->encode_decision_object
1068 (
1069 4,
1070 $gate_command.
1071 $amid_command.
1072 $subscriber_command.
1073 $gate_id.
1074 $total_object
1075 );
1076  
1077 my $data_block = $handle_object.$context_object.$decision_object.$decision2_object;
1078  
1079 my ( $cops_message ) = $self->encode_cops_message(
1080 1,0,2, 32778, $data_block);
1081 $self->send_message($cops_message);
1082 }
1083 if ( $self->get_command()=~/^info$/ )
1084 {
1085 my $subscriber_ip=$self->subscriber_get();
1086 my $handle_object = $self->encode_handle_object($self->{_GLOBAL}{'message_client_handle'});
1087 my $context_object = $self->encode_context_object( 8, 0 );
1088  
1089 my $temp = pack("nn",1,1);
1090  
1091 my $decision_object = $self->encode_decision_object ( 1, $temp);
1092  
1093 my $gate_command = $self->encode_sub_transaction_id(7,rand(4095));
1094 my $amid_command = $self->encode_sub_amid(1,1);
1095 my $subscriber_command = $self->encode_sub_subscriber_id(
1096 $self->subscriber_type(),$subscriber_ip);
1097  
1098 my $gate_id = $self->encode_gate_id ();
1099  
1100 my $decision2_object = $self->encode_decision_object
1101 (
1102 4,
1103 $gate_command.
1104 $amid_command.
1105 $subscriber_command.
1106 $gate_id
1107 );
1108 my $data_block = $handle_object.$context_object.$decision_object.$decision2_object;
1109 my ( $cops_message ) = $self->encode_cops_message(
1110 1,0,2, 32778, $data_block);
1111 $self->send_message($cops_message);
1112 }
1113 if ( $self->get_command()=~/^delete$/ )
1114 {
1115 my $subscriber_ip=$self->subscriber_get();
1116 my $handle_object = $self->encode_handle_object($self->{_GLOBAL}{'message_client_handle'});
1117 my $context_object = $self->encode_context_object( 8, 0 );
1118 my $temp = pack("nn",1,1);
1119 my $decision_object = $self->encode_decision_object ( 1, $temp);
1120 my $gate_command = $self->encode_sub_transaction_id(10,rand(4095));
1121 my $amid_command = $self->encode_sub_amid(1,1);
1122 my $subscriber_command = $self->encode_sub_subscriber_id(
1123 $self->subscriber_type(),$subscriber_ip);
1124 my $gate_id = $self->encode_gate_id ();
1125 my $decision2_object = $self->encode_decision_object
1126 (
1127 4,
1128 $gate_command.
1129 $amid_command.
1130 $subscriber_command.
1131 $gate_id
1132 );
1133 my $data_block = $handle_object.$context_object.$decision_object.$decision2_object;
1134 my ( $cops_message ) = $self->encode_cops_message(
1135 1,0,2, 32778, $data_block);
1136 $self->send_message($cops_message);
1137 }
1138  
1139 if ( $self->get_command()=~/^synch$/i )
1140 {
1141 print "Enter SYNC HERE!!!!!!\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1142 my $subscriber_ip=$self->subscriber_get();
1143 my $handle_object = $self->encode_handle_object($self->{_GLOBAL}{'message_client_handle'});
1144 my $context_object = $self->encode_context_object( 8, 0 );
1145 my $temp = pack("nn",1,1);
1146 my $decision_object = $self->encode_decision_object ( 1, $temp);
1147 my $gate_command = $self->encode_sub_transaction_id(20,rand(4095));
1148 my $amid_command = $self->encode_sub_amid(1,1);
1149 my $synch_id = $self->encode_synch_id();
1150 my $subscriber_command = $self->encode_sub_subscriber_id(
1151 $self->subscriber_type(),$subscriber_ip);
1152 my $decision2_object = $self->encode_decision_object
1153 (
1154 4,
1155 $gate_command.
1156 $amid_command.
1157 $synch_id
1158 );
1159 my $data_block = $handle_object.$context_object.$decision2_object;
1160 my ( $cops_message ) = $self->encode_cops_message(
1161 1,0,5, 32778, $data_block);
1162 $self->{_GLOBAL}{'wait_for_SSC'}=1;
1163 $self->send_message($cops_message);
1164 }
1165 }
1166 if ( $self->{_GLOBAL}{'message_opcode'}=~/^rpt$/i )
1167 {
1168 my %returned_data;
1169  
1170 my $classifier_count = 0;
1171  
1172 my $data_block = $self->{_GLOBAL}{'message'}{'Client Specific Info'}{'Handle'};
1173  
1174 while ( length($data_block)> 0 )
1175 {
1176 my ( $sub_length,$major, $minor) = unpack("nCC",$data_block);
1177 my $data_part = substr($data_block,4,$sub_length-4);
1178  
1179 my $type = $self->object_type_decode($major,$minor);
1180  
1181 print "Sub batch data block length '".$sub_length."'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1182 print "Major is '$major' minor is '$minor' type is '$type'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1183  
1184 if ( $type=~/^transaction identifier$/i )
1185 {
1186 my ( $mtid, $gate_command ) = $self->decode_transaction_identifier( $data_part );
1187 my ( $gate_transform ) = $self-> decode_gate_actions( $gate_command );
1188 print "Transaction ID is '$mtid' gate command '$gate_command' was '$gate_transform'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1189 $returned_data{'MM_GateCommandType'} = $gate_transform;
1190 $returned_data{'MM_TransactionId'} = $mtid;
1191 }
1192 if ( $type=~/^Gate Identifier$/i )
1193 {
1194 my ( $gate_id ) = $self->decode_gate_id ($data_part);
1195 $returned_data{'GateId_GateIdentifier'} = $gate_id;
1196 print "Gate ID is '$gate_id'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1197 }
1198  
1199 if ( $type=~/^Application Manager Identifier$/i )
1200 {
1201 my ( $amidat, $amidam ) = $self->decode_application_manager_identifier( $data_part );
1202 $returned_data{'AMID_ApplicationManagerIDApplicationType'}=$amidat;
1203 $returned_data{'AMID_ApplicationManagerIDApplicationManagerTag'}=$amidam;
1204 }
1205  
1206 if ( $type=~/^Classifier$/i )
1207 {
1208 print "\n\n\nEntering Classifier Decode\n\n\n\n\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1209 my $classifier_array = $self->classifier_arrays(1);
1210 my $unpacker="";
1211 foreach my $class_entry ( @{${$classifier_array}} )
1212 {
1213 my $type_c = $self->attribute_pack($class_entry);
1214 $unpacker.=$type_c;
1215 }
1216 my @temp=unpack($unpacker,$data_part);
1217 my $class_prefix=$type."_".$classifier_count."_";
1218 my $attribute_count =0;
1219 foreach my $class_entry ( @{${$classifier_array}} )
1220 {
1221 my $class_name = $class_prefix.$class_entry;
1222 if ( $class_entry=~/IP$/ )
1223 { $temp[$attribute_count]=$self->_IpIntToQuad($temp[$attribute_count]); }
1224 $returned_data{$class_name}=$temp[$attribute_count];
1225 $attribute_count++;
1226 }
1227 $class_prefix=$type."_Count";
1228 $returned_data{$class_prefix}++;
1229 $classifier_count++;
1230 }
1231  
1232 if ( $type=~/^Gate Specification$/ )
1233 {
1234 print "\n\n\nEntering Gate Specification Decode\n\n\n\n\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1235 my $gate_array = $self->gate_array();
1236 my $unpacker="";
1237 my $attribute_count =0;
1238 foreach my $gate_entry ( @{${$gate_array}} )
1239 {
1240 my $type_g = $self->attribute_pack($gate_entry);
1241 $unpacker.=$type_g;
1242 }
1243 my @temp=unpack($unpacker,$data_part);
1244 my $gate_prefix = $type."_"; $gate_prefix=~s/ //g;
1245 foreach my $gate_entry ( @{${$gate_array}} )
1246 {
1247 my $gate_name = $gate_prefix.$gate_entry;
1248 $returned_data{$gate_name}=$temp[$attribute_count];
1249 $attribute_count++;
1250 }
1251 }
1252  
1253 if ( $type=~/^Event-Generation-Info$/i )
1254 {
1255 print "\n\nEntering Event Generation Info Decode\n\n\n\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1256 my $event_array = $self->rks_array();
1257 my $unpacker="";
1258 my $attribute_count =0;
1259 foreach my $event_entry ( @{${$event_array}} )
1260 {
1261 my $event_g = $self->attribute_pack($event_entry);
1262 if ( $event_g=~/^string/ig )
1263 {
1264 my ( $name, $number ) = (split(/-/,$event_g))[0,1];
1265 $event_g="A[$number]";
1266 }
1267 $unpacker.=$event_g;
1268 }
1269 my @temp=unpack($unpacker,$data_part);
1270 foreach my $event_entry ( @{${$event_array}} )
1271 {
1272 if ( $event_entry=~/_IP/g )
1273 {
1274 $temp[$attribute_count] = $self->_IpIntToQuad($temp[$attribute_count]);
1275 }
1276 $returned_data{$event_entry}=$temp[$attribute_count];
1277 $attribute_count++;
1278 }
1279 }
1280  
1281 if ( $type=~/^Time Based Usage Limit$/i )
1282 {
1283 my $time = unpack ("N",$data_part);
1284 $returned_data{'TimeBasedUsageLimit'}=$time;
1285 print "Time found set is '$time'\n";
1286 }
1287  
1288 if ( $type=~/^Gate Time Info$/i )
1289 {
1290 my $time = unpack("N",$data_part);
1291 $returned_data{'GateTimeInfo'} = $time;
1292 }
1293  
1294 if ( $type=~/^Gate Usage Info$/i )
1295 {
1296 if ( $self->{_GLOBAL}{'DEBUG'}>0 )
1297 {
1298 print "Length of decode is '".length($data_part)."'\n";
1299 for($a=0;$a<length($data_part);$a++)
1300 {
1301 printf("%02x-", ord(substr($data_part,$a,2)));
1302 }
1303 print "\n";
1304 }
1305  
1306 my $data_transfer = $self->decode_64bit_number($data_part);
1307 $returned_data{'GateUsageInfo'}=$data_transfer;
1308 }
1309  
1310 if ( $type=~/^Gate State$/i )
1311 {
1312 my ( $state, $reason ) = unpack("nn",$data_part);
1313 $returned_data{'GateState_State_Number'} = $state;
1314 $returned_data{'GateState_Reason_Number'} = $reason;
1315 $returned_data{'GateState_State_Description'} = $self->gate_states($state);
1316 $returned_data{'GateState_Reason_Description'} = $self->gate_reasons($reason);
1317 }
1318  
1319 if ( $type=~/^Packet Cable Error$/i )
1320 {
1321 my ( $error, $sub_error ) = unpack("nn",$data_part);
1322 $returned_data{'PacketCableError_Main_Number'}= $error;
1323 $returned_data{'PacketCableError_Main_Description'}= $self->packetcable_errors($error);
1324 $returned_data{'PacketCableError_Sub_Number'}= $sub_error;
1325 foreach my $env_codes ( grep { /^current_Envelope/ } keys %{$self->{_GLOBAL}} )
1326 {
1327 my $temp = $env_codes;
1328 $temp=~s/current_Envelope_/Error_/g;
1329 $returned_data{$temp}= $self->{_GLOBAL}{$env_codes};
1330 }
1331
1332 }
1333  
1334  
1335 $data_block = substr($data_block,$sub_length,length($data_block)-$sub_length);
1336 }
1337 $self->{_GLOBAL}{'DataHandler'}->(
1338 $self,
1339 \%returned_data
1340 );
1341  
1342  
1343 if ( !$self->{_GLOBAL}{'wait_for_SSC'} )
1344 {
1345 if ( $self->{_GLOBAL}{'ListenServer'}==0 )
1346 {
1347 return 1;
1348 }
1349 }
1350 }
1351  
1352 if ( $self->{_GLOBAL}{'message_opcode'}=~/^ssc$/i )
1353 {
1354 $self->{_GLOBAL}{'wait_for_SSC'}=0;
1355 if ( $self->{_GLOBAL}{'ListenServer'}==0 )
1356 {
1357 return 1;
1358 }
1359 }
1360  
1361 if ( $self->{_GLOBAL}{'message_opcode'}=~/^ka$/i )
1362 {
1363 print "KA Message received, sending KA message.\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1364 my ( $cops_message ) = $self->encode_cops_message_no_payload(
1365 1,0,9,$self->{_GLOBAL}{'message_client_id'} );
1366 $self->send_message($cops_message);
1367 }
1368 }
1369  
1370 }
1371  
1372 return 1;
1373 }
1374  
1375 sub encode_handle_object
1376 {
1377 my ( $self ) = shift;
1378 my ( $data ) = shift;
1379  
1380 my $handle_object = $self->encode_cops_object( 1,1,$data );
1381  
1382 return $handle_object;
1383 }
1384  
1385 sub encode_context_object
1386 {
1387 my ( $self ) = shift;
1388 my ( $rtype ) = shift;
1389 my ( $mtype ) = shift;
1390  
1391 my ( $enc_rtype ) = pack("n",$rtype);
1392 my ( $enc_mtype ) = pack("n",$mtype);
1393  
1394 my $context_object = $self->encode_cops_object( 2,1, $enc_rtype.$enc_mtype);
1395  
1396 return $context_object;
1397 }
1398  
1399 sub encode_decision_object
1400 {
1401 my ( $self ) = shift;
1402 my ( $sub_command ) = shift;
1403 my ( $data ) = shift;
1404  
1405 my ( $pad ) = 0;
1406  
1407 my ($stub_pri, $sub_sec) = (split(/\./,sprintf("%f",length($data)/4)))[0,1];
1408 if ( $stub_pri ==0 && $sub_sec>0 )
1409 {
1410 $pad = 4 - length($data);
1411 }
1412  
1413 if ( $stub_pri > 0 && $sub_sec<1 )
1414 {
1415 my $temp = $stub_pri*4;
1416 $pad = $temp - length($data);
1417 }
1418  
1419 for ( $a=0; $a<$pad; $a++)
1420 {
1421 $data.=pack("C",0);
1422 }
1423  
1424 print "Length of data is '".length($data)."'\n\n\n\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1425  
1426 my $decision_object = $self->encode_cops_object( 6, $sub_command , $data);
1427  
1428 return $decision_object;
1429 }
1430  
1431 sub encode_sub_classifier
1432 {
1433 my ( $self ) = shift;
1434 my ( $data ) = shift;
1435 my ( $packed ) = "";
1436  
1437 my ( $classifier_array );
1438  
1439 if ( ${$data}{'Classifier_Type'}=~/^classifier$/i )
1440 {
1441 $packed .= pack("CC",6,1);
1442 $classifier_array = $self->classifier_arrays(1);
1443 }
1444  
1445 if ( ${$data}{'Classifier_Type'}=~/^extended$/i )
1446 {
1447 $packed .= pack("CC",6,2);
1448 $classifier_array = $self->classifier_arrays(2);
1449 }
1450  
1451 $packed.=$self->general_pack( $data, $classifier_array );
1452  
1453 if ( $self->{_GLOBAL}{'DEBUG'}>4 )
1454 {
1455 print "Classifier ENCODER \n\n\n\n\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1456 for($a=0;$a<length($packed);$a++)
1457 {
1458 printf("%02x-", ord(substr($packed,$a,2)));
1459 }
1460 print "\n";
1461 }
1462  
1463  
1464 my ( $length ) = pack("n",(length($packed)+2));
1465  
1466 return $length.$packed;
1467 }
1468  
1469 sub align_string
1470 {
1471 my ( $self ) = shift;
1472 my ( $string ) = shift;
1473  
1474 $string.=pack("C",0);
1475  
1476 my $align = length($string) % 4;
1477 $align = 4 - $align;
1478 if ( $align > 0 && $align <4 )
1479 {
1480 print "Align is '$align'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1481 for ( my $tmp=0; $tmp<$align; $tmp ++ )
1482 {
1483 print "Adding '$tmp'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1484 $string.=pack("C",0);
1485 }
1486 print "Finished alignment\n\n\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1487 }
1488 print "Length of output is '".length($string)."'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1489 my ( $length ) = pack("n",(length($string)+2));
1490 return $string;
1491 }
1492  
1493 sub encode_synch_id
1494 {
1495 my ( $self ) = shift;
1496 my ( $packed ) = "";
1497 $packed = pack("CC",18,1);
1498 $packed.= pack("CCCC",1,0,1,0);
1499 my ( $length ) = pack("n",(length($packed)+2));
1500 return $length.$packed;
1501 }
1502  
1503 sub encode_gate_id
1504 {
1505 my ( $self ) = shift;
1506 my ( $data ) = shift;
1507 my ( $packed ) = "";
1508 $packed = pack("CC",4,1);
1509 $packed .= pack("N", $self->get_gate_id() );
1510 my ( $length ) = pack("n",(length($packed)+2));
1511 return $length.$packed;
1512 }
1513  
1514 sub encode_time_limit
1515 {
1516 my ( $self ) = shift;
1517 my ( $data ) = shift;
1518 my ( $packed ) = "";
1519 if ( $self->{_GLOBAL}{'DEBUG'}> 4 )
1520 {
1521 print "Encoder time set to '".$data."'\n";
1522 }
1523  
1524 $packed = pack("CC",10,1);
1525 $packed .= pack("N",$data);
1526 if ( $self->{_GLOBAL}{'DEBUG'}> 4 )
1527 {
1528 print "Encoder Time packing \n";
1529 for($a=0;$a<length($packed);$a++)
1530 {
1531 printf("%02x-", ord(substr($packed,$a,2)));
1532 }
1533 print "\n";
1534 }
1535  
1536 my ( $length ) = pack("n",(length($packed)+2));
1537 return $length.$packed;
1538 }
1539  
1540 sub encode_byte_limit
1541 {
1542 my ( $self ) = shift;
1543 my ( $data ) = shift;
1544 my ( $packed ) = "";
1545 $packed = pack("CC",9,1);
1546 if ( !$data )
1547 { $data=0; }
1548 $packed .= $self->encode_64bit_number($data);
1549 my ( $length ) = pack("n",(length($packed)+2));
1550 return $length.$packed;
1551 }
1552  
1553 sub encode_sub_traffic
1554 {
1555 my ( $self ) = shift;
1556 my ( $data ) = shift;
1557 my ( $packed ) = "";
1558  
1559 my $type = $self->envelope_type ( ${$data}{'Service_Type'} );
1560 my @parts = (split(/,/,$type))[0,1,2];
1561 $packed = pack("CC", $parts[1], $parts[2]);
1562 ${$data}{'Type'} = $self->Envelope_Type_remap(${$data}{'Envelope_Type'});
1563 ${$data}{'Envelope'} = ${$data}{'Type'};
1564 my $envelope_header = $self->envelope_header( $parts[2] );
1565 $packed.=$self->general_pack( $data, $envelope_header );
1566  
1567 my $auth = ${$data}{'Type'} & 0x01;
1568 my $reserved = ${$data}{'Type'} & 0x02;
1569 my $commit = ${$data}{'Type'} & 0x04;
1570  
1571 print "Auth is '$auth'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1572 print "Reserved is '$reserved'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1573 print "Commit is '$commit'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1574  
1575 if ( $auth > 0 )
1576 {
1577 print "\n\nENTERING AUTH PHASE\n\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1578 $packed.=$self->encode_envelope( $parts[2], "Envelope_authorize_", $data ); }
1579 if ( $reserved > 0 )
1580 {
1581 print "\n\nENTERING RESERVED PHASE\n\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1582 $packed.=$self->encode_envelope( $parts[2], "Envelope_reserve_", $data ); }
1583 if ( $commit > 0 )
1584 {
1585 print "\n\nENTERING COMMIT PHASE\n\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1586 $packed.=$self->encode_envelope( $parts[2], "Envelope_commit_", $data ); }
1587  
1588 if ( $self->{_GLOBAL}{'DEBUG'}>4 )
1589 {
1590 print "Envelope ENCODER \n\n\n\n\n";
1591 for($a=0;$a<length($packed);$a++)
1592 {
1593 printf("%02x-", ord(substr($packed,$a,2)));
1594 }
1595 print "\n";
1596 }
1597  
1598 if ( $self->{_GLOBAL}{'DEBUG'}>4 )
1599 {
1600 print "Length of complete envelope is '".length($packed)."'\n";
1601 }
1602  
1603 my ( $length ) = pack("n",(length($packed)+2));
1604  
1605 if ( $self->{_GLOBAL}{'DEBUG'}>4 )
1606 {
1607 print "Header attached to envelope is '".length($length)."'\n";
1608 }
1609  
1610 return $length.$packed;
1611 }
1612  
1613 sub encode_envelope
1614 {
1615 my ( $self ) = shift;
1616 my ( $number ) = shift;
1617 my ( $type ) = shift;
1618 my ( $data ) = shift;
1619 my ( $result ) = "";
1620 if ( $self->{_GLOBAL}{'DEBUG'}>4 )
1621 {
1622 print "Envelope encoding information Number '$number' type '$type'\n";
1623 }
1624 my $envelope_main = $self->envelope_array( $number );
1625 $result.=$self->general_pack( $data, $envelope_main, $type );
1626 return $result;
1627 }
1628  
1629 sub encode_sub_amid
1630 {
1631 my ( $self ) = shift;
1632 my ( $app_type ) = shift;
1633 my ( $man_tag ) = shift;
1634 my ( $packed ) = pack("CCnn", 2,1,$app_type, $man_tag);
1635 my ( $length ) = pack("n",(length($packed)+2));
1636 return $length.$packed;
1637 }
1638  
1639 sub encode_sub_subscriber_id
1640 {
1641 my ( $self ) = shift;
1642 my ( $type ) = shift;
1643 my ( $ip ) = shift;
1644  
1645 my ( $convert_ip ) = $self->_IpQuadToInt($ip);
1646 my ( $packed ) = pack("CCN", 3,$type,$convert_ip);
1647 my ( $length ) = pack("n",(length($packed)+2));
1648 return $length.$packed;
1649 }
1650  
1651 sub encode_sub_transaction_id
1652 {
1653 my ( $self ) = shift;
1654 my ( $gate_command ) = shift;
1655 my ( $trans_id ) = shift;
1656 my ( $packed ) = pack("CCnn", 1,1,$trans_id,$gate_command);
1657 my ( $length ) = pack("n",(length($packed)+2));
1658 return $length.$packed;
1659 }
1660  
1661 sub encode_sub_decision
1662 {
1663 my ( $self ) = shift;
1664 my ( $snum ) = shift;
1665 my ( $stype ) = shift;
1666 my ( $data ) = shift;
1667  
1668 my ( $pad ) = 0;
1669  
1670 my ($stub_pri, $sub_sec) = (split(/\./,sprintf("%f",length($data)/4)))[0,1];
1671 if ( $stub_pri ==0 && $sub_sec>0 )
1672 {
1673 $pad = 4 - length($data);
1674 }
1675  
1676 if ( $stub_pri > 0 && $sub_sec<1 )
1677 {
1678 my $temp = $stub_pri*4;
1679 $pad = $temp - length($data);
1680 }
1681  
1682 for ( $a=0; $a<$pad; $a++)
1683 {
1684 $data.=pack("C",0);
1685 }
1686  
1687 my ( $length ) = length ($data)+2;
1688  
1689 my $sub_decision = pack("nCCN",$length, $snum, $stype, $data);
1690  
1691 return $sub_decision;
1692 }
1693  
1694 sub send_message
1695 {
1696 my ( $self ) = shift;
1697 my ( $message ) = shift;
1698 if ( !$self->{_GLOBAL}{'Handle'} ) { return 0; }
1699 my ( $length_sent );
1700 eval {
1701 local $SIG{ALRM} = sub { die "alarm\n" };
1702 alarm 1;
1703 $length_sent = syswrite ( $self->{_GLOBAL}{'Handle'}, $message );
1704 alarm 0;
1705 };
1706  
1707 print "length sent is '$length_sent'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1708  
1709 if ( $@=~/alarm/i )
1710 { return 0; }
1711  
1712 print "Sending message of size '".length($message)."'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1713  
1714 if ( $self->{_GLOBAL}{'DEBUG'}>4 )
1715 {
1716 for($a=0;$a<length($message);$a++)
1717 {
1718 printf("%02x-", ord(substr($message,$a,2)));
1719 }
1720 print "\n";
1721 }
1722  
1723 if ( $length_sent==length($message) )
1724 { return 1; }
1725 return 0;
1726 }
1727  
1728 sub set_gate_id
1729 {
1730 my ( $self ) = shift;
1731 my ( $gate_id ) = shift;
1732 $self->{_GLOBAL}{'current_gate_id'} = $gate_id;
1733 return 1;
1734 }
1735  
1736 sub get_gate_id
1737 {
1738 my ( $self ) = shift;
1739 if ( !$self->{_GLOBAL}{'current_gate_id'} )
1740 {
1741 return "";
1742 }
1743 return $self->{_GLOBAL}{'current_gate_id'};
1744 }
1745  
1746 sub set_command
1747 {
1748 my ( $self ) = shift;
1749 my ( $command ) = shift;
1750 $self->{_GLOBAL}{'current_command'} = $command;
1751 return 1;
1752 }
1753  
1754 sub get_command
1755 {
1756 my ( $self ) = shift;
1757 return $self->{_GLOBAL}{'current_command'};
1758 }
1759  
1760 sub clear_command
1761 {
1762 my ( $self ) = shift;
1763 $self->{_GLOBAL}{'current_command'}="";
1764 return 1;
1765 }
1766  
1767 sub envelope_add
1768 {
1769 my ( $self ) = shift;
1770 my ( $data ) = shift;
1771 my ( %test ) ;
1772 while (my($field, $val) = splice(@{$data}, 0, 2))
1773 {
1774 my $temp = "current_Envelope_".$field;
1775 $self->{_GLOBAL}{$temp} = $val;
1776 $test{$field}= $val;
1777 }
1778  
1779 my $encode_envelope = $self->encode_sub_traffic(\%test);
1780 $self->{_GLOBAL}{'Envelope_Encoded'} = $encode_envelope;
1781 return 1;
1782 }
1783  
1784 sub envelope_get
1785 {
1786 my ( $self ) = shift;
1787 if ( $self->{_GLOBAL}{'Envelope_Encoded'} )
1788 {
1789 return $self->{_GLOBAL}{'Envelope_Encoded'};
1790 }
1791 return "";
1792 }
1793  
1794 sub classifier_add
1795 {
1796 my ( $self ) = shift;
1797 my ( $data ) = shift;
1798 my ( %test );
1799 while (my($field, $val) = splice(@{$data}, 0, 2))
1800 { $test{$field}= $val; }
1801 my $encode_classifier = $self->encode_sub_classifier ( \%test );
1802 $self->{_GLOBAL}{'Classifier_Encoded'} .= $encode_classifier;
1803 return 1;
1804 }
1805  
1806 sub classifier_clear
1807 {
1808 my ( $self ) = shift;
1809 $self->{_GLOBAL}{'Classifier_Encoded'} = "";
1810 }
1811  
1812 sub classifier_get
1813 {
1814 my ( $self ) = shift;
1815 if ( $self->{_GLOBAL}{'Classifier_Encoded'} )
1816 {
1817 return $self->{_GLOBAL}{'Classifier_Encoded'};
1818 }
1819 return "";
1820 }
1821  
1822  
1823 sub subscriber_set
1824 {
1825 my ( $self ) = shift;
1826 my ( $subscriber_type ) = shift;
1827 my ( $ip ) = shift;
1828 if ( $subscriber_type=~/^ipv4$/i )
1829 {
1830 print "Entry IP is '$ip'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1831 my ( $test ) = $self->_IpQuadToInt($ip);
1832 $ip = $self->_IpIntToQuad($test);
1833 print "Exit IP is '$ip'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1834 $self->{_GLOBAL}{'cpe_device_type'}=$subscriber_type;
1835 $self->{_GLOBAL}{'cpe_device_ip'}=$ip;
1836 }
1837  
1838 if ( $subscriber_type=~/^ipv6$/i )
1839 {
1840 my @ipv6 = split(/:/,$ip);
1841 my $test = "";
1842 if ( scalar(@ipv6)!=8 )
1843 {
1844 print "IPv6 entry invalid, full address required.\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1845 return 1;
1846 }
1847 foreach my $ipv6_seg ( @ipv6 )
1848 {
1849 my $testa = hex($ipv6_seg);
1850 $test .= pack("n",$testa);
1851 }
1852 $self->{_GLOBAL}{'cpe_device_type'}=$subscriber_type;
1853 $self->{_GLOBAL}{'cpe_device_ip'}=$test;
1854 }
1855 return 1;
1856 }
1857  
1858 sub subscriber_type
1859 {
1860 my ( $self ) = shift;
1861 if ( !$self->{_GLOBAL}{'cpe_device_type'} )
1862 { return 0; }
1863 if ( $self->{_GLOBAL}{'cpe_device_type'}=~/^ipv4$/ )
1864 { return 1; }
1865 if ( $self->{_GLOBAL}{'cpe_device_type'}=~/^ipv6$/ )
1866 { return 2; }
1867 return 0;
1868 }
1869  
1870 sub subscriber_clear
1871 {
1872 my ( $self ) = shift;
1873 $self->{_GLOBAL}{'cpe_device_ip'}="";
1874 $self->{_GLOBAL}{'cpe_device_type'}=0;
1875 return 1;
1876 }
1877  
1878 sub subscriber_get
1879 {
1880 my ( $self ) = shift;
1881 if ( !$self->{_GLOBAL}{'cpe_device_ip'} )
1882 {
1883 return $self->_IpIntToQuad(0);
1884 }
1885 return $self->{_GLOBAL}{'cpe_device_ip'};
1886 }
1887  
1888 sub get_error
1889 {
1890 my ( $self ) = shift;
1891 return $self->{_GLOBAL}{'ERROR'};
1892 }
1893  
1894 sub decode_message_type
1895 {
1896 my ( $self ) = shift;
1897 my ( $decode_data ) = $self->{_GLOBAL}{'data_received'};
1898  
1899 $self->{_GLOBAL}{'message_count'}=0;
1900  
1901 if ( !$decode_data )
1902 {
1903 $self->{_GLOBAL}{'message_opcode'} = "NULL";
1904 return 1; }
1905  
1906 my ( $vflags, $v2, $client_id, $total_length ) = unpack("CCnN",$decode_data);
1907 my ( $version ) = $vflags;
1908  
1909 $version >>= 4; $version = $version & 0x0F; $vflags = $vflags & 0x0F;
1910 my ( $opcode ) = $self->decode_cops_operations( $v2 );
1911  
1912 print "Version is '$version' flags is '$vflags'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1913 print "Opcode is '$opcode'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1914 print "client id is '$client_id'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1915  
1916 print "New total length is '$total_length'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1917 print "Length of dd '".length($decode_data)."\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1918  
1919 $decode_data = substr ( $decode_data, 8, $total_length-8 );
1920  
1921  
1922 if ( $self->{_GLOBAL}{'DEBUG'}>0 )
1923 {
1924 for($a=0;$a<length($decode_data);$a++)
1925 {
1926 printf("%02x-", ord(substr($decode_data,$a,1)));
1927 }
1928 print "\n";
1929 }
1930  
1931  
1932 print "Remaing data set length is '".length($decode_data)."'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1933  
1934 while ( length($decode_data)> 0 )
1935 {
1936 $decode_data = $self-> decode_cops_message ( $decode_data );
1937 }
1938  
1939  
1940 print "all cops messages decoded.\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1941  
1942 $self->{_GLOBAL}{'message_client_id'} = $client_id;
1943 $self->{_GLOBAL}{'message_opcode'} = $opcode;
1944  
1945 $self->{_GLOBAL}{'data_received'} = substr( $self->{_GLOBAL}{'data_received'}, $total_length, length($self->{_GLOBAL}{'data_received'})-$total_length);
1946  
1947 return 1;
1948 }
1949  
1950 sub decode_cops_message
1951 {
1952 my ( $self ) = shift;
1953 my ( $data ) = shift;
1954  
1955 my ( $total_length, $cnum, $ctype ) = unpack("nCC",$data);
1956  
1957 print "Cops message length is '$total_length'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1958  
1959 $cnum = $self->decode_c_num_type ( $cnum );
1960 $ctype = $self->decode_c_num_type ( $ctype );
1961  
1962 print "Cnum is '$cnum'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1963 print "CType is '$ctype'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1964  
1965 print "Total length is '$total_length'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1966  
1967 my $message = substr($data,4, $total_length-4 );
1968  
1969 print "Pre length data is '".length($message)."'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1970  
1971 $self->{_GLOBAL}{'message'}{ $cnum }{$ctype } = $message;
1972  
1973 print "Length of message is '".length($self->{_GLOBAL}{'message'}{ $cnum }{$ctype })."'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1974
1975 $data = substr($data,$total_length, length($data)-$total_length );
1976  
1977 return $data;
1978 }
1979  
1980  
1981 sub decode_cops_operations
1982 {
1983 my ( $self ) = shift;
1984 my ( $opcode ) = shift;
1985  
1986 my %opcodes = (
1987 '1' => 'REQ',
1988 '2' => 'DEC',
1989 '3' => 'RPT',
1990 '4' => 'DRQ',
1991 '5' => 'SSQ',
1992 '6' => 'OPN',
1993 '7' => 'CAT',
1994 '8' => 'CC',
1995 '9' => 'KA',
1996 '10' => 'SSC'
1997 );
1998  
1999 return $opcodes{$opcode};
2000 }
2001  
2002 sub decode_c_num_type
2003 {
2004 my ( $self ) = shift;
2005 my ( $opcode ) = shift;
2006  
2007 my %cnumtypes = (
2008 '1' => 'Handle',
2009 '2' => 'Context',
2010 '3' => 'In Interface',
2011 '4' => 'Out Interface',
2012 '5' => 'Reason Code',
2013 '6' => 'Decision',
2014 '7' => 'LPDP Decision',
2015 '8' => 'Error',
2016 '9' => 'Client Specific Info',
2017 '10' => 'Keep-Alive Timer',
2018 '11' => 'PEP Identification',
2019 '12' => 'Report Type',
2020 '13' => 'PDP Redirect Address',
2021 '14' => 'Last PDP Address',
2022 '15' => 'Accounting Timer',
2023 '16' => 'Message Integrity'
2024 );
2025  
2026 return $cnumtypes{$opcode};
2027 }
2028  
2029 sub decode_gate_actions
2030 {
2031 my ( $self ) = shift;
2032 my ( $transcode ) = shift;
2033 my ( %transaction_ids ) = (
2034 '1' => 'GATE-ALLOC',
2035 '2' => 'GATE-ALLOC-ACK',
2036 '3' => 'GATE-ALLOC-ERR',
2037 '4' => 'GATE-SET',
2038 '5' => 'GATE-SET-ACK',
2039 '6' => 'GATE-SET-ERR',
2040 '7' => 'GATE-INFO',
2041 '8' => 'GATE-INFO-ACK',
2042 '9' => 'GATE-INFO-ERR',
2043 '10' => 'GATE-DELETE',
2044 '11' => 'GATE-DELETE-ACK',
2045 '12' => 'GATE-DELETE-ERR',
2046 '13' => 'GATE-OPEN',
2047 '14' => 'GATE-CLOSE'
2048 );
2049 return $transaction_ids{$transcode} if $transaction_ids{$transcode};
2050 }
2051  
2052 sub encode_gate_ids
2053 {
2054 my ( $self ) = shift;
2055 my ( $transcode ) = shift;
2056 my ( %transaction_ids ) = (
2057 'GATE-ALLOC' => '1',
2058 'GATE-ALLOC-ACK' => '2',
2059 'GATE-ALLOC-ERR' => '3',
2060 'GATE-SET' => '4',
2061 'GATE-SET-ACK' => '5',
2062 'GATE-SET-ERR' => '6',
2063 'GATE-INFO' => '7',
2064 'GATE-INFO-ACK' => '8',
2065 'GATE-INFO-ERR' => '9',
2066 'GATE-DELETE' => '10',
2067 'GATE-DELETE-ACK' => '11',
2068 'GATE-DELETE-ERR' => '12',
2069 'GATE-OPEN' => '13',
2070 'GATE-CLOSE' => '14'
2071 );
2072 return $transaction_ids{$transcode} if $transaction_ids{$transcode};
2073 }
2074  
2075 sub classifier_arrays
2076 {
2077 my ( $self ) = shift;
2078 my ( $type ) = shift;
2079 my ( @classifiers ) =
2080 (
2081 [],
2082 [
2083 "Classifier_IPProtocolId",
2084 "Classifier_TOSField",
2085 "Classifier_TOSMask",
2086 "Classifier_SourceIP",
2087 "Classifier_DestinationIP",
2088 "Classifier_SourcePort",
2089 "Classifier_DestinationPort",
2090 "Classifier_Priority",
2091 "Reserved1Byte",
2092 "Reserved1Byte",
2093 "Reserved1Byte"
2094 ],
2095 [
2096 "EClassifier_IPProtocolId",
2097 "EClassifier_TOSField",
2098 "EClassifier_TOSMask",
2099 "EClassifier_SourceIP",
2100 "EClassifier_SourceMask",
2101 "EClassifier_DestinationIP",
2102 "EClassifier_DestinationMask",
2103 "EClassifier_SourcePortStart",
2104 "EClassifier_SourcePortEnd",
2105 "EClassifier_DestinationPortStart",
2106 "EClassifier_DestinationPortEnd",
2107 "EClassifier_ClassifierID",
2108 "EClassifier_Priority",
2109 "EClassifier_State",
2110 "EClassifier_Action",
2111 "Reserved1Byte",
2112 "Reserved1Byte",
2113 "Reserved1Byte"
2114 ]
2115 );
2116 return \$classifiers[$type];
2117 }
2118  
2119 sub packetcable_errors
2120 {
2121 my ( $self ) = shift;
2122 my ( $error ) = shift;
2123 my ( @errors ) =
2124 (
2125 "",
2126 "Insufficient resources",
2127 "Unknown Gate ID",
2128 "Unknown",
2129 "Unknown",
2130 "Unknown",
2131 "Missing Required Object",
2132 "Invalid Object",
2133 "Volume based usage limit exceeded",
2134 "Time based usage limit exceeded",
2135 "Session Class Limit Exceeded",
2136 "Undefined Service Class Name",
2137 "Incompatible Envelope",
2138 "Invalid subscriber identifier",
2139 "Unauthorized AMID",
2140 "Number of Classifiers not supported",
2141 "Policy Exception",
2142 "Invalid field value in object",
2143 "Transport Error",
2144 "Unknown gate command",
2145 "DOCSIS 1.0 CM",
2146 "Number of SIDs exceeded in CM",
2147 "Number of SIDs exceeded in CMTS",
2148 "Unauthorized PSID",
2149 "No state for PDPD",
2150 "Unsupport Sync Type",
2151 "State data incomplete",
2152 );
2153  
2154 if (!$errors[$error])
2155 { return "Other, unspecified error"; }
2156 return $errors[$error];
2157 }
2158  
2159 sub gate_states
2160 {
2161 my ( $self ) = shift;
2162 my ( $state_no ) = shift;
2163 my ( @states ) =
2164 (
2165 "",
2166 "Idle/Closed",
2167 "Auhorized",
2168 "Reserved",
2169 "Committed",
2170 "Committed Recovery"
2171 );
2172 if ( !$states[$state_no] )
2173 { return "Unknown"; }
2174 return $states[$state_no];
2175 }
2176  
2177 sub gate_reasons
2178 {
2179 my ( $self ) = shift;
2180 my ( $reason ) = shift;
2181 my ( @reasons ) =
2182 (
2183 "",
2184 "Close Initiated by CMTS because of reservation reassignment",
2185 "Close Initiated by CMTS because of lack of DOCSIS responses",
2186 "Close Initiated by CMTS because of timer T1 expiry",
2187 "Close Initiated by CMTS because of timer T2 expiry",
2188 "Inactivity timer (T3) expired",
2189 "Close Initiated by CMTS because of a lack of reservation maintenance",
2190 "Gate state unchanged, but volume limit reached",
2191 "Close Initiated by CMTS because of timer T4 expiry",
2192 "Gate State unchanged, but timer T2 expiry caused reservation reduction",
2193 "Gate State unchanged, but time limit reached",
2194 "Close Initiated by PS or CMTS, volume limit reached",
2195 "Close Initiated by PS or CMTS, time limit reached",
2196 "Close Initiated by CMTS, other"
2197 );
2198  
2199 if ( !$reasons[$reason] )
2200 {
2201 return "Other";
2202 }
2203 return $reasons[$reason];
2204 }
2205  
2206 sub gate_array
2207 {
2208 my ( $self ) = shift;
2209 my ( @gate_headers ) =
2210 (
2211 [
2212 "Gate_Flags",
2213 "Gate_TOSField",
2214 "Gate_TOSMask",
2215 "Gate_Class",
2216 "Gate_T1",
2217 "Gate_T2",
2218 "Gate_T3",
2219 "Gate_T4"
2220 ]
2221 );
2222 return \$gate_headers[0];
2223 }
2224  
2225 sub rks_array
2226 {
2227 my ( $self ) = shift;
2228 my ( @rks_headers ) =
2229 (
2230 [
2231 "PRKS_IPAddress",
2232 "PRKS_Port",
2233 "Reserved",
2234 "Reserved",
2235 "SRKS_IPAddress",
2236 "SRKS_Port",
2237 "Reserved",
2238 "Reserved",
2239 "BCID_TimeStamp",
2240 "BCID_ElementID",
2241 "BCID_TimeZone",
2242 "BCID_EventCounter"
2243 ]
2244 );
2245 return \$rks_headers[0];
2246 }
2247  
2248 sub rks_set
2249 {
2250 my ( $self ) = shift;
2251 my ( $data ) = shift;
2252 my ( %test );
2253 while (my($field, $val) = splice(@{$data}, 0, 2))
2254 {
2255 $test{$field}= $val;
2256 }
2257 my $rks_encoded = $self->rks_encode(\%test);
2258 $self->{_GLOBAL}{'RKS_Encoded'} = $rks_encoded;
2259 return 1;
2260 }
2261  
2262 sub rks_clear
2263 {
2264 my ( $self ) = shift;
2265 $self->{_GLOBAL}{'RKS_Encoded'} = "";
2266 return 1;
2267 }
2268  
2269 sub rks_encode
2270 {
2271 my ( $self ) = shift;
2272 my ( $data ) = shift;
2273 my ( $packed ) = "";
2274 my ( $done_pack ) =0;
2275  
2276 $packed = pack("CC",8,1);
2277 my ( $rks_headers ) = $self->rks_array();
2278 $packed .= $self->general_pack ( $data, $rks_headers );
2279 my ( $length ) = pack("n",(length($packed)+2));
2280 return $length.$packed;
2281 }
2282  
2283 sub rks_get
2284 {
2285 my ( $self ) = shift;
2286 if ( $self->{_GLOBAL}{'RKS_Encoded'} )
2287 {
2288 return $self->{_GLOBAL}{'RKS_Encoded'};
2289 }
2290 return "";
2291 }
2292  
2293  
2294 sub opaque_clear
2295 {
2296 my ( $self ) = shift;
2297 $self->{_GLOBAL}{'OpaqueData'}="";
2298 return 1;
2299 }
2300  
2301 sub opaque_set
2302 {
2303 my ( $self ) = shift;
2304 my ( $data ) = shift;
2305 my ( %test );
2306 my ( $encoded ) = "";
2307 while (my($field, $val) = splice(@{$data}, 0, 2))
2308 { $test{$field}= $val }
2309 if ( $test{'OpaqueData'} )
2310 {
2311 $encoded = pack("CC",11,1);
2312 $encoded.= $self->align_string( $test{'OpaqueData'} );
2313 my ( $length ) = pack("n",(length($encoded)+2));
2314 $encoded= $length.$encoded;
2315 }
2316 $self->{_GLOBAL}{'OpaqueData'} = $encoded;
2317 return 1;
2318 }
2319  
2320 sub timebase_set
2321 {
2322 my ( $self ) = shift;
2323 my ( $data ) = shift;
2324 if ( $self->{_GLOBAL}{'DEBUG'}> 4 )
2325 {
2326 print "Time set to '".$data."'\n";
2327 }
2328 if ( $data > 0 )
2329 {
2330 my $timer_encode = $self->encode_time_limit( $data );
2331 $self->{_GLOBAL}{'TimeLimit'} = $timer_encode;
2332 }
2333 return 1;
2334 }
2335  
2336 sub timebase_clear
2337 {
2338 my ( $self ) = shift;
2339 $self->{_GLOBAL}{'TimeLimit'} = "";
2340 return 1;
2341 }
2342  
2343 sub volume_set
2344 {
2345 my ( $self ) = shift;
2346 my ( $data ) = shift;
2347 if ( $data>0 )
2348 {
2349 my $timer_encode = $self->encode_byte_limit( $data );
2350 $self->{_GLOBAL}{'VolumeLimit'} = $timer_encode;
2351 }
2352 return 1;
2353 }
2354  
2355 sub volume_clear
2356 {
2357 my ( $self ) = shift;
2358 $self->{_GLOBAL}{'VolumeLimit'} = "";
2359 return 1;
2360 }
2361  
2362 sub opaque_get
2363 {
2364 my ( $self ) = shift;
2365 if ( $self->{_GLOBAL}{'OpaqueData'} )
2366 {
2367 return $self->{_GLOBAL}{'OpaqueData'};
2368 }
2369 return "";
2370 }
2371  
2372 sub volume_get
2373 {
2374 my ( $self ) = shift;
2375 if ( length($self->{_GLOBAL}{'VolumeLimit'})>0 )
2376 {
2377 return $self->{_GLOBAL}{'VolumeLimit'};
2378 }
2379 return "";
2380 }
2381  
2382 sub timebase_get
2383 {
2384 my ( $self ) = shift;
2385 if ( length($self->{_GLOBAL}{'TimeLimit'})>0 )
2386 {
2387 return $self->{_GLOBAL}{'TimeLimit'};
2388 }
2389 return "";
2390 }
2391  
2392 sub gate_specification_add
2393 {
2394 my ( $self ) = shift;
2395 my ( $data ) = shift;
2396 my ( %test );
2397 while (my($field, $val) = splice(@{$data}, 0, 2))
2398 {
2399 $test{$field}= $val;
2400 }
2401 my ( $priority ) =0;
2402 my ( $preemption ) =0;
2403 foreach my $field ( keys %test )
2404 {
2405 my $val = $test{$field};
2406 print "Gate key is '$field' value is '$val'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
2407 if ( $field=~/^direction$/i )
2408 {
2409 if (!$test{'Gate_Flags'}) { $test{'Gate_Flags'}=0; }
2410 $test{'Gate_Flags'}=$test{'Gate_Flags'} & 0xFE;
2411 if ( $val=~/^downstream$/i )
2412 {
2413 $test{'Gate_Flags'}=$test{'Gate_Flags'} & 0xFE;
2414 }
2415 if ( $val=~/^upstream$/i )
2416 {
2417 $test{'Gate_Flags'}=$test{'Gate_Flags'} & 0xFE;
2418 $test{'Gate_Flags'}=$test{'Gate_Flags'} + 0x01;
2419 }
2420 }
2421  
2422 if ( $field=~/^dscptosmark$/i )
2423 {
2424 if ( !$test{'Gate_Flags'} ) { $test{'Gate_Flags'}=0; }
2425 $test{'Gate_Flags'} = $test{'Gate_Flags'} & 0xFD;
2426 $val = $val & 0x01; $val = $val << 1; $val = $val & 0x02;
2427 $test{'Gate_Flags'} = $test{'Gate_Flags'} + $val;
2428 }
2429  
2430 if ( $field=~/^priority$/i )
2431 {
2432 if ( !$test{'Gate_Class'} ) { $test{'Gate_Class'}=0; }
2433 $val = $val & 0x07;
2434 $priority = $val;
2435 }
2436 if ( $field=~/^preemption$/i )
2437 {
2438 if ( $test{'Gate_Class'} ) { $test{'Gate_Class'}=0; }
2439 $val = $val & 0x01; $val = $val << 3;
2440 $val = $val & 0x08;
2441 $preemption = $val;
2442 }
2443 if ( $priority || $preemption )
2444 {
2445 $test{'Gate_Class'} = $test{'Gate_Class'} & 0xF0;
2446 $test{'Gate_Class'} = $test{'Gate_Class'} + $priority;
2447 $test{'Gate_Class'} = $test{'Gate_Class'} + $preemption;
2448 }
2449 }
2450 my $encode_gate_spec = $self->encode_sub_gate_spec ( \%test );
2451 $self->{_GLOBAL}{'Gate_Specification_Encoded'} = $encode_gate_spec;
2452 return 1;
2453 }
2454  
2455 sub gate_specification_clear
2456 {
2457 my ( $self ) = shift;
2458 $self->{_GLOBAL}{'Gate_Specification_Encoded'} = "";
2459 }
2460  
2461 sub gate_specification_get
2462 {
2463 my ( $self ) = shift;
2464 if ( $self->{_GLOBAL}{'Gate_Specification_Encoded'} )
2465 {
2466 return $self->{_GLOBAL}{'Gate_Specification_Encoded'};
2467 }
2468 return "";
2469 }
2470  
2471  
2472 sub encode_sub_gate_spec
2473 {
2474 my ( $self ) = shift;
2475 my ( $data ) = shift;
2476 my ( $packed ) = "";
2477  
2478 $packed = pack("CC",5,1);
2479 my ( $gate_headers ) = $self->gate_array();
2480 $packed.= $self->general_pack( $data, $gate_headers );
2481 my ( $length ) = pack("n",(length($packed)+2));
2482 return $length.$packed;
2483 }
2484  
2485 sub envelope_type
2486 {
2487 my ( $self ) = shift;
2488 my ( $part ) = shift;
2489 my ( %types ) =
2490 (
2491 "None" => "None,0,0",
2492 "Flow Spec"=> "Flow Spec,7,1",
2493 "DOCSIS Service Class Name"=> "DOCSIS Service Class Name,7,2",
2494 "Best Effort Service" => "Best Effort Service,7,3",
2495 "Non-Real-Time Polling Service" => "Non-Real-Time Polling Service,7,4",
2496 "Real-Time Polling Service" => "Real-Time Polling Service,7,5",
2497 "Unsolicited Grant Service" => "Unsolicited Grant Service,7,6",
2498 "Unsolicited Grant Service with Activity Detection" => "Unsolicited Grant Service with Activity Detection,7,7",
2499 "Downstream" => "Downstream,7,8"
2500 );
2501 return $types{$part};
2502 }
2503  
2504 sub envelope_header
2505 {
2506 my ( $self ) = shift;
2507 my ( $part ) = shift;
2508 my ( @headers ) =
2509 (
2510 # There is no 7-0
2511 [
2512 ],
2513 # 7-1
2514 # Flow Spec
2515 [
2516 "Envelope",
2517 "Service Number",
2518 "Reserved1Byte",
2519 "Reserved1Byte"
2520 ],
2521 # 7-2
2522 # DOCSIS Service Class Name
2523 [
2524 "Envelope",
2525 "Reserved1Byte",
2526 "Reserved1Byte",
2527 "Reserved1Byte",
2528 "ServiceClassName"
2529 ],
2530 # 7-3
2531 # Best Effort Service
2532 [
2533 "Envelope",
2534 "Reserved1Byte",
2535 "Reserved1Byte",
2536 "Reserved1Byte"
2537 ],
2538 # 7-4
2539 # Non-Real_time Polling Service
2540 [
2541 "Envelope",
2542 "Reserved1Byte",
2543 "Reserved1Byte",
2544 "Reserved1Byte"
2545 ],
2546 # 7-5
2547 # Real_time Polling Service
2548 [
2549 "Envelope",
2550 "Reserved1Byte",
2551 "Reserved1Byte",
2552 "Reserved1Byte"
2553 ],
2554 # 7-6
2555 # Unsolicited Grant Sevice
2556 [
2557 "Envelope",
2558 "Reserved1Byte",
2559 "Reserved1Byte",
2560 "Reserved1Byte"
2561 ],
2562 # 7-7
2563 # Unsolicited Grant Service with Activity Detection
2564 [
2565 "Envelope",
2566 "Reserved1Byte",
2567 "Reserved1Byte",
2568 "Reserved1Byte"
2569 ],
2570 # 7-8
2571 # Downstream Service
2572 [
2573 "Envelope",
2574 "Reserved1Byte",
2575 "Reserved1Byte",
2576 "Reserved1Byte"
2577 ]
2578 );
2579 return \$headers[$part];
2580 }
2581  
2582 sub envelope_array
2583 {
2584 my ( $self ) = shift;
2585 my ( $part ) = shift;
2586 my ( @envelopes ) =
2587 (
2588 # There is no 7-0
2589 [
2590 ],
2591 # 7-1
2592 [
2593 "Token Bucket Rate",
2594 "Token Bucket Size",
2595 "Peak Data Rate",
2596 "Minimum Policed Unit",
2597 "Maximum Packet Size",
2598 "Rate",
2599 "Slack Term"
2600 ],
2601 # 7-2
2602 # A little unsure here.
2603 [
2604 "ServiceClassName"
2605 ],
2606 # 7-3
2607 [
2608 "Traffic Priority",
2609 "Reserved1Byte",
2610 "Reserved1Byte",
2611 "Reserved1Byte",
2612 "Request Transmission Policy",
2613 "Maximum Sustained Traffic Rate",
2614 "Maximum Traffic Burst",
2615 "Minimum Reserved Traffic Rate",
2616 "Assumed Minimum Reserved Traffic Rate Packet Size",
2617 "Maximum Concatenated Burst",
2618 "Required Attribute Mask",
2619 "Forbidden Attribute Mask",
2620 "Attribute Aggregation Rule Mask"
2621 ],
2622 # 7-4
2623 [
2624 "Traffic Priority",
2625 "Reserved1Byte",
2626 "Reserved1Byte",
2627 "Reserved1Byte",
2628 "Request Transmission Policy",
2629 "Maximum Sustained Traffic Rate",
2630 "Maximum Traffic Burst",
2631 "Minimum Reserved Traffic Rate",
2632 "Assumed Minimum Reserved Traffic Rate Packet Size",
2633 "Maximum Concatenated Burst",
2634 "Nominal Polling Interval",
2635 "Required Attribute Mask",
2636 "Forbidden Attribute Mask",
2637 "Attribute Aggregation Rule Mask"
2638 ],
2639 # 7-5
2640 [
2641 "Request Transmission Policy",
2642 "Maximum Sustained Traffic Rate",
2643 "Maximum Traffic Burst",
2644 "Minimum Reserved Traffic Rate",
2645 "Assumed Minimum Reserved Traffic Rate Packet Size",
2646 "Maximum Concatenated Burst",
2647 "Nominal Polling Interval",
2648 "Tolerated Poll Jitter",
2649 "Required Attribute Mask",
2650 "Forbidden Attribute Mask",
2651 "Attribute Aggregation Rule Mask"
2652 ],
2653 # 7-6
2654 [
2655 "Request Transmission Policy",
2656 "Unsolicited Grant Size",
2657 "Grants Per Interval",
2658 "Reserved1Byte",
2659 "Nominal Grant Interval",
2660 "Tolerated Grant Jitter",
2661 "Required Attribute Mask",
2662 "Forbidden Attribute Mask",
2663 "Attribute Aggregation Rule Mask"
2664 ],
2665 # 7-7
2666 [
2667 "Request Transmission Policy",
2668 "Unsolicited Grant Size",
2669 "Grants Per Interval",
2670 "Reserved1Byte",
2671 "Nominal Grant Interval",
2672 "Tolerated Grant Jitter",
2673 "Nominal Polling Interval",
2674 "Tolerated Poll Jitter",
2675 "Required Attribute Mask",
2676 "Forbidden Attribute Mask",
2677 "Attribute Aggregation Rule Mask"
2678 ],
2679 # 7-8
2680 [
2681 "Traffic Priority",
2682 "Reserved1Byte",
2683 "Reserved1Byte",
2684 "Reserved1Byte",
2685 "Maximum Sustained Traffic Rate",
2686 "Maximum Traffic Burst",
2687 "Minimum Reserved Traffic Rate",
2688 "Assumed Minimum Reserved Traffic Rate Packet Size",
2689 "Maximum Downstream Latency",
2690 "Downstream Peak Traffic Rate",
2691 "Required Attribute Mask",
2692 "Forbidden Attribute Mask",
2693 "Attribute Aggregation Rule Mask"
2694 ]
2695  
2696 );
2697 if ($self->{_GLOBAL}{'DEBUG'}> 4 )
2698 {
2699 print "Returning envelope array number '$part'\n";
2700 }
2701 return \$envelopes[$part];
2702 }
2703  
2704 sub attribute_pack
2705 {
2706 my ( $self ) = shift;
2707 my ( $attribute ) = shift;
2708  
2709 my ( %attributes ) =
2710 (
2711 "Assumed Minimum Reserved Traffic Rate Packet Size" => 'n',
2712 "Downstream Peak Traffic Rate" => 'N',
2713 "Attribute Aggregation Rule Mask" => 'N',
2714 "Forbidden Attribute Mask" => 'N',
2715 "Grants Per Interval" => 'C',
2716 "Maximum Concatenated Burst" => 'n',
2717 "Maximum Downstream Latency" => 'N',
2718 "Maximum Packet Size" => 'N',
2719 "Maximum Sustained Traffic Rate" => 'N',
2720 "Maximum Traffic Burst" => 'N',
2721 "Minimum Policed Unit" => 'N',
2722 "Minimum Reserved Traffic Rate" => 'N',
2723 "Nominal Grant Interval" => 'N',
2724 "Nominal Polling Interval" => 'N',
2725 "Peak Data Rate" => 'N',
2726 "Rate" => 'N',
2727 "Request Transmission Policy" => 'N',
2728 "Required Attribute Mask" => 'N',
2729 "Reserved1Byte" => 'C',
2730 "Reserved2Bytes" => 'n',
2731 "Reserved3Bytes" => 'Cn',
2732 "Slack Term" => 'N',
2733 "Token Bucket Rate" => 'N',
2734 "Token Bucket Size" => 'N',
2735 "Tolerated Grant Jitter" => 'N',
2736 "Tolerated Poll Jitter" => 'N',
2737 "Traffic Priority" => 'C',
2738 "Unsolicited Grant Size" => 'n',
2739 "Envelope" => 'C',
2740 "Service Number" => 'C',
2741 "String" => 'String',
2742 "ServiceClassName" => 'String',
2743 "Gate_Flags" => 'C',
2744 "Gate_TOSField" => 'C',
2745 "Gate_TOSMask" => 'C',
2746 "Gate_Class" => 'C',
2747 "Gate_T1" => 'n',
2748 "Gate_T2" => 'n',
2749 "Gate_T3" => 'n',
2750 "Gate_T4" => 'n',
2751 "Classifier_IPProtocolId" => 'n',
2752 "Classifier_TOSField" => 'C',
2753 "Classifier_TOSMask" => 'C',
2754 "Classifier_SourceIP" => 'N',
2755 "Classifier_DestinationIP" => 'N',
2756 "Classifier_SourcePort" => 'n',
2757 "Classifier_DestinationPort" => 'n',
2758 "Classifier_Priority" => 'C',
2759 "EClassifier_IPProtocolId" => 'n',
2760 "EClassifier_TOSField" => 'C',
2761 "EClassifier_TOSMask" => 'C',
2762 "EClassifier_SourceIP" => 'N',
2763 "EClassifier_SourceMask" => 'N',
2764 "EClassifier_DestinationIP" => 'N',
2765 "EClassifier_DestinationMask" => 'N',
2766 "EClassifier_SourcePortStart" => 'n',
2767 "EClassifier_SourcePortEnd" => 'n',
2768 "EClassifier_DestinationPortStart" => 'n',
2769 "EClassifier_DestinationPortEnd" => 'n',
2770 "EClassifier_ClassifierID" => 'n',
2771 "EClassifier_Priority" => 'C',
2772 "EClassifier_State" => 'C',
2773 "EClassifier_Action" => 'C',
2774 "PRKS_IPAddress" => 'N',
2775 "PRKS_Port" => 'n',
2776 "PRKS_Flags" => 'C',
2777 "Reserved" => 'C',
2778 "SRKS_IPAddress" => 'N',
2779 "SRKS_Port" => 'n',
2780 "SRKS_Flags" => 'C',
2781 "Reserved" => 'C',
2782 "BCID_TimeStamp" => 'N',
2783 "BCID_ElementID" => 'String-8',
2784 "BCID_TimeZone" => 'String-8',
2785 "BCID_EventCounter" => 'N',
2786 "RADIUS_EventMessageVersionID" => 'n',
2787 "RADIUS_BCID_Timestamp" => 'N',
2788 "RADIUS_BCID_ElementID" => 'String-8',
2789 "RADIUS_BCID_TimeZone" => 'String-8',
2790 "RADIUS_BCID_EventCounter" => 'N',
2791 "RADIUS_EventMessageType" => 'n',
2792 "RADIUS_ElementType" => 'n',
2793 "RADIUS_ElementID" => 'String-8',
2794 "RADIUS_TimeZone" => 'String-8',
2795 "RADIUS_SequenceNumber" => 'N',
2796 "RADIUS_EventTime" => 'String-18',
2797 "RADIUS_Status" => 'N',
2798 "RADIUS_Priority" => 'C',
2799 "RADIUS_AttributeCount" => 'n',
2800 "RADIUS_EventObject" => 'C',
2801 "RADIUS_QosStatus" => 'N',
2802 "RADIUS_ServiceClassName" => 'String-16'
2803  
2804 );
2805  
2806 return $attributes{$attribute};
2807 }
2808  
2809 sub decode_transaction_identifier
2810 {
2811 my ( $self ) = shift;
2812 my ( $data ) = shift;
2813 my ( $mmtid, $gate_command ) = unpack("nn",$data);
2814 return ($mmtid,$gate_command);
2815 }
2816  
2817 sub decode_application_manager_identifier
2818 {
2819 my ( $self ) = shift;
2820 my ( $data ) = shift;
2821 my ( $amidat, $amidam ) = $self->decode_transaction_identifier($data);
2822 return ($amidat, $amidam);
2823 }
2824  
2825 sub decode_subscriber_id
2826 {
2827 my ( $self ) = shift;
2828 my ( $data ) = shift;
2829 my ( $sub_id ) = unpack("N",$data);
2830 my ( $sub_ip ) = $self->_IpIntToQuad($sub_id);
2831 return ( $sub_id, $sub_ip );
2832 }
2833  
2834 sub decode_gate_id
2835 {
2836 my ( $self ) = shift;
2837 my ( $data ) = shift;
2838 my ( $gate_id ) = unpack("N",$data);
2839 return $gate_id;
2840 }
2841  
2842 sub object_type_decode
2843 {
2844 my ( $self ) = shift;
2845 my ( $major ) = shift;
2846 my ( $minor ) = shift;
2847 my ( @object_types ) =
2848 (
2849 # Nothing at 0
2850 [],
2851 # Transaction Identifier 1
2852 [
2853 "",
2854 "Transaction Identifier"
2855 ],
2856 # Application Manager Identifier 2
2857 [
2858 "",
2859 "Application Manager Identifier"
2860 ],
2861 # Subscriber Identifier 3
2862 [
2863 "",
2864 "Subscriber Identifier"
2865 ],
2866 # Gate Identifier 4
2867 [
2868 "",
2869 "Gate Identifier"
2870 ],
2871 # Sub routines for decode done up to here.
2872 # Gate Specification 5
2873 [
2874 "",
2875 "Gate Specification"
2876 ],
2877 # Classifier 6
2878 [
2879 "",
2880 "Classifier",
2881 "Extended Classifier"
2882 ],
2883 # Traffic Profile 6
2884 [
2885 "",
2886 "Flow Spec",
2887 "DOCSIS Service Class Name",
2888 "Best Effort Service",
2889 "Non-Real-Time Polling Service",
2890 "Real-Time Polling Service",
2891 "Unsolicited Grant Service",
2892 "Unsolicited Grant Service with Activity Detection",
2893 "Downstream"
2894 ],
2895 # Event-Generation-Info 7
2896 [
2897 "",
2898 "Event-Generation-Info",
2899 ],
2900 # Volume Based Usage Limit
2901 [
2902 "",
2903 "Volume Based Usage Limit"
2904 ],
2905 # Time Based Usage Limit
2906 [
2907 "",
2908 "Time Based Usage Limit"
2909 ],
2910 # Opaque Data
2911 [
2912 "",
2913 "Opaque Data"
2914 ],
2915 # Gate Time Info
2916 [
2917 "",
2918 "Gate Time Info"
2919 ],
2920 # Gate Usage Info
2921 [
2922 "",
2923 "Gate Usage Info"
2924 ],
2925 # Packet Cable Error
2926 [
2927 "",
2928 "Packet Cable Error"
2929 ],
2930 # Gate State
2931 [
2932 "",
2933 "Gate State"
2934 ],
2935 # Version Info
2936 [
2937 "",
2938 "Version Info"
2939 ],
2940 # Policy Server Identifier
2941 [
2942 "",
2943 "Policy Server Identifier"
2944 ],
2945 # Synch Options
2946 [
2947 "",
2948 "Synch Options"
2949 ],
2950 # Msg Receipt Key
2951 [
2952 "",
2953 "Msg Receipt Key"
2954 ]
2955 );
2956 return $object_types[$major][$minor];
2957 }
2958  
2959 sub Envelope_Type_remap
2960 {
2961 my ( $self ) = shift;
2962 my ( $envelope_setting ) = shift;
2963 my ( $envelope_type ) = 0;
2964 my ( @types ) = qw [ authorize:1 reserve:2 commit:4 ];
2965 foreach my $find_type ( @types )
2966 {
2967 my ( $name, $number ) = (split(/:/,$find_type))[0,1];
2968 if ( $envelope_setting=~/$name/ig )
2969 {
2970 print "Debug envelope number is '$number'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
2971 $envelope_type+=$number;
2972 }
2973 }
2974 return $envelope_type;
2975 }
2976  
2977 sub encode_cops_message_no_payload
2978 {
2979 my ( $self ) = shift;
2980 my ( $version ) = shift;
2981 my ( $flags ) = shift;
2982 my ( $opcode ) = shift;
2983 my ( $clienttype ) = shift;
2984 my ( $data ) = "";
2985 my ( $header_v ) = ( ($version<<4) | $flags );
2986 my ( $data_block ) = pack("CCn",$header_v,$opcode,$clienttype);
2987 my ( $length ) = pack("N",length($data)+8);
2988 $data_block.=$length.$data;
2989 return $data_block;
2990 }
2991  
2992 sub encode_cops_message
2993 {
2994 my ( $self ) = shift;
2995 my ( $version ) = shift;
2996 my ( $flags ) = shift;
2997 my ( $opcode ) = shift;
2998 my ( $clienttype ) = shift;
2999 my ( $data ) = shift;
3000  
3001 my ( $header_v ) = ( ($version<<4) | $flags );
3002 my ( $data_block ) = pack("CCn",$header_v,$opcode,$clienttype);
3003 my ( $length ) = pack("N",length($data)+8);
3004 $data_block.=$length.$data;
3005 return $data_block;
3006 }
3007  
3008 sub encode_cops_object
3009 {
3010 my ( $self ) = shift;
3011 my ( $cnum ) = shift;
3012 my ( $ctype ) = shift;
3013 my ( $data ) = shift;
3014  
3015 my ( $align ) = 0;
3016  
3017 if ( !$data) { $data=""; }
3018  
3019 $align = length($data) % 4;
3020  
3021 print "Length of data is '".length($data)."' alignment requried is '$align'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
3022  
3023 $align = 4 - $align;
3024  
3025 if ( $align > 0 && $align <4 )
3026 {
3027 print "Align is '$align'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
3028 for ( my $tmp=0; $tmp<$align; $tmp ++ )
3029 {
3030 print "Adding '$tmp'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
3031 $data.=pack("C",0);
3032 }
3033 print "Finished alignment\n\n\n" if $self->{_GLOBAL}{'DEBUG'}>0;
3034 }
3035  
3036 my ( $length ) = length($data)+4;
3037  
3038 my ( $data_block ) = pack("nCC",$length,$cnum,$ctype);
3039 $data_block.=$data;
3040  
3041 return $data_block;
3042 }
3043  
3044 sub generate_subscriber_id
3045 {
3046 my ( $self ) = shift;
3047 my ( $ip ) = shift;
3048 my ( $cops_object ) = "";
3049 $cops_object = $self->encode_cops_object( 2,1, $self->_IpQuadToInt($ip) );
3050 return $cops_object;
3051 }
3052  
3053 sub generate_gate_id
3054 {
3055 my ( $self ) = shift;
3056 my ( $id ) = shift;
3057 my ( $cops_object ) = "";
3058 $cops_object = $self->encode_cops_object( 2,1, pack("N",$id) );
3059 return $cops_object;
3060 }
3061  
3062 sub generate_transaction_id
3063 {
3064 my ( $self ) = shift;
3065 my ( $command ) = shift;
3066 print "\n\ntransaction id is '".$self->encode_gate_ids($command)."'\n\n\n" if $self->{_GLOBAL}{'DEBUG'}>0;
3067 $command = pack("nn",$self->{_GLOBAL}{'TRANSACTION_COUNT'} , $self->encode_gate_ids($command) );
3068 $self->{_GLOBAL}{'TRANSACTION_COUNT'}++;
3069 my ( $cops_object ) = $self->encode_cops_object( 1,1, $command );
3070 return $cops_object;
3071 }
3072  
3073 sub generate_decision
3074 {
3075 my ( $self ) = shift;
3076 my ( $type ) = shift;
3077 my ( $command ) = shift;
3078 my ( $flags ) = shift;
3079 my ( $payload ) = pack("nn", $command, $flags );
3080 my ( $cops_object ) = $self->encode_cops_object( 6,$type, $payload );
3081 return $cops_object;
3082 }
3083  
3084 sub decode_radius_attribute
3085 {
3086 # This has only been tested with the output of
3087 # FreeRadius 2.1.9
3088 # The required perl code is included.
3089 #
3090 my ( $self ) = shift;
3091 my ( $radius_name ) = shift;
3092 my ( $radius_value ) = shift;
3093 my ( $data_return ) = shift;
3094  
3095 my ( $rad_position ) = $self->_decode_radius_attributes($radius_name);
3096  
3097 my ( $radius_array ) = $self->_decode_radius_attribute_layout( $rad_position );
3098  
3099 if ( !$radius_array ) { return 0; }
3100  
3101 $radius_value = (split(/x/,$radius_value))[1];
3102 if ( !$radius_value) { return 0; }
3103 my ( $radius_value_packed ) = "";
3104 # This a bit of a hack.
3105 for (my $a=0;$a<length($radius_value); $a+=2 )
3106 {
3107 my $char = "0x".substr($radius_value,$a,2);
3108 my $pack = pack("C",oct($char));
3109 $radius_value_packed.=$pack;
3110 }
3111  
3112 my ( $done_pack ) = 0;
3113  
3114 foreach my $rad_a ( @{${$radius_array}} )
3115 {
3116 my $type_a = $self->attribute_pack($rad_a);
3117 my $type_v = 0;
3118 my $name = "";
3119 my $data = "";
3120 my $true_length = 0;
3121 if ( $type_a=~/^string/i )
3122 {
3123 my ( $name_s, $length_s ) = (split(/-/,$type_a))[0,1];
3124 $data = substr($radius_value_packed,0,$length_s);
3125 $radius_value_packed = substr( $radius_value_packed, $length_s, length($radius_value_packed)-$length_s);
3126 }
3127 else
3128 {
3129 $data = unpack( $type_a, $radius_value_packed);
3130 if ( $type_a=~/^n$/ ) { $true_length=2; }
3131 if ( $type_a=~/^N$/ ) { $true_length=4; }
3132 if ( $type_a=~/^C$/ ) { $true_length=1; }
3133 $radius_value_packed = substr( $radius_value_packed, $true_length, length($radius_value_packed)-$true_length);
3134 }
3135 $rad_a=~s/^RADIUS_//g;
3136 ${$data_return}{$rad_a}=$data;
3137 if ( $rad_a=~/^EventMessageType$/i )
3138 { ${$data_return}{'EventMessageTypeName'}=$self->_decode_radius_message_type($data); }
3139 $data="";
3140 $true_length=0;
3141 }
3142 return 1;
3143 }
3144  
3145  
3146 sub _decode_radius_attributes
3147 {
3148 my ( $self ) = shift;
3149 my ( $radius_name ) = shift;
3150 my ( %types ) =
3151 (
3152 'CableLabs-Event-Message' => 0,
3153 'CableLabs-QoS-Descriptor' => 1
3154 );
3155 return $types{$radius_name};
3156 }
3157  
3158 sub _decode_radius_attribute_layout
3159 {
3160 my ( $self ) = shift;
3161 my ( $part ) = shift;
3162 my ( @headers ) =
3163 (
3164 # CableLabs-Event-Message
3165 [
3166 "RADIUS_EventMessageVersionID",
3167 "RADIUS_BCID_Timestamp",
3168 "RADIUS_BCID_ElementID",
3169 "RADIUS_BCID_TimeZone",
3170 "RADIUS_BCID_EventCounter",
3171 "RADIUS_EventMessageType",
3172 "RADIUS_ElementType",
3173 "RADIUS_ElementID",
3174 "RADIUS_TimeZone",
3175 "RADIUS_SequenceNumber",
3176 "RADIUS_EventTime",
3177 "RADIUS_Status",
3178 "RADIUS_Priority",
3179 "RADIUS_AttributeCount",
3180 "RADIUS_EventObject"
3181 ],
3182 # CableLabs-QoS-Descriptor
3183 [
3184 "RADIUS_QosStatus",
3185 "RADIUS_ServiceClassName"
3186 ]
3187 );
3188 return \$headers[$part];
3189 }
3190  
3191 sub _decode_radius_message_type
3192 {
3193 my ( $self ) = shift;
3194 my ( $message ) = shift;
3195 my ( @message_types ) =
3196 (
3197 "Reserved",
3198 "Signaling_Start",
3199 "Signaling_Stop",
3200 "Database_Query",
3201 "Intelligent_Peripheral_Usage_Start",
3202 "Intelligent_Peripheral_Usage_Stop",
3203 "Service_Instance",
3204 "QoS_Reserve",
3205 "QoS_Release",
3206 "Service_Activation",
3207 "Service_Deactivation",
3208 "Media_Report",
3209 "Signal_Instance",
3210 "Interconnect_(Signaling)_Start",
3211 "Interconnect_(Signaling)_Stop",
3212 "Call_Answer",
3213 "Call_Disconnect",
3214 "Time_Change",
3215 "18_Missed",
3216 "QoS_Commit",
3217 "Media_Alive",
3218 "Conference_Party_Change"
3219 );
3220  
3221 if ( $message_types[$message] )
3222 {
3223 return $message_types[$message];
3224 }
3225 return "Unknown";
3226 }
3227  
3228 sub _IpQuadToInt
3229 {
3230 my ($self)= shift;
3231 my($Quad) = shift;
3232 if ( !$Quad ) { return 0; }
3233 my($Ip1, $Ip2, $Ip3, $Ip4) = split(/\./, $Quad);
3234 my($IpInt) = (($Ip1 << 24) | ($Ip2 << 16) | ($Ip3 << 8) | $Ip4);
3235 return($IpInt);
3236 }
3237  
3238 sub _IpIntToQuad
3239 {
3240 my ( $self ) = shift;
3241 my($Int) = shift;
3242 my($Ip1) = $Int & 0xFF; $Int >>= 8;
3243 my($Ip2) = $Int & 0xFF; $Int >>= 8;
3244 my($Ip3) = $Int & 0xFF; $Int >>= 8;
3245 my($Ip4) = $Int & 0xFF; return("$Ip4.$Ip3.$Ip2.$Ip1");
3246 }
3247  
3248 sub decode_64bit_number
3249 {
3250 # see comments on 64bit stuff.
3251 my ( $self ) = shift;
3252 my ( $message ) = shift;
3253 my ($part1,$part2) = unpack("NN",$message);
3254 $part1 = $part1<<32;
3255 $part1+=$part2;
3256 return $part1;
3257 }
3258  
3259 sub encode_64bit_number
3260 {
3261 # It seems Q does not work, well not for me
3262 # and this is the quickest way to fix it.
3263 # You STILL NEED 64 BIT SUPPORT!!
3264 my ( $self ) = shift;
3265 my ( $number ) = shift;
3266 # any bit to 64bit number in.
3267 my($test1) = $number & 0xFFFFFFFF; $number >>= 32;
3268 my($test2) = $number & 0xFFFFFFFF;
3269 my $message = pack("NN",$test2,$test1);
3270 return $message;
3271 }
3272  
3273 sub general_pack
3274 {
3275 my ( $self ) = shift;
3276 my ( $data ) = shift;
3277 my ( $array_pointer ) = shift;
3278 my ( $prefix ) = shift;
3279 my ( $packed ) = "";
3280 if ( !$prefix ) { $prefix=""; }
3281 foreach my $env_c ( @{${$array_pointer}} )
3282 {
3283 my $type_c = $self->attribute_pack($env_c);
3284 my $type_v = 0;
3285 my $type_push = $prefix.$env_c;
3286 if ( $env_c=~/^reserved/i )
3287 {
3288 print "Reserved type found.\n" if $self->{_GLOBAL}{'DEBUG'}>0;
3289 $packed.=pack("C",0);
3290 }
3291 if ( $type_c=~/^string/i )
3292 {
3293 my ( $type, $numlen ) = (split(/-/,$type_c))[0,1];
3294 if ( !$numlen )
3295 {
3296 $packed.=$self->align_string(${$data}{$type_push});
3297 }
3298 else
3299 {
3300 $packed.=pack("A[$numlen]",${$data}{$type_push});
3301 }
3302 }
3303 if ( $type_c!~/^string/i && $env_c!~/^reserved/i )
3304 {
3305 if ( !${$data}{$type_push} )
3306 {
3307 $type_v=0;
3308 }
3309 else
3310 {
3311 if ( ($env_c=~/IP/ ) && ( $env_c!~/IPProtocolId/))
3312 {
3313 $type_v = $self->_IpQuadToInt(${$data}{$type_push});
3314 }
3315 else
3316 {
3317 $type_v = ${$data}{$type_push};
3318 }
3319 }
3320 $packed.=pack( $type_c , $type_v );
3321 }
3322 print "General Encoder Name us '$env_c' value is '$type_v' type is '$type_c'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
3323 }
3324 return $packed;
3325 }
3326  
3327 =cut
3328  
3329 sub function2 {
3330 }
3331  
3332 =head1 AUTHOR
3333  
3334 shamrock@cpan.org, C<< <shamrock at cpan.org> >>
3335  
3336 =head1 BUGS
3337  
3338 - Sync messages to Cisco CMTS do not seem to work. I have tried alternative
3339 formats, headers, etc but to no avail. They do work to Motorola and Aris.
3340 I have raised this with Cisco but do not expect a response any time soon.
3341 If anyone has a packet trace with a working Synch using a Cisco CMTS that
3342 would be useful.
3343  
3344 - The different traffic profiles need work, see examples/profiles.
3345 The following examples produce an 'Unspecified error' and may be down to
3346 the values being used. If any one can help with the values that should be
3347 used, packet trace, then I can look at improving their use.
3348  
3349 Flow Spec
3350 Non-Real-Time Polling Service
3351 Real-Time Polling Service
3352 Unsolicited Grant Service
3353  
3354  
3355 Please report any bugs or feature requests to C<bug-cops-cmts at rt.cpan.org>, or through
3356 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=COPS-Client>. I will be notified, and then you'll
3357 automatically be notified of progress on your bug as I make changes.
3358  
3359 =head1 SUPPORT
3360  
3361 Please do email me if you have any issues so they can be looked at as soon
3362 as possible.
3363  
3364 You can find documentation for this module with the perldoc command.
3365  
3366 perldoc COPS::Client
3367  
3368  
3369 You can also look for information at:
3370  
3371 =over 4
3372  
3373 =item * RT: CPAN's request tracker
3374  
3375 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=COPS-Client>
3376  
3377 =item * AnnoCPAN: Annotated CPAN documentation
3378  
3379 L<http://annocpan.org/dist/COPS-Client>
3380  
3381 =item * CPAN Ratings
3382  
3383 L<http://cpanratings.perl.org/d/COPS-Client>
3384  
3385 =item * Search CPAN
3386  
3387 L<http://search.cpan.org/dist/COPS-Client/>
3388  
3389 =back
3390  
3391  
3392 =head1 ACKNOWLEDGEMENTS
3393  
3394  
3395 =head1 COPYRIGHT & LICENSE
3396  
3397 Copyright 2012 shamrock@cpan.org, all rights reserved.
3398  
3399 This program is free software; you can redistribute it and/or modify it
3400 under the same terms as Perl itself.
3401  
3402  
3403 =cut
3404  
3405 1; # End of COPS::Client