@@ -181,6 +181,18 @@ type HostInfo struct {
181
181
tokens []string
182
182
}
183
183
184
+ func newHostInfo (addr net.IP , port int ) (* HostInfo , error ) {
185
+ if ! validIpAddr (addr ) {
186
+ return nil , errors .New ("invalid host address" )
187
+ }
188
+ host := & HostInfo {}
189
+ host .hostname = addr .String ()
190
+ host .port = port
191
+
192
+ host .connectAddress = addr
193
+ return host , nil
194
+ }
195
+
184
196
func (h * HostInfo ) Equal (host * HostInfo ) bool {
185
197
if h == host {
186
198
// prevent rlock reentry
@@ -213,14 +225,12 @@ func (h *HostInfo) connectAddressLocked() (net.IP, string) {
213
225
} else if validIpAddr (h .rpcAddress ) {
214
226
return h .rpcAddress , "rpc_adress"
215
227
} else if validIpAddr (h .preferredIP ) {
216
- // where does perferred_ip get set?
217
228
return h .preferredIP , "preferred_ip"
218
229
} else if validIpAddr (h .broadcastAddress ) {
219
230
return h .broadcastAddress , "broadcast_address"
220
- } else if validIpAddr (h .peer ) {
221
- return h .peer , "peer"
222
231
}
223
- return net .IPv4zero , "invalid"
232
+ return h .peer , "peer"
233
+
224
234
}
225
235
226
236
// nodeToNodeAddress returns address broadcasted between node to nodes.
@@ -240,24 +250,13 @@ func (h *HostInfo) nodeToNodeAddress() net.IP {
240
250
}
241
251
242
252
// Returns the address that should be used to connect to the host.
243
- // If you wish to override this, use an AddressTranslator or
244
- // use a HostFilter to SetConnectAddress()
253
+ // If you wish to override this, use an AddressTranslator
245
254
func (h * HostInfo ) ConnectAddress () net.IP {
246
255
h .mu .RLock ()
247
256
defer h .mu .RUnlock ()
248
257
249
- if addr , _ := h .connectAddressLocked (); validIpAddr (addr ) {
250
- return addr
251
- }
252
- panic (fmt .Sprintf ("no valid connect address for host: %v. Is your cluster configured correctly?" , h ))
253
- }
254
-
255
- func (h * HostInfo ) SetConnectAddress (address net.IP ) * HostInfo {
256
- // TODO(zariel): should this not be exported?
257
- h .mu .Lock ()
258
- defer h .mu .Unlock ()
259
- h .connectAddress = address
260
- return h
258
+ addr , _ := h .connectAddressLocked ()
259
+ return addr
261
260
}
262
261
263
262
func (h * HostInfo ) BroadcastAddress () net.IP {
@@ -491,6 +490,10 @@ func checkSystemSchema(control *controlConn) (bool, error) {
491
490
return true , nil
492
491
}
493
492
493
+ func (s * Session ) newHostInfoFromMap (addr net.IP , port int , row map [string ]interface {}) (* HostInfo , error ) {
494
+ return s .hostInfoFromMap (row , & HostInfo {connectAddress : addr , port : port })
495
+ }
496
+
494
497
// Given a map that represents a row from either system.local or system.peers
495
498
// return as much information as we can in *HostInfo
496
499
func (s * Session ) hostInfoFromMap (row map [string ]interface {}, host * HostInfo ) (* HostInfo , error ) {
@@ -606,6 +609,9 @@ func (s *Session) hostInfoFromMap(row map[string]interface{}, host *HostInfo) (*
606
609
}
607
610
608
611
ip , port := s .cfg .translateAddressPort (host .ConnectAddress (), host .port )
612
+ if ! validIpAddr (ip ) {
613
+ return nil , fmt .Errorf ("invalid host address (before translation: %v:%v, after translation: %v:%v)" , host .ConnectAddress (), host .port , ip .String (), port )
614
+ }
609
615
host .connectAddress = ip
610
616
host .port = port
611
617
@@ -623,7 +629,7 @@ func (s *Session) hostInfoFromIter(iter *Iter, connectAddress net.IP, defaultPor
623
629
return nil , errors .New ("query returned 0 rows" )
624
630
}
625
631
626
- host , err := s .hostInfoFromMap ( rows [ 0 ], & HostInfo { connectAddress : connectAddress , port : defaultPort } )
632
+ host , err := s .newHostInfoFromMap ( connectAddress , defaultPort , rows [ 0 ] )
627
633
if err != nil {
628
634
return nil , err
629
635
}
@@ -674,7 +680,7 @@ func (r *ringDescriber) getClusterPeerInfo(localHost *HostInfo) ([]*HostInfo, er
674
680
675
681
for _ , row := range rows {
676
682
// extract all available info about the peer
677
- host , err := r .session .hostInfoFromMap ( row , & HostInfo { port : r .session .cfg .Port } )
683
+ host , err := r .session .newHostInfoFromMap ( nil , r .session .cfg .Port , row )
678
684
if err != nil {
679
685
return nil , err
680
686
} else if ! isValidPeer (host ) {
0 commit comments