mirror of
https://github.com/bjdgyc/anylink.git
synced 2025-08-08 06:32:04 +08:00
优化payload
This commit is contained in:
@@ -88,14 +88,14 @@ func tapWrite(ifce *water.Interface, cSess *sessdata.ConnSession) {
|
||||
}()
|
||||
|
||||
var (
|
||||
err error
|
||||
payload *sessdata.Payload
|
||||
frame ethernet.Frame
|
||||
err error
|
||||
pl *sessdata.Payload
|
||||
frame ethernet.Frame
|
||||
)
|
||||
|
||||
for {
|
||||
select {
|
||||
case payload = <-cSess.PayloadIn:
|
||||
case pl = <-cSess.PayloadIn:
|
||||
case <-cSess.CloseChan:
|
||||
return
|
||||
}
|
||||
@@ -103,16 +103,15 @@ func tapWrite(ifce *water.Interface, cSess *sessdata.ConnSession) {
|
||||
// var frame ethernet.Frame
|
||||
fb := getByteFull()
|
||||
frame = *fb
|
||||
pData := *payload.Data
|
||||
switch payload.LType {
|
||||
switch pl.LType {
|
||||
default:
|
||||
// log.Println(payload)
|
||||
case sessdata.LTypeEthernet:
|
||||
copy(frame, pData)
|
||||
frame = frame[:len(pData)]
|
||||
copy(frame, pl.Data)
|
||||
frame = frame[:len(pl.Data)]
|
||||
case sessdata.LTypeIPData: // 需要转换成 Ethernet 数据
|
||||
ip_src := waterutil.IPv4Source(pData)
|
||||
if waterutil.IsIPv6(pData) || !ip_src.Equal(cSess.IpAddr) {
|
||||
ip_src := waterutil.IPv4Source(pl.Data)
|
||||
if waterutil.IsIPv6(pl.Data) || !ip_src.Equal(cSess.IpAddr) {
|
||||
// 过滤掉IPv6的数据
|
||||
// 非分配给客户端ip,直接丢弃
|
||||
continue
|
||||
@@ -121,7 +120,7 @@ func tapWrite(ifce *water.Interface, cSess *sessdata.ConnSession) {
|
||||
// packet := gopacket.NewPacket(data, layers.LayerTypeIPv4, gopacket.Default)
|
||||
// fmt.Println("get:", packet)
|
||||
|
||||
ip_dst := waterutil.IPv4Destination(pData)
|
||||
ip_dst := waterutil.IPv4Destination(pl.Data)
|
||||
// fmt.Println("get:", ip_src, ip_dst)
|
||||
|
||||
var dstHw net.HardwareAddr
|
||||
@@ -141,8 +140,8 @@ func tapWrite(ifce *water.Interface, cSess *sessdata.ConnSession) {
|
||||
}
|
||||
// fmt.Println("Gateway", ip_dst, dstAddr.HardwareAddr)
|
||||
|
||||
frame.Prepare(dstHw, cSess.MacHw, ethernet.NotTagged, ethernet.IPv4, len(pData))
|
||||
copy(frame[12+2:], pData)
|
||||
frame.Prepare(dstHw, cSess.MacHw, ethernet.NotTagged, ethernet.IPv4, len(pl.Data))
|
||||
copy(frame[12+2:], pl.Data)
|
||||
}
|
||||
|
||||
// packet := gopacket.NewPacket(frame, layers.LayerTypeEthernet, gopacket.Default)
|
||||
@@ -154,7 +153,7 @@ func tapWrite(ifce *water.Interface, cSess *sessdata.ConnSession) {
|
||||
}
|
||||
|
||||
putByte(fb)
|
||||
putPayload(payload)
|
||||
putPayload(pl)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -167,7 +166,7 @@ func tapRead(ifce *water.Interface, cSess *sessdata.ConnSession) {
|
||||
var (
|
||||
err error
|
||||
n int
|
||||
buf []byte
|
||||
data []byte
|
||||
frame ethernet.Frame
|
||||
)
|
||||
|
||||
@@ -192,7 +191,7 @@ func tapRead(ifce *water.Interface, cSess *sessdata.ConnSession) {
|
||||
continue
|
||||
case ethernet.IPv4:
|
||||
// 发送IP数据
|
||||
data := frame.Payload()
|
||||
data = frame.Payload()
|
||||
|
||||
ip_dst := waterutil.IPv4Destination(data)
|
||||
if !ip_dst.Equal(cSess.IpAddr) {
|
||||
@@ -204,7 +203,12 @@ func tapRead(ifce *water.Interface, cSess *sessdata.ConnSession) {
|
||||
// packet := gopacket.NewPacket(data, layers.LayerTypeIPv4, gopacket.Default)
|
||||
// fmt.Println("put:", packet)
|
||||
|
||||
if payloadOut(cSess, sessdata.LTypeIPData, 0x00, data) {
|
||||
pl := getPayload()
|
||||
// 拷贝数据到pl
|
||||
copy(pl.Data, data)
|
||||
// 更新切片长度
|
||||
pl.Data = pl.Data[:len(data)]
|
||||
if payloadOut(cSess, pl) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -225,7 +229,7 @@ func tapRead(ifce *water.Interface, cSess *sessdata.ConnSession) {
|
||||
// 返回ARP数据
|
||||
src := &arpdis.Addr{IP: cSess.IpAddr, HardwareAddr: cSess.MacHw}
|
||||
dst := &arpdis.Addr{IP: arpReq.SourceProtAddress, HardwareAddr: frame.Source()}
|
||||
buf, err = arpdis.NewARPReply(src, dst)
|
||||
data, err = arpdis.NewARPReply(src, dst)
|
||||
if err != nil {
|
||||
base.Error(err)
|
||||
return
|
||||
@@ -242,7 +246,15 @@ func tapRead(ifce *water.Interface, cSess *sessdata.ConnSession) {
|
||||
copy(addr.HardwareAddr, frame.Source())
|
||||
arpdis.Add(addr)
|
||||
|
||||
if payloadIn(cSess, sessdata.LTypeEthernet, 0x00, buf) {
|
||||
pl := getPayload()
|
||||
// 设置为二层数据类型
|
||||
pl.LType = sessdata.LTypeEthernet
|
||||
// 拷贝数据到pl
|
||||
copy(pl.Data, data)
|
||||
// 更新切片长度
|
||||
pl.Data = pl.Data[:len(data)]
|
||||
|
||||
if payloadIn(cSess, pl) {
|
||||
return
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user