EPP/Protocol.pm

filter: Statementstmt 37.8% 14 / 37 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 62.5% 5 / 8 Podpod 0.0% 0 / 3 totaltotal 31.6% 19 / 60 CC 7 SCAR 33.5
CC
7
Cov
29%
get_frame
56.0
send_frame
2.0
prep_frame
2.0
CRAP 28.6
« EPP/Simple.pm ↑ Summary EPP/Client.pm »
1 package Net::EPP::Protocol;
2 1 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw($THRESHOLD);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
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<Extensible Provisioning
32 Protocol (EPP)|https://www.rfc-editor.org/info/std69> as defined in
33 L<RFC 5734|https://www.rfc-editor.org/rfc/rfc5734.html>. The only user of this
34 module is L<Net::EPP::Client>, 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<get_frame()> method will croak.
48  
49 The default value is 1GB.
50  
51 =cut
52  
53 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 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<IO::Handle> or one of its subclasses (ie
65 C<IO::Socket::*>).
66  
67 If the transmission fails for whatever reason, this method will C<croak()>, so
68 be sure to enclose it in an C<eval()>.
69  
70 =cut
71  
72 0 ▶sub get_frame {
Subroutine get_frame: not called CC=7 SCAR=40.3
Pod undocumented
73 0 my ($class, $fh) = @_;
74  
75 0 my $hdr;
76 0 ▶ if (!defined($fh->read($hdr, 4))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $fh->read($hdr, 4)) { } 0 0 0
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 ▶ if ($length < 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($length < 0) { } 0 0 0
elsif (0 == $length) { } 0 0 0
elsif ($length > $Net::EPP::Protocol::THRESHOLD) { } 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) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $xml < $length) 0 0 0
94 0 $buffer = '';
95 0 $fh->read($buffer, ($length - length($xml)));
96 0 ▶ last if (length($buffer) == 0); # in case the socket has closed
Branch: 1 branch
BranchTrueFalseTotal
if length $buffer == 0 0 0 0
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<IO::Handle> or one of its subclasses
111 (ie C<IO::Socket::*>).
112  
113 If the transmission fails for whatever reason, this method will C<croak()>, so
114 be sure to enclose it in an C<eval()>. Otherwise, it will return a true value.
115  
116 =cut
117  
118 0 ▶sub send_frame {
Subroutine send_frame: not called CC=1 SCAR=6.9
Pod undocumented
119 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 0 ▶sub prep_frame {
Subroutine prep_frame: not called CC=1 SCAR=6.9
Pod undocumented
138 0 my ($class, $xml) = @_;
139 0 return pack('N', length($xml) + 4) . $xml;
140 }
141  
142 1 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
« EPP/Simple.pm ↑ Summary EPP/Client.pm »