File Coverage

blib/lib/Net/EPP/Protocol.pm
Criterion Covered Total %
statement 13 36 36.1
branch 0 10 0.0
condition n/a
subroutine 5 8 62.5
pod 0 3 0.0
total 18 57 31.5


line stmt bran cond sub pod time code
1             package Net::EPP::Protocol;
2 1     1   6 use bytes;
  1         2  
  1         5  
3 1     1   29 use Carp;
  1         3  
  1         52  
4 1     1   6 use vars qw($THRESHOLD);
  1         2  
  1         32  
5 1     1   4 use strict;
  1         2  
  1         49  
6              
7             =pod
8              
9             =head1 NAME
10              
11             Net::EPP::Protocol - Low-level functions useful for both EPP clients and
12             servers.
13              
14             =head1 SYNOPSIS
15              
16             #!/usr/bin/perl
17             use Net::EPP::Protocol;
18             use IO::Socket;
19             use strict;
20              
21             # create a socket:
22              
23             my $socket = IO::Socket::INET->new( ... );
24              
25             # send a frame down the socket:
26              
27             Net::EPP::Protocol->send_frame($socket, $xml);
28              
29             # get a frame from the socket:
30              
31             my $xml = Net::EPP::Protocol->get_frame($socket);
32              
33             =head1 DESCRIPTION
34              
35             This module implements functions that are common to both EPP clients and
36             servers that implement the TCP transport as defined in RFC 4934. The
37             main consumer of this module is currently L.
38              
39             =head1 VARIABLES
40              
41             =head2 $Net::EPP::Protocol::THRESHOLD
42              
43             At least one EPP server implementation sends an unframed plain text error
44             message when a client connects from an unauthorised address. As a result, when
45             the first four bytes of the message are unpacked, the client tries to read and
46             allocate a very large amount of memory.
47              
48             If the apparent frame length received from a server exceeds the value of
49             C<$Net::EPP::Protocol::THRESHOLD>, the C method will croak.
50              
51             The default value is 1GB.
52              
53             =cut
54              
55             BEGIN {
56 1     1   312 our $THRESHOLD = 1000000000;
57             }
58              
59             =pod
60              
61             =head1 METHODS
62              
63             my $xml = Net::EPP::Protocol->get_frame($socket);
64              
65             This method reads a frame from the socket and returns a scalar
66             containing the XML. C<$socket> must be an L or one of its
67             subclasses (ie C).
68              
69             If the transmission fails for whatever reason, this method will
70             C, so be sure to enclose it in an C.
71              
72             =cut
73              
74             sub get_frame {
75 0     0 0   my ($class, $fh) = @_;
76              
77 0           my $hdr;
78 0 0         if (!defined($fh->read($hdr, 4))) {
79 0           croak("Got a bad frame length from peer - connection closed?");
80              
81             } else {
82 0           my $length = (unpack('N', $hdr) - 4);
83 0 0         if ($length < 0) {
    0          
    0          
84 0           croak("Got a bad frame length from peer - connection closed?");
85              
86             } elsif (0 == $length) {
87 0           croak('Frame length is zero');
88              
89             } elsif ($length > $THRESHOLD) {
90 0           croak("Frame length is $length which exceeds $THRESHOLD");
91              
92             } else {
93 0           my $xml = '';
94 0           my $buffer;
95 0           while (length($xml) < $length) {
96 0           $buffer = '';
97 0           $fh->read($buffer, ($length - length($xml)));
98 0 0         last if (length($buffer) == 0); # in case the socket has closed
99 0           $xml .= $buffer;
100             }
101              
102 0           return $xml;
103              
104             }
105             }
106             }
107              
108             =pod
109              
110             Net::EPP::Protocol->send_frame($socket, $xml);
111              
112             This method prepares an RFC 4934 compliant EPP frame and transmits it to
113             the remote peer. C<$socket> must be an L or one of its
114             subclasses (ie C).
115              
116             If the transmission fails for whatever reason, this method will
117             C, so be sure to enclose it in an C. Otherwise, it will
118             return a true value.
119              
120             =cut
121              
122             sub send_frame {
123 0     0 0   my ($class, $fh, $xml) = @_;
124 0           $fh->print($class->prep_frame($xml));
125 0           $fh->flush;
126 0           return 1;
127             }
128              
129             =pod
130              
131             my $frame = Net::EPP::Protocol->prep_frame($xml);
132              
133             This method returns the XML frame in "wire format" with the protocol
134             header prepended to it. The return value can be printed directly to an
135             open socket, for example:
136              
137             print STDOUT Net::EPP::Protocol->prep_frame($frame->toString);
138              
139             =cut
140              
141             sub prep_frame {
142 0     0 0   my ($class, $xml) = @_;
143 0           return pack('N', length($xml) + 4).$xml;
144             }
145              
146             1;