1 /*
2  * Copyright (C) 2007 Sipwise GmbH
3  * Based on the concept of "announcement", Copyright (C) 2002-2003 Fhg Fokus
4  *
5  * This file is part of SEMS, a free SIP media server.
6  *
7  * SEMS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * For a license to use the sems software under conditions
13  * other than those described here, or to purchase support for this
14  * software, please contact iptel.org by e-mail at the following addresses:
15  *    info@iptel.org
16  *
17  * SEMS is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25  */
26 
27 #include "Click2Dial.h"
28 #include "AmSessionContainer.h"
29 #include "AmConfig.h"
30 #include "AmUtils.h"
31 #include "AmApi.h"
32 #include "AmPlugIn.h"
33 
34 #include "AmMediaProcessor.h"
35 
36 #include "sems.h"
37 #include "log.h"
38 
39 #define MOD_NAME "click2dial"
40 
41 EXPORT_SESSION_FACTORY(Click2DialFactory, MOD_NAME);
42 
43 string Click2DialFactory::AnnouncePath;
44 string Click2DialFactory::AnnounceFile;
45 
46 bool Click2DialFactory::relay_early_media_sdp = true;
47 
Click2DialFactory(const string & _app_name)48 Click2DialFactory::Click2DialFactory(const string& _app_name)
49 : AmSessionFactory(_app_name)
50 {
51 }
52 
53 
onLoad()54 int Click2DialFactory::onLoad()
55 {
56   AmConfigReader cfg;
57   if(cfg.loadFile(AmConfig::ModConfigPath + string(MOD_NAME ".conf")))
58     return -1;
59 
60   // get application specific global parameters
61   configureModule(cfg);
62 
63   AnnouncePath = cfg.getParameter("announce_path",ANNOUNCE_PATH);
64   if(!AnnouncePath.empty() && AnnouncePath[AnnouncePath.length()-1] != '/')
65     AnnouncePath += "/";
66 
67   AnnounceFile = cfg.getParameter("default_announce",ANNOUNCE_FILE);
68   DBG("AnnounceFile = %s\n",AnnounceFile.c_str());
69 
70   string announce_file = AnnouncePath + AnnounceFile;
71   if(!file_exists(announce_file)) {
72     ERROR("default file for ann_b2b module does not exist ('%s').\n",
73       announce_file.c_str());
74     return -1;
75   }
76 
77   if (cfg.getParameter("relay_early_media") == "no")
78     relay_early_media_sdp = false;
79 
80   return 0;
81 }
82 
83 
getAnnounceFile(const AmSipRequest & req)84 string Click2DialFactory::getAnnounceFile(const AmSipRequest& req)
85 {
86   string announce_path = AnnouncePath;
87   string announce_file = announce_path + req.domain
88     + "/" + req.user + ".wav";
89 
90   DBG("trying '%s'\n",announce_file.c_str());
91   if(file_exists(announce_file))
92     goto end;
93 
94   announce_file = announce_path + req.user + ".wav";
95   DBG("trying '%s'\n",announce_file.c_str());
96   if(file_exists(announce_file))
97     goto end;
98   announce_file = AnnouncePath + AnnounceFile;
99 
100   end:
101   return announce_file;
102 }
103 
104 
onInvite(const AmSipRequest & req,const string & app_name,AmArg & session_params)105 AmSession* Click2DialFactory::onInvite(const AmSipRequest& req, const string& app_name, AmArg& session_params)
106 {
107   UACAuthCred* cred = NULL;
108   string callee_uri, a_realm, a_user, a_pwd;
109 
110   if(session_params.size() != 4) {
111     ERROR("Need 4 parameters, got %lu\n",
112 	  (long unsigned int)session_params.size());
113     return NULL;
114   }
115 
116   if(session_params.get(0).getType() == AmArg::CStr) {
117     a_realm = string(session_params.get(0).asCStr());
118   }
119   else {
120     ERROR("All arguments have to be CStr\n");
121     return NULL;
122   }
123   if(session_params.get(1).getType() == AmArg::CStr) {
124     a_user = string(session_params.get(1).asCStr());
125   }
126   else {
127     ERROR("All arguments have to be CStr\n");
128     return NULL;
129   }
130   if (session_params.get(2).getType() == AmArg::CStr) {
131     a_pwd = string(session_params.get(2).asCStr());
132   }
133   else {
134     ERROR("All arguments have to be CStr\n");
135     return NULL;
136   }
137 
138   if (session_params.get(3).getType() == AmArg::CStr) {
139     callee_uri = string(session_params.get(3).asCStr());
140   }
141   else {
142     ERROR("All arguments have to be CStr\n");
143     return NULL;
144   }
145 
146   cred = new UACAuthCred(a_realm, a_user, a_pwd);
147   if(cred == NULL) {
148     ERROR("Failed to create authentication handle\n");
149     return NULL;
150   }
151 
152   AmSession* s = new C2DCallerDialog(req, getAnnounceFile(req), callee_uri, cred);
153   if(s == NULL) {
154     ERROR("Failed to create a click2dial dialog");
155     return NULL;
156   }
157 
158   if (!AmUACAuth::enable(s)) {
159     ERROR("Failed to get authentication event handler");
160     delete s;
161     return NULL;
162   }
163 
164   return s;
165 }
166 
167 
onInvite(const AmSipRequest & req,const string & app_name,const map<string,string> & app_params)168 AmSession* Click2DialFactory::onInvite(const AmSipRequest& req, const string& app_name,
169 				       const map<string,string>& app_params)
170 {
171   return new C2DCallerDialog(req, getAnnounceFile(req), "", NULL);
172 }
173 
174 
C2DCallerDialog(const AmSipRequest & req,const string & filename,const string & c_uri,UACAuthCred * credentials)175 C2DCallerDialog::C2DCallerDialog(const AmSipRequest& req,
176 const string& filename, const string& c_uri, UACAuthCred* credentials)
177 : AmB2BCallerSession(), filename(filename), callee_uri(c_uri),
178 cred(credentials)
179 {
180   set_sip_relay_only(false);
181   set_sip_relay_early_media_sdp(Click2DialFactory::relay_early_media_sdp);
182 }
183 
onInvite(const AmSipRequest & req)184 void C2DCallerDialog::onInvite(const AmSipRequest& req)
185 {
186   ERROR("incoming calls not supported in click2dial app!\n");
187   dlg->reply(req,606,"Not Acceptable");
188   setStopped();
189 }
190 
onInvite2xx(const AmSipReply & reply)191 void C2DCallerDialog::onInvite2xx(const AmSipReply& reply)
192 {
193   invite_req.body = reply.body;
194   invite_req.cseq = reply.cseq;
195   est_invite_cseq = reply.cseq;
196 }
197 
onSessionStart()198 void C2DCallerDialog::onSessionStart()
199 {
200   setReceiving(false);
201   if(wav_file.open(filename,AmAudioFile::Read))
202     throw string("AnnouncementDialog::onSessionStart: Cannot open file\n");
203   setOutput(&wav_file);
204 
205   AmB2BCallerSession::onSessionStart();
206 }
207 
updateUACTransCSeq(unsigned int old_cseq,unsigned int new_cseq)208 void C2DCallerDialog::updateUACTransCSeq(unsigned int old_cseq, unsigned int new_cseq) {
209   if (old_cseq == invite_req.cseq) {
210     DBG("updating invite_req.cseq %u -> %u\n", old_cseq, new_cseq);
211     invite_req.cseq = new_cseq;
212   }
213   if (old_cseq == est_invite_cseq) {
214     DBG("updating est_invite_cseq %u -> %u\n", old_cseq, new_cseq);
215     est_invite_cseq = new_cseq;
216   }
217 
218 }
219 
process(AmEvent * event)220 void C2DCallerDialog::process(AmEvent* event)
221 {
222   AmAudioEvent* audio_event = dynamic_cast<AmAudioEvent*>(event);
223   if(audio_event && audio_event->event_id == AmAudioEvent::cleared) {
224 
225     if(getCalleeStatus() != None)
226       return;
227     AmMediaProcessor::instance()->removeSession(this);
228 
229     connectCallee(string("<") + callee_uri + ">", callee_uri);
230     return;
231   }
232 
233   AmB2BCallerSession::process(event);
234 }
235 
236 
createCalleeSession()237 void C2DCallerDialog::createCalleeSession()
238 {
239 
240   UACAuthCred* c;
241   if (cred.get()){
242     c = new UACAuthCred(cred->realm, cred->user, cred->pwd);
243   } else {
244     c = new UACAuthCred();
245   }
246 
247   AmB2BCalleeSession* callee_session = new C2DCalleeDialog(this, c);
248   AmSipDialog* callee_dlg = callee_session->dlg;
249 
250   AmB2BSession::setOtherId(AmSession::getNewId());
251 
252   callee_dlg->setLocalTag(AmB2BSession::getOtherId());
253   callee_dlg->setCallid(AmSession::getNewId());
254   callee_dlg->setLocalParty(dlg->getLocalParty());
255   callee_dlg->setRemoteParty(dlg->getRemoteParty());
256   callee_dlg->setRemoteUri(dlg->getRemoteUri());
257 
258   callee_session->start();
259 
260   AmSessionContainer* sess_cont = AmSessionContainer::instance();
261   sess_cont->addSession(AmB2BSession::getOtherId(),callee_session);
262 }
263 
264 
onB2BEvent(B2BEvent * ev)265 void C2DCallerDialog::onB2BEvent(B2BEvent* ev)
266 {
267   if(ev->event_id == B2BSipReply) {
268     AmSipReply& reply = ((B2BSipReplyEvent*)ev)->reply;
269 
270     if(((reply.code == 407)||(reply.code == 401))  && cred.get() != NULL) {
271       AmB2BSession::onB2BEvent(ev);
272       return;
273     }
274   }
275   AmB2BCallerSession::onB2BEvent(ev);
276 }
277 
278 
onSipReply(const AmSipRequest & req,const AmSipReply & reply,AmBasicSipDialog::Status old_dlg_status)279 void C2DCallerDialog::onSipReply(const AmSipRequest& req,
280                                  const AmSipReply& reply,
281                                  AmBasicSipDialog::Status old_dlg_status)
282 {
283   AmB2BCallerSession::onSipReply(req, reply, old_dlg_status);
284 
285   if ((old_dlg_status < AmSipDialog::Connected) &&
286       (dlg->getStatus() == AmSipDialog::Disconnected)) {
287     DBG("Outbound call failed with reply %d %s.\n",
288         reply.code, reply.reason.c_str());
289     setStopped();
290   }
291 }
292 
293 
C2DCalleeDialog(const AmB2BCallerSession * caller,UACAuthCred * credentials)294 C2DCalleeDialog::C2DCalleeDialog(const AmB2BCallerSession* caller, UACAuthCred* credentials)
295 : AmB2BCalleeSession(caller), cred(credentials)
296 {
297   setAuthHandler();
298 }
299 
300 
setAuthHandler()301 void C2DCalleeDialog::setAuthHandler()
302 {
303   if(cred.get() != NULL) {
304     AmSessionEventHandlerFactory* uac_auth_f =
305       AmPlugIn::instance()->getFactory4Seh("uac_auth");
306     if (uac_auth_f != NULL) {
307       AmSessionEventHandler *h = uac_auth_f->getHandler(this);
308       if (h != NULL ) {
309         DBG("uac-auth enabled for new callee session.\n");
310         addHandler(h);
311       }
312       else {
313         ERROR("uac_auth interface not accessible. "
314           "Load uac_auth for authenticated dialout.\n");
315       }
316     }
317   }
318 }
319