Ejemplo n.º 1
0
func (n *Node) receiveVersion(version string) {

	// If this is not a new incoming connection, error.
	if n.conn.State != connect.ConnStateInitialIncoming {
		n.conn.Close()
		return
	}

	// Verify that we support this version.
	found := false
	for _, supported := range connect.Versions {
		if version == supported {
			found = true
			break
		}
	}

	if !found {
		n.conn.Close()
		return
	}

	// Set this connection's protocol version.
	n.conn.Version = version

	// Send our capabilities list.
	n.conn.WriteLine(connect.MakeCap(connect.Capabilities))

	// Move into capabilities negotiation state.
	n.conn.State = connect.ConnStateCapabilityNegotiationIncoming
}
Ejemplo n.º 2
0
func (n *Node) receiveCap(capabilities []string) {

	switch n.conn.State {
	case connect.ConnStateCapabilityNegotiationOutgoing:

		// We're expecting a list of supporting capabilities,
		// and need to send back the subset we support.
		var shared []string
		for _, remoteCapability := range capabilities {
			for _, capability := range connect.Capabilities {
				if capability == remoteCapability {
					shared = append(shared, capability)
					break
				}
			}
		}

		// Set this connection's enabled capabilities.
		n.conn.Capabilities = shared

		// Send back the picked capability set.
		n.conn.WriteLine(connect.MakeCap(shared))

		// Send our degraded notification.
		n.conn.WriteLine(connect.MakeDegraded(Degraded))

		// Move into degraded notification state.
		n.conn.State = connect.ConnStateDegradedNotification

	case connect.ConnStateCapabilityNegotiationIncoming:

		// We're expecting a list of selected capabilities.
		// Verify that we have the capabilities they send.
		for _, selected := range capabilities {
			matched := false
			for _, capability := range connect.Capabilities {
				if capability == selected {
					matched = true
					break
				}
			}

			if !matched {
				// Invalid capability.
				n.conn.Close()
				return
			}
		}

		// Set this connection's enabled capabilities.
		n.conn.Capabilities = capabilities

		// Send our degraded notification.
		n.conn.WriteLine(connect.MakeDegraded(Degraded))

		// Move into degraded notification state.
		n.conn.State = connect.ConnStateDegradedNotification

	default:

		// Invalid state for this message.
		n.conn.Close()
	}
}