@@ -10,16 +10,18 @@ export default class UserInput extends React.Component {
10
10
classful : true ,
11
11
classNameValue : "A" ,
12
12
numberOfHostsNeeded : 0 ,
13
+ generatedIPAddress : "ip..." ,
13
14
addresses : JSON . stringify ( addresses )
14
15
}
16
+ localStorage . clear ( ) ;
15
17
}
16
18
17
19
handleclassNameChange ( event ) {
18
20
this . setState ( { classNameValue : event . target . value } )
19
21
}
20
22
21
23
handleclassNameRadioButtonChange ( bool ) {
22
- this . setState ( { classNameful : bool } ) ;
24
+ this . setState ( { classful : bool } ) ;
23
25
}
24
26
25
27
handleSubmit ( event ) {
@@ -31,10 +33,6 @@ export default class UserInput extends React.Component {
31
33
return Math . floor ( Math . random ( ) * Math . floor ( max ) ) ;
32
34
}
33
35
34
- dec2bin ( dec ) {
35
- return ( dec >>> 0 ) . toString ( 2 ) ;
36
- }
37
-
38
36
generateIPv4Address ( ) {
39
37
console . log ( this . state . addresses ) ;
40
38
let map ;
@@ -45,37 +43,51 @@ export default class UserInput extends React.Component {
45
43
}
46
44
let randomAddress ;
47
45
let r = 0 ;
48
- if ( this . state . classful ) {
49
- if ( this . state . classNameValue === "A" ) {
50
- r += ( this . getRandomInt ( 128 ) ) << 24 ;
51
- } else if ( this . state . classNameValue === "B" ) {
52
- r += ( this . getRandomInt ( 64 ) + 128 ) << 24 ;
53
- r += ( this . getRandomInt ( 256 ) << 16 ) ;
54
- } else if ( this . state . classNameValue === "C" ) {
55
- r += ( this . getRandomInt ( 32 ) + 192 ) << 24 ;
56
- r += ( this . getRandomInt ( 256 ) ) << 16 ;
57
- r += ( this . getRandomInt ( 256 ) ) << 8 ;
58
- } else if ( this . state . classNameValue === "D" ) {
59
- r += ( this . getRandomInt ( 16 ) + 224 ) << 24 ;
60
- r += ( this . getRandomInt ( 256 ) ) << 16 ;
61
- r += ( this . getRandomInt ( 256 ) ) << 8 ;
62
- r += ( this . getRandomInt ( 256 ) ) ;
63
- } else if ( this . state . classNameValue === "E" ) {
64
- r += Math . ceil ( Math . log2 ( this . state . numberOfHostsNeeded ) ) ;
65
- console . elog ( r ) ;
46
+ do {
47
+ if ( this . state . classful ) {
48
+ console . log ( "classful!" ) ;
49
+ if ( this . state . classNameValue === "A" ) {
50
+ r += ( this . getRandomInt ( 128 ) ) << 24 ;
51
+ } else if ( this . state . classNameValue === "B" ) {
52
+ r += ( this . getRandomInt ( 64 ) + 128 ) << 24 ;
53
+ r += ( this . getRandomInt ( 256 ) << 16 ) ;
54
+ } else if ( this . state . classNameValue === "C" ) {
55
+ r += ( this . getRandomInt ( 32 ) + 192 ) << 24 ;
56
+ r += ( this . getRandomInt ( 256 ) ) << 16 ;
57
+ r += ( this . getRandomInt ( 256 ) ) << 8 ;
58
+ } else if ( this . state . classNameValue === "D" ) {
59
+ r += ( this . getRandomInt ( 16 ) + 224 ) << 24 ;
60
+ r += ( this . getRandomInt ( 256 ) ) << 16 ;
61
+ r += ( this . getRandomInt ( 256 ) ) << 8 ;
62
+ r += ( this . getRandomInt ( 256 ) ) ;
63
+ } else if ( this . state . classNameValue === "E" ) {
64
+ console . log ( "Cannot assign class E!" ) ;
65
+ }
66
+ } else {
67
+ let prefixBits = Math . ceil ( Math . log2 ( this . state . numberOfHostsNeeded ) ) ;
68
+ r += this . getRandomInt ( Math . pow ( 2 , 32 - prefixBits ) ) ;
69
+ r = r << ( prefixBits ) ;
66
70
}
67
- } else {
68
-
69
- }
71
+ randomAddress = this . intToIPAddressString ( r ) ;
72
+ } while ( ( randomAddress in map ) ) ;
70
73
71
- randomAddress = r . toString ( ) ;
72
- console . log ( map ) ;
73
- if ( ! ( randomAddress in map ) ) map [ randomAddress ] = "yes" ;
74
- else console . log ( "Address already existed!" ) ;
75
-
74
+ this . setState ( { generatedIPAddress : randomAddress } ) ;
75
+ map [ randomAddress ] = true ;
76
76
localStorage . setItem ( "addresses" , JSON . stringify ( map ) ) ;
77
- console . log ( r . toString ( 2 ) ) ;
78
- // localStorage.clear();
77
+ console . log ( map )
78
+ }
79
+
80
+ intToIPAddressString ( ip ) {
81
+ let bytes = [ 4 ] ;
82
+ bytes [ 3 ] = ip & 0xFF ;
83
+ bytes [ 2 ] = ( ip >> 8 ) & 0xFF ;
84
+ bytes [ 1 ] = ( ip >> 16 ) & 0xFF ;
85
+ bytes [ 0 ] = ( ip >> 24 ) & 0xFF ;
86
+ return `${ bytes [ 0 ] } .${ bytes [ 1 ] } .${ bytes [ 2 ] } .${ bytes [ 3 ] } ` ;
87
+ }
88
+
89
+ sleep ( ms ) {
90
+ return new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
79
91
}
80
92
81
93
handleNumberOfHostsChange ( event ) {
@@ -87,18 +99,18 @@ export default class UserInput extends React.Component {
87
99
< div >
88
100
< form onSubmit = { e => this . handleSubmit ( e ) } >
89
101
< div className = "form-group-row" >
90
- < legend className = "col-form-label" > classNameful or classNameless ?</ legend >
102
+ < legend className = "col-form-label" > classful or classless ?</ legend >
91
103
< div className = "radio" >
92
104
< label >
93
105
< input type = "radio"
94
- checked = { this . state . classNameful === true }
106
+ checked = { this . state . classful === true }
95
107
onChange = { e => this . handleclassNameRadioButtonChange ( true ) } />
96
108
classful
97
109
</ label >
98
110
</ div >
99
111
< div className = "form-group" >
100
112
< label htmlFor = "classNameSelect" > Pick addressing className</ label >
101
- < select className = "form-control" id = "classNameSelect" value = { this . state . classNameValue } onChange = { e => this . handleclassNameChange ( e ) } disabled = { ! this . state . classNameful } >
113
+ < select className = "form-control" id = "classNameSelect" value = { this . state . classNameValue } onChange = { e => this . handleclassNameChange ( e ) } disabled = { ! this . state . classful } >
102
114
< option > A</ option >
103
115
< option > B</ option >
104
116
< option > C</ option >
@@ -109,14 +121,14 @@ export default class UserInput extends React.Component {
109
121
< div className = "radio" >
110
122
< label >
111
123
< input type = "radio"
112
- checked = { this . state . classNameful === false }
124
+ checked = { this . state . classful === false }
113
125
onChange = { e => this . handleclassNameRadioButtonChange ( false ) } />
114
126
classless
115
127
</ label >
116
128
</ div >
117
129
< div className = "input-group" >
118
130
< span className = "input-group-addon" id = "basic-addon1" > Number of hosts needed:</ span >
119
- < input type = "number" className = "form-control" placeholder = "hosts.." aria-describedby = "basic-addon1" disabled = { this . state . classNameful } onChange = { e => { this . handleNumberOfHostsChange ( e ) } } />
131
+ < input type = "number" className = "form-control" placeholder = "hosts.." aria-describedby = "basic-addon1" disabled = { this . state . classful } onChange = { e => { this . handleNumberOfHostsChange ( e ) } } />
120
132
</ div >
121
133
< br />
122
134
< button type = "submit" value = "submit" className = "btn btn-primary" > Assign!</ button >
@@ -126,6 +138,17 @@ export default class UserInput extends React.Component {
126
138
)
127
139
}
128
140
141
+ getIPAddressesDisplay ( ) {
142
+ return (
143
+ < div >
144
+ < div className = "form-group" >
145
+ < label for = "exampleFormControlInput1" > Your IP address:</ label >
146
+ < input className = "form-control" type = "text" name = "country" value = { this . state . generatedIPAddress } readOnly />
147
+ </ div >
148
+ </ div >
149
+ )
150
+ }
151
+
129
152
getIPv6TabContent ( ) {
130
153
return (
131
154
< div >
@@ -152,6 +175,8 @@ export default class UserInput extends React.Component {
152
175
{ this . getIPv6TabContent ( ) }
153
176
</ div >
154
177
</ div >
178
+ < br > </ br >
179
+ { this . getIPAddressesDisplay ( ) }
155
180
</ div >
156
181
)
157
182
}
0 commit comments