Skip to content

Commit

Permalink
config: Parse network interfaces.
Browse files Browse the repository at this point in the history
This adds config helpers for parsing network interface
states based on the config parameters set. Work towards getnetworkinfo implementation.
  • Loading branch information
dnldd committed Nov 5, 2018
1 parent 5f80817 commit f4e7b80
Showing 1 changed file with 185 additions and 0 deletions.
185 changes: 185 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ type config struct {
miningAddrs []dcrutil.Address
minRelayTxFee dcrutil.Amount
whitelists []*net.IPNet
networks *Networks
}

// serviceOptions defines the configuration options for the daemon as a service on
Expand Down Expand Up @@ -416,6 +417,185 @@ func createDefaultConfigFile(destPath string) error {
return err
}

// Network types
var (
IPV4 = "ipv4"
IPV6 = "ipv6"
Onion = "onion"
)

// NetworkInfo represents an state of a network interface.
type NetworkInfo struct {
Name string `json:"name"`
Reachable bool `json:"reachable"`
Limited bool `json:"limited"`
Proxy string `json:"proxy"`
ProxyRandomizeCredentials bool `json:"proxyrandomizecredentials"`
}

// Networks represents the state of network protocols.
type Networks map[string]*NetworkInfo

// setReachable sets the reachable state for the provided network protocol.
func (n Networks) setReachable(protocol string, value bool) error {
fmt.Println("setting reachable")
net, ok := n[protocol]
if !ok {
return fmt.Errorf("network (%v) not found", protocol)
}

net.Reachable = value
n[protocol] = net
return nil
}

// setLimited sets the limited state for the provided network.
func (n Networks) setLimited(protocol string, value bool) error {
fmt.Println("setting limited")
net, ok := n[protocol]
if !ok {
return fmt.Errorf("network (%v) not found", protocol)
}

net.Limited = value
n[protocol] = net
return nil
}

// setProxy sets the proxy state for the provided network.
func (n Networks) setProxy(protocol string, value string) error {
fmt.Println("setting proxy")
net, ok := n[protocol]
if !ok {
return fmt.Errorf("network (%v) not found", protocol)
}

net.Proxy = value
n[protocol] = net
return nil
}

// setProxyRandomizeCredentials sets the limited state for the provided network.
func (n Networks) setProxyRandomizeCredentials(protocol string, value bool) error {
fmt.Println("setting proxy random creds")
net, ok := n[protocol]
if !ok {
return fmt.Errorf("network (%v) not found", protocol)
}

net.ProxyRandomizeCredentials = value
n[protocol] = net
return nil
}

// generateInfo is a convenience function that creates a slice from the network
// info map.
func (n Networks) generateInfo() ([]*NetworkInfo, error) {
nInfo := make([]*NetworkInfo, 3)
for net, info := range n {
switch net {
case IPV4:
nInfo[0] = info
case IPV6:
nInfo[1] = info
case Onion:
nInfo[2] = info
default:
return nil, fmt.Errorf("Unknown network type: %v", net)
}
}

return nInfo, nil
}

// parseNetworkInterfaces updates all network interface states based on the
// provided configuration.
func parseNetworkInterfaces(cfg *config) (*Networks, error) {
networks := &Networks{
IPV4: &NetworkInfo{
Name: IPV4,
},
IPV6: &NetworkInfo{
Name: IPV6,
},
Onion: &NetworkInfo{
Name: Onion,
},
}

if cfg.DisableListen {
return networks, nil
}

v4Addrs, v6Addrs, _, err := parseListeners(cfg.Listeners)
if err != nil {
return nil, err
}

var limited bool
if len(v4Addrs)+len(v6Addrs) == 1 {
limited = true
}

if len(v4Addrs) > 0 {
err = networks.setReachable(IPV4, true)
if err != nil {
return nil, err
}

if cfg.Proxy != "" {
err = networks.setProxy(IPV4, cfg.Proxy)
if err != nil {
return nil, err
}
}

if limited {
err = networks.setLimited(IPV4, true)
if err != nil {
return nil, err
}
}
}

if len(v6Addrs) > 0 {
err = networks.setReachable(IPV6, true)
if err != nil {
return nil, err
}

// the onion network is only reachable if a proxy or onion proxy
// is set.
if cfg.Proxy != "" || cfg.OnionProxy != "" {
err = networks.setProxy(IPV6, cfg.Proxy)
if err != nil {
return nil, err
}

if !cfg.NoOnion {
err = networks.setReachable(Onion, true)
if err != nil {
return nil, err
}

err = networks.setProxy(Onion, cfg.Proxy)
if err != nil {
return nil, err
}

if cfg.TorIsolation {
err = networks.setProxyRandomizeCredentials(Onion, true)
if err != nil {
return nil, err
}
}
}
}
}

return networks, nil
}

// loadConfig initializes and parses the config using a config file and command
// line options.
//
Expand Down Expand Up @@ -1141,6 +1321,11 @@ func loadConfig() (*config, []string, error) {
}
}

cfg.networks, err = parseNetworkInterfaces(&cfg)
if err != nil {
return nil, nil, err
}

// Warn about missing config file only after all other configuration is
// done. This prevents the warning on help messages and invalid
// options. Note this should go directly before the return.
Expand Down

0 comments on commit f4e7b80

Please sign in to comment.