lib/Net/SinFP3/Input/Server.pm untested

filter: Statementstmt 0.0% 0 / 169 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 269 CC 19 SCAR 50.7
CC
19
Cov
0%
postRun
342.0
run
6.0
give
2.0
CRAP 159.1
1 #
2 # $Id: Server.pm,v 008243d3e89a 2018/07/21 14:54:07 gomor $
3 #
4 package Net::SinFP3::Input::Server;
5 use strict;
6 use warnings;
7  
8 use base qw(Net::SinFP3::Input);
9 our @AS = qw(
10 timeout
11 _server
12 _client
13 _request
14 _sel
15 );
16 __PACKAGE__->cgBuildIndices;
17 __PACKAGE__->cgBuildAccessorsScalar(\@AS);
18  
19 use Net::SinFP3::Next::Client;
20  
21 use IO::Socket::INET;
22 use IO::Select;
23 use Net::Frame::Layer::SinFP3 qw(:consts);
24 use Net::Frame::Layer::SinFP3::Tlv;
25 use Net::Frame::Simple;
26  
27 sub give {
28 return [
29 'Net::SinFP3::Next::Client',
30 ];
31 }
32  
33 sub new {
34 my $self = shift->SUPER::new(
35 timeout => 5,
36 @_,
37 );
38  
39 my $global = $self->global;
40 my $log = $global->log;
41  
42 return $self;
43 }
44  
45 sub init {
46 my $self = shift->SUPER::init(@_) or return;
47  
48 my $global = $self->global;
49 my $log = $global->log;
50  
51 my $ip = $global->targetIp || '127.0.0.1';
52 my $port = $global->port || 32000;
53  
54 my $s = IO::Socket::INET->new(
55 LocalAddr => $ip,
56 LocalPort => $port,
57 Domain => AF_INET,
58 ReuseAddr => 1,
59 Type => SOCK_STREAM,
60 Listen => $global->jobs,
61 ) or $log->fatal("Unable to start server socket");
62  
63 $s->blocking(0);
64 $s->autoflush(1);
65  
66 my $sel = IO::Select->new;
67 $sel->add($s);
68  
69 $log->verbose("Server listening on: [$ip]:$port");
70  
71 $self->_sel($sel);
72 $self->_server($s);
73  
74 return 1;
75 }
76  
77 sub run {
78 my $self = shift->SUPER::run(@_) or return;
79  
80 my $global = $self->global;
81 my $log = $global->log;
82  
83 my $sel = $self->_sel;
84 my $server = $self->_server;
85  
86 $log->debug("Waiting client");
87 while (my @read = $sel->can_read) {
88 if (my $client = $server->accept) {
89 $log->verbose("Client accepted: [".$client->peerhost."]:".
90 $client->peerport);
91 $self->_client($client);
92 return Net::SinFP3::Next::Client->new(
93 global => $global,
94 peerhost => $client->peerhost,
95 peerport => $client->peerport,
96 );
97 }
98 }
99  
100 return;
101 }
102  
103 sub _sendResponse {
104 my $self = shift;
105 my ($request, $code) = @_;
106  
107 my $global = $self->global;
108 my $log = $global->log;
109 my $input = $global->input;
110 my $client = $self->_client;
111  
112 my $sinfp3 = Net::Frame::Layer::SinFP3->new(
113 version => NF_SINFP3_VERSION1,
114 type => $request->type,
115 flags => $request->flags,
116 code => $code,
117 tlvCount => 0,
118 tlvList => [],
119 );
120  
121 my $simple = Net::Frame::Simple->new(layers => [ $sinfp3 ]);
122 $log->debug($simple->print);
123  
124 my $raw = $simple->raw;
125 my $r = $client->syswrite($raw, length($raw));
126 $log->debug("syswrite ret: $r");
127  
128 $client->close;
129 $log->debug("Closing client connection");
130  
131 return 1;
132 }
133  
134 sub _sendResponseBadVersion {
135 my $self = shift;
136 my ($request) = @_;
137 return $self->_sendResponse($request, NF_SINFP3_CODE_BADVERSION);
138 }
139  
140 sub _sendResponseBadType {
141 my $self = shift;
142 my ($request) = @_;
143 return $self->_sendResponse($request, NF_SINFP3_CODE_BADTYPE);
144 }
145  
146 sub _sendResponseBadTlvCount {
147 my $self = shift;
148 my ($request) = @_;
149 return $self->_sendResponse($request, NF_SINFP3_CODE_BADTLVCOUNT);
150 }
151  
152 sub _sendResponseBadTlv {
153 my $self = shift;
154 my ($request) = @_;
155 return $self->_sendResponse($request, NF_SINFP3_CODE_BADTLV);
156 }
157  
158 sub postRun {
159 my $self = shift->SUPER::postRun(@_) or return;
160  
161 my $global = $self->global;
162 my $log = $global->log;
163  
164 my $client = $self->_client;
165 my $server = $self->_server;
166  
167 $server->close;
168  
169 $client->blocking(0);
170 $client->autoflush(1);
171  
172 my $sel = IO::Select->new;
173 $sel->add($client);
174  
175 my $eof = 0;
176 my $request = '';
177 my $toRead = 1024;
178 while (my @read2 = $sel->can_read($self->timeout)) {
179 my $buf = '';
180 my $r = $client->sysread($buf, $toRead);
181 if (! defined($r)) {
182 $log->error("Client: sysread: $!");
183 return;
184 }
185 if ($r == 0) { # End-of-file
186 $eof++;
187 last;
188 }
189  
190 $request .= $buf;
191  
192 #$log->debug("Client: read: [".unpack("H*", $request)."]");
193  
194 # We need the first 8 bytes to parse packet header
195 my $read = length($request);
196 if ($read >= 8) {
197 my $hdr = substr($request, 0, 8);
198 my $p = Net::Frame::Layer::SinFP3->new(raw => $hdr)->unpack;
199 #$log->debug($p->print);
200  
201 if ($p->version != NF_SINFP3_VERSION1) {
202 return $self->_sendResponseBadVersion($p);
203 }
204 if ($p->type != NF_SINFP3_TYPE_REQUESTPASSIVE) {
205 return $self->_sendResponseBadType($p);
206 }
207 if ($p->tlvCount != 2) { # Frame type + frame
208 return $self->_sendResponseBadTlvCount($p);
209 }
210  
211 my $requestLen = $p->length + 8;
212  
213 if ($requestLen == $read) {
214 $log->debug("Packet complete: read: [$read] bytes. Request [".unpack("H*", $request)."]");
215 my $frame = Net::Frame::Simple->new(
216 firstLayer => 'SinFP3',
217 raw => $request,
218 );
219 $log->debug($frame->print);
220  
221 my $layer;
222 for my $tlv ($frame->ref->{SinFP3}->tlvList) {
223 if ($tlv->type == NF_SINFP3_TLV_TYPE_FRAMEPROTOCOL) {
224 my $proto = unpack('C', $tlv->value);
225 if ($proto == NF_SINFP3_TLV_VALUE_ETH) {
226 $layer = 'ETH';
227 }
228 elsif ($proto == NF_SINFP3_TLV_VALUE_IPv4) {
229 $layer = 'IPv4';
230 }
231 elsif ($proto == NF_SINFP3_TLV_VALUE_IPv6) {
232 $layer = 'IPv6';
233 }
234 else {
235 return $self->_sendResponseBadTlv($p);
236 }
237 }
238 elsif ($layer && $tlv->type == NF_SINFP3_TLV_TYPE_FRAMEPASSIVE) {
239 # Sanity checks on provided frame
240 my $minLen = 40; # IPv4 + TCP headers
241 if ($layer eq 'IPv4') {
242 $minLen = 40; # IPv4 + TCP headers
243 }
244 elsif ($layer eq 'IPv6') {
245 $minLen = 60; # IPv6 + TCP headers
246 }
247 elsif ($layer eq 'ETH') {
248 $minLen = 54; # ETH + IPv4 + TCP
249 }
250  
251 if (length($tlv->value) < $minLen) {
252 return $self->_sendResponseBadTlv($p);
253 }
254  
255 my $simple = Net::Frame::Simple->new(
256 firstLayer => $layer,
257 raw => $tlv->value,
258 );
259 $log->debug($simple->print);
260  
261 # Save request, so Output::Client can use it
262 $self->_request($frame);
263  
264 my $next = Net::SinFP3::Next::Frame->new(
265 global => $global,
266 frame => $simple,
267 );
268 $global->next($next); # We change 'en live'
269 return $self;
270 }
271 }
272  
273 # If we are here, there was an on error on TLV
274 return $self->_sendResponseBadTlv($p);
275 }
276 else {
277 # Request not complete, need to read more
278 next;
279 }
280 }
281 else {
282 # Request not complete, need to read more
283 next;
284 }
285 }
286  
287 if ($eof) {
288 $log->debug("Eof from client");
289 }
290  
291 # XXX: to handle timeout
292 #$log->debug("Timeout from client read");
293  
294 $log->debug("Closing client connection");
295 $client->close;
296  
297 return;
298 }
299  
300 # Father Server process has to close client socket after forking()
301 # So client will be able to close() socket.
302 sub postFork {
303 my $self = shift->SUPER::postFork(@_) or return;
304  
305 my $client = $self->_client;
306 $client->close;
307  
308 return 1;
309 }
310  
311 sub post {
312 my $self = shift->SUPER::post(@_) or return;
313  
314 $self->_client->close;
315  
316 return 1;
317 }
318  
319 1;
320  
321 __END__