Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
(ARM).Porting TCP-IP programmer's guide.Ver 1.6.pdf
Скачиваний:
43
Добавлен:
23.08.2013
Размер:
2.64 Mб
Скачать

DHCP Client Functions

4.1DHCP client functions

All DHCP client functions are found in inet\dhcpclnt.c. Many of them also require inet\dhcputil.c to be added to your project. The DHCP client functions are:

dhc_init() on page 4-2

dhc_discover() on page 4-3

dhc_set_callback() on page 4-5

dhc_halt() on page 4-5

dhc_second() on page 4-6.

4.1.1dhc_init()

This function initializes the DHCP client. It must be called before attempting to use DHCP to configure any network interfaces. This function attempts to open a UDP connection to listen for incoming replies. After DHCP has been initialized, the application must call dhc_second() one time every second.

Syntax

int dhcinit(void)

Return value

Returns one of the following:

0

If successful.

ENP_ code If not successful (see ENP_ error codes on page A-2).

4-2

Copyright © 1998-2001 ARM Limited. All rights reserved.

ARM DUI 0144B

DHCP Client Functions

4.1.2dhc_discover()

This function begins the process of configuring a network interface using DHCP. The process may take several seconds, and the DHCP client will keep retrying if the service does not respond. You must abandon the attempt using dhc_halt() (see dhc_halt() on page 4-5) if the interface is not configured within a reasonable period (such as 30 seconds).

Syntax

int dhc_discover(int net)

where:

net

Is the index of the network interface to be configured.

Return value

Returns one of the following:

0

If successful.

ENP_ code If not successful (see ENP_ error codes on page A-2).

Usage

A simple way to check for completion is to set the IP address for the interface to 0.0.0.0 before starting, and waiting until it becomes nonzero, as illustrated in Example 4-1.

Example 4-1

#ifdef DHCP_CLIENT dhc_init();

for(i = 0; i < MAXNETS; i++)

{

dprintf("Using DHCP to obtain IP address information for interface %d\n", i );

nets[i]->n_ipaddr = 0L; dhc_discover(i);

}

do {

tk_yield(); e = 0;

for(i = 0; i < MAXNETS; i++)

{

ARM DUI 0144B

Copyright © 1998-2001 ARM Limited. All rights reserved.

4-3

DHCP Client Functions

if ( nets[i]->n_ipaddr ) e++;

}

} while ( e != MAXNETS );

dprintf("All interfaces are now configured.\n" ); #endif

As an alternative to polling the IP address, you can install a callback to signal completion. See dhc_set_callback() on page 4-5 for details.

4-4

Copyright © 1998-2001 ARM Limited. All rights reserved.

ARM DUI 0144B

DHCP Client Functions

4.1.3dhc_set_callback()

This function installs a callback to be used to signal that the configuration of an interface is complete. This allows a multi-threaded RTOS to use DHCP without the overhead of polling loops.

Syntax

void dhc_set_callback(int net, int(*routine)(int, int))

where:

net

Is the index of the network interface of interest.

routine Is the function to call when configuration is complete. This receives the index of the interface and the current DHCP state of the interface.

Return value

None.

Usage

This function is used to inform the application that the network interface is now available for use. It typically does this by setting a volatile flag, or by signaling the RTOS thread(s) waiting for the interface. The callback routine is called several times per interface, each time with an updated state. The interface can be used when the state is DHCS_BOUND.

4.1.4dhc_halt()

This function stops all DHCP activity on a network interface.

Syntax

void dhc_halt(int net)

where:

net

Is the index of the network interface on which to abandon DHCP.

Return value

None.

ARM DUI 0144B

Copyright © 1998-2001 ARM Limited. All rights reserved.

4-5