The Dynamic Host Configuration Protocol (DHCP) allows for the automatic network configuration of hosts. Automatic configuration usually involves assigning IP addresses but can include other configuration parameters, as we saw in Chapter 9. On a network that uses DHCP, there are two sorts of entities: clients that request a configuration and servers that provide the clients with functional configurations.
An embedded Linux system can easily be used as a DHCP server. In my example system, for instance, the SYSM module can provide dynamic configurations to the UI modules. Conversely, an embedded Linux system may need to obtain its own configuration from a DHCP server. My UI modules, for example, may obtain their configurations from the SYSM module.
The standard DHCP package used in most Linux distributions is the one distributed by the Internet Software Consortium (ISC). Although the package may seem to be a good candidate for use in embedded Linux systems because of its widespread use and the fact that it includes both the client and the server, its Makefiles and configuration scripts do not allow cross-compilation in any way.
There is, nevertheless, another open source package that provides both a DHCP server and a DHCP client, and that can be used in an embedded Linux system: udhcp. The udhcp project is maintained as part of the BusyBox project, and its web site is located at http://udhcp.busybox.net/. The package is available from that web site and is distributed under the terms of the GPL. udhcp depends only on the C library and can be compiled both with glibc and uClibc.
Begin by downloading and extracting the udhcp package in your ${PRJROOT}/sysapps directory. For my SYSM module, for example, I used udhcp 0.9.8. Move to the package's directory for the rest of the operations:
$ cd ${PRJROOT}/sysapps/udhcp-0.9.8
There is no configuration needed with this package. Hence, compiling the package is all that needs to be done:
$ make CROSS_COMPILE=arm-uclibc-
Here, too, the compilation time is short. If you want to build statically, add LDFLAGS="-static" to the make command line. Also set CROSS_COMPILE to arm-linux- if you would prefer to build with glibc instead of uClibc. When linked against glibc and stripped,[7] the server and the client are around 16 KB in size when linked dynamically. The client is 375 KB in size and the server 450 KB in size when linked statically against glibc and stripped. Note that udhcp uses glibc's NSS and will require Makefile modifications in order to pass the link options at the end of the compile lines, not in the middle as it does by default. You will also need to set LDFLAGS to values similar as the ones we used earlier to build Boa and thttpd statically against glibc. When linked against uClibc and stripped, the server and the client are around 15 KB in size when linked dynamically and 40 KB in size when linked statically.
[7] The udhcp Makefile automatically strips the binaries once they are built.
If you are using the server in your system, copy it to your target's /usr/sbin directory:
$ cp udhcpd ${PRJROOT}/rootfs/usr/sbin
If you are using the client, copy it to your target's /sbin directory:
$ cp udhcpc ${PRJROOT}/rootfs/sbin
Both server and client need configuration files and runtime files to store information regarding lease status.
For the server, create a /var/lib/misc directory and a lease file, and copy the sample configuration file to your target's root filesystem:
$ mkdir -p ${PRJROOT}/rootfs/var/lib/misc $ touch ${PRJROOT}/rootfs/var/lib/misc/udhcpd.leases $ cp samples/udhcpd.conf ${PRJROOT}/rootfs/etc
If you forget to create the lease file, the server will refuse to start.
For the client, create a /etc/udhcpc directory and a /usr/share/udhcpc directory, and copy one of the sample configuration files to /usr/share/udhcpc/default.script:
$ mkdir -p ${PRJROOT}/rootfs/etc/udhcpc $ mkdir -p ${PRJROOT}/rootfs/usr/share/udhcpc $ cp samples/sample.renew \ > ${PRJROOT}/rootfs/usr/share/udhcpc/default.script
Also, edit your target's /etc/inittab file to start the daemon you need. For instance, here is the line for the DHCP server I use in my SYSM module:
::respawn:/usr/sbin/udhcpd
For a complete discussion on the configuration and use of udhcpd and udhcpc, read the manpages included with the package and look at the project's web site.