diff --git a/config/config.go b/config/config.go index 98d422f..c1c8332 100644 --- a/config/config.go +++ b/config/config.go @@ -1,13 +1,32 @@ package config +import "net" + +// Config represents the main config of a node. type Config struct { - PublicKey string - PrivateKey string - Host string - Port uint16 - Server *ServerConfig + PublicKey string // Node public key + PrivateKey string // Node private key + Host string // Node host + Port uint16 // Node port + Server *ServerConfig // List of servers +} + +// ServerConfig represents a server configuration. +type ServerConfig struct { + PublicKey string // Server public key + Host string // Server hostname or IP + Port uint16 // Server port + Keys []*KeyConfig // List of keys +} + +// KeyConfig represents a key on a server. +type KeyConfig struct { + ID string // Key UUID + KeyShare string // Keyshare + KeyMetaInfo string // Key Metainformation } +// Returns a server, given its ID func (config *Config) GetServerByID(id string) *ServerConfig { if config.Server.PublicKey == id { return config.Server @@ -15,25 +34,26 @@ func (config *Config) GetServerByID(id string) *ServerConfig { return nil } -func (config *Config) GetServerIPs() []string { +// Returns a list of IPs. If a server has a hostname instead of an IP, it resolves it. +func (config *Config) GetServerIPs() ([]string, error) { ips := make([]string, 1) - ips[0] = config.Server.Host - return ips + // try to parse as IP + ip, err := net.ResolveIPAddr("ip", config.Server.Host) + if err != nil { + return nil, err + } + ips[0] = ip.String() + return ips, nil } +// Returns the list of public keys of the servers func (config *Config) GetServerPubKeys() []string { pubkeys := make([]string, 1) pubkeys[0] = config.Server.PublicKey return pubkeys } -type ServerConfig struct { - PublicKey string - Host string - Port uint16 - Keys []*KeyConfig -} - +// Returns a key in a server, based on its ID func (serverConfig *ServerConfig) GetKeyByID(id string) *KeyConfig { for _, key := range serverConfig.Keys { if key.ID == id { @@ -42,9 +62,3 @@ func (serverConfig *ServerConfig) GetKeyByID(id string) *KeyConfig { } return nil } - -type KeyConfig struct { - ID string - KeyShare string - KeyMetaInfo string -} diff --git a/node.go b/node.go index 6523b3f..cece135 100644 --- a/node.go +++ b/node.go @@ -9,6 +9,7 @@ import ( "github.com/pebbe/zmq4" "github.com/spf13/viper" "log" + "net" "sync" ) @@ -22,7 +23,7 @@ const TchsmProtocol = "tcp" type Node struct { privKey string // The private key for the node, used in ZMQ CURVE Auth. pubKey string // The public key for the node, used in ZMQ CURVE Auth. - host string // A string representing the IP the node is going to use to listen to requests. + host *net.IPAddr // A string representing the IP the node is going to use to listen to requests. port uint16 // a int representing the port the node is going to use to listen to requests config *config.Config // A pointer to the struct which saves the configuration of the node. context *zmq4.Context // The context used by zmq connections. @@ -33,11 +34,14 @@ type Node struct { // InitNode inits the node using the configuration provided. Returns a started node or an error if the function fails. func InitNode(config *config.Config) (*Node, error) { - + ip, err := net.ResolveIPAddr("ip", config.Host) + if err != nil { + return nil, err + } node := &Node{ pubKey: config.PublicKey, privKey: config.PrivateKey, - host: config.Host, + host: ip, port: config.Port, config: config, servers: make([]*Server, 0), @@ -48,8 +52,11 @@ func InitNode(config *config.Config) (*Node, error) { return nil, err } node.context = context - - zmq4.AuthAllow(TchsmDomain, config.GetServerIPs()...) + ips, err := config.GetServerIPs() + if err != nil { + return nil, err + } + zmq4.AuthAllow(TchsmDomain, ips...) zmq4.AuthCurveAdd(TchsmDomain, config.GetServerPubKeys()...) in, err := context.NewSocket(zmq4.ROUTER) @@ -68,9 +75,14 @@ func InitNode(config *config.Config) (*Node, error) { node.socket = in serverConfig := config.Server + + serverIP, err := net.ResolveIPAddr("ip", serverConfig.Host) + if err != nil { + return nil, err + } server := &Server{ pubKey: serverConfig.PublicKey, - host: serverConfig.Host, + host: serverIP, port: serverConfig.Port, client: node, channel: make(chan *message.Message), diff --git a/server.go b/server.go index a79e60c..6ab5831 100644 --- a/server.go +++ b/server.go @@ -8,12 +8,13 @@ import ( "github.com/niclabs/tcrsa" "github.com/pebbe/zmq4" "log" + "net" ) // Server represents the connection with the Distributed TCHSM server. // It saves its connection values, its public key, and the keyshares and keymetainfo sent by the server. type Server struct { - host string // IP where the server is listening. + host *net.IPAddr // IP where the server is listening. port uint16 // Port where the server is listening. pubKey string // Public key of the server. Used for SMQ CURVE auth. keys map[string]*Key // Dictionary with key shares created by this server.