diff --git a/sflphone-common/src/managerimpl.cpp b/sflphone-common/src/managerimpl.cpp index 9c7b36c7d82111b20aa3934da28a6599ce380add..9dde9c3752c4c21ac9f21cfe0c6ae81609b51e75 100644 --- a/sflphone-common/src/managerimpl.cpp +++ b/sflphone-common/src/managerimpl.cpp @@ -3232,6 +3232,9 @@ void ManagerImpl::setSipPort (int port) int ManagerImpl::getSipPort (void) { + int port; + + // port = getConfigInt (IP2IP , CONFIG_SIP_PORT) return getConfigInt (PREFERENCES , CONFIG_SIP_PORT); } @@ -4145,6 +4148,14 @@ ManagerImpl::loadAccountMap() TokenList::iterator iter = sections.begin(); + // Those calls that are placed to an uri that cannot be + // associated to an account are using that special account. + // An account, that is not account, in the sense of + // registration. This is useful since the Account object + // provides a handful of method that simplifies URI creation + // and loading of various settings. + _directIpAccount = AccountCreator::createAccount (AccountCreator::SIP_DIRECT_IP_ACCOUNT, ""); + while (iter != sections.end()) { // Check if it starts with "Account:" (SIP and IAX pour le moment) if ( (int) (iter->find ("Account:")) != 0) { @@ -4175,20 +4186,13 @@ ManagerImpl::loadAccountMap() iter++; } - // Those calls that are placed to an uri that cannot be - // associated to an account are using that special account. - // An account, that is not account, in the sense of - // registration. This is useful since the Account object - // provides a handful of method that simplifies URI creation - // and loading of various settings. - _directIpAccount = AccountCreator::createAccount (AccountCreator::SIP_DIRECT_IP_ACCOUNT, ""); - if (_directIpAccount == NULL) { _debug ("Failed to create direct ip calls \"account\"\n"); } else { // Force the options to be loaded // No registration in the sense of // the REGISTER method is performed. + _debug ("Succeed to create direct ip calls \"account\"\n"); _directIpAccount->registerVoIPLink(); } @@ -4233,6 +4237,7 @@ ManagerImpl::getAccount (const AccountID& accountID) // In our definition, // this is the "direct ip calls account" if (accountID == AccountNULL) { + _debug ("Returns the direct IP account\n"); return _directIpAccount; } diff --git a/sflphone-common/src/sip/sipaccount.cpp b/sflphone-common/src/sip/sipaccount.cpp index b7ea7760c44e4300c6b532c23d1349b768133b9e..f3e99400ea406c80abd60e7a477980dc2416a6d6 100644 --- a/sflphone-common/src/sip/sipaccount.cpp +++ b/sflphone-common/src/sip/sipaccount.cpp @@ -328,21 +328,15 @@ void SIPAccount::loadConfig() } // Load network settings + // Local parameters std::string localPort = Manager::instance().getConfigString (_accountID, LOCAL_PORT); + setLocalPort (atoi (localPort.c_str())); + setLocalAddress (Manager::instance().getConfigString (_accountID, LOCAL_ADDRESS)); + // Published parameters std::string publishedPort = Manager::instance().getConfigString (_accountID, PUBLISHED_PORT); - - _localPort = atoi (localPort.c_str()); - - _actualSessionPort = _localPort; - - _publishedPort = atoi (publishedPort.c_str()); - - _localIpAddress = Manager::instance().getConfigString (_accountID, LOCAL_ADDRESS); - - _actualSessionAddress = _localIpAddress; - - _publishedIpAddress = Manager::instance().getConfigString (_accountID, PUBLISHED_ADDRESS); + setPublishedPort (atoi (publishedPort.c_str())); + setPublishedAddress (Manager::instance().getConfigString (_accountID, PUBLISHED_ADDRESS)); // Init TLS settings if the user wants to use TLS bool tlsEnabled = Manager::instance().getConfigBool (_accountID, TLS_ENABLE); diff --git a/sflphone-common/src/sip/sipaccount.h b/sflphone-common/src/sip/sipaccount.h index 8ad7451fb2b89b4ff74237e1018bc7db4ffe6c21..7a5b9af4be3a508d54954b6c41a8e037f843ac4e 100644 --- a/sflphone-common/src/sip/sipaccount.h +++ b/sflphone-common/src/sip/sipaccount.h @@ -321,7 +321,7 @@ class SIPAccount : public Account pjsip_transport_type_e _transportType; - pjsip_transport* _transport; + pjsip_transport* _transport; // Special hack that is not here to stay // See #1852 @@ -340,15 +340,16 @@ class SIPAccount : public Account // The STUN server name, if applicable pj_str_t _stunServerName; - // The STUN server port, if applicable - pj_uint16_t _stunPort; + // The STUN server port, if applicable + pj_uint16_t _stunPort; // Display Name that can be used in SIP URI. std::string _displayName; - // The actual address we use in the SDP to be contacted - // it needs to be per account, otherwise the same address is used for every account - std::string _actualSessionAddress; + // The actual address we use in the SDP to be contacted + // it needs to be per account, otherwise the same address is used for every account + // TODO Use the published address or the local address + std::string _actualSessionAddress; pj_uint16_t _actualSessionPort; }; diff --git a/sflphone-common/src/sip/sipvoiplink.cpp b/sflphone-common/src/sip/sipvoiplink.cpp index be7c8bc7f2bc8f44e72f64f3e4729c736d62bf43..e49b33cf07c660e5dc8ff5df29d102fa0ce103e0 100644 --- a/sflphone-common/src/sip/sipvoiplink.cpp +++ b/sflphone-common/src/sip/sipvoiplink.cpp @@ -248,6 +248,7 @@ SIPVoIPLink* SIPVoIPLink::instance (const AccountID& id) { if (!_instance) { + _debug ("Create new SIPVoIPLink instance\n"); _instance = new SIPVoIPLink (id); } @@ -269,6 +270,8 @@ bool SIPVoIPLink::init() if (initDone()) return false; + // TODO This port should be the one configured for the IP profile + // and not the global one _regPort = Manager::instance().getSipPort(); /* Instanciate the C++ thread */ @@ -473,7 +476,7 @@ int SIPVoIPLink::sendRegister (AccountID id) status = createUDPServer (id); if (status != PJ_SUCCESS) { - _debug ("Failed to initialize UDP transport with a local address for account %s\n. Try to use the local UDP transport", id.c_str()); + _debug ("Use the local UDP transport", id.c_str()); account->setAccountTransport (_localUDPTransport); } } @@ -676,8 +679,9 @@ SIPVoIPLink::newOutgoingCall (const CallID& id, const std::string& toUrl) call->setPeerNumber (toUri); - localAddr = account->getSessionAddress (); - setCallAudioLocal (call, call->getLocalIp()); + // TODO May use the published address as well + localAddr = account->getLocalAddress (); + setCallAudioLocal (call, localAddr); try { _debug ("Creating new rtp session in newOutgoingCall\n"); @@ -1429,13 +1433,13 @@ bool SIPVoIPLink::new_ip_to_ip_call (const CallID& id, const std::string& to) pjsip_dialog *dialog; pjsip_inv_session *inv; pjsip_tx_data *tdata; + std::string localAddress; /* Create the call */ call = new SIPCall (id, Call::Outgoing, _pool); if (call) { call->setCallConfiguration (Call::IPtoIP); - setCallAudioLocal (call, getLocalIPAddress()); call->initRecFileName(); AccountID accountId = Manager::instance().getAccountFromCall (id); @@ -1446,6 +1450,14 @@ bool SIPVoIPLink::new_ip_to_ip_call (const CallID& id, const std::string& to) _debug ("Account is null. Returning\n"); return !PJ_SUCCESS; } + + // Set SDP parameters + localAddress = account->getLocalAddress (); + if (localAddress == "0.0.0.0"){ + _debug ("Here is the local address: %s\n", localAddress.c_str ()); + loadSIPLocalIP (&localAddress); + } + setCallAudioLocal (call, localAddress); _debug ("toUri received in new_ip_to_ip call %s\n", to.c_str()); @@ -1453,7 +1465,7 @@ bool SIPVoIPLink::new_ip_to_ip_call (const CallID& id, const std::string& to) call->setPeerNumber (toUri); _debug ("toUri in new_ip_to_ip call %s\n", toUri.c_str()); // Building the local SDP offer - call->getLocalSDP()->set_ip_address (getLocalIPAddress ()); + call->getLocalSDP()->set_ip_address (localAddress); call->getLocalSDP()->create_initial_offer(); try { @@ -1647,6 +1659,7 @@ bool SIPVoIPLink::pjsip_init() pj_str_t accepted; std::string name_mod; pj_dns_resolver *p_resv; + std::string addr; name_mod = "sflphone"; @@ -1682,17 +1695,13 @@ bool SIPVoIPLink::pjsip_init() PJ_ASSERT_RETURN (status == PJ_SUCCESS, 1); - // Start resolving STUN server - // if we useStun and we failed to receive something on port 5060, we try a random port - // If use STUN server, firewall address setup - if (!loadSIPLocalIP()) { + if (!loadSIPLocalIP (&addr)) { _debug ("UserAgent: Unable to determine network capabilities\n"); return false; } port = _regPort; - - _localPort = port; + // _localPort = port; // Retrieve Direct IP Calls settings. // This corresponds to the accountID set to @@ -1707,9 +1716,9 @@ bool SIPVoIPLink::pjsip_init() directIpCallsTlsEnabled = account->isTlsEnabled(); } - _localExternAddress = _localIPAddress; + //_localExternAddress = _localIPAddress; - _localExternPort = _localPort; + //_localExternPort = _localPort; // Create a UDP listener meant for all accounts // for which TLS was not enabled errPjsip = createUDPServer(); @@ -1902,7 +1911,12 @@ int SIPVoIPLink::createUDPServer (AccountID id) pjsip_host_port a_name; char tmpIP[32]; pjsip_transport *transport; + std::string listeningAddress = "127.0.0.1"; + int listeningPort = _regPort; + /* Use my local address as default value */ + if (!loadSIPLocalIP (&listeningAddress)) + return !PJ_SUCCESS; /* * Retrieve the account information @@ -1913,13 +1927,14 @@ int SIPVoIPLink::createUDPServer (AccountID id) // Set information to the local address and port if (account == NULL) { - // We are trying to initialize a UDP transport available for all local accounts and direct IP calls _debug ("Account is null in createUDPServer.\n"); - _localExternAddress = _localIPAddress; - _localExternPort = _localPort; } else { - _localExternAddress = account->getSessionAddress (); - _localExternPort = account->getSessionPort (); + // We are trying to initialize a UDP transport available for all local accounts and direct IP calls + if (account->getLocalAddress () != "0.0.0.0"){ + listeningAddress = account->getLocalAddress (); + _debug ("**************************************** lsitening -%s-\n", listeningAddress.data()); + } + listeningPort = account->getLocalPort (); } // Init bound address to ANY @@ -1927,22 +1942,21 @@ int SIPVoIPLink::createUDPServer (AccountID id) bound_addr.sin_addr.s_addr = pj_htonl (PJ_INADDR_ANY); - bound_addr.sin_port = pj_htons ( (pj_uint16_t) _localPort); + bound_addr.sin_port = pj_htons ( (pj_uint16_t) listeningPort); bound_addr.sin_family = PJ_AF_INET; pj_bzero (bound_addr.sin_zero, sizeof (bound_addr.sin_zero)); // Create UDP-Server (default port: 5060) - strcpy (tmpIP, _localExternAddress.data()); + strcpy (tmpIP, listeningAddress.data()); pj_strdup2 (_pool, &a_name.host, tmpIP); - a_name.port = (pj_uint16_t) _localExternPort; + a_name.port = (pj_uint16_t) listeningPort; status = pjsip_udp_transport_start (_endpt, &bound_addr, &a_name, 1, &transport); - // Get the transport manager associated with // this endpoint pjsip_tpmgr * tpmgr = NULL; @@ -1959,16 +1973,15 @@ int SIPVoIPLink::createUDPServer (AccountID id) // pjsip_tpmgr_acquire_transport () return status; } else { - _debug ("UserAgent: UDP server listening on port %d\n", _localExternPort); + _debug ("UserAgent: UDP server listening on port %d\n", listeningPort); if (account == NULL) _localUDPTransport = transport; else - account->setAccountTransport (transport); + account->setAccountTransport (transport); } - _debug ("Transport initialized successfully! \n"); - + _debug ("Transport initialized successfully on %s:%i\n", listeningAddress.c_str (), listeningPort); return PJ_SUCCESS; } @@ -2050,7 +2063,7 @@ std::string SIPVoIPLink::findLocalAddressFromUri (const std::string& uri, pjsip_ return machineName; } - // _debug ("Local ADdress From URI: %s\n", localAddress.ptr); + _debug ("Local address discovered from attached transport: %s\n", localAddress.ptr); return std::string (localAddress.ptr, localAddress.slen); } @@ -2146,6 +2159,7 @@ int SIPVoIPLink::findLocalPortFromUri (const std::string& uri, pjsip_transport * _debug ("Failed to find local address from transport\n"); } + _debug ("Local port discovered from attached transport: %i\n", port); return port; } @@ -2357,24 +2371,23 @@ pj_status_t SIPVoIPLink::createTlsTransport (AccountID id) return PJ_SUCCESS; } -bool SIPVoIPLink::loadSIPLocalIP() +bool SIPVoIPLink::loadSIPLocalIP (std::string *addr) { bool returnValue = true; + std::string localAddress = "127.0.0.1"; + pj_sockaddr ip_addr; - if (_localIPAddress == "127.0.0.1") { - pj_sockaddr ip_addr; - - if (pj_gethostip (pj_AF_INET(), &ip_addr) != PJ_SUCCESS) { - // Update the registration state if no network capabilities found - _debug ("UserAgent: Get host ip failed!\n"); - returnValue = false; - } else { - _localIPAddress = std::string (pj_inet_ntoa (ip_addr.ipv4.sin_addr)); - _debug ("UserAgent: Checking network, setting local IP address to: %s\n", _localIPAddress.data()); - } + if (pj_gethostip (pj_AF_INET(), &ip_addr) != PJ_SUCCESS) { + // Update the registration state if no network capabilities found + _debug ("UserAgent: Get host ip failed!\n"); + returnValue = false; + } else { + localAddress = std::string (pj_inet_ntoa (ip_addr.ipv4.sin_addr)); + _debug ("UserAgent: Checking network, setting local IP address to: %s\n", localAddress.data()); } + *addr = localAddress; return returnValue; } @@ -3025,18 +3038,18 @@ mod_on_rx_request (pjsip_rx_data *rdata) account = dynamic_cast<SIPAccount *> (Manager::instance().getAccount (account_id)); if (account != NULL) { - if (account_id == AccountNULL) - addrToUse = link->getLocalIPAddress(); - else - addrToUse = account->getSessionAddress (); - } else { - addrToUse = link->getLocalIPAddress(); + // TODO May use the published address as well + addrToUse = account->getLocalAddress (); } + if (addrToUse == "0.0.0.0") + { + link->loadSIPLocalIP (&addrToUse); + } - // Have to do some stuff with the SDP - // Set the codec map, IP, peer number and so on... for the SIPCall object - setCallAudioLocal (call, link->getLocalIPAddress()); + // Have to do some stuff with the SDP + // Set the codec map, IP, peer number and so on... for the SIPCall object + setCallAudioLocal (call, addrToUse); // We retrieve the remote sdp offer in the rdata struct to begin the negociation call->getLocalSDP()->set_ip_address (addrToUse); diff --git a/sflphone-common/src/sip/sipvoiplink.h b/sflphone-common/src/sip/sipvoiplink.h index 6d437d4bf78354c2d4f5e0a925b49d633be54b0a..f29bf679e080ed6e49986c7c008120c830aeaa9e 100644 --- a/sflphone-common/src/sip/sipvoiplink.h +++ b/sflphone-common/src/sip/sipvoiplink.h @@ -295,6 +295,8 @@ class SIPVoIPLink : public VoIPLink */ pj_status_t init_transport_selector (pjsip_transport *transport, pjsip_tpselector **tp_sel); + bool loadSIPLocalIP (std::string *addr); + private: /** * Constructor @@ -364,16 +366,8 @@ class SIPVoIPLink : public VoIPLink pj_status_t createAlternateUdpTransport (AccountID id); - bool loadSIPLocalIP(); - std::string getLocalIP() {return _localExternAddress;} - /* Flag to check if the STUN server is valid or not */ - bool validStunServer; - - /** The current STUN server address */ - std::string _stunServer; - /** Local Extern Address is the IP address seen by peers for SIP listener */ std::string _localExternAddress; @@ -383,9 +377,6 @@ class SIPVoIPLink : public VoIPLink /** For registration use only */ int _regPort; - /** Do we use stun? */ - bool _useStun; - /** Threading object */ EventThread* _evThread; ost::Mutex _mutexSIP; diff --git a/sflphone-common/src/voiplink.cpp b/sflphone-common/src/voiplink.cpp index 9bf46226d3d6509d7df3c40d786d92c5852bd7e0..2ebc8d6723a6c9e5062dc7cfbe18310e5429c635 100644 --- a/sflphone-common/src/voiplink.cpp +++ b/sflphone-common/src/voiplink.cpp @@ -24,7 +24,7 @@ #include "voiplink.h" #include "manager.h" -VoIPLink::VoIPLink (const AccountID& accountID) : _accountID (accountID), _localIPAddress ("127.0.0.1"), _localPort (0), _initDone (false) +VoIPLink::VoIPLink (const AccountID& accountID) : _accountID (accountID), _localPort (0), _initDone (false) { } diff --git a/sflphone-common/src/voiplink.h b/sflphone-common/src/voiplink.h index 09ea20b3c2708f63a6b0a38053a214755843c5fa..16de9afdae71cee6a9a0f146434206f9f0e943f1 100644 --- a/sflphone-common/src/voiplink.h +++ b/sflphone-common/src/voiplink.h @@ -181,8 +181,6 @@ class VoIPLink { bool initDone (void) { return _initDone; } void initDone (bool state) { _initDone = state; } - std::string getLocalIPAddress (void) { return _localIPAddress; } - /** Add a call to the call map (protected by mutex) * @param call A call pointer with a unique pointer * @return bool True if the call was unique and added @@ -233,9 +231,6 @@ class VoIPLink { /** Mutex to protect call map */ ost::Mutex _callMapMutex; - /** Get Local IP Address (ie: 127.0.0.1, 192.168.0.1, ...) */ - std::string _localIPAddress; - /** Get local listening port (5060 for SIP, ...) */ unsigned int _localPort;