1 /* |
|
2 * Copyright (c) 2000, 2003, 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 class TCPClient { |
|
38 |
|
39 private Socket tcpSocket; |
|
40 private BufferedOutputStream out; |
|
41 private BufferedInputStream in; |
|
42 |
|
43 public TCPClient(String hostname, int port) throws IOException { |
|
44 tcpSocket = new Socket(hostname, port); |
|
45 out = new BufferedOutputStream(tcpSocket.getOutputStream()); |
|
46 in = new BufferedInputStream(tcpSocket.getInputStream()); |
|
47 } |
|
48 |
|
49 public void send(byte[] data) throws IOException { |
|
50 byte[] lenField = new byte[4]; |
|
51 intToNetworkByteOrder(data.length, lenField, 0, 4); |
|
52 out.write(lenField); |
|
53 |
|
54 out.write(data); |
|
55 out.flush(); |
|
56 } |
|
57 |
|
58 public byte[] receive() throws IOException { |
|
59 byte[] lenField = new byte[4]; |
|
60 int count = readFully(lenField, 4); |
|
61 |
|
62 if (count != 4) { |
|
63 if (Krb5.DEBUG) { |
|
64 System.out.println( |
|
65 ">>>DEBUG: TCPClient could not read length field"); |
|
66 } |
|
67 return null; |
|
68 } |
|
69 |
|
70 int len = networkByteOrderToInt(lenField, 0, 4); |
|
71 if (Krb5.DEBUG) { |
|
72 System.out.println( |
|
73 ">>>DEBUG: TCPClient reading " + len + " bytes"); |
|
74 } |
|
75 if (len <= 0) { |
|
76 if (Krb5.DEBUG) { |
|
77 System.out.println( |
|
78 ">>>DEBUG: TCPClient zero or negative length field: "+len); |
|
79 } |
|
80 return null; |
|
81 } |
|
82 |
|
83 byte data[] = new byte[len]; |
|
84 count = readFully(data, len); |
|
85 if (count != len) { |
|
86 if (Krb5.DEBUG) { |
|
87 System.out.println( |
|
88 ">>>DEBUG: TCPClient could not read complete packet (" + |
|
89 len + "/" + count + ")"); |
|
90 } |
|
91 return null; |
|
92 } else { |
|
93 return data; |
|
94 } |
|
95 } |
|
96 |
|
97 public void close() throws IOException { |
|
98 tcpSocket.close(); |
|
99 } |
|
100 |
|
101 /** |
|
102 * Read requested number of bytes before returning. |
|
103 * @return The number of bytes actually read; -1 if none read |
|
104 */ |
|
105 private int readFully(byte[] inBuf, int total) throws IOException { |
|
106 int count, pos = 0; |
|
107 |
|
108 while (total > 0) { |
|
109 count = in.read(inBuf, pos, total); |
|
110 |
|
111 if (count == -1) { |
|
112 return (pos == 0? -1 : pos); |
|
113 } |
|
114 pos += count; |
|
115 total -= count; |
|
116 } |
|
117 return pos; |
|
118 } |
|
119 |
|
120 /** |
|
121 * Returns the integer represented by 4 bytes in network byte order. |
|
122 */ |
|
123 private static final int networkByteOrderToInt(byte[] buf, int start, |
|
124 int count) { |
|
125 if (count > 4) { |
|
126 throw new IllegalArgumentException( |
|
127 "Cannot handle more than 4 bytes"); |
|
128 } |
|
129 |
|
130 int answer = 0; |
|
131 |
|
132 for (int i = 0; i < count; i++) { |
|
133 answer <<= 8; |
|
134 answer |= ((int)buf[start+i] & 0xff); |
|
135 } |
|
136 return answer; |
|
137 } |
|
138 |
|
139 /** |
|
140 * Encodes an integer into 4 bytes in network byte order in the buffer |
|
141 * supplied. |
|
142 */ |
|
143 private static final void intToNetworkByteOrder(int num, byte[] buf, |
|
144 int start, int count) { |
|
145 if (count > 4) { |
|
146 throw new IllegalArgumentException( |
|
147 "Cannot handle more than 4 bytes"); |
|
148 } |
|
149 |
|
150 for (int i = count-1; i >= 0; i--) { |
|
151 buf[start+i] = (byte)(num & 0xff); |
|
152 num >>>= 8; |
|
153 } |
|
154 } |
|
155 } |
|