blib/lib/IO/Socket/TIPC.pm

filter: Statementstmt 32.9% 53 / 161 Branchbran 4.9% 5 / 102 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 50.0% 12 / 24 Podpod 61.5% 8 / 13 totaltotal 22.6% 78 / 345 CC 65 SCAR 67.0
CC
65
Cov
20%
configure
1722.0
fixup_args
42.0
enforce_required_args
20.0
CRAP 809.3
↑ Summary blib/lib/IO/Socket/TIPC/Sockaddr.pm »
1 package IO::Socket::TIPC;
2 8 ▶use IO::Socket::TIPC::Sockaddr ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use AutoLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 our @ISA = qw(Exporter IO::Socket);
11  
12 8 our $VERSION = '1.08';
13  
14 =head1 NAME
15  
16 IO::Socket::TIPC - TIPC sockets for Perl
17  
18 =head1 SYNOPSIS
19  
20 use IO::Socket::TIPC;
21 my $sock = IO::Socket::TIPC->new(
22 SocketType => "stream",
23 Peer => "{1000, 100}"
24 );
25 die "Could not connect to {1000, 100}: $!\n" unless $sock;
26  
27 More in-depth examples are available in the B<EXAMPLES> section, below.
28  
29  
30 =head1 DESCRIPTION
31  
32 TIPC stands for Transparent Inter-Process Communication. See
33 http://tipc.sf.net/ for details.
34  
35 This perl module subclasses IO::Socket, in order to use TIPC sockets
36 in the customary (and convenient) Perl fashion.
37  
38 TIPC supports 4 types of socket: I<SOCK_STREAM>, I<SOCK_SEQPACKET>,
39 I<SOCK_RDM> and I<SOCK_DGRAM>. These are all available through this
40 perl API, though the usage varies depending on which kind of socket
41 you use.
42  
43 I<SOCK_STREAM> and I<SOCK_SEQPACKET> are connection-based sockets.
44 These sockets are strictly client/server. For servers, B<new>() will
45 call B<bind>() for you, to bind to a I<Local>* name, and you then
46 B<accept>() connections from clients, each of which get their own
47 socket (returned from B<accept>). For clients, B<new>() will call
48 B<connect>() for you, to connect to the specified I<Peer>* name, and
49 once that succeeds, you can do I/O on the socket directly. In this
50 respect, usage details are very similar to I<TCP> over I<IPv4>.
51  
52 See the B<EXAMPLES> section, for an example of connection-based socket
53 use.
54  
55 I<SOCK_RDM> and I<SOCK_DGRAM> are connectionless sockets. You cannot
56 use the normal send/recv/print/getline methods on them, because the
57 network stack will not know which host on the network to send or
58 receive from. Instead, once you have called B<new>() to create the
59 socket, you use B<sendto> and B<recvfrom> to send and receive
60 individual packets to/from a specified peer, indicated using an
61 IO::Socket::TIPC::Sockaddr class object.
62  
63 Connectionless sockets (I<SOCK_RDM> and I<SOCK_DGRAM>) are often
64 bind()ed to a particular I<Name> or I<Nameseq> address, in order to
65 allow them to listen for packets sent to a well-known destination
66 (the I<Name>). You can use I<LocalName> or I<LocalNameseq> parameters
67 to B<new>(), to select a name or name-sequence to bind to. As above,
68 these parameters internally become I<Name> and I<Nameseq> arguments to
69 IO::Socket::TIPC::Sockaddr->B<new>(), and the result is passed to
70 B<bind>(). This is very similar to typical uses of I<UDP> over
71 I<IPv4>.
72  
73 Since connectionless sockets are not linked to a particular peer, you
74 can use B<sendto> to send a packet to some peer with a given Name in
75 the network, and B<recvfrom> to receive replies from a peer in the
76 network who sends a packet to your I<Name> (or I<Nameseq>). You can
77 also use I<Nameseq> addressses to send multicast packets to *every*
78 peer with a given name. Please see the I<Programmers_Guide.txt>
79 document (linked in B<REFERENCES>) for more details.
80  
81 See the B<EXAMPLES> section, for an example of connection-less socket
82 use.
83  
84 =cut
85  
86  
87 sub AUTOLOAD {
88 # This AUTOLOAD is used to 'autoload' constants from the constant()
89 # XS function.
90  
91 52 ▶ my $constname;
Subroutine AUTOLOAD: called CC=3 SCAR=11.0
92 52 our $AUTOLOAD;
93 52 ($constname = $AUTOLOAD) =~ s/.*:://;
94 52 ▶ croak "&IO::Socket::TIPC::constant not defined" if $constname eq 'constant';
Branch: 1 branch
BranchTrueFalseTotal
if $constname eq "constant" 0 52 52
95 52 my ($error, $val) = constant($constname);
96 52 ▶ if ($error) { $val = undef; } # undefined constants just return undef.
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 7 45 52
97 {
98 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
99 52 ▶ *$AUTOLOAD = sub { $val };
Subroutine __ANON__: called CC=1 SCAR=0.0
100 }
101 52 goto &$AUTOLOAD;
102 }
103  
104 =head1 CONSTRUCTOR
105  
106 B<new> returns a TIPC socket object. This object inherits from
107 IO::Socket, and thus inherits all the methods of that class.
108  
109 This module was modeled specifically after I<IO::Socket::INET>, and
110 shares some things in common with that class. Specifically, the
111 I<Listen> parameter, the I<Peer>* and I<Loca>l* nomenclature, and the
112 behind-the-scenes calls to B<socket>(), B<bind>(), B<listen>(),
113 B<connect>(), and so on.
114  
115 Connection-based sockets (I<SOCK_STREAM> and I<SOCK_SEQPACKET>) come
116 in "server" and "client" varieties. To create a server socket,
117 specify the I<Listen> argument to B<new>(). You can bind a
118 name to the socket, thus making your server easier to find, by
119 providing one or more I<Local>* parameters. To create a client
120 socket, do B<NOT> provide the I<Listen> argument. Instead, provide
121 one or more I<Peer>* parameters, and B<new> will call B<connect>()
122 for you.
123  
124 All I<Local>* parameters are passed directly to
125 IO::Socket::TIPC::Sockaddr->B<new>(), minus the "Local" prefix, and the
126 resulting sockaddr is passed to B<bind>(). Similarly, all I<Peer>*
127 parameters are passed directly to IO::Socket::TIPC::Sockaddr->B<new>(),
128 minus the "Peer" prefix, and the result is passed to B<connect>(). The
129 keywords I<Local> and I<Peer> themselves become the first string
130 parameter to IO::Socket::TIPC::Sockaddr->B<new>(); see the
131 IO::Socket::TIPC::Sockaddr documentation for details.
132  
133 =head2 ARGUMENTS to new()
134  
135 =head2 SocketType
136  
137 This field is B<required>. It tells the system what type of socket
138 to use. The following constants will work, if they were imported:
139 I<SOCK_STREAM>, I<SOCK_SEQPACKET>, I<SOCK_RDM>, or I<SOCK_DGRAM>.
140 Otherwise, you can just use the following text strings: "stream",
141 "seqpacket", "rdm", or "dgram".
142  
143 =head2 Listen
144  
145 This field is only valid for connection-based B<SocketType>s. Its
146 existence specifies that this is a server socket. It is common to
147 also specify some I<Local>* arguments, so B<new>() can B<bind> your
148 shiny new server socket to a well-known name.
149  
150 =head2 Importance
151  
152 This field informs the TIPC network stack of what priority it should
153 consider delivering your messages to be. It corresponds to the
154 I<TIPC_IMPORTANCE> option, from B<setsockopt>. If you provide this
155 field, ->B<new>() will call B<setsockopt> for you to set the value.
156  
157 Default is I<TIPC_LOW_IMPORTANCE>. Valid arguments are any of:
158  
159 TIPC_LOW_IMPORTANCE
160 TIPC_MEDIUM_IMPORTANCE
161 TIPC_HIGH_IMPORTANCE
162 TIPC_CRITICAL_IMPORTANCE
163  
164 See I<Programmers_Guide.txt> (linked in B<REFERENCES>) for details.
165  
166  
167 =head2 ConnectTimeout
168  
169 This field specifies the B<connect>() timeout, in milliseconds. If
170 you provide this field, ->B<new>() will call B<setsockopt> to set
171 the I<TIPC_CONN_TIMEOUT> value on your socket.
172  
173 See I<Programmers_Guide.txt> (linked in B<REFERENCES>) for details.
174  
175 B<Careful>: I<ConnectTimeout> should not be confused with I<Timeout>,
176 which is handled internally by IO::Socket and means something else.
177  
178  
179 =head2 Local*
180  
181 This field is valid (and recommended) for all connectionless socket
182 types, and for all servers using connection-type sockets. The
183 I<Local>* parameter(s) determine which address your socket will get
184 B<bind>()ed to.
185  
186 Any arguments prefixed with "Local" will be passed to
187 IO::Socket::TIPC::Sockaddr->B<new>(), with the "Local" prefix
188 removed. If you specify the word I<Local>, itself, the argument
189 will be passed as the first string parameter to
190 IO::Socket::TIPC::Sockaddr->B<new>(); all other I<Local>* arguments
191 end up in the hash parameter list. See the documentation for
192 IO::Socket::TIPC::Sockaddr, for details. Also, skip to the
193 B<EXAMPLES> section to see what this stuff looks like.
194  
195 =head2 Peer*
196  
197 This field is only valid for B<clients> (as opposed to B<servers>)
198 using connection-type sockets, and is required for this case. The
199 I<Peer>* parameter(s) determine which address your socket will get
200 B<connect>()ed to.
201  
202 Any arguments prefixed with "Peer" will be passed to
203 IO::Socket::TIPC::Sockaddr->B<new>(), with the "Peer" prefix
204 removed. If you specify the word I<Peer>, itself, the argument
205 will be passed as the first string parameter to
206 IO::Socket::TIPC::Sockaddr->B<new>(); all other I<Peer>* arguments
207 end up in the hash parameter list. See the documentation for
208 IO::Socket::TIPC::Sockaddr, for details. Also, skip to the
209 B<EXAMPLES> section to see what this stuff looks like.
210  
211 =cut
212  
213 sub new {
214 # pass it down to IO::Socket
215 0 ▶ my $class = shift;
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
216 0 return IO::Socket::new($class,@_);
217 }
218  
219 sub configure {
220 # IO::Socket calls us back via this method call, from IO::Socket->new().
221 0 ▶ my($socket, $args) = @_;
Subroutine configure: not called CC=41 SCAR=74.5
Pod undocumented
222 0 my (%local, %peer, $local, $peer);
223 # move Local* args into %local, Peer* args into %peer.
224 # keys "Local" and "Peer" themselves go into $local and $peer.
225 # These become arguments to IO::Socket::TIPC::Sockaddr->new().
226 0 foreach my $key (sort keys %$args) {
227 0 ▶ if($key =~ /^local/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^local/i) 0 0 0
228 0 my $newkey = substr($key,5);
229 0 ▶ if(length($newkey)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $newkey) { } 0 0 0
230 0 $local{$newkey} = $$args{$key};
231 } else {
232 0 $local = $$args{$key};
233 }
234 0 delete($$args{$key});
235 }
236 0 ▶ if($key =~ /^peer/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^peer/i) 0 0 0
237 0 my $newkey = substr($key,4);
238 0 ▶ if(length($newkey)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $newkey) { } 0 0 0
239 0 $peer{$newkey} = $$args{$key};
240 } else {
241 0 $peer = $$args{$key};
242 }
243 0 delete($$args{$key});
244 }
245 }
246 0 ▶ return undef unless fixup_args($args);
Branch: 1 branch
BranchTrueFalseTotal
unless fixup_args($args) 0 0 0
247 0 ▶ return undef unless enforce_required_args($args);
Branch: 1 branch
BranchTrueFalseTotal
unless enforce_required_args($args) 0 0 0
248 0 my $connectionless = 0;
249 0 my $listener = 0;
250 0 ▶ my $connector = (scalar keys %peer) || (defined $peer);
Condition0%0/3
scalar keys %peer || defined $peer
l!l&&r!l&&!r
000
MC/DC0%0/2
scalar keys %peer || defined $peer
scalar keys %peer defined $peer
Truth table0%0/3
A || B
A= scalar keys %peerB= defined $peer
ABresult
1X1
011
000
251 0 ▶ my $binder = (scalar keys %local) || (defined $local);
Condition0%0/3
scalar keys %local || defined $local
l!l&&r!l&&!r
000
MC/DC0%0/2
scalar keys %local || defined $local
scalar keys %local defined $local
Truth table0%0/3
A || B
A= scalar keys %localB= defined $local
ABresult
1X1
011
000
252 0 ▶ $listener = 1 if(exists($$args{Listen}) && $$args{Listen});
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"Listen"} and $args->{"Listen"} 0 0 0
Condition0%0/3
exists $args->{"Listen"} and $args->{"Listen"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $args->{"Listen"} and $args->{"Listen"}
exists $args->{"Listen"} $args->{"Listen"}
Truth table0%0/3
A and B
A= exists $args->{"Listen"}B= $args->{"Listen"}
ABresult
0X0
100
111
253 0 ▶ unless(looks_like_number($$args{SocketType})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($args->{"SocketType"})) 0 0 0
254 0 my %socket_types = (
255 stream => SOCK_STREAM,
256 seqpacket => SOCK_SEQPACKET,
257 rdm => SOCK_RDM,
258 dgram => SOCK_DGRAM,
259 );
260 0 ▶ if(exists($socket_types{lc($$args{SocketType})})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $socket_types{lc $args->{'SocketType'}}) { } 0 0 0
261 0 $$args{SocketType} = $socket_types{lc($$args{SocketType})};
262 } else {
263 0 croak "unknown SocketType $$args{SocketType}!";
264 }
265 0 ▶ $connectionless = 1 if $$args{SocketType} == SOCK_RDM;
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"SocketType"} == 4 0 0 0
266 0 ▶ $connectionless = 1 if $$args{SocketType} == SOCK_DGRAM;
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"SocketType"} == 2 0 0 0
267 }
268 0 ▶ croak "Connectionless socket types cannot listen(), but you've told me to Listen."
Branch: 1 branch
BranchTrueFalseTotal
if $connectionless and $listener 0 0 0
Condition0%0/3
$connectionless and $listener
!ll&&!rl&&r
000
MC/DC0%0/2
$connectionless and $listener
$connectionless $listener
Truth table0%0/3
A and B
A= $connectionlessB= $listener
ABresult
0X0
100
111
269 if($connectionless && $listener);
270 0 ▶ croak "Connectionless socket types cannot connect(), but you've given me a Peer address."
Branch: 1 branch
BranchTrueFalseTotal
if $connectionless and $connector 0 0 0
Condition0%0/3
$connectionless and $connector
!ll&&!rl&&r
000
MC/DC0%0/2
$connectionless and $connector
$connectionless $connector
Truth table0%0/3
A and B
A= $connectionlessB= $connector
ABresult
0X0
100
111
271 if($connectionless && $connector);
272 0 ▶ croak "Listener sockets cannot connect, but you've given me a Peer address."
Branch: 1 branch
BranchTrueFalseTotal
if $listener and $connector 0 0 0
Condition0%0/3
$listener and $connector
!ll&&!rl&&r
000
MC/DC0%0/2
$listener and $connector
$listener $connector
Truth table0%0/3
A and B
A= $listenerB= $connector
ABresult
0X0
100
111
273 if($listener && $connector);
274 0 ▶ croak "Connect()ing sockets cannot bind, but you've given me a Local address."
Branch: 1 branch
BranchTrueFalseTotal
if $connector and $binder 0 0 0
Condition0%0/3
$connector and $binder
!ll&&!rl&&r
000
MC/DC0%0/2
$connector and $binder
$connector $binder
Truth table0%0/3
A and B
A= $connectorB= $binder
ABresult
0X0
100
111
275 if($connector && $binder);
276  
277 # If we've gotten this far, I figure everything is ok.
278 # unless Sockaddr barfs, of course.
279 0 ▶ $socket->socket(PF_TIPC(), $$args{SocketType}, 0)
Branch: 1 branch
BranchTrueFalseTotal
$socket->socket(PF_TIPC(), $args->{"SocketType"}, 0) or croak("Could not create socket: $!") 0 0 0
280 or croak "Could not create socket: $!";
281  
282 # setsockopt/fcntl stuff goes here.
283 0 ▶ if(exists $$args{ConnectTimeout}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"ConnectTimeout"}) 0 0 0
284 $socket->setsockopt(SOL_TIPC(), TIPC_CONN_TIMEOUT(), $$args{ConnectTimeout})
285 0 ▶ or croak "TIPC_CONN_TIMEOUT: $!";
Branch: 1 branch
BranchTrueFalseTotal
$socket->setsockopt(SOL_TIPC(), TIPC_CONN_TIMEOUT(), $args->{"ConnectTimeout"}) or croak("TIPC_CONN_TIMEOUT: $!") 0 0 0
286 }
287 0 ▶ if(exists $$args{Importance}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"Importance"}) 0 0 0
288 $socket->setsockopt(SOL_TIPC(), TIPC_IMPORTANCE() , $$args{Importance})
289 0 ▶ or croak "TIPC_IMPORTANCE: $!";
Branch: 1 branch
BranchTrueFalseTotal
$socket->setsockopt(SOL_TIPC(), TIPC_IMPORTANCE(), $args->{"Importance"}) or croak("TIPC_IMPORTANCE: $!") 0 0 0
290 }
291 0 ▶ if($binder) {
Branch: 1 branch
BranchTrueFalseTotal
if ($binder) 0 0 0
292 0 my $baddr;
293 0 ▶ if(defined($local)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $local) { } 0 0 0
294 0 ▶ if(ref($local) && ref($local) eq "IO::Socket::TIPC::Sockaddr") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $local and ref $local eq 'IO::Socket::TIPC::Sockaddr') { } 0 0 0
Condition0%0/3
ref $local and ref $local eq "IO::Socket::TIPC::Sockaddr"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $local and ref $local eq "IO::Socket::TIPC::Sockaddr"
ref $local ref $local eq "IO::Socket::TIPC::Sockaddr"
Truth table0%0/3
A and B
A= ref $localB= ref $local eq "IO::Socket::TIPC::Sockaddr"
ABresult
0X0
100
111
295 0 $baddr = $local;
296 } else {
297 0 $baddr = IO::Socket::TIPC::Sockaddr->new($local, %local);
298 }
299 } else {
300 0 $baddr = IO::Socket::TIPC::Sockaddr->new(%local);
301 }
302 0 ▶ $socket->bind($baddr)
Branch: 1 branch
BranchTrueFalseTotal
$socket->bind($baddr) or croak("Could not bind socket: $!") 0 0 0
303 or croak "Could not bind socket: $!";
304 }
305 0 ▶ if($connector) {
Branch: 1 branch
BranchTrueFalseTotal
if ($connector) 0 0 0
306 0 my $caddr;
307 0 ▶ if(defined($peer)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $peer) { } 0 0 0
308 0 ▶ if(ref($peer) && ref($peer) eq "IO::Socket::TIPC::Sockaddr") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $peer and ref $peer eq 'IO::Socket::TIPC::Sockaddr') { } 0 0 0
Condition0%0/3
ref $peer and ref $peer eq "IO::Socket::TIPC::Sockaddr"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $peer and ref $peer eq "IO::Socket::TIPC::Sockaddr"
ref $peer ref $peer eq "IO::Socket::TIPC::Sockaddr"
Truth table0%0/3
A and B
A= ref $peerB= ref $peer eq "IO::Socket::TIPC::Sockaddr"
ABresult
0X0
100
111
309 0 $caddr = $peer;
310 } else {
311 0 $caddr = IO::Socket::TIPC::Sockaddr->new($peer, %peer);
312 }
313 } else {
314 0 $caddr = IO::Socket::TIPC::Sockaddr->new(%peer);
315 }
316 0 ▶ $socket->connect($caddr)
Branch: 1 branch
BranchTrueFalseTotal
$socket->connect($caddr) or croak("Could not connect socket: $!") 0 0 0
317 or croak "Could not connect socket: $!";
318 }
319 0 ▶ if($listener) {
Branch: 1 branch
BranchTrueFalseTotal
if ($listener) 0 0 0
320 0 ▶ $socket->listen()
Branch: 1 branch
BranchTrueFalseTotal
$socket->listen or croak("Could not listen: $!") 0 0 0
321 or croak "Could not listen: $!";
322 }
323 0 return $socket;
324 }
325  
326 # a "0" denotes an optional value. a "1" is required.
327 8 my %valid_args = (
328 Listen => 0,
329 SocketType => 1,
330 Importance => 0,
331 ConnectTimeout => 0,
332 );
333  
334 sub enforce_required_args {
335 0 ▶ my $args = shift;
Subroutine enforce_required_args: not called CC=4 SCAR=30.0
Pod undocumented
336 0 foreach my $key (sort keys %$args) {
337 0 ▶ if($valid_args{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($valid_args{$key}) 0 0 0
338 # argument is required.
339 0 ▶ unless(exists($$args{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{$key}) 0 0 0
340 # argument not provided!
341 0 croak "argument $key is REQUIRED.";
342 }
343 }
344 }
345 0 return 1;
346 }
347  
348 sub fixup_args {
349 0 ▶ my $args = shift;
Subroutine fixup_args: not called CC=6 SCAR=37.4
Pod undocumented
350 # Validate hash-key arguments to IO::Socket::TIPC->new()
351 0 foreach my $key (sort keys %$args) {
352 0 ▶ if(!exists($valid_args{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $valid_args{$key}) 0 0 0
353 # This key needs to be fixed up. Search for it.
354 0 my $lckey = lc($key);
355 0 my $fixed = 0;
356 0 foreach my $goodkey (sort keys %valid_args) {
357 0 ▶ if($lckey eq lc($goodkey)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lckey eq lc $goodkey) 0 0 0
358 # Found it. Fix it up.
359 0 $$args{$goodkey} = $$args{$key};
360 0 delete($$args{$key});
361 0 $fixed = 1;
362 0 last;
363 }
364 }
365 0 ▶ croak("unknown argument $key")
Branch: 1 branch
BranchTrueFalseTotal
unless $fixed 0 0 0
366 unless $fixed;
367 }
368 }
369 0 return 1;
370 }
371  
372  
373 =head1 METHODS
374  
375 =head2 sendto(addr, message [, flags])
376  
377 B<sendto> is used with connectionless sockets, to send a message to a
378 given address. The addr parameter should be an
379 IO::Socket::TIPC::Sockaddr object.
380  
381 my $addr = IO::Socket::TIPC::Sockaddr->new("{4242, 100}");
382 $sock->sendto($addr, "Hello there!\n");
383  
384 The third parameter, I<flags>, defaults to 0 when not specified. The
385 TIPC I<Programmers_Guide.txt> says: "TIPC supports the I<MSG_DONTWAIT>
386 flag when sending; all other flags are ignored."
387  
388 You may have noticed that B<sendto> and the B<send> builtin do more
389 or less the same thing with the order of arguments changed. The main
390 reason to use B<sendto> is because you can pass it a
391 IO::Socket::TIPC::Sockaddr object directly, where B<send> requires you
392 to dereference the blessed reference to get at the raw binary "struct
393 sockaddr_tipc" bits. So, B<sendto> is just a matter of convenience.
394  
395 Ironically, this B<sendto> method calls the B<send> builtin, which in
396 turn calls the C B<sendto> function.
397  
398 =cut
399  
400 sub sendto {
401 0 ▶ my ($self, $addr, $message, $flags) = @_;
Subroutine sendto: not called CC=4 SCAR=30.0
Pod documented
402 0 ▶ croak "sendto given an undef message" unless defined $message;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message 0 0 0
403 0 ▶ croak "sendto given a non-address?"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $addr eq "IO::Socket::TIPC::Sockaddr" 0 0 0
404 unless ref($addr) eq "IO::Socket::TIPC::Sockaddr";
405 0 ▶ $flags = 0 unless defined $flags;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flags 0 0 0
406 0 return $self->send($message, $flags, $$addr);
407 }
408  
409  
410 =head2 recvfrom(buffer [, length [, flags]])
411  
412 B<recvfrom> is used with connectionless sockets, to receive a message
413 from a peer. It returns a IO::Socket::TIPC::Sockaddr object,
414 containing the address of whoever sent the message. It will write
415 the received packet (up to $length bytes) in $buffer.
416  
417 my $buffer;
418 my $sender = $sock->recvfrom($buffer, 30);
419 $sock->sendto($sender, "I got your message.");
420  
421 The second parameter, I<length>, defaults to I<TIPC_MAX_USER_MSG_SIZE>
422 when not specified. The third parameter, I<flags>, defaults to 0 when
423 not specified.
424  
425 The TIPC I<Programmers_Guide.txt> says: "TIPC supports the I<MSG_PEEK>
426 flag when receiving, as well as the I<MSG_WAITALL> flag when receiving
427 on a I<SOCK_STREAM> socket; all other flags are ignored."
428  
429 You may have noticed that B<recvfrom> and the B<recv> builtin do
430 more or less the same thing with the order of arguments changed.
431 The main reason to use B<recvfrom> is because it will return a
432 IO::Socket::TIPC::Sockaddr object, where B<recv> just returns a
433 binary blob containing the C "struct sockaddr_tipc" data, which, by
434 itself, cannot be inspected or modified. So, B<recvfrom> is just a
435 matter of convenience.
436  
437 Ironically, this B<recvfrom> method calls the B<recv> builtin, which
438 in turn calls the C B<recvfrom> function.
439  
440 =cut
441  
442 sub recvfrom {
443 # note: the $buffer argument is written to by recv().
444 0 ▶ my ($self, $buffer, $length, $flags) = @_;
Subroutine recvfrom: not called CC=4 SCAR=30.0
Pod documented
445 0 ▶ $flags = 0 unless defined $flags;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flags 0 0 0
446 0 ▶ $length = TIPC_MAX_USER_MSG_SIZE() unless defined $length;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $length 0 0 0
447 0 ▶ croak "how am I supposed to recvfrom() a packet of length 0?"
Branch: 1 branch
BranchTrueFalseTotal
unless $length > 0 0 0 0
448 unless $length > 0;
449 0 my $rv = $self->recv($_[1], $length, $flags);
450 0 return IO::Socket::TIPC::Sockaddr->new_from_data($rv);
451 }
452  
453  
454 =head2 bind(addr)
455  
456 B<bind> attaches a well-known "name" to an otherwise random (and hard
457 to find) socket port. It is possible to bind more than one name to a
458 socket. B<bind> is useful for all connectionless sockets, and for
459 "server" sockets (the one you get from B<new>(I<Listen> => 1), not the
460 ones returned from B<accept>).
461  
462 This method is really just a wrapper around the Perl B<bind> builtin,
463 which dereferences IO::Socket::TIPC::Sockaddr class instances when
464 necessary.
465  
466 =cut
467  
468 sub bind {
469 0 ▶ my ($sock, $addr) = @_;
Subroutine bind: not called CC=2 SCAR=17.9
Pod documented
470 0 ▶ $addr = $$addr while ref $addr;
Branch: 1 branch
BranchTrueFalseTotal
ref $addr and do { $addr = $$addr } 0 0 0
471 0 return $sock->SUPER::bind($addr);
472 }
473  
474  
475 =head2 connect(addr)
476  
477 B<connect> seeks out a server socket (which was B<bind>ed to a
478 well-known "name") and connects to it. B<connect> is only valid for
479 connection-type sockets which have not already had B<listen> or
480 B<bind> called on them. In practice, you should not ever need this
481 method; B<new> calls it for you when you specify one or more
482 I<Peer> arguments.
483  
484 This method is really just a wrapper around the Perl B<connect>
485 builtin, which dereferences IO::Socket::TIPC::Sockaddr class
486 instances when necessary.
487  
488 =cut
489  
490 sub connect {
491 0 ▶ my ($sock, $addr) = @_;
Subroutine connect: not called CC=2 SCAR=17.9
Pod documented
492 0 ▶ $addr = $$addr while ref $addr;
Branch: 1 branch
BranchTrueFalseTotal
ref $addr and do { $addr = $$addr } 0 0 0
493 0 return $sock->SUPER::connect($addr);
494 }
495  
496  
497 =head2 getpeername
498  
499 B<getpeername> returns the sockaddr of the peer you're connected
500 to. Compare B<getsockname>. Use this if you've just B<accept>()ed
501 a new connection, and you're curious who you're talking to.
502  
503 my $client = $server->accept();
504 my $caddr = $client->getpeername();
505 print("Got connection from ", $caddr->stringify(), "\n");
506  
507 B<getpeername> doesn't actually return a I<name> sockaddr, it returns
508 an I<id>. Thus, B<getpeerid> is an alias for B<getpeername>, to aid
509 readability. I<Programmers_Guide.txt> has the following comment: The
510 use of "name" in getpeername() can be confusing, as the routine does
511 not actually return the TIPC names or name sequences that have been
512 bound to the peer socket.
513  
514 This method is really just a wrapper around the Perl B<getpeername>
515 builtin, to wrap return values into IO::Socket::TIPC::Sockaddr class
516 instances for you.
517  
518  
519 =cut
520  
521 sub getpeername {
522 0 ▶ my ($self) = @_;
Subroutine getpeername: not called CC=2 SCAR=17.9
Pod documented
523 0 my $rv = CORE::getpeername($self);
524 0 ▶ return $rv unless defined $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 0 0
525 0 return IO::Socket::TIPC::Sockaddr->new_from_data($rv);
526 }
527 0 ▶sub getpeerid { my $self = shift; return $self->getpeername(@_) };
Subroutine getpeerid: not called CC=1 SCAR=6.9
Pod undocumented
528  
529 =head2 getsockname
530  
531 B<getsockname> returns the sockaddr of your own socket, this is the
532 address your peer sees you coming from. Compare B<getpeername>.
533  
534 my $client = $server->accept();
535 my $caddr = $client->getsockname();
536 print("The client connected to me as ", $caddr->stringify(), "\n");
537  
538 B<getsockname> doesn't actually return a I<name> sockaddr, it returns
539 an I<id>. Thus, B<getsockid> is an alias for B<getsockname>, to aid
540 readability. I<Programmers_Guide.txt> has the following comment: The
541 use of "name" in getsockname() can be confusing, as the routine does
542 not actually return the TIPC names or name sequences that have been
543 bound to the peer socket.
544  
545 This method is really just a wrapper around the Perl B<getsockname>
546 builtin, to wrap return values into IO::Socket::TIPC::Sockaddr class
547 instances for you.
548  
549  
550 =cut
551  
552 sub getsockname {
553 0 ▶ my ($self) = @_;
Subroutine getsockname: not called CC=2 SCAR=17.9
Pod documented
554 0 my $rv = CORE::getsockname($self);
555 0 ▶ return $rv unless defined $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 0 0
556 0 return IO::Socket::TIPC::Sockaddr->new_from_data($rv);
557 }
558 0 ▶sub getsockid { my $self = shift; return $self->getsockname(@_) };
Subroutine getsockid: not called CC=1 SCAR=6.9
Pod undocumented
559  
560  
561 =head2 listen
562  
563 B<listen> tells the operating system that this is a server socket,
564 and that you will be B<accept>()ing client connections on it. It is
565 only valid for connection-type sockets, and only if B<connect> has
566 not been called on it. It is much more useful if you have B<bind>ed
567 the socket to a well-known name; otherwise, most clients will have
568 difficulty knowing what to B<connect> to.
569  
570 This module does not actually implement a B<listen> method; when you
571 call it, you are really just calling the Perl builtin. See the
572 perlfunc manpage for more details.
573  
574 =head2 accept
575  
576 B<accept> asks the operating system to return a session socket, for
577 communicating with a client which has just B<connect>ed to you. It is
578 only valid for connection-type sockets, which you have previously
579 called B<listen> on.
580  
581 This module does not actually implement an B<accept> method; when you
582 call it, you are really just calling the Perl builtin. See the
583 perlfunc manpage for more details.
584  
585  
586 =head2 getsockopt(level, optname)
587  
588 Query a socket option. For TIPC-level stuff, I<level> should be
589 I<SOL_TIPC>.
590  
591 The TIPC I<Programmers_Guide.txt> (linked in B<REFERENCES>) says:
592 - TIPC does not currently support socket options for level
593 SOL_SOCKET, such as SO_SNDBUF.
594 - TIPC does not currently support socket options for level
595 IPPROTO_TCP, such as TCP_MAXSEG. Attempting to get the value
596 of these options on a SOCK_STREAM socket returns the value 0.
597  
598 See B<setsockopt>(), below, for a list of I<SOL_TIPC> options.
599  
600 This module does not actually implement a B<getsockopt> method; when
601 you call it, you are really just calling the Perl builtin. See the
602 perlfunc manpage for more details.
603  
604  
605 =head2 setsockopt(level, optname, optval)
606  
607 Set a socket option. For TIPC-level stuff, I<level> should be
608 I<SOL_TIPC>.
609  
610 The TIPC I<Programmers_Guide.txt> (linked in B<REFERENCES>) says:
611 - TIPC does not currently support socket options for level
612 SOL_SOCKET, such as SO_SNDBUF.
613 - TIPC does not currently support socket options for level
614 IPPROTO_TCP, such as TCP_MAXSEG. Attempting to get the value
615 of these options on a SOCK_STREAM socket returns the value 0.
616  
617 For level I<SOL_TIPC>, the following options are available:
618  
619 TIPC_IMPORTANCE
620 TIPC_SRC_DROPPABLE
621 TIPC_DEST_DROPPABLE
622 TIPC_CONN_TIMEOUT
623  
624 These are documented in detail in I<Programmers_Guide.txt>. See also,
625 ->B<new>()'s I<Importance> and I<ConnectTimeout> options.
626  
627 This module does not actually implement a B<setsockopt> method; when
628 you call it, you are really just calling the Perl builtin. See the
629 perlfunc manpage for more details.
630  
631  
632 =head2 detect()
633  
634 B<detect> determines whether TIPC is usable on your system. It will
635 return a true value if it detects TIPC support has been loaded into
636 your operating system kernel, and will return 0 otherwise.
637  
638 =cut
639  
640 sub detect {
641 5 ▶ if($^O eq 'linux') {
Subroutine detect: called CC=5 SCAR=23.0
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if ($^O eq 'linux') { } 5 0 5
elsif ($^O eq 'solaris') { } 0 0 0
642 5 ▶ return 1 if `grep -c ^TIPC /proc/net/protocols` == 1;
Branch: 1 branch
BranchTrueFalseTotal
if `grep -c ^TIPC /proc/net/protocols` == 1 0 5 5
643 }
644 elsif($^O eq 'solaris') {
645 0 ▶ return 1 if `modinfo -c | grep -w tipc | grep -cv UNLOADED` == 1;
Branch: 1 branch
BranchTrueFalseTotal
if `modinfo -c | grep -w tipc | grep -cv UNLOADED` == 1 0 0 0
646 }
647 5 return 0;
648 }
649  
650  
651 =head1 EXAMPLES
652  
653 Examples of connection-based socket use:
654  
655 # SERVER PROCESS
656 # create a server listening on Name {4242, 100}.
657 $sock1 = IO::Socket::TIPC->new(
658 SocketType => "seqpacket",
659 Listen => 1,
660 Local => "{4242, 100}",
661 LocalScope => "zone",
662 );
663 $client = $sock1->accept();
664 # Wait for the client to say something intelligent
665 $something_intelligent = $client->getline();
666  
667  
668 # CLIENT PROCESS
669 # connect to the above server
670 $sock2 = IO::Socket::TIPC->new(
671 SocketType => "seqpacket",
672 Peer => "{4242, 100}",
673 );
674 # Say something intelligent
675 $sock2->print("Dah, he is Olle, you are Sven.\n");
676
677 Examples of connectionless socket use:
678  
679 # NODE 1
680 # Create a server listening on Name {4242, 101}.
681 $sock1 = IO::Socket::TIPC->new(
682 SocketType => "rdm",
683 Local => "{4242, 101}",
684 LocalScope => "zone",
685 );
686 $data = "TAG! You are \"it\".\n";
687 # send a hello packet from sock1 to sock2
688 $addr2 = IO::Socket::TIPC::Sockaddr->new("{4242, 102}");
689 $sock1->sendto($addr2, $data);
690  
691  
692 # NODE 2
693 # Create another server listening on Name {4242, 102}.
694 $sock2 = IO::Socket::TIPC->new(
695 SocketType => "rdm",
696 Local => "{4242, 102}",
697 LocalScope => "zone",
698 );
699 # receive that first hello packet
700 $sender = $sock2->recvfrom($rxdata, 256);
701 # Reply
702 $sock2->sendto($sender, "Me too.\n");
703
704 # send a multicast packet to all sock1s in the world
705 $maddr1 = IO::Socket::TIPC::Sockaddr->new("{4242,101,101}");
706 $sock2->sendto($maddr2, "My brain hurts!\n");
707  
708  
709 =head1 EXPORT
710  
711 None by default.
712  
713 =head2 Exportable constants and macros
714  
715 ":tipc" tag (defines from tipc.h, loosely grouped by function):
716 AF_TIPC, PF_TIPC, SOL_TIPC
717 TIPC_ADDR_ID, TIPC_ADDR_MCAST, TIPC_ADDR_NAME, TIPC_ADDR_NAMESEQ
718 TIPC_ZONE_SCOPE, TIPC_CLUSTER_SCOPE, TIPC_NODE_SCOPE
719 TIPC_ERRINFO, TIPC_RETDATA, TIPC_DESTNAME
720 TIPC_IMPORTANCE, TIPC_SRC_DROPPABLE, TIPC_DEST_DROPPABLE,
721 TIPC_CONN_TIMEOUT
722 TIPC_LOW_IMPORTANCE, TIPC_MEDIUM_IMPORTANCE, TIPC_HIGH_IMPORTANCE,
723 TIPC_CRITICAL_IMPORTANCE
724 TIPC_MAX_USER_MSG_SIZE
725 TIPC_OK, TIPC_ERR_NO_NAME, TIPC_ERR_NO_NODE, TIPC_ERR_NO_PORT,
726 TIPC_ERR_OVERLOAD, TIPC_CONN_SHUTDOWN
727 TIPC_PUBLISHED, TIPC_WITHDRAWN, TIPC_SUBSCR_TIMEOUT
728 TIPC_SUB_NO_BIND_EVTS, TIPC_SUB_NO_UNBIND_EVTS,
729 TIPC_SUB_PORTS, TIPC_SUB_SERVICE, TIPC_SUB_SINGLE_EVT
730 TIPC_CFG_SRV, TIPC_TOP_SRV, TIPC_RESERVED_TYPES
731 TIPC_WAIT_FOREVER
732 tipc_addr, tipc_zone, tipc_cluster, tipc_node
733  
734 (Those last 4 are re-exports from the Sockaddr module. See the
735 IO::Socket::TIPC::Sockaddr documentation.)
736  
737 ":sock" tag (re-exports from IO::Socket):
738 SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET, SOCK_RDM
739 MSG_DONTWAIT, MSG_PEEK, MSG_WAITALL, MSG_CTRUNC
740  
741 To get all of the above constants, say:
742  
743 use IO::Socket::TIPC ":all";
744  
745 To get all of the tipc stuff, say:
746  
747 use IO::Socket::TIPC ":tipc";
748  
749 To get only the socket stuff, say:
750  
751 use IO::Socket::TIPC ":sock";
752  
753 To get only the constants you plan to use, say something like:
754  
755 use IO::Socket::TIPC qw(SOCK_RDM TIPC_NODE_SCOPE TIPC_ADDR_NAMESEQ);
756  
757 Despite supporting all the above constants, please note that some
758 effort was made so normal users will never actually need any of
759 them. For instance, in place of the I<SOCK_>* socktypes, you can
760 just specify "stream", "dgram", "seqpacket" or "rdm". In place
761 of the I<TIPC_>*I<_SCOPE> defines, given to
762 IO::Socket::TIPC::Sockaddr->B<new>() as the I<Scope> parameter, you
763 can simply say I<"zone">, I<"cluster"> or I<"node">.
764  
765 =cut
766  
767  
768  
769 8 ▶use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
770 8 XSLoader::load('IO::Socket::TIPC', $VERSION);
771  
772 8 IO::Socket::TIPC->register_domain(PF_TIPC());
773  
774 8 my @TIPC_STUFF = ( qw(
775 AF_TIPC PF_TIPC SOL_TIPC TIPC_ADDR_ID TIPC_ADDR_MCAST TIPC_ADDR_NAME
776 TIPC_ADDR_NAMESEQ TIPC_CFG_SRV TIPC_CLUSTER_SCOPE TIPC_CONN_SHUTDOWN
777 TIPC_CONN_TIMEOUT TIPC_CRITICAL_IMPORTANCE TIPC_DESTNAME
778 TIPC_DEST_DROPPABLE TIPC_ERRINFO TIPC_ERR_NO_NAME TIPC_ERR_NO_NODE
779 TIPC_ERR_NO_PORT TIPC_ERR_OVERLOAD TIPC_HIGH_IMPORTANCE TIPC_IMPORTANCE
780 TIPC_LOW_IMPORTANCE TIPC_MAX_USER_MSG_SIZE TIPC_MEDIUM_IMPORTANCE
781 TIPC_NODE_SCOPE TIPC_OK TIPC_PUBLISHED TIPC_RESERVED_TYPES TIPC_RETDATA
782 TIPC_SRC_DROPPABLE TIPC_SUBSCR_TIMEOUT TIPC_SUB_NO_BIND_EVTS
783 TIPC_SUB_NO_UNBIND_EVTS TIPC_SUB_PORTS TIPC_SUB_SERVICE TIPC_SUB_SINGLE_EVT
784 TIPC_TOP_SRV TIPC_WAIT_FOREVER TIPC_WITHDRAWN TIPC_ZONE_SCOPE
785 tipc_addr tipc_zone tipc_cluster tipc_node
786 ) );
787 8 my @SOCK_STUFF = ( qw(
788 SOCK_STREAM SOCK_DGRAM SOCK_SEQPACKET SOCK_RDM
789 MSG_DONTWAIT MSG_PEEK MSG_WAITALL MSG_CTRUNC
790 ) );
791  
792 8 our @EXPORT = qw();
793 8 our @EXPORT_OK = qw();
794  
795 8 our %EXPORT_TAGS = (
796 'all' => [ @TIPC_STUFF, @SOCK_STUFF ],
797 'tipc' => [ @TIPC_STUFF ],
798 'sock' => [ @SOCK_STUFF ],
799 );
800 8 Exporter::export_ok_tags('all');
801  
802 8 1;
803 __END__
↑ Summary blib/lib/IO/Socket/TIPC/Sockaddr.pm »