mirror of
https://github.com/impact-eintr/netstack.git
synced 2025-10-13 00:23:41 +08:00
handleLocal开启本地环回
This commit is contained in:
@@ -9,7 +9,6 @@ import (
|
|||||||
"netstack/tcpip"
|
"netstack/tcpip"
|
||||||
"netstack/tcpip/header"
|
"netstack/tcpip/header"
|
||||||
"netstack/tcpip/link/fdbased"
|
"netstack/tcpip/link/fdbased"
|
||||||
"netstack/tcpip/link/loopback"
|
|
||||||
"netstack/tcpip/link/tuntap"
|
"netstack/tcpip/link/tuntap"
|
||||||
"netstack/tcpip/network/arp"
|
"netstack/tcpip/network/arp"
|
||||||
"netstack/tcpip/network/ipv4"
|
"netstack/tcpip/network/ipv4"
|
||||||
@@ -95,22 +94,15 @@ func main() {
|
|||||||
MTU: 1500,
|
MTU: 1500,
|
||||||
Address: tcpip.LinkAddress(maddr),
|
Address: tcpip.LinkAddress(maddr),
|
||||||
ResolutionRequired: true,
|
ResolutionRequired: true,
|
||||||
|
HandleLocal: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
loopbackLinkID := loopback.New()
|
|
||||||
|
|
||||||
// 新建相关协议的协议栈
|
// 新建相关协议的协议栈
|
||||||
s := stack.New([]string{ipv4.ProtocolName, arp.ProtocolName},
|
s := stack.New([]string{ipv4.ProtocolName, arp.ProtocolName},
|
||||||
[]string{tcp.ProtocolName, udp.ProtocolName}, stack.Options{})
|
[]string{tcp.ProtocolName, udp.ProtocolName}, stack.Options{})
|
||||||
|
|
||||||
// 新建抽象的网卡
|
// 新建抽象的网卡
|
||||||
_ = loopbackLinkID
|
if err := s.CreateNamedNIC(1, "eth0", linkID); err != nil {
|
||||||
if err := s.CreateNamedNIC(1, "lo", loopbackLinkID); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_ = linkID
|
|
||||||
if err := s.CreateNamedNIC(2, "fdbase", linkID); err != nil {
|
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,7 +1,6 @@
|
|||||||
package fdbased
|
package fdbased
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
"netstack/logger"
|
"netstack/logger"
|
||||||
"netstack/tcpip"
|
"netstack/tcpip"
|
||||||
@@ -123,13 +122,13 @@ func (e *endpoint) WritePacket(r *stack.Route, hdr buffer.Prependable,
|
|||||||
// 如果路由信息中有配置源MAC地址,那么使用该地址
|
// 如果路由信息中有配置源MAC地址,那么使用该地址
|
||||||
// 如果没有,则使用本网卡的地址
|
// 如果没有,则使用本网卡的地址
|
||||||
if r.LocalLinkAddress != "" {
|
if r.LocalLinkAddress != "" {
|
||||||
ethHdr.SrcAddr = r.LocalLinkAddress // 源网卡地址 说明这是一个转发报文
|
ethHdr.SrcAddr = r.LocalLinkAddress
|
||||||
} else {
|
} else {
|
||||||
ethHdr.SrcAddr = e.addr // 说明这是一个原始报文
|
ethHdr.SrcAddr = e.addr
|
||||||
}
|
}
|
||||||
eth.Encode(ethHdr) // 将以太帧信息作为报文头编入
|
eth.Encode(ethHdr) // 将以太帧信息作为报文头编入
|
||||||
logger.GetInstance().Info(logger.ETH, func() {
|
logger.GetInstance().Info(logger.ETH, func() {
|
||||||
log.Println("链路层写回以太报文 ", r.LocalAddress, " to ", r.RemoteAddress)
|
log.Println(e.handleLocal, r.LocalLinkAddress, "链路层写回以太报文 ", r.LocalAddress, " to ", r.RemoteAddress)
|
||||||
})
|
})
|
||||||
// 写入网卡中
|
// 写入网卡中
|
||||||
if payload.Size() == 0 {
|
if payload.Size() == 0 {
|
||||||
|
Reference in New Issue
Block a user