Commit 1a68b3ad authored by Alexandre Bourget's avatar Alexandre Bourget
Browse files

Implement registration refreshing for IAX. Fixes #15

parent 080f42c9
......@@ -52,7 +52,7 @@ IAXAccount::registerAccount()
{
if (_link && !_registered) {
init();
unregisterAccount();
//unregisterAccount(); No need to unregister first.
IAXVoIPLink* tmplink = dynamic_cast<IAXVoIPLink*> (_link);
if (tmplink) {
// Stuff needed for IAX registration
......
......@@ -53,6 +53,7 @@ IAXVoIPLink::IAXVoIPLink(const AccountID& accountID)
{
_evThread = new EventThread(this);
_regSession = NULL;
_nextRefreshStamp = 0;
// to get random number for RANDOM_PORT
srand (time(NULL));
......@@ -279,7 +280,10 @@ IAXVoIPLink::getEvent()
_mutexIAX.leaveMutex();
}
//iaxRefreshRegistrations();
// Refresh registration.
if (_nextRefreshStamp && _nextRefreshStamp - 2 < time(NULL)) {
setRegister();
}
// thread wait 5 millisecond
_evThread->sleep(5);
......@@ -305,41 +309,48 @@ bool
IAXVoIPLink::setRegister()
{
bool result = false;
if (_regSession == NULL) {
if (_host.empty()) {
Manager::instance().displayConfigError("Fill host field for IAX Account");
return false;
}
if (_user.empty()) {
Manager::instance().displayConfigError("Fill user field for IAX Account");
return false;
}
// lock
_mutexIAX.enterMutex();
_regSession = iax_session_new();
if (_host.empty()) {
Manager::instance().displayConfigError("Fill host field for IAX Account");
return false;
}
if (_user.empty()) {
Manager::instance().displayConfigError("Fill user field for IAX Account");
return false;
}
if (!_regSession) {
_debug("error when generating new session for register");
} else {
// refresh
// last reg
char host[_host.length()+1];
strcpy(host, _host.c_str());
char user[_user.length()+1];
strcpy(user, _user.c_str());
char pass[_pass.length()+1];
strcpy(pass, _pass.c_str());
//iax_register don't use const char*
_debug("IAX Sending registration to %s with user %s\n", host, user);
int val = iax_register(_regSession, host, user, pass, 300);
_debug ("Return value: %d\n", val);
result = true;
}
// lock
_mutexIAX.enterMutex();
_mutexIAX.leaveMutex();
if (_regSession == NULL) {
_regSession = iax_session_new();
}
if (!_regSession) {
_debug("Error when generating new session for register");
} else {
// refresh
// last reg
char host[_host.length()+1];
strcpy(host, _host.c_str());
char user[_user.length()+1];
strcpy(user, _user.c_str());
char pass[_pass.length()+1];
strcpy(pass, _pass.c_str());
// iax_register doesn't use const char*
_debug("IAX Sending registration to %s with user %s\n", host, user);
int val = iax_register(_regSession, host, user, pass, 120);
_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;
}
// unlock
_mutexIAX.leaveMutex();
return result;
}
......@@ -350,14 +361,16 @@ bool
IAXVoIPLink::setUnregister()
{
if (_regSession) {
// lock here
/** @todo Should send a REGREL in setUnregister()... */
_mutexIAX.enterMutex();
//iax_send_regrel(); doesn't exist yet :)
iax_destroy(_regSession);
_regSession = NULL;
// unlock here
_mutexIAX.leaveMutex();
return false;
_regSession = NULL;
}
_nextRefreshStamp = 0;
return false;
}
......@@ -738,13 +751,24 @@ IAXVoIPLink::iaxHandleRegReply(iax_event* event)
{
if (event->etype == IAX_EVENT_REGREJ) {
/* Authentication failed! */
_mutexIAX.enterMutex();
iax_destroy(_regSession);
_mutexIAX.leaveMutex();
_regSession = NULL;
Manager::instance().registrationFailed(getAccountID());
Manager::instance().registrationFailed(getAccountID());
}
else if (event->etype == IAX_EVENT_REGACK) {
/* 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());
}
}
......
......@@ -49,6 +49,11 @@ public:
bool checkNetwork (void) { return false; }
void terminate (void);
/**
* Send out registration
*
* @return The new registration state (are we registered ?)
*/
bool setRegister (void);
/**
......@@ -155,8 +160,16 @@ private:
/** IAX full name */
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;
/** Connection to audio card/device */
AudioLayer* audiolayer;
/** When we receive data, we decode it inside this buffer */
......@@ -164,8 +177,8 @@ private:
/** When we send data, we encode it inside this buffer*/
unsigned char* _sendDataEncoded;
/** 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 */
/** 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 */
SFLDataFormat* _dataAudioLayer;
/** Buffer for 8000hz samples in conversion */
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment