1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.socks;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.util.CharsetUtil;
20 import io.netty.util.internal.StringUtil;
21
22 final class SocksCommonUtils {
23 public static final SocksRequest UNKNOWN_SOCKS_REQUEST = new UnknownSocksRequest();
24 public static final SocksResponse UNKNOWN_SOCKS_RESPONSE = new UnknownSocksResponse();
25
26 private static final int SECOND_ADDRESS_OCTET_SHIFT = 16;
27 private static final int FIRST_ADDRESS_OCTET_SHIFT = 24;
28 private static final int THIRD_ADDRESS_OCTET_SHIFT = 8;
29 private static final int XOR_DEFAULT_VALUE = 0xff;
30
31
32
33
34 private SocksCommonUtils() {
35
36 }
37
38 public static String intToIp(int i) {
39 return String.valueOf(i >> FIRST_ADDRESS_OCTET_SHIFT & XOR_DEFAULT_VALUE) + '.' +
40 (i >> SECOND_ADDRESS_OCTET_SHIFT & XOR_DEFAULT_VALUE) + '.' +
41 (i >> THIRD_ADDRESS_OCTET_SHIFT & XOR_DEFAULT_VALUE) + '.' +
42 (i & XOR_DEFAULT_VALUE);
43 }
44
45 private static final char[] ipv6conseqZeroFiller = {':', ':'};
46 private static final char ipv6hextetSeparator = ':';
47
48
49
50
51
52 public static String ipv6toCompressedForm(byte[] src) {
53 assert src.length == 16;
54
55
56 int cmprHextet = -1;
57
58 int cmprSize = 0;
59 for (int hextet = 0; hextet < 8;) {
60 int curByte = hextet * 2;
61 int size = 0;
62 while (curByte < src.length && src[curByte] == 0
63 && src[curByte + 1] == 0) {
64 curByte += 2;
65 size++;
66 }
67 if (size > cmprSize) {
68 cmprHextet = hextet;
69 cmprSize = size;
70 }
71 hextet = curByte / 2 + 1;
72 }
73 if (cmprHextet == -1 || cmprSize < 2) {
74
75 return ipv6toStr(src);
76 }
77 StringBuilder sb = new StringBuilder(39);
78 ipv6toStr(sb, src, 0, cmprHextet);
79 sb.append(ipv6conseqZeroFiller);
80 ipv6toStr(sb, src, cmprHextet + cmprSize, 8);
81 return sb.toString();
82 }
83
84
85
86
87 public static String ipv6toStr(byte[] src) {
88 assert src.length == 16;
89 StringBuilder sb = new StringBuilder(39);
90 ipv6toStr(sb, src, 0, 8);
91 return sb.toString();
92 }
93
94 private static void ipv6toStr(StringBuilder sb, byte[] src, int fromHextet, int toHextet) {
95 int i;
96 toHextet --;
97 for (i = fromHextet; i < toHextet; i++) {
98 appendHextet(sb, src, i);
99 sb.append(ipv6hextetSeparator);
100 }
101
102 appendHextet(sb, src, i);
103 }
104
105 private static void appendHextet(StringBuilder sb, byte[] src, int i) {
106 StringUtil.toHexString(sb, src, i << 1, 2);
107 }
108
109 static String readUsAscii(ByteBuf buffer, int length) {
110 String s = buffer.toString(buffer.readerIndex(), length, CharsetUtil.US_ASCII);
111 buffer.skipBytes(length);
112 return s;
113 }
114 }