summaryrefslogtreecommitdiff
path: root/ecos/packages/net/common/current/src/dhcp_support.c
blob: 7fff191446fef74e4a5513a67589644e49d6a882 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*==========================================================================
//
//      dhcp_support.c
//
//      Support code == friendly API for DHCP client
//
//==========================================================================
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
// -------------------------------------------                              
// This file is part of eCos, the Embedded Configurable Operating System.   
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
//
// 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     
// 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 WARRANTY; without even the implied warranty of MERCHANTABILITY or    
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
// for more details.                                                        
//
// You should have received a copy of the GNU General Public License        
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
//
// 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 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 License. However the source code for this file    
// must still be made available in accordance with section (3) of the GNU   
// General Public License v2.                                               
//
// This exception does not invalidate any other reasons why a work based    
// on this file might be covered by the GNU General Public License.         
// -------------------------------------------                              
// ####ECOSGPLCOPYRIGHTEND####                                              
//==========================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s):   hmt
// Contributors: gthomas
// Date:        2000-07-01
// Purpose:     DHCP support
// Description: 
//
//####DESCRIPTIONEND####
//
//========================================================================*/

#include <pkgconf/system.h>
#include <pkgconf/net.h>

#ifdef CYGPKG_NET_DHCP

#include <network.h>
#include <dhcp.h>

// ---------------------------------------------------------------------------
#ifdef CYGHWR_NET_DRIVER_ETH0
cyg_uint8   eth0_dhcpstate = 0;
#endif
#ifdef CYGHWR_NET_DRIVER_ETH1
cyg_uint8   eth1_dhcpstate = 0;
#endif

cyg_sem_t dhcp_needs_attention;

#ifdef CYGHWR_NET_DRIVER_ETH0
struct dhcp_lease eth0_lease = { &dhcp_needs_attention, 0,0,0,0,0,0 };
#endif
#ifdef CYGHWR_NET_DRIVER_ETH1
struct dhcp_lease eth1_lease = { &dhcp_needs_attention, 0,0,0,0,0,0 };
#endif

// ---------------------------------------------------------------------------
//
// 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
// flexible.  The dhcp_prot.c module should do all the work... 
//
// ---------------------------------------------------------------------------

// return value: 1 => everything OK, no change.
// 0 => close your connections, then call do_dhcp_halt() to halt the
// interface(s) in question (it knows because the state will be NOTBOUND).
// After that you can return to the start and use
// init_all_network_interfaces(); as usual, or call do_dhcp_bind() by hand,
// or whatever...
int dhcp_bind( void )
{
#ifdef CYGHWR_NET_DRIVER_ETH0
    cyg_uint8 old_eth0_dhcpstate = eth0_dhcpstate;
#endif
#ifdef CYGHWR_NET_DRIVER_ETH1
    cyg_uint8 old_eth1_dhcpstate = eth1_dhcpstate;
#endif

    // If there are no interfaces at all, init it every time, doesn't
    // matter.  In case we are called from elsewhere...
    if ( 1
#ifdef CYGHWR_NET_DRIVER_ETH0
         && eth0_dhcpstate == 0
#endif
#ifdef CYGHWR_NET_DRIVER_ETH1
         && eth1_dhcpstate == 0
#endif
        )
        cyg_semaphore_init( &dhcp_needs_attention, 0 );

    // Run the state machine...
#ifdef CYGHWR_NET_DRIVER_ETH0
    if (eth0_up
        && DHCPSTATE_FAILED != eth0_dhcpstate )
            eth0_up = do_dhcp(eth0_name, &eth0_bootp_data, &eth0_dhcpstate, &eth0_lease);
#endif            
#ifdef CYGHWR_NET_DRIVER_ETH1
    if (eth1_up
        && DHCPSTATE_FAILED != eth1_dhcpstate )
            eth1_up = do_dhcp(eth1_name, &eth1_bootp_data, &eth1_dhcpstate, &eth1_lease);
#endif            

    // If the interface newly came up, initialize it:
    // (this duplicates the code in init_all_network_interfaces() really).
#ifdef CYGHWR_NET_DRIVER_ETH0
    if ( eth0_up
         && eth0_dhcpstate == DHCPSTATE_BOUND
         && old_eth0_dhcpstate != eth0_dhcpstate ) {
        if (!init_net(eth0_name, &eth0_bootp_data)) {
            eth0_up = false;
        }
    }
#endif
#ifdef CYGHWR_NET_DRIVER_ETH1
    if ( eth1_up
         && eth1_dhcpstate == DHCPSTATE_BOUND
         && old_eth1_dhcpstate != eth1_dhcpstate ) {
        if (!init_net(eth1_name, &eth1_bootp_data)) {
            eth1_up = false;
        }
    }
#endif

#ifdef CYGHWR_NET_DRIVER_ETH0
    if ( old_eth0_dhcpstate == DHCPSTATE_BOUND &&
         eth0_dhcpstate == DHCPSTATE_NOTBOUND )
        return 0; // a lease timed out; we became unbound
#endif
#ifdef CYGHWR_NET_DRIVER_ETH1
    if ( old_eth1_dhcpstate == DHCPSTATE_BOUND &&
         eth1_dhcpstate == DHCPSTATE_NOTBOUND )
        return 0; // a lease timed out; we became unbound
#endif
    return 1; // all is well
}


// Shutdown any interface whose state is DHCPSTATE_NOTBOUND.
int dhcp_halt( void )
{
#ifdef CYGHWR_NET_DRIVER_ETH0
    if ( eth0_up
         && eth0_dhcpstate != DHCPSTATE_FAILED ) {
        do_dhcp_down_net(eth0_name, &eth0_bootp_data, &eth0_dhcpstate, &eth0_lease);
    }
    eth0_up = false;
#endif
#ifdef CYGHWR_NET_DRIVER_ETH1
    if ( eth1_up
         && eth1_dhcpstate != DHCPSTATE_FAILED ) {
        do_dhcp_down_net(eth1_name, &eth1_bootp_data, &eth1_dhcpstate, &eth1_lease);
    }
    eth1_up = false;
#endif
    return 1;
}


// Release (and set state to DHCPSTATE_NOTBOUND) all interfaces - we are
// closing down.  (unlikely but maybe useful for testing)
int dhcp_release( void )
{
#ifdef CYGHWR_NET_DRIVER_ETH0
    if (eth0_up)
        do_dhcp_release(eth0_name, &eth0_bootp_data, &eth0_dhcpstate, &eth0_lease);
#endif
#ifdef CYGHWR_NET_DRIVER_ETH1
    if (eth1_up)
        do_dhcp_release(eth1_name, &eth1_bootp_data, &eth1_dhcpstate, &eth1_lease);
#endif
    return 1;
}


// ------------------------------------------------------------------------
// The management thread function
//
// Note: 2007-01-15
//  This single management thread attempts to keep all configured
//  interfaces alive via DHCP.  While this may be sufficient for 
//  many systems, it falls short of perfect.  There should probably
//  be a separate thread for each possible interface, along with
//  appropriate CDL to control how each inteface is managed.
//
void dhcp_mgt_entry( cyg_addrword_t loop_on_failure )
{
    int j;
    bool any_interfaces_up;

    while ( 1 ) {
        while ( 1 ) {
            cyg_semaphore_wait( &dhcp_needs_attention );
            if ( ! dhcp_bind() ) // a lease expired
                break; // If we need to re-bind
        }
        dhcp_halt(); // tear everything down
        if ( !loop_on_failure )
            return; // exit the thread/return
        do {
            init_all_network_interfaces(); // re-initialize
            // If at least one interface is up, then the DHCP machine will run
            any_interfaces_up = false;
#ifdef CYGHWR_NET_DRIVER_ETH0
            any_interfaces_up |= eth0_up;
#endif
#ifdef CYGHWR_NET_DRIVER_ETH1
            any_interfaces_up |= eth1_up;
#endif
        } while (!any_interfaces_up);
        for ( j = 0; j < CYGPKG_NET_NLOOP; j++ )
            init_loopback_interface( j );
#ifdef CYGPKG_SNMPAGENT
        SnmpdShutDown(0); // Cycle the snmpd state
#endif
    }
}

#ifdef CYGOPT_NET_DHCP_DHCP_THREAD
// Then we provide such a thread...
cyg_handle_t dhcp_mgt_thread_h = 0;
cyg_thread   dhcp_mgt_thread;

static cyg_uint8 dhcp_mgt_stack[ CYGPKG_NET_DHCP_THREAD_STACK_SIZE ];

void dhcp_start_dhcp_mgt_thread( void )
{
    if ( ! dhcp_mgt_thread_h ) {
        cyg_semaphore_init( &dhcp_needs_attention, 0 );
        cyg_thread_create(
            CYGPKG_NET_DHCP_THREAD_PRIORITY,   /* scheduling info (eg pri) */
            dhcp_mgt_entry,                    /* entry point function */
            CYGOPT_NET_DHCP_DHCP_THREAD_PARAM, /* entry data */
            "DHCP lease mgt",                  /* optional thread name */
            dhcp_mgt_stack,                    /* stack base, NULL = alloc */
            CYGPKG_NET_DHCP_THREAD_STACK_SIZE, /* stack size, 0 = default */
            &dhcp_mgt_thread_h,                /* returned thread handle */
            &dhcp_mgt_thread                   /* put thread here */
            );

        cyg_thread_resume(dhcp_mgt_thread_h);
    }
}


#endif // CYGOPT_NET_DHCP_DHCP_THREAD

#endif // CYGPKG_NET_DHCP

// EOF dhcp_support.c