Skip to content

Commit de9519d

Browse files
authored
Fix MAC and IP address comparison logic in generators (#164)
This commit corrects the comparison logic in setMacGenerator and setIpGenerator methods to accurately compare MAC and IP addresses byte by byte. The previous method relied on array reference comparison, which led to incorrect maximum address detection and subsequent address duplication. The newly introduced helper functions, compareMacs and compareIps, ensure element-wise comparison, preventing duplication by correctly identifying and initializing the maximum address for each generator. before: ![image](https://github.com/user-attachments/assets/dd776ef4-8f92-4ed8-99ed-3c9d834a4326) after: ![image](https://github.com/user-attachments/assets/b88f08a2-ae91-4629-9cae-95be602e396b)
1 parent 0ac2529 commit de9519d

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

src/context.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ import {
77
urManager,
88
} from "./types/viewportManager";
99
import { Layer } from "./types/devices/device";
10-
import { IpAddress, IpAddressGenerator } from "./packets/ip";
10+
import { compareIps, IpAddress, IpAddressGenerator } from "./packets/ip";
1111
import { layerFromName } from "./types/devices/layer";
1212
import { SpeedMultiplier } from "./types/devices/speedMultiplier";
13-
import { MacAddress, MacAddressGenerator } from "./packets/ethernet";
13+
import {
14+
compareMacs,
15+
MacAddress,
16+
MacAddressGenerator,
17+
} from "./packets/ethernet";
1418
import { Colors } from "./utils";
1519

1620
export class GlobalContext {
@@ -136,15 +140,19 @@ export class GlobalContext {
136140

137141
private setIpGenerator() {
138142
let maxIp = IpAddress.parse("10.0.0.0");
143+
139144
for (const [, device] of this.datagraph.getDevices()) {
140145
if (isNetworkNode(device)) {
141146
const ip = IpAddress.parse(device.ip);
142-
if (maxIp.octets < ip.octets) {
143-
maxIp = ip;
147+
if (ip === null) {
148+
console.error("Failed to parse IP address: " + device.ip);
149+
} else {
150+
if (compareIps(maxIp, ip) < 0) {
151+
maxIp = ip;
152+
}
144153
}
145154
}
146155
}
147-
// TODO: we should use IpAddress instead of string here and in Datagraph
148156
const baseIp = maxIp.toString();
149157
const mask = "255.255.255.255";
150158
this.ipGenerator = new IpAddressGenerator(baseIp, mask);
@@ -155,7 +163,7 @@ export class GlobalContext {
155163
for (const [, device] of this.datagraph.getDevices()) {
156164
if (isLinkNode(device)) {
157165
const mac = MacAddress.parse(device.mac);
158-
if (maxMac.octets < mac.octets) {
166+
if (compareMacs(maxMac, mac) < 0) {
159167
maxMac = mac;
160168
}
161169
}

src/packets/ethernet.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,11 @@ export interface FramePayload {
166166
// Get details of the payload
167167
getDetails(layer: Layer): Record<string, string | number | object>;
168168
}
169+
170+
export function compareMacs(mac1: MacAddress, mac2: MacAddress): number {
171+
for (let i = 0; i < 6; i++) {
172+
if (mac1.octets[i] < mac2.octets[i]) return -1;
173+
if (mac1.octets[i] > mac2.octets[i]) return 1;
174+
}
175+
return 0; // equal
176+
}

src/packets/ip.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,11 @@ export function computeIpChecksum(octets: Uint8Array): number {
299299
const carry = sum >> 16;
300300
return 0xffff ^ (checksum + carry);
301301
}
302+
303+
export function compareIps(ip1: IpAddress, ip2: IpAddress): number {
304+
for (let i = 0; i < 4; i++) {
305+
if (ip1.octets[i] < ip2.octets[i]) return -1;
306+
if (ip1.octets[i] > ip2.octets[i]) return 1;
307+
}
308+
return 0; // equal
309+
}

0 commit comments

Comments
 (0)