Use the Development Board to create a DHCP server in an Embedded System
1. recompile the kernel and add support for DHCP.
2. Check whether busybox contains DHCP applications. If not, recompile busybox or directly add an independent application.
3. The configuration file of the DHCP server in the etc directory. udhcpd. conf needs to be configured, including the allocated IP address and DNS.
My file system directly has DHCP software, namely udhcpd and udhcpc. The former is the server side, and the latter is used as the client side, so that you can directly configure udhcpd. conf. Configure udhcpd. conf according to examples in busybox, and then run
Udhcpd-fs/etc/udhcpd. conf
The DHCP server can be started. Of course, there is also a small problem in the middle. At this time, the udhcpd prompts that no udhcpd is called during execution. leases files. You only need to create the following files in the corresponding folder.
If everything goes well, we can see that the Development Board allocates IP addresses, GW, and DNS to the PC!
///////////////////////////////////
* ************* DHCP
The udhcpc udhcpd tool used on the Development Board can be compiled directly from busybox.
The DHCP tool used on the PC can use sudo apt-Get install udhcpc
Sudo apt-Get install udhcpd
Use a PC as the udhcpd server, and the Development Board obtains the IP address from the PC (the opposite is the same)
On PC: sudo VI/etc/udhcpd. conf
Udhcpd. conf in busybox has a prototype: busybox-1.14.2/examples/udhcp/udhcpd. conf
Modify start/end/option DNS
Start 192.168.4.200 # default: 192.168.0.20
End 192.168.4.254 # default: 192.168.0.254
// Ip address allocation Interval
# Examles
Opt DNS 192.168.1.2 192.168.1.10
Option subnet limit 255.0
Opt router 192.168.1.2
Opt wins 192.168.1.10
Option DNS 192.168.4.127 # appened to above DNS servers for a total of 3
Option Domain Local
Option lease 864000
// Option DNS 192.168.4.127 IP address of the server machine
Connect the PC to the Development Board with a network cable.
After that, sudo udhcpd
Run udhcpc on the Development Board to obtain IP addresses from the PC.
/////////////////////////////////
DHCP is inevitable because network communication is required. The ideal network communication method is supported in the following three ways:
1. Access an existing network. This requires that the client can be used as a DHCP client.
2. dynamically allocate IP addresses as DHCP servers.
3. Specify a fixed IP Address
There are no more than 3rd cases. Let's briefly describe the first two cases.
Procedure:
(1) Configure DHCP in the network entry of the kernel;
(2) Put
Udhcp server (udhcpd)
Udhcp client (udhcpc) is selected.Udhcpd is the terminal device used as the DHCP server.
A udhcpc is a client that uses a terminal device as a DHCP client.
An example of DHCP in busybox is provided,
[Zhh @ localhost busybox-1.14.1] $ ls./examples/udhcp/
Sample. Bound sample. deconfig sample. Nak sample. Renew sample. script simple. Script udhcpd. conf
For example, when using udhcpc
You can use simple. script directly without changing the name. The default directory file in busybox is/usr/share/udhcpc/Default. script.
You can view help
# Udhcpc -- Help
Busybox v1.14.1 (10:35:16 Cst) Multi-call binary
Usage: udhcpc [-cfbnqtvo] [-c cid] [-V vcls] [-H hostname] [-I interface]
[-P pidfile] [-r IP] [-s script] [-o dhcp-Option]... [-P n]
-V classid vendor Class Identifier
-I interface to use (default: eth0)
-H,-H hostname client hostname
-C clientid Client Identifier
-C suppress Default Client Identifier
-P file create pidfile
-R ip Address to request
-S file run file at DHCP events (default/usr/share/udhcpc/Default. Script)
-T n send up to N request packets
-T n try to get a lease for n seconds (default 3)
-A n wait n seconds (default 20) after failure
-O opt request DHCP option OPT (cumulative)
-O do not request any options (unless-O is also given)
-F run in foreground
-B background if lease is not immediately obtained
-S log to syslog too
-N exit with failure if lease is not immediately obtained
-Q quit after obtaining lease
-R release IP on quit
-P n use port n instead of default 68
-A use arping to validate offered address
#
This makes it easy to understand.
The following describes how to use udhcpd. You can also directly use the udhcpd. conf that comes with busybox as a reference. You will not detail how to set this file. Refer to the relevant documentation. How to Use udhcpd
# Udhcpd -- Help
Busybox v1.14.1 (10:35:16 Cst) Multi-call binary
Usage: udhcpd [-fs] [-P n] [configfile]
DHCP server
-F run in foreground
-S log to syslog too
-P n use port n instead of default 67
The help is clear at a glance.
When I use it directly
# Udhcpd/etc/udhcpd. conf
You can.
Another lease file should be brief. When udhcpd is used, a lease file will be required to be created. Just create one as prompted. What does it do? It is to record the IP address obtained by the client. If there is no lease file, a new IP address will be assigned to the client again. If yes, the original IP address will be used.
//////////////////////////////////
Use DHCP in an embedded Linux system to automatically assign IP addresses to the Development Board
1. Check that DHCP is selected when the Linux kernel compilation is generated.
Networking --->
Networking Support
Networking options --->
PACKET socket // Add. Configure config_packet
[*] IP: DHCP support // Add
[*] Network packet filtering (replaces ipchains) ---> // Add the file. If this option is left blank, configure config_netfilter.
NOTE: If PACKET socket is not selected, the following error occurs when running the udhcpc command: [*] network packet filtering (replaces ipchains) --->:
~ # Udhcpc
Udhcpc (v0.9.9-pre) started
Udhcpc [208]: udhcpc (v0.9.9-pre) started
Fatal: Couldn't listen on socket, address family not supported by Protocol
Udhcpc [208]: Fatal: Couldn't listen on socket, address family not supported by Protocol
2. Compile busybox to make menuconfig and add the following options:
Networking utilities --->
Udhcp server/client --->
[] Udhcp server (udhcpd) // This is not used as the server, so it is not selected. Generate udhcpd command
Udhcp client (udhcpc) // generate udhcpc command
[] Lease display utility (dumpleases)
[] Log udhcp messages to syslog (instead of stdout)
[] Compile udhcp with noisy debugging messages
If busybox does not compile the corresponding options, you can also download the corresponding files from the Internet, use arm-Linux cross-compilation to get udhcpd, udhcpc command copy to usr/sbin.
Note: The udhcp_0.9.8cvs20050303.orig.tar.gz file on the internet is recommended. decompress the file and modify the makefile,
Add cross_compile = arm-Linux-
Comment out 12 rows of combined_binary = 1,
Otherwise, the udhcpc command is not generated.
Note: udhcp of version 0.9.8 does not seem to work either. recompile busybox1.9.2.
3. Use simple in busybox. script,/usr/share/udhcpc/, and rename it to default. script, modify default. resolv_conf = "/etc/resolv. conf "is resolv_conf ="/tmp/resolv. conf"
Note: It seems that the following default. script files cannot be found on the Internet. The above method is better. Because the cramfs file system is used, the etc directory cannot be written. When udhcp. C is run, a message is displayed, indicating that resolv. conf cannot be created.
4. Copy the generated udhcpc to the development board/sbin and run udhcpc on the console. You can quickly obtain the IP address from the DHCP server. However, if there is a problem, the Development Board can obtain the IP address, and you can also see the allocated address in the vro. The problem is that you can use the ping command to ping the Development Board or the Development Board to ping the PC, the Development Board will crash. I tried many methods, checked Route, checked ARP, and modified default. script.
.
It took a whole day to complete the solution.
1. First, I suspected the problem of busybox. OK. On the Development Board, I transplanted version 1.9.2. I found the latest version 1.14.0 on the Internet. For more information about porting, see another article.
2. busybox1.9.2 seems to be faulty. After udhcpc, you can view IP information through ifconfig to see that the broadcast address bcast is faulty. It is always 255.255.255.255. The correct value should be 192.168.1.255 (network segment: 192.168.1 ), if you use busybox1.14.0, the problem is that ping is still unavailable.
3. Disable the NIC after the udhcpc is detected by accident, and then restart the NIC to use the command
Ifconfig eth0 down
Ifconfig eht0 up
Then it will work normally. The problem is that you cannot ping the local machine. You can set the loopback address.
Ifconfig lo 127.0.0.1