home *** CD-ROM | disk | FTP | other *** search
/ c't freeware shareware 1997 / CT_SW_97.ISO / pc / software / entwickl / win95 / pw32i306.exe / lib / socket.pm < prev    next >
Text File  |  1997-01-21  |  7KB  |  273 lines

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