PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/IO/Socket/TIPC.pm

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