Skip to content
Snippets Groups Projects
Commit 1a68b3ad authored by Alexandre Bourget's avatar Alexandre Bourget
Browse files

Implement registration refreshing for IAX. Fixes #15

parent 080f42c9
No related branches found
No related tags found
No related merge requests found
...@@ -52,7 +52,7 @@ IAXAccount::registerAccount() ...@@ -52,7 +52,7 @@ IAXAccount::registerAccount()
{ {
if (_link && !_registered) { if (_link && !_registered) {
init(); init();
unregisterAccount(); //unregisterAccount(); No need to unregister first.
IAXVoIPLink* tmplink = dynamic_cast<IAXVoIPLink*> (_link); IAXVoIPLink* tmplink = dynamic_cast<IAXVoIPLink*> (_link);
if (tmplink) { if (tmplink) {
// Stuff needed for IAX registration // Stuff needed for IAX registration
......
...@@ -53,6 +53,7 @@ IAXVoIPLink::IAXVoIPLink(const AccountID& accountID) ...@@ -53,6 +53,7 @@ IAXVoIPLink::IAXVoIPLink(const AccountID& accountID)
{ {
_evThread = new EventThread(this); _evThread = new EventThread(this);
_regSession = NULL; _regSession = NULL;
_nextRefreshStamp = 0;
// to get random number for RANDOM_PORT // to get random number for RANDOM_PORT
srand (time(NULL)); srand (time(NULL));
...@@ -279,7 +280,10 @@ IAXVoIPLink::getEvent() ...@@ -279,7 +280,10 @@ IAXVoIPLink::getEvent()
_mutexIAX.leaveMutex(); _mutexIAX.leaveMutex();
} }
//iaxRefreshRegistrations(); // Refresh registration.
if (_nextRefreshStamp && _nextRefreshStamp - 2 < time(NULL)) {
setRegister();
}
// thread wait 5 millisecond // thread wait 5 millisecond
_evThread->sleep(5); _evThread->sleep(5);
...@@ -305,7 +309,7 @@ bool ...@@ -305,7 +309,7 @@ bool
IAXVoIPLink::setRegister() IAXVoIPLink::setRegister()
{ {
bool result = false; bool result = false;
if (_regSession == NULL) {
if (_host.empty()) { if (_host.empty()) {
Manager::instance().displayConfigError("Fill host field for IAX Account"); Manager::instance().displayConfigError("Fill host field for IAX Account");
return false; return false;
...@@ -317,10 +321,13 @@ IAXVoIPLink::setRegister() ...@@ -317,10 +321,13 @@ IAXVoIPLink::setRegister()
// lock // lock
_mutexIAX.enterMutex(); _mutexIAX.enterMutex();
_regSession = iax_session_new();
if (_regSession == NULL) {
_regSession = iax_session_new();
}
if (!_regSession) { if (!_regSession) {
_debug("error when generating new session for register"); _debug("Error when generating new session for register");
} else { } else {
// refresh // refresh
// last reg // last reg
...@@ -330,16 +337,20 @@ IAXVoIPLink::setRegister() ...@@ -330,16 +337,20 @@ IAXVoIPLink::setRegister()
strcpy(user, _user.c_str()); strcpy(user, _user.c_str());
char pass[_pass.length()+1]; char pass[_pass.length()+1];
strcpy(pass, _pass.c_str()); strcpy(pass, _pass.c_str());
//iax_register don't use const char* // iax_register doesn't use const char*
_debug("IAX Sending registration to %s with user %s\n", host, user); _debug("IAX Sending registration to %s with user %s\n", host, user);
int val = iax_register(_regSession, host, user, pass, 300); int val = iax_register(_regSession, host, user, pass, 120);
_debug ("Return value: %d\n", val); _debug ("Return value: %d\n", val);
// set the time-out to 15 seconds, after that, resend a registration request.
// until we unregister.
_nextRefreshStamp = time(NULL) + 10;
result = true; result = true;
} }
// unlock
_mutexIAX.leaveMutex(); _mutexIAX.leaveMutex();
}
return result; return result;
} }
...@@ -350,14 +361,16 @@ bool ...@@ -350,14 +361,16 @@ bool
IAXVoIPLink::setUnregister() IAXVoIPLink::setUnregister()
{ {
if (_regSession) { if (_regSession) {
// lock here /** @todo Should send a REGREL in setUnregister()... */
_mutexIAX.enterMutex(); _mutexIAX.enterMutex();
//iax_send_regrel(); doesn't exist yet :)
iax_destroy(_regSession); iax_destroy(_regSession);
_regSession = NULL;
// unlock here
_mutexIAX.leaveMutex(); _mutexIAX.leaveMutex();
return false;
_regSession = NULL;
} }
_nextRefreshStamp = 0;
return false; return false;
} }
...@@ -738,13 +751,24 @@ IAXVoIPLink::iaxHandleRegReply(iax_event* event) ...@@ -738,13 +751,24 @@ IAXVoIPLink::iaxHandleRegReply(iax_event* event)
{ {
if (event->etype == IAX_EVENT_REGREJ) { if (event->etype == IAX_EVENT_REGREJ) {
/* Authentication failed! */ /* Authentication failed! */
_mutexIAX.enterMutex();
iax_destroy(_regSession); iax_destroy(_regSession);
_mutexIAX.leaveMutex();
_regSession = NULL; _regSession = NULL;
Manager::instance().registrationFailed(getAccountID());
Manager::instance().registrationFailed(getAccountID());
} }
else if (event->etype == IAX_EVENT_REGACK) { else if (event->etype == IAX_EVENT_REGACK) {
/* Authentication succeeded */ /* Authentication succeeded */
_mutexIAX.enterMutex();
iax_destroy(_regSession);
_mutexIAX.leaveMutex();
_regSession = NULL;
// I mean, save the timestamp, so that we re-register again in the REFRESH time.
// Defaults to 60, as per draft-guy-iax-03.
_nextRefreshStamp = time(NULL) + (event->ies.refresh ? event->ies.refresh : 60);
Manager::instance().registrationSucceed(getAccountID()); Manager::instance().registrationSucceed(getAccountID());
} }
} }
......
...@@ -49,6 +49,11 @@ public: ...@@ -49,6 +49,11 @@ public:
bool checkNetwork (void) { return false; } bool checkNetwork (void) { return false; }
void terminate (void); void terminate (void);
/**
* Send out registration
*
* @return The new registration state (are we registered ?)
*/
bool setRegister (void); bool setRegister (void);
/** /**
...@@ -155,8 +160,16 @@ private: ...@@ -155,8 +160,16 @@ private:
/** IAX full name */ /** IAX full name */
std::string _fullName; std::string _fullName;
/** Timestamp of when we should refresh the registration up with
* the registrar. Values can be: EPOCH timestamp, 0 if we want no registration, 1
* to force a registration. */
int _nextRefreshStamp;
/** Mutex for iax_ calls, since we're the only one dealing with the incorporated
* iax_stuff inside this class. */
ost::Mutex _mutexIAX; ost::Mutex _mutexIAX;
/** Connection to audio card/device */
AudioLayer* audiolayer; AudioLayer* audiolayer;
/** When we receive data, we decode it inside this buffer */ /** When we receive data, we decode it inside this buffer */
...@@ -164,8 +177,8 @@ private: ...@@ -164,8 +177,8 @@ private:
/** When we send data, we encode it inside this buffer*/ /** When we send data, we encode it inside this buffer*/
unsigned char* _sendDataEncoded; unsigned char* _sendDataEncoded;
/** After that we send the data inside this buffer if there is a format conversion or rate conversion */ /** After that we send the data inside this buffer if there is a format conversion or rate conversion. */
/** Also use for getting mic-ringbuffer data */ /* Also use for getting mic-ringbuffer data */
SFLDataFormat* _dataAudioLayer; SFLDataFormat* _dataAudioLayer;
/** Buffer for 8000hz samples in conversion */ /** Buffer for 8000hz samples in conversion */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment