reindented code to default indentation

git-svn-id: http://svn.berlios.de/svnroot/repos/sems/trunk@385 8eb893ce-cfd4-0310-b710-fb5ebe64c474
sayer/1.4-spce2.6
Stefan Sayer 19 years ago
parent 39d53e7873
commit e2818b87e7

@ -33,10 +33,10 @@
using namespace std;
bool ContactInfo::isEqual(const ContactInfo& c) const {
return (uri_user == c.uri_user) &&
(!strcasecmp(uri_host.c_str(),
c.uri_host.c_str())) &&
(uri_port == c.uri_port);
return (uri_user == c.uri_user) &&
(!strcasecmp(uri_host.c_str(),
c.uri_host.c_str())) &&
(uri_port == c.uri_port);
}
/*
@ -44,35 +44,35 @@ bool ContactInfo::isEqual(const ContactInfo& c) const {
*/
static inline int skip_name(string& s, unsigned int pos)
{
size_t i;
int last_wsp, quoted = 0;
size_t i;
int last_wsp, quoted = 0;
for(i = pos; i < s.length(); i++) {
char c = s[i];
if (!quoted) {
if ((c == ' ') || (c == '\t')) {
last_wsp = i;
} else {
if (c == '<') {
return i;
}
if (c == '\"') {
quoted = 1;
}
}
} else {
if ((c == '\"') && (s[i-1] != '\\')) quoted = 0;
}
for(i = pos; i < s.length(); i++) {
char c = s[i];
if (!quoted) {
if ((c == ' ') || (c == '\t')) {
last_wsp = i;
} else {
if (c == '<') {
return i;
}
if (quoted) {
cout << "ERROR" << "skip_name(): Closing quote missing in name part of Contact\n";
} else {
cout << "ERROR" << "skip_name(): Error in contact, scheme separator not found\n";
if (c == '\"') {
quoted = 1;
}
}
} else {
if ((c == '\"') && (s[i-1] != '\\')) quoted = 0;
}
}
return -1;
if (quoted) {
cout << "ERROR" << "skip_name(): Closing quote missing in name part of Contact\n";
} else {
cout << "ERROR" << "skip_name(): Error in contact, scheme separator not found\n";
}
return -1;
}
#define ST1 1 /* Basic state */
@ -88,74 +88,74 @@ static inline int skip_name(string& s, unsigned int pos)
*/
static inline int skip_uri(string& s, unsigned int pos)
{
unsigned int len = s.length() - pos;
unsigned int p = pos;
unsigned int len = s.length() - pos;
unsigned int p = pos;
register int st = ST1;
register int st = ST1;
while(len) {
switch(s[p]) {
case ',':
case ';':
if (st == ST1) return p;
break;
while(len) {
switch(s[p]) {
case ',':
case ';':
if (st == ST1) return p;
break;
case '\"':
switch(st) {
case ST1: st = ST2; break;
case ST2: st = ST1; break;
case ST3: st = ST4; break;
case ST4: st = ST3; break;
case ST5: st = ST2; break;
case ST6: st = ST4; break;
}
break;
case '\"':
switch(st) {
case ST1: st = ST2; break;
case ST2: st = ST1; break;
case ST3: st = ST4; break;
case ST4: st = ST3; break;
case ST5: st = ST2; break;
case ST6: st = ST4; break;
}
break;
case '<':
switch(st) {
case ST1: st = ST3; break;
case ST3:
cout << "ERROR" << "skip_uri(): Second < found\n";
return -1;
case ST5: st = ST2; break;
case ST6: st = ST4; break;
}
break;
case '<':
switch(st) {
case ST1: st = ST3; break;
case ST3:
cout << "ERROR" << "skip_uri(): Second < found\n";
return -1;
case ST5: st = ST2; break;
case ST6: st = ST4; break;
}
break;
case '>':
switch(st) {
case ST1:
cout << "ERROR" << "skip_uri(): > is first\n";
return -2;
case '>':
switch(st) {
case ST1:
cout << "ERROR" << "skip_uri(): > is first\n";
return -2;
case ST3: st = ST1; break;
case ST5: st = ST2; break;
case ST6: st = ST4; break;
}
break;
case ST3: st = ST1; break;
case ST5: st = ST2; break;
case ST6: st = ST4; break;
}
break;
case '\\':
switch(st) {
case ST2: st = ST5; break;
case ST4: st = ST6; break;
case ST5: st = ST2; break;
case ST6: st = ST4; break;
}
break;
case '\\':
switch(st) {
case ST2: st = ST5; break;
case ST4: st = ST6; break;
case ST5: st = ST2; break;
case ST6: st = ST4; break;
}
break;
default: break;
default: break;
}
}
p++;
len--;
}
p++;
len--;
}
if (st != ST1) {
cout << "ERROR" << "skip_uri(): < or \" not closed\n";
return -3;
}
return p;
if (st != ST1) {
cout << "ERROR" << "skip_uri(): < or \" not closed\n";
return -3;
}
return p;
}
#define uS0 0 // start
@ -170,92 +170,92 @@ static inline int skip_uri(string& s, unsigned int pos)
*
*/
bool ContactInfo::parse_uri() {
// assuming user@host
size_t pos = 0; int st = uS0;
size_t p1 = 0;
int eq = 0; const char* sip_prot = "SIP:";
uri_user = ""; uri_host = ""; uri_port = ""; uri_param = "";
// assuming user@host
size_t pos = 0; int st = uS0;
size_t p1 = 0;
int eq = 0; const char* sip_prot = "SIP:";
uri_user = ""; uri_host = ""; uri_port = ""; uri_param = "";
while (pos<uri.length()) {
char c = uri[pos];
switch(st) {
case uS0: {
switch (c) {
case '<': { st = uS1; } break;
default: {
if ((eq<=4)&&(toupper(c) ==sip_prot[eq]))
eq++;
if (eq==5) { // found sip:
st = uS2; p1 = pos;
};
} break;
};
} break;
case uS1: {
if (c == ':') { st = uS2; p1 = pos;}
} break;
case uS2: {
switch(c) {
case '@': {
uri_user = uri.substr(p1+1, pos-p1-1);
st = uS3; p1 = pos;
} ; break;
case ':': {
uri_host = uri.substr(p1+1, pos-p1-1);
st = uS4; p1 = pos;
}; break;
case ';': {
uri_host = uri.substr(p1+1, pos-p1-1);
st = uS5; p1 = pos;
}; break;
case '>': {
uri_host = uri.substr(p1+1, pos-p1-1);
st = uS6; p1 = pos;
}; break;
}
} break;
case uS3: {
switch (c) {
case ':': { uri_host = uri.substr(p1+1, pos-p1-1);
st = uS4; p1 = pos; }
break;
case ';': { uri_host = uri.substr(p1+1, pos-p1-1);
st = uS5; p1 = pos; }
break;
case '>': { uri_host = uri.substr(p1+1, pos-p1-1);
st = uS6; p1 = pos; }
break;
};
} break;
case uS4: {
switch (c) {
case ';': { uri_port = uri.substr(p1+1, pos-p1-1);
st = uS5; p1 = pos; }
break;
case '>': { uri_port = uri.substr(p1+1, pos-p1-1);
st = uS6; p1 = pos; }
break;
};
} break;
case uS5: {
switch (c) {
case '>': { uri_param = uri.substr(p1+1, pos-p1-1);
st = uS6; p1 = pos; }
break;
};
} break;
};
pos++;
}
switch(st) {
case uS2:
case uS3: uri_host = uri.substr(p1+1, pos-p1-1); break;
case uS4: uri_port = uri.substr(p1+1, pos-p1-1); break;
case uS5: uri_param = uri.substr(p1+1, pos-p1-1); break;
case uS0:
case uS1: { cout << "ERROR while parsing uri"; return false; } break;
while (pos<uri.length()) {
char c = uri[pos];
switch(st) {
case uS0: {
switch (c) {
case '<': { st = uS1; } break;
default: {
if ((eq<=4)&&(toupper(c) ==sip_prot[eq]))
eq++;
if (eq==5) { // found sip:
st = uS2; p1 = pos;
};
return true;
} break;
};
} break;
case uS1: {
if (c == ':') { st = uS2; p1 = pos;}
} break;
case uS2: {
switch(c) {
case '@': {
uri_user = uri.substr(p1+1, pos-p1-1);
st = uS3; p1 = pos;
} ; break;
case ':': {
uri_host = uri.substr(p1+1, pos-p1-1);
st = uS4; p1 = pos;
}; break;
case ';': {
uri_host = uri.substr(p1+1, pos-p1-1);
st = uS5; p1 = pos;
}; break;
case '>': {
uri_host = uri.substr(p1+1, pos-p1-1);
st = uS6; p1 = pos;
}; break;
}
} break;
case uS3: {
switch (c) {
case ':': { uri_host = uri.substr(p1+1, pos-p1-1);
st = uS4; p1 = pos; }
break;
case ';': { uri_host = uri.substr(p1+1, pos-p1-1);
st = uS5; p1 = pos; }
break;
case '>': { uri_host = uri.substr(p1+1, pos-p1-1);
st = uS6; p1 = pos; }
break;
};
} break;
case uS4: {
switch (c) {
case ';': { uri_port = uri.substr(p1+1, pos-p1-1);
st = uS5; p1 = pos; }
break;
case '>': { uri_port = uri.substr(p1+1, pos-p1-1);
st = uS6; p1 = pos; }
break;
};
} break;
case uS5: {
switch (c) {
case '>': { uri_param = uri.substr(p1+1, pos-p1-1);
st = uS6; p1 = pos; }
break;
};
} break;
};
pos++;
}
switch(st) {
case uS2:
case uS3: uri_host = uri.substr(p1+1, pos-p1-1); break;
case uS4: uri_port = uri.substr(p1+1, pos-p1-1); break;
case uS5: uri_param = uri.substr(p1+1, pos-p1-1); break;
case uS0:
case uS1: { cout << "ERROR while parsing uri"; return false; } break;
};
return true;
}
#define pS0 0 // start
@ -266,67 +266,67 @@ bool ContactInfo::parse_uri() {
*
*/
bool ContactInfo::parse_params(string& line, int& pos) {
size_t p1=pos, p2=pos;
int st = 0; int quoted = false;
char last_c = ' ';
params.clear();
while((size_t)pos < line.length()) {
char c = line[pos];
if (!quoted) {
if (c == ',')
break;
if (c == '\"') {
quoted = 1;
} else if (c == '=') {
p2 = pos; st = pS2;
} else if (c == ';') {
if ((st == pS2) ||(st == pS1)) {
params[line.substr(p1, p2-p1)]
= line.substr(p2+1, pos-p2-1);
st = pS0;
}
} else {
if (st == pS0) {
st = pS1;
p1 = pos;
}
}
} else {
if ((c == '\"') && (last_c != '\\')) quoted = 0;
}
last_c = c;
pos++;
size_t p1=pos, p2=pos;
int st = 0; int quoted = false;
char last_c = ' ';
params.clear();
while((size_t)pos < line.length()) {
char c = line[pos];
if (!quoted) {
if (c == ',')
break;
if (c == '\"') {
quoted = 1;
} else if (c == '=') {
p2 = pos; st = pS2;
} else if (c == ';') {
if ((st == pS2) ||(st == pS1)) {
params[line.substr(p1, p2-p1)]
= line.substr(p2+1, pos-p2-1);
st = pS0;
}
} else {
if (st == pS0) {
st = pS1;
p1 = pos;
}
}
} else {
if ((c == '\"') && (last_c != '\\')) quoted = 0;
}
last_c = c;
pos++;
}
if (st == pS2)
params[line.substr(p1, p2-p1)] = line.substr(p2+1, pos-p2);
if (st == pS2)
params[line.substr(p1, p2-p1)] = line.substr(p2+1, pos-p2);
return true;
return true;
}
bool ContactInfo::parse_contact(string& line, size_t pos, size_t& end) {
int p0 = skip_name(line, pos);
if (p0 < 0) return false;
int p1 = skip_uri(line, p0);
if (p1 < 0) return false;
uri = line.substr(p0, p1-p0);
if (!parse_uri()) return false;
parse_params(line, p1);
end = p1;
return true;
int p0 = skip_name(line, pos);
if (p0 < 0) return false;
int p1 = skip_uri(line, p0);
if (p1 < 0) return false;
uri = line.substr(p0, p1-p0);
if (!parse_uri()) return false;
parse_params(line, p1);
end = p1;
return true;
}
void ContactInfo::dump() {
DBG("--- Contact Info --- \n");
DBG(" uri '%s'\n", uri.c_str());
DBG(" uri_user '%s'\n", uri_user.c_str());
DBG(" uri_host '%s'\n", uri_host.c_str());
DBG(" uri_port '%s'\n", uri_port.c_str());
DBG(" uri_param '%s'\n", uri_param.c_str());
for (map<string, string>::iterator it = params.begin();
it != params.end(); it++)
DBG(" param '%s'='%s'\n", it->first.c_str(), it->second.c_str()) ;
DBG("-------------------- \n");
DBG("--- Contact Info --- \n");
DBG(" uri '%s'\n", uri.c_str());
DBG(" uri_user '%s'\n", uri_user.c_str());
DBG(" uri_host '%s'\n", uri_host.c_str());
DBG(" uri_port '%s'\n", uri_port.c_str());
DBG(" uri_param '%s'\n", uri_param.c_str());
for (map<string, string>::iterator it = params.begin();
it != params.end(); it++)
DBG(" param '%s'='%s'\n", it->first.c_str(), it->second.c_str()) ;
DBG("-------------------- \n");
}

@ -31,20 +31,20 @@ using std::map;
using std::string;
struct ContactInfo {
string display_name;
string uri;
string display_name;
string uri;
string uri_user;
string uri_host;
string uri_port;
string uri_param;
string uri_user;
string uri_host;
string uri_port;
string uri_param;
map<string, string> params;
map<string, string> params;
bool isEqual(const ContactInfo& c) const;
bool parse_contact(string& line, size_t pos, size_t& end);
bool parse_uri();
bool parse_params(string& line, int& pos);
void dump();
ContactInfo() { }
bool isEqual(const ContactInfo& c) const;
bool parse_contact(string& line, size_t pos, size_t& end);
bool parse_uri();
bool parse_params(string& line, int& pos);
void dump();
ContactInfo() { }
};

@ -39,121 +39,121 @@ EXPORT_PLUGIN_CLASS_FACTORY(SIPRegistrarClient, MOD_NAME);
SIPRegistration::SIPRegistration(const string& handle,
const SIPRegistrationInfo& info,
const string& sess_link)
: info(info),
dlg(this),
cred(info.domain, info.auth_user, info.pwd),
active(false),
reg_begin(0),
reg_expires(0),
remove(false),
sess_link(sess_link),
reg_send_begin(0),
waiting_result(false),
seh(NULL)
const SIPRegistrationInfo& info,
const string& sess_link)
: info(info),
dlg(this),
cred(info.domain, info.auth_user, info.pwd),
active(false),
reg_begin(0),
reg_expires(0),
remove(false),
sess_link(sess_link),
reg_send_begin(0),
waiting_result(false),
seh(NULL)
{
req.cmd = "sems";
req.user = info.user;
req.method = "REGISTER";
req.dstip = AmConfig::LocalIP;
req.r_uri = "sip:"+info.domain;
req.from = info.name+" <sip:"+info.user+"@"+info.domain+">";
req.from_uri = "sip:"+info.user+"@"+info.domain;
req.from_tag = handle;
req.to = req.from;
req.to_tag = "";
req.callid = AmSession::getNewId() + "@" + AmConfig::LocalIP;
//
// clear dlg.callid? ->reregister?
dlg.updateStatusFromLocalRequest(req);
req.cmd = "sems";
req.user = info.user;
req.method = "REGISTER";
req.dstip = AmConfig::LocalIP;
req.r_uri = "sip:"+info.domain;
req.from = info.name+" <sip:"+info.user+"@"+info.domain+">";
req.from_uri = "sip:"+info.user+"@"+info.domain;
req.from_tag = handle;
req.to = req.from;
req.to_tag = "";
req.callid = AmSession::getNewId() + "@" + AmConfig::LocalIP;
//
// clear dlg.callid? ->reregister?
dlg.updateStatusFromLocalRequest(req);
dlg.sip_ip = AmConfig::LocalSIPIP;
if (AmConfig::LocalSIPPort)
dlg.sip_port = int2str(AmConfig::LocalSIPPort);
dlg.cseq = 50;
dlg.sip_ip = AmConfig::LocalSIPIP;
if (AmConfig::LocalSIPPort)
dlg.sip_port = int2str(AmConfig::LocalSIPPort);
dlg.cseq = 50;
}
SIPRegistration::~SIPRegistration() {
setSessionEventHandler(NULL);
setSessionEventHandler(NULL);
}
void SIPRegistration::setSessionEventHandler(AmSessionEventHandler* new_seh) {
if (seh)
delete seh;
seh = new_seh;
if (seh)
delete seh;
seh = new_seh;
}
void SIPRegistration::doRegistration() {
waiting_result = true;
req.to_tag = "";
dlg.remote_tag = "";
req.r_uri = "sip:"+info.domain;
dlg.remote_uri = req.r_uri;
dlg.next_hop = "";
dlg.sendRequest(req.method, "", "", "Expires: 1000\n");
waiting_result = true;
req.to_tag = "";
dlg.remote_tag = "";
req.r_uri = "sip:"+info.domain;
dlg.remote_uri = req.r_uri;
dlg.next_hop = "";
dlg.sendRequest(req.method, "", "", "Expires: 1000\n");
// save TS
struct timeval now;
gettimeofday(&now, NULL);
reg_send_begin = now.tv_sec;
// save TS
struct timeval now;
gettimeofday(&now, NULL);
reg_send_begin = now.tv_sec;
}
void SIPRegistration::doUnregister() {
waiting_result = true;
req.to_tag = "";
dlg.remote_tag = "";
req.r_uri = "sip:"+info.domain;
dlg.remote_uri = req.r_uri;
dlg.next_hop = "";
dlg.sendRequest(req.method, "", "", "Expires: 0\n");
waiting_result = true;
req.to_tag = "";
dlg.remote_tag = "";
req.r_uri = "sip:"+info.domain;
dlg.remote_uri = req.r_uri;
dlg.next_hop = "";
dlg.sendRequest(req.method, "", "", "Expires: 0\n");
// save TS
struct timeval now;
gettimeofday(&now, NULL);
reg_send_begin = now.tv_sec;
// save TS
struct timeval now;
gettimeofday(&now, NULL);
reg_send_begin = now.tv_sec;
}
void SIPRegistration::onSendRequest(const string& method,
const string& content_type,
const string& body,
string& hdrs,
unsigned int cseq) {
if (seh)
seh->onSendRequest(method, content_type, body,
hdrs,cseq);
const string& content_type,
const string& body,
string& hdrs,
unsigned int cseq) {
if (seh)
seh->onSendRequest(method, content_type, body,
hdrs,cseq);
}
void SIPRegistration::onSendReply(const AmSipRequest& req,
unsigned int code,
const string& reason,
const string& content_type,
const string& body,
string& hdrs) {
if (seh)
seh->onSendReply(req,code,reason,
content_type,body,hdrs);
unsigned int code,
const string& reason,
const string& content_type,
const string& body,
string& hdrs) {
if (seh)
seh->onSendReply(req,code,reason,
content_type,body,hdrs);
}
SIPRegistration::RegistrationState SIPRegistration::getState() {
if (active)
return RegisterActive;
if (waiting_result)
return RegisterPending;
if (active)
return RegisterActive;
if (waiting_result)
return RegisterPending;
return RegisterExpired;
return RegisterExpired;
}
unsigned int SIPRegistration::getExpiresLeft() {
struct timeval now;
gettimeofday(&now, NULL);
struct timeval now;
gettimeofday(&now, NULL);
int diff = reg_begin + reg_expires - now.tv_sec;
if (diff < 0)
return 0;
else
return diff;
int diff = reg_begin + reg_expires - now.tv_sec;
if (diff < 0)
return 0;
else
return diff;
}
//-----------------------------------------------------------
@ -161,230 +161,230 @@ SIPRegistrarClient* SIPRegistrarClient::_instance=0;
SIPRegistrarClient* SIPRegistrarClient::instance()
{
if(_instance == NULL){
_instance = new SIPRegistrarClient(MOD_NAME);
}
return _instance;
if(_instance == NULL){
_instance = new SIPRegistrarClient(MOD_NAME);
}
return _instance;
}
SIPRegistrarClient::SIPRegistrarClient(const string& name)
: AmSIPEventHandler(name),
AmEventQueue(this) ,
uac_auth_i(NULL),
AmDynInvokeFactory(MOD_NAME)
: AmSIPEventHandler(name),
AmEventQueue(this) ,
uac_auth_i(NULL),
AmDynInvokeFactory(MOD_NAME)
{
}
void SIPRegistrarClient::run() {
DBG("SIPRegistrarClient starting...\n");
AmDynInvokeFactory* uac_auth_f = AmPlugIn::instance()->getFactory4Di("uac_auth");
if (uac_auth_f == NULL) {
DBG("unable to get a uac_auth factory. registrations will not be authenticated.\n");
DBG("(do you want to load uac_auth module?)\n");
} else {
uac_auth_i = uac_auth_f->getInstance();
}
DBG("SIPRegistrarClient starting...\n");
AmDynInvokeFactory* uac_auth_f = AmPlugIn::instance()->getFactory4Di("uac_auth");
if (uac_auth_f == NULL) {
DBG("unable to get a uac_auth factory. registrations will not be authenticated.\n");
DBG("(do you want to load uac_auth module?)\n");
} else {
uac_auth_i = uac_auth_f->getInstance();
}
while (true) {
if (registrations.size()) {
unsigned int cnt = 250;
while (cnt > 0) {
usleep(2000); // every 2 ms
processEvents();
cnt--;
}
checkTimeouts();
} else {
waitForEvent();
processEvents();
}
}
while (true) {
if (registrations.size()) {
unsigned int cnt = 250;
while (cnt > 0) {
usleep(2000); // every 2 ms
processEvents();
cnt--;
}
checkTimeouts();
} else {
waitForEvent();
processEvents();
}
}
}
void SIPRegistration::onRegisterExpired() {
if (sess_link.length()) {
AmSessionContainer::instance()->postEvent(sess_link,
new SIPRegistrationEvent(SIPRegistrationEvent::RegisterTimeout,
req.from_tag));
}
DBG("Registration '%s' expired.\n", (info.user+"@"+info.domain).c_str());
remove = true;
if (sess_link.length()) {
AmSessionContainer::instance()->postEvent(sess_link,
new SIPRegistrationEvent(SIPRegistrationEvent::RegisterTimeout,
req.from_tag));
}
DBG("Registration '%s' expired.\n", (info.user+"@"+info.domain).c_str());
remove = true;
}
void SIPRegistration::onRegisterSendTimeout() {
if (sess_link.length()) {
AmSessionContainer::instance()->postEvent(sess_link,
new SIPRegistrationEvent(SIPRegistrationEvent::RegisterSendTimeout,
req.from_tag));
}
DBG("Registration '%s' REGISTER request timeout.\n",
(info.user+"@"+info.domain).c_str());
remove = true;
if (sess_link.length()) {
AmSessionContainer::instance()->postEvent(sess_link,
new SIPRegistrationEvent(SIPRegistrationEvent::RegisterSendTimeout,
req.from_tag));
}
DBG("Registration '%s' REGISTER request timeout.\n",
(info.user+"@"+info.domain).c_str());
remove = true;
}
void SIPRegistrarClient::checkTimeouts() {
// DBG("checking timeouts...\n");
struct timeval now;
gettimeofday(&now, NULL);
reg_mut.lock();
vector<string> remove_regs;
for (map<string, SIPRegistration*>::iterator it = registrations.begin();
it != registrations.end(); it++) {
if (it->second->active) {
if (it->second->registerExpired(now.tv_sec)) {
SIPRegistration* reg = it->second;
reg->onRegisterExpired();
} else if (!it->second->waiting_result &&
it->second->timeToReregister(now.tv_sec)) {
it->second->doRegistration();
}
} else if (it->second->remove) {
remove_regs.push_back(it->first);
} else if (it->second->waiting_result &&
it->second->registerSendTimeout(now.tv_sec)) {
SIPRegistration* reg = it->second;
reg->onRegisterSendTimeout();
}
}
for (vector<string>::iterator it = remove_regs.begin();
it != remove_regs.end(); it++) {
DBG("removing registration\n");
SIPRegistration* reg = registrations[*it];
registrations.erase(*it);
if (reg)
delete reg;
}
// DBG("checking timeouts...\n");
struct timeval now;
gettimeofday(&now, NULL);
reg_mut.lock();
vector<string> remove_regs;
for (map<string, SIPRegistration*>::iterator it = registrations.begin();
it != registrations.end(); it++) {
if (it->second->active) {
if (it->second->registerExpired(now.tv_sec)) {
SIPRegistration* reg = it->second;
reg->onRegisterExpired();
} else if (!it->second->waiting_result &&
it->second->timeToReregister(now.tv_sec)) {
it->second->doRegistration();
}
} else if (it->second->remove) {
remove_regs.push_back(it->first);
} else if (it->second->waiting_result &&
it->second->registerSendTimeout(now.tv_sec)) {
SIPRegistration* reg = it->second;
reg->onRegisterSendTimeout();
}
}
for (vector<string>::iterator it = remove_regs.begin();
it != remove_regs.end(); it++) {
DBG("removing registration\n");
SIPRegistration* reg = registrations[*it];
registrations.erase(*it);
if (reg)
delete reg;
}
reg_mut.unlock();
reg_mut.unlock();
}
int SIPRegistrarClient::onLoad() {
instance()->start();
return 0;
instance()->start();
return 0;
}
void SIPRegistrarClient::process(AmEvent* ev) {
AmSipReplyEvent* sip_rep = dynamic_cast<AmSipReplyEvent*>(ev);
if (sip_rep) {
onSipReplyEvent(sip_rep);
return;
}
AmSipReplyEvent* sip_rep = dynamic_cast<AmSipReplyEvent*>(ev);
if (sip_rep) {
onSipReplyEvent(sip_rep);
return;
}
SIPNewRegistrationEvent* new_reg = dynamic_cast<SIPNewRegistrationEvent*>(ev);
if (new_reg) {
onNewRegistration(new_reg);
return;
}
SIPNewRegistrationEvent* new_reg = dynamic_cast<SIPNewRegistrationEvent*>(ev);
if (new_reg) {
onNewRegistration(new_reg);
return;
}
SIPRemoveRegistrationEvent* rem_reg = dynamic_cast<SIPRemoveRegistrationEvent*>(ev);
if (rem_reg) {
onRemoveRegistration(rem_reg);
return;
}
SIPRemoveRegistrationEvent* rem_reg = dynamic_cast<SIPRemoveRegistrationEvent*>(ev);
if (rem_reg) {
onRemoveRegistration(rem_reg);
return;
}
}
void SIPRegistrarClient::onSipReplyEvent(AmSipReplyEvent* ev) {
SIPRegistration* reg = get_reg(ev->reply.local_tag);
if (reg != NULL) {
reg->onSipReply(ev->reply);
}
SIPRegistration* reg = get_reg(ev->reply.local_tag);
if (reg != NULL) {
reg->onSipReply(ev->reply);
}
}
bool SIPRegistration::registerSendTimeout(time_t now_sec) {
return now_sec > reg_send_begin + REGISTER_SEND_TIMEOUT;
return now_sec > reg_send_begin + REGISTER_SEND_TIMEOUT;
}
bool SIPRegistration::timeToReregister(time_t now_sec) {
// if (active)
// DBG("compare %lu with %lu\n",(reg_begin+reg_expires), (unsigned long)now_sec);
return (((unsigned long)reg_begin+reg_expires/2) < (unsigned long)now_sec);
// if (active)
// DBG("compare %lu with %lu\n",(reg_begin+reg_expires), (unsigned long)now_sec);
return (((unsigned long)reg_begin+reg_expires/2) < (unsigned long)now_sec);
}
bool SIPRegistration::registerExpired(time_t now_sec) {
return ((reg_begin+reg_expires) < (unsigned int)now_sec);
return ((reg_begin+reg_expires) < (unsigned int)now_sec);
}
void SIPRegistration::onSipReply(AmSipReply& reply) {
if ((seh!=NULL) && seh->onSipReply(reply))
return;
waiting_result = false;
dlg.updateStatus(reply);
if ((reply.code>=200)&&(reply.code<300)) {
DBG("positive reply to REGISTER!\n");
size_t end = 0;
string local_contact_hdr = dlg.getContactHdr();
local_contact.parse_contact(local_contact_hdr, (size_t)0, end);
local_contact.dump();
string contacts = getHeader(reply.hdrs, "Contact", "m");
bool found = false;
if (!contacts.length()) {
DBG("received positive reply to de-Register \n");
remove = true;
} else {
end = 0;
while (!found) {
if (!server_contact.parse_contact(contacts, end, end)) {
ERROR("while parsing contact\n");
break;
}
server_contact.dump();
if ((seh!=NULL) && seh->onSipReply(reply))
return;
waiting_result = false;
dlg.updateStatus(reply);
if ((reply.code>=200)&&(reply.code<300)) {
DBG("positive reply to REGISTER!\n");
size_t end = 0;
string local_contact_hdr = dlg.getContactHdr();
local_contact.parse_contact(local_contact_hdr, (size_t)0, end);
local_contact.dump();
string contacts = getHeader(reply.hdrs, "Contact", "m");
bool found = false;
if (!contacts.length()) {
DBG("received positive reply to de-Register \n");
remove = true;
} else {
end = 0;
while (!found) {
if (!server_contact.parse_contact(contacts, end, end)) {
ERROR("while parsing contact\n");
break;
}
server_contact.dump();
if (server_contact.isEqual(local_contact)) {
DBG("contact found\n");
found = active = true;
bool str2i(const string& str, unsigned int& result);
if (server_contact.isEqual(local_contact)) {
DBG("contact found\n");
found = active = true;
bool str2i(const string& str, unsigned int& result);
if (str2i(server_contact.params["expires"], reg_expires)) {
ERROR("could not extract expires value.\n");
reg_expires = 500;
}
DBG("got an expires of %d\n", reg_expires);
// save TS
struct timeval now;
gettimeofday(&now, NULL);
reg_begin = now.tv_sec;
if (sess_link.length()) {
DBG("posting SIPRegistrationEvent to '%s'\n", sess_link.c_str());
AmSessionContainer::instance()->postEvent(sess_link,
new SIPRegistrationEvent(SIPRegistrationEvent::RegisterSuccess,
req.from_tag,
reply.code, reply.reason));
}
break;
}
}
}
if (!found) {
if (sess_link.length()) {
AmSessionContainer::instance()->postEvent(sess_link,
new SIPRegistrationEvent(SIPRegistrationEvent::RegisterNoContact,
req.from_tag,
reply.code, reply.reason));
}
active = false;
}
} else if (reply.code >= 300) {
DBG("Registration failed.\n");
if (sess_link.length()) {
AmSessionContainer::instance()->postEvent(sess_link,
new SIPRegistrationEvent(SIPRegistrationEvent::RegisterFailed,
req.from_tag,
reply.code, reply.reason));
}
active = false;
remove = true;
if (str2i(server_contact.params["expires"], reg_expires)) {
ERROR("could not extract expires value.\n");
reg_expires = 500;
}
DBG("got an expires of %d\n", reg_expires);
// save TS
struct timeval now;
gettimeofday(&now, NULL);
reg_begin = now.tv_sec;
if (sess_link.length()) {
DBG("posting SIPRegistrationEvent to '%s'\n", sess_link.c_str());
AmSessionContainer::instance()->postEvent(sess_link,
new SIPRegistrationEvent(SIPRegistrationEvent::RegisterSuccess,
req.from_tag,
reply.code, reply.reason));
}
break;
}
}
}
if (!found) {
if (sess_link.length()) {
AmSessionContainer::instance()->postEvent(sess_link,
new SIPRegistrationEvent(SIPRegistrationEvent::RegisterNoContact,
req.from_tag,
reply.code, reply.reason));
}
active = false;
}
} else if (reply.code >= 300) {
DBG("Registration failed.\n");
if (sess_link.length()) {
AmSessionContainer::instance()->postEvent(sess_link,
new SIPRegistrationEvent(SIPRegistrationEvent::RegisterFailed,
req.from_tag,
reply.code, reply.reason));
}
active = false;
remove = true;
}
}
void SIPRegistrarClient::onNewRegistration(SIPNewRegistrationEvent* new_reg) {
@ -421,9 +421,9 @@ void SIPRegistrarClient::onNewRegistration(SIPNewRegistrationEvent* new_reg) {
}
void SIPRegistrarClient::onRemoveRegistration(SIPRemoveRegistrationEvent* new_reg) {
SIPRegistration* reg = get_reg(new_reg->handle);
if (reg)
reg->doUnregister();
SIPRegistration* reg = get_reg(new_reg->handle);
if (reg)
reg->doUnregister();
}
@ -431,159 +431,159 @@ void SIPRegistrarClient::on_stop() { }
bool SIPRegistrarClient::onSipReply(const AmSipReply& rep) {
DBG("got reply with tag '%s'\n", rep.local_tag.c_str());
DBG("got reply with tag '%s'\n", rep.local_tag.c_str());
if (instance()->hasRegistration(rep.local_tag)) {
instance()->postEvent(new AmSipReplyEvent(rep));
return true;
} else
return false;
if (instance()->hasRegistration(rep.local_tag)) {
instance()->postEvent(new AmSipReplyEvent(rep));
return true;
} else
return false;
}
bool SIPRegistrarClient::hasRegistration(const string& handle) {
return get_reg(handle) != NULL;
return get_reg(handle) != NULL;
}
SIPRegistration* SIPRegistrarClient::
get_reg(const string& reg_id)
{
DBG("get registration '%s'\n", reg_id.c_str());
SIPRegistration* res = NULL;
reg_mut.lock();
map<string, SIPRegistration*>::iterator it =
registrations.find(reg_id);
if (it!=registrations.end())
res = it->second;
reg_mut.unlock();
DBG("get registration : res = '%ld' (this = %ld)\n", (long)res, (long)this);
return res;
DBG("get registration '%s'\n", reg_id.c_str());
SIPRegistration* res = NULL;
reg_mut.lock();
map<string, SIPRegistration*>::iterator it =
registrations.find(reg_id);
if (it!=registrations.end())
res = it->second;
reg_mut.unlock();
DBG("get registration : res = '%ld' (this = %ld)\n", (long)res, (long)this);
return res;
}
SIPRegistration* SIPRegistrarClient::
get_reg_unsafe(const string& reg_id)
{
// DBG("get registration_unsafe '%s'\n", reg_id.c_str());
SIPRegistration* res = NULL;
map<string, SIPRegistration*>::iterator it =
registrations.find(reg_id);
if (it!=registrations.end())
res = it->second;
// DBG("get registration_unsafe : res = '%ld' (this = %ld)\n", (long)res, (long)this);
return res;
SIPRegistration* res = NULL;
map<string, SIPRegistration*>::iterator it =
registrations.find(reg_id);
if (it!=registrations.end())
res = it->second;
// DBG("get registration_unsafe : res = '%ld' (this = %ld)\n", (long)res, (long)this);
return res;
}
SIPRegistration* SIPRegistrarClient::
remove_reg(const string& reg_id) {
reg_mut.lock();
SIPRegistration* reg = remove_reg_unsafe(reg_id);
reg_mut.unlock();
return reg;
reg_mut.lock();
SIPRegistration* reg = remove_reg_unsafe(reg_id);
reg_mut.unlock();
return reg;
}
SIPRegistration* SIPRegistrarClient::
remove_reg_unsafe(const string& reg_id) {
DBG("removing registration '%s'\n", reg_id.c_str());
SIPRegistration* reg = NULL;
map<string, SIPRegistration*>::iterator it =
registrations.find(reg_id);
if (it!=registrations.end()) {
reg = it->second;
registrations.erase(it);
}
return reg;
DBG("removing registration '%s'\n", reg_id.c_str());
SIPRegistration* reg = NULL;
map<string, SIPRegistration*>::iterator it =
registrations.find(reg_id);
if (it!=registrations.end()) {
reg = it->second;
registrations.erase(it);
}
return reg;
}
void SIPRegistrarClient::
add_reg(const string& reg_id, SIPRegistration* new_reg)
{
DBG("adding registration '%s' (this = %ld)\n", reg_id.c_str(), (long)this);
SIPRegistration* reg = NULL;
reg_mut.lock();
map<string, SIPRegistration*>::iterator it =
registrations.find(reg_id);
if (it!=registrations.end()) {
reg = it->second;
DBG("adding registration '%s' (this = %ld)\n", reg_id.c_str(), (long)this);
SIPRegistration* reg = NULL;
reg_mut.lock();
map<string, SIPRegistration*>::iterator it =
registrations.find(reg_id);
if (it!=registrations.end()) {
reg = it->second;
}
registrations[reg_id] = new_reg;
reg_mut.unlock();
}
registrations[reg_id] = new_reg;
reg_mut.unlock();
if (reg != NULL)
delete reg; // old one with the same ltag
if (reg != NULL)
delete reg; // old one with the same ltag
}
// API
string SIPRegistrarClient::createRegistration(const string& domain,
const string& user,
const string& name,
const string& auth_user,
const string& pwd,
const string& sess_link) {
const string& user,
const string& name,
const string& auth_user,
const string& pwd,
const string& sess_link) {
string handle = AmSession::getNewId();
instance()->
postEvent(new SIPNewRegistrationEvent(SIPRegistrationInfo(domain, user,
name, auth_user, pwd),
handle, sess_link));
return handle;
string handle = AmSession::getNewId();
instance()->
postEvent(new SIPNewRegistrationEvent(SIPRegistrationInfo(domain, user,
name, auth_user, pwd),
handle, sess_link));
return handle;
}
void SIPRegistrarClient::removeRegistration(const string& handle) {
instance()->
postEvent(new SIPRemoveRegistrationEvent(handle));
instance()->
postEvent(new SIPRemoveRegistrationEvent(handle));
}
bool SIPRegistrarClient::getRegistrationState(const string& handle,
unsigned int& state,
unsigned int& expires_left) {
bool res = false;
reg_mut.lock();
SIPRegistration* reg = get_reg_unsafe(handle);
if (reg) {
res = true;
state = reg->getState();
expires_left = reg->getExpiresLeft();
}
unsigned int& state,
unsigned int& expires_left) {
bool res = false;
reg_mut.lock();
SIPRegistration* reg = get_reg_unsafe(handle);
if (reg) {
res = true;
state = reg->getState();
expires_left = reg->getExpiresLeft();
}
reg_mut.unlock();
return res;
reg_mut.unlock();
return res;
}
void SIPRegistrarClient::invoke(const string& method, const AmArg& args,
AmArg& ret)
AmArg& ret)
{
if(method == "createRegistration"){
ret.push(createRegistration(args.get(0).asCStr(),
args.get(1).asCStr(),
args.get(2).asCStr(),
args.get(3).asCStr(),
args.get(4).asCStr(),
args.get(5).asCStr()
).c_str());
if(method == "createRegistration"){
ret.push(createRegistration(args.get(0).asCStr(),
args.get(1).asCStr(),
args.get(2).asCStr(),
args.get(3).asCStr(),
args.get(4).asCStr(),
args.get(5).asCStr()
).c_str());
}
else if(method == "removeRegistration"){
removeRegistration(args.get(0).asCStr());
}
else if(method == "getRegistrationState"){
unsigned int state;
unsigned int expires;
if (instance()->getRegistrationState(args.get(0).asCStr(),
state, expires)){
ret.push(1);
ret.push((int)state);
ret.push((int)expires);
} else {
ret.push(AmArg((int)0));
}
else if(method == "removeRegistration"){
removeRegistration(args.get(0).asCStr());
}
else if(method == "getRegistrationState"){
unsigned int state;
unsigned int expires;
if (instance()->getRegistrationState(args.get(0).asCStr(),
state, expires)){
ret.push(1);
ret.push((int)state);
ret.push((int)expires);
} else {
ret.push(AmArg((int)0));
}
} else if(method == "_list"){
ret.push(AmArg("createRegistration"));
ret.push(AmArg("removeRegistration"));
ret.push(AmArg("getRegistrationState"));
} else
throw AmDynInvoke::NotImplemented(method);
} else if(method == "_list"){
ret.push(AmArg("createRegistration"));
ret.push(AmArg("removeRegistration"));
ret.push(AmArg("getRegistrationState"));
} else
throw AmDynInvoke::NotImplemented(method);
}

@ -43,189 +43,189 @@ using std::map;
using std::string;
struct SIPRegistrationInfo {
string domain;
string user;
string name;
string auth_user;
string pwd;
SIPRegistrationInfo(const string& domain,
const string& user,
const string& name,
const string& auth_user,
const string& pwd)
: domain(domain),user(user),name(name),
auth_user(auth_user),pwd(pwd)
{ }
string domain;
string user;
string name;
string auth_user;
string pwd;
SIPRegistrationInfo(const string& domain,
const string& user,
const string& name,
const string& auth_user,
const string& pwd)
: domain(domain),user(user),name(name),
auth_user(auth_user),pwd(pwd)
{ }
};
class SIPRegistration : public AmSipDialogEventHandler,
public DialogControl,
public CredentialHolder
public DialogControl,
public CredentialHolder
{
AmSipDialog dlg;
UACAuthCred cred;
AmSipDialog dlg;
UACAuthCred cred;
SIPRegistrationInfo info;
SIPRegistrationInfo info;
// session to post events to
string sess_link;
// session to post events to
string sess_link;
AmSessionEventHandler* seh;
AmSessionEventHandler* seh;
AmSipRequest req;
AmSipRequest req;
ContactInfo server_contact;
ContactInfo local_contact;
ContactInfo server_contact;
ContactInfo local_contact;
time_t reg_begin;
unsigned int reg_expires;
time_t reg_send_begin;
time_t reg_begin;
unsigned int reg_expires;
time_t reg_send_begin;
public:
SIPRegistration(const string& handle,
const SIPRegistrationInfo& info,
const string& sess_link);
~SIPRegistration();
SIPRegistration(const string& handle,
const SIPRegistrationInfo& info,
const string& sess_link);
~SIPRegistration();
void setSessionEventHandler(AmSessionEventHandler* new_seh);
void setSessionEventHandler(AmSessionEventHandler* new_seh);
void doRegistration();
void doUnregister();
void doRegistration();
void doUnregister();
inline bool timeToReregister(time_t now_sec);
inline bool registerExpired(time_t now_sec);
void onRegisterExpired();
void onRegisterSendTimeout();
inline bool registerSendTimeout(time_t now_sec);
void onSendRequest(const string& method,
const string& content_type,
const string& body,
string& hdrs,
unsigned int cseq);
inline bool timeToReregister(time_t now_sec);
inline bool registerExpired(time_t now_sec);
void onRegisterExpired();
void onRegisterSendTimeout();
inline bool registerSendTimeout(time_t now_sec);
void onSendRequest(const string& method,
const string& content_type,
const string& body,
string& hdrs,
unsigned int cseq);
void onSendReply(const AmSipRequest& req,
unsigned int code,
const string& reason,
const string& content_type,
const string& body,
string& hdrs);
// DialogControl if
AmSipDialog* getDlg() { return &dlg; }
// CredentialHolder
UACAuthCred* getCredentials() { return &cred; }
void onSipReply(AmSipReply& reply);
/** is this registration registered? */
bool active;
/** should this registration be removed from container? */
bool remove;
/** are we waiting for the response to a register? */
bool waiting_result;
enum RegistrationState {
RegisterPending = 0,
RegisterActive,
RegisterExpired
};
/** return the state of the registration */
RegistrationState getState();
/** return the expires left for the registration */
unsigned int getExpiresLeft();
void onSendReply(const AmSipRequest& req,
unsigned int code,
const string& reason,
const string& content_type,
const string& body,
string& hdrs);
// DialogControl if
AmSipDialog* getDlg() { return &dlg; }
// CredentialHolder
UACAuthCred* getCredentials() { return &cred; }
void onSipReply(AmSipReply& reply);
/** is this registration registered? */
bool active;
/** should this registration be removed from container? */
bool remove;
/** are we waiting for the response to a register? */
bool waiting_result;
enum RegistrationState {
RegisterPending = 0,
RegisterActive,
RegisterExpired
};
/** return the state of the registration */
RegistrationState getState();
/** return the expires left for the registration */
unsigned int getExpiresLeft();
};
class SIPNewRegistrationEvent;
class SIPRemoveRegistrationEvent;
class SIPRegistrarClient : public AmSIPEventHandler,
public AmThread,
public AmEventQueue,
public AmEventHandler,
public AmDynInvoke,
public AmDynInvokeFactory
public AmThread,
public AmEventQueue,
public AmEventHandler,
public AmDynInvoke,
public AmDynInvokeFactory
{
// registrations container
AmMutex reg_mut;
std::map<std::string, SIPRegistration*> registrations;
void add_reg(const string& reg_id,
SIPRegistration* new_reg);
SIPRegistration* remove_reg(const string& reg_id);
SIPRegistration* remove_reg_unsafe(const string& reg_id);
SIPRegistration* get_reg(const string& reg_id);
SIPRegistration* get_reg_unsafe(const string& reg_id);
void onSipReplyEvent(AmSipReplyEvent* ev);
void onNewRegistration(SIPNewRegistrationEvent* new_reg);
void onRemoveRegistration(SIPRemoveRegistrationEvent* new_reg);
static SIPRegistrarClient* _instance;
AmDynInvoke* uac_auth_i;
void checkTimeouts();
public:
SIPRegistrarClient(const string& name);
// DI factory
AmDynInvoke* getInstance() { return instance(); }
// DI API
static SIPRegistrarClient* instance();
void invoke(const string& method,
const AmArg& args, AmArg& ret);
// registrations container
AmMutex reg_mut;
std::map<std::string, SIPRegistration*> registrations;
void add_reg(const string& reg_id,
SIPRegistration* new_reg);
SIPRegistration* remove_reg(const string& reg_id);
SIPRegistration* remove_reg_unsafe(const string& reg_id);
SIPRegistration* get_reg(const string& reg_id);
SIPRegistration* get_reg_unsafe(const string& reg_id);
void onSipReplyEvent(AmSipReplyEvent* ev);
void onNewRegistration(SIPNewRegistrationEvent* new_reg);
void onRemoveRegistration(SIPRemoveRegistrationEvent* new_reg);
static SIPRegistrarClient* _instance;
AmDynInvoke* uac_auth_i;
void checkTimeouts();
public:
SIPRegistrarClient(const string& name);
// DI factory
AmDynInvoke* getInstance() { return instance(); }
// DI API
static SIPRegistrarClient* instance();
void invoke(const string& method,
const AmArg& args, AmArg& ret);
bool onSipReply(const AmSipReply& rep);
int onLoad();
bool onSipReply(const AmSipReply& rep);
int onLoad();
void run();
void on_stop();
void process(AmEvent* ev);
void run();
void on_stop();
void process(AmEvent* ev);
// API
string createRegistration(const string& domain,
const string& user,
const string& name,
const string& auth_user,
const string& pwd,
const string& sess_link);
void removeRegistration(const string& handle);
// API
string createRegistration(const string& domain,
const string& user,
const string& name,
const string& auth_user,
const string& pwd,
const string& sess_link);
void removeRegistration(const string& handle);
bool hasRegistration(const string& handle);
bool hasRegistration(const string& handle);
bool getRegistrationState(const string& handle, unsigned int& state,
unsigned int& expires_left);
bool getRegistrationState(const string& handle, unsigned int& state,
unsigned int& expires_left);
enum {
AddRegistration,
RemoveRegistration
} RegEvents;
enum {
AddRegistration,
RemoveRegistration
} RegEvents;
};
struct SIPNewRegistrationEvent : public AmEvent {
SIPNewRegistrationEvent(const SIPRegistrationInfo& info,
const string& handle,
const string& sess_link)
: info(info), handle(handle), sess_link(sess_link),
AmEvent(SIPRegistrarClient::AddRegistration) { }
SIPNewRegistrationEvent(const SIPRegistrationInfo& info,
const string& handle,
const string& sess_link)
: info(info), handle(handle), sess_link(sess_link),
AmEvent(SIPRegistrarClient::AddRegistration) { }
string handle;
string sess_link;
SIPRegistrationInfo info;
string handle;
string sess_link;
SIPRegistrationInfo info;
};
class SIPRemoveRegistrationEvent : public AmEvent {
public:
string handle;
SIPRemoveRegistrationEvent(const string& handle)
: handle(handle),
AmEvent(SIPRegistrarClient::RemoveRegistration) { }
string handle;
SIPRemoveRegistrationEvent(const string& handle)
: handle(handle),
AmEvent(SIPRegistrarClient::RemoveRegistration) { }
};
#endif

Loading…
Cancel
Save