001/**
002 *
003 * Copyright the original author or authors
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.bytestreams.socks5;
018
019import java.io.IOException;
020import java.io.InputStream;
021import java.io.OutputStream;
022import java.net.Socket;
023import java.net.SocketException;
024
025import org.jivesoftware.smackx.bytestreams.BytestreamSession;
026
027/**
028 * Socks5BytestreamSession class represents a SOCKS5 Bytestream session.
029 * 
030 * @author Henning Staib
031 */
032public class Socks5BytestreamSession implements BytestreamSession {
033
034    /* the underlying socket of the SOCKS5 Bytestream */
035    private final Socket socket;
036
037    /* flag to indicate if this session is a direct or mediated connection */
038    private final boolean isDirect;
039
040    protected Socks5BytestreamSession(Socket socket, boolean isDirect) {
041        this.socket = socket;
042        this.isDirect = isDirect;
043    }
044
045    /**
046     * Returns <code>true</code> if the session is established through a direct connection between
047     * the initiator and target, <code>false</code> if the session is mediated over a SOCKS proxy.
048     * 
049     * @return <code>true</code> if session is a direct connection, <code>false</code> if session is
050     *         mediated over a SOCKS5 proxy
051     */
052    public boolean isDirect() {
053        return this.isDirect;
054    }
055
056    /**
057     * Returns <code>true</code> if the session is mediated over a SOCKS proxy, <code>false</code>
058     * if this session is established through a direct connection between the initiator and target.
059     * 
060     * @return <code>true</code> if session is mediated over a SOCKS5 proxy, <code>false</code> if
061     *         session is a direct connection
062     */
063    public boolean isMediated() {
064        return !this.isDirect;
065    }
066
067    public InputStream getInputStream() throws IOException {
068        return this.socket.getInputStream();
069    }
070
071    public OutputStream getOutputStream() throws IOException {
072        return this.socket.getOutputStream();
073    }
074
075    public int getReadTimeout() throws IOException {
076        try {
077            return this.socket.getSoTimeout();
078        }
079        catch (SocketException e) {
080            throw new IOException("Error on underlying Socket");
081        }
082    }
083
084    public void setReadTimeout(int timeout) throws IOException {
085        try {
086            this.socket.setSoTimeout(timeout);
087        }
088        catch (SocketException e) {
089            throw new IOException("Error on underlying Socket");
090        }
091    }
092
093    public void close() throws IOException {
094        this.socket.close();
095    }
096
097}