home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / bin / network.pl < prev    next >
Encoding:
Perl Script  |  1999-12-28  |  3.0 KB  |  124 lines

  1. package network;
  2. $version = "950311.5";
  3.  
  4.  
  5.  
  6. sub connect_to
  7. {
  8.     local(*FD, $arg1, $arg2) = @_;
  9.     local($from, $to)   = ($arg1, $arg2); ## for one interpretation.
  10.     local($host, $port) = ($arg1, $arg2); ## for the other
  11.  
  12.     if (defined($to) && length($from)==16 && length($to)==16) {
  13.     } elsif (defined($host)) {
  14.     $to = &get_addr($host, $port);
  15.     return qq/unknown address "$host"/ unless defined $to;
  16.     $from = &my_addr;
  17.     } else {
  18.     return "unknown arguments to network'connect_to";
  19.     }
  20.  
  21.     return "connect_to failed (socket: $!)"  unless &my_inet_socket(*FD);
  22.     return "connect_to failed (bind: $!)"    unless bind(FD, $from);
  23.     return "connect_to failed (connect: $!)" unless connect(FD, $to);
  24.     local($old) = select(FD); $| = 1; select($old);
  25.     undef;
  26. }
  27.  
  28.  
  29.  
  30. sub listen_at
  31. {
  32.     local(*FD, $port) = @_;
  33.     local($empty) = pack('S n a4 x8', 2 ,$port, "\0\0\0\0");
  34.     return "listen_for failed (socket: $!)"  unless &my_inet_socket(*FD);
  35.     return "listen_for failed (bind: $!)"    unless bind(FD, $empty);
  36.     return "listen_for failed (listen: $!)"  unless listen(FD, 5);
  37.     local($old) = select(FD); $| = 1; select($old);
  38.     undef;
  39. }
  40.  
  41.  
  42. sub addr_to_ascii
  43. {
  44.     local($addr) = @_;
  45.     return "bad arg" if length $addr != 16;
  46.     return join('.', unpack("CCCC", (unpack('S n a4 x8', $addr))[2]));
  47. }
  48.  
  49. sub get_addr
  50. {
  51.     local($host, $port) = @_;
  52.     return $addr{$host,$port} if defined $addr{$host,$port};
  53.     local($addr);
  54.  
  55.     if ($host =~ m/^\d+\.\d+\.\d+\.\d+$/)
  56.     {
  57.     $addr = pack("C4", split(/\./, $host));
  58.     }
  59.     elsif ($addr = (gethostbyname($host))[4], !defined $addr)
  60.     {
  61.         local(@lookup) = `nslookup $host 2>&1`;
  62.     if (@lookup)
  63.     {
  64.         local($lookup) = join('', @lookup[2 .. $#lookup]);
  65.         if ($lookup =~ m/^Address:\s*(\d+\.\d+\.\d+\.\d+)/) {
  66.             $addr = pack("C4", split(/\./, $1));
  67.         }
  68.     }
  69.     if (!defined $addr) {
  70.         return undef;
  71.     }
  72.     }
  73.     $addr{$host,$port} = pack('S n a4 x8', 2 ,$port, $addr);
  74. }
  75.  
  76.  
  77. sub my_addr
  78. {
  79.     local(@x) = gethostbyname('localhost');
  80.     local(@y) = gethostbyname($x[0]);
  81.         return pack('S n a4 x8', 2 ,0, $y[4]);
  82. }
  83.  
  84.  
  85. sub my_inet_socket
  86. {
  87.     local(*FD) = @_;
  88.     local($socket);
  89.  
  90.     if (!defined $socket_values_queried)
  91.     {
  92.     if (!defined &main'_SYS_SOCKET_H_) {
  93.       eval 'package main;
  94.             local($^W) = 0;
  95.                 require("sys/socket.ph")||require("socket.ph");';
  96.     }
  97.  
  98.     $PF_INET     = defined &main'PF_INET ? &main'PF_INET : 2;
  99.     $AF_NS       = defined &main'AF_NS   ? &main'AF_NS   : 6;
  100.     $SOCK_STREAM = &main'SOCK_STREAM if defined &main'SOCK_STREAM;
  101.  
  102.     $socket_values_queried = 1;
  103.     }
  104.  
  105.     if (defined $SOCK_STREAM) {
  106.     $socket = socket(FD, $PF_INET, $SOCK_STREAM, $AF_NS);
  107.     } else {
  108.     $socket = socket(FD, $PF_INET, 1, $AF_NS);
  109.     if ($socket) {
  110.         $SOCK_STREAM = 1; ## got it.
  111.     } elsif ($! =~ m/not supported/i) {
  112.         $socket = socket(FD, $PF_INET, $SOCK_STREAM = 2, $AF_NS);
  113.     }
  114.     }
  115.     $socket;
  116. }
  117.  
  118. sub dummy {
  119.   1 || $version || &dummy;
  120. }
  121.  
  122. 1;
  123. __END__
  124.