Leaked source code of windows server 2003
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.

453 lines
11 KiB

  1. package Socket;
  2. our($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
  3. $VERSION = "1.72";
  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 a list 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 a list 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. use warnings::register;
  116. require Exporter;
  117. use XSLoader ();
  118. @ISA = qw(Exporter);
  119. @EXPORT = qw(
  120. inet_aton inet_ntoa pack_sockaddr_in unpack_sockaddr_in
  121. pack_sockaddr_un unpack_sockaddr_un
  122. sockaddr_in sockaddr_un
  123. INADDR_ANY INADDR_BROADCAST INADDR_LOOPBACK INADDR_NONE
  124. AF_802
  125. AF_APPLETALK
  126. AF_CCITT
  127. AF_CHAOS
  128. AF_DATAKIT
  129. AF_DECnet
  130. AF_DLI
  131. AF_ECMA
  132. AF_GOSIP
  133. AF_HYLINK
  134. AF_IMPLINK
  135. AF_INET
  136. AF_LAT
  137. AF_MAX
  138. AF_NBS
  139. AF_NIT
  140. AF_NS
  141. AF_OSI
  142. AF_OSINET
  143. AF_PUP
  144. AF_SNA
  145. AF_UNIX
  146. AF_UNSPEC
  147. AF_X25
  148. IOV_MAX
  149. MSG_BCAST
  150. MSG_CTLFLAGS
  151. MSG_CTLIGNORE
  152. MSG_CTRUNC
  153. MSG_DONTROUTE
  154. MSG_DONTWAIT
  155. MSG_EOF
  156. MSG_EOR
  157. MSG_ERRQUEUE
  158. MSG_FIN
  159. MSG_MAXIOVLEN
  160. MSG_MCAST
  161. MSG_NOSIGNAL
  162. MSG_OOB
  163. MSG_PEEK
  164. MSG_PROXY
  165. MSG_RST
  166. MSG_SYN
  167. MSG_TRUNC
  168. MSG_URG
  169. MSG_WAITALL
  170. PF_802
  171. PF_APPLETALK
  172. PF_CCITT
  173. PF_CHAOS
  174. PF_DATAKIT
  175. PF_DECnet
  176. PF_DLI
  177. PF_ECMA
  178. PF_GOSIP
  179. PF_HYLINK
  180. PF_IMPLINK
  181. PF_INET
  182. PF_LAT
  183. PF_MAX
  184. PF_NBS
  185. PF_NIT
  186. PF_NS
  187. PF_OSI
  188. PF_OSINET
  189. PF_PUP
  190. PF_SNA
  191. PF_UNIX
  192. PF_UNSPEC
  193. PF_X25
  194. SCM_CONNECT
  195. SCM_CREDENTIALS
  196. SCM_CREDS
  197. SCM_RIGHTS
  198. SCM_TIMESTAMP
  199. SHUT_RD
  200. SHUT_RDWR
  201. SHUT_WR
  202. SOCK_DGRAM
  203. SOCK_RAW
  204. SOCK_RDM
  205. SOCK_SEQPACKET
  206. SOCK_STREAM
  207. SOL_SOCKET
  208. SOMAXCONN
  209. SO_ACCEPTCONN
  210. SO_BROADCAST
  211. SO_DEBUG
  212. SO_DONTLINGER
  213. SO_DONTROUTE
  214. SO_ERROR
  215. SO_KEEPALIVE
  216. SO_LINGER
  217. SO_OOBINLINE
  218. SO_RCVBUF
  219. SO_RCVLOWAT
  220. SO_RCVTIMEO
  221. SO_REUSEADDR
  222. SO_REUSEPORT
  223. SO_SNDBUF
  224. SO_SNDLOWAT
  225. SO_SNDTIMEO
  226. SO_TYPE
  227. SO_USELOOPBACK
  228. UIO_MAXIOV
  229. );
  230. @EXPORT_OK = qw(CR LF CRLF $CR $LF $CRLF
  231. IPPROTO_TCP
  232. TCP_KEEPALIVE
  233. TCP_MAXRT
  234. TCP_MAXSEG
  235. TCP_NODELAY
  236. TCP_STDURG);
  237. %EXPORT_TAGS = (
  238. crlf => [qw(CR LF CRLF $CR $LF $CRLF)],
  239. all => [@EXPORT, @EXPORT_OK],
  240. );
  241. BEGIN {
  242. sub CR () {"\015"}
  243. sub LF () {"\012"}
  244. sub CRLF () {"\015\012"}
  245. }
  246. *CR = \CR();
  247. *LF = \LF();
  248. *CRLF = \CRLF();
  249. sub sockaddr_in {
  250. if (@_ == 6 && !wantarray) { # perl5.001m compat; use this && die
  251. my($af, $port, @quad) = @_;
  252. warnings::warn "6-ARG sockaddr_in call is deprecated"
  253. if warnings::enabled();
  254. pack_sockaddr_in($port, inet_aton(join('.', @quad)));
  255. } elsif (wantarray) {
  256. croak "usage: (port,iaddr) = sockaddr_in(sin_sv)" unless @_ == 1;
  257. unpack_sockaddr_in(@_);
  258. } else {
  259. croak "usage: sin_sv = sockaddr_in(port,iaddr))" unless @_ == 2;
  260. pack_sockaddr_in(@_);
  261. }
  262. }
  263. sub sockaddr_un {
  264. if (wantarray) {
  265. croak "usage: (filename) = sockaddr_un(sun_sv)" unless @_ == 1;
  266. unpack_sockaddr_un(@_);
  267. } else {
  268. croak "usage: sun_sv = sockaddr_un(filename)" unless @_ == 1;
  269. pack_sockaddr_un(@_);
  270. }
  271. }
  272. sub INADDR_ANY ();
  273. sub INADDR_BROADCAST ();
  274. sub INADDR_LOOPBACK ();
  275. sub INADDR_LOOPBACK ();
  276. sub AF_802 ();
  277. sub AF_APPLETALK ();
  278. sub AF_CCITT ();
  279. sub AF_CHAOS ();
  280. sub AF_DATAKIT ();
  281. sub AF_DECnet ();
  282. sub AF_DLI ();
  283. sub AF_ECMA ();
  284. sub AF_GOSIP ();
  285. sub AF_HYLINK ();
  286. sub AF_IMPLINK ();
  287. sub AF_INET ();
  288. sub AF_LAT ();
  289. sub AF_MAX ();
  290. sub AF_NBS ();
  291. sub AF_NIT ();
  292. sub AF_NS ();
  293. sub AF_OSI ();
  294. sub AF_OSINET ();
  295. sub AF_PUP ();
  296. sub AF_SNA ();
  297. sub AF_UNIX ();
  298. sub AF_UNSPEC ();
  299. sub AF_X25 ();
  300. sub IOV_MAX ();
  301. sub MSG_BCAST ();
  302. sub MSG_CTLFLAGS ();
  303. sub MSG_CTLIGNORE ();
  304. sub MSG_CTRUNC ();
  305. sub MSG_DONTROUTE ();
  306. sub MSG_DONTWAIT ();
  307. sub MSG_EOF ();
  308. sub MSG_EOR ();
  309. sub MSG_ERRQUEUE ();
  310. sub MSG_FIN ();
  311. sub MSG_MAXIOVLEN ();
  312. sub MSG_MCAST ();
  313. sub MSG_NOSIGNAL ();
  314. sub MSG_OOB ();
  315. sub MSG_PEEK ();
  316. sub MSG_PROXY ();
  317. sub MSG_RST ();
  318. sub MSG_SYN ();
  319. sub MSG_TRUNC ();
  320. sub MSG_URG ();
  321. sub MSG_WAITALL ();
  322. sub PF_802 ();
  323. sub PF_APPLETALK ();
  324. sub PF_CCITT ();
  325. sub PF_CHAOS ();
  326. sub PF_DATAKIT ();
  327. sub PF_DECnet ();
  328. sub PF_DLI ();
  329. sub PF_ECMA ();
  330. sub PF_GOSIP ();
  331. sub PF_HYLINK ();
  332. sub PF_IMPLINK ();
  333. sub PF_INET ();
  334. sub PF_LAT ();
  335. sub PF_MAX ();
  336. sub PF_NBS ();
  337. sub PF_NIT ();
  338. sub PF_NS ();
  339. sub PF_OSI ();
  340. sub PF_OSINET ();
  341. sub PF_PUP ();
  342. sub PF_SNA ();
  343. sub PF_UNIX ();
  344. sub PF_UNSPEC ();
  345. sub PF_X25 ();
  346. sub SCM_CONNECT ();
  347. sub SCM_CREDENTIALS ();
  348. sub SCM_CREDS ();
  349. sub SCM_RIGHTS ();
  350. sub SCM_TIMESTAMP ();
  351. sub SHUT_RD ();
  352. sub SHUT_RDWR ();
  353. sub SHUT_WR ();
  354. sub SOCK_DGRAM ();
  355. sub SOCK_RAW ();
  356. sub SOCK_RDM ();
  357. sub SOCK_SEQPACKET ();
  358. sub SOCK_STREAM ();
  359. sub SOL_SOCKET ();
  360. sub SOMAXCONN ();
  361. sub SO_ACCEPTCONN ();
  362. sub SO_BROADCAST ();
  363. sub SO_DEBUG ();
  364. sub SO_DONTLINGER ();
  365. sub SO_DONTROUTE ();
  366. sub SO_ERROR ();
  367. sub SO_KEEPALIVE ();
  368. sub SO_LINGER ();
  369. sub SO_OOBINLINE ();
  370. sub SO_RCVBUF ();
  371. sub SO_RCVLOWAT ();
  372. sub SO_RCVTIMEO ();
  373. sub SO_REUSEADDR ();
  374. sub SO_SNDBUF ();
  375. sub SO_SNDLOWAT ();
  376. sub SO_SNDTIMEO ();
  377. sub SO_TYPE ();
  378. sub SO_USELOOPBACK ();
  379. sub UIO_MAXIOV ();
  380. sub AUTOLOAD {
  381. my($constname);
  382. ($constname = $AUTOLOAD) =~ s/.*:://;
  383. my $val = constant($constname, @_ ? $_[0] : 0);
  384. if ($! != 0) {
  385. my ($pack,$file,$line) = caller;
  386. croak "Your vendor has not defined Socket macro $constname, used";
  387. }
  388. eval "sub $AUTOLOAD () { $val }";
  389. goto &$AUTOLOAD;
  390. }
  391. XSLoader::load 'Socket', $VERSION;
  392. 1;