DHCPD is a DHCP server in Linux ,:
Https://www.isc.org/software/dhcp
After the download is complete
Tar-zxvf dhcp-4.2.1-P1.tar.gz
./Configure
Make
Make install
Edit the/etc/DHCPD. conf file
Ddns-Update-style none;
Subnet 192.168.66.0 netmask 255.255.255.0 {
Option routers 192.168.66.1;
Option subnet-mask limit 255.0;
Option domain-name "wayos ";
Option domain-name-servers 61.139.2.69;
Range 192.168.66.2 192.168.66.254;
Default-lease-time 600;
Max-lease-time 7200;
}
192.168.66.0 is the network segment of the host
If the program prompts no "/var/DB/DHCPD. leases" file, touch
Main
{
/* Set up the ISC and DNS Library managers */
/* Set up the client Classification System .*/
/* Initialize the OMAPI system .*/
/* Set up the OMAPI wrappers for common objects .*/
/* Set up the OMAPI wrappers for various Server database internal
Objects .*/
/* Initially, log errors to stderr as well as to syslogd .*/
/* Parse command line parameters */
/* Get user and group info if those options were given */
/* Get around the ISC declaration of Group */
/* Default to the DHCP/Bootp port .*/
/* Set up the initial DHCP option universe .*/
/* Add the ddns update style enumeration prior to parsing .*/
/* Set up various hooks .*/
Dhcp_interface_setup_hook = dhcpd_interface_setup_hook;
Bootp_packet_handler = do_packet;
/* Set up the standard name service Updater routine .*/
/* Initialize ICMP support ...*/
/* Read the DHCPD. conf file ...*/
/* Start up the database ...*/
/* Discover all the network interfaces and initialize them .*/
/* First part of becoming a daemon ...*/
/* Become session leader and get PID ...*/
/* Receive packets and dispatch them ...*/
Dispatch ();
}
Key code:
The discover. c file contains the bootp_packet_handler function pointer declaration.
Void (* bootp_packet_handler) proto (struct interface_info *,
Struct dhcp_packet *, unsigned,
Unsigned int,
Struct iaddr, struct hardware *));
In the DHCPD. c file, the main function assigns a value to the bootp_packet_handler function pointer, and then calls the discover_interfaces function:
Bootp_packet_handler = do_packet;
Discover_interfaces (discover_server );
The discover. c file has the definition of the discover_interfaces function. The key is to call the got_one function:
Status = omapi_register_io_object (omapi_object_t *) TMP,
If_readsocket,
0, got_one, 0, 0 );
The discover. c file is defined by the got_one function. The key code is to call the receive_packet function and the bootp_packet_handler function pointer:
Receive_packet (IP, U. packbuf, sizeof U, & from, & hfrom)
If (bootp_packet_handler ){
Ifrom. Len = 4;
Memcpy (ifrom. iabuf, & from. sin_addr, ifrom. Len );
(* Bootp_packet_handler) (IP, & U. packet, (unsigned) result,
From. sin_port, ifrom, & hfrom );
}
The socket. c file contains the receive_packet function definition.
Result = recvfrom (interface-> rfdesc, (char *) BUF, Len, 0,
(Struct sockaddr *) from, & FLEN );
The options. c file contains the do_packet Function Definition:
Packet_allocate (& decoded_packet, MDL );
Decoded_packet-> raw = packet;
Decoded_packet-> packet_length = Len;
Decoded_packet-> client_port = from_port;
Decoded_packet-> client_addr = from;
Interface_reference (& decoded_packet-> interface, interface, MDL );
Decoded_packet-> haddr = hfrom;
/* If there's an option buffer, try to parse it .*/
If (decoded_packet-> packet_length> = dhcp_fixed_non_udp + 4 ){
If (! Parse_options (decoded_packet )){
If (decoded_packet-> options)
Option_state_dereference
(& Decoded_packet-> options, MDL );
Packet_dereference (& decoded_packet, MDL );
Return;
}
If (decoded_packet-> options_valid &&
(OP = lookup_option (& dhcp_universe,
Decoded_packet-> options,
Dho_dhcp_message_type ))){
Struct data_string DP;
Memset (& DP, 0, sizeof DP );
Evaluate_option_cache (& DP, decoded_packet,
(Struct lease *) 0,
(Struct client_state *) 0,
Decoded_packet-> options,
(Struct option_state *) 0,
(Struct binding_scope **) 0,
Op, MDL );
If (DP. Len> 0)
Decoded_packet-> packet_type = DP. Data [0];
Else
Decoded_packet-> packet_type = 0;
Data_string_forget (& DP, MDL );
}
}
If (decoded_packet-> packet_type)
DHCP (decoded_packet );
Else
BOOTP (decoded_packet );
The DHCP. c file is defined as a DHCP function. The main task is to process packages corresponding to packet-> packet_type:
Switch (packet-> packet_type ){
Case dhcpdiscover:
Dhcpdiscover (packet, ms_nulltp );
Break;
Case dhcprequest:
Dhcprequest (packet, ms_nulltp, lease );
Break;
Case dhcprelease:
Dhcprelease (packet, ms_nulltp );
Break;
Case DHCPDECLINE:
DHCPDECLINE (packet, ms_nulltp );
Break;
Case dhcpinform:
Dhcpinform (packet, ms_nulltp );
Break;
Case dhcpleasequery:
Dhcpleasequery (packet, ms_nulltp );
Break;
Case dhcpack:
Case dhcpoffer:
Case dhcpnak:
Case dhcpleaseunassigned:
Case dhcpleaseunknown:
Case dhcpleaseactive:
Break;
Default:
Errmsg = "unknown packet type ";
Goto bad_packet;
}
The DHCP. c file is defined by the dhcprequest function. The execution process of this function is as follows:
/* Find the lease that matches the Address requested by
Client .*/
/* If it's renewing, we are the only server to hear it, so
* We have to serve it .*/
/* If we do know where it came from and it asked for
Address that is not on that shared network, Nak it .*/
/* If the address the client asked for is ours, but it wasn' t
Available for the client, Nak it .*/
/* Otherwise, send the lease to the client if we found one .*/