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