blib/lib/COPS/Client.pm untested

filter: Statementstmt 0.0% 0 / 1311 Branchbran 0.0% 0 / 412 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 0.0% 0 / 102 Podpod 0.0% 0 / 97 totaltotal 0.0% 0 / 1977 CC 207 SCAR 61.7
CC
207
Cov
0%
check_data_available
2550.0
get_listener_connect
342.0
encode_sub_traffic
210.0
CRAP 475.9
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.06
16  
17 =cut
18  
19 our $VERSION = '0.06';
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 # For you code part I think for the DOCSIS Service Class Name, if stype is 2 [snum/stype/len 7/2/16],
1572 # you need to ignore auth, reserve and commit
1573 # In this context $parts[1] = snum $parts[2] = stype
1574 # we set auth, reserved and commit to 0 as we dont need them
1575 if ( $parts[2]==2 )
1576 {
1577 $auth=0; $reserved=0; $commit=0;
1578 }
1579  
1580 print "Auth is '$auth'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1581 print "Reserved is '$reserved'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1582 print "Commit is '$commit'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1583  
1584 if ( $auth > 0 )
1585 {
1586 print "\n\nENTERING AUTH PHASE\n\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1587 $packed.=$self->encode_envelope( $parts[2], "Envelope_authorize_", $data ); }
1588 if ( $reserved > 0 )
1589 {
1590 print "\n\nENTERING RESERVED PHASE\n\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1591 $packed.=$self->encode_envelope( $parts[2], "Envelope_reserve_", $data ); }
1592 if ( $commit > 0 )
1593 {
1594 print "\n\nENTERING COMMIT PHASE\n\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1595 $packed.=$self->encode_envelope( $parts[2], "Envelope_commit_", $data ); }
1596  
1597 if ( $self->{_GLOBAL}{'DEBUG'}>4 )
1598 {
1599 print "Envelope ENCODER \n\n\n\n\n";
1600 for($a=0;$a<length($packed);$a++)
1601 {
1602 printf("%02x-", ord(substr($packed,$a,2)));
1603 }
1604 print "\n";
1605 }
1606  
1607 if ( $self->{_GLOBAL}{'DEBUG'}>4 )
1608 {
1609 print "Length of complete envelope is '".length($packed)."'\n";
1610 }
1611  
1612 my ( $length ) = pack("n",(length($packed)+2));
1613  
1614 if ( $self->{_GLOBAL}{'DEBUG'}>4 )
1615 {
1616 print "Header attached to envelope is '".length($length)."'\n";
1617 }
1618  
1619 return $length.$packed;
1620 }
1621  
1622 sub encode_envelope
1623 {
1624 my ( $self ) = shift;
1625 my ( $number ) = shift;
1626 my ( $type ) = shift;
1627 my ( $data ) = shift;
1628 my ( $result ) = "";
1629 if ( $self->{_GLOBAL}{'DEBUG'}>4 )
1630 {
1631 print "Envelope encoding information Number '$number' type '$type'\n";
1632 }
1633 my $envelope_main = $self->envelope_array( $number );
1634 $result.=$self->general_pack( $data, $envelope_main, $type );
1635 return $result;
1636 }
1637  
1638 sub encode_sub_amid
1639 {
1640 my ( $self ) = shift;
1641 my ( $app_type ) = shift;
1642 my ( $man_tag ) = shift;
1643 my ( $packed ) = pack("CCnn", 2,1,$app_type, $man_tag);
1644 my ( $length ) = pack("n",(length($packed)+2));
1645 return $length.$packed;
1646 }
1647  
1648 sub encode_sub_subscriber_id
1649 {
1650 my ( $self ) = shift;
1651 my ( $type ) = shift;
1652 my ( $ip ) = shift;
1653  
1654 my ( $convert_ip ) = $self->_IpQuadToInt($ip);
1655 my ( $packed ) = pack("CCN", 3,$type,$convert_ip);
1656 my ( $length ) = pack("n",(length($packed)+2));
1657 return $length.$packed;
1658 }
1659  
1660 sub encode_sub_transaction_id
1661 {
1662 my ( $self ) = shift;
1663 my ( $gate_command ) = shift;
1664 my ( $trans_id ) = shift;
1665 my ( $packed ) = pack("CCnn", 1,1,$trans_id,$gate_command);
1666 my ( $length ) = pack("n",(length($packed)+2));
1667 return $length.$packed;
1668 }
1669  
1670 sub encode_sub_decision
1671 {
1672 my ( $self ) = shift;
1673 my ( $snum ) = shift;
1674 my ( $stype ) = shift;
1675 my ( $data ) = shift;
1676  
1677 my ( $pad ) = 0;
1678  
1679 my ($stub_pri, $sub_sec) = (split(/\./,sprintf("%f",length($data)/4)))[0,1];
1680 if ( $stub_pri ==0 && $sub_sec>0 )
1681 {
1682 $pad = 4 - length($data);
1683 }
1684  
1685 if ( $stub_pri > 0 && $sub_sec<1 )
1686 {
1687 my $temp = $stub_pri*4;
1688 $pad = $temp - length($data);
1689 }
1690  
1691 for ( $a=0; $a<$pad; $a++)
1692 {
1693 $data.=pack("C",0);
1694 }
1695  
1696 my ( $length ) = length ($data)+2;
1697  
1698 my $sub_decision = pack("nCCN",$length, $snum, $stype, $data);
1699  
1700 return $sub_decision;
1701 }
1702  
1703 sub send_message
1704 {
1705 my ( $self ) = shift;
1706 my ( $message ) = shift;
1707 if ( !$self->{_GLOBAL}{'Handle'} ) { return 0; }
1708 my ( $length_sent );
1709 eval {
1710 local $SIG{ALRM} = sub { die "alarm\n" };
1711 alarm 1;
1712 $length_sent = syswrite ( $self->{_GLOBAL}{'Handle'}, $message );
1713 alarm 0;
1714 };
1715  
1716 print "length sent is '$length_sent'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1717  
1718 if ( $@=~/alarm/i )
1719 { return 0; }
1720  
1721 print "Sending message of size '".length($message)."'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1722  
1723 if ( $self->{_GLOBAL}{'DEBUG'}>4 )
1724 {
1725 for($a=0;$a<length($message);$a++)
1726 {
1727 printf("%02x-", ord(substr($message,$a,2)));
1728 }
1729 print "\n";
1730 }
1731  
1732 if ( $length_sent==length($message) )
1733 { return 1; }
1734 return 0;
1735 }
1736  
1737 sub set_gate_id
1738 {
1739 my ( $self ) = shift;
1740 my ( $gate_id ) = shift;
1741 $self->{_GLOBAL}{'current_gate_id'} = $gate_id;
1742 return 1;
1743 }
1744  
1745 sub get_gate_id
1746 {
1747 my ( $self ) = shift;
1748 if ( !$self->{_GLOBAL}{'current_gate_id'} )
1749 {
1750 return "";
1751 }
1752 return $self->{_GLOBAL}{'current_gate_id'};
1753 }
1754  
1755 sub set_command
1756 {
1757 my ( $self ) = shift;
1758 my ( $command ) = shift;
1759 $self->{_GLOBAL}{'current_command'} = $command;
1760 return 1;
1761 }
1762  
1763 sub get_command
1764 {
1765 my ( $self ) = shift;
1766 return $self->{_GLOBAL}{'current_command'};
1767 }
1768  
1769 sub clear_command
1770 {
1771 my ( $self ) = shift;
1772 $self->{_GLOBAL}{'current_command'}="";
1773 return 1;
1774 }
1775  
1776 sub envelope_add
1777 {
1778 my ( $self ) = shift;
1779 my ( $data ) = shift;
1780 my ( %test ) ;
1781 while (my($field, $val) = splice(@{$data}, 0, 2))
1782 {
1783 my $temp = "current_Envelope_".$field;
1784 $self->{_GLOBAL}{$temp} = $val;
1785 $test{$field}= $val;
1786 }
1787  
1788 my $encode_envelope = $self->encode_sub_traffic(\%test);
1789 $self->{_GLOBAL}{'Envelope_Encoded'} = $encode_envelope;
1790 return 1;
1791 }
1792  
1793 sub envelope_get
1794 {
1795 my ( $self ) = shift;
1796 if ( $self->{_GLOBAL}{'Envelope_Encoded'} )
1797 {
1798 return $self->{_GLOBAL}{'Envelope_Encoded'};
1799 }
1800 return "";
1801 }
1802  
1803 sub classifier_add
1804 {
1805 my ( $self ) = shift;
1806 my ( $data ) = shift;
1807 my ( %test );
1808 while (my($field, $val) = splice(@{$data}, 0, 2))
1809 { $test{$field}= $val; }
1810 my $encode_classifier = $self->encode_sub_classifier ( \%test );
1811 $self->{_GLOBAL}{'Classifier_Encoded'} .= $encode_classifier;
1812 return 1;
1813 }
1814  
1815 sub classifier_clear
1816 {
1817 my ( $self ) = shift;
1818 $self->{_GLOBAL}{'Classifier_Encoded'} = "";
1819 }
1820  
1821 sub classifier_get
1822 {
1823 my ( $self ) = shift;
1824 if ( $self->{_GLOBAL}{'Classifier_Encoded'} )
1825 {
1826 return $self->{_GLOBAL}{'Classifier_Encoded'};
1827 }
1828 return "";
1829 }
1830  
1831  
1832 sub subscriber_set
1833 {
1834 my ( $self ) = shift;
1835 my ( $subscriber_type ) = shift;
1836 my ( $ip ) = shift;
1837 if ( $subscriber_type=~/^ipv4$/i )
1838 {
1839 print "Entry IP is '$ip'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1840 my ( $test ) = $self->_IpQuadToInt($ip);
1841 $ip = $self->_IpIntToQuad($test);
1842 print "Exit IP is '$ip'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1843 $self->{_GLOBAL}{'cpe_device_type'}=$subscriber_type;
1844 $self->{_GLOBAL}{'cpe_device_ip'}=$ip;
1845 }
1846  
1847 if ( $subscriber_type=~/^ipv6$/i )
1848 {
1849 my @ipv6 = split(/:/,$ip);
1850 my $test = "";
1851 if ( scalar(@ipv6)!=8 )
1852 {
1853 print "IPv6 entry invalid, full address required.\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1854 return 1;
1855 }
1856 foreach my $ipv6_seg ( @ipv6 )
1857 {
1858 my $testa = hex($ipv6_seg);
1859 $test .= pack("n",$testa);
1860 }
1861 $self->{_GLOBAL}{'cpe_device_type'}=$subscriber_type;
1862 $self->{_GLOBAL}{'cpe_device_ip'}=$test;
1863 }
1864 return 1;
1865 }
1866  
1867 sub subscriber_type
1868 {
1869 my ( $self ) = shift;
1870 if ( !$self->{_GLOBAL}{'cpe_device_type'} )
1871 { return 0; }
1872 if ( $self->{_GLOBAL}{'cpe_device_type'}=~/^ipv4$/ )
1873 { return 1; }
1874 if ( $self->{_GLOBAL}{'cpe_device_type'}=~/^ipv6$/ )
1875 { return 2; }
1876 return 0;
1877 }
1878  
1879 sub subscriber_clear
1880 {
1881 my ( $self ) = shift;
1882 $self->{_GLOBAL}{'cpe_device_ip'}="";
1883 $self->{_GLOBAL}{'cpe_device_type'}=0;
1884 return 1;
1885 }
1886  
1887 sub subscriber_get
1888 {
1889 my ( $self ) = shift;
1890 if ( !$self->{_GLOBAL}{'cpe_device_ip'} )
1891 {
1892 return $self->_IpIntToQuad(0);
1893 }
1894 return $self->{_GLOBAL}{'cpe_device_ip'};
1895 }
1896  
1897 sub get_error
1898 {
1899 my ( $self ) = shift;
1900 return $self->{_GLOBAL}{'ERROR'};
1901 }
1902  
1903 sub decode_message_type
1904 {
1905 my ( $self ) = shift;
1906 my ( $decode_data ) = $self->{_GLOBAL}{'data_received'};
1907  
1908 $self->{_GLOBAL}{'message_count'}=0;
1909  
1910 if ( !$decode_data )
1911 {
1912 $self->{_GLOBAL}{'message_opcode'} = "NULL";
1913 return 1; }
1914  
1915 my ( $vflags, $v2, $client_id, $total_length ) = unpack("CCnN",$decode_data);
1916 my ( $version ) = $vflags;
1917  
1918 $version >>= 4; $version = $version & 0x0F; $vflags = $vflags & 0x0F;
1919 my ( $opcode ) = $self->decode_cops_operations( $v2 );
1920  
1921 print "Version is '$version' flags is '$vflags'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1922 print "Opcode is '$opcode'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1923 print "client id is '$client_id'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1924  
1925 print "New total length is '$total_length'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1926 print "Length of dd '".length($decode_data)."\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1927  
1928 $decode_data = substr ( $decode_data, 8, $total_length-8 );
1929  
1930  
1931 if ( $self->{_GLOBAL}{'DEBUG'}>0 )
1932 {
1933 for($a=0;$a<length($decode_data);$a++)
1934 {
1935 printf("%02x-", ord(substr($decode_data,$a,1)));
1936 }
1937 print "\n";
1938 }
1939  
1940  
1941 print "Remaing data set length is '".length($decode_data)."'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1942  
1943 while ( length($decode_data)> 0 )
1944 {
1945 $decode_data = $self-> decode_cops_message ( $decode_data );
1946 }
1947  
1948  
1949 print "all cops messages decoded.\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1950  
1951 $self->{_GLOBAL}{'message_client_id'} = $client_id;
1952 $self->{_GLOBAL}{'message_opcode'} = $opcode;
1953  
1954 $self->{_GLOBAL}{'data_received'} = substr( $self->{_GLOBAL}{'data_received'}, $total_length, length($self->{_GLOBAL}{'data_received'})-$total_length);
1955  
1956 return 1;
1957 }
1958  
1959 sub decode_cops_message
1960 {
1961 my ( $self ) = shift;
1962 my ( $data ) = shift;
1963  
1964 my ( $total_length, $cnum, $ctype ) = unpack("nCC",$data);
1965  
1966 print "Cops message length is '$total_length'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1967  
1968 $cnum = $self->decode_c_num_type ( $cnum );
1969 $ctype = $self->decode_c_num_type ( $ctype );
1970  
1971 print "Cnum is '$cnum'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1972 print "CType is '$ctype'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1973  
1974 print "Total length is '$total_length'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1975  
1976 my $message = substr($data,4, $total_length-4 );
1977  
1978 print "Pre length data is '".length($message)."'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1979  
1980 $self->{_GLOBAL}{'message'}{ $cnum }{$ctype } = $message;
1981  
1982 print "Length of message is '".length($self->{_GLOBAL}{'message'}{ $cnum }{$ctype })."'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
1983
1984 $data = substr($data,$total_length, length($data)-$total_length );
1985  
1986 return $data;
1987 }
1988  
1989  
1990 sub decode_cops_operations
1991 {
1992 my ( $self ) = shift;
1993 my ( $opcode ) = shift;
1994  
1995 my %opcodes = (
1996 '1' => 'REQ',
1997 '2' => 'DEC',
1998 '3' => 'RPT',
1999 '4' => 'DRQ',
2000 '5' => 'SSQ',
2001 '6' => 'OPN',
2002 '7' => 'CAT',
2003 '8' => 'CC',
2004 '9' => 'KA',
2005 '10' => 'SSC'
2006 );
2007  
2008 return $opcodes{$opcode};
2009 }
2010  
2011 sub decode_c_num_type
2012 {
2013 my ( $self ) = shift;
2014 my ( $opcode ) = shift;
2015  
2016 my %cnumtypes = (
2017 '1' => 'Handle',
2018 '2' => 'Context',
2019 '3' => 'In Interface',
2020 '4' => 'Out Interface',
2021 '5' => 'Reason Code',
2022 '6' => 'Decision',
2023 '7' => 'LPDP Decision',
2024 '8' => 'Error',
2025 '9' => 'Client Specific Info',
2026 '10' => 'Keep-Alive Timer',
2027 '11' => 'PEP Identification',
2028 '12' => 'Report Type',
2029 '13' => 'PDP Redirect Address',
2030 '14' => 'Last PDP Address',
2031 '15' => 'Accounting Timer',
2032 '16' => 'Message Integrity'
2033 );
2034  
2035 return $cnumtypes{$opcode};
2036 }
2037  
2038 sub decode_gate_actions
2039 {
2040 my ( $self ) = shift;
2041 my ( $transcode ) = shift;
2042 my ( %transaction_ids ) = (
2043 '1' => 'GATE-ALLOC',
2044 '2' => 'GATE-ALLOC-ACK',
2045 '3' => 'GATE-ALLOC-ERR',
2046 '4' => 'GATE-SET',
2047 '5' => 'GATE-SET-ACK',
2048 '6' => 'GATE-SET-ERR',
2049 '7' => 'GATE-INFO',
2050 '8' => 'GATE-INFO-ACK',
2051 '9' => 'GATE-INFO-ERR',
2052 '10' => 'GATE-DELETE',
2053 '11' => 'GATE-DELETE-ACK',
2054 '12' => 'GATE-DELETE-ERR',
2055 '13' => 'GATE-OPEN',
2056 '14' => 'GATE-CLOSE'
2057 );
2058 return $transaction_ids{$transcode} if $transaction_ids{$transcode};
2059 }
2060  
2061 sub encode_gate_ids
2062 {
2063 my ( $self ) = shift;
2064 my ( $transcode ) = shift;
2065 my ( %transaction_ids ) = (
2066 'GATE-ALLOC' => '1',
2067 'GATE-ALLOC-ACK' => '2',
2068 'GATE-ALLOC-ERR' => '3',
2069 'GATE-SET' => '4',
2070 'GATE-SET-ACK' => '5',
2071 'GATE-SET-ERR' => '6',
2072 'GATE-INFO' => '7',
2073 'GATE-INFO-ACK' => '8',
2074 'GATE-INFO-ERR' => '9',
2075 'GATE-DELETE' => '10',
2076 'GATE-DELETE-ACK' => '11',
2077 'GATE-DELETE-ERR' => '12',
2078 'GATE-OPEN' => '13',
2079 'GATE-CLOSE' => '14'
2080 );
2081 return $transaction_ids{$transcode} if $transaction_ids{$transcode};
2082 }
2083  
2084 sub classifier_arrays
2085 {
2086 my ( $self ) = shift;
2087 my ( $type ) = shift;
2088 my ( @classifiers ) =
2089 (
2090 [],
2091 [
2092 "Classifier_IPProtocolId",
2093 "Classifier_TOSField",
2094 "Classifier_TOSMask",
2095 "Classifier_SourceIP",
2096 "Classifier_DestinationIP",
2097 "Classifier_SourcePort",
2098 "Classifier_DestinationPort",
2099 "Classifier_Priority",
2100 "Reserved1Byte",
2101 "Reserved1Byte",
2102 "Reserved1Byte"
2103 ],
2104 [
2105 "EClassifier_IPProtocolId",
2106 "EClassifier_TOSField",
2107 "EClassifier_TOSMask",
2108 "EClassifier_SourceIP",
2109 "EClassifier_SourceMask",
2110 "EClassifier_DestinationIP",
2111 "EClassifier_DestinationMask",
2112 "EClassifier_SourcePortStart",
2113 "EClassifier_SourcePortEnd",
2114 "EClassifier_DestinationPortStart",
2115 "EClassifier_DestinationPortEnd",
2116 "EClassifier_ClassifierID",
2117 "EClassifier_Priority",
2118 "EClassifier_State",
2119 "EClassifier_Action",
2120 "Reserved1Byte",
2121 "Reserved1Byte",
2122 "Reserved1Byte"
2123 ]
2124 );
2125 return \$classifiers[$type];
2126 }
2127  
2128 sub packetcable_errors
2129 {
2130 my ( $self ) = shift;
2131 my ( $error ) = shift;
2132 my ( @errors ) =
2133 (
2134 "",
2135 "Insufficient resources",
2136 "Unknown Gate ID",
2137 "Unknown",
2138 "Unknown",
2139 "Unknown",
2140 "Missing Required Object",
2141 "Invalid Object",
2142 "Volume based usage limit exceeded",
2143 "Time based usage limit exceeded",
2144 "Session Class Limit Exceeded",
2145 "Undefined Service Class Name",
2146 "Incompatible Envelope",
2147 "Invalid subscriber identifier",
2148 "Unauthorized AMID",
2149 "Number of Classifiers not supported",
2150 "Policy Exception",
2151 "Invalid field value in object",
2152 "Transport Error",
2153 "Unknown gate command",
2154 "DOCSIS 1.0 CM",
2155 "Number of SIDs exceeded in CM",
2156 "Number of SIDs exceeded in CMTS",
2157 "Unauthorized PSID",
2158 "No state for PDPD",
2159 "Unsupport Sync Type",
2160 "State data incomplete",
2161 );
2162  
2163 if (!$errors[$error])
2164 { return "Other, unspecified error"; }
2165 return $errors[$error];
2166 }
2167  
2168 sub gate_states
2169 {
2170 my ( $self ) = shift;
2171 my ( $state_no ) = shift;
2172 my ( @states ) =
2173 (
2174 "",
2175 "Idle/Closed",
2176 "Auhorized",
2177 "Reserved",
2178 "Committed",
2179 "Committed Recovery"
2180 );
2181 if ( !$states[$state_no] )
2182 { return "Unknown"; }
2183 return $states[$state_no];
2184 }
2185  
2186 sub gate_reasons
2187 {
2188 my ( $self ) = shift;
2189 my ( $reason ) = shift;
2190 my ( @reasons ) =
2191 (
2192 "",
2193 "Close Initiated by CMTS because of reservation reassignment",
2194 "Close Initiated by CMTS because of lack of DOCSIS responses",
2195 "Close Initiated by CMTS because of timer T1 expiry",
2196 "Close Initiated by CMTS because of timer T2 expiry",
2197 "Inactivity timer (T3) expired",
2198 "Close Initiated by CMTS because of a lack of reservation maintenance",
2199 "Gate state unchanged, but volume limit reached",
2200 "Close Initiated by CMTS because of timer T4 expiry",
2201 "Gate State unchanged, but timer T2 expiry caused reservation reduction",
2202 "Gate State unchanged, but time limit reached",
2203 "Close Initiated by PS or CMTS, volume limit reached",
2204 "Close Initiated by PS or CMTS, time limit reached",
2205 "Close Initiated by CMTS, other"
2206 );
2207  
2208 if ( !$reasons[$reason] )
2209 {
2210 return "Other";
2211 }
2212 return $reasons[$reason];
2213 }
2214  
2215 sub gate_array
2216 {
2217 my ( $self ) = shift;
2218 my ( @gate_headers ) =
2219 (
2220 [
2221 "Gate_Flags",
2222 "Gate_TOSField",
2223 "Gate_TOSMask",
2224 "Gate_Class",
2225 "Gate_T1",
2226 "Gate_T2",
2227 "Gate_T3",
2228 "Gate_T4"
2229 ]
2230 );
2231 return \$gate_headers[0];
2232 }
2233  
2234 sub rks_array
2235 {
2236 my ( $self ) = shift;
2237 my ( @rks_headers ) =
2238 (
2239 [
2240 "PRKS_IPAddress",
2241 "PRKS_Port",
2242 "Reserved",
2243 "Reserved",
2244 "SRKS_IPAddress",
2245 "SRKS_Port",
2246 "Reserved",
2247 "Reserved",
2248 "BCID_TimeStamp",
2249 "BCID_ElementID",
2250 "BCID_TimeZone",
2251 "BCID_EventCounter"
2252 ]
2253 );
2254 return \$rks_headers[0];
2255 }
2256  
2257 sub rks_set
2258 {
2259 my ( $self ) = shift;
2260 my ( $data ) = shift;
2261 my ( %test );
2262 while (my($field, $val) = splice(@{$data}, 0, 2))
2263 {
2264 $test{$field}= $val;
2265 }
2266 my $rks_encoded = $self->rks_encode(\%test);
2267 $self->{_GLOBAL}{'RKS_Encoded'} = $rks_encoded;
2268 return 1;
2269 }
2270  
2271 sub rks_clear
2272 {
2273 my ( $self ) = shift;
2274 $self->{_GLOBAL}{'RKS_Encoded'} = "";
2275 return 1;
2276 }
2277  
2278 sub rks_encode
2279 {
2280 my ( $self ) = shift;
2281 my ( $data ) = shift;
2282 my ( $packed ) = "";
2283 my ( $done_pack ) =0;
2284  
2285 $packed = pack("CC",8,1);
2286 my ( $rks_headers ) = $self->rks_array();
2287 $packed .= $self->general_pack ( $data, $rks_headers );
2288 my ( $length ) = pack("n",(length($packed)+2));
2289 return $length.$packed;
2290 }
2291  
2292 sub rks_get
2293 {
2294 my ( $self ) = shift;
2295 if ( $self->{_GLOBAL}{'RKS_Encoded'} )
2296 {
2297 return $self->{_GLOBAL}{'RKS_Encoded'};
2298 }
2299 return "";
2300 }
2301  
2302  
2303 sub opaque_clear
2304 {
2305 my ( $self ) = shift;
2306 $self->{_GLOBAL}{'OpaqueData'}="";
2307 return 1;
2308 }
2309  
2310 sub opaque_set
2311 {
2312 my ( $self ) = shift;
2313 my ( $data ) = shift;
2314 my ( %test );
2315 my ( $encoded ) = "";
2316 while (my($field, $val) = splice(@{$data}, 0, 2))
2317 { $test{$field}= $val }
2318 if ( $test{'OpaqueData'} )
2319 {
2320 $encoded = pack("CC",11,1);
2321 $encoded.= $self->align_string( $test{'OpaqueData'} );
2322 my ( $length ) = pack("n",(length($encoded)+2));
2323 $encoded= $length.$encoded;
2324 }
2325 $self->{_GLOBAL}{'OpaqueData'} = $encoded;
2326 return 1;
2327 }
2328  
2329 sub timebase_set
2330 {
2331 my ( $self ) = shift;
2332 my ( $data ) = shift;
2333 if ( $self->{_GLOBAL}{'DEBUG'}> 4 )
2334 {
2335 print "Time set to '".$data."'\n";
2336 }
2337 if ( $data > 0 )
2338 {
2339 my $timer_encode = $self->encode_time_limit( $data );
2340 $self->{_GLOBAL}{'TimeLimit'} = $timer_encode;
2341 }
2342 return 1;
2343 }
2344  
2345 sub timebase_clear
2346 {
2347 my ( $self ) = shift;
2348 $self->{_GLOBAL}{'TimeLimit'} = "";
2349 return 1;
2350 }
2351  
2352 sub volume_set
2353 {
2354 my ( $self ) = shift;
2355 my ( $data ) = shift;
2356 if ( $data>0 )
2357 {
2358 my $timer_encode = $self->encode_byte_limit( $data );
2359 $self->{_GLOBAL}{'VolumeLimit'} = $timer_encode;
2360 }
2361 return 1;
2362 }
2363  
2364 sub volume_clear
2365 {
2366 my ( $self ) = shift;
2367 $self->{_GLOBAL}{'VolumeLimit'} = "";
2368 return 1;
2369 }
2370  
2371 sub opaque_get
2372 {
2373 my ( $self ) = shift;
2374 if ( $self->{_GLOBAL}{'OpaqueData'} )
2375 {
2376 return $self->{_GLOBAL}{'OpaqueData'};
2377 }
2378 return "";
2379 }
2380  
2381 sub volume_get
2382 {
2383 my ( $self ) = shift;
2384 if ( length($self->{_GLOBAL}{'VolumeLimit'})>0 )
2385 {
2386 return $self->{_GLOBAL}{'VolumeLimit'};
2387 }
2388 return "";
2389 }
2390  
2391 sub timebase_get
2392 {
2393 my ( $self ) = shift;
2394 if ( length($self->{_GLOBAL}{'TimeLimit'})>0 )
2395 {
2396 return $self->{_GLOBAL}{'TimeLimit'};
2397 }
2398 return "";
2399 }
2400  
2401 sub gate_specification_add
2402 {
2403 my ( $self ) = shift;
2404 my ( $data ) = shift;
2405 my ( %test );
2406 while (my($field, $val) = splice(@{$data}, 0, 2))
2407 {
2408 $test{$field}= $val;
2409 }
2410 my ( $priority ) =0;
2411 my ( $preemption ) =0;
2412 foreach my $field ( keys %test )
2413 {
2414 my $val = $test{$field};
2415 print "Gate key is '$field' value is '$val'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
2416 if ( $field=~/^direction$/i )
2417 {
2418 if (!$test{'Gate_Flags'}) { $test{'Gate_Flags'}=0; }
2419 $test{'Gate_Flags'}=$test{'Gate_Flags'} & 0xFE;
2420 if ( $val=~/^downstream$/i )
2421 {
2422 $test{'Gate_Flags'}=$test{'Gate_Flags'} & 0xFE;
2423 }
2424 if ( $val=~/^upstream$/i )
2425 {
2426 $test{'Gate_Flags'}=$test{'Gate_Flags'} & 0xFE;
2427 $test{'Gate_Flags'}=$test{'Gate_Flags'} + 0x01;
2428 }
2429 }
2430  
2431 if ( $field=~/^dscptosmark$/i )
2432 {
2433 if ( !$test{'Gate_Flags'} ) { $test{'Gate_Flags'}=0; }
2434 $test{'Gate_Flags'} = $test{'Gate_Flags'} & 0xFD;
2435 $val = $val & 0x01; $val = $val << 1; $val = $val & 0x02;
2436 $test{'Gate_Flags'} = $test{'Gate_Flags'} + $val;
2437 }
2438  
2439 if ( $field=~/^priority$/i )
2440 {
2441 if ( !$test{'Gate_Class'} ) { $test{'Gate_Class'}=0; }
2442 $val = $val & 0x07;
2443 $priority = $val;
2444 }
2445 if ( $field=~/^preemption$/i )
2446 {
2447 if ( $test{'Gate_Class'} ) { $test{'Gate_Class'}=0; }
2448 $val = $val & 0x01; $val = $val << 3;
2449 $val = $val & 0x08;
2450 $preemption = $val;
2451 }
2452 if ( $priority || $preemption )
2453 {
2454 $test{'Gate_Class'} = $test{'Gate_Class'} & 0xF0;
2455 $test{'Gate_Class'} = $test{'Gate_Class'} + $priority;
2456 $test{'Gate_Class'} = $test{'Gate_Class'} + $preemption;
2457 }
2458 }
2459 my $encode_gate_spec = $self->encode_sub_gate_spec ( \%test );
2460 $self->{_GLOBAL}{'Gate_Specification_Encoded'} = $encode_gate_spec;
2461 return 1;
2462 }
2463  
2464 sub gate_specification_clear
2465 {
2466 my ( $self ) = shift;
2467 $self->{_GLOBAL}{'Gate_Specification_Encoded'} = "";
2468 }
2469  
2470 sub gate_specification_get
2471 {
2472 my ( $self ) = shift;
2473 if ( $self->{_GLOBAL}{'Gate_Specification_Encoded'} )
2474 {
2475 return $self->{_GLOBAL}{'Gate_Specification_Encoded'};
2476 }
2477 return "";
2478 }
2479  
2480  
2481 sub encode_sub_gate_spec
2482 {
2483 my ( $self ) = shift;
2484 my ( $data ) = shift;
2485 my ( $packed ) = "";
2486  
2487 $packed = pack("CC",5,1);
2488 my ( $gate_headers ) = $self->gate_array();
2489 $packed.= $self->general_pack( $data, $gate_headers );
2490 my ( $length ) = pack("n",(length($packed)+2));
2491 return $length.$packed;
2492 }
2493  
2494 sub envelope_type
2495 {
2496 my ( $self ) = shift;
2497 my ( $part ) = shift;
2498 my ( %types ) =
2499 (
2500 "None" => "None,0,0",
2501 "Flow Spec"=> "Flow Spec,7,1",
2502 "DOCSIS Service Class Name"=> "DOCSIS Service Class Name,7,2",
2503 "Best Effort Service" => "Best Effort Service,7,3",
2504 "Non-Real-Time Polling Service" => "Non-Real-Time Polling Service,7,4",
2505 "Real-Time Polling Service" => "Real-Time Polling Service,7,5",
2506 "Unsolicited Grant Service" => "Unsolicited Grant Service,7,6",
2507 "Unsolicited Grant Service with Activity Detection" => "Unsolicited Grant Service with Activity Detection,7,7",
2508 "Downstream" => "Downstream,7,8"
2509 );
2510 return $types{$part};
2511 }
2512  
2513 sub envelope_header
2514 {
2515 my ( $self ) = shift;
2516 my ( $part ) = shift;
2517 my ( @headers ) =
2518 (
2519 # There is no 7-0
2520 [
2521 ],
2522 # 7-1
2523 # Flow Spec
2524 [
2525 "Envelope",
2526 "Service Number",
2527 "Reserved1Byte",
2528 "Reserved1Byte"
2529 ],
2530 # 7-2
2531 # DOCSIS Service Class Name
2532 [
2533 "Envelope",
2534 "Reserved1Byte",
2535 "Reserved1Byte",
2536 "Reserved1Byte",
2537 "ServiceClassName"
2538 ],
2539 # 7-3
2540 # Best Effort Service
2541 [
2542 "Envelope",
2543 "Reserved1Byte",
2544 "Reserved1Byte",
2545 "Reserved1Byte"
2546 ],
2547 # 7-4
2548 # Non-Real_time Polling Service
2549 [
2550 "Envelope",
2551 "Reserved1Byte",
2552 "Reserved1Byte",
2553 "Reserved1Byte"
2554 ],
2555 # 7-5
2556 # Real_time Polling Service
2557 [
2558 "Envelope",
2559 "Reserved1Byte",
2560 "Reserved1Byte",
2561 "Reserved1Byte"
2562 ],
2563 # 7-6
2564 # Unsolicited Grant Sevice
2565 [
2566 "Envelope",
2567 "Reserved1Byte",
2568 "Reserved1Byte",
2569 "Reserved1Byte"
2570 ],
2571 # 7-7
2572 # Unsolicited Grant Service with Activity Detection
2573 [
2574 "Envelope",
2575 "Reserved1Byte",
2576 "Reserved1Byte",
2577 "Reserved1Byte"
2578 ],
2579 # 7-8
2580 # Downstream Service
2581 [
2582 "Envelope",
2583 "Reserved1Byte",
2584 "Reserved1Byte",
2585 "Reserved1Byte"
2586 ]
2587 );
2588 return \$headers[$part];
2589 }
2590  
2591 sub envelope_array
2592 {
2593 my ( $self ) = shift;
2594 my ( $part ) = shift;
2595 my ( @envelopes ) =
2596 (
2597 # There is no 7-0
2598 [
2599 ],
2600 # 7-1
2601 [
2602 "Token Bucket Rate",
2603 "Token Bucket Size",
2604 "Peak Data Rate",
2605 "Minimum Policed Unit",
2606 "Maximum Packet Size",
2607 "Rate",
2608 "Slack Term"
2609 ],
2610 # 7-2
2611 # A little unsure here.
2612 [
2613 "ServiceClassName"
2614 ],
2615 # 7-3
2616 [
2617 "Traffic Priority",
2618 "Reserved1Byte",
2619 "Reserved1Byte",
2620 "Reserved1Byte",
2621 "Request Transmission Policy",
2622 "Maximum Sustained Traffic Rate",
2623 "Maximum Traffic Burst",
2624 "Minimum Reserved Traffic Rate",
2625 "Assumed Minimum Reserved Traffic Rate Packet Size",
2626 "Maximum Concatenated Burst",
2627 "Required Attribute Mask",
2628 "Forbidden Attribute Mask",
2629 "Attribute Aggregation Rule Mask"
2630 ],
2631 # 7-4
2632 [
2633 "Traffic Priority",
2634 "Reserved1Byte",
2635 "Reserved1Byte",
2636 "Reserved1Byte",
2637 "Request Transmission Policy",
2638 "Maximum Sustained Traffic Rate",
2639 "Maximum Traffic Burst",
2640 "Minimum Reserved Traffic Rate",
2641 "Assumed Minimum Reserved Traffic Rate Packet Size",
2642 "Maximum Concatenated Burst",
2643 "Nominal Polling Interval",
2644 "Required Attribute Mask",
2645 "Forbidden Attribute Mask",
2646 "Attribute Aggregation Rule Mask"
2647 ],
2648 # 7-5
2649 [
2650 "Request Transmission Policy",
2651 "Maximum Sustained Traffic Rate",
2652 "Maximum Traffic Burst",
2653 "Minimum Reserved Traffic Rate",
2654 "Assumed Minimum Reserved Traffic Rate Packet Size",
2655 "Maximum Concatenated Burst",
2656 "Nominal Polling Interval",
2657 "Tolerated Poll Jitter",
2658 "Required Attribute Mask",
2659 "Forbidden Attribute Mask",
2660 "Attribute Aggregation Rule Mask"
2661 ],
2662 # 7-6
2663 [
2664 "Request Transmission Policy",
2665 "Unsolicited Grant Size",
2666 "Grants Per Interval",
2667 "Reserved1Byte",
2668 "Nominal Grant Interval",
2669 "Tolerated Grant Jitter",
2670 "Required Attribute Mask",
2671 "Forbidden Attribute Mask",
2672 "Attribute Aggregation Rule Mask"
2673 ],
2674 # 7-7
2675 [
2676 "Request Transmission Policy",
2677 "Unsolicited Grant Size",
2678 "Grants Per Interval",
2679 "Reserved1Byte",
2680 "Nominal Grant Interval",
2681 "Tolerated Grant Jitter",
2682 "Nominal Polling Interval",
2683 "Tolerated Poll Jitter",
2684 "Required Attribute Mask",
2685 "Forbidden Attribute Mask",
2686 "Attribute Aggregation Rule Mask"
2687 ],
2688 # 7-8
2689 [
2690 "Traffic Priority",
2691 "Reserved1Byte",
2692 "Reserved1Byte",
2693 "Reserved1Byte",
2694 "Maximum Sustained Traffic Rate",
2695 "Maximum Traffic Burst",
2696 "Minimum Reserved Traffic Rate",
2697 "Assumed Minimum Reserved Traffic Rate Packet Size",
2698 "Maximum Downstream Latency",
2699 "Downstream Peak Traffic Rate",
2700 "Required Attribute Mask",
2701 "Forbidden Attribute Mask",
2702 "Attribute Aggregation Rule Mask"
2703 ]
2704  
2705 );
2706 if ($self->{_GLOBAL}{'DEBUG'}> 4 )
2707 {
2708 print "Returning envelope array number '$part'\n";
2709 }
2710 return \$envelopes[$part];
2711 }
2712  
2713 sub attribute_pack
2714 {
2715 my ( $self ) = shift;
2716 my ( $attribute ) = shift;
2717  
2718 my ( %attributes ) =
2719 (
2720 "Assumed Minimum Reserved Traffic Rate Packet Size" => 'n',
2721 "Downstream Peak Traffic Rate" => 'N',
2722 "Attribute Aggregation Rule Mask" => 'N',
2723 "Forbidden Attribute Mask" => 'N',
2724 "Grants Per Interval" => 'C',
2725 "Maximum Concatenated Burst" => 'n',
2726 "Maximum Downstream Latency" => 'N',
2727 "Maximum Packet Size" => 'N',
2728 "Maximum Sustained Traffic Rate" => 'N',
2729 "Maximum Traffic Burst" => 'N',
2730 "Minimum Policed Unit" => 'N',
2731 "Minimum Reserved Traffic Rate" => 'N',
2732 "Nominal Grant Interval" => 'N',
2733 "Nominal Polling Interval" => 'N',
2734 "Peak Data Rate" => 'N',
2735 "Rate" => 'N',
2736 "Request Transmission Policy" => 'N',
2737 "Required Attribute Mask" => 'N',
2738 "Reserved1Byte" => 'C',
2739 "Reserved2Bytes" => 'n',
2740 "Reserved3Bytes" => 'Cn',
2741 "Slack Term" => 'N',
2742 "Token Bucket Rate" => 'N',
2743 "Token Bucket Size" => 'N',
2744 "Tolerated Grant Jitter" => 'N',
2745 "Tolerated Poll Jitter" => 'N',
2746 "Traffic Priority" => 'C',
2747 "Unsolicited Grant Size" => 'n',
2748 "Envelope" => 'C',
2749 "Service Number" => 'C',
2750 "String" => 'String',
2751 "ServiceClassName" => 'String',
2752 "Gate_Flags" => 'C',
2753 "Gate_TOSField" => 'C',
2754 "Gate_TOSMask" => 'C',
2755 "Gate_Class" => 'C',
2756 "Gate_T1" => 'n',
2757 "Gate_T2" => 'n',
2758 "Gate_T3" => 'n',
2759 "Gate_T4" => 'n',
2760 "Classifier_IPProtocolId" => 'n',
2761 "Classifier_TOSField" => 'C',
2762 "Classifier_TOSMask" => 'C',
2763 "Classifier_SourceIP" => 'N',
2764 "Classifier_DestinationIP" => 'N',
2765 "Classifier_SourcePort" => 'n',
2766 "Classifier_DestinationPort" => 'n',
2767 "Classifier_Priority" => 'C',
2768 "EClassifier_IPProtocolId" => 'n',
2769 "EClassifier_TOSField" => 'C',
2770 "EClassifier_TOSMask" => 'C',
2771 "EClassifier_SourceIP" => 'N',
2772 "EClassifier_SourceMask" => 'N',
2773 "EClassifier_DestinationIP" => 'N',
2774 "EClassifier_DestinationMask" => 'N',
2775 "EClassifier_SourcePortStart" => 'n',
2776 "EClassifier_SourcePortEnd" => 'n',
2777 "EClassifier_DestinationPortStart" => 'n',
2778 "EClassifier_DestinationPortEnd" => 'n',
2779 "EClassifier_ClassifierID" => 'n',
2780 "EClassifier_Priority" => 'C',
2781 "EClassifier_State" => 'C',
2782 "EClassifier_Action" => 'C',
2783 "PRKS_IPAddress" => 'N',
2784 "PRKS_Port" => 'n',
2785 "PRKS_Flags" => 'C',
2786 "Reserved" => 'C',
2787 "SRKS_IPAddress" => 'N',
2788 "SRKS_Port" => 'n',
2789 "SRKS_Flags" => 'C',
2790 "Reserved" => 'C',
2791 "BCID_TimeStamp" => 'N',
2792 "BCID_ElementID" => 'String-8',
2793 "BCID_TimeZone" => 'String-8',
2794 "BCID_EventCounter" => 'N',
2795 "RADIUS_EventMessageVersionID" => 'n',
2796 "RADIUS_BCID_Timestamp" => 'N',
2797 "RADIUS_BCID_ElementID" => 'String-8',
2798 "RADIUS_BCID_TimeZone" => 'String-8',
2799 "RADIUS_BCID_EventCounter" => 'N',
2800 "RADIUS_EventMessageType" => 'n',
2801 "RADIUS_ElementType" => 'n',
2802 "RADIUS_ElementID" => 'String-8',
2803 "RADIUS_TimeZone" => 'String-8',
2804 "RADIUS_SequenceNumber" => 'N',
2805 "RADIUS_EventTime" => 'String-18',
2806 "RADIUS_Status" => 'N',
2807 "RADIUS_Priority" => 'C',
2808 "RADIUS_AttributeCount" => 'n',
2809 "RADIUS_EventObject" => 'C',
2810 "RADIUS_QosStatus" => 'N',
2811 "RADIUS_ServiceClassName" => 'String-16'
2812  
2813 );
2814  
2815 return $attributes{$attribute};
2816 }
2817  
2818 sub decode_transaction_identifier
2819 {
2820 my ( $self ) = shift;
2821 my ( $data ) = shift;
2822 my ( $mmtid, $gate_command ) = unpack("nn",$data);
2823 return ($mmtid,$gate_command);
2824 }
2825  
2826 sub decode_application_manager_identifier
2827 {
2828 my ( $self ) = shift;
2829 my ( $data ) = shift;
2830 my ( $amidat, $amidam ) = $self->decode_transaction_identifier($data);
2831 return ($amidat, $amidam);
2832 }
2833  
2834 sub decode_subscriber_id
2835 {
2836 my ( $self ) = shift;
2837 my ( $data ) = shift;
2838 my ( $sub_id ) = unpack("N",$data);
2839 my ( $sub_ip ) = $self->_IpIntToQuad($sub_id);
2840 return ( $sub_id, $sub_ip );
2841 }
2842  
2843 sub decode_gate_id
2844 {
2845 my ( $self ) = shift;
2846 my ( $data ) = shift;
2847 my ( $gate_id ) = unpack("N",$data);
2848 return $gate_id;
2849 }
2850  
2851 sub object_type_decode
2852 {
2853 my ( $self ) = shift;
2854 my ( $major ) = shift;
2855 my ( $minor ) = shift;
2856 my ( @object_types ) =
2857 (
2858 # Nothing at 0
2859 [],
2860 # Transaction Identifier 1
2861 [
2862 "",
2863 "Transaction Identifier"
2864 ],
2865 # Application Manager Identifier 2
2866 [
2867 "",
2868 "Application Manager Identifier"
2869 ],
2870 # Subscriber Identifier 3
2871 [
2872 "",
2873 "Subscriber Identifier"
2874 ],
2875 # Gate Identifier 4
2876 [
2877 "",
2878 "Gate Identifier"
2879 ],
2880 # Sub routines for decode done up to here.
2881 # Gate Specification 5
2882 [
2883 "",
2884 "Gate Specification"
2885 ],
2886 # Classifier 6
2887 [
2888 "",
2889 "Classifier",
2890 "Extended Classifier"
2891 ],
2892 # Traffic Profile 6
2893 [
2894 "",
2895 "Flow Spec",
2896 "DOCSIS Service Class Name",
2897 "Best Effort Service",
2898 "Non-Real-Time Polling Service",
2899 "Real-Time Polling Service",
2900 "Unsolicited Grant Service",
2901 "Unsolicited Grant Service with Activity Detection",
2902 "Downstream"
2903 ],
2904 # Event-Generation-Info 7
2905 [
2906 "",
2907 "Event-Generation-Info",
2908 ],
2909 # Volume Based Usage Limit
2910 [
2911 "",
2912 "Volume Based Usage Limit"
2913 ],
2914 # Time Based Usage Limit
2915 [
2916 "",
2917 "Time Based Usage Limit"
2918 ],
2919 # Opaque Data
2920 [
2921 "",
2922 "Opaque Data"
2923 ],
2924 # Gate Time Info
2925 [
2926 "",
2927 "Gate Time Info"
2928 ],
2929 # Gate Usage Info
2930 [
2931 "",
2932 "Gate Usage Info"
2933 ],
2934 # Packet Cable Error
2935 [
2936 "",
2937 "Packet Cable Error"
2938 ],
2939 # Gate State
2940 [
2941 "",
2942 "Gate State"
2943 ],
2944 # Version Info
2945 [
2946 "",
2947 "Version Info"
2948 ],
2949 # Policy Server Identifier
2950 [
2951 "",
2952 "Policy Server Identifier"
2953 ],
2954 # Synch Options
2955 [
2956 "",
2957 "Synch Options"
2958 ],
2959 # Msg Receipt Key
2960 [
2961 "",
2962 "Msg Receipt Key"
2963 ]
2964 );
2965 return $object_types[$major][$minor];
2966 }
2967  
2968 sub Envelope_Type_remap
2969 {
2970 my ( $self ) = shift;
2971 my ( $envelope_setting ) = shift;
2972 my ( $envelope_type ) = 0;
2973 my ( @types ) = qw [ authorize:1 reserve:2 commit:4 ];
2974 foreach my $find_type ( @types )
2975 {
2976 my ( $name, $number ) = (split(/:/,$find_type))[0,1];
2977 if ( $envelope_setting=~/$name/ig )
2978 {
2979 print "Debug envelope number is '$number'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
2980 $envelope_type+=$number;
2981 }
2982 }
2983 return $envelope_type;
2984 }
2985  
2986 sub encode_cops_message_no_payload
2987 {
2988 my ( $self ) = shift;
2989 my ( $version ) = shift;
2990 my ( $flags ) = shift;
2991 my ( $opcode ) = shift;
2992 my ( $clienttype ) = shift;
2993 my ( $data ) = "";
2994 my ( $header_v ) = ( ($version<<4) | $flags );
2995 my ( $data_block ) = pack("CCn",$header_v,$opcode,$clienttype);
2996 my ( $length ) = pack("N",length($data)+8);
2997 $data_block.=$length.$data;
2998 return $data_block;
2999 }
3000  
3001 sub encode_cops_message
3002 {
3003 my ( $self ) = shift;
3004 my ( $version ) = shift;
3005 my ( $flags ) = shift;
3006 my ( $opcode ) = shift;
3007 my ( $clienttype ) = shift;
3008 my ( $data ) = shift;
3009  
3010 my ( $header_v ) = ( ($version<<4) | $flags );
3011 my ( $data_block ) = pack("CCn",$header_v,$opcode,$clienttype);
3012 my ( $length ) = pack("N",length($data)+8);
3013 $data_block.=$length.$data;
3014 return $data_block;
3015 }
3016  
3017 sub encode_cops_object
3018 {
3019 my ( $self ) = shift;
3020 my ( $cnum ) = shift;
3021 my ( $ctype ) = shift;
3022 my ( $data ) = shift;
3023  
3024 my ( $align ) = 0;
3025  
3026 if ( !$data) { $data=""; }
3027  
3028 $align = length($data) % 4;
3029  
3030 print "Length of data is '".length($data)."' alignment requried is '$align'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
3031  
3032 $align = 4 - $align;
3033  
3034 if ( $align > 0 && $align <4 )
3035 {
3036 print "Align is '$align'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
3037 for ( my $tmp=0; $tmp<$align; $tmp ++ )
3038 {
3039 print "Adding '$tmp'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
3040 $data.=pack("C",0);
3041 }
3042 print "Finished alignment\n\n\n" if $self->{_GLOBAL}{'DEBUG'}>0;
3043 }
3044  
3045 my ( $length ) = length($data)+4;
3046  
3047 my ( $data_block ) = pack("nCC",$length,$cnum,$ctype);
3048 $data_block.=$data;
3049  
3050 return $data_block;
3051 }
3052  
3053 sub generate_subscriber_id
3054 {
3055 my ( $self ) = shift;
3056 my ( $ip ) = shift;
3057 my ( $cops_object ) = "";
3058 $cops_object = $self->encode_cops_object( 2,1, $self->_IpQuadToInt($ip) );
3059 return $cops_object;
3060 }
3061  
3062 sub generate_gate_id
3063 {
3064 my ( $self ) = shift;
3065 my ( $id ) = shift;
3066 my ( $cops_object ) = "";
3067 $cops_object = $self->encode_cops_object( 2,1, pack("N",$id) );
3068 return $cops_object;
3069 }
3070  
3071 sub generate_transaction_id
3072 {
3073 my ( $self ) = shift;
3074 my ( $command ) = shift;
3075 print "\n\ntransaction id is '".$self->encode_gate_ids($command)."'\n\n\n" if $self->{_GLOBAL}{'DEBUG'}>0;
3076 $command = pack("nn",$self->{_GLOBAL}{'TRANSACTION_COUNT'} , $self->encode_gate_ids($command) );
3077 $self->{_GLOBAL}{'TRANSACTION_COUNT'}++;
3078 my ( $cops_object ) = $self->encode_cops_object( 1,1, $command );
3079 return $cops_object;
3080 }
3081  
3082 sub generate_decision
3083 {
3084 my ( $self ) = shift;
3085 my ( $type ) = shift;
3086 my ( $command ) = shift;
3087 my ( $flags ) = shift;
3088 my ( $payload ) = pack("nn", $command, $flags );
3089 my ( $cops_object ) = $self->encode_cops_object( 6,$type, $payload );
3090 return $cops_object;
3091 }
3092  
3093 sub decode_radius_attribute
3094 {
3095 # This has only been tested with the output of
3096 # FreeRadius 2.1.9
3097 # The required perl code is included.
3098 #
3099 my ( $self ) = shift;
3100 my ( $radius_name ) = shift;
3101 my ( $radius_value ) = shift;
3102 my ( $data_return ) = shift;
3103  
3104 my ( $rad_position ) = $self->_decode_radius_attributes($radius_name);
3105  
3106 my ( $radius_array ) = $self->_decode_radius_attribute_layout( $rad_position );
3107  
3108 if ( !$radius_array ) { return 0; }
3109  
3110 $radius_value = (split(/x/,$radius_value))[1];
3111 if ( !$radius_value) { return 0; }
3112 my ( $radius_value_packed ) = "";
3113 # This a bit of a hack.
3114 for (my $a=0;$a<length($radius_value); $a+=2 )
3115 {
3116 my $char = "0x".substr($radius_value,$a,2);
3117 my $pack = pack("C",oct($char));
3118 $radius_value_packed.=$pack;
3119 }
3120  
3121 my ( $done_pack ) = 0;
3122  
3123 foreach my $rad_a ( @{${$radius_array}} )
3124 {
3125 my $type_a = $self->attribute_pack($rad_a);
3126 my $type_v = 0;
3127 my $name = "";
3128 my $data = "";
3129 my $true_length = 0;
3130 if ( $type_a=~/^string/i )
3131 {
3132 my ( $name_s, $length_s ) = (split(/-/,$type_a))[0,1];
3133 $data = substr($radius_value_packed,0,$length_s);
3134 $radius_value_packed = substr( $radius_value_packed, $length_s, length($radius_value_packed)-$length_s);
3135 }
3136 else
3137 {
3138 $data = unpack( $type_a, $radius_value_packed);
3139 if ( $type_a=~/^n$/ ) { $true_length=2; }
3140 if ( $type_a=~/^N$/ ) { $true_length=4; }
3141 if ( $type_a=~/^C$/ ) { $true_length=1; }
3142 $radius_value_packed = substr( $radius_value_packed, $true_length, length($radius_value_packed)-$true_length);
3143 }
3144 $rad_a=~s/^RADIUS_//g;
3145 ${$data_return}{$rad_a}=$data;
3146 if ( $rad_a=~/^EventMessageType$/i )
3147 { ${$data_return}{'EventMessageTypeName'}=$self->_decode_radius_message_type($data); }
3148 $data="";
3149 $true_length=0;
3150 }
3151 return 1;
3152 }
3153  
3154  
3155 sub _decode_radius_attributes
3156 {
3157 my ( $self ) = shift;
3158 my ( $radius_name ) = shift;
3159 my ( %types ) =
3160 (
3161 'CableLabs-Event-Message' => 0,
3162 'CableLabs-QoS-Descriptor' => 1
3163 );
3164 return $types{$radius_name};
3165 }
3166  
3167 sub _decode_radius_attribute_layout
3168 {
3169 my ( $self ) = shift;
3170 my ( $part ) = shift;
3171 my ( @headers ) =
3172 (
3173 # CableLabs-Event-Message
3174 [
3175 "RADIUS_EventMessageVersionID",
3176 "RADIUS_BCID_Timestamp",
3177 "RADIUS_BCID_ElementID",
3178 "RADIUS_BCID_TimeZone",
3179 "RADIUS_BCID_EventCounter",
3180 "RADIUS_EventMessageType",
3181 "RADIUS_ElementType",
3182 "RADIUS_ElementID",
3183 "RADIUS_TimeZone",
3184 "RADIUS_SequenceNumber",
3185 "RADIUS_EventTime",
3186 "RADIUS_Status",
3187 "RADIUS_Priority",
3188 "RADIUS_AttributeCount",
3189 "RADIUS_EventObject"
3190 ],
3191 # CableLabs-QoS-Descriptor
3192 [
3193 "RADIUS_QosStatus",
3194 "RADIUS_ServiceClassName"
3195 ]
3196 );
3197 return \$headers[$part];
3198 }
3199  
3200 sub _decode_radius_message_type
3201 {
3202 my ( $self ) = shift;
3203 my ( $message ) = shift;
3204 my ( @message_types ) =
3205 (
3206 "Reserved",
3207 "Signaling_Start",
3208 "Signaling_Stop",
3209 "Database_Query",
3210 "Intelligent_Peripheral_Usage_Start",
3211 "Intelligent_Peripheral_Usage_Stop",
3212 "Service_Instance",
3213 "QoS_Reserve",
3214 "QoS_Release",
3215 "Service_Activation",
3216 "Service_Deactivation",
3217 "Media_Report",
3218 "Signal_Instance",
3219 "Interconnect_(Signaling)_Start",
3220 "Interconnect_(Signaling)_Stop",
3221 "Call_Answer",
3222 "Call_Disconnect",
3223 "Time_Change",
3224 "18_Missed",
3225 "QoS_Commit",
3226 "Media_Alive",
3227 "Conference_Party_Change"
3228 );
3229  
3230 if ( $message_types[$message] )
3231 {
3232 return $message_types[$message];
3233 }
3234 return "Unknown";
3235 }
3236  
3237 sub _IpQuadToInt
3238 {
3239 my ($self)= shift;
3240 my($Quad) = shift;
3241 if ( !$Quad ) { return 0; }
3242 my($Ip1, $Ip2, $Ip3, $Ip4) = split(/\./, $Quad);
3243 my($IpInt) = (($Ip1 << 24) | ($Ip2 << 16) | ($Ip3 << 8) | $Ip4);
3244 return($IpInt);
3245 }
3246  
3247 sub _IpIntToQuad
3248 {
3249 my ( $self ) = shift;
3250 my($Int) = shift;
3251 my($Ip1) = $Int & 0xFF; $Int >>= 8;
3252 my($Ip2) = $Int & 0xFF; $Int >>= 8;
3253 my($Ip3) = $Int & 0xFF; $Int >>= 8;
3254 my($Ip4) = $Int & 0xFF; return("$Ip4.$Ip3.$Ip2.$Ip1");
3255 }
3256  
3257 sub decode_64bit_number
3258 {
3259 # see comments on 64bit stuff.
3260 my ( $self ) = shift;
3261 my ( $message ) = shift;
3262 my ($part1,$part2) = unpack("NN",$message);
3263 $part1 = $part1<<32;
3264 $part1+=$part2;
3265 return $part1;
3266 }
3267  
3268 sub encode_64bit_number
3269 {
3270 # It seems Q does not work, well not for me
3271 # and this is the quickest way to fix it.
3272 # You STILL NEED 64 BIT SUPPORT!!
3273 my ( $self ) = shift;
3274 my ( $number ) = shift;
3275 # any bit to 64bit number in.
3276 my($test1) = $number & 0xFFFFFFFF; $number >>= 32;
3277 my($test2) = $number & 0xFFFFFFFF;
3278 my $message = pack("NN",$test2,$test1);
3279 return $message;
3280 }
3281  
3282 sub general_pack
3283 {
3284 my ( $self ) = shift;
3285 my ( $data ) = shift;
3286 my ( $array_pointer ) = shift;
3287 my ( $prefix ) = shift;
3288 my ( $packed ) = "";
3289 if ( !$prefix ) { $prefix=""; }
3290 foreach my $env_c ( @{${$array_pointer}} )
3291 {
3292 my $type_c = $self->attribute_pack($env_c);
3293 my $type_v = 0;
3294 my $type_push = $prefix.$env_c;
3295 if ( $env_c=~/^reserved/i )
3296 {
3297 print "Reserved type found.\n" if $self->{_GLOBAL}{'DEBUG'}>0;
3298 $packed.=pack("C",0);
3299 }
3300 if ( $type_c=~/^string/i )
3301 {
3302 my ( $type, $numlen ) = (split(/-/,$type_c))[0,1];
3303 if ( !$numlen )
3304 {
3305 $packed.=$self->align_string(${$data}{$type_push});
3306 }
3307 else
3308 {
3309 $packed.=pack("A[$numlen]",${$data}{$type_push});
3310 }
3311 }
3312 if ( $type_c!~/^string/i && $env_c!~/^reserved/i )
3313 {
3314 if ( !${$data}{$type_push} )
3315 {
3316 $type_v=0;
3317 }
3318 else
3319 {
3320 if ( ($env_c=~/IP/ ) && ( $env_c!~/IPProtocolId/))
3321 {
3322 $type_v = $self->_IpQuadToInt(${$data}{$type_push});
3323 }
3324 else
3325 {
3326 $type_v = ${$data}{$type_push};
3327 }
3328 }
3329 $packed.=pack( $type_c , $type_v );
3330 }
3331 print "General Encoder Name us '$env_c' value is '$type_v' type is '$type_c'\n" if $self->{_GLOBAL}{'DEBUG'}>0;
3332 }
3333 return $packed;
3334 }
3335  
3336 =cut
3337  
3338 sub function2 {
3339 }
3340  
3341 =head1 AUTHOR
3342  
3343 shamrock@cpan.org, C<< <shamrock at cpan.org> >>
3344  
3345 =head1 BUGS
3346  
3347 - Sync messages to Cisco CMTS do not seem to work. I have tried alternative
3348 formats, headers, etc but to no avail. They do work to Motorola and Aris.
3349 I have raised this with Cisco but do not expect a response any time soon.
3350 If anyone has a packet trace with a working Synch using a Cisco CMTS that
3351 would be useful.
3352  
3353 - The different traffic profiles need work, see examples/profiles.
3354 The following examples produce an 'Unspecified error' and may be down to
3355 the values being used. If any one can help with the values that should be
3356 used, packet trace, then I can look at improving their use.
3357  
3358 Flow Spec
3359 Non-Real-Time Polling Service
3360 Real-Time Polling Service
3361 Unsolicited Grant Service
3362  
3363  
3364 Please report any bugs or feature requests to C<bug-cops-cmts at rt.cpan.org>, or through
3365 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=COPS-Client>. I will be notified, and then you'll
3366 automatically be notified of progress on your bug as I make changes.
3367  
3368 =head1 SUPPORT
3369  
3370 Please do email me if you have any issues so they can be looked at as soon
3371 as possible.
3372  
3373 You can find documentation for this module with the perldoc command.
3374  
3375 perldoc COPS::Client
3376  
3377  
3378 You can also look for information at:
3379  
3380 =over 4
3381  
3382 =item * RT: CPAN's request tracker
3383  
3384 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=COPS-Client>
3385  
3386 =item * AnnoCPAN: Annotated CPAN documentation
3387  
3388 L<http://annocpan.org/dist/COPS-Client>
3389  
3390 =item * CPAN Ratings
3391  
3392 L<http://cpanratings.perl.org/d/COPS-Client>
3393  
3394 =item * Search CPAN
3395  
3396 L<http://search.cpan.org/dist/COPS-Client/>
3397  
3398 =back
3399  
3400  
3401 =head1 ACKNOWLEDGEMENTS
3402  
3403  
3404 =head1 COPYRIGHT & LICENSE
3405  
3406 Copyright 2012 shamrock@cpan.org, all rights reserved.
3407  
3408 This program is free software; you can redistribute it and/or modify it
3409 under the same terms as Perl itself.
3410  
3411  
3412 =cut
3413  
3414 1; # End of COPS::Client