-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoc.go
More file actions
79 lines (79 loc) · 2.42 KB
/
Copy pathdoc.go
File metadata and controls
79 lines (79 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Package tuntap provides cross-platform support for creating and managing
// TUN (network tunnel) devices.
//
// TUN devices operate at layer 3 (network layer), handling raw IP packets
// without Ethernet framing. They are commonly used to implement VPNs,
// virtual networks, and other packet tunneling applications.
//
// # Supported Platforms
//
// This package supports Linux, macOS (Darwin), FreeBSD, OpenBSD, and Windows.
// Each platform provides a NativeTun type that implements the tun.Tun interface
// from github.com/asciimoth/gonnect/tun.
//
// # Basic Usage
//
// Create a TUN device by calling CreateTUN with the desired interface name
// and MTU:
//
// tun, err := tuntap.CreateTUN("utun0", 1420)
// if err != nil {
// log.Fatal(err)
// }
// defer tun.Close()
//
// name, _ := tun.Name()
// fmt.Printf("Created interface: %s\n", name)
//
// # Reading and Writing Packets
//
// Use the Read and Write methods to exchange IP packets with the TUN device.
// Read accepts a slice of buffers and populates them with incoming packets,
// returning the count and the size of each packet:
//
// bufs := make([][]byte, tuntap.IdealBatchSize)
// for i := range bufs {
// bufs[i] = make([]byte, 65535)
// }
// sizes := make([]int, tuntap.IdealBatchSize)
//
// n, err := tun.Read(bufs, sizes, 0)
// if err != nil {
// // handle error
// }
// for i := 0; i < n; i++ {
// packet := bufs[i][0:sizes[i]]
// // process IP packet
// }
//
// Write sends one or more IP packets from the provided buffers:
//
// _, err = tun.Write(bufs[:1], 0)
//
// # Platform-Specific Batch Sizes
//
// On Linux, when the kernel supports IFF_VNET_HDR, the TUN device enables
// virtio network header mode. This allows batched I/O (up to
// IdealBatchSize packets per call) and enables TCP/UDP generic receive
// offload (GRO) and generic segmentation offload (GSO). On all other
// platforms, BatchSize() returns 1.
//
// # Device Events
//
// The Events method returns a channel that emits device state changes
// (interface up/down, MTU updates). Callers should consume from this
// channel in a goroutine:
//
// go func() {
// for event := range tun.Events() {
// // handle event
// }
// }()
//
// # Windows
//
// On Windows, this package uses the Wintun driver
// (golang.zx2c4.com/wintun). The global variables WintunTunnelType and
// WintunStaticRequestedGUID can be set before calling CreateTUN to
// customize the adapter type and GUID.
package tuntap