Dhcp4java dhcpserver routine

Source: Internet
Author: User

Java code
/*
* This file is part of dhcp4java, a dhcp api for the Java language.
* (C) 2006 Stephen Hadinger
*
* This library is free software; you can redistribute it and/or
* Modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* Version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* But without any warranty; without even the implied warranty
* MERCHANTABILITY or fitness for a special PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You shoshould have written ed a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
Package org. dhcp4java. server;
 
Import static org. dhcp4java. DHCPConstants. BOOTREPLY;
Import static org. dhcp4java. DHCPConstants. DHCPACK;
Import static org. dhcp4java. DHCPConstants. DHCPOFFER;
Import static org. dhcp4java. DHCPConstants. DHO_DHCP_LEASE_TIME;
Import static org. dhcp4java. DHCPConstants. DHO_DHCP_SERVER_IDENTIFIER;
Import static org. dhcp4java. DHCPConstants. DHO_ROUTERS;
Import static org. dhcp4java. DHCPConstants. DHO_SUBNET_MASK;
Import static org. dhcp4java. DHCPConstants. HTYPE_ETHER;
 
Import java.net. InetAddress;
Import java.net. UnknownHostException;
Import java. util. HashMap;
Import java. util. Properties;
Import java. util. logging. Level;
Import java. util. logging. Logger;
 
Import org. dhcp4java. DHCPCoreServer;
Import org. dhcp4java. DHCPOption;
Import org. dhcp4java. DHCPPacket;
Import org. dhcp4java. DHCPResponseFactory;
Import org. dhcp4java. DHCPServerInitException;
Import org. dhcp4java. DHCPServlet;
 
/**
* A sample DHCP servlet (under construction ).
*
* @ Author STEPHEN an Hadinger
* @ Version 0.60
*/
Public class DHCPStaticServlet extends DHCPServlet {
 
Private static final Logger logger = Logger
. GetLogger ("org. dhcp4java. examplesserver. dhcpstaticserver ");
 
Private HashMap <String, InetAddress> macIpMap = new HashMap <String, InetAddress> ();
DHCPOption [] commonOptions;
 
/*
* (Non-Javadoc)
*
* @ See org. dhcp4java. DHCPServlet # init (java. util. Properties)
*/
@ Override
Public void init (Properties props ){
// We create a dummy packet to extract "common options"
DHCPPacket temp = new DHCPPacket ();
Try {
// Parse all properties to extract static client definitions
For (Object keyObject: props. keySet ()){
String key = (String) keyObject );
If (key. startsWith ("client .")){
String addrString = (String) props. get (keyObject );
Try {
InetAddress addr = InetAddress. getByName (addrString );
This. macIpMap. put (key. substring ("client.". length ())
. ToLowerCase (), addr );
} Catch (UnknownHostException e ){
Logger. log (Level. SEVERE, "cocould not parse InetAddress"
+ AddrString, e );
}
}
}
 
Temp. setOptionAsInetAddress (DHO_DHCP_SERVER_IDENTIFIER,
"192.168.1.1 ");
Temp. setOptionAsInt (DHO_DHCP_LEASE_TIME, 3600*24*7);/* 1 week */
Temp. setOptionAsInetAddress (DHO_SUBNET_MASK, "255.255.255.0 ");
Temp. setOptionAsInetAddress (DHO_ROUTERS, "192.168.1.1 ");
 
// Store options in a instance array
This. commonOptions = temp. getOptionsArray ();
} Catch (UnknownHostException e ){
Throw new RuntimeException (e );
}
}
 
/**
* @ See org. dhcp4java. DHCPServlet # doDiscover (org. dhcp4java. DHCPPacket)
*/
@ Override
Protected DHCPPacket doDiscover (DHCPPacket request ){
InetAddress clientIp = this. calcAddrFromMac (request );
If (clientIp = null ){
Return null;
}
 
DHCPPacket response = new DHCPPacket ();
Response. setOp (BOOTREPLY );
Response. setHtype (request. getHtype ());
Response. setHlen (request. getHlen ());
Response. setHops (byte) 0 );
Response. setXid (request. getXid ());
Response. setSecs (short) 0 );
Response. setYiaddr (clientIp );
Response. setSiaddr (server. getSockAddress (). getAddress (); // server
// Identifier
Response. setFlags (request. getFlags ());
Response. setGiaddrRaw (request. getGiaddrRaw ());
Response. setChaddr (request. getChaddr ());
 
Response. setDHCPMessageType (DHCPOFFER );
Response. setOptions (this. commonOptions );
 
Response. setAddrPort (DHCPResponseFactory. getdefasocketaddress (
Request, response. getOp ()));
 
Return response;
}
 
/**
* @ See org. dhcp4java. DHCPServlet # doRequest (org. dhcp4java. DHCPPacket)
*/
@ Override
Protected DHCPPacket doRequest (DHCPPacket request ){
InetAddress clientIp = this. calcAddrFromMac (request );
If (clientIp = null ){
Return null;
}
 
DHCPPacket response = new DHCPPacket ();
Response. setOp (BOOTREPLY );
Response. setHtype (request. getHtype ());
Response. setHlen (request. getHlen ());
Response. setHops (byte) 0 );
Response. setXid (request. getXid ());
Response. setSecs (short) 0 );
Response. setCiaddrRaw (request. getCiaddrRaw ());
Response. setYiaddr (clientIp );
Response. setSiaddr (server. getSockAddress (). getAddress (); // server
// Identifier
Response. setFlags (request. getFlags ());
Response. setGiaddrRaw (request. getGiaddrRaw ());
Response. setChaddr (request. getChaddr ());
 
Response. setDHCPMessageType (DHCPACK );
Response. setOptions (this. commonOptions );
 
Response. setAddrPort (DHCPResponseFactory. getdefasocketaddress (
Request, response. getOp ()));
 
Return response;
}
 
/**
* @ See org. dhcp4java. DHCPServlet # doDecline (org. dhcp4java. DHCPPacket)
*/
@ Override
Protected DHCPPacket doDecline (DHCPPacket request ){
Logger. warning ("DHCPDECLINE received:" + request. toString ());
Return null;
}
 
/**
* @ See org. dhcp4java. DHCPServlet # dorelbench (org. dhcp4java. DHCPPacket)
*/
@ Override
Protected DHCPPacket dorel.pdf (DHCPPacket request ){
Logger.info ("DHCPRELEASE received:" + request. toString ());
Return null;
}
 
/**
* Calculate address from packet and @ MAC Address.
*
* <P>
* Also checks if this client is accepted (Vendor, User ...)
*
* @ Param request
* @ Return address for client, or null if ignore
*/
Private InetAddress calcAddrFromMac (DHCPPacket request ){
// Check @ MAC address format
If (request. getHtype ()! = HTYPE_ETHER) & (request. getHlen ()! = 6 )){
Return null;
}
 
// Look for map
InetAddress clientIp = this. macIpMap. get (request. getChaddrAsHex ()
. ToLowerCase ());
If (clientIp = null ){
Return null; // not found
}
 
Return clientIp;
}
 
/**
* Server launcher.
*
* @ Param args
* Command line arguments-ignored
*/
Public static void main (String [] args ){
Try {
DHCPCoreServer server = DHCPCoreServer. initServer (
New DHCPStaticServlet (), null );
Logger. setLevel (Level. ALL );
New Thread (server). start ();
} Catch (DHCPServerInitException e ){
Logger. log (Level. SEVERE, "Server init", e );
}
}
}

Author: "Embedded Java & C"
 

Related Keywords:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.