home *** CD-ROM | disk | FTP | other *** search
/ CLIX - Fazer Clix Custa Nix / CLIX-CD.cdr / mac / lib / Socket.pm < prev    next >
Text File  |  1998-04-04  |  12KB  |  393 lines

  1. package Socket;
  2.  
  3. use vars qw($VERSION @ISA @EXPORT);
  4. $VERSION = "1.6";
  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');
  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. In addition, some structure manipulation functions are available:
  49.  
  50. =over
  51.  
  52. =item inet_aton HOSTNAME
  53.  
  54. Takes a string giving the name of a host, and translates that
  55. to the 4-byte string (structure). Takes arguments of both
  56. the 'rtfm.mit.edu' type and '18.181.0.24'. If the host name
  57. cannot be resolved, returns undef. For multi-homed hosts (hosts
  58. with more than one address), the first address found is returned.
  59.  
  60. =item inet_ntoa IP_ADDRESS
  61.  
  62. Takes a four byte ip address (as returned by inet_aton())
  63. and translates it into a string of the form 'd.d.d.d'
  64. where the 'd's are numbers less than 256 (the normal
  65. readable four dotted number notation for internet addresses).
  66.  
  67. =item INADDR_ANY
  68.  
  69. Note: does not return a number, but a packed string.
  70.  
  71. Returns the 4-byte wildcard ip address which specifies any
  72. of the hosts ip addresses. (A particular machine can have
  73. more than one ip address, each address corresponding to
  74. a particular network interface. This wildcard address
  75. allows you to bind to all of them simultaneously.)
  76. Normally equivalent to inet_aton('0.0.0.0').
  77.  
  78. =item INADDR_BROADCAST
  79.  
  80. Note: does not return a number, but a packed string.
  81.  
  82. Returns the 4-byte 'this-lan' ip broadcast address.
  83. This can be useful for some protocols to solicit information
  84. from all servers on the same LAN cable.
  85. Normally equivalent to inet_aton('255.255.255.255').
  86.  
  87. =item INADDR_LOOPBACK
  88.  
  89. Note - does not return a number.
  90.  
  91. Returns the 4-byte loopback address. Normally equivalent
  92. to inet_aton('localhost').
  93.  
  94. =item INADDR_NONE
  95.  
  96. Note - does not return a number.
  97.  
  98. Returns the 4-byte 'invalid' ip address. Normally equivalent
  99. to inet_aton('255.255.255.255').
  100.  
  101. =item sockaddr_in PORT, ADDRESS
  102.  
  103. =item sockaddr_in SOCKADDR_IN
  104.  
  105. In an array context, unpacks its SOCKADDR_IN argument and returns an array
  106. consisting of (PORT, ADDRESS).  In a scalar context, packs its (PORT,
  107. ADDRESS) arguments as a SOCKADDR_IN and returns it.  If this is confusing,
  108. use pack_sockaddr_in() and unpack_sockaddr_in() explicitly.
  109.  
  110. =item pack_sockaddr_in PORT, IP_ADDRESS
  111.  
  112. Takes two arguments, a port number and a 4 byte IP_ADDRESS (as returned by
  113. inet_aton()). Returns the sockaddr_in structure with those arguments
  114. packed in with AF_INET filled in.  For internet domain sockets, this
  115. structure is normally what you need for the arguments in bind(),
  116. connect(), and send(), and is also returned by getpeername(),
  117. getsockname() and recv().
  118.  
  119. =item unpack_sockaddr_in SOCKADDR_IN
  120.  
  121. Takes a sockaddr_in structure (as returned by pack_sockaddr_in()) and
  122. returns an array of two elements: the port and the 4-byte ip-address.
  123. Will croak if the structure does not have AF_INET in the right place.
  124.  
  125. =item sockaddr_un PATHNAME
  126.  
  127. =item sockaddr_un SOCKADDR_UN
  128.  
  129. In an array context, unpacks its SOCKADDR_UN argument and returns an array
  130. consisting of (PATHNAME).  In a scalar context, packs its PATHNAME
  131. arguments as a SOCKADDR_UN and returns it.  If this is confusing, use
  132. pack_sockaddr_un() and unpack_sockaddr_un() explicitly.
  133. These are only supported if your system has E<lt>F<sys/un.h>E<gt>.
  134.  
  135. =item pack_sockaddr_un PATH
  136.  
  137. Takes one argument, a pathname. Returns the sockaddr_un structure with
  138. that path packed in with AF_UNIX filled in. For unix domain sockets, this
  139. structure is normally what you need for the arguments in bind(),
  140. connect(), and send(), and is also returned by getpeername(),
  141. getsockname() and recv().
  142.  
  143. =item unpack_sockaddr_un SOCKADDR_UN
  144.  
  145. Takes a sockaddr_un structure (as returned by pack_sockaddr_un())
  146. and returns the pathname.  Will croak if the structure does not
  147. have AF_UNIX in the right place.
  148.  
  149. =back
  150.  
  151. A number of additional manipulation functions are available for the Apple Macintosh
  152.  
  153. =item sockaddr_atlk NET, NODE, SOCKET
  154.  
  155. =item sockaddr_atlk SOCKADDR_ATLK
  156.  
  157. In an array context, unpacks its SOCKADDR_ATLK argument and returns an array
  158. consisting of (NET,NODE,SOCKET).  In a scalar context, packs its 
  159. arguments as a SOCKADDR_ATLK and returns it.  If this is confusing, use
  160. pack_sockaddr_atlk() and unpack_sockaddr_atlk() explicitly.
  161.  
  162. =item pack_sockaddr_atlk NET, NODE, SOCKET
  163.  
  164. Returns the sockaddr_atlk structure withthe arguments packed in with AF_APPLETALK
  165. filled in. For AppleTalk (ADSP) sockets, this structure is normally what you need 
  166. for the arguments in bind(), connect(), and send(), and is also returned by 
  167. getpeername(), getsockname() and recv().
  168.  
  169. =item unpack_sockaddr_atlk SOCKADDR_ATLK
  170.  
  171. Takes a sockaddr_atlk structure (as returned by pack_sockaddr_atlk())
  172. and returns an array of three elements: the network number, node number, and socket
  173. number.  Will croak if the structure does not have AF_APPLETALK in the right place.
  174.  
  175. =item sockaddr_atlk_sym OBJECT, TYPE, ZONE
  176.  
  177. In an array context, croaks.  In a scalar context, packs its 
  178. arguments as a SOCKADDR_ATLK_SYM and returns it.  If this is confusing, use
  179. pack_sockaddr_atlk_sym() explicitly.
  180.  
  181. =item pack_sockaddr_atlk_sym OBJECT, TYPE, ZONE
  182.  
  183. Returns the sockaddr_atlk_sym structure with the arguments packed in with 
  184. ATALK_SYMADDR filled in. For AppleTalk (ADSP) sockets, you can use this
  185. structure for the arguments in bind() and connect(). No socket calls ever
  186. return a sockaddr_atlk_sym, they always return a numeric sockaddr_atlk
  187. instead.
  188.  
  189. =item sockaddr_ppc LOC_SEL, OBJ, TYPE, ZONE, SCRIPT, NAME, PORTSEL, (TYPE | TYPE, CREATOR) 
  190.  
  191. =item sockaddr_ppc SOCKADDR_PPC
  192.  
  193. In an array context, unpacks its SOCKADDR_PPC argument and returns an array
  194. consisting of (LOC_SEL, OBJ, TYPE, ZONE, SCRIPT, NAME, PORTSEL, (TYPE | TYPE, 
  195. CREATOR)).  In a scalar context, packs its  arguments as a SOCKADDR_PPC and 
  196. returns it.  If this is confusing, use pack_sockaddr_ppc() and 
  197. unpack_sockaddr_ppc() explicitly.
  198.  
  199. =item pack_sockaddr_ppc LOC_SEL, OBJ, TYPE, ZONE, SCRIPT, NAME, PORTSEL, (TYPE | TYPE, CREATOR)
  200.  
  201. Returns the sockaddr_ppc structure withthe arguments packed in with AF_PPC
  202. filled in. For Process-to-Process Communication toolbox sockets, this structure 
  203. is normally what you need  for the arguments in bind(), connect(), and send(), 
  204. and is also returned by getpeername(), getsockname() and recv().
  205.  
  206. =item unpack_sockaddr_ppc SOCKADDR_PPC
  207.  
  208. Takes a sockaddr_ppc structure (as returned by pack_sockaddr_ppc())
  209. and returns an array of 8 or 9 elements: the location variant selector,
  210. location object, type, and zone strings, the port name script code, the port 
  211. name, the port variant selector, and the port type or creator and file
  212. type. Will croak if the structure does not have AF_PPC in the right place.
  213.  
  214. =back
  215.  
  216. =cut
  217.  
  218. use Carp;
  219.  
  220. require Exporter;
  221. require DynaLoader;
  222. @ISA = qw(Exporter DynaLoader);
  223. @EXPORT = qw(
  224.     inet_aton inet_ntoa pack_sockaddr_in unpack_sockaddr_in
  225.     pack_sockaddr_un unpack_sockaddr_un
  226.     sockaddr_in sockaddr_un
  227.     sockaddr_atlk sockaddr_atlk_sym  sockaddr_ppc
  228.     pack_sockaddr_atlk pack_sockaddr_atlk_sym pack_sockaddr_ppc
  229.     unpack_sockaddr_atlk unpack_sockaddr_ppc
  230.     INADDR_ANY INADDR_BROADCAST INADDR_LOOPBACK INADDR_NONE
  231.     AF_802
  232.     AF_APPLETALK
  233.     AF_CCITT
  234.     AF_CHAOS
  235.     AF_DATAKIT
  236.     AF_DECnet
  237.     AF_DLI
  238.     AF_ECMA
  239.     AF_GOSIP
  240.     AF_HYLINK
  241.     AF_IMPLINK
  242.     AF_INET
  243.     AF_LAT
  244.     AF_MAX
  245.     AF_NBS
  246.     AF_NIT
  247.     AF_NS
  248.     AF_OSI
  249.     AF_OSINET
  250.     AF_PUP
  251.     AF_SNA
  252.     AF_UNIX
  253.     AF_UNSPEC
  254.     AF_X25
  255.     MSG_DONTROUTE
  256.     MSG_MAXIOVLEN
  257.     MSG_OOB
  258.     MSG_PEEK
  259.     PF_802
  260.     PF_APPLETALK
  261.     PF_CCITT
  262.     PF_CHAOS
  263.     PF_DATAKIT
  264.     PF_DECnet
  265.     PF_DLI
  266.     PF_ECMA
  267.     PF_GOSIP
  268.     PF_HYLINK
  269.     PF_IMPLINK
  270.     PF_INET
  271.     PF_LAT
  272.     PF_MAX
  273.     PF_NBS
  274.     PF_NIT
  275.     PF_NS
  276.     PF_OSI
  277.     PF_OSINET
  278.     PF_PUP
  279.     PF_SNA
  280.     PF_UNIX
  281.     PF_UNSPEC
  282.     PF_X25
  283.     SOCK_DGRAM
  284.     SOCK_RAW
  285.     SOCK_RDM
  286.     SOCK_SEQPACKET
  287.     SOCK_STREAM
  288.     SOL_SOCKET
  289.     SOMAXCONN
  290.     SO_ACCEPTCONN
  291.     SO_BROADCAST
  292.     SO_DEBUG
  293.     SO_DONTLINGER
  294.     SO_DONTROUTE
  295.     SO_ERROR
  296.     SO_KEEPALIVE
  297.     SO_LINGER
  298.     SO_OOBINLINE
  299.     SO_RCVBUF
  300.     SO_RCVLOWAT
  301.     SO_RCVTIMEO
  302.     SO_REUSEADDR
  303.     SO_SNDBUF
  304.     SO_SNDLOWAT
  305.     SO_SNDTIMEO
  306.     SO_TYPE
  307.     SO_USELOOPBACK
  308. );
  309.  
  310. if ($^O eq "MacOS") {   
  311.     push @EXPORT, qw(
  312.         AF_PPC
  313.         AF_PAP
  314.         ATALK_SYMADDR
  315.         PF_PPC
  316.         PF_PAP
  317.         ppcNoLocation
  318.         ppcNBPLocation
  319.         ppcNBPTypeLocation
  320.         ppcByCreatorAndType
  321.         ppcByString
  322.     );
  323. }
  324.  
  325. sub sockaddr_in {
  326.     if (@_ == 6 && !wantarray) { # perl5.001m compat; use this && die
  327.     my($af, $port, @quad) = @_;
  328.     carp "6-ARG sockaddr_in call is deprecated" if $^W;
  329.     pack_sockaddr_in($port, inet_aton(join('.', @quad)));
  330.     } elsif (wantarray) {
  331.     croak "usage:   (port,iaddr) = sockaddr_in(sin_sv)" unless @_ == 1;
  332.         unpack_sockaddr_in(@_);
  333.     } else {
  334.     croak "usage:   sin_sv = sockaddr_in(port,iaddr))" unless @_ == 2;
  335.         pack_sockaddr_in(@_);
  336.     }
  337. }
  338.  
  339. sub sockaddr_un {
  340.     if (wantarray) {
  341.     croak "usage:   (filename) = sockaddr_un(sun_sv)" unless @_ == 1;
  342.         unpack_sockaddr_un(@_);
  343.     } else {
  344.     croak "usage:   sun_sv = sockaddr_un(filename)" unless @_ == 1;
  345.         pack_sockaddr_un(@_);
  346.     }
  347. }
  348.  
  349. sub sockaddr_atlk {
  350.     if (wantarray) {
  351.     croak "usage:   (filename) = sockaddr_atlk(satlk_sv)" unless @_ == 1;
  352.         unpack_sockaddr_atlk(@_);
  353.     } else {
  354.     croak "usage:   satlk_sv = sockaddr_atlk(net,node,socket)" unless @_ == 3;
  355.         pack_sockaddr_atlk(@_);
  356.     }
  357. }
  358.  
  359. sub sockaddr_atlk_sym {
  360.     if (wantarray) {
  361.     croak "Cannot unpack atlk_sym";
  362.     } else {
  363.     croak "usage:   satlk_sym_sv = sockaddr_atlk_sym(obj,type,zone)" unless @_ == 3;
  364.         pack_sockaddr_atlk_sym(@_);
  365.     }
  366. }
  367.  
  368. sub sockaddr_ppc {
  369.     if (wantarray) {
  370.     croak "usage:   (filename) = sockaddr_ppc(sappc_sv)" unless @_ == 1;
  371.         unpack_sockaddr_ppc(@_);
  372.     } else {
  373.     croak "usage:   sppc_sv = sockaddr_ppc(type,name,porttype)" unless @_ == 3;
  374.         pack_sockaddr_ppc(@_);
  375.     }
  376. }
  377.  
  378. sub AUTOLOAD {
  379.     my($constname);
  380.     ($constname = $AUTOLOAD) =~ s/.*:://;
  381.     my $val = constant($constname, @_ ? $_[0] : 0);
  382.     if ($! != 0) {
  383.     my ($pack,$file,$line) = caller;
  384.     croak "Your vendor has not defined Socket macro $constname, used";
  385.     }
  386.     eval "sub $AUTOLOAD { $val }";
  387.     goto &$AUTOLOAD;
  388. }
  389.  
  390. bootstrap Socket $VERSION;
  391.  
  392. 1;
  393.