File: Connection.cxx

package info (click to toggle)
resiprocate 1%3A1.11.0~beta1-3%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 37,944 kB
  • sloc: cpp: 206,325; xml: 12,515; sh: 12,418; ansic: 6,973; makefile: 2,315; php: 1,150; python: 355; sql: 142; objc: 91; perl: 21; csh: 5
file content (605 lines) | stat: -rw-r--r-- 17,093 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif

#include "rutil/ResipAssert.h"
#include "rutil/Socket.hxx"
#include "rutil/Logger.hxx"
#include "resip/stack/Connection.hxx"
#include "resip/stack/ConnectionManager.hxx"
#include "resip/stack/InteropHelper.hxx"
#include "resip/stack/SipMessage.hxx"
#include "resip/stack/TcpBaseTransport.hxx"
#include "rutil/WinLeakCheck.hxx"

#ifdef USE_SSL
#include "resip/stack/ssl/Security.hxx"
#endif

#ifdef WIN32
#include <Mswsock.h>
#endif

#ifdef USE_SIGCOMP
#include <osc/Stack.h>
#include <osc/SigcompMessage.h>
#endif

using namespace resip;

volatile bool Connection::mEnablePostConnectSocketFuncCall = false;

#define RESIPROCATE_SUBSYSTEM Subsystem::TRANSPORT

Connection::Connection(Transport* transport,const Tuple& who, Socket socket,
                       Compression &compression,
                       bool isServer)
   : ConnectionBase(transport,who,compression),
     mFirstWriteAfterConnectedPending(false),
     mInWritable(false),
     mFlowTimerEnabled(false),
     mPollItemHandle(0),
     mIsServer(isServer)
{
   mWho.mFlowKey=(FlowKey)socket;
   InfoLog (<< "Connection::Connection: new connection created to who: " << mWho << ", is server = " << mIsServer);

   if(transport && isWebSocket(transport->transport()))
   {
      mSendingTransmissionFormat = WebSocketHandshake;
      mReceivingTransmissionFormat = WebSocketHandshake;
   }

   if(mWho.mFlowKey && ConnectionBase::transport())
   {
      getConnectionManager().addConnection(this);
   }
}

Connection::~Connection()
{
   if(mWho.mFlowKey && ConnectionBase::transport())
   {
      getConnectionManager().removeConnection(this);
      // remove first then close, since conn manager may need socket
      closeSocket(mWho.mFlowKey);
   }
}

void
Connection::requestWrite(SendData* sendData)
{
   mOutstandingSends.push_back(sendData);
   if (isWritable())
   {
      ensureWritable();
   }
}

void 
Connection::removeFrontOutstandingSend()
{
   delete mOutstandingSends.front();
   mOutstandingSends.pop_front();

   if (mOutstandingSends.empty())
   {
      resip_assert(mInWritable);
      getConnectionManager().removeFromWritable(this);
      mInWritable = false;
   }
}

int
Connection::performWrite()
{
   if(transportWrite())
   {
      // If we get here it means:
      // a. on a previous invocation, SSL_do_handshake wanted to write
      //         (SSL_ERROR_WANT_WRITE)
      // b. now the handshake is complete or it wants to read
      if(mInWritable)
      {
         getConnectionManager().removeFromWritable(this);
         mInWritable = false;
      }
      else
      {
         WarningLog(<<"performWrite invoked while not in write set");
      }
      return 0; // Q. What does this transportWrite() mean?
                // A. It makes the TLS handshake move along after it
                //    was waiting in the write set.
   }

   // If the TLS handshake returned SSL_ERROR_WANT_WRITE again
   // then we could get here without really having something to write
   // so just return, remaining in the write set.
   if(mOutstandingSends.empty())
   {
      // FIXME: this needs to be more elaborate with respect
      // to TLS handshaking but it doesn't appear we can do that
      // without ABI breakage.
      return 0;
   }

   switch(mOutstandingSends.front()->command)
   {
   case SendData::CloseConnection:
      // .bwc. Close this connection.
      return -1;
      break;
   case SendData::EnableFlowTimer:
      enableFlowTimer();
      removeFrontOutstandingSend();
      return 0;
      break;
   default:
      // do nothing
      break;
   }

   const Data& sigcompId = mOutstandingSends.front()->sigcompId;

   if(mSendingTransmissionFormat == Unknown)
   {
      if (sigcompId.size() > 0 && mCompression.isEnabled())
      {
         mSendingTransmissionFormat = Compressed;
      }
      else
      {
         mSendingTransmissionFormat = Uncompressed;
      }
   }
   else if(mSendingTransmissionFormat == WebSocketHandshake)
   {
      mSendingTransmissionFormat = WebSocketData;
   }
   else if(mSendingTransmissionFormat == WebSocketData)
   {
      SendData *dataWs, *oldSd;
      const Data& dataRaw = mOutstandingSends.front()->data;
      UInt64 dataSize = 1 + 1 + dataRaw.size();
      UInt64 lSize = (UInt64)dataRaw.size();
      UInt8* uBuffer;

      if(lSize > 0x7D && lSize <= 0xFFFF)
      {
         dataSize += 2;
      }
      else if(lSize > 0xFFFF)
      {
         dataSize += 8;
      }

      oldSd = mOutstandingSends.front();
      dataWs = new SendData(oldSd->destination,
            Data(Data::Take, new char[(int)dataSize], (Data::size_type)dataSize),
            oldSd->transactionId,
            oldSd->sigcompId,
            false);
      resip_assert(dataWs && dataWs->data.data());
      uBuffer = (UInt8*)dataWs->data.data();

      uBuffer[0] = 0x82;
      if(lSize <= 0x7D)
      {
         uBuffer[1] = (UInt8)lSize;
         uBuffer = &uBuffer[2];
      }
      else if(lSize <= 0xFFFF)
      {
         uBuffer[1] = 0x7E;
         uBuffer[2] = (UInt8)((lSize >> 8) & 0xFF);
         uBuffer[3] = (UInt8)(lSize & 0xFF);
         uBuffer = &uBuffer[4];
      }
      else
      {
         uBuffer[1] = 0x7F;
         uBuffer[2] = (UInt8)((lSize >> 56) & 0xFF);
         uBuffer[3] = (UInt8)((lSize >> 48) & 0xFF);
         uBuffer[4] = (UInt8)((lSize >> 40) & 0xFF);
         uBuffer[5] = (UInt8)((lSize >> 32) & 0xFF);
         uBuffer[6] = (UInt8)((lSize >> 24) & 0xFF);
         uBuffer[7] = (UInt8)((lSize >> 16) & 0xFF);
         uBuffer[8] = (UInt8)((lSize >> 8) & 0xFF);
         uBuffer[9] = (UInt8)(lSize & 0xFF);
         uBuffer = &uBuffer[10];
      }

      memcpy(uBuffer, dataRaw.data(), dataRaw.size());
      mOutstandingSends.front() = dataWs;
      dataWs = 0;
      delete oldSd;
   }

#ifdef USE_SIGCOMP
   // Perform compression here, if appropriate
   if (mSendingTransmissionFormat == Compressed
       && !(mOutstandingSends.front()->isAlreadyCompressed))
   {
      const Data& uncompressed = mOutstandingSends.front()->data;
      osc::SigcompMessage *sm = 
        mSigcompStack->compressMessage(uncompressed.data(), uncompressed.size(),
                                       sigcompId.data(), sigcompId.size(),
                                       true);
      DebugLog (<< "Compressed message from "
                << uncompressed.size() << " bytes to " 
                << sm->getStreamLength() << " bytes");

      SendData *oldSd = mOutstandingSends.front();
      SendData *newSd = new SendData(oldSd->destination,
                                     Data(sm->getStreamMessage(),
                                          sm->getStreamLength()),
                                     oldSd->transactionId,
                                     oldSd->sigcompId,
                                     true);
      mOutstandingSends.front() = newSd;
      delete oldSd;
      delete sm;
   }
#endif

   // Note:  The first time the socket is available for write, is when the TCP connect call is completed
   if (mFirstWriteAfterConnectedPending)
   {
      mFirstWriteAfterConnectedPending = false;  // reset

      // Notify all outstanding sends that we are now connected - stops the TCP Connection timer for all transactions
      for (std::list<SendData*>::iterator it = mOutstandingSends.begin(); it != mOutstandingSends.end(); it++)
      {
         mTransport->setTcpConnectState((*it)->transactionId, TcpConnectState::Connected);
      }
      if (mEnablePostConnectSocketFuncCall)
      {
          mTransport->callSocketFunc(getSocket());
      }
   }

   const Data& data = mOutstandingSends.front()->data;
   int nBytes = write(data.data() + mSendPos,int(data.size() - mSendPos));

   //DebugLog (<< "Tried to send " << data.size() - mSendPos << " bytes, sent " << nBytes << " bytes");

   if (nBytes < 0)
   {
      //fail(data.transactionId);
      InfoLog(<< "Write failed on socket: " << this->getSocket() << ", closing connection");
      return -1;
   }
   else if (nBytes == 0)
   {
      // Nothing was written - likely socket buffers are backed up and EWOULDBLOCK was returned
      // no need to do calculations in else statement
      return 0;
   }
   else
   {
      // Safe because of the conditional above ( < 0 ).
      Data::size_type bytesWritten = static_cast<Data::size_type>(nBytes);
      mSendPos += bytesWritten;
      if (mSendPos == data.size())
      {
         mSendPos = 0;
         removeFrontOutstandingSend();
      }
      return bytesWritten;
   }
}


bool 
Connection::performWrites(unsigned int max)
{
   int res;
   // if max==0, we will overflow into UINT_MAX. This is intentional.
   while((res=performWrite())>0 && !mOutstandingSends.empty() && --max!=0)
   {;}

   if(res<0)
   {
      delete this;
      return false;
   }
   return true;
}

void 
Connection::ensureWritable()
{
   if(!mInWritable)
   {
      //assert(!mOutstandingSends.empty()); // empty during TLS handshake
      // therefore must be careful to check mOutstandingSends later
      getConnectionManager().addToWritable(this);
      mInWritable = true;
   }
}

ConnectionManager&
Connection::getConnectionManager() const
{
   TcpBaseTransport* transport = static_cast<TcpBaseTransport*>(ConnectionBase::transport());
   
   return transport->getConnectionManager();
}
            
EncodeStream& 
resip::operator<<(EncodeStream& strm, const resip::Connection& c)
{
   strm << "CONN: " << &c << " " << int(c.getSocket()) << " " << c.mWho;
   return strm;
}

int
Connection::read()
{
   std::pair<char*, size_t> writePair = getWriteBuffer();
   size_t bytesToRead = resipMin(writePair.second, 
                                 static_cast<size_t>(Connection::ChunkSize));
         
   resip_assert(bytesToRead > 0);

   int bytesRead = read(writePair.first, (int)bytesToRead);
   if (bytesRead <= 0)
   {
      return bytesRead;
   }  
   // mBuffer might have been reallocated inside read()
   writePair = getCurrentWriteBuffer();

   getConnectionManager().touch(this);

#ifdef USE_SIGCOMP
   // If this is the first data we read, determine whether the
   // connection is compressed.
   if(mReceivingTransmissionFormat == Unknown)
   {
     if (((writePair.first[0] & 0xf8) == 0xf8) && mCompression.isEnabled())
     {
       mReceivingTransmissionFormat = Compressed;
     }
     else
     {
       mReceivingTransmissionFormat = Uncompressed;
     }
   }

   // SigComp compressed messages are handed very differently
   // than non-compressed messages: they are guaranteed to
   // be framed within SigComp, and each frame contains
   // *exactly* one SIP message. Processing looks a lot like
   // it does for Datagram-oriented transports.

   if (mReceivingTransmissionFormat == Compressed)
   {
     decompressNewBytes(bytesRead);
   }
   else
#endif
   {
      if (mReceivingTransmissionFormat == WebSocketHandshake)
      {
         bool dropConnection = false;
         if(wsProcessHandshake(bytesRead, dropConnection))
         {
            ensureWritable();
            if(performWrites())
            {
               mReceivingTransmissionFormat = WebSocketData;
            }
         }
         else if(dropConnection)
         {
            bytesRead=-1;
         }
      }
      else
      {
         if (mReceivingTransmissionFormat == WebSocketData)
         {
            if(!wsProcessData(bytesRead))
            {
               bytesRead=-1;
            }
         }
         else if(!preparseNewBytes(bytesRead))
         {
            // Iffy; only way we have right now to indicate that this connection has
            // gone away.
            bytesRead=-1;
         }
      }
   }
   return bytesRead;
}

bool 
Connection::performReads(unsigned int max)
{
   int bytesRead;

   // if max==0, we will overflow into UINT_MAX. This is intentional.
   while((bytesRead = read())>0 && --max!=0)
   {
      DebugLog(<< "Connection::performReads() " << " read=" << bytesRead);
   }

   if ( bytesRead < 0 ) 
   {
      DebugLog(<< "Closing connection bytesRead=" << bytesRead);
      delete this;
      return false;
   }
   return true;
}

void
Connection::enableFlowTimer()
{
   if(!mFlowTimerEnabled)
   {
      mFlowTimerEnabled = true;

      // ensure connection is in a FlowTimer LRU list on the connection manager
      getConnectionManager().moveToFlowTimerLru(this);
   }
}

void
Connection::onDoubleCRLF()
{
   // !bwc! TODO might need to make this more efficient.
   // !bwc! Need to make this sigcomp-friendly
   if(InteropHelper::getOutboundVersion()>=8)
   {
      DebugLog(<<"Sending response CRLF (aka pong).");
      requestWrite(new SendData(mWho,Symbols::CRLF,Data::Empty,Data::Empty));
   }
}

void
Connection::onSingleCRLF()
{
   DebugLog(<<"Received response CRLF (aka pong).");
   mTransport->keepAlivePong(mWho);
}

bool 
Connection::hasDataToRead()
{
   return true;
}

bool 
Connection::isGood()
{
   return true;
}

bool 
Connection::checkConnectionTimedout()
{
   int errNum = 0;
   int errNumSize = sizeof(errNum);
   if(getsockopt(mWho.mFlowKey, SOL_SOCKET, SO_ERROR, (char *)&errNum, (socklen_t *)&errNumSize) == 0)
   {
      if (errNum == ETIMEDOUT || errNum == EHOSTUNREACH || 
          errNum == ECONNREFUSED || errNum == ECONNABORTED)
      {
         InfoLog(<< "Exception on socket " << mWho.mFlowKey << " code: " << errNum << "; closing connection");
         setFailureReason(TransportFailure::ConnectionException, errNum);
         delete this;
         return true;
      }
      else if (errNum != 0)
      {
         WarningLog(<< "checkConnectionTimedout " << mWho.mFlowKey << " code: " << errNum << "; ignoring - should we error out?");
      }
   }
   return false;
}

bool 
Connection::isWritable()
{
   return true;
}

/**
    Virtual function of FdPollItemIf, called to process io events
**/
void
Connection::processPollEvent(FdPollEventMask mask) {
   /* The original code in ConnectionManager.cxx didn't check
    * for error events unless no writable event. (e.g., writable
    * masked error. Why?)
    */
   if ( mask & FPEM_Error ) 
   {
      Socket fd = getSocket();
      int errNum = getSocketError(fd);
      InfoLog(<< "Exception on socket " << fd << " code: " << errNum << "; closing connection");
      setFailureReason(TransportFailure::ConnectionException, errNum);
      delete this;
      return;
   }
   if ( mask & FPEM_Write ) 
   {
      if(!performWrites())
      {
         // Just deleted self
         return;
      }
   }
   if ( mask & FPEM_Read ) 
   {
      performReads();
   }
}

bool 
Connection::isServer() const
{ 
   return mIsServer; 
}

void 
Connection::invokeAfterSocketCreationFunc() const
{
    mTransport->callSocketFunc(getSocket());
}

/* ====================================================================
 * The Vovida Software License, Version 1.0 
 * 
 * Copyright (c) 2000
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 
 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
 *    and "Vovida Open Communication Application Library (VOCAL)" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact vocal@vovida.org.
 *
 * 4. Products derived from this software may not be called "VOCAL", nor
 *    may "VOCAL" appear in their name, without prior written
 *    permission of Vovida Networks, Inc.
 * 
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
 * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 * 
 * ====================================================================
 * 
 * This software consists of voluntary contributions made by Vovida
 * Networks, Inc. and many individuals on behalf of Vovida Networks,
 * Inc.  For more information on Vovida Networks, Inc., please see
 * <http://www.vovida.org/>.
 *
 * vi: set shiftwidth=3 expandtab:
 */