Documentation ¶
Index ¶
- Constants
- func AddRouteToTun(cidr string, name string) error
- func BytesToUint16(bytes []byte, offset int) uint16
- func BytesToUint32(bytes []byte, offset int) uint32
- func DialTun(stream net.Conn, name string)
- func ExecCommand(command string) error
- func IPToArray4(ip net.IP) [4]byte
- func SetupTunDevice(name string) error
- func Uint16ToBytes(value uint16) []byte
- func Uint32ToBytes(value uint32) []byte
- type ByteBuffer
- type IPFrame
- type Option
- type RecycleByteBuffer
- func (buffer *RecycleByteBuffer) Capacity() int
- func (buffer *RecycleByteBuffer) Clean()
- func (buffer *RecycleByteBuffer) Mark()
- func (buffer *RecycleByteBuffer) Read(dst []byte) int
- func (buffer *RecycleByteBuffer) ReadIndex() int
- func (buffer *RecycleByteBuffer) ReadableBytes() int
- func (buffer *RecycleByteBuffer) Recover()
- func (buffer *RecycleByteBuffer) Write(src []byte)
- func (buffer *RecycleByteBuffer) WriteIndex() int
- type TunConn
- func (tun *TunConn) CleanSingleTunRoute(cidr string) error
- func (tun *TunConn) CleanTunDevice() error
- func (tun *TunConn) CleanTunRoute() error
- func (tun *TunConn) Close() error
- func (tun *TunConn) LocalAddr() net.Addr
- func (tun *TunConn) Read(packet []byte) (int, error)
- func (tun *TunConn) RemoteAddr() net.Addr
- func (tun *TunConn) SetDeadline(_ time.Time) error
- func (tun *TunConn) SetReadDeadline(_ time.Time) error
- func (tun *TunConn) SetWriteDeadline(_ time.Time) error
- func (tun *TunConn) TunReceiveLoop()
- func (tun *TunConn) TunWriteLoop()
- func (tun *TunConn) Write(packet []byte) (int, error)
Constants ¶
const ( PacketSize = 65536 ReceiveSize = 50 SendSize = 50 )
Variables ¶
This section is empty.
Functions ¶
func AddRouteToTun ¶
AddRouteToTun route data to tun device , witch dst IP belongs to the cidr
func BytesToUint16 ¶
func BytesToUint32 ¶
func ExecCommand ¶
func IPToArray4 ¶
func SetupTunDevice ¶
Setup Tun Device TODO: setup tun device by code but not command and need to check wether there is a tun
func Uint16ToBytes ¶
func Uint32ToBytes ¶
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 (*IPFrame) GetProtocol ¶
func (*IPFrame) GetSourceIP ¶
func (*IPFrame) GetTargetIP ¶
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 NewTunConn ¶
NewTunConn create fd and calls tun device
func (*TunConn) CleanSingleTunRoute ¶
CleanSingleTunRoute Delete Single Route attach to Tun
func (*TunConn) CleanTunDevice ¶
CleanTunDevice delete all the Route and change iin kernel
func (*TunConn) CleanTunRoute ¶
CleanTunRoute Delete All Routes attach to Tun
func (*TunConn) Read ¶
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 (*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