Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | package Socket; |
73c78b0a | 2 | |
17f410f9 | 3 | our($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS); |
735631af | 4 | $VERSION = "1.91"; |
3b35bae3 AD |
5 | |
6 | =head1 NAME | |
7 | ||
036d8bd4 | 8 | Socket, sockaddr_in, sockaddr_un, inet_aton, inet_ntoa, inet_pton, inet_ntop - load the C socket.h defines and structure manipulators |
3b35bae3 AD |
9 | |
10 | =head1 SYNOPSIS | |
11 | ||
12 | use Socket; | |
13 | ||
4633a7c4 | 14 | $proto = getprotobyname('udp'); |
8e07c86e | 15 | socket(Socket_Handle, PF_INET, SOCK_DGRAM, $proto); |
4633a7c4 LW |
16 | $iaddr = gethostbyname('hishost.com'); |
17 | $port = getservbyname('time', 'udp'); | |
18 | $sin = sockaddr_in($port, $iaddr); | |
19 | send(Socket_Handle, 0, 0, $sin); | |
20 | ||
21 | $proto = getprotobyname('tcp'); | |
22 | socket(Socket_Handle, PF_INET, SOCK_STREAM, $proto); | |
67d7c167 | 23 | $port = getservbyname('smtp', 'tcp'); |
4633a7c4 LW |
24 | $sin = sockaddr_in($port,inet_aton("127.1")); |
25 | $sin = sockaddr_in(7,inet_aton("localhost")); | |
26 | $sin = sockaddr_in(7,INADDR_LOOPBACK); | |
27 | connect(Socket_Handle,$sin); | |
28 | ||
29 | ($port, $iaddr) = sockaddr_in(getpeername(Socket_Handle)); | |
30 | $peer_host = gethostbyaddr($iaddr, AF_INET); | |
31 | $peer_addr = inet_ntoa($iaddr); | |
32 | ||
33 | $proto = getprotobyname('tcp'); | |
34 | socket(Socket_Handle, PF_UNIX, SOCK_STREAM, $proto); | |
2359510d SD |
35 | unlink('/var/run/usock'); |
36 | $sun = sockaddr_un('/var/run/usock'); | |
4633a7c4 | 37 | connect(Socket_Handle,$sun); |
3b35bae3 AD |
38 | |
39 | =head1 DESCRIPTION | |
40 | ||
41 | This module is just a translation of the C F<socket.h> file. | |
42 | Unlike the old mechanism of requiring a translated F<socket.ph> | |
43 | file, this uses the B<h2xs> program (see the Perl source distribution) | |
44 | and your native C compiler. This means that it has a | |
4633a7c4 LW |
45 | far more likely chance of getting the numbers right. This includes |
46 | all of the commonly used pound-defines like AF_INET, SOCK_STREAM, etc. | |
3b35bae3 | 47 | |
fdb41d65 CN |
48 | Also, some common socket "newline" constants are provided: the |
49 | constants C<CR>, C<LF>, and C<CRLF>, as well as C<$CR>, C<$LF>, and | |
50 | C<$CRLF>, which map to C<\015>, C<\012>, and C<\015\012>. If you do | |
51 | not want to use the literal characters in your programs, then use | |
52 | the constants provided here. They are not exported by default, but can | |
53 | be imported individually, and with the C<:crlf> export tag: | |
54 | ||
55 | use Socket qw(:DEFAULT :crlf); | |
56 | ||
8e07c86e AD |
57 | In addition, some structure manipulation functions are available: |
58 | ||
bbc7dcd2 | 59 | =over 4 |
2ae324a7 | 60 | |
8e07c86e AD |
61 | =item inet_aton HOSTNAME |
62 | ||
6fe628c6 JH |
63 | Takes a string giving the name of a host, and translates that to an |
64 | opaque string (if programming in C, struct in_addr). Takes arguments | |
65 | of both the 'rtfm.mit.edu' type and '18.181.0.24'. If the host name | |
66 | cannot be resolved, returns undef. For multi-homed hosts (hosts with | |
67 | more than one address), the first address found is returned. | |
8e07c86e | 68 | |
2528d3bc | 69 | For portability do not assume that the result of inet_aton() is 32 |
6fe628c6 JH |
70 | bits wide, in other words, that it would contain only the IPv4 address |
71 | in network order. | |
2528d3bc | 72 | |
8e07c86e AD |
73 | =item inet_ntoa IP_ADDRESS |
74 | ||
6fe628c6 JH |
75 | Takes a string (an opaque string as returned by inet_aton(), |
76 | or a v-string representing the four octets of the IPv4 address in | |
2528d3bc | 77 | network order) and translates it into a string of the form 'd.d.d.d' |
6fe628c6 JH |
78 | where the 'd's are numbers less than 256 (the normal human-readable |
79 | four dotted number notation for Internet addresses). | |
8e07c86e AD |
80 | |
81 | =item INADDR_ANY | |
82 | ||
4633a7c4 | 83 | Note: does not return a number, but a packed string. |
8e07c86e AD |
84 | |
85 | Returns the 4-byte wildcard ip address which specifies any | |
6fe628c6 | 86 | of the hosts ip addresses. (A particular machine can have |
8e07c86e AD |
87 | more than one ip address, each address corresponding to |
88 | a particular network interface. This wildcard address | |
89 | allows you to bind to all of them simultaneously.) | |
90 | Normally equivalent to inet_aton('0.0.0.0'). | |
91 | ||
7e1af8bc | 92 | =item INADDR_BROADCAST |
93 | ||
94 | Note: does not return a number, but a packed string. | |
95 | ||
96 | Returns the 4-byte 'this-lan' ip broadcast address. | |
97 | This can be useful for some protocols to solicit information | |
98 | from all servers on the same LAN cable. | |
99 | Normally equivalent to inet_aton('255.255.255.255'). | |
100 | ||
8e07c86e AD |
101 | =item INADDR_LOOPBACK |
102 | ||
103 | Note - does not return a number. | |
104 | ||
6fe628c6 | 105 | Returns the 4-byte loopback address. Normally equivalent |
8e07c86e | 106 | to inet_aton('localhost'). |
3b35bae3 | 107 | |
8e07c86e AD |
108 | =item INADDR_NONE |
109 | ||
110 | Note - does not return a number. | |
111 | ||
6fe628c6 | 112 | Returns the 4-byte 'invalid' ip address. Normally equivalent |
8e07c86e AD |
113 | to inet_aton('255.255.255.255'). |
114 | ||
2a84dff3 GA |
115 | =item sockaddr_family SOCKADDR |
116 | ||
117 | Takes a sockaddr structure (as returned by pack_sockaddr_in(), | |
118 | pack_sockaddr_un() or the perl builtin functions getsockname() and | |
119 | getpeername()) and returns the address family tag. It will match the | |
120 | constant AF_INET for a sockaddr_in and AF_UNIX for a sockaddr_un. It | |
121 | can be used to figure out what unpacker to use for a sockaddr of | |
122 | unknown type. | |
123 | ||
4633a7c4 LW |
124 | =item sockaddr_in PORT, ADDRESS |
125 | ||
126 | =item sockaddr_in SOCKADDR_IN | |
127 | ||
91e74348 | 128 | In a list context, unpacks its SOCKADDR_IN argument and returns an array |
4633a7c4 LW |
129 | consisting of (PORT, ADDRESS). In a scalar context, packs its (PORT, |
130 | ADDRESS) arguments as a SOCKADDR_IN and returns it. If this is confusing, | |
131 | use pack_sockaddr_in() and unpack_sockaddr_in() explicitly. | |
132 | ||
133 | =item pack_sockaddr_in PORT, IP_ADDRESS | |
8e07c86e | 134 | |
6fe628c6 JH |
135 | Takes two arguments, a port number and an opaque string, IP_ADDRESS |
136 | (as returned by inet_aton(), or a v-string). Returns the sockaddr_in | |
137 | structure with those arguments packed in with AF_INET filled in. For | |
138 | Internet domain sockets, this structure is normally what you need for | |
139 | the arguments in bind(), connect(), and send(), and is also returned | |
140 | by getpeername(), getsockname() and recv(). | |
8e07c86e AD |
141 | |
142 | =item unpack_sockaddr_in SOCKADDR_IN | |
143 | ||
4633a7c4 | 144 | Takes a sockaddr_in structure (as returned by pack_sockaddr_in()) and |
6fe628c6 JH |
145 | returns an array of two elements: the port and an opaque string |
146 | representing the IP address (you can use inet_ntoa() to convert the | |
147 | address to the four-dotted numeric format). Will croak if the | |
148 | structure does not have AF_INET in the right place. | |
4633a7c4 LW |
149 | |
150 | =item sockaddr_un PATHNAME | |
151 | ||
152 | =item sockaddr_un SOCKADDR_UN | |
153 | ||
91e74348 | 154 | In a list context, unpacks its SOCKADDR_UN argument and returns an array |
1fef88e7 | 155 | consisting of (PATHNAME). In a scalar context, packs its PATHNAME |
4633a7c4 LW |
156 | arguments as a SOCKADDR_UN and returns it. If this is confusing, use |
157 | pack_sockaddr_un() and unpack_sockaddr_un() explicitly. | |
1fef88e7 | 158 | These are only supported if your system has E<lt>F<sys/un.h>E<gt>. |
4633a7c4 LW |
159 | |
160 | =item pack_sockaddr_un PATH | |
161 | ||
162 | Takes one argument, a pathname. Returns the sockaddr_un structure with | |
163 | that path packed in with AF_UNIX filled in. For unix domain sockets, this | |
164 | structure is normally what you need for the arguments in bind(), | |
165 | connect(), and send(), and is also returned by getpeername(), | |
166 | getsockname() and recv(). | |
167 | ||
168 | =item unpack_sockaddr_un SOCKADDR_UN | |
169 | ||
170 | Takes a sockaddr_un structure (as returned by pack_sockaddr_un()) | |
171 | and returns the pathname. Will croak if the structure does not | |
172 | have AF_UNIX in the right place. | |
3b35bae3 | 173 | |
036d8bd4 SP |
174 | =item inet_pton ADDRESS_FAMILY, HOSTNAME |
175 | ||
176 | Takes an address family, either AF_INET or AF_INET6, and a string giving | |
177 | the name of a host, and translates that to an opaque string | |
178 | (if programming in C, struct in_addr or struct in6_addr depending on the | |
179 | address family passed in). The host string may be a string hostname, such | |
180 | as 'www.perl.org', or an IP address. If using an IP address, the type of | |
181 | IP address must be consistant with the address family passed into the function. | |
182 | ||
d6896be3 JV |
183 | This function is not exported by default. |
184 | ||
036d8bd4 SP |
185 | =item inet_ntop ADDRESS_FAMILY, IP_ADDRESS |
186 | ||
187 | Takes an address family, either AF_INET or AF_INET6, and a string | |
188 | (an opaque string as returned by inet_aton() or inet_pton()) and | |
189 | translates it to an IPv4 or IPv6 address string. | |
190 | ||
d6896be3 JV |
191 | This function is not exported by default. |
192 | ||
2ae324a7 | 193 | =back |
194 | ||
3b35bae3 AD |
195 | =cut |
196 | ||
a0d0e21e | 197 | use Carp; |
d3a7d8c7 | 198 | use warnings::register; |
a0d0e21e LW |
199 | |
200 | require Exporter; | |
da4061d3 | 201 | require XSLoader; |
9426adcd | 202 | @ISA = qw(Exporter); |
a0d0e21e | 203 | @EXPORT = qw( |
2a84dff3 GA |
204 | inet_aton inet_ntoa |
205 | sockaddr_family | |
206 | pack_sockaddr_in unpack_sockaddr_in | |
4633a7c4 LW |
207 | pack_sockaddr_un unpack_sockaddr_un |
208 | sockaddr_in sockaddr_un | |
7e1af8bc | 209 | INADDR_ANY INADDR_BROADCAST INADDR_LOOPBACK INADDR_NONE |
a0d0e21e | 210 | AF_802 |
7198d2fd | 211 | AF_AAL |
a0d0e21e LW |
212 | AF_APPLETALK |
213 | AF_CCITT | |
214 | AF_CHAOS | |
7198d2fd | 215 | AF_CTF |
a0d0e21e LW |
216 | AF_DATAKIT |
217 | AF_DECnet | |
218 | AF_DLI | |
219 | AF_ECMA | |
220 | AF_GOSIP | |
221 | AF_HYLINK | |
222 | AF_IMPLINK | |
223 | AF_INET | |
7198d2fd JH |
224 | AF_INET6 |
225 | AF_ISO | |
226 | AF_KEY | |
227 | AF_LAST | |
a0d0e21e | 228 | AF_LAT |
7198d2fd | 229 | AF_LINK |
a0d0e21e LW |
230 | AF_MAX |
231 | AF_NBS | |
232 | AF_NIT | |
233 | AF_NS | |
234 | AF_OSI | |
235 | AF_OSINET | |
236 | AF_PUP | |
7198d2fd | 237 | AF_ROUTE |
a0d0e21e LW |
238 | AF_SNA |
239 | AF_UNIX | |
240 | AF_UNSPEC | |
7198d2fd JH |
241 | AF_USER |
242 | AF_WAN | |
a0d0e21e | 243 | AF_X25 |
6b1016b5 | 244 | IOV_MAX |
131c565a SP |
245 | IP_OPTIONS |
246 | IP_HDRINCL | |
247 | IP_TOS | |
248 | IP_TTL | |
249 | IP_RECVOPTS | |
250 | IP_RECVRETOPTS | |
251 | IP_RETOPTS | |
6b1016b5 | 252 | MSG_BCAST |
7198d2fd | 253 | MSG_BTAG |
de4597cb JH |
254 | MSG_CTLFLAGS |
255 | MSG_CTLIGNORE | |
256 | MSG_CTRUNC | |
a0d0e21e | 257 | MSG_DONTROUTE |
de4597cb JH |
258 | MSG_DONTWAIT |
259 | MSG_EOF | |
260 | MSG_EOR | |
261 | MSG_ERRQUEUE | |
7198d2fd | 262 | MSG_ETAG |
de4597cb | 263 | MSG_FIN |
a0d0e21e | 264 | MSG_MAXIOVLEN |
6b1016b5 | 265 | MSG_MCAST |
de4597cb | 266 | MSG_NOSIGNAL |
a0d0e21e LW |
267 | MSG_OOB |
268 | MSG_PEEK | |
de4597cb JH |
269 | MSG_PROXY |
270 | MSG_RST | |
271 | MSG_SYN | |
272 | MSG_TRUNC | |
273 | MSG_URG | |
274 | MSG_WAITALL | |
7198d2fd | 275 | MSG_WIRE |
a0d0e21e | 276 | PF_802 |
7198d2fd | 277 | PF_AAL |
a0d0e21e LW |
278 | PF_APPLETALK |
279 | PF_CCITT | |
280 | PF_CHAOS | |
7198d2fd | 281 | PF_CTF |
a0d0e21e LW |
282 | PF_DATAKIT |
283 | PF_DECnet | |
284 | PF_DLI | |
285 | PF_ECMA | |
286 | PF_GOSIP | |
287 | PF_HYLINK | |
288 | PF_IMPLINK | |
289 | PF_INET | |
7198d2fd JH |
290 | PF_INET6 |
291 | PF_ISO | |
292 | PF_KEY | |
293 | PF_LAST | |
a0d0e21e | 294 | PF_LAT |
7198d2fd | 295 | PF_LINK |
a0d0e21e LW |
296 | PF_MAX |
297 | PF_NBS | |
298 | PF_NIT | |
299 | PF_NS | |
300 | PF_OSI | |
301 | PF_OSINET | |
302 | PF_PUP | |
7198d2fd | 303 | PF_ROUTE |
a0d0e21e LW |
304 | PF_SNA |
305 | PF_UNIX | |
306 | PF_UNSPEC | |
7198d2fd JH |
307 | PF_USER |
308 | PF_WAN | |
a0d0e21e | 309 | PF_X25 |
de4597cb JH |
310 | SCM_CONNECT |
311 | SCM_CREDENTIALS | |
312 | SCM_CREDS | |
313 | SCM_RIGHTS | |
314 | SCM_TIMESTAMP | |
ca6e1c26 JH |
315 | SHUT_RD |
316 | SHUT_RDWR | |
317 | SHUT_WR | |
a0d0e21e LW |
318 | SOCK_DGRAM |
319 | SOCK_RAW | |
320 | SOCK_RDM | |
321 | SOCK_SEQPACKET | |
322 | SOCK_STREAM | |
323 | SOL_SOCKET | |
324 | SOMAXCONN | |
325 | SO_ACCEPTCONN | |
7198d2fd JH |
326 | SO_ATTACH_FILTER |
327 | SO_BACKLOG | |
a0d0e21e | 328 | SO_BROADCAST |
7198d2fd | 329 | SO_CHAMELEON |
a0d0e21e | 330 | SO_DEBUG |
7198d2fd JH |
331 | SO_DETACH_FILTER |
332 | SO_DGRAM_ERRIND | |
a0d0e21e LW |
333 | SO_DONTLINGER |
334 | SO_DONTROUTE | |
335 | SO_ERROR | |
7198d2fd | 336 | SO_FAMILY |
a0d0e21e LW |
337 | SO_KEEPALIVE |
338 | SO_LINGER | |
339 | SO_OOBINLINE | |
7198d2fd JH |
340 | SO_PASSCRED |
341 | SO_PASSIFNAME | |
342 | SO_PEERCRED | |
343 | SO_PROTOCOL | |
344 | SO_PROTOTYPE | |
a0d0e21e LW |
345 | SO_RCVBUF |
346 | SO_RCVLOWAT | |
347 | SO_RCVTIMEO | |
348 | SO_REUSEADDR | |
b2f54bf3 | 349 | SO_REUSEPORT |
7198d2fd JH |
350 | SO_SECURITY_AUTHENTICATION |
351 | SO_SECURITY_ENCRYPTION_NETWORK | |
352 | SO_SECURITY_ENCRYPTION_TRANSPORT | |
a0d0e21e LW |
353 | SO_SNDBUF |
354 | SO_SNDLOWAT | |
355 | SO_SNDTIMEO | |
7198d2fd | 356 | SO_STATE |
a0d0e21e LW |
357 | SO_TYPE |
358 | SO_USELOOPBACK | |
7198d2fd JH |
359 | SO_XOPEN |
360 | SO_XSE | |
6b1016b5 | 361 | UIO_MAXIOV |
a0d0e21e LW |
362 | ); |
363 | ||
1494e794 JP |
364 | @EXPORT_OK = qw(CR LF CRLF $CR $LF $CRLF |
365 | ||
d6896be3 JV |
366 | inet_pton |
367 | inet_ntop | |
368 | ||
131c565a SP |
369 | IPPROTO_IP |
370 | IPPROTO_IPV6 | |
371 | IPPROTO_RAW | |
91c526f1 | 372 | IPPROTO_ICMP |
1494e794 | 373 | IPPROTO_TCP |
91c526f1 GA |
374 | IPPROTO_UDP |
375 | ||
1494e794 JP |
376 | TCP_KEEPALIVE |
377 | TCP_MAXRT | |
378 | TCP_MAXSEG | |
379 | TCP_NODELAY | |
f686127b SF |
380 | TCP_STDURG |
381 | TCP_CORK | |
382 | TCP_KEEPIDLE | |
383 | TCP_KEEPINTVL | |
384 | TCP_KEEPCNT | |
385 | TCP_SYNCNT | |
386 | TCP_LINGER2 | |
387 | TCP_DEFER_ACCEPT | |
388 | TCP_WINDOW_CLAMP | |
389 | TCP_INFO | |
390 | TCP_QUICKACK | |
391 | TCP_CONGESTION | |
392 | TCP_MD5SIG); | |
fdb41d65 CN |
393 | |
394 | %EXPORT_TAGS = ( | |
dde527fc | 395 | crlf => [qw(CR LF CRLF $CR $LF $CRLF)], |
fdb41d65 CN |
396 | all => [@EXPORT, @EXPORT_OK], |
397 | ); | |
398 | ||
399 | BEGIN { | |
400 | sub CR () {"\015"} | |
401 | sub LF () {"\012"} | |
402 | sub CRLF () {"\015\012"} | |
403 | } | |
404 | ||
405 | *CR = \CR(); | |
406 | *LF = \LF(); | |
407 | *CRLF = \CRLF(); | |
408 | ||
4633a7c4 LW |
409 | sub sockaddr_in { |
410 | if (@_ == 6 && !wantarray) { # perl5.001m compat; use this && die | |
411 | my($af, $port, @quad) = @_; | |
d3a7d8c7 GS |
412 | warnings::warn "6-ARG sockaddr_in call is deprecated" |
413 | if warnings::enabled(); | |
4633a7c4 LW |
414 | pack_sockaddr_in($port, inet_aton(join('.', @quad))); |
415 | } elsif (wantarray) { | |
416 | croak "usage: (port,iaddr) = sockaddr_in(sin_sv)" unless @_ == 1; | |
417 | unpack_sockaddr_in(@_); | |
418 | } else { | |
419 | croak "usage: sin_sv = sockaddr_in(port,iaddr))" unless @_ == 2; | |
420 | pack_sockaddr_in(@_); | |
421 | } | |
422 | } | |
423 | ||
424 | sub sockaddr_un { | |
425 | if (wantarray) { | |
426 | croak "usage: (filename) = sockaddr_un(sun_sv)" unless @_ == 1; | |
427 | unpack_sockaddr_un(@_); | |
428 | } else { | |
37120919 AD |
429 | croak "usage: sun_sv = sockaddr_un(filename)" unless @_ == 1; |
430 | pack_sockaddr_un(@_); | |
4633a7c4 LW |
431 | } |
432 | } | |
433 | ||
da4061d3 | 434 | XSLoader::load(); |
a0d0e21e | 435 | |
a0d0e21e | 436 | 1; |