/*==========================================================================
|
/*==========================================================================
|
//
|
//
|
// dhcp_support.c
|
// dhcp_support.c
|
//
|
//
|
// Support code == friendly API for DHCP client
|
// Support code == friendly API for DHCP client
|
//
|
//
|
//==========================================================================
|
//==========================================================================
|
//####ECOSGPLCOPYRIGHTBEGIN####
|
//####ECOSGPLCOPYRIGHTBEGIN####
|
// -------------------------------------------
|
// -------------------------------------------
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
//
|
//
|
// eCos is free software; you can redistribute it and/or modify it under
|
// eCos is free software; you can redistribute it and/or modify it under
|
// the terms of the GNU General Public License as published by the Free
|
// the terms of the GNU General Public License as published by the Free
|
// Software Foundation; either version 2 or (at your option) any later version.
|
// Software Foundation; either version 2 or (at your option) any later version.
|
//
|
//
|
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
|
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
// for more details.
|
// for more details.
|
//
|
//
|
// You should have received a copy of the GNU General Public License along
|
// You should have received a copy of the GNU General Public License along
|
// with eCos; if not, write to the Free Software Foundation, Inc.,
|
// with eCos; if not, write to the Free Software Foundation, Inc.,
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
//
|
//
|
// As a special exception, if other files instantiate templates or use macros
|
// As a special exception, if other files instantiate templates or use macros
|
// or inline functions from this file, or you compile this file and link it
|
// or inline functions from this file, or you compile this file and link it
|
// with other works to produce a work based on this file, this file does not
|
// with other works to produce a work based on this file, this file does not
|
// by itself cause the resulting work to be covered by the GNU General Public
|
// by itself cause the resulting work to be covered by the GNU General Public
|
// License. However the source code for this file must still be made available
|
// License. However the source code for this file must still be made available
|
// in accordance with section (3) of the GNU General Public License.
|
// in accordance with section (3) of the GNU General Public License.
|
//
|
//
|
// This exception does not invalidate any other reasons why a work based on
|
// This exception does not invalidate any other reasons why a work based on
|
// this file might be covered by the GNU General Public License.
|
// this file might be covered by the GNU General Public License.
|
//
|
//
|
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
|
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
|
// at http://sources.redhat.com/ecos/ecos-license/
|
// at http://sources.redhat.com/ecos/ecos-license/
|
// -------------------------------------------
|
// -------------------------------------------
|
//####ECOSGPLCOPYRIGHTEND####
|
//####ECOSGPLCOPYRIGHTEND####
|
//==========================================================================
|
//==========================================================================
|
//#####DESCRIPTIONBEGIN####
|
//#####DESCRIPTIONBEGIN####
|
//
|
//
|
// Author(s): hmt
|
// Author(s): hmt
|
// Contributors: gthomas
|
// Contributors: gthomas
|
// Date: 2000-07-01
|
// Date: 2000-07-01
|
// Purpose: DHCP support
|
// Purpose: DHCP support
|
// Description:
|
// Description:
|
//
|
//
|
//####DESCRIPTIONEND####
|
//####DESCRIPTIONEND####
|
//
|
//
|
//========================================================================*/
|
//========================================================================*/
|
|
|
#include <pkgconf/system.h>
|
#include <pkgconf/system.h>
|
#include <pkgconf/net.h>
|
#include <pkgconf/net.h>
|
|
|
#ifdef CYGPKG_NET_DHCP
|
#ifdef CYGPKG_NET_DHCP
|
|
|
#include <network.h>
|
#include <network.h>
|
#include <dhcp.h>
|
#include <dhcp.h>
|
|
|
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
#ifdef CYGHWR_NET_DRIVER_ETH0
|
#ifdef CYGHWR_NET_DRIVER_ETH0
|
cyg_uint8 eth0_dhcpstate = 0;
|
cyg_uint8 eth0_dhcpstate = 0;
|
#endif
|
#endif
|
#ifdef CYGHWR_NET_DRIVER_ETH1
|
#ifdef CYGHWR_NET_DRIVER_ETH1
|
cyg_uint8 eth1_dhcpstate = 0;
|
cyg_uint8 eth1_dhcpstate = 0;
|
#endif
|
#endif
|
|
|
cyg_sem_t dhcp_needs_attention;
|
cyg_sem_t dhcp_needs_attention;
|
|
|
#ifdef CYGHWR_NET_DRIVER_ETH0
|
#ifdef CYGHWR_NET_DRIVER_ETH0
|
struct dhcp_lease eth0_lease = { &dhcp_needs_attention, 0,0,0,0,0,0 };
|
struct dhcp_lease eth0_lease = { &dhcp_needs_attention, 0,0,0,0,0,0 };
|
#endif
|
#endif
|
#ifdef CYGHWR_NET_DRIVER_ETH1
|
#ifdef CYGHWR_NET_DRIVER_ETH1
|
struct dhcp_lease eth1_lease = { &dhcp_needs_attention, 0,0,0,0,0,0 };
|
struct dhcp_lease eth1_lease = { &dhcp_needs_attention, 0,0,0,0,0,0 };
|
#endif
|
#endif
|
|
|
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
//
|
//
|
// The point of this module is to deal with all the horrid written out in
|
// The point of this module is to deal with all the horrid written out in
|
// full stuff of having two interfaces; it's ugly but it's also most
|
// full stuff of having two interfaces; it's ugly but it's also most
|
// flexible. The dhcp_prot.c module should do all the work...
|
// flexible. The dhcp_prot.c module should do all the work...
|
//
|
//
|
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
|
|
// return value: 1 => everything OK, no change.
|
// return value: 1 => everything OK, no change.
|
// 0 => close your connections, then call do_dhcp_halt() to halt the
|
// 0 => close your connections, then call do_dhcp_halt() to halt the
|
// interface(s) in question (it knows because the state will be NOTBOUND).
|
// interface(s) in question (it knows because the state will be NOTBOUND).
|
// After that you can return to the start and use
|
// After that you can return to the start and use
|
// init_all_network_interfaces(); as usual, or call do_dhcp_bind() by hand,
|
// init_all_network_interfaces(); as usual, or call do_dhcp_bind() by hand,
|
// or whatever...
|
// or whatever...
|
int dhcp_bind( void )
|
int dhcp_bind( void )
|
{
|
{
|
#ifdef CYGHWR_NET_DRIVER_ETH0
|
#ifdef CYGHWR_NET_DRIVER_ETH0
|
cyg_uint8 old_eth0_dhcpstate = eth0_dhcpstate;
|
cyg_uint8 old_eth0_dhcpstate = eth0_dhcpstate;
|
#endif
|
#endif
|
#ifdef CYGHWR_NET_DRIVER_ETH1
|
#ifdef CYGHWR_NET_DRIVER_ETH1
|
cyg_uint8 old_eth1_dhcpstate = eth1_dhcpstate;
|
cyg_uint8 old_eth1_dhcpstate = eth1_dhcpstate;
|
#endif
|
#endif
|
|
|
// If there are no interfaces at all, init it every time, doesn't
|
// If there are no interfaces at all, init it every time, doesn't
|
// matter. In case we are called from elsewhere...
|
// matter. In case we are called from elsewhere...
|
if ( 1
|
if ( 1
|
#ifdef CYGHWR_NET_DRIVER_ETH0
|
#ifdef CYGHWR_NET_DRIVER_ETH0
|
&& eth0_dhcpstate == 0
|
&& eth0_dhcpstate == 0
|
#endif
|
#endif
|
#ifdef CYGHWR_NET_DRIVER_ETH1
|
#ifdef CYGHWR_NET_DRIVER_ETH1
|
&& eth1_dhcpstate == 0
|
&& eth1_dhcpstate == 0
|
#endif
|
#endif
|
)
|
)
|
cyg_semaphore_init( &dhcp_needs_attention, 0 );
|
cyg_semaphore_init( &dhcp_needs_attention, 0 );
|
|
|
// Run the state machine...
|
// Run the state machine...
|
#ifdef CYGHWR_NET_DRIVER_ETH0
|
#ifdef CYGHWR_NET_DRIVER_ETH0
|
if (eth0_up
|
if (eth0_up
|
&& DHCPSTATE_FAILED != eth0_dhcpstate )
|
&& DHCPSTATE_FAILED != eth0_dhcpstate )
|
eth0_up = do_dhcp(eth0_name, ð0_bootp_data, ð0_dhcpstate, ð0_lease);
|
eth0_up = do_dhcp(eth0_name, ð0_bootp_data, ð0_dhcpstate, ð0_lease);
|
#endif
|
#endif
|
#ifdef CYGHWR_NET_DRIVER_ETH1
|
#ifdef CYGHWR_NET_DRIVER_ETH1
|
if (eth1_up
|
if (eth1_up
|
&& DHCPSTATE_FAILED != eth1_dhcpstate )
|
&& DHCPSTATE_FAILED != eth1_dhcpstate )
|
eth1_up = do_dhcp(eth1_name, ð1_bootp_data, ð1_dhcpstate, ð1_lease);
|
eth1_up = do_dhcp(eth1_name, ð1_bootp_data, ð1_dhcpstate, ð1_lease);
|
#endif
|
#endif
|
|
|
// If the interface newly came up, initialize it:
|
// If the interface newly came up, initialize it:
|
// (this duplicates the code in init_all_network_interfaces() really).
|
// (this duplicates the code in init_all_network_interfaces() really).
|
#ifdef CYGHWR_NET_DRIVER_ETH0
|
#ifdef CYGHWR_NET_DRIVER_ETH0
|
if ( eth0_up
|
if ( eth0_up
|
&& eth0_dhcpstate == DHCPSTATE_BOUND
|
&& eth0_dhcpstate == DHCPSTATE_BOUND
|
&& old_eth0_dhcpstate != eth0_dhcpstate ) {
|
&& old_eth0_dhcpstate != eth0_dhcpstate ) {
|
if (!init_net(eth0_name, ð0_bootp_data)) {
|
if (!init_net(eth0_name, ð0_bootp_data)) {
|
eth0_up = false;
|
eth0_up = false;
|
}
|
}
|
}
|
}
|
#endif
|
#endif
|
#ifdef CYGHWR_NET_DRIVER_ETH1
|
#ifdef CYGHWR_NET_DRIVER_ETH1
|
if ( eth1_up
|
if ( eth1_up
|
&& eth1_dhcpstate == DHCPSTATE_BOUND
|
&& eth1_dhcpstate == DHCPSTATE_BOUND
|
&& old_eth1_dhcpstate != eth1_dhcpstate ) {
|
&& old_eth1_dhcpstate != eth1_dhcpstate ) {
|
if (!init_net(eth1_name, ð1_bootp_data)) {
|
if (!init_net(eth1_name, ð1_bootp_data)) {
|
eth1_up = false;
|
eth1_up = false;
|
}
|
}
|
}
|
}
|
#endif
|
#endif
|
|
|
#ifdef CYGHWR_NET_DRIVER_ETH0
|
#ifdef CYGHWR_NET_DRIVER_ETH0
|
if ( old_eth0_dhcpstate == DHCPSTATE_BOUND &&
|
if ( old_eth0_dhcpstate == DHCPSTATE_BOUND &&
|
eth0_dhcpstate == DHCPSTATE_NOTBOUND )
|
eth0_dhcpstate == DHCPSTATE_NOTBOUND )
|
return 0; // a lease timed out; we became unbound
|
return 0; // a lease timed out; we became unbound
|
#endif
|
#endif
|
#ifdef CYGHWR_NET_DRIVER_ETH1
|
#ifdef CYGHWR_NET_DRIVER_ETH1
|
if ( old_eth1_dhcpstate == DHCPSTATE_BOUND &&
|
if ( old_eth1_dhcpstate == DHCPSTATE_BOUND &&
|
eth1_dhcpstate == DHCPSTATE_NOTBOUND )
|
eth1_dhcpstate == DHCPSTATE_NOTBOUND )
|
return 0; // a lease timed out; we became unbound
|
return 0; // a lease timed out; we became unbound
|
#endif
|
#endif
|
return 1; // all is well
|
return 1; // all is well
|
}
|
}
|
|
|
|
|
// Shutdown any interface whose state is DHCPSTATE_NOTBOUND.
|
// Shutdown any interface whose state is DHCPSTATE_NOTBOUND.
|
int dhcp_halt( void )
|
int dhcp_halt( void )
|
{
|
{
|
#ifdef CYGHWR_NET_DRIVER_ETH0
|
#ifdef CYGHWR_NET_DRIVER_ETH0
|
if ( eth0_up
|
if ( eth0_up
|
&& eth0_dhcpstate != DHCPSTATE_FAILED ) {
|
&& eth0_dhcpstate != DHCPSTATE_FAILED ) {
|
do_dhcp_down_net(eth0_name, ð0_bootp_data, ð0_dhcpstate, ð0_lease);
|
do_dhcp_down_net(eth0_name, ð0_bootp_data, ð0_dhcpstate, ð0_lease);
|
}
|
}
|
eth0_up = false;
|
eth0_up = false;
|
#endif
|
#endif
|
#ifdef CYGHWR_NET_DRIVER_ETH1
|
#ifdef CYGHWR_NET_DRIVER_ETH1
|
if ( eth1_up
|
if ( eth1_up
|
&& eth1_dhcpstate != DHCPSTATE_FAILED ) {
|
&& eth1_dhcpstate != DHCPSTATE_FAILED ) {
|
do_dhcp_down_net(eth1_name, ð1_bootp_data, ð1_dhcpstate, ð1_lease);
|
do_dhcp_down_net(eth1_name, ð1_bootp_data, ð1_dhcpstate, ð1_lease);
|
}
|
}
|
eth1_up = false;
|
eth1_up = false;
|
#endif
|
#endif
|
return 1;
|
return 1;
|
}
|
}
|
|
|
|
|
// Release (and set state to DHCPSTATE_NOTBOUND) all interfaces - we are
|
// Release (and set state to DHCPSTATE_NOTBOUND) all interfaces - we are
|
// closing down. (unlikely but maybe useful for testing)
|
// closing down. (unlikely but maybe useful for testing)
|
int dhcp_release( void )
|
int dhcp_release( void )
|
{
|
{
|
#ifdef CYGHWR_NET_DRIVER_ETH0
|
#ifdef CYGHWR_NET_DRIVER_ETH0
|
if (eth0_up)
|
if (eth0_up)
|
do_dhcp_release(eth0_name, ð0_bootp_data, ð0_dhcpstate, ð0_lease);
|
do_dhcp_release(eth0_name, ð0_bootp_data, ð0_dhcpstate, ð0_lease);
|
#endif
|
#endif
|
#ifdef CYGHWR_NET_DRIVER_ETH1
|
#ifdef CYGHWR_NET_DRIVER_ETH1
|
if (eth1_up)
|
if (eth1_up)
|
do_dhcp_release(eth1_name, ð1_bootp_data, ð1_dhcpstate, ð1_lease);
|
do_dhcp_release(eth1_name, ð1_bootp_data, ð1_dhcpstate, ð1_lease);
|
#endif
|
#endif
|
return 1;
|
return 1;
|
}
|
}
|
|
|
|
|
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
// The management thread function
|
// The management thread function
|
void dhcp_mgt_entry( cyg_addrword_t loop_on_failure )
|
void dhcp_mgt_entry( cyg_addrword_t loop_on_failure )
|
{
|
{
|
int j;
|
int j;
|
while ( 1 ) {
|
while ( 1 ) {
|
while ( 1 ) {
|
while ( 1 ) {
|
cyg_semaphore_wait( &dhcp_needs_attention );
|
cyg_semaphore_wait( &dhcp_needs_attention );
|
if ( ! dhcp_bind() ) // a lease expired
|
if ( ! dhcp_bind() ) // a lease expired
|
break; // If we need to re-bind
|
break; // If we need to re-bind
|
}
|
}
|
dhcp_halt(); // tear everything down
|
dhcp_halt(); // tear everything down
|
if ( !loop_on_failure )
|
if ( !loop_on_failure )
|
return; // exit the thread/return
|
return; // exit the thread/return
|
init_all_network_interfaces(); // re-initialize
|
init_all_network_interfaces(); // re-initialize
|
for ( j = 0; j < CYGPKG_NET_NLOOP; j++ )
|
for ( j = 0; j < CYGPKG_NET_NLOOP; j++ )
|
init_loopback_interface( j );
|
init_loopback_interface( j );
|
#ifdef CYGPKG_SNMPAGENT
|
#ifdef CYGPKG_SNMPAGENT
|
SnmpdShutDown(0); // Cycle the snmpd state
|
SnmpdShutDown(0); // Cycle the snmpd state
|
#endif
|
#endif
|
}
|
}
|
}
|
}
|
|
|
#ifdef CYGOPT_NET_DHCP_DHCP_THREAD
|
#ifdef CYGOPT_NET_DHCP_DHCP_THREAD
|
// Then we provide such a thread...
|
// Then we provide such a thread...
|
cyg_handle_t dhcp_mgt_thread_h = 0;
|
cyg_handle_t dhcp_mgt_thread_h = 0;
|
cyg_thread dhcp_mgt_thread;
|
cyg_thread dhcp_mgt_thread;
|
|
|
#define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + sizeof(struct bootp))
|
#define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + sizeof(struct bootp))
|
static cyg_uint8 dhcp_mgt_stack[ STACK_SIZE ];
|
static cyg_uint8 dhcp_mgt_stack[ STACK_SIZE ];
|
|
|
void dhcp_start_dhcp_mgt_thread( void )
|
void dhcp_start_dhcp_mgt_thread( void )
|
{
|
{
|
if ( ! dhcp_mgt_thread_h ) {
|
if ( ! dhcp_mgt_thread_h ) {
|
cyg_semaphore_init( &dhcp_needs_attention, 0 );
|
cyg_semaphore_init( &dhcp_needs_attention, 0 );
|
cyg_thread_create(
|
cyg_thread_create(
|
CYGPKG_NET_DHCP_THREAD_PRIORITY, /* scheduling info (eg pri) */
|
CYGPKG_NET_DHCP_THREAD_PRIORITY, /* scheduling info (eg pri) */
|
dhcp_mgt_entry, /* entry point function */
|
dhcp_mgt_entry, /* entry point function */
|
CYGOPT_NET_DHCP_DHCP_THREAD_PARAM, /* entry data */
|
CYGOPT_NET_DHCP_DHCP_THREAD_PARAM, /* entry data */
|
"DHCP lease mgt", /* optional thread name */
|
"DHCP lease mgt", /* optional thread name */
|
dhcp_mgt_stack, /* stack base, NULL = alloc */
|
dhcp_mgt_stack, /* stack base, NULL = alloc */
|
STACK_SIZE, /* stack size, 0 = default */
|
STACK_SIZE, /* stack size, 0 = default */
|
&dhcp_mgt_thread_h, /* returned thread handle */
|
&dhcp_mgt_thread_h, /* returned thread handle */
|
&dhcp_mgt_thread /* put thread here */
|
&dhcp_mgt_thread /* put thread here */
|
);
|
);
|
|
|
cyg_thread_resume(dhcp_mgt_thread_h);
|
cyg_thread_resume(dhcp_mgt_thread_h);
|
}
|
}
|
}
|
}
|
|
|
|
|
#endif // CYGOPT_NET_DHCP_DHCP_THREAD
|
#endif // CYGOPT_NET_DHCP_DHCP_THREAD
|
|
|
#endif // CYGPKG_NET_DHCP
|
#endif // CYGPKG_NET_DHCP
|
|
|
// EOF dhcp_support.c
|
// EOF dhcp_support.c
|
|
|