Check checks the tunnel device specified by path is present and accessible.
(path string)
| 16 | |
| 17 | // Check checks the tunnel device specified by path is present and accessible. |
| 18 | func (t *Tun) Check(path string) error { |
| 19 | f, err := os.OpenFile(path, os.O_RDWR, 0) |
| 20 | if err != nil { |
| 21 | return fmt.Errorf("TUN device is not available: %w", err) |
| 22 | } |
| 23 | defer f.Close() |
| 24 | |
| 25 | info, err := f.Stat() |
| 26 | if err != nil { |
| 27 | return fmt.Errorf("getting stat information for TUN file: %w", err) |
| 28 | } |
| 29 | |
| 30 | sys, ok := info.Sys().(*syscall.Stat_t) |
| 31 | if !ok { |
| 32 | return fmt.Errorf("%w", ErrTUNInfo) |
| 33 | } |
| 34 | |
| 35 | const expectedRdev = 2760 // corresponds to major 10 and minor 200 |
| 36 | if sys.Rdev != expectedRdev { |
| 37 | return fmt.Errorf("%w: %d instead of expected %d", |
| 38 | ErrTUNBadRdev, sys.Rdev, expectedRdev) |
| 39 | } |
| 40 | |
| 41 | if err := f.Close(); err != nil { |
| 42 | return fmt.Errorf("closing TUN device: %w", err) |
| 43 | } |
| 44 | |
| 45 | return nil |
| 46 | } |