コード例 #1
0
void CPacketConn::RcvLoop()
{
	if (m_Server) {
		do {
			WaitForSingleObject(m_Tcp->GetReadEvent(), INFINITE);
		} while (!m_Kill && RcvPacket());
	} else {
		PostMessage(m_hWnd, UWM_TCPCONNECT, TRUE, m_Cookie);
		while (RcvPacket()) {
			;
		}
		m_Tcp->Disconnect();
		PostMessage(m_hWnd, UWM_TCPCONNECT, FALSE, m_Cookie);
	}
}
コード例 #2
0
ファイル: socksproxy.c プロジェクト: TonyAlloa/miranda-dev
static void DispatcherThread(void *pUser)
{
	CONNINST *pInst = (CONNINST*)pUser;
	char *pszUser, *pszPass, *pszError, *pszMsgBuf=NULL;
	unsigned int uiLength, cbMsgBuf=0, bAuthenticated = 0, iConnected=0, iLogin=1;
	char command=0, reply=0;

	if (pInst->hProxy->pCfg->bVerbose && pInst->hProxy->pCfg->fpLog)
		fprintf (pInst->hProxy->pCfg->fpLog, "Imo2sproxy::DispatcherThread()\n");

	if (!(pInst->hInst = Imo2S_Init(EventHandler, pInst, pInst->hProxy->pCfg->iFlags)))
	{
		pInst->hProxy->pCfg->logerror (stderr, "Connection %d: Cannot start Imo2Skype instance.\n", pInst->hSock);
		FreeConnection (pInst);
		return;
	}

	// FIXME: Static user+pass from cmdline, until there is a possibility for 
	// a client to authenticate
	pszUser = pInst->hProxy->pCfg->pszUser;
	pszPass = pInst->hProxy->pCfg->pszPass;

	// FIXME: We should enable logging dependent on a loglevel rather than just enabling it
	if (pInst->hProxy->pCfg->bVerbose)
		Imo2S_SetLog (pInst->hInst, pInst->hProxy->pCfg->fpLog);


	while (pInst->hProxy->iRunning)
	{
		if (RcvPacket(pInst, &uiLength, sizeof(uiLength))<=0) break;
		LockMutex(pInst->sendmutex);

		if (uiLength == 0)
		{
			if (RcvPacket(pInst, &command, 1)<=0)
			{
				UnlockMutex(pInst->sendmutex);
				break;
			}
			switch (command)
			{
				case AUTHENTICATE:
					if (pInst->hProxy->pMyCfg->pszAuthPass) reply=1;
					break;
				case CAPABILITIES:
					if (pInst->hProxy->pMyCfg->pszAuthPass) reply=USE_AUTHENTICATION;
					break;
			}
			if (SendPacket (pInst, &reply, 1)<=0)
			{
				UnlockMutex(pInst->sendmutex);
				break;
			}
			UnlockMutex(pInst->sendmutex);
			continue;
		}

		if (uiLength >= cbMsgBuf)
		{
			pszMsgBuf = realloc (pszMsgBuf, uiLength+1);
			if (!pszMsgBuf)
			{
				UnlockMutex(pInst->sendmutex);
				break;
			}
			cbMsgBuf=uiLength+1;
		}

		if (RcvPacket(pInst, pszMsgBuf, uiLength)<=0)
		{
			UnlockMutex(pInst->sendmutex);
			break;
		}

		if (command)
		{
			if (command == AUTHENTICATE)
			{
				bAuthenticated = pInst->hProxy->pMyCfg->pszAuthPass && strcmp(pInst->hProxy->pMyCfg->pszAuthPass, pszMsgBuf) == 0;
				if (SendPacket (pInst, &bAuthenticated, 1)<=0)
				{
					UnlockMutex(pInst->sendmutex);
					break;
				}
			}
			command = 0;
		}
		UnlockMutex(pInst->sendmutex);

		if (iLogin)
		{
			if (Imo2S_Login (pInst->hInst, pszUser, pszPass, &pszError) != 1)
			{
				pInst->hProxy->pCfg->logerror (stderr, "Connection %d: Cannot login with (%s/****): %s\n", 
					pInst->hSock, pszUser, pszError);
				FreeConnection (pInst);
				return;
			}
			iLogin = 0;
		}

		if (pInst->hProxy->pMyCfg->pszAuthPass && !bAuthenticated)continue;
		pszMsgBuf[uiLength]=0;
		if (pInst->hProxy->pCfg->bVerbose && pInst->hProxy->pCfg->fpLog)
		{
			fprintf (pInst->hProxy->pCfg->fpLog, "%03d< [%s]\n", pInst->hSock, pszMsgBuf);
			fflush (pInst->hProxy->pCfg->fpLog);
		}
		if (!iConnected)
		{
			LockMutex (pInst->connected);
			iConnected = pInst->iConnectionStat;
			if (!iConnected)
			{
				pInst->hProxy->pCfg->logerror(stderr, "Invalid username / password");
			}
		}
		Imo2S_Send (pInst->hInst, pszMsgBuf);	
	}

	FreeConnection (pInst);
	if (pszMsgBuf) free (pszMsgBuf);
	return;
}