blib/lib/Zuzu/Module/Socks.pm

filter: Statementstmt 21.5% 59 / 274 Branchbran 0.0% 0 / 134 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 19.0% 8 / 42 Podpod 0.0% 0 / 1 totaltotal 13.8% 67 / 483 CC 80 SCAR 29.9
CC
80
Cov
14%
__ANON__
72.0
_peer_from_argument
42.0
__ANON__
42.0
CRAP 19.9
« blib/lib/Zuzu/Doc/CLI.pm ↑ Summary blib/lib/Zuzu/Value/Array.pm »
1 package Zuzu::Module::Socks;
2  
3 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 our $VERSION = '0.007002';
6  
7 2 ▶use IO::Socket::INET ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use IO::Socket::UNIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Socket qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
10 SOCK_DGRAM
11 SOCK_STREAM
12 2 );
13 2 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use Zuzu::Util::NativeHelpers qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
16 native_class
17 native_function
18 native_object
19 zuzu_bool
20 2 );
21 2 ▶use Zuzu::Value::Function;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 0 ▶sub _sock_from_object {
Subroutine _sock_from_object: not called CC=4 SCAR=30.0
24 0 my ( $obj ) = @_;
25  
26 0 ▶ return undef if not blessed($obj);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($obj) 0 0 0
27 0 ▶ return undef if not $obj->isa( 'Zuzu::Value::Object' );
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->isa("Zuzu::Value::Object") 0 0 0
28 0 ▶ return undef if not exists $obj->slots->{_sock};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $obj->slots->{"_sock"} 0 0 0
29  
30 0 return $obj->slots->{_sock};
31 }
32  
33 0 ▶sub _peer_from_argument {
Subroutine _peer_from_argument: not called CC=6 SCAR=37.4
34 0 my ( $value ) = @_;
35  
36 0 ▶ return undef if not defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
37 0 ▶ return undef if not blessed($value);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($value) 0 0 0
38 0 ▶ return undef if not $value->isa( 'Zuzu::Value::Object' );
Branch: 1 branch
BranchTrueFalseTotal
unless $value->isa("Zuzu::Value::Object") 0 0 0
39 0 ▶ return undef if not exists $value->slots->{_sock};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $value->slots->{"_sock"} 0 0 0
40  
41 0 my $peer = $value->slots->{_sock};
42 0 ▶ return undef if not defined $peer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $peer 0 0 0
43  
44 0 return $peer->peername;
45 }
46  
47 0 ▶sub _raw_mode {
Subroutine _raw_mode: not called CC=2 SCAR=17.9
48 0 my ( $value ) = @_;
49  
50 0 ▶ return zuzu_bool( $value, 0 ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
zuzu_bool($value, 0) ? : 0 0 0
51 }
52  
53 0 ▶sub _call_callback {
Subroutine _call_callback: not called CC=3 SCAR=24.8
54 0 my ( $runtime, $callback, $args ) = @_;
55  
56 0 ▶ return if not blessed($callback)
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($callback) and $callback->isa("Zuzu::Value::Function") 0 0 0
Condition0%0/3
builtin::blessed($callback) and $callback->isa("Zuzu::Value::Function")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($callback) and $callback->isa("Zuzu::Value::Function")
builtin::blessed($callback) $callback->isa("Zuzu::Value::Function")
Truth table0%0/3
A and B
A= builtin::blessed($callback)B= $callback->isa("Zuzu::Value::Function")
ABresult
0X0
100
111
57 or not $callback->isa( 'Zuzu::Value::Function' );
58  
59 0 $runtime->_call_function( $callback, $args, '<std/io/socks>', 0 );
60  
61 0 return;
62 }
63  
64 0 ▶sub _assert_net {
Subroutine _assert_net: not called CC=1 SCAR=6.9
65 0 my ( $runtime, $method ) = @_;
66  
67 0 $runtime->assert_capability(
68 'net',
69 "Socks.$method is denied by runtime policy",
70 '<runtime>',
71 0,
72 );
73  
74 0 return;
75 }
76  
77 0 ▶sub _new_socket_object {
Subroutine _new_socket_object: not called CC=2 SCAR=17.9
78 0 my ( $class_obj, $socket, $is_raw ) = @_;
79  
80 0 ▶ return native_object(
Branch: 1 branch
BranchTrueFalseTotal
$is_raw ? : 0 0 0
81 class => $class_obj,
82 slots => {
83 _sock => $socket,
84 _raw => $is_raw ? 1 : 0,
85 },
86 const => {
87 _sock => 1,
88 _raw => 0,
89 },
90 );
91 }
92  
93 0 ▶sub _apply_read_mode {
Subroutine _apply_read_mode: not called CC=3 SCAR=24.8
94 0 my ( $socket, $raw ) = @_;
95  
96 0 ▶ return if not defined $socket;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $socket 0 0 0
97 0 ▶ return if $raw;
Branch: 1 branch
BranchTrueFalseTotal
if $raw 0 0 0
98  
99 0 binmode $socket, ':encoding(UTF-8)';
100  
101 0 return;
102 }
103  
104 0 ▶sub _recv_max_bytes {
Subroutine _recv_max_bytes: not called CC=3 SCAR=24.8
105 0 my ( $value ) = @_;
106  
107 0 ▶ my $max = defined $value ? int( $value ) : 65536;
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 0 0 0
108 0 ▶ $max = 1 if $max < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $max < 1 0 0 0
109  
110 0 return $max;
111 }
112  
113 2 ▶sub IMPORT {
Subroutine IMPORT: called CC=3 SCAR=22.4
Pod undocumented
114 2 my ( $class, $runtime ) = @_;
115  
116 2 my $socket_class = native_class(
117 name => 'Socket',
118 );
119 2 my $tcp_socket_class = native_class(
120 name => 'TCPSocket',
121 );
122 2 my $tcp_server_class = native_class(
123 name => 'TCPServer',
124 );
125 2 my $udp_socket_class = native_class(
126 name => 'UDPSocket',
127 );
128 2 my $unix_socket_class = native_class(
129 name => 'UnixSocket',
130 );
131 2 my $unix_server_class = native_class(
132 name => 'UnixServer',
133 );
134  
135 2 for my $klass ( $socket_class, $tcp_socket_class, $udp_socket_class, $unix_socket_class ) {
136 $klass->methods->{read} = native_function(
137 name => 'read',
138 0 ▶ native => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
139 0 my ( $self, $length ) = @_;
140 0 _assert_net( $runtime, 'read' );
141 0 my $sock = _sock_from_object( $self );
142 0 ▶ return undef if not defined $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sock 0 0 0
143 0 ▶ my $bytes = defined $length ? int( $length ) : 4096;
Branch: 1 branch
BranchTrueFalseTotal
defined $length ? : 0 0 0
144 0 ▶ $bytes = 1 if $bytes < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $bytes < 1 0 0 0
145 0 my $buffer = '';
146 0 my $count = read( $sock, $buffer, $bytes );
147 0 ▶ return undef if not defined $count;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $count 0 0 0
148 0 ▶ return undef if $count == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $count == 0 0 0 0
149 0 return $buffer;
150 },
151 8 );
152  
153 $klass->methods->{write} = native_function(
154 name => 'write',
155 0 ▶ native => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
156 0 my ( $self, @args ) = @_;
157 0 _assert_net( $runtime, 'write' );
158 0 my $sock = _sock_from_object( $self );
159 0 ▶ return 0 if not defined $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sock 0 0 0
160 0 ▶ my $data = join '', map { defined $_ ? "$_" : '' } @args;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
161 0 my $ok = print { $sock } $data;
162 0 ▶ return $ok ? length( $data ) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 0 0 0
163 },
164 8 );
165  
166 $klass->methods->{print} = native_function(
167 name => 'print',
168 0 ▶ native => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
169 0 my ( $self, @args ) = @_;
170 0 _assert_net( $runtime, 'print' );
171 0 my $sock = _sock_from_object( $self );
172 0 ▶ return undef if not defined $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sock 0 0 0
173 0 ▶ my $data = join '', map { defined $_ ? "$_" : '' } @args;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
174 0 print { $sock } $data;
175 0 return undef;
176 },
177 8 );
178  
179 $klass->methods->{say} = native_function(
180 name => 'say',
181 0 ▶ native => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
182 0 my ( $self, @args ) = @_;
183 0 _assert_net( $runtime, 'say' );
184 0 my $sock = _sock_from_object( $self );
185 0 ▶ return undef if not defined $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sock 0 0 0
186 0 ▶ my $data = join '', map { defined $_ ? "$_" : '' } @args;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
187 0 print { $sock } $data . "\n";
188 0 return undef;
189 },
190 8 );
191  
192 $klass->methods->{next_line} = native_function(
193 name => 'next_line',
194 0 ▶ native => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
195 0 my ( $self, $raw_opt ) = @_;
196 0 _assert_net( $runtime, 'next_line' );
197 0 my $sock = _sock_from_object( $self );
198 0 ▶ return undef if not defined $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sock 0 0 0
199 0 my $raw = _raw_mode( $raw_opt );
200 0 _apply_read_mode( $sock, $raw );
201 0 my $line = <$sock>;
202 0 ▶ return defined $line ? $line : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $line ? : 0 0 0
203 },
204 8 );
205  
206 $klass->methods->{each_line} = native_function(
207 name => 'each_line',
208 0 ▶ native => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
209 0 my ( $self, $callback, $raw_opt ) = @_;
210 0 _assert_net( $runtime, 'each_line' );
211 0 my $sock = _sock_from_object( $self );
212 0 ▶ return $self if not defined $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sock 0 0 0
213 0 my $raw = _raw_mode( $raw_opt );
214 0 _apply_read_mode( $sock, $raw );
215 0 ▶ while ( my $line = <$sock> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $sock)) 0 0 0
216 0 _call_callback( $runtime, $callback, [ $line ] );
217 }
218 0 return $self;
219 },
220 8 );
221  
222 $klass->methods->{close} = native_function(
223 name => 'close',
224 0 ▶ native => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
225 0 my ( $self ) = @_;
226 0 _assert_net( $runtime, 'close' );
227 0 my $sock = _sock_from_object( $self );
228 0 ▶ return 0 if not defined $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sock 0 0 0
229 0 my $ok = close $sock;
230 0 ▶ return $ok ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 0 0 0
231 },
232 8 );
233 }
234  
235 $socket_class->methods->{is_open} = native_function(
236 name => 'is_open',
237 0 ▶ native => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
238 0 my ( $self ) = @_;
239 0 _assert_net( $runtime, 'is_open' );
240 0 my $sock = _sock_from_object( $self );
241 0 ▶ return 0 if not defined $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sock 0 0 0
242 0 ▶ return fileno( $sock ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
fileno $sock ? : 0 0 0
243 },
244 2 );
245  
246 $tcp_socket_class->methods->{peer_host} = native_function(
247 name => 'peer_host',
248 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
249 0 my ( $self ) = @_;
250 0 my $sock = _sock_from_object( $self );
251 0 ▶ return undef if not defined $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sock 0 0 0
252 0 return $sock->peerhost;
253 },
254 2 );
255  
256 $tcp_socket_class->methods->{peer_port} = native_function(
257 name => 'peer_port',
258 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
259 0 my ( $self ) = @_;
260 0 my $sock = _sock_from_object( $self );
261 0 ▶ return undef if not defined $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sock 0 0 0
262 0 return $sock->peerport;
263 },
264 2 );
265  
266  
267 2 for my $klass ( $tcp_server_class, $unix_server_class ) {
268 $klass->methods->{close} = native_function(
269 name => 'close',
270 0 ▶ native => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
271 0 my ( $self ) = @_;
272 0 my $sock = _sock_from_object( $self );
273 0 ▶ return 0 if not defined $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sock 0 0 0
274 0 my $ok = close $sock;
275 0 ▶ return $ok ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 0 0 0
276 },
277 4 );
278 }
279  
280 $tcp_server_class->methods->{accept} = native_function(
281 name => 'accept',
282 0 ▶ native => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
283 0 my ( $self, $raw_opt ) = @_;
284 0 _assert_net( $runtime, 'accept' );
285 0 my $server = _sock_from_object( $self );
286 0 ▶ return undef if not defined $server;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $server 0 0 0
287 0 my $client = $server->accept();
288 0 ▶ return undef if not defined $client;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $client 0 0 0
289 0 my $raw = _raw_mode( $raw_opt );
290 0 _apply_read_mode( $client, $raw );
291 0 return _new_socket_object( $tcp_socket_class, $client, $raw );
292 },
293 2 );
294  
295 $tcp_server_class->methods->{port} = native_function(
296 name => 'port',
297 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
298 0 my ( $self ) = @_;
299 0 my $server = _sock_from_object( $self );
300 0 ▶ return undef if not defined $server;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $server 0 0 0
301 0 return $server->sockport;
302 },
303 2 );
304  
305 $tcp_server_class->methods->{host} = native_function(
306 name => 'host',
307 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
308 0 my ( $self ) = @_;
309 0 my $server = _sock_from_object( $self );
310 0 ▶ return undef if not defined $server;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $server 0 0 0
311 0 return $server->sockhost;
312 },
313 2 );
314  
315  
316 $udp_socket_class->methods->{port} = native_function(
317 name => 'port',
318 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
319 0 my ( $self ) = @_;
320 0 my $sock = _sock_from_object( $self );
321 0 ▶ return undef if not defined $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sock 0 0 0
322 0 return $sock->sockport;
323 },
324 2 );
325  
326 $udp_socket_class->methods->{host} = native_function(
327 name => 'host',
328 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
329 0 my ( $self ) = @_;
330 0 my $sock = _sock_from_object( $self );
331 0 ▶ return undef if not defined $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sock 0 0 0
332 0 return $sock->sockhost;
333 },
334 2 );
335  
336 $udp_socket_class->methods->{recv} = native_function(
337 name => 'recv',
338 0 ▶ native => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
339 0 my ( $self, $max_bytes, $flags ) = @_;
340 0 _assert_net( $runtime, 'recv' );
341 0 my $sock = _sock_from_object( $self );
342 0 ▶ return undef if not defined $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sock 0 0 0
343 0 my $max = _recv_max_bytes( $max_bytes );
344 0 my $buffer = '';
345 0 ▶ my $ok = recv( $sock, $buffer, $max, int( $flags // 0 ) );
Condition0%0/2
$flags // 0
l!l
00
MC/DC0%0/1
$flags // 0
$flags
Truth table0%0/2
A
A= $flags
Aresult
11
00
346 0 ▶ return undef if not defined $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ok 0 0 0
347 0 return $buffer;
348 },
349 2 );
350  
351 $udp_socket_class->methods->{send} = native_function(
352 name => 'send',
353 0 ▶ native => sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
354 0 my ( $self, $data, $peer_or_flags, $maybe_flags ) = @_;
355 0 _assert_net( $runtime, 'send' );
356 0 my $sock = _sock_from_object( $self );
357 0 ▶ return 0 if not defined $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sock 0 0 0
358 0 ▶ my $payload = defined $data ? "$data" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $data ? : 0 0 0
359 0 my $peer = _peer_from_argument( $peer_or_flags );
360 0 my $flags = 0;
361 0 ▶ if ( defined $peer ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $peer) { } 0 0 0
362 0 ▶ $flags = int( $maybe_flags // 0 );
Condition0%0/2
$maybe_flags // 0
l!l
00
MC/DC0%0/1
$maybe_flags // 0
$maybe_flags
Truth table0%0/2
A
A= $maybe_flags
Aresult
11
00
363 }
364 else {
365 0 ▶ $flags = int( $peer_or_flags // 0 );
Condition0%0/2
$peer_or_flags // 0
l!l
00
MC/DC0%0/1
$peer_or_flags // 0
$peer_or_flags
Truth table0%0/2
A
A= $peer_or_flags
Aresult
11
00
366 }
367 0 my $written;
368 0 ▶ if ( defined $peer ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $peer) { } 0 0 0
369 0 $written = send( $sock, $payload, $flags, $peer );
370 }
371 else {
372 0 $written = send( $sock, $payload, $flags );
373 }
374 0 ▶ return defined $written ? $written : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $written ? : 0 0 0
375 },
376 2 );
377  
378 $unix_server_class->methods->{accept} = native_function(
379 name => 'accept',
380 0 ▶ native => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
381 0 my ( $self, $raw_opt ) = @_;
382 0 _assert_net( $runtime, 'accept' );
383 0 my $server = _sock_from_object( $self );
384 0 ▶ return undef if not defined $server;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $server 0 0 0
385 0 my $client = $server->accept();
386 0 ▶ return undef if not defined $client;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $client 0 0 0
387 0 my $raw = _raw_mode( $raw_opt );
388 0 _apply_read_mode( $client, $raw );
389 0 return _new_socket_object( $unix_socket_class, $client, $raw );
390 },
391 2 );
392  
393 $unix_server_class->methods->{path} = native_function(
394 name => 'path',
395 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
396 0 my ( $self ) = @_;
397 0 my $server = _sock_from_object( $self );
398 0 ▶ return undef if not defined $server;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $server 0 0 0
399 0 return $server->hostpath;
400 },
401 2 );
402  
403 my $connect_tcp = native_function(
404 name => 'connect_tcp',
405 0 ▶ native => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
406 0 my ( $host, $port, $raw_opt ) = @_;
407 0 _assert_net( $runtime, 'connect_tcp' );
408 0 my $raw = _raw_mode( $raw_opt );
409 0 ▶ my $sock = IO::Socket::INET->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $host ? : 0 0 0
Condition0%0/2
$port // 0
l!l
00
MC/DC0%0/1
$port // 0
$port
Truth table0%0/2
A
A= $port
Aresult
11
00
410 Proto => 'tcp',
411 PeerHost => defined $host ? "$host" : '127.0.0.1',
412 PeerPort => int( $port // 0 ),
413 );
414 0 ▶ return undef if not defined $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sock 0 0 0
415 0 _apply_read_mode( $sock, $raw );
416 0 return _new_socket_object( $tcp_socket_class, $sock, $raw );
417 },
418 2 );
419  
420 my $listen_tcp = native_function(
421 name => 'listen_tcp',
422 0 ▶ native => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
423 0 my ( $host, $port, $backlog ) = @_;
424 0 _assert_net( $runtime, 'listen_tcp' );
425 0 ▶ my $sock = IO::Socket::INET->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $host ? : 0 0 0
Condition0%0/4
$port // 0
l!l
00
$backlog // 5
l!l
00
MC/DC0%0/1
$port // 0
$port
MC/DC0%0/1
$backlog // 5
$backlog
Truth table0%0/2
A
A= $port
Aresult
11
00
Truth table0%0/2
A
A= $backlog
Aresult
11
00
426 Proto => 'tcp',
427 LocalHost => defined $host ? "$host" : '127.0.0.1',
428 LocalPort => int( $port // 0 ),
429 Listen => int( $backlog // 5 ),
430 ReuseAddr => 1,
431 );
432 0 ▶ return undef if not defined $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sock 0 0 0
433 0 return _new_socket_object( $tcp_server_class, $sock, 1 );
434 },
435 2 );
436  
437 my $bind_udp = native_function(
438 name => 'bind_udp',
439 0 ▶ native => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
440 0 my ( $host, $port, $raw_opt ) = @_;
441 0 _assert_net( $runtime, 'bind_udp' );
442 0 my $raw = _raw_mode( $raw_opt );
443 0 ▶ my $sock = IO::Socket::INET->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $host ? : 0 0 0
Condition0%0/2
$port // 0
l!l
00
MC/DC0%0/1
$port // 0
$port
Truth table0%0/2
A
A= $port
Aresult
11
00
444 Proto => 'udp',
445 LocalHost => defined $host ? "$host" : '127.0.0.1',
446 LocalPort => int( $port // 0 ),
447 Type => SOCK_DGRAM,
448 ReuseAddr => 1,
449 );
450 0 ▶ return undef if not defined $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sock 0 0 0
451 0 _apply_read_mode( $sock, $raw );
452 0 return _new_socket_object( $udp_socket_class, $sock, $raw );
453 },
454 2 );
455  
456 my $connect_udp = native_function(
457 name => 'connect_udp',
458 0 ▶ native => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
459 0 my ( $host, $port, $raw_opt ) = @_;
460 0 _assert_net( $runtime, 'connect_udp' );
461 0 my $raw = _raw_mode( $raw_opt );
462 0 ▶ my $sock = IO::Socket::INET->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $host ? : 0 0 0
Condition0%0/2
$port // 0
l!l
00
MC/DC0%0/1
$port // 0
$port
Truth table0%0/2
A
A= $port
Aresult
11
00
463 Proto => 'udp',
464 PeerHost => defined $host ? "$host" : '127.0.0.1',
465 PeerPort => int( $port // 0 ),
466 Type => SOCK_DGRAM,
467 );
468 0 ▶ return undef if not defined $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sock 0 0 0
469 0 _apply_read_mode( $sock, $raw );
470 0 return _new_socket_object( $udp_socket_class, $sock, $raw );
471 },
472 2 );
473  
474 my $listen_unix = native_function(
475 name => 'listen_unix',
476 0 ▶ native => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
477 0 my ( $path, $backlog ) = @_;
478 0 _assert_net( $runtime, 'listen_unix' );
479 0 ▶ return undef if not defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 0 0
480 0 unlink "$path";
481 0 ▶ my $sock = IO::Socket::UNIX->new(
Condition0%0/2
$backlog // 5
l!l
00
MC/DC0%0/1
$backlog // 5
$backlog
Truth table0%0/2
A
A= $backlog
Aresult
11
00
482 Type => SOCK_STREAM,
483 Local => "$path",
484 Listen => int( $backlog // 5 ),
485 );
486 0 ▶ return undef if not defined $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sock 0 0 0
487 0 return _new_socket_object( $unix_server_class, $sock, 1 );
488 },
489 2 );
490  
491 my $connect_unix = native_function(
492 name => 'connect_unix',
493 0 ▶ native => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
494 0 my ( $path, $raw_opt ) = @_;
495 0 _assert_net( $runtime, 'connect_unix' );
496 0 ▶ return undef if not defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 0 0
497 0 my $raw = _raw_mode( $raw_opt );
498 0 my $sock = IO::Socket::UNIX->new(
499 Type => SOCK_STREAM,
500 Peer => "$path",
501 );
502 0 ▶ return undef if not defined $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sock 0 0 0
503 0 _apply_read_mode( $sock, $raw );
504 0 return _new_socket_object( $unix_socket_class, $sock, $raw );
505 },
506 2 );
507  
508 return {
509 2 Socket => $socket_class,
510 TCPSocket => $tcp_socket_class,
511 TCPServer => $tcp_server_class,
512 UDPSocket => $udp_socket_class,
513 UnixSocket => $unix_socket_class,
514 UnixServer => $unix_server_class,
515 connect_tcp => $connect_tcp,
516 listen_tcp => $listen_tcp,
517 bind_udp => $bind_udp,
518 connect_udp => $connect_udp,
519 listen_unix => $listen_unix,
520 connect_unix => $connect_unix,
521 };
522 }
523  
524 =pod
525  
526 =head1 NAME
527  
528 Zuzu::Module::Socks - builtin std/io/socks module.
529  
530 =head1 SYNOPSIS
531  
532 from std/io/socks import *;
533  
534 let srv := listen_tcp( "127.0.0.1", 0 );
535 let cli := connect_tcp( "127.0.0.1", srv.port() );
536 let peer := srv.accept();
537  
538 cli.say( "hello" );
539 let line := peer.next_line();
540  
541 =head1 DESCRIPTION
542  
543 This module exposes practical networking helpers for:
544  
545 =over
546  
547 =item *
548  
549 TCP client/server sockets.
550  
551 =item *
552  
553 UDP sockets using datagram send/recv.
554  
555 =item *
556  
557 Unix-domain stream sockets.
558  
559 =back
560  
561 Where possible, socket methods are aligned with stream/file
562 interfaces, including C<read>, C<write>, C<print>, C<say>,
563 C<next_line>, C<each_line>, and C<close>.
564  
565 =head1 EXPORTED SYMBOLS
566  
567 =over
568  
569 =item C<Socket>
570  
571 Common stream-like methods across socket objects.
572  
573 =item C<TCPSocket>, C<TCPServer>
574  
575 TCP client/server classes.
576  
577 =item C<UDPSocket>
578  
579 UDP socket class with C<send> and C<recv>.
580  
581 =item C<UnixSocket>, C<UnixServer>
582  
583 Unix-domain stream client/server classes.
584  
585 =item C<connect_tcp>, C<listen_tcp>
586  
587 Create TCP client/server sockets.
588  
589 =item C<bind_udp>, C<connect_udp>
590  
591 Create UDP sockets.
592  
593 =item C<listen_unix>, C<connect_unix>
594  
595 Create Unix-domain stream sockets.
596  
597 =back
598  
599 =head1 COPYRIGHT AND LICENCE
600  
601 B<< Zuzu::Module::Socks >> is copyright Toby Inkster.
602  
603 It is free software; you may redistribute it and/or modify it under
604 the terms of either the Artistic License 1.0 or the GNU General Public
605 License version 2.
606  
607 =cut
608  
609 2 1;
« blib/lib/Zuzu/Doc/CLI.pm ↑ Summary blib/lib/Zuzu/Value/Array.pm »