~ubuntu-branches/ubuntu/saucy/resiprocate/saucy-proposed

« back to all changes in this revision

Viewing changes to .pc/0002-discard-repeated-requests-received-over-reliable-tra.patch/resip/stack/TransactionState.cxx

  • Committer: Package Import Robot
  • Author(s): Daniel Pocock
  • Date: 2013-03-17 12:30:47 UTC
  • Revision ID: package-import@ubuntu.com-20130317123047-hg9iyafz2otqi0d3
Tags: 1.8.5-2
* Remove proxy-auth headers after validating them (Closes: #697341)
* Discard repeated requests over reliable transport (Closes: #703232)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#if defined(HAVE_CONFIG_H)
 
2
#include "config.h"
 
3
#endif
 
4
 
 
5
#include "resip/stack/AbandonServerTransaction.hxx"
 
6
#include "resip/stack/CancelClientInviteTransaction.hxx"
 
7
#include "resip/stack/TerminateFlow.hxx"
 
8
#include "resip/stack/EnableFlowTimer.hxx"
 
9
#include "resip/stack/ZeroOutStatistics.hxx"
 
10
#include "resip/stack/PollStatistics.hxx"
 
11
#include "resip/stack/ConnectionTerminated.hxx"
 
12
#include "resip/stack/KeepAlivePong.hxx"
 
13
#include "resip/stack/DnsInterface.hxx"
 
14
#include "resip/stack/DnsResultMessage.hxx"
 
15
#include "resip/stack/DnsResult.hxx"
 
16
#include "resip/stack/Helper.hxx"
 
17
#include "resip/stack/MethodTypes.hxx"
 
18
#include "resip/stack/SipMessage.hxx"
 
19
#include "resip/stack/SendData.hxx"
 
20
#include "resip/stack/SipStack.hxx"
 
21
#include "resip/stack/StatisticsManager.hxx"
 
22
#include "resip/stack/TimerMessage.hxx"
 
23
#include "resip/stack/TransactionController.hxx"
 
24
#include "resip/stack/TransactionMessage.hxx"
 
25
#include "resip/stack/TransactionState.hxx"
 
26
#include "resip/stack/TransactionTerminated.hxx"
 
27
#include "resip/stack/TransportFailure.hxx"
 
28
#include "resip/stack/TransactionUserMessage.hxx"
 
29
#include "resip/stack/TransportFailure.hxx"
 
30
#include "resip/stack/TransportSelector.hxx"
 
31
#include "resip/stack/TransactionUser.hxx"
 
32
#include "resip/stack/TuSelector.hxx"
 
33
#include "resip/stack/InteropHelper.hxx"
 
34
#include "resip/stack/KeepAliveMessage.hxx"
 
35
#include "rutil/DnsUtil.hxx"
 
36
#include "rutil/Logger.hxx"
 
37
#include "rutil/MD5Stream.hxx"
 
38
#include "rutil/Socket.hxx"
 
39
#include "rutil/Random.hxx"
 
40
#include "rutil/WinLeakCheck.hxx"
 
41
 
 
42
using namespace resip;
 
43
 
 
44
#define RESIPROCATE_SUBSYSTEM Subsystem::TRANSACTION
 
45
 
 
46
unsigned long TransactionState::StatelessIdCounter = 0;
 
47
 
 
48
TransactionState::TransactionState(TransactionController& controller, Machine m, 
 
49
                                   State s, const Data& id, MethodTypes method, const Data& methodText, TransactionUser* tu) : 
 
50
   mController(controller),
 
51
   mMachine(m), 
 
52
   mState(s),
 
53
   mIsAbandoned(false),
 
54
   mIsReliable(true), // !jf! 
 
55
   mNextTransmission(0),
 
56
   mDnsResult(0),
 
57
   mId(id),
 
58
   mMethod(method),
 
59
   mMethodText(method==UNKNOWN ? new Data(methodText) : 0),
 
60
   mCurrentMethodType(UNKNOWN),
 
61
   mCurrentResponseCode(0),
 
62
   mAckIsValid(false),
 
63
   mWaitingForDnsResult(false),
 
64
   mTransactionUser(tu),
 
65
   mFailureReason(TransportFailure::None),
 
66
   mFailureSubCode(0)
 
67
{
 
68
   StackLog (<< "Creating new TransactionState: " << *this);
 
69
}
 
70
 
 
71
 
 
72
TransactionState* 
 
73
TransactionState::makeCancelTransaction(TransactionState* tr, Machine machine, const Data& tid)
 
74
{
 
75
   TransactionState* cancel = new TransactionState(tr->mController, 
 
76
                                                   machine, 
 
77
                                                   Trying, 
 
78
                                                   tid, 
 
79
                                                   CANCEL, 
 
80
                                                   Data::Empty,
 
81
                                                   tr->mTransactionUser);
 
82
   // !jf! don't set this since it will be set by TransactionState::processReliability()
 
83
   //cancel->mIsReliable = tr->mIsReliable;  
 
84
   cancel->mResponseTarget = tr->mResponseTarget;
 
85
   cancel->mTarget = tr->mTarget;
 
86
   cancel->add(tid);
 
87
 
 
88
   // !jf! don't call processServerNonInvite since it will delete
 
89
   // the sip message which needs to get sent to the TU
 
90
   cancel->processReliability(tr->mTarget.getType());
 
91
   return cancel;
 
92
}
 
93
 
 
94
void 
 
95
TransactionState::handleInternalCancel(SipMessage* cancel,
 
96
                                       TransactionState& clientInvite)
 
97
{
 
98
   TransactionState* state = TransactionState::makeCancelTransaction(&clientInvite, ClientNonInvite, clientInvite.mId+"cancel");
 
99
   // Make sure the branch in the CANCEL matches the current 
 
100
   // branch of the INVITE, in case we have done a DNS failover (the transport 
 
101
   // sequences could be different by now)
 
102
   cancel->header(h_Vias).front().param(p_branch)=clientInvite.mNextTransmission->const_header(h_Vias).front().param(p_branch);
 
103
   state->processClientNonInvite(cancel);
 
104
   // for the INVITE in case we never get a 487
 
105
   clientInvite.mController.mTimers.add(Timer::TimerCleanUp, clientInvite.mId, 128*Timer::T1);
 
106
}
 
107
 
 
108
bool
 
109
TransactionState::handleBadRequest(const resip::SipMessage& badReq, TransactionController& controller)
 
110
{
 
111
   assert(badReq.isRequest() && badReq.method() != ACK);
 
112
   try
 
113
   {
 
114
      SipMessage* error = Helper::makeResponse(badReq,400);
 
115
      if(badReq.getReason())
 
116
      {
 
117
         error->header(h_StatusLine).reason()+="(" + *(badReq.getReason())+ ")";
 
118
      }
 
119
      Tuple target(badReq.getSource());
 
120
 
 
121
      if(badReq.isExternal())
 
122
      {
 
123
         controller.mTransportSelector.transmit(error,target);
 
124
         delete error;
 
125
         return true;
 
126
      }
 
127
      else
 
128
      {
 
129
         // ?bwc? Should we put together a TransactionState here so we can
 
130
         // send a 400 to the TU?
 
131
         // TODO if we send the error to the TU, don't delete the error
 
132
         delete error;
 
133
         return false;
 
134
      }
 
135
   }
 
136
   catch(resip::BaseException& e)
 
137
   {
 
138
      ErrLog(<< "Exception thrown in TransactionState::handleBadRequest."
 
139
                  " This shouldn't happen. " << e);
 
140
      return false;
 
141
   }
 
142
}
 
143
 
 
144
TransactionState::~TransactionState()
 
145
{
 
146
   assert(mState != Bogus);
 
147
 
 
148
   if (mDnsResult)
 
149
   {
 
150
      mDnsResult->destroy();
 
151
   }
 
152
 
 
153
   //StackLog (<< "Deleting TransactionState " << mId << " : " << this);
 
154
   erase(mId);
 
155
   
 
156
   delete mNextTransmission;
 
157
   delete mMethodText;
 
158
   mNextTransmission = 0;
 
159
   mMethodText = 0;
 
160
 
 
161
   mState = Bogus;
 
162
}
 
163
 
 
164
bool
 
165
TransactionState::processSipMessageAsNew(SipMessage* sip, TransactionController& controller, const Data& tid)
 
166
{
 
167
   MethodTypes method=sip->method();
 
168
   StackLog (<< "No matching transaction for " << sip->brief());
 
169
   TransactionUser* tu = 0;      
 
170
   if (sip->isExternal())
 
171
   {
 
172
      if (controller.mTuSelector.haveTransactionUsers() && sip->isRequest())
 
173
      {
 
174
         tu = controller.mTuSelector.selectTransactionUser(*sip);
 
175
         if (!tu)
 
176
         {
 
177
            //InfoLog (<< "Didn't find a TU for " << sip->brief());
 
178
            // !bwc! We really should do something other than a 500 here.
 
179
            // If none of the TUs liked the request because of the Request-
 
180
            // Uri scheme, we should be returning a 416, for example.
 
181
            // ?bwc? Um, should we _really_ be doing this statelessly?
 
182
            InfoLog( << "No TU found for message: " << sip->brief());               
 
183
            SipMessage* noMatch = Helper::makeResponse(*sip, 500);
 
184
            Tuple target(sip->getSource());
 
185
 
 
186
            controller.mTransportSelector.transmit(noMatch, target);
 
187
            delete noMatch;
 
188
            return false;
 
189
         }
 
190
         else
 
191
         {
 
192
            //InfoLog (<< "Found TU for " << sip->brief());
 
193
         }
 
194
      }
 
195
   }
 
196
   else
 
197
   {
 
198
      tu = sip->getTransactionUser();
 
199
      if (!tu)
 
200
      {
 
201
         //InfoLog (<< "No TU associated with " << sip->brief());
 
202
      }
 
203
   }
 
204
               
 
205
   if (sip->isRequest())
 
206
   {
 
207
      // create a new state object and insert in the TransactionMap
 
208
               
 
209
      if (sip->isExternal()) // new sip msg from transport
 
210
      {
 
211
         if (method == INVITE)
 
212
         {
 
213
            // !rk! This might be needlessly created.  Design issue.
 
214
            TransactionState* state = new TransactionState(controller, 
 
215
                                                            ServerInvite, 
 
216
                                                            Trying, 
 
217
                                                            tid, 
 
218
                                                            INVITE,
 
219
                                                            Data::Empty,
 
220
                                                            tu);
 
221
 
 
222
            state->mNextTransmission = state->make100(sip);
 
223
            state->mResponseTarget = sip->getSource(); // UACs source address
 
224
            // since we don't want to reply to the source port if rport present 
 
225
            state->mResponseTarget.setPort(Helper::getPortForReply(*sip));
 
226
            state->mIsReliable = isReliable(state->mResponseTarget.getType());
 
227
            state->add(tid);
 
228
               
 
229
            if (Timer::T100 == 0)
 
230
            {
 
231
               state->sendCurrentToWire(); // will get deleted when this is deleted
 
232
               state->mState = Proceeding;
 
233
            }
 
234
            else
 
235
            {
 
236
               //StackLog(<<" adding T100 timer (INV)");
 
237
               controller.mTimers.add(Timer::TimerTrying, tid, Timer::T100);
 
238
            }
 
239
            state->sendToTU(sip);
 
240
            return true;
 
241
         }
 
242
         else if (method == CANCEL)
 
243
         {
 
244
            TransactionState* matchingInvite = 
 
245
               controller.mServerTransactionMap.find(sip->getTransactionId());
 
246
            if (matchingInvite == 0)
 
247
            {
 
248
               InfoLog (<< "No matching INVITE for incoming (from wire) CANCEL to uas");
 
249
               //was TransactionState::sendToTU(tu, controller, Helper::makeResponse(*sip, 481));
 
250
               SipMessage* response = Helper::makeResponse(*sip, 481);
 
251
               Tuple target(sip->getSource());
 
252
               controller.mTransportSelector.transmit(response, target);
 
253
               
 
254
               delete response;
 
255
               return false;
 
256
            }
 
257
            else
 
258
            {
 
259
               assert(matchingInvite);
 
260
               TransactionState* state = TransactionState::makeCancelTransaction(matchingInvite, ServerNonInvite, tid);
 
261
               state->startServerNonInviteTimerTrying(*sip,tid);
 
262
               state->sendToTU(sip);
 
263
               return true;
 
264
            }
 
265
         }
 
266
         else if (method != ACK)
 
267
         {
 
268
            TransactionState* state = new TransactionState(controller, 
 
269
                                                            ServerNonInvite,
 
270
                                                            Trying, 
 
271
                                                            tid, 
 
272
                                                            method,
 
273
                                                            sip->methodStr(),
 
274
                                                            tu);
 
275
            state->mResponseTarget = sip->getSource();
 
276
            // since we don't want to reply to the source port if rport present 
 
277
            state->mResponseTarget.setPort(Helper::getPortForReply(*sip));
 
278
            state->add(tid);
 
279
            state->mIsReliable = isReliable(state->mResponseTarget.getType());
 
280
            state->startServerNonInviteTimerTrying(*sip,tid);
 
281
            state->sendToTU(sip);
 
282
            return true;
 
283
         }
 
284
            
 
285
         // Incoming ACK just gets passed to the TU
 
286
         //StackLog(<< "Adding incoming message to TU fifo " << tid);
 
287
         TransactionState::sendToTU(tu, controller, sip);
 
288
      }
 
289
      else // new sip msg from the TU
 
290
      {
 
291
         if (method == INVITE)
 
292
         {
 
293
            TransactionState* state = new TransactionState(controller, 
 
294
                                                            ClientInvite, 
 
295
                                                            Calling, 
 
296
                                                            tid, 
 
297
                                                            INVITE,
 
298
                                                            Data::Empty,
 
299
                                                            tu);
 
300
            state->add(state->mId);
 
301
            state->processClientInvite(sip);
 
302
         }
 
303
         else if (method == ACK)
 
304
         {
 
305
            TransactionState* state = new TransactionState(controller, 
 
306
                                                            Stateless, 
 
307
                                                            Calling, 
 
308
                                                            tid, 
 
309
                                                            ACK,
 
310
                                                            Data::Empty,
 
311
                                                            tu);
 
312
            state->add(state->mId);
 
313
            state->mController.mTimers.add(Timer::TimerStateless, state->mId, Timer::TS );
 
314
            state->processStateless(sip);
 
315
         }
 
316
         else if (method == CANCEL)
 
317
         {
 
318
            TransactionState* matchingInvite = controller.mClientTransactionMap.find(sip->getTransactionId());
 
319
               
 
320
            if (matchingInvite == 0)
 
321
            {
 
322
               InfoLog (<< "No matching INVITE for incoming (from TU) CANCEL to uac");
 
323
               TransactionState::sendToTU(tu, controller, Helper::makeResponse(*sip,481));
 
324
               return false;
 
325
            }
 
326
            else if (matchingInvite->mState == Calling) // CANCEL before 1xx received
 
327
            {
 
328
               WarningLog(<< "You can't CANCEL a request until a provisional has been received");
 
329
               StackLog (<< *matchingInvite);
 
330
               StackLog (<< *sip);
 
331
 
 
332
               matchingInvite->mIsAbandoned = true;
 
333
               return false;
 
334
            }
 
335
            else if (matchingInvite->mState == Completed)
 
336
            {
 
337
               // A final response was already seen for this INVITE transaction
 
338
               matchingInvite->sendToTU(Helper::makeResponse(*sip, 200));
 
339
               return false;
 
340
            }
 
341
            else
 
342
            {
 
343
               handleInternalCancel(sip, *matchingInvite);
 
344
            }
 
345
         }
 
346
         else 
 
347
         {
 
348
            TransactionState* state = new TransactionState(controller, 
 
349
                                                            ClientNonInvite, 
 
350
                                                            Trying, 
 
351
                                                            tid, 
 
352
                                                            method,
 
353
                                                            sip->methodStr(),
 
354
                                                            tu);
 
355
            state->add(tid);
 
356
            state->processClientNonInvite(sip);
 
357
         }
 
358
      }
 
359
   }
 
360
   else if (sip->isResponse()) // stray response
 
361
   {
 
362
      if (controller.mDiscardStrayResponses)
 
363
      {
 
364
         InfoLog (<< "discarding stray response: " << sip->brief());
 
365
         return false;
 
366
      }
 
367
      else
 
368
      {
 
369
         StackLog (<< "forwarding stateless response: " << sip->brief());
 
370
         TransactionState* state = 
 
371
            new TransactionState(controller, 
 
372
                                 Stateless, 
 
373
                                 Calling, 
 
374
                                 Data(StatelessIdCounter++), 
 
375
                                 method,
 
376
                                 sip->methodStr(),
 
377
                                 tu);
 
378
         state->add(state->mId);
 
379
         state->mController.mTimers.add(Timer::TimerStateless, state->mId, Timer::TS );
 
380
         state->processStateless(sip);
 
381
      }
 
382
   }
 
383
   else // wasn't a request or a response
 
384
   {
 
385
      ErrLog (<< "Got a SipMessage that was neither a request nor response!" 
 
386
               << sip->brief());
 
387
      return false;
 
388
   }
 
389
 
 
390
   return true;
 
391
}
 
392
 
 
393
void
 
394
TransactionState::process(TransactionController& controller, 
 
395
                           TransactionMessage* message)
 
396
{
 
397
   {
 
398
      KeepAliveMessage* keepAlive = dynamic_cast<KeepAliveMessage*>(message);
 
399
      if (keepAlive)
 
400
      {
 
401
         StackLog ( << "Sending keep alive to: " << keepAlive->getDestination());      
 
402
         controller.mTransportSelector.transmit(keepAlive, keepAlive->getDestination());
 
403
         delete keepAlive;
 
404
         return;      
 
405
      }
 
406
 
 
407
      ConnectionTerminated* term = dynamic_cast<ConnectionTerminated*>(message);
 
408
      if (term)
 
409
      {
 
410
         if(term->hasTransactionUser())
 
411
         {
 
412
            controller.mTuSelector.add(term);
 
413
         }
 
414
         else
 
415
         {
 
416
            // .bwc. This means we are using this message to close a connection.
 
417
            controller.mTransportSelector.closeConnection(term->getFlow());
 
418
         }
 
419
         delete term;
 
420
         return;
 
421
      }
 
422
 
 
423
      KeepAlivePong* pong = dynamic_cast<KeepAlivePong*>(message);
 
424
      if (pong)
 
425
      {
 
426
         controller.mTuSelector.add(pong);
 
427
         delete pong;
 
428
         return;
 
429
      }
 
430
 
 
431
      TerminateFlow* termFlow = dynamic_cast<TerminateFlow*>(message);
 
432
      if(termFlow)
 
433
      {
 
434
         controller.mTransportSelector.terminateFlow(termFlow->getFlow());
 
435
         delete termFlow;
 
436
         return;
 
437
      }
 
438
 
 
439
      EnableFlowTimer* enableFlowTimer = dynamic_cast<EnableFlowTimer*>(message);
 
440
      if(enableFlowTimer)
 
441
      {
 
442
         controller.mTransportSelector.enableFlowTimer(enableFlowTimer->getFlow());
 
443
         delete enableFlowTimer;
 
444
         return;
 
445
      }
 
446
 
 
447
      ZeroOutStatistics* zeroOutStatistics = dynamic_cast<ZeroOutStatistics*>(message);
 
448
      if(zeroOutStatistics)
 
449
      {
 
450
         controller.mStatsManager.zeroOut();
 
451
         delete zeroOutStatistics;
 
452
         return;
 
453
      }
 
454
 
 
455
      PollStatistics* pollStatistics = dynamic_cast<PollStatistics*>(message);
 
456
      if(pollStatistics)
 
457
      {
 
458
         controller.mStatsManager.poll();
 
459
         delete pollStatistics;
 
460
         return;
 
461
      }
 
462
   }
 
463
   
 
464
   // .bwc. We can't do anything without a tid here. Check this first.
 
465
   Data tid;   
 
466
   try
 
467
   {
 
468
      tid = message->getTransactionId();
 
469
   }
 
470
   catch(SipMessage::Exception&)
 
471
   {
 
472
      // .bwc This is not our error. Do not ErrLog.
 
473
      DebugLog( << "TransactionState::process dropping message with invalid tid " << message->brief());
 
474
      delete message;
 
475
      return;
 
476
   }
 
477
   
 
478
   SipMessage* sip = dynamic_cast<SipMessage*>(message);
 
479
   MethodTypes method = UNKNOWN;
 
480
 
 
481
   if(sip)
 
482
   {
 
483
      method=sip->method();
 
484
      // ?bwc? Should this come after checking for error conditions?
 
485
      if(controller.mStack.statisticsManagerEnabled() && sip->isExternal())
 
486
      {
 
487
         controller.mStatsManager.received(sip);
 
488
      }
 
489
      
 
490
      // .bwc. Check for error conditions we can respond to.
 
491
      if(sip->isRequest() && method != ACK)
 
492
      {
 
493
         // .bwc. If we are going to statelessly send a 503, we should do it
 
494
         // in the transport, before we do expensive stuff like basicCheck and
 
495
         // stampReceived. If the TU fifo is backed up, we should send a 
 
496
         // _stateful_ 503 below. (And only if the specific TU that can handle
 
497
         // this message is backed up; if other TUs are congested, we should let 
 
498
         // it pass.)
 
499
         /*
 
500
         if(sip->isExternal() && (controller.isTUOverloaded() || controller.mStateMacFifo.isRejecting()))
 
501
         {
 
502
            SipMessage* tryLater = Helper::makeResponse(*sip, 503);
 
503
            if( controller.mStateMacFifo.isRejecting() )
 
504
              tryLater->header(h_RetryAfter).value() = controller.mStateMacFifo.getRetryAfter();
 
505
            else
 
506
              tryLater->header(h_RetryAfter).value() = 32 + (Random::getRandom() % 32);
 
507
              
 
508
            //tryLater->header(h_RetryAfter).comment() = "Server busy TRANS";
 
509
            Tuple target(sip->getSource());
 
510
            delete sip;
 
511
            controller.mTransportSelector.transmit(tryLater, target);
 
512
            delete tryLater;
 
513
            return;
 
514
         }
 
515
         */
 
516
         
 
517
         if(sip->isInvalid())
 
518
         {
 
519
            handleBadRequest(*sip,controller);
 
520
            delete sip;
 
521
            return;
 
522
         }         
 
523
      }
 
524
 
 
525
#ifdef PEDANTIC_STACK
 
526
      try
 
527
      {
 
528
         sip->parseAllHeaders();
 
529
      }
 
530
      catch(resip::ParseException& e)
 
531
      {
 
532
         if(sip->isRequest() && method!=ACK)
 
533
         {
 
534
            handleBadRequest(*sip,controller);
 
535
         }
 
536
         
 
537
         InfoLog(<< "Exception caught by pedantic stack: " << e);
 
538
      }
 
539
#endif      
 
540
      
 
541
      // This ensures that CANCEL requests form unique transactions
 
542
      if (method == CANCEL) 
 
543
      {
 
544
         tid += "cancel";
 
545
      }
 
546
   }
 
547
      
 
548
   TransactionState* state = 0;
 
549
   if (message->isClientTransaction()) 
 
550
     state = controller.mClientTransactionMap.find(tid);
 
551
   else 
 
552
     state = controller.mServerTransactionMap.find(tid);
 
553
   
 
554
   if (state && sip && sip->isExternal())
 
555
   {
 
556
      // Various kinds of response fixup.
 
557
      if(sip->isResponse() &&
 
558
         state->mNextTransmission)
 
559
      {
 
560
         // .bwc. This code (if enabled) ensures that responses have the same
 
561
         // CallId and tags as the request did (excepting the introduction of a 
 
562
         // remote tag). This is to protect dialog-stateful TUs that don't react 
 
563
         // gracefully when a stupid/malicious endpoint fiddles with the tags 
 
564
         // and/or CallId when it isn't supposed to. (DUM is one such TU)
 
565
         if(state->mController.getFixBadDialogIdentifiers())
 
566
         {
 
567
            if(sip->const_header(h_CallId).isWellFormed())
 
568
            {
 
569
               if(!(sip->const_header(h_CallId) == 
 
570
                           state->mNextTransmission->const_header(h_CallId)))
 
571
               {
 
572
                  InfoLog(<< "Other end modified our Call-Id... correcting.");
 
573
                  sip->header(h_CallId) = state->mNextTransmission->const_header(h_CallId);
 
574
               }
 
575
            }
 
576
            else
 
577
            {
 
578
               InfoLog(<< "Other end corrupted our CallId... correcting.");
 
579
               sip->header(h_CallId) = state->mNextTransmission->const_header(h_CallId);
 
580
            }
 
581
   
 
582
            const NameAddr& from = state->mNextTransmission->const_header(h_From);
 
583
            if(sip->const_header(h_From).isWellFormed())
 
584
            {
 
585
               // Overwrite tag.
 
586
               if(from.exists(p_tag))
 
587
               {
 
588
                  if(sip->const_header(h_From).param(p_tag) != from.param(p_tag))
 
589
                  {
 
590
                     InfoLog(<<"Other end modified our local tag... correcting.");
 
591
                     sip->header(h_From).param(p_tag) = from.param(p_tag);
 
592
                  }
 
593
               }
 
594
               else if(sip->const_header(h_From).exists(p_tag))
 
595
               {
 
596
                  if(sip->const_header(h_From).exists(p_tag))
 
597
                  {
 
598
                     InfoLog(<<"Other end added a local tag for us... removing.");
 
599
                     sip->header(h_From).remove(p_tag);
 
600
                  }
 
601
               }
 
602
            }
 
603
            else
 
604
            {
 
605
               InfoLog(<<"Other end corrupted our From header... replacing.");
 
606
               // Whole header is hosed, overwrite.
 
607
               sip->header(h_From) = from;
 
608
            }
 
609
   
 
610
            const NameAddr& to = state->mNextTransmission->const_header(h_To);
 
611
            if(sip->const_header(h_To).isWellFormed())
 
612
            {
 
613
               // Overwrite tag.
 
614
               if(to.exists(p_tag))
 
615
               {
 
616
                  if(sip->const_header(h_To).param(p_tag) != to.param(p_tag))
 
617
                  {
 
618
                     InfoLog(<<"Other end modified the (existing) remote tag... "
 
619
                                 "correcting.");
 
620
                     sip->header(h_To).param(p_tag) = to.param(p_tag);
 
621
                  }
 
622
               }
 
623
            }
 
624
            else
 
625
            {
 
626
               InfoLog(<<"Other end corrupted our To header... replacing.");
 
627
               // Whole header is hosed, overwrite.
 
628
               sip->header(h_To) = to;
 
629
            }
 
630
         }
 
631
 
 
632
         // .bwc. This code (if enabled) ensures that responses have the same
 
633
         // CSeq number as the request did. This is to protect TUs that don't 
 
634
         // react gracefully when a stupid/malicious endpoint fiddles with the 
 
635
         // CSeq number. (This is a very cheap check; we already parse the CSeq
 
636
         // for incoming messages)
 
637
         if(state->mController.getFixBadCSeqNumbers())
 
638
         {
 
639
            unsigned int old=state->mNextTransmission->const_header(h_CSeq).sequence();
 
640
            if(sip->const_header(h_CSeq).sequence()!=old)
 
641
            {
 
642
               InfoLog(<<"Other end changed our CSeq number... replacing.");
 
643
               sip->header(h_CSeq).sequence()=old;
 
644
            }
 
645
 
 
646
            if(state->mNextTransmission->exists(h_RAck))
 
647
            {
 
648
               if(!(sip->const_header(h_RAck)==state->mNextTransmission->const_header(h_RAck)))
 
649
               {
 
650
                  InfoLog(<<"Other end changed our RAck... replacing.");
 
651
                  sip->header(h_RAck)=state->mNextTransmission->const_header(h_RAck);
 
652
               }
 
653
            }
 
654
         }
 
655
      }
 
656
      // .bwc. This code ensures that the transaction state-machine can recover
 
657
      // from ACK/200 with the same tid as the original INVITE. This problem is
 
658
      // stupidly common. 
 
659
      if(sip->isRequest() && method == ACK && !state->mAckIsValid)
 
660
      {
 
661
         // Must have received an ACK to a 200;
 
662
         // We will never respond to this, so nothing will need this tid for
 
663
         // driving transaction state. Additionally, 
 
664
         InfoLog(<<"Someone sent us an ACK/200 with the same tid as the "
 
665
                     "original INVITE. This is bad behavior, and should be "
 
666
                     "corrected in the client.");
 
667
         sip->mIsBadAck200=true;
 
668
         // .bwc. This is a new stateless transaction, despite its tid.
 
669
         state=0;
 
670
      }
 
671
   }
 
672
 
 
673
   if(state && sip)
 
674
   {
 
675
      switch(state->mMethod)
 
676
      {
 
677
         case INVITE:
 
678
            if(method != INVITE && method != ACK)
 
679
            {
 
680
               // Maybe respond if a request?
 
681
               delete sip;
 
682
               return;
 
683
            }
 
684
            break;
 
685
         case UNKNOWN:
 
686
            if(!state->mMethodText || *(state->mMethodText) != sip->methodStr())
 
687
            {
 
688
               // Maybe respond if a request?
 
689
               delete sip;
 
690
               return;
 
691
            }
 
692
            break;
 
693
         default:
 
694
            if(state->mMethod != method)
 
695
            {
 
696
               // Maybe respond if a request?
 
697
               delete sip;
 
698
               return;
 
699
            }
 
700
            break;
 
701
      }
 
702
   }
 
703
 
 
704
   if (state) // found transaction for sip msg
 
705
   {
 
706
      StackLog (<< "Found matching transaction for " << message->brief() << " -> " << *state);
 
707
 
 
708
      switch (state->mMachine)
 
709
      {
 
710
         case ClientNonInvite:
 
711
            state->processClientNonInvite(message);
 
712
            break;
 
713
         case ClientInvite:
 
714
            // ACK from TU will be Stateless
 
715
            assert (!sip || !(state->isFromTU(sip) &&  sip->isRequest() && method == ACK));
 
716
            state->processClientInvite(message);
 
717
            break;
 
718
         case ServerNonInvite:
 
719
            state->processServerNonInvite(message);
 
720
            break;
 
721
         case ServerInvite:
 
722
            state->processServerInvite(message);
 
723
            break;
 
724
         case Stateless:
 
725
            state->processStateless(message);
 
726
            break;
 
727
         case ClientStale:
 
728
            state->processClientStale(message);
 
729
            break;
 
730
         case ServerStale:
 
731
            state->processServerStale(message);
 
732
            break;
 
733
         default:
 
734
            CritLog(<<"internal state error");
 
735
            assert(0);
 
736
            return;
 
737
      }
 
738
   }
 
739
   else if (sip)  // new transaction
 
740
   {
 
741
      try
 
742
      {
 
743
         bool processed = processSipMessageAsNew(sip, controller, tid);
 
744
         if (!processed)
 
745
         {
 
746
            delete sip;      
 
747
         }
 
748
      }
 
749
      catch(resip::ParseException& e)   
 
750
      {
 
751
         StackLog ( << "Got badly formatted sip message, error: " << e.what());      
 
752
         if(sip->isRequest() && sip->method()!=ACK)
 
753
         {
 
754
            handleBadRequest(*sip, controller);
 
755
         }         
 
756
         delete sip;      
 
757
      }
 
758
      catch(std::exception& err)      
 
759
      {
 
760
         StackLog ( << "Got error: " << err.what());
 
761
         delete sip;
 
762
      }
 
763
   } 
 
764
   else // timer or other non-sip msg
 
765
   {
 
766
      //StackLog (<< "discarding non-sip message: " << message->brief());
 
767
      delete message;
 
768
   }
 
769
}
 
770
 
 
771
void
 
772
TransactionState::processTimer(TransactionController& controller,
 
773
                                 TimerMessage* message)
 
774
{
 
775
   Data tid = message->getTransactionId();
 
776
 
 
777
   if(controller.getRejectionBehavior()==CongestionManager::REJECTING_NON_ESSENTIAL)
 
778
   {
 
779
      // .bwc. State machine fifo is backed up; we probably should not be 
 
780
      // retransmitting anything right now. If we have a retransmit timer, 
 
781
      // reschedule for later, but don't retransmit.
 
782
      switch(message->getType())
 
783
      {
 
784
         case Timer::TimerA: // doubling
 
785
            controller.mTimers.add(Timer::TimerA, 
 
786
                                    tid, 
 
787
                                    message->getDuration()*2);
 
788
            delete message;
 
789
            return;
 
790
         case Timer::TimerE1:// doubling, until T2
 
791
         case Timer::TimerG: // doubling, until T2
 
792
            controller.mTimers.add(message->getType(), 
 
793
                                    tid, 
 
794
                                    resipMin(message->getDuration()*2,
 
795
                                             Timer::T2));
 
796
            delete message;
 
797
            return;
 
798
         case Timer::TimerE2:// just reset
 
799
            controller.mTimers.add(Timer::TimerE2, 
 
800
                                    tid, 
 
801
                                    Timer::T2);
 
802
            delete message;
 
803
            return;
 
804
         default:
 
805
            ; // let it through
 
806
      }
 
807
   }
 
808
 
 
809
   TransactionState* state = 0;
 
810
   if (message->isClientTransaction()) state = controller.mClientTransactionMap.find(tid);
 
811
   else state = controller.mServerTransactionMap.find(tid);
 
812
   
 
813
   if (state) // found transaction for timer
 
814
   {
 
815
      StackLog (<< "Found matching transaction for " << message->brief() << " -> " << *state);
 
816
 
 
817
      switch (state->mMachine)
 
818
      {
 
819
         case ClientNonInvite:
 
820
            state->processClientNonInvite(message);
 
821
            break;
 
822
         case ClientInvite:
 
823
            state->processClientInvite(message);
 
824
            break;
 
825
         case ServerNonInvite:
 
826
            state->processServerNonInvite(message);
 
827
            break;
 
828
         case ServerInvite:
 
829
            state->processServerInvite(message);
 
830
            break;
 
831
         case Stateless:
 
832
            state->processStateless(message);
 
833
            break;
 
834
         case ClientStale:
 
835
            state->processClientStale(message);
 
836
            break;
 
837
         case ServerStale:
 
838
            state->processServerStale(message);
 
839
            break;
 
840
         default:
 
841
            CritLog(<<"internal state error");
 
842
            assert(0);
 
843
            return;
 
844
      }
 
845
   }
 
846
   else
 
847
   {
 
848
      delete message;
 
849
   }
 
850
 
 
851
}
 
852
 
 
853
void
 
854
TransactionState::startServerNonInviteTimerTrying(SipMessage& sip, const Data& tid)
 
855
{
 
856
   unsigned int duration = 3500;
 
857
   if(Timer::T1 != 500) // optimzed for T1 == 500
 
858
   {
 
859
      // Iteratively calculate how much time before TimerE reaches T2 (RFC4320) - could be improved
 
860
      duration = Timer::T1;
 
861
      while(duration*2<Timer::T2) duration = duration * 2;
 
862
   }
 
863
   resetNextTransmission(make100(&sip));  // Store for use when timer expires
 
864
   mController.mTimers.add(Timer::TimerTrying, tid, duration );  // Start trying timer so that we can send 100 to NITs as recommened in RFC4320
 
865
}
 
866
 
 
867
void
 
868
TransactionState::processStateless(TransactionMessage* message)
 
869
{
 
870
   // for ACK messages from the TU, there is no transaction, send it directly
 
871
   // to the wire // rfc3261 17.1 Client Transaction
 
872
   SipMessage* sip = dynamic_cast<SipMessage*>(message);
 
873
   StackLog (<< "TransactionState::processStateless: " << message->brief());
 
874
 
 
875
   // !jf! There is a leak for Stateless transactions associated with ACK to 200
 
876
   if (isFromTU(message))
 
877
   {
 
878
      resetNextTransmission(sip);
 
879
      sendCurrentToWire();
 
880
   }
 
881
   else if(sip && isFromWire(sip))
 
882
   {
 
883
      InfoLog (<< "Received message from wire on a stateless transaction");
 
884
      StackLog (<< *sip);
 
885
      //assert(0);
 
886
      sendToTU(sip);
 
887
   }
 
888
   else if (isTransportError(message))
 
889
   {
 
890
      processTransportFailure(message);
 
891
      
 
892
      delete message;
 
893
      delete this;
 
894
   }
 
895
   else if (isTimer(message))
 
896
   {
 
897
      TimerMessage* timer = dynamic_cast<TimerMessage*>(message);
 
898
      if (timer->getType() == Timer::TimerStateless)
 
899
      {
 
900
         delete message;
 
901
         delete this;
 
902
      }
 
903
      else
 
904
      {
 
905
         delete timer;
 
906
         assert(0);
 
907
      }
 
908
   }
 
909
   else if(dynamic_cast<DnsResultMessage*>(message))
 
910
   {
 
911
      handleSync(mDnsResult);
 
912
      delete message;
 
913
   }
 
914
   else if (isAbandonServerTransaction(message))
 
915
   {
 
916
      // ?
 
917
      delete message;
 
918
   }
 
919
   else
 
920
   {
 
921
      delete message;
 
922
      assert(0);
 
923
   }
 
924
}
 
925
 
 
926
void 
 
927
TransactionState::saveOriginalContactAndVia(const SipMessage& sip)
 
928
{
 
929
   if(sip.exists(h_Contacts) && sip.const_header(h_Contacts).size() == 1 &&
 
930
      sip.const_header(h_Contacts).front().isWellFormed())
 
931
   {
 
932
      mOriginalContact = std::auto_ptr<NameAddr>(new NameAddr(sip.header(h_Contacts).front()));
 
933
   }
 
934
   mOriginalVia = std::auto_ptr<Via>(new Via(sip.header(h_Vias).front()));
 
935
}
 
936
 
 
937
void TransactionState::restoreOriginalContactAndVia()
 
938
{
 
939
   if (mOriginalContact.get())
 
940
   {
 
941
      mNextTransmission->header(h_Contacts).front() = *mOriginalContact;
 
942
   }                  
 
943
   if (mOriginalVia.get())
 
944
   {
 
945
      mOriginalVia->param(p_branch).incrementTransportSequence();
 
946
      mNextTransmission->header(h_Vias).front() = *mOriginalVia;
 
947
   }
 
948
}
 
949
 
 
950
void
 
951
TransactionState::processClientNonInvite(TransactionMessage* msg)
 
952
 
953
   StackLog (<< "TransactionState::processClientNonInvite: " << msg->brief());
 
954
 
 
955
   if (isRequest(msg) && isFromTU(msg))
 
956
   {
 
957
      //StackLog (<< "received new non-invite request");
 
958
      SipMessage* sip = dynamic_cast<SipMessage*>(msg);
 
959
      resetNextTransmission(sip);
 
960
      saveOriginalContactAndVia(*sip);
 
961
      mController.mTimers.add(Timer::TimerF, mId, Timer::TF);
 
962
      sendCurrentToWire();
 
963
   }
 
964
   else if (isResponse(msg) && isFromWire(msg)) // from the wire
 
965
   {
 
966
      //StackLog (<< "received response from wire");
 
967
 
 
968
      SipMessage* sip = dynamic_cast<SipMessage*>(msg);
 
969
      int code = sip->const_header(h_StatusLine).responseCode();
 
970
      if (code >= 100 && code < 200) // 1XX
 
971
      {
 
972
         if (mState == Trying || mState == Proceeding)
 
973
         {
 
974
            //?slg? if we set the timer in Proceeding, then every 1xx response will cause another TimerE2 to be set and many retransmissions will occur - which is not correct
 
975
            // Should we restart the E2 timer though?  If so, we need to use somekind of timer sequence number so that previous E2 timers get discarded.
 
976
            if (!mIsReliable && mState == Trying)
 
977
            {
 
978
               mController.mTimers.add(Timer::TimerE2, mId, Timer::T2 );
 
979
            }
 
980
            mState = Proceeding;
 
981
            sendToTU(msg); // don't delete            
 
982
         }
 
983
         else
 
984
         {
 
985
            // ignore
 
986
            delete msg;
 
987
         }
 
988
      }
 
989
      else if (code >= 200)
 
990
      {
 
991
         // don't notify the TU of retransmissions
 
992
         if (mState == Trying || mState == Proceeding)
 
993
         {
 
994
            sendToTU(msg); // don't delete
 
995
         }
 
996
         else if (mState == Completed)
 
997
         {
 
998
            delete msg;
 
999
         }
 
1000
         else
 
1001
         {
 
1002
            assert(0);
 
1003
            delete sip;
 
1004
         }
 
1005
         
 
1006
         if (mIsReliable)
 
1007
         {
 
1008
            terminateClientTransaction(mId);
 
1009
            delete this;
 
1010
         }
 
1011
         else if (mState != Completed) // prevent TimerK reproduced
 
1012
         {
 
1013
            mState = Completed;
 
1014
            mController.mTimers.add(Timer::TimerK, mId, Timer::T4 );
 
1015
            // !bwc! Got final response in NIT. We don't need to do anything
 
1016
            // except quietly absorb retransmissions. Dump all state.
 
1017
            if(mDnsResult)
 
1018
            {
 
1019
               mDnsResult->destroy();
 
1020
               mDnsResult=0;
 
1021
               mWaitingForDnsResult=false;
 
1022
            }
 
1023
            resetNextTransmission(0);
 
1024
         }
 
1025
      }
 
1026
      else
 
1027
      {
 
1028
         assert(0);
 
1029
         delete sip;
 
1030
      }
 
1031
   }
 
1032
   else if (isTimer(msg))
 
1033
   {
 
1034
      //StackLog (<< "received timer in client non-invite transaction");
 
1035
 
 
1036
      TimerMessage* timer = dynamic_cast<TimerMessage*>(msg);
 
1037
      switch (timer->getType())
 
1038
      {
 
1039
         case Timer::TimerE1:
 
1040
            if (mState == Trying)
 
1041
            {
 
1042
               unsigned long d = timer->getDuration();
 
1043
               if (d < Timer::T2) d *= 2;
 
1044
               mController.mTimers.add(Timer::TimerE1, mId, d);
 
1045
               StackLog (<< "Transmitting current message");
 
1046
               sendCurrentToWire();
 
1047
               delete timer;
 
1048
            }
 
1049
            else
 
1050
            {
 
1051
               // ignore
 
1052
               delete msg;
 
1053
            }
 
1054
            break;
 
1055
 
 
1056
         case Timer::TimerE2:
 
1057
            if (mState == Proceeding)
 
1058
            {
 
1059
               mController.mTimers.add(Timer::TimerE2, mId, Timer::T2);
 
1060
               StackLog (<< "Transmitting current message");
 
1061
               sendCurrentToWire();
 
1062
               delete timer;
 
1063
            }
 
1064
            else 
 
1065
            {
 
1066
               // ignore
 
1067
               delete msg;
 
1068
            }
 
1069
            break;
 
1070
 
 
1071
         case Timer::TimerF:
 
1072
            if (mState == Trying || mState == Proceeding)
 
1073
            {
 
1074
               // !bwc! We hold onto this until we get a response from the wire
 
1075
               // in client transactions, for this contingency.
 
1076
               assert(mNextTransmission);
 
1077
               if(mWaitingForDnsResult)
 
1078
               {
 
1079
                  WarningLog(<< "Transaction timed out while waiting for DNS "
 
1080
                              "result uri=" << 
 
1081
                              mNextTransmission->const_header(h_RequestLine).uri());
 
1082
                  sendToTU(Helper::makeResponse(*mNextTransmission, 503, "DNS Timeout"));
 
1083
               }
 
1084
               else
 
1085
               {
 
1086
                  sendToTU(Helper::makeResponse(*mNextTransmission, 408));
 
1087
               }
 
1088
               terminateClientTransaction(mId);
 
1089
               delete this;
 
1090
            }
 
1091
            
 
1092
            delete msg;
 
1093
            break;
 
1094
 
 
1095
         case Timer::TimerK:
 
1096
            terminateClientTransaction(mId);
 
1097
            delete msg;
 
1098
            delete this;
 
1099
            break;
 
1100
 
 
1101
         default:
 
1102
            //InfoLog (<< "Ignoring timer: " << *msg);
 
1103
            delete msg;
 
1104
            break;
 
1105
      }
 
1106
   }
 
1107
   else if (isTransportError(msg))
 
1108
   {
 
1109
      processTransportFailure(msg);
 
1110
      delete msg;
 
1111
   }
 
1112
   else if(dynamic_cast<DnsResultMessage*>(msg))
 
1113
   {
 
1114
      handleSync(mDnsResult);
 
1115
      delete msg;
 
1116
   }
 
1117
   else if (isAbandonServerTransaction(msg))
 
1118
   {
 
1119
      // ?
 
1120
      delete msg;
 
1121
   }
 
1122
   else
 
1123
   {
 
1124
      //StackLog (<< "TransactionState::processClientNonInvite: message unhandled");
 
1125
      delete msg;
 
1126
   }
 
1127
}
 
1128
 
 
1129
void
 
1130
TransactionState::processClientInvite(TransactionMessage* msg)
 
1131
{
 
1132
   StackLog(<< "TransactionState::processClientInvite: " << msg->brief() << " " << *this);
 
1133
   if (isRequest(msg) && isFromTU(msg))
 
1134
   {
 
1135
      SipMessage* sip = dynamic_cast<SipMessage*>(msg);
 
1136
      switch (sip->method())
 
1137
      {
 
1138
         // Received INVITE request from TU="Transaction User", Start Timer B which controls
 
1139
         // transaction timeouts. 
 
1140
         case INVITE:
 
1141
            if(mState==Calling && !mNextTransmission && mMsgToRetransmit.empty())
 
1142
            {
 
1143
               resetNextTransmission(sip);
 
1144
               saveOriginalContactAndVia(*sip);
 
1145
               mController.mTimers.add(Timer::TimerB, mId, Timer::TB );
 
1146
               sendCurrentToWire();
 
1147
            }
 
1148
            else
 
1149
            {
 
1150
               WarningLog(<< "TU sent us a duplicate INVITE: fix this!");
 
1151
               delete sip;
 
1152
            }
 
1153
            break;
 
1154
            
 
1155
         case CANCEL:
 
1156
            assert(0);
 
1157
            delete msg;
 
1158
            break;
 
1159
 
 
1160
         default:
 
1161
            WarningLog(<< "TU sent us an erroneous request inside a Client"
 
1162
                        " INVITE transaction: fix this!");
 
1163
            delete msg;
 
1164
            break;
 
1165
      }
 
1166
   }
 
1167
   else if (isResponse(msg) && isFromWire(msg))
 
1168
   {
 
1169
      SipMessage* sip = dynamic_cast<SipMessage*>(msg);
 
1170
      int code = sip->const_header(h_StatusLine).responseCode();
 
1171
      switch (sip->method())
 
1172
      {
 
1173
         case INVITE:
 
1174
            /* If the client transaction receives a provisional response while in
 
1175
               the "Calling" state, it transitions to the "Proceeding" state. In the
 
1176
               "Proceeding" state, the client transaction SHOULD NOT retransmit the
 
1177
               request any longer (this will be Handled in  "else if (isTimer(msg))")
 
1178
               The Retransmissions will be stopped, Not by Cancelling Timers but
 
1179
               by Ignoring the fired Timers depending upon the State which stack is in.   
 
1180
            */
 
1181
            if (code >= 100 && code < 200) // 1XX
 
1182
            {
 
1183
               if (mState == Calling || mState == Proceeding)
 
1184
               {
 
1185
                  mState = Proceeding;
 
1186
                  if(mIsAbandoned)
 
1187
                  {
 
1188
                     SipMessage* cancel = Helper::makeCancel(*mNextTransmission);
 
1189
                     // Iterate through message decorators on the INVITE and see if any need to be copied to the CANCEL
 
1190
                     mNextTransmission->copyOutboundDecoratorsToStackCancel(*cancel);
 
1191
                     handleInternalCancel(cancel, *this);
 
1192
                     mIsAbandoned=false;
 
1193
                  }
 
1194
                  // !bwc! We have gotten a response. We don't need to
 
1195
                  // retransmit the original INVITE anymore (so we clear mMsgToRetransmit), 
 
1196
                  // but we do need to retain the full original INVITE until we get a final 
 
1197
                  // response, in case we need to forge an ACK.
 
1198
                  mMsgToRetransmit.clear();
 
1199
                  sendToTU(sip); // don't delete msg
 
1200
               }
 
1201
               else
 
1202
               {
 
1203
                  delete msg;
 
1204
               }
 
1205
            }
 
1206
 
 
1207
            /* When in either the "Calling" or "Proceeding" states, reception of a
 
1208
               2xx response MUST cause the client transaction to enter the
 
1209
               "Terminated" state, and the response MUST be passed up to the TU 
 
1210
               State Machine is changed to Stale since, we wanted to ensure that 
 
1211
               all 2xx gets to TU
 
1212
            */
 
1213
            else if (code >= 200 && code < 300)
 
1214
            {
 
1215
               mIsAbandoned=false;
 
1216
               sendToTU(sip); // don't delete msg
 
1217
               //terminateClientTransaction(mId);
 
1218
               mMachine = ClientStale;
 
1219
               // !bwc! We have a final response. We don't need either of
 
1220
               // mMsgToRetransmit or mNextTransmission. We ignore further
 
1221
               // traffic.
 
1222
               resetNextTransmission(0);
 
1223
               if(mDnsResult)
 
1224
               {
 
1225
                  mDnsResult->destroy();
 
1226
                  mDnsResult=0;
 
1227
                  mWaitingForDnsResult=false;
 
1228
               }
 
1229
               StackLog (<< "Received 2xx on client invite transaction");
 
1230
               StackLog (<< *this);
 
1231
               mController.mTimers.add(Timer::TimerStaleClient, mId, Timer::TS );
 
1232
            }
 
1233
            else if (code >= 300)
 
1234
            {
 
1235
               mIsAbandoned=false;
 
1236
               // When in either the "Calling" or "Proceeding" states, reception of a
 
1237
               // response with status code from 300-699 MUST cause the client
 
1238
               // transaction to transition to "Completed".
 
1239
               if (mIsReliable)
 
1240
               {
 
1241
                  // Stack MUST pass the received response up to the TU, and the client
 
1242
                  // transaction MUST generate an ACK request, even if the transport is
 
1243
                  // reliable
 
1244
                  SipMessage* ack = Helper::makeFailureAck(*mNextTransmission, *sip);
 
1245
                  mNextTransmission->copyOutboundDecoratorsToStackFailureAck(*ack);
 
1246
                  resetNextTransmission(ack);
 
1247
                  
 
1248
                  // want to use the same transport as was selected for Invite
 
1249
                  assert(mTarget.getType() != UNKNOWN_TRANSPORT);
 
1250
                  sendCurrentToWire();
 
1251
                  sendToTU(sip); // don't delete msg
 
1252
                  terminateClientTransaction(mId);
 
1253
                  
 
1254
                  // !bwc! We only do this because we are assured the ACK
 
1255
                  // will make it to the other end; if we are using an 
 
1256
                  // unreliable transport, we need to stick around to absorb
 
1257
                  // retransmissions of the response.
 
1258
                  delete this;
 
1259
               }
 
1260
               else
 
1261
               {
 
1262
                  if (mState == Calling || mState == Proceeding)
 
1263
                  {
 
1264
                     // MUST pass the received response up to the TU, and the client
 
1265
                     // transaction MUST generate an ACK request, even if the transport is
 
1266
                     // reliable, if transport is Unreliable then Fire the Timer D which 
 
1267
                     // take care of re-Transmission of ACK 
 
1268
                     mState = Completed;
 
1269
                     mController.mTimers.add(Timer::TimerD, mId, Timer::TD );
 
1270
                     SipMessage* ack = Helper::makeFailureAck(*mNextTransmission, *sip);
 
1271
                     mNextTransmission->copyOutboundDecoratorsToStackFailureAck(*ack);
 
1272
                     resetNextTransmission(ack);
 
1273
                     sendCurrentToWire();
 
1274
                     if(mDnsResult)
 
1275
                     {
 
1276
                        mDnsResult->destroy();
 
1277
                        mDnsResult=0;
 
1278
                        mWaitingForDnsResult=false;
 
1279
                     }
 
1280
                     sendToTU(sip); // don't delete msg
 
1281
                  }
 
1282
                  else if (mState == Completed)
 
1283
                  {
 
1284
                     // Any retransmissions of the final response that
 
1285
                     // are received while in the "Completed" state MUST
 
1286
                     // cause the ACK to be re-passed to the transport
 
1287
                     // layer for retransmission.
 
1288
                     sendCurrentToWire();
 
1289
                     delete sip;
 
1290
                  }
 
1291
                  else
 
1292
                  {
 
1293
                     /* This should never Happen if it happens we should have a plan
 
1294
                        what to do here?? for now assert will work
 
1295
                     */
 
1296
                     CritLog(  << "State invalid");
 
1297
                     // !ah! syslog
 
1298
                     assert(0);
 
1299
                     delete sip;
 
1300
                  }
 
1301
               }
 
1302
            }
 
1303
            else
 
1304
            {
 
1305
               delete sip;
 
1306
               assert(0);
 
1307
            }
 
1308
            break;
 
1309
            
 
1310
         case CANCEL:
 
1311
            assert(0);
 
1312
            delete sip;
 
1313
            break;
 
1314
 
 
1315
         default:
 
1316
            delete msg;
 
1317
            break;
 
1318
      }
 
1319
   }
 
1320
   else if (isTimer(msg))
 
1321
   {
 
1322
      /* Handle Transaction Timers , Retransmission Timers which were set and Handle
 
1323
         Cancellation of Timers for Re-transmissions here */
 
1324
 
 
1325
      TimerMessage* timer = dynamic_cast<TimerMessage*>(msg);
 
1326
      StackLog (<< "timer fired: " << *timer);
 
1327
      
 
1328
      switch (timer->getType())
 
1329
      {
 
1330
         case Timer::TimerA:
 
1331
            if (mState == Calling && !mIsAbandoned)
 
1332
            {
 
1333
               unsigned long d = timer->getDuration()*2;
 
1334
               // TimerA is supposed to double with each retransmit RFC3261 17.1.1          
 
1335
 
 
1336
               mController.mTimers.add(Timer::TimerA, mId, d);
 
1337
               DebugLog (<< "Retransmitting INVITE ");
 
1338
               sendCurrentToWire();
 
1339
            }
 
1340
            delete msg;
 
1341
            break;
 
1342
 
 
1343
         case Timer::TimerB:
 
1344
            if (mState == Calling)
 
1345
            {
 
1346
               assert(mNextTransmission && mNextTransmission->isRequest() &&
 
1347
                        mNextTransmission->method()==INVITE);
 
1348
               if(mWaitingForDnsResult)
 
1349
               {
 
1350
                  WarningLog(<< "Transaction timed out while waiting for DNS "
 
1351
                              "result uri=" << 
 
1352
                              mNextTransmission->const_header(h_RequestLine).uri());
 
1353
                  sendToTU(Helper::makeResponse(*mNextTransmission, 503, "DNS Timeout"));
 
1354
               }
 
1355
               else
 
1356
               {
 
1357
                  sendToTU(Helper::makeResponse(*mNextTransmission, 408));
 
1358
               }
 
1359
               terminateClientTransaction(mId);
 
1360
               delete this;
 
1361
            }
 
1362
            delete msg;
 
1363
            break;
 
1364
 
 
1365
         case Timer::TimerD:
 
1366
            terminateClientTransaction(mId);
 
1367
            delete msg;
 
1368
            delete this;
 
1369
            break;
 
1370
 
 
1371
         case Timer::TimerCleanUp:
 
1372
            // !ah! Cancelled Invite Cleanup Timer fired.
 
1373
            StackLog (<< "Timer::TimerCleanUp: " << *this << std::endl << *mNextTransmission);
 
1374
            if (mState == Proceeding)
 
1375
            {
 
1376
               assert(mNextTransmission && mNextTransmission->isRequest() && 
 
1377
                        mNextTransmission->method() == INVITE);
 
1378
               InfoLog(<<"Making 408 for canceled invite that received no response: "<< mNextTransmission->brief());
 
1379
               if(mWaitingForDnsResult)
 
1380
               {
 
1381
                  WarningLog(<< "Transaction timed out while waiting for DNS "
 
1382
                              "result uri=" << 
 
1383
                              mNextTransmission->const_header(h_RequestLine).uri());
 
1384
                  sendToTU(Helper::makeResponse(*mNextTransmission, 503, "DNS Timeout"));
 
1385
               }
 
1386
               else
 
1387
               {
 
1388
                  sendToTU(Helper::makeResponse(*mNextTransmission, 408));
 
1389
               }
 
1390
               terminateClientTransaction(msg->getTransactionId());
 
1391
               delete this;
 
1392
            }
 
1393
            delete msg;
 
1394
            break;
 
1395
 
 
1396
         default:
 
1397
            delete msg;
 
1398
            break;
 
1399
      }
 
1400
   }
 
1401
   else if (isTransportError(msg))
 
1402
   {
 
1403
      processTransportFailure(msg);
 
1404
      delete msg;
 
1405
   }
 
1406
   else if (isCancelClientTransaction(msg))
 
1407
   {
 
1408
      // TU wants to CANCEL this transaction. See if we can...
 
1409
      if(mState==Proceeding)
 
1410
      {
 
1411
         // We can send the CANCEL now.
 
1412
         SipMessage* cancel=Helper::makeCancel(*mNextTransmission);
 
1413
         mNextTransmission->copyOutboundDecoratorsToStackCancel(*cancel);
 
1414
         TransactionState::handleInternalCancel(cancel, *this);
 
1415
      }
 
1416
      else if(mState==Calling)
 
1417
      {
 
1418
         // We can't send the CANCEL yet, remember to.
 
1419
         mIsAbandoned = true;
 
1420
      }
 
1421
      delete msg;
 
1422
   }
 
1423
   else if(dynamic_cast<DnsResultMessage*>(msg))
 
1424
   {
 
1425
      handleSync(mDnsResult);
 
1426
      delete msg;
 
1427
   }
 
1428
   else
 
1429
   {
 
1430
      //StackLog ( << "TransactionState::processClientInvite: message unhandled");
 
1431
      delete msg;
 
1432
   }
 
1433
}
 
1434
 
 
1435
void
 
1436
TransactionState::processServerNonInvite(TransactionMessage* msg)
 
1437
{
 
1438
   StackLog (<< "TransactionState::processServerNonInvite: " << msg->brief());
 
1439
 
 
1440
   if (isRequest(msg) && !isInvite(msg) && isFromWire(msg)) // retransmission from the wire
 
1441
   {
 
1442
      if (mState == Trying)
 
1443
      {
 
1444
         // ignore
 
1445
         delete msg;
 
1446
      }
 
1447
      else if (mState == Proceeding || mState == Completed)
 
1448
      {
 
1449
         if(mIsAbandoned)
 
1450
         {
 
1451
            assert(mState == Completed);
 
1452
            mIsAbandoned=false;
 
1453
            // put a 500 in mNextTransmission
 
1454
            SipMessage* req = dynamic_cast<SipMessage*>(msg);
 
1455
            resetNextTransmission(Helper::makeResponse(*req, 500));
 
1456
            sendCurrentToWire();
 
1457
         }
 
1458
         else
 
1459
         {
 
1460
            sendCurrentToWire();
 
1461
         }
 
1462
         delete msg;
 
1463
      }
 
1464
      else
 
1465
      {
 
1466
         CritLog (<< "Fatal error in TransactionState::processServerNonInvite " 
 
1467
                  << msg->brief()
 
1468
                  << " state=" << *this);
 
1469
         assert(0);
 
1470
         delete msg;
 
1471
         return;
 
1472
      }
 
1473
   }
 
1474
   else if (isResponse(msg) && isFromTU(msg))
 
1475
   {
 
1476
      SipMessage* sip = dynamic_cast<SipMessage*>(msg);
 
1477
      int code = sip->const_header(h_StatusLine).responseCode();
 
1478
      if (code >= 100 && code < 200) // 1XX
 
1479
      {
 
1480
         if (mState == Trying || mState == Proceeding)
 
1481
         {
 
1482
            resetNextTransmission(sip);
 
1483
            mState = Proceeding;
 
1484
            sendCurrentToWire(); // don't delete msg
 
1485
         }
 
1486
         else
 
1487
         {
 
1488
            // ignore
 
1489
            delete msg;
 
1490
         }
 
1491
      }
 
1492
      else if (code >= 200 && code <= 699)
 
1493
      {
 
1494
         if (mIsReliable)
 
1495
         {
 
1496
            resetNextTransmission(sip);
 
1497
            sendCurrentToWire();
 
1498
            terminateServerTransaction(mId);
 
1499
            
 
1500
            // !bwc! We can only do this because we are in a reliable
 
1501
            // transport, and do not need to hang around to soak up 
 
1502
            // retransmissions.
 
1503
            delete this;
 
1504
         }
 
1505
         else
 
1506
         {
 
1507
            if (mState == Trying || mState == Proceeding)
 
1508
            {
 
1509
               mState = Completed;
 
1510
               mController.mTimers.add(Timer::TimerJ, mId, 64*Timer::T1 );
 
1511
               resetNextTransmission(sip);
 
1512
               sendCurrentToWire();
 
1513
            }
 
1514
            else if (mState == Completed)
 
1515
            {
 
1516
               // ignore
 
1517
               delete sip;
 
1518
            }
 
1519
            else
 
1520
            {
 
1521
               CritLog (<< "Fatal error in TransactionState::processServerNonInvite " 
 
1522
                        << msg->brief()
 
1523
                        << " state=" << *this);
 
1524
               assert(0);
 
1525
               delete sip;
 
1526
               return;
 
1527
            }
 
1528
         }
 
1529
      }
 
1530
      else
 
1531
      {
 
1532
         // ignore
 
1533
         delete msg;
 
1534
      }
 
1535
   }
 
1536
   else if (isTimer(msg))
 
1537
   {
 
1538
      TimerMessage* timer = dynamic_cast<TimerMessage*>(msg);
 
1539
      assert(timer);
 
1540
      switch (timer->getType())
 
1541
      {
 
1542
         case Timer::TimerJ:
 
1543
            if (mState == Completed)
 
1544
            {
 
1545
               terminateServerTransaction(mId);
 
1546
               delete this;
 
1547
            }
 
1548
            delete msg;
 
1549
            break;
 
1550
 
 
1551
         case Timer::TimerTrying:
 
1552
            if (mState == Trying)
 
1553
            {
 
1554
               // Timer E has reached T2 - send a 100 as recommended by RFC4320 NIT-Problem-Actions
 
1555
               sendCurrentToWire();
 
1556
               mState = Proceeding;
 
1557
            }
 
1558
            delete msg;
 
1559
            break;
 
1560
 
 
1561
         default:
 
1562
            delete msg;
 
1563
            break;
 
1564
      }
 
1565
   }
 
1566
   else if (isTransportError(msg))
 
1567
   {
 
1568
      processTransportFailure(msg);
 
1569
      delete msg;
 
1570
   }
 
1571
   else if (isAbandonServerTransaction(msg))
 
1572
   {
 
1573
      if(mState==Trying || mState==Proceeding)
 
1574
      {
 
1575
         mIsAbandoned = true;
 
1576
 
 
1577
         // !bwc! We could check to see if we have a 100 lying around, and 
 
1578
         // convert it into a 500 for immediate transmission, but it is not 
 
1579
         // clear that this is a good idea, especially if the TU has abandoned 
 
1580
         // this transaction after the remote endpoint has stopped 
 
1581
         // retransmitting. Maybe we could use a time-stamp to help here? Would
 
1582
         // it be worth the extra memory footprint?
 
1583
 
 
1584
         if (mIsReliable)
 
1585
         {
 
1586
            // If we haven't sent a 500 yet, we never will (no retransmissions 
 
1587
            // to make the response with).
 
1588
            terminateServerTransaction(mId);
 
1589
            delete this;
 
1590
         }
 
1591
         else
 
1592
         {
 
1593
            // If we haven't sent a 500 yet, we'll do so when the next
 
1594
            // retransmission comes in. In the meantime, set up timers for
 
1595
            // transaction termination.
 
1596
            mState = Completed;
 
1597
            mController.mTimers.add(Timer::TimerJ, mId, 64*Timer::T1 );
 
1598
         }
 
1599
      }
 
1600
      delete msg;
 
1601
   }
 
1602
   else if(dynamic_cast<DnsResultMessage*>(msg))
 
1603
   {
 
1604
      handleSync(mDnsResult);
 
1605
      delete msg;
 
1606
   }
 
1607
   else
 
1608
   {
 
1609
      //StackLog (<< "TransactionState::processServerNonInvite: message unhandled");
 
1610
      delete msg;
 
1611
   }
 
1612
}
 
1613
 
 
1614
 
 
1615
void
 
1616
TransactionState::processServerInvite(TransactionMessage* msg)
 
1617
{
 
1618
   StackLog (<< "TransactionState::processServerInvite: " << msg->brief());
 
1619
   if (isRequest(msg) && isFromWire(msg))
 
1620
   {
 
1621
      SipMessage* sip = dynamic_cast<SipMessage*>(msg);
 
1622
      switch (sip->method())
 
1623
      {
 
1624
         case INVITE:
 
1625
            // note: handling of initial INVITE message is done in TransactionState:process
 
1626
            if(mIsAbandoned)
 
1627
            {
 
1628
               mIsAbandoned=false;
 
1629
               mAckIsValid=true;
 
1630
               resetNextTransmission(Helper::makeResponse(*sip, 500));
 
1631
               mState = Completed;
 
1632
               mController.mTimers.add(Timer::TimerH, mId, Timer::TH );
 
1633
               if (!mIsReliable)
 
1634
               {
 
1635
                  mController.mTimers.add(Timer::TimerG, mId, Timer::T1 );
 
1636
               }
 
1637
               sendCurrentToWire();
 
1638
               delete msg;
 
1639
               return;
 
1640
            }
 
1641
 
 
1642
            if (mState == Proceeding || mState == Completed)
 
1643
            {
 
1644
               /*
 
1645
                 The server transaction has already been constructed so this
 
1646
                 message is a retransmission.  The server transaction must
 
1647
                 respond with a 100 Trying _or_ the last provisional response
 
1648
                 passed from the TU for this transaction.
 
1649
               */
 
1650
               //StackLog (<< "Received invite from wire - forwarding to TU state=" << mState);
 
1651
               
 
1652
               // !bwc! If we have nothing to respond with, make something.
 
1653
               if (mMsgToRetransmit.empty() && !mNextTransmission)
 
1654
               {
 
1655
                  resetNextTransmission(make100(sip));
 
1656
               }
 
1657
               delete sip;
 
1658
               sendCurrentToWire();
 
1659
            }
 
1660
            else
 
1661
            {
 
1662
               //StackLog (<< "Received invite from wire - ignoring state=" << mState);
 
1663
               delete msg;
 
1664
            }
 
1665
            break;
 
1666
            
 
1667
         case ACK:
 
1668
            /*
 
1669
              If an ACK is received while the server transaction is in the
 
1670
              "Completed" state, the server transaction MUST transition to the
 
1671
              "Confirmed" state.
 
1672
            */
 
1673
            if (mState == Completed)
 
1674
            {
 
1675
               if (mIsReliable)
 
1676
               {
 
1677
                  //StackLog (<< "Received ACK in Completed (reliable) - delete transaction");
 
1678
                  terminateServerTransaction(mId);
 
1679
                  delete this; 
 
1680
                  delete msg;
 
1681
               }
 
1682
               else
 
1683
               {
 
1684
                  //StackLog (<< "Received ACK in Completed (unreliable) - confirmed, start Timer I");
 
1685
                  mState = Confirmed;
 
1686
                  mController.mTimers.add(Timer::TimerI, mId, Timer::T4 );
 
1687
                  // !bwc! Got an ACK/failure; we can stop retransmitting
 
1688
                  // our failure response now.
 
1689
                  resetNextTransmission(0);
 
1690
                  delete sip;
 
1691
               }
 
1692
            }
 
1693
            else
 
1694
            {
 
1695
               //StackLog (<< "Ignore ACK not in Completed state");
 
1696
               delete msg;
 
1697
            }
 
1698
            break;
 
1699
 
 
1700
         case CANCEL:
 
1701
            assert(0);
 
1702
            delete sip;
 
1703
            break;
 
1704
 
 
1705
         default:
 
1706
            //StackLog (<< "Received unexpected request. Ignoring message");
 
1707
            delete msg;
 
1708
            break;
 
1709
      }
 
1710
   }
 
1711
   else if (isResponse(msg, 100, 699) && isFromTU(msg))
 
1712
   {
 
1713
      SipMessage* sip = dynamic_cast<SipMessage*>(msg);
 
1714
      int code = sip->const_header(h_StatusLine).responseCode();
 
1715
      switch (sip->method())
 
1716
      {
 
1717
         case INVITE:
 
1718
            if (code == 100)
 
1719
            {
 
1720
               if (mState == Trying || mState == Proceeding)
 
1721
               {
 
1722
                  //StackLog (<< "Received 100 in Trying or Proceeding. Send over wire");
 
1723
                  resetNextTransmission(sip); // may be replacing the 100
 
1724
                  mState = Proceeding;
 
1725
                  sendCurrentToWire(); // don't delete msg
 
1726
               }
 
1727
               else
 
1728
               {
 
1729
                  //StackLog (<< "Ignoring 100 - not in Trying or Proceeding.");
 
1730
                  delete msg;
 
1731
               }
 
1732
            }
 
1733
            else if (code > 100 && code < 200)
 
1734
            {
 
1735
               if (mState == Trying || mState == Proceeding)
 
1736
               {
 
1737
                  //StackLog (<< "Received 1xx in Trying or Proceeding. Send over wire");
 
1738
                  resetNextTransmission(sip); // may be replacing the 100
 
1739
                  mState = Proceeding;
 
1740
                  sendCurrentToWire(); // don't delete msg
 
1741
               }
 
1742
               else
 
1743
               {
 
1744
                  //StackLog (<< "Received 100 when not in Trying State. Ignoring");
 
1745
                  delete msg;
 
1746
               }
 
1747
            }
 
1748
            else if (code >= 200 && code < 300)
 
1749
            {
 
1750
               if (mState == Trying || mState == Proceeding)
 
1751
               {
 
1752
                  StackLog (<< "Received 2xx when in Trying or Proceeding State of server invite transaction");
 
1753
                  StackLog (<< *this);
 
1754
                  resetNextTransmission(sip); // may be replacing the 100
 
1755
                  sendCurrentToWire();
 
1756
                  
 
1757
                  // Keep the StaleServer transaction around, so we can keep the
 
1758
                  // source Tuple that the request was received on. 
 
1759
                  //terminateServerTransaction(mId);
 
1760
                  mMachine = ServerStale;
 
1761
                  mController.mTimers.add(Timer::TimerStaleServer, mId, Timer::TS );
 
1762
               }
 
1763
               else
 
1764
               {
 
1765
                  //StackLog (<< "Received 2xx when not in Trying or Proceeding State. Ignoring");
 
1766
                  delete msg;
 
1767
               }
 
1768
            }
 
1769
            else if (code >= 300)
 
1770
            {
 
1771
               /*
 
1772
                 While in the "Proceeding" state, if the TU passes a response with
 
1773
                 status code from 300 to 699 to the server transaction, For unreliable 
 
1774
                 transports,timer G is set to fire in T1 seconds, and is not set to 
 
1775
                 fire for reliable transports.when the "Completed" state is entered, 
 
1776
                 timer H MUST be set to fire in 64*T1 seconds for all transports.  
 
1777
                 Timer H determines when the server transaction abandons retransmitting 
 
1778
                 the response
 
1779
               */
 
1780
 
 
1781
               if (mState == Trying || mState == Proceeding)
 
1782
               {
 
1783
                  mAckIsValid=true;
 
1784
                  StackLog (<< "Received failed response in Trying or Proceeding. Start Timer H, move to completed." << *this);
 
1785
                  resetNextTransmission(sip);
 
1786
                  mState = Completed;
 
1787
                  mController.mTimers.add(Timer::TimerH, mId, Timer::TH );
 
1788
                  if (!mIsReliable)
 
1789
                  {
 
1790
                     mController.mTimers.add(Timer::TimerG, mId, Timer::T1 );
 
1791
                  }
 
1792
                  sendCurrentToWire(); // don't delete msg
 
1793
               }
 
1794
               else
 
1795
               {
 
1796
                  //StackLog (<< "Received Final response when not in Trying or Proceeding State. Ignoring");
 
1797
                  delete msg;
 
1798
               }
 
1799
            }
 
1800
            else
 
1801
            {
 
1802
               //StackLog (<< "Received Invalid response line. Ignoring");
 
1803
               delete msg;
 
1804
            }
 
1805
            break;
 
1806
            
 
1807
         case CANCEL:
 
1808
            assert(0);
 
1809
            delete sip;
 
1810
            break;
 
1811
            
 
1812
         default:
 
1813
            //StackLog (<< "Received response to non invite or cancel. Ignoring");
 
1814
            delete msg;
 
1815
            break;
 
1816
      }
 
1817
   }
 
1818
   else if (isTimer(msg))
 
1819
   {
 
1820
      TimerMessage* timer = dynamic_cast<TimerMessage*>(msg);
 
1821
      switch (timer->getType())
 
1822
      {
 
1823
         case Timer::TimerG:
 
1824
            if (mState == Completed)
 
1825
            {
 
1826
               StackLog (<< "TimerG fired. retransmit, and re-add TimerG");
 
1827
               sendCurrentToWire();
 
1828
               mController.mTimers.add(Timer::TimerG, mId, resipMin(Timer::T2, timer->getDuration()*2) );  //  TimerG is supposed to double - up until a max of T2 RFC3261 17.2.1
 
1829
            }
 
1830
            break;
 
1831
 
 
1832
            /*
 
1833
              If timer H fires while in the "Completed" state, it implies that the
 
1834
              ACK was never received.  In this case, the server transaction MUST
 
1835
              transition to the "Terminated" state, and MUST indicate to the TU
 
1836
              that a transaction failure has occurred. WHY we need to inform TU
 
1837
              for Failure cases ACK ? do we really need to do this ???       
 
1838
 
 
1839
              !jf! this used to re-add TimerH if there was an associated CANCEL
 
1840
              transaction. Don't know why. 
 
1841
            */
 
1842
         case Timer::TimerH:
 
1843
         case Timer::TimerI:
 
1844
            if (timer->getType() == Timer::TimerH)
 
1845
            {
 
1846
               InfoLog (<< "No ACK was received on a server transaction (Timer H)");
 
1847
            }
 
1848
            terminateServerTransaction(mId);
 
1849
            delete this;
 
1850
            break;
 
1851
 
 
1852
         case Timer::TimerTrying:
 
1853
            if (mState == Trying)
 
1854
            {
 
1855
               //StackLog (<< "TimerTrying fired. Send a 100");
 
1856
               sendCurrentToWire(); // will get deleted when this is deleted
 
1857
               mState = Proceeding;
 
1858
            }
 
1859
            else
 
1860
            {
 
1861
               //StackLog (<< "TimerTrying fired. Not in Trying state. Ignoring");
 
1862
            }
 
1863
            break;
 
1864
            
 
1865
         default:
 
1866
            CritLog(<<"unexpected timer fired: " << timer->getType());
 
1867
            assert(0); // programming error if any other timer fires
 
1868
            break;
 
1869
      }
 
1870
      delete timer;
 
1871
   }
 
1872
   else if (isTransportError(msg))
 
1873
   {
 
1874
      processTransportFailure(msg);
 
1875
      delete msg;
 
1876
   }
 
1877
   else if (isAbandonServerTransaction(msg))
 
1878
   {
 
1879
      if((mState == Trying || mState == Proceeding) && !mIsAbandoned)
 
1880
      {
 
1881
         // We need to schedule teardown, and 500 the next retransmission.
 
1882
         if(mNextTransmission)
 
1883
         {
 
1884
            mMsgToRetransmit.clear();
 
1885
            // hey, we had a 1xx laying around! Turn it into a 500 and send.
 
1886
            assert(mNextTransmission->isResponse());
 
1887
            assert(mNextTransmission->const_header(h_StatusLine).statusCode()/100==1);
 
1888
            mNextTransmission->header(h_StatusLine).statusCode()=500;
 
1889
            mNextTransmission->header(h_StatusLine).reason()="Server Error";
 
1890
            sendCurrentToWire();
 
1891
            mAckIsValid=true;
 
1892
            StackLog (<< "Received failed response in Trying or Proceeding. Start Timer H, move to completed." << *this);
 
1893
            mState = Completed;
 
1894
            mController.mTimers.add(Timer::TimerH, mId, Timer::TH );
 
1895
            if (!mIsReliable)
 
1896
            {
 
1897
               mController.mTimers.add(Timer::TimerG, mId, Timer::T1 );
 
1898
            }
 
1899
         }
 
1900
         else
 
1901
         {
 
1902
            // !bwc! TODO try to convert mMsgToRetransmit if present.
 
1903
            if(mIsReliable)
 
1904
            {
 
1905
               // We will never see another retransmission of the INVITE. We
 
1906
               // need to bail.
 
1907
               terminateServerTransaction(mId);
 
1908
               delete this;
 
1909
            }
 
1910
            else
 
1911
            {
 
1912
               // We should see a retransmission of the INVITE shortly, or we
 
1913
               // will time out eventually. Be patient...
 
1914
               mIsAbandoned = true;
 
1915
            }
 
1916
         }
 
1917
      }
 
1918
      delete msg;
 
1919
   }
 
1920
   else if(dynamic_cast<DnsResultMessage*>(msg))
 
1921
   {
 
1922
      handleSync(mDnsResult);
 
1923
      delete msg;
 
1924
   }
 
1925
   else
 
1926
   {
 
1927
      //StackLog (<< "TransactionState::processServerInvite: message unhandled");
 
1928
      delete msg;
 
1929
   }
 
1930
}
 
1931
 
 
1932
 
 
1933
void
 
1934
TransactionState::processClientStale(TransactionMessage* msg)
 
1935
{
 
1936
   StackLog (<< "TransactionState::processClientStale: " << msg->brief());
 
1937
 
 
1938
   if (isTimer(msg))
 
1939
   {
 
1940
      TimerMessage* timer = dynamic_cast<TimerMessage*>(msg);
 
1941
      if (timer->getType() == Timer::TimerStaleClient)
 
1942
      {
 
1943
         terminateClientTransaction(mId);
 
1944
         delete this;
 
1945
         delete msg;
 
1946
      }
 
1947
      else
 
1948
      {
 
1949
         delete msg;
 
1950
      }
 
1951
   }
 
1952
   else if (isTransportError(msg))
 
1953
   {
 
1954
      WarningLog (<< "Got a transport error in Stale Client state");
 
1955
      StackLog (<< *this);
 
1956
      processTransportFailure(msg);
 
1957
      delete msg;
 
1958
   }
 
1959
   else if(isResponse(msg, 200, 299))
 
1960
   {
 
1961
      assert(isFromWire(msg));
 
1962
      sendToTU(msg);
 
1963
   }
 
1964
   else if(dynamic_cast<DnsResultMessage*>(msg))
 
1965
   {
 
1966
      handleSync(mDnsResult);
 
1967
      delete msg;
 
1968
   }
 
1969
   else if (isAbandonServerTransaction(msg))
 
1970
   {
 
1971
      // ?
 
1972
      delete msg;
 
1973
   }
 
1974
   else if (isCancelClientTransaction(msg))
 
1975
   {
 
1976
      // ?
 
1977
      delete msg;
 
1978
   }
 
1979
   else
 
1980
   {
 
1981
      // might have received some other response because a downstream UAS is
 
1982
      // misbehaving. For instance, sending a 487/INVITE after already
 
1983
      // sending a 200/INVITE. It could also be some other message type.
 
1984
      StackLog (<< "Discarding extra message: " << *msg);
 
1985
      delete msg;
 
1986
   }
 
1987
}
 
1988
 
 
1989
void
 
1990
TransactionState::processServerStale(TransactionMessage* msg)
 
1991
{
 
1992
   StackLog (<< "TransactionState::processServerStale: " << msg->brief());
 
1993
 
 
1994
   SipMessage* sip = dynamic_cast<SipMessage*>(msg);
 
1995
   if (isTimer(msg))
 
1996
   {
 
1997
      TimerMessage* timer = dynamic_cast<TimerMessage*>(msg);
 
1998
      if (timer->getType() == Timer::TimerStaleServer)
 
1999
      {
 
2000
         delete msg;
 
2001
         terminateServerTransaction(mId);
 
2002
         delete this;
 
2003
      }
 
2004
      else
 
2005
      {
 
2006
         delete msg;
 
2007
      }
 
2008
   }
 
2009
   else if (isTransportError(msg))
 
2010
   {
 
2011
      WarningLog (<< "Got a transport error in Stale Server state");
 
2012
      StackLog (<< *this);
 
2013
      processTransportFailure(msg);
 
2014
      delete msg;
 
2015
   }
 
2016
   else if (sip && isRequest(sip) && sip->method() == ACK)
 
2017
   {
 
2018
      // .bwc. We should never fall into this block. There is code in process
 
2019
      // that should prevent it.
 
2020
      assert(isFromWire(msg));
 
2021
      InfoLog (<< "Passing ACK directly to TU: " << sip->brief());
 
2022
      sendToTU(msg);
 
2023
   }
 
2024
   else if (sip && isRequest(sip) && sip->method() == INVITE)
 
2025
   {
 
2026
      // this can happen when an upstream UAC never received the 200 and
 
2027
      // retransmits the INVITE when using unreliable transport
 
2028
      // Drop the INVITE since the 200 will get retransmitted by the downstream UAS
 
2029
      StackLog (<< "Dropping retransmitted INVITE in stale server transaction" << sip->brief());
 
2030
      delete msg;
 
2031
   }
 
2032
   else if (isResponse(msg) && isFromTU(msg))
 
2033
   {
 
2034
      resetNextTransmission(sip);
 
2035
      sendCurrentToWire(); 
 
2036
   }
 
2037
   else if(dynamic_cast<DnsResultMessage*>(msg))
 
2038
   {
 
2039
      handleSync(mDnsResult);
 
2040
      delete msg;
 
2041
   }
 
2042
   else if (isAbandonServerTransaction(msg))
 
2043
   {
 
2044
      // ?
 
2045
      delete msg;
 
2046
   }
 
2047
   else
 
2048
   {
 
2049
      // .bwc. This can very easily be triggered by a stupid/malicious 
 
2050
      // endpoint. This is not an error in our code. Do not ErrLog this.
 
2051
      InfoLog(<<"ServerStale unexpected condition, dropping message.");
 
2052
      if (sip)
 
2053
      {
 
2054
         InfoLog(<<sip->brief());
 
2055
      }
 
2056
      delete msg;
 
2057
   }
 
2058
}
 
2059
 
 
2060
 
 
2061
void
 
2062
TransactionState::processNoDnsResults()
 
2063
{
 
2064
   if(!mNextTransmission || mNextTransmission->method()==ACK)
 
2065
   {
 
2066
      // This is probably an ACK; since we know we will never need to send a 
 
2067
      // response to an ACK, we delete mNextTransmission as soon as we 
 
2068
      // serialize it.
 
2069
      return;
 
2070
   }
 
2071
 
 
2072
   WarningCategory warning;
 
2073
   SipMessage* response = Helper::makeResponse(*mNextTransmission, 503);
 
2074
   warning.hostname() = mController.mHostname;
 
2075
   warning.code() = 399;
 
2076
   warning.text().reserve(100);
 
2077
 
 
2078
   if(mDnsResult)
 
2079
   {
 
2080
      InfoLog (<< "Ran out of dns entries for " << mDnsResult->target() << ". Send 503");
 
2081
      assert(mDnsResult->available() == DnsResult::Finished);
 
2082
      oDataStream warnText(warning.text());
 
2083
      warnText << "No other DNS entries to try ("
 
2084
               << mFailureReason << "," << mFailureSubCode << ")";
 
2085
   }
 
2086
   else
 
2087
   {
 
2088
      oDataStream warnText(warning.text());
 
2089
      warnText << "Transport failure ("
 
2090
               << mFailureReason << "," << mFailureSubCode << ")";
 
2091
   }
 
2092
 
 
2093
   switch(mFailureReason)
 
2094
   {
 
2095
      case TransportFailure::None:
 
2096
         response->header(h_StatusLine).reason() = "No DNS results";
 
2097
         break;
 
2098
 
 
2099
      case TransportFailure::Failure:
 
2100
      case TransportFailure::TransportNoSocket:
 
2101
      case TransportFailure::TransportBadConnect:
 
2102
      case TransportFailure::ConnectionUnknown:
 
2103
      case TransportFailure::ConnectionException:
 
2104
         response->header(h_StatusLine).reason() = "Transport failure: no transports left to try";
 
2105
         break;
 
2106
      case TransportFailure::NoTransport:
 
2107
         response->header(h_StatusLine).reason() = "No matching transport found";
 
2108
         break;
 
2109
      case TransportFailure::NoRoute:
 
2110
         response->header(h_StatusLine).reason() = "No route to host";
 
2111
         break;
 
2112
      case TransportFailure::CertNameMismatch:
 
2113
         response->header(h_StatusLine).reason() = "Certificate Name Mismatch";
 
2114
         break;
 
2115
      case TransportFailure::CertValidationFailure:
 
2116
         response->header(h_StatusLine).reason() = "Certificate Validation Failure";
 
2117
         break;
 
2118
      case TransportFailure::TransportNoExistConn:
 
2119
         if(InteropHelper::getOutboundVersion() >= 5)
 
2120
         {
 
2121
            response->header(h_StatusLine).statusCode() = 430;
 
2122
         }
 
2123
         else
 
2124
         {
 
2125
            response->header(h_StatusLine).statusCode() = 410;
 
2126
         }
 
2127
         response->header(h_StatusLine).reason() = "Flow failed";
 
2128
         warning.text() = "Flow no longer exists";
 
2129
         break;
 
2130
      case TransportFailure::TransportShutdown:
 
2131
         response->header(h_StatusLine).reason() = "Transport shutdown: no transports left to try";
 
2132
         break;
 
2133
   }
 
2134
 
 
2135
   response->header(h_Warnings).push_back(warning);
 
2136
 
 
2137
   sendToTU(response); 
 
2138
   terminateClientTransaction(mId);
 
2139
   if (mMachine != Stateless)
 
2140
   {
 
2141
      delete this;
 
2142
   }
 
2143
}
 
2144
 
 
2145
void
 
2146
TransactionState::processTransportFailure(TransactionMessage* msg)
 
2147
{
 
2148
   TransportFailure* failure = dynamic_cast<TransportFailure*>(msg);
 
2149
   assert(failure);
 
2150
   assert(mState!=Bogus);
 
2151
 
 
2152
   // Store failure reasons
 
2153
   if (failure->getFailureReason() > mFailureReason)
 
2154
   {
 
2155
      mFailureReason = failure->getFailureReason();
 
2156
      mFailureSubCode = failure->getFailureSubCode();
 
2157
   }
 
2158
 
 
2159
   if (mNextTransmission &&  // Note:  If we just transmitted an ACK then mNextTransmission is cleared, so this check is necessary
 
2160
       mNextTransmission->isRequest() && 
 
2161
       mNextTransmission->method() == CANCEL &&
 
2162
       mState != Completed && 
 
2163
       mState != Terminated)
 
2164
   {
 
2165
      WarningLog (<< "Failed to deliver a CANCEL request");
 
2166
      StackLog (<< *this);
 
2167
      assert(mMethod==CANCEL);
 
2168
 
 
2169
      // In the case of a client-initiated CANCEL, we don't want to
 
2170
      // try other transports in the case of transport error as the
 
2171
      // CANCEL MUST be sent to the same IP/PORT as the orig. INVITE.
 
2172
      //?dcm? insepct failure enum?
 
2173
      SipMessage* response = Helper::makeResponse(*mNextTransmission, 503);
 
2174
      WarningCategory warning;
 
2175
      warning.hostname() = mController.mHostname;
 
2176
      warning.code() = 399;
 
2177
      warning.text() = "Failed to deliver CANCEL using the same transport as the INVITE was used";
 
2178
      response->header(h_Warnings).push_back(warning);
 
2179
         
 
2180
      sendToTU(response);
 
2181
      return;
 
2182
   }
 
2183
 
 
2184
   if(!mDnsResult)
 
2185
   {
 
2186
      InfoLog(<< "Transport failure on send that did not use DNS.");
 
2187
      processNoDnsResults();
 
2188
   }
 
2189
   // else If we did DNS resolution, then check if we should try to failover to another DNS entry
 
2190
   else if(mDnsResult)
 
2191
   {
 
2192
      // .bwc. Greylist for 32s
 
2193
      // !bwc! TODO make this duration configurable.
 
2194
      mDnsResult->greylistLast(Timer::getTimeMs()+32000);
 
2195
 
 
2196
      // .bwc. We should only try multiple dns results if we are originating a
 
2197
      // request. Additionally, there are (potential) cases where it would not
 
2198
      // be appropriate to fail over even then.
 
2199
      bool shouldFailover=false;
 
2200
      if(mMachine==ClientNonInvite)
 
2201
      {
 
2202
         if(mState==Completed || mState==Terminated)
 
2203
         {
 
2204
            WarningLog(<<"Got a TransportFailure message in a " << mState <<
 
2205
                         " ClientNonInvite transaction. How did this happen? Since we have"
 
2206
                         " already completed the transaction, we shouldn't try"
 
2207
                         " additional DNS results.");
 
2208
         }
 
2209
         else
 
2210
         {
 
2211
            shouldFailover=true;
 
2212
         }
 
2213
      }
 
2214
      else if(mMachine==ClientInvite)
 
2215
      {
 
2216
         if(mState==Completed || mState==Terminated)
 
2217
         {
 
2218
            // .bwc. Perhaps the attempted transmission of the ACK failed here.
 
2219
            // (assuming this transaction got a failure response; not sure what
 
2220
            // might have happened if this is not the case)
 
2221
            // In any case, we should not try sending the INVITE anywhere else.
 
2222
            InfoLog(<<"Got a TransportFailure message in a " << mState <<
 
2223
                      " ClientInvite transaction. Since we have"
 
2224
                      " already completed the transaction, we shouldn't try"
 
2225
                      " additional DNS results.");
 
2226
         }
 
2227
         else
 
2228
         {
 
2229
            if(mState==Proceeding)
 
2230
            {
 
2231
               // .bwc. We need to revert our state back to Calling, since we are
 
2232
               // going to be sending the INVITE to a new endpoint entirely.
 
2233
 
 
2234
               // !bwc!
 
2235
               // An interesting consequence occurs if our failover ultimately
 
2236
               // sends to the same instance of a resip stack; we increment the 
 
2237
               // transport sequence in our branch parameter, but any resip-based
 
2238
               // stack will ignore this change, and process this "new" request as
 
2239
               // a retransmission! Furthermore, our state will be out of phase
 
2240
               // with the state at the remote endpoint, and if we have sent a
 
2241
               // PRACK, it will know (and stuff will break)!
 
2242
               // TODO What else needs to be done here to safely revert our state?
 
2243
               mState=Calling;
 
2244
            }
 
2245
            shouldFailover=true;
 
2246
         }
 
2247
      }
 
2248
   
 
2249
      if(shouldFailover)
 
2250
      {
 
2251
         InfoLog (<< "Try sending request to a different dns result");
 
2252
         assert(mMethod!=CANCEL);
 
2253
 
 
2254
         switch (mDnsResult->available())
 
2255
         {
 
2256
            case DnsResult::Available:
 
2257
               InfoLog(<< "We have another DNS result to try.");
 
2258
               restoreOriginalContactAndVia();
 
2259
               mTarget = mDnsResult->next();
 
2260
               mMsgToRetransmit.clear();
 
2261
               processReliability(mTarget.getType());
 
2262
               sendCurrentToWire();
 
2263
               break;
 
2264
            
 
2265
            case DnsResult::Pending:
 
2266
               InfoLog(<< "We have a DNS query pending.");
 
2267
               mWaitingForDnsResult=true;
 
2268
               restoreOriginalContactAndVia();
 
2269
               mMsgToRetransmit.clear();
 
2270
               break;
 
2271
 
 
2272
            case DnsResult::Finished:
 
2273
               InfoLog(<< "No DNS results remain.");
 
2274
               processNoDnsResults();
 
2275
               break;
 
2276
 
 
2277
            case DnsResult::Destroyed:
 
2278
            default:
 
2279
               InfoLog (<< "Bad state: " << *this);
 
2280
               assert(0);
 
2281
         }
 
2282
      }
 
2283
      else
 
2284
      {
 
2285
         InfoLog(<< "Transport failure on send, and failover is disabled.");
 
2286
         processNoDnsResults();
 
2287
      }
 
2288
   }
 
2289
}
 
2290
 
 
2291
// called by DnsResult
 
2292
void
 
2293
TransactionState::rewriteRequest(const Uri& rewrite)
 
2294
{
 
2295
   // !bwc! TODO We need to address the race-conditions caused by callbacks
 
2296
   // into a class whose thread-safety is accomplished through message-passing.
 
2297
   // This function could very easily be called while other processing is
 
2298
   // taking place due to a message from the state-machine fifo. In the end, I
 
2299
   // imagine that we will need to have the callback place a message onto the
 
2300
   // queue, and move all the code below into a function that handles that
 
2301
   // message.
 
2302
 
 
2303
   assert(mNextTransmission->isRequest());
 
2304
   if (mNextTransmission->const_header(h_RequestLine).uri() != rewrite)
 
2305
   {
 
2306
      InfoLog (<< "Rewriting request-uri to " << rewrite);
 
2307
      mNextTransmission->header(h_RequestLine).uri() = rewrite;
 
2308
      // !bwc! Changing mNextTransmission invalidates mMsgToRetransmit.
 
2309
      mMsgToRetransmit.clear();
 
2310
   }
 
2311
}
 
2312
 
 
2313
void
 
2314
TransactionState::handle(DnsResult* result)
 
2315
{
 
2316
   // ?bwc? Maybe optmize this to use handleSync() directly when running in
 
2317
   // single-threaded mode?
 
2318
   DnsResultMessage* dns = new DnsResultMessage(mId,isClient());
 
2319
   mController.mStateMacFifo.add(static_cast<TransactionMessage*>(dns));
 
2320
}
 
2321
 
 
2322
void
 
2323
TransactionState::handleSync(DnsResult* result)
 
2324
{
 
2325
   StackLog (<< *this << " got DNS result: " << *result);
 
2326
   
 
2327
   // .bwc. Were we expecting something from mDnsResult?
 
2328
   if (mWaitingForDnsResult) 
 
2329
   {
 
2330
      assert(mDnsResult);
 
2331
      switch (mDnsResult->available())
 
2332
      {
 
2333
         case DnsResult::Available:
 
2334
            mWaitingForDnsResult=false;
 
2335
            mTarget = mDnsResult->next();
 
2336
            assert( mTarget.transport==0 );
 
2337
            // below allows TU to which transport we send on
 
2338
            // (The Via mechanism for setting transport doesn't work for TLS)
 
2339
            mTarget.transport = mNextTransmission->getDestination().transport;
 
2340
            processReliability(mTarget.getType());
 
2341
            sendCurrentToWire();
 
2342
            break;
 
2343
            
 
2344
         case DnsResult::Finished:
 
2345
            mWaitingForDnsResult=false;
 
2346
            processNoDnsResults();
 
2347
            break;
 
2348
 
 
2349
         case DnsResult::Pending:
 
2350
            break;
 
2351
            
 
2352
         case DnsResult::Destroyed:
 
2353
         default:
 
2354
            assert(0);
 
2355
            break;
 
2356
      }
 
2357
   }
 
2358
}
 
2359
 
 
2360
void
 
2361
TransactionState::processReliability(TransportType type)
 
2362
{
 
2363
   switch (type)
 
2364
   {
 
2365
      case UDP:
 
2366
      case DCCP:
 
2367
         if (mIsReliable)
 
2368
         {
 
2369
            mIsReliable = false;
 
2370
            StackLog (<< "Unreliable transport: " << *this);
 
2371
            switch (mMachine)
 
2372
            {
 
2373
               case ClientNonInvite:
 
2374
                  mController.mTimers.add(Timer::TimerE1, mId, Timer::T1 );
 
2375
                  break;
 
2376
                  
 
2377
               case ClientInvite:
 
2378
                  mController.mTimers.add(Timer::TimerA, mId, Timer::T1 );
 
2379
                  break;
 
2380
 
 
2381
               default:
 
2382
                  break;
 
2383
            }
 
2384
         }
 
2385
         break;
 
2386
         
 
2387
      default:
 
2388
         if (!mIsReliable)
 
2389
         {
 
2390
            mIsReliable = true;
 
2391
         }
 
2392
         break;
 
2393
   }
 
2394
}
 
2395
 
 
2396
// !ah! only used one place, so leaving it here instead of making a helper.
 
2397
// !ah! broken out for clarity -- only used for forceTargets.
 
2398
// Expects that host portion is IP address notation.
 
2399
 
 
2400
static const Tuple
 
2401
simpleTupleForUri(const Uri& uri)
 
2402
{
 
2403
   const Data& host = uri.host();
 
2404
   int port = uri.port();
 
2405
 
 
2406
   resip::TransportType transport = UNKNOWN_TRANSPORT;
 
2407
 
 
2408
  if (uri.exists(p_transport))
 
2409
   {
 
2410
      transport = Tuple::toTransport(uri.param(p_transport));
 
2411
   }
 
2412
 
 
2413
   if (transport == UNKNOWN_TRANSPORT)
 
2414
   {
 
2415
      transport = UDP;
 
2416
   }
 
2417
   if (port == 0)
 
2418
   {
 
2419
      switch(transport)
 
2420
      {
 
2421
         case TLS:
 
2422
            port = Symbols::DefaultSipsPort;
 
2423
            break;
 
2424
         case UDP:
 
2425
         case TCP:
 
2426
         default:
 
2427
            port = Symbols::DefaultSipPort;
 
2428
            break;
 
2429
         // !ah! SCTP?
 
2430
 
 
2431
      }
 
2432
   }
 
2433
 
 
2434
   return Tuple(host,port,transport);
 
2435
}
 
2436
 
 
2437
void
 
2438
TransactionState::sendCurrentToWire() 
 
2439
{
 
2440
   if(!mMsgToRetransmit.empty())
 
2441
   {
 
2442
      if(mController.mStack.statisticsManagerEnabled())
 
2443
      {
 
2444
         mController.mStatsManager.retransmitted(mCurrentMethodType, 
 
2445
                                                   isClient(), 
 
2446
                                                   mCurrentResponseCode);
 
2447
      }
 
2448
 
 
2449
      mController.mTransportSelector.retransmit(mMsgToRetransmit);
 
2450
   }
 
2451
   else if(mNextTransmission) // initial transmission; need to determine target
 
2452
   {
 
2453
      SipMessage* sip=mNextTransmission;
 
2454
      bool transmitted=false;
 
2455
 
 
2456
      if(isClient())
 
2457
      {
 
2458
         if(mTarget.getType() != UNKNOWN_TRANSPORT) // mTarget is set, so just send.
 
2459
         {
 
2460
            transmitted=mController.mTransportSelector.transmit(
 
2461
                        sip, 
 
2462
                        mTarget,
 
2463
                        mIsReliable ? 0 : &mMsgToRetransmit);
 
2464
         }
 
2465
         else // mTarget isn't set...
 
2466
         {
 
2467
            if (sip->getDestination().mFlowKey) //...but sip->getDestination() will work
 
2468
            {
 
2469
               // ?bwc? Maybe we should be nice to the TU and do DNS in this case?
 
2470
               assert(sip->getDestination().getType() != UNKNOWN_TRANSPORT);
 
2471
 
 
2472
               // .bwc. We have the FlowKey. This completely specifies our 
 
2473
               // Transport (and Connection, if applicable). No DNS required.
 
2474
               DebugLog(<< "Sending to tuple: " << sip->getDestination());
 
2475
               mTarget = sip->getDestination();
 
2476
               processReliability(mTarget.getType());
 
2477
               transmitted=mController.mTransportSelector.transmit(
 
2478
                           sip, 
 
2479
                           mTarget,
 
2480
                           mIsReliable ? 0 : &mMsgToRetransmit);
 
2481
            }
 
2482
            else // ...so DNS is required...
 
2483
            {
 
2484
               if(mDnsResult == 0) // ... and we haven't started a DNS query yet.
 
2485
               {
 
2486
                  StackLog (<< "sendToWire with no dns result: " << *this);
 
2487
                  assert(sip->isRequest());
 
2488
                  assert(mMethod!=CANCEL); // .bwc. mTarget should be set in this case.
 
2489
                  mDnsResult = mController.mTransportSelector.createDnsResult(this);
 
2490
                  mWaitingForDnsResult=true;
 
2491
                  mController.mTransportSelector.dnsResolve(mDnsResult, sip);
 
2492
               }
 
2493
               else // ... but our DNS query isn't done yet.
 
2494
               {
 
2495
                  // .bwc. While the resolver was attempting to find a target, another
 
2496
                  // request came down from the TU. This could be a bug in the TU, or 
 
2497
                  // could be a retransmission of an ACK/200. Either way, we cannot
 
2498
                  // expect to ever be able to send this request (nowhere to store it
 
2499
                  // temporarily).
 
2500
                  // ?bwc? Higher log-level?
 
2501
                  DebugLog(<< "Received a second request from the TU for a transaction"
 
2502
                           " that already existed, before the DNS subsystem was done "
 
2503
                           "resolving the target for the first request. Either the TU"
 
2504
                           " has messed up, or it is retransmitting ACK/200 (the only"
 
2505
                           " valid case for this to happen)");
 
2506
               }
 
2507
            }
 
2508
         }
 
2509
      }
 
2510
      else // server transaction
 
2511
      {
 
2512
         assert(mDnsResult == 0);
 
2513
         assert(sip->exists(h_Vias));
 
2514
         assert(!sip->const_header(h_Vias).empty());
 
2515
 
 
2516
         // .bwc. Code that tweaks mResponseTarget based on stuff in the SipMessage.
 
2517
         // ?bwc? Why?
 
2518
         if (sip->hasForceTarget())
 
2519
         {
 
2520
            // ?bwc? Override the target for a single response? Should we even
 
2521
            // allow this? What about client transactions? Should we overwrite 
 
2522
            // mResponseTarget here? I don't think this has been thought out properly.
 
2523
            Tuple target = simpleTupleForUri(sip->getForceTarget());
 
2524
            StackLog(<<"!ah! response with force target going to : "<<target);
 
2525
            transmitted=mController.mTransportSelector.transmit(
 
2526
                        sip, 
 
2527
                        mTarget,
 
2528
                        mIsReliable ? 0 : &mMsgToRetransmit);
 
2529
         }
 
2530
         else
 
2531
         {
 
2532
            if (sip->const_header(h_Vias).front().exists(p_rport) && sip->const_header(h_Vias).front().param(p_rport).hasValue())
 
2533
            {
 
2534
               // ?bwc? This was not setting the port in mResponseTarget before. Why would
 
2535
               // the rport be different than the port in mResponseTarget? Didn't we 
 
2536
               // already set this? Maybe the TU messed with it? If so, why should we pay 
 
2537
               // attention to it? Again, this hasn't been thought out.
 
2538
               mResponseTarget.setPort(sip->const_header(h_Vias).front().param(p_rport).port());
 
2539
               StackLog(<< "rport present in response: " << mResponseTarget.getPort());
 
2540
            }
 
2541
   
 
2542
            StackLog(<< "tid=" << sip->getTransactionId() << " sending to : " << mResponseTarget);
 
2543
            transmitted=mController.mTransportSelector.transmit(
 
2544
                        sip, 
 
2545
                        mResponseTarget,
 
2546
                        mIsReliable ? 0 : &mMsgToRetransmit);
 
2547
         }
 
2548
      }
 
2549
 
 
2550
      // !bwc! If we don't have DNS results yet, or TransportSelector::transmit
 
2551
      // fails, we hang on to the full original SipMessage, in the hope that 
 
2552
      // next time it works.
 
2553
      if (transmitted)
 
2554
      {
 
2555
         if(mController.mStack.statisticsManagerEnabled())
 
2556
         {
 
2557
            mController.mStatsManager.sent(sip);
 
2558
         }
 
2559
 
 
2560
         mCurrentMethodType = sip->method();
 
2561
         if(sip->isResponse())
 
2562
         {
 
2563
            mCurrentResponseCode = sip->const_header(h_StatusLine).statusCode();
 
2564
         }
 
2565
 
 
2566
         // !bwc! If mNextTransmission is a non-ACK request, we need to save the
 
2567
         // initial request in case we need to send a simulated 408 or a 503 to 
 
2568
         // the TU (at least, until we get a response back)
 
2569
         if(!mNextTransmission->isRequest() || mNextTransmission->method()==ACK)
 
2570
         {
 
2571
            delete mNextTransmission;
 
2572
            mNextTransmission=0;
 
2573
         }
 
2574
      }
 
2575
   }
 
2576
   else
 
2577
   {
 
2578
      assert(0);
 
2579
   }
 
2580
}
 
2581
 
 
2582
void
 
2583
TransactionState::sendToTU(TransactionMessage* msg)
 
2584
{
 
2585
   SipMessage* sipMsg = dynamic_cast<SipMessage*>(msg);
 
2586
   if (sipMsg && sipMsg->isResponse() && mDnsResult)
 
2587
   {
 
2588
      // whitelisting rules.
 
2589
      switch (sipMsg->const_header(h_StatusLine).statusCode())
 
2590
      {
 
2591
         case 503:
 
2592
            // blacklist last target.
 
2593
            // .bwc. If there is no Retry-After, we do not blacklist
 
2594
            // (see RFC 3261 sec 21.5.4 para 1)
 
2595
            if(sipMsg->exists(resip::h_RetryAfter) && 
 
2596
               sipMsg->const_header(resip::h_RetryAfter).isWellFormed())
 
2597
            {
 
2598
               unsigned int relativeExpiry= sipMsg->const_header(resip::h_RetryAfter).value();
 
2599
               
 
2600
               if(relativeExpiry!=0)
 
2601
               {
 
2602
                  mDnsResult->blacklistLast(resip::Timer::getTimeMs()+relativeExpiry*1000);
 
2603
               }
 
2604
            }
 
2605
            
 
2606
            break;
 
2607
         case 408:
 
2608
            if(sipMsg->getReceivedTransport() == 0 && 
 
2609
                  (mState == Trying || mState==Calling))  // only greylist if internally generated and we haven't received any responses yet
 
2610
            {
 
2611
               // greylist last target.
 
2612
               // ?bwc? How long do we greylist this for? Probably should make
 
2613
               // this configurable. TODO
 
2614
               mDnsResult->greylistLast(resip::Timer::getTimeMs() + 32000);
 
2615
            }
 
2616
 
 
2617
            break;
 
2618
         default:
 
2619
            // !bwc! Debatable.
 
2620
            mDnsResult->whitelistLast();
 
2621
            break;
 
2622
      }
 
2623
   }
 
2624
 
 
2625
   CongestionManager::RejectionBehavior behavior=CongestionManager::NORMAL;
 
2626
   behavior=mController.mTuSelector.getRejectionBehavior(mTransactionUser);
 
2627
 
 
2628
   if(behavior!=CongestionManager::NORMAL)
 
2629
   {
 
2630
      if(sipMsg)
 
2631
      {
 
2632
         assert(sipMsg->isExternal());
 
2633
         if(sipMsg->isRequest())
 
2634
         {
 
2635
            // .bwc. This could be an initial request, or an ACK/200.
 
2636
            if(sipMsg->method()==ACK)
 
2637
            {
 
2638
               // ACK/200 is a continuation of old work. We only reject if
 
2639
               // we're really hosed.
 
2640
               if(behavior==CongestionManager::REJECTING_NON_ESSENTIAL)
 
2641
               {
 
2642
                  delete msg;
 
2643
                  return;
 
2644
               }
 
2645
            }
 
2646
            else
 
2647
            {
 
2648
               // .bwc. This is new work. Reject.
 
2649
               SipMessage* response(Helper::makeResponse(*sipMsg, 503));
 
2650
               delete sipMsg;
 
2651
               
 
2652
               UInt16 retryAfter=mController.mTuSelector.getExpectedWait(mTransactionUser);
 
2653
               response->header(h_RetryAfter).value()=retryAfter;
 
2654
               response->setFromTU();
 
2655
               if(mMethod==INVITE)
 
2656
               {
 
2657
                  processServerInvite(response);
 
2658
               }
 
2659
               else
 
2660
               {
 
2661
                  processServerNonInvite(response);
 
2662
               }
 
2663
               return;
 
2664
            }
 
2665
         }
 
2666
         else
 
2667
         {
 
2668
            // .bwc. This could be a response from the wire, or an internally
 
2669
            // generated pseudo-response. This is always a continuation of
 
2670
            // old work.
 
2671
            if(behavior==CongestionManager::REJECTING_NON_ESSENTIAL &&
 
2672
               mTransactionUser &&
 
2673
               !mTransactionUser->responsesMandatory())
 
2674
            {
 
2675
               delete sipMsg;
 
2676
               return;
 
2677
            }
 
2678
         }
 
2679
      }
 
2680
      else
 
2681
      {
 
2682
         // .bwc. This is some sort of timer, or other message. If we don't know 
 
2683
         // any better, we need to assume this is essential for the safe
 
2684
         // operation of the TU.
 
2685
      }
 
2686
   }
 
2687
   
 
2688
   TransactionState::sendToTU(mTransactionUser, mController, msg);
 
2689
}
 
2690
 
 
2691
void
 
2692
TransactionState::sendToTU(TransactionUser* tu, TransactionController& controller, TransactionMessage* msg) 
 
2693
{   
 
2694
   msg->setTransactionUser(tu);
 
2695
   controller.mTuSelector.add(msg, TimeLimitFifo<Message>::InternalElement);
 
2696
}
 
2697
 
 
2698
SipMessage*
 
2699
TransactionState::make100(SipMessage* request) const
 
2700
{
 
2701
   return (Helper::makeResponse(*request, 100));
 
2702
}
 
2703
 
 
2704
void
 
2705
TransactionState::add(const Data& tid)
 
2706
{
 
2707
   if (isClient())
 
2708
   {
 
2709
      mController.mClientTransactionMap.add(tid, this);
 
2710
   }
 
2711
   else
 
2712
   {
 
2713
      mController.mServerTransactionMap.add(tid, this);
 
2714
   }
 
2715
}
 
2716
 
 
2717
void
 
2718
TransactionState::erase(const Data& tid)
 
2719
{
 
2720
   if (isClient())
 
2721
   {
 
2722
      mController.mClientTransactionMap.erase(tid);
 
2723
   }
 
2724
   else
 
2725
   {
 
2726
      mController.mServerTransactionMap.erase(tid);
 
2727
   }
 
2728
}
 
2729
 
 
2730
bool
 
2731
TransactionState::isRequest(TransactionMessage* msg) const
 
2732
{
 
2733
   SipMessage* sip = dynamic_cast<SipMessage*>(msg);   
 
2734
   return sip && sip->isRequest();
 
2735
}
 
2736
 
 
2737
bool
 
2738
TransactionState::isInvite(TransactionMessage* msg) const
 
2739
{
 
2740
   if (isRequest(msg))
 
2741
   {
 
2742
      SipMessage* sip = dynamic_cast<SipMessage*>(msg);
 
2743
      return (sip->method()) == INVITE;
 
2744
   }
 
2745
   return false;
 
2746
}
 
2747
 
 
2748
bool
 
2749
TransactionState::isResponse(TransactionMessage* msg, int lower, int upper) const
 
2750
{
 
2751
   SipMessage* sip = dynamic_cast<SipMessage*>(msg);
 
2752
   if (sip && sip->isResponse())
 
2753
   {
 
2754
      int c = sip->const_header(h_StatusLine).responseCode();
 
2755
      return (c >= lower && c <= upper);
 
2756
   }
 
2757
   return false;
 
2758
}
 
2759
 
 
2760
bool
 
2761
TransactionState::isTimer(TransactionMessage* msg) const
 
2762
{
 
2763
   return dynamic_cast<TimerMessage*>(msg) != 0;
 
2764
}
 
2765
 
 
2766
bool
 
2767
TransactionState::isFromTU(TransactionMessage* msg) const
 
2768
{
 
2769
   SipMessage* sip = dynamic_cast<SipMessage*>(msg);
 
2770
   return sip && !sip->isExternal();
 
2771
}
 
2772
 
 
2773
bool
 
2774
TransactionState::isFromWire(TransactionMessage* msg) const
 
2775
{
 
2776
   SipMessage* sip = dynamic_cast<SipMessage*>(msg);
 
2777
   return sip && sip->isExternal();
 
2778
}
 
2779
 
 
2780
bool
 
2781
TransactionState::isTransportError(TransactionMessage* msg) const
 
2782
{
 
2783
   return dynamic_cast<TransportFailure*>(msg) != 0;
 
2784
}
 
2785
 
 
2786
bool 
 
2787
TransactionState::isAbandonServerTransaction(TransactionMessage* msg) const
 
2788
{
 
2789
   return dynamic_cast<AbandonServerTransaction*>(msg) != 0;
 
2790
}
 
2791
 
 
2792
bool 
 
2793
TransactionState::isCancelClientTransaction(TransactionMessage* msg) const
 
2794
{
 
2795
   return dynamic_cast<CancelClientInviteTransaction*>(msg) != 0;
 
2796
}
 
2797
 
 
2798
 
 
2799
const Data&
 
2800
TransactionState::tid(SipMessage* sip) const
 
2801
{
 
2802
   assert(0);
 
2803
   assert (mMachine != Stateless || (mMachine == Stateless && !mId.empty()));
 
2804
   assert (mMachine == Stateless || (mMachine != Stateless && sip));
 
2805
   return (mId.empty() && sip) ? sip->getTransactionId() : mId;
 
2806
}
 
2807
 
 
2808
void
 
2809
TransactionState::terminateClientTransaction(const Data& tid)
 
2810
{
 
2811
   mState = Terminated;
 
2812
   if (mController.mTuSelector.isTransactionUserStillRegistered(mTransactionUser) && 
 
2813
       mTransactionUser->isRegisteredForTransactionTermination())
 
2814
   {
 
2815
      //StackLog (<< "Terminate client transaction " << tid);
 
2816
      sendToTU(new TransactionTerminated(tid, true, mTransactionUser));
 
2817
   }
 
2818
}
 
2819
 
 
2820
void
 
2821
TransactionState::terminateServerTransaction(const Data& tid)
 
2822
{
 
2823
   mState = Terminated;
 
2824
   if (mController.mTuSelector.isTransactionUserStillRegistered(mTransactionUser) && 
 
2825
       mTransactionUser->isRegisteredForTransactionTermination())
 
2826
   {
 
2827
      //StackLog (<< "Terminate server transaction " << tid);
 
2828
      sendToTU(new TransactionTerminated(tid, false, mTransactionUser));
 
2829
   }
 
2830
}
 
2831
 
 
2832
bool 
 
2833
TransactionState::isClient() const
 
2834
{
 
2835
   switch(mMachine)
 
2836
   {
 
2837
      case ClientNonInvite:
 
2838
      case ClientInvite:
 
2839
      case ClientStale:
 
2840
      case Stateless:
 
2841
         return true;
 
2842
      case ServerNonInvite:
 
2843
      case ServerInvite:
 
2844
      case ServerStale:
 
2845
         return false;
 
2846
      default:
 
2847
         assert(0);
 
2848
   }
 
2849
   return false;
 
2850
}
 
2851
 
 
2852
EncodeStream& 
 
2853
resip::operator<<(EncodeStream& strm, const resip::TransactionState& state)
 
2854
{
 
2855
   strm << "tid=" << state.mId << " [ ";
 
2856
   switch (state.mMachine)
 
2857
   {
 
2858
      case TransactionState::ClientNonInvite:
 
2859
         strm << "ClientNonInvite";
 
2860
         break;
 
2861
      case TransactionState::ClientInvite:
 
2862
         strm << "ClientInvite";
 
2863
         break;
 
2864
      case TransactionState::ServerNonInvite:
 
2865
         strm << "ServerNonInvite";
 
2866
         break;
 
2867
      case TransactionState::ServerInvite:
 
2868
         strm << "ServerInvite";
 
2869
         break;
 
2870
      case TransactionState::Stateless:
 
2871
         strm << "Stateless";
 
2872
         break;
 
2873
      case TransactionState::ClientStale:
 
2874
         strm << "ClientStale";
 
2875
         break;
 
2876
      case TransactionState::ServerStale:
 
2877
         strm << "ServerStale";
 
2878
         break;
 
2879
   }
 
2880
   
 
2881
   strm << "/";
 
2882
   switch (state.mState)
 
2883
   {
 
2884
      case TransactionState::Calling:
 
2885
         strm << "Calling";
 
2886
         break;
 
2887
      case TransactionState::Trying:
 
2888
         strm << "Trying";
 
2889
         break;
 
2890
      case TransactionState::Proceeding:
 
2891
         strm << "Proceeding";
 
2892
         break;
 
2893
      case TransactionState::Completed:
 
2894
         strm << "Completed";
 
2895
         break;
 
2896
      case TransactionState::Confirmed:
 
2897
         strm << "Confirmed";
 
2898
         break;
 
2899
      case TransactionState::Terminated:
 
2900
         strm << "Terminated";
 
2901
         break;
 
2902
      case TransactionState::Bogus:
 
2903
         strm << "Bogus";
 
2904
         break;
 
2905
   }
 
2906
   
 
2907
   strm << (state.mIsReliable ? " reliable" : " unreliable");
 
2908
   strm << " target=" << state.mResponseTarget;
 
2909
   //if (state.mTransactionUser) strm << " tu=" << *state.mTransactionUser;
 
2910
   //else strm << "default TU";
 
2911
   strm << "]";
 
2912
   return strm;
 
2913
}
 
2914
 
 
2915
 
 
2916
/* Local Variables: */
 
2917
/* c-file-style: "ellemtel" */
 
2918
/* End: */
 
2919
 
 
2920
/* ====================================================================
 
2921
 * The Vovida Software License, Version 1.0 
 
2922
 * 
 
2923
 * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
 
2924
 * 
 
2925
 * Redistribution and use in source and binary forms, with or without
 
2926
 * modification, are permitted provided that the following conditions
 
2927
 * are met:
 
2928
 * 
 
2929
 * 1. Redistributions of source code must retain the above copyright
 
2930
 *    notice, this list of conditions and the following disclaimer.
 
2931
 * 
 
2932
 * 2. Redistributions in binary form must reproduce the above copyright
 
2933
 *    notice, this list of conditions and the following disclaimer in
 
2934
 *    the documentation and/or other materials provided with the
 
2935
 *    distribution.
 
2936
 * 
 
2937
 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
 
2938
 *    and "Vovida Open Communication Application Library (VOCAL)" must
 
2939
 *    not be used to endorse or promote products derived from this
 
2940
 *    software without prior written permission. For written
 
2941
 *    permission, please contact vocal@vovida.org.
 
2942
 *
 
2943
 * 4. Products derived from this software may not be called "VOCAL", nor
 
2944
 *    may "VOCAL" appear in their name, without prior written
 
2945
 *    permission of Vovida Networks, Inc.
 
2946
 * 
 
2947
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
 
2948
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 
2949
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
 
2950
 * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
 
2951
 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
 
2952
 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
 
2953
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
2954
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
2955
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 
2956
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
2957
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 
2958
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 
2959
 * DAMAGE.
 
2960
 * 
 
2961
 * ====================================================================
 
2962
 * 
 
2963
 * This software consists of voluntary contributions made by Vovida
 
2964
 * Networks, Inc. and many individuals on behalf of Vovida Networks,
 
2965
 * Inc.  For more information on Vovida Networks, Inc., please see
 
2966
 * <http://www.vovida.org/>.
 
2967
 *
 
2968
 * vi: set shiftwidth=3 expandtab:
 
2969
 */