diff --git a/dnsmasq.conf b/dnsmasq.conf index 8caf2db..2946493 100644 --- a/dnsmasq.conf +++ b/dnsmasq.conf @@ -342,11 +342,11 @@ dhcp-option=option:router,10.0.0.1 #dhcp-option=option:ntp-server,1ww,w # Send DHCPv6 option. Note [] around IPv6 addresses. -#dhcp-option=option6:dns-server,[fec0::3] +#dhcp-option=option6:dns-server,[fec0::b08d:adff:fead:cda1] # Send DHCPv6 option for namservers as the machine running # dnsmasq and another. -dhcp-option=option6:dns-server,[fec0::3] +dhcp-option=option6:dns-server,[fec0::b08d:adff:fead:cda1] # Ask client to poll for option changes every six hours. (RFC4242) #dhcp-option=option6:information-refresh-time,6h diff --git a/gp/gp b/gp/gp new file mode 100755 index 0000000..573b731 Binary files /dev/null and b/gp/gp differ diff --git a/gp/main.go b/gp/main.go new file mode 100644 index 0000000..6c37a0a --- /dev/null +++ b/gp/main.go @@ -0,0 +1,123 @@ +package main + +import ( + "github.com/vishvananda/netlink" + "io/ioutil" + "strings" + "net" + "fmt" + "os" + "regexp" +) + +var env map[string]string=make(map[string]string) + +func main(){ + readInitEnviron() + + macAddr,_ := net.ParseMAC(getEnv("IF_ADDR")) +// fpAddr,_ := netlink.ParseAddr(getEnv("IP_ADDR")) +// gatewayAddr := net.ParseIP(getEnv("GATEWAY_ADDR")) + + linkInterfaceType:=getEnvWithDefault("IF_TYPE","macvlan") + + link:=findFirstInterface(linkInterfaceType) + + renameInterface(link,"eth0") + + renameMacAddress(link,macAddr) + +/* + #netlink.AddrAdd(link,ipAddr) + + #netlink.LinkSetUp(link) + + #addRoute(gatewayAddr,link) +*/ +} + +func readInitEnviron(){ + envFilePath:="/proc/1/environ" + content, err := ioutil.ReadFile(envFilePath) + kvParseRegex,_:=regexp.Compile("^([^=]+)=(.+)$") + if err != nil { + panic(fmt.Sprintf("environ file %s not existed!",envFilePath)) + } + lines := strings.Split(string(content), "\x00") + for _,element := range lines { + result_slice := kvParseRegex.FindStringSubmatch(element) + if len(result_slice) < 2 { + continue + }else{ + env[result_slice[1]]=result_slice[2] + } + } +} +func renameMacAddress(link netlink.Link,new_name net.HardwareAddr){ + err:=netlink.LinkSetHardwareAddr(link,new_name) + if(err != nil){ + panic(fmt.Sprintf("cannot rename mac address %s -> %s",link.Attrs().HardwareAddr,new_name)) + } else { + fmt.Printf("rename mac address %s -> %s\n",link.Attrs().HardwareAddr,new_name) + } + +} + +func renameInterface(link netlink.Link,new_name string) { + err:=netlink.LinkSetName(link,new_name) + if(err != nil){ + panic(fmt.Sprintf("cannot rename interface %s -> %s",link.Attrs().Name,new_name)) + } else { + fmt.Printf("rename interface %s -> %s\n",link.Attrs().Name,new_name) + } +} + +func findFirstInterface(if_type string) netlink.Link { + links,_:=netlink.LinkList() + for _,element := range links { + if(element.Type() == if_type){ + fmt.Println(element.Attrs().Name) + return element + } + } + panic(fmt.Sprintf("cannot get type: %s interface",if_type)) +} + +func getEnv(variable string) string { + envValue,ok:=env[variable] + if ok { + return envValue + } + envValue=os.Getenv(variable) + + if(envValue == ""){ + panic(fmt.Sprintf("cannot get ${%s}",variable)) + } else{ + return envValue + } +} + +func getEnvWithDefault(variable string, ret_is_empty string) string { + envValue,ok:=env[variable] + if ok { + return envValue + } + envValue=os.Getenv(variable) + + if(envValue == ""){ + return ret_is_empty + } else{ + return envValue + } +} + +func addRoute(gateway net.IP,link netlink.Link){ + default_network := &net.IPNet{ + IP: net.IPv4(0, 0, 0, 0), + Mask: net.CIDRMask(0, 32), + } + + route := &netlink.Route{LinkIndex: link.Attrs().Index, Dst: default_network, Gw:gateway} + + netlink.RouteAdd(route) +} diff --git a/gp/src/github.com/vishvananda/netlink b/gp/src/github.com/vishvananda/netlink new file mode 160000 index 0000000..814b43a --- /dev/null +++ b/gp/src/github.com/vishvananda/netlink @@ -0,0 +1 @@ +Subproject commit 814b43a0c71f01a137581a3fadf37619e71751b8