Allow exact IP, update error msg, fix getIFIndex

This commit is contained in:
KusakabeSi 2022-01-02 19:00:54 +00:00
parent 9886e2bf09
commit eb6bb7d15d
3 changed files with 38 additions and 6 deletions

View File

@ -192,7 +192,35 @@ func GenNMCfg(NMCinfigPath string, enableP2P bool, printExample bool) (err error
if enableP2P { if enableP2P {
econfig.NextHopTable = make(mtypes.NextHopTable) econfig.NextHopTable = make(mtypes.NextHopTable)
} }
ModeIDmax := 0
for id, _ := range NMCfg.NetworkName {
if ModeIDmax < id {
ModeIDmax = id
}
}
IPv4Block := NMCfg.EdgeNode.IPv4Range
if IPv4Block != "" {
_, _, err = tap.GetIP(4, IPv4Block, uint32(ModeIDmax))
if err != nil {
return err
}
}
IPv6Block := NMCfg.EdgeNode.IPv6Range
if IPv6Block != "" {
_, _, err = tap.GetIP(6, IPv6Block, uint32(ModeIDmax))
if err != nil {
return err
}
}
IPv6LLBlock := NMCfg.EdgeNode.IPv6LLRange
if IPv6LLBlock != "" {
_, _, err = tap.GetIP(6, IPv6LLBlock, uint32(ModeIDmax))
if err != nil {
return err
}
}
econfig.DynamicRoute.NTPConfig.Servers = make([]string, 0) econfig.DynamicRoute.NTPConfig.Servers = make([]string, 0)
econfig.DynamicRoute.SuperNode.PSKey = "" econfig.DynamicRoute.SuperNode.PSKey = ""
econfig.DynamicRoute.SuperNode.EndpointV4 = "" econfig.DynamicRoute.SuperNode.EndpointV4 = ""

View File

@ -55,6 +55,9 @@ func GetIP(version int, netcidr string, uid uint32) (net.IP, net.IPMask, error)
} }
} }
ones, bits := the_net.Mask.Size() ones, bits := the_net.Mask.Size()
if ones == bits {
return the_net.IP, the_net.Mask, nil
}
maxuid := big.NewInt(1) maxuid := big.NewInt(1)
maxuid.Lsh(maxuid, uint((bits - ones))) maxuid.Lsh(maxuid, uint((bits - ones)))
ip_use := big.NewInt(int64(uid)) ip_use := big.NewInt(int64(uid))

View File

@ -291,19 +291,19 @@ func (tap *NativeTap) addIPAddr(version string, ip net.IP, mask net.IPMask) (err
e := exec.Command("ip", "addr", "add", ip.String()+"/"+masklen, "dev", name) e := exec.Command("ip", "addr", "add", ip.String()+"/"+masklen, "dev", name)
ret, err := e.CombinedOutput() ret, err := e.CombinedOutput()
if err != nil { if err != nil {
fmt.Println("Please make sure `ip` tool installed") fmt.Printf("Failed to set ip %v to interface %v, please make sure `ip` tool installed\n", ip.String()+"/"+masklen, name)
return fmt.Errorf(string(ret)) return fmt.Errorf(string(ret))
} }
} else if version == "6ll" { } else if version == "6ll" {
_, llnet, _ := net.ParseCIDR("fe80::/64") _, llnet, _ := net.ParseCIDR("fe80::/64")
if llnet.Contains(ip) == false { if !llnet.Contains(ip) {
return fmt.Errorf("%v is not a link-local address", ip) return fmt.Errorf("%v is not a link-local address", ip)
} }
e := exec.Command("ip", "addr", "add", ip.String()+"/64", "dev", name) e := exec.Command("ip", "addr", "add", ip.String()+"/64", "dev", name)
ret, err := e.CombinedOutput() ret, err := e.CombinedOutput()
if err != nil { if err != nil {
fmt.Println("Please make sure `ip` tool installed") fmt.Printf("Failed to set ip %v to interface %v, please make sure `ip` tool installed\n", ip.String()+"/64", name)
return fmt.Errorf(string(ret)) return fmt.Errorf(string(ret))
} }
} }
@ -330,8 +330,9 @@ func (tap *NativeTap) setUp() (err error) {
func getIFIndex(name string) (ret int32, err error) { func getIFIndex(name string) (ret int32, err error) {
var ifr [ifReqSize]byte var ifr [ifReqSize]byte
copy(ifr[:unix.IFNAMSIZ], name) // 0-16
err = ioctlRequest(unix.SIOCGIFINDEX, uintptr(unsafe.Pointer(&ifr[0]))) err = ioctlRequest(unix.SIOCGIFINDEX, uintptr(unsafe.Pointer(&ifr[0])))
return *(*int32)(unsafe.Pointer(&ifr[unix.IFNAMSIZ])), nil return *(*int32)(unsafe.Pointer(&ifr[unix.IFNAMSIZ])), err
} }
func (tap *NativeTap) setMTU(n uint16) (err error) { func (tap *NativeTap) setMTU(n uint16) (err error) {
@ -359,7 +360,7 @@ func (tap *NativeTap) MTU() (int, error) {
copy(ifr[:], name) copy(ifr[:], name)
err = ioctlRequest(unix.SIOCGIFMTU, uintptr(unsafe.Pointer(&ifr[0]))) err = ioctlRequest(unix.SIOCGIFMTU, uintptr(unsafe.Pointer(&ifr[0])))
return int(*(*int32)(unsafe.Pointer(&ifr[unix.IFNAMSIZ]))), nil return int(*(*int32)(unsafe.Pointer(&ifr[unix.IFNAMSIZ]))), err
} }
func (tap *NativeTap) Name() (string, error) { func (tap *NativeTap) Name() (string, error) {
@ -558,7 +559,7 @@ func CreateTAPFromFile(file *os.File, iconfig mtypes.InterfaceConf, NodeID mtype
e := exec.Command("ip", "addr", "flush", "dev", tapname) e := exec.Command("ip", "addr", "flush", "dev", tapname)
_, err := e.CombinedOutput() _, err := e.CombinedOutput()
if err != nil { if err != nil {
fmt.Println("Please make sure `ip` tool installed") fmt.Printf("Failed to flush ip from interface %v , please make sure `ip` tool installed\n", tapname)
return nil, err return nil, err
} }
cidrstr := iconfig.IPv6LLPrefix cidrstr := iconfig.IPv6LLPrefix