📄 s4n_service.cpp
字号:
/*
* Copyright (C) 2003-2007 Funambol, Inc
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY, TITLE, NONINFRINGEMENT 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
*/
// service.cpp : register and deregister notlstnr service.
//
#include <windows.h>
#include <service.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include "pim/utils.h"
#include "pim/SettingFunctions.h"
#include "notify/s4n_service.h"
#include "base/Log.h"
#include "pim/ClientSettings.h"
#define SERVICE_PREFIX TEXT("S4N")
#define SERVICE_DLL TEXT("notlstnr.dll")
#define SERVICE_PREFIX_INDEX TEXT("S4N0:")
#define SERVICE_INDEX 0
#define SERVICE_PORT 4745
typedef HANDLE (*PFN_REGISTER_SERVICE)(
LPCWSTR lpszType,
DWORD dwIndex,
LPCWSTR lpszLib,
DWORD dwInfo
);
typedef BOOL (*PFN_DEREGISTER_SERVICE) (
HANDLE hDevice
);
typedef BOOL (*PFN_SERVICE_IO_CONTROL) (
HANDLE hService,
DWORD dwIoControlCode,
LPVOID lpInBuf,
DWORD nInBufSize,
LPVOID lpOutBuf,
DWORD nOutBufSize,
LPDWORD lpBytesReturned,
LPOVERLAPPED lpOverlapped
);
typedef BOOL (*PFN_SERVICE_ADD_PORT) (
HANDLE hService,
SOCKADDR* pSockAddr,
INT cbSockAddr,
INT iProtocol,
WCHAR szRegWritePath
);
typedef HANDLE (*PFN_GET_SERVICE_HANDLE) (
LPWSTR szPrefix,
LPWSTR szDllName,
DWORD pdwDllBuf
);
int getListenerPort(void)
{
// ClientSettings cs(APPLICATION_URI);
// ClientSettings::getInstance()->readConfig();
string s = ClientSettings::getInstance()->getPushPort();
if (s == "")
return SERVICE_PORT;
return atoi(s.c_str());
/*
wchar_t buf[32];
memset(buf, 0, 32*sizeof(wchar_t));
getClientConfigurationInternal (TEXT(""), PROPERTY_SPDM_NOTIFICATION_PORT,
buf, FALSE);
if(buf[0] == 0)
return SERVICE_PORT;
return wcstol(buf, NULL, 10);
*/
}
int registerService() {
PFN_REGISTER_SERVICE pfnRegister = NULL;
PFN_SERVICE_IO_CONTROL pfnIOControl = NULL;
PFN_SERVICE_ADD_PORT pfnAddPort = NULL;
SOCKADDR_IN SockAddr;
HANDLE s = 0;
int port = 0;
int ret = 0;
HMODULE h = LoadLibrary(L"COREDLL.dll");
if (h == NULL) {
LOG.error("registerService: library not found");
goto finally;
}
pfnRegister =
(PFN_REGISTER_SERVICE)GetProcAddress(h, L"RegisterService");
if (pfnRegister == NULL) {
LOG.error("registerService: function RegisterService not found");
goto finally;
}
pfnIOControl =
(PFN_SERVICE_IO_CONTROL)GetProcAddress(h, L"ServiceIoControl");
if (pfnIOControl == NULL) {
LOG.error("registerService: function ServiceIoControl not found");
goto finally;
}
pfnAddPort =
(PFN_SERVICE_ADD_PORT)GetProcAddress(h, L"ServiceAddPort");
if (pfnAddPort == NULL) {
LOG.error("registerService: function ServiceAddPort not found");
goto finally;
}
port = getListenerPort();
s = (pfnRegister)(SERVICE_PREFIX, SERVICE_INDEX, SERVICE_DLL, port);
if (s == NULL) {
ret = GetLastError();
if (ret == 120) {
LOG.debug("Service already registered");
}
else {
LOG.debug("registerService: registering error (%d)", ret);
}
goto finally;
}
//
// Now we register the super service...
//
memset(&SockAddr,0,sizeof(SockAddr));
if(!(pfnIOControl)(s, IOCTL_SERVICE_REGISTER_SOCKADDR,0,0,0,0,0,0)) {
LOG.error("registerService: the service does not support socked address registering");
goto finally; //does not support super services
}
SockAddr.sin_family = AF_INET;
SockAddr.sin_port=htons(port);
if (! (pfnAddPort)(s,(SOCKADDR*)&SockAddr, sizeof(SockAddr), 0,0)) {
LOG.error("registerService: I cannot listen on port %d", port);
goto finally;
}
//When all sockets have been bound, the service must be informed that it
//is ready to start.
if(!(pfnIOControl)(s, IOCTL_SERVICE_STARTED,0,0,0,0,0,0)) {
LOG.error("registerService: the service could not start....");
goto finally; //if the service cannot start
}
LOG.debug("Service sucessfully registerd");
finally:
if (h) {
FreeLibrary(h);
}
return ret;
}
int deregisterService() {
HMODULE h = NULL;
HANDLE s = NULL; // service handle
int ret = 0;
PFN_GET_SERVICE_HANDLE pfnGetHandle = NULL;
PFN_DEREGISTER_SERVICE pfnDeregister = NULL;
h = LoadLibrary(L"COREDLL.dll");
if (h == NULL) {
LOG.error("deregisterService: library not found");
goto finally;
}
pfnDeregister =
(PFN_DEREGISTER_SERVICE)GetProcAddress(h, L"DeregisterService");
if (pfnDeregister == NULL) {
LOG.error("deregisterService: function DeregisterService not found");
goto finally;
}
pfnGetHandle =
(PFN_GET_SERVICE_HANDLE)GetProcAddress(h, L"GetServiceHandle");
if (pfnGetHandle == NULL) {
LOG.error("deregisterService: function GetServiceHandle not found");
goto finally;
}
s = (pfnGetHandle)(SERVICE_PREFIX_INDEX, NULL, 0);
if (s == INVALID_HANDLE_VALUE) {
LOG.error("deregisterService: Service not found");
ret = GetLastError();
goto finally;
}
if ((pfnDeregister)(s) == 0) {
ret = GetLastError();
LOG.error("Deregistering error: %ld", ret);
goto finally;
}
s = NULL;
LOG.debug("Service sucessfully deregisterd");
finally:
if (h) {
FreeLibrary(h);
}
return ret;
}
void activateService() {
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -