Socks5BytestreamSession.java

  1. /**
  2.  *
  3.  * Copyright the original author or authors
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.bytestreams.socks5;

  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.OutputStream;
  21. import java.net.Socket;
  22. import java.net.SocketException;

  23. import org.jivesoftware.smackx.bytestreams.BytestreamSession;

  24. /**
  25.  * Socks5BytestreamSession class represents a SOCKS5 Bytestream session.
  26.  *
  27.  * @author Henning Staib
  28.  */
  29. public class Socks5BytestreamSession implements BytestreamSession {

  30.     /* the underlying socket of the SOCKS5 Bytestream */
  31.     private final Socket socket;

  32.     /* flag to indicate if this session is a direct or mediated connection */
  33.     private final boolean isDirect;

  34.     public Socks5BytestreamSession(Socket socket, boolean isDirect) {
  35.         this.socket = socket;
  36.         this.isDirect = isDirect;
  37.     }

  38.     /**
  39.      * Returns <code>true</code> if the session is established through a direct connection between
  40.      * the initiator and target, <code>false</code> if the session is mediated over a SOCKS proxy.
  41.      *
  42.      * @return <code>true</code> if session is a direct connection, <code>false</code> if session is
  43.      *         mediated over a SOCKS5 proxy
  44.      */
  45.     public boolean isDirect() {
  46.         return this.isDirect;
  47.     }

  48.     /**
  49.      * Returns <code>true</code> if the session is mediated over a SOCKS proxy, <code>false</code>
  50.      * if this session is established through a direct connection between the initiator and target.
  51.      *
  52.      * @return <code>true</code> if session is mediated over a SOCKS5 proxy, <code>false</code> if
  53.      *         session is a direct connection
  54.      */
  55.     public boolean isMediated() {
  56.         return !this.isDirect;
  57.     }

  58.     @Override
  59.     public InputStream getInputStream() throws IOException {
  60.         return this.socket.getInputStream();
  61.     }

  62.     @Override
  63.     public OutputStream getOutputStream() throws IOException {
  64.         return this.socket.getOutputStream();
  65.     }

  66.     @Override
  67.     public int getReadTimeout() throws IOException {
  68.         try {
  69.             return this.socket.getSoTimeout();
  70.         }
  71.         catch (SocketException e) {
  72.             throw new IOException("Error on underlying Socket");
  73.         }
  74.     }

  75.     @Override
  76.     public void setReadTimeout(int timeout) throws IOException {
  77.         try {
  78.             this.socket.setSoTimeout(timeout);
  79.         }
  80.         catch (SocketException e) {
  81.             throw new IOException("Error on underlying Socket");
  82.         }
  83.     }

  84.     @Override
  85.     public void close() throws IOException {
  86.         this.socket.close();
  87.     }

  88. }