Coder Social home page Coder Social logo

dhcp's Introduction

DHCP-MON

DHCP-MON is a Node.js implementation of a DHCP socket connection. It provides a simple and efficient way to handle DHCP events in your network.

license Node.js CI Coverage Status npm version

NPM

Installation

You can install DHCP-MON using npm:

npm install dhcp-mon

Usage

DHCP-MON comes with TypeScript declarations, which can help you understand the module API. See declarations

DHCP Monitor Example

import { BOOTMessageType, Server } from "dhcp-mon";

const 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 Example

import { BOOTMessageType, Server } from "dhcp-mon";

const 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);
});

const ips = {};

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

  const 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];
    }
  }

  const offer = s.createOffer(pkt);

  offer.yiaddr = ip;

  s.send(offer);
});

s.on("request", (e) => {
  console.log("REQUEST");
  const 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

For more information about DHCP, you can refer to the following RFCs:

Related Projects

dhcp's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.