Internet Packet Lab

Three fictional experiments. Nothing is sent to a network.

1. Mini DNS

2. Route around broken links

Paths: A → B → D → F or A → C → E → F.

3. Message delivery

Code from the book

Copy and edit the samples in a JavaScript environment. The controls above use the same functions.

Lab 1

const directory = {
  "museum.example": "192.0.2.10",
  "school.example": "192.0.2.20"
};
function resolve(name) {
  return Object.hasOwn(directory, name)
    ? directory[name] : null;
}
console.log(resolve("museum.example"));
// 192.0.2.10

Lab 2

const graph = {
  A:["B","C"], B:["A","D"],
  C:["A","E"], D:["B","F"],
  E:["C","F"], F:["D","E"]
};
function route(start, end, broken = []) {
  const queue = [[start]], seen = new Set([start]);
  while (queue.length) {
    const path = queue.shift();
    const last = path[path.length - 1];
    if (last === end) return path;
    for (const next of graph[last]) {
      const blocked = broken.some(([a,b]) =>
        (a===last && b===next) ||
        (b===last && a===next));
      if (!blocked && !seen.has(next)) {
        seen.add(next);
        queue.push([...path,next]);
      }
    }
  }
  return null;
}
console.log(route("A","F",[["B","D"]]));
// ["A","C","E","F"]

Lab 3

function splitMessage(text) {
  const chunks = [];
  for (let i=0; i<text.length; i+=3) {
    chunks.push({id:i/3, data:text.slice(i,i+3)});
  }
  return chunks;
}
function assemble(chunks, count) {
  const byId = new Map(chunks.map(c=>[c.id,c.data]));
  const missing = [];
  for (let i=0; i<count; i++) {
    if (!byId.has(i)) missing.push(i);
  }
  if (missing.length) return {missing};
  return {text:Array.from({length:count},
    (_,i)=>byId.get(i)).join("")};
}
const sent = splitMessage("HELLO LEO");
console.log(assemble([sent[2],sent[0]],3));
// { missing: [1] }
console.log(assemble(sent.slice().reverse(),3));
// { text: "HELLO LEO" }

Return to the companion index