001: //serverCoreSocket.java
002: //-------------------------------------
003: //part of YACY
004: //
005: //(C) 2006 by Martin Thelian
006: //
007: //last change: $LastChangedDate: 2006-05-12 16:35:56 +0200 (Fr, 12 Mai 2006) $ by $LastChangedBy: theli $
008: //Revision: $LastChangedRevision: 2086 $
009: //
010: //This program is free software; you can redistribute it and/or modify
011: //it under the terms of the GNU General Public License as published by
012: //the Free Software Foundation; either version 2 of the License, or
013: //(at your option) any later version.
014: //
015: //This program is distributed in the hope that it will be useful,
016: //but WITHOUT ANY WARRANTY; without even the implied warranty of
017: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
018: //GNU General Public License for more details.
019: //
020: //You should have received a copy of the GNU General Public License
021: //along with this program; if not, write to the Free Software
022: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
023: //
024: //Using this software in any meaning (reading, learning, copying, compiling,
025: //running) means that you agree that the Author(s) is (are) not responsible
026: //for cost, loss of data or any harm that may be caused directly or indirectly
027: //by usage of this softare or this documentation. The usage of this software
028: //is on your own risk. The installation and usage (starting/running) of this
029: //software may allow other people or application to access your computer and
030: //any attached devices and is highly dependent on the configuration of the
031: //software which must be done by the user of the software; the author(s) is
032: //(are) also not responsible for proper configuration and usage of the
033: //software, even if provoked by documentation provided together with
034: //the software.
035: //
036: //Any changes to this file according to the GPL as documented in the file
037: //gpl.txt aside this file in the shipment you received can be done to the
038: //lines that follows this copyright notice here, but changes must not be
039: //done inside the copyright notive above. A re-distribution must contain
040: //the intact and unchanged copyright notice.
041: //Contributions and changes to the program code must be marked as such.
042:
043: package de.anomic.server;
044:
045: import java.io.IOException;
046: import java.io.InputStream;
047: import java.io.OutputStream;
048: import java.io.PushbackInputStream;
049: import java.net.InetAddress;
050: import java.net.Socket;
051: import java.net.SocketAddress;
052: import java.net.SocketException;
053: import java.nio.channels.SocketChannel;
054:
055: public class serverCoreSocket extends Socket {
056:
057: private PushbackInputStream input = null;
058: private Socket sock = null;
059: private boolean isSSL = false;
060: private String sslType = null;
061:
062: public serverCoreSocket(Socket sock) throws IOException {
063: this .sock = sock;
064:
065: // determine the socket type
066: detectSSL();
067: }
068:
069: public boolean isSSL() {
070: return this .isSSL;
071: }
072:
073: public String getProtocol() {
074: return this .sslType;
075: }
076:
077: private void detectSSL() throws IOException {
078: InputStream in = getInputStream();
079:
080: // read the first 5 bytes to determine the protocol type
081: byte[] preRead = new byte[5];
082: int read, count = 0;
083: while ((count < preRead.length) && ((read = in.read()) != -1)) {
084: preRead[count] = (byte) read;
085: count++;
086: }
087: if (count < preRead.length) {
088: ((PushbackInputStream) in).unread(preRead, 0, count);
089: return;
090: }
091:
092: int idx = 0;
093: if ((preRead[0] & 0xFF) == 22) {
094: // we have detected the ContentType field.
095: // 22 means "handshake"
096: idx = 1;
097: } else {
098: // SSL messages have two preceding bytes
099: // byte nr 3 specifies the handshake type
100: // 3 means "ClientHello"
101: int handshakeType = preRead[2] & 0x00FF;
102: if (handshakeType == 1)
103: this .isSSL = true;
104: idx = 3;
105: }
106:
107: // determine the protocol version
108: if (preRead[idx] == 3
109: && (preRead[idx + 1] >= 0 && preRead[idx + 1] <= 2)) {
110: switch (preRead[idx + 1]) {
111: case 0:
112: this .sslType = "SSL_3";
113: break;
114: case 1:
115: this .sslType = "TLS_1";
116: break;
117: case 2:
118: this .sslType = "TLS_1_1";
119: break;
120: }
121: this .isSSL = true;
122: } else {
123: // maybe SSL_2, but we can not be sure
124: }
125:
126: // unread pre read bytes
127: ((PushbackInputStream) in).unread(preRead);
128: }
129:
130: public InetAddress getInetAddress() {
131: return this .sock.getInetAddress();
132: }
133:
134: public InetAddress getLocalAddress() {
135: return this .sock.getLocalAddress();
136: }
137:
138: public int getPort() {
139: return this .sock.getPort();
140: }
141:
142: public int getLocalPort() {
143: return this .sock.getLocalPort();
144: }
145:
146: public SocketAddress getRemoteSocketAddress() {
147: return this .sock.getRemoteSocketAddress();
148: }
149:
150: public SocketAddress getLocalSocketAddress() {
151: return this .sock.getLocalSocketAddress();
152: }
153:
154: public SocketChannel getChannel() {
155: return this .sock.getChannel();
156: }
157:
158: public InputStream getInputStream() throws IOException {
159: if (this .input == null) {
160: this .input = new PushbackInputStream(this .sock
161: .getInputStream(), 100);
162: }
163: return this .input;
164: }
165:
166: public OutputStream getOutputStream() throws IOException {
167: return this .sock.getOutputStream();
168: }
169:
170: public synchronized void close() throws IOException {
171: this .sock.close();
172: }
173:
174: public void shutdownInput() throws IOException {
175: this .sock.shutdownInput();
176: }
177:
178: public void shutdownOutput() throws IOException {
179: this .sock.shutdownOutput();
180: }
181:
182: public String toString() {
183: return this .sock.toString();
184: }
185:
186: public boolean isConnected() {
187: return this .sock.isConnected();
188: }
189:
190: public boolean isBound() {
191: return this .sock.isBound();
192: }
193:
194: public boolean isClosed() {
195: return this .sock.isClosed();
196: }
197:
198: public boolean isInputShutdown() {
199: return this .sock.isInputShutdown();
200: }
201:
202: public boolean isOutputShutdown() {
203: return this .sock.isOutputShutdown();
204: }
205:
206: public synchronized void setSoTimeout(int timeout)
207: throws SocketException {
208: this.sock.setSoTimeout(timeout);
209: }
210:
211: }
|