home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 November / PCO1197.ISO / FilesBBS / WIN95 / PROD_W95 / F_32579 / java / net / PlainSocketImpl.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-05-03  |  4.7 KB  |  218 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 var6) {
  37.             var3 = var6;
  38.          }
  39.       } catch (UnknownHostException var7) {
  40.          var3 = var7;
  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 var4) {
  54.          this.socketClose();
  55.          throw var4;
  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.    private void doSOCKSConnect(InetAddress var1, int var2) throws IOException {
  68.       this.connectToSocksServer();
  69.       this.sendSOCKSCommandPacket(1, var1, var2);
  70.       int var3 = this.getSOCKSReply();
  71.       switch (var3) {
  72.          case 90:
  73.             return;
  74.          case 91:
  75.          case 92:
  76.             throw new SocketException("SOCKS server cannot conect to identd");
  77.          case 93:
  78.             throw new SocketException("User name does not match identd name");
  79.          default:
  80.       }
  81.    }
  82.  
  83.    private int getSOCKSReply() throws IOException {
  84.       InputStream var1 = this.getInputStream();
  85.       byte[] var2 = new byte[8];
  86.       if (var1.read(var2) != var2.length) {
  87.          throw new SocketException("Malformed reply from SOCKS server");
  88.       } else if (var2[0] != 0) {
  89.          throw new SocketException("Malformed reply from SOCKS server");
  90.       } else {
  91.          return var2[1];
  92.       }
  93.    }
  94.  
  95.    private void connectToSocksServer() throws IOException {
  96.       String var1 = System.getProperty("socksProxyHost");
  97.       if (var1 != null) {
  98.          InetAddress var2 = InetAddress.getByName(var1);
  99.          String var3 = System.getProperty("socksProxyPort", "1080");
  100.  
  101.          int var4;
  102.          try {
  103.             var4 = Integer.parseInt(var3);
  104.          } catch (Exception var5) {
  105.             throw new SocketException("Bad port number format");
  106.          }
  107.  
  108.          this.doConnect(var2, var4);
  109.       }
  110.    }
  111.  
  112.    private void doConnect(InetAddress var1, int var2) throws IOException {
  113.       ProtocolException var3 = null;
  114.       int var4 = 0;
  115.  
  116.       while(var4 < 3) {
  117.          try {
  118.             this.socketConnect(var1, var2);
  119.             return;
  120.          } catch (ProtocolException var6) {
  121.             this.socketClose();
  122.             super.fd = new FileDescriptor();
  123.             this.socketCreate(true);
  124.             var3 = var6;
  125.             ++var4;
  126.          } catch (IOException var7) {
  127.             this.socketClose();
  128.             throw var7;
  129.          }
  130.       }
  131.  
  132.       this.socketClose();
  133.       throw var3;
  134.    }
  135.  
  136.    private void sendSOCKSCommandPacket(int var1, InetAddress var2, int var3) throws IOException {
  137.       byte[] var4 = this.makeCommandPacket(var1, var2, var3);
  138.       OutputStream var5 = this.getOutputStream();
  139.       var5.write(var4);
  140.    }
  141.  
  142.    private byte[] makeCommandPacket(int var1, InetAddress var2, int var3) {
  143.       ByteArrayOutputStream var4 = new ByteArrayOutputStream(9);
  144.       var4.write(4);
  145.       var4.write(var1);
  146.       var4.write(var3 >> 8 & 255);
  147.       var4.write(var3 & 255);
  148.       byte[] var5 = var2.getAddress();
  149.       var4.write(var5, 0, var5.length);
  150.       String var6 = System.getProperty("user.name");
  151.       byte[] var7 = new byte[var6.length()];
  152.       var6.getBytes(0, var6.length(), var7, 0);
  153.       var4.write(var7, 0, var7.length);
  154.       var4.write(0);
  155.       return var4.toByteArray();
  156.    }
  157.  
  158.    private boolean usingSocks() {
  159.       return System.getProperty("socksProxyHost") != null;
  160.    }
  161.  
  162.    protected synchronized void bind(InetAddress var1, int var2) throws IOException {
  163.       this.socketBind(var1, var2);
  164.    }
  165.  
  166.    protected synchronized void listen(int var1) throws IOException {
  167.       this.socketListen(var1);
  168.    }
  169.  
  170.    protected synchronized void accept(SocketImpl var1) throws IOException {
  171.       this.socketAccept(var1);
  172.    }
  173.  
  174.    protected synchronized InputStream getInputStream() throws IOException {
  175.       return new SocketInputStream(this);
  176.    }
  177.  
  178.    protected synchronized OutputStream getOutputStream() throws IOException {
  179.       return new SocketOutputStream(this);
  180.    }
  181.  
  182.    protected synchronized int available() throws IOException {
  183.       return this.socketAvailable();
  184.    }
  185.  
  186.    protected synchronized void close() throws IOException {
  187.       if (super.fd != null) {
  188.          this.socketClose();
  189.       }
  190.  
  191.    }
  192.  
  193.    protected synchronized void finalize() throws IOException {
  194.       if (super.fd != null) {
  195.          this.socketClose();
  196.       }
  197.  
  198.    }
  199.  
  200.    private native void socketCreate(boolean var1) throws IOException;
  201.  
  202.    private native void socketConnect(InetAddress var1, int var2) throws IOException;
  203.  
  204.    private native void socketBind(InetAddress var1, int var2) throws IOException;
  205.  
  206.    private native void socketListen(int var1) throws IOException;
  207.  
  208.    private native void socketAccept(SocketImpl var1) throws IOException;
  209.  
  210.    private native int socketAvailable() throws IOException;
  211.  
  212.    private native void socketClose() throws IOException;
  213.  
  214.    static {
  215.       System.loadLibrary("net");
  216.    }
  217. }
  218.