DHCP Client Service - dhcp
#include <tcpinit.h>
#include <sys/socket.h>
#include < netinet/in.h>
#include < arpa/inet.h >
The tcp and udp server or tcpd provides IPV4 TCP, and IP protocol support with low level drivers for specific chip support packages. It is a fast and easy to use TCP solution which requires a simple setup structure at creation time to get it up and running. It offers static IP addresses in IPV4 format unless provisioned with the DHCP client service for IP address retrieval.
The dhcp client service uses the standard dhcp protocol to lease an IP address from a dhcp server. Typically, these servers are included in routers and are configured for IPV4 subnets to provide local network IP addresses. Both the client and the server are easily setup.
More precisely, the dhcp client service is actually a library that is called and does not run as a separate thread. This library uses the calling thread's context and sets up the etherinit structure for use with tcpd. It implements the standard dhcp client protocol in the library and returns control to the calling thread when complete.
This is an example of the various setup parameters for the tcpd - in this case using SPI communication with a 28J60.
#define GW_IP_ADDR 0xf010a8c0 //"192.168.16.240"
#define ENC_ADDR 0x100000
#define ENC_INT_VECT 0 //Interrupt 0
#define ENC_INT_LEVEL 3
static char tcpbuf[1024*8];
struct tcpinit etherinit = {
sizeof(struct tcpinit), /* message size */
GW_IP_ADDR, /* default gateway */
ENC_ADDR, /* address of enc28j60 chip */
ENC_INT_VECT, /* interrupt vector */
0xc710a8c0, /* internet address *///"192.168.16.199"
{0x00,0x04,0xa3,0x00,0x4a,0x19}, /* ethernet address */
{0,0}, /* padding */
ENC_INT_LEVEL, /* interrupt level */
0, /* unit number */
tcpbuf, /* buffer pointer */
sizeof(tcpbuf), /* size of buffer */
{'l','e',0}, /* interface name */
};
This partial example shows the creation and registration of the tcpd using tcp_shell followed with the lease of an IP address and discovery of the default gateway.
if(pthread_create(&pid, &attr, &tcp_shell, 0)!=0)
{
xprintf("pthread_create = %d\n", errno);
pthread_exit(0);
}
if(dir_register("/dev/tcpd", pid, TYPE_SERVER)==0)
{
xprintf("dir_register = %d\n", errno);
pthread_exit(0);
}
// Try to get our IP and default GW
if (dhcp_get_params (ðerinit) == -1)
{
xprintf("DHCP could not get params! errno = %d\n", errno);
while (1) sleep(1);
}
xprintf("Get DHCP params:\n");
xprintf(" Client IP: %d.%d.%d.%d\n",
((unsigned char *)ðerinit.tcp_IP_address)[0],
((unsigned char *)ðerinit.tcp_IP_address)[1],
((unsigned char *)ðerinit.tcp_IP_address)[2],
((unsigned char *)ðerinit.tcp_IP_address)[3]);
xprintf(" Default GW IP: %d.%d.%d.%d\n",
((unsigned char *)ðerinit.tcp_default_gateway)[0],
((unsigned char *)ðerinit.tcp_default_gateway)[1],
((unsigned char *)ðerinit.tcp_default_gateway)[2],
((unsigned char *)ðerinit.tcp_default_gateway)[3]);
...
There is a demo available for the Unison and DSPnano dhcp client server which is found in installdir/demos.