Code Monkey home page Code Monkey logo

dhcppython's People

Contributors

lvfrazao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

dhcppython's Issues

sample code in readme.md could not run in my computer

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

Option 50 (Address Request) in DHCP Request

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,
        )

Sockets are not closed when DHCPClient object is deleted

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()

struct.error: unpack requires a buffer of 7 bytes

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,

Trying to sort through code ...

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?

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.