@@ -181,6 +181,18 @@ type HostInfo struct {
181181 tokens []string
182182}
183183
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+
184196func (h * HostInfo ) Equal (host * HostInfo ) bool {
185197 if h == host {
186198 // prevent rlock reentry
@@ -213,14 +225,12 @@ func (h *HostInfo) connectAddressLocked() (net.IP, string) {
213225 } else if validIpAddr (h .rpcAddress ) {
214226 return h .rpcAddress , "rpc_adress"
215227 } else if validIpAddr (h .preferredIP ) {
216- // where does perferred_ip get set?
217228 return h .preferredIP , "preferred_ip"
218229 } else if validIpAddr (h .broadcastAddress ) {
219230 return h .broadcastAddress , "broadcast_address"
220- } else if validIpAddr (h .peer ) {
221- return h .peer , "peer"
222231 }
223- return net .IPv4zero , "invalid"
232+ return h .peer , "peer"
233+
224234}
225235
226236// nodeToNodeAddress returns address broadcasted between node to nodes.
@@ -240,24 +250,13 @@ func (h *HostInfo) nodeToNodeAddress() net.IP {
240250}
241251
242252// 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
245254func (h * HostInfo ) ConnectAddress () net.IP {
246255 h .mu .RLock ()
247256 defer h .mu .RUnlock ()
248257
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
261260}
262261
263262func (h * HostInfo ) BroadcastAddress () net.IP {
@@ -491,6 +490,10 @@ func checkSystemSchema(control *controlConn) (bool, error) {
491490 return true , nil
492491}
493492
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+
494497// Given a map that represents a row from either system.local or system.peers
495498// return as much information as we can in *HostInfo
496499func (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) (*
606609 }
607610
608611 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+ }
609615 host .connectAddress = ip
610616 host .port = port
611617
@@ -623,7 +629,7 @@ func (s *Session) hostInfoFromIter(iter *Iter, connectAddress net.IP, defaultPor
623629 return nil , errors .New ("query returned 0 rows" )
624630 }
625631
626- host , err := s .hostInfoFromMap ( rows [ 0 ], & HostInfo { connectAddress : connectAddress , port : defaultPort } )
632+ host , err := s .newHostInfoFromMap ( connectAddress , defaultPort , rows [ 0 ] )
627633 if err != nil {
628634 return nil , err
629635 }
@@ -674,7 +680,7 @@ func (r *ringDescriber) getClusterPeerInfo(localHost *HostInfo) ([]*HostInfo, er
674680
675681 for _ , row := range rows {
676682 // 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 )
678684 if err != nil {
679685 return nil , err
680686 } else if ! isValidPeer (host ) {
0 commit comments