cni

package
v1.16.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 14, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PacketSize  = 65536
	ReceiveSize = 50
	SendSize    = 50
)

Variables

This section is empty.

Functions

func AddRouteToTun

func AddRouteToTun(cidr string, name string) error

AddRouteToTun route data to tun device , witch dst IP belongs to the cidr

func BytesToUint16

func BytesToUint16(bytes []byte, offset int) uint16

func BytesToUint32

func BytesToUint32(bytes []byte, offset int) uint32

func DialTun

func DialTun(stream net.Conn, name string)

DialTun run by P2P host

func ExecCommand

func ExecCommand(command string) error

func IPToArray4

func IPToArray4(ip net.IP) [4]byte

func SetupTunDevice

func SetupTunDevice(name string) error

Setup Tun Device TODO: setup tun device by code but not command and need to check wether there is a tun

func Uint16ToBytes

func Uint16ToBytes(value uint16) []byte

func Uint32ToBytes

func Uint32ToBytes(value uint32) []byte

Types

type ByteBuffer

type ByteBuffer interface {
	// Write data from src to buffer
	// panic if no enough space
	Write(src []byte)

	// Read data from buffer to dst
	// returns the number of bytes actually read
	Read(dst []byte) int

	// Capacity  of this buffer
	Capacity() int

	// ReadableBytes how many bytes can be read
	ReadableBytes() int

	// ReadIndex read index
	ReadIndex() int

	// WriteIndex write index for next byte
	WriteIndex() int

	// Mark  current buffer status for subsequent recovery
	Mark()

	// Recover  buffer status to mark point
	// if the Mark method has not been called before, the behavior is unknown
	Recover()

	// Clean  status
	Clean()
}

func NewRecycleByteBuffer

func NewRecycleByteBuffer(size int) ByteBuffer

type IPFrame

type IPFrame struct {
	// 4 bits
	Version byte

	// 4 bits
	// in 4bytes
	HeaderLen byte

	// 8 bits
	Tos byte

	// 16 bits
	// in byte
	TotalLen uint16

	// 16 bits
	Identification uint16

	// 3 bits
	Flag byte

	// 13 bits
	Offset uint16

	// 8 bits
	TTL byte

	// 8 bits
	Protocol byte

	// 16 bits
	HeaderCheckSum uint16

	// 32 bits
	Source net.IP

	// 32 bits
	Target net.IP

	// options
	Options []Option

	// payload
	Payload []byte
}

func ParseIPFrame

func ParseIPFrame(buffer ByteBuffer) (*IPFrame, error)

ParseIPFrame TUN - IPv4 Packet:

+---------------------------------------------------------------------------------------------------------------+
|       | Octet |           0           |           1           |           2           |           3           |
| Octet |  Bit  |00|01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|
+---------------------------------------------------------------------------------------------------------------+
|   0   |   0   |  Version  |    IHL    |      DSCP       | ECN |                 Total  Length                 |
+---------------------------------------------------------------------------------------------------------------+
|   4   |  32   |                Identification                 | Flags  |           Fragment Offset            |
+---------------------------------------------------------------------------------------------------------------+
|   8   |  64   |     Time To Live      |       Protocol        |                Header Checksum                |
+---------------------------------------------------------------------------------------------------------------+
|  12   |  96   |                                       Source IP Address                                       |
+---------------------------------------------------------------------------------------------------------------+
|  16   |  128  |                                    Destination IP Address                                     |
+---------------------------------------------------------------------------------------------------------------+
|  20   |  160  |                                     Options (if IHL > 5)                                      |
+---------------------------------------------------------------------------------------------------------------+
|  24   |  192  |                                                                                               |
|  30   |  224  |                                            Payload                                            |
|  ...  |  ...  |                                                                                               |
+---------------------------------------------------------------------------------------------------------------+

func (*IPFrame) GetPayloadLen

func (frame *IPFrame) GetPayloadLen() int

func (*IPFrame) GetProtocol

func (frame *IPFrame) GetProtocol() string

func (*IPFrame) GetSourceIP

func (frame *IPFrame) GetSourceIP() string

func (*IPFrame) GetTargetIP

func (frame *IPFrame) GetTargetIP() string

func (*IPFrame) Strings

func (frame *IPFrame) Strings() string

func (*IPFrame) ToBytes

func (frame *IPFrame) ToBytes() []byte

type Option

type Option struct {
	Key   uint16
	Value uint16
}

type RecycleByteBuffer

type RecycleByteBuffer struct {
	// contains filtered or unexported fields
}

func (*RecycleByteBuffer) Capacity

func (buffer *RecycleByteBuffer) Capacity() int

func (*RecycleByteBuffer) Clean

func (buffer *RecycleByteBuffer) Clean()

func (*RecycleByteBuffer) Mark

func (buffer *RecycleByteBuffer) Mark()

func (*RecycleByteBuffer) Read

func (buffer *RecycleByteBuffer) Read(dst []byte) int

func (*RecycleByteBuffer) ReadIndex

func (buffer *RecycleByteBuffer) ReadIndex() int

func (*RecycleByteBuffer) ReadableBytes

func (buffer *RecycleByteBuffer) ReadableBytes() int

func (*RecycleByteBuffer) Recover

func (buffer *RecycleByteBuffer) Recover()

func (*RecycleByteBuffer) Write

func (buffer *RecycleByteBuffer) Write(src []byte)

func (*RecycleByteBuffer) WriteIndex

func (buffer *RecycleByteBuffer) WriteIndex() int

type TunConn

type TunConn struct {

	// Receive pipeline for transport data to p2p
	ReceivePipe chan []byte

	// Tcp pipeline for transport data to p2p
	WritePipe chan []byte
	// contains filtered or unexported fields
}

func Accept

func Accept() (*TunConn, error)

func Dial

func Dial() (*TunConn, error)

func NewTunConn

func NewTunConn(name string) (*TunConn, error)

NewTunConn create fd and calls tun device

func (*TunConn) CleanSingleTunRoute

func (tun *TunConn) CleanSingleTunRoute(cidr string) error

CleanSingleTunRoute Delete Single Route attach to Tun

func (*TunConn) CleanTunDevice

func (tun *TunConn) CleanTunDevice() error

CleanTunDevice delete all the Route and change iin kernel

func (*TunConn) CleanTunRoute

func (tun *TunConn) CleanTunRoute() error

CleanTunRoute Delete All Routes attach to Tun

func (*TunConn) Close

func (tun *TunConn) Close() error

func (*TunConn) LocalAddr

func (tun *TunConn) LocalAddr() net.Addr

LocalAddr return Local Tun Addr

func (*TunConn) Read

func (tun *TunConn) Read(packet []byte) (int, error)

Read Packet From TunConn. It is designed to Read data from TunConn RecievePipe Channel data flow as : tun ---data---> tunConn.ReceivePipe ---Read(cni)---> cni when you need to directly read from tun ,you could try tunConn.tun.Read(b []byte)

func (*TunConn) RemoteAddr

func (tun *TunConn) RemoteAddr() net.Addr

func (*TunConn) SetDeadline

func (tun *TunConn) SetDeadline(_ time.Time) error

func (*TunConn) SetReadDeadline

func (tun *TunConn) SetReadDeadline(_ time.Time) error

func (*TunConn) SetWriteDeadline

func (tun *TunConn) SetWriteDeadline(_ time.Time) error

func (*TunConn) TunReceiveLoop

func (tun *TunConn) TunReceiveLoop()

TunReceiveLoop Acting as Accept(). It will listen to tun, when any cni Read from tun ,this cni will be read into ReceivePipe data flow as : tun ---TunReceiveLoop()---> tunConn.ReceivePipe TunReceiveLoop works like producer and Read acts as consumer, when TunConn start, ReceiveLoop should also start to listen to tun

func (*TunConn) TunWriteLoop

func (tun *TunConn) TunWriteLoop()

TunWriteLoop Acting as Dial(). It will "dial" tun, when any cni Write into WritePipe ,this cni will be Write into tun in the form of rae socket data flow as : tun <---TunWriteLoop()--- tunConn.WriteLoop TunWriteLoop() works like consumer and Write acts as producer, when TunConn start, WriteLoop should also start to dial tun

func (*TunConn) Write

func (tun *TunConn) Write(packet []byte) (int, error)

Write Packet To TunConn. It is designed to Write data to TunConn WritePipe Channel data flow as : tun <---data--- tunConn.WritePipe <---Write(cni)--- cni when you need to directly write to tun ,you could try tunConn.tun.Write(b []byte)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL