Produced by Araxis Merge on 9/25/2018 2:13:04 PM Central Daylight Time. See www.araxis.com for information about Merge. This report uses XHTML and CSS2, and is best viewed with a modern standards-compliant browser. For optimum results when printing this report, use landscape orientation and enable printing of background images and colours in your browser.
# | Location | File | Last Modified |
---|---|---|---|
1 | build 3.zip\build 3\MHLTH_YS_137_Source\JavaScript\resources\javaJDF-1.8.0\src\com\sun\jndi\dns | DNSDatagramSocketFactory.java | Mon Jan 22 14:46:50 2018 UTC |
2 | build 3.zip\build 3\MHLTH_YS_137_Source\JavaScript\resources\javaJDF-1.8.0\src\com\sun\jndi\dns | DNSDatagramSocketFactory.java | Wed Sep 12 16:27:23 2018 UTC |
Description | Between Files 1 and 2 |
|
---|---|---|
Text Blocks | Lines | |
Unchanged | 2 | 490 |
Changed | 1 | 2 |
Inserted | 0 | 0 |
Removed | 0 | 0 |
Whitespace | |
---|---|
Character case | Differences in character case are significant |
Line endings | Differences in line endings (CR and LF characters) are ignored |
CR/LF characters | Not shown in the comparison detail |
No regular expressions were active.
1 | /* | |
2 | * Copyrig ht (c) 201 7, Oracle and/or its affiliate s. All rig hts reserv ed. | |
3 | * DO NOT ALTER OR R EMOVE COPY RIGHT NOTI CES OR THI S FILE HEA DER. | |
4 | * | |
5 | * This co de is free software; you can r edistribut e it and/o r modify i t | |
6 | * under t he terms o f the GNU General Pu blic Licen se version 2 only, a s | |
7 | * publish ed by the Free Softw are Founda tion. Ora cle design ates this | |
8 | * particu lar file a s subject to the "Cl asspath" e xception a s provided | |
9 | * by Orac le in the LICENSE fi le that ac companied this code. | |
10 | * | |
11 | * This co de is dist ributed in the hope that it wi ll be usef ul, but WI THOUT | |
12 | * ANY WAR RANTY; wit hout even the implie d warranty of MERCHA NTABILITY or | |
13 | * FITNESS FOR A PAR TICULAR PU RPOSE. Se e the GNU General Pu blic Licen se | |
14 | * version 2 for mor e details (a copy is included in the LIC ENSE file that | |
15 | * accompa nied this code). | |
16 | * | |
17 | * You sho uld have r eceived a copy of th e GNU Gene ral Public License v ersion | |
18 | * 2 along with this work; if not, write to the Fr ee Softwar e Foundati on, | |
19 | * Inc., 5 1 Franklin St, Fifth Floor, Bo ston, MA 0 2110-1301 USA. | |
20 | * | |
21 | * Please contact Or acle, 500 Oracle Par kway, Redw ood Shores , CA 94065 USA | |
22 | * or visi t www.orac le.com if you need a dditional informatio n or have any | |
23 | * questio ns. | |
24 | */ | |
25 | package co m.sun.jndi .dns; | |
26 | ||
27 | import jav a.io.IOExc eption; | |
28 | import jav a.net.Data gramSocket ; | |
29 | import jav a.net.Prot ocolFamily ; | |
30 | import jav a.net.Sock etExceptio n; | |
31 | import jav a.net.Inet SocketAddr ess; | |
32 | import jav a.nio.chan nels.Datag ramChannel ; | |
33 | import jav a.util.Obj ects; | |
34 | import jav a.util.Ran dom; | |
35 | ||
36 | class DNSD atagramSoc ketFactory { | |
37 | static final int DEVIATION = 3; | |
38 | static final int THRESHOLD = 6; | |
39 | static final int BIT_DEVIA TION = 2; | |
40 | static final int HISTORY = 32; | |
41 | static final int MAX_RANDO M_TRIES = 5; | |
42 | /** | |
43 | * The dynamic a llocation port range (aka ephe meral port s), as con figured | |
44 | * on the system . Use nest ed class f or lazy ev aluation. | |
45 | */ | |
46 | static final cla ss Ephemer alPortRang e { | |
47 | pr ivate Ephe meralPortR ange() {} | |
48 | st atic final int LOWER = sun.net .PortConfi g.getLower (); | |
49 | st atic final int UPPER = sun.net .PortConfi g.getUpper (); | |
50 | st atic final int RANGE = UPPER - LOWER + 1 ; | |
51 | } | |
52 | ||
53 | // Rec ords a sub set of max {@code ca pacity} pr eviously u sed ports | |
54 | static final cla ss PortHis tory { | |
55 | fi nal int ca pacity; | |
56 | fi nal int[] ports; | |
57 | fi nal Random random; | |
58 | in t index; | |
59 | Po rtHistory( int capaci ty, Random random) { | |
60 | this.ran dom = rand om; | |
61 | this.cap acity = ca pacity; | |
62 | this.por ts = new i nt[capacit y]; | |
63 | } | |
64 | // returns t rue if the history c ontains th e specifie d port. | |
65 | pu blic boole an contain s(int port ) { | |
66 | int p = 0; | |
67 | for (int i=0; i<ca pacity; i+ +) { | |
68 | if ( (p = ports [i]) == 0 || p == po rt) break; | |
69 | } | |
70 | return p == port; | |
71 | } | |
72 | // Adds the port to th e history - doesn't check whet her the po rt | |
73 | // is alread y present. Always ad ds the por t and alwa ys return true. | |
74 | pu blic boole an add(int port) { | |
75 | if (port s[index] ! = 0) { // at max cap acity | |
76 | // r emove one port at ra ndom and s tore the n ew port th ere | |
77 | port s[random.n extInt(cap acity)] = port; | |
78 | } else { // there' s a free s lot | |
79 | port s[index] = port; | |
80 | } | |
81 | if (++in dex == cap acity) ind ex = 0; | |
82 | return t rue; | |
83 | } | |
84 | // Adds the port to th e history if not alr eady prese nt. | |
85 | // Return tr ue if the port was a dded, fals e if the p ort was al ready | |
86 | // present. | |
87 | pu blic boole an offer(i nt port) { | |
88 | if (cont ains(port) ) return f alse; | |
89 | else ret urn add(po rt); | |
90 | } | |
91 | } | |
92 | ||
93 | int last PORT
|
|
94 | int su itablePort Count; | |
95 | int un suitablePo rtCount; | |
96 | final ProtocolFa mily famil y; // null (default) means dua l stack | |
97 | final int thresh oldCount; // decisio n point | |
98 | final int deviat ion; | |
99 | final Random ran dom; | |
100 | final PortHistor y history; | |
101 | ||
102 | DNSDat agramSocke tFactory() { | |
103 | th is(new Ran dom()); | |
104 | } | |
105 | ||
106 | DNSDat agramSocke tFactory(R andom rand om) { | |
107 | th is(Objects .requireNo nNull(rand om), null, DEVIATION , THRESHOL D); | |
108 | } | |
109 | DNSDat agramSocke tFactory(R andom rand om, | |
110 | P rotocolFam ily family , | |
111 | i nt deviati on, | |
112 | i nt thresho ld) { | |
113 | th is.random = Objects. requireNon Null(rando m); | |
114 | th is.history = new Por tHistory(H ISTORY, ra ndom); | |
115 | th is.family = family; | |
116 | th is.deviati on = Math. max(1, dev iation); | |
117 | th is.thresho ldCount = Math.max(2 , threshol d); | |
118 | } | |
119 | ||
120 | /** | |
121 | * Ope ns a datag ram socket listening to the wi ldcard add ress on a | |
122 | * ran dom port. If the und erlying OS supports UDP port r andomizati on | |
123 | * out of the bo x (if bind ing a sock et to port 0 binds i t to a ran dom | |
124 | * por t) then th e underlyi ng OS impl ementation is used. Otherwise, this | |
125 | * met hod will a llocate an d bind a s ocket on a randomly selected e phemeral | |
126 | * por t in the d ynamic ran ge. | |
127 | * @re turn A new DatagramS ocket boun d to a ran dom port. | |
128 | * @th rows Socke tException if the so cket canno t be creat ed. | |
129 | */ | |
130 | public synchroni zed Datagr amSocket o pen() thro ws SocketE xception { | |
131 | in t lastseen = lastpor t; | |
132 | Da tagramSock et s; | |
133 | ||
134 | bo olean thre sholdCross ed = unsui tablePortC ount > thr esholdCoun t; | |
135 | if (threshol dCrossed) { | |
136 | // Under lying stac k does not support r andom UDP port out o f the box. | |
137 | // Use o ur own alg orithm to allocate a random UD P port | |
138 | s = open Random(); | |
139 | if (s != null) ret urn s; | |
140 | ||
141 | // could n't alloca te a rando m port: re set all co unters and fall | |
142 | // throu gh. | |
143 | unsuitab lePortCoun t = 0; sui tablePortC ount = 0; lastseen = 0; | |
144 | } | |
145 | ||
146 | // Allocate an ephemer al port (p ort 0) | |
147 | s = openDefa ult(); | |
148 | la stport = s .getLocalP ort(); | |
149 | if (lastseen == 0) { | |
150 | history. offer(last port); | |
151 | return s ; | |
152 | } | |
153 | ||
154 | th resholdCro ssed = sui tablePortC ount > thr esholdCoun t; | |
155 | bo olean farE nough = In teger.bitC ount(lasts een ^ last port) > BI T_DEVIATIO N | |
156 | && Math.abs( lastport - lastseen) > deviati on; | |
157 | bo olean recy cled = his tory.conta ins(lastpo rt); | |
158 | bo olean suit able = (th resholdCro ssed || fa rEnough && !recycled ); | |
159 | if (suitable && !recyc led) histo ry.add(las tport); | |
160 | ||
161 | if (suitable ) { | |
162 | if (!thr esholdCros sed) { | |
163 | suit ablePortCo unt++; | |
164 | } else i f (!farEno ugh || rec ycled) { | |
165 | unsu itablePort Count = 1; | |
166 | suit ablePortCo unt = thre sholdCount /2; | |
167 | } | |
168 | // Eithe r the unde rlying sta ck support s random U DP port al location, | |
169 | // or th e new port is suffic iently dis tant from last port to make | |
170 | // it lo ok like it is. Let's use it. | |
171 | return s ; | |
172 | } | |
173 | ||
174 | // Undecided ... the ne w port was too close . Let's al locate a r andom | |
175 | // port usin g our own algorithm | |
176 | as sert !thre sholdCross ed; | |
177 | Da tagramSock et ss = op enRandom() ; | |
178 | if (ss == nu ll) return s; | |
179 | un suitablePo rtCount++; | |
180 | s. close(); | |
181 | re turn ss; | |
182 | } | |
183 | ||
184 | privat e Datagram Socket ope nDefault() throws So cketExcept ion { | |
185 | if (family ! = null) { | |
186 | try { | |
187 | Data gramChanne l c = Data gramChanne l.open(fam ily); | |
188 | try { | |
189 | DatagramSo cket s = c .socket(); | |
190 | s.bind(nul l); | |
191 | return s; | |
192 | } ca tch (Throw able x) { | |
193 | c.close(); | |
194 | throw x; | |
195 | } | |
196 | } catch (SocketExc eption x) { | |
197 | thro w x; | |
198 | } catch (IOExcepti on x) { | |
199 | Sock etExceptio n e = new SocketExce ption(x.ge tMessage() ); | |
200 | e.in itCause(x) ; | |
201 | thro w e; | |
202 | } | |
203 | } | |
204 | re turn new D atagramSoc ket(); | |
205 | } | |
206 | ||
207 | synchr onized boo lean isUsi ngNativePo rtRandomiz ation() { | |
208 | re turn unsu itablePort Count <= t hresholdCo unt | |
209 | && s uitablePor tCount > t hresholdCo unt; | |
210 | } | |
211 | ||
212 | synchr onized boo lean isUsi ngJavaPort Randomizat ion() { | |
213 | re turn unsui tablePortC ount > thr esholdCoun t ; | |
214 | } | |
215 | ||
216 | synchr onized boo lean isUnd ecided() { | |
217 | re turn !isUs ingJavaPor tRandomiza tion() | |
218 | && ! isUsingNat ivePortRan domization (); | |
219 | } | |
220 | ||
221 | privat e Datagram Socket ope nRandom() { | |
222 | in t maxtries = MAX_RAN DOM_TRIES; | |
223 | wh ile (maxtr ies-- > 0) { | |
224 | int port = Ephemer alPortRang e.LOWER | |
225 | + random.n extInt(Eph emeralPort Range.RANG E); | |
226 | try { | |
227 | if ( family != null) { | |
228 | DatagramCh annel c = DatagramCh annel.open (family); | |
229 | try { | |
230 | Datagr amSocket s = c.socke t(); | |
231 | s.bind (new InetS ocketAddre ss(port)); | |
232 | return s; | |
233 | } catch (T hrowable x ) { | |
234 | c.clos e(); | |
235 | throw x; | |
236 | } | |
237 | } | |
238 | retu rn new Dat agramSocke t(port); | |
239 | } catch (IOExcepti on x) { | |
240 | // t ry again u ntil maxtr ies == 0; | |
241 | } | |
242 | } | |
243 | re turn null; | |
244 | } | |
245 | ||
246 | } |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.