1 /*
2 * Copyright (C) 2008 Raphael Coeffic
3 *
4 * This file is part of SEMS, a free SIP media server.
5 *
6 * SEMS is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version. This program is released under
10 * the GPL with the additional exemption that compiling, linking,
11 * and/or using OpenSSL is allowed.
12 *
13 * For a license to use the SEMS software under conditions
14 * other than those described here, or to purchase support for this
15 * software, please contact iptel.org by e-mail at the following addresses:
16 * info@iptel.org
17 *
18 * SEMS is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 */
27
28 #include "AmSessionContainer.h"
29 #include "AmSipDialog.h"
30 #include "AmUtils.h"
31 #include "log.h"
32
33 #include "AmSipDispatcher.h"
34 #include "AmEventDispatcher.h"
35
36 AmSipDispatcher *AmSipDispatcher::_instance;
37
instance()38 AmSipDispatcher* AmSipDispatcher::instance()
39 {
40 return _instance ? _instance : ((_instance = new AmSipDispatcher()));
41 }
42
handleSipMsg(const string & dialog_id,AmSipReply & reply)43 void AmSipDispatcher::handleSipMsg(const string& dialog_id, AmSipReply &reply)
44 {
45 const string& id = dialog_id.empty() ? reply.from_tag : dialog_id;
46 AmSipReplyEvent* ev = new AmSipReplyEvent(reply);
47
48 if(!AmEventDispatcher::instance()->post(id,ev)){
49 if ((reply.code >= 100) && (reply.code < 300)) {
50 if (AmConfig::UnhandledReplyLoglevel >= 0) {
51 _LOG(AmConfig::UnhandledReplyLoglevel,
52 "unhandled SIP reply: %s\n", reply.print().c_str());
53 }
54 } else {
55 WARN("unhandled SIP reply: %s\n", reply.print().c_str());
56 }
57 delete ev;
58 }
59 }
60
handleSipMsg(AmSipRequest & req)61 void AmSipDispatcher::handleSipMsg(AmSipRequest &req)
62 {
63 string callid = req.callid;
64 string remote_tag = req.from_tag;
65 string local_tag = req.to_tag;
66
67 AmEventDispatcher* ev_disp = AmEventDispatcher::instance();
68
69 if(req.method == SIP_METH_CANCEL){
70
71 if(ev_disp->postSipRequest(req)){
72 return;
73 }
74
75 // CANCEL of a (here) non-existing dialog
76 AmSipDialog::reply_error(req,481,SIP_REPLY_NOT_EXIST);
77 return;
78 }
79 else if(!local_tag.empty()) {
80 // in-dlg request
81 AmSipRequestEvent* ev = new AmSipRequestEvent(req);
82
83 // Contact-user may contain internal dialog ID (must be tried before using
84 // local_tag for identification)
85 if(!req.user.empty() && ev_disp->post(req.user,ev))
86 return;
87
88 if(ev_disp->post(local_tag,ev))
89 return;
90
91 delete ev;
92 if(req.method != SIP_METH_ACK) {
93 AmSipDialog::reply_error(req,481,
94 "Call leg/Transaction does not exist");
95 }
96 else {
97 DBG("received ACK for non-existing dialog "
98 "(callid=%s;remote_tag=%s;local_tag=%s)\n",
99 callid.c_str(),remote_tag.c_str(),local_tag.c_str());
100 }
101
102 return;
103 }
104
105 DBG("method: `%s' [%zd].\n", req.method.c_str(), req.method.length());
106 if(req.method == SIP_METH_INVITE){
107
108 AmSessionContainer::instance()->startSessionUAS(req);
109 }
110 else if(req.method == SIP_METH_BYE ||
111 req.method == SIP_METH_PRACK){
112
113 // BYE/PRACK of a (here) non-existing dialog
114 AmSipDialog::reply_error(req,481,SIP_REPLY_NOT_EXIST);
115 return;
116
117 } else {
118
119 string app_name;
120 AmSessionFactory* sess_fact = AmPlugIn::instance()->findSessionFactory(req,app_name);
121 if (sess_fact) {
122 try {
123 sess_fact->onOoDRequest(req);
124 return;
125 } catch (AmSession::Exception& e) {
126 AmSipDialog::reply_error(req,e.code,e.reason, e.hdrs);
127 ERROR("%i %s %s\n",e.code,e.reason.c_str(), e.hdrs.c_str());
128 return;
129 }
130 }
131
132 if (req.method == SIP_METH_OPTIONS) {
133 AmSessionFactory::replyOptions(req);
134 return;
135 }
136
137 AmSipDialog::reply_error(req,404,"Not found");
138 }
139
140 }
141