History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/sun/security/krb5/internal/NetClient.java (1 of 18352 files). Show all

Unified · Split

java/pkg/sun/security/krb5/internal/NetClient.java+222−0
@@ -0,0 +1,222 @@
1+/*
2+ * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
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
7+ * published by the Free Software Foundation. Oracle designates this
8+ * particular file as subject to the "Classpath" exception as provided
9+ * by Oracle in the LICENSE file that accompanied this code.
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+ *
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.
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+
37+public abstract class NetClient {
38+ public static NetClient getInstance(String protocol, String hostname, int port,
39+ int timeout) throws IOException {
40+ if (protocol.equals("TCP")) {
41+ return new TCPClient(hostname, port, timeout);
42+ } else {
43+ return new UDPClient(hostname, port, timeout);
44+ }
45+ }
46+
47+ abstract public void send(byte[] data) throws IOException;
48+
49+ abstract public byte[] receive() throws IOException;
50+
51+ abstract public void close() throws IOException;
52+}
53+
54+class TCPClient extends NetClient {
55+
56+ private Socket tcpSocket;
57+ private BufferedOutputStream out;
58+ private BufferedInputStream in;
59+
60+ TCPClient(String hostname, int port, int timeout)
61+ throws IOException {
62+ tcpSocket = new Socket();
63+ tcpSocket.connect(new InetSocketAddress(hostname, port), timeout);
64+ out = new BufferedOutputStream(tcpSocket.getOutputStream());
65+ in = new BufferedInputStream(tcpSocket.getInputStream());
66+ tcpSocket.setSoTimeout(timeout);
67+ }
68+
69+ @Override
70+ public void send(byte[] data) throws IOException {
71+ byte[] lenField = new byte[4];
72+ intToNetworkByteOrder(data.length, lenField, 0, 4);
73+ out.write(lenField);
74+
75+ out.write(data);
76+ out.flush();
77+ }
78+
79+ @Override
80+ public byte[] receive() throws IOException {
81+ byte[] lenField = new byte[4];
82+ int count = readFully(lenField, 4);
83+
84+ if (count != 4) {
85+ if (Krb5.DEBUG) {
86+ System.out.println(
87+ ">>>DEBUG: TCPClient could not read length field");
88+ }
89+ return null;
90+ }
91+
92+ int len = networkByteOrderToInt(lenField, 0, 4);
93+ if (Krb5.DEBUG) {
94+ System.out.println(
95+ ">>>DEBUG: TCPClient reading " + len + " bytes");
96+ }
97+ if (len <= 0) {
98+ if (Krb5.DEBUG) {
99+ System.out.println(
100+ ">>>DEBUG: TCPClient zero or negative length field: "+len);
101+ }
102+ return null;
103+ }
104+
105+ byte data[] = new byte[len];
106+ count = readFully(data, len);
107+ if (count != len) {
108+ if (Krb5.DEBUG) {
109+ System.out.println(
110+ ">>>DEBUG: TCPClient could not read complete packet (" +
111+ len + "/" + count + ")");
112+ }
113+ return null;
114+ } else {
115+ return data;
116+ }
117+ }
118+
119+ @Override
120+ public void close() throws IOException {
121+ tcpSocket.close();
122+ }
123+
124+ /**
125+ * Read requested number of bytes before returning.
126+ * @return The number of bytes actually read; -1 if none read
127+ */
128+ private int readFully(byte[] inBuf, int total) throws IOException {
129+ int count, pos = 0;
130+
131+ while (total > 0) {
132+ count = in.read(inBuf, pos, total);
133+
134+ if (count == -1) {
135+ return (pos == 0? -1 : pos);
136+ }
137+ pos += count;
138+ total -= count;
139+ }
140+ return pos;
141+ }
142+
143+ /**
144+ * Returns the integer represented by 4 bytes in network byte order.
145+ */
146+ private static int networkByteOrderToInt(byte[] buf, int start,
147+ int count) {
148+ if (count > 4) {
149+ throw new IllegalArgumentException(
150+ "Cannot handle more than 4 bytes");
151+ }
152+
153+ int answer = 0;
154+
155+ for (int i = 0; i < count; i++) {
156+ answer <<= 8;
157+ answer |= ((int)buf[start+i] & 0xff);
158+ }
159+ return answer;
160+ }
161+
162+ /**
163+ * Encodes an integer into 4 bytes in network byte order in the buffer
164+ * supplied.
165+ */
166+ private static void intToNetworkByteOrder(int num, byte[] buf,
167+ int start, int count) {
168+ if (count > 4) {
169+ throw new IllegalArgumentException(
170+ "Cannot handle more than 4 bytes");
171+ }
172+
173+ for (int i = count-1; i >= 0; i--) {
174+ buf[start+i] = (byte)(num & 0xff);
175+ num >>>= 8;
176+ }
177+ }
178+}
179+
180+class UDPClient extends NetClient {
181+ InetAddress iaddr;
182+ int iport;
183+ int bufSize = 65507;
184+ DatagramSocket dgSocket;
185+ DatagramPacket dgPacketIn;
186+
187+ UDPClient(String hostname, int port, int timeout)
188+ throws UnknownHostException, SocketException {
189+ iaddr = InetAddress.getByName(hostname);
190+ iport = port;
191+ dgSocket = new DatagramSocket();
192+ dgSocket.setSoTimeout(timeout);
193+ }
194+
195+ @Override
196+ public void send(byte[] data) throws IOException {
197+ DatagramPacket dgPacketOut = new DatagramPacket(data, data.length,
198+ iaddr, iport);
199+ dgSocket.send(dgPacketOut);
200+ }
201+
202+ @Override
203+ public byte[] receive() throws IOException {
204+ byte ibuf[] = new byte[bufSize];
205+ dgPacketIn = new DatagramPacket(ibuf, ibuf.length);
206+ try {
207+ dgSocket.receive(dgPacketIn);
208+ }
209+ catch (SocketException e) {
210+ dgSocket.receive(dgPacketIn);
211+ }
212+ byte[] data = new byte[dgPacketIn.getLength()];
213+ System.arraycopy(dgPacketIn.getData(), 0, data, 0,
214+ dgPacketIn.getLength());
215+ return data;
216+ }
217+
218+ @Override
219+ public void close() {
220+ dgSocket.close();
221+ }
222+}