// test long-credential authentication HRESULT CTestMessageHandler::Test4() { HRESULT hr=S_OK; CStunMessageBuilder builder1, builder2; CStunMessageReader readerResponse; CSocketAddress addrMapped; uint16_t errorcode = 0; char szNonce[MAX_STUN_AUTH_STRING_SIZE+1]; char szRealm[MAX_STUN_AUTH_STRING_SIZE+1]; // ----------------------------------------------------------------------- // simulate a user making a request with no message integrity attribute (or username, or realm) InitBindingRequest(builder1); builder1.FixLengthField(); ChkA(SendHelper(builder1, &readerResponse, _spAuthLong)); Chk(readerResponse.GetErrorCode(&errorcode)); ChkIfA(readerResponse.GetMessageClass() != ::StunMsgClassFailureResponse, E_UNEXPECTED); ChkIf(errorcode != ::STUN_ERROR_UNAUTHORIZED, E_UNEXPECTED); readerResponse.GetStringAttributeByType(STUN_ATTRIBUTE_REALM, szRealm, ARRAYSIZE(szRealm)); readerResponse.GetStringAttributeByType(STUN_ATTRIBUTE_NONCE, szNonce, ARRAYSIZE(szNonce)); // -------------------------------------------------------------------------------- // now simulate the follow-up request readerResponse.Reset(); InitBindingRequest(builder2); builder2.AddNonce(szNonce); builder2.AddRealm(szRealm); builder2.AddUserName("AuthorizedUser"); builder2.AddMessageIntegrityLongTerm("AuthorizedUser", szRealm, "password"); builder2.FixLengthField(); ChkA(SendHelper(builder2, &readerResponse, _spAuthLong)); ChkIfA(readerResponse.GetMessageClass() != ::StunMsgClassSuccessResponse, E_UNEXPECTED); // should have a mapped address ChkA(readerResponse.GetMappedAddress(&addrMapped)); // and the message integrity field should be valid ChkA(readerResponse.ValidateMessageIntegrityLong("AuthorizedUser", szRealm, "password")); Cleanup: return hr; }
// test simple authentication HRESULT CTestMessageHandler::Test3() { CStunMessageBuilder builder1, builder2, builder3; CStunMessageReader readerResponse; uint16_t errorcode = 0; HRESULT hr = S_OK; // ----------------------------------------------------------------------- // simulate an authorized user making a request with a valid password ChkA(InitBindingRequest(builder1)); builder1.AddStringAttribute(STUN_ATTRIBUTE_USERNAME, "AuthorizedUser"); builder1.AddMessageIntegrityShortTerm("password"); builder1.FixLengthField(); ChkA(SendHelper(builder1, &readerResponse, _spAuthShort)); ChkA(readerResponse.ValidateMessageIntegrityShort("password")); // ----------------------------------------------------------------------- // simulate a user with a bad password readerResponse.Reset(); InitBindingRequest(builder2); builder2.AddStringAttribute(STUN_ATTRIBUTE_USERNAME, "WrongUser"); builder2.AddMessageIntegrityShortTerm("wrongpassword"); builder2.FixLengthField(); ChkA(SendHelper(builder2, &readerResponse, _spAuthShort)) errorcode = 0; ChkA(readerResponse.GetErrorCode(&errorcode)); ChkIfA(errorcode != ::STUN_ERROR_UNAUTHORIZED, E_FAIL); // ----------------------------------------------------------------------- // simulate a client sending no credentials - we expect it to fire back with a 400/bad-request readerResponse.Reset(); ChkA(InitBindingRequest(builder3)); ChkA(SendHelper(builder3, &readerResponse, _spAuthShort)); errorcode = 0; ChkA(readerResponse.GetErrorCode(&errorcode)); ChkIfA(errorcode != ::STUN_ERROR_BADREQUEST, E_FAIL); Cleanup: return hr; }
/// /// just calls SendHelper to prepare the buffer. /// int Port::SayServer (const YARPNameID& pid, const char *buf) { int result = YARP_FAIL; if (pid.isValid()) { result = SendHelper (pid, buf, ACE_OS::strlen(buf)+1, MSG_ID_NULL); } return result; }
const char *CSocketHTTPDataSource::send(const char *pzProcedureName, const char *xml, int nXMLLen, void *PooledConnection, void **ppUserData, const char *pzNamespace) { // service the request locally if conf'd by txml.txt or SetConnectionInfo() const char *pXML = CFileDataSource::send(pzProcedureName,xml,nXMLLen,PooledConnection,ppUserData,pzNamespace); if (pXML) return pXML; char *pHTTP = 0; GString *XMLResults = new GString(100000); GString **pp = (GString **)ppUserData; GString *p = *pp; p = XMLResults; // go to the server for the response int nAttempts = 0; for (int i = 0; i < 1; i++) { try { SendHelper( xml, nXMLLen, *XMLResults, pzNamespace ); nAttempts = 0; pHTTP = (char *)(const char *)*XMLResults; // XML starts after HTTP 1.1 headers pXML = pHTTP; while(memcmp(pXML,"HTTP",4)==0) { // advance past the HTTP header(s) pXML = strstr(pXML,"\r\n\r\n") + 4; } }catch( CSocketError &rErr ) { nAttempts++; i--; if (nAttempts > 3) { throw GException("Socket data soruce", 2001, rErr.m_nErrorCode, rErr.m_pzOperation); } } } return pXML; }
/// /// this commands the port to terminate gracefully. int Port::SaySelfEnd(void) { int result = YARP_FAIL; if (self_id == NULL && name.c_str()[0] != '\0') { if (protocol_type == YARP_MCAST) { self_id = YARPNameService::LocateName(name.c_str(), network_name.c_str(), YARP_UDP); self_id->setServiceType (YARP_TCP); YARPEndpointManager::CreateOutputEndpoint (*self_id); YARPEndpointManager::ConnectEndpoints (*self_id, name); } else { self_id = YARPNameService::LocateName(name.c_str(), network_name.c_str(), protocol_type); if (self_id->getServiceType() != YARP_QNET) self_id->setServiceType (YARP_TCP); YARPEndpointManager::CreateOutputEndpoint (*self_id); YARPEndpointManager::ConnectEndpoints (*self_id, name); } } else /// silly but to guarantee self_id is !NULL. if (self_id != NULL && !self_id->isValid() && name.c_str()[0] != '\0') { if (protocol_type == YARP_MCAST) { self_id = YARPNameService::LocateName(name.c_str(), network_name.c_str(), YARP_UDP); self_id->setServiceType (YARP_TCP); YARPEndpointManager::CreateOutputEndpoint (*self_id); YARPEndpointManager::ConnectEndpoints (*self_id, name); } else { self_id = YARPNameService::LocateName(name.c_str(), network_name.c_str(), protocol_type); if (self_id->getServiceType() != YARP_QNET) self_id->setServiceType (YARP_TCP); YARPEndpointManager::CreateOutputEndpoint (*self_id); YARPEndpointManager::ConnectEndpoints (*self_id, name); } } if (self_id != NULL) { if (self_id->isValid()) { // need to make sure the detach message will be read, // even if other messages are arriving Relinquish(); out_mutex.Wait(); ignore_data = 1; out_mutex.Post(); Relinquish(); result = SendHelper (*self_id, NULL, 0, MSG_ID_DETACH_ALL); } Relinquish(); /// wait for message to be received. complete_msg_thread.Wait(); /// deletes the endpoint. YARPEndpointManager::Close (*self_id); YARPNameService::DeleteName (self_id); self_id = NULL; ///YARPScheduler::yield(); /// tell the main thread to complete the termination function. complete_terminate.Signal(); YARPThread::Join (); } return result; }