lvfrazao / dhcppython Goto Github PK
View Code? Open in Web Editor NEWPure Python library for parsing and manipulating DHCP packets
License: Apache License 2.0
Pure Python library for parsing and manipulating DHCP packets
License: Apache License 2.0
The various sockets are not closed when the dhcppython.client.DHCPClient
is deleted, which leads to future DCHPClient
objects not being able to obtain leases since the replies are delivered to the older sockets instead. I added a __del__
method to the class that closes them and now I can create new client objects:
def __del__(self):
for sock in self.listening_sockets:
sock.close()
for sock in self.writing_sockets:
sock.close()
for sock in self.except_sockets:
sock.close()
Hi,
Currently, I am using dhcppython, but I encounter an error when trying to run packet.view_packet()
on an incoming packet (for this case, it's from dhcpcd dhcp client).
Traceback (most recent call last):
File "/usr/local/lib/python3.10/asyncio/events.py", line 80, in _run
self._context.run(self._callback, *self._args)
File "/usr/local/lib/python3.10/asyncio/selector_events.py", line 1026, in _read_ready
self._protocol.datagram_received(data, addr)
File "/app/tmp.py", line 25, in datagram_received
raise e
File "/app/tmp.py", line 21, in datagram_received
logging.debug(packet.view_packet())
File "/usr/local/lib/python3.10/site-packages/dhcppython/packet.py", line 261, in view_packet
f"{opt.key} {opt.value[opt.key]} ({opt.length})", opt.length
File "/usr/local/lib/python3.10/site-packages/dhcppython/options.py", line 1617, in value
hwtype, hwaddr = struct.unpack(">B6s", self.data)
struct.error: unpack requires a buffer of 7 bytes
To replicate this issue, run this small DHCP listener, and run dhcpcd
from another PC / VM
import asyncio
import logging
import signal
import socket
import sys
from asyncio import Future, transports
from dhcppython.packet import DHCPPacket
class DHCPProtocol(asyncio.DatagramProtocol):
def connection_made(self, transport: transports.DatagramTransport) -> None:
self.transport = transport
def datagram_received(self, data: bytes, addr: str) -> None:
try:
packet = DHCPPacket.from_bytes(bytes(data))
logging.debug(f'received dhcp packet:')
logging.debug(packet)
logging.debug(packet.view_packet())
except Exception as e:
logging.error(e)
raise e
class DHCPServer:
def __init__(self, interface: bytes) -> Future:
self.loop = asyncio.get_running_loop()
self.interface = interface
self.awaiter = asyncio.Future()
async def start(self) -> None:
self.transport, protocol = await self.loop.create_datagram_endpoint(
lambda: DHCPProtocol(),
local_addr=('0.0.0.0', 67),
allow_broadcast=True)
self.transport.get_extra_info('socket').setsockopt(
socket.SOL_SOCKET, 25, self.interface)
logging.info('Starting DHCP service')
return await self.awaiter
async def stop(self) -> None:
logging.info('Stopping DHCP service')
self.transport.close()
self.awaiter.set_result(None)
async def main() -> None:
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
# change the interface name according to your network interface name
interface = b'eth0'
server = DHCPServer(interface=interface)
loop = asyncio.get_running_loop()
for sig in (signal.SIGINT, signal.SIGTERM):
loop.add_signal_handler(sig,
lambda: asyncio.create_task(server.stop()))
await server.start()
if __name__ == '__main__':
asyncio.run(main())
It seems the error is on options.ClientIdentifier
class.
Any help would be appreciated.
Thanks,
It seems that dnsmasq
will not reply correctly to a DCHP Request packet that does not have option 50 (Address Request) included. According to (this DHCP overview)[https://learn.microsoft.com/en-us/windows-server/troubleshoot/dynamic-host-configuration-protocol-basics], filling in only the Client IP Address is RFC compliant, although on my network dnsmasq
doesn't seem to process the DHCPREQUEST
packets that arrive if the source IP is 0.0.0.0 and only the ciaddr
field is filled in.
Changing the request packet in get_lease()
allows my devices to get IP addresses:
# R
# dnsmasq wants option field DHCP Requested Address filled in
option_list = option_list if option_list else options.OptionList()
option_list.insert(0, options.options.short_value_to_object(50, offer.yiaddr.packed))
request = packet.DHCPPacket.Request(
mac_addr,
int(default_timer() - start),
tx_id,
use_broadcast=broadcast,
option_list=option_list,
#client_ip=offer.yiaddr,
relay=relay,
)
Hello!
I'm trying to use the client to communicate with isc-dhcpd server. While using, I found that the client tries to listen to port 67 in the initialization phase:
dhcppython/dhcppython/client.py
Line 90 in c442c3f
However, isc-dhcpd uses port 67 to serve. could you please explain why the client listens to port 67?
Is there DHCPv6 support?
I am trying to see what it takes to define an option (say 161 "FTP_Server" which is a string). I am not seeing it in the code. Any thoughts?
I copy sample code in readme.md which function at DHCP client, my env is centos7 and python3.8.2,
my code is:
import dhcppython
client = dhcppython.client.DHCPClient(interface="wlp3s0")
lease = client.get_lease(mac_addr="fa:b3:9e:89:86:32", broadcast=True, relay=None, server="255.255.255.255", options_list=None)
why it comes :
Traceback (most recent call last):
File "dhcp_clients.py", line 8, in
client = dhcppython.client.DHCPClient(interface="wlp3s0") # wlp3s0
File "/usr/local/Python3.8/lib/python3.8/site-packages/dhcppython/client.py", line 96, in init
self.writing_sockets = self.get_writing_sockets()
File "/usr/local/Python3.8/lib/python3.8/site-packages/dhcppython/client.py", line 329, in get_writing_sockets
client_sock = self.get_socket(host, port)
File "/usr/local/Python3.8/lib/python3.8/site-packages/dhcppython/client.py", line 320, in get_socket
sock.bind((host, port))
OSError: [Errno 98] Address already in use
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.