This commit is contained in:
anlicheng 2026-03-30 12:31:19 +08:00
parent 19414b4543
commit 92f224e721

View File

@ -564,7 +564,8 @@ actor SDLContextActor {
SDLLogger.shared.log("[SDLContext] get invalid arp packet", level: .debug) SDLLogger.shared.log("[SDLContext] get invalid arp packet", level: .debug)
} }
case .ipv4: case .ipv4:
guard let ipPacket = IPPacket(layerPacket.data), ipPacket.header.destination == networkAddr.ip else { // ip
guard let ipPacket = IPPacket(layerPacket.data) else {
return return
} }
@ -676,19 +677,37 @@ actor SDLContextActor {
SDLLogger.shared.log("[SDLContext] flow_session: \(flowSession)", level: .debug) SDLLogger.shared.log("[SDLContext] flow_session: \(flowSession)", level: .debug)
} }
// arpmac //
if let dstMac = await self.arpServer.query(ip: dstIp) { // ip
SDLLogger.shared.log("[SDLContext] dstIp: \(dstIp.asIpAddress()), dst_mac is: \(SDLUtil.formatMacAddress(mac: dstMac))", level: .debug) if SDLUtil.inSameNetwork(ip: dstIp, compareIp: networkAddr.ip, maskLen: networkAddr.maskLen) {
await self.routeLayerPacket(dstMac: dstMac, type: .ipv4, data: packet.data) // arpmac
if let dstMac = await self.arpServer.query(ip: dstIp) {
SDLLogger.shared.log("[SDLContext] dstIp: \(dstIp.asIpAddress()), dst_mac is: \(SDLUtil.formatMacAddress(mac: dstMac))", level: .debug)
await self.routeLayerPacket(dstMac: dstMac, type: .ipv4, data: packet.data)
}
else {
SDLLogger.shared.log("[SDLContext] dstIp: \(dstIp.asIpAddress()) arp query not found, broadcast", level: .debug)
// // arp广
// let arpReqeust = ARPPacket.arpRequest(senderIP: networkAddr.ip, senderMAC: networkAddr.mac, targetIP: dstIp)
// await self.routeLayerPacket(dstMac: ARPPacket.broadcastMac , type: .arp, data: arpReqeust.marshal())
try? await self.arpServer.arpRequest(targetIp: dstIp, use: self.quicClient)
}
} }
//
//
else { else {
SDLLogger.shared.log("[SDLContext] dstIp: \(dstIp.asIpAddress()) arp query not found, broadcast", level: .debug) let exitNodeIp: UInt32 = 1234
// // arp广 SDLLogger.shared.log("[SDLContext] global dstIp: \(packet.header.destination.asIpAddress())", level: .debug)
// let arpReqeust = ARPPacket.arpRequest(senderIP: networkAddr.ip, senderMAC: networkAddr.mac, targetIP: dstIp) // arpmac
// await self.routeLayerPacket(dstMac: ARPPacket.broadcastMac , type: .arp, data: arpReqeust.marshal()) if let dstMac = await self.arpServer.query(ip: exitNodeIp) {
await self.routeLayerPacket(dstMac: dstMac, type: .ipv4, data: packet.data)
try? await self.arpServer.arpRequest(targetIp: dstIp, use: self.quicClient) }
else {
try? await self.arpServer.arpRequest(targetIp: exitNodeIp, use: self.quicClient)
}
} }
} }
private func routeLayerPacket(dstMac: Data, type: LayerPacket.PacketType, data: Data) async { private func routeLayerPacket(dstMac: Data, type: LayerPacket.PacketType, data: Data) async {
@ -739,6 +758,9 @@ actor SDLContextActor {
let routes: [NEIPv4Route] = [ let routes: [NEIPv4Route] = [
NEIPv4Route(destinationAddress: networkAddress.netAddress, subnetMask: networkAddress.maskAddress), NEIPv4Route(destinationAddress: networkAddress.netAddress, subnetMask: networkAddress.maskAddress),
NEIPv4Route(destinationAddress: dnsServer, subnetMask: "255.255.255.255"), NEIPv4Route(destinationAddress: dnsServer, subnetMask: "255.255.255.255"),
// TODO
NEIPv4Route(destinationAddress: "172.16.1.0", subnetMask: "255.255.255.0"),
] ]
// Add code here to start the process of connecting the tunnel. // Add code here to start the process of connecting the tunnel.