Integrate macperl patch #16868.
[p5sagit/p5-mst-13.2.git] / ext / IO / lib / IO / Socket / UNIX.pm
1 # IO::Socket::UNIX.pm
2 #
3 # Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
4 # This program is free software; you can redistribute it and/or
5 # modify it under the same terms as Perl itself.
6
7 package IO::Socket::UNIX;
8
9 use strict;
10 our(@ISA, $VERSION);
11 use IO::Socket;
12 use Socket;
13 use Carp;
14
15 @ISA = qw(IO::Socket);
16 $VERSION = "1.20_00";
17 $VERSION = eval $VERSION;
18
19 IO::Socket::UNIX->register_domain( AF_UNIX );
20
21 sub new {
22     my $class = shift;
23     unshift(@_, "Peer") if @_ == 1;
24     return $class->SUPER::new(@_);
25 }
26
27 sub configure {
28     my($sock,$arg) = @_;
29     my($bport,$cport);
30
31     my $type = $arg->{Type} || SOCK_STREAM;
32
33     $sock->socket(AF_UNIX, $type, 0) or
34         return undef;
35
36     if(exists $arg->{Local}) {
37         my $addr = sockaddr_un($arg->{Local});
38         $sock->bind($addr) or
39             return undef;
40     }
41     if(exists $arg->{Listen} && $type != SOCK_DGRAM) {
42         $sock->listen($arg->{Listen} || 5) or
43             return undef;
44     }
45     elsif(exists $arg->{Peer}) {
46         my $addr = sockaddr_un($arg->{Peer});
47         $sock->connect($addr) or
48             return undef;
49     }
50
51     $sock;
52 }
53
54 sub hostpath {
55     @_ == 1 or croak 'usage: $sock->hostpath()';
56     my $n = $_[0]->sockname || return undef;
57     (sockaddr_un($n))[0];
58 }
59
60 sub peerpath {
61     @_ == 1 or croak 'usage: $sock->peerpath()';
62     my $n = $_[0]->peername || return undef;
63     (sockaddr_un($n))[0];
64 }
65
66 1; # Keep require happy
67
68 __END__
69
70 =head1 NAME
71
72 IO::Socket::UNIX - Object interface for AF_UNIX domain sockets
73
74 =head1 SYNOPSIS
75
76     use IO::Socket::UNIX;
77
78 =head1 DESCRIPTION
79
80 C<IO::Socket::UNIX> provides an object interface to creating and using sockets
81 in the AF_UNIX domain. It is built upon the L<IO::Socket> interface and
82 inherits all the methods defined by L<IO::Socket>.
83
84 =head1 CONSTRUCTOR
85
86 =over 4
87
88 =item new ( [ARGS] )
89
90 Creates an C<IO::Socket::UNIX> object, which is a reference to a
91 newly created symbol (see the C<Symbol> package). C<new>
92 optionally takes arguments, these arguments are in key-value pairs.
93
94 In addition to the key-value pairs accepted by L<IO::Socket>,
95 C<IO::Socket::UNIX> provides.
96
97     Type        Type of socket (eg SOCK_STREAM or SOCK_DGRAM)
98     Local       Path to local fifo
99     Peer        Path to peer fifo
100     Listen      Create a listen socket
101
102 If the constructor is only passed a single argument, it is assumed to
103 be a C<Peer> specification.
104
105
106  NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
107
108 As of VERSION 1.18 all IO::Socket objects have autoflush turned on
109 by default. This was not the case with earlier releases.
110
111  NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
112
113 =back
114
115 =head1 METHODS
116
117 =over 4
118
119 =item hostpath()
120
121 Returns the pathname to the fifo at the local end
122
123 =item peerpath()
124
125 Returns the pathanme to the fifo at the peer end
126
127 =back
128
129 =head1 SEE ALSO
130
131 L<Socket>, L<IO::Socket>
132
133 =head1 AUTHOR
134
135 Graham Barr. Currently maintained by the Perl Porters.  Please report all
136 bugs to <perl5-porters@perl.org>.
137
138 =head1 COPYRIGHT
139
140 Copyright (c) 1996-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
141 This program is free software; you can redistribute it and/or
142 modify it under the same terms as Perl itself.
143
144 =cut