| 1 | package waterscip-go gomod github.com/songgao/water . `github.com/songgao/water`/ |
| 2 | |
| 3 | import ( |
| 4 | "errorsscip-go gomod github.com/golang/go/src go1.11 errors/" |
| 5 | "ioscip-go gomod github.com/golang/go/src go1.11 io/" |
| 6 | ) |
| 7 | |
| 8 | // Interface is a TUN/TAP interface. |
| 9 | // |
| 10 | // MultiQueue(Linux kernel > 3.8): With MultiQueue enabled, user should hold multiple |
| 11 | // interfaces to send/receive packet in parallel. |
| 12 | // Kernel document about MultiQueue: https://www.kernel.org/doc/Documentation/networking/tuntap.txt |
| 13 | type Interfacescip-go gomod github.com/songgao/water . `github.com/songgao/water`/Interface# struct { |
| 14 | isTAPscip-go gomod github.com/songgao/water . `github.com/songgao/water`/Interface#isTAP. bool |
| 15 | ioscip-go gomod github.com/golang/go/src go1.11 io/.ReadWriteCloserscip-go gomod github.com/songgao/water . `github.com/songgao/water`/Interface#ReadWriteCloser. |
| 16 | namescip-go gomod github.com/songgao/water . `github.com/songgao/water`/Interface#name. string |
| 17 | } |
| 18 | |
| 19 | // DeviceType is the type for specifying device types. |
| 20 | type DeviceTypescip-go gomod github.com/songgao/water . `github.com/songgao/water`/DeviceType# int |
| 21 | |
| 22 | // TUN and TAP device types. |
| 23 | const ( |
| 24 | _ = iota |
| 25 | TUNscip-go gomod github.com/songgao/water . `github.com/songgao/water`/TUN. |
| 26 | TAPscip-go gomod github.com/songgao/water . `github.com/songgao/water`/TAP. |
| 27 | ) |
| 28 | |
| 29 | // Config defines parameters required to create a TUN/TAP interface. It's only |
| 30 | // used when the device is initialized. A zero-value Config is a valid |
| 31 | // configuration. |
| 32 | type Configscip-go gomod github.com/songgao/water . `github.com/songgao/water`/Config# struct { |
| 33 | // DeviceType specifies whether the device is a TUN or TAP interface. A |
| 34 | // zero-value is treated as TUN. |
| 35 | DeviceTypescip-go gomod github.com/songgao/water . `github.com/songgao/water`/Config#DeviceType. DeviceTypescip-go gomod github.com/songgao/water . `github.com/songgao/water`/DeviceType# |
| 36 | |
| 37 | // PlatformSpecificParams defines parameters that differ on different |
| 38 | // platforms. See comments for the type for more details. |
| 39 | PlatformSpecificParamsscip-go gomod github.com/songgao/water . `github.com/songgao/water`/Config#PlatformSpecificParams. |
| 40 | } |
| 41 | |
| 42 | func defaultConfigscip-go gomod github.com/songgao/water . `github.com/songgao/water`/defaultConfig().() Configscip-go gomod github.com/songgao/water . `github.com/songgao/water`/Config# { |
| 43 | return Configscip-go gomod github.com/songgao/water . `github.com/songgao/water`/Config#{ |
| 44 | DeviceTypescip-go gomod github.com/songgao/water . `github.com/songgao/water`/Config#DeviceType.: TUNscip-go gomod github.com/songgao/water . `github.com/songgao/water`/TUN., |
| 45 | PlatformSpecificParamsscip-go gomod github.com/songgao/water . `github.com/songgao/water`/Config#PlatformSpecificParams.: defaultPlatformSpecificParamsscip-go gomod github.com/songgao/water . `github.com/songgao/water`/defaultPlatformSpecificParams().(), |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | var zeroConfigscip-go gomod github.com/songgao/water . `github.com/songgao/water`/zeroConfig. Configscip-go gomod github.com/songgao/water . `github.com/songgao/water`/Config# |
| 50 | |
| 51 | // New creates a new TUN/TAP interface using config. |
| 52 | func Newscip-go gomod github.com/songgao/water . `github.com/songgao/water`/New().(config Configscip-go gomod github.com/songgao/water . `github.com/songgao/water`/Config#) (ifce *Interfacescip-go gomod github.com/songgao/water . `github.com/songgao/water`/Interface#, err error) { |
| 53 | if zeroConfigscip-go gomod github.com/songgao/water . `github.com/songgao/water`/zeroConfig. == config { |
| 54 | config = defaultConfigscip-go gomod github.com/songgao/water . `github.com/songgao/water`/defaultConfig().() |
| 55 | } |
| 56 | if config.PlatformSpecificParamsscip-go gomod github.com/songgao/water . `github.com/songgao/water`/Config#PlatformSpecificParams. == zeroConfigscip-go gomod github.com/songgao/water . `github.com/songgao/water`/zeroConfig..PlatformSpecificParamsscip-go gomod github.com/songgao/water . `github.com/songgao/water`/Config#PlatformSpecificParams. { |
| 57 | config.PlatformSpecificParamsscip-go gomod github.com/songgao/water . `github.com/songgao/water`/Config#PlatformSpecificParams. = defaultPlatformSpecificParamsscip-go gomod github.com/songgao/water . `github.com/songgao/water`/defaultPlatformSpecificParams().() |
| 58 | } |
| 59 | switch config.DeviceTypescip-go gomod github.com/songgao/water . `github.com/songgao/water`/Config#DeviceType. { |
| 60 | case TUNscip-go gomod github.com/songgao/water . `github.com/songgao/water`/TUN., TAPscip-go gomod github.com/songgao/water . `github.com/songgao/water`/TAP.: |
| 61 | return openDevscip-go gomod github.com/songgao/water . `github.com/songgao/water`/openDev().(config) |
| 62 | default: |
| 63 | return nil, errorsscip-go gomod github.com/golang/go/src go1.11 errors/.Newscip-go gomod github.com/golang/go/src go1.11 errors/New().("unknown device type") |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // IsTUN returns true if ifce is a TUN interface. |
| 68 | func (ifce *Interfacescip-go gomod github.com/songgao/water . `github.com/songgao/water`/Interface#) IsTUNscip-go gomod github.com/songgao/water . `github.com/songgao/water`/Interface#IsTUN().() bool { |
| 69 | return !ifce.isTAPscip-go gomod github.com/songgao/water . `github.com/songgao/water`/Interface#isTAP. |
| 70 | } |
| 71 | |
| 72 | // IsTAP returns true if ifce is a TAP interface. |
| 73 | func (ifce *Interfacescip-go gomod github.com/songgao/water . `github.com/songgao/water`/Interface#) IsTAPscip-go gomod github.com/songgao/water . `github.com/songgao/water`/Interface#IsTAP().() bool { |
| 74 | return ifce.isTAPscip-go gomod github.com/songgao/water . `github.com/songgao/water`/Interface#isTAP. |
| 75 | } |
| 76 | |
| 77 | // Name returns the interface name of ifce, e.g. tun0, tap1, tun0, etc.. |
| 78 | func (ifce *Interfacescip-go gomod github.com/songgao/water . `github.com/songgao/water`/Interface#) Namescip-go gomod github.com/songgao/water . `github.com/songgao/water`/Interface#Name().() string { |
| 79 | return ifce.namescip-go gomod github.com/songgao/water . `github.com/songgao/water`/Interface#name. |
| 80 | } |