예제 #1
0
파일: peer.go 프로젝트: jddixon/xlNode_go
func CollectConnectors(peer *Peer, ss []string) (rest []string, err error) {
	rest = ss
	line, err := xc.NextNBLine(&rest)
	if err == nil {
		if line == "connectors {" {
			for {
				line, err = xc.NextNBLine(&rest)
				if err == nil {
					if line == "}" {
						break
					}
				}
				var ctor xt.ConnectorI
				ctor, err = xt.ParseConnector(line)
				if err != nil {
					return
				}
				err = peer.AddConnector(ctor)
				if err != nil {
					return
				}
			}
		}
	}
	// if there are no connectors, not a very useful peer
	return
}
예제 #2
0
func collectMyEnds(mi *ClientInfo, ss []string) (rest []string, err error) {
	rest = ss
	var line string
	line, err = xc.NextNBLine(&rest)
	if err == nil {
		if line == "endPoints {" {
			for {
				line = strings.TrimSpace(rest[0]) // peek
				if line == "}" {
					break
				}
				line, err = xc.NextNBLine(&rest)
				if err == nil {
					// XXX NO CHECK THAT THIS IS A VALID ENDPOINT
					mi.MyEnds = append(mi.MyEnds, line)
				}
			}
			if err == nil {
				line, err = xc.NextNBLine(&rest)
				if err == nil {
					if line != "}" {
						err = MissingClosingBrace
					}
				}
			}
		} else {
			err = MissingEndPointsSection
		}
	}
	return
}
예제 #3
0
func collectClientAttrs(mi *ClientInfo, ss []string) (rest []string, err error) {
	rest = ss
	var line string
	line, err = xc.NextNBLine(&rest) // trims
	if err == nil {
		// attrs line looks like "attrs: 0xHHHH..." where H is a hex digit
		if strings.HasPrefix(line, "attrs: 0x") {
			var val []byte
			var attrs uint64
			line := line[9:]
			val, err = hex.DecodeString(line)
			if err == nil {
				if len(val) != 8 {
					err = WrongNumberOfBytesInAttrs
				} else {
					for i := 0; i < 8; i++ {
						// assume little-endian ; but printf has put
						// high order bytes first - ie, it's big-endian
						attrs |= uint64(val[i]) << uint(8*(7-i))
					}
					mi.Attrs = attrs
				}
			}
		} else {
			err = BadAttrsLine
		}
	}
	return
}
예제 #4
0
파일: peer.go 프로젝트: jddixon/xlNode_go
func ParsePeerFromStrings(ss []string) (peer *Peer, rest []string, err error) {
	bn, rest, err := ParseBNFromStrings(ss, "peer")
	if err == nil {
		peer = &Peer{BaseNode: *bn}
		rest, err = CollectConnectors(peer, rest)
		if err == nil {
			var line string
			line, err = xc.NextNBLine(&rest)
			if err == nil {
				if line != "}" {
					err = NotASerializedPeer
				}
			}
		}
	}
	return
}
예제 #5
0
func ParseClientInfoFromStrings(ss []string) (
	mi *ClientInfo, rest []string, err error) {

	bn, rest, err := xn.ParseBNFromStrings(ss, "clientInfo")
	if err == nil {
		mi = &ClientInfo{BaseNode: *bn}
		rest, err = collectClientAttrs(mi, rest)
		if err == nil {
			rest, err = collectMyEnds(mi, rest)
		}
		if err == nil {
			var line string
			// expect and consume a closing brace
			line, err = xc.NextNBLine(&rest)
			if err == nil {
				if line != "}" {
					err = MissingClosingBrace
				}
			}
		}
	}
	return
}
예제 #6
0
파일: node.go 프로젝트: jddixon/xlNode_go
func ParseFromStrings(ss []string) (node *Node, rest []string, err error) {

	var line string
	var m *xi.IDMap
	bn, rest, err := ParseBNFromStrings(ss, "node")
	if err == nil {
		node = &Node{BaseNode: *bn}
		m, err = xi.NewNewIDMap()
		if err == nil {
			node.peerMap = m
		}
	}
	if err == nil {
		line, err = xc.NextNBLine(&rest)
	}
	if err == nil {
		parts := strings.Split(line, ": ")
		if parts[0] == "lfs" {
			node.lfs = strings.TrimSpace(parts[1])
		} else {
			fmt.Println("MISSING LFS")
			err = NotASerializedNode
		}

		var ckPriv, skPriv *rsa.PrivateKey
		if err == nil {
			// move some of this into ExpectRSAPrivateKey() !
			line, err = xc.NextNBLine(&rest)
			if err == nil {
				parts = strings.Split(line, ": ")
				if parts[0] == "ckPriv" && parts[1] == "-----BEGIN -----" {
					ckPriv, err = ExpectRSAPrivateKey(&rest)
					node.ckPriv = ckPriv
				} else {
					fmt.Println("MISSING OR ILL-FORMED COMMS_KEY")
					err = NotASerializedNode
				}
			}
		} // FOO

		if err == nil {
			// move some of this into ExpectRSAPrivateKey() !
			line, err = xc.NextNBLine(&rest)
			if err == nil {
				parts = strings.Split(line, ": ")
				if parts[0] == "skPriv" && parts[1] == "-----BEGIN -----" {
					skPriv, err = ExpectRSAPrivateKey(&rest)
					node.skPriv = skPriv
				} else {
					fmt.Println("MISSING OR ILL-FORMED SIG_KEY")
					err = NotASerializedNode
				}
			}
		} // FOO

		// endPoints
		if err == nil {
			line, err = xc.NextNBLine(&rest)
		}
		if err == nil {
			if line == "endPoints {" {
				for err == nil {
					line, err = xc.NextNBLine(&rest)
					if err != nil {
						break
					}
					if line == "}" {
						// prepend := []string{line}
						// rest = append(prepend, rest...)
						break
					}
					var ep xt.EndPointI
					ep, err = xt.ParseEndPoint(line)
					if err != nil {
						break
					}
					_, err = node.AddEndPoint(ep)
					if err != nil {
						break
					}
				}
			} else {
				fmt.Println("MISSING END_POINTS BLOCK")
				fmt.Printf("    EXPECTED 'endPoints {', GOT: '%s'\n", line)
				err = NotASerializedNode
			}
		}

		// peers
		if err == nil {
			line, err = xc.NextNBLine(&rest)
		}
		if err == nil {
			if line == "peers {" {
				for {
					line = strings.TrimSpace(rest[0])
					if line == "}" { // ZZZ
						break
					}
					var peer *Peer
					peer, rest, err = ParsePeerFromStrings(rest)
					if err != nil {
						break
					}
					_, err = node.AddPeer(peer)
					if err != nil {
						break
					}
				}
			} else {
				fmt.Println("MISSING PEERS BLOCK")
				fmt.Printf("    EXPECTED 'peers {', GOT: '%s'\n", line)
				err = NotASerializedNode
			}
			line, err = xc.NextNBLine(&rest) // discard the ZZZ }

		}
		// gateways, but not yet
		// XXX STUB XXX

		// expect closing brace for node {
		// XXX we need an expect(&rest)

		line, err = xc.NextNBLine(&rest)
		if err == nil {
			if line != "}" {
				fmt.Printf("extra text at end of node declaration: '%s'\n", line)
			}
		}
	}
	if err != nil {
		node = nil
	}
	return
}
예제 #7
0
func ParseRegClusterFromStrings(ss []string) (
	rc *RegCluster, rest []string, err error) {

	var (
		attrs            uint64
		name             string
		id               *xi.NodeID
		epCount, maxSize uint32
	)
	rest = ss

	var line string
	line, err = xc.NextNBLine(&rest) // the line is trimmed
	if err == nil {
		if line != "regCluster {" {
			fmt.Println("MISSING regCluster {")
			err = IllFormedCluster
		} else {
			line, err = xc.NextNBLine(&rest)
			if err == nil {
				if strings.HasPrefix(line, "Attrs: ") {
					var i int64
					i, err = strconv.ParseInt(line[7:], 0, 64)
					if err == nil {
						attrs = uint64(i)
					}
				} else {
					fmt.Printf("BAD ATTRS in line '%s'", line)
					err = IllFormedCluster
				}
			}
		}
	}
	if err == nil {
		line, err = xc.NextNBLine(&rest)
		if err == nil {
			if strings.HasPrefix(line, "Name: ") {
				name = line[6:]
			} else {
				fmt.Printf("BAD NAME in line '%s'", line)
				err = IllFormedCluster
			}
		}
	}
	if err == nil {
		// collect ID
		line, err = xc.NextNBLine(&rest)
		if err == nil {
			if strings.HasPrefix(line, "ID: ") {
				var val []byte
				val, err = hex.DecodeString(line[4:])
				if err == nil {
					id, err = xi.New(val)
				}
			} else {
				fmt.Println("BAD ID")
				err = IllFormedCluster
			}
		}
	}
	if err == nil {
		line, err = xc.NextNBLine(&rest)
		if err == nil {
			if strings.HasPrefix(line, "epCount: ") {
				var count int
				count, err = strconv.Atoi(line[9:])
				if err == nil {
					epCount = uint32(count)
				}
			} else {
				fmt.Println("BAD END POINT COUNT")
				err = IllFormedCluster
			}
		}
	}
	if err == nil {
		line, err = xc.NextNBLine(&rest)
		if err == nil {
			if strings.HasPrefix(line, "maxSize: ") {
				var size int
				size, err = strconv.Atoi(line[9:])
				if err == nil {
					maxSize = uint32(size)
				}
			} else {
				fmt.Println("BAD MAX_SIZE")
				err = IllFormedCluster
			}
		}
	}
	if err == nil {
		rc, err = NewRegCluster(name, id, attrs, maxSize, epCount)
	}
	if err == nil {
		line, err = xc.NextNBLine(&rest)
		if err == nil {
			if line == "Members {" {
				for {
					line = strings.TrimSpace(rest[0]) // peek
					if line == "}" {
						break
					}
					var member *ClientInfo
					member, rest, err = ParseClientInfoFromStrings(rest)
					if err != nil {
						break
					}
					err = rc.AddMember(member)
					if err != nil {
						break
					}
				}
			} else {
				err = MissingMembersList
			}
		}
	}

	// expect closing brace for Members list
	if err == nil {
		line, err = xc.NextNBLine(&rest)
		if err == nil {
			if line != "}" {
				err = MissingClosingBrace
			}
		}
	}
	// expect closing brace  for cluster
	if err == nil {
		line, err = xc.NextNBLine(&rest)
		if err == nil {
			if line != "}" {
				err = MissingClosingBrace
			}
		}
	}

	return
}
예제 #8
0
func ParseRegCred(s string) (rc *RegCred, err error) {

	var (
		line    string
		parts   []string
		name    string
		nodeID  *xi.NodeID
		ck, sk  *rsa.PublicKey
		e       []xt.EndPointI
		version xu.DecimalVersion
	)
	ss := strings.Split(s, "\n")
	line, err = xc.NextNBLine(&ss)
	if (err == nil) && (line != "regCred {") {
		err = IllFormedRegCred
	}
	if err == nil {
		line, err = xc.NextNBLine(&ss)
		if err == nil {
			parts = strings.Split(line, ": ")
			if len(parts) == 2 && parts[0] == "Name" {
				name = strings.TrimLeft(parts[1], " \t")
			} else {
				err = IllFormedRegCred
			}
		}
	}
	if err == nil {
		var id []byte
		line, err = xc.NextNBLine(&ss)
		if err == nil {
			parts = strings.Split(line, ": ")
			if len(parts) == 2 && parts[0] == "ID" {
				id, err = hex.DecodeString(parts[1])
			} else {
				err = IllFormedRegCred
			}
		}
		if err == nil {
			nodeID, err = xi.New(id)
		}
	}
	if err == nil {
		line, err = xc.NextNBLine(&ss)
		if err == nil {
			parts = strings.Split(line, ": ")
			if len(parts) == 2 && parts[0] == "CommsPubKey" {
				ck, err = xc.RSAPubKeyFromDisk([]byte(parts[1]))
			} else {
				err = IllFormedRegCred
			}
		}
	}
	if err == nil {
		line, err = xc.NextNBLine(&ss)
		if err == nil {
			parts = strings.Split(line, ": ")
			if len(parts) == 2 && parts[0] == "SigPubKey" {
				sk, err = xc.RSAPubKeyFromDisk([]byte(parts[1]))
			} else {
				err = IllFormedRegCred
			}
		}
	}
	if err == nil {
		line, err = xc.NextNBLine(&ss)
		if err == nil {
			// collect EndPoints section; this should be turned into a
			// utility function
			if line == "EndPoints {" {
				for err == nil {
					line = strings.TrimSpace(ss[0]) // peek
					if line == "}" {
						break
					}
					line, err = xc.NextNBLine(&ss)
					if err == nil {
						line = strings.TrimSpace(line)
						parts := strings.Split(line, ": ")
						if len(parts) != 2 || parts[0] != "TcpEndPoint" {
							err = IllFormedRegCred
						} else {
							var ep xt.EndPointI
							ep, err = xt.NewTcpEndPoint(parts[1])
							if err == nil {
								e = append(e, ep)
							}
						}
					}
				}
				if err == nil {
					line, err = xc.NextNBLine(&ss)
					if (err == nil) && (line != "}") {
						err = MissingClosingBrace
					}
				}
			} else {
				err = MissingEndPointsSection
			}
		}
	}
	if err == nil {
		line, err = xc.NextNBLine(&ss)
		if err == nil {
			parts = strings.Split(line, ": ")
			if len(parts) == 2 && parts[0] == "Version" {
				version, err = xu.ParseDecimalVersion(parts[1])
			} else {
				err = IllFormedRegCred
			}
		}
	}
	if err == nil {
		rc = &RegCred{
			Name:        name,
			ID:          nodeID,
			CommsPubKey: ck,
			SigPubKey:   sk,
			EndPoints:   e,
			Version:     version,
		}
	}
	return
}
예제 #9
0
// Version of the above which consumes a slice of strings.  XXX Copies the
// slice unnecessarily.
func ParseBNFromStrings(ss []string, whichType string) (bn *BaseNode, rest []string, err error) {
	var (
		name        string
		nodeID      *xi.NodeID
		commsPubKey *rsa.PublicKey
		sigPubKey   *rsa.PublicKey
		overlays    []xo.OverlayI
	)
	s, err := xc.NextNBLine(&ss)
	if err == nil {
		opener := fmt.Sprintf("%s {", whichType) // "peer {" or "node {"
		if s != opener {
			err = NotExpectedOpener
		}
	}
	if err == nil {
		s, err := xc.NextNBLine(&ss)
		if err == nil {
			if strings.HasPrefix(s, "name: ") {
				name = s[6:]
			} else {
				err = NotABaseNode
			}
		}
	}
	if err == nil {
		s, err = xc.NextNBLine(&ss)
		if err == nil {
			if strings.HasPrefix(s, "nodeID: ") {
				var val []byte
				val, err = hex.DecodeString(s[8:])
				if err == nil {
					nodeID, err = xi.NewNodeID(val)
				}
			} else {
				err = NotABaseNode
			}
		}
	}
	if err == nil {
		s, err = xc.NextNBLine(&ss)
		if err == nil {
			if strings.HasPrefix(s, "commsPubKey: ") {
				var ckPEM []byte
				ckPEM, err = xc.CollectPEMRSAPublicKey(s[13:], &ss)
				if err == nil {
					commsPubKey, err = xc.RSAPubKeyFromPEM(ckPEM)
				}
			} else {
				// DEBUG
				fmt.Printf("\ndon't see commsPubKey\n")
				// END
				err = NotABaseNode
			}
		}
	}
	if err == nil {
		s, err = xc.NextNBLine(&ss)
		if err == nil {
			if strings.HasPrefix(s, "sigPubKey: ") {
				var skPEM []byte
				skPEM, err = xc.CollectPEMRSAPublicKey(s[11:], &ss)
				if err == nil {
					sigPubKey, err = xc.RSAPubKeyFromPEM(skPEM)
				}
			} else {
				// DEBUG
				fmt.Printf("\ndon't see sigPubKey\n")
				// END
				err = NotABaseNode
			}
		}
	}
	if err == nil {
		s, err = xc.NextNBLine(&ss)
		if err == nil {
			if s == "overlays {" {
				for {
					s, err = xc.NextNBLine(&ss)
					if err == nil {
						if s == "" { // end of strings
							err = NotABaseNode
							break
						} else if s == "}" {
							prepend := []string{s}
							ss = append(prepend, ss...)
							break
						}
					}
					var o xo.OverlayI
					o, err = xo.Parse(s)
					if err == nil {
						overlays = append(overlays, o)
					}
				}
			} else {
				err = NotABaseNode
			}
		}
	}
	if err == nil {
		s, err = xc.NextNBLine(&ss)
		if err == nil {
			if s != "}" {
				err = NotABaseNode
			}
		}
	}
	if err == nil {
		var bn = BaseNode{name, nodeID, commsPubKey, sigPubKey, overlays}
		return &bn, ss, nil
	} else {
		return nil, nil, err
	}
}
예제 #10
-1
/**
 * Parse a serialized RegNode, returning one with an open acceptor.
 */
func ParseRegNodeFromStrings(ss []string) (
	rn *RegNode, rest []string, err error) {

	var (
		line   string
		node   *xn.Node
		ckPriv *rsa.PrivateKey
		skPriv *rsa.PrivateKey
	)
	rest = ss
	line, err = xc.NextNBLine(&rest)
	if (err == nil) && (line != "regNode {") {
		err = MissingRegNodeLine
	} else {
		node, rest, err = xn.ParseFromStrings(rest)
		if err == nil {
			line, err = xc.NextNBLine(&rest)
			if err == nil {
				parts := strings.Split(line, ": ")
				if parts[0] == "ckPriv" && parts[1] == "-----BEGIN -----" {
					ckPriv, err = xn.ExpectRSAPrivateKey(&rest)
				} else {
					err = MissingPrivateKey
				}
				if err == nil {
					line, err = xc.NextNBLine(&rest)
					if err == nil {
						parts := strings.Split(line, ": ")
						if parts[0] == "skPriv" && parts[1] == "-----BEGIN -----" {
							skPriv, err = xn.ExpectRSAPrivateKey(&rest)
						} else {
							err = MissingPrivateKey
						}
						if err == nil {
							line, err = xc.NextNBLine(&rest)
							if (err == nil) && (line != "}") {
								err = MissingClosingBrace
							}
							if err == nil {
								// Try to open the acceptor.
								err = node.OpenAcc()
								if err == nil {
									rn, err = NewRegNode(node, ckPriv, skPriv)
								}
							}
						}
					}
				}
			}
		}
	}
	return
}