home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 10 / AU_CD10.iso / Updates / Perl / Non-RPC / !Perl / lib / zip / Socket.pm < prev    next >
Text File  |  1998-11-30  |  8KB  |  328 lines

  1. package Socket;
  2.  
  3. use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  4. $VERSION = "1.7";
  5.  
  6. =head1 NAME
  7.  
  8. Socket, sockaddr_in, sockaddr_un, inet_aton, inet_ntoa - load the C socket.h defines and structure manipulators 
  9.  
  10. =head1 SYNOPSIS
  11.  
  12.     use Socket;
  13.  
  14.     $proto = getprotobyname('udp');
  15.     socket(Socket_Handle, PF_INET, SOCK_DGRAM, $proto);
  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);
  23.     $port = getservbyname('smtp', 'tcp');
  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);
  35.     unlink('/tmp/usock');
  36.     $sun = sockaddr_un('/tmp/usock');
  37.     connect(Socket_Handle,$sun);
  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 
  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.
  47.  
  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.  
  57. In addition, some structure manipulation functions are available:
  58.  
  59. =over
  60.  
  61. =item inet_aton HOSTNAME
  62.  
  63. Takes a string giving the name of a host, and translates that
  64. to the 4-byte string (structure). Takes arguments of both
  65. 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
  67. with more than one address), the first address found is returned.
  68.  
  69. =item inet_ntoa IP_ADDRESS
  70.  
  71. Takes a four byte ip address (as returned by inet_aton())
  72. and translates it into a string of the form 'd.d.d.d'
  73. where the 'd's are numbers less than 256 (the normal
  74. readable four dotted number notation for internet addresses).
  75.  
  76. =item INADDR_ANY
  77.  
  78. Note: does not return a number, but a packed string.
  79.  
  80. Returns the 4-byte wildcard ip address which specifies any
  81. of the hosts ip addresses. (A particular machine can have
  82. more than one ip address, each address corresponding to
  83. a particular network interface. This wildcard address
  84. allows you to bind to all of them simultaneously.)
  85. Normally equivalent to inet_aton('0.0.0.0').
  86.  
  87. =item INADDR_BROADCAST
  88.  
  89. Note: does not return a number, but a packed string.
  90.  
  91. Returns the 4-byte 'this-lan' ip broadcast address.
  92. This can be useful for some protocols to solicit information
  93. from all servers on the same LAN cable.
  94. Normally equivalent to inet_aton('255.255.255.255').
  95.  
  96. =item INADDR_LOOPBACK
  97.  
  98. Note - does not return a number.
  99.  
  100. Returns the 4-byte loopback address. Normally equivalent
  101. to inet_aton('localhost').
  102.  
  103. =item INADDR_NONE
  104.  
  105. Note - does not return a number.
  106.  
  107. Returns the 4-byte 'invalid' ip address. Normally equivalent
  108. to inet_aton('255.255.255.255').
  109.  
  110. =item sockaddr_in PORT, ADDRESS
  111.  
  112. =item sockaddr_in SOCKADDR_IN
  113.  
  114. In an array context, unpacks its SOCKADDR_IN argument and returns an array
  115. consisting of (PORT, ADDRESS).  In a scalar context, packs its (PORT,
  116. ADDRESS) arguments as a SOCKADDR_IN and returns it.  If this is confusing,
  117. use pack_sockaddr_in() and unpack_sockaddr_in() explicitly.
  118.  
  119. =item pack_sockaddr_in PORT, IP_ADDRESS
  120.  
  121. Takes two arguments, a port number and a 4 byte IP_ADDRESS (as returned by
  122. inet_aton()). Returns the sockaddr_in structure with those arguments
  123. packed in with AF_INET filled in.  For internet domain sockets, this
  124. structure is normally what you need for the arguments in bind(),
  125. connect(), and send(), and is also returned by getpeername(),
  126. getsockname() and recv().
  127.  
  128. =item unpack_sockaddr_in SOCKADDR_IN
  129.  
  130. Takes a sockaddr_in structure (as returned by pack_sockaddr_in()) and
  131. returns an array of two elements: the port and the 4-byte ip-address.
  132. Will croak if the structure does not have AF_INET in the right place.
  133.  
  134. =item sockaddr_un PATHNAME
  135.  
  136. =item sockaddr_un SOCKADDR_UN
  137.  
  138. In an array context, unpacks its SOCKADDR_UN argument and returns an array
  139. consisting of (PATHNAME).  In a scalar context, packs its PATHNAME
  140. arguments as a SOCKADDR_UN and returns it.  If this is confusing, use
  141. pack_sockaddr_un() and unpack_sockaddr_un() explicitly.
  142. These are only supported if your system has E<lt>F<sys/un.h>E<gt>.
  143.  
  144. =item pack_sockaddr_un PATH
  145.  
  146. Takes one argument, a pathname. Returns the sockaddr_un structure with
  147. that path packed in with AF_UNIX filled in. For unix domain sockets, this
  148. structure is normally what you need for the arguments in bind(),
  149. connect(), and send(), and is also returned by getpeername(),
  150. getsockname() and recv().
  151.  
  152. =item unpack_sockaddr_un SOCKADDR_UN
  153.  
  154. Takes a sockaddr_un structure (as returned by pack_sockaddr_un())
  155. and returns the pathname.  Will croak if the structure does not
  156. have AF_UNIX in the right place.
  157.  
  158. =back
  159.  
  160. =cut
  161.  
  162. use Carp;
  163.  
  164. require Exporter;
  165. require DynaLoader;
  166. @ISA = qw(Exporter DynaLoader);
  167. @EXPORT = qw(
  168.     inet_aton inet_ntoa pack_sockaddr_in unpack_sockaddr_in
  169.     pack_sockaddr_un unpack_sockaddr_un
  170.     sockaddr_in sockaddr_un
  171.     INADDR_ANY INADDR_BROADCAST INADDR_LOOPBACK INADDR_NONE
  172.     AF_802
  173.     AF_APPLETALK
  174.     AF_CCITT
  175.     AF_CHAOS
  176.     AF_DATAKIT
  177.     AF_DECnet
  178.     AF_DLI
  179.     AF_ECMA
  180.     AF_GOSIP
  181.     AF_HYLINK
  182.     AF_IMPLINK
  183.     AF_INET
  184.     AF_LAT
  185.     AF_MAX
  186.     AF_NBS
  187.     AF_NIT
  188.     AF_NS
  189.     AF_OSI
  190.     AF_OSINET
  191.     AF_PUP
  192.     AF_SNA
  193.     AF_UNIX
  194.     AF_UNSPEC
  195.     AF_X25
  196.     MSG_CTLFLAGS
  197.     MSG_CTLIGNORE
  198.     MSG_CTRUNC
  199.     MSG_DONTROUTE
  200.     MSG_DONTWAIT
  201.     MSG_EOF
  202.     MSG_EOR
  203.     MSG_ERRQUEUE
  204.     MSG_FIN
  205.     MSG_MAXIOVLEN
  206.     MSG_NOSIGNAL
  207.     MSG_OOB
  208.     MSG_PEEK
  209.     MSG_PROXY
  210.     MSG_RST
  211.     MSG_SYN
  212.     MSG_TRUNC
  213.     MSG_URG
  214.     MSG_WAITALL
  215.     PF_802
  216.     PF_APPLETALK
  217.     PF_CCITT
  218.     PF_CHAOS
  219.     PF_DATAKIT
  220.     PF_DECnet
  221.     PF_DLI
  222.     PF_ECMA
  223.     PF_GOSIP
  224.     PF_HYLINK
  225.     PF_IMPLINK
  226.     PF_INET
  227.     PF_LAT
  228.     PF_MAX
  229.     PF_NBS
  230.     PF_NIT
  231.     PF_NS
  232.     PF_OSI
  233.     PF_OSINET
  234.     PF_PUP
  235.     PF_SNA
  236.     PF_UNIX
  237.     PF_UNSPEC
  238.     PF_X25
  239.     SCM_CONNECT
  240.     SCM_CREDENTIALS
  241.     SCM_CREDS
  242.     SCM_RIGHTS
  243.     SCM_TIMESTAMP
  244.     SOCK_DGRAM
  245.     SOCK_RAW
  246.     SOCK_RDM
  247.     SOCK_SEQPACKET
  248.     SOCK_STREAM
  249.     SOL_SOCKET
  250.     SOMAXCONN
  251.     SO_ACCEPTCONN
  252.     SO_BROADCAST
  253.     SO_DEBUG
  254.     SO_DONTLINGER
  255.     SO_DONTROUTE
  256.     SO_ERROR
  257.     SO_KEEPALIVE
  258.     SO_LINGER
  259.     SO_OOBINLINE
  260.     SO_RCVBUF
  261.     SO_RCVLOWAT
  262.     SO_RCVTIMEO
  263.     SO_REUSEADDR
  264.     SO_SNDBUF
  265.     SO_SNDLOWAT
  266.     SO_SNDTIMEO
  267.     SO_TYPE
  268.     SO_USELOOPBACK
  269. );
  270.  
  271. @EXPORT_OK = qw(CR LF CRLF $CR $LF $CRLF);
  272.  
  273. %EXPORT_TAGS = (
  274.     crlf    => [qw(CR LF CRLF $CR $LF $CRLF)],
  275.     all     => [@EXPORT, @EXPORT_OK],
  276. );
  277.  
  278. BEGIN {
  279.     sub CR   () {"\015"}
  280.     sub LF   () {"\012"}
  281.     sub CRLF () {"\015\012"}
  282. }
  283.  
  284. *CR   = \CR();
  285. *LF   = \LF();
  286. *CRLF = \CRLF();
  287.  
  288. sub sockaddr_in {
  289.     if (@_ == 6 && !wantarray) { # perl5.001m compat; use this && die
  290.     my($af, $port, @quad) = @_;
  291.     carp "6-ARG sockaddr_in call is deprecated" if $^W;
  292.     pack_sockaddr_in($port, inet_aton(join('.', @quad)));
  293.     } elsif (wantarray) {
  294.     croak "usage:   (port,iaddr) = sockaddr_in(sin_sv)" unless @_ == 1;
  295.         unpack_sockaddr_in(@_);
  296.     } else {
  297.     croak "usage:   sin_sv = sockaddr_in(port,iaddr))" unless @_ == 2;
  298.         pack_sockaddr_in(@_);
  299.     }
  300. }
  301.  
  302. sub sockaddr_un {
  303.     if (wantarray) {
  304.     croak "usage:   (filename) = sockaddr_un(sun_sv)" unless @_ == 1;
  305.         unpack_sockaddr_un(@_);
  306.     } else {
  307.     croak "usage:   sun_sv = sockaddr_un(filename)" unless @_ == 1;
  308.         pack_sockaddr_un(@_);
  309.     }
  310. }
  311.  
  312.  
  313. sub AUTOLOAD {
  314.     my($constname);
  315.     ($constname = $AUTOLOAD) =~ s/.*:://;
  316.     my $val = constant($constname, @_ ? $_[0] : 0);
  317.     if ($! != 0) {
  318.     my ($pack,$file,$line) = caller;
  319.     croak "Your vendor has not defined Socket macro $constname, used";
  320.     }
  321.     eval "sub $AUTOLOAD { $val }";
  322.     goto &$AUTOLOAD;
  323. }
  324.  
  325. bootstrap Socket $VERSION;
  326.  
  327. 1;
  328.