📄 btgw.cxx
字号:
break;
}
if (fSyntaxError)
break;
}
while (iswspace (*pVal))
++pVal;
if ((*pVal != '\0') && (*pVal != ';')) {
fSyntaxError = TRUE;
DEBUGMSG(1, (L"BLUETOOTH GATEWAY:: Syntax error near line %d, expected RASAUTH={NONE|PAP CHAP MSCHAP MSCHAPv2}" CRLF, line));
break;
}
} else if (wcsicmp (pKey, L"ENCRYPT") == 0) {
if (wcsnicmp (pVal, L"ON", 2) == 0) {
pConfig->fe = TRUE;
pVal += 2;
} else if (wcsnicmp (pVal, L"OFF", 3) == 0) {
pConfig->fe = FALSE;
pVal += 3;
} else {
fSyntaxError = TRUE;
RETAILMSG(1, (L"BLUETOOTH GATEWAY:: Syntax error near line %d, expected ENCRYPT={ON|OFF}" CRLF, line));
break;
}
while (iswspace (*pVal))
++pVal;
if ((*pVal != '\0') && (*pVal != ';')) {
fSyntaxError = TRUE;
RETAILMSG(1, (L"BLUETOOTH GATEWAY:: Syntax error near line %d, expected ENCRYPT={ON|OFF}" CRLF, line));
break;
}
} else {
fSyntaxError = TRUE;
RETAILMSG(1, (L"BLUETOOTH GATEWAY:: Syntax error near line %d, unrecognized token %s" CRLF, line, pKey));
break;
}
}
fclose (fp);
} else
RETAILMSG(1, (L"BLUETOOTH GATEWAY:: config file %s not found" CRLF, szFileName));
if (fSyntaxError) {
Free (pFileS);
Free (pFileP);
Free (pRegistry);
return FALSE;
}
ServerSetAuthentication (pConfig->uiRasAuth);
RETAILMSG(1, (L"BLUETOOTH GATEWAY:: Merging configuration : ports" CRLF));
Port *pP = pConfig->pPortList;
while (pP) {
pP->fOld = TRUE;
pP = pP->pNext;
}
while (pFileP) {
Port *pNext = pFileP->pNext;
Port *pExist = Get (pConfig->pPortList, pFileP->index);
if (pExist && (fa_old == pConfig->fa) && (fe_old == pConfig->fe) && (rasauth_old == pConfig->uiRasAuth) &&
((pExist->channel == pFileP->channel) || (pFileP->channel == 0))) {
RETAILMSG(1, (L"BLUETOOTH GATEWAY:: Port %d on channel %d already exists" CRLF, pFileP->index, pFileP->channel));
pExist->fOld = FALSE;
delete pFileP;
pFileP = pNext;
continue;
}
if (pExist) {
RETAILMSG(1, (L"BLUETOOTH GATEWAY:: Port %d on channel %d => channel %d %s %s" CRLF, pFileP->index, pExist->channel, pFileP->channel, pConfig->fa ? L"AUTH" : L"", pConfig->fe ? L"ENCRYPT" : L""));
ClosePort (pExist);
pExist->channel = pFileP->channel;
pExist->fOld = FALSE;
delete pFileP;
pFileP = pNext;
OpenPort (pExist, pConfig->fa, pConfig->fe, pConfig->uiRasAuth);
continue;
}
pFileP->pNext = pConfig->pPortList;
pConfig->pPortList = pFileP;
OpenPort (pFileP, pConfig->fa, pConfig->fe, pConfig->uiRasAuth);
pFileP = pNext;
}
Port *pParentP = NULL;
pP = pConfig->pPortList;
while (pP) {
if (pP->fOld) {
RETAILMSG(1, (L"BLUETOOTH GATEWAY:: Port %d on channel %d is closed" CRLF, pP->index, pP->channel));
if (! pParentP)
pConfig->pPortList = pP->pNext;
else
pParentP->pNext = pP->pNext;
ClosePort (pP);
delete pP;
pP = pParentP ? pParentP->pNext : pConfig->pPortList;
continue;
}
pParentP = pP;
pP = pP->pNext;
}
RETAILMSG(1, (L"BLUETOOTH GATEWAY:: Merging configuration : security" CRLF));
Security *pS = pConfig->pSecurityList;
while (pS) {
pS->fDeleteKEY = TRUE;
pS->fDeletePIN = TRUE;
pS = pS->pNext;
}
// New = file + registry - old...
while (pRegistry) {
Security *pNext = pRegistry->pNext;
Security *pExist = Get (pConfig->pSecurityList, pRegistry->bt);
Security *pFileSec = Get (pFileS, pRegistry->bt);
if (pFileSec) // Registry ALWAYS superceedes file for link keys
pFileSec->cKey = 0;
if (pExist && (pRegistry->cKey == 16)) {
if ((pExist->acKey == 0) || memcmp (pExist->acKey, pRegistry->acKey, 16))
RETAILMSG(1, (L"BLUETOOTH GATEWAY:: KEY for %04x%08x is updated" CRLF, GET_NAP(pRegistry->bt), GET_SAP(pRegistry->bt)));
pExist->cKey = 16;
memcpy (pExist->acKey, pRegistry->acKey, 16);
delete pRegistry;
pRegistry = pNext;
continue;
}
RETAILMSG(1, (L"BLUETOOTH GATEWAY:: New KEY for %04x%08x" CRLF, GET_NAP(pRegistry->bt), GET_SAP(pRegistry->bt)));
pRegistry->pNext = pConfig->pSecurityList;
pConfig->pSecurityList = pRegistry;
pRegistry = pNext;
}
while (pFileS) {
Security *pNext = pFileS->pNext;
Security *pExist = Get (pConfig->pSecurityList, pFileS->bt);
if (pExist) {
if (pFileS->cKey) {
pExist->fDeleteKEY = FALSE;
if (memcmp (pFileS->acKey, pExist->acKey, 16)) {
RETAILMSG(1, (L"BLUETOOTH GATEWAY:: KEY for %04x%08x is updated" CRLF, GET_NAP(pFileS->bt), GET_SAP(pFileS->bt)));
pExist->cKey = 16;
memcpy (pExist->acKey, pFileS->acKey, 16);
BthSetLinkKey (&pExist->bt, pExist->acKey);
}
}
if (pFileS->cPin) {
pExist->fDeletePIN = FALSE;
if ((pFileS->cPin != pExist->cPin) || memcmp (pFileS->acPin, pExist->acPin, 16)) {
RETAILMSG(1, (L"BLUETOOTH GATEWAY:: PIN for %04x%08x is updated" CRLF, GET_NAP(pFileS->bt), GET_SAP(pFileS->bt)));
pExist->cPin = pFileS->cPin;
memcpy (pExist->acPin, pFileS->acPin, pFileS->cPin);
BthSetPIN (&pExist->bt, pExist->cPin, pExist->acPin);
}
}
delete pFileS;
pFileS = pNext;
continue;
}
if (pFileS->cKey) {
RETAILMSG(1, (L"BLUETOOTH GATEWAY:: New KEY for %04x%08x" CRLF, GET_NAP(pFileS->bt), GET_SAP(pFileS->bt)));
BthSetLinkKey (&pFileS->bt, pFileS->acKey);
}
if (pFileS->cPin) {
RETAILMSG(1, (L"BLUETOOTH GATEWAY:: New PIN for %04x%08x" CRLF, GET_NAP(pFileS->bt), GET_SAP(pFileS->bt)));
BthSetPIN (&pFileS->bt, pFileS->cPin, pFileS->acPin);
}
pFileS->pNext = pConfig->pSecurityList;
pConfig->pSecurityList = pFileS;
pFileS = pNext;
}
Security *pParentS = NULL;
pS = pConfig->pSecurityList;
while (pS) {
if (pS->fDeletePIN) {
RETAILMSG(1, (L"BLUETOOTH GATEWAY:: PIN for %04x%08x is retired" CRLF, GET_NAP(pS->bt), GET_SAP(pS->bt)));
if (pS->cPin)
BthRevokePIN (&pS->bt);
pS->cPin = 0;
pS->fDeletePIN = FALSE;
}
if (pS->fDeleteKEY) {
RETAILMSG(1, (L"BLUETOOTH GATEWAY:: KEY for %04x%08x is retired" CRLF, GET_NAP(pS->bt), GET_SAP(pS->bt)));
if (pS->cKey)
BthRevokeLinkKey (&pS->bt);
pS->cKey = 0;
pS->fDeleteKEY = FALSE;
}
if ((pS->cKey == 0) && (pS->cPin == 0)) {
if (! pParentS)
pConfig->pSecurityList = pS->pNext;
else
pParentS->pNext = pS->pNext;
delete pS;
pS = pParentS ? pParentS->pNext : pConfig->pSecurityList;
continue;
}
pParentS = pS;
pS = pS->pNext;
}
return TRUE;
}
static Config *ReadConfig (void) {
Config *pConfig = new Config;
WCHAR szFileName[_MAX_PATH];
GetPersistentName (szFileName, _MAX_PATH);
Parse (szFileName, pConfig);
return pConfig;
}
static void WriteByteSequence (FILE *fp, int c, unsigned char *p) {
while (c > 0) {
fputc (hxd((*p) >> 4), fp);
fputc (hxd((*p) & 0xf), fp);
fputwc (L' ', fp);
++p;
--c;
}
fputc ('\n', fp);
}
static void Write (Config *pConfig) {
WCHAR szFileName[_MAX_PATH];
GetPersistentName (szFileName, _MAX_PATH);
FILE *fp = _wfopen (szFileName, L"w");
if (! fp) {
RETAILMSG(1, (L"BLUETOOTH GATEWAY:: Could not save configuration: failed to open %s! Error = %d" CRLF, szFileName, GetLastError ()));
return;
}
PrintDefaultHeader (fp, L"");
fwprintf (fp, L"AUTH=%s\t\t; Legal values are ON or OFF\n", pConfig->fa ? L"ON":L"OFF");
fwprintf (fp, L"ENCRYPT=%s\t\t; Legal values are ON or OFF\n", pConfig->fe ? L"ON":L"OFF");
if (pConfig->uiRasAuth) {
fwprintf (fp, L"RASAUTH=");
if ((pConfig->uiRasAuth & RASEO_ProhibitPAP) == 0)
fwprintf (fp, L"PAP ");
if ((pConfig->uiRasAuth & RASEO_ProhibitCHAP) == 0)
fwprintf (fp, L"CHAP ");
if ((pConfig->uiRasAuth & RASEO_ProhibitMsCHAP) == 0)
fwprintf (fp, L"MSCHAP ");
if ((pConfig->uiRasAuth & RASEO_ProhibitMsCHAP2) == 0)
fwprintf (fp, L"MSCHAPv2 ");
fwprintf (fp, L"\t\t; Legal values are NONE or a list of any of PAP CHAP MSCHAP MSCHAPv2\n");
}
Port *pP = pConfig->pPortList;
while (pP) {
fwprintf (fp, L"; configure COM%d:\n", pP->index);
WCHAR szChan[20];
WCHAR szNdx2[20];
szChan[0] = '\0';
szNdx2[0] = '\0';
if (pP->channel)
wsprintf (szChan, L":%d", pP->channel);
if (pP->fModem)
wsprintf (szNdx2, L",%d", pP->index2);
fwprintf (fp, L"PORT=%s,%d%s%s\n", pP->szDevName, pP->index, szChan, szNdx2);
pP = pP->pNext;
}
Security *pS = pConfig->pSecurityList;
while (pS) {
if (pS->cPin) {
fwprintf (fp, L"; set PIN code for device %04x%08x\n", GET_NAP(pS->bt), GET_SAP(pS->bt));
fwprintf (fp, L"PIN%04x%08x=", GET_NAP(pS->bt), GET_SAP(pS->bt));
WriteByteSequence (fp, pS->cPin, pS->acPin);
}
if (pS->cKey) {
fwprintf (fp, L"; set link key for device %04x%08x\n", GET_NAP(pS->bt), GET_SAP(pS->bt));
fwprintf (fp, L"KEY%04x%08x=", GET_NAP(pS->bt), GET_SAP(pS->bt));
WriteByteSequence (fp, pS->cKey, pS->acKey);
}
pS = pS->pNext;
}
fclose (fp);
RETAILMSG(1, (L"BLUETOOTH GATEWAY:: configuration saved to file %s" CRLF, szFileName));
}
static void UpdateConfig (Config *pConfig) {
WCHAR szFileName[_MAX_PATH];
GetPersistentName (szFileName, _MAX_PATH);
if (Parse (szFileName, pConfig))
Write (pConfig);
}
//
// main
//
//
int wmain (int argc, WCHAR **argv) {
RETAILMSG(1, (CRLF CRLF CRLF CRLF L"Welcome to Windows CE Bluetooth gateway" CRLF CRLF CRLF CRLF));
int fSleep = TRUE;
HANDLE hExit = CreateEvent (NULL, FALSE, FALSE, L"system/events/bluetooth/gateway/exit");
if (! hExit) {
RETAILMSG(1, (L"BLUETOOTH GATEWAY:: Could not create event! Error = %d" CRLF, GetLastError ()));
return 0;
}
for (int i = 1 ; i < argc ; ++i) {
if (wcsicmp (argv[i], L"-nosleep") == 0)
fSleep = FALSE;
else
RETAILMSG(1, (L"BLUETOOTH GATEWAY:: unknown parameter %s" CRLF, argv[i]));
}
if (fSleep) {
for (i = 0 ; i < GW_SLEEP ; ++i) {
RETAILMSG(1, (L"BLUETOOTH GATEWAY:: Sleeping for 1000 ms (%d)" CRLF, GW_SLEEP - i));
Sleep (1000);
}
}
if (SetGlobalPPPConfig() != 0) {
CloseHandle (hExit);
RETAILMSG(1, (L"BLUETOOTH GATEWAY:: Initialization of PPP server failed!" CRLF));
return 0;
}
WSADATA wsd;
if (WSAStartup (MAKEWORD(1,0), &wsd)) {
CloseHandle (hExit);
RETAILMSG(1, (L"BLUETOOTH GATEWAY:: Initialization of socket subsystem failed! Error = %d" CRLF, WSAGetLastError ()));
return 0;
}
HANDLE hEvent = CreateEvent (NULL, FALSE, FALSE, L"system/events/bluetooth/gateway/refresh");
if (! hEvent) {
CloseHandle (hExit);
RETAILMSG(1, (L"BLUETOOTH GATEWAY:: Could not create event! Error = %d" CRLF, GetLastError ()));
return 0;
}
HANDLE hSecurity = CreateEvent (NULL, TRUE, FALSE, BTH_NAMEDEVENT_PAIRING_CHANGED);
HANDLE hWaitArray[3];
hWaitArray[0] = hEvent;
hWaitArray[1] = hExit;
hWaitArray[2] = hSecurity;
DWORD cWaitEvents = hSecurity ? 3 : 2;
Config *pConfig = ReadConfig ();
DisplayPPP ();
for ( ; ; ) {
DWORD dwRes = WaitForMultipleObjects (cWaitEvents, hWaitArray, FALSE, INFINITE);
if ((dwRes == WAIT_OBJECT_0) || (dwRes == (WAIT_OBJECT_0 + 2))) { // Refresh config
UpdateConfig (pConfig);
DisplayPPP ();
continue;
}
if (dwRes != (WAIT_OBJECT_0 + 1))
RETAILMSG(1, (L"BLUETOOTH GATEWAY:: fatal error %d" CRLF, GetLastError ()));
break;
}
RETAILMSG(1, (CRLF CRLF CRLF CRLF L"BLUETOOTH GATEWAY:: cleaning up and exiting..." CRLF CRLF CRLF CRLF));
while (pConfig->pPortList) {
Port *pThis = pConfig->pPortList;
pConfig->pPortList = pConfig->pPortList->pNext;
ClosePort (pThis);
delete pThis;
}
while (pConfig->pSecurityList) {
Security *pThis = pConfig->pSecurityList;
pConfig->pSecurityList = pConfig->pSecurityList->pNext;
if (pThis->cPin)
BthRevokePIN (&pThis->bt);
delete pThis;
}
delete pConfig;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -