home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / java / net / PlainSocketImpl.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-01-27  |  4.2 KB  |  219 lines

  1. package java.net;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.FileDescriptor;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8.  
  9. class PlainSocketImpl extends SocketImpl {
  10.    private static final int SOCKS_PROTO_VERS = 4;
  11.    private static final int SOCKS_REPLY_VERS = 4;
  12.    private static final int COMMAND_CONNECT = 1;
  13.    private static final int COMMAND_BIND = 2;
  14.    private static final int REQUEST_GRANTED = 90;
  15.    private static final int REQUEST_REJECTED = 91;
  16.    private static final int REQUEST_REJECTED_NO_IDENTD = 92;
  17.    private static final int REQUEST_REJECTED_DIFF_IDENTS = 93;
  18.    public static final String socksServerProp = "socksProxyHost";
  19.    public static final String socksPortProp = "socksProxyPort";
  20.    public static final String socksDefaultPortStr = "1080";
  21.  
  22.    protected synchronized void create(boolean var1) throws IOException {
  23.       super.fd = new FileDescriptor();
  24.       this.socketCreate(var1);
  25.    }
  26.  
  27.    protected void connect(String var1, int var2) throws UnknownHostException, IOException {
  28.       Object var3 = null;
  29.  
  30.       try {
  31.          InetAddress var4 = InetAddress.getByName(var1);
  32.  
  33.          try {
  34.             this.connectToAddress(var4, var2);
  35.             return;
  36.          } catch (IOException var8) {
  37.             var3 = var8;
  38.          }
  39.       } catch (UnknownHostException var9) {
  40.          var3 = var9;
  41.       }
  42.  
  43.       this.socketClose();
  44.       throw var3;
  45.    }
  46.  
  47.    protected void connect(InetAddress var1, int var2) throws IOException {
  48.       super.port = var2;
  49.       super.address = var1;
  50.  
  51.       try {
  52.          this.connectToAddress(var1, var2);
  53.       } catch (IOException var5) {
  54.          this.socketClose();
  55.          throw var5;
  56.       }
  57.    }
  58.  
  59.    private void connectToAddress(InetAddress var1, int var2) throws IOException {
  60.       if (this.usingSocks()) {
  61.          this.doSOCKSConnect(var1, var2);
  62.       } else {
  63.          this.doConnect(var1, var2);
  64.       }
  65.  
  66.    }
  67.  
  68.    private void doSOCKSConnect(InetAddress var1, int var2) throws IOException {
  69.       this.connectToSocksServer();
  70.       this.sendSOCKSCommandPacket(1, var1, var2);
  71.       int var3 = this.getSOCKSReply();
  72.       switch (var3) {
  73.          case 90:
  74.             return;
  75.          case 91:
  76.          case 92:
  77.             throw new SocketException("SOCKS server cannot conect to identd");
  78.          case 93:
  79.             throw new SocketException("User name does not match identd name");
  80.          default:
  81.       }
  82.    }
  83.  
  84.    private int getSOCKSReply() throws IOException {
  85.       InputStream var1 = this.getInputStream();
  86.       byte[] var2 = new byte[8];
  87.       if (var1.read(var2) != var2.length) {
  88.          throw new SocketException("Malformed reply from SOCKS server");
  89.       } else if (var2[0] != 0) {
  90.          throw new SocketException("Malformed reply from SOCKS server");
  91.       } else {
  92.          return var2[1];
  93.       }
  94.    }
  95.  
  96.    private void connectToSocksServer() throws IOException {
  97.       String var1 = System.getProperty("s\u0000o\u0000c\u0000k\u0000s\u0000P\u0000r\u0000o\u0000x\u0000y\u0000H\u0000o\u0000s\u0000t\u0000");
  98.       if (var1 != null) {
  99.          InetAddress var2 = InetAddress.getByName(var1);
  100.          String var3 = System.getProperty("s\u0000o\u0000c\u0000k\u0000s\u0000P\u0000r\u0000o\u0000x\u0000y\u0000P\u0000o\u0000r\u0000t\u0000", "1\u00000\u00008\u00000\u0000");
  101.  
  102.          int var4;
  103.          try {
  104.             var4 = Integer.parseInt(var3);
  105.          } catch (Exception var5) {
  106.             throw new SocketException("Bad port number format");
  107.          }
  108.  
  109.          this.doConnect(var2, var4);
  110.       }
  111.    }
  112.  
  113.    private void doConnect(InetAddress var1, int var2) throws IOException {
  114.       Object var3 = null;
  115.       int var4 = 0;
  116.  
  117.       while(true) {
  118.          try {
  119.             this.socketConnect(var1, var2);
  120.             return;
  121.          } catch (ProtocolException var7) {
  122.             this.socketClose();
  123.             super.fd = new FileDescriptor();
  124.             this.socketCreate(true);
  125.             ++var4;
  126.             if (var4 >= 3) {
  127.                this.socketClose();
  128.                throw var7;
  129.             }
  130.          } catch (IOException var8) {
  131.             this.socketClose();
  132.             throw var8;
  133.          }
  134.       }
  135.    }
  136.  
  137.    private void sendSOCKSCommandPacket(int var1, InetAddress var2, int var3) throws IOException {
  138.       byte[] var4 = this.makeCommandPacket(var1, var2, var3);
  139.       OutputStream var5 = this.getOutputStream();
  140.       var5.write(var4);
  141.    }
  142.  
  143.    private byte[] makeCommandPacket(int var1, InetAddress var2, int var3) {
  144.       ByteArrayOutputStream var4 = new ByteArrayOutputStream(9);
  145.       var4.write(4);
  146.       var4.write(var1);
  147.       var4.write(var3 >> 8 & 255);
  148.       var4.write(var3 & 255);
  149.       byte[] var5 = var2.getAddress();
  150.       var4.write(var5, 0, var5.length);
  151.       String var6 = System.getProperty("user.name");
  152.       byte[] var7 = new byte[var6.length()];
  153.       var6.getBytes(0, var6.length(), var7, 0);
  154.       var4.write(var7, 0, var7.length);
  155.       var4.write(0);
  156.       return var4.toByteArray();
  157.    }
  158.  
  159.    private boolean usingSocks() {
  160.       return System.getProperty("s\u0000o\u0000c\u0000k\u0000s\u0000P\u0000r\u0000o\u0000x\u0000y\u0000H\u0000o\u0000s\u0000t\u0000") != null;
  161.    }
  162.  
  163.    protected synchronized void bind(InetAddress var1, int var2) throws IOException {
  164.       this.socketBind(var1, var2);
  165.    }
  166.  
  167.    protected synchronized void listen(int var1) throws IOException {
  168.       this.socketListen(var1);
  169.    }
  170.  
  171.    protected synchronized void accept(SocketImpl var1) throws IOException {
  172.       this.socketAccept(var1);
  173.    }
  174.  
  175.    protected synchronized InputStream getInputStream() throws IOException {
  176.       return new SocketInputStream(this);
  177.    }
  178.  
  179.    protected synchronized OutputStream getOutputStream() throws IOException {
  180.       return new SocketOutputStream(this);
  181.    }
  182.  
  183.    protected synchronized int available() throws IOException {
  184.       return this.socketAvailable();
  185.    }
  186.  
  187.    protected synchronized void close() throws IOException {
  188.       if (super.fd != null) {
  189.          this.socketClose();
  190.       }
  191.  
  192.    }
  193.  
  194.    protected synchronized void finalize() throws IOException {
  195.       if (super.fd != null) {
  196.          this.socketClose();
  197.       }
  198.  
  199.    }
  200.  
  201.    private native void socketCreate(boolean var1) throws IOException;
  202.  
  203.    private native void socketConnect(InetAddress var1, int var2) throws IOException;
  204.  
  205.    private native void socketBind(InetAddress var1, int var2) throws IOException;
  206.  
  207.    private native void socketListen(int var1) throws IOException;
  208.  
  209.    private native void socketAccept(SocketImpl var1) throws IOException;
  210.  
  211.    private native int socketAvailable() throws IOException;
  212.  
  213.    private native void socketClose() throws IOException;
  214.  
  215.    static {
  216.       System.loadLibrary("javart");
  217.    }
  218. }
  219.