1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  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
18package org.conscrypt;
19
20import java.io.IOException;
21import java.net.InetAddress;
22import java.net.Socket;
23import java.net.SocketAddress;
24import java.net.SocketException;
25
26/**
27 * This class wraps the SSL functionality over an existing connected socket.
28 *
29 * @hide
30 */
31@Internal
32public class OpenSSLSocketImplWrapper extends OpenSSLSocketImpl {
33
34    private Socket socket;
35
36    protected OpenSSLSocketImplWrapper(Socket socket, String hostname, int port,
37            boolean autoClose, SSLParametersImpl sslParameters) throws IOException {
38        super(socket, hostname, port, autoClose, sslParameters);
39        if (!socket.isConnected()) {
40            throw new SocketException("Socket is not connected.");
41        }
42        this.socket = socket;
43    }
44
45    @Override
46    public void connect(SocketAddress sockaddr, int timeout)
47        throws IOException {
48        throw new IOException("Underlying socket is already connected.");
49    }
50
51    @Override
52    public void connect(SocketAddress sockaddr) throws IOException {
53        throw new IOException("Underlying socket is already connected.");
54    }
55
56    @Override
57    public void bind(SocketAddress sockaddr) throws IOException {
58        throw new IOException("Underlying socket is already connected.");
59    }
60
61    @Override
62    public SocketAddress getRemoteSocketAddress() {
63        return socket.getRemoteSocketAddress();
64    }
65
66    @Override
67    public SocketAddress getLocalSocketAddress() {
68        return socket.getLocalSocketAddress();
69    }
70
71    @Override
72    public InetAddress getLocalAddress() {
73        return socket.getLocalAddress();
74    }
75
76    @Override
77    public InetAddress getInetAddress() {
78        return socket.getInetAddress();
79    }
80
81    @Override
82    public String toString() {
83        return "SSL socket over " + socket.toString();
84    }
85
86    @Override
87    public void setSoLinger(boolean on, int linger) throws SocketException {
88        socket.setSoLinger(on, linger);
89    }
90
91    @Override
92    public void setTcpNoDelay(boolean on) throws SocketException {
93        socket.setTcpNoDelay(on);
94    }
95
96    @Override
97    public void setReuseAddress(boolean on) throws SocketException {
98        socket.setReuseAddress(on);
99    }
100
101    @Override
102    public void setKeepAlive(boolean on) throws SocketException {
103        socket.setKeepAlive(on);
104    }
105
106    @Override
107    public void setTrafficClass(int tos) throws SocketException {
108        socket.setTrafficClass(tos);
109    }
110
111    @Override
112    @SuppressWarnings("UnsynchronizedOverridesSynchronized")
113    public void setSendBufferSize(int size) throws SocketException {
114        socket.setSendBufferSize(size);
115    }
116
117    @Override
118    @SuppressWarnings("UnsynchronizedOverridesSynchronized")
119    public void setReceiveBufferSize(int size) throws SocketException {
120        socket.setReceiveBufferSize(size);
121    }
122
123    @Override
124    public boolean getTcpNoDelay() throws SocketException {
125        return socket.getTcpNoDelay();
126    }
127
128    @Override
129    public boolean getReuseAddress() throws SocketException {
130        return socket.getReuseAddress();
131    }
132
133    @Override
134    public boolean getOOBInline() throws SocketException {
135        return socket.getOOBInline();
136    }
137
138    @Override
139    public boolean getKeepAlive() throws SocketException {
140        return socket.getKeepAlive();
141    }
142
143    @Override
144    public int getTrafficClass() throws SocketException {
145        return socket.getTrafficClass();
146    }
147
148    @Override
149    @SuppressWarnings("UnsynchronizedOverridesSynchronized")
150    public int getSoTimeout() throws SocketException {
151        return socket.getSoTimeout();
152    }
153
154    @Override
155    public int getSoLinger() throws SocketException {
156        return socket.getSoLinger();
157    }
158
159    @Override
160    @SuppressWarnings("UnsynchronizedOverridesSynchronized")
161    public int getSendBufferSize() throws SocketException {
162        return socket.getSendBufferSize();
163    }
164
165    @Override
166    @SuppressWarnings("UnsynchronizedOverridesSynchronized")
167    public int getReceiveBufferSize() throws SocketException {
168        return socket.getReceiveBufferSize();
169    }
170
171    @Override
172    public boolean isConnected() {
173        return socket.isConnected();
174    }
175
176    @Override
177    public boolean isClosed() {
178        return socket.isClosed();
179    }
180
181    @Override
182    public boolean isBound() {
183        return socket.isBound();
184    }
185
186    @Override
187    public boolean isOutputShutdown() {
188        return socket.isOutputShutdown();
189    }
190
191    @Override
192    public boolean isInputShutdown() {
193        return socket.isInputShutdown();
194    }
195
196    @Override
197    public int getPort() {
198        return socket.getPort();
199    }
200
201    @Override
202    public int getLocalPort() {
203        return socket.getLocalPort();
204    }
205}
206