Exemple #1
0
static void
TestSharedVariables()
{
    NPT_SharedVariable shared;
    SharedVarThread t1(1, shared);
    SharedVarThread t2(2, shared);
    SharedVarThread t3(2, shared);
    
    t1.Start();
    t2.Start();
    t3.Start();
    NPT_System::Sleep(3.0);
    shared.SetValue(1);
    NPT_System::Sleep(2.0);
    shared.SetValue(2);
    
    NPT_Result result = t1.Wait(20000);
    CHECK(result == NPT_SUCCESS);
    CHECK(t1.m_Result == NPT_SUCCESS);
    result = t2.Wait(20000);
    CHECK(result == NPT_SUCCESS);
    CHECK(t2.m_Result == NPT_SUCCESS);
    result = t3.Wait(20000);
    CHECK(result == NPT_SUCCESS);
    CHECK(t3.m_Result == NPT_SUCCESS);
}
/*----------------------------------------------------------------------
|   TestSharedVariables
+---------------------------------------------------------------------*/
static void
TestSharedVariables()
{
	NPT_SharedVariable shared;
	SharedVariableThread t0(1, 2, shared, 0);
	SharedVariableThread t1(2, 1, shared, 0.001f);

	t0.Start();
	t1.Start();

	shared.SetValue(1);
	NPT_Result result = t0.Wait(10000);
	t0.m_Stop = true;
	t1.m_Stop = true;
	t1.Wait();

	NPT_Console::OutputF("T0 transitions=%d, result: %d\n", t0.m_Transitions, t0.m_Result);
	NPT_Console::OutputF("T1 transitions=%d, result: %d\n", t1.m_Transitions, t1.m_Result);

	CHECK(t0.m_Result == NPT_SUCCESS);
	CHECK(t1.m_Result == NPT_SUCCESS);
}
void
TlsTestServer::Run()
{
    printf("@@@ starting TLS server\n");
    NPT_TcpServerSocket socket;
    NPT_SocketAddress address(NPT_IpAddress::Any, 0);
    NPT_Result result = socket.Bind(address);
    if (NPT_FAILED(result)) {
        fprintf(stderr, "@@@ Bind failed (%d)\n", result);
        return;
    }
    result = socket.GetInfo(m_SocketInfo);
    if (NPT_FAILED(result)) {
        fprintf(stderr, "@@@ GetInfo failed (%d)\n", result);
        return;
    }
    m_Ready.SetValue(1);
    
    printf("@@@ Waiting for connection\n");
    NPT_Socket* client = NULL;
    socket.WaitForNewClient(client);
    printf("@@@ Client connected\n");
    
    NPT_TlsContextReference tls_context;
    if (m_Mode == 0) {
        tls_context = new NPT_TlsContext();
    } else if (m_Mode == 1) {
        /* require client authentication */
        tls_context = new NPT_TlsContext(NPT_TLS_CONTEXT_OPTION_REQUIRE_CLIENT_CERTIFICATE | NPT_TLS_CONTEXT_OPTION_VERIFY_LATER);
    }
    /* self-signed cert */
    result = tls_context->LoadKey(NPT_TLS_KEY_FORMAT_PKCS8, TestClient_p8_1, TestClient_p8_1_len, "neptune");
    CHECK(result == NPT_SUCCESS);
    result = tls_context->SelfSignCertificate("MyServerCommonName", "MyServerOrganization", "MyServerOrganizationalName");
    
    NPT_InputStreamReference  socket_input;
    NPT_OutputStreamReference socket_output;
    client->GetInputStream(socket_input);
    client->GetOutputStream(socket_output);
    NPT_TlsServerSession session(tls_context, socket_input, socket_output);
    delete client;
    
    result = session.Handshake();
    if (m_Mode == 1) {
        /* expect a self-signed client cert */
        result = session.VerifyPeerCertificate();
        printf("@@@ Certificate Verification Result = %d (%s)\n", result, NPT_ResultText(result));
        if (result != NPT_ERROR_TLS_CERTIFICATE_SELF_SIGNED) {
            printf("!ERROR, cert verification expected %d, got %d\n", NPT_ERROR_TLS_CERTIFICATE_SELF_SIGNED, result);
            return;
        }

        NPT_TlsCertificateInfo cert_info;
        result = session.GetPeerCertificateInfo(cert_info);
        CHECK(result == NPT_SUCCESS);
        PrintCertificateInfo(cert_info);
    } else {
        if (NPT_FAILED(result)) {
            fprintf(stderr, "@@@ Handshake failed (%d : %s)\n", result, NPT_ResultText(result));
            return;
        }
    }
    
    NPT_OutputStreamReference tls_output;
    session.GetOutputStream(tls_output);
    tls_output->WriteString("Hello, Client\n");
    
    printf("@@@ TLS server done\n");
    //NPT_System::Sleep(1.0);
}
Exemple #4
0
/*----------------------------------------------------------------------
|   PLT_SyncMediaBrowser::WaitForResponse
+---------------------------------------------------------------------*/
NPT_Result
PLT_SyncMediaBrowser::WaitForResponse(NPT_SharedVariable& shared_var)
{
    return shared_var.WaitUntilEquals(1, 30000);
}
NPT_Result
GPAC_MediaController::WaitForResponse(NPT_SharedVariable& shared_var)
{
    return shared_var.WaitUntilEquals(1, 30000);
}
Exemple #6
0
void FrontEnd::processSsdpSearch(SsdpServerTask *task, Interface *intf, const NPT_DataBuffer& data, const NPT_SocketAddress& fromAddr)
{
	do {
		NPT_HttpRequest *req;
		NPT_InputStreamReference inputStream0(new NPT_MemoryStream(data.GetData(), data.GetDataSize()));
		NPT_BufferedInputStream inputStream(inputStream0);
		if (NPT_FAILED(NPT_HttpRequest::Parse(inputStream, NULL, req))) {
			break;
		}

		PtrHolder<NPT_HttpRequest> req1(req);
		if (req->GetMethod().Compare("M-SEARCH") != 0 || req->GetProtocol().Compare(NPT_HTTP_PROTOCOL_1_1) != 0 || req->GetUrl().GetPath().Compare("*") != 0) {
			break;
		}

		NPT_HttpHeader *hdrMan = req->GetHeaders().GetHeader("MAN");
		if (!hdrMan || hdrMan->GetValue().Compare("\"ssdp:discover\"") != 0) {
			break;
		}

		NPT_HttpHeader *hdrHost = req->GetHeaders().GetHeader("HOST");
		if (!hdrHost || (hdrHost->GetValue().Compare("239.255.255.250:1900") != 0 && hdrHost->GetValue().Compare("239.255.255.250") != 0)) {
			break;
		}

		int mx;
		NPT_HttpHeader *hdrMX = req->GetHeaders().GetHeader("MX");
		if (!hdrMX || NPT_FAILED(NPT_ParseInteger(hdrMX->GetValue(), mx)) || mx < 1) {
			break;
		}

		if (mx > 120) {
			mx = 120;
		}

		NPT_HttpHeader *hdrST = req->GetHeaders().GetHeader("ST");
		if (!hdrST) {
			break;
		}

		NPT_List<MatchContext*> matchList;

		NPT_UdpSocket sock(NPT_SOCKET_FLAG_CANCELLABLE);
		sock.Bind(NPT_SocketAddress(intf->m_context.m_ifAddr, 0));
		NPT_SharedVariable waitVar;
		waitVar.SetValue(0);

		{
			ReadLocker locker(m_dsLock);
			for (NPT_Ordinal i = 0; i < m_deviceImplList.GetItemCount(); i++) {
				NPT_List<DeviceImplInfo*>::Iterator it = m_deviceImplList.GetItem(i);
				DeviceImplInfo *info = *it;
				MatchContext *matchContext = new MatchContext();
				if (info->m_deviceImpl->match(hdrST->GetValue(), matchContext->matches)) {
					matchList.Add(matchContext);
					matchContext->deviceUuid = info->m_deviceImpl->uuid();
					matchContext->expireSeconds = info->m_deviceImpl->m_expireSeconds;
					matchContext->descPath = info->m_deviceImpl->m_descPath;
					matchContext->httpRoot = info->m_context.m_httpRoot;
				} else {
					delete matchContext;
				}
			}
		}

		SsdpSearchAbortCallback abortCallback(&sock, &waitVar);
		if (task->registerAbortCallback(&abortCallback)) {

			for (NPT_Ordinal i = 0; i < matchList.GetItemCount(); i++) {
				MatchContext *matchContext = *matchList.GetItem(i);

				NPT_String location = NPT_String::Format("http://%s:%d%s%s", intf->m_context.m_ifAddr.ToString().GetChars(), intf->m_context.m_httpPort, matchContext->httpRoot.GetChars(), matchContext->descPath.GetChars());

				bool broken = false;

				for (NPT_Ordinal j = 0; j < matchContext->matches.GetItemCount(); j++) {
					NPT_List<DeviceImplMatch>::Iterator it2 = matchContext->matches.GetItem(j);

					NPT_Timeout timeout = NPT_System::GetRandomInteger() % (mx * 1000);
					// TODO: wait or not ???
					timeout = 0;
					if (NPT_SUCCEEDED(waitVar.WaitWhileEquals(0, timeout))) {
						break;
					}

					{
						ReadLocker locker(m_dsLock);
						if (m_deviceImplIndex.HasKey(matchContext->deviceUuid)) {
							NPT_TimeStamp ts;
							NPT_System::GetCurrentTimeStamp(ts);
							NPT_String dateStr = NPT_DateTime(ts).ToString(NPT_DateTime::FORMAT_RFC_1123);
							NPT_String resp = NPT_String::Format("HTTP/1.1 200 OK\r\nCACHE-CONTROL: max-age=%d\r\nDATE: %s\r\nEXT: \r\nLOCATION: %s\r\nSERVER: %s\r\nST: %s\r\nUSN: %s\r\nCUSTOM:%s\r\n\r\n",
														matchContext->expireSeconds, dateStr.GetChars(), location.GetChars(), m_serverHeader.GetChars(), it2->m_st.GetChars(), it2->m_usn.GetChars(), m_DevName.GetChars());
							NPT_DataBuffer packet(resp.GetChars(), resp.GetLength(), false);
							sock.Send(packet, &fromAddr);
						}
					}
				}

				if (broken) {
					break;
				}
			}

			task->unregisterAbortCallback(&abortCallback);
		}

		matchList.Apply(NPT_ObjectDeleter<MatchContext>());
	} while (false);
}