⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 btgw.cxx

📁 Windows CE操作系统中适用的蓝牙驱动程序
💻 CXX
📖 第 1 页 / 共 4 页
字号:
	fwprintf (fp, L";   PORT=name,number[:channel][,number] use COMx: port, use pre-defined channel\n");
	fwprintf (fp, L";              if given, otherwise auto-allocate\n");
	fwprintf (fp, L";              if second index is specified, use modem emulator on it\n");
	fwprintf (fp, L";   PINaddress=sequence of 1 to 16 double hex characters\n");
	fwprintf (fp, L";       e. g. PIN000101020304=30 31 32 32 34\n");
	fwprintf (fp, L";   KEYaddress=sequence of 16 double hex characters - same as PIN\n;\n");
}

static void MakeDefaultConfig (WCHAR *szName) {
	HKEY hk;
	int fReg = FALSE;
	WCHAR buffer[512];

	if (ERROR_SUCCESS == RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"software\\microsoft\\bluetooth\\gateway", 0, KEY_READ, &hk)) {
		DWORD dwType = 0;
		DWORD dwSize = sizeof(buffer);

		if (ERROR_SUCCESS == RegQueryValueEx (hk, L"config", NULL, &dwType, (BYTE *)buffer, &dwSize)) {
			RETAILMSG(1, (L"BLUETOOTH GATEWAY:: Found registry definition for default gateway config in %s" CRLF, szName));
			fReg = TRUE;
		}

		RegCloseKey (hk);
	}

	if (! fReg) {
		RETAILMSG(1, (L"BLUETOOTH GATEWAY:: Making default gateway config in %s" CRLF, szName));
		wcscpy (buffer, DEFAULT_CONFIG_FILE);
	}

	FILE *fp = _wfopen (szName, L"w");
	if (! fp) {
		RETAILMSG(1, (L"BLUETOOTH GATEWAY:: Could not create configuration: failed to open %s! Error = %d" CRLF, szName, GetLastError ()));
		return;
	}

	PrintDefaultHeader (fp, L"(default configuration)");

	fwprintf (fp, L"%s", buffer);

	fclose (fp);
}

static void GetPersistentName (WCHAR *szName, int ccName) {
	ASSERT (ccName > (CONFIG_NAME_SZ + 2));

	WIN32_FIND_DATA wfd;

	for (int i = 0 ; i < 2 ; ++i) {
		HANDLE hSearch = FindFirstFile (L"\\*.*", &wfd);
		if (hSearch != INVALID_HANDLE_VALUE) {
			do {
				if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
					(wfd.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY)) {
					int cSize = wcslen (wfd.cFileName) + 3 + CONFIG_NAME_SZ;
					if (cSize <= ccName) {
						szName[0] = L'\\';
						wcscpy (szName + 1, wfd.cFileName);
						wcscat (szName, L"\\" CONFIG_NAME);
						DWORD dwRes = GetFileAttributes (szName);
						if (((dwRes != 0xffffffff) && ((dwRes & FILE_ATTRIBUTE_DIRECTORY) == 0)) || // Found!
							 ((i == 1) && (dwRes == 0xffffffff))) {	// Found place for it!
							FindClose (hSearch);
							if ((i == 1) && (dwRes == 0xffffffff))
								MakeDefaultConfig (szName);

							return;
						}
					}
				}
			} while (FindNextFile (hSearch, &wfd));
			FindClose (hSearch);
		}
	}

	wcscpy (szName, L"\\" CONFIG_NAME);

	if (GetFileAttributes (szName) == 0xffffffff)
		MakeDefaultConfig (szName);

	return;
}

static Security *Get (Security *pList, BT_ADDR bt) {
	while (pList) {
		if (pList->bt == bt)
			return pList;
		pList = pList->pNext;
	}

	return NULL;
}

static Port *Get (Port *pList, int index) {
	while (pList) {
		if (pList->index == index)
			return pList;
		pList = pList->pNext;
	}

	return NULL;
}

static void Free (Security *pList) {
	while (pList) {
		Security *pNext = pList->pNext;
		delete pList;
		pList = pNext;
	}
}

static void Free (Port *pList) {
	while (pList) {
		Port *pNext = pList->pNext;
		delete pList;
		pList = pNext;
	}
}

static Security *GetRegistrySecurity (void) {
	HKEY hKey;

	Security *pResult = NULL;

	if (ERROR_SUCCESS != RegOpenKeyEx (HKEY_LOCAL_MACHINE, L"software\\microsoft\\bluetooth\\security", 0, KEY_READ, &hKey))
		return NULL;

	for (DWORD dwIndex = 0 ; ; ++dwIndex) {
		unsigned char link_key[16];
		DWORD clink_key = sizeof(link_key);

		WCHAR szName[32];
		DWORD cName = 32;

		DWORD dwType = 0;

		WCHAR *p = szName + 3;

		LONG res = RegEnumValue (hKey, dwIndex, szName, &cName, NULL, &dwType, link_key, &clink_key);
		if (res == ERROR_NO_MORE_ITEMS)
			break;

		BT_ADDR bt;

		if ((res == ERROR_SUCCESS) && (dwType == REG_BINARY) && (clink_key > 0) &&
				(clink_key < 17) && (cName == 13) &&
				(((wcsnicmp (szName, L"KEY", 3) == 0) && (clink_key == 16)) ||
				(wcsnicmp (szName, L"PIN", 3) == 0)) && GetBA(&p, &bt)) {
			Security *pNew = Get (pResult, bt);
			if (! pNew) {
				pNew = new Security;

				if (! pNew) {
					RETAILMSG (1, (L"BLUETOOTH GATEWAY:: OUT OF MEMORY! Data may be lost!\n"));
					break;
				}

				pNew->bt = bt;
				pNew->pNext = pResult;
				pResult = pNew;
			}

			if ((clink_key == 16) && (wcsnicmp (szName, L"KEY", 3) == 0)) {
				pNew->cKey = 16;
				memcpy (pNew->acKey, link_key, 16);
				RETAILMSG (1, (L"BLUETOOTH GATEWAY:: Found link key for %04x%08x\n", GET_NAP(bt), GET_SAP(bt)));
			} else {
				// Registry is ONLY harvested for LINK keys.
				// PINs are exclusively managed by the file.
				// pNew->cPin = clink_key;
				// memcpy (pNew->acPin, link_key, clink_key);
			   RETAILMSG (1, (L"BLUETOOTH GATEWAY:: Found and ignored PIN code for %04x%08x in registry\n", GET_NAP(bt), GET_SAP(bt)));
			}
		}
	}

	RegCloseKey (hKey);

	return pResult;
}

//
//	Config file structure
//
//	PORT=name,index[:channel][,modem port index]
//	PIN<address>=sequence1..16
//  KEY<address>=sequence16
//  ENCRYPT={ON|OFF}
//  AUTHN={ON|OFF}
//  RASAUTH={NONE|PAP CHAP MSCHAP MSCHAPv2}
//	sequence={0xXX}+
//
static int Parse (WCHAR *szFileName, Config *pConfig) {
	Security *pRegistry = GetRegistrySecurity ();

	Security *pFileS = NULL;
	Port *pFileP = NULL;

	int fSyntaxError = FALSE;

	int fa_old = pConfig->fa;
	int fe_old = pConfig->fe;

	unsigned int rasauth_old = pConfig->uiRasAuth;

	pConfig->uiRasAuth = 0;

	FILE *fp = _wfopen (szFileName, L"r");
	if (fp) {
		RETAILMSG(1, (L"BLUETOOTH GATEWAY:: Using config file %s" CRLF, szFileName));

		WCHAR szBuffer[256];
		int line = 0;
		while (! feof (fp)) {
			++line;

			if (! fgetws (szBuffer, sizeof(szBuffer)/sizeof(szBuffer[0]), fp))
				break;

			WCHAR *p = szBuffer;
			PREFAST_ASSERT (p);
			
			while (iswspace(*p))
				++p;

			if ((*p == '\0') || (*p == ';'))
				continue;

			WCHAR *pKey=p;
			while ((*p != '\0') && (*p != '=') && (! iswspace(*p)))
				++p;

			WCHAR *pKeyEnd = p;

			while (iswspace(*p))
				++p;

			if ((*p != '=') || (*p == '\0')) {
				fSyntaxError = TRUE;
				ASSERT(0);
				RETAILMSG(1, (L"BLUETOOTH GATEWAY:: Syntax error near line %d, expected \'=\'" CRLF, line));
				break;
			}

			++p;

			while (iswspace(*p))
				++p;

			WCHAR *pVal = p;

			PREFAST_ASSERT (pKey);
			PREFAST_ASSERT (pKeyEnd);
			
			*pKeyEnd = L'\0';

			if (wcsicmp (pKey, L"PORT") == 0) {
				WCHAR *pName=pVal;
				while (*pVal && (! iswspace(*pVal)) && (*pVal != ','))
					++pVal;

				while (iswspace(*pVal)) {
					*pVal = '\0';
					++pVal;
				}

				if (*pVal != ',') {
					fSyntaxError = TRUE;
					RETAILMSG(1, (L"BLUETOOTH GATEWAY:: Syntax error near line %d, expected PORT=name,0..9[:1-31][,0..9]" CRLF, line));
					break;
				}

				*pVal = '\0';

				if (wcslen (pName) >= _MAX_PATH) {
					fSyntaxError = TRUE;
					RETAILMSG(1, (L"BLUETOOTH GATEWAY:: Syntax error near line %d, expected PORT=name,0..9[:1-31][,0..9]" CRLF, line));
					break;
				}

				++pVal;

				while (iswspace(*pVal))
					++pVal;

				unsigned int index = 0;
				unsigned int channel = 0;

				if ((! GetDI (&pVal, &index)) || (index > 9)) {
					fSyntaxError = TRUE;
					RETAILMSG(1, (L"BLUETOOTH GATEWAY:: Syntax error near line %d, expected PORT=name,0..9[:1-31][,0..9]" CRLF, line));
					break;
				}

				while (iswspace (*pVal))
					++pVal;

				if (*pVal == ':') {
					++pVal;

					while (iswspace (*pVal))
						++pVal;

					if ((! GetDI (&pVal, &channel)) || (channel == 0) || (channel > 31)) {
						fSyntaxError = TRUE;
						RETAILMSG(1, (L"BLUETOOTH GATEWAY:: Syntax error near line %d, expected PORT=name,0..9[:1-31][,0..9]" CRLF, line));
						break;
					}
				}

				while (iswspace (*pVal))
					++pVal;

				unsigned int index2 = 0;
				int fHaveIndex2 = FALSE;

				if (*pVal == ',') {	// Emulator port
					++pVal;

					while (iswspace (*pVal))
						++pVal;

					if ((! GetDI (&pVal, &index2)) || (index2 > 9)) {
						fSyntaxError = TRUE;
						RETAILMSG(1, (L"BLUETOOTH GATEWAY:: Syntax error near line %d, expected PORT=name,0..9[:1-31][,0..9]" CRLF, line));
						break;
					}

					fHaveIndex2 = TRUE;
				}

				while (iswspace (*pVal))
					++pVal;

				if ((*pVal != '\0') && (*pVal != ';')) {
					fSyntaxError = TRUE;
					RETAILMSG(1, (L"BLUETOOTH GATEWAY:: Syntax error near line %d, expected PORT=name,0..9[:1-31][,0..9]" CRLF, line));
					break;
				}

				Port *pNewPort = new Port;
				pNewPort->pNext = pFileP;
				pFileP = pNewPort;
				pNewPort->index = index;
				pNewPort->channel = channel;
				wcscpy (pNewPort->szDevName, pName);
				if (fHaveIndex2) {
					pNewPort->fModem = TRUE;
					pNewPort->index2 = index2;
				}

				RETAILMSG(1, (L"BLUETOOTH GATEWAY:: <%s> Port %d channel %d" CRLF, szFileName, pNewPort->index, pNewPort->channel));
				if (pNewPort->fModem)
				    RETAILMSG(1, (L"BLUETOOTH GATEWAY:: Modem emulation port %d" CRLF, pNewPort->index2));
			} else if ((wcsnicmp (pKey, L"PIN", 3) == 0) || (wcsnicmp (pKey, L"KEY", 3) == 0)) {
				BT_ADDR bt;
				p = pKey + 3;
				if (! GetBA(&p, &bt)) {
					fSyntaxError = TRUE;
					RETAILMSG(1, (L"BLUETOOTH GATEWAY:: Syntax error near line %d, expected PIN/KEY<bt_addr>=xx xx xx..." CRLF, line));
					break;
				}

				unsigned char value[16];
				for (int i = 0 ; i < 16 ; ++i) {
					if (! GetUx (&pVal, value+i, 2, FALSE))
						break;
				}

				if (((*pVal != L'\0') && (*pVal != L';')) || (((pKey[0] == 'K') || (pKey[0] == 'k')) && (i != 16))) {
					fSyntaxError = TRUE;
					RETAILMSG(1, (L"BLUETOOTH GATEWAY:: Syntax error near line %d, expected sequence of hex digits" CRLF, line));
					break;
				}

				Security *pNewS = Get (pFileS, bt);

				if (! pNewS) {
					pNewS = new Security;
					pNewS->pNext = pFileS;
					pFileS = pNewS;
					pNewS->bt = bt;
				}

				if (pKey[0] == L'K') {
					pNewS->cKey = 16;
					memcpy (pNewS->acKey, value, 16);

					RETAILMSG(1, (L"BLUETOOTH GATEWAY:: <%s> KEY %04x%08x" CRLF, szFileName, GET_NAP(pFileS->bt), GET_SAP(pFileS->bt)));
				} else {
					pNewS->cPin = i;
					memcpy (pNewS->acPin, value, i);

					RETAILMSG(1, (L"BLUETOOTH GATEWAY:: <%s> PIN %04x%08x" CRLF, szFileName, GET_NAP(pFileS->bt), GET_SAP(pFileS->bt)));
				}
			} else if (wcsicmp (pKey, L"AUTH") == 0) {
				if (wcsnicmp (pVal, L"ON", 2) == 0) {
					pConfig->fa = TRUE;
					pVal += 2;
				} else if (wcsnicmp (pVal, L"OFF", 3) == 0) {
					pConfig->fa = FALSE;
					pVal += 3;
				} else {
					fSyntaxError = TRUE;
					RETAILMSG(1, (L"BLUETOOTH GATEWAY:: Syntax error near line %d, expected AUTH={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 AUTH={ON|OFF}" CRLF, line));
					break;
				}
			} else if (wcsicmp (pKey, L"RASAUTH") == 0) {
				if (wcsnicmp (pVal, L"NONE", 4) == 0) {
					pConfig->uiRasAuth = 0;
					pVal += 4;
				} else {
 					pConfig->uiRasAuth = bmProhibitAll;

					for ( ; ; ) {
						if (wcsnicmp (pVal, L"MSCHAPv2", 8) == 0) {
							pConfig->uiRasAuth &= ~RASEO_ProhibitMsCHAP2;
							pVal += 8;
						} else if (wcsnicmp (pVal, L"MSCHAP", 6) == 0) {
							pConfig->uiRasAuth &= ~RASEO_ProhibitMsCHAP;
							pVal += 6;
						} else if (wcsnicmp (pVal, L"CHAP", 4) == 0) {
							pConfig->uiRasAuth &= ~RASEO_ProhibitCHAP;
							pVal += 4;
						} else if (wcsnicmp (pVal, L"PAP", 3) == 0) {
							pConfig->uiRasAuth &= ~RASEO_ProhibitPAP;
							pVal += 3;
						} else {
							fSyntaxError = TRUE;
							DEBUGMSG(1, (L"BLUETOOTH GATEWAY:: Syntax error near line %d, unknown RAS auth type" CRLF, line));
							break;
						}

						while (iswspace (*pVal))
							++pVal;

						if ((*pVal == '\0') || (*pVal == ';'))
							break;
					}

					if ((pConfig->uiRasAuth == bmProhibitAll) && (! fSyntaxError)) {
						fSyntaxError = TRUE;
						DEBUGMSG(1, (L"BLUETOOTH GATEWAY:: Syntax error near line %d, expected RAS auth type" CRLF, line));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -