Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

327 lines
8.1 KiB

  1. package Socket;
  2. use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  3. $VERSION = "1.7";
  4. =head1 NAME
  5. Socket, sockaddr_in, sockaddr_un, inet_aton, inet_ntoa - load the C socket.h defines and structure manipulators
  6. =head1 SYNOPSIS
  7. use Socket;
  8. $proto = getprotobyname('udp');
  9. socket(Socket_Handle, PF_INET, SOCK_DGRAM, $proto);
  10. $iaddr = gethostbyname('hishost.com');
  11. $port = getservbyname('time', 'udp');
  12. $sin = sockaddr_in($port, $iaddr);
  13. send(Socket_Handle, 0, 0, $sin);
  14. $proto = getprotobyname('tcp');
  15. socket(Socket_Handle, PF_INET, SOCK_STREAM, $proto);
  16. $port = getservbyname('smtp', 'tcp');
  17. $sin = sockaddr_in($port,inet_aton("127.1"));
  18. $sin = sockaddr_in(7,inet_aton("localhost"));
  19. $sin = sockaddr_in(7,INADDR_LOOPBACK);
  20. connect(Socket_Handle,$sin);
  21. ($port, $iaddr) = sockaddr_in(getpeername(Socket_Handle));
  22. $peer_host = gethostbyaddr($iaddr, AF_INET);
  23. $peer_addr = inet_ntoa($iaddr);
  24. $proto = getprotobyname('tcp');
  25. socket(Socket_Handle, PF_UNIX, SOCK_STREAM, $proto);
  26. unlink('/tmp/usock');
  27. $sun = sockaddr_un('/tmp/usock');
  28. connect(Socket_Handle,$sun);
  29. =head1 DESCRIPTION
  30. This module is just a translation of the C F<socket.h> file.
  31. Unlike the old mechanism of requiring a translated F<socket.ph>
  32. file, this uses the B<h2xs> program (see the Perl source distribution)
  33. and your native C compiler. This means that it has a
  34. far more likely chance of getting the numbers right. This includes
  35. all of the commonly used pound-defines like AF_INET, SOCK_STREAM, etc.
  36. Also, some common socket "newline" constants are provided: the
  37. constants C<CR>, C<LF>, and C<CRLF>, as well as C<$CR>, C<$LF>, and
  38. C<$CRLF>, which map to C<\015>, C<\012>, and C<\015\012>. If you do
  39. not want to use the literal characters in your programs, then use
  40. the constants provided here. They are not exported by default, but can
  41. be imported individually, and with the C<:crlf> export tag:
  42. use Socket qw(:DEFAULT :crlf);
  43. In addition, some structure manipulation functions are available:
  44. =over
  45. =item inet_aton HOSTNAME
  46. Takes a string giving the name of a host, and translates that
  47. to the 4-byte string (structure). Takes arguments of both
  48. the 'rtfm.mit.edu' type and '18.181.0.24'. If the host name
  49. cannot be resolved, returns undef. For multi-homed hosts (hosts
  50. with more than one address), the first address found is returned.
  51. =item inet_ntoa IP_ADDRESS
  52. Takes a four byte ip address (as returned by inet_aton())
  53. and translates it into a string of the form 'd.d.d.d'
  54. where the 'd's are numbers less than 256 (the normal
  55. readable four dotted number notation for internet addresses).
  56. =item INADDR_ANY
  57. Note: does not return a number, but a packed string.
  58. Returns the 4-byte wildcard ip address which specifies any
  59. of the hosts ip addresses. (A particular machine can have
  60. more than one ip address, each address corresponding to
  61. a particular network interface. This wildcard address
  62. allows you to bind to all of them simultaneously.)
  63. Normally equivalent to inet_aton('0.0.0.0').
  64. =item INADDR_BROADCAST
  65. Note: does not return a number, but a packed string.
  66. Returns the 4-byte 'this-lan' ip broadcast address.
  67. This can be useful for some protocols to solicit information
  68. from all servers on the same LAN cable.
  69. Normally equivalent to inet_aton('255.255.255.255').
  70. =item INADDR_LOOPBACK
  71. Note - does not return a number.
  72. Returns the 4-byte loopback address. Normally equivalent
  73. to inet_aton('localhost').
  74. =item INADDR_NONE
  75. Note - does not return a number.
  76. Returns the 4-byte 'invalid' ip address. Normally equivalent
  77. to inet_aton('255.255.255.255').
  78. =item sockaddr_in PORT, ADDRESS
  79. =item sockaddr_in SOCKADDR_IN
  80. In an array context, unpacks its SOCKADDR_IN argument and returns an array
  81. consisting of (PORT, ADDRESS). In a scalar context, packs its (PORT,
  82. ADDRESS) arguments as a SOCKADDR_IN and returns it. If this is confusing,
  83. use pack_sockaddr_in() and unpack_sockaddr_in() explicitly.
  84. =item pack_sockaddr_in PORT, IP_ADDRESS
  85. Takes two arguments, a port number and a 4 byte IP_ADDRESS (as returned by
  86. inet_aton()). Returns the sockaddr_in structure with those arguments
  87. packed in with AF_INET filled in. For internet domain sockets, this
  88. structure is normally what you need for the arguments in bind(),
  89. connect(), and send(), and is also returned by getpeername(),
  90. getsockname() and recv().
  91. =item unpack_sockaddr_in SOCKADDR_IN
  92. Takes a sockaddr_in structure (as returned by pack_sockaddr_in()) and
  93. returns an array of two elements: the port and the 4-byte ip-address.
  94. Will croak if the structure does not have AF_INET in the right place.
  95. =item sockaddr_un PATHNAME
  96. =item sockaddr_un SOCKADDR_UN
  97. In an array context, unpacks its SOCKADDR_UN argument and returns an array
  98. consisting of (PATHNAME). In a scalar context, packs its PATHNAME
  99. arguments as a SOCKADDR_UN and returns it. If this is confusing, use
  100. pack_sockaddr_un() and unpack_sockaddr_un() explicitly.
  101. These are only supported if your system has E<lt>F<sys/un.h>E<gt>.
  102. =item pack_sockaddr_un PATH
  103. Takes one argument, a pathname. Returns the sockaddr_un structure with
  104. that path packed in with AF_UNIX filled in. For unix domain sockets, this
  105. structure is normally what you need for the arguments in bind(),
  106. connect(), and send(), and is also returned by getpeername(),
  107. getsockname() and recv().
  108. =item unpack_sockaddr_un SOCKADDR_UN
  109. Takes a sockaddr_un structure (as returned by pack_sockaddr_un())
  110. and returns the pathname. Will croak if the structure does not
  111. have AF_UNIX in the right place.
  112. =back
  113. =cut
  114. use Carp;
  115. require Exporter;
  116. require DynaLoader;
  117. @ISA = qw(Exporter DynaLoader);
  118. @EXPORT = qw(
  119. inet_aton inet_ntoa pack_sockaddr_in unpack_sockaddr_in
  120. pack_sockaddr_un unpack_sockaddr_un
  121. sockaddr_in sockaddr_un
  122. INADDR_ANY INADDR_BROADCAST INADDR_LOOPBACK INADDR_NONE
  123. AF_802
  124. AF_APPLETALK
  125. AF_CCITT
  126. AF_CHAOS
  127. AF_DATAKIT
  128. AF_DECnet
  129. AF_DLI
  130. AF_ECMA
  131. AF_GOSIP
  132. AF_HYLINK
  133. AF_IMPLINK
  134. AF_INET
  135. AF_LAT
  136. AF_MAX
  137. AF_NBS
  138. AF_NIT
  139. AF_NS
  140. AF_OSI
  141. AF_OSINET
  142. AF_PUP
  143. AF_SNA
  144. AF_UNIX
  145. AF_UNSPEC
  146. AF_X25
  147. MSG_CTLFLAGS
  148. MSG_CTLIGNORE
  149. MSG_CTRUNC
  150. MSG_DONTROUTE
  151. MSG_DONTWAIT
  152. MSG_EOF
  153. MSG_EOR
  154. MSG_ERRQUEUE
  155. MSG_FIN
  156. MSG_MAXIOVLEN
  157. MSG_NOSIGNAL
  158. MSG_OOB
  159. MSG_PEEK
  160. MSG_PROXY
  161. MSG_RST
  162. MSG_SYN
  163. MSG_TRUNC
  164. MSG_URG
  165. MSG_WAITALL
  166. PF_802
  167. PF_APPLETALK
  168. PF_CCITT
  169. PF_CHAOS
  170. PF_DATAKIT
  171. PF_DECnet
  172. PF_DLI
  173. PF_ECMA
  174. PF_GOSIP
  175. PF_HYLINK
  176. PF_IMPLINK
  177. PF_INET
  178. PF_LAT
  179. PF_MAX
  180. PF_NBS
  181. PF_NIT
  182. PF_NS
  183. PF_OSI
  184. PF_OSINET
  185. PF_PUP
  186. PF_SNA
  187. PF_UNIX
  188. PF_UNSPEC
  189. PF_X25
  190. SCM_CONNECT
  191. SCM_CREDENTIALS
  192. SCM_CREDS
  193. SCM_RIGHTS
  194. SCM_TIMESTAMP
  195. SOCK_DGRAM
  196. SOCK_RAW
  197. SOCK_RDM
  198. SOCK_SEQPACKET
  199. SOCK_STREAM
  200. SOL_SOCKET
  201. SOMAXCONN
  202. SO_ACCEPTCONN
  203. SO_BROADCAST
  204. SO_DEBUG
  205. SO_DONTLINGER
  206. SO_DONTROUTE
  207. SO_ERROR
  208. SO_KEEPALIVE
  209. SO_LINGER
  210. SO_OOBINLINE
  211. SO_RCVBUF
  212. SO_RCVLOWAT
  213. SO_RCVTIMEO
  214. SO_REUSEADDR
  215. SO_SNDBUF
  216. SO_SNDLOWAT
  217. SO_SNDTIMEO
  218. SO_TYPE
  219. SO_USELOOPBACK
  220. );
  221. @EXPORT_OK = qw(CR LF CRLF $CR $LF $CRLF);
  222. %EXPORT_TAGS = (
  223. crlf => [qw(CR LF CRLF $CR $LF $CRLF)],
  224. all => [@EXPORT, @EXPORT_OK],
  225. );
  226. BEGIN {
  227. sub CR () {"\015"}
  228. sub LF () {"\012"}
  229. sub CRLF () {"\015\012"}
  230. }
  231. *CR = \CR();
  232. *LF = \LF();
  233. *CRLF = \CRLF();
  234. sub sockaddr_in {
  235. if (@_ == 6 && !wantarray) { # perl5.001m compat; use this && die
  236. my($af, $port, @quad) = @_;
  237. carp "6-ARG sockaddr_in call is deprecated" if $^W;
  238. pack_sockaddr_in($port, inet_aton(join('.', @quad)));
  239. } elsif (wantarray) {
  240. croak "usage: (port,iaddr) = sockaddr_in(sin_sv)" unless @_ == 1;
  241. unpack_sockaddr_in(@_);
  242. } else {
  243. croak "usage: sin_sv = sockaddr_in(port,iaddr))" unless @_ == 2;
  244. pack_sockaddr_in(@_);
  245. }
  246. }
  247. sub sockaddr_un {
  248. if (wantarray) {
  249. croak "usage: (filename) = sockaddr_un(sun_sv)" unless @_ == 1;
  250. unpack_sockaddr_un(@_);
  251. } else {
  252. croak "usage: sun_sv = sockaddr_un(filename)" unless @_ == 1;
  253. pack_sockaddr_un(@_);
  254. }
  255. }
  256. sub AUTOLOAD {
  257. my($constname);
  258. ($constname = $AUTOLOAD) =~ s/.*:://;
  259. my $val = constant($constname, @_ ? $_[0] : 0);
  260. if ($! != 0) {
  261. my ($pack,$file,$line) = caller;
  262. croak "Your vendor has not defined Socket macro $constname, used";
  263. }
  264. eval "sub $AUTOLOAD { $val }";
  265. goto &$AUTOLOAD;
  266. }
  267. bootstrap Socket $VERSION;
  268. 1;