use DHCP in Linux system
DHCP is the Dynamic Host Configuration Protocol,which allows a networked machine to obtain its IP address and other informationautomatically from a DHCP server.
There are two methods through which a Linux systemcan obtain an IP address using DHCP:
1. Kernel level configuration.
2. User level configuration.
Kernel level auto-configuration:
To use kernel level auto configurtion select belowoptions in the Linux kernel configuration.
-> Networking support (NET [=y])
-> Networking options
-> TCP/IP networking (INET [=y])
[*] IP: kernel level autoconfiguration
[*] IP: DHCP support
Advantage:
In case of your root file system in NFS and wanted to have a IP dynamicaly configirued though DHCP, kernel level DHCP will come to rescue you. Using kernel level DHCP you will have have a valid IP address by the time the Linuxkernel have completed the booting
and can immediately mount an NFS root filesystem.
Disadvantage:
1. Can only configure basic network parameters, which do not include items suchas DNS servers.
User level auto-configuration
I will use DHCP client applet that comes Busyboxnamed udhcpc
As per Busybox:
udhcpc is a DHCP client geared primarily toward embedded systems, while striving to be fully functional and RFC compliant. Theudhcp client negotiates a lease with the DHCP server and runs a script when alease is obtained or lost.
User level auto-configuration is much more flexible,but has the does require that there is a root file system available when the system has finished booting which does not depend on the network being configured. This file system is usually on a Flash, SD/MMC, etc.,
In order to use udhcpc, the Linux kernel must beconfigured with packet socket support (CONFIG_PACKET):
-> Networking support (NET [=y])
-> Networking options
< *> Packet socket
copy the sample scripts to /etc/udhcpc/ of yourdevice.
edit the file /etc/udhcpc/sample.script changed thepath where the scripts are located.
1 |
exec /etc/udhcpc/sample.$1 |
Create a dummy script named sample.fail
1 |
$ touch /etc/udhcpc/sample.fail |
Now start the DHCP client.
|
$ udhcpc -i wlan0 -s /etc/udhcpc/sample.script Configuring IP using DHCP udhcpc (v1.18.3) started Sending discover... Sending select for 192.168.7.10... Lease of 192.168.7.10 obtained, lease time 604800 deleting routers route: ioctl 0x890c failed: No such process adding dns 192.168.7.1 Initializing completed |
Run dhcpc client on br0 interface.
(choose MTK4120 as examples here)
Step1:config packet socket support by make menuconfigcommand
In order to use udhcpc, the Linux kernel must beconfigured with packet socket support (CONFIG_PACKET):
-> Networking support (NET [=y])
-> Networking options
< *> Packet socket
Step2:copy dhcpcscript to corresponding location
This script should configure the system ip, gateway, and resolv.conf etc.The busybox provides sample scripts, which is placed in the examples/udhcp/directory.deteil path see below screenshot:
We can found it is underthe path of SDK4120/source/user/busybox/examples/udhcp/sample.script
Here give a explain is needed,how to use this scriptand where is the correspond location that simple.script shout copy to?Actuallyin SDK4120/source/user/busybox/examples/udhcp/sample.script have specify thescript location,let we open and have a look it :
$Vi SDK4120/source/user/busybox/examples/udhcp/sample.script
This mean the path where the scripts are located :
/user/share/udhcpc/
Also there are existing some otherfile can find the location
SDK4120/source/user/busybox/networking/udhcp/udhcpc.cThere areexsisting one line like this:
client_config.script =DEFAULT_SCRIPT;
SDK4120/source/user/busybox/networking/udhcp/common.c
#defineDEFAULT_SCRIPT CONFIG_DHCPC_DEFAULT_SCRIPT
SDK4120/source/user/busybox/include/config/dhcpc/defaut/script.h
#defineCONFIG_DHCPC_DEFAULT_SCRIPT "/usr/share/udhcpc/default.script"
Here,everything are clearnow,simpe.script shout copy to /usr/share/udhcpc/
And simple.script should renameto default.script,of course ,we can changed/usr/share/udhcpc/default.script to simple.scriptalso ,it own same effect.
Step3:DHCPC Mapping to br0
change SDK4120/source/user/busybox/networking/udhcp/udhcpc.c
client_config.interface= "eth0";
to
client_config.interface= "br0";
Step4: start the DHCP client when device connected wireless
Changed SDK4120/source/user/goahead/src/goahead.c (can add to any other daemon progress also)
/*tocheck wether exist dhcp client process,if not then startup */
system("ps aux | grep udhcpc | grep -v grep >/dev/null && { echo 'ok'>/dev/null ; break; } || { sleep 1 ; udhcpc; } ");