1
1
import React from 'react' ;
2
2
import autoBind from 'react-autobind' ;
3
+ import style from './style.css' ;
3
4
4
5
export default class UserInput extends React . Component {
5
6
constructor ( props ) {
@@ -14,9 +15,11 @@ export default class UserInput extends React.Component {
14
15
numberOfNetworksNeeded : 0 ,
15
16
generatedIPAddress : "" ,
16
17
ipAddressInBinary : "" ,
18
+ IPv6AddressInColonHex : "" ,
19
+ IPv6AddressInBinary : "" ,
17
20
addresses : JSON . stringify ( addresses )
18
21
}
19
- localStorage . clear ( ) ;
22
+ // localStorage.clear();
20
23
}
21
24
22
25
handleclassNameChange ( event ) {
@@ -63,24 +66,28 @@ export default class UserInput extends React.Component {
63
66
let randomAddress ;
64
67
let binaryString ;
65
68
let r = 0 ;
69
+ let subnetMask = 0 ;
66
70
do {
67
71
if ( this . state . classful ) {
68
72
if ( this . state . classNameValue === "A" ) {
69
- r = ( this . getRandomInt ( 128 ) ) << 24 ;
73
+ subnetMask = 24 ;
74
+ r = ( this . getRandomInt ( 128 ) ) << subnetMask ;
70
75
binaryString = r . toString ( 2 ) ;
71
76
while ( binaryString . length < 31 )
72
77
binaryString = "0" + binaryString ;
73
78
binaryString = "0" + binaryString ;
74
79
75
80
} else if ( this . state . classNameValue === "B" ) {
76
- r = ( this . getRandomInt ( 16384 ) ) << 16 ;
81
+ subnetMask = 16 ;
82
+ r = ( this . getRandomInt ( 16384 ) ) << subnetMask ;
77
83
binaryString = r . toString ( 2 ) ;
78
84
while ( binaryString . length < 30 )
79
85
binaryString = "0" + binaryString ;
80
86
binaryString = "10" + binaryString ;
81
87
82
88
} else if ( this . state . classNameValue === "C" ) {
83
- r = ( this . getRandomInt ( 2097152 ) ) << 8 ;
89
+ subnetMask = 8 ;
90
+ r = ( this . getRandomInt ( 2097152 ) ) << subnetMask ;
84
91
binaryString = r . toString ( 2 ) ;
85
92
while ( binaryString . length < 29 )
86
93
binaryString = "0" + binaryString ;
@@ -94,24 +101,28 @@ export default class UserInput extends React.Component {
94
101
95
102
} else if ( this . state . classNameValue === "E" ) {
96
103
this . setState ( { generatedIPAddress : "Cannot assign class E addressses!" } ) ;
97
- this . setState ( { ipAddressInBinary : "Cannot assign class E addressses!" } ) ;
104
+ this . setState ( { ipAddressInBinary : "Cannot assign class E addressses!" } ) ;
98
105
return ;
99
106
}
100
107
} else {
101
- if ( this . state . numberOfHostsNeeded === 0 ) return ;
108
+ let numberOfHostsNeeded = this . state . numberOfHostsNeeded ;
109
+ if ( numberOfHostsNeeded === 0 || numberOfHostsNeeded == "" ) return ;
102
110
let suffixBits = Math . ceil ( Math . log2 ( this . state . numberOfHostsNeeded ) ) ;
103
- r = this . getRandomInt ( Math . pow ( 2 , 32 - suffixBits ) ) ;
111
+ subnetMask = 32 - suffixBits ;
112
+ r = this . getRandomInt ( Math . pow ( 2 , subnetMask ) ) ;
104
113
r = r << ( suffixBits ) ;
105
114
binaryString = r . toString ( 2 ) ;
106
115
if ( binaryString [ 0 ] === '-' ) binaryString = binaryString . substr ( 1 ) ;
107
- while ( binaryString . length < 32 )
116
+ while ( binaryString . length < 32 ) {
108
117
binaryString = "0" + binaryString ;
118
+ this . sleep ( 1000 ) ;
119
+ }
109
120
}
110
121
111
- } while ( ( r in map ) ) ;
122
+ } while ( r in map ) ;
123
+
112
124
map [ r ] = true ;
113
-
114
- randomAddress = this . intToIPAddressString ( r ) ;
125
+ randomAddress = this . intToIPAddressString ( r ) + "/" + subnetMask . toString ( ) ;
115
126
this . setState ( { generatedIPAddress : randomAddress } ) ;
116
127
this . setState ( { ipAddressInBinary : binaryString } ) ;
117
128
@@ -121,7 +132,6 @@ export default class UserInput extends React.Component {
121
132
generateIPv6Address ( ) {
122
133
let numberOfNetworksNeeded = this . state . numberOfNetworksNeeded ;
123
134
let zeroCompression = this . state . zeroCompression ;
124
- // console.log(`numberOfHostsNeeded: ${numberOfNetworksNeeded}\nzeroCompression: ${zeroCompression}`);
125
135
126
136
let map ;
127
137
if ( localStorage . getItem ( "IPv6Addresses" ) !== null ) {
@@ -137,29 +147,54 @@ export default class UserInput extends React.Component {
137
147
}
138
148
139
149
let suffixBits = 64 - networkBits ;
150
+ let randomNumber = 0 ;
151
+ let result ;
152
+ do {
153
+ randomNumber = this . getRandomInt ( Math . pow ( 2 , suffixBits ) ) ;
154
+ for ( let i = randomNumber . toString ( 2 ) . length ; i < 64 ; i ++ ) {
155
+ randomNumber = randomNumber * 2 ;
156
+ }
140
157
141
- let randomNumber = this . getRandomInt ( Math . pow ( 2 , suffixBits ) ) ;
142
- for ( let i = randomNumber . toString ( 2 ) . length ; i < 64 ; i ++ ) {
143
- randomNumber = randomNumber * 2 ;
144
- }
158
+ let hexString = randomNumber . toString ( 16 ) ;
159
+ result = "" ;
145
160
146
- let hexString = randomNumber . toString ( 16 ) ;
147
- let result = "" ;
161
+ let count = 0 ;
162
+ for ( let i = 0 ; i < hexString . length ; ) {
163
+ if ( count === 4 ) {
164
+ result += ":" ;
165
+ count = 0 ;
166
+ } else {
167
+ let c = hexString . charAt ( i ) ;
168
+ result += c ;
169
+ count ++ ;
170
+ i ++ ;
171
+ }
172
+ }
173
+ result += ":0000:0000:0000:0000" ;
174
+
175
+ let array = result . split ( ":" ) ;
176
+ let compressed = "::" ;
177
+ let foundNonZeroes = false ;
178
+ if ( zeroCompression ) {
179
+ for ( let i = array . length - 1 ; i >= 0 ; i -- ) {
180
+ let s = array [ i ] ;
181
+ if ( s !== "0000" || foundNonZeroes ) {
182
+ compressed = ":" + parseInt ( s , 16 ) . toString ( 16 ) + compressed ;
183
+ foundNonZeroes = true ;
184
+ }
185
+ }
148
186
149
- let count = 0 ;
150
- for ( let i = 0 ; i < hexString . length ; ) {
151
- if ( count === 4 ) {
152
- result += ":" ;
153
- count = 0 ;
154
- } else {
155
- let c = hexString . charAt ( i ) ;
156
- result += c ;
157
- count ++ ;
158
- i ++ ;
187
+ result = compressed . substr ( 1 ) ;
159
188
}
160
- }
161
- result += ":0000:0000:0000:0000" ;
162
- console . log ( result ) ;
189
+
190
+ result += "/" + ( 64 - networkBits ) . toString ( ) ;
191
+ } while ( result in map ) ;
192
+
193
+ map [ result ] = true ;
194
+ localStorage . setItem ( "IPv6Addresses" , JSON . stringify ( map ) ) ;
195
+
196
+ this . setState ( { IPv6AddressInColonHex : result } ) ;
197
+ this . setState ( { IPv6AddressInBinary : randomNumber . toString ( 2 ) } ) ;
163
198
}
164
199
165
200
intToIPAddressString ( ip ) {
@@ -219,11 +254,11 @@ export default class UserInput extends React.Component {
219
254
< div >
220
255
< div className = "form-group" >
221
256
< label htmlFor = "exampleFormControlInput1" > Your IPv4 address in decimal:</ label >
222
- < input className = "form-control" type = "text" name = "country" value = { this . state . generatedIPAddress } readOnly />
257
+ < input className = { "form-control " + style . courier } type = "text" name = "country" value = { this . state . generatedIPAddress } readOnly />
223
258
</ div >
224
259
< div className = "form-group" >
225
260
< label htmlFor = "exampleFormControlInput1" > Your IPv4 address in binary:</ label >
226
- < input className = "form-control" type = "text" name = "country" value = { this . state . ipAddressInBinary } readOnly />
261
+ < input className = { "form-control " + style . courier } type = "text" name = "country" value = { this . state . ipAddressInBinary } readOnly />
227
262
</ div >
228
263
</ div >
229
264
</ div >
@@ -263,11 +298,11 @@ export default class UserInput extends React.Component {
263
298
< div >
264
299
< div className = "form-group" >
265
300
< label htmlFor = "exampleFormControlInput1" > Your IPv6 address in colon hex:</ label >
266
- < input className = "form-control" type = "text" name = "country" value = { this . state . generatedIPAddress } readOnly />
301
+ < input className = { "form-control " + style . courier } type = "text" name = "country" value = { this . state . IPv6AddressInColonHex } readOnly />
267
302
</ div >
268
303
< div className = "form-group" >
269
304
< label htmlFor = "exampleFormControlInput1" > Your IPv6 address in binary:</ label >
270
- < input className = "form-control" type = "text" name = "country" value = { this . state . ipAddressInBinary } readOnly />
305
+ < input className = { "form-control " + style . courier } type = "text" name = "country" value = { this . state . IPv6AddressInBinary } readOnly />
271
306
</ div >
272
307
</ div >
273
308
</ div >
@@ -285,6 +320,7 @@ export default class UserInput extends React.Component {
285
320
</ ul >
286
321
287
322
< div className = "tab-content" >
323
+ < br > </ br >
288
324
< div id = "IPv4Tab" className = "tab-pane fade in active" >
289
325
{ this . getIPv4TabContent ( ) }
290
326
</ div >
0 commit comments