Skip to content

Commit

Permalink
Monor changes in tun module
Browse files Browse the repository at this point in the history
  • Loading branch information
vikulin committed Dec 17, 2023
1 parent 403826f commit b6e711c
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/tun/tun.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ type TunAdapter struct {
isOpen bool
isEnabled bool // Used by the writer to drop sessionTraffic if not enabled
config struct {
fd int32
name InterfaceName
mtu InterfaceMTU
}
}

// Gets the maximum supported MTU for the platform based on the defaults in
// defaults.GetDefaults().
// config.GetDefaults().
func getSupportedMTU(mtu uint64) uint64 {
if mtu < 1280 {
return 1280
Expand Down Expand Up @@ -111,7 +112,11 @@ func (tun *TunAdapter) _start() error {
}
tun.addr = tun.rwc.Address()
tun.subnet = tun.rwc.Subnet()
addr := fmt.Sprintf("%s/%d", net.IP(tun.addr[:]).String(), 8*len(tun.core.GetPrefix())-1)
prefix := tun.core.GetPrefix()
var addr string
if tun.core.IsValidAddress(tun.addr) {
addr = fmt.Sprintf("%s/%d", net.IP(tun.addr[:]).String(), 8*len(prefix[:])-1)
}
if tun.config.name == "none" || tun.config.name == "dummy" {
tun.log.Debugln("Not starting TUN as ifname is none or dummy")
tun.isEnabled = false
Expand All @@ -122,7 +127,13 @@ func (tun *TunAdapter) _start() error {
if tun.rwc.MaxMTU() < mtu {
mtu = tun.rwc.MaxMTU()
}
if err := tun.setup(string(tun.config.name), addr, mtu); err != nil {
var err error
if tun.config.fd > 0 {
err = tun.setupFD(tun.config.fd, addr, mtu)

Check failure on line 132 in src/tun/tun.go

View workflow job for this annotation

GitHub Actions / Lint

tun.setupFD undefined (type *TunAdapter has no field or method setupFD) (typecheck)

Check failure on line 132 in src/tun/tun.go

View workflow job for this annotation

GitHub Actions / Lint

tun.setupFD undefined (type *TunAdapter has no field or method setupFD)) (typecheck)

Check failure on line 132 in src/tun/tun.go

View workflow job for this annotation

GitHub Actions / Lint

tun.setupFD undefined (type *TunAdapter has no field or method setupFD) (typecheck)

Check failure on line 132 in src/tun/tun.go

View workflow job for this annotation

GitHub Actions / Lint

tun.setupFD undefined (type *TunAdapter has no field or method setupFD)) (typecheck)
} else {
err = tun.setup(string(tun.config.name), addr, mtu)
}
if err != nil {
return err
}
if tun.MTU() != mtu {
Expand All @@ -145,6 +156,8 @@ func (tun *TunAdapter) IsStarted() bool {
return isOpen
}

// Start the setup process for the TUN adapter. If successful, starts the
// read/write goroutines to handle packets on that interface.
func (tun *TunAdapter) Stop() error {
var err error
phony.Block(tun, func() {
Expand Down

0 comments on commit b6e711c

Please sign in to comment.