Skip to content

oxygen/dhcp

Repository files navigation

DHCP-MON

license CircleCI Coverage Status npm version

NPM

NodeJS implementation of DHCP socket connection

Install

npm install dhcp-mon

Declarations

Module has got TypeScript declarations. It can be helpful to understand the module API

See declarations

Examples

DHCP monitor

"use strict";
let { BOOTMessageType, Server } = require("dhcp-mon");

let s = new Server("192.168.1.1");

s.on("listening", () => {
    console.log("Server start", s.address);
});

s.on("dhcp", e => {
    console.log(e.packet.toString());
});

s.bind();

DHCP server

"use strict";
let { BOOTMessageType, Server } = require("dhcp-mon");

let s = new Server({
    serverId: "192.168.1.1",
    gateways: ["192.168.1.1"],
    domainServer: ["192.168.1.1"],
});

s.on("listening", () => {
    console.log("Server start", s.address);
});

let ips = {};

s.on("discover", e => {
    console.log("DISCOVER");

    let pkt = e.packet;

    // Get IP by MAC
    let ip = "0.0.0.0";
    if (pkt.op === BOOTMessageType.request) {
        if (!(pkt.chaddr in ips))
            ip = ips[pkt.chaddr] = `192.168.1.${Object.keys(ips).length + 2}`;
        else
            ip = ips[pkt.chaddr];
    }

    let offer = s.createOffer(pkt);

    offer.yiaddr = ip;

    s.send(offer);
});
s.on("request", e => {
    console.log("REQUEST");
    let ack = s.createAck(e.packet);

    ack.yiaddr = ips[e.packet.chaddr];

    s.send(ack);
});
s.on("release", e => {
    console.log("RELEASE");
    delete ips[e.packet.chaddr];
});

s.bind();

RFC

RELATED

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •