diff --git a/src/managerimpl.cpp b/src/managerimpl.cpp index 63757186c00aec85e4d18b506a52b8c6c901e3ea..76c5587747988c75fdb46fcbf04a991cf6294bf2 100644 --- a/src/managerimpl.cpp +++ b/src/managerimpl.cpp @@ -2285,6 +2285,30 @@ ManagerImpl::getAccount(const AccountID& accountID) return iter->second; } +AccountID +ManagerImpl::getAccountIdFromNameAndServer(const std::string& userName, const std::string& server) +{ + AccountMap::iterator iter; + SIPAccount *account; + + // Try to find the account id from username and server name by full match + for(iter = _accountMap.begin(); iter != _accountMap.end(); ++iter) { + account = dynamic_cast<SIPAccount *>(iter->second); + if(account->fullMatch(userName, server)) + return iter->first; + } + + // We failed! Then only match the username + for(iter = _accountMap.begin(); iter != _accountMap.end(); ++iter) { + account = dynamic_cast<SIPAccount *>(iter->second); + if(account->userMatch(userName)) + return iter->first; + } + + // Failed again! return AccountNULL + return AccountNULL; +} + VoIPLink* ManagerImpl::getAccountLink(const AccountID& accountID) { diff --git a/src/managerimpl.h b/src/managerimpl.h index 9a1c10bd2716ad3c0c74730a95fa78e8f92bcbe5..f3c1e71478ea61327a9be45a0bab71d5d0e29821 100644 --- a/src/managerimpl.h +++ b/src/managerimpl.h @@ -1001,6 +1001,7 @@ class ManagerImpl { */ bool accountExists(const AccountID& accountID); +public: /** * Get an account pointer * @param accountID account ID to get @@ -1008,7 +1009,6 @@ class ManagerImpl { */ Account* getAccount(const AccountID& accountID); -public: /** Return the AccountID from a CallID * Protected by mutex * @param callID the CallID in the list @@ -1023,6 +1023,8 @@ public: */ VoIPLink* getAccountLink(const AccountID& accountID); + AccountID getAccountIdFromNameAndServer(const std::string& userName, const std::string& server); + private: /** * The UserAgent provides sip operation facilities for all sip accounts diff --git a/src/sipaccount.cpp b/src/sipaccount.cpp index 5923493ec2b0156b06d222062a4136c67d0e62ef..9d3aff2f6b362a51f1f36517d2212baaef3f3aca 100644 --- a/src/sipaccount.cpp +++ b/src/sipaccount.cpp @@ -24,10 +24,12 @@ #include "sipvoiplink.h" #include "manager.h" #include "user_cfg.h" +#include "useragent.h" SIPAccount::SIPAccount(const AccountID& accountID) : Account(accountID) { + _cred = NULL; _link = new SIPVoIPLink(accountID); } @@ -36,6 +38,8 @@ SIPAccount::~SIPAccount() { delete _link; _link = NULL; + delete _cred; + _cred = NULL; } void @@ -77,3 +81,16 @@ SIPAccount::loadConfig() // SIP specific //none } + +bool +SIPAccount::fullMatch(const std::string& userName, const std::string& server) +{ + return (userName == _userName && server == _server); +} + +bool +SIPAccount::userMatch(const std::string& userName) +{ + return (userName == _userName); +} + diff --git a/src/sipaccount.h b/src/sipaccount.h index d547f8bbf0463c47b96933728ee78620e0c79d3f..48eb62c7a8e24b6081227e79bad8ef926a662977 100644 --- a/src/sipaccount.h +++ b/src/sipaccount.h @@ -22,6 +22,8 @@ #include "account.h" +struct pjsip_cred_info; + class SIPVoIPLink; /** @@ -58,7 +60,32 @@ public: */ void unregisterVoIPLink(); + + void setUserName(const std::string &name) {_userName = name;} + + std::string getUserName() {return _userName;} + + void setServer(const std::string &server) {_server = server;} + + std::string getServer() {return _server;} + + void setCredInfo(pjsip_cred_info *cred) {_cred = cred;} + + pjsip_cred_info *getCredInfo() {return _cred;} + + void setContact(const std::string contact) {_contact = contact;} + + std::string getContact() {return _contact;} + + bool fullMatch(const std::string& userName, const std::string& server); + + bool userMatch(const std::string& userName); + private: + std::string _userName; + std::string _server; + pjsip_cred_info *_cred; + std::string _contact; }; #endif diff --git a/src/useragent.cpp b/src/useragent.cpp index bc14c3d60b0d2e699b9f912825bf4f5f7e84f4c3..23a4bc334a5b5ceb1e2d50d8346771dce322c658 100644 --- a/src/useragent.cpp +++ b/src/useragent.cpp @@ -23,6 +23,7 @@ #include "sipcall.h" #include "useragent.h" #include "sipvoiplink.h" +#include "sipaccount.h" #define DEFAULT_SIP_PORT 5060 #define RANDOM_SIP_PORT rand() % 64000 + 1024 @@ -289,13 +290,6 @@ void UserAgent::sipDestory() { _thread = NULL; } - // Clear the Account Basic Info List - size = _accBaseInfoList.size(); - for (int i = 0; i < size; i++) { - delete _accBaseInfoList[i]; - } - _accBaseInfoList.clear(); - if (_endpt) { /* Terminate all presence subscriptions. */ //pjsua_pres_shutdown(); @@ -368,6 +362,8 @@ bool UserAgent::addAccount(AccountID id, pjsip_regc **regc2, const std::string& pj_mutex_lock(_mutex); std::string tmp; + SIPAccount *account; + status = pjsip_regc_create(_endpt, (void *) currentId, ®c_cb, ®c); if (status != PJ_SUCCESS) { _debug("UserAgent: Unable to create regc.\n"); @@ -391,18 +387,21 @@ bool UserAgent::addAccount(AccountID id, pjsip_regc **regc2, const std::string& return status; } - AccBaseInfo *info = getAccountInfoFromId(id); - - if(info == NULL) - info = new AccBaseInfo(); + account = dynamic_cast<SIPAccount *> (Manager::instance().getAccount(id)); + pjsip_cred_info *cred = account->getCredInfo(); + + if(!cred) + cred = new pjsip_cred_info(); - pj_bzero(&info->cred, sizeof (info->cred)); - pj_strdup2(_pool, &info->cred.username, user.data()); - info->cred.data_type = PJSIP_CRED_DATA_PLAIN_PASSWD; - pj_strdup2(_pool, &info->cred.data, passwd.data()); - pj_strdup2(_pool, &info->cred.realm, "*"); - pj_strdup2(_pool, &info->cred.scheme, "digest"); - pjsip_regc_set_credentials(regc, 1, &info->cred); + pj_bzero(cred, sizeof (pjsip_cred_info)); + pj_strdup2(_pool, &cred->username, user.data()); + cred->data_type = PJSIP_CRED_DATA_PLAIN_PASSWD; + pj_strdup2(_pool, &cred->data, passwd.data()); + pj_strdup2(_pool, &cred->realm, "*"); + pj_strdup2(_pool, &cred->scheme, "digest"); + pjsip_regc_set_credentials(regc, 1, cred); + + account->setCredInfo(cred); pjsip_tx_data *tdata; status = pjsip_regc_register(regc, PJ_TRUE, &tdata); @@ -417,11 +416,9 @@ bool UserAgent::addAccount(AccountID id, pjsip_regc **regc2, const std::string& return status; } - info->userName = user; - info->server = server; - info->id = id; - pj_strdup2(_pool, &info->contact, contact.ptr); - _accBaseInfoList.push_back(info); + account->setUserName(user); + account->setServer(server); + account->setContact(contactTmp); // associate regc with account *regc2 = regc; @@ -617,6 +614,7 @@ void UserAgent::set_voicemail_info( AccountID account, pjsip_msg_body *body ){ std::string delimiter = "/"; std::string msg_body, voicemail_str; + _debug("UserAgent: checking the voice message!\n"); // The voicemail message is formated like that: // Voice-Message: 1/0 . 1 is the number we want to retrieve in this case @@ -672,11 +670,9 @@ pj_bool_t UserAgent::mod_on_rx_request(pjsip_rx_data *rdata) { std::string server = std::string(sip_uri->host.ptr, sip_uri->host.slen); // Get the account id of callee from username and server - account_id = getInstance()->getAccountIdFromNameAndServer(userName, server); + account_id = Manager::instance().getAccountIdFromNameAndServer(userName, server); if(account_id == AccountNULL) { _debug("UserAgent: Username %s doesn't match any account!\n"); - //delete call; - //call = NULL; return PJ_FALSE; } _debug("UserAgent: The receiver is : %s@%s\n", userName.data(), server.data()); @@ -803,40 +799,6 @@ pj_bool_t UserAgent::mod_on_rx_request(pjsip_rx_data *rdata) { return PJ_SUCCESS; } -AccountID UserAgent::getAccountIdFromNameAndServer(const std::string& userName, const std::string& server) { - int size = _accBaseInfoList.size(); - // Try to find the account id from username and server name by full match - for (int i = 0; i < size; i++) { - if (_accBaseInfoList[i]->userName == userName && - _accBaseInfoList[i]->server == server) { - //_debug("UserAgent: Full match\n"); - return _accBaseInfoList[i]->id; - } - } - - // We failed! Then only match the username - for (int i = 0; i < size; i++) { - if (_accBaseInfoList[i]->userName == userName) { - //_debug("UserAgent: Username match\n"); - return _accBaseInfoList[i]->id; - } - } - - // Failed again! return AccountNULL - return AccountNULL; -} - -UserAgent::AccBaseInfo* UserAgent::getAccountInfoFromId(AccountID id) { - int size = _accBaseInfoList.size(); - for (int i = 0; i < size; i++) { - if (_accBaseInfoList[i]->id == id) { - return _accBaseInfoList[i]; - } - } - - return NULL; -} - bool UserAgent::setCallAudioLocal(SIPCall* call) { // Firstly, we use the local IP and port number unsigned int callLocalAudioPort = RANDOM_LOCAL_PORT; @@ -882,24 +844,25 @@ bool UserAgent::makeOutgoingCall(const std::string& strTo, SIPCall* call, const pj_status_t status; pjsip_dialog *dialog; pjsip_tx_data *tdata; - pj_str_t from, to; + pj_str_t from, to, contact; // Get the basic information about the callee account - AccBaseInfo* accBase = getAccountInfoFromId(id); + SIPAccount* account = dynamic_cast<SIPAccount *>(Manager::instance().getAccount(id)); // Generate the from URI - std::string strFrom = "sip:" + accBase->userName + "@" + accBase->server; + std::string strFrom = "sip:" + account->getUserName() + "@" + account->getServer(); _debug("UserAgent: Make a new call from:%s to %s. Contact is %s\n", - strFrom.data(), strTo.data(), accBase->contact.ptr); + strFrom.data(), strTo.data(), account->getContact().data()); // pjsip need the from and to information in pj_str_t format pj_strdup2(_pool, &from, strFrom.data()); pj_strdup2(_pool, &to, strTo.data()); + pj_strdup2(_pool, &contact, account->getContact().data()); // create the dialog (UAC) status = pjsip_dlg_create_uac(pjsip_ua_instance(), &from, - &accBase->contact, + &contact, &to, NULL, &dialog); @@ -917,7 +880,7 @@ bool UserAgent::makeOutgoingCall(const std::string& strTo, SIPCall* call, const PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); // Set auth information - pjsip_auth_clt_set_credentials(&dialog->auth_sess, 1, &accBase->cred); + pjsip_auth_clt_set_credentials(&dialog->auth_sess, 1, account->getCredInfo()); // Associate current call in the invite session inv->mod_data[_mod.id] = call; diff --git a/src/useragent.h b/src/useragent.h index 3c3273248bd3f147dd620fbc9afe57fe8a05fb89..8251e157421dd5752807dc895f75e20726cc086c 100644 --- a/src/useragent.h +++ b/src/useragent.h @@ -70,16 +70,6 @@ private: static UserAgent *_current; - struct AccBaseInfo { - std::string userName; - std::string server; - AccountID id; - pjsip_cred_info cred; - pj_str_t contact; - }; - - typedef std::vector<AccBaseInfo *> AccBaseInfoList; - AccBaseInfoList _accBaseInfoList; /* Sleep with polling */ void busy_sleep(unsigned msec); void sipDestory(); @@ -117,9 +107,6 @@ public: int getModId() {return _mod.id;} - AccountID getAccountIdFromNameAndServer(const std::string& userName, const std::string& server); - AccBaseInfo* getAccountInfoFromId(AccountID id); - bool setCallAudioLocal(SIPCall* call); int answer(SIPCall* call);