Skip to content
Snippets Groups Projects
Commit 03584a17 authored by Emmanuel Milou's avatar Emmanuel Milou
Browse files

[#2174] Create a default UDP transport + use tp selector for dialogs also

parent 71007b4f
No related branches found
No related tags found
No related merge requests found
...@@ -31,8 +31,10 @@ SIPAccount::SIPAccount (const AccountID& accountID) ...@@ -31,8 +31,10 @@ SIPAccount::SIPAccount (const AccountID& accountID)
, _registrationExpire ("") , _registrationExpire ("")
, _localIpAddress ("") , _localIpAddress ("")
, _publishedIpAddress ("") , _publishedIpAddress ("")
, _actualSessionAddress ("")
, _localPort (atoi (DEFAULT_SIP_PORT)) , _localPort (atoi (DEFAULT_SIP_PORT))
, _publishedPort (atoi (DEFAULT_SIP_PORT)) , _publishedPort (atoi (DEFAULT_SIP_PORT))
, _actualSessionPort (atoi (DEFAULT_SIP_PORT))
, _transportType (PJSIP_TRANSPORT_UNSPECIFIED) , _transportType (PJSIP_TRANSPORT_UNSPECIFIED)
, _resolveOnce (false) , _resolveOnce (false)
, _credentialCount (0) , _credentialCount (0)
...@@ -326,10 +328,12 @@ void SIPAccount::loadConfig() ...@@ -326,10 +328,12 @@ void SIPAccount::loadConfig()
std::string publishedPort = Manager::instance().getConfigString (_accountID, PUBLISHED_PORT); std::string publishedPort = Manager::instance().getConfigString (_accountID, PUBLISHED_PORT);
_localPort = atoi (localPort.c_str()); _localPort = atoi (localPort.c_str());
_actualSessionPort = _localPort;
_publishedPort = atoi (publishedPort.c_str()); _publishedPort = atoi (publishedPort.c_str());
_localIpAddress = Manager::instance().getConfigString (_accountID, LOCAL_ADDRESS); _localIpAddress = Manager::instance().getConfigString (_accountID, LOCAL_ADDRESS);
_actualSessionAddress = _localIpAddress;
_publishedIpAddress = Manager::instance().getConfigString (_accountID, PUBLISHED_ADDRESS); _publishedIpAddress = Manager::instance().getConfigString (_accountID, PUBLISHED_ADDRESS);
......
...@@ -253,6 +253,15 @@ class SIPAccount : public Account ...@@ -253,6 +253,15 @@ class SIPAccount : public Account
inline void setAccountTransport (pjsip_transport *transport) { _transport = transport; } inline void setAccountTransport (pjsip_transport *transport) { _transport = transport; }
inline std::string getSessionAddress () { return _actualSessionAddress; }
inline void setSessionAddress (std::string addr) { _actualSessionAddress = addr; }
inline pj_uint16_t getSessionPort () { return _actualSessionPort; }
inline void setSessionPort (pj_uint16_t port) { _actualSessionPort = port; }
private: private:
/* Maps a string description of the SSL method /* Maps a string description of the SSL method
...@@ -336,6 +345,11 @@ class SIPAccount : public Account ...@@ -336,6 +345,11 @@ class SIPAccount : public Account
// Display Name that can be used in SIP URI. // Display Name that can be used in SIP URI.
std::string _displayName; 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;
pj_uint16_t _actualSessionPort;
}; };
#endif #endif
...@@ -71,7 +71,7 @@ struct result { ...@@ -71,7 +71,7 @@ struct result {
pjsip_server_addresses servers; pjsip_server_addresses servers;
}; };
std::map<AccountID, pjsip_transport *> udp_transports; pjsip_transport *_localUDPTransport;
const pj_str_t STR_USER_AGENT = { (char*) "User-Agent", 10 }; const pj_str_t STR_USER_AGENT = { (char*) "User-Agent", 10 };
...@@ -99,6 +99,9 @@ void handle_incoming_options (pjsip_rx_data *rxdata); ...@@ -99,6 +99,9 @@ void handle_incoming_options (pjsip_rx_data *rxdata);
std::string fetch_header_value (pjsip_msg *msg, std::string field); std::string fetch_header_value (pjsip_msg *msg, std::string field);
std::string getLocalAddressAssociatedToAccount (AccountID id);
/* /*
* The global pool factory * The global pool factory
*/ */
...@@ -465,7 +468,8 @@ int SIPVoIPLink::sendRegister (AccountID id) ...@@ -465,7 +468,8 @@ int SIPVoIPLink::sendRegister (AccountID id)
{ {
status = createUDPServer (id); status = createUDPServer (id);
if (status != PJ_SUCCESS) { if (status != PJ_SUCCESS) {
_debug ("Failed to initialize UDP transport with a local address for account %s\n", id.c_str()); _debug ("Failed to initialize UDP transport with a local address for account %s\n. Try to use the local UDT transport", id.c_str());
account->setAccountTransport (_localUDPTransport);
} }
} }
...@@ -653,6 +657,7 @@ SIPVoIPLink::newOutgoingCall (const CallID& id, const std::string& toUrl) ...@@ -653,6 +657,7 @@ SIPVoIPLink::newOutgoingCall (const CallID& id, const std::string& toUrl)
{ {
SIPAccount * account = NULL; SIPAccount * account = NULL;
pj_status_t status; pj_status_t status;
std::string localAddr;
SIPCall* call = new SIPCall (id, Call::Outgoing, _pool); SIPCall* call = new SIPCall (id, Call::Outgoing, _pool);
...@@ -672,7 +677,8 @@ SIPVoIPLink::newOutgoingCall (const CallID& id, const std::string& toUrl) ...@@ -672,7 +677,8 @@ SIPVoIPLink::newOutgoingCall (const CallID& id, const std::string& toUrl)
call->setPeerNumber (toUri); call->setPeerNumber (toUri);
setCallAudioLocal (call, getLocalIPAddress()); localAddr = account->getSessionAddress ();
setCallAudioLocal (call, localAddr);
try { try {
_debug ("Creating new rtp session in newOutgoingCall\n"); _debug ("Creating new rtp session in newOutgoingCall\n");
...@@ -685,7 +691,8 @@ SIPVoIPLink::newOutgoingCall (const CallID& id, const std::string& toUrl) ...@@ -685,7 +691,8 @@ SIPVoIPLink::newOutgoingCall (const CallID& id, const std::string& toUrl)
_debug ("Try to make a call to: %s with call ID: %s\n", toUrl.data(), id.data()); _debug ("Try to make a call to: %s with call ID: %s\n", toUrl.data(), id.data());
// Building the local SDP offer // Building the local SDP offer
call->getLocalSDP()->set_ip_address (getLocalIP()); // localAddr = getLocalAddressAssociatedToAccount (account->getAccountID());
call->getLocalSDP()->set_ip_address (localAddr);
status = call->getLocalSDP()->create_initial_offer(); status = call->getLocalSDP()->create_initial_offer();
if (status != PJ_SUCCESS) { if (status != PJ_SUCCESS) {
...@@ -1237,6 +1244,7 @@ SIPVoIPLink::SIPStartCall (SIPCall* call, const std::string& subject UNUSED) ...@@ -1237,6 +1244,7 @@ SIPVoIPLink::SIPStartCall (SIPCall* call, const std::string& subject UNUSED)
pjsip_inv_session *inv; pjsip_inv_session *inv;
pjsip_dialog *dialog; pjsip_dialog *dialog;
pjsip_tx_data *tdata; pjsip_tx_data *tdata;
pjsip_transport *transport;
AccountID id; AccountID id;
...@@ -1327,6 +1335,11 @@ SIPVoIPLink::SIPStartCall (SIPCall* call, const std::string& subject UNUSED) ...@@ -1327,6 +1335,11 @@ SIPVoIPLink::SIPStartCall (SIPCall* call, const std::string& subject UNUSED)
// Associate current invite session in the call // Associate current invite session in the call
call->setInvSession (inv); call->setInvSession (inv);
// Set the appropriate transport
pjsip_tpselector *tp;
init_transport_selector (account->getAccountTransport (), &tp);
status = pjsip_dlg_set_transport (dialog, tp);
status = pjsip_inv_send_msg (inv, tdata); status = pjsip_inv_send_msg (inv, tdata);
if (status != PJ_SUCCESS) { if (status != PJ_SUCCESS) {
...@@ -1702,12 +1715,12 @@ bool SIPVoIPLink::pjsip_init() ...@@ -1702,12 +1715,12 @@ bool SIPVoIPLink::pjsip_init()
_localExternPort = _localPort; _localExternPort = _localPort;
// Create a UDP listener meant for all accounts // Create a UDP listener meant for all accounts
// for which TLS was not enabled // for which TLS was not enabled
//errPjsip = createUDPServer(); errPjsip = createUDPServer();
// If the above UDP server // If the above UDP server
// could not be created, then give it another try // could not be created, then give it another try
// on a random sip port // on a random sip port
/*if (errPjsip != PJ_SUCCESS) { if (errPjsip != PJ_SUCCESS) {
_debug ("UserAgent: Could not initialize SIP listener on port %d\n", _localExternPort); _debug ("UserAgent: Could not initialize SIP listener on port %d\n", _localExternPort);
_localExternPort = _localPort = RANDOM_SIP_PORT; _localExternPort = _localPort = RANDOM_SIP_PORT;
...@@ -1718,7 +1731,7 @@ bool SIPVoIPLink::pjsip_init() ...@@ -1718,7 +1731,7 @@ bool SIPVoIPLink::pjsip_init()
_debug ("UserAgent: Fail to initialize SIP listener on port %d\n", _localExternPort); _debug ("UserAgent: Fail to initialize SIP listener on port %d\n", _localExternPort);
return errPjsip; return errPjsip;
} }
}*/ }
_debug ("UserAgent: SIP Init -- listening on port %d\n", _localExternPort); _debug ("UserAgent: SIP Init -- listening on port %d\n", _localExternPort);
...@@ -1894,11 +1907,20 @@ int SIPVoIPLink::createUDPServer (AccountID id) ...@@ -1894,11 +1907,20 @@ int SIPVoIPLink::createUDPServer (AccountID id)
SIPAccount * account = NULL; SIPAccount * account = NULL;
account = dynamic_cast<SIPAccount *> (Manager::instance().getAccount (id)); account = dynamic_cast<SIPAccount *> (Manager::instance().getAccount (id));
// Set information to the local address and port
if (account == NULL) { if (account == NULL) {
_debug ("Account is null. Returning"); // We are trying to initialize a UDP transport available for all local accounts and direct IP calls
return !PJ_SUCCESS; _debug ("Account is null.");
_localExternAddress = _localIPAddress;
_localExternPort = _localPort;
}
else
{
_localExternAddress = account->getSessionAddress ();
_localExternPort = account->getSessionPort ();
} }
// Init bound address to ANY // Init bound address to ANY
pj_memset (&bound_addr, 0, sizeof (bound_addr)); pj_memset (&bound_addr, 0, sizeof (bound_addr));
...@@ -1916,14 +1938,19 @@ int SIPVoIPLink::createUDPServer (AccountID id) ...@@ -1916,14 +1938,19 @@ int SIPVoIPLink::createUDPServer (AccountID id)
if (status != PJ_SUCCESS) { if (status != PJ_SUCCESS) {
_debug ("UserAgent: (%d) Unable to start UDP transport!\n", status); _debug ("UserAgent: (%d) Unable to start UDP transport!\n", status);
return -1; // Try to acquire an existing one
// pjsip_tpmgr_acquire_transport ()
return status;
} else { } else {
_debug ("UserAgent: UDP server listening on port %d\n", _localExternPort); _debug ("UserAgent: UDP server listening on port %d\n", _localExternPort);
if (account == NULL)
_localUDPTransport = transport;
else
account->setAccountTransport (transport);
} }
_debug ("Transport initialized successfully! \n"); _debug ("Transport initialized successfully! \n");
account->setAccountTransport (transport);
return PJ_SUCCESS; return PJ_SUCCESS;
} }
...@@ -1945,10 +1972,15 @@ std::string SIPVoIPLink::findLocalAddressFromUri (const std::string& uri, pjsip_ ...@@ -1945,10 +1972,15 @@ std::string SIPVoIPLink::findLocalAddressFromUri (const std::string& uri, pjsip_
std::string machineName (pjMachineName.ptr, pjMachineName.slen); std::string machineName (pjMachineName.ptr, pjMachineName.slen);
if (genericUri == NULL) { if (genericUri == NULL) {
_debug ("genericUri is NULL in findLocalPortFromUri\n"); _debug ("genericUri is NULL in findLocalAddressFromUri\n");
return machineName; return machineName;
} }
if (transport == NULL) {
_debug ("transport is NULL in findLocalAddressFromUri\n. Try the local UDP transport");
transport = _localUDPTransport;
}
pjsip_sip_uri * sip_uri = NULL; pjsip_sip_uri * sip_uri = NULL;
sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri (genericUri); sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri (genericUri);
...@@ -1982,7 +2014,7 @@ std::string SIPVoIPLink::findLocalAddressFromUri (const std::string& uri, pjsip_ ...@@ -1982,7 +2014,7 @@ std::string SIPVoIPLink::findLocalAddressFromUri (const std::string& uri, pjsip_
pj_status_t status; pj_status_t status;
/* Init the transport selector */ /* Init the transport selector */
_debug ("Transport ID: %s\n", transport->obj_name); //_debug ("Transport ID: %s\n", transport->obj_name);
status = init_transport_selector (transport, &tp_sel); status = init_transport_selector (transport, &tp_sel);
if (status == PJ_SUCCESS) if (status == PJ_SUCCESS)
...@@ -2034,6 +2066,11 @@ int SIPVoIPLink::findLocalPortFromUri (const std::string& uri, pjsip_transport * ...@@ -2034,6 +2066,11 @@ int SIPVoIPLink::findLocalPortFromUri (const std::string& uri, pjsip_transport *
return atoi (DEFAULT_SIP_PORT); return atoi (DEFAULT_SIP_PORT);
} }
if (transport == NULL) {
_debug ("transport is NULL in findLocalPortFromUri - Try the local UDP transport\n");
transport = _localUDPTransport;
}
pjsip_sip_uri * sip_uri = NULL; pjsip_sip_uri * sip_uri = NULL;
sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri (genericUri); sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri (genericUri);
...@@ -2189,6 +2226,11 @@ pj_status_t SIPVoIPLink::createAlternateUdpTransport (AccountID id) ...@@ -2189,6 +2226,11 @@ pj_status_t SIPVoIPLink::createAlternateUdpTransport (AccountID id)
_localExternAddress = std::string (a_name.host.ptr); _localExternAddress = std::string (a_name.host.ptr);
_localExternPort = (int)a_name.port; _localExternPort = (int)a_name.port;
// Set the address to be used in SDP
account->setSessionAddress (_localExternAddress);
account->setSessionPort (_localExternPort);
//account->setStunServerName (a_name.host); //account->setStunServerName (a_name.host);
//account->setStunPort (a_name.port); //account->setStunPort (a_name.port);
...@@ -2776,6 +2818,7 @@ mod_on_rx_request (pjsip_rx_data *rdata) ...@@ -2776,6 +2818,7 @@ mod_on_rx_request (pjsip_rx_data *rdata)
CallID id; CallID id;
SIPCall* call; SIPCall* call;
pjsip_inv_session *inv; pjsip_inv_session *inv;
SIPAccount *account;
pjmedia_sdp_session *r_sdp; pjmedia_sdp_session *r_sdp;
// voicemail part // voicemail part
...@@ -2908,12 +2951,21 @@ mod_on_rx_request (pjsip_rx_data *rdata) ...@@ -2908,12 +2951,21 @@ mod_on_rx_request (pjsip_rx_data *rdata)
return false; return false;
} }
std::string addrToUse;
account = dynamic_cast<SIPAccount *> (Manager::instance().getAccount (account_id));
if (account != NULL) {
addrToUse = account->getSessionAddress ();
}
else
addrToUse = link->getLocalIPAddress();
// Have to do some stuff with the SDP // Have to do some stuff with the SDP
// Set the codec map, IP, peer number and so on... for the SIPCall object // Set the codec map, IP, peer number and so on... for the SIPCall object
setCallAudioLocal (call, link->getLocalIPAddress()); setCallAudioLocal (call, addrToUse);
// We retrieve the remote sdp offer in the rdata struct to begin the negociation // We retrieve the remote sdp offer in the rdata struct to begin the negociation
call->getLocalSDP()->set_ip_address (link->getLocalIPAddress()); call->getLocalSDP()->set_ip_address (addrToUse);
get_remote_sdp_from_offer (rdata, &r_sdp); get_remote_sdp_from_offer (rdata, &r_sdp);
...@@ -3603,3 +3655,41 @@ pj_bool_t stun_sock_on_rx_data (pj_stun_sock *stun_sock, void *pkt, unsigned pkt ...@@ -3603,3 +3655,41 @@ pj_bool_t stun_sock_on_rx_data (pj_stun_sock *stun_sock, void *pkt, unsigned pkt
{ {
return PJ_TRUE; return PJ_TRUE;
} }
std::string getLocalAddressAssociatedToAccount (AccountID id)
{
SIPAccount *account = NULL;
pj_sockaddr_in local_addr_ipv4;
pjsip_transport *tspt;
std::string localAddr;
pj_str_t tmp;
account = dynamic_cast<SIPAccount *> (Manager::instance().getAccount (id));
// Set the local address
if (account != NULL) {
tspt = account->getAccountTransport ();
if (tspt != NULL) {
local_addr_ipv4 = tspt->local_addr.ipv4;
}
else {
_debug ("In getLocalAddressAssociatedToAccount: transport is null");
local_addr_ipv4 = _localUDPTransport->local_addr.ipv4;
}
}
else {
_debug ("In getLocalAddressAssociatedToAccount: account is null");
local_addr_ipv4 = _localUDPTransport->local_addr.ipv4;
}
_debug ("slbvasjklbvaskbvaskvbaskvaskvbsdfk: %i\n", local_addr_ipv4.sin_addr.s_addr);
tmp = pj_str (pj_inet_ntoa (local_addr_ipv4.sin_addr));
localAddr = std::string (tmp.ptr);
_debug ("slbvasjklbvaskbvaskvbaskvaskvbsdfk: %s\n", localAddr.c_str());
return localAddr;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment