blob: 9c9256341b34cad158f52046f99befe73000bb0c [file] [log] [blame]
Shuyi Chend7955ce2013-05-22 14:51:55 -07001/**
2 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14package org.jivesoftware.smackx.bytestreams.socks5;
15
16import java.io.DataInputStream;
17import java.io.IOException;
18
19import org.jivesoftware.smack.XMPPException;
20import org.jivesoftware.smack.util.StringUtils;
21
22/**
23 * A collection of utility methods for SOcKS5 messages.
24 *
25 * @author Henning Staib
26 */
27class Socks5Utils {
28
29 /**
30 * Returns a SHA-1 digest of the given parameters as specified in <a
31 * href="http://xmpp.org/extensions/xep-0065.html#impl-socks5">XEP-0065</a>.
32 *
33 * @param sessionID for the SOCKS5 Bytestream
34 * @param initiatorJID JID of the initiator of a SOCKS5 Bytestream
35 * @param targetJID JID of the target of a SOCKS5 Bytestream
36 * @return SHA-1 digest of the given parameters
37 */
38 public static String createDigest(String sessionID, String initiatorJID, String targetJID) {
39 StringBuilder b = new StringBuilder();
40 b.append(sessionID).append(initiatorJID).append(targetJID);
41 return StringUtils.hash(b.toString());
42 }
43
44 /**
45 * Reads a SOCKS5 message from the given InputStream. The message can either be a SOCKS5 request
46 * message or a SOCKS5 response message.
47 * <p>
48 * (see <a href="http://tools.ietf.org/html/rfc1928">RFC1928</a>)
49 *
50 * @param in the DataInputStream to read the message from
51 * @return the SOCKS5 message
52 * @throws IOException if a network error occurred
53 * @throws XMPPException if the SOCKS5 message contains an unsupported address type
54 */
55 public static byte[] receiveSocks5Message(DataInputStream in) throws IOException, XMPPException {
56 byte[] header = new byte[5];
57 in.readFully(header, 0, 5);
58
59 if (header[3] != (byte) 0x03) {
60 throw new XMPPException("Unsupported SOCKS5 address type");
61 }
62
63 int addressLength = header[4];
64
65 byte[] response = new byte[7 + addressLength];
66 System.arraycopy(header, 0, response, 0, header.length);
67
68 in.readFully(response, header.length, addressLength + 2);
69
70 return response;
71 }
72
73}