Browse code

reshaped AmSipDialog::reply to not require the request.

Raphael Coeffic authored on 15/04/2011 11:48:20
Showing 14 changed files
... ...
@@ -152,16 +152,10 @@ void SIPRegistration::onSendRequest(const string& method,
152 152
 		       hdrs,flags,cseq);
153 153
 }
154 154
 	
155
-void SIPRegistration::onSendReply(const AmSipRequest& req,
156
-				  unsigned int  code,
157
-				  const string& reason,
158
-				  const string& content_type,
159
-				  const string& body,
160
-				  string& hdrs,
161
-				  int flags) {
155
+void SIPRegistration::onSendReply(AmSipReply& reply, int flags) 
156
+{
162 157
   if (seh)
163
-    seh->onSendReply(req,code,reason,
164
-		     content_type,body,hdrs,flags);
158
+    seh->onSendReply(reply,flags);
165 159
 }
166 160
 
167 161
 SIPRegistration::RegistrationState SIPRegistration::getState() {
... ...
@@ -111,13 +111,7 @@ class SIPRegistration : //public AmSipDialogEventHandler,
111 111
 		     int flags,
112 112
 		     unsigned int cseq);
113 113
 	
114
-  void onSendReply(const AmSipRequest& req,
115
-		   unsigned int  code,
116
-		   const string& reason,
117
-		   const string& content_type,
118
-		   const string& body,
119
-		   string& hdrs,
120
-		   int flags);
114
+  void onSendReply(AmSipReply& reply, int flags);
121 115
 
122 116
   // DialogControl if
123 117
   AmSipDialog* getDlg() { return &dlg; }
... ...
@@ -876,7 +876,7 @@ void SBCDialog::onBye(const AmSipRequest& req)
876 876
 
877 877
 void SBCDialog::onCancel()
878 878
 {
879
-  dlg.reply(invite_req, 487, "Request terminated");
879
+  dlg.reply(cancel.cseq, 487, "Request terminated");
880 880
   stopCall();
881 881
 }
882 882
 
... ...
@@ -952,7 +952,7 @@ void AmB2BCallerSession::onInvite(const AmSipRequest& req)
952 952
 
953 953
 void AmB2BCallerSession::onCancel(const AmSipRequest& cancel)
954 954
 {
955
-  dlg.reply(invite_req, 487, "Request terminated");
955
+  dlg.reply(cancel.cseq, 487, "Request terminated");
956 956
   terminateOtherLeg();
957 957
   terminateLeg();
958 958
 }
... ...
@@ -881,9 +881,9 @@ void AmSession::onBye(const AmSipRequest& req)
881 881
   setStopped();
882 882
 }
883 883
 
884
-void AmSession::onCancel(const AmSipRequest& req)
884
+void AmSession::onCancel(const AmSipRequest& cancel)
885 885
 {
886
-  dlg.reply(invite_req, 487, "Request terminated");
886
+  dlg.reply(cancel.cseq, 487, "Request terminated");
887 887
   setStopped();
888 888
 }
889 889
 
... ...
@@ -900,11 +900,9 @@ void AmSession::onSendRequest(const string& method, const string& content_type,
900 900
   CALL_EVENT_H(onSendRequest,method,content_type,body,hdrs,flags,cseq);
901 901
 }
902 902
 
903
-void AmSession::onSendReply(const AmSipRequest& req, unsigned int  code, 
904
-			    const string& reason, const string& content_type,
905
-			    const string& body, string& hdrs, int flags)
903
+void AmSession::onSendReply(AmSipReply& reply, int flags)
906 904
 {
907
-  CALL_EVENT_H(onSendReply,req,code,reason,content_type,body,hdrs,flags);
905
+  CALL_EVENT_H(onSendReply,reply,flags);
908 906
 }
909 907
 
910 908
 /** Hook called when an SDP offer is required */
... ...
@@ -610,13 +610,7 @@ public:
610 610
 			     unsigned int cseq);
611 611
 
612 612
   /** Called by AmSipDialog when a reply is sent */
613
-  virtual void onSendReply(const AmSipRequest& req,
614
-			   unsigned int  code,
615
-			   const string& reason,
616
-			   const string& content_type,
617
-			   const string& body,
618
-			   string& hdrs,
619
-			   int flags);
613
+  virtual void onSendReply(AmSipReply& reply, int flags);
620 614
 
621 615
   /** Hook called when an SDP offer is required */
622 616
   virtual bool getSdpOffer(AmSdp& offer);
... ...
@@ -69,13 +69,7 @@ bool AmSessionEventHandler::onSendRequest(const string& method,
69 69
   return false;
70 70
 }
71 71
 
72
-bool AmSessionEventHandler::onSendReply(const AmSipRequest& req,
73
-					unsigned int  code,
74
-					const string& reason,
75
-					const string& content_type,
76
-					const string& body,
77
-					string& hdrs,
78
-					int flags)
72
+bool AmSessionEventHandler::onSendReply(AmSipReply& reply, int flags)
79 73
 {
80 74
   return false;
81 75
 }
... ...
@@ -78,13 +78,7 @@ public:
78 78
 			     int flags,
79 79
 			     unsigned int cseq);
80 80
 
81
-  virtual bool onSendReply(const AmSipRequest& req,
82
-			   unsigned int  code,
83
-			   const string& reason,
84
-			   const string& content_type,
85
-			   const string& body,
86
-			   string& hdrs,
87
-			   int flags);
81
+  virtual bool onSendReply(AmSipReply& reply, int flags);
88 82
 };
89 83
 
90 84
 
... ...
@@ -448,19 +448,6 @@ int AmSipDialog::onTxRequest(AmSipRequest& req)
448 448
 // UAS behavior for locally sent replies
449 449
 int AmSipDialog::onTxReply(AmSipReply& reply)
450 450
 {
451
-  TransMap::iterator t_it = uas_trans.find(reply.cseq);
452
-  if(t_it == uas_trans.end()){
453
-    ERROR("could not find any transaction matching request\n");
454
-    ERROR("reply code=%i; method=%s; callid=%s; local_tag=%s; "
455
-	  "remote_tag=%s; cseq=%i\n",
456
-	  reply.code,reply.cseq_method.c_str(),callid.c_str(),local_tag.c_str(),
457
-	  remote_tag.c_str(),reply.cseq);
458
-    return -1;
459
-  }
460
-  DBG("reply: transaction found!\n");
461
-    
462
-  AmSipTransaction& t = t_it->second;
463
-
464 451
   // update Dialog status
465 452
   switch(status){
466 453
 
... ...
@@ -545,19 +532,16 @@ int AmSipDialog::onTxReply(AmSipReply& reply)
545 532
     reply.body = "";
546 533
     reply.content_type = "";
547 534
   }
548
-  
535
+
549 536
   if ((reply.code >= 200) && 
550
-      ((t.method != "INVITE") || (reply.cseq_method != "CANCEL"))) {
537
+      ((reply.cseq_method != "CANCEL"))) {
551 538
     
552
-    DBG("reply.cseq_method = %s; t.method = %s\n",
553
-	reply.cseq_method.c_str(),t.method.c_str());
554
-    
555
-    if(t.method == "INVITE")
539
+    if(reply.cseq_method == "INVITE") 
556 540
       pending_invites--;
557 541
     
558
-    uas_trans.erase(t_it);
542
+    uas_trans.erase(reply.cseq);
559 543
   }
560
-  
544
+    
561 545
   return 0;
562 546
 }
563 547
   
... ...
@@ -980,7 +964,6 @@ string AmSipDialog::getRoute()
980 964
   return res;
981 965
 }
982 966
 
983
-
984 967
 int AmSipDialog::reply(const AmSipRequest& req,
985 968
 		       unsigned int  code,
986 969
 		       const string& reason,
... ...
@@ -989,22 +972,44 @@ int AmSipDialog::reply(const AmSipRequest& req,
989 972
 		       const string& hdrs,
990 973
 		       int flags)
991 974
 {
992
-  string m_hdrs = hdrs;
993
-
994
-  hdl->onSendReply(req,code,reason,
995
-		   content_type,body,m_hdrs,flags);
996
-
997
-  rel100OnReplyOut(req, code, m_hdrs);
975
+  return reply(req.cseq,code,reason,content_type,body,hdrs,flags);
976
+}
998 977
 
978
+int AmSipDialog::reply(unsigned int  req_cseq,
979
+		       unsigned int  code,
980
+		       const string& reason,
981
+		       const string& content_type,
982
+		       const string& body,
983
+		       const string& hdrs,
984
+		       int flags)
985
+{
986
+  TransMap::const_iterator t_it = uas_trans.find(req_cseq);
987
+  if(t_it == uas_trans.end()){
988
+    ERROR("could not find any transaction matching request cseq\n");
989
+    ERROR("request cseq=%i; reply code=%i; callid=%s; local_tag=%s; "
990
+	  "remote_tag=%s\n",
991
+	  req_cseq,code,callid.c_str(),
992
+	  local_tag.c_str(),remote_tag.c_str());
993
+    return -1;
994
+  }
995
+  DBG("reply: transaction found!\n");
996
+    
997
+  string m_hdrs = hdrs;
998
+  const AmSipTransaction& t = t_it->second;
999 999
   AmSipReply reply;
1000 1000
 
1001 1001
   reply.code = code;
1002 1002
   reply.reason = reason;
1003
-  reply.tt = req.tt;
1003
+  reply.tt = t.tt;
1004 1004
   reply.to_tag = local_tag;
1005 1005
   reply.hdrs = m_hdrs;
1006
-  reply.cseq = req.cseq;
1007
-  reply.cseq_method = req.method;
1006
+  reply.cseq = t.cseq;
1007
+  reply.cseq_method = t.method;
1008
+  reply.content_type = content_type;
1009
+  reply.body = body;
1010
+
1011
+  hdl->onSendReply(reply,flags);
1012
+  rel100OnReplyOut(reply);
1008 1013
 
1009 1014
   if (!flags&SIP_FLAGS_VERBATIM) {
1010 1015
     // add Signature
... ...
@@ -1012,14 +1017,11 @@ int AmSipDialog::reply(const AmSipRequest& req,
1012 1017
       reply.hdrs += SIP_HDR_COLSP(SIP_HDR_SERVER) + AmConfig::Signature + CRLF;
1013 1018
   }
1014 1019
 
1015
-  if (code < 300 && req.method != "CANCEL" && req.method != "BYE"){
1020
+  if (code < 300 && t.method != "CANCEL" && t.method != "BYE"){
1016 1021
     /* if 300<=code<400, explicit contact setting should be done */
1017 1022
     reply.contact = getContactHdr();
1018 1023
   }
1019 1024
 
1020
-  reply.content_type = content_type;
1021
-  reply.body = body;
1022
-
1023 1025
   if(onTxReply(reply)){
1024 1026
     DBG("onTxReply failed\n");
1025 1027
     return -1;
... ...
@@ -1031,31 +1033,33 @@ int AmSipDialog::reply(const AmSipRequest& req,
1031 1033
 
1032 1034
   if(ret){
1033 1035
     ERROR("Could not send reply: code=%i; reason='%s'; method=%s; call-id=%s; cseq=%i\n",
1034
-	  reply.code,reply.reason.c_str(),req.method.c_str(),req.callid.c_str(),req.cseq);
1036
+	  reply.code,reply.reason.c_str(),reply.cseq_method.c_str(),
1037
+	  reply.callid.c_str(),reply.cseq);
1035 1038
   }
1036 1039
   return ret;
1037 1040
 }
1038 1041
 
1039 1042
 
1040
-void AmSipDialog::rel100OnReplyOut(const AmSipRequest &req, unsigned int code, 
1041
-    string &hdrs)
1043
+void AmSipDialog::rel100OnReplyOut(AmSipReply& reply)
1042 1044
 {
1043 1045
   if (reliable_1xx == REL100_IGNORED)
1044 1046
     return;
1045 1047
 
1046
-  if (req.method == SIP_METH_INVITE) {
1047
-    if (100 < code && code < 200) {
1048
+  if (reply.cseq_method == SIP_METH_INVITE) {
1049
+    if (100 < reply.code && reply.code < 200) {
1048 1050
       switch (reliable_1xx) {
1049 1051
         case REL100_SUPPORTED:
1050
-          if (! key_in_list(getHeader(hdrs, SIP_HDR_REQUIRE), SIP_EXT_100REL))
1051
-            hdrs += SIP_HDR_COLSP(SIP_HDR_SUPPORTED) SIP_EXT_100REL CRLF;
1052
+          if (! key_in_list(getHeader(reply.hdrs, SIP_HDR_REQUIRE), 
1053
+			    SIP_EXT_100REL))
1054
+            reply.hdrs += SIP_HDR_COLSP(SIP_HDR_SUPPORTED) SIP_EXT_100REL CRLF;
1052 1055
           break;
1053 1056
         case REL100_REQUIRE:
1054 1057
           // add Require HF
1055
-          if (! key_in_list(getHeader(hdrs, SIP_HDR_REQUIRE), SIP_EXT_100REL))
1056
-            hdrs += SIP_HDR_COLSP(SIP_HDR_REQUIRE) SIP_EXT_100REL CRLF;
1058
+          if (! key_in_list(getHeader(reply.hdrs, SIP_HDR_REQUIRE), 
1059
+			    SIP_EXT_100REL))
1060
+            reply.hdrs += SIP_HDR_COLSP(SIP_HDR_REQUIRE) SIP_EXT_100REL CRLF;
1057 1061
           // add RSeq HF
1058
-          if (getHeader(hdrs, SIP_HDR_RSEQ).length())
1062
+          if (getHeader(reply.hdrs, SIP_HDR_RSEQ).length())
1059 1063
             // already added (by app?)
1060 1064
             break;
1061 1065
           if (! rseq) { // only init rseq if 1xx is used
... ...
@@ -1069,12 +1073,12 @@ void AmSipDialog::rel100OnReplyOut(const AmSipRequest &req, unsigned int code,
1069 1073
                   "PRACKed");
1070 1074
             rseq ++;
1071 1075
           }
1072
-          hdrs += SIP_HDR_COLSP(SIP_HDR_RSEQ) + int2str(rseq) + CRLF;
1076
+          reply.hdrs += SIP_HDR_COLSP(SIP_HDR_RSEQ) + int2str(rseq) + CRLF;
1073 1077
           break;
1074 1078
         default:
1075 1079
           break;
1076 1080
       }
1077
-    } else if (code < 300 && reliable_1xx == REL100_REQUIRE) { //code = 2xx
1081
+    } else if (reply.code < 300 && reliable_1xx == REL100_REQUIRE) { //code = 2xx
1078 1082
       if (rseq && !rseq_confirmed) 
1079 1083
         // reliable 1xx is pending, 2xx'ing not allowed yet
1080 1084
         throw AmSession::Exception(491, "last reliable 1xx not yet PRACKed");
... ...
@@ -142,8 +142,7 @@ private:
142 142
   void rel100OnTimeout(const AmSipRequest &req, const AmSipReply &rpl);
143 143
 
144 144
   void rel100OnRequestOut(const string &method, string &hdrs);
145
-  void rel100OnReplyOut(const AmSipRequest &req, unsigned int code, 
146
-			string &hdrs);
145
+  void rel100OnReplyOut(AmSipReply& reply);
147 146
 
148 147
   /** @return 0 on success */
149 148
   int sendRequest(const string& method, 
... ...
@@ -231,7 +230,7 @@ private:
231 230
 
232 231
   void uasTimeout(AmSipTimeoutEvent* to_ev);
233 232
     
234
-  /** @return 0 on success */
233
+  /** @return 0 on success (deprecated) */
235 234
   int reply(const AmSipRequest& req,
236 235
 	    unsigned int  code, 
237 236
 	    const string& reason,
... ...
@@ -240,6 +239,15 @@ private:
240 239
 	    const string& hdrs = "",
241 240
 	    int flags = 0);
242 241
 
242
+  /** @return 0 on success */
243
+  int reply(unsigned int  req_cseq,
244
+	    unsigned int  code, 
245
+	    const string& reason,
246
+	    const string& content_type = "",
247
+	    const string& body = "",
248
+	    const string& hdrs = "",
249
+	    int flags = 0);
250
+
243 251
   /** @return 0 on success */
244 252
   int sendRequest(const string& method, 
245 253
 		  const string& content_type = "",
... ...
@@ -339,13 +347,7 @@ class AmSipDialogEventHandler
339 347
 			     unsigned int cseq)=0;
340 348
     
341 349
   /** Hook called before a reply is sent */
342
-  virtual void onSendReply(const AmSipRequest& req,
343
-			   unsigned int  code,
344
-			   const string& reason,
345
-			   const string& content_type,
346
-			   const string& body,
347
-			   string& hdrs,
348
-			   int flags)=0;
350
+  virtual void onSendReply(AmSipReply& reply, int flags)=0;
349 351
 
350 352
   /** Hook called when a local INVITE request has been replied with 2xx */
351 353
   virtual void onInvite2xx(const AmSipReply& reply)=0;
... ...
@@ -165,30 +165,27 @@ bool SessionTimer::onSendRequest(const string& method,
165 165
 }
166 166
 
167 167
 
168
-bool SessionTimer::onSendReply(const AmSipRequest& req,
169
-			       unsigned int  code,const string& reason,
170
-			       const string& content_type,const string& body,
171
-			       string& hdrs,
172
-			       int flags)
168
+bool SessionTimer::onSendReply(AmSipReply& reply, int flags)
173 169
 {
174 170
   // only in 2xx responses to INV/UPD
175
-  if  (((req.method != SIP_METH_INVITE) && (req.method != SIP_METH_UPDATE)) ||
176
-       (code < 200) || (code >= 300))
171
+  if  (((reply.cseq_method != SIP_METH_INVITE) && 
172
+	(reply.cseq_method != SIP_METH_UPDATE)) ||
173
+       (reply.code < 200) || (reply.code >= 300))
177 174
     return false;
178 175
 
179
-  addOptionTag(hdrs, SIP_HDR_SUPPORTED, TIMER_OPTION_TAG);
176
+  addOptionTag(reply.hdrs, SIP_HDR_SUPPORTED, TIMER_OPTION_TAG);
180 177
 
181 178
   if (((session_refresher_role==UAC) && (session_refresher==refresh_remote))
182 179
       || ((session_refresher_role==UAS) && remote_timer_aware)) {
183
-    addOptionTag(hdrs, SIP_HDR_REQUIRE, TIMER_OPTION_TAG);
180
+    addOptionTag(reply.hdrs, SIP_HDR_REQUIRE, TIMER_OPTION_TAG);
184 181
   } else {
185
-    removeOptionTag(hdrs, SIP_HDR_REQUIRE, TIMER_OPTION_TAG);
182
+    removeOptionTag(reply.hdrs, SIP_HDR_REQUIRE, TIMER_OPTION_TAG);
186 183
   }
187 184
 
188 185
   // remove (possibly existing) Session-Expires header
189
-  removeHeader(hdrs, SIP_HDR_SESSION_EXPIRES);
186
+  removeHeader(reply.hdrs, SIP_HDR_SESSION_EXPIRES);
190 187
 
191
-  hdrs += SIP_HDR_COLSP(SIP_HDR_SESSION_EXPIRES) +
188
+  reply.hdrs += SIP_HDR_COLSP(SIP_HDR_SESSION_EXPIRES) +
192 189
     int2str(session_interval) + ";refresher="+
193 190
     (session_refresher_role==UAC ? "uac":"uas")+CRLF;
194 191
 
... ...
@@ -161,13 +161,7 @@ class SessionTimer: public AmSessionEventHandler
161 161
 			     int flags,
162 162
 			     unsigned int cseq);
163 163
 
164
-  virtual bool onSendReply(const AmSipRequest& req,
165
-			   unsigned int  code,
166
-			   const string& reason,
167
-			   const string& content_type,
168
-			   const string& body,
169
-			   string& hdrs,
170
-			   int flags);
164
+  virtual bool onSendReply(AmSipReply& reply, int flags);
171 165
 };
172 166
 
173 167
 
... ...
@@ -222,11 +222,7 @@ bool UACAuth::onSendRequest(const string& method,
222 222
 }
223 223
 
224 224
 
225
-bool UACAuth::onSendReply(const AmSipRequest& req,
226
-			  unsigned int  code,const string& reason,
227
-			  const string& content_type,const string& body,
228
-			  string& hdrs,
229
-			  int flags)
225
+bool UACAuth::onSendReply(AmSipReply& reply, int flags)
230 226
 {
231 227
   return false;
232 228
 }
... ...
@@ -147,13 +147,7 @@ class UACAuth : public AmSessionEventHandler
147 147
 			     int flags,
148 148
 			     unsigned int cseq);
149 149
 	
150
-  virtual bool onSendReply(const AmSipRequest& req,
151
-			   unsigned int  code,
152
-			   const string& reason,
153
-			   const string& content_type,
154
-			   const string& body,
155
-			   string& hdrs,
156
-			   int flags);
150
+  virtual bool onSendReply(AmSipReply& reply, int flags);
157 151
 };
158 152
 
159 153