File Coverage

blib/lib/IO/Socket/TIPC.pm
Criterion Covered Total %
statement 41 149 27.5
branch 5 98 5.1
condition 0 27 0.0
subroutine 12 24 50.0
pod 8 13 61.5
total 66 311 21.2


line stmt bran cond sub pod time code
1             package IO::Socket::TIPC;
2 8     8   35886 use IO::Socket::TIPC::Sockaddr ':all';
  8         28  
  8         1934  
3 8     8   75 use strict;
  8         75  
  8         550  
4 8     8   50 use Carp;
  8         12  
  8         510  
5 8     8   11399 use IO::Socket;
  8         278933  
  8         98  
6 8     8   5995 use Scalar::Util qw(looks_like_number);
  8         15  
  8         431  
7 8     8   8270 use AutoLoader;
  8         13613  
  8         52  
8 8     8   303 use Exporter;
  8         17  
  8         1600  
9              
10             our @ISA = qw(Exporter IO::Socket);
11              
12             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 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, I,
39             I and I. 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 and I are connection-based sockets.
44             These sockets are strictly client/server. For servers, B() will
45             call B() for you, to bind to a I* name, and you then
46             B() connections from clients, each of which get their own
47             socket (returned from B). For clients, B() will call
48             B() for you, to connect to the specified I* 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 over I.
51              
52             See the B section, for an example of connection-based socket
53             use.
54              
55             I and I 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() to create the
59             socket, you use B and B 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 and I) are often
64             bind()ed to a particular I or I address, in order to
65             allow them to listen for packets sent to a well-known destination
66             (the I). You can use I or I parameters
67             to B(), to select a name or name-sequence to bind to. As above,
68             these parameters internally become I and I arguments to
69             IO::Socket::TIPC::Sockaddr->B(), and the result is passed to
70             B(). This is very similar to typical uses of I over
71             I.
72              
73             Since connectionless sockets are not linked to a particular peer, you
74             can use B to send a packet to some peer with a given Name in
75             the network, and B to receive replies from a peer in the
76             network who sends a packet to your I (or I). You can
77             also use I addressses to send multicast packets to *every*
78             peer with a given name. Please see the I
79             document (linked in B) for more details.
80              
81             See the B 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 54     54   31411 my $constname;
92 54         69 our $AUTOLOAD;
93 54         278 ($constname = $AUTOLOAD) =~ s/.*:://;
94 54 50       414 croak "&IO::Socket::TIPC::constant not defined" if $constname eq 'constant';
95 54         263 my ($error, $val) = constant($constname);
96 54 100       120 if ($error) { $val = undef; } # undefined constants just return undef.
  3         7  
97             {
98 8     8   51 no strict 'refs';
  8         21  
  8         16061  
  54         88  
99 54     129   473 *$AUTOLOAD = sub { $val };
  129         32714  
100             }
101 54         220 goto &$AUTOLOAD;
102             }
103              
104             =head1 CONSTRUCTOR
105              
106             B 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, and
110             shares some things in common with that class. Specifically, the
111             I parameter, the I* and Il* nomenclature, and the
112             behind-the-scenes calls to B(), B(), B(),
113             B(), and so on.
114              
115             Connection-based sockets (I and I) come
116             in "server" and "client" varieties. To create a server socket,
117             specify the I argument to B(). You can bind a
118             name to the socket, thus making your server easier to find, by
119             providing one or more I* parameters. To create a client
120             socket, do B provide the I argument. Instead, provide
121             one or more I* parameters, and B will call B()
122             for you.
123              
124             All I* parameters are passed directly to
125             IO::Socket::TIPC::Sockaddr->B(), minus the "Local" prefix, and the
126             resulting sockaddr is passed to B(). Similarly, all I*
127             parameters are passed directly to IO::Socket::TIPC::Sockaddr->B(),
128             minus the "Peer" prefix, and the result is passed to B(). The
129             keywords I and I themselves become the first string
130             parameter to IO::Socket::TIPC::Sockaddr->B(); 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. It tells the system what type of socket
138             to use. The following constants will work, if they were imported:
139             I, I, I, or I.
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 Bs. Its
146             existence specifies that this is a server socket. It is common to
147             also specify some I* arguments, so B() can B 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 option, from B. If you provide this
155             field, ->B() will call B for you to set the value.
156              
157             Default is I. 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 (linked in B) for details.
165              
166              
167             =head2 ConnectTimeout
168              
169             This field specifies the B() timeout, in milliseconds. If
170             you provide this field, ->B() will call B to set
171             the I value on your socket.
172              
173             See I (linked in B) for details.
174              
175             B: I should not be confused with I,
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* parameter(s) determine which address your socket will get
184             B()ed to.
185              
186             Any arguments prefixed with "Local" will be passed to
187             IO::Socket::TIPC::Sockaddr->B(), with the "Local" prefix
188             removed. If you specify the word I, itself, the argument
189             will be passed as the first string parameter to
190             IO::Socket::TIPC::Sockaddr->B(); all other I* 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 section to see what this stuff looks like.
194              
195             =head2 Peer*
196              
197             This field is only valid for B (as opposed to B)
198             using connection-type sockets, and is required for this case. The
199             I* parameter(s) determine which address your socket will get
200             B()ed to.
201              
202             Any arguments prefixed with "Peer" will be passed to
203             IO::Socket::TIPC::Sockaddr->B(), with the "Peer" prefix
204             removed. If you specify the word I, itself, the argument
205             will be passed as the first string parameter to
206             IO::Socket::TIPC::Sockaddr->B(); all other I* 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 section to see what this stuff looks like.
210              
211             =cut
212              
213             sub new {
214             # pass it down to IO::Socket
215 0     0 1 0 my $class = shift;
216 0         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     0 0 0 my($socket, $args) = @_;
222 0         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         0 foreach my $key (sort keys %$args) {
227 0 0       0 if($key =~ /^local/i) {
228 0         0 my $newkey = substr($key,5);
229 0 0       0 if(length($newkey)) {
230 0         0 $local{$newkey} = $$args{$key};
231             } else {
232 0         0 $local = $$args{$key};
233             }
234 0         0 delete($$args{$key});
235             }
236 0 0       0 if($key =~ /^peer/i) {
237 0         0 my $newkey = substr($key,4);
238 0 0       0 if(length($newkey)) {
239 0         0 $peer{$newkey} = $$args{$key};
240             } else {
241 0         0 $peer = $$args{$key};
242             }
243 0         0 delete($$args{$key});
244             }
245             }
246 0 0       0 return undef unless fixup_args($args);
247 0 0       0 return undef unless enforce_required_args($args);
248 0         0 my $connectionless = 0;
249 0         0 my $listener = 0;
250 0   0     0 my $connector = (scalar keys %peer) || (defined $peer);
251 0   0     0 my $binder = (scalar keys %local) || (defined $local);
252 0 0 0     0 $listener = 1 if(exists($$args{Listen}) && $$args{Listen});
253 0 0       0 unless(looks_like_number($$args{SocketType})) {
254 0         0 my %socket_types = (
255             stream => SOCK_STREAM,
256             seqpacket => SOCK_SEQPACKET,
257             rdm => SOCK_RDM,
258             dgram => SOCK_DGRAM,
259             );
260 0 0       0 if(exists($socket_types{lc($$args{SocketType})})) {
261 0         0 $$args{SocketType} = $socket_types{lc($$args{SocketType})};
262             } else {
263 0         0 croak "unknown SocketType $$args{SocketType}!";
264             }
265 0 0       0 $connectionless = 1 if $$args{SocketType} == SOCK_RDM;
266 0 0       0 $connectionless = 1 if $$args{SocketType} == SOCK_DGRAM;
267             }
268 0 0 0     0 croak "Connectionless socket types cannot listen(), but you've told me to Listen."
269             if($connectionless && $listener);
270 0 0 0     0 croak "Connectionless socket types cannot connect(), but you've given me a Peer address."
271             if($connectionless && $connector);
272 0 0 0     0 croak "Listener sockets cannot connect, but you've given me a Peer address."
273             if($listener && $connector);
274 0 0 0     0 croak "Connect()ing sockets cannot bind, but you've given me a Local address."
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 0       0 $socket->socket(PF_TIPC(), $$args{SocketType}, 0)
280             or croak "Could not create socket: $!";
281              
282             # setsockopt/fcntl stuff goes here.
283 0 0       0 if(exists $$args{ConnectTimeout}) {
284 0 0       0 $socket->setsockopt(SOL_TIPC(), TIPC_CONN_TIMEOUT(), $$args{ConnectTimeout})
285             or croak "TIPC_CONN_TIMEOUT: $!";
286             }
287 0 0       0 if(exists $$args{Importance}) {
288 0 0       0 $socket->setsockopt(SOL_TIPC(), TIPC_IMPORTANCE() , $$args{Importance})
289             or croak "TIPC_IMPORTANCE: $!";
290             }
291 0 0       0 if($binder) {
292 0         0 my $baddr;
293 0 0       0 if(defined($local)) {
294 0 0 0     0 if(ref($local) && ref($local) eq "IO::Socket::TIPC::Sockaddr") {
295 0         0 $baddr = $local;
296             } else {
297 0         0 $baddr = IO::Socket::TIPC::Sockaddr->new($local, %local);
298             }
299             } else {
300 0         0 $baddr = IO::Socket::TIPC::Sockaddr->new(%local);
301             }
302 0 0       0 $socket->bind($baddr)
303             or croak "Could not bind socket: $!";
304             }
305 0 0       0 if($connector) {
306 0         0 my $caddr;
307 0 0       0 if(defined($peer)) {
308 0 0 0     0 if(ref($peer) && ref($peer) eq "IO::Socket::TIPC::Sockaddr") {
309 0         0 $caddr = $peer;
310             } else {
311 0         0 $caddr = IO::Socket::TIPC::Sockaddr->new($peer, %peer);
312             }
313             } else {
314 0         0 $caddr = IO::Socket::TIPC::Sockaddr->new(%peer);
315             }
316 0 0       0 $socket->connect($caddr)
317             or croak "Could not connect socket: $!";
318             }
319 0 0       0 if($listener) {
320 0 0       0 $socket->listen()
321             or croak "Could not listen: $!";
322             }
323 0         0 return $socket;
324             }
325              
326             # a "0" denotes an optional value. a "1" is required.
327             my %valid_args = (
328             Listen => 0,
329             SocketType => 1,
330             Importance => 0,
331             ConnectTimeout => 0,
332             );
333              
334             sub enforce_required_args {
335 0     0 0 0 my $args = shift;
336 0         0 foreach my $key (sort keys %$args) {
337 0 0       0 if($valid_args{$key}) {
338             # argument is required.
339 0 0       0 unless(exists($$args{$key})) {
340             # argument not provided!
341 0         0 croak "argument $key is REQUIRED.";
342             }
343             }
344             }
345 0         0 return 1;
346             }
347              
348             sub fixup_args {
349 0     0 0 0 my $args = shift;
350             # Validate hash-key arguments to IO::Socket::TIPC->new()
351 0         0 foreach my $key (sort keys %$args) {
352 0 0       0 if(!exists($valid_args{$key})) {
353             # This key needs to be fixed up. Search for it.
354 0         0 my $lckey = lc($key);
355 0         0 my $fixed = 0;
356 0         0 foreach my $goodkey (sort keys %valid_args) {
357 0 0       0 if($lckey eq lc($goodkey)) {
358             # Found it. Fix it up.
359 0         0 $$args{$goodkey} = $$args{$key};
360 0         0 delete($$args{$key});
361 0         0 $fixed = 1;
362 0         0 last;
363             }
364             }
365 0 0       0 croak("unknown argument $key")
366             unless $fixed;
367             }
368             }
369 0         0 return 1;
370             }
371              
372              
373             =head1 METHODS
374              
375             =head2 sendto(addr, message [, flags])
376              
377             B 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, defaults to 0 when not specified. The
385             TIPC I says: "TIPC supports the I
386             flag when sending; all other flags are ignored."
387              
388             You may have noticed that B and the B builtin do more
389             or less the same thing with the order of arguments changed. The main
390             reason to use B is because you can pass it a
391             IO::Socket::TIPC::Sockaddr object directly, where B requires you
392             to dereference the blessed reference to get at the raw binary "struct
393             sockaddr_tipc" bits. So, B is just a matter of convenience.
394              
395             Ironically, this B method calls the B builtin, which in
396             turn calls the C B function.
397              
398             =cut
399              
400             sub sendto {
401 0     0 1 0 my ($self, $addr, $message, $flags) = @_;
402 0 0       0 croak "sendto given an undef message" unless defined $message;
403 0 0       0 croak "sendto given a non-address?"
404             unless ref($addr) eq "IO::Socket::TIPC::Sockaddr";
405 0 0       0 $flags = 0 unless defined $flags;
406 0         0 return $self->send($message, $flags, $$addr);
407             }
408              
409              
410             =head2 recvfrom(buffer [, length [, flags]])
411              
412             B 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, defaults to I
422             when not specified. The third parameter, I, defaults to 0 when
423             not specified.
424              
425             The TIPC I says: "TIPC supports the I
426             flag when receiving, as well as the I flag when receiving
427             on a I socket; all other flags are ignored."
428              
429             You may have noticed that B and the B builtin do
430             more or less the same thing with the order of arguments changed.
431             The main reason to use B is because it will return a
432             IO::Socket::TIPC::Sockaddr object, where B just returns a
433             binary blob containing the C "struct sockaddr_tipc" data, which, by
434             itself, cannot be inspected or modified. So, B is just a
435             matter of convenience.
436              
437             Ironically, this B method calls the B builtin, which
438             in turn calls the C B function.
439              
440             =cut
441              
442             sub recvfrom {
443             # note: the $buffer argument is written to by recv().
444 0     0 1 0 my ($self, $buffer, $length, $flags) = @_;
445 0 0       0 $flags = 0 unless defined $flags;
446 0 0       0 $length = TIPC_MAX_USER_MSG_SIZE() unless defined $length;
447 0 0       0 croak "how am I supposed to recvfrom() a packet of length 0?"
448             unless $length > 0;
449 0         0 my $rv = $self->recv($_[1], $length, $flags);
450 0         0 return IO::Socket::TIPC::Sockaddr->new_from_data($rv);
451             }
452              
453              
454             =head2 bind(addr)
455              
456             B 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 is useful for all connectionless sockets, and for
459             "server" sockets (the one you get from B(I => 1), not the
460             ones returned from B).
461              
462             This method is really just a wrapper around the Perl B builtin,
463             which dereferences IO::Socket::TIPC::Sockaddr class instances when
464             necessary.
465              
466             =cut
467              
468             sub bind {
469 0     0 1 0 my ($sock, $addr) = @_;
470 0         0 $addr = $$addr while ref $addr;
471 0         0 return $sock->SUPER::bind($addr);
472             }
473              
474              
475             =head2 connect(addr)
476              
477             B seeks out a server socket (which was Bed to a
478             well-known "name") and connects to it. B is only valid for
479             connection-type sockets which have not already had B or
480             B called on them. In practice, you should not ever need this
481             method; B calls it for you when you specify one or more
482             I arguments.
483              
484             This method is really just a wrapper around the Perl B
485             builtin, which dereferences IO::Socket::TIPC::Sockaddr class
486             instances when necessary.
487              
488             =cut
489              
490             sub connect {
491 0     0 1 0 my ($sock, $addr) = @_;
492 0         0 $addr = $$addr while ref $addr;
493 0         0 return $sock->SUPER::connect($addr);
494             }
495              
496              
497             =head2 getpeername
498              
499             B returns the sockaddr of the peer you're connected
500             to. Compare B. Use this if you've just B()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 doesn't actually return a I sockaddr, it returns
508             an I. Thus, B is an alias for B, to aid
509             readability. I 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
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     0 1 0 my ($self) = @_;
523 0         0 my $rv = CORE::getpeername($self);
524 0 0       0 return $rv unless defined $rv;
525 0         0 return IO::Socket::TIPC::Sockaddr->new_from_data($rv);
526             }
527 0     0 0 0 sub getpeerid { my $self = shift; return $self->getpeername(@_) };
  0         0  
528              
529             =head2 getsockname
530              
531             B returns the sockaddr of your own socket, this is the
532             address your peer sees you coming from. Compare B.
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 doesn't actually return a I sockaddr, it returns
539             an I. Thus, B is an alias for B, to aid
540             readability. I 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
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     0 1 0 my ($self) = @_;
554 0         0 my $rv = CORE::getsockname($self);
555 0 0       0 return $rv unless defined $rv;
556 0         0 return IO::Socket::TIPC::Sockaddr->new_from_data($rv);
557             }
558 0     0 0 0 sub getsockid { my $self = shift; return $self->getsockname(@_) };
  0         0  
559              
560              
561             =head2 listen
562              
563             B tells the operating system that this is a server socket,
564             and that you will be B()ing client connections on it. It is
565             only valid for connection-type sockets, and only if B has
566             not been called on it. It is much more useful if you have Bed
567             the socket to a well-known name; otherwise, most clients will have
568             difficulty knowing what to B to.
569              
570             This module does not actually implement a B 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 asks the operating system to return a session socket, for
577             communicating with a client which has just Bed to you. It is
578             only valid for connection-type sockets, which you have previously
579             called B on.
580              
581             This module does not actually implement an B 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 should be
589             I.
590              
591             The TIPC I (linked in B) 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(), below, for a list of I options.
599              
600             This module does not actually implement a B 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 should be
608             I.
609              
610             The TIPC I (linked in B) 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, 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. See also,
625             ->B()'s I and I options.
626              
627             This module does not actually implement a B 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 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 50   5 1 122612 if($^O eq 'linux') {
    0          
642 5 50       48653 return 1 if `grep -c ^TIPC /proc/net/protocols` == 1;
643             }
644             elsif($^O eq 'solaris') {
645 0 0       0 return 1 if `modinfo -c | grep -w tipc | grep -cv UNLOADED` == 1;
646             }
647 5         249 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* socktypes, you can
760             just specify "stream", "dgram", "seqpacket" or "rdm". In place
761             of the I*I<_SCOPE> defines, given to
762             IO::Socket::TIPC::Sockaddr->B() as the I parameter, you
763             can simply say I<"zone">, I<"cluster"> or I<"node">.
764              
765             =cut
766              
767              
768              
769 8     8   69 use XSLoader;
  8         33  
  8         1711  
770             XSLoader::load('IO::Socket::TIPC', $VERSION);
771              
772             IO::Socket::TIPC->register_domain(PF_TIPC());
773              
774             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             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             our @EXPORT = qw();
793             our @EXPORT_OK = qw();
794              
795             our %EXPORT_TAGS = (
796             'all' => [ @TIPC_STUFF, @SOCK_STUFF ],
797             'tipc' => [ @TIPC_STUFF ],
798             'sock' => [ @SOCK_STUFF ],
799             );
800             Exporter::export_ok_tags('all');
801              
802             1;
803             __END__