Exemple #1
0
func NewIncomingPeer(ipstr string) (p *onePeer, e error) {
	x := strings.Index(ipstr, ":")
	if x != -1 {
		ipstr = ipstr[:x] // remove port number
	}
	ip := net.ParseIP(ipstr)
	if ip != nil && len(ip) == 16 {
		if common.IsIPBlocked(ip[12:16]) {
			e = errors.New(ipstr + " is blocked")
			return
		}
		p = new(onePeer)
		copy(p.Ip4[:], ip[12:16])
		p.Services = common.Services
		copy(p.Ip6[:], ip[:12])
		p.Port = common.DefaultTcpPort
		if dbp := PeerDB.Get(qdb.KeyType(p.UniqID())); dbp != nil && NewPeer(dbp).Banned != 0 {
			e = errors.New(p.Ip() + " is banned")
			p = nil
		} else {
			p.Time = uint32(time.Now().Unix())
			p.Save()
		}
	} else {
		e = errors.New("Error parsing IP '" + ipstr + "'")
	}
	return
}
Exemple #2
0
// Fetch a given number of best (most recenty seen) peers.
// Set unconnected to true to only get those that we are not connected to.
func GetBestPeers(limit uint, unconnected bool) (res manyPeers) {
	if proxyPeer != nil {
		if !unconnected || !ConnectionActive(proxyPeer) {
			return manyPeers{proxyPeer}
		}
		return manyPeers{}
	}
	peerdb_mutex.Lock()
	tmp := make(manyPeers, 0)
	PeerDB.Browse(func(k qdb.KeyType, v []byte) uint32 {
		ad := NewPeer(v)
		if ad.Banned == 0 && ValidIp4(ad.Ip4[:]) && !common.IsIPBlocked(ad.Ip4[:]) {
			if !unconnected || !ConnectionActive(ad) {
				tmp = append(tmp, ad)
			}
		}
		return 0
	})
	peerdb_mutex.Unlock()
	// Copy the top rows to the result buffer
	if len(tmp) > 0 {
		sort.Sort(tmp)
		if uint(len(tmp)) < limit {
			limit = uint(len(tmp))
		}
		res = make(manyPeers, limit)
		copy(res, tmp[:limit])
	}
	return
}