Beispiel #1
0
static unsigned __stdcall NetlibBindAcceptThread(void* param)
{
    SOCKET s;
    SOCKADDR_IN sin;
    int sinLen;
    struct NetlibConnection *nlc;
    struct NetlibBoundPort *nlbp = (NetlibBoundPort*)param;

    NetlibLogf(nlbp->nlu, "(%u) Port %u opened for incoming connections", nlbp->s, nlbp->wPort);
    for(;;)
    {
        sinLen = sizeof(sin);
        s = accept(nlbp->s, (struct sockaddr*)&sin, &sinLen);
        if (s == INVALID_SOCKET) break;
        NetlibLogf(nlbp->nlu, "New incoming connection on port %u from %s (%d)", nlbp->wPort, inet_ntoa(sin.sin_addr), s);
        nlc = (NetlibConnection*)mir_calloc(sizeof(NetlibConnection));
        nlc->handleType = NLH_CONNECTION;
        nlc->nlu = nlbp->nlu;
        nlc->s = s;
        nlc->s2 = INVALID_SOCKET;
        InitializeCriticalSection(&nlc->csHttpSequenceNums);
        nlc->hOkToCloseEvent = CreateEvent(NULL, TRUE, TRUE, NULL);
        nlc->dontCloseNow = 0;
        NetlibInitializeNestedCS(&nlc->ncsSend);
        NetlibInitializeNestedCS(&nlc->ncsRecv);
        nlbp->pfnNewConnectionV2((HANDLE)nlc,ntohl(sin.sin_addr.S_un.S_addr), nlbp->pExtra);
    }
    NetlibUPnPDeletePortMapping(nlbp->wExPort, "TCP");
    return 0;
}
Beispiel #2
0
static bool NetlibHttpGatewayOscarPost(NetlibConnection *nlc, const char *buf, int len, int flags)
{
	NETLIBHTTPREQUEST *nlhrReply = NULL;
	NetlibConnection nlcSend = {0};

	nlcSend.handleType       = NLH_CONNECTION;
	nlcSend.nlu              = nlc->nlu;
	nlcSend.nlhpi            = nlc->nlhpi;
	nlcSend.s                = nlc->s2;
	nlcSend.usingHttpGateway = nlc->usingHttpGateway;
	nlcSend.szProxyServer    = nlc->szProxyServer;
	nlcSend.wProxyPort       = nlc->wProxyPort;
	nlcSend.proxyType        = nlc->proxyType;

	if (!NetlibReconnect(&nlcSend)) return false;
	nlc->s2 = nlcSend.s;

	nlcSend.hOkToCloseEvent	= CreateEvent(NULL, TRUE, TRUE, NULL);
	NetlibInitializeNestedCS(&nlcSend.ncsRecv);
	NetlibInitializeNestedCS(&nlcSend.ncsSend);

	bool res = NetlibHttpGatewaySend(&nlcSend, reqOldPost, buf, len);
	if (res)
	{
		NETLIBHTTPREQUEST *nlhrReply = NetlibHttpRecv(&nlcSend, flags | MSG_RAW | MSG_DUMPPROXY, MSG_RAW | MSG_DUMPPROXY);
		if (nlhrReply != NULL)
		{
			if (nlhrReply->resultCode != 200) 
			{
				NetlibHttpSetLastErrorUsingHttpResult(nlhrReply->resultCode);
				res = false;
			}
			NetlibHttpFreeRequestStruct(0, (LPARAM)nlhrReply);
		}
		else
			res = false;
	}

	NetlibDeleteNestedCS(&nlcSend.ncsSend);
	NetlibDeleteNestedCS(&nlcSend.ncsRecv);
	CloseHandle(nlcSend.hOkToCloseEvent);

	nlc->s2 = nlcSend.s;
	mir_free((char*)nlcSend.nloc.szHost);

	EnterCriticalSection(&nlc->csHttpSequenceNums);

	nlc->nlhpi.firstPostSequence++;
	if (nlc->nlhpi.flags & NLHPIF_GETPOSTSAMESEQUENCE) nlc->nlhpi.firstGetSequence++;

	LeaveCriticalSection(&nlc->csHttpSequenceNums);

	return res;
}
Beispiel #3
0
INT_PTR NetlibOpenConnection(WPARAM wParam,LPARAM lParam)
{
	NETLIBOPENCONNECTION *nloc = (NETLIBOPENCONNECTION*)lParam;
	struct NetlibUser *nlu = (struct NetlibUser*)wParam;
	struct NetlibConnection *nlc;

	NetlibLogf(nlu,"Connection request to %s:%d (Flags %x)....", nloc->szHost, nloc->wPort, nloc->flags);

	if (GetNetlibHandleType(nlu) != NLH_USER || !(nlu->user.flags & NUF_OUTGOING) || nloc == NULL ||
		(nloc->cbSize != NETLIBOPENCONNECTION_V1_SIZE && nloc->cbSize != sizeof(NETLIBOPENCONNECTION)) || 
		nloc->szHost == NULL || nloc->wPort == 0)
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return 0;
	}
	nlc = (struct NetlibConnection*)mir_calloc(sizeof(struct NetlibConnection));
	nlc->handleType = NLH_CONNECTION;
	nlc->nlu = nlu;
	nlc->nloc = *nloc;
	nlc->nloc.szHost = mir_strdup(nloc->szHost);
	nlc->s = INVALID_SOCKET;
	nlc->s2 = INVALID_SOCKET;
	nlc->dnsThroughProxy = nlu->settings.dnsThroughProxy != 0;

	InitializeCriticalSection(&nlc->csHttpSequenceNums);
	nlc->hOkToCloseEvent = CreateEvent(NULL,TRUE,TRUE,NULL);
	nlc->dontCloseNow = 0;
	NetlibInitializeNestedCS(&nlc->ncsSend);
	NetlibInitializeNestedCS(&nlc->ncsRecv);

	if (!NetlibDoConnect(nlc))
	{
		FreePartiallyInitedConnection(nlc);		
		return 0;
	}

	if (iUPnPCleanup == 0)
	{
		EnterCriticalSection(&csNetlibUser);
		if (iUPnPCleanup == 0) 
		{
			iUPnPCleanup = 1;
			forkthread(NetlibUPnPCleanup, 0, NULL);
		}
		LeaveCriticalSection(&csNetlibUser);
	}

	return (INT_PTR)nlc;
}