blib/lib/IO/Socket/TIPC/Sockaddr.pm

filter: Statementstmt 47.7% 96 / 201 Branchbran 30.4% 39 / 128 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 63.1% 12 / 19 Podpod 35.7% 5 / 14 totaltotal 41.9% 152 / 362 CC 69 SCAR 33.1
CC
69
Cov
41%
string_parsing_stuff
66.1
new
36.1
divine_address_type
31.9
CRAP 27.4
« blib/lib/IO/Socket/TIPC.pm ↑ Summary tipc.h »
1 package IO::Socket::TIPC::Sockaddr;
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 our @ISA = qw(Exporter);
9  
10 =head1 NAME
11  
12 IO::Socket::TIPC::Sockaddr - struct sockaddr_tipc class
13  
14 =head1 SYNOPSIS
15  
16 use IO::Socket::TIPC::Sockaddr;
17  
18  
19 =head1 DESCRIPTION
20  
21 TIPC Sockaddrs are used with TIPC sockets, to specify local or remote
22 endpoints for communication. They are used in the B<bind>(),
23 B<connect>(), B<sendto>() and B<recvfrom>() calls.
24  
25 Sockaddrs can be broken down into 3 address-types, I<"name">,
26 I<"nameseq"> and I<"id">. the I<Programmers_Guide.txt> (linked to in
27 B<REFERENCES>) explains the details much better than I ever could, I suggest
28 reading it before trying to use this module. Also, the B<EXAMPLES> section
29 is useful for getting a feel for how this module works.
30  
31 =cut
32  
33  
34 # Virtually this whole file is just hand-holding for the caller's benefit.
35 #
36 # You can pass it strings like Id => "<a.b.c:d>", or Nameseq => "{a,b,c}".
37 # You can pass it the pieces, like AddrType => 'name', Type => 4242, Instance => 1.
38 # You can pass it a mixture of the two, like Id => "<a.b.c>", Ref => 8295.
39 # You can even omit the AddrType parameter, it'll guess from the other args.
40  
41 # Passing the pieces (and specifying the AddrType) is the most efficient way to
42 # use this module, but not the most convenient, so other options exist.
43  
44  
45 sub divine_address_type {
46 1 ▶ my $args = shift;
Subroutine divine_address_type: called CC=8 SCAR=34.6
Pod undocumented
47 # try to figure out what type of address this is.
48 1 ▶ if(exists($$args{Type})) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args->{'Type'}) { } 1 0 1
elsif (exists $args->{'Ref'}) { } 0 0 0
49 1 ▶ if(exists($$args{Instance})) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $args->{'Instance'}) { } 1 0 1
elsif (exists $args->{'Lower'}) { } 0 0 0
elsif (exists $args->{'Upper'}) { } 0 0 0
50 1 $$args{AddrType} = 'name';
51 }
52 elsif(exists($$args{Lower})) {
53 0 $$args{AddrType} = 'nameseq';
54 $$args{Upper} = $$args{Lower}
55 0 ▶ unless exists $$args{Upper};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"Upper"} 0 0 0
56 }
57 elsif(exists($$args{Upper})) {
58 0 $$args{AddrType} = 'nameseq';
59 $$args{Lower} = $$args{Upper}
60 0 ▶ unless exists $$args{Lower};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"Lower"} 0 0 0
61 }
62 } elsif(exists($$args{Ref})) {
63 0 $$args{AddrType} = 'id';
64 } else {
65 0 croak("could not guess AddrType - please specify it");
66 }
67 1 return 1;
68 }
69  
70 8 my %valid_args = (
71 'AddrType' => [qw(id name nameseq)], # 'id', 'name', or 'nameseq'
72 'Scope' => [qw( name nameseq)], # TIPC_*_SCOPE, for binding, how far to advertise a name
73 'Ref' => [qw(id )], # <a.b.c:D>
74 'Id' => [qw(id )], # <A.B.C> (string or uint32) or <A.B.C:D> (string)
75 'Zone' => [qw(id )], # <A.b.c:d>
76 'Cluster' => [qw(id )], # <a.B.c:d>
77 'Node' => [qw(id )], # <a.b.C:d>
78 'Name' => [qw( name )], # {A,B} (string)
79 'Type' => [qw( name nameseq)], # {A,b} or {A,b,c}
80 'Instance' => [qw( name )], # {a,B}
81 'Domain' => [qw( name )], # tipc_addr, connect/sendto, how far to search for a name
82 'Lower' => [qw( nameseq)], # {a,B,c}
83 'Upper' => [qw( nameseq)], # {a,b,C}
84 'Nameseq' => [qw( nameseq)], # {A,B,C} (string)
85 );
86
87 sub validate_args_for_address_type {
88 5 ▶ my $args = shift;
Subroutine validate_args_for_address_type: called CC=4 SCAR=14.0
Pod undocumented
89 5 my $addrtype = $$args{AddrType};
90 # Validate hash-key arguments for this address type
91 5 foreach my $key (sort keys %$args) {
92 17 my $ref = $valid_args{$key};
93 17 ▶ die "got here ($key)" unless defined $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ref 0 17 17
94 17 my %valid = map { $_ => 1 } (@$ref);
95 croak("argument $key not valid for AddrType $addrtype")
96 17 ▶ unless exists($valid{$addrtype});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $valid{$addrtype} 0 17 17
97 }
98 5 return 1;
99 }
100  
101 sub fixup_hash_names {
102 5 ▶ my $args = shift;
Subroutine fixup_hash_names: called CC=6 SCAR=30.2
Pod undocumented
103 # Validate hash-key arguments to IO::Socket::TIPC::Sockaddr->new()
104 5 foreach my $key (sort keys %$args) {
105 16 ▶ if(!exists($valid_args{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $valid_args{$key}) 0 16 16
106 # This key needs to be fixed up. Search for it.
107 0 my $lckey = lc($key);
108 0 my $fixed = 0;
109 0 foreach my $goodkey (sort keys %valid_args) {
110 0 ▶ if($lckey eq lc($goodkey)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lckey eq lc $goodkey) 0 0 0
111 # Found it. Fix it up.
112 0 $$args{$goodkey} = $$args{$key};
113 0 delete($$args{$key});
114 0 $fixed = 1;
115 0 last;
116 }
117 }
118 0 ▶ croak("unknown argument $key")
Branch: 1 branch
BranchTrueFalseTotal
unless $fixed 0 0 0
119 unless $fixed;
120 }
121 }
122 5 return 1;
123 }
124  
125 sub string_parsing_stuff {
126 5 ▶ my $args = shift;
Subroutine string_parsing_stuff: called CC=11 SCAR=41.9
Pod undocumented
127 5 my %details;
128 5 ▶ if(exists($$args{Id})) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $args->{'Id'}) { } 0 5 5
elsif (exists $args->{'Name'}) { } 0 5 5
elsif (exists $args->{'Nameseq'}) { } 0 5 5
129 # just in case the user did Id => '<1.2.3>', Ref => 4, pass in the Ref
130 0 ▶ $details{Ref} = $$args{Ref} if exists $$args{Ref};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"Ref"} 0 0 0
131 0 ▶ return undef unless tipc_parse_string(\%details,$$args{Id});
Branch: 1 branch
BranchTrueFalseTotal
unless tipc_parse_string(\%details, $args->{"Id"}) 0 0 0
132 0 $$args{Zone} = $details{Zone};
133 0 $$args{Cluster} = $details{Cluster};
134 0 $$args{Node} = $details{Node};
135 0 ▶ $$args{Ref} = $details{Ref} if exists($details{Ref});
Branch: 1 branch
BranchTrueFalseTotal
if exists $details{"Ref"} 0 0 0
136 } elsif(exists($$args{Name})) {
137 0 ▶ return undef unless tipc_parse_string(\%details,$$args{Name});
Branch: 1 branch
BranchTrueFalseTotal
unless tipc_parse_string(\%details, $args->{"Name"}) 0 0 0
138 0 $$args{Type} = $details{Type};
139 0 $$args{Instance} = $details{Instance};
140 } elsif(exists($$args{Nameseq})) {
141 0 ▶ return undef unless tipc_parse_string(\%details,$$args{Nameseq});
Branch: 1 branch
BranchTrueFalseTotal
unless tipc_parse_string(\%details, $args->{"Nameseq"}) 0 0 0
142 0 $$args{Type} = $details{Type};
143 0 $$args{Lower} = $details{Lower};
144 0 $$args{Upper} = $details{Upper};
145 }
146 5 ▶ if(exists($details{AddrType})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $details{"AddrType"}) 0 5 5
147 0 ▶ $$args{AddrType} = $details{AddrType} unless exists $$args{AddrType};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"AddrType"} 0 0 0
148 }
149 5 return 1;
150 }
151  
152 8 my %addr_prereqs = (
153 'id' => [qw(Zone Cluster Node Ref)],
154 'name' => [qw(Scope Type Instance)],
155 'nameseq' => [qw(Scope Type Lower Upper)],
156 );
157  
158 sub check_prereqs_for_address_type {
159 4 ▶ my $args = shift;
Subroutine check_prereqs_for_address_type: called CC=4 SCAR=14.1
Pod undocumented
160 4 my $addrtype = $$args{AddrType};
161 4 my $ref = $addr_prereqs{$addrtype};
162 4 ▶ croak "got here ($addrtype)" unless defined $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ref 0 4 4
163 4 foreach my $key (@$ref) {
164 croak "addrtype $addrtype requires a $key value"
165 12 ▶ unless exists($$args{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{$key} 0 12 12
166 }
167 4 1;
168 }
169  
170  
171  
172 =head1 CONSTRUCTOR
173  
174 ...->new ( "string", key=>value, key=>value... )
175 ...->new ( key=>value, key=>value... )
176 ...->new_from_data ( $binary_data )
177  
178 Creates an "IO::Socket::TIPC::Sockaddr" object, which is really just a
179 bunch of fluff to manage C "struct sockaddr_tipc" values easily.
180  
181 Use the B<new_from_data> constructor if you want to wrap this class
182 around some sockaddr_tipc data you obtained from somewhere else.
183 (for instance, from the B<getpeername> builtin.)
184  
185 Use the B<new>() constructor to create a new sockaddr object. It
186 optionally takes a string as its first argument. Any other arguments
187 are in the form of Key => Value pairs.
188  
189 =head2 Initial String Argument (optional)
190  
191 You can pass any type of TIPC address as a string, to fill in most of
192 the below values for you. This is a very useful way to save lots of
193 typing, and keeps it more readable. Here is a list of possible string
194 arguments, and their hash-parameter equivalents:
195  
196 "<1.2.3:4>" is equivalent to:
197 AddrType => TIPC_ADDR_ID,
198 Zone => 1,
199 Cluster => 2,
200 Node => 3,
201 Ref => 4
202  
203 "{1, 2}" is equivalent to:
204 AddrType => TIPC_ADDR_NAME,
205 Type => 1,
206 Instance => 2
207  
208 "{1, 2, 3}" is equivalent to:
209 AddrType => TIPC_ADDR_NAMESEQ,
210 Type => 1,
211 Lower => 2,
212 Upper => 3
213  
214 Of course, noone B<has> to spell the fields out in such excruciating
215 detail (you can pass the same strings in I<Id>/I<Name>/I<Nameseq>
216 parameters), but it illustrates my point nicely.
217  
218 The string does not define everything useful about the address...
219 consider specifying the I<Scope> parameter for arguments to B<bind>,
220 and the I<Domain> parameter for I<name>s you plan to B<connect> to.
221  
222  
223 =head2 AddrType
224  
225 This tells Sockaddr whether to create an I<id>, I<name> or I<nameseq>
226 address. The default is guessed from the other arguments it was
227 given; pass the I<AddrType> argument to make it explicit. In
228 practice, this is rarely (never?) needed.
229  
230 If the right constants were imported, you can pass the following
231 arguments: I<TIPC_ADDR_ID>, I<TIPC_ADDR_NAME>, I<TIPC_ADDR_NAMESEQ>,
232 or I<TIPC_ADDR_MCAST> (which is an alias for I<TIPC_ADDR_NAMESEQ>).
233 Otherwise, you can just say I<"id">, I<"name"> or I<"nameseq">, these
234 will work equally well.
235  
236  
237 =head2 Scope
238  
239 Valid for I<name> and I<nameseq> addresses. Specifies how loudly to
240 advertise the name/nameseq, to the rest of the network. The default
241 is I<TIPC_NODE_SCOPE>.
242  
243 If the right constants were imported, you can pass the following
244 arguments: I<TIPC_ZONE_SCOPE>, I<TIPC_CLUSTER_SCOPE>, or
245 I<TIPC_NODE_SCOPE>. Otherwise, you can just say I<"zone">,
246 I<"cluster"> or I<"node">, which will work equally well.
247  
248  
249 =head2 Id
250  
251 Defines an I<id> address. An I<id> address has the format
252 "<Zone.Cluster.Node:Ref>". With the I<Id> parameter, you can specify
253 the "<Zone.Cluster.Node>" portion of that address, either with a
254 string (like "<1.2.3>") or as an unsigned 32-bit integer.
255 Alternately, you can define the whole thing, Ref included, as a
256 string (like "<1.2.3:4>"). This is a useful way to avoid having to
257 specify the I<Ref>, I<Zone>, I<Cluster>, and I<Node> parameters
258 individually.
259  
260  
261 =head2 Ref
262  
263 Valid for I<id> addresses. This 32-bit field is usually assigned
264 randomly by the operating system, and only needs to be set when you
265 are attempting to connect to someone else.
266  
267  
268 =head2 Zone
269  
270 Valid for I<id> addresses. This 8-bit field defines the I<Zone>
271 portion of the Id address. See the I<Id> parameter.
272  
273  
274 =head2 Cluster
275  
276 Valid for I<id> addresses. This 12-bit field defines the
277 I<Cluster> portion of the Id address. See the I<Id> parameter.
278  
279  
280 =head2 Node
281  
282 Valid for I<id> addresses. This 12-bit field defines the I<Node>
283 portion of the Id address. See the I<Id> parameter.
284  
285  
286 =head2 Name
287  
288 Defines a I<name> address. A I<name> address comprises two fields,
289 I<Type> and I<Instance>, 32 bits each. It has the format
290 "{Name, Instance}". Name addresses also have a I<Domain> flag,
291 which is used in B<connect>ing, to specify where to start looking
292 for the server.
293  
294 The I<Name> parameter is useful for defining a name address all in
295 one go (minus the I<Domain>). Pass it a string, like "{1, 2}",
296 to avoid having to specify the I<Type> and I<Instance> parameters
297 individually.
298  
299  
300 =head2 Type
301  
302 Required for I<name> and I<nameseq> addresses. This 32-bit field
303 defines the I<Type> portion of the address.
304  
305  
306 =head2 Instance
307  
308 Required for I<name> addresses. This 32-bit field defines the
309 I<Instance> portion of the address.
310  
311  
312 =head2 Domain
313  
314 Valid for I<name> addresses. This 32-bit field defines the starting
315 point, when searching for a server by name. You can pass it an
316 integer, or a TIPC address string, of the form "<1.2.3>".
317  
318  
319 =head2 Nameseq
320  
321 Defines a I<nameseq> address. A I<nameseq> address comprises three
322 fields, I<Type>, I<Lower> and I<Upper>, 32 bits each. The I<Lower>
323 and I<Upper> attributes define a range of I<Instance> values (see
324 I<Name>).
325  
326 I<nameseq> addresses have the format "{Type, Lower, Upper}".
327  
328 The I<Nameseq> parameter is useful for defining a nameseq address
329 all in one go. Pass it a string, like "{1, 2, 3}", to avoid having
330 to specify the I<Type>, I<Lower> and I<Upper> parameters
331 individually.
332  
333  
334 =head2 Lower
335  
336 Required for I<nameseq> addresses. This 32-bit field defines the
337 lower end of an I<Instance> range. If unspecified, it defaults to
338 I<Upper>, resulting in a "range" of 1.
339  
340  
341 =head2 Upper
342  
343 Required for I<nameseq> addresses. This 32-bit field defines the
344 upper end of an I<Instance> range. If unspecified, it defaults to
345 I<Lower>, resulting in a "range" of 1.
346  
347 =cut
348  
349 sub new {
350 5 ▶ my $package = shift;
Subroutine new: called CC=21 SCAR=35.9
Pod undocumented
351 5 my %args = ();
352 5 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 5 0 5
353 5 ▶ if(scalar @_ & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ & 1) 2 3 5
354 2 ▶ return undef unless tipc_parse_string(\%args, shift);
Branch: 1 branch
BranchTrueFalseTotal
unless tipc_parse_string(\%args, shift()) 0 2 2
355 }
356 5 %args = (%args, @_);
357 }
358 # sanity-check input, correct capitalization, make sure all keys are valid
359 5 ▶ return undef unless fixup_hash_names(\%args);
Branch: 1 branch
BranchTrueFalseTotal
unless fixup_hash_names(\%args) 0 5 5
360 # handle things like Id => '<1.2.3:4>'
361 5 ▶ return undef unless string_parsing_stuff(\%args);
Branch: 1 branch
BranchTrueFalseTotal
unless string_parsing_stuff(\%args) 0 5 5
362 5 ▶ unless(exists($args{AddrType})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"AddrType"}) 1 4 5
363 1 ▶ return undef unless divine_address_type(\%args);
Branch: 1 branch
BranchTrueFalseTotal
unless divine_address_type(\%args) 0 1 1
364 }
365 # check that we don't have any extra values. (like Name, for an "id" addr)
366 5 ▶ return undef unless validate_args_for_address_type(\%args);
Branch: 1 branch
BranchTrueFalseTotal
unless validate_args_for_address_type(\%args) 0 5 5
367 # fill in some optional stuff
368 5 ▶ if($args{AddrType} eq 'name') {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"AddrType"} eq "name") 5 0 5
369 5 ▶ if(exists($args{Domain})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'Domain'}) { } 1 4 5
370 1 ▶ unless(looks_like_number($args{Domain})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($args{"Domain"})) 0 1 1
371 0 my $href = {};
372 0 tipc_parse_string($href,$args{Domain});
373 croak "Domain string should be an id!"
374 0 ▶ unless $$href{AddrType} eq 'id';
Branch: 1 branch
BranchTrueFalseTotal
unless $href->{"AddrType"} eq "id" 0 0 0
375 0 $args{Domain} = tipc_addr(@$href{'Zone','Cluster','Node'});
376 }
377 } else {
378 4 $args{Domain} = 0;
379 }
380 }
381 5 ▶ if(exists($args{Scope})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'Scope'}) { } 1 4 5
382 1 my $scope = $args{Scope};
383 1 my %valid_scopes = (
384 IO::Socket::TIPC::TIPC_ZONE_SCOPE() => 1,
385 IO::Socket::TIPC::TIPC_CLUSTER_SCOPE() => 1,
386 IO::Socket::TIPC::TIPC_NODE_SCOPE() => 1,
387 );
388 1 my %scope_values = (
389 zone => IO::Socket::TIPC::TIPC_ZONE_SCOPE(),
390 cluster => IO::Socket::TIPC::TIPC_CLUSTER_SCOPE(),
391 node => IO::Socket::TIPC::TIPC_NODE_SCOPE(),
392 );
393 1 ▶ unless(exists($valid_scopes{$scope})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $valid_scopes{$scope}) 1 0 1
394 $args{Scope} = $scope_values{lc($scope)}
395 1 ▶ if exists $scope_values{lc($scope)};
Branch: 1 branch
BranchTrueFalseTotal
if exists $scope_values{lc $scope} 0 1 1
396 }
397 1 $scope = $args{Scope};
398 croak("invalid Scope $scope")
399 1 ▶ unless exists $valid_scopes{$scope};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $valid_scopes{$scope} 1 0 1
400 } else {
401 4 $args{Scope} = IO::Socket::TIPC::TIPC_NODE_SCOPE();
402 }
403  
404 # check that we do have the arguments we need.
405 4 ▶ return undef unless check_prereqs_for_address_type(\%args);
Branch: 1 branch
BranchTrueFalseTotal
unless check_prereqs_for_address_type(\%args) 0 4 4
406 4 my $sockaddr = _tipc_create();
407 4 _tipc_fill_common($sockaddr, $args{Scope});
408 4 ▶ if($args{AddrType} eq 'id') {
Branch: 3 branches
BranchTrueFalseTotal
if ($args{'AddrType'} eq 'id') { } 0 4 4
elsif ($args{'AddrType'} eq 'name') { } 4 0 4
elsif ($args{'AddrType'} eq 'nameseq') { } 0 0 0
409 0 _tipc_fill_id_pieces($sockaddr, @args{"Ref","Zone","Cluster","Node"});
410 } elsif($args{AddrType} eq 'name') {
411 4 _tipc_fill_name($sockaddr, @args{"Type","Instance","Domain"});
412 } elsif($args{AddrType} eq 'nameseq') {
413 0 _tipc_fill_nameseq($sockaddr, @args{"Type","Lower","Upper"});
414 } else {
415 0 croak("invalid AddrType $args{AddrType}");
416 }
417 4 return $sockaddr;
418 }
419  
420 sub new_from_data {
421 0 ▶ my ($package, $data) = @_;
Subroutine new_from_data: not called CC=1 SCAR=6.9
Pod undocumented
422 0 get_family(\$data); # this calls _sanity_check
423 0 return bless(\$data, $package);
424 }
425  
426  
427 =head1 METHODS
428  
429 =head2 stringify()
430  
431 B<stringify> returns a string representing the sockaddr. These
432 strings are the same as the ones used in the TIPC documentation,
433 see I<Programmers_Guide.txt> (linked to in B<REFERENCES>). Depending
434 on the address type, it will return something that looks like one of:
435  
436 "<1.2.3:4>" # ID, addr = 1.2.3, ref = 4
437 "{4242, 100}" # NAME, type = 4242, instance = 100
438 "{4242, 100, 101}" # NAMESEQ, type = 4242, range 100-101
439  
440 Note that these strings are intended for use as shorthand, with
441 someone familiar with TIPC. They do not include all the fields of
442 the sockaddr structure, and sometimes the hidden fields are important.
443 In particular, they are missing the I<Scope> and I<Domain> fields,
444 which affect how far away binding/connecting may occur for I<name>s and
445 I<nameseq>s. If you need to store an address for reuse, you are better
446 off reusing the Sockaddr object itself, rather than storing one of
447 these strings.
448  
449 =head2 get/set routines
450  
451 The C structure looks like this (minor edits for clarity):
452  
453 struct sockaddr_tipc {
454 unsigned short family;
455 unsigned char addrtype;
456 signed char scope;
457 union {
458 struct {
459 __u32 ref;
460 __u32 node;
461 } id;
462 struct {
463 __u32 type;
464 __u32 lower;
465 __u32 upper;
466 } nameseq;
467 struct {
468 struct {
469 __u32 type;
470 __u32 instance;
471 } name;
472 __u32 domain;
473 } name;
474 } addr;
475 };
476  
477 Each of these fields has methods to get and set it. The only
478 exception is "family", which is always set to I<AF_TIPC>, and
479 has very good reasons for being read-only.
480  
481 An exhaustive list of these methods follows. All functions return
482 integers, "val" means an unsigned integer argument, "<1.2.3>" means a
483 string-address argument (obviously).
484  
485 =over
486  
487 =item global stuff
488  
489 get_family()
490 get_addrtype() set_addrtype(val)
491 get_scope() set_scope(val)
492  
493 =item TIPC_ADDR_ID stuff
494  
495 get_ref() set_ref(val)
496 get_id() set_id(val) or set_id("<1.2.3>")
497 get_zone() set_zone(val)
498 get_cluster() set_cluster(val)
499 get_node() set_node(val)
500  
501 NOTE: for id-style addresses, direct access to the address as a whole (id) is
502 allowed, as well as its constituent components (zone, cluster, and node).
503 This may cause confusion, since the whole address is called "node" in the C
504 structure, but "node" refers to only a portion of the address here.
505  
506  
507 =item TIPC_ADDR_NAME stuff
508  
509 get_ntype() set_ntype(val)
510 get_instance() set_instance(val)
511 get_domain() set_domain(val) or set_domain("<1.2.3>")
512  
513 =item TIPC_ADDR_NAMESEQ stuff
514  
515 get_stype() set_stype(val)
516 get_lower() set_lower(val)
517 get_upper() set_upper(val)
518  
519 =item Type helpers
520  
521 get_type() set_type(arg)
522  
523 The B<get_type>/B<set_type> functions call either B<get_ntype>/B<set_ntype>,
524 or B<get_stype>/B<set_stype>, depending on whether the I<addrtype> is I<name>
525 or I<nameseq>.
526  
527 =back
528  
529 =cut
530  
531 # NOTE: Most of the above accessor calls go straight to XS code. The
532 # following subroutines are wrappers, to handle cases where I want to
533 # parse a string or something before it goes down to the XS layer.
534  
535 # wrap set_domain: accept string-address arguments
536 sub set_domain {
537 0 ▶ my ($self, $addr) = @_;
Subroutine set_domain: not called CC=3 SCAR=24.8
Pod undocumented
538 0 ▶ unless(looks_like_number($addr)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($addr)) 0 0 0
539 0 my $components = {};
540 0 tipc_parse_string($components, $addr);
541 croak "'domain' is an address field."
542 0 ▶ unless $$components{AddrType} eq 'id';
Branch: 1 branch
BranchTrueFalseTotal
unless $components->{"AddrType"} eq "id" 0 0 0
543 0 $addr = tipc_addr(@$components{'Zone', 'Cluster', 'Node'});
544 }
545 0 return $self->_tipc_set_domain($addr);
546 }
547  
548 # wrap set_id: accept string-address arguments
549 sub set_id {
550 0 ▶ my ($self, $addr) = @_;
Subroutine set_id: not called CC=3 SCAR=24.8
Pod undocumented
551 0 ▶ unless(looks_like_number($addr)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($addr)) 0 0 0
552 0 my $components = {};
553 0 tipc_parse_string($components, $addr);
554 croak "'id' is an address field."
555 0 ▶ unless $$components{AddrType} eq 'id';
Branch: 1 branch
BranchTrueFalseTotal
unless $components->{"AddrType"} eq "id" 0 0 0
556 0 $addr = tipc_addr(@$components{'Zone', 'Cluster', 'Node'});
557 }
558 0 return $self->_tipc_set_id($addr);
559 }
560  
561  
562 =head1 SUBROUTINES (non-methods)
563  
564 =head2 tipc_zone(int)
565  
566 Unpacks the Zone from a TIPC address (integer). You can also pass it a string
567 address, like "<1.2.3>". Returns the zone as an integer. Example below.
568  
569 =cut
570  
571 sub tipc_zone {
572 0 ▶ my ($addr) = @_;
Subroutine tipc_zone: not called CC=3 SCAR=24.8
Pod documented
573 0 ▶ unless(looks_like_number($addr)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($addr)) 0 0 0
574 0 my $components = {};
575 0 tipc_parse_string($components, $addr);
576 croak "'zone' is an 'id' address field."
577 0 ▶ unless $$components{AddrType} eq 'id';
Branch: 1 branch
BranchTrueFalseTotal
unless $components->{"AddrType"} eq "id" 0 0 0
578 0 $addr = tipc_addr(@$components{'Zone', 'Cluster', 'Node'});
579 }
580 0 return _tipc_zone($addr);
581 }
582  
583 =head2 tipc_cluster(int)
584  
585 Unpacks the Cluster from a TIPC address (integer). You can also pass it a
586 string address, like "<1.2.3>". Returns the cluster as an integer.
587  
588 my $zone = tipc_zone(0x01002003); # $zone is now set to 1
589 my $cluster = tipc_zone(0x01002003); # $cluster is now set to 2
590 my $node = tipc_zone(0x01002003); # $node is now set to 3
591 printf("<%i.%i.%i>\n",
592 $zone, $cluster, $node); # prints <1.2.3>
593  
594 =cut
595  
596 sub tipc_cluster {
597 0 ▶ my ($addr) = @_;
Subroutine tipc_cluster: not called CC=3 SCAR=24.8
Pod documented
598 0 ▶ unless(looks_like_number($addr)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($addr)) 0 0 0
599 0 my $components = {};
600 0 tipc_parse_string($components, $addr);
601 croak "'cluster' is an 'id' address field."
602 0 ▶ unless $$components{AddrType} eq 'id';
Branch: 1 branch
BranchTrueFalseTotal
unless $components->{"AddrType"} eq "id" 0 0 0
603 0 $addr = tipc_addr(@$components{'Zone', 'Cluster', 'Node'});
604 }
605 0 return _tipc_cluster($addr);
606 }
607  
608 =head2 tipc_node(int)
609  
610 Unpacks the Node from a TIPC address (integer). You can also pass it a string
611 address, like "<1.2.3>". Returns the node as an integer. Example above.
612  
613 =cut
614  
615 sub tipc_node {
616 0 ▶ my ($addr) = @_;
Subroutine tipc_node: not called CC=3 SCAR=24.8
Pod documented
617 0 ▶ unless(looks_like_number($addr)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($addr)) 0 0 0
618 0 my $components = {};
619 0 tipc_parse_string($components, $addr);
620 croak "'node' is an 'id' address field."
621 0 ▶ unless $$components{AddrType} eq 'id';
Branch: 1 branch
BranchTrueFalseTotal
unless $components->{"AddrType"} eq "id" 0 0 0
622 0 $addr = tipc_addr(@$components{'Zone', 'Cluster', 'Node'});
623 }
624 0 return _tipc_node($addr);
625 }
626  
627  
628 =head2 tipc_addr(int)
629  
630 Packs a zone, cluster and node into a tipc address. You can also pass it
631 a "<1.2.3>" string address.
632  
633 my $addr = tipc_addr($zone, $cluster, $node);
634 printf("0x%x\n", $addr); # prints 0x01002003
635  
636 =cut
637  
638 sub tipc_addr {
639 0 ▶ my ($zone, $cluster, $node) = @_;
Subroutine tipc_addr: not called CC=3 SCAR=24.8
Pod documented
640 0 ▶ unless(looks_like_number($zone)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($zone)) 0 0 0
641 0 my $addr = $zone;
642 0 my $components = {};
643 0 tipc_parse_string($components, $addr);
644 croak "this is not an 'id' address."
645 0 ▶ unless $$components{AddrType} eq 'id';
Branch: 1 branch
BranchTrueFalseTotal
unless $components->{"AddrType"} eq "id" 0 0 0
646 0 return _tipc_addr(@$components{'Zone', 'Cluster', 'Node'});
647 }
648 0 return _tipc_addr($zone, $cluster, $node);
649 }
650  
651  
652 =head2 tipc_parse_string(hashref, string)
653  
654 Given a string that looks like "<1.2.3:4>", "<1.2.3>", "{1, 2}", or
655 "{1, 2, 3}", chop it into its components. Puts the components into
656 appropriately named keys in hashref, like I<Zone>, I<Cluster>,
657 I<Node>, I<Ref>, I<Type>, I<Instance>, I<Upper>, I<Lower>. It also
658 guesses the I<AddrType> of the string you passed. Returns 1 on
659 success, croaks on error.
660  
661 my $href = {};
662 tipc_parse_string($href, "<1.2.3:4>");
663 printf("Address <%i.%i.%i:%i> is of type %s\n",
664 @$href{"Zone", "Cluster", "Node", "Ref", "AddrType"});
665 # prints "Address <1.2.3:4> is of type id\n"
666  
667 This is a function which B<new>() uses internally, to turn user
668 provided garbage into some values it can actually use. There is
669 no need to call it directly, unless you want to use the same parser
670 for some other reason, like input checking.
671  
672 =cut
673  
674 sub tipc_parse_string {
675 2 ▶ my ($args, $string) = @_;
Subroutine tipc_parse_string: called CC=9 SCAR=33.9
Pod documented
676 # we got a string. we accept the following types of string:
677 # ID: '<a.b.c>' (REF=0)
678 # ID (dec): '12345' (REF=0)
679 # ID (hex): '0x01002003' (REF=0)
680 # ID+REF: '<a.b.c:d>'
681 # NAME: '{a,b}'
682 # NAMESEQ: '{a,b,c}'
683 2 my $valid = 0;
684 # handle string ID+REF or string ID
685 2 ▶ if($string =~ /^<(\d+)\.(\d+)\.(\d+)(:(\d+))?>$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^<(\d+)\.(\d+)\.(\d+)(:(\d+))?>$/) 0 2 2
686 0 $$args{AddrType} = 'id';
687 0 $$args{Zone} = $1;
688 0 $$args{Cluster} = $2;
689 0 $$args{Node} = $3;
690 0 ▶ $$args{Ref} = $5 if defined $5;
Branch: 1 branch
BranchTrueFalseTotal
if defined $5 0 0 0
691 0 ▶ $$args{Ref} = 0 unless defined $$args{Ref};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"Ref"} 0 0 0
692 0 $valid = 1;
693 }
694 # handle decimal ID
695 2 ▶ if($string =~ /^(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^(\d+)$/) 0 2 2
696 0 $$args{Zone} = tipc_zone($1);
697 0 $$args{Cluster} = tipc_cluster($1);
698 0 $$args{Node} = tipc_node($1);
699 0 printf(STDERR "dec: <%i.%i.%i>\n",@$args{'Zone','Cluster','Node'});
700 0 $$args{AddrType} = 'id';
701 0 $valid = 1;
702 }
703 # handle hex ID
704 2 ▶ if($string =~ /^0x([0-9a-fA-F]{1,8})$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^0x([0-9a-fA-F]{1,8})$/) 0 2 2
705 0 $$args{Zone} = tipc_zone(hex($1));
706 0 $$args{Cluster} = tipc_cluster(hex($1));
707 0 $$args{Node} = tipc_node(hex($1));
708 0 $$args{AddrType} = 'id';
709 0 $valid = 1;
710 }
711
712 # handle string NAME
713 2 ▶ if($string =~ /^\{(\d+),\s*(\d+)\}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^\{(\d+),\s*(\d+)\}$/) 2 0 2
714 2 $$args{AddrType} = 'name';
715 2 $$args{Type} = $1;
716 2 $$args{Instance} = $2;
717 2 $valid = 1;
718 }
719 # handle string NAMESEQ
720 2 ▶ if($string =~ /^\{(\d+),\s*(\d+),\s*(\d+)\}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^\{(\d+),\s*(\d+),\s*(\d+)\}$/) 0 2 2
721 0 $$args{AddrType} = 'nameseq';
722 0 $$args{Type} = $1;
723 0 $$args{Lower} = $2;
724 0 $$args{Upper} = $3;
725 0 $valid = 1;
726 }
727 2 ▶ croak("string argument '$string' is not a valid TIPC address.")
Branch: 1 branch
BranchTrueFalseTotal
unless $valid 0 2 2
728 unless($valid);
729 2 return 1;
730 }
731  
732 =head1 EXPORT
733  
734 None by default.
735  
736 =head2 Exportable subroutines
737  
738 tipc_addr
739 tipc_zone
740 tipc_cluster
741 tipc_node
742 tipc_parse_string
743  
744 =cut
745  
746 8 our @EXPORT = qw();
747 8 our @EXPORT_OK = qw();
748  
749 8 our %EXPORT_TAGS = (
750 'all' => [ qw(
751 tipc_addr tipc_zone tipc_cluster tipc_node tipc_parse_string
752 ) ]
753 );
754 8 Exporter::export_ok_tags('all');
755  
756 8 1;
757 __END__
« blib/lib/IO/Socket/TIPC.pm ↑ Summary tipc.h »