|
| 1 | +package ifce |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/songgao/water" |
| 8 | + "github.com/vishvananda/netlink" |
| 9 | +) |
| 10 | + |
| 11 | +// MaximumTransmissionUnit (MTU) is the size of the largest protocol data unit (PDU) that can be communicated in a |
| 12 | +// single network layer transaction. |
| 13 | +const MaximumTransmissionUnit = 1000 |
| 14 | + |
| 15 | +const InterfaceName = "shb" |
| 16 | + |
| 17 | +var ErrGenerateInterface = errors.New("failed to generate an interface") |
| 18 | + |
| 19 | +func generateInterfaceName() (string, error) { |
| 20 | + const attempts = 10 |
| 21 | + |
| 22 | + for i := 0; i < attempts; i++ { |
| 23 | + name := fmt.Sprintf("%s%d", InterfaceName, i) |
| 24 | + |
| 25 | + if _, err := netlink.LinkByName(name); err == nil { |
| 26 | + continue |
| 27 | + } |
| 28 | + |
| 29 | + return name, nil |
| 30 | + } |
| 31 | + |
| 32 | + return "", ErrGenerateInterface |
| 33 | +} |
| 34 | + |
| 35 | +type Interface struct { |
| 36 | + face *water.Interface |
| 37 | + link netlink.Link |
| 38 | +} |
| 39 | + |
| 40 | +var ( |
| 41 | + ErrInterfaceCreate = errors.New("failed to create the interface") |
| 42 | + ErrInterfaceUp = errors.New("failed to get up the interface") |
| 43 | + ErrInterfaceConfiguration = errors.New("failed to configure the interface") |
| 44 | + ErrInterfaceMTU = errors.New("failed to configure the MTU") |
| 45 | +) |
| 46 | + |
| 47 | +func NewInterface(addrr string) (*Interface, error) { |
| 48 | + name, err := generateInterfaceName() |
| 49 | + if err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + |
| 53 | + iface, err := water.New(water.Config{ |
| 54 | + DeviceType: water.TUN, |
| 55 | + PlatformSpecificParams: water.PlatformSpecificParams{ |
| 56 | + Name: name, |
| 57 | + MultiQueue: true, |
| 58 | + }, |
| 59 | + }) |
| 60 | + if err != nil { |
| 61 | + return nil, errors.Join(ErrInterfaceCreate, err) |
| 62 | + } |
| 63 | + |
| 64 | + addr, err := netlink.ParseAddr(addrr) |
| 65 | + if err != nil { |
| 66 | + return nil, errors.Join(ErrInterfaceConfiguration, err) |
| 67 | + } |
| 68 | + |
| 69 | + link, err := netlink.LinkByName(iface.Name()) |
| 70 | + if err != nil { |
| 71 | + return nil, errors.Join(ErrInterfaceConfiguration, err) |
| 72 | + } |
| 73 | + |
| 74 | + if err := netlink.AddrAdd(link, addr); err != nil { |
| 75 | + return nil, errors.Join(ErrInterfaceConfiguration, err) |
| 76 | + } |
| 77 | + |
| 78 | + if err := netlink.LinkSetMTU(link, MaximumTransmissionUnit); err != nil { |
| 79 | + return nil, errors.Join(ErrInterfaceMTU, err) |
| 80 | + } |
| 81 | + |
| 82 | + return &Interface{ |
| 83 | + face: iface, |
| 84 | + link: link, |
| 85 | + }, nil |
| 86 | +} |
| 87 | + |
| 88 | +func (i *Interface) Up() error { |
| 89 | + if err := netlink.LinkSetUp(i.link); err != nil { |
| 90 | + return errors.Join(ErrInterfaceUp, err) |
| 91 | + } |
| 92 | + |
| 93 | + return nil |
| 94 | +} |
| 95 | + |
| 96 | +func (i *Interface) Name() string { |
| 97 | + return i.face.Name() |
| 98 | +} |
| 99 | + |
| 100 | +func (i *Interface) Close() error { |
| 101 | + return i.face.Close() |
| 102 | +} |
| 103 | + |
| 104 | +func (i *Interface) Read(buffer []byte) (int, error) { |
| 105 | + return i.face.Read(buffer) |
| 106 | +} |
| 107 | + |
| 108 | +func (i *Interface) Write(buffer []byte) (int, error) { |
| 109 | + return i.face.Write(buffer) |
| 110 | +} |
0 commit comments