Sfoglia il codice sorgente

Rename config attrs; log messages cleaned up

Eugene Yakubovich 10 anni fa
parent
commit
0eecabc7cb
4 ha cambiato i file con 28 aggiunte e 27 eliminazioni
  1. 20 16
      subnet/config.go
  2. 4 4
      subnet/subnet.go
  3. 2 5
      udp/cproxy.go
  4. 2 2
      udp/proxy.go

+ 20 - 16
subnet/config.go

@@ -9,9 +9,9 @@ import (
 
 type Config struct {
 	Network    pkg.IP4Net
-	FirstIP    pkg.IP4
-	LastIP     pkg.IP4
-	HostSubnet uint
+	SubnetMin  pkg.IP4
+	SubnetMax  pkg.IP4
+	SubnetLen  uint
 }
 
 func ParseConfig(s string) (*Config, error) {
@@ -21,31 +21,35 @@ func ParseConfig(s string) (*Config, error) {
 		return nil, err
 	}
 
-	if cfg.HostSubnet > 0 {
-		if cfg.HostSubnet < cfg.Network.PrefixLen {
+	if cfg.SubnetLen > 0 {
+		if cfg.SubnetLen < cfg.Network.PrefixLen {
 			return nil, errors.New("HostSubnet is larger network than Network")
 		}
 	} else {
 		// try to give each host a /24 but if the whole network
 		// is /24 or smaller, half the network
 		if cfg.Network.PrefixLen < 24 {
-			cfg.HostSubnet = 24
+			cfg.SubnetLen = 24
 		} else {
-			cfg.HostSubnet = cfg.Network.PrefixLen + 1
+			cfg.SubnetLen = cfg.Network.PrefixLen + 1
 		}
 	}
 
-	if cfg.FirstIP == pkg.IP4(0) {
-		cfg.FirstIP = cfg.Network.IP
-	} else if !cfg.Network.Contains(cfg.FirstIP) {
-		return nil, errors.New("FirstIP is not in the range of the Network")
+	subnetSize := pkg.IP4(1 << (32 - cfg.SubnetLen))
+
+	if cfg.SubnetMin == pkg.IP4(0) {
+		// skip over the first subnet otherwise it causes problems. e.g.
+		// if Network is 10.100.0.0/16, having an interface with 10.0.0.0
+		// makes ping think it's a broadcast address (not sure why)
+		cfg.SubnetMin = cfg.Network.IP + subnetSize
+	} else if !cfg.Network.Contains(cfg.SubnetMin) {
+		return nil, errors.New("SubnetMin is not in the range of the Network")
 	}
 
-	if cfg.LastIP == pkg.IP4(0) {
-		cfg.LastIP = cfg.Network.Next().IP
-		cfg.LastIP -= (1 << (32 - cfg.HostSubnet))
-	} else if !cfg.Network.Contains(cfg.LastIP) {
-		return nil, errors.New("LastIP is not in the range of the Network")
+	if cfg.SubnetMax == pkg.IP4(0) {
+		cfg.SubnetMax = cfg.Network.Next().IP - subnetSize
+	} else if !cfg.Network.Contains(cfg.SubnetMax) {
+		return nil, errors.New("SubnetMax is not in the range of the Network")
 	}
 
 	return cfg, nil

+ 4 - 4
subnet/subnet.go

@@ -305,13 +305,13 @@ type BaseAttrs struct {
 }
 
 func (sm *SubnetManager) allocateSubnet() (pkg.IP4Net, error) {
-	log.Infof("Picking subnet in range %s ... %s", sm.config.FirstIP, sm.config.LastIP)
+	log.Infof("Picking subnet in range %s ... %s", sm.config.SubnetMin, sm.config.SubnetMax)
 
 	var bag []pkg.IP4
-	sn := pkg.IP4Net{sm.config.FirstIP, sm.config.HostSubnet}
+	sn := pkg.IP4Net{sm.config.SubnetMin, sm.config.SubnetLen}
 
 OuterLoop:
-	for ; sn.IP <= sm.config.LastIP && len(bag) < 100; sn = sn.Next() {
+	for ; sn.IP <= sm.config.SubnetMax && len(bag) < 100; sn = sn.Next() {
 		for _, l := range sm.leases {
 			if sn.Overlaps(l.Network) {
 				continue OuterLoop
@@ -324,7 +324,7 @@ OuterLoop:
 		return pkg.IP4Net{}, errors.New("out of subnets")
 	} else {
 		i := pkg.RandInt(0, len(bag))
-		return pkg.IP4Net{bag[i], sm.config.HostSubnet}, nil
+		return pkg.IP4Net{bag[i], sm.config.SubnetLen}, nil
 	}
 }
 

+ 2 - 5
udp/cproxy.go

@@ -22,7 +22,6 @@ func runCProxy(tun *os.File, conn *os.File, ctl *os.File, tunIP pkg.IP4) {
 	if log.V(1) {
 		log_errors = 1
 	}
-	log.Info("C.run_proxy: log_errors: ", log_errors)
 
 	C.run_proxy(
 		C.int(tun.Fd()),
@@ -31,8 +30,6 @@ func runCProxy(tun *os.File, conn *os.File, ctl *os.File, tunIP pkg.IP4) {
 		C.in_addr_t(tunIP.NetworkOrder()),
 		C.int(log_errors),
 	)
-
-	log.Info("C.run_proxy exited")
 }
 
 func writeCommand(f *os.File, cmd *C.command) {
@@ -102,7 +99,7 @@ func fastProxy(sm *subnet.SubnetManager, tun *os.File, conn *net.UDPConn, tunIP
 				writeCommand(ctl, &cmd)
 
 			} else if evt.Type == subnet.SubnetRemoved {
-				log.Info("Subnet removed: %v", evt.Lease.Network)
+				log.Info("Subnet removed: ", evt.Lease.Network)
 
 				cmd := C.command{
 					cmd:          C.CMD_DEL_ROUTE,
@@ -113,7 +110,7 @@ func fastProxy(sm *subnet.SubnetManager, tun *os.File, conn *net.UDPConn, tunIP
 				writeCommand(ctl, &cmd)
 
 			} else {
-				log.Errorf("Internal error: unknown event type: %d", int(evt.Type))
+				log.Error("Internal error: unknown event type: ", int(evt.Type))
 			}
 		}
 	}

+ 2 - 2
udp/proxy.go

@@ -127,11 +127,11 @@ func proxy(sm *subnet.SubnetManager, tun *os.File, conn *net.UDPConn, port int)
 				rtr.SetRoute(evt.Lease.Network, attrs.PublicIP)
 
 			} else if evt.Type == subnet.SubnetRemoved {
-				log.Info("Subnet removed: %v", evt.Lease.Network)
+				log.Info("Subnet removed: ", evt.Lease.Network)
 				rtr.DelRoute(evt.Lease.Network)
 
 			} else {
-				log.Errorf("Internal error: unknown event type: %d", int(evt.Type))
+				log.Error("Internal error: unknown event type: ", int(evt.Type))
 			}
 		}
 	}