[DHCPCSVC]

- Code formatting
- Move ROS service functionalities from BSD module to where it really belongs.

svn path=/trunk/; revision=64291
This commit is contained in:
Hermès Bélusca-Maïto 2014-09-25 22:01:42 +00:00
parent 52858fdcec
commit 12afcc85bd
3 changed files with 214 additions and 183 deletions

View file

@ -55,8 +55,6 @@
#include <rosdhcp.h> #include <rosdhcp.h>
#include <winsvc.h>
#define PERIOD 0x2e #define PERIOD 0x2e
#define hyphenchar(c) ((c) == 0x2d) #define hyphenchar(c) ((c) == 0x2d)
#define bslashchar(c) ((c) == 0x5c) #define bslashchar(c) ((c) == 0x5c)
@ -108,95 +106,10 @@ int check_arp( struct interface_info *ip, struct client_lease *lp )
time_t scripttime; time_t scripttime;
static WCHAR ServiceName[] = L"DHCP";
SERVICE_STATUS_HANDLE ServiceStatusHandle = 0; int
SERVICE_STATUS ServiceStatus; init_client(void)
/* XXX Implement me */
int check_arp( struct interface_info *ip, struct client_lease *lp ) {
return 1;
}
static VOID
UpdateServiceStatus(DWORD dwState)
{ {
ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
ServiceStatus.dwCurrentState = dwState;
ServiceStatus.dwControlsAccepted = 0;
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwServiceSpecificExitCode = 0;
ServiceStatus.dwCheckPoint = 0;
if (dwState == SERVICE_START_PENDING ||
dwState == SERVICE_STOP_PENDING ||
dwState == SERVICE_PAUSE_PENDING ||
dwState == SERVICE_CONTINUE_PENDING)
ServiceStatus.dwWaitHint = 10000;
else
ServiceStatus.dwWaitHint = 0;
SetServiceStatus(ServiceStatusHandle,
&ServiceStatus);
}
static DWORD WINAPI
ServiceControlHandler(DWORD dwControl,
DWORD dwEventType,
LPVOID lpEventData,
LPVOID lpContext)
{
switch (dwControl)
{
case SERVICE_CONTROL_STOP:
UpdateServiceStatus(SERVICE_STOP_PENDING);
UpdateServiceStatus(SERVICE_STOPPED);
return ERROR_SUCCESS;
case SERVICE_CONTROL_PAUSE:
UpdateServiceStatus(SERVICE_PAUSED);
return ERROR_SUCCESS;
case SERVICE_CONTROL_CONTINUE:
UpdateServiceStatus(SERVICE_START_PENDING);
UpdateServiceStatus(SERVICE_RUNNING);
return ERROR_SUCCESS;
case SERVICE_CONTROL_INTERROGATE:
SetServiceStatus(ServiceStatusHandle,
&ServiceStatus);
return ERROR_SUCCESS;
case SERVICE_CONTROL_SHUTDOWN:
UpdateServiceStatus(SERVICE_STOP_PENDING);
UpdateServiceStatus(SERVICE_STOPPED);
return ERROR_SUCCESS;
default :
return ERROR_CALL_NOT_IMPLEMENTED;
}
}
VOID NTAPI
ServiceMain(DWORD argc, LPWSTR *argv)
{
ServiceStatusHandle = RegisterServiceCtrlHandlerExW(ServiceName,
ServiceControlHandler,
NULL);
if (!ServiceStatusHandle)
{
DbgPrint("DHCPCSVC: Unable to register service control handler (%lx)\n", GetLastError());
return;
}
UpdateServiceStatus(SERVICE_START_PENDING);
ApiInit(); ApiInit();
AdapterInit(); AdapterInit();
@ -214,26 +127,23 @@ ServiceMain(DWORD argc, LPWSTR *argv)
DbgPrint("DHCPCSVC: PipeInit() failed!\n"); DbgPrint("DHCPCSVC: PipeInit() failed!\n");
AdapterStop(); AdapterStop();
ApiFree(); ApiFree();
UpdateServiceStatus(SERVICE_STOPPED); return 0; // FALSE
} }
DH_DbgPrint(MID_TRACE,("DHCP Service Started\n")); return 1; // TRUE
}
UpdateServiceStatus(SERVICE_RUNNING); void
stop_client(void)
DH_DbgPrint(MID_TRACE,("Going into dispatch()\n")); {
// AdapterStop();
DH_DbgPrint(MID_TRACE, ("DHCPCSVC: DHCP service is starting up\n")); // ApiFree();
dispatch();
DbgPrint("DHCPCSVC: DHCP service is shutting down\n");
//AdapterStop();
//ApiFree();
/* FIXME: Close pipe and kill pipe thread */ /* FIXME: Close pipe and kill pipe thread */
}
UpdateServiceStatus(SERVICE_STOPPED); /* XXX Implement me */
int check_arp( struct interface_info *ip, struct client_lease *lp ) {
return 1;
} }
/* /*

View file

@ -7,68 +7,76 @@
*/ */
#include <rosdhcp.h> #include <rosdhcp.h>
#include <winsvc.h>
#define NDEBUG #define NDEBUG
#include <debug.h> #include <debug.h>
static WCHAR ServiceName[] = L"DHCP";
SERVICE_STATUS_HANDLE ServiceStatusHandle = 0;
SERVICE_STATUS ServiceStatus;
static HANDLE PipeHandle = INVALID_HANDLE_VALUE; static HANDLE PipeHandle = INVALID_HANDLE_VALUE;
DWORD APIENTRY DhcpCApiInitialize(LPDWORD Version) { DWORD APIENTRY
DhcpCApiInitialize(LPDWORD Version)
{
DWORD PipeMode; DWORD PipeMode;
/* Wait for the pipe to be available */ /* Wait for the pipe to be available */
if (WaitNamedPipeW(DHCP_PIPE_NAME, NMPWAIT_USE_DEFAULT_WAIT)) if (!WaitNamedPipeW(DHCP_PIPE_NAME, NMPWAIT_USE_DEFAULT_WAIT))
{
/* It's available, let's try to open it */
PipeHandle = CreateFileW(DHCP_PIPE_NAME,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
/* Check if we succeeded in opening the pipe */
if (PipeHandle == INVALID_HANDLE_VALUE)
{
/* We didn't */
return GetLastError();
}
else
{
/* Change the pipe into message mode */
PipeMode = PIPE_READMODE_MESSAGE;
if (!SetNamedPipeHandleState(PipeHandle, &PipeMode, NULL, NULL))
{
/* Mode change failed */
CloseHandle(PipeHandle);
PipeHandle = INVALID_HANDLE_VALUE;
return GetLastError();
}
else
{
/* We're good to go */
*Version = 2;
return NO_ERROR;
}
}
}
else
{ {
/* No good, we failed */ /* No good, we failed */
return GetLastError(); return GetLastError();
} }
/* It's available, let's try to open it */
PipeHandle = CreateFileW(DHCP_PIPE_NAME,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
/* Check if we succeeded in opening the pipe */
if (PipeHandle == INVALID_HANDLE_VALUE)
{
/* We didn't */
return GetLastError();
}
/* Change the pipe into message mode */
PipeMode = PIPE_READMODE_MESSAGE;
if (!SetNamedPipeHandleState(PipeHandle, &PipeMode, NULL, NULL))
{
/* Mode change failed */
CloseHandle(PipeHandle);
PipeHandle = INVALID_HANDLE_VALUE;
return GetLastError();
}
else
{
/* We're good to go */
*Version = 2;
return NO_ERROR;
}
} }
VOID APIENTRY DhcpCApiCleanup() { VOID APIENTRY
DhcpCApiCleanup(VOID)
{
CloseHandle(PipeHandle); CloseHandle(PipeHandle);
PipeHandle = INVALID_HANDLE_VALUE; PipeHandle = INVALID_HANDLE_VALUE;
} }
DWORD APIENTRY DhcpQueryHWInfo( DWORD AdapterIndex, DWORD APIENTRY
PDWORD MediaType, DhcpQueryHWInfo(DWORD AdapterIndex,
PDWORD Mtu, PDWORD MediaType,
PDWORD Speed ) { PDWORD Mtu,
PDWORD Speed)
{
COMM_DHCP_REQ Req; COMM_DHCP_REQ Req;
COMM_DHCP_REPLY Reply; COMM_DHCP_REPLY Reply;
DWORD BytesRead; DWORD BytesRead;
@ -89,16 +97,18 @@ DWORD APIENTRY DhcpQueryHWInfo( DWORD AdapterIndex,
return 0; return 0;
} }
if( !Reply.Reply ) return 0; if (Reply.Reply == 0)
else { return 0;
*MediaType = Reply.QueryHWInfo.MediaType;
*Mtu = Reply.QueryHWInfo.Mtu; *MediaType = Reply.QueryHWInfo.MediaType;
*Speed = Reply.QueryHWInfo.Speed; *Mtu = Reply.QueryHWInfo.Mtu;
return 1; *Speed = Reply.QueryHWInfo.Speed;
} return 1;
} }
DWORD APIENTRY DhcpLeaseIpAddress( DWORD AdapterIndex ) { DWORD APIENTRY
DhcpLeaseIpAddress(DWORD AdapterIndex)
{
COMM_DHCP_REQ Req; COMM_DHCP_REQ Req;
COMM_DHCP_REPLY Reply; COMM_DHCP_REPLY Reply;
DWORD BytesRead; DWORD BytesRead;
@ -122,7 +132,9 @@ DWORD APIENTRY DhcpLeaseIpAddress( DWORD AdapterIndex ) {
return Reply.Reply; return Reply.Reply;
} }
DWORD APIENTRY DhcpReleaseIpAddressLease( DWORD AdapterIndex ) { DWORD APIENTRY
DhcpReleaseIpAddressLease(DWORD AdapterIndex)
{
COMM_DHCP_REQ Req; COMM_DHCP_REQ Req;
COMM_DHCP_REPLY Reply; COMM_DHCP_REPLY Reply;
DWORD BytesRead; DWORD BytesRead;
@ -146,7 +158,9 @@ DWORD APIENTRY DhcpReleaseIpAddressLease( DWORD AdapterIndex ) {
return Reply.Reply; return Reply.Reply;
} }
DWORD APIENTRY DhcpRenewIpAddressLease( DWORD AdapterIndex ) { DWORD APIENTRY
DhcpRenewIpAddressLease(DWORD AdapterIndex)
{
COMM_DHCP_REQ Req; COMM_DHCP_REQ Req;
COMM_DHCP_REPLY Reply; COMM_DHCP_REPLY Reply;
DWORD BytesRead; DWORD BytesRead;
@ -170,9 +184,11 @@ DWORD APIENTRY DhcpRenewIpAddressLease( DWORD AdapterIndex ) {
return Reply.Reply; return Reply.Reply;
} }
DWORD APIENTRY DhcpStaticRefreshParams( DWORD AdapterIndex, DWORD APIENTRY
DWORD Address, DhcpStaticRefreshParams(DWORD AdapterIndex,
DWORD Netmask ) { DWORD Address,
DWORD Netmask)
{
COMM_DHCP_REQ Req; COMM_DHCP_REQ Req;
COMM_DHCP_REPLY Reply; COMM_DHCP_REPLY Reply;
DWORD BytesRead; DWORD BytesRead;
@ -232,9 +248,9 @@ DhcpNotifyConfigChange(LPWSTR ServerName,
DWORD IpIndex, DWORD IpIndex,
DWORD IpAddress, DWORD IpAddress,
DWORD SubnetMask, DWORD SubnetMask,
int DhcpAction) INT DhcpAction)
{ {
DbgPrint("DHCPCSVC: DhcpNotifyConfigChange not implemented yet\n"); DPRINT1("DHCPCSVC: DhcpNotifyConfigChange not implemented yet\n");
return 0; return 0;
} }
@ -262,11 +278,12 @@ DhcpNotifyConfigChange(LPWSTR ServerName,
* *
* \remarks This is a ReactOS-only routine * \remarks This is a ReactOS-only routine
*/ */
DWORD APIENTRY DhcpRosGetAdapterInfo( DWORD AdapterIndex, DWORD APIENTRY
PBOOL DhcpEnabled, DhcpRosGetAdapterInfo(DWORD AdapterIndex,
PDWORD DhcpServer, PBOOL DhcpEnabled,
time_t *LeaseObtained, PDWORD DhcpServer,
time_t *LeaseExpires ) time_t* LeaseObtained,
time_t* LeaseExpires)
{ {
COMM_DHCP_REQ Req; COMM_DHCP_REQ Req;
COMM_DHCP_REPLY Reply; COMM_DHCP_REPLY Reply;
@ -283,16 +300,19 @@ DWORD APIENTRY DhcpRosGetAdapterInfo( DWORD AdapterIndex,
&Reply, sizeof(Reply), &Reply, sizeof(Reply),
&BytesRead, NULL); &BytesRead, NULL);
if ( 0 != Result && 0 != Reply.Reply ) { if (Result && Reply.Reply != 0)
*DhcpEnabled = Reply.GetAdapterInfo.DhcpEnabled; *DhcpEnabled = Reply.GetAdapterInfo.DhcpEnabled;
} else { else
*DhcpEnabled = FALSE; *DhcpEnabled = FALSE;
}
if ( *DhcpEnabled ) { if (*DhcpEnabled)
{
*DhcpServer = Reply.GetAdapterInfo.DhcpServer; *DhcpServer = Reply.GetAdapterInfo.DhcpServer;
*LeaseObtained = Reply.GetAdapterInfo.LeaseObtained; *LeaseObtained = Reply.GetAdapterInfo.LeaseObtained;
*LeaseExpires = Reply.GetAdapterInfo.LeaseExpires; *LeaseExpires = Reply.GetAdapterInfo.LeaseExpires;
} else { }
else
{
*DhcpServer = INADDR_NONE; *DhcpServer = INADDR_NONE;
*LeaseObtained = 0; *LeaseObtained = 0;
*LeaseExpires = 0; *LeaseExpires = 0;
@ -301,22 +321,120 @@ DWORD APIENTRY DhcpRosGetAdapterInfo( DWORD AdapterIndex,
return Reply.Reply; return Reply.Reply;
} }
static VOID
UpdateServiceStatus(DWORD dwState)
{
ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
ServiceStatus.dwCurrentState = dwState;
ServiceStatus.dwControlsAccepted = 0;
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwServiceSpecificExitCode = 0;
ServiceStatus.dwCheckPoint = 0;
if (dwState == SERVICE_START_PENDING ||
dwState == SERVICE_STOP_PENDING ||
dwState == SERVICE_PAUSE_PENDING ||
dwState == SERVICE_CONTINUE_PENDING)
ServiceStatus.dwWaitHint = 10000;
else
ServiceStatus.dwWaitHint = 0;
SetServiceStatus(ServiceStatusHandle,
&ServiceStatus);
}
static DWORD WINAPI
ServiceControlHandler(DWORD dwControl,
DWORD dwEventType,
LPVOID lpEventData,
LPVOID lpContext)
{
switch (dwControl)
{
case SERVICE_CONTROL_STOP:
UpdateServiceStatus(SERVICE_STOP_PENDING);
UpdateServiceStatus(SERVICE_STOPPED);
return ERROR_SUCCESS;
case SERVICE_CONTROL_PAUSE:
UpdateServiceStatus(SERVICE_PAUSED);
return ERROR_SUCCESS;
case SERVICE_CONTROL_CONTINUE:
UpdateServiceStatus(SERVICE_START_PENDING);
UpdateServiceStatus(SERVICE_RUNNING);
return ERROR_SUCCESS;
case SERVICE_CONTROL_INTERROGATE:
SetServiceStatus(ServiceStatusHandle,
&ServiceStatus);
return ERROR_SUCCESS;
case SERVICE_CONTROL_SHUTDOWN:
UpdateServiceStatus(SERVICE_STOP_PENDING);
UpdateServiceStatus(SERVICE_STOPPED);
return ERROR_SUCCESS;
default :
return ERROR_CALL_NOT_IMPLEMENTED;
}
}
VOID WINAPI
ServiceMain(DWORD argc, LPWSTR* argv)
{
ServiceStatusHandle = RegisterServiceCtrlHandlerExW(ServiceName,
ServiceControlHandler,
NULL);
if (!ServiceStatusHandle)
{
DPRINT1("DHCPCSVC: Unable to register service control handler (%lx)\n", GetLastError());
return;
}
UpdateServiceStatus(SERVICE_START_PENDING);
if (!init_client())
{
DPRINT1("DHCPCSVC: init_client() failed!\n");
UpdateServiceStatus(SERVICE_STOPPED);
return;
}
DH_DbgPrint(MID_TRACE,("DHCP Service Started\n"));
UpdateServiceStatus(SERVICE_RUNNING);
DH_DbgPrint(MID_TRACE,("Going into dispatch()\n"));
DH_DbgPrint(MID_TRACE,("DHCPCSVC: DHCP service is starting up\n"));
dispatch();
DH_DbgPrint(MID_TRACE,("DHCPCSVC: DHCP service is shutting down\n"));
stop_client();
UpdateServiceStatus(SERVICE_STOPPED);
}
INT WINAPI INT WINAPI
DllMain(PVOID hinstDll, DllMain(PVOID hinstDll,
ULONG dwReason, ULONG dwReason,
PVOID reserved) PVOID reserved)
{ {
switch (dwReason) switch (dwReason)
{ {
case DLL_PROCESS_ATTACH: case DLL_PROCESS_ATTACH:
DisableThreadLibraryCalls(hinstDll); DisableThreadLibraryCalls(hinstDll);
break; break;
case DLL_PROCESS_DETACH: case DLL_PROCESS_DETACH:
break; break;
} }
return TRUE; return TRUE;
} }
/* EOF */ /* EOF */

View file

@ -81,6 +81,9 @@ typedef DWORD (*PipeSendFunc)( COMM_DHCP_REPLY *Reply );
#define random rand #define random rand
#define srandom srand #define srandom srand
int init_client(void);
void stop_client(void);
void AdapterInit(VOID); void AdapterInit(VOID);
HANDLE StartAdapterDiscovery(VOID); HANDLE StartAdapterDiscovery(VOID);
void AdapterStop(VOID); void AdapterStop(VOID);