mirror of
https://github.com/reactos/reactos.git
synced 2025-02-24 09:25:10 +00:00
[DHCPCSVC]
- Fix ipconfig hang with ipconfig /renew and ipconfig /release - Fix duplicate protocol entries being added for the same adapter - Use WSAEventSelect and WaitForMultipleObjects to manage waiting between timeouts, adapter state changes, and incoming packets svn path=/branches/wlan-bringup/; revision=54912
This commit is contained in:
parent
83a2fb2da4
commit
12f1e92c72
3 changed files with 105 additions and 46 deletions
|
@ -1,6 +1,6 @@
|
||||||
#include "rosdhcp.h"
|
#include "rosdhcp.h"
|
||||||
|
|
||||||
static SOCKET DhcpSocket = INVALID_SOCKET;
|
SOCKET DhcpSocket = INVALID_SOCKET;
|
||||||
static LIST_ENTRY AdapterList;
|
static LIST_ENTRY AdapterList;
|
||||||
static WSADATA wsd;
|
static WSADATA wsd;
|
||||||
|
|
||||||
|
@ -209,6 +209,7 @@ DWORD WINAPI AdapterDiscoveryThread(LPVOID Context) {
|
||||||
PDHCP_ADAPTER Adapter = NULL;
|
PDHCP_ADAPTER Adapter = NULL;
|
||||||
HANDLE AdapterStateChangedEvent = (HANDLE)Context;
|
HANDLE AdapterStateChangedEvent = (HANDLE)Context;
|
||||||
struct interface_info *ifi = NULL;
|
struct interface_info *ifi = NULL;
|
||||||
|
struct protocol *proto;
|
||||||
int i, AdapterCount = 0, Broadcast;
|
int i, AdapterCount = 0, Broadcast;
|
||||||
|
|
||||||
/* FIXME: Kill this thread when the service is stopped */
|
/* FIXME: Kill this thread when the service is stopped */
|
||||||
|
@ -245,6 +246,10 @@ DWORD WINAPI AdapterDiscoveryThread(LPVOID Context) {
|
||||||
/* We're still active so we stay in the list */
|
/* We're still active so we stay in the list */
|
||||||
ifi = &Adapter->DhclientInfo;
|
ifi = &Adapter->DhclientInfo;
|
||||||
} else {
|
} else {
|
||||||
|
proto = find_protocol_by_adapter(&Adapter->DhclientInfo);
|
||||||
|
if (proto)
|
||||||
|
remove_protocol(proto);
|
||||||
|
|
||||||
/* We've lost our link so out we go */
|
/* We've lost our link so out we go */
|
||||||
RemoveEntryList(&Adapter->ListEntry);
|
RemoveEntryList(&Adapter->ListEntry);
|
||||||
free(Adapter);
|
free(Adapter);
|
||||||
|
|
|
@ -14,6 +14,8 @@
|
||||||
|
|
||||||
static CRITICAL_SECTION ApiCriticalSection;
|
static CRITICAL_SECTION ApiCriticalSection;
|
||||||
|
|
||||||
|
extern HANDLE AdapterStateChangedEvent;
|
||||||
|
|
||||||
VOID ApiInit() {
|
VOID ApiInit() {
|
||||||
InitializeCriticalSection( &ApiCriticalSection );
|
InitializeCriticalSection( &ApiCriticalSection );
|
||||||
}
|
}
|
||||||
|
@ -35,6 +37,7 @@ VOID ApiFree() {
|
||||||
DWORD DSLeaseIpAddress( PipeSendFunc Send, COMM_DHCP_REQ *Req ) {
|
DWORD DSLeaseIpAddress( PipeSendFunc Send, COMM_DHCP_REQ *Req ) {
|
||||||
COMM_DHCP_REPLY Reply;
|
COMM_DHCP_REPLY Reply;
|
||||||
PDHCP_ADAPTER Adapter;
|
PDHCP_ADAPTER Adapter;
|
||||||
|
struct protocol* proto;
|
||||||
|
|
||||||
ApiLock();
|
ApiLock();
|
||||||
|
|
||||||
|
@ -43,11 +46,19 @@ DWORD DSLeaseIpAddress( PipeSendFunc Send, COMM_DHCP_REQ *Req ) {
|
||||||
Reply.Reply = Adapter ? 1 : 0;
|
Reply.Reply = Adapter ? 1 : 0;
|
||||||
|
|
||||||
if( Adapter ) {
|
if( Adapter ) {
|
||||||
|
proto = find_protocol_by_adapter( &Adapter->DhclientInfo );
|
||||||
|
if (proto)
|
||||||
|
remove_protocol(proto);
|
||||||
|
|
||||||
add_protocol( Adapter->DhclientInfo.name,
|
add_protocol( Adapter->DhclientInfo.name,
|
||||||
Adapter->DhclientInfo.rfdesc, got_one,
|
Adapter->DhclientInfo.rfdesc, got_one,
|
||||||
&Adapter->DhclientInfo );
|
&Adapter->DhclientInfo );
|
||||||
|
|
||||||
Adapter->DhclientInfo.client->state = S_INIT;
|
Adapter->DhclientInfo.client->state = S_INIT;
|
||||||
state_reboot(&Adapter->DhclientInfo);
|
state_reboot(&Adapter->DhclientInfo);
|
||||||
|
|
||||||
|
if (AdapterStateChangedEvent != NULL)
|
||||||
|
SetEvent(AdapterStateChangedEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
ApiUnlock();
|
ApiUnlock();
|
||||||
|
@ -95,6 +106,12 @@ DWORD DSReleaseIpAddressLease( PipeSendFunc Send, COMM_DHCP_REQ *Req ) {
|
||||||
proto = find_protocol_by_adapter( &Adapter->DhclientInfo );
|
proto = find_protocol_by_adapter( &Adapter->DhclientInfo );
|
||||||
if (proto)
|
if (proto)
|
||||||
remove_protocol(proto);
|
remove_protocol(proto);
|
||||||
|
|
||||||
|
Adapter->DhclientInfo.client->active = NULL;
|
||||||
|
Adapter->DhclientInfo.client->state = S_INIT;
|
||||||
|
|
||||||
|
if (AdapterStateChangedEvent != NULL)
|
||||||
|
SetEvent(AdapterStateChangedEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
ApiUnlock();
|
ApiUnlock();
|
||||||
|
@ -105,6 +122,7 @@ DWORD DSReleaseIpAddressLease( PipeSendFunc Send, COMM_DHCP_REQ *Req ) {
|
||||||
DWORD DSRenewIpAddressLease( PipeSendFunc Send, COMM_DHCP_REQ *Req ) {
|
DWORD DSRenewIpAddressLease( PipeSendFunc Send, COMM_DHCP_REQ *Req ) {
|
||||||
COMM_DHCP_REPLY Reply;
|
COMM_DHCP_REPLY Reply;
|
||||||
PDHCP_ADAPTER Adapter;
|
PDHCP_ADAPTER Adapter;
|
||||||
|
struct protocol* proto;
|
||||||
|
|
||||||
ApiLock();
|
ApiLock();
|
||||||
|
|
||||||
|
@ -118,12 +136,20 @@ DWORD DSRenewIpAddressLease( PipeSendFunc Send, COMM_DHCP_REQ *Req ) {
|
||||||
|
|
||||||
Reply.Reply = 1;
|
Reply.Reply = 1;
|
||||||
|
|
||||||
|
proto = find_protocol_by_adapter( &Adapter->DhclientInfo );
|
||||||
|
if (proto)
|
||||||
|
remove_protocol(proto);
|
||||||
|
|
||||||
add_protocol( Adapter->DhclientInfo.name,
|
add_protocol( Adapter->DhclientInfo.name,
|
||||||
Adapter->DhclientInfo.rfdesc, got_one,
|
Adapter->DhclientInfo.rfdesc, got_one,
|
||||||
&Adapter->DhclientInfo );
|
&Adapter->DhclientInfo );
|
||||||
|
|
||||||
Adapter->DhclientInfo.client->state = S_INIT;
|
Adapter->DhclientInfo.client->state = S_INIT;
|
||||||
state_reboot(&Adapter->DhclientInfo);
|
state_reboot(&Adapter->DhclientInfo);
|
||||||
|
|
||||||
|
if (AdapterStateChangedEvent != NULL)
|
||||||
|
SetEvent(AdapterStateChangedEvent);
|
||||||
|
|
||||||
ApiUnlock();
|
ApiUnlock();
|
||||||
|
|
||||||
return Send( &Reply );
|
return Send( &Reply );
|
||||||
|
@ -154,6 +180,9 @@ DWORD DSStaticRefreshParams( PipeSendFunc Send, COMM_DHCP_REQ *Req ) {
|
||||||
&Adapter->NteContext,
|
&Adapter->NteContext,
|
||||||
&Adapter->NteInstance );
|
&Adapter->NteInstance );
|
||||||
Reply.Reply = NT_SUCCESS(Status);
|
Reply.Reply = NT_SUCCESS(Status);
|
||||||
|
|
||||||
|
if (AdapterStateChangedEvent != NULL)
|
||||||
|
SetEvent(AdapterStateChangedEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
ApiUnlock();
|
ApiUnlock();
|
||||||
|
|
|
@ -47,6 +47,8 @@
|
||||||
//#include <ifaddrs.h>
|
//#include <ifaddrs.h>
|
||||||
//#include <poll.h>
|
//#include <poll.h>
|
||||||
|
|
||||||
|
extern SOCKET DhcpSocket;
|
||||||
|
HANDLE AdapterStateChangedEvent = NULL;
|
||||||
struct protocol *protocols = NULL;
|
struct protocol *protocols = NULL;
|
||||||
struct timeout *timeouts = NULL;
|
struct timeout *timeouts = NULL;
|
||||||
static struct timeout *free_timeouts = NULL;
|
static struct timeout *free_timeouts = NULL;
|
||||||
|
@ -63,16 +65,18 @@ void (*bootp_packet_handler)(struct interface_info *,
|
||||||
void
|
void
|
||||||
dispatch(void)
|
dispatch(void)
|
||||||
{
|
{
|
||||||
int count, to_msec, err;
|
int count, to_msec;
|
||||||
struct protocol *l;
|
struct protocol *l;
|
||||||
fd_set fds;
|
|
||||||
time_t howlong, cur_time;
|
time_t howlong, cur_time;
|
||||||
struct timeval timeval;
|
HANDLE Events[2];
|
||||||
HANDLE AdapterStateChangedEvent;
|
int EventCount = 1;
|
||||||
|
|
||||||
AdapterStateChangedEvent = StartAdapterDiscovery();
|
Events[0] = StartAdapterDiscovery();
|
||||||
if (!AdapterStateChangedEvent)
|
if (!Events[0])
|
||||||
return;
|
return;
|
||||||
|
AdapterStateChangedEvent = Events[0];
|
||||||
|
|
||||||
|
Events[1] = WSA_INVALID_EVENT;
|
||||||
|
|
||||||
ApiLock();
|
ApiLock();
|
||||||
|
|
||||||
|
@ -83,7 +87,8 @@ dispatch(void)
|
||||||
*/
|
*/
|
||||||
time(&cur_time);
|
time(&cur_time);
|
||||||
|
|
||||||
if (timeouts) {
|
if (timeouts)
|
||||||
|
{
|
||||||
struct timeout *t;
|
struct timeout *t;
|
||||||
|
|
||||||
if (timeouts->when <= cur_time) {
|
if (timeouts->when <= cur_time) {
|
||||||
|
@ -105,55 +110,75 @@ dispatch(void)
|
||||||
if (howlong > INT_MAX / 1000)
|
if (howlong > INT_MAX / 1000)
|
||||||
howlong = INT_MAX / 1000;
|
howlong = INT_MAX / 1000;
|
||||||
to_msec = howlong * 1000;
|
to_msec = howlong * 1000;
|
||||||
|
|
||||||
/* Set up the descriptors to be polled. */
|
|
||||||
FD_ZERO(&fds);
|
|
||||||
|
|
||||||
for (l = protocols; l; l = l->next)
|
|
||||||
FD_SET(l->fd, &fds);
|
|
||||||
|
|
||||||
/* Wait for a packet or a timeout... XXX */
|
|
||||||
timeval.tv_sec = to_msec / 1000;
|
|
||||||
timeval.tv_usec = to_msec % 1000;
|
|
||||||
|
|
||||||
ApiUnlock();
|
|
||||||
|
|
||||||
count = select(0, &fds, NULL, NULL, &timeval);
|
|
||||||
|
|
||||||
ApiLock();
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ApiUnlock();
|
to_msec = INFINITE;
|
||||||
WaitForSingleObject(AdapterStateChangedEvent, INFINITE);
|
|
||||||
ApiLock();
|
|
||||||
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DH_DbgPrint(MID_TRACE,("Select: %d\n", count));
|
if (Events[1] == WSA_INVALID_EVENT && DhcpSocket != INVALID_SOCKET)
|
||||||
|
{
|
||||||
|
Events[1] = WSACreateEvent();
|
||||||
|
if (Events[1] != WSA_INVALID_EVENT)
|
||||||
|
{
|
||||||
|
count = WSAEventSelect(DhcpSocket, Events[1], FD_READ | FD_CLOSE);
|
||||||
|
if (count != NO_ERROR)
|
||||||
|
{
|
||||||
|
WSACloseEvent(Events[1]);
|
||||||
|
Events[1] = WSA_INVALID_EVENT;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
EventCount = 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (Events[1] != WSA_INVALID_EVENT && DhcpSocket == INVALID_SOCKET)
|
||||||
|
{
|
||||||
|
WSACloseEvent(Events[1]);
|
||||||
|
Events[1] = WSA_INVALID_EVENT;
|
||||||
|
|
||||||
/* Not likely to be transitory... */
|
EventCount = 1;
|
||||||
if (count == SOCKET_ERROR) {
|
}
|
||||||
err = WSAGetLastError();
|
|
||||||
error("poll: %d", err);
|
ApiUnlock();
|
||||||
break;
|
count = WaitForMultipleObjects(EventCount,
|
||||||
|
Events,
|
||||||
|
FALSE,
|
||||||
|
to_msec);
|
||||||
|
ApiLock();
|
||||||
|
if (count == WAIT_OBJECT_0)
|
||||||
|
{
|
||||||
|
/* Adapter state change */
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else if (count == WAIT_OBJECT_0 + 1)
|
||||||
|
{
|
||||||
|
/* Packet received */
|
||||||
|
|
||||||
|
/* WSA events are manual reset events */
|
||||||
|
WSAResetEvent(Events[1]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Timeout */
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (l = protocols; l; l = l->next) {
|
for (l = protocols; l; l = l->next) {
|
||||||
struct interface_info *ip;
|
struct interface_info *ip;
|
||||||
ip = l->local;
|
ip = l->local;
|
||||||
if (FD_ISSET(l->fd, &fds)) {
|
|
||||||
if (ip && (l->handler != got_one ||
|
if (ip && (l->handler != got_one ||
|
||||||
!ip->dead)) {
|
!ip->dead)) {
|
||||||
DH_DbgPrint(MID_TRACE,("Handling %x\n", l));
|
DH_DbgPrint(MID_TRACE,("Handling %x\n", l));
|
||||||
(*(l->handler))(l);
|
(*(l->handler))(l);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} while (1);
|
} while (1);
|
||||||
|
|
||||||
CloseHandle(AdapterStateChangedEvent);
|
AdapterStateChangedEvent = NULL;
|
||||||
|
CloseHandle(Events[0]);
|
||||||
|
WSACloseEvent(Events[1]);
|
||||||
|
|
||||||
ApiUnlock();
|
ApiUnlock();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue