Dont understand the main in the code of dhclient.c

Ted Lemon Ted.Lemon at nominum.com
Mon Jul 7 22:48:42 UTC 2008


> I have read the file dhclient.c, but i haven't found out where
> and how the main calls the majors functions for making, sending
> the messages DHCP like make_discover, send_discover,... or for
> treating the messages received from server like dhcpoffer,..

The thing you need to understand is that the code is not linear - it's event-driven.   So the DHCP client sets up all the state that's required to run, and then it sets up a timer that sends the first DHCPDISCOVER or DHCPREQUEST.   When the timer expires, typically in about a second, the timer expiry function calls the appropriate state_* function to send off a request.   When a packet comes in, the appropriate packet handling function is called.   If no packet comes in, another timer expires and triggers a retransmission, or else triggers a call to a different state_* function.

So you have to read the code with that idea in mind, not with the idea that you're following a single thread step by step from start to end.

One way to see how this happens is to run gdb on the DHCP client and watch what happens.   You will find that after all the main functions run, you wind up in select().   When select() returns, that means an event has happened.   If you continue to trace with gdb, you will see which event happened, and how it was processed.



More information about the dhcp-users mailing list