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    public 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    @Override
068    public InputStream getInputStream() throws IOException {
069        return this.socket.getInputStream();
070    }
071
072    @Override
073    public OutputStream getOutputStream() throws IOException {
074        return this.socket.getOutputStream();
075    }
076
077    @Override
078    public int getReadTimeout() throws IOException {
079        try {
080            return this.socket.getSoTimeout();
081        }
082        catch (SocketException e) {
083            throw new IOException("Error on underlying Socket");
084        }
085    }
086
087    @Override
088    public void setReadTimeout(int timeout) throws IOException {
089        try {
090            this.socket.setSoTimeout(timeout);
091        }
092        catch (SocketException e) {
093            throw new IOException("Error on underlying Socket");
094        }
095    }
096
097    @Override
098    public void close() throws IOException {
099        this.socket.close();
100    }
101
102}