comparison packages/net/tcpip/current/include/dhcp.h @ 110:84e4bde58b26 ecos-sw-2000-07-14

Merge from eCos master repository on 2000-07-14-22:00:02-BST
author jlarmour
date Mon, 17 Jul 2000 14:42:27 +0000
parents
children d397fc472bcf
comparison
equal deleted inserted replaced
109:5720c4b6e1d2 110:84e4bde58b26
1 #ifndef CYGONCE_NET_TCPIP_DHCP_H
2 #define CYGONCE_NET_TCPIP_DHCP_H
3
4 //==========================================================================
5 //
6 // include/dhcp.h
7 //
8 // DHCP protocol support
9 //
10 //==========================================================================
11 //####COPYRIGHTBEGIN####
12 //
13 // -------------------------------------------
14 // The contents of this file are subject to the Red Hat eCos Public License
15 // Version 1.1 (the "License"); you may not use this file except in
16 // compliance with the License. You may obtain a copy of the License at
17 // http://www.redhat.com/
18 //
19 // Software distributed under the License is distributed on an "AS IS"
20 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
21 // License for the specific language governing rights and limitations under
22 // the License.
23 //
24 // The Original Code is eCos - Embedded Configurable Operating System,
25 // released September 30, 1998.
26 //
27 // The Initial Developer of the Original Code is Red Hat.
28 // Portions created by Red Hat are
29 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
30 // All Rights Reserved.
31 // -------------------------------------------
32 //
33 //####COPYRIGHTEND####
34 //####BSDCOPYRIGHTBEGIN####
35 //
36 // -------------------------------------------
37 //
38 // Portions of this software may have been derived from OpenBSD or other sources,
39 // and are covered by the appropriate copyright disclaimers included herein.
40 //
41 // -------------------------------------------
42 //
43 //####BSDCOPYRIGHTEND####
44 //==========================================================================
45 //#####DESCRIPTIONBEGIN####
46 //
47 // Author(s): hmt
48 // Contributors: gthomas
49 // Date: 2000-07-01
50 // Purpose: Support DHCP initialization in eCos TCPIP stack
51 // Description:
52 //
53 //
54 //####DESCRIPTIONEND####
55 //
56 //==========================================================================
57
58 //
59 // DHCP. RFC2131, RFC1533, RFC1534
60 // See also bootp.h
61 //
62
63 #include <pkgconf/system.h>
64 #include <pkgconf/net.h>
65
66 #ifdef CYGPKG_NET_DHCP
67
68 #include <machine/types.h>
69
70 #include <cyg/kernel/kapi.h>
71
72 #include <bootp.h>
73
74 // DHCP messages; these are sent in the tag TAG_DHCP_MESS_TYPE already
75 // defined in bootp.h
76
77 #define DHCPDISCOVER 1
78 #define DHCPOFFER 2
79 #define DHCPREQUEST 3
80 #define DHCPDECLINE 4
81 #define DHCPACK 5
82 #define DHCPNAK 6
83 #define DHCPRELEASE 7
84
85 // DHCP interface state machine states; these are published so that app
86 // code can know what to do... (see page 35 of RFC2131)
87
88 // These we will use in the normal course of events
89 #define DHCPSTATE_INIT 1
90 #define DHCPSTATE_SELECTING 2 // wait for replies to b/c DISCOVER
91 #define DHCPSTATE_REQUESTING 3
92 #define DHCPSTATE_REQUEST_RECV 4 // wait for replies to b/c REQUEST
93 #define DHCPSTATE_BOUND 5
94 #define DHCPSTATE_RENEWING 6 // wait for replies to u/c REQUEST
95 #define DHCPSTATE_RENEW_RECV 7
96 #define DHCPSTATE_REBINDING 8 // wait for replies to b/c REQUEST
97 #define DHCPSTATE_REBIND_RECV 9
98 #define DHCPSTATE_BOOTP_FALLBACK 10 // fall back to plain bootp
99 #define DHCPSTATE_NOTBOUND 11 // To let app tidy up
100 #define DHCPSTATE_FAILED 12 // Net is down
101 #define DHCPSTATE_DO_RELEASE 13 // Force release of the current lease
102 // These we don't use
103 //#define DHCPSTATE_INITREBOOT
104 //#define DHCPSTATE_REBOOTING
105
106 // These are to let the app inspect the state of the interfaces when
107 // managing them itself, by analogy with eth0_up &c; eth0_bootp_data and so
108 // on will still be used with DHCP.
109 #ifdef CYGHWR_NET_DRIVER_ETH0
110 extern cyg_uint8 eth0_dhcpstate;
111 #endif
112 #ifdef CYGHWR_NET_DRIVER_ETH1
113 extern cyg_uint8 eth1_dhcpstate;
114 #endif
115
116 // This is public so the app wait on it or poll it when managing DHCP
117 // itself. It will be zero while the app should wait, and posted when a
118 // call to do_dhcp() is needed.
119 extern cyg_sem_t dhcp_needs_attention;
120
121 // This routine is used at startup time, and after relinquishing leases or
122 // after a lease timeout: it does DHCP or bootp or static setup according
123 // to configuration.
124 extern void init_all_network_interfaces(void);
125
126 // This routine does the work of renewing leases &c.
127 // return value: 1 => everything OK, no change.
128 // 0 => close your connections, then call do_dhcp_halt() to halt the
129 // interface(s) in question (it knows because the state will be NOTBOUND).
130 // After that you can return to the start and use
131 // init_all_network_interfaces() as usual.
132 extern int dhcp_bind( void );
133
134 // Shutdown any interface which is not already shut down - whether
135 // initialized by DHCP or not. Reason: because of the broadcast-while-not-
136 // -fully-initialized nature of the DHCP conversation, all other interfaces
137 // must be shut down during that. So one down, all down, is required.
138 extern int dhcp_halt( void );
139
140 // Release (and set state to DHCPSTATE_NOTBOUND) all interfaces - we are
141 // closing down. (unlikely but useful for testing)
142 // Interfaces are left up; use dhcp_halt() to bring them right down, then
143 // call init_all_network_interfaces() as usual to restart all.
144 extern int dhcp_release( void );
145
146 // The intent with this API is that a simple DHCP client thread, which
147 // maintains the state of the interfaces, can go as follows:
148 // (after init_all_networks is called from elsewhere)
149 //
150 // while ( 1 ) {
151 // while ( 1 ) {
152 // cyg_semaphore_wait( &dhcp_needs_attention );
153 // if ( ! dhcp_bind() ) // a lease expired
154 // break; // If we need to re-bind
155 // }
156 // dhcp_halt(); // tear everything down
157 // init_all_network_interfaces(); // re-initialize
158 // }
159 //
160 // and if the application does not want to suffer the overhead of a
161 // separate thread and its stack for this, this functionality can be placed
162 // in the app's server loop in an obvious fashion. That is the goal of
163 // breaking out these internal elements. For example, some server might
164 // be arranged to poll DHCP from time to time like this:
165 //
166 // while ( 1 ) {
167 // init_all_network_interfaces();
168 // open-my-listen-sockets();
169 // while ( 1 ) {
170 // serve-one-request();
171 // // sleeps if no connections, but not forever; so this loop is
172 // // polled a few times a minute...
173 // if ( cyg_semaphore_trywait( &dhcp_needs_attention )) {
174 // if ( ! dhcp_bind() ) {
175 // close-my-listen-sockets();
176 // dhcp_halt();
177 // break;
178 // }
179 // }
180 // }
181 // }
182 //
183 // ------------------------------------------------------------------------
184
185 #ifdef CYGOPT_NET_DHCP_DHCP_THREAD
186 // Then we provide such a thread...
187
188 // Provide a separate thread to renew DHCP leases; otherwise the
189 // application MUST periodically examine the semaphore dhcp_needs_attention
190 // and call dhcp_bind() if it is signalled. If enabled, this thread does
191 // all that for you. Independent of this option, initialization of the
192 // interfaces still occurs in init_all_network_interfaces() and your
193 // startup code must call that. It will start the DHCP management thread
194 // if necessary. If a lease fails to be renewed, the management thread
195 // will shut down all interfaces and attempt to initialize all the
196 // interfaces again from scratch. This may cause chaos in the app, which
197 // is why managing the DHCP state in an application aware thread is
198 // actually better, just far less convenient for testing.
199
200 extern cyg_handle_t dhcp_mgt_thread_h; // To allow its external manipulation.
201 extern cyg_thread dhcp_mgt_thread; // The object itself
202
203 extern void dhcp_start_dhcp_mgt_thread( void );
204
205 #endif
206
207 // The function is provided unconditionally so that the app can put it in a
208 // thread of its own. If the parameter is true, it loops forever; if
209 // false, the call returns if a lease expires, and the caller must tidy up
210 // or reboot the whole machine.
211 extern cyg_thread_entry_t dhcp_mgt_entry;
212 extern void dhcp_mgt_entry( cyg_addrword_t loop_on_failure ); // the function
213
214 // ---------------------------------------------------------------------------
215 // These are rather more internal routines, internal to the protocol engine
216 // in dhcp_prot.c - those above are in dhcp_support.c
217
218 #define DHCP_LEASE_T1 1
219 #define DHCP_LEASE_T2 2
220 #define DHCP_LEASE_EX 4
221
222 struct dhcp_lease {
223 cyg_tick_count_t t1, t2, expiry;
224 volatile cyg_uint8 next;
225 volatile cyg_uint8 which;
226 cyg_handle_t alarm;
227 cyg_alarm alarm_obj;
228 };
229
230 #ifdef CYGHWR_NET_DRIVER_ETH0
231 extern struct dhcp_lease eth0_lease;
232 #endif
233 #ifdef CYGHWR_NET_DRIVER_ETH1
234 extern struct dhcp_lease eth1_lease;
235 #endif
236
237 extern int
238 do_dhcp(const char *interface, struct bootp *res,
239 cyg_uint8 *pstate, struct dhcp_lease *lease);
240 // NB *res and *pstate and *lease are all INOUT; *res must point to a valid
241 // record from "last time".
242
243 extern int
244 do_dhcp_down_net(const char *intf, struct bootp *res,
245 cyg_uint8 *pstate, struct dhcp_lease *lease);
246
247 extern int
248 do_dhcp_release(const char *intf, struct bootp *res,
249 cyg_uint8 *pstate, struct dhcp_lease *lease);
250
251 #endif // CYGPKG_NET_DHCP
252
253 #endif // CYGONCE_NET_TCPIP_DHCP_H
254 // EOF dhcp.h