Skip to content

Commit b9dbb4e

Browse files
fix: send echo reply through correct interface (#285)
This PR fixes an issue where echo replies were being sent through the same interface the matching echo request was received. That results in dropped replies in cases a router is connected to more than one viable next-hop. Example: ![example with fix](https://github.com/user-attachments/assets/aedcccc3-43f0-4d77-b9fa-522b37457356) Without the fix, the response shown on the example was being sent to device 2 with the MAC address of device 3. --------- Co-authored-by: Pedro Gallino <[email protected]>
1 parent 69ce983 commit b9dbb4e

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/types/packet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ export function sendViewPacket(
365365
viewgraph: ViewGraph,
366366
srcId: DeviceId,
367367
rawPacket: EthernetFrame,
368-
sendingIface?: number,
368+
sendingIface: number,
369369
) {
370370
const srcMac = rawPacket.source;
371371
const dstMac = rawPacket.destination;

src/types/view-devices/vNetworkDevice.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ export abstract class ViewNetworkDevice extends ViewDevice {
168168
: undefined;
169169
}
170170

171+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
171172
handleDatagram(datagram: IPv4Packet, iface: number) {
172173
console.debug("Packet has reach its destination!");
173174
const dstDevice = this.viewgraph.getDeviceByIP(datagram.sourceAddress);
@@ -181,16 +182,17 @@ export abstract class ViewNetworkDevice extends ViewDevice {
181182
case ICMP_PROTOCOL_NUMBER: {
182183
const request: EchoRequest = datagram.payload as EchoRequest;
183184
if (dstDevice && request.type === ICMP_REQUEST_TYPE_NUMBER) {
184-
const { src, nextHop, dst } = ViewNetworkDevice.getForwardingData(
185-
this.id,
186-
dstDevice.id,
187-
this.viewgraph,
188-
);
185+
const { src, nextHop, dst, sendingIface } =
186+
ViewNetworkDevice.getForwardingData(
187+
this.id,
188+
dstDevice.id,
189+
this.viewgraph,
190+
);
189191
const echoReply = new EchoReply(0);
190192
const ipPacket = new IPv4Packet(src.ip, dst.ip, echoReply);
191193
const frame = new EthernetFrame(src.mac, nextHop.mac, ipPacket);
192194
console.debug(`Sending EchoReply to ${dstDevice}`);
193-
sendViewPacket(this.viewgraph, this.id, frame, iface);
195+
sendViewPacket(this.viewgraph, this.id, frame, sendingIface);
194196
}
195197
break;
196198
}

0 commit comments

Comments
 (0)