- 浏览: 296248 次
- 性别:
- 来自: 广州
文章分类
/* * This file is part of dhcp4java, a DHCP API for the Java language. * (c) 2006 Stephan 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 of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received 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 Stephan 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, "Could 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.getDefaultSocketAddress( 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.getDefaultSocketAddress( 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#doRelease(org.dhcp4java.DHCPPacket) */ @Override protected DHCPPacket doRelease(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); } } }
发表评论
-
java IP地址转换
2013-03-28 09:05 1036/**将给定的字节数组转换成IPV4的十进制分段表示格式的ip ... -
ScreenCapture
2012-08-02 11:59 1041import java.awt.Rectangle; impo ... -
UdpSpeedTest
2012-06-04 18:26 1012import java.net.DatagramPacket ... -
PieChart3DDemo3.java
2012-04-24 13:41 1171import java.awt.BorderLayout ... -
java modbus
2012-04-06 13:54 1242modbus -
16款Java图表组件
2012-04-06 13:50 2484开源Java图表组件 1. JFreeChar ... -
java md5
2012-02-17 14:29 831import java.security.MessageDig ... -
FreeModbus
2012-02-14 11:15 862http://freemodbus.berlios.de/ap ... -
java 获取当前日期与时间
2012-01-19 16:52 966time = new SimpleDateFormat(&q ... -
java string sort
2012-01-19 16:46 916Arrays.sort(filelist, new Co ... -
java run dos command
2012-01-19 16:44 741Runtime.getRuntime().exec(" ... -
eclipse plugs
2012-01-17 16:47 764http://checkthread.org/eclipse- ... -
java
2012-01-12 17:18 964BufferedWriter out = new Buf ... -
Jpcap
2012-01-04 19:36 1016Jpcap is a Java library for ... -
java 获取网卡信息
2011-12-21 15:25 1202public static void main(Stri ... -
java 多网卡绑定例程
2011-12-21 15:01 1636单播: DatagramSocket s = n ... -
java multicast demo
2011-12-20 08:46 878import java.net.DatagramPack ... -
JavaService把java程序发布为windows服务
2011-12-09 14:45 992http://forge.ow2.org/projects/j ... -
chilkatsoft
2011-11-30 17:23 972http://www.chilkatsoft.com/ -
csv
2011-11-30 16:40 955Java CSV is a small fast open s ...
相关推荐
网上的DEMO都是无法用的,我调了半天的源码看懂了DHCP4Java的原理,对网上DEMO做了修改直接可以运行,我的运行环境是JDK1.8+WIN7x64+两个直连虚拟机(就是说网络中没有其他服务器),3分而已物有所值!!网络开发的...
DHCP4Java是一个基于Java语言实现的DHCP(动态主机配置协议)库,它为开发者提供了在Java应用程序中实现DHCP服务和客户端功能的能力。DHCP是一种网络协议,主要用于自动分配或动态更新IP地址、子网掩码、默认网关等...
5. **Java_DHCP4Java**:标签中的"java_dhcp4java"表明这个项目是用Java语言编写的,遵循Java的面向对象设计原则和编程规范。开发者可以利用Java的强大特性和跨平台性来部署DHCP服务。 6. **DHCP4Ja与dhcp4java**:...
在Java中实现DHCP服务,可以让我们构建自己的网络管理工具或者进行相关研究。本篇文章将详细探讨如何使用Java来实现DHCP服务器和客户端的功能。 首先,我们要理解DHCP的工作流程,它通常包括四个阶段:发现...
在Java中实现DHCP服务器和客户端可以帮助开发者更好地理解和控制网络配置过程,特别是在大规模网络环境中。下面我们将深入探讨如何在Java中实现DHCP服务及其相关知识。 首先,要理解DHCP协议的基本工作流程: 1. ...
java 操作dhcp的工具包,封装dhcp报文格式 Classes DHCPConstants DHCPCoreServer DHCPOption DHCPPacket DHCPResponseFactory DHCPServlet HardwareAddress InetCidr Util Exceptions ...
在Java中实现DHCP服务器是一项技术挑战,它需要深入理解网络协议栈的工作原理,以及如何在Java中构建网络服务。 首先,Java提供了一个强大的网络编程API,包括`java.net`和`java.nio`包,这些可以帮助我们创建...
### Win7 x64下实现DHCP Server功能 在企业级网络环境中,DHCP (Dynamic Host Configuration Protocol) 服务器的功能非常重要,它能够自动为局域网内的设备分配IP地址和其他网络参数,极大地简化了网络管理的工作量...
HanWin DHCP BOOTP TFTP Server 绿色破解 功能齐全使用简单
"dhcp-j(server)"可能是关于在Java环境中搭建和管理DHCP服务器的一个专题,可能包含如何在Java平台上实现或与Java相关的DHCP服务配置。标签"dhcp-j dhcp-j dhcp-j"进一步强调了这个主题的核心是DHCP与Java的结合。 ...
"DHCP Server探测工具"就是为了解决这一问题而设计的。 该工具的主要功能是帮助网络管理员快速定位网络中的DHCP服务器。通过扫描网络段,它可以检测到运行DHCP服务的设备,并提供相关的服务器信息。这包括服务器的...
在本项目"DHCPServer.zip"中,包含了一个使用C#语言编写的DHCP服务器源码,名为"SmallDHCPServer_CodeProject"。这个项目旨在实现网络监控和配置的功能,对于学习和理解DHCP工作原理以及C#编程在网络安全领域的应用...
DHCP Server V7.0是一款高级的DHCP服务器软件,旨在简化网络管理员的工作,提供高效且可靠的IP地址管理服务。下面将详细讨论DHCP服务器的基本原理、功能以及V7.0版本可能包含的特性。 1. DHCP基本原理: DHCP基于...
A DHCP Server is a protocol server for TCP/IP networks that supplies IP addresses to other computers. DHCP Server for Windows also supports TFTP, DNS and HTTP protocol which makes a nice package for ...
在标题提到的"DHCPServer DHCP服务器"中,我们聚焦于如何在没有路由器或交换机的情况下,利用DHCPServer来为多台设备提供网络配置服务。 首先,了解DHCP的工作原理至关重要。当一台新设备连接到网络时,它会发送一...
4. **网络通信**:服务器需使用UDP(User Datagram Protocol)在端口67(服务器端)和68(客户端端)之间进行通信,因为DHCP是基于UDP的无连接协议。 5. **配置文件解析**:Open DHCP Server可能允许用户通过配置...
标签: DHCP Server for Windows A DHCP Server is a protocol server for TCP/IP networks that supplies IP addresses to other computers. DHCP Server for Windows also supports TFTP, DNS and ...
open DHCP server 软件在有网或者无网情况下可以将平板、电脑、手机。通过交换机弄成统一局域网
在标题和描述中提到的"dhcp server dhcp client",指的是DHCP服务器与DHCP客户端之间的交互过程。 DHCP服务器是网络中的关键组件,它负责管理IP地址池,当一个新设备请求网络连接时,服务器会为其提供所需的网络...
Windows Server:Dhcp管理工具