mirror of
				https://github.com/yggdrasil-network/yggdrasil-go.git
				synced 2025-11-04 11:15:07 +03:00 
			
		
		
		
	Set interface IP and MTU on BSD
This commit is contained in:
		
							parent
							
								
									f8dda26dba
								
							
						
					
					
						commit
						9e4d169208
					
				
					 2 changed files with 66 additions and 10 deletions
				
			
		| 
						 | 
					@ -1,17 +1,60 @@
 | 
				
			||||||
// +build openbsd freebsd solaris
 | 
					// +build openbsd freebsd solaris netbsd dragonfly
 | 
				
			||||||
 | 
					
 | 
				
			||||||
package yggdrasil
 | 
					package yggdrasil
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import "fmt"
 | 
				
			||||||
 | 
					import "os/exec"
 | 
				
			||||||
 | 
					import "strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import water "github.com/neilalexander/water"
 | 
					import water "github.com/neilalexander/water"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// This is to catch BSD platforms
 | 
					// This is to catch BSD platforms
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const TUNSIFINFO = (0x80000000) | ((4 & 0x1fff) << 16) | uint32(byte('t'))<<8 | 91
 | 
				
			||||||
 | 
					const TUNGIFINFO = (0x40000000) | ((4 & 0x1fff) << 16) | uint32(byte('t'))<<8 | 92
 | 
				
			||||||
 | 
					const SIOCAIFADDR_IN6 = (0x80000000) | ((4 & 0x1fff) << 16) | uint32(byte('i'))<<8 | 27
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type in6_addrlifetime struct {
 | 
				
			||||||
 | 
						ia6t_expire    float64
 | 
				
			||||||
 | 
						ia6t_preferred float64
 | 
				
			||||||
 | 
						ia6t_vltime    uint32
 | 
				
			||||||
 | 
						ia6t_pltime    uint32
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type sockaddr_in6 struct {
 | 
				
			||||||
 | 
						sin6_len      uint8
 | 
				
			||||||
 | 
						sin6_family   uint8
 | 
				
			||||||
 | 
						sin6_port     uint8
 | 
				
			||||||
 | 
						sin6_flowinfo uint32
 | 
				
			||||||
 | 
						sin6_addr     [8]uint16
 | 
				
			||||||
 | 
						sin6_scope_id uint32
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type in6_aliasreq struct {
 | 
				
			||||||
 | 
						ifra_name       [16]byte
 | 
				
			||||||
 | 
						ifra_addr       sockaddr_in6
 | 
				
			||||||
 | 
						ifra_dstaddr    sockaddr_in6
 | 
				
			||||||
 | 
						ifra_prefixmask sockaddr_in6
 | 
				
			||||||
 | 
						ifra_flags      uint32
 | 
				
			||||||
 | 
						ifra_lifetime   in6_addrlifetime
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type tuninfo struct {
 | 
				
			||||||
 | 
						tun_baudrate int
 | 
				
			||||||
 | 
						tun_mtu      int16
 | 
				
			||||||
 | 
						tun_type     uint8
 | 
				
			||||||
 | 
						tun_dummy    uint8
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error {
 | 
					func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int) error {
 | 
				
			||||||
	var config water.Config
 | 
						var config water.Config
 | 
				
			||||||
	if iftapmode {
 | 
						switch {
 | 
				
			||||||
 | 
						case iftapmode || ifname[:8] == "/dev/tap":
 | 
				
			||||||
		config = water.Config{DeviceType: water.TAP}
 | 
							config = water.Config{DeviceType: water.TAP}
 | 
				
			||||||
	} else {
 | 
						case !iftapmode || ifname[:8] == "/dev/tun":
 | 
				
			||||||
		config = water.Config{DeviceType: water.TUN}
 | 
							config = water.Config{DeviceType: water.TUN}
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							panic("TUN/TAP name must be in format /dev/tunX or /dev/tapX")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	config.Name = ifname
 | 
						config.Name = ifname
 | 
				
			||||||
	iface, err := water.New(config)
 | 
						iface, err := water.New(config)
 | 
				
			||||||
| 
						 | 
					@ -24,6 +67,24 @@ func (tun *tunDevice) setup(ifname string, iftapmode bool, addr string, mtu int)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (tun *tunDevice) setupAddress(addr string) error {
 | 
					func (tun *tunDevice) setupAddress(addr string) error {
 | 
				
			||||||
	tun.core.log.Println("Platform not supported, you must set the address of", tun.iface.Name(), "to", addr)
 | 
						// Set address
 | 
				
			||||||
 | 
						cmd := exec.Command("ifconfig", tun.iface.Name(), "inet6", addr)
 | 
				
			||||||
 | 
						tun.core.log.Printf("ifconfig command: %v", strings.Join(cmd.Args, " "))
 | 
				
			||||||
 | 
						output, err := cmd.CombinedOutput()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							tun.core.log.Printf("ipconfig failed: %v.", err)
 | 
				
			||||||
 | 
							tun.core.log.Println(string(output))
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						// Set MTU and bring device up
 | 
				
			||||||
 | 
						cmd = exec.Command("ifconfig", tun.iface.Name(), "mtu", fmt.Sprintf("%d", tun.mtu), "up")
 | 
				
			||||||
 | 
						tun.core.log.Printf("ifconfig command: %v", strings.Join(cmd.Args, " "))
 | 
				
			||||||
 | 
						output, err = cmd.CombinedOutput()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							tun.core.log.Printf("ipconfig failed: %v.", err)
 | 
				
			||||||
 | 
							tun.core.log.Println(string(output))
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,9 +1,4 @@
 | 
				
			||||||
// +build !linux
 | 
					// +build !linux,!darwin,!windows,!openbsd,!freebsd,!solaris,!netbsd,!dragonfly
 | 
				
			||||||
// +build !darwin
 | 
					 | 
				
			||||||
// +build !windows
 | 
					 | 
				
			||||||
// +build !openbsd
 | 
					 | 
				
			||||||
// +build !freebsd
 | 
					 | 
				
			||||||
// +build !solaris
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
package yggdrasil
 | 
					package yggdrasil
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue