Example #1
0
// Load serialized log entries into the IDMap from which they are
// retrievable using the content key.  Conventionally the logEntry
// file is in LFS/U/L, where LFS is the path to the local file system.
//
// We are guaranteed that the file exists and that m is not nil.
//
func loadEntries(pathToFTLog string, m *xi.IDMap, whichSHA int) (
	count int, err error) {

	f, err := os.OpenFile(pathToFTLog, os.O_RDONLY, 0600)
	if err == nil {
		defer f.Close()
		scanner := bufio.NewScanner(f)
		for scanner.Scan() {
			var entry *LogEntry
			line := scanner.Text()
			entry, err = ParseLogEntry(line, whichSHA)
			if err != nil {
				break
			}
			err = m.Insert(entry.key, entry)
			if err != nil {
				break
			}
			count++
		}
	}
	return
}
Example #2
0
// XXX Creating a Node with a list of live connections seems nonsensical.
func New(name string, id *xi.NodeID, lfs string,
	ckPriv, skPriv *rsa.PrivateKey,
	o []xo.OverlayI, e []xt.EndPointI, p []*Peer) (n *Node, err error) {

	// lfs should be a well-formed POSIX path; if the directory does
	// not exist we should create it.
	err = xf.CheckLFS(lfs, 0700)

	// The ckPriv is an RSA key used to encrypt short messages.
	if err == nil {
		if ckPriv == nil {
			ckPriv, err = rsa.GenerateKey(rand.Reader, 2048)
		}
		if err == nil {
			// The skPriv is an RSA key used to create digital signatures.
			if skPriv == nil {
				skPriv, err = rsa.GenerateKey(rand.Reader, 2048)
			}
		}
	}
	// The node communicates through its endpoints.  These are
	// contained in overlays.  If an endpoint in 127.0.0.0/8
	// is in the list of endpoints, that overlay is automatically
	// added to the list of overlays with the name "localhost".
	// Other IPv4 endpoints are assumed to be in 0.0.0.0/0
	// ("globalV4") unless there is another containing overlay
	// except that endpoints in private address space are treated
	// differently.  Unless there is an overlay with a containing
	// address space, addresses in 10/8 are assigned to "privateA",
	// addresses in 172.16/12 are assigned to "privateB", and
	// any in 192.168/16 are assigned to "privateC".  All of these
	// overlays are automatically created unless there is a
	// pre-existing overlay whose address range is the same as one
	// of these are contained within one of them.

	var (
		endPoints []xt.EndPointI
		acceptors []xt.AcceptorI // each must share index with endPoint
		overlays  []xo.OverlayI
		m         *xi.IDMap
		peers     []*Peer // an empty slice
	)

	if err == nil {
		m, err = xi.NewNewIDMap()
	}
	if err == nil {
		if p != nil {
			count := len(p)
			for i := 0; i < count; i++ {
				err = m.Insert(p[i].GetNodeID().Value(), &p[i])
				if err != nil {
					break
				}
				peers = append(peers, p[i])
			}
		}
	}
	if err == nil {
		commsPubKey := &(*ckPriv).PublicKey
		sigPubKey := &(*skPriv).PublicKey

		var baseNode *BaseNode
		baseNode, err = NewBaseNode(name, id, commsPubKey, sigPubKey, overlays)
		if err == nil {
			n = &Node{ckPriv: ckPriv,
				skPriv:    skPriv,
				acceptors: acceptors,
				endPoints: endPoints,
				peers:     peers,
				gateways:  nil,
				lfs:       lfs,
				peerMap:   m,
				BaseNode:  *baseNode}
			if err == nil {
				if o != nil {
					count := len(o)
					for i := 0; i < count; i++ {
						overlays = append(overlays, o[i])
					}
				}
				if e != nil {
					count := len(e)
					for i := 0; i < count; i++ {
						// _, err = addEndPoint(e[i], &endPoints, &acceptors, &overlays)
						_, err = n.AddEndPoint(e[i])
					}
				}
			}
		}
	}
	return
}