engine

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	BROADCAST_MAC_ADDR = []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
)

Functions

func ParseIpAddr

func ParseIpAddr(ipAddrStr string) ([]byte, error)

func ParseMacAddr

func ParseMacAddr(macAddrStr string) ([]byte, error)

Types

type Config

type Config struct {
	DebugLog     bool                  // 调试日志
	NetIfList    []*NetIfConfig        // 网卡列表
	RoutingTable []*RoutingEntryConfig // 路由表
}

type Engine

type Engine struct {
	DebugLog       bool                        // 调试日志
	Stop           bool                        // 停止标志
	NetIfMap       map[string]*NetIf           // 网络接口集合 key:接口名 value:接口实例
	RoutingTable   []*RoutingEntry             // 路由表
	Ipv4PktFwdHook func(ipv4Pkt []byte) []byte // ip报文路由转发数据修改钩子
}

func InitEngine

func InitEngine(config *Config) (*Engine, error)

func (*Engine) GetNetIf

func (e *Engine) GetNetIf(name string) *NetIf

func (*Engine) RunEngine

func (e *Engine) RunEngine()

func (*Engine) StopEngine

func (e *Engine) StopEngine()

type InNatEntry added in v1.0.1

type InNatEntry struct {
	IpAddr        uint32 // ip地址
	Port          uint16 // 端口
	LastAliveTime uint32 // 上一次活跃时间
}

type NetIf

type NetIf struct {
	Name              string                      // 接口名
	MacAddr           []byte                      // mac地址
	IpAddr            []byte                      // ip地址
	NetworkMask       []byte                      // 子网掩码
	EthRxChan         chan []byte                 // 物理层接收管道
	EthTxChan         chan []byte                 // 物理层发送管道
	LoChan            chan []byte                 // 本地回环管道
	Engine            *Engine                     // 归属Engine指针
	ArpCacheTable     map[uint32]uint64           // arp缓存表 key:ip value:mac
	ArpCacheTableLock sync.RWMutex                // arp缓存表锁
	NatEnable         bool                        // 是否开启nat
	NatTable          map[OutNatEntry]*InNatEntry // nat表
	NatTableLock      sync.RWMutex                // nat表锁
	NatPortAlloc      map[uint32]*PortAlloc       // nat端口分配表 key:目的ip value:端口分配信息
	NatPortAllocLock  sync.RWMutex                // nat端口分配表锁
	HandleUdp         func(udpPayload []byte, udpSrcPort uint16, udpDstPort uint16, ipv4SrcAddr []byte)
}

func (*NetIf) GetArpCache

func (i *NetIf) GetArpCache(ipAddr []byte) (macAddr []byte)

func (*NetIf) HandleArp

func (i *NetIf) HandleArp(ethPayload []byte, ethSrcMac []byte)

func (*NetIf) MacAddrToU added in v1.0.1

func (i *NetIf) MacAddrToU(macAddr []byte) (macAddrU uint64)

func (*NetIf) NatTableClear added in v1.0.2

func (i *NetIf) NatTableClear()

func (*NetIf) PacketHandle

func (i *NetIf) PacketHandle()

func (*NetIf) RxEthernet added in v1.0.1

func (i *NetIf) RxEthernet(ethFrm []byte)

func (*NetIf) RxIcmp

func (i *NetIf) RxIcmp(ipv4Payload []byte, ipv4SrcAddr []byte)

func (*NetIf) RxIpv4

func (i *NetIf) RxIpv4(ethPayload []byte)

func (*NetIf) RxTcp

func (i *NetIf) RxTcp()

func (*NetIf) RxUdp

func (i *NetIf) RxUdp(ipv4Payload []byte, ipv4SrcAddr []byte)

func (*NetIf) SetArpCache

func (i *NetIf) SetArpCache(ipAddr []byte, macAddr []byte)

func (*NetIf) TxEthernet added in v1.0.1

func (i *NetIf) TxEthernet(ethPayload []byte, ethDstMac []byte, ethProto uint16) []byte

func (*NetIf) TxIcmp

func (i *NetIf) TxIcmp(icmpPayload []byte, seq uint16, ipv4DstAddr []byte) []byte

func (*NetIf) TxIpv4

func (i *NetIf) TxIpv4(ipv4Payload []byte, ipv4HeadProto uint8, ipv4DstAddr []byte) []byte

func (*NetIf) TxTcpAck

func (i *NetIf) TxTcpAck(tcpSrcPort uint16, tcpDstPort uint16, ipv4DstAddr []byte) []byte

func (*NetIf) TxTcpSyn

func (i *NetIf) TxTcpSyn(tcpSrcPort uint16, tcpDstPort uint16, ipv4DstAddr []byte) []byte

func (*NetIf) TxTcpSynAck

func (i *NetIf) TxTcpSynAck(tcpSrcPort uint16, tcpDstPort uint16, ipv4DstAddr []byte) []byte

func (*NetIf) TxUdp

func (i *NetIf) TxUdp(udpPayload []byte, udpSrcPort uint16, udpDstPort uint16, ipv4DstAddr []byte) []byte

func (*NetIf) UToMacAddr added in v1.0.1

func (i *NetIf) UToMacAddr(macAddrU uint64) (macAddr []byte)

type NetIfConfig

type NetIfConfig struct {
	Name        string      // 网卡名
	MacAddr     string      // mac地址
	IpAddr      string      // ip地址
	NetworkMask string      // 子网掩码
	NatEnable   bool        // 网络地址转换
	EthRxChan   chan []byte // 物理层接收管道
	EthTxChan   chan []byte // 物理层发送管道
}

type OutNatEntry added in v1.0.1

type OutNatEntry struct {
	DstIpAddr uint32 // 目的ip地址
	DstPort   uint16 // 目的端口
	SrcIpAddr uint32 // 源ip地址
	SrcPort   uint16 // 源端口
}

type PortAlloc added in v1.0.2

type PortAlloc struct {
	Lock          sync.RWMutex        // 锁
	AllocPortMap  map[SrcAddr]uint16  // 分配端口集合 key:源地址 value:端口
	RemainPortMap map[uint16]struct{} // 剩余端口集合 key:端口
}

type RoutingEntry added in v1.0.1

type RoutingEntry struct {
	DstIpAddr   []byte // 目的ip地址
	NetworkMask []byte // 网络掩码
	NextHop     []byte // 下一跳
	NetIf       string // 出接口
}

type RoutingEntryConfig added in v1.0.1

type RoutingEntryConfig struct {
	DstIpAddr   string // 目的ip地址
	NetworkMask string // 网络掩码
	NextHop     string // 下一跳
	NetIf       string // 出接口
}

type SrcAddr added in v1.0.2

type SrcAddr struct {
	IpAddr uint32 // ip地址
	Port   uint16 // 端口
}

Jump to

Keyboard shortcuts

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