ReactOS 0.4.15-dev-8096-ga0eec98
dispatch.c File Reference
#include <rosdhcp.h>
#include <reactos/debug.h>
Include dependency graph for dispatch.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

void dispatch (HANDLE hStopEvent)
 
void got_one (struct protocol *l)
 
void add_timeout (time_t when, void(*where)(void *), void *what)
 
void cancel_timeout (void(*where)(void *), void *what)
 
void add_protocol (char *name, int fd, void(*handler)(struct protocol *), void *local)
 
void remove_protocol (struct protocol *proto)
 
struct protocolfind_protocol_by_adapter (struct interface_info *info)
 
int interface_link_status (char *ifname)
 

Variables

SOCKET DhcpSocket
 
HANDLE hAdapterStateChangedEvent
 
struct protocolprotocols = NULL
 
struct timeouttimeouts = NULL
 
static struct timeoutfree_timeouts = NULL
 
void(* bootp_packet_handler )(struct interface_info *, struct dhcp_packet *, int, unsigned int, struct iaddr, struct hardware *)
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 44 of file dispatch.c.

Function Documentation

◆ add_protocol()

void add_protocol ( char name,
int  fd,
void(*)(struct protocol *)  handler,
void local 
)

Definition at line 342 of file dispatch.c.

344{
345 struct protocol *p;
346
347 p = malloc(sizeof(*p));
348 if (!p)
349 error("can't allocate protocol struct for %s", name);
350
351 p->fd = fd;
352 p->handler = handler;
353 p->local = local;
354 p->next = protocols;
355 protocols = p;
356}
struct protocol * protocols
Definition: dispatch.c:56
#define malloc
Definition: debug_ros.c:4
UINT(* handler)(MSIPACKAGE *)
Definition: action.c:7482
#define local
Definition: zutil.h:30
GLfloat GLfloat p
Definition: glext.h:8902
#define error(str)
Definition: mkdosfs.c:1605
static int fd
Definition: io.c:51
struct define * next
Definition: compiler.c:65
Definition: name.c:39

Referenced by AdapterDiscoveryThread(), DSLeaseIpAddress(), and DSRenewIpAddressLease().

◆ add_timeout()

void add_timeout ( time_t  when,
void(*)(void *)  where,
void what 
)

Definition at line 253 of file dispatch.c.

254{
255 struct timeout *t, *q;
256
257 DH_DbgPrint(MID_TRACE,("Adding timeout %x %p %x\n", when, where, what));
258 /* See if this timeout supersedes an existing timeout. */
259 t = NULL;
260 for (q = timeouts; q; q = q->next) {
261 if (q->func == where && q->what == what) {
262 if (t)
263 t->next = q->next;
264 else
265 timeouts = q->next;
266 break;
267 }
268 t = q;
269 }
270
271 /* If we didn't supersede a timeout, allocate a timeout
272 structure now. */
273 if (!q) {
274 if (free_timeouts) {
276 free_timeouts = q->next;
277 q->func = where;
278 q->what = what;
279 } else {
280 q = malloc(sizeof(struct timeout));
281 if (!q) {
282 error("Can't allocate timeout structure!");
283 return;
284 }
285 q->func = where;
286 q->what = what;
287 }
288 }
289
290 q->when = when;
291
292 /* Now sort this timeout into the timeout list. */
293
294 /* Beginning of list? */
295 if (!timeouts || timeouts->when > q->when) {
296 q->next = timeouts;
297 timeouts = q;
298 return;
299 }
300
301 /* Middle of list? */
302 for (t = timeouts; t->next; t = t->next) {
303 if (t->next->when > q->when) {
304 q->next = t->next;
305 t->next = q;
306 return;
307 }
308 }
309
310 /* End of list. */
311 t->next = q;
312 q->next = NULL;
313}
static struct timeout * free_timeouts
Definition: dispatch.c:58
struct timeout * timeouts
Definition: dispatch.c:57
#define MID_TRACE
Definition: debug.h:15
#define DH_DbgPrint(_t_, _x_)
Definition: debug.h:49
#define NULL
Definition: types.h:112
GLdouble GLdouble t
Definition: gl.h:2047
GLdouble GLdouble GLdouble GLdouble q
Definition: gl.h:2063
Definition: dhcpd.h:245
void * what
Definition: dhcpd.h:249
time_t when
Definition: dhcpd.h:247

Referenced by bind_lease(), dhcpoffer(), send_discover(), and send_request().

◆ cancel_timeout()

void cancel_timeout ( void(*)(void *)  where,
void what 
)

Definition at line 316 of file dispatch.c.

317{
318 struct timeout *t, *q;
319
320 /* Look for this timeout on the list, and unlink it if we find it. */
321 t = NULL;
322 for (q = timeouts; q; q = q->next) {
323 if (q->func == where && q->what == what) {
324 if (t)
325 t->next = q->next;
326 else
327 timeouts = q->next;
328 break;
329 }
330 t = q;
331 }
332
333 /* If we found the timeout, put it on the free list. */
334 if (q) {
335 q->next = free_timeouts;
337 }
338}

Referenced by dhcpack(), dhcpnak(), dhcpoffer(), send_request(), and state_selecting().

◆ dispatch()

void dispatch ( HANDLE  hStopEvent)

Definition at line 70 of file dispatch.c.

71{
72 int count, to_msec;
73 struct protocol *l;
74 time_t howlong, cur_time;
75 HANDLE Events[3];
76 int EventCount = 2;
77
79 Events[1] = hStopEvent;
81
82 ApiLock();
83
84 do {
85 /*
86 * Call any expired timeouts, and then if there's still
87 * a timeout registered, time out the select call then.
88 */
89 time(&cur_time);
90
91 if (timeouts)
92 {
93 struct timeout *t;
94
95 if (timeouts->when <= cur_time) {
96 t = timeouts;
97 timeouts = timeouts->next;
98 (*(t->func))(t->what);
99 t->next = free_timeouts;
101 continue;
102 }
103
104 /*
105 * Figure timeout in milliseconds, and check for
106 * potential overflow, so we can cram into an
107 * int for poll, while not polling with a
108 * negative timeout and blocking indefinitely.
109 */
110 howlong = timeouts->when - cur_time;
111 if (howlong > INT_MAX / 1000)
112 howlong = INT_MAX / 1000;
113 to_msec = howlong * 1000;
114 }
115 else
116 {
117 to_msec = INFINITE;
118 }
119
121 {
122 Events[2] = WSACreateEvent();
123 if (Events[2] != WSA_INVALID_EVENT)
124 {
126 if (count != NO_ERROR)
127 {
130 }
131 else
132 {
133 EventCount = 3;
134 }
135 }
136 }
138 {
141
142 EventCount = 2;
143 }
144
145 ApiUnlock();
146 count = WaitForMultipleObjects(EventCount,
147 Events,
148 FALSE,
149 to_msec);
150 ApiLock();
151 if (count == WAIT_OBJECT_0)
152 {
153 /* Adapter state change */
154 continue;
155 }
156 else if (count == WAIT_OBJECT_0 + 1)
157 {
158 /* Stop event signalled */
159 DPRINT("Dispatch thread stop event!\n");
160 break;
161 }
162 else if (count == WAIT_OBJECT_0 + 2)
163 {
164 /* Packet received */
165
166 /* WSA events are manual reset events */
168 }
169 else
170 {
171 /* Timeout */
172 continue;
173 }
174
175 for (l = protocols; l; l = l->next) {
176 struct interface_info *ip;
177 ip = l->local;
178 if (ip && (l->handler != got_one ||
179 !ip->dead)) {
180 DH_DbgPrint(MID_TRACE,("Handling %x\n", l));
181 (*(l->handler))(l);
182 }
183 }
184 } while (1);
185
187
188 ApiUnlock();
189
190 DPRINT("Dispatch thread stopped!\n");
191}
VOID ApiLock()
Definition: api.c:22
VOID ApiUnlock()
Definition: api.c:26
HANDLE hAdapterStateChangedEvent
Definition: dhcpcsvc.c:20
SOCKET DhcpSocket
Definition: adapter.c:8
void got_one(struct protocol *l)
Definition: dispatch.c:194
HANDLE Events[3]
Definition: schedsvc.c:40
r l[0]
Definition: byte_order.h:168
#define NO_ERROR
Definition: dderror.h:5
HANDLE hStopEvent
Definition: dhcpcsvc.c:19
time_t cur_time
#define FALSE
Definition: types.h:117
BOOL WSAAPI WSAResetEvent(IN WSAEVENT hEvent)
Definition: event.c:53
BOOL WSAAPI WSACloseEvent(IN WSAEVENT hEvent)
Definition: event.c:23
INT WSAAPI WSAEventSelect(IN SOCKET s, IN WSAEVENT hEventObject, IN LONG lNetworkEvents)
Definition: select.c:182
__kernel_time_t time_t
Definition: linux.h:252
#define INFINITE
Definition: serial.h:102
GLuint GLuint GLsizei count
Definition: gl.h:1545
#define INT_MAX
Definition: limits.h:40
__u16 time
Definition: mkdosfs.c:8
#define DPRINT
Definition: sndvol32.h:73
Definition: dhcpd.h:62
DWORD WINAPI WaitForMultipleObjects(IN DWORD nCount, IN CONST HANDLE *lpHandles, IN BOOL bWaitAll, IN DWORD dwMilliseconds)
Definition: synch.c:151
#define WAIT_OBJECT_0
Definition: winbase.h:406
WINSOCK_API_LINKAGE WSAEVENT WSAAPI WSACreateEvent(void)
Definition: event.c:42
#define WSA_INVALID_EVENT
Definition: winsock2.h:623
#define FD_READ
Definition: winsock.h:405
#define INVALID_SOCKET
Definition: winsock.h:332
#define FD_CLOSE
Definition: winsock.h:410

Referenced by create_database(), create_list(), create_view(), CxxHandleV8Frame(), database_invoke(), ICreateTypeInfo2_fnSetTypeFlags(), init_test_dispatch(), InstallerImpl_OpenDatabase(), InstallerImpl_OpenPackage(), InstallerImpl_Products(), InstallerImpl_RelatedProducts(), InstallerImpl_SummaryInformation(), main(), run_script(), ServiceMain(), session_invoke(), svc_create(), svc_reg(), svc_tp_create(), test_CreateTypeLib(), test_IConnectionPoint(), test_IWinHttpRequest_Invoke(), test_ParseDisplayName(), test_recinfo(), test_VarCat(), and test_VarUI1FromDisp().

◆ find_protocol_by_adapter()

struct protocol * find_protocol_by_adapter ( struct interface_info info)

Definition at line 412 of file dispatch.c.

413{
414 struct protocol *p;
415
416 for( p = protocols; p; p = p->next ) {
417 if( p->local == (void *)info ) return p;
418 }
419
420 return NULL;
421}

Referenced by AdapterDiscoveryThread(), DSLeaseIpAddress(), DSReleaseIpAddressLease(), DSRenewIpAddressLease(), DSStaticRefreshParams(), and IsReconnectHackNeeded().

◆ got_one()

void got_one ( struct protocol l)

Definition at line 194 of file dispatch.c.

195{
196 struct sockaddr_in from;
197 struct hardware hfrom;
198 struct iaddr ifrom;
200 union {
201 /*
202 * Packet input buffer. Must be as large as largest
203 * possible MTU.
204 */
205 unsigned char packbuf[4095];
206 struct dhcp_packet packet;
207 } u;
208 struct interface_info *ip = l->local;
210
211 if ((result = receive_packet(ip, u.packbuf, sizeof(u), &from,
212 &hfrom)) == -1) {
213 warning("receive_packet failed on %s: %d", ip->name,
215 ip->errors++;
216 if (ip->errors > 20) {
217 /* our interface has gone away. */
218 warning("Interface %s no longer appears valid.",
219 ip->name);
220 ip->dead = 1;
221 closesocket(l->fd);
224 if (adapter) {
225 RemoveEntryList(&adapter->ListEntry);
226 free(adapter);
227 }
228 }
229 return;
230 }
231 if (result == 0)
232 return;
233
235 ifrom.len = 4;
236 memcpy(ifrom.iabuf, &from.sin_addr, ifrom.len);
237
238
239 adapter = AdapterFindByHardwareAddress(u.packet.chaddr,
240 u.packet.hlen);
241
242 if (!adapter) {
243 warning("Discarding packet with a non-matching target physical address\n");
244 return;
245 }
246
247 (*bootp_packet_handler)(&adapter->DhclientInfo, &u.packet, result,
248 from.sin_port, ifrom, &hfrom);
249 }
250}
PDHCP_ADAPTER AdapterFindByHardwareAddress(u_int8_t haddr[16], u_int8_t hlen)
Definition: adapter.c:540
PDHCP_ADAPTER AdapterFindInfo(struct interface_info *ip)
Definition: adapter.c:526
void remove_protocol(struct protocol *proto)
Definition: dispatch.c:359
void(* bootp_packet_handler)(struct interface_info *, struct dhcp_packet *, int, unsigned int, struct iaddr, struct hardware *)
Definition: dispatch.c:59
#define free
Definition: debug_ros.c:5
#define RemoveEntryList(Entry)
Definition: env_spec_w32.h:986
return adapter
GLuint64EXT * result
Definition: glext.h:11304
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble * u
Definition: glfuncs.h:240
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define closesocket
Definition: ncftp.h:477
int ssize_t
Definition: rosdhcp.h:48
#define warning(s)
Definition: debug.h:83
ssize_t receive_packet(struct interface_info *ip, unsigned char *packet_data, size_t packet_len, struct sockaddr_in *dest, struct hardware *hardware)
Definition: socket.c:34
CardRegion * from
Definition: spigame.cpp:19
Definition: dhcpd.h:125
Definition: dhcpd.h:135
int PASCAL FAR WSAGetLastError(void)
Definition: dllmain.c:112

Referenced by AdapterDiscoveryThread(), dispatch(), DSLeaseIpAddress(), DSRenewIpAddressLease(), and slurpstring().

◆ interface_link_status()

int interface_link_status ( char ifname)

Definition at line 424 of file dispatch.c.

425{
426 return (1);
427}

◆ remove_protocol()

void remove_protocol ( struct protocol proto)

Definition at line 359 of file dispatch.c.

360{
361 struct protocol *p, *next, *prev;
362 struct interface_info *ip = proto->local;
363 struct timeout *t, *q, *u;
364
365 t = NULL;
366 q = timeouts;
367 while (q != NULL)
368 {
369 /* Remove all timeouts for this protocol */
370 if (q->what == ip)
371 {
372 /* Unlink the timeout from previous */
373 if (t)
374 t->next = q->next;
375 else
376 timeouts = q->next;
377
378 /* Advance to the next timeout */
379 u = q->next;
380
381 /* Add it to the free list */
382 q->next = free_timeouts;
384 }
385 else
386 {
387 /* Advance to the next timeout */
388 u = q->next;
389
390 /* Update the previous pointer */
391 t = q;
392 }
393
394 /* Advance */
395 q = u;
396 }
397
398 prev = NULL;
399 for (p = protocols; p; p = next) {
400 next = p->next;
401 if (p == proto) {
402 if (prev)
403 prev->next = p->next;
404 else
405 protocols = p->next;
406 free(p);
407 }
408 }
409}
static unsigned __int64 next
Definition: rand_nt.c:6
struct protocol * next
Definition: dhcpd.h:253

Referenced by AdapterDiscoveryThread(), DSLeaseIpAddress(), DSReleaseIpAddressLease(), DSRenewIpAddressLease(), DSStaticRefreshParams(), and got_one().

Variable Documentation

◆ bootp_packet_handler

Definition at line 59 of file dispatch.c.

Referenced by got_one(), and init_client().

◆ DhcpSocket

SOCKET DhcpSocket
extern

Definition at line 8 of file adapter.c.

Referenced by dispatch().

◆ free_timeouts

struct timeout* free_timeouts = NULL
static

Definition at line 58 of file dispatch.c.

Referenced by add_timeout(), cancel_timeout(), dispatch(), and remove_protocol().

◆ hAdapterStateChangedEvent

HANDLE hAdapterStateChangedEvent
extern

Definition at line 20 of file dhcpcsvc.c.

Referenced by dispatch(), and ServiceMain().

◆ protocols

◆ timeouts