This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
This is my patch patch.1n for perl5.001.
[perl5.git] / ext / Socket / Socket.pm
CommitLineData
a0d0e21e 1package Socket;
3b35bae3
AD
2
3=head1 NAME
4
8e07c86e 5Socket - load the C socket.h defines and structure manipulators
3b35bae3
AD
6
7=head1 SYNOPSIS
8
9 use Socket;
10
11 $proto = (getprotobyname('udp'))[2];
8e07c86e
AD
12 socket(Socket_Handle, PF_INET, SOCK_DGRAM, $proto);
13 $sockaddr_in = pack_sockaddr_in(AF_INET,7,inet_aton("localhost"));
14 $sockaddr_in = pack_sockaddr_in(AF_INET,7,INADDR_LOOPBACK);
15 connect(Socket_Handle,$sockaddr_in);
16 $peer = inet_ntoa((unpack_sockaddr_in(getpeername(Socket_Handle)))[2]);
17
3b35bae3
AD
18
19=head1 DESCRIPTION
20
21This module is just a translation of the C F<socket.h> file.
22Unlike the old mechanism of requiring a translated F<socket.ph>
23file, this uses the B<h2xs> program (see the Perl source distribution)
24and your native C compiler. This means that it has a
25far more likely chance of getting the numbers right.
26
8e07c86e
AD
27In addition, some structure manipulation functions are available:
28
29=item inet_aton HOSTNAME
30
31Takes a string giving the name of a host, and translates that
32to the 4-byte string (structure). Takes arguments of both
33the 'rtfm.mit.edu' type and '18.181.0.24'. If the host name
34cannot be resolved, returns undef.
35
36=item inet_ntoa IP_ADDRESS
37
38Takes a four byte ip address (as returned by inet_aton())
39and translates it into a string of the form 'd.d.d.d'
40where the 'd's are numbers less than 256 (the normal
41readable four dotted number notation for internet addresses).
42
43=item INADDR_ANY
44
45Note - does not return a number.
46
47Returns the 4-byte wildcard ip address which specifies any
48of the hosts ip addresses. (A particular machine can have
49more than one ip address, each address corresponding to
50a particular network interface. This wildcard address
51allows you to bind to all of them simultaneously.)
52Normally equivalent to inet_aton('0.0.0.0').
53
54=item INADDR_LOOPBACK
55
56Note - does not return a number.
57
58Returns the 4-byte loopback address. Normally equivalent
59to inet_aton('localhost').
3b35bae3 60
8e07c86e
AD
61=item INADDR_NONE
62
63Note - does not return a number.
64
65Returns the 4-byte invalid ip address. Normally equivalent
66to inet_aton('255.255.255.255').
67
68=item pack_sockaddr_in FAMILY, PORT, IP_ADDRESS
69
70Takes three arguments, an address family (normally AF_INET),
71a port number, and a 4 byte IP_ADDRESS (as returned by
72inet_aton()). Returns the sockaddr_in structure with those
73arguments packed in. For internet domain sockets, this structure
74is normally what you need for the arguments in bind(), connect(),
75and send(), and is also returned by getpeername(), getsockname()
76and recv().
77
78=item unpack_sockaddr_in SOCKADDR_IN
79
80Takes a sockaddr_in structure (as returned by pack_sockaddr_in())
81and returns an array of three elements: the address family,
82the port, and the 4-byte ip-address.
3b35bae3
AD
83
84=cut
85
a0d0e21e
LW
86use Carp;
87
88require Exporter;
3b35bae3 89use AutoLoader;
a0d0e21e 90require DynaLoader;
fec02dd3 91@ISA = qw(Exporter DynaLoader);
a0d0e21e 92@EXPORT = qw(
8e07c86e
AD
93 inet_aton inet_ntoa pack_sockaddr_in unpack_sockaddr_in
94 INADDR_ANY INADDR_LOOPBACK INADDR_NONE
a0d0e21e
LW
95 AF_802
96 AF_APPLETALK
97 AF_CCITT
98 AF_CHAOS
99 AF_DATAKIT
100 AF_DECnet
101 AF_DLI
102 AF_ECMA
103 AF_GOSIP
104 AF_HYLINK
105 AF_IMPLINK
106 AF_INET
107 AF_LAT
108 AF_MAX
109 AF_NBS
110 AF_NIT
111 AF_NS
112 AF_OSI
113 AF_OSINET
114 AF_PUP
115 AF_SNA
116 AF_UNIX
117 AF_UNSPEC
118 AF_X25
119 MSG_DONTROUTE
120 MSG_MAXIOVLEN
121 MSG_OOB
122 MSG_PEEK
123 PF_802
124 PF_APPLETALK
125 PF_CCITT
126 PF_CHAOS
127 PF_DATAKIT
128 PF_DECnet
129 PF_DLI
130 PF_ECMA
131 PF_GOSIP
132 PF_HYLINK
133 PF_IMPLINK
134 PF_INET
135 PF_LAT
136 PF_MAX
137 PF_NBS
138 PF_NIT
139 PF_NS
140 PF_OSI
141 PF_OSINET
142 PF_PUP
143 PF_SNA
144 PF_UNIX
145 PF_UNSPEC
146 PF_X25
147 SOCK_DGRAM
148 SOCK_RAW
149 SOCK_RDM
150 SOCK_SEQPACKET
151 SOCK_STREAM
152 SOL_SOCKET
153 SOMAXCONN
154 SO_ACCEPTCONN
155 SO_BROADCAST
156 SO_DEBUG
157 SO_DONTLINGER
158 SO_DONTROUTE
159 SO_ERROR
160 SO_KEEPALIVE
161 SO_LINGER
162 SO_OOBINLINE
163 SO_RCVBUF
164 SO_RCVLOWAT
165 SO_RCVTIMEO
166 SO_REUSEADDR
167 SO_SNDBUF
168 SO_SNDLOWAT
169 SO_SNDTIMEO
170 SO_TYPE
171 SO_USELOOPBACK
172);
173
174sub AUTOLOAD {
a0d0e21e
LW
175 local($constname);
176 ($constname = $AUTOLOAD) =~ s/.*:://;
177 $val = constant($constname, @_ ? $_[0] : 0);
178 if ($! != 0) {
179 if ($! =~ /Invalid/) {
180 $AutoLoader::AUTOLOAD = $AUTOLOAD;
181 goto &AutoLoader::AUTOLOAD;
182 }
183 else {
184 ($pack,$file,$line) = caller;
185 croak "Your vendor has not defined Socket macro $constname, used";
186 }
187 }
188 eval "sub $AUTOLOAD { $val }";
189 goto &$AUTOLOAD;
190}
191
192bootstrap Socket;
193
194# Preloaded methods go here. Autoload methods go after __END__, and are
195# processed by the autosplit program.
196
1971;
198__END__