1 /* |
|
2 * Copyright (c) 2013, 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 package sun.management.jdp; |
|
26 |
|
27 import java.io.ByteArrayInputStream; |
|
28 import java.io.DataInputStream; |
|
29 import java.io.EOFException; |
|
30 import java.io.IOException; |
|
31 import java.io.UnsupportedEncodingException; |
|
32 import java.util.Collections; |
|
33 import java.util.HashMap; |
|
34 import java.util.Map; |
|
35 |
|
36 /** |
|
37 * JdpPacketReader responsible for reading a packet <p>This class gets a byte |
|
38 * array as it came from a Net, validates it and breaks a part </p> |
|
39 */ |
|
40 public final class JdpPacketReader { |
|
41 |
|
42 private final DataInputStream pkt; |
|
43 private Map<String, String> pmap = null; |
|
44 |
|
45 /** |
|
46 * Create packet reader, extract and check magic and version |
|
47 * |
|
48 * @param packet - packet received from a Net |
|
49 * @throws JdpException |
|
50 */ |
|
51 public JdpPacketReader(byte[] packet) |
|
52 throws JdpException { |
|
53 ByteArrayInputStream bais = new ByteArrayInputStream(packet); |
|
54 pkt = new DataInputStream(bais); |
|
55 |
|
56 try { |
|
57 int magic = pkt.readInt(); |
|
58 JdpGenericPacket.checkMagic(magic); |
|
59 } catch (IOException e) { |
|
60 throw new JdpException("Invalid JDP packet received, bad magic"); |
|
61 } |
|
62 |
|
63 try { |
|
64 short version = pkt.readShort(); |
|
65 JdpGenericPacket.checkVersion(version); |
|
66 } catch (IOException e) { |
|
67 throw new JdpException("Invalid JDP packet received, bad protocol version"); |
|
68 } |
|
69 } |
|
70 |
|
71 /** |
|
72 * Get next entry from packet |
|
73 * |
|
74 * @return the entry |
|
75 * @throws EOFException |
|
76 * @throws JdpException |
|
77 */ |
|
78 public String getEntry() |
|
79 throws EOFException, JdpException { |
|
80 |
|
81 try { |
|
82 short len = pkt.readShort(); |
|
83 // Artificial setting the "len" field to Short.MAX_VALUE may cause a reader to allocate |
|
84 // to much memory. Prevent this possible DOS attack. |
|
85 if (len < 1 && len > pkt.available()) { |
|
86 throw new JdpException("Broken JDP packet. Invalid entry length field."); |
|
87 } |
|
88 |
|
89 byte[] b = new byte[len]; |
|
90 if (pkt.read(b) != len) { |
|
91 throw new JdpException("Broken JDP packet. Unable to read entry."); |
|
92 } |
|
93 return new String(b, "UTF-8"); |
|
94 |
|
95 } catch (EOFException e) { |
|
96 throw e; |
|
97 } catch (UnsupportedEncodingException ex) { |
|
98 throw new JdpException("Broken JDP packet. Unable to decode entry."); |
|
99 } catch (IOException e) { |
|
100 throw new JdpException("Broken JDP packet. Unable to read entry."); |
|
101 } |
|
102 |
|
103 |
|
104 } |
|
105 |
|
106 /** |
|
107 * return packet content as a key/value map |
|
108 * |
|
109 * @return map containing packet entries pair of entries treated as |
|
110 * key,value |
|
111 * @throws IOException |
|
112 * @throws JdpException |
|
113 */ |
|
114 public Map<String, String> getDiscoveryDataAsMap() |
|
115 throws JdpException { |
|
116 // return cached map if possible |
|
117 if (pmap != null) { |
|
118 return pmap; |
|
119 } |
|
120 |
|
121 String key = null, value = null; |
|
122 |
|
123 final Map<String, String> tmpMap = new HashMap<>(); |
|
124 try { |
|
125 while (true) { |
|
126 key = getEntry(); |
|
127 value = getEntry(); |
|
128 tmpMap.put(key, value); |
|
129 } |
|
130 } catch (EOFException e) { |
|
131 // EOF reached on reading value, report broken packet |
|
132 // otherwise ignore it. |
|
133 if (value == null) { |
|
134 throw new JdpException("Broken JDP packet. Key without value." + key); |
|
135 } |
|
136 } |
|
137 |
|
138 pmap = Collections.unmodifiableMap(tmpMap); |
|
139 return pmap; |
|
140 } |
|
141 } |
|