⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tuim.cxx

📁 一个著名的SIP协议栈
💻 CXX
📖 第 1 页 / 共 3 页
字号:

   Uri from = msg->header(h_From).uri();
   DebugLog ( << "got notify from " << from );

   Contents* contents = msg->getContents();
   if ( !contents )
   {
      InfoLog(<< "Received NOTIFY message event with no contents" );
      mCallback->presenceUpdate( from, true, Data::Empty );
      return;
   }

   Mime mime = contents->getType();
   DebugLog ( << "got  NOTIFY event with body of type  " << mime.type() << "/" << mime.subType() );
  
   Pidf* body = dynamic_cast<Pidf*>(contents);
   if ( !body )
   {
      InfoLog(<< "Received NOTIFY message event with no PIDF contents" );
      mCallback->presenceUpdate( from, true, Data::Empty );
      return;
   }
 
   Data note;
   bool open = body->getSimpleStatus( &note );

   bool changed = true;


   // update if found in budy list 
   for ( BuddyIterator i=mBuddies.begin(); i != mBuddies.end(); i++)
   {
      Uri u = i->uri; // getBuddyUri(i);
      
      if ( u.getAor() == from.getAor() )
      {

         if ( (i->status == note) &&
              (i->online == open) )
         {
            changed = false;
         }
         
         i->status = note;
         i->online = open;
      }
   }
   
   InfoLog(<< "Processed NOTIFY message : Presence changed: " << changed );
   // notify callback 
   if (changed)
   {
      assert(mCallback);
      mCallback->presenceUpdate( from, open, note );
   }
}


void 
TuIM::processMessageRequest(SipMessage* msg)
{
   assert( msg );
   assert( msg->header(h_RequestLine).getMethod() == MESSAGE );
   
   NameAddr contact; 
   contact.uri() = mContact;
            
   SipMessage* response = Helper::makeResponse(*msg, 200, contact, "OK");
   mStack->send( *response );
   delete response; response=0;
               
   Contents* contents = msg->getContents();
   if ( !contents )
   {
      InfoLog(<< "Received Message message with no contents" );
      delete msg; msg=0;
      return;
   }

   assert( contents );
   Mime mime = contents->getType();
   DebugLog ( << "got body of type  " << mime.type() << "/" << mime.subType() );

   Data signedBy;
   SignatureStatus sigStat = SignatureNone;
   bool encrypted=false;

#if defined( USE_SSL )
   Uri from = msg->header(h_From).uri();
   signedBy = from.getAorNoPort();
   
   InfoLog ( << "assuming signedBy is " << signedBy );
   
   MultipartSignedContents* mBody = dynamic_cast<MultipartSignedContents*>(contents);
   if ( mBody )
   {
      Security* sec = mStack->getSecurity();
      assert(sec);
      
      contents = sec->checkSignature( mBody, &signedBy, &sigStat );
      
      //ErrLog("Signed by " << signedBy << " stat = " << sigStat );
      
      if ( !contents )
      { 
         Uri from = msg->header(h_From).uri();
         InfoLog( << "Some problem decoding multipart/signed message");
         
         mCallback->receivePageFailed( from );
         return;
      } 
   }
  
   Pkcs7SignedContents* sBody = dynamic_cast<Pkcs7SignedContents*>(contents);
   if ( sBody )
   {
      assert( sBody );
      Security* sec = mStack->getSecurity();
      assert(sec);

      contents = sec->decrypt( mAor.getAor(), sBody );

      if ( !contents )
      { 
         Uri from = msg->header(h_From).uri();
         InfoLog( << "Some problem decoding signed SMIME message");
        
         mCallback->receivePageFailed( from );
         return;
      }

      encrypted=true;
   }

   Pkcs7Contents* eBody = dynamic_cast<Pkcs7Contents*>(contents);
   if ( eBody )
   {
      assert( eBody );
      Security* sec = mStack->getSecurity();
      assert(sec);

      contents = sec->decrypt( mAor.getAor(), eBody );

      if ( !contents )
      { 
         Uri from = msg->header(h_From).uri();
         InfoLog( << "Some problem decoding SMIME message");
        
         mCallback->receivePageFailed( from );
         return;
      }

      encrypted=true;
   }
 
#endif

   if ( contents )
   {
      PlainContents* plain = dynamic_cast<PlainContents*>(contents);
      if ( plain )
      {
         assert( plain );
         const Data& text = plain->text();
         DebugLog ( << "got message from with text of <" << text << ">" );
                 
         Uri from = msg->header(h_From).uri();
         DebugLog ( << "got message from " << from );
                  
         assert( mCallback );
         mCallback->receivedPage( text, from, signedBy, sigStat, encrypted );
         return;
      }
 
      CpimContents* cpim = dynamic_cast<CpimContents*>(contents);
      if ( cpim )
      {
         assert( cpim );
         const Data& text = cpim->text();
         DebugLog ( << "got CPIM message from with text of <" << text << ">" );
                 
         // !cj! TODO - should get from out of CPIM message 
         Uri from = msg->header(h_From).uri();
         DebugLog ( << "got message from " << from );
                  
         assert( mCallback );
         mCallback->receivedPage( text, from, signedBy, sigStat, encrypted );
         return;
      }
 
      MultipartMixedContents* mixed = dynamic_cast<MultipartMixedContents*>(contents);
      if ( mixed )
      {
         InfoLog( << "Got a multipart mixed" );
       
         contents = NULL;
         
         MultipartMixedContents::Parts& parts = mixed->parts();
         for( MultipartMixedContents::Parts::const_iterator i = parts.begin(); 
              i != parts.end();  
              ++i)
         {
            Contents* c = *i;  
            assert( c );
            InfoLog ( << "mixed has a " << c->getType() );

            if ( c->getType() == Mime("text","plain") )
            {
               InfoLog ( << "mixed has sipfrag " << c->getType() );
      
               PlainContents* plainBody = dynamic_cast<PlainContents*>(c);
               if ( plainBody )
               {
                  assert( plainBody );
                  const Data& text = plainBody->text();
                  DebugLog ( << "got message from with text of <" << text << ">" );
                  
                  Uri from = msg->header(h_From).uri();
                  DebugLog ( << "got message from " << from );
                  
                  assert( mCallback );
                  mCallback->receivedPage( text, from, signedBy, sigStat, encrypted );
                  return;
               }

               // !cj! TODO - should deal with CPIM too 
            }
            
         }
         
         return;
      }
   
#if 1   // !cj! TODO remove 
      OctetContents* octets = dynamic_cast<OctetContents*>(contents);
      if (octets)
      {
         assert( contents );
         const Data& text = octets->getBodyData();
         DebugLog ( << "got message from with text of <" << text << ">" );
         
         Uri from = msg->header(h_From).uri();
         DebugLog ( << "got message from " << from );
                  
         assert( mCallback );
         mCallback->receivedPage( text, from, signedBy, sigStat, encrypted );
         return;
      }
#endif

      // deal with it if no one else has 
      {
         InfoLog ( << "Can not handle type " << contents->getType() );
         Uri from = msg->header(h_From).uri();
         mCallback->receivePageFailed( from );
         return;
      }
   }
}


void
TuIM::processResponse(SipMessage* msg)
{  
   assert( msg->exists(h_CallId));
   CallId id = msg->header(h_CallId);
   assert( id.value() != Data::Empty );

   processSipFrag( msg );
   
   // see if it is a registraition response 
   CallId regId = mRegistrationDialog.getCallId(); 

   Data v1 = id.value();
   Data v2 = regId.value();

   InfoLog( << "want id =" << id );

   if ( id == regId )
   {
      InfoLog ( << "matched the reg dialog" 
                 <<  mRegistrationDialog.getCallId() << " = " << id  );
      processRegisterResponse( msg );
      return;
   }
   
   // see if it is a subscribe response 
   for ( BuddyIterator i=mBuddies.begin(); i != mBuddies.end(); i++)
   {
      Buddy& buddy = *i;
      assert(  buddy.presDialog );
      InfoLog( << "check buddy id =" <<  buddy.presDialog->getCallId() );
      if ( buddy.presDialog->getCallId() == id  )
      {
         DebugLog ( << "matched the subscribe dialog" );
         processSubscribeResponse( msg, buddy );
         return;
      }
   }
   
   // see if it is a publish response
   for ( StateAgentIterator i=mStateAgents.begin(); i != mStateAgents.end(); i++)
   {
      assert( i->dialog );
      InfoLog( << "check publish id =" <<  i->dialog->getCallId() );
      if ( i->dialog->getCallId() == id  )
      {
         DebugLog ( << "matched the publish dialog" );
         processPublishResponse( msg, *i );
         return;
      }
   }
   
   // see if it is a notify response
   for ( SubscriberIterator i=mSubscribers.begin(); i != mSubscribers.end(); i++)
   {
      DeprecatedDialog* dialog = i->dialog;
      assert( dialog );
      InfoLog( << "check subscriber id =" <<  dialog->getCallId() );
      if ( dialog->getCallId() == id  )
      {
         DebugLog ( << "matched the notify dialog" );
         processNotifyResponse( msg, *dialog );
         return;
      }
   }
   
   // see if it is a page response
   for ( PageIterator i=mPages.begin(); i != mPages.end(); i++)
   {
      assert( i->dialog ); 
      InfoLog( << "check page id =" <<  i->dialog->getCallId() );
      if ( i->dialog->getCallId() == id  )
      {
         DebugLog ( << "matched the MESSAGE dialog" );
         processPageResponse( msg, *i );
         return;
      }
   }
  
   int number = msg->header(h_StatusLine).responseCode();
   InfoLog( << "got response that DID NOT MATCH of type " << number );
}


void 
TuIM::processRegisterResponse(SipMessage* msg)
{
   int number = msg->header(h_StatusLine).responseCode();
   Uri to = msg->header(h_To).uri();
   InfoLog ( << "register of " << to << " got response " << number );   
   unsigned int cSeq = msg->header(h_CSeq).sequence();

   if ( number<200 )
   {
      return;
   }

   if ( number >= 200 )
   { 
      mRegistrationDialog.createDialogAsUAC( *msg );
   }
   
   if ( ((number == 401) || (number == 407)) && (cSeq != mLastAuthCSeq) )
   {
      SipMessage* reg = mRegistrationDialog.makeRegister();
      
      const Data cnonce = Data::Empty;
      unsigned int nonceCount=0;
       
      Helper::addAuthorization(*reg,*msg,mAor.user(),mRegistrationPassword,cnonce,nonceCount);

      mLastAuthCSeq = reg->header(h_CSeq).sequence();

      reg->header(h_Expires).value() = mRegistrationTimeSeconds;
      reg->header(h_Contacts).front().param(p_expires) = mRegistrationTimeSeconds;
   
      mNextTimeToRegister = Timer::getRandomFutureTimeMs( mRegistrationTimeSeconds*1000 );

      InfoLog( << *reg );
      
      setOutbound( *reg );

      mStack->send( *reg );

      delete reg; reg=NULL;
      
      return;
   }
   
   if ( number >= 300 )
   {
      assert( mCallback );
      mCallback->registrationFailed( to, number );
      return;
   }
   
   if ( (number>=200) && (number<300) )
   {
      int expires = mRegistrationTimeSeconds;

      if ( msg->exists(h_Expires) )
      {
         expires = msg->header(h_Expires).value();
      }
      
      // loop throught the contacts, find me, and extract expire time
      resip::ParserContainer<resip::NameAddr>::iterator i =  msg->header(h_Contacts).begin();
      while ( i != msg->header(h_Contacts).end() )
      {
         try
         { 
            Uri uri = i->uri();

            if ( uri.getAor() == mContact.getAor() )
            {
               int e = i->param(p_expires);
               DebugLog(<< "match " << uri.getAor() << " e=" << e );

               expires = e;
            }
         }
         catch ( exception* )
         {
            InfoLog(<< "Bad contact in 2xx to register - skipped" );
         }
         
         i++;
      }

      if ( expires < 15 )
      {
         InfoLog(<< "Got very small expiers of " << expires );
         expires = 15;
      }

      mNextTimeToRegister = Timer::getRandomFutureTimeMs( expires*1000 );
      
      mCallback->registrationWorked( to );

      return;
   }
}


void 
TuIM::processNotifyResponse(SipMessage* msg, DeprecatedDialog& d )
{ 
   int number = msg->header(h_StatusLine).responseCode();
   DebugLog( << "got NOTIFY response of type " << number );
   
   if ( number >= 300 )
   {
      // TODO 
   }
}


void 
TuIM::processPublishResponse(SipMessage* msg, StateAgent& sa )
{ 
   int number = msg->header(h_StatusLine).responseCode();
   DebugLog( << "got PUBLISH response of type " << number );
   
   if ( number >= 300 )
   {
      // TODO
   }
}


void 
TuIM::processPageResponse(SipMessage* msg, Page& page )
{
   int number = msg->header(h_StatusLine).responseCode();
   DebugLog( << "got MESSAGE response of type " << number );
   
   if ( number >= 400 )
   {
      Uri dest = msg->header(h_To).uri();
      assert( mCallback );
      mCallback->sendPageFailed( dest,number );
   }

   if ( (number>=300) && (number<400) )
   {
      ParserContainer<NameAddr>::iterator dest =  msg->header(h_Contacts).begin();
      while ( dest != msg->header(h_Contacts).end() )
      {
         DebugLog(<< "Got a 3xx to" << *dest  );

         // send a message to redirected location
         Uri uri = dest->uri();
         sendPage( page.text, uri, page.sign, page.encryptFor );
         
         dest++;
      }
   }

   if (  (number>=200) && (number<300) )
   { 
      // got a final response for notify - can remove this dialog information
      CallId id = msg->header(h_CallId);

      for( PageIterator i=mPages.begin(); i != mPages.end(); i++ )

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -