void doListOperations() { UtlSList testList; // fill the list appendCountItems(testList, NUM_PERFORMANCE_STRINGS); // take the first half off the front if (!testList.isEmpty()) { getCountItems(testList, NUM_PERFORMANCE_STRINGS / 2); } // take the rest off the end by reference if (!testList.isEmpty()) { UtlContainable* lastItem = testList.last(); delete dynamic_cast<UtlString*>(testList.removeReference(lastItem)); } // fill the list appendCountItems(testList, NUM_PERFORMANCE_STRINGS); // search the list for each item by value UtlString target; int targetIndex; for (targetIndex = 0; targetIndex < NUM_PERFORMANCE_STRINGS; targetIndex += 1) { target = string[targetIndex]; UtlString* found = dynamic_cast<UtlString*>(testList.find(&target)); if (found) { externalForSideEffects = found->length(); } } // get the object in the middle of the list by index, and remove it by value while(!testList.isEmpty()) { int numberLeft = testList.entries(); UtlString* middle = dynamic_cast<UtlString*>(testList.at((numberLeft / 2))); delete dynamic_cast<UtlString*>(testList.remove(middle)); } // fill the list appendCountItems(testList, NUM_PERFORMANCE_STRINGS); // iterate over each item in the list UtlSListIterator iterate(testList); UtlString* item; while ((item = dynamic_cast<UtlString*>(iterate()))) { externalForSideEffects = item->length(); delete item; } }
void testNoRouteLocal() { const char* message = "INVITE sip:[email protected] SIP/2.0\r\n" "Via: SIP/2.0/TCP 10.1.1.3:33855\r\n" "To: sip:[email protected]\r\n" "From: Caller <sip:[email protected]>; tag=30543f3483e1cb11ecb40866edd3295b\r\n" "Call-Id: f88dfabce84b6a2787ef024a7dbe8749\r\n" "Cseq: 1 INVITE\r\n" "Max-Forwards: 20\r\n" "Contact: [email protected]\r\n" "Content-Length: 0\r\n" "\r\n"; SipMessage testMsg(message, strlen(message)); Url requestUri; UtlSList removedRoutes; testMsg.normalizeProxyRoutes(UserAgent, requestUri, &removedRoutes); UtlString normalizedMsg; int msgLen; testMsg.getBytes(&normalizedMsg, &msgLen); ASSERT_STR_EQUAL(message, normalizedMsg.data()); UtlString requestUriResult; requestUri.toString(requestUriResult); ASSERT_STR_EQUAL("sip:[email protected]", requestUriResult.data()); CPPUNIT_ASSERT(removedRoutes.isEmpty()); }
void testDoubleRoute() { const char* message = "INVITE sip:[email protected] SIP/2.0\r\n" "Route: <sip:[email protected];lr>, <sip:[email protected];lr>\r\n" "Via: SIP/2.0/TCP 10.1.1.3:33855\r\n" "To: sip:[email protected]\r\n" "From: Caller <sip:[email protected]>; tag=30543f3483e1cb11ecb40866edd3295b\r\n" "Call-Id: f88dfabce84b6a2787ef024a7dbe8749\r\n" "Cseq: 1 INVITE\r\n" "Max-Forwards: 20\r\n" "Contact: [email protected]\r\n" "Content-Length: 0\r\n" "\r\n"; SipMessage testMsg(message, strlen(message)); Url requestUri; UtlSList removedRoutes; testMsg.normalizeProxyRoutes(UserAgent, requestUri, &removedRoutes); UtlString normalizedMsg; int msgLen; testMsg.getBytes(&normalizedMsg, &msgLen); const char* expectedMessage = // route header removed, uri swapped "INVITE sip:[email protected] SIP/2.0\r\n" "Via: SIP/2.0/TCP 10.1.1.3:33855\r\n" "To: sip:[email protected]\r\n" "From: Caller <sip:[email protected]>; tag=30543f3483e1cb11ecb40866edd3295b\r\n" "Call-Id: f88dfabce84b6a2787ef024a7dbe8749\r\n" "Cseq: 1 INVITE\r\n" "Max-Forwards: 20\r\n" "Contact: [email protected]\r\n" "Content-Length: 0\r\n" "\r\n"; ASSERT_STR_EQUAL(expectedMessage, normalizedMsg.data()); UtlString requestUriResult; requestUri.toString(requestUriResult); ASSERT_STR_EQUAL("sip:[email protected]", requestUriResult.data()); UtlString* removedRoute; CPPUNIT_ASSERT( removedRoute = static_cast<UtlString*>(removedRoutes.get())); ASSERT_STR_EQUAL("<sip:[email protected];lr>", removedRoute->data()); delete removedRoute; CPPUNIT_ASSERT( removedRoute = static_cast<UtlString*>(removedRoutes.get())); ASSERT_STR_EQUAL("<sip:[email protected];lr>", removedRoute->data()); delete removedRoute; CPPUNIT_ASSERT(removedRoutes.isEmpty()); }
void getCountItems(UtlSList& list, size_t itemsToPop) { assert(itemsToPop); for (; !list.isEmpty() && itemsToPop; itemsToPop--) { delete dynamic_cast<UtlString*>(list.get()); } }
UtlBoolean DialByNameDB::insertRow ( const Url& contact ) const { UtlBoolean result = FALSE; if ( m_pFastDB != NULL ) { // Fetch the display name UtlString identity, displayName, contactString; contact.getIdentity( identity ); contact.getDisplayName( displayName ); contact.toString( contactString ); // Make sure that the contact URL is valid and contains // a contactIdentity and a contactDisplayName if ( !identity.isNull() && !displayName.isNull() ) { UtlSList dtmfStrings; getDigitStrings ( displayName, dtmfStrings ); if ( !dtmfStrings.isEmpty() ) { // Thread Local Storage m_pFastDB->attach(); // Search for a matching row before deciding to update or insert dbCursor< DialByNameRow > cursor(dbCursorForUpdate); DialByNameRow row; dbQuery query; // Primary Key is identity query="np_identity=",identity; // Purge all existing entries associated with this identity if ( cursor.select( query ) > 0 ) { cursor.removeAllSelected(); } // insert all dtmf combinations for this user unsigned int i; for (i=0; i<dtmfStrings.entries(); i++) { UtlString* digits = (UtlString*)dtmfStrings.at(i); row.np_contact = contactString; row.np_identity = identity; row.np_digits = digits->data(); insert (row); } // Commit rows to memory - multiprocess workaround m_pFastDB->detach(0); } } } return result; }
void requestGet(Url& url, UtlSList& names) { XmlRpcRequest* request; XmlRpcResponse response; request = new XmlRpcRequest(url, "configurationParameter.get"); request->addParam(&DataSet); if (!names.isEmpty()) { request->addParam(&names); } if (!request->execute(response/*, &pSocket*/)) { exitFault(response); } else { UtlContainable* value; if (response.getResponse(value)) { UtlHashMap* paramList = dynamic_cast<UtlHashMap*>(value); if (paramList) { UtlHashMapIterator params(*paramList); UtlString* name; while ((name = dynamic_cast<UtlString*>(params()))) { UtlString* value = dynamic_cast<UtlString*>(paramList->findValue(name)); printf("%s : %s\n", name->data(), value->data()); } } else { fprintf(stderr, "Incorrect type returned.\n"); exit(1); } } else { fprintf(stderr, "No value returned.\n"); exit(1); } } delete request; request = NULL; }
//deep copy of aliases void SipLine::copyAliases(UtlSList& dest, const UtlSList& source) const { // Clear dest list if (!dest.isEmpty()) { dest.destroyAll() ; } // Copy maintaining order int length = source.entries() ; for (int i=0; i<length; i++) { UtlString* pEntry = (UtlString*) source.at(i) ; dest.append(new UtlString(*pEntry)) ; } }
void testTwoRoutesOneLocal() { const char* message = "INVITE sip:[email protected] SIP/2.0\r\n" "Route: <sip:example.com;lr>, <sip:proxy.somewhere.com;lr>\r\n" "Via: SIP/2.0/TCP 10.1.1.3:33855\r\n" "To: sip:[email protected]\r\n" "From: Caller <sip:[email protected]>; tag=30543f3483e1cb11ecb40866edd3295b\r\n" "Call-Id: f88dfabce84b6a2787ef024a7dbe8749\r\n" "Cseq: 1 INVITE\r\n" "Max-Forwards: 20\r\n" "Contact: [email protected]\r\n" "Content-Length: 0\r\n" "\r\n"; SipMessage testMsg(message, strlen(message)); Url requestUri; UtlSList removedRoutes; testMsg.normalizeProxyRoutes(UserAgent, requestUri, &removedRoutes); UtlString normalizedMsg; int msgLen; testMsg.getBytes(&normalizedMsg, &msgLen); UtlString requestUriResult; requestUri.toString(requestUriResult); ASSERT_STR_EQUAL("sip:[email protected]", requestUriResult.data()); UtlString* removedRoute; CPPUNIT_ASSERT( removedRoute = dynamic_cast<UtlString*>(removedRoutes.get())); ASSERT_STR_EQUAL("<sip:example.com;lr>", removedRoute->data()); delete removedRoute; CPPUNIT_ASSERT(removedRoutes.isEmpty()); UtlString route; /* "Route: <sip:proxy.somewhere.com;lr>\r\n" */ CPPUNIT_ASSERT(getNormalRoute(testMsg, 0, route)); ASSERT_STR_EQUAL("<sip:proxy.somewhere.com;lr>", route.data()); CPPUNIT_ASSERT(!getNormalRoute(testMsg, 1, route)); }
void requestDelete(Url& url, UtlSList& names) { XmlRpcRequest* request; XmlRpcResponse response; request = new XmlRpcRequest(url, "configurationParameter.delete"); request->addParam(&DataSet); if (!names.isEmpty()) { request->addParam(&names); } if (!request->execute(response/*, &pSocket*/)) { exitFault(response); } else { UtlContainable* value; if (response.getResponse(value)) { UtlInt* deletedCount = dynamic_cast<UtlInt*>(value); if (deletedCount) { if (Verbose == Feedback) { printf("deleted %d parameters.\n", (int)deletedCount->getValue()); } } else { fprintf(stderr, "Incorrect type returned.\n"); exit(1); } } else { fprintf(stderr, "No value returned.\n"); exit(1); } } delete request; request = NULL; }
int supervisorMain(bool bOriginalSupervisor) { // Create forked process which will do nothing unless parent dies. Parent continues with initialization. forkSupervisorInWaiting(); // Drop privileges down to the specified user & group const char * sipxpbxuser = SipXecsService::User(); const char * sipxpbxgroup = SipXecsService::Group(); if (NULL == sipxpbxuser || 0 == strlen(sipxpbxuser)) { osPrintf("sipXsupervisor: Failed to setuid(%s), username not defined.\n", sipxpbxuser); return 2; } if (NULL == sipxpbxgroup || 0 == strlen(sipxpbxgroup)) { osPrintf("sipXsupervisor: Failed to setgid(%s), groupname not defined.\n", sipxpbxgroup); return 2; } struct group * grp = getgrnam(sipxpbxgroup); if (NULL == grp) { if (0 != errno) { osPrintf("getgrnam(%s) failed, errno = %d.", sipxpbxgroup, errno); } else { osPrintf( "sipXsupervisor: getgrnam(%s) failed, user does not exist.", sipxpbxgroup); } return 3; } struct passwd * pwd = getpwnam(sipxpbxuser); if (NULL == pwd) { if (0 != errno) { osPrintf("getpwnam(%s) failed, errno = %d.", sipxpbxuser, errno); } else { osPrintf( "sipXsupervisor: getpwnam(%s) failed, user does not exist.", sipxpbxuser); } return 3; } // Change group first, cause once user is changed this cannot be done. if (0 != setgid(grp->gr_gid)) { osPrintf("sipXsupervisor: setgid(%d) failed, errno = %d.", (int)grp->gr_gid, errno); return 4; } if (0 != setuid(pwd->pw_uid)) { osPrintf("sipXsupervisor: setuid(%d) failed, errno = %d.", (int)pwd->pw_uid, errno); return 4; } # if 0 // Only output problems. This keeps the startup output clean. osPrintf("sipXsupervisor: Dropped privileges with setuid(%s)/setgid(%s).", sipxpbxuser, sipxpbxgroup); #endif OsMsgQShared::setQueuePreference(OsMsgQShared::QUEUE_UNLIMITED); // Block all signals in this the main thread // Any threads created after this will have all signals masked. OsTask::blockSignals(); // Create a new task to wait for signals. Only that task // will ever see a signal from the outside. SignalTask* signalTask = new SignalTask(); signalTask->start() ; // All osPrintf output should go to the console until the log file is initialized. enableConsoleOutput(true); // Initialize the log file. Os::LoggerHelper::instance().processName = "Supervisor"; UtlString logFile = SipXecsService::Path(SipXecsService::LogDirType, "sipxsupervisor.log"); Os::LoggerHelper::instance().initialize(PRI_DEBUG, logFile.data()); if (!bOriginalSupervisor) { Os::Logger::instance().log(FAC_SUPERVISOR, PRI_CRIT, "Restarting sipxsupervisor after unexpected shutdown"); } Os::Logger::instance().log(FAC_SUPERVISOR, PRI_NOTICE, ">>>>> Starting sipxsupervisor version %s", VERSION); // Now that the log file is initialized, stop sending osPrintf to the console. // All relevant log messages from this point on must use Os::Logger::instance().log(). enableConsoleOutput(false); fflush(NULL); // Flush all output so children don't get a buffer of output // Open the supervisor configuration file OsConfigDb supervisorConfiguration; OsPath supervisorConfigPath = SipXecsService::Path(SipXecsService::ConfigurationDirType, CONFIG_SETTINGS_FILE); if (OS_SUCCESS != supervisorConfiguration.loadFromFile(supervisorConfigPath.data())) { Os::Logger::instance().log(FAC_SUPERVISOR,PRI_WARNING, "Failed to open supervisor configuration file at '%s'", supervisorConfigPath.data() ); } // Set logging based on the supervisor configuration - TODO change default to "NOTICE" ? OsSysLogPriority logPri = SipXecsService::setLogPriority(supervisorConfiguration, SUPERVISOR_PREFIX, PRI_INFO ); Os::Logger::instance().setLoggingPriorityForFacility(FAC_ALARM, logPri); // Open the domain configuration file OsConfigDb domainConfiguration; OsPath domainConfigPath = SipXecsService::domainConfigPath(); if (OS_SUCCESS != domainConfiguration.loadFromFile(domainConfigPath.data())) { Os::Logger::instance().log(FAC_SUPERVISOR,PRI_ERR, "Failed to open domain configuration '%s'", domainConfigPath.data() ); } // @TODO const char* managementIpBindAddress; int managementPortNumber; managementPortNumber = domainConfiguration.getPort(SipXecsService::DomainDbKey::SUPERVISOR_PORT); if (PORT_NONE == managementPortNumber) { Os::Logger::instance().log(FAC_SUPERVISOR,PRI_WARNING, "%s not configured in '%s', using default: %d", SipXecsService::DomainDbKey::SUPERVISOR_PORT, domainConfigPath.data(), DEFAULT_SUPERVISOR_PORT ); managementPortNumber=DEFAULT_SUPERVISOR_PORT; } else if (PORT_DEFAULT == managementPortNumber) { Os::Logger::instance().log(FAC_SUPERVISOR,PRI_NOTICE,"%s set to default: %d", SipXecsService::DomainDbKey::SUPERVISOR_PORT, DEFAULT_SUPERVISOR_PORT ); managementPortNumber=DEFAULT_SUPERVISOR_PORT; } UtlSList allowedPeers; UtlString configHosts; domainConfiguration.get(SipXecsService::DomainDbKey::CONFIG_HOSTS, configHosts); if (!configHosts.isNull()) { UtlString hostName; for (int hostIndex = 0; NameValueTokenizer::getSubField(configHosts.data(), hostIndex, ", \t", &hostName); hostIndex++) { // Found at least one config hostname. if (!allowedPeers.contains(&hostName)) { Os::Logger::instance().log(FAC_SUPERVISOR,PRI_DEBUG, "%s value '%s'", SipXecsService::DomainDbKey::CONFIG_HOSTS, hostName.data() ); allowedPeers.insert(new UtlString(hostName)); } } } else { Os::Logger::instance().log(FAC_SUPERVISOR,PRI_ERR, "%s not configured in '%s'", SipXecsService::DomainDbKey::CONFIG_HOSTS, domainConfigPath.data() ); } UtlString superHost; supervisorConfiguration.get(SUPERVISOR_HOST, superHost); if (!superHost.isNull()) { if (!allowedPeers.contains(&superHost)) { allowedPeers.insert(new UtlString(superHost)); } } else { Os::Logger::instance().log(FAC_SUPERVISOR,PRI_ERR, "%s not configured in '%s'", SUPERVISOR_HOST, supervisorConfigPath.data() ); } if (allowedPeers.isEmpty()) { Os::Logger::instance().log(FAC_SUPERVISOR,PRI_ERR, "No configuration peers configured."); } if (!cAlarmServer::getInstance()->init()) { Os::Logger::instance().log(FAC_SUPERVISOR, PRI_ERR, "sipXsupervisor failed to init AlarmServer"); } // Initialize management interfaces on the TLS socket OsSSLServerSocket serverSocket(50, managementPortNumber /*@TODO managementIpBindAddress */); HttpServer httpServer(&serverSocket); // set up but don't start https server XmlRpcDispatch xmlRpcDispatcher(&httpServer); // attach xml-rpc service to https pSipxRpcImpl = new SipxRpc(&xmlRpcDispatcher, allowedPeers); // register xml-rpc methods HttpFileAccess fileAccessService(&httpServer, pSipxRpcImpl); // attach file xfer to https if (serverSocket.isOk()) { Os::Logger::instance().log(FAC_SUPERVISOR, PRI_DEBUG, "Starting Management HTTP Server"); httpServer.start(); } else { Os::Logger::instance().log(FAC_SUPERVISOR, PRI_ERR, "Management listening socket failure"); } // Read the process definitions. UtlString processDefinitionDirectory = SipXecsService::Path(SipXecsService::DataDirType, "process.d"); SipxProcessManager* processManager = SipxProcessManager::getInstance(); processManager->instantiateProcesses(processDefinitionDirectory); // 3.0 had different process def files. The only important thing in them is the // state of the services. Transfer this state to the new process def files. upgradeFrom3_0(); doWaitLoop(); // Successful run. return 0; }