author | weijun |
Wed, 23 May 2012 15:51:10 +0800 | |
changeset 12842 | f6f9cb8f6b97 |
parent 9268 | 0fee35a7b071 |
child 14342 | 8435a30053c1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9268 | 2 |
* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
/* |
|
27 |
* |
|
28 |
* (C) Copyright IBM Corp. 1999 All Rights Reserved. |
|
29 |
* Copyright 1997 The Open Group Research Institute. All rights reserved. |
|
30 |
*/ |
|
31 |
||
32 |
package sun.security.krb5.internal; |
|
33 |
||
34 |
import java.io.*; |
|
35 |
import java.net.*; |
|
36 |
||
12842 | 37 |
public abstract class NetClient implements AutoCloseable { |
7175
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
38 |
public static NetClient getInstance(String protocol, String hostname, int port, |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
39 |
int timeout) throws IOException { |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
40 |
if (protocol.equals("TCP")) { |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
41 |
return new TCPClient(hostname, port, timeout); |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
42 |
} else { |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
43 |
return new UDPClient(hostname, port, timeout); |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
44 |
} |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
45 |
} |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
46 |
|
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
47 |
abstract public void send(byte[] data) throws IOException; |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
48 |
abstract public byte[] receive() throws IOException; |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
49 |
abstract public void close() throws IOException; |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
50 |
} |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
51 |
|
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
52 |
class TCPClient extends NetClient { |
2 | 53 |
|
54 |
private Socket tcpSocket; |
|
55 |
private BufferedOutputStream out; |
|
56 |
private BufferedInputStream in; |
|
57 |
||
7175
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
58 |
TCPClient(String hostname, int port, int timeout) |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
59 |
throws IOException { |
9268 | 60 |
tcpSocket = new Socket(); |
61 |
tcpSocket.connect(new InetSocketAddress(hostname, port), timeout); |
|
2 | 62 |
out = new BufferedOutputStream(tcpSocket.getOutputStream()); |
63 |
in = new BufferedInputStream(tcpSocket.getInputStream()); |
|
7175
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
64 |
tcpSocket.setSoTimeout(timeout); |
2 | 65 |
} |
66 |
||
7175
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
67 |
@Override |
2 | 68 |
public void send(byte[] data) throws IOException { |
69 |
byte[] lenField = new byte[4]; |
|
70 |
intToNetworkByteOrder(data.length, lenField, 0, 4); |
|
71 |
out.write(lenField); |
|
72 |
||
73 |
out.write(data); |
|
74 |
out.flush(); |
|
75 |
} |
|
76 |
||
7175
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
77 |
@Override |
2 | 78 |
public byte[] receive() throws IOException { |
79 |
byte[] lenField = new byte[4]; |
|
80 |
int count = readFully(lenField, 4); |
|
81 |
||
82 |
if (count != 4) { |
|
83 |
if (Krb5.DEBUG) { |
|
84 |
System.out.println( |
|
85 |
">>>DEBUG: TCPClient could not read length field"); |
|
86 |
} |
|
87 |
return null; |
|
88 |
} |
|
89 |
||
90 |
int len = networkByteOrderToInt(lenField, 0, 4); |
|
91 |
if (Krb5.DEBUG) { |
|
92 |
System.out.println( |
|
93 |
">>>DEBUG: TCPClient reading " + len + " bytes"); |
|
94 |
} |
|
95 |
if (len <= 0) { |
|
96 |
if (Krb5.DEBUG) { |
|
97 |
System.out.println( |
|
98 |
">>>DEBUG: TCPClient zero or negative length field: "+len); |
|
99 |
} |
|
100 |
return null; |
|
101 |
} |
|
102 |
||
103 |
byte data[] = new byte[len]; |
|
104 |
count = readFully(data, len); |
|
105 |
if (count != len) { |
|
106 |
if (Krb5.DEBUG) { |
|
107 |
System.out.println( |
|
108 |
">>>DEBUG: TCPClient could not read complete packet (" + |
|
109 |
len + "/" + count + ")"); |
|
110 |
} |
|
111 |
return null; |
|
112 |
} else { |
|
113 |
return data; |
|
114 |
} |
|
115 |
} |
|
116 |
||
7175
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
117 |
@Override |
2 | 118 |
public void close() throws IOException { |
119 |
tcpSocket.close(); |
|
120 |
} |
|
121 |
||
122 |
/** |
|
123 |
* Read requested number of bytes before returning. |
|
124 |
* @return The number of bytes actually read; -1 if none read |
|
125 |
*/ |
|
126 |
private int readFully(byte[] inBuf, int total) throws IOException { |
|
127 |
int count, pos = 0; |
|
128 |
||
129 |
while (total > 0) { |
|
130 |
count = in.read(inBuf, pos, total); |
|
131 |
||
132 |
if (count == -1) { |
|
133 |
return (pos == 0? -1 : pos); |
|
134 |
} |
|
135 |
pos += count; |
|
136 |
total -= count; |
|
137 |
} |
|
138 |
return pos; |
|
139 |
} |
|
140 |
||
141 |
/** |
|
142 |
* Returns the integer represented by 4 bytes in network byte order. |
|
143 |
*/ |
|
7175
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
144 |
private static int networkByteOrderToInt(byte[] buf, int start, |
2 | 145 |
int count) { |
146 |
if (count > 4) { |
|
147 |
throw new IllegalArgumentException( |
|
148 |
"Cannot handle more than 4 bytes"); |
|
149 |
} |
|
150 |
||
151 |
int answer = 0; |
|
152 |
||
153 |
for (int i = 0; i < count; i++) { |
|
154 |
answer <<= 8; |
|
155 |
answer |= ((int)buf[start+i] & 0xff); |
|
156 |
} |
|
157 |
return answer; |
|
158 |
} |
|
159 |
||
160 |
/** |
|
161 |
* Encodes an integer into 4 bytes in network byte order in the buffer |
|
162 |
* supplied. |
|
163 |
*/ |
|
7175
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
164 |
private static void intToNetworkByteOrder(int num, byte[] buf, |
2 | 165 |
int start, int count) { |
166 |
if (count > 4) { |
|
167 |
throw new IllegalArgumentException( |
|
168 |
"Cannot handle more than 4 bytes"); |
|
169 |
} |
|
170 |
||
171 |
for (int i = count-1; i >= 0; i--) { |
|
172 |
buf[start+i] = (byte)(num & 0xff); |
|
173 |
num >>>= 8; |
|
174 |
} |
|
175 |
} |
|
176 |
} |
|
7175
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
177 |
|
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
178 |
class UDPClient extends NetClient { |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
179 |
InetAddress iaddr; |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
180 |
int iport; |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
181 |
int bufSize = 65507; |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
182 |
DatagramSocket dgSocket; |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
183 |
DatagramPacket dgPacketIn; |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
184 |
|
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
185 |
UDPClient(String hostname, int port, int timeout) |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
186 |
throws UnknownHostException, SocketException { |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
187 |
iaddr = InetAddress.getByName(hostname); |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
188 |
iport = port; |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
189 |
dgSocket = new DatagramSocket(); |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
190 |
dgSocket.setSoTimeout(timeout); |
12842 | 191 |
dgSocket.connect(iaddr, iport); |
7175
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
192 |
} |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
193 |
|
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
194 |
@Override |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
195 |
public void send(byte[] data) throws IOException { |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
196 |
DatagramPacket dgPacketOut = new DatagramPacket(data, data.length, |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
197 |
iaddr, iport); |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
198 |
dgSocket.send(dgPacketOut); |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
199 |
} |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
200 |
|
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
201 |
@Override |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
202 |
public byte[] receive() throws IOException { |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
203 |
byte ibuf[] = new byte[bufSize]; |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
204 |
dgPacketIn = new DatagramPacket(ibuf, ibuf.length); |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
205 |
try { |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
206 |
dgSocket.receive(dgPacketIn); |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
207 |
} |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
208 |
catch (SocketException e) { |
12842 | 209 |
if (e instanceof PortUnreachableException) { |
210 |
throw e; |
|
211 |
} |
|
7175
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
212 |
dgSocket.receive(dgPacketIn); |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
213 |
} |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
214 |
byte[] data = new byte[dgPacketIn.getLength()]; |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
215 |
System.arraycopy(dgPacketIn.getData(), 0, data, 0, |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
216 |
dgPacketIn.getLength()); |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
217 |
return data; |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
218 |
} |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
219 |
|
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
220 |
@Override |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
221 |
public void close() { |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
222 |
dgSocket.close(); |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
223 |
} |
46ec4c2dbc16
6952519: kdc_timeout is not being honoured when using TCP
weijun
parents:
5506
diff
changeset
|
224 |
} |