mirror of
				https://github.com/yggdrasil-network/yggdrasil-go.git
				synced 2025-11-04 03:05:07 +03:00 
			
		
		
		
	Move tunDevice into router
This commit is contained in:
		
							parent
							
								
									d9b376b3ad
								
							
						
					
					
						commit
						10157483f9
					
				
					 4 changed files with 24 additions and 20 deletions
				
			
		| 
						 | 
					@ -160,9 +160,9 @@ func (a *admin) init(c *Core, listenaddr string) {
 | 
				
			||||||
		}()
 | 
							}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		return admin_info{
 | 
							return admin_info{
 | 
				
			||||||
			a.core.tun.iface.Name(): admin_info{
 | 
								a.core.router.tun.iface.Name(): admin_info{
 | 
				
			||||||
				"tap_mode": a.core.tun.iface.IsTAP(),
 | 
									"tap_mode": a.core.router.tun.iface.IsTAP(),
 | 
				
			||||||
				"mtu":      a.core.tun.mtu,
 | 
									"mtu":      a.core.router.tun.mtu,
 | 
				
			||||||
			},
 | 
								},
 | 
				
			||||||
		}, nil
 | 
							}, nil
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
| 
						 | 
					@ -185,8 +185,8 @@ func (a *admin) init(c *Core, listenaddr string) {
 | 
				
			||||||
			return admin_info{}, errors.New("Failed to configure adapter")
 | 
								return admin_info{}, errors.New("Failed to configure adapter")
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			return admin_info{
 | 
								return admin_info{
 | 
				
			||||||
				a.core.tun.iface.Name(): admin_info{
 | 
									a.core.router.tun.iface.Name(): admin_info{
 | 
				
			||||||
					"tap_mode": a.core.tun.iface.IsTAP(),
 | 
										"tap_mode": a.core.router.tun.iface.IsTAP(),
 | 
				
			||||||
					"mtu":      ifmtu,
 | 
										"mtu":      ifmtu,
 | 
				
			||||||
				},
 | 
									},
 | 
				
			||||||
			}, nil
 | 
								}, nil
 | 
				
			||||||
| 
						 | 
					@ -539,12 +539,12 @@ func (a *admin) removePeer(p string) error {
 | 
				
			||||||
// startTunWithMTU creates the tun/tap device, sets its address, and sets the MTU to the provided value.
 | 
					// startTunWithMTU creates the tun/tap device, sets its address, and sets the MTU to the provided value.
 | 
				
			||||||
func (a *admin) startTunWithMTU(ifname string, iftapmode bool, ifmtu int) error {
 | 
					func (a *admin) startTunWithMTU(ifname string, iftapmode bool, ifmtu int) error {
 | 
				
			||||||
	// Close the TUN first if open
 | 
						// Close the TUN first if open
 | 
				
			||||||
	_ = a.core.tun.close()
 | 
						_ = a.core.router.tun.close()
 | 
				
			||||||
	// Then reconfigure and start it
 | 
						// Then reconfigure and start it
 | 
				
			||||||
	addr := a.core.router.addr
 | 
						addr := a.core.router.addr
 | 
				
			||||||
	straddr := fmt.Sprintf("%s/%v", net.IP(addr[:]).String(), 8*len(address_prefix)-1)
 | 
						straddr := fmt.Sprintf("%s/%v", net.IP(addr[:]).String(), 8*len(address_prefix)-1)
 | 
				
			||||||
	if ifname != "none" {
 | 
						if ifname != "none" {
 | 
				
			||||||
		err := a.core.tun.setup(ifname, iftapmode, straddr, ifmtu)
 | 
							err := a.core.router.tun.setup(ifname, iftapmode, straddr, ifmtu)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return err
 | 
								return err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
| 
						 | 
					@ -559,9 +559,9 @@ func (a *admin) startTunWithMTU(ifname string, iftapmode bool, ifmtu int) error
 | 
				
			||||||
			a.core.sessions.sendPingPong(sinfo, false)
 | 
								a.core.sessions.sendPingPong(sinfo, false)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		// Aaaaand... go!
 | 
							// Aaaaand... go!
 | 
				
			||||||
		go a.core.tun.read()
 | 
							go a.core.router.tun.read()
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	go a.core.tun.write()
 | 
						go a.core.router.tun.write()
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -28,7 +28,6 @@ type Core struct {
 | 
				
			||||||
	sessions    sessions
 | 
						sessions    sessions
 | 
				
			||||||
	router      router
 | 
						router      router
 | 
				
			||||||
	dht         dht
 | 
						dht         dht
 | 
				
			||||||
	tun         tunDevice
 | 
					 | 
				
			||||||
	admin       admin
 | 
						admin       admin
 | 
				
			||||||
	searches    searches
 | 
						searches    searches
 | 
				
			||||||
	multicast   multicast
 | 
						multicast   multicast
 | 
				
			||||||
| 
						 | 
					@ -59,7 +58,6 @@ func (c *Core) init(bpub *boxPubKey,
 | 
				
			||||||
	c.peers.init(c)
 | 
						c.peers.init(c)
 | 
				
			||||||
	c.router.init(c)
 | 
						c.router.init(c)
 | 
				
			||||||
	c.switchTable.init(c, c.sigPub) // TODO move before peers? before router?
 | 
						c.switchTable.init(c, c.sigPub) // TODO move before peers? before router?
 | 
				
			||||||
	c.tun.init(c)
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Get the current build name. This is usually injected if built from git,
 | 
					// Get the current build name. This is usually injected if built from git,
 | 
				
			||||||
| 
						 | 
					@ -188,7 +186,7 @@ func (c *Core) Start(nc *config.NodeConfig, log *log.Logger) error {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ip := net.IP(c.router.addr[:]).String()
 | 
						ip := net.IP(c.router.addr[:]).String()
 | 
				
			||||||
	if err := c.tun.start(nc.IfName, nc.IfTAPMode, fmt.Sprintf("%s/%d", ip, 8*len(address_prefix)-1), nc.IfMTU); err != nil {
 | 
						if err := c.router.tun.start(nc.IfName, nc.IfTAPMode, fmt.Sprintf("%s/%d", ip, 8*len(address_prefix)-1), nc.IfMTU); err != nil {
 | 
				
			||||||
		c.log.Println("Failed to start TUN/TAP")
 | 
							c.log.Println("Failed to start TUN/TAP")
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -200,7 +198,7 @@ func (c *Core) Start(nc *config.NodeConfig, log *log.Logger) error {
 | 
				
			||||||
// Stops the Yggdrasil node.
 | 
					// Stops the Yggdrasil node.
 | 
				
			||||||
func (c *Core) Stop() {
 | 
					func (c *Core) Stop() {
 | 
				
			||||||
	c.log.Println("Stopping...")
 | 
						c.log.Println("Stopping...")
 | 
				
			||||||
	c.tun.close()
 | 
						c.router.tun.close()
 | 
				
			||||||
	c.admin.close()
 | 
						c.admin.close()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -293,10 +291,10 @@ func (c *Core) GetTUNDefaultIfTAPMode() bool {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Gets the current TUN/TAP interface name.
 | 
					// Gets the current TUN/TAP interface name.
 | 
				
			||||||
func (c *Core) GetTUNIfName() string {
 | 
					func (c *Core) GetTUNIfName() string {
 | 
				
			||||||
	return c.tun.iface.Name()
 | 
						return c.router.tun.iface.Name()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Gets the current TUN/TAP interface MTU.
 | 
					// Gets the current TUN/TAP interface MTU.
 | 
				
			||||||
func (c *Core) GetTUNIfMTU() int {
 | 
					func (c *Core) GetTUNIfMTU() int {
 | 
				
			||||||
	return c.tun.mtu
 | 
						return c.router.tun.mtu
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -30,6 +30,10 @@ import (
 | 
				
			||||||
	"golang.org/x/net/ipv6"
 | 
						"golang.org/x/net/ipv6"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type adapter struct {
 | 
				
			||||||
 | 
						tunDevice
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// The router struct has channels to/from the tun/tap device and a self peer (0), which is how messages are passed between this node and the peers/switch layer.
 | 
					// The router struct has channels to/from the tun/tap device and a self peer (0), which is how messages are passed between this node and the peers/switch layer.
 | 
				
			||||||
// The router's mainLoop goroutine is responsible for managing all information related to the dht, searches, and crypto sessions.
 | 
					// The router's mainLoop goroutine is responsible for managing all information related to the dht, searches, and crypto sessions.
 | 
				
			||||||
type router struct {
 | 
					type router struct {
 | 
				
			||||||
| 
						 | 
					@ -39,6 +43,7 @@ type router struct {
 | 
				
			||||||
	in        <-chan []byte          // packets we received from the network, link to peer's "out"
 | 
						in        <-chan []byte          // packets we received from the network, link to peer's "out"
 | 
				
			||||||
	out       func([]byte)           // packets we're sending to the network, link to peer's "in"
 | 
						out       func([]byte)           // packets we're sending to the network, link to peer's "in"
 | 
				
			||||||
	toRecv    chan router_recvPacket // packets to handle via recvPacket()
 | 
						toRecv    chan router_recvPacket // packets to handle via recvPacket()
 | 
				
			||||||
 | 
						tun       tunDevice              // TUN/TAP adapter
 | 
				
			||||||
	recv      chan<- []byte          // place where the tun pulls received packets from
 | 
						recv      chan<- []byte          // place where the tun pulls received packets from
 | 
				
			||||||
	send      <-chan []byte          // place where the tun puts outgoing packets
 | 
						send      <-chan []byte          // place where the tun puts outgoing packets
 | 
				
			||||||
	reset     chan struct{}          // signal that coords changed (re-init sessions/dht)
 | 
						reset     chan struct{}          // signal that coords changed (re-init sessions/dht)
 | 
				
			||||||
| 
						 | 
					@ -75,11 +80,12 @@ func (r *router) init(core *Core) {
 | 
				
			||||||
	send := make(chan []byte, 32)
 | 
						send := make(chan []byte, 32)
 | 
				
			||||||
	r.recv = recv
 | 
						r.recv = recv
 | 
				
			||||||
	r.send = send
 | 
						r.send = send
 | 
				
			||||||
	r.core.tun.recv = recv
 | 
						r.tun.recv = recv
 | 
				
			||||||
	r.core.tun.send = send
 | 
						r.tun.send = send
 | 
				
			||||||
	r.reset = make(chan struct{}, 1)
 | 
						r.reset = make(chan struct{}, 1)
 | 
				
			||||||
	r.admin = make(chan func(), 32)
 | 
						r.admin = make(chan func(), 32)
 | 
				
			||||||
	r.cryptokey.init(r.core)
 | 
						r.cryptokey.init(r.core)
 | 
				
			||||||
 | 
						r.tun.init(r.core)
 | 
				
			||||||
	// go r.mainLoop()
 | 
						// go r.mainLoop()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -279,7 +285,7 @@ func (r *router) sendPacket(bs []byte) {
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			// Create the ICMPv6 response from it
 | 
								// Create the ICMPv6 response from it
 | 
				
			||||||
			icmpv6Buf, err := r.core.tun.icmpv6.create_icmpv6_tun(
 | 
								icmpv6Buf, err := r.tun.icmpv6.create_icmpv6_tun(
 | 
				
			||||||
				bs[8:24], bs[24:40],
 | 
									bs[8:24], bs[24:40],
 | 
				
			||||||
				ipv6.ICMPTypeDestinationUnreachable, 1, ptb)
 | 
									ipv6.ICMPTypeDestinationUnreachable, 1, ptb)
 | 
				
			||||||
			if err == nil {
 | 
								if err == nil {
 | 
				
			||||||
| 
						 | 
					@ -304,7 +310,7 @@ func (r *router) sendPacket(bs []byte) {
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			// Create the ICMPv6 response from it
 | 
								// Create the ICMPv6 response from it
 | 
				
			||||||
			icmpv6Buf, err := r.core.tun.icmpv6.create_icmpv6_tun(
 | 
								icmpv6Buf, err := r.tun.icmpv6.create_icmpv6_tun(
 | 
				
			||||||
				bs[8:24], bs[24:40],
 | 
									bs[8:24], bs[24:40],
 | 
				
			||||||
				ipv6.ICMPTypePacketTooBig, 0, ptb)
 | 
									ipv6.ICMPTypePacketTooBig, 0, ptb)
 | 
				
			||||||
			if err == nil {
 | 
								if err == nil {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -273,7 +273,7 @@ func (ss *sessions) createSession(theirPermKey *boxPubKey) *sessionInfo {
 | 
				
			||||||
	sinfo.mySesPriv = *priv
 | 
						sinfo.mySesPriv = *priv
 | 
				
			||||||
	sinfo.myNonce = *newBoxNonce()
 | 
						sinfo.myNonce = *newBoxNonce()
 | 
				
			||||||
	sinfo.theirMTU = 1280
 | 
						sinfo.theirMTU = 1280
 | 
				
			||||||
	sinfo.myMTU = uint16(ss.core.tun.mtu)
 | 
						sinfo.myMTU = uint16(ss.core.router.tun.mtu)
 | 
				
			||||||
	now := time.Now()
 | 
						now := time.Now()
 | 
				
			||||||
	sinfo.time = now
 | 
						sinfo.time = now
 | 
				
			||||||
	sinfo.mtuTime = now
 | 
						sinfo.mtuTime = now
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue